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