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.

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