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.

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