OpenSDE Framework (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1268 lines
32 KiB

  1. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  2. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. #
  4. # Filename: lib/functions.in
  5. # Copyright (C) 2006 - 2008 The OpenSDE Project
  6. # Copyright (C) 2004 - 2006 The T2 SDE Project
  7. # Copyright (C) 1998 - 2003 Clifford Wolf
  8. #
  9. # More information can be found in the files COPYING and README.
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; version 2 of the License. A copy of the
  14. # GNU General Public License can be found in the file COPYING.
  15. # --- SDE-COPYRIGHT-NOTE-END ---
  16. . ${SDEROOT:-.}/lib/core-functions.in
  17. # This function returns a "uniqe id" as output
  18. #
  19. get_unique() {
  20. local hostsum=`hostname 2>/dev/null | tr '()' _`
  21. if [ -z "$hostsum" -a -x /sbin/ip ]; then
  22. hostsum=`/sbin/ip link show eth0 |
  23. sed -e '/link\//!d' -e 's,.*link[^ ]* \([^ ]*\) .*,\1,'`
  24. fi
  25. if [ -z "$hostsum" -a -f /sbin/ifconfig ]; then
  26. hostsum=`/sbin/ifconfig eth0 |
  27. sed -e '/HWaddr/!d' -e 's/.* HWaddr \([^ ]*\)/\1/'`
  28. fi
  29. if [ -z "$hostsum" -a -x /usr/bin/hostid ]; then
  30. hostsum=`/usr/bin/hostid`
  31. fi
  32. date "+%Y%m%d.%H%M%S.$$.$hostsum"
  33. }
  34. # this functions expands an string replacing % with all possible values
  35. # listed after
  36. case "$BASH_VERSION" in # damn workaround for different syntax in both versions
  37. 2*)
  38. get_expanded() {
  39. local string="$1"; shift
  40. while [ $# -gt 0 ]; do
  41. echo "${string//\\%/$1}"
  42. shift
  43. done
  44. }
  45. ;;
  46. 3*)
  47. get_expanded() {
  48. local string="$1"; shift
  49. while [ $# -gt 0 ]; do
  50. echo "${string//\%/$1}"
  51. shift
  52. done
  53. }
  54. ;;
  55. esac
  56. # atstage <stagename> ...
  57. # returns true if the build is on a stage related to the given name
  58. #
  59. atstage() {
  60. local x=
  61. for x; do
  62. case "$x" in
  63. toolchain)
  64. [ $stagelevel -gt 0 ] || return 0 ;;
  65. cross|crossbuild|crosscompile)
  66. [ $stagelevel -ne 1 ] || return 0 ;;
  67. rebuild)
  68. [ $stagelevel -ne 9 ] || return 0 ;;
  69. native)
  70. [ $stagelevel -lt 2 ] || return 0 ;;
  71. *)
  72. echo "atstage: '$x' stagename is not supported." >&2
  73. break ;;
  74. esac
  75. done
  76. return 1
  77. }
  78. # hasflag FLAG
  79. #
  80. hasflag() {
  81. local flag=
  82. for flag; do
  83. if ! echo "$desc_F" | grep -q -e "^\(.* \)\?$flag\( .*\)\?\$"; then
  84. return 1
  85. fi
  86. done
  87. return 0
  88. }
  89. # bz2filename [<filename>]
  90. # outputs filename converted to .bz2. stdin and $1 inputs are accepted
  91. #
  92. bz2filename() {
  93. local pattern='-e s,\.\(t\?\)\(gz\|Z\)$,.\1bz2,'
  94. pattern="-e s,\.gpg$,, $pattern"
  95. pattern="-e s,\.tar$,\.tar.bz2, $pattern"
  96. if [ -n "$1" ]; then
  97. echo "$1" | sed $pattern
  98. else
  99. sed $pattern
  100. fi
  101. }
  102. # Hook variables
  103. #
  104. unset hook_functions hook_fcounter
  105. declare -a hook_functions='()'
  106. hook_fcounter=0
  107. # This function adds a code fragment to a named hook with the named priority
  108. #
  109. # hook_add hook_name priority code
  110. #
  111. hook_add() {
  112. hook_functions[hook_fcounter]="$3" # declare -a hookidx_$1
  113. eval "hookidx_$1[\${#hookidx_$1[@]}]=\"$2 $hook_fcounter\""
  114. eval "(( hookdirty_$1++ ))" || true ; (( hook_fcounter++ )) || true
  115. }
  116. # This function executes all code fragments from the named hook
  117. #
  118. # hook_eval hook_name
  119. #
  120. hook_eval() {
  121. while read pri fnr ; do
  122. [ "$pri" ] && eval "${hook_functions[fnr]}"
  123. done < <( IFS=$'\n' ; eval "echo \"\${hookidx_$1[*]}\"" | sort )
  124. eval "unset hookdirty_$1"
  125. }
  126. # This function prints all hooks and their current contents
  127. #
  128. # hook_dump
  129. #
  130. hook_dump() {
  131. for hook in ${!hookidx_*} ; do
  132. hook=${hook#hookidx_}
  133. echo ; echo "Contents of hook $hook:"
  134. while read pri fnr ; do
  135. echo ; echo " $pri ($fnr)"
  136. echo "${hook_functions[fnr]}" | sed 's,^, ,'
  137. done < <( IFS=$'\n'
  138. eval "echo \"\${hookidx_$hook[*]}\"" | sort )
  139. if eval "[ -n \"\$hookdirty_\$hook\" ]"; then
  140. echo ; echo -n " Hook is marked as dirty: "
  141. eval "echo \"\${hookdirty_$hook}\""
  142. fi
  143. done
  144. echo
  145. }
  146. # This function can be used to duplicate a shell-function. E.g. when
  147. # overwriting a shell-function but the old one should stay available under
  148. # a new name:
  149. #
  150. # copy_function set_confopt set_confopt_foobar_old
  151. #
  152. # set_confopt() {
  153. # ....
  154. # set_confopt_foobar_old "$@"
  155. # ....
  156. # }
  157. #
  158. copy_function() {
  159. eval "$( declare -f $1 | sed "1 s,$1,$2," )"
  160. }
  161. # | column_clean |
  162. #
  163. # convert tabs to spaces, transform multiple consecutive spaces to one,
  164. # remove leading and trailing spaces
  165. column_clean() {
  166. tr '\t' ' ' | tr -s ' ' | sed -e 's,^[ ]*,,; s,[ ]*$,,;'
  167. }
  168. # | column_clean_tab |
  169. #
  170. # see column_clean, but with tabs
  171. column_clean_tab() {
  172. tr ' ' '\t' | tr -s '\t' | sed -e 's,^[\t]*,,; s,[\t]*$,,;'
  173. }
  174. # This function sets the 'confopt' and some other variables.
  175. # Re-run it in the package .conf file if you modify $prefix
  176. #
  177. set_confopt() {
  178. local x= z=
  179. prefix=${prefix#/}
  180. if atstage toolchain; then
  181. z="$root"
  182. fi
  183. confopt="--prefix=$z/$prefix"
  184. for x in bindir sbindir libdir datadir includedir \
  185. docdir infodir mandir sysconfdir localstatedir
  186. do
  187. # bindir=/usr/bin
  188. eval "$x=`pkggetdir $x`"
  189. # --bindir=$root\$bindir
  190. confopt="$confopt --$x=$z\$$x"
  191. done
  192. export LIBSUFF=${libdir##*/lib}
  193. if [ "$SDECFG_CONFIGURE_OPTS" ] ; then
  194. confopt="$confopt $SDECFG_CONFIGURE_OPTS"
  195. fi
  196. if [ "$SDECFG_DEBUG" = 0 ] ; then
  197. confopt="$confopt --disable-debug"
  198. fi
  199. if ! atstage native || [ "$SDECFG_DISABLE_NLS" = 1 ] ; then
  200. confopt="${confopt//--enable-nls/} --disable-nls"
  201. fi
  202. confopt="$confopt \$extraconfopt"
  203. if atstage toolchain; then
  204. confopt="$confopt --target=\$arch_target --host=\$arch_build"
  205. else
  206. confopt="$confopt --build=\$arch_build --host=\$arch_target"
  207. fi
  208. }
  209. #
  210. # eval_config_command $( eval echo $confopt )
  211. #
  212. function eval_config_command() {
  213. local config_command=
  214. local config_cache=$builddir/config.cache.$buildloop
  215. for x in /usr/share/automake/*
  216. do
  217. [ -x "$x" -a -f "$x" ] || continue
  218. x="$( basename "$x" )"
  219. if [ -L $x -a ! -e $x ] ; then
  220. echo "Fixing dead symlink $x."
  221. ln -sf /usr/share/automake/$x .
  222. fi
  223. done
  224. if [ -n "$configcache" ] || atstage cross; then
  225. if atstage cross; then
  226. create_config_cache >> $config_cache
  227. fi
  228. if [ -n "$configcache" ]; then
  229. cat <<-EOT
  230. # Some custom values
  231. EOT
  232. for x in $configcache; do
  233. echo "$x"
  234. done
  235. fi >> $config_cache
  236. grep -q '.--cache-file=' $configscript &&
  237. set -- "$@" "--cache-file=$config_cache"
  238. export cache_file=$config_cache
  239. fi
  240. config_command="$configprefix $configscript"
  241. sub_scripts="$( find $( dirname $configscript ) -name configure )"
  242. if [ "$cleanconfopt" = "0" ]; then
  243. config_command="$config_command $@"
  244. else
  245. # remove unsupported config script options
  246. for x ; do
  247. if grep -q "[[ ]${x%%=*}[]= ):]" $configscript $sub_scripts ; then
  248. config_command="$config_command $x"
  249. elif [[ $x = --*able-* ]] && egrep -q "\-\-(en|dis)able-\*" $configscript ||
  250. [[ $x = --with* ]] && egrep -q "\-\-with(|out)-\*" $configscript; then
  251. echo "Autodetection for option impossible: " \
  252. "$x passed thru."
  253. config_command="$config_command $x"
  254. else
  255. echo "Removing unsupported '$x' from" \
  256. "configure option string."
  257. fi
  258. done
  259. fi
  260. echo Running "$config_command"
  261. eval "$config_command"
  262. }
  263. # run 'make check' if Makefile supports it.
  264. #
  265. function run_check() {
  266. if grep -q -e "^check:" ./Makefile; then
  267. echo "Running make check ..."
  268. $MAKE check
  269. fi
  270. if grep -q -e "^test:" ./Makefile; then
  271. echo "Running make test ..."
  272. $MAKE test
  273. fi
  274. }
  275. # move the static libs from lib to usr/lib and correct the libdir used
  276. # inside the .la file
  277. #
  278. postflist_static_lib() {
  279. echo "Processing static lib corrections ..."
  280. egrep '^(lib|lib64)/.*\.(a\..*|a|la)$' $builddir/flist.txt |
  281. while read fn ; do
  282. [ -e $root/$fn -o -L $root/$fn ] || continue
  283. case "$fn" in
  284. *.la)
  285. sed "s,\([ =']\)/lib\(.*\),\1/usr/lib\2,g" \
  286. $root/$fn > $root/usr/$fn
  287. so=${fn%.la}.so
  288. ln -svf $root/$so $root/usr/$so
  289. add_flist $root/usr/$so
  290. rm $root/$fn
  291. ;;
  292. *)
  293. mv -fv $root/$fn $root/usr/$fn
  294. ;;
  295. esac
  296. add_flist $root/usr/$fn
  297. done
  298. # this check might be removed in the future when we decide this is not
  299. # an issue anymore ...
  300. echo "Verifing the .la files ..."
  301. # defect_la="`egrep '(lib|lib64)/.*\.la$' $builddir/flist.txt |
  302. # xargs egrep 'dependency_libs=.*-pthread.*' |
  303. # cut -d : -f1 | sort -u | tr '\n' ' '`"
  304. # if [ "$defect_la" ] ; then
  305. # abort "-pthread in: $defect_la!"
  306. # fi
  307. defect_la="`egrep '(lib|lib64)/.*\.la$' $builddir/flist.txt |
  308. xargs egrep "dependency_libs=.*(TOOLCHAIN|BACKUP|$SDECFG_ID).*" |
  309. cut -d : -f1 | sort -u | tr '\n' ' '`"
  310. if [ "$defect_la" ]; then
  311. local la
  312. echo_warning "Detected problems in following libtool files:"
  313. for la in $defect_la; do
  314. echo_warning " $la"
  315. done
  316. echo_warning "In absence of a proper fix (or replacement) for libtool and friends,"
  317. echo_warning "for now the .la files are automatically corrected."
  318. # Cleanup dependency_libs, remove build system path
  319. local dependency_libs
  320. local dlibsnew=
  321. local dlibtmp deplib
  322. local libsub=${libdir##*/}
  323. for la in $defect_la; do
  324. eval `grep ^dependency_libs= $root/$la`
  325. for deplib in $dependency_libs; do
  326. if [ $libsub != lib ]; then
  327. case "$deplib" in
  328. */lib|*/lib/*)
  329. deplib="`echo $deplib | sed "s,/lib$,/$libsub,g; s,/lib/,/$libsub/,g"`"
  330. ;;
  331. esac
  332. fi
  333. case "$deplib" in
  334. *TOOLCHAIN*|*BACKUP*|*$SDECFG_ID*) ;;
  335. *)
  336. dlibtmp=$dlibsnew ; var_remove dlibtmp ' ' "$deplib"
  337. [ "$dlibtmp" = "$dlibsnew" ] && var_append dlibsnew ' ' "$deplib"
  338. ;;
  339. esac
  340. done
  341. sed -i "s,^dependency_libs=.*,dependency_libs='$dlibsnew'," $root/$la
  342. dlibsnew=
  343. done
  344. fi
  345. }
  346. # Parse the *.desc file. Use the description from etc/desc_format and
  347. # save the tag data in $desc_*.
  348. #
  349. parse_desc() {
  350. tag="`grep '^\[' $base/etc/desc_format |
  351. sed 's, (\*),,; s,\] \[,|,g; s,\[,,; s,\],,;'`"
  352. for tag in $tag ; do
  353. tagdata="`egrep "^\[($tag)\]" $confdir/$1.desc |
  354. cut -f2- -d']' | sed 's,^ ,,'`"
  355. tag="`echo $tag | cut -f1 -d'|' | tr - _`"
  356. eval "desc_$tag=\"\$tagdata\""
  357. done
  358. ver="`echo "$desc_V" | tail -n 1 | cut -f1 -d' '`"
  359. extraver="`echo "$desc_V" | tail -n 1 | cut -s -f2- -d' '`"
  360. [ -z "$extraver" ] && extraver="${sdever//DEV-*/DEV}"
  361. }
  362. # This function is used for forcing a file to be in the flist
  363. #
  364. add_flist() {
  365. for addfile ; do
  366. echo "$addfile" | fl_wrparse -D -r "$xroot/" \
  367. >> $builddir/flist.txt
  368. done
  369. }
  370. # This function is used for forcing a package to be in the dependency list
  371. #
  372. add_dependency() {
  373. for addpackage ; do
  374. echo "$addpackage: add_dependency()" \
  375. >> $builddir/dependencies.debug
  376. done
  377. }
  378. # This function is used to subsitute some important variables
  379. # using a D_ prefix thru m4 ...
  380. rock_substitute() {
  381. sed -e s,D_prefix,$prefix,g -e s,D_sysconfdir,$sysconfdir,g \
  382. -e s,D_docdir,$docdir,g -e s,D_localstatedir,$localstatedir,g \
  383. -e s,D_datadir,$datadir,g -e s,D_infodir,$infodir,g \
  384. -e s,D_bindir,$bindir,g -e s,D_sbindir,$sbindir,g \
  385. -e s,D_libdir,$libdir,g -e s,D_mandir,$mandir,g \
  386. -e s,D_ver,$ver,g $1
  387. }
  388. # This outputs a predefined config.cache file as it needed by some
  389. # packages to cross-build correctly in stages 0 and 1.
  390. #
  391. create_config_cache() {
  392. cat $base/architecture/share/config.cache
  393. if [ $createarchcache -eq 1 ]; then
  394. cat <<-EOT
  395. # Architecture specific stuff
  396. #
  397. arch_sizeof_char=1
  398. ac_cv_sizeof_short=$arch_sizeof_short
  399. ac_cv_sizeof_int=$arch_sizeof_int
  400. ac_cv_sizeof_long=$arch_sizeof_long
  401. ac_cv_sizeof_long_long=$arch_sizeof_long_long
  402. ac_cv_sizeof_char_p=$arch_sizeof_char_p
  403. ac_cv_sizeof_void_p=$arch_sizeof_char_p
  404. ac_cv_c_bigendian=$arch_bigendian
  405. EOT
  406. fi
  407. }
  408. # Abort build before actual build starts
  409. # (is overwritten in Build-Pkg)
  410. #
  411. abort() {
  412. echo -e "The package build aborted with the following config" \
  413. "error:\n$*" > $root/var/adm/logs/$stagelevel-$xpkg.err
  414. echo_errorquote "`cat $root/var/adm/logs/$stagelevel-$xpkg.err`"
  415. echo_pkg_abort $stagelevel $repository $xpkg
  416. exit 1
  417. }
  418. # Dump Environment
  419. #
  420. dump_env() {
  421. # Dump $base - only set if there is not already an older value.
  422. #
  423. echo "base=\"\${base:-$base}\""
  424. # Dump all variables including their flags, but skip read-only
  425. # variables and $base.
  426. #
  427. # Substitute base directory with ${base}.
  428. #
  429. for name in ${!a*} ${!b*} ${!c*} ${!d*} ${!e*} ${!f*} ${!g*} ${!h*} \
  430. ${!i*} ${!j*} ${!k*} ${!l*} ${!m*} ${!n*} ${!o*} ${!p*} \
  431. ${!q*} ${!r*} ${!s*} ${!t*} ${!u*} ${!v*} ${!w*} ${!x*} \
  432. ${!y*} ${!z*} \
  433. ${!A*} ${!B*} ${!C*} ${!D*} ${!E*} ${!F*} ${!G*} ${!H*} \
  434. ${!I*} ${!J*} ${!K*} ${!L*} ${!M*} ${!N*} ${!O*} ${!P*} \
  435. ${!Q*} ${!R*} ${!S*} ${!T*} ${!U*} ${!V*} ${!W*} ${!X*} \
  436. ${!Y*} ${!Z*} ${!_*}
  437. do
  438. [ $name = base -o $name = PWD ] && continue
  439. if declare -p $name | head -n 1 | grep -qv '^declare -[a-z]*r'
  440. then
  441. declare -p $name | sed "s,\\(^\\|[\"=:]\\)$base\\([\"=:/]\\|\$\\),\\1\${base}\\2,g"
  442. fi
  443. done
  444. # Dump functions
  445. #
  446. declare -f | sed 's/^declare -f //; s/<<(/< <(/;'
  447. # Dump aliases
  448. #
  449. alias
  450. }
  451. # Returns the value of a field of the .desc file of a package
  452. #
  453. pkgdesc() {
  454. local pattern=
  455. local descfile=$( ls -1d $base/package/*/$2/$2.desc 2> /dev/null )
  456. case "$1" in
  457. ver) pattern="V" ;;
  458. *) pattern="$1" ;;
  459. esac
  460. if [ -s "$descfile" ]; then
  461. sed -n "s,^\[$pattern\][ \t]\+\(.\+\)[ \t]*$,\1,p" "$descfile"
  462. else
  463. echo_error "pkgdesc: Package $2 not found."
  464. fi
  465. }
  466. # Check if a package is already installed
  467. #
  468. # It does check the build-list if not in the rebuild stage - and
  469. # the really installed package data for rebuilds (and so manual builds).
  470. #
  471. # space delimited list, -f as first arguent for effective check
  472. #
  473. pkginstalled() {
  474. local effective=0
  475. if [ $# -eq 0 ]; then
  476. return 1 # nothing
  477. elif [ "$1" = "-f" ]; then
  478. effective=1; shift
  479. elif atstage rebuild; then
  480. effective=1
  481. fi
  482. if [ $effective -eq 0 ]; then
  483. local pattern=
  484. [ $# -gt 0 ] || return 1 # we expect at least one package
  485. if [ $# -eq 1 ]; then
  486. pattern="$1"
  487. else
  488. pattern="($@)"
  489. fi
  490. pattern="${pattern//+/\\+}"
  491. egrep -q "^X.* ${pattern// /|} " $base/config/$config/packages
  492. # we return what egrep returns
  493. else
  494. local pkg=
  495. # be happy if any package from the list is installed
  496. for pkg; do
  497. [ ! -f "$root/var/adm/packages/$pkg" ] || return 0
  498. done
  499. return 1
  500. fi
  501. }
  502. # pkgprefix [-t] [<type>] <package>
  503. # returns the prefix or the location of a 'type' of an already build package
  504. #
  505. pkgprefix() {
  506. local type= pkg=
  507. local dotest= abortmsg=
  508. local prefix= value=
  509. # -t : only see if it's possible to get the values
  510. if [ "$1" = "-t" ]; then
  511. dotest=1; shift
  512. fi
  513. if [ $# -eq 2 ]; then
  514. type="$1"; shift
  515. fi
  516. pkg="$1"
  517. # test usual error causes
  518. if [ -z "$pkg" ]; then
  519. abortmsg="you must specify a package"
  520. elif [ ! -f "$root/var/adm/packages/$pkg" ]; then
  521. abortmsg="package $pkg is not present"
  522. elif grep -q "^Prefix:" "$root/var/adm/packages/$pkg"; then
  523. prefix=$( grep "^Prefix: " "$root/var/adm/packages/$pkg" | cut -d' ' -f2- )
  524. else
  525. abortmsg=`echo "$pkg record is old, please rebuild the package $pkg. As an alternative, you can insert a line into the file $root/var/adm/packages/$pkg containing the text below:\n\nPrefix: <prefix>" | fmt -w 55 | sed 's,$,\\n,g'`
  526. fi
  527. if [ "$dotest" ]; then
  528. # test mode: abort or continue
  529. if [ "$abortmsg" ]; then
  530. abort "pkgprefix: $abortmsg"
  531. else
  532. return 0
  533. fi
  534. elif [ "$abortmsg" ]; then
  535. echo "pkgprefix: $abortmsg" 1>&2
  536. elif [ -z "$type" -o "$type" = "prefix" ]; then
  537. echo "$prefix"
  538. return 0
  539. elif [ "$type" = "ver" ]; then
  540. value=$( grep "^Package Name and Version:" "$root/var/adm/packages/$pkg" | cut -d' ' -f6 )
  541. else
  542. value=$( grep "^Location $type: " "$root/var/adm/packages/$pkg" | cut -d' ' -f3- )
  543. if [ -z "$value" ]; then
  544. # try default location for that $prefix
  545. value=$( xpkg="$pkg"; pkggetdir "$type" )
  546. fi
  547. fi
  548. echo "${value:-PKGPREFIX_ERROR}"
  549. }
  550. # pkggetdir <type> (needs $prefix and $xpkg)
  551. # returns the location for the file of a 'type' considering it's prefix
  552. #
  553. pkggetdir() {
  554. local xprefix=${prefix:+/$prefix}
  555. case "$1" in
  556. bindir) echo "$xprefix/bin" ;;
  557. sbindir) echo "$xprefix/sbin" ;;
  558. libdir)
  559. if [ "$SDECFG_MULTILIB" = 1 ]; then
  560. case $arch_machine in
  561. powerpc64|sparc64|x86_64|mips64)
  562. echo "$xprefix/lib64" ;;
  563. *)
  564. echo "$xprefix/lib" ;;
  565. esac
  566. else
  567. echo "$xprefix/lib"
  568. fi
  569. ;;
  570. datadir) echo "${xprefix:-/usr}/share" ;;
  571. infodir) echo "${xprefix:-/usr}/info" ;;
  572. mandir) echo "${xprefix:-/usr}/man" ;;
  573. docdir) echo "${xprefix:-/usr}/doc/$xpkg" ;;
  574. includedir) echo "${xprefix:-/usr}/include" ;;
  575. sysconfdir) echo "/etc${xprefix##/usr*}" ;;
  576. localstatedir) echo "/var${xprefix##/usr*}" ;;
  577. esac
  578. }
  579. # This function generates the T2 package checksum of $confdir.
  580. # The checksum includes the filenames and content (except of the .cache),
  581. # including subdirs but without whitespaces and comments and some tag lines
  582. # that are not vital for rebuilds during update checks.
  583. #
  584. # pkgchksum package-name | full-patch
  585. #
  586. pkgchksum() {
  587. (
  588. # expand to full patch if only a package name was specified
  589. [[ $1 == */* ]] || set $base/package/*/$1/
  590. cd $1
  591. # find all files (without hidden (e.g. .svn) files)
  592. find . ! -path '*/.*' ! -name '*.cache' -print -exec cat \{\} \; \
  593. 2>/dev/null |
  594. # strip some unimportant stuff (e.g. comments, whitespaces, ...)
  595. sed \
  596. -e '/^[ ]*#.*/d' \
  597. -e '/^\[COPY\]/d' \
  598. -e '/^\[CV-*\]/d' \
  599. -e '/^\[[T,I,U,A,M,L,S,C]\]/d' \
  600. -e 's/[\t ]*//g' \
  601. -e '/^ *$/d' |
  602. md5sum | cut -d ' ' -f 1
  603. )
  604. }
  605. # Create Package Database for gasgui install tool
  606. #
  607. create_package_db() {
  608. rm -f $3.tmp
  609. for file in $( echo $1/descs/* ) ; do
  610. [ -f "$file" ] || continue
  611. pkg="${file##*/}"
  612. # only include the package if a binary file is available
  613. if [ "$SDECFG_PKGFILE_VER" = 1 ] ; then
  614. v=-$(grep '^Package Name and Version' \
  615. $1/packages/$pkg | cut -f6 -d' ')
  616. else
  617. v=
  618. fi
  619. bfile=${pkg}${v}.$SDECFG_PKGFILE_TYPE
  620. if [ -e $2/$bfile ] ; then
  621. [ "$pkg" = TRANS.TBL ] && continue
  622. ( echo -e "$pkg"
  623. echo -e "\027"
  624. cat $1/descs/$pkg | grep -v '\[COPY\]'
  625. echo -e "\027"
  626. cat $1/dependencies/$pkg
  627. echo -e "\027"
  628. cat $1/cksums/$pkg
  629. echo -e "\027"
  630. echo -e "\004"
  631. ) >> $3.tmp
  632. else
  633. echo_error "Binary file for $bfile not present." \
  634. "Skipped in package database."
  635. fi
  636. done
  637. gzip -c $3.tmp > $3 ; rm -f $3.tmp
  638. }
  639. # Add files to the 'badfiles' list
  640. #
  641. register_badfiles() {
  642. local x desc="$1"
  643. shift
  644. for x in "$@"; do
  645. var_append badfiles $'\n' " $x\$"
  646. badfiles_desc[$badfiles_nr]=" $x\$"$'\n'"$desc"
  647. (( badfiles_nr++ ))
  648. done
  649. }
  650. # Detect the available patchfiles
  651. #
  652. detect_patchfiles()
  653. {
  654. local x= y=
  655. patchfiles="`ls $confdir/*.patch{,.$arch} \
  656. $confdir/*.patch_$xpkg{.$arch} \
  657. 2> /dev/null | tr '\n' ' '`"
  658. for x in $( get_reverted $targetchain ); do
  659. for y in pkg_$pkg.patch{,.$arch} xpkg_$xpkg.patch{,.$arch}; do
  660. if [ -f $base/target/$x/$y ]; then
  661. patchfiles="$patchfiles $base/target/$x/$y"
  662. fi
  663. done
  664. done
  665. }
  666. # Apply the given $patchfiles
  667. # [ hook called for each patch ] [ filter script ]
  668. #
  669. apply_patchfiles() {
  670. local hook="$1"
  671. local filter="$2"
  672. [ "$filter" ] || filter=cat
  673. for x in $patchfiles; do
  674. # correct the abolute path - e.g for patchfiles supplied
  675. # in the .desc file
  676. # we assume relative path patches are mirrorables //mnemoc
  677. if [ ! -e "$x" -a -n "${x##*/*}" ] ; then
  678. x="$base/download/mirror/${x:0:1}/$x"
  679. fi
  680. # Apply the patch if the file does exist or issue a warning
  681. if [ -f "$x" ] ; then
  682. echo "Apply patch $x ..."
  683. if [[ $x = *.bz2 ]] ; then
  684. patch_file=`mktemp` ; patch_del=1
  685. bzcat $x > $patch_file
  686. else
  687. patch_file=$x ; patch_del=0
  688. fi
  689. $filter $patch_file | patch $patchopt
  690. [ $patch_del = 1 ] && rm $patch_file
  691. eval "$hook"
  692. else
  693. echo_warning "Unable to apply patch: $x (File not found!)"
  694. fi
  695. done
  696. }
  697. # -------------------------------------------------------------------
  698. # The automatic extraction of archive (prior to building) supports
  699. # multiple archive types. For every archive type we have a separate
  700. # func that knows how to extract the archive. However, every func
  701. # should deliver two file: untar.txt and xsrcdir.txt.
  702. #
  703. # untar.txt needs to contain all extracted files.
  704. # xsrcdir.txt need to contain the top level extraction directories.
  705. # -------------------------------------------------------------------
  706. autoextract_tar_bz2() {
  707. echo "Extracting $xsrctar ($taropt) ... "
  708. tar -v $taropt $1 > untar.txt
  709. }
  710. autoextract_zip() {
  711. echo "Extracting $xsrctar ($zipopt) ... "
  712. unzip $zipopt $1 | sed 's,^.*/$,,' |
  713. cut -f4 -d" " | grep -v "^$" > untar.txt
  714. }
  715. # Main program for building a package
  716. #
  717. build_this_package() {
  718. if [ ".$desc_SRC" = "." ] ; then
  719. # Autodetect source tar and extract it
  720. #
  721. if [ "$srctar" = auto ] ; then
  722. xsourceballs=$( echo "$desc_D" | head -n 1 | tr ' ' '\t' | tr -s '\t' |
  723. cut -f2 | bz2filename )
  724. if [ -z "$xsourceballs" ] ; then
  725. echo "Can't auto-detect srctar for package '$xpkg'!"
  726. false
  727. fi
  728. else
  729. xsourceballs="$srctar"
  730. fi
  731. elif [ "$srctar" = auto ] ; then
  732. sourceballs=$( echo "$desc_D" | tr ' ' '\t' | tr -s '\t' |
  733. cut -f2 | bz2filename )
  734. xsrcpattern=$( echo "$desc_SRC" | tr ' ' '\t' | tr -s '\t' | tr '\t' '\n' )
  735. xsourceballs=$( echo "$sourceballs" | grep -F "$xsrcpattern" )
  736. else
  737. xsourceballs="$srctar"
  738. fi
  739. for xsrctar in $xsourceballs; do
  740. saved_patchfiles="$patchfiles"
  741. buildloop=1
  742. var_append patchfiles " " \
  743. "`ls $confdir/*.patch.${xsrctar/-[v0-9]*/} 2> /dev/null`"
  744. if [ "$xsrctar" != none -a "$autoextract" = 1 ]; then
  745. cd $builddir
  746. if [ -z "$custextract" ]; then
  747. # No custom extraction, so determine what
  748. # autoextraction to use.
  749. case "$xsrctar" in
  750. *.zip) custextract="autoextract_zip" ;;
  751. *) custextract="autoextract_tar_bz2" ;; # *.tar.bz2|*.tbz2|*.tbz
  752. esac
  753. fi
  754. if [ -n "$custextract" ]; then
  755. # Do the actual extraction of the archive.
  756. eval "$custextract $archdir/$xsrctar"
  757. cat untar.txt |
  758. sed 's,^\./,,' | cut -f1 -d/ |
  759. sort -u > xsrcdir.txt
  760. fi
  761. #
  762. if [ "$srcdir" = auto ]; then
  763. xsrcdir=${xsrctar%.tar.bz2}
  764. xsrcdir=${xsrcdir%.tbz2}
  765. xsrcdir=${xsrcdir%.tbz}
  766. if [ ! -d $xsrcdir ] ; then
  767. for x in $pkg-$ver ${pkg}_$ver $pkg \
  768. $xpkg-$ver ${xpkg}_$ver $xpkg \
  769. "$( cat xsrcdir.txt )"
  770. do
  771. [ -d "$x" ] && xsrcdir="$x"
  772. done
  773. fi
  774. else
  775. xsrcdir="$srcdir"
  776. fi
  777. #
  778. if [ "$chownsrcdir" = 1 ]; then
  779. echo "Fixing ownership and permissions ..."
  780. chown -R 0:0 $builddir/$xsrcdir
  781. fi
  782. #
  783. if [ "$nocvsinsrcdir" = 1 ]; then
  784. echo "Removing CVS, .svn, .git, {arch} and .arch-ids directories ..."
  785. egrep '(^|/)(CVS|\.svn|\.git|\{arch\}|\.arch-ids)(/|$)' untar.txt |
  786. while read x; do
  787. echo "Removing $x ..."
  788. rm -rf "$x"
  789. done
  790. fi
  791. #
  792. echo "Changeing into $builddir/$xsrcdir ..."
  793. cd $builddir/$xsrcdir
  794. # Apply patches
  795. #
  796. if [ $autopatch = 1 ]; then
  797. hook_eval prepatch
  798. apply_patchfiles
  799. hook_eval postpatch
  800. fi
  801. else
  802. cd $builddir
  803. fi
  804. if [ "$createprefix" = 1 ]; then
  805. echo "Creating $root/$prefix/<..> if required ..."
  806. for x in $foodirlist; do
  807. eval "x=\"$root\$$x\""
  808. if [ ! -e $x ]; then
  809. mkdir -p $x
  810. rmemptydir="$rmemptydir $x"
  811. fi
  812. done
  813. fi
  814. if [ -z "$custmain" ]; then
  815. while [ ${buildloop:-1} -le ${buildloops:-1} ]; do
  816. [ "${buildloops:-1}" = "1" ] || echo "loop ${buildloop:-1} of ${buildloops:-1} for $xsrctar."
  817. hook_eval preconf
  818. # Maybe generate a configure first
  819. #
  820. if [ $autogen -eq 1 -o \
  821. \( -f autogen.sh -a ! -f configure \) ] ; then
  822. if [ -f autogen.sh ] ; then
  823. echo "Running package autogen script."
  824. sed -i '/^\.\/configure /d' autogen.sh
  825. sh autogen.sh
  826. else
  827. echo "Running builtin autogen script."
  828. libtoolize --force --automake ; aclocal
  829. if grep AM_INIT_AUTOMAKE \
  830. configure.[ia][nc]
  831. then automake ; fi
  832. autoconf
  833. fi
  834. fi
  835. # Run configure scripts etc.
  836. #
  837. if [ $runconf = 1 ]; then
  838. if [ -n "$( type -p $configscript )" -o $autogen = 1 ]
  839. then
  840. eval_config_command $( eval echo $confopt )
  841. fi
  842. fi
  843. # automated package build
  844. # styles without make run first:
  845. if [ -f setup.py -a $runpysetup = 1 ] ; then
  846. pyconfopt="${pyconfopt:=--prefix $root/$prefix}"
  847. hook_eval premake
  848. eval ${pyscript:-python} setup.py build install $pyconfopt
  849. hook_eval postmake
  850. else # styles that include a make run
  851. if [ ! -f Makefile -a ! -f makefile -a \
  852. -f Makefile.PL -a $runmkpl = 1 ]; then
  853. perl Makefile.PL ${plconfopt:-INSTALLDIRS=perl}
  854. fi
  855. #
  856. if [ ! -f Makefile -a ! -f makefile -a \
  857. -f Imakefile -a $runxmkmf = 1 ]; then
  858. xmkmf -a
  859. fi
  860. #
  861. # Build it
  862. #
  863. hook_eval premake
  864. if [ "$makeopt" ]; then
  865. eval echo "Running $MAKE $makeopt"
  866. eval "$MAKE $makeopt"
  867. fi
  868. hook_eval inmake
  869. if [ "$makeinstopt" ]; then
  870. eval echo "Running $MAKE $makeinstopt"
  871. eval "$MAKE $makeinstopt"
  872. fi
  873. hook_eval postmake
  874. fi
  875. buildloop=$( expr ${buildloop:-1} + 1 )
  876. done
  877. else
  878. eval "$custmain"
  879. for x in preconf premake inmake postmake; do
  880. if eval "[ -n \"\$hookdirty_$x\" ]"; then
  881. echo "Hook $x is still marked as dirty ..."
  882. hook_eval $x
  883. fi
  884. done
  885. fi
  886. if [ "$createdocs" != 0 ]; then
  887. if [ ! -e $root$docdir ]; then
  888. mkdir -p $docdir
  889. rmemptydir="$rmemptydir $root$docdir"
  890. fi
  891. [ -z "$createdocs" ] && createdocs="$SDECFG_CREATE_DOCS"
  892. fi
  893. if [ "$createdocs" = 1 ]; then
  894. echo "Trying to copy the default documentation ..."
  895. for x in [A-Z][A-Z]* *.lsm ChangeLog* README LICENSE COPYING; do
  896. [ "${x#*.[cho0-9]}" ] || continue
  897. [ "${x#*.info*}" ] || continue
  898. [ "${x#*.TAR*}" ] || continue
  899. [ "${x#*akefile*}" ] || continue
  900. [ -f $x ] && cp -v $x $root$docdir/$x
  901. done
  902. echo "Trying to copy even more documentation ..."
  903. [ -d $builddir/$xsrcdir ] && cd $builddir/$xsrcdir
  904. for x in `find -type d \( -name 'doc' -o -name 'docs' \
  905. -o -name '[Dd]ocumentation' \) ! -empty`
  906. do
  907. if [ -d "$x" -a "`echo $x/*`" != "$x/*" ]
  908. then cp -rLv $x/* $root$docdir || true ; fi
  909. done
  910. for x in $confdir/*.doc; do
  911. if [ -f $x ]
  912. then cp -v $x $root$docdir/${x%.doc}; fi
  913. done
  914. find $root$docdir/ -name '*.[0-9]' -o -name '*.info*' \
  915. -o -name '[Mm]akefile*' |
  916. xargs -r rm -f 2> /dev/null || true
  917. find $root$docdir/* -type d -empty 2> /dev/null |
  918. xargs -r rmdir 2> /dev/null || true
  919. fi
  920. hook_eval postdoc
  921. if atstage native && [ -f /sbin/ldconfig ] ; then
  922. echo "Running ldconfig ..."
  923. ldconfig
  924. fi
  925. patchfiles="$saved_patchfiles"
  926. done
  927. if [ "$rmemptydir" ]; then
  928. rmdir $rmemptydir 2> /dev/null || true
  929. fi
  930. return 0
  931. }
  932. # source_file cksum file url
  933. #
  934. # Create the file path from 'file' and 'url'.
  935. # cksum and url are ignored
  936. # ([D] tag compatible format)
  937. #
  938. source_file() {
  939. local pre= file="$2" url="$3" mirror="mirror"
  940. # '-' as $url prefix means, nomirrorable
  941. [ "${url:0:1}" = "-" ] && mirror="local"
  942. # inside Build-Pkg $archdir is set
  943. if [ -n "$archdir" ]; then
  944. pre=$base/; file="$( bz2filename $file )"
  945. fi
  946. echo ${pre}download/${mirror}/${file:0:1}/$file
  947. }
  948. # match_source_file [-p] pattern [[package] ...]
  949. #
  950. # returns path and name of a downloaded file from a list of packages, matching a grep pattern
  951. # without -p it only returns it's name, not the path.
  952. #
  953. match_source_file() {
  954. local pattern= package= showpath=0
  955. local x= file= url= mirror=
  956. local found=1
  957. if [ "$1" = "-p" ]; then
  958. showpath=1; shift
  959. fi
  960. pattern="$1"; shift
  961. for package in ${*:-$pkg}; do
  962. while read x x file url x; do
  963. file="$( bz2filename $file )"
  964. found=0
  965. if [ $showpath -eq 0 ]; then
  966. echo $file
  967. else
  968. [ "${url:0:1}" = "-" ] && mirror="local" || mirror="mirror"
  969. echo $base/download/${mirror}/${file:0:1}/$file
  970. fi
  971. done < <( grep -e "^\[D\].*$pattern" $base/package/*/$package/$package.desc )
  972. done
  973. return $found
  974. }
  975. # create the virtual $archdir symlinks
  976. #
  977. populate_archdir()
  978. {
  979. local x= missing=0
  980. for x in `match_source_file -p .`; do
  981. if [ ! -f $x ]; then
  982. echo_warning "File not found: ${x#$base/}"
  983. missing=1
  984. elif [ ! -e "$builddir/archdir/${x##*/}" ]; then
  985. ln -vs $x $builddir/archdir/
  986. fi
  987. done
  988. if [ $missing -eq 1 ]; then
  989. echo_warning "Did you run download for this package?"
  990. false
  991. fi
  992. }
  993. # search for the package confdir
  994. #
  995. detect_confdir()
  996. {
  997. confdir=
  998. if [ -z "$pkgdir" ] ; then
  999. for x in package/*/$pkg/$pkg.desc ; do
  1000. if [ -f "$x" ] ; then
  1001. if [ "$confdir" ] ; then
  1002. echo_pkg_deny $stagelevel $pkg "in multiple trees"
  1003. echo "Package in multiple trees: $pkg !" \
  1004. > $root/var/adm/logs/$stagelevel-$xpkg.err
  1005. exit 1
  1006. fi
  1007. x=${x#package/}; x=${x%%/*}
  1008. confdir="$base/package/$x/$pkg"
  1009. repository=$x
  1010. fi
  1011. done
  1012. else
  1013. if [ -f "$pkgdir/$pkg.desc" ] ; then
  1014. confdir="$pkgdir"
  1015. repository=extern
  1016. fi
  1017. fi
  1018. }
  1019. # initialize standard vars and hooks
  1020. #
  1021. init_vars_and_hooks()
  1022. {
  1023. makeopt='CC="$CC" CPP="$CPP" CXX="$CXX"'
  1024. if atstage toolchain; then
  1025. makeopt="$makeopt"' prefix="$root/$prefix"'
  1026. else
  1027. makeopt="$makeopt"' prefix="/$prefix"'
  1028. fi
  1029. if ! atstage native; then
  1030. makeopt="$makeopt"' CC_FOR_BUILD="$BUILDCC"'
  1031. makeopt="$makeopt"' BUILDCC="$BUILDCC" BUILD_CC="$BUILD_CC"'
  1032. makeopt="$makeopt"' HOSTCC="$HOSTCC" HOST_CC="$HOST_CC"'
  1033. makeopt="$makeopt"' STRIP="$STRIP" AR="$AR" LD="$LD"'
  1034. makeopt="$makeopt"' RANLIB="$RANLIB" NM="$NM"'
  1035. fi
  1036. if atstage native; then
  1037. flistdel="$flistdel|`echo $base | sed s,^/,,`/.*"
  1038. fi
  1039. if atstage cross; then
  1040. makeopt="$makeopt"' INSTALL_PREFIX="$root"'
  1041. makeopt="$makeopt"' DESTDIR="$root" DEST_DIR="$root"'
  1042. makeopt="$makeopt"' INSTROOT="$root" INSTALLROOT="$root"'
  1043. fi
  1044. makeinstopt="$makeopt"' install'
  1045. custmain=
  1046. buildloop=1 buildloops=1
  1047. [ "$SDECFG_DO_CHECK" = 1 ] && hook_add inmake 6 'run_check'
  1048. hook_add postflist 3 'postflist_static_lib'
  1049. createarchcache=0
  1050. configprefix=; configcache= ; autogen=0
  1051. configscript="./configure" ; extraconfopt=
  1052. srcdir=auto ; srctar=auto
  1053. taropt="--use-compress-program=bzip2 -xf"
  1054. mainfunction="build_this_package"
  1055. runconf=1 ; runxmkmf=1 ; runmkpl=1 ; runpysetup=1 ; autopatch=1
  1056. autoextract=1 ; chownsrcdir=1 ; nocvsinsrcdir=1 ; cleanconfopt=1
  1057. patchopt="-bfp1 -z .orig"
  1058. createprefix=1 ; createdocs= ; rmemptydir=
  1059. check_shared=1
  1060. check_usrlocal=1
  1061. check_badfiles=1
  1062. badfiles= badfiles_nr=0
  1063. declare -a badfiles_desc
  1064. }
  1065. # this is a 2nd lightweight and modular "build this package" implementation
  1066. # currently only used for the postlinux stuff - later maybe for more -ReneR
  1067. #
  1068. build_package()
  1069. {
  1070. logstamp=$PWD/log
  1071. (
  1072. set -e
  1073. pushd $base
  1074. super=$pkg
  1075. pkg="$1" ; xpkg="$pkg"
  1076. conffile="$2" ; [ "$conffile" ] || conffile="$pkg.conf"
  1077. unset ${!hook*}
  1078. declare -a hook_functions='()'
  1079. hook_fcounter=0
  1080. init_vars_and_hooks
  1081. detect_confdir
  1082. detect_patchfiles
  1083. parse_desc $pkg
  1084. # Erase positional parameters to prevent unintended parameter
  1085. # passing. We do not want to pass the current positional parameters
  1086. # to the loaded script.
  1087. set --
  1088. eval "$desc_O"
  1089. echo_status "Building $xpkg ($ver) within $super (using $conffile)."
  1090. for x in $( get_expanded $base/target/%/pkg_$pkg.conf $targetchain ) \
  1091. $confdir/$conffile; do
  1092. if [ -f $x ]; then
  1093. if [[ $x == */$conffile ]]; then
  1094. echo "Reading package configuration ($conffile) from package directory."
  1095. else
  1096. echo "Reading package configuration from target directory."
  1097. fi
  1098. . $x
  1099. break
  1100. fi
  1101. done
  1102. # short path - to not abort on missing downloads of postlinux.conf
  1103. # packages that are not built anyway -ReneR
  1104. if [ "$custmain" = "true" ] ; then
  1105. echo "Nothing is going to be done ayway - returning quickly."
  1106. return
  1107. fi
  1108. populate_archdir
  1109. popd
  1110. # dump for debugging
  1111. hook_dump > $builddir/debug.hooks.$pkg
  1112. dump_env > $builddir/debug.buildenv.$pkg
  1113. echo "Running main build function '$mainfunction' ..."
  1114. cd $builddir
  1115. eval "$mainfunction"
  1116. touch $logstamp
  1117. )
  1118. [ -f $logstamp ] || return 1
  1119. rm $logstamp
  1120. return 0
  1121. }