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.

1216 lines
30 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:-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. #
  587. # pkgchksum package-name
  588. #
  589. pkgchksum() {
  590. (
  591. # expand to full patch if only a package name was specified
  592. [[ $1 == */* ]] || set $base/package/*/$1/
  593. sh "$base/lib/sde-package/pkgchksum.sh" "$1"
  594. )
  595. }
  596. # Add files to the 'badfiles' list
  597. #
  598. register_badfiles() {
  599. local x desc="$1"
  600. shift
  601. for x in "$@"; do
  602. var_append badfiles $'\n' " $x\$"
  603. badfiles_desc[$badfiles_nr]=" $x\$"$'\n'"$desc"
  604. (( badfiles_nr++ ))
  605. done
  606. }
  607. # Detect the available patchfiles
  608. #
  609. detect_patchfiles()
  610. {
  611. local x= y=
  612. patchfiles="`ls $confdir/*.patch{,.$arch} \
  613. $confdir/*.patch_$xpkg{.$arch} \
  614. 2> /dev/null | tr '\n' ' '`"
  615. for x in $( get_reverted $targetchain ); do
  616. for y in pkg_$pkg.patch{,.$arch} xpkg_$xpkg.patch{,.$arch}; do
  617. if [ -f $base/target/$x/$y ]; then
  618. patchfiles="$patchfiles $base/target/$x/$y"
  619. fi
  620. done
  621. done
  622. }
  623. # Apply the given $patchfiles
  624. # [ hook called for each patch ] [ filter script ]
  625. #
  626. apply_patchfiles() {
  627. local hook="$1"
  628. local filter="$2"
  629. [ "$filter" ] || filter=cat
  630. for x in $patchfiles; do
  631. # correct the abolute path - e.g for patchfiles supplied
  632. # in the .desc file
  633. # we assume relative path patches are mirrorables //mnemoc
  634. if [ ! -e "$x" -a -n "${x##*/*}" ] ; then
  635. x="$base/download/mirror/${x:0:1}/$x"
  636. fi
  637. # Apply the patch if the file does exist or issue a warning
  638. if [ -f "$x" ] ; then
  639. echo "Apply patch $x ..."
  640. if [[ $x = *.bz2 ]] ; then
  641. patch_file=`mktemp` ; patch_del=1
  642. bzcat $x > $patch_file
  643. else
  644. patch_file=$x ; patch_del=0
  645. fi
  646. $filter $patch_file | patch $patchopt
  647. [ $patch_del = 1 ] && rm $patch_file
  648. eval "$hook"
  649. else
  650. echo_warning "Unable to apply patch: $x (File not found!)"
  651. fi
  652. done
  653. }
  654. # -------------------------------------------------------------------
  655. # The automatic extraction of archive (prior to building) supports
  656. # multiple archive types. For every archive type we have a separate
  657. # func that knows how to extract the archive. However, every func
  658. # should deliver two file: untar.txt and xsrcdir.txt.
  659. #
  660. # untar.txt needs to contain all extracted files.
  661. # xsrcdir.txt need to contain the top level extraction directories.
  662. # -------------------------------------------------------------------
  663. autoextract_tar_bz2() {
  664. echo "Extracting $xsrctar ($taropt) ... "
  665. tar -v $taropt $1 > untar.txt
  666. }
  667. autoextract_zip() {
  668. echo "Extracting $xsrctar ($zipopt) ... "
  669. unzip $zipopt $1 | sed 's,^.*/$,,' |
  670. cut -f4 -d" " | grep -v "^$" > untar.txt
  671. }
  672. # Main program for building a package
  673. #
  674. build_this_package() {
  675. if [ ".$desc_SRC" = "." ] ; then
  676. # Autodetect source tar and extract it
  677. #
  678. if [ "$srctar" = auto ] ; then
  679. xsourceballs=$( echo "$desc_D" | head -n 1 | tr ' ' '\t' | tr -s '\t' |
  680. cut -f2 | bz2filename )
  681. if [ -z "$xsourceballs" ] ; then
  682. echo "Can't auto-detect srctar for package '$xpkg'!"
  683. false
  684. fi
  685. else
  686. xsourceballs="$srctar"
  687. fi
  688. elif [ "$srctar" = auto ] ; then
  689. sourceballs=$( echo "$desc_D" | tr ' ' '\t' | tr -s '\t' |
  690. cut -f2 | bz2filename )
  691. xsrcpattern=$( echo "$desc_SRC" | tr ' ' '\t' | tr -s '\t' | tr '\t' '\n' )
  692. xsourceballs=$( echo "$sourceballs" | grep -F "$xsrcpattern" )
  693. else
  694. xsourceballs="$srctar"
  695. fi
  696. for xsrctar in $xsourceballs; do
  697. saved_patchfiles="$patchfiles"
  698. buildloop=1
  699. var_append patchfiles " " \
  700. "`ls $confdir/*.patch.${xsrctar/-[v0-9]*/} 2> /dev/null`"
  701. if [ "$xsrctar" != none -a "$autoextract" = 1 ]; then
  702. cd $builddir
  703. if [ -z "$custextract" ]; then
  704. # No custom extraction, so determine what
  705. # autoextraction to use.
  706. case "$xsrctar" in
  707. *.zip) custextract="autoextract_zip" ;;
  708. *) custextract="autoextract_tar_bz2" ;; # *.tar.bz2|*.tbz2|*.tbz
  709. esac
  710. fi
  711. if [ -n "$custextract" ]; then
  712. # Do the actual extraction of the archive.
  713. eval "$custextract $archdir/$xsrctar"
  714. cat untar.txt |
  715. sed 's,^\./,,' | cut -f1 -d/ |
  716. sort -u > xsrcdir.txt
  717. fi
  718. #
  719. if [ "$srcdir" = auto ]; then
  720. xsrcdir=${xsrctar%.tar.bz2}
  721. xsrcdir=${xsrcdir%.tbz2}
  722. xsrcdir=${xsrcdir%.tbz}
  723. if [ ! -d $xsrcdir ] ; then
  724. for x in $pkg-$ver ${pkg}_$ver $pkg \
  725. $xpkg-$ver ${xpkg}_$ver $xpkg \
  726. "$( cat xsrcdir.txt )"
  727. do
  728. [ -d "$x" ] && xsrcdir="$x"
  729. done
  730. fi
  731. else
  732. xsrcdir="$srcdir"
  733. fi
  734. #
  735. if [ "$chownsrcdir" = 1 ]; then
  736. echo "Fixing ownership and permissions ..."
  737. chown -R 0:0 $builddir/$xsrcdir
  738. fi
  739. #
  740. if [ "$nocvsinsrcdir" = 1 ]; then
  741. echo "Removing CVS, .svn, .git, {arch} and .arch-ids directories ..."
  742. egrep '(^|/)(CVS|\.svn|\.git|\{arch\}|\.arch-ids)(/|$)' untar.txt |
  743. while read x; do
  744. echo "Removing $x ..."
  745. rm -rf "$x"
  746. done
  747. fi
  748. #
  749. echo "Changeing into $builddir/$xsrcdir ..."
  750. cd $builddir/$xsrcdir
  751. # Apply patches
  752. #
  753. if [ $autopatch = 1 ]; then
  754. hook_eval prepatch
  755. apply_patchfiles
  756. hook_eval postpatch
  757. fi
  758. else
  759. cd $builddir
  760. fi
  761. if [ "$createprefix" = 1 ]; then
  762. echo "Creating $root/$prefix/<..> if required ..."
  763. for x in $foodirlist; do
  764. eval "x=\"$root\$$x\""
  765. if [ ! -e $x ]; then
  766. mkdir -p $x
  767. rmemptydir="$rmemptydir $x"
  768. fi
  769. done
  770. fi
  771. if [ -z "$custmain" ]; then
  772. while [ ${buildloop:-1} -le ${buildloops:-1} ]; do
  773. [ "${buildloops:-1}" = "1" ] || echo "loop ${buildloop:-1} of ${buildloops:-1} for $xsrctar."
  774. hook_eval preconf
  775. # Maybe generate a configure first
  776. #
  777. if [ $autogen -eq 1 -o \
  778. \( -f autogen.sh -a ! -f configure \) ] ; then
  779. if [ -f autogen.sh ] ; then
  780. echo "Running package autogen script."
  781. sed -i '/^\.\/configure /d' autogen.sh
  782. sh autogen.sh
  783. else
  784. echo "Running builtin autogen script."
  785. libtoolize --force --automake ; aclocal
  786. if grep AM_INIT_AUTOMAKE \
  787. configure.[ia][nc]
  788. then automake ; fi
  789. autoconf
  790. fi
  791. fi
  792. # Run configure scripts etc.
  793. #
  794. if [ $runconf = 1 ]; then
  795. if [ -n "$( type -p $configscript )" -o $autogen = 1 ]
  796. then
  797. eval_config_command $( eval echo $confopt )
  798. fi
  799. fi
  800. # automated package build
  801. # styles without make run first:
  802. if [ -f setup.py -a $runpysetup = 1 ] ; then
  803. pyconfopt="${pyconfopt:=--prefix $root/$prefix}"
  804. hook_eval premake
  805. eval ${pyscript:-python} setup.py build install $pyconfopt
  806. hook_eval postmake
  807. else # styles that include a make run
  808. if [ ! -f Makefile -a ! -f makefile -a \
  809. -f Makefile.PL -a $runmkpl = 1 ]; then
  810. perl Makefile.PL ${plconfopt:-INSTALLDIRS=perl}
  811. fi
  812. #
  813. if [ ! -f Makefile -a ! -f makefile -a \
  814. -f Imakefile -a $runxmkmf = 1 ]; then
  815. xmkmf -a
  816. fi
  817. #
  818. # Build it
  819. #
  820. hook_eval premake
  821. if [ "$makeopt" ]; then
  822. eval echo "Running $MAKE $makeopt"
  823. eval "$MAKE $makeopt"
  824. fi
  825. hook_eval inmake
  826. if [ "$makeinstopt" ]; then
  827. eval echo "Running $MAKE $makeinstopt"
  828. eval "$MAKE $makeinstopt"
  829. fi
  830. hook_eval postmake
  831. fi
  832. buildloop=$( expr ${buildloop:-1} + 1 )
  833. done
  834. else
  835. eval "$custmain"
  836. for x in preconf premake inmake postmake; do
  837. if eval "[ -n \"\$hookdirty_$x\" ]"; then
  838. echo "Hook $x is still marked as dirty ..."
  839. hook_eval $x
  840. fi
  841. done
  842. fi
  843. if [ "$createdocs" != 0 ]; then
  844. if [ ! -e $root$docdir ]; then
  845. mkdir -p $docdir
  846. rmemptydir="$rmemptydir $root$docdir"
  847. fi
  848. [ -z "$createdocs" ] && createdocs="$SDECFG_CREATE_DOCS"
  849. fi
  850. if [ "$createdocs" = 1 ]; then
  851. echo "Trying to copy the default documentation ..."
  852. for x in [A-Z][A-Z]* *.lsm ChangeLog* README LICENSE COPYING; do
  853. [ "${x#*.[cho0-9]}" ] || continue
  854. [ "${x#*.info*}" ] || continue
  855. [ "${x#*.TAR*}" ] || continue
  856. [ "${x#*akefile*}" ] || continue
  857. [ -f $x ] && cp -v $x $root$docdir/$x
  858. done
  859. echo "Trying to copy even more documentation ..."
  860. [ -d $builddir/$xsrcdir ] && cd $builddir/$xsrcdir
  861. for x in `find -type d \( -name 'doc' -o -name 'docs' \
  862. -o -name '[Dd]ocumentation' \) ! -empty`
  863. do
  864. if [ -d "$x" -a "`echo $x/*`" != "$x/*" ]
  865. then cp -rLv $x/* $root$docdir || true ; fi
  866. done
  867. for x in $confdir/*.doc; do
  868. if [ -f $x ]
  869. then cp -v $x $root$docdir/${x%.doc}; fi
  870. done
  871. find $root$docdir/ -name '*.[0-9]' -o -name '*.info*' \
  872. -o -name '[Mm]akefile*' |
  873. xargs -r rm -f 2> /dev/null || true
  874. find $root$docdir/* -type d -empty 2> /dev/null |
  875. xargs -r rmdir 2> /dev/null || true
  876. fi
  877. hook_eval postdoc
  878. if atstage native && [ -f /sbin/ldconfig ] ; then
  879. echo "Running ldconfig ..."
  880. ldconfig
  881. fi
  882. patchfiles="$saved_patchfiles"
  883. done
  884. if [ "$rmemptydir" ]; then
  885. rmdir $rmemptydir 2> /dev/null || true
  886. fi
  887. return 0
  888. }
  889. # source_file cksum file url
  890. #
  891. # Create the file path from 'file' and 'url'.
  892. # cksum and url are ignored
  893. # ([D] tag compatible format)
  894. #
  895. source_file() {
  896. local pre= file="$2" url="$3" mirror="mirror"
  897. # '-' as $url prefix means, nomirrorable
  898. [ "${url:0:1}" = "-" ] && mirror="local"
  899. # inside Build-Pkg $archdir is set
  900. if [ -n "$archdir" ]; then
  901. pre=$base/; file="$( bz2filename $file )"
  902. fi
  903. echo ${pre}download/${mirror}/${file:0:1}/$file
  904. }
  905. # match_source_file [-p] pattern [[package] ...]
  906. #
  907. # returns path and name of a downloaded file from a list of packages, matching a grep pattern
  908. # without -p it only returns it's name, not the path.
  909. #
  910. match_source_file() {
  911. local pattern= package= showpath=0
  912. local x= file= url= mirror=
  913. local found=1
  914. if [ "$1" = "-p" ]; then
  915. showpath=1; shift
  916. fi
  917. pattern="$1"; shift
  918. for package in ${*:-$pkg}; do
  919. while read x x file url x; do
  920. file="$( bz2filename $file )"
  921. found=0
  922. if [ $showpath -eq 0 ]; then
  923. echo $file
  924. else
  925. [ "${url:0:1}" = "-" ] && mirror="local" || mirror="mirror"
  926. echo $base/download/${mirror}/${file:0:1}/$file
  927. fi
  928. done < <( grep -e "^\[D\].*$pattern" $base/package/*/$package/$package.desc )
  929. done
  930. return $found
  931. }
  932. # create the virtual $archdir symlinks
  933. #
  934. populate_archdir()
  935. {
  936. local x= missing=0
  937. for x in `match_source_file -p .`; do
  938. if [ ! -f $x ]; then
  939. echo_warning "File not found: ${x#$base/}"
  940. missing=1
  941. elif [ ! -e "$builddir/archdir/${x##*/}" ]; then
  942. ln -vs $x $builddir/archdir/
  943. fi
  944. done
  945. if [ $missing -eq 1 ]; then
  946. echo_warning "Did you run download for this package?"
  947. false
  948. fi
  949. }
  950. # search for the package confdir
  951. #
  952. detect_confdir()
  953. {
  954. confdir=
  955. if [ -z "$pkgdir" ] ; then
  956. for x in package/*/$pkg/$pkg.desc ; do
  957. if [ -f "$x" ] ; then
  958. if [ "$confdir" ] ; then
  959. echo_pkg_deny $stagelevel $pkg "in multiple trees"
  960. echo "Package in multiple trees: $pkg !" \
  961. > $root/var/adm/logs/$stagelevel-$xpkg.err
  962. exit 1
  963. fi
  964. x=${x#package/}; x=${x%%/*}
  965. confdir="$base/package/$x/$pkg"
  966. repository=$x
  967. fi
  968. done
  969. else
  970. if [ -f "$pkgdir/$pkg.desc" ] ; then
  971. confdir="$pkgdir"
  972. repository=extern
  973. fi
  974. fi
  975. }
  976. # initialize standard vars and hooks
  977. #
  978. init_vars_and_hooks()
  979. {
  980. makeopt='CC="$CC" CPP="$CPP" CXX="$CXX"'
  981. if atstage toolchain; then
  982. makeopt="$makeopt"' prefix="$root/$prefix"'
  983. else
  984. makeopt="$makeopt"' prefix="/$prefix"'
  985. fi
  986. if ! atstage native; then
  987. makeopt="$makeopt"' CC_FOR_BUILD="$BUILDCC"'
  988. makeopt="$makeopt"' BUILDCC="$BUILDCC" BUILD_CC="$BUILD_CC"'
  989. makeopt="$makeopt"' HOSTCC="$HOSTCC" HOST_CC="$HOST_CC"'
  990. makeopt="$makeopt"' STRIP="$STRIP" AR="$AR" LD="$LD"'
  991. makeopt="$makeopt"' RANLIB="$RANLIB" NM="$NM"'
  992. fi
  993. if atstage native; then
  994. flistdel="$flistdel|`echo $base | sed s,^/,,`/.*"
  995. fi
  996. if atstage cross; then
  997. makeopt="$makeopt"' INSTALL_PREFIX="$root"'
  998. makeopt="$makeopt"' DESTDIR="$root" DEST_DIR="$root"'
  999. makeopt="$makeopt"' INSTROOT="$root" INSTALLROOT="$root"'
  1000. fi
  1001. makeinstopt="$makeopt"' install'
  1002. custmain=
  1003. buildloop=1 buildloops=1
  1004. [ "$SDECFG_DO_CHECK" = 1 ] && hook_add inmake 6 'run_check'
  1005. hook_add postflist 3 'postflist_static_lib'
  1006. createarchcache=0
  1007. configprefix=; configcache= ; autogen=0
  1008. configscript="./configure" ; extraconfopt=
  1009. srcdir=auto ; srctar=auto
  1010. taropt="--use-compress-program=bzip2 -xf"
  1011. mainfunction="build_this_package"
  1012. runconf=1 ; runxmkmf=1 ; runmkpl=1 ; runpysetup=1 ; autopatch=1
  1013. autoextract=1 ; chownsrcdir=1 ; nocvsinsrcdir=1 ; cleanconfopt=1
  1014. patchopt="-bfp1 -z .orig"
  1015. createprefix=1 ; createdocs= ; rmemptydir=
  1016. check_shared=1
  1017. check_usrlocal=1
  1018. check_badfiles=1
  1019. badfiles= badfiles_nr=0
  1020. declare -a badfiles_desc
  1021. }
  1022. # this is a 2nd lightweight and modular "build this package" implementation
  1023. # currently only used for the postlinux stuff - later maybe for more -ReneR
  1024. #
  1025. build_package()
  1026. {
  1027. logstamp=$PWD/log
  1028. (
  1029. set -e
  1030. pushd $base
  1031. super=$pkg
  1032. pkg="$1" ; xpkg="$pkg"
  1033. conffile="$2" ; [ "$conffile" ] || conffile="$pkg.conf"
  1034. unset ${!hook*}
  1035. declare -a hook_functions='()'
  1036. hook_fcounter=0
  1037. init_vars_and_hooks
  1038. detect_confdir
  1039. detect_patchfiles
  1040. parse_desc $pkg
  1041. # Erase positional parameters to prevent unintended parameter
  1042. # passing. We do not want to pass the current positional parameters
  1043. # to the loaded script.
  1044. set --
  1045. eval "$desc_O"
  1046. echo_status "Building $xpkg ($ver) within $super (using $conffile)."
  1047. for x in $( get_expanded $base/target/%/pkg_$pkg.conf $targetchain ) \
  1048. $confdir/$conffile; do
  1049. if [ -f $x ]; then
  1050. if [[ $x == */$conffile ]]; then
  1051. echo "Reading package configuration ($conffile) from package directory."
  1052. else
  1053. echo "Reading package configuration from target directory."
  1054. fi
  1055. . $x
  1056. break
  1057. fi
  1058. done
  1059. # short path - to not abort on missing downloads of postlinux.conf
  1060. # packages that are not built anyway -ReneR
  1061. if [ "$custmain" = "true" ] ; then
  1062. echo "Nothing is going to be done ayway - returning quickly."
  1063. return
  1064. fi
  1065. populate_archdir
  1066. popd
  1067. # dump for debugging
  1068. hook_dump > $builddir/debug.hooks.$pkg
  1069. dump_env > $builddir/debug.buildenv.$pkg
  1070. echo "Running main build function '$mainfunction' ..."
  1071. cd $builddir
  1072. eval "$mainfunction"
  1073. touch $logstamp
  1074. )
  1075. [ -f $logstamp ] || return 1
  1076. rm $logstamp
  1077. return 0
  1078. }