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.

429 lines
13 KiB

  1. #!/bin/bash
  2. #
  3. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  4. #
  5. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  6. # Please add additional copyright information _after_ the line containing
  7. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  8. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  9. #
  10. # ROCK Linux: rock-src/scripts/Build-Target
  11. # ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # (at your option) any later version. A copy of the GNU General Public
  17. # License can be found at Documentation/COPYING.
  18. #
  19. # Many people helped and are helping developing ROCK Linux. Please
  20. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  21. # file for details.
  22. #
  23. # --- ROCK-COPYRIGHT-NOTE-END ---
  24. #
  25. # Run this command from the ROCK directory as ./scripts/Build-Target
  26. # after running the ./scripts/Config and ./scripts/Download commands.
  27. #
  28. # It compiles/builds all the packages and stores them as tar balls suitable
  29. # for distribution.
  30. #
  31. # This script is the ROCK work-horse. It builds in a chroot environment
  32. # (stage 2..9) and goes through a number of build stages:
  33. #
  34. config=default
  35. build_only_this_job=
  36. stages=0123456789
  37. daemon_mode=0
  38. options="$*"
  39. while [ "$1" ] ; do
  40. case "$1" in
  41. -cfg) config=$2 ; shift ; shift ;;
  42. -stages) stages=$2 ; shift ; shift ;;
  43. -job) build_only_this_job=$2 ; shift ; shift ;;
  44. -daemon) daemon_mode=1 ; shift ;;
  45. -nodaemon) daemon_mode=0 ; shift ;;
  46. *) echo "Usage: $0 [ -daemon ] [ -cfg <config> ]" \
  47. "[ -stages <list> ] [ -job <stage>-<package> ]"
  48. echo
  49. echo " Compile/build all packages for a given configuration and store them as tar"
  50. echo " balls suitable for distribution."
  51. echo " This script is the ROCK work-horse. It builds in a chroot environment"
  52. echo " (stage 2..9) and goes through a number of build stages."
  53. echo " Run this command from the ROCK directory as ./scripts/Build-Target after"
  54. echo " running the ./scripts/Config and ./scripts/Download commands."
  55. echo
  56. echo " -cfg <config> the build configuration to use; defaults to 'default'"
  57. echo " -daemon run as daemon in the background"
  58. echo " -stages <list> only build the stages listed in the parameter"
  59. echo " -job <stage>-<package> build only one job: the given package"
  60. echo " in the given stage"
  61. exit 1 ;;
  62. esac
  63. done
  64. if [ "$daemon_mode" = 1 ] ; then
  65. . config/$config/config
  66. echo "Running $0 in the background (messages go to logfile only).."
  67. echo "Logfile: build/$ROCKCFG_ID/ROCK/logs/build_target.log"
  68. nohup $0 $options -nodaemon > /dev/null 2> /dev/null < /dev/null &
  69. exit 0
  70. fi
  71. . scripts/parse-config
  72. . scripts/functions
  73. # so we can disable stuff not available in Build-Target
  74. export ROCK_BUILD_TARGET=1
  75. build_root="$base/build/$ROCKCFG_ID"
  76. build_rock="$base/build/$ROCKCFG_ID/ROCK"
  77. build_logs="$build_rock/logs" ; mkdir -p "${build_logs}"
  78. build_pkgs="$build_rock/pkgs" ; mkdir -p "${build_root}"
  79. if [ "$ROCKCFG_PARANOIA_CHECK" = 1 ] ; then
  80. ./scripts/Check-System || exit 1
  81. fi
  82. # Package Build loop - executed by build-target
  83. #
  84. pkgloop() {
  85. if [ "$ROCKCFG_NOBROKENDEPS" = 1 ]; then
  86. nobrokendeps="-nobrokendeps"
  87. else
  88. nobrokendeps=""
  89. fi
  90. if [ "$ROCKCFG_RETRY_BROKEN" -eq 1 -a \
  91. -z "$build_only_this_job" -a \
  92. "`ls ${build_root}/var/adm/logs/*.err 2> /dev/null`" ] ; then
  93. echo_header "Removing old error logs ..."
  94. for x in ${build_root}/var/adm/logs/*.err ; do
  95. echo_status "Removing ${x#$build_root/} ..."
  96. rm -f $x
  97. done
  98. fi
  99. if [ -z "$build_only_this_job" -a \
  100. "`ls ${build_root}/var/adm/logs/*.out 2> /dev/null`" ] ; then
  101. echo_header "Removing old output logs ..."
  102. for x in ${build_root}/var/adm/logs/*.out ; do
  103. echo_status "Removing ${x#$build_root/} ..."
  104. rm -f $x
  105. done
  106. fi
  107. if [ "$ROCKCFG_PARALLEL" = 1 -a -z "$build_only_this_job" ]
  108. then
  109. qdir="$base/build/$ROCKCFG_ID/ROCK/queue"
  110. mkdir -p $qdir
  111. if [ -z "$ROCKCFG_PARALLEL_ADDJOB" ] ; then
  112. echo "
  113. Job Queue for parallel (cluster) build created.
  114. You have not configuread a command for adding jobs. So you need to use the
  115. ROCK Linux built-in job queue. To do so, you need to login to your build
  116. nodes and execute the command:
  117. ./scripts/Build-Job -cfg $config -daemon
  118. "
  119. touch $qdir/use_build_job_daemon
  120. else
  121. echo "
  122. Job Queue for parallel (cluster) build created.
  123. You have configuread a command for adding jobs. So if everything is configured
  124. in the right way, the nodes will automatically start building the jobs.
  125. "
  126. rm -f $qdir/use_build_job_daemon
  127. fi
  128. newqueue=1 ; printstatus=1 ; printstatus_counter=30
  129. finished=0 ; trap 'rm -f $qdir/queue.txt ; exit 130' SIGINT
  130. while
  131. if [ -f $qdir/print_status ] ; then
  132. rm -f $qdir/print_status
  133. printstatus=1 ; printstatus_counter=0
  134. fi
  135. if [ "$printstatus" = 1 ] ; then
  136. if [ $printstatus_counter -le 0 ] ; then
  137. ./scripts/Create-ParaStatus -cfg "$config"
  138. printstatus=0 ; printstatus_counter=300
  139. else
  140. printstatus_counter=$(($printstatus_counter - 1))
  141. fi
  142. fi
  143. for x in $qdir/*.msg ; do
  144. if [ -f "$x" ] ; then
  145. expand -t20 < $x ; rm -f $x
  146. printstatus=1 ; newqueue=1
  147. fi
  148. done
  149. if [ "$newqueue" = 1 ] ; then
  150. ./scripts/Create-PkgQueue \
  151. -cfg $config -stages $stages \
  152. $nobrokendeps | sort -r -n -k2 | \
  153. if [ $ROCKCFG_PARALLEL_MAX -gt 0 ]
  154. then head -n $ROCKCFG_PARALLEL_MAX
  155. else cat ; fi > $qdir/queue.new
  156. mv $qdir/queue.new $qdir/queue.txt
  157. newqueue=0
  158. while read next ; do
  159. set $next ; qid="$1-$6"
  160. if [ ! -f $qdir/$qid.job ] ; then
  161. date "+%H:%M %Y-%m-%d: creating new job '$qid'" | expand -t20
  162. echo "Job $qid waiting in the job queue (priority $2)" > $qdir/$qid.todo
  163. printstatus=1
  164. # we need to remove some variables that should be uniqe per node
  165. dump_env | grep -v HOSTNAME= > $qdir/$qid.new
  166. mv $qdir/$qid.new $qdir/$qid.job
  167. if [ "$ROCKCFG_PARALLEL_ADDJOB" ] ; then
  168. x="${ROCKCFG_PARALLEL_ADDJOB//\{\}/.\/scripts\/Build-Job -cfg $config $qid}" ; sh -c "$x"
  169. fi
  170. fi
  171. done < $qdir/queue.txt
  172. fi
  173. if [ -s $qdir/queue.txt ] || \
  174. [ "`ls $qdir/*.job 2>/dev/null`" ]
  175. then
  176. finished=0
  177. else
  178. finished=$(( $finished + 1 ))
  179. newqueue=1
  180. fi
  181. [ $finished -le 3 ]
  182. do
  183. sleep 1
  184. done
  185. echo "All packages build."
  186. sleep 2 ; rm -rf $qdir ; trap - SIGINT
  187. else
  188. if [ "$build_only_this_job" ] ; then
  189. rm -f "${build_root}"/var/adm/logs/${build_only_this_job}.log"
  190. rm -f "${build_root}"/var/adm/logs/${build_only_this_job}.err"
  191. next="$( awk 'BEGIN { FS=" "; }
  192. $0 ~ /[ =]'${build_only_this_job#*-}' / && \
  193. $2 ~ /'${build_only_this_job%%-*}'/ \
  194. { $1="'${build_only_this_job%%-*}' 0";
  195. print; exit; }' < config/$config/packages )"
  196. [ "$next" ] && pkgloop_package $next
  197. exit 0
  198. else
  199. while
  200. next="`./scripts/Create-PkgQueue \
  201. -cfg "$config" -stages $stages -single $nobrokendeps`"
  202. [ "$next" ]
  203. do
  204. pkgloop_package $next
  205. done
  206. fi
  207. fi
  208. local pkglst=`mktemp` errors=0; rm -f src/invalid-files.lst
  209. echo_header "Searching for old liggering files ..."
  210. grep "^X" config/$config/packages | cut -d' ' -f5 | sed 's,.*=,,' |
  211. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then
  212. while read p; do
  213. v=$( grep -h '^Package Name and Version:' build/$ROCKCFG_ID/var/adm/packages/$p:* \
  214. build/$ROCKCFG_ID/var/adm/packages/$p 2> /dev/null | cut -f6 -d' ' | head -n1 )
  215. echo "$p-$v"
  216. done
  217. else
  218. cat
  219. fi > $pkglst
  220. for file in $( ls build/$ROCKCFG_ID/ROCK/pkgs/ ) ; do
  221. x="$file"; [ "$x" = packages.db ] && continue
  222. [ $ROCKCFG_CREATE_GEM = 0 ] || x=${x%.gem}
  223. [ $ROCKCFG_CREATE_TARBZ2 = 0 ] || x=${x%.tar.bz2}
  224. y=$( echo $x | sed 's,:[^-]*,,' )
  225. if ! grep -qx "$y" $pkglst; then
  226. file="build/$ROCKCFG_ID/ROCK/pkgs/$file"
  227. echo_error "$file [$x $y] should not be present" \
  228. "(now in src/invalid-files.lst)!"
  229. mkdir -p src; echo "$file" >> src/invalid-files.lst
  230. errors=1
  231. fi
  232. done
  233. for dir in build/$ROCKCFG_ID/var/adm/{cache,cksums,dependencies,descs,flists,md5sums,packages} ; do
  234. for file in $( ls $dir | cut -f1 -d: ) ; do
  235. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then x="$file-.*"; else x="$file"; fi
  236. if ! grep -xq "$x" $pkglst ; then
  237. echo_error "$dir/$file should not be present (now in src/invalid-files.lst)!"
  238. mkdir -p src; echo "$dir/$file" >> src/invalid-files.lst
  239. errors=1
  240. fi
  241. done
  242. done
  243. for file in $( ls build/$ROCKCFG_ID/var/adm/logs/ ) ; do
  244. x="`echo $file | sed -e 's/^.-//' -e 's/\.log//' -e 's/\.err//' -e s'/\.out//'`"
  245. if [ $ROCKCFG_PKGFILE_VER = 1 ] ; then x="$x-.*"; else x="$x"; fi
  246. if ! grep -xq "$x" $pkglst ; then
  247. file="build/$ROCKCFG_ID/var/adm/logs/$file"
  248. echo_error "$file should not be present (now in src/invalid-files.lst)!"
  249. mkdir -p src; echo "$file" >> src/invalid-files.lst
  250. errors=1
  251. fi
  252. done
  253. [ $errors = 0 ] && echo_status "No errors found."
  254. rm $pkglst
  255. }
  256. # Process one line of output generated by Create-PkgQueue
  257. #
  258. pkgloop_package() {
  259. for x in stagelevel pkg_depnr pkg_stages pkg_pri pkg_tree \
  260. pkg_name pkg_ver pkg_prefix pkg_extra
  261. do eval "$x=\$1" ; shift ; done
  262. # Maybe this is a forked package
  263. pkg_basename="${pkg_name%=*}"
  264. pkg_name="${pkg_name#*=}"
  265. [ "$build_only_this_job" -a \
  266. "$stagelevel-$pkg_name" != "$build_only_this_job" ] && return
  267. [ $(expr "$pkg_stages" : ".*$stagelevel.*") -eq 0 ] && return
  268. pkg_laststage=$(echo "$pkg_stages" | sed "s,-,,g; s,.*\(.\),\1,")
  269. cmd_root="-root auto"
  270. [ $stagelevel -gt 1 ] && cmd_root="$cmd_root -chroot"
  271. if [ "$pkg_prefix" != "/" ] ; then
  272. cmd_prefix="-prefix $pkg_prefix"
  273. else cmd_prefix="" ; fi
  274. cmd_buildpkg="./scripts/Build-Pkg -$stagelevel -cfg $config"
  275. cmd_buildpkg="$cmd_buildpkg $cmd_root $cmd_prefix $pkg_basename=$pkg_name"
  276. # Execute action handler
  277. pkgloop_action || [ "$ROCKCFG_ABORT_ON_ERROR" != 1 ] || exit 1
  278. if [ -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err -a \
  279. "$ROCKCFG_SENDMAIL" = 1 ]
  280. then
  281. {
  282. cat << EOT
  283. Subject: [ROCK Build-Target] $stagelevel-$pkg_name in $config failed
  284. Building package $pkg_name failed in stage $stagelevel:
  285. ----
  286. EOT
  287. echo; tail -n 200 ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err; echo
  288. } | $ROCKCFG_SENDMAIL_BIN $ROCKCFG_SENDMAIL_TO
  289. fi
  290. if [ ! -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.log -a \
  291. ! -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err ]
  292. then
  293. echo_header "Package build ended abnormally!"
  294. echo_error "Usually a package build creates eighter a *.log"
  295. echo_error "or a *.err file. Neither the 1st nor the 2nd is"
  296. echo_error "there. So I'm going to create a *.err file now"
  297. echo_error "and abort the build process."
  298. touch ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err
  299. exit 1
  300. fi
  301. if [ $pkg_laststage -eq $stagelevel ] && \
  302. [ "$ROCKCFG_CREATE_TARBZ2" = 1 -o "$ROCKCFG_CREATE_GEM" = 1 ]
  303. then
  304. if [ -f ${build_root}/var/adm/logs/$stagelevel-$pkg_name.err ]
  305. then echo_error "Creation of binary package isn't possible,"
  306. echo_error "because the package was not successfully"
  307. echo_error "built in (at least) the current stage."
  308. else
  309. for spkg in $( cd ${build_root}/var/adm/packages/; ls ${pkg_name} ${pkg_name}:* 2>/dev/null )
  310. do
  311. echo_header "Creating binary package file for ${spkg}."
  312. mkdir -p "${build_pkgs}"
  313. if [ "$ROCKCFG_PKGFILE_VER" = 1 ]
  314. then
  315. v="-$( grep '^Package Name and Version:' \
  316. ${build_root}/var/adm/packages/$spkg | cut -f6 -d' ' )"
  317. else
  318. v=""
  319. fi
  320. echo_status "Building build/.../pkgs/`
  321. `${spkg}${v}.tar.bz2"
  322. ( cd "$build_root/"
  323. cut -f2- -d' ' var/adm/flists/$spkg | \
  324. tar -cf- --no-recursion --files-from=- | bzip2
  325. ) > "${build_pkgs}/${spkg}${v}.tar.bz2.tmp"
  326. if [ "$ROCKCFG_CREATE_GEM" = 1 ] ; then
  327. echo_status "Building build/.../pkgs/`
  328. `${spkg}${v}.gem"
  329. mine -C "$build_root/var/adm" \
  330. "${build_pkgs}/${spkg}${v}.tar.bz2.tmp" \
  331. $spkg "$build_pkgs/${spkg}${v}.gem.tmp"
  332. fi
  333. if [ "$ROCKCFG_CREATE_TARBZ2" = 1 ] ; then
  334. mv "$build_pkgs/${spkg}${v}.tar.bz2.tmp" \
  335. "$build_pkgs/${spkg}${v}.tar.bz2"
  336. if [ "$ROCKCFG_CREATE_GEM" = 1 ] ; then
  337. mv "$build_pkgs/${spkg}${v}.gem.tmp" \
  338. "$build_pkgs/${spkg}${v}.gem"
  339. fi
  340. else
  341. echo_status "Removing temporary tar.bz2."
  342. rm -f "$build_pkgs/${spkg}${v}.tar.bz2.tmp"
  343. mv "$build_pkgs/${spkg}${v}.gem.tmp" \
  344. "$build_pkgs/${spkg}${v}.gem"
  345. fi
  346. done
  347. fi
  348. fi
  349. }
  350. # Action executed by pkgloop(). This function may be redefined
  351. # before calling pkgloop().
  352. #
  353. pkgloop_action() {
  354. $cmd_buildpkg
  355. }
  356. # Try to umount any directories mounted by Build-Pkg -chroot
  357. # if we are the last process using them.
  358. #
  359. umount_chroot() {
  360. exec 201> /dev/null
  361. if [ -z "$( fuser ${build_logs}/*.log )" ]; then
  362. umount -d -f $build_rock/{loop,config,download} 2> /dev/null
  363. umount -d -f -l $build_rock/{loop,config,download} 2> /dev/null
  364. umount -d -f $build_root/proc 2> /dev/null
  365. umount -d -f -l $build_root/proc 2> /dev/null
  366. fi
  367. }
  368. # must trap outside the group command
  369. trap 'umount_chroot' EXIT
  370. {
  371. ln -sf build_target_$$.log ${build_logs}/build_target.log
  372. ./scripts/Build-Tools -1 -cfg $config
  373. . ./target/$ROCKCFG_TARGET/build.sh
  374. } 2>&1 201>> "${build_logs}/build_target_$$.log" | \
  375. tee -a "${build_logs}/build_target_$$.log"
  376. if [ "$ROCKCFG_SENDMAIL" = 1 ]; then
  377. $ROCKCFG_SENDMAIL_BIN $ROCKCFG_SENDMAIL_TO << EOT
  378. Subject: [ROCK Build-Target] $config finished.
  379. Finished building $config.
  380. EOT
  381. fi