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.

1266 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 - 2007 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/scripts/config.cache
  393. if [ $createarchcache -eq 1 ]; then
  394. cat <<-EOT
  395. # Architecture specific stuff\n"
  396. arch_sizeof_char=1
  397. ac_cv_sizeof_short=$arch_sizeof_short
  398. ac_cv_sizeof_int=$arch_sizeof_int
  399. ac_cv_sizeof_long=$arch_sizeof_long
  400. ac_cv_sizeof_long_long=$arch_sizeof_long_long
  401. ac_cv_sizeof_char_p=$arch_sizeof_char_p
  402. ac_cv_sizeof_void_p=$arch_sizeof_char_p
  403. ac_cv_c_bigendian=$arch_bigendian
  404. EOT
  405. fi
  406. }
  407. # Abort build before actual build starts
  408. # (is overwritten in Build-Pkg)
  409. #
  410. abort() {
  411. echo -e "The package build aborted with the following config" \
  412. "error:\n$*" > $root/var/adm/logs/$stagelevel-$xpkg.err
  413. echo_errorquote "`cat $root/var/adm/logs/$stagelevel-$xpkg.err`"
  414. echo_pkg_abort $stagelevel $repository $xpkg
  415. exit 1
  416. }
  417. # Dump Environment
  418. #
  419. dump_env() {
  420. # Dump $base - only set if there is not already an older value.
  421. #
  422. echo "base=\"\${base:-$base}\""
  423. # Dump all variables including their flags, but skip read-only
  424. # variables and $base.
  425. #
  426. # Substitute base directory with ${base}.
  427. #
  428. for name in ${!a*} ${!b*} ${!c*} ${!d*} ${!e*} ${!f*} ${!g*} ${!h*} \
  429. ${!i*} ${!j*} ${!k*} ${!l*} ${!m*} ${!n*} ${!o*} ${!p*} \
  430. ${!q*} ${!r*} ${!s*} ${!t*} ${!u*} ${!v*} ${!w*} ${!x*} \
  431. ${!y*} ${!z*} \
  432. ${!A*} ${!B*} ${!C*} ${!D*} ${!E*} ${!F*} ${!G*} ${!H*} \
  433. ${!I*} ${!J*} ${!K*} ${!L*} ${!M*} ${!N*} ${!O*} ${!P*} \
  434. ${!Q*} ${!R*} ${!S*} ${!T*} ${!U*} ${!V*} ${!W*} ${!X*} \
  435. ${!Y*} ${!Z*} ${!_*}
  436. do
  437. [ $name = base -o $name = PWD ] && continue
  438. if declare -p $name | head -n 1 | grep -qv '^declare -[a-z]*r'
  439. then
  440. declare -p $name | sed "s,\\(^\\|[\"=:]\\)$base\\([\"=:/]\\|\$\\),\\1\${base}\\2,g"
  441. fi
  442. done
  443. # Dump functions
  444. #
  445. declare -f | sed 's/^declare -f //; s/<<(/< <(/;'
  446. # Dump aliases
  447. #
  448. alias
  449. }
  450. # Returns the value of a field of the .desc file of a package
  451. #
  452. pkgdesc() {
  453. local pattern=
  454. local descfile=$( ls -1d $base/package/*/$2/$2.desc 2> /dev/null )
  455. case "$1" in
  456. ver) pattern="V" ;;
  457. *) pattern="$1" ;;
  458. esac
  459. if [ -s "$descfile" ]; then
  460. sed -n "s,^\[$pattern\][ \t]\+\(.\+\)[ \t]*$,\1,p" "$descfile"
  461. else
  462. echo_error "pkgdesc: Package $2 not found."
  463. fi
  464. }
  465. # Check if a package is already installed
  466. #
  467. # It does check the build-list if not in the rebuild stage - and
  468. # the really installed package data for rebuilds (and so manual builds).
  469. #
  470. # space delimited list, -f as first arguent for effective check
  471. #
  472. pkginstalled() {
  473. local effective=0
  474. if [ $# -eq 0 ]; then
  475. return 1 # nothing
  476. elif [ "$1" == "-f" ]; then
  477. effective=1; shift
  478. elif atstage rebuild; then
  479. effective=1
  480. fi
  481. if [ $effective -eq 0 ]; then
  482. local pattern=
  483. [ $# -gt 0 ] || return 1 # we expect at least one package
  484. if [ $# -eq 1 ]; then
  485. pattern="$1"
  486. else
  487. pattern="($@)"
  488. fi
  489. pattern="${pattern//+/\\+}"
  490. egrep -q "^X.* ${pattern// /|} " $base/config/$config/packages
  491. # we return what egrep returns
  492. else
  493. local pkg=
  494. # be happy if any package from the list is installed
  495. for pkg; do
  496. [ ! -f "$root/var/adm/packages/$pkg" ] || return 0
  497. done
  498. return 1
  499. fi
  500. }
  501. # pkgprefix [-t] [<type>] <package>
  502. # returns the prefix or the location of a 'type' of an already build package
  503. #
  504. pkgprefix() {
  505. local type= pkg=
  506. local dotest= abortmsg=
  507. local prefix= value=
  508. # -t : only see if it's possible to get the values
  509. if [ "$1" = "-t" ]; then
  510. dotest=1; shift
  511. fi
  512. if [ $# -eq 2 ]; then
  513. type="$1"; shift
  514. fi
  515. pkg="$1"
  516. # test usual error causes
  517. if [ -z "$pkg" ]; then
  518. abortmsg="you must specify a package"
  519. elif [ ! -f "$root/var/adm/packages/$pkg" ]; then
  520. abortmsg="package $pkg is not present"
  521. elif grep -q "^Prefix:" "$root/var/adm/packages/$pkg"; then
  522. prefix=$( grep "^Prefix: " "$root/var/adm/packages/$pkg" | cut -d' ' -f2- )
  523. else
  524. 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'`
  525. fi
  526. if [ "$dotest" ]; then
  527. # test mode: abort or continue
  528. if [ "$abortmsg" ]; then
  529. abort "pkgprefix: $abortmsg"
  530. else
  531. return 0
  532. fi
  533. elif [ "$abortmsg" ]; then
  534. echo "pkgprefix: $abortmsg" 1>&2
  535. elif [ -z "$type" -o "$type" = "prefix" ]; then
  536. echo "$prefix"
  537. return 0
  538. elif [ "$type" = "ver" ]; then
  539. value=$( grep "^Package Name and Version:" "$root/var/adm/packages/$pkg" | cut -d' ' -f6 )
  540. else
  541. value=$( grep "^Location $type: " "$root/var/adm/packages/$pkg" | cut -d' ' -f3- )
  542. if [ -z "$value" ]; then
  543. # try default location for that $prefix
  544. value=$( xpkg="$pkg"; pkggetdir "$type" )
  545. fi
  546. fi
  547. echo "${value:-PKGPREFIX_ERROR}"
  548. }
  549. # pkggetdir <type> (needs $prefix and $xpkg)
  550. # returns the location for the file of a 'type' considering it's prefix
  551. #
  552. pkggetdir() {
  553. local xprefix=${prefix:+/$prefix}
  554. case "$1" in
  555. bindir) echo "$xprefix/bin" ;;
  556. sbindir) echo "$xprefix/sbin" ;;
  557. libdir)
  558. if [ "$SDECFG_MULTILIB" = 1 ]; then
  559. case $arch_machine in
  560. powerpc64|sparc64|x86_64|mips64)
  561. echo "$xprefix/lib64" ;;
  562. *)
  563. echo "$xprefix/lib" ;;
  564. esac
  565. else
  566. echo "$xprefix/lib"
  567. fi
  568. ;;
  569. datadir) echo "${xprefix:-/usr}/share" ;;
  570. infodir) echo "${xprefix:-/usr}/info" ;;
  571. mandir) echo "${xprefix:-/usr}/man" ;;
  572. docdir) echo "${xprefix:-/usr}/doc/$xpkg" ;;
  573. includedir) echo "${xprefix:-/usr}/include" ;;
  574. sysconfdir) echo "/etc${xprefix##/usr*}" ;;
  575. localstatedir) echo "/var${xprefix##/usr*}" ;;
  576. esac
  577. }
  578. # This function generates the T2 package checksum of $confdir.
  579. # The checksum includes the filenames and content (except of the .cache),
  580. # including subdirs but without whitespaces and comments and some tag lines
  581. # that are not vital for rebuilds during update checks.
  582. #
  583. # pkgchksum package-name | full-patch
  584. #
  585. pkgchksum() {
  586. (
  587. # expand to full patch if only a package name was specified
  588. [[ $1 == */* ]] || set $base/package/*/$1/
  589. cd $1
  590. # find all files (without hidden (e.g. .svn) files)
  591. find . ! -path '*/.*' ! -name '*.cache' -print -exec cat \{\} \; \
  592. 2>/dev/null |
  593. # strip some unimportant stuff (e.g. comments, whitespaces, ...)
  594. sed \
  595. -e '/^[ ]*#.*/d' \
  596. -e '/^\[COPY\]/d' \
  597. -e '/^\[CV-*\]/d' \
  598. -e '/^\[[T,I,U,A,M,L,S,C]\]/d' \
  599. -e 's/[\t ]*//g' \
  600. -e '/^ *$/d' |
  601. md5sum | cut -d ' ' -f 1
  602. )
  603. }
  604. # Create Package Database for gasgui install tool
  605. #
  606. create_package_db() {
  607. rm -f $3.tmp
  608. for file in $( echo $1/descs/* ) ; do
  609. [ -f "$file" ] || continue
  610. pkg="${file##*/}"
  611. # only include the package if a binary file is available
  612. if [ "$SDECFG_PKGFILE_VER" = 1 ] ; then
  613. v=-$(grep '^Package Name and Version' \
  614. $1/packages/$pkg | cut -f6 -d' ')
  615. else
  616. v=""
  617. fi
  618. bfile=${pkg}${v}.$SDECFG_PKGFILE_TYPE
  619. if [ -e $2/$bfile ] ; then
  620. [ "$pkg" = TRANS.TBL ] && continue
  621. ( echo -e "$pkg"
  622. echo -e "\027"
  623. cat $1/descs/$pkg | grep -v '\[COPY\]'
  624. echo -e "\027"
  625. cat $1/dependencies/$pkg
  626. echo -e "\027"
  627. cat $1/cksums/$pkg
  628. echo -e "\027"
  629. echo -e "\004"
  630. ) >> $3.tmp
  631. else
  632. echo_error "Binary file for $bfile not present." \
  633. "Skipped in package database."
  634. fi
  635. done
  636. gzip -c $3.tmp > $3 ; rm -f $3.tmp
  637. }
  638. # Add files to the 'badfiles' list
  639. #
  640. register_badfiles() {
  641. local x desc="$1"
  642. shift
  643. for x in "$@"; do
  644. var_append badfiles $'\n' " $x\$"
  645. badfiles_desc[$badfiles_nr]=" $x\$"$'\n'"$desc"
  646. (( badfiles_nr++ ))
  647. done
  648. }
  649. # Detect the available patchfiles
  650. #
  651. detect_patchfiles()
  652. {
  653. local x= y=
  654. patchfiles="`ls $confdir/*.patch{,.$arch} \
  655. $confdir/*.patch_$xpkg{.$arch} \
  656. 2> /dev/null | tr '\n' ' '`"
  657. for x in $( get_reverted $targetchain ); do
  658. for y in pkg_$pkg.patch{,.$arch} xpkg_$xpkg.patch{,.$arch}; do
  659. if [ -f $base/target/$x/$y ]; then
  660. patchfiles="$patchfiles $base/target/$x/$y"
  661. fi
  662. done
  663. done
  664. }
  665. # Apply the given $patchfiles
  666. # [ hook called for each patch ] [ filter script ]
  667. #
  668. apply_patchfiles() {
  669. local hook="$1"
  670. local filter="$2"
  671. [ "$filter" ] || filter=cat
  672. for x in $patchfiles; do
  673. # correct the abolute path - e.g for patchfiles supplied
  674. # in the .desc file
  675. # we assume relative path patches are mirrorables //mnemoc
  676. if [ ! -e "$x" -a -n "${x##*/*}" ] ; then
  677. x="$base/download/mirror/${x:0:1}/$x"
  678. fi
  679. # Apply the patch if the file does exist or issue a warning
  680. if [ -f "$x" ] ; then
  681. echo "Apply patch $x ..."
  682. if [[ $x = *.bz2 ]] ; then
  683. patch_file=`mktemp` ; patch_del=1
  684. bzcat $x > $patch_file
  685. else
  686. patch_file=$x ; patch_del=0
  687. fi
  688. $filter $patch_file | patch $patchopt
  689. [ $patch_del = 1 ] && rm $patch_file
  690. eval "$hook"
  691. else
  692. echo_warning "Unable to apply patch: $x (File not found!)"
  693. fi
  694. done
  695. }
  696. # -------------------------------------------------------------------
  697. # The automatic extraction of archive (prior to building) supports
  698. # multiple archive types. For every archive type we have a separate
  699. # func that knows how to extract the archive. However, every func
  700. # should deliver two file: untar.txt and xsrcdir.txt.
  701. #
  702. # untar.txt needs to contain all extracted files.
  703. # xsrcdir.txt need to contain the top level extraction directories.
  704. # -------------------------------------------------------------------
  705. autoextract_tar_bz2() {
  706. echo "Extracting $xsrctar ($taropt) ... "
  707. tar -v $taropt $1 > untar.txt
  708. }
  709. autoextract_zip() {
  710. echo "Extracting $xsrctar ($zipopt) ... "
  711. unzip $zipopt $1 | sed 's,^.*/$,,' |
  712. cut -f4 -d" " | grep -v "^$" > untar.txt
  713. }
  714. # Main program for building a package
  715. #
  716. build_this_package() {
  717. if [ ".$desc_SRC" == "." ] ; then
  718. # Autodetect source tar and extract it
  719. #
  720. if [ "$srctar" = auto ] ; then
  721. xsourceballs=$( echo "$desc_D" | head -n 1 | tr ' ' '\t' | tr -s '\t' |
  722. cut -f2 | bz2filename )
  723. if [ -z "$xsourceballs" ] ; then
  724. echo "Can't auto-detect srctar for package '$xpkg'!"
  725. false
  726. fi
  727. else
  728. xsourceballs="$srctar"
  729. fi
  730. elif [ "$srctar" = auto ] ; then
  731. sourceballs=$( echo "$desc_D" | tr ' ' '\t' | tr -s '\t' |
  732. cut -f2 | bz2filename )
  733. xsrcpattern=$( echo "$desc_SRC" | tr ' ' '\t' | tr -s '\t' | tr '\t' '\n' )
  734. xsourceballs=$( echo "$sourceballs" | grep -F "$xsrcpattern" )
  735. else
  736. xsourceballs="$srctar"
  737. fi
  738. for xsrctar in $xsourceballs; do
  739. saved_patchfiles="$patchfiles"
  740. buildloop=1
  741. var_append patchfiles " " \
  742. "`ls $confdir/*.patch.${xsrctar/-[v0-9]*/} 2> /dev/null`"
  743. if [ "$xsrctar" != none -a "$autoextract" = 1 ]; then
  744. cd $builddir
  745. if [ -z "$custextract" ]; then
  746. # No custom extraction, so determine what
  747. # autoextraction to use.
  748. case "$xsrctar" in
  749. *.zip) custextract="autoextract_zip" ;;
  750. *) custextract="autoextract_tar_bz2" ;; # *.tar.bz2|*.tbz2|*.tbz
  751. esac
  752. fi
  753. if [ -n "$custextract" ]; then
  754. # Do the actual extraction of the archive.
  755. eval "$custextract $archdir/$xsrctar"
  756. cat untar.txt |
  757. sed 's,^\./,,' | cut -f1 -d/ |
  758. sort -u > xsrcdir.txt
  759. fi
  760. #
  761. if [ "$srcdir" = auto ]; then
  762. xsrcdir=${xsrctar%.tar.bz2}
  763. xsrcdir=${xsrcdir%.tbz2}
  764. xsrcdir=${xsrcdir%.tbz}
  765. if [ ! -d $xsrcdir ] ; then
  766. for x in $pkg-$ver ${pkg}_$ver $pkg \
  767. $xpkg-$ver ${xpkg}_$ver $xpkg \
  768. "$( cat xsrcdir.txt )"
  769. do
  770. [ -d "$x" ] && xsrcdir="$x"
  771. done
  772. fi
  773. else
  774. xsrcdir="$srcdir"
  775. fi
  776. #
  777. if [ "$chownsrcdir" = 1 ]; then
  778. echo "Fixing ownership and permissions ..."
  779. chown -R 0:0 $builddir/$xsrcdir
  780. fi
  781. #
  782. if [ "$nocvsinsrcdir" = 1 ]; then
  783. echo "Removing CVS, .svn, .git, {arch} and .arch-ids directories ..."
  784. egrep '(^|/)(CVS|\.svn|\.git|\{arch\}|\.arch-ids)(/|$)' untar.txt |
  785. while read x; do
  786. echo "Removing $x ..."
  787. rm -rf "$x"
  788. done
  789. fi
  790. #
  791. echo "Changeing into $builddir/$xsrcdir ..."
  792. cd $builddir/$xsrcdir
  793. # Apply patches
  794. #
  795. if [ $autopatch = 1 ]; then
  796. hook_eval prepatch
  797. apply_patchfiles
  798. hook_eval postpatch
  799. fi
  800. else
  801. cd $builddir
  802. fi
  803. if [ "$createprefix" = 1 ]; then
  804. echo "Creating $root/$prefix/<..> if required ..."
  805. for x in $foodirlist; do
  806. eval "x=\"$root\$$x\""
  807. if [ ! -e $x ]; then
  808. mkdir -p $x
  809. rmemptydir="$rmemptydir $x"
  810. fi
  811. done
  812. fi
  813. if [ -z "$custmain" ]; then
  814. while [ ${buildloop:-1} -le ${buildloops:-1} ]; do
  815. [ "${buildloops:-1}" == "1" ] || echo "loop ${buildloop:-1} of ${buildloops:-1} for $xsrctar."
  816. hook_eval preconf
  817. # Maybe generate a configure first
  818. #
  819. if [ $autogen -eq 1 -o \
  820. \( -f autogen.sh -a ! -f configure \) ] ; then
  821. if [ -f autogen.sh ] ; then
  822. echo "Running package autogen script."
  823. sed -i '/^\.\/configure /d' autogen.sh
  824. sh autogen.sh
  825. else
  826. echo "Running builtin autogen script."
  827. libtoolize --force --automake ; aclocal
  828. if grep AM_INIT_AUTOMAKE \
  829. configure.[ia][nc]
  830. then automake ; fi
  831. autoconf
  832. fi
  833. fi
  834. # Run configure scripts etc.
  835. #
  836. if [ $runconf = 1 ]; then
  837. if [ -n "$( type -p $configscript )" -o $autogen = 1 ]
  838. then
  839. eval_config_command $( eval echo $confopt )
  840. fi
  841. fi
  842. # automated package build
  843. # styles without make run first:
  844. if [ -f setup.py -a $runpysetup = 1 ] ; then
  845. pyconfopt="${pyconfopt:=--prefix $root/$prefix}"
  846. hook_eval premake
  847. eval ${pyscript:-python} setup.py build install $pyconfopt
  848. hook_eval postmake
  849. else # styles that include a make run
  850. if [ ! -f Makefile -a ! -f makefile -a \
  851. -f Makefile.PL -a $runmkpl = 1 ]; then
  852. perl Makefile.PL ${plconfopt:-INSTALLDIRS=perl}
  853. fi
  854. #
  855. if [ ! -f Makefile -a ! -f makefile -a \
  856. -f Imakefile -a $runxmkmf = 1 ]; then
  857. xmkmf -a
  858. fi
  859. #
  860. # Build it
  861. #
  862. hook_eval premake
  863. if [ "$makeopt" ]; then
  864. eval echo "Running $MAKE $makeopt"
  865. eval "$MAKE $makeopt"
  866. fi
  867. hook_eval inmake
  868. if [ "$makeinstopt" ]; then
  869. eval echo "Running $MAKE $makeinstopt"
  870. eval "$MAKE $makeinstopt"
  871. fi
  872. hook_eval postmake
  873. fi
  874. buildloop=$( expr ${buildloop:-1} + 1 )
  875. done
  876. else
  877. eval "$custmain"
  878. for x in preconf premake inmake postmake; do
  879. if eval "[ -n \"\$hookdirty_$x\" ]"; then
  880. echo "Hook $x is still marked as dirty ..."
  881. hook_eval $x
  882. fi
  883. done
  884. fi
  885. if [ "$createdocs" != 0 ]; then
  886. if [ ! -e $root$docdir ]; then
  887. mkdir -p $docdir
  888. rmemptydir="$rmemptydir $root$docdir"
  889. fi
  890. [ -z "$createdocs" ] && createdocs="$SDECFG_CREATE_DOCS"
  891. fi
  892. if [ "$createdocs" = 1 ]; then
  893. echo "Trying to copy the default documentation ..."
  894. for x in [A-Z][A-Z]* *.lsm ChangeLog* README LICENSE COPYING; do
  895. [ "${x#*.[cho0-9]}" ] || continue
  896. [ "${x#*.info*}" ] || continue
  897. [ "${x#*.TAR*}" ] || continue
  898. [ "${x#*akefile*}" ] || continue
  899. [ -f $x ] && cp -v $x $root$docdir/$x
  900. done
  901. echo "Trying to copy even more documentation ..."
  902. [ -d $builddir/$xsrcdir ] && cd $builddir/$xsrcdir
  903. for x in `find -type d \( -name 'doc' -o -name 'docs' \
  904. -o -name '[Dd]ocumentation' \) ! -empty`
  905. do
  906. if [ -d "$x" -a "`echo $x/*`" != "$x/*" ]
  907. then cp -rLv $x/* $root$docdir || true ; fi
  908. done
  909. for x in $confdir/*.doc; do
  910. if [ -f $x ]
  911. then cp -v $x $root$docdir/${x%.doc}; fi
  912. done
  913. find $root$docdir/ -name '*.[0-9]' -o -name '*.info*' \
  914. -o -name '[Mm]akefile*' |
  915. xargs -r rm -f 2> /dev/null || true
  916. find $root$docdir/* -type d -empty 2> /dev/null |
  917. xargs -r rmdir 2> /dev/null || true
  918. fi
  919. hook_eval postdoc
  920. if atstage native && [ -f /sbin/ldconfig ] ; then
  921. echo "Running ldconfig ..."
  922. ldconfig
  923. fi
  924. patchfiles="$saved_patchfiles"
  925. done
  926. if [ "$rmemptydir" ]; then
  927. rmdir $rmemptydir 2> /dev/null || true
  928. fi
  929. return 0
  930. }
  931. # source_file cksum file url
  932. #
  933. # Create the file path from 'file' and 'url'.
  934. # cksum and url are ignored
  935. # ([D] tag compatible format)
  936. #
  937. source_file() {
  938. local pre="" file="$2" url="$3" mirror="mirror"
  939. # '-' as $url prefix means, nomirrorable
  940. [ "${url:0:1}" == "-" ] && mirror="local"
  941. # inside Build-Pkg $archdir is set
  942. if [ -n "$archdir" ]; then
  943. pre=$base/; file="$( bz2filename $file )"
  944. fi
  945. echo ${pre}download/${mirror}/${file:0:1}/$file
  946. }
  947. # match_source_file [-p] pattern [[package] ...]
  948. #
  949. # returns path and name of a downloaded file from a list of packages, matching a grep pattern
  950. # without -p it only returns it's name, not the path.
  951. #
  952. match_source_file() {
  953. local pattern= package= showpath=0
  954. local x= file= url= mirror=
  955. local found=1
  956. if [ "$1" == "-p" ]; then
  957. showpath=1; shift
  958. fi
  959. pattern="$1"; shift
  960. for package in ${*:-$pkg}; do
  961. while read x x file url x; do
  962. file="$( bz2filename $file )"
  963. found=0
  964. if [ $showpath -eq 0 ]; then
  965. echo $file
  966. else
  967. [ "${url:0:1}" == "-" ] && mirror="local" || mirror="mirror"
  968. echo $base/download/${mirror}/${file:0:1}/$file
  969. fi
  970. done < <( grep -e "^\[D\].*$pattern" $base/package/*/$package/$package.desc )
  971. done
  972. return $found
  973. }
  974. # create the virtual $archdir symlinks
  975. #
  976. populate_archdir()
  977. {
  978. local x= missing=0
  979. for x in `match_source_file -p .`; do
  980. if [ ! -f $x ]; then
  981. echo_warning "File not found: ${x#$base/}"
  982. missing=1
  983. elif [ ! -e "$builddir/archdir/${x##*/}" ]; then
  984. ln -vs $x $builddir/archdir/
  985. fi
  986. done
  987. if [ $missing -eq 1 ]; then
  988. echo_warning "Did you run download for this package?"
  989. false
  990. fi
  991. }
  992. # search for the package confdir
  993. #
  994. detect_confdir()
  995. {
  996. confdir=""
  997. if [ -z "$pkgdir" ] ; then
  998. for x in package/*/$pkg/$pkg.desc ; do
  999. if [ -f "$x" ] ; then
  1000. if [ "$confdir" ] ; then
  1001. echo_pkg_deny $stagelevel $pkg "in multiple trees"
  1002. echo "Package in multiple trees: $pkg !" \
  1003. > $root/var/adm/logs/$stagelevel-$xpkg.err
  1004. exit 1
  1005. fi
  1006. x=${x#package/}; x=${x%%/*}
  1007. confdir="$base/package/$x/$pkg"
  1008. repository=$x
  1009. fi
  1010. done
  1011. else
  1012. if [ -f "$pkgdir/$pkg.desc" ] ; then
  1013. confdir="$pkgdir"
  1014. repository=extern
  1015. fi
  1016. fi
  1017. }
  1018. # initialize standard vars and hooks
  1019. #
  1020. init_vars_and_hooks()
  1021. {
  1022. makeopt='CC="$CC" CPP="$CPP" CXX="$CXX"'
  1023. if atstage toolchain; then
  1024. makeopt="$makeopt"' prefix="$root/$prefix"'
  1025. else
  1026. makeopt="$makeopt"' prefix="/$prefix"'
  1027. fi
  1028. if ! atstage native; then
  1029. makeopt="$makeopt"' CC_FOR_BUILD="$BUILDCC"'
  1030. makeopt="$makeopt"' BUILDCC="$BUILDCC" BUILD_CC="$BUILD_CC"'
  1031. makeopt="$makeopt"' HOSTCC="$HOSTCC" HOST_CC="$HOST_CC"'
  1032. makeopt="$makeopt"' STRIP="$STRIP" AR="$AR" LD="$LD"'
  1033. makeopt="$makeopt"' RANLIB="$RANLIB" NM="$NM"'
  1034. fi
  1035. if atstage native; then
  1036. flistdel="$flistdel|`echo $base | sed s,^/,,`/.*"
  1037. fi
  1038. if atstage cross; then
  1039. makeopt="$makeopt"' INSTALL_PREFIX="$root"'
  1040. makeopt="$makeopt"' DESTDIR="$root" DEST_DIR="$root"'
  1041. makeopt="$makeopt"' INSTROOT="$root" INSTALLROOT="$root"'
  1042. fi
  1043. makeinstopt="$makeopt"' install'
  1044. custmain=""
  1045. buildloop=1 buildloops=1
  1046. [ "$SDECFG_DO_CHECK" = 1 ] && hook_add inmake 6 'run_check'
  1047. hook_add postflist 3 'postflist_static_lib'
  1048. createarchcache=0
  1049. configprefix=""; configcache="" ; autogen=0
  1050. configscript="./configure" ; extraconfopt=""
  1051. srcdir=auto ; srctar=auto
  1052. taropt="--use-compress-program=bzip2 -xf"
  1053. mainfunction="build_this_package"
  1054. runconf=1 ; runxmkmf=1 ; runmkpl=1 ; runpysetup=1 ; autopatch=1
  1055. autoextract=1 ; chownsrcdir=1 ; nocvsinsrcdir=1 ; cleanconfopt=1
  1056. patchopt="-bfp1 -z .orig"
  1057. createprefix=1 ; createdocs="" ; rmemptydir=""
  1058. check_shared=1
  1059. check_usrlocal=1
  1060. check_badfiles=1
  1061. badfiles="" badfiles_nr=0
  1062. declare -a badfiles_desc
  1063. }
  1064. # this is a 2nd lightweight and modular "build this package" implementation
  1065. # currently only used for the postlinux stuff - later maybe for more -ReneR
  1066. #
  1067. build_package()
  1068. {
  1069. logstamp=$PWD/log
  1070. (
  1071. set -e
  1072. pushd $base
  1073. super=$pkg
  1074. pkg="$1" ; xpkg="$pkg"
  1075. conffile="$2" ; [ "$conffile" ] || conffile="$pkg.conf"
  1076. unset ${!hook*}
  1077. declare -a hook_functions='()'
  1078. hook_fcounter=0
  1079. init_vars_and_hooks
  1080. detect_confdir
  1081. detect_patchfiles
  1082. parse_desc $pkg
  1083. # Erase positional parameters to prevent unintended parameter
  1084. # passing. We do not want to pass the current positional parameters
  1085. # to the loaded script.
  1086. set --
  1087. eval "$desc_O"
  1088. echo_status "Building $xpkg ($ver) within $super (using $conffile)."
  1089. for x in $( get_expanded $base/target/%/pkg_$pkg.conf $targetchain ) \
  1090. $confdir/$conffile; do
  1091. if [ -f $x ]; then
  1092. if [[ $x == */$conffile ]]; then
  1093. echo "Reading package configuration ($conffile) from package directory."
  1094. else
  1095. echo "Reading package configuration from target directory."
  1096. fi
  1097. . $x
  1098. break
  1099. fi
  1100. done
  1101. # short path - to not abort on missing downloads of postlinux.conf
  1102. # packages that are not built anyway -ReneR
  1103. if [ "$custmain" = "true" ] ; then
  1104. echo "Nothing is going to be done ayway - returning quickly."
  1105. return
  1106. fi
  1107. populate_archdir
  1108. popd
  1109. # dump for debugging
  1110. hook_dump > $builddir/debug.hooks.$pkg
  1111. dump_env > $builddir/debug.buildenv.$pkg
  1112. echo "Running main build function '$mainfunction' ..."
  1113. cd $builddir
  1114. eval "$mainfunction"
  1115. touch $logstamp
  1116. )
  1117. [ -f $logstamp ] || return 1
  1118. rm $logstamp
  1119. return 0
  1120. }