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.

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