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.

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