mirror of the now-defunct rocklinux.org
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.

606 lines
16 KiB

  1. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  2. #
  3. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. # Please add additional copyright information _after_ the line containing
  5. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  6. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  7. #
  8. # ROCK Linux: rock-src/scripts/functions
  9. # ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
  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; either version 2 of the License, or
  14. # (at your option) any later version. A copy of the GNU General Public
  15. # License can be found at Documentation/COPYING.
  16. #
  17. # Many people helped and are helping developing ROCK Linux. Please
  18. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  19. # file for details.
  20. #
  21. # --- ROCK-COPYRIGHT-NOTE-END ---
  22. # This function returns a "uniqe id" as output
  23. #
  24. get_unique() {
  25. date "+%s.$$.`/sbin/ifconfig -a | cksum | cut -f1 -d' '`"
  26. }
  27. # Hook variables
  28. #
  29. unset hook_functions hook_fcounter
  30. declare -a hook_functions='()'
  31. hook_fcounter=0
  32. # This function adds a code fragment to a named hook with the named priority
  33. #
  34. # hook_add hook_name priority code
  35. #
  36. hook_add() {
  37. hook_functions[hook_fcounter]="$3" # declare -a hookidx_$1
  38. eval "hookidx_$1[\${#hookidx_$1[@]}]=\"$2 $hook_fcounter\""
  39. eval "(( hookdirty_$1++ ))"; (( hook_fcounter++ ))
  40. }
  41. # This function executes all code fragments from the named hook
  42. #
  43. # hook_eval hook_name
  44. #
  45. hook_eval() {
  46. while read pri fnr ; do
  47. eval "${hook_functions[fnr]}"
  48. done < <( IFS=$'\n' ; eval "echo \"\${hookidx_$1[*]}\"" | sort )
  49. eval "unset hookdirty_$1"
  50. }
  51. # This function prints all hooks and their current contents
  52. #
  53. # hook_dump
  54. #
  55. hook_dump() {
  56. for hook in ${!hookidx_*} ; do
  57. hook=${hook#hookidx_}
  58. echo ; echo "Contents of hook $hook:"
  59. while read pri fnr ; do
  60. echo ; echo " $pri ($fnr)"
  61. echo "${hook_functions[fnr]}" | sed 's,^, ,'
  62. done < <( IFS=$'\n'
  63. eval "echo \"\${hookidx_$hook[*]}\"" | sort )
  64. if eval "[ -n \"\$hookdirty_\$hook\" ]"; then
  65. echo ; echo -n " Hook is marked as dirty: "
  66. eval "echo \"\${hookdirty_$hook}\""
  67. fi
  68. done
  69. echo
  70. }
  71. # This functions append, insert or remove values in variables:
  72. #
  73. # var_append PATH ":" "$HOME/bin"
  74. # var_insert CC_WRAPPER_INSERT " " "-O3"
  75. # var_remove CC_WRAPPER_INSERT " " "-O3"
  76. #
  77. # var_remove_regex CC_WRAPPER_INSERT " " "-O.*"
  78. #
  79. # var_insert_before_regex patchfiles " " "mypatch.diff" ".*\/foo.diff"
  80. #
  81. # 1st Parameter: Variable Name
  82. # 2nd Parameter: Delimiter Text
  83. # 3rd Parameter: Value (or regex)
  84. # 4th Parameter: regex for insert_before
  85. #
  86. var_append() {
  87. eval "[ \"\$$1\" ] && $1=\"\${$1}$2\"" || true
  88. eval "$1=\"\${$1}\$3\""
  89. }
  90. var_insert() {
  91. eval "[ \"\$$1\" ] && $1=\"$2\$$1\"" || true
  92. eval "$1=\"\$3\$$1\""
  93. }
  94. var_remove() {
  95. local a=${2//\/\\/}
  96. local b=${3//\/\\/}
  97. eval '[ "$'$1'" = "$3" ] && '$1'="" || true'
  98. eval $1'="${'$1'//$a$b$a/$2}"'
  99. eval $1'="${'$1'%$a$b}"'
  100. eval $1'="${'$1'#$b$a}"'
  101. }
  102. var_remove_regex() {
  103. eval "$1=\"\`echo \"\$$1\" | awk -- ' { split(\$0, a, \"$2\"); for (c1=c2=1; c1 in a; c1++) if ( a[c1] !~ /^$3\\\$/ ) b[c2++]=a[c1]; for (c1=1; c1 in b; c1++) printf \"%s%s\", (c1 > 0 ? \"$2\" : \"\"), b[c1]; }'\`\""
  104. }
  105. var_insert_before_regex() {
  106. eval "$1=\"\`echo \"\$$1\" | awk -- '{ split(\$0, a, \"$2\"); for (d=c1=c2=1; c1 in a; c1++) { if ( d && a[c1] ~ /^$4\\\$/ ) { b[c2++]=\"$3\"; d=0; } b[c2++]=a[c1]; } if (d) b[c2++]=\"$3\"; for (c1=1; c1 in b; c1++) printf \"%s%s\", (c1 > 0 ? \"$2\" : \"\"), b[c1]; }'\`\""
  107. }
  108. # This function can be used to duplicate a shell-function. E.g. when
  109. # overwriting a shell-function but the old one should stay available under
  110. # a new name:
  111. #
  112. # copy_function set_confopt set_confopt_foobar_old
  113. #
  114. # set_confopt() {
  115. # ....
  116. # set_confopt_foobar_old "$@"
  117. # ....
  118. # }
  119. #
  120. copy_function() {
  121. eval "$( declare -f $1 | sed "1 s,$1,$2," )"
  122. }
  123. # This function sets the 'confopt' and some other variables.
  124. # Re-run it in the package .conf file if you modify $prefix
  125. #
  126. set_confopt() {
  127. confopt="--prefix=$root/$prefix"
  128. docdir="$root/$prefix/doc/$pkg"
  129. bindir="$root/$prefix/bin"
  130. confopt="$confopt --bindir=\$bindir"
  131. sbindir="$root/$prefix/sbin"
  132. confopt="$confopt --sbindir=\$sbindir"
  133. libdir="$root/$prefix/lib"
  134. confopt="$confopt --libdir=\$libdir"
  135. datadir="$root/$prefix/share"
  136. confopt="$confopt --datadir=\$datadir"
  137. infodir="$root/$prefix/info"
  138. confopt="$confopt --infodir=\$infodir"
  139. mandir="$root/$prefix/man"
  140. confopt="$confopt --mandir=\$mandir"
  141. if [ "${prefix#opt/}" != "$prefix" ] ; then
  142. sysconfdir="$root/etc/$prefix"
  143. localstatedir="$root/var/$prefix"
  144. elif [ "${prefix#usr}" != "$prefix" ] ; then
  145. sysconfdir="$root/etc"
  146. localstatedir="$root/var"
  147. else
  148. sysconfdir="$root/$prefix/etc"
  149. localstatedir="$root/$prefix/var"
  150. fi
  151. confopt="$confopt --sysconfdir=\$sysconfdir"
  152. confopt="$confopt --localstatedir=\$localstatedir"
  153. if [ "$ROCKCFG_CONFIGURE_OPTS" ] ; then
  154. confopt="$confopt $ROCKCFG_CONFIGURE_OPTS"
  155. fi
  156. if [ "$ROCKCFG_OPT" = 1 ] ; then
  157. confopt="$confopt --disable-debug"
  158. fi
  159. if [ "$stagelevel" -le 1 -o "$ROCKCFG_DISABLE_NLS" = 1 ] ; then
  160. confopt="${confopt//--enable-nls/} --disable-nls"
  161. fi
  162. confopt="$confopt \$extraconfopt"
  163. if [ "$stagelevel" -eq 0 ]; then
  164. confopt="$confopt --target=\$arch_target --host=\$arch_build"
  165. else
  166. confopt="$confopt --build=\$arch_build --host=\$arch_target"
  167. fi
  168. }
  169. #
  170. # eval_config_command $( eval echo $confopt )
  171. #
  172. function eval_config_command() {
  173. local config_command
  174. for x in /usr/share/automake/*
  175. do
  176. [ -x "$x" -a -f "$x" ] || continue
  177. x="$( basename "$x" )"
  178. if [ -L $x -a ! -e $x ] ; then
  179. echo "Fixing dead symlink $x."
  180. ln -sf /usr/share/automake/$x .
  181. fi
  182. done
  183. if [ $autogen -eq 1 -a -f configure.in ] ; then
  184. if [ -f configure.orig ] ; then
  185. echo "Found configure.orig. Not" \
  186. "running autogen script."
  187. elif [ -f autogen.sh ] ; then
  188. echo "Running package autogen script."
  189. sh autogen.sh
  190. else
  191. echo "Running builtin autogen script."
  192. libtoolize --force --automake ; aclocal
  193. if grep AM_INIT_AUTOMAKE configure.in
  194. then automake ; fi
  195. autoconf
  196. fi
  197. fi
  198. if [ $stagelevel -le 1 ] ; then
  199. create_config_cache >> config.cache
  200. grep -q '.--cache-file=' $configscript &&
  201. set -- "$@" "--cache-file=config.cache"
  202. export cache_file=config.cache
  203. fi
  204. config_command="$configprefix $configscript"
  205. sub_scripts="$( find $( dirname $configscript ) -name configure )"
  206. # remove unsupported config script options
  207. for x ; do
  208. if grep -q "[[ ]${x%%=*}[]= ):]" $configscript $sub_scripts ; then
  209. config_command="$config_command $x"
  210. elif [[ $x = --*able-* ]] && egrep -q "\-\-(en|dis)able-\*" $configscript ; then
  211. echo "Autodetection for option impossible: " \
  212. "$x passed thru."
  213. config_command="$config_command $x"
  214. else
  215. echo "Removing unsupported '$x' from" \
  216. "configure option string."
  217. fi
  218. done
  219. echo Running "$config_command"
  220. eval "$config_command"
  221. }
  222. # Parse the *.desc file. Use the description from PKG-DESC-FORMAT and
  223. # save the tag data in $desc_*.
  224. #
  225. parse_desc() {
  226. tag="`grep '^\[' $base/Documentation/Developers/PKG-DESC-FORMAT | \
  227. sed 's, (\*),,; s,\] \[,|,g; s,\[,,; s,\],,;'`"
  228. for tag in $tag ; do
  229. tagdata="`egrep "^\[($tag)\]" $confdir/$1.desc | \
  230. cut -f2- -d']' | sed 's,^ ,,'`"
  231. tag="`echo $tag | cut -f1 -d'|' | tr - _`"
  232. eval "desc_$tag=\"\$tagdata\""
  233. done
  234. }
  235. # This function is used for forcing a file to be in the flist
  236. #
  237. add_flist() {
  238. for addfile ; do
  239. echo "$addfile" | fl_wrparse -D -r "$xroot/" \
  240. >> $builddir/flist.txt
  241. done
  242. }
  243. # This function is used for forcing a package to be in the dependency list
  244. #
  245. add_dependency() {
  246. for addpackage ; do
  247. echo "$addpackage: add_dependency()" \
  248. >> $builddir/dependencies.debug
  249. done
  250. }
  251. # This function is used to subsitute some important variables
  252. # using a D_ prefix thru m4 ...
  253. rock_substitute() {
  254. sed -e s,D_prefix,$prefix,g -e s,D_sysconfdir,$sysconfdir,g \
  255. -e s,D_docdir,$docdir,g -e s,D_localstatedir,$localstatedir,g \
  256. -e s,D_datadir,$datadir,g -e s,D_infodir,$infodir,g \
  257. -e s,D_bindir,$bindir,g -e s,D_sbindir,$sbindir,g \
  258. -e s,D_libdir,$libdir,g -e s,D_mandir,$mandir,g $1
  259. }
  260. # This outputs a predefined config.cache file as it needed by some
  261. # packages to cross-build correctly in stages 0 and 1.
  262. #
  263. create_config_cache() {
  264. cat $base/scripts/config.cache
  265. echo -e "\n# Architecture specific stuff\n"
  266. echo "ac_cv_sizeof_short=$arch_sizeof_short"
  267. echo "ac_cv_sizeof_int=$arch_sizeof_int"
  268. echo "ac_cv_sizeof_long=$arch_sizeof_long"
  269. echo "ac_cv_sizeof_long_long=$arch_sizeof_long_long"
  270. echo "ac_cv_sizeof_char_p=$arch_sizeof_char_p"
  271. echo "ac_cv_c_bigendian=$arch_bigendian"
  272. echo "ac_cv_prog_CC=$CC"
  273. }
  274. # Abort build before actual build starts
  275. # (is overwritten in Build-Pkg)
  276. #
  277. abort() {
  278. echo -e "The package build aborted with the following config" \
  279. "error:\n$*" > $root/var/adm/logs/$stagelevel-$pkg.err
  280. echo_errorquote "`cat $root/var/adm/logs/$stagelevel-$pkg.err`"
  281. echo_pkg_abort $stagelevel $repository $pkg
  282. exit 1
  283. }
  284. # Dump Environment
  285. #
  286. dump_env() {
  287. # Dump $base - only set if there is not already an older value.
  288. #
  289. echo "base=\"\${base:-$base}\""
  290. # Dump all variables including their flags, but skip read-only
  291. # variables and $base.
  292. #
  293. # Substitute base directory with ${base}.
  294. #
  295. for name in ${!a*} ${!b*} ${!c*} ${!d*} ${!e*} ${!f*} ${!g*} ${!h*} \
  296. ${!i*} ${!j*} ${!k*} ${!l*} ${!m*} ${!n*} ${!o*} ${!p*} \
  297. ${!q*} ${!r*} ${!s*} ${!t*} ${!u*} ${!v*} ${!w*} ${!x*} \
  298. ${!y*} ${!z*} \
  299. ${!A*} ${!B*} ${!C*} ${!D*} ${!E*} ${!F*} ${!G*} ${!H*} \
  300. ${!I*} ${!J*} ${!K*} ${!L*} ${!M*} ${!N*} ${!O*} ${!P*} \
  301. ${!Q*} ${!R*} ${!S*} ${!T*} ${!U*} ${!V*} ${!W*} ${!X*} \
  302. ${!Y*} ${!Z*} ${!_*}
  303. do
  304. [ $name = base ] && continue
  305. if declare -p $name | head -1 | grep -qv '^declare -[a-z]*r'
  306. then
  307. declare -p $name | sed "s,\\(^\\|[\"=:]\\)$base\\([\"=:/]\\|\$\\),\\1\${base}\\2,g"
  308. fi
  309. done
  310. # Dump functions
  311. #
  312. declare -f | sed 's/^declare -f //; s/<<(/< <(/;'
  313. # Dump aliases
  314. #
  315. alias
  316. }
  317. # Check for a package in configuration
  318. #
  319. pkgcheck() {
  320. egrep -q "^$2.* ($1) " $base/config/$config/packages
  321. }
  322. # Register a window-manager
  323. #
  324. register_wm() {
  325. [ -e /usr/share/rock-registry/wm ] || mkdir -p $root/usr/share/rock-registry/wm
  326. echo -e "name=\"$2\"\nexec=\"$3\"\n" > $root/usr/share/rock-registry/wm/$1
  327. }
  328. # Register an application
  329. #
  330. register_application() {
  331. [ -e /usr/share/rock-registry/app ] \
  332. || mkdir -p /usr/share/rock-registry/app
  333. echo -e "name=\"$2\"\nexec=\"$3\"\n" > /usr/share/rock-registry/app/$1
  334. }
  335. # Create Package Database for gasgui install tool
  336. #
  337. create_package_db() {
  338. rm -f $3.tmp
  339. for file in $( ls -dr $1/descs/?* ) ; do
  340. pkg="${file##*/}"
  341. # only include the package if a binary file is available
  342. if [ "$ROCKCFG_PKGFILE_VER" = 1 ] ; then
  343. v=-$(grep '^Package Name and Version' \
  344. $1/packages/$pkg | cut -f6 -d' ')
  345. else
  346. v=""
  347. fi
  348. if [ "$ROCKCFG_CREATE_GEM" = 1 ] ; then
  349. bfile=${pkg}${v}.gem
  350. else
  351. bfile=${pkg}${v}.tar.bz2
  352. fi
  353. if [ -e $2/$bfile ] ; then
  354. [ "$pkg" = TRANS.TBL ] && continue
  355. ( echo -e "$pkg"
  356. echo -e "\027"
  357. cat $1/descs/$pkg
  358. echo -e "\027"
  359. cat $1/dependencies/$pkg
  360. echo -e "\027"
  361. cat $1/cksums/$pkg
  362. echo -e "\027" ) >> $3.tmp
  363. else
  364. echo_error "Binary file for $bfile not present." \
  365. "Skipped in package database."
  366. fi
  367. done
  368. gzip -c $3.tmp > $3 ; rm -f $3.tmp
  369. }
  370. # Add files to the 'badfiles' list
  371. #
  372. register_badfiles() {
  373. local x desc="$1"
  374. shift
  375. for x in "$@"; do
  376. var_append badfiles $'\n' " $x\$"
  377. badfiles_desc[$badfiles_nr]=" $x\$"$'\n'"$desc"
  378. (( badfiles_nr++ ))
  379. done
  380. }
  381. # Apply the given $patchfiles
  382. #
  383. apply_patchfiles() {
  384. for x in $patchfiles; do
  385. echo "Apply patch $x ..."
  386. if [[ $x = *.bz2 ]] ; then
  387. bzcat $x | patch $patchopt
  388. else
  389. eval patch $patchopt < $x
  390. fi
  391. done
  392. }
  393. # Main program for building a package
  394. #
  395. build_this_package() {
  396. if [ ".$desc_SRC" == "." ] ; then
  397. # Autodetect source tar and extract it
  398. #
  399. if [ $srctar = auto ] ; then
  400. xsourceballs=$( echo "$desc_D" | head -1 | tr ' ' '\t' | tr -s '\t' | \
  401. cut -f2 | sed 's,.\(t\?\)gz$,.\1bz2,' )
  402. if [ -z "$xsourceballs" ] ; then
  403. echo "Can't auto-detect srctar for package '$pkg'!"
  404. false
  405. fi
  406. else
  407. xsourceballs="$srctar"
  408. fi
  409. else
  410. sourceballs=$( echo "$desc_D" | tr ' ' '\t' | tr -s '\t' | \
  411. cut -f2 | sed 's,.\(t\?\)gz$,.\1bz2,' )
  412. xsrcpattern=$( echo "$desc_SRC" | tr ' ' '\t' | tr -s '\t' | tr '\t' '\n' )
  413. xsourceballs=$( echo "$sourceballs" | grep -F "$xsrcpattern" )
  414. fi
  415. for xsrctar in $xsourceballs; do
  416. if [ $autoextract = 1 ]; then
  417. echo "Extracting $xsrctar ($taropt) ... "
  418. cd $builddir
  419. tar -v $taropt $archdir/$xsrctar |
  420. cut -f1 -d/ | sort -u > xsrcdir.txt
  421. #
  422. if [ $srcdir = auto ]; then
  423. xsrcdir=${xsrctar%.tar.bz2}
  424. xsrcdir=${xsrcdir%.tbz2}
  425. if [ ! -d $xsrcdir ] ; then
  426. for x in $pkg-$ver $pkg_$ver $pkg \
  427. "`cat xsrcdir.txt`" ; do
  428. [ -d "$x" ] && xsrcdir="$x"
  429. done
  430. fi
  431. else
  432. xsrcdir="$srcdir"
  433. fi
  434. #
  435. echo "Changeing into $builddir/$xsrcdir ..."
  436. [ "$chownsrcdir" = 1 ] && chown -R 0.0 $builddir/$xsrcdir
  437. cd $builddir/$xsrcdir
  438. # Apply patches
  439. #
  440. if [ $autopatch = 1 ]; then
  441. hook_eval prepatch
  442. apply_patchfiles
  443. hook_eval postpatch
  444. fi
  445. else
  446. cd $builddir
  447. fi
  448. if [ "$createprefix" = 1 ]; then
  449. echo "Creating $root/$prefix/<..> if required ..."
  450. for x in $docdir $root/$prefix/bin $root/$prefix/lib \
  451. $root/$prefix/share $sysconfdir $localstatedir
  452. do
  453. if [ ! -e $x ]; then
  454. mkdir -p $x
  455. rmemptydir="$rmemptydir $x"
  456. fi
  457. done
  458. fi
  459. if [ ".$custmain" = "." ]
  460. then
  461. # Run configure scripts etc.
  462. #
  463. hook_eval preconf
  464. #
  465. if [ $runconf = 1 ]; then
  466. if [ -f $configscript -o $autogen = 1 ]
  467. then
  468. eval_config_command $( eval echo $confopt )
  469. fi
  470. fi
  471. #
  472. if [ ! -f Makefile -a ! -f makefile -a \
  473. -f Makefile.PL -a $runmkpl = 1 ]; then
  474. perl Makefile.PL
  475. fi
  476. #
  477. if [ ! -f Makefile -a ! -f makefile -a \
  478. -f Imakefile -a $runxmkmf = 1 ]; then
  479. xmkmf -a
  480. fi
  481. # Build it
  482. #
  483. hook_eval premake
  484. if [ "$makeopt" ]; then eval "$MAKE $makeopt"; fi
  485. hook_eval inmake
  486. if [ "$makeinstopt" ]; then eval "$MAKE $makeinstopt"; fi
  487. hook_eval postmake
  488. else
  489. eval "$custmain"
  490. for x in preconf premake inmake postmake; do
  491. if eval "[ -n \"\$hookdirty_$x\" ]"; then
  492. echo "Hook $x is still marked as dirty ..."
  493. hook_eval $x
  494. fi
  495. done
  496. fi
  497. if [ "$createdocs" != 0 ]; then
  498. if [ ! -e $docdir ]; then
  499. mkdir -p $docdir
  500. rmemptydir="$rmemptydir $docdir"
  501. fi
  502. [ -z "$createdocs" ] && createdocs="$ROCKCFG_CREATE_DOCS"
  503. fi
  504. for file in $confdir/*.desktop ; do
  505. [ -f $file ] || continue
  506. echo -n "Install desktop file '$file': "
  507. mkdir -p $root/usr/share/rock-registry/app
  508. rock_substitute $file > \
  509. $root/usr/share/rock-registry/app/`basename $file`
  510. done
  511. if [ "$createdocs" = 1 ]; then
  512. echo "Trying to copy the default documentation ..."
  513. [ -d $builddir/$xsrcdir ] && cd $builddir/$xsrcdir
  514. for x in `find -type d \( -name 'doc' -o -name 'docs' -o \
  515. -name '[Dd]ocumentation' \) ! -empty`
  516. do
  517. if [ -d "$x" -a "`echo $x/*`" != "$x/*" ]
  518. then cp -rLv $x/* $docdir || true ; fi
  519. done
  520. for x in [A-Z][A-Z]* *.lsm ChangeLog*; do
  521. [ "${x#*.[cho0-9]}" ] || continue
  522. [ "${x#*.info*}" ] || continue
  523. if [ -f $x ] ; then cp -v $x $docdir/$x ; fi
  524. done
  525. for x in $confdir/*.doc; do
  526. if [ -f $x ]; then cp -v $x $docdir/${x%.doc}; fi
  527. done
  528. find $docdir/ -name '*.[0-9]' -o -name '*.info*' \
  529. -o -name '[Mm]akefile*' | \
  530. xargs -r rm -f 2> /dev/null || true
  531. find $docdir/* -type d -empty 2> /dev/null | \
  532. xargs -r rmdir 2> /dev/null || true
  533. fi
  534. hook_eval postdoc
  535. done
  536. if [ "$rmemptydir" ]; then
  537. rmdir $rmemptydir 2> /dev/null || true
  538. fi
  539. return 0
  540. }