]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makepkg
script/makepkg: make port_bdep_tree and port_rdep_tree use chroot
[CDN/Mosi.git] / script / makepkg
1 #!/bin/sh
2
3 # Load shlib and modules
4 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
5 want log root
6
7 ########
8 #
9 # Chroot handling
10 #
11 ########
12
13 # Prepare a chroot for work
14 chprepare() {
15   # Verify environment sanity
16   [ -d "${chroot_dir}" ] && omg "${chroot_dir}: directory exists; purging" && chdestroy
17   mount | grep -q "${chroot_dir}" && wtf "Stuff is mounted under ${chroot_dir}; cannot continue"
18   [ -d "${seed_dir}" -a -f "${seed_dir}/COPYRIGHT" ] || wtf "Use 'makeworld' to create a root build first"
19   [ -f "/usr/ports/UPDATING" ] || wtf "Need ports tree in /usr/ports to build"
20   [ -f "/usr/src/sys/conf/newvers.sh" ] || omg "No base sourcetree in /usr/src"
21
22   meh "Setting up chroot tree"
23
24   # Just in case we're aborted partway through the prepare
25   trap "chdestroy" exit hup int term kill
26
27   # Populate initial seed
28   mkdir -p "$(dirname "${chroot_dir}")" || wtf "chroot path create failed"
29   cp -pR "${seed_dir}" "${chroot_dir}" || wtf "chroot seeding failed"
30
31   # Create distfile cache
32   mkdir -p "${dist_dir}" || wtf "mkdir ${dist_dir} failed"
33
34   # Create mountpoint directories
35   mkdir -p "${chroot_dir}/dev" || wtf "mkdir /dev failed"
36   mkdir -p "${chroot_dir}/usr/src" || wtf "mkdir /usr/src failed"
37   mkdir -p "${chroot_dir}/usr/obj" || wtf "mkdir /usr/obj failed"
38   mkdir -p "${chroot_dir}/usr/ports" || wtf "mkdir /usr/ports failed"
39   mkdir -p "${chroot_dir}/var/ports/distfiles" || wtf "mkdir /var/ports/distfiles failed"
40
41   # Create pkg directories
42   mkdir -p "${pkg_dir}" || wtf "mkdir ${chroot_pkg_dir} failed"
43   mkdir -p "${bdeps_dir}" || wtf "mkdir ${chroot_bdeps_dir} failed"
44
45   # Copy in cached configuration, if necessary
46   [ -f "${conf_dir}/make.conf" ] && cp -p "${conf_dir}/make.conf" "${chroot_dir}/etc/make.conf"
47   [ -f "${conf_dir}/port_options.cpio" ] && ( cd ${chroot_dir}/var/db/ports; cpio -i ) < "${conf_dir}/port_options.cpio"
48
49   # If you have WITHOUT_INFO set in src.conf during buildworld/installworld, ports that need
50   # makeinfo and install-info will fail horribly; stub them
51   [ -f "${chroot_dir}/usr/bin/makeinfo" ] || ln -sf true "${chroot_dir}/usr/bin/makeinfo"
52   [ -f "${chroot_dir}/usr/bin/install-info" ] || ln -sf true "${chroot_dir}/usr/bin/install-info"
53
54   trap "" exit hup int term kill
55   meh "Chroot tree set up in ${chroot_dir}"
56
57   return 0
58 }
59
60 # Set up chroot mounts and runtime configuration
61 chstartup() {
62   [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -d "${chroot_dir}/dev" ] || wtf "Chroot not prepared"
63   chup? && return 0
64
65   # Rollback if a problem occurs during startup
66   trap "chshutdown" exit hup int term kill
67
68   meh "Starting up chroot"
69
70   # Necessary mountpoints
71   mount -t devfs devfs "${chroot_dir}/dev" || wtf "mount /dev failed"
72   mount -t nullfs -r /usr/src "${chroot_dir}/usr/src" || wtf "mount /usr/src failed"
73   mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || wtf "mount /usr/ports failed"
74   mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || wtf "mount /var/ports/distfiles failed"
75
76   # Chroot configuration
77   cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || wtf "seeding /etc/resolv.conf failed"
78
79   trap "" exit hup int term kill
80   meh "Chroot up and running in ${chroot_dir}"
81
82   return 0
83 }
84
85 # Check if the chroot is probably ready for use
86 chup?() {
87   [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -c "${chroot_dir}/dev/null" ]
88   return $?
89 }
90
91 # Unmount all chroot directories
92 chshutdown() {
93   # Short-circuit if nothing is mounted
94   mount | grep -q "${chroot_dir}" || return 0
95   meh "Shutting down chroot"
96   umount "${chroot_dir}/var/ports/distfiles"
97   umount "${chroot_dir}/usr/ports"
98   umount "${chroot_dir}/usr/src"
99   umount "${chroot_dir}/dev"
100   return 0
101 }
102
103 # Remove all chroot contents
104 chdestroy() {
105   chshutdown
106   meh "Destroying chroot"
107   chflags -R noschg "${chroot_dir}" || wtf "noschg failed"
108   rm -Rf "${chroot_dir}" || wtf "chroot removal failed"
109   return 0
110 }
111
112 # Evaluate a command line within the chroot
113 cheval() {
114   chup? || wtf "Chroot not ready"
115   chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
116   return $?
117 }
118
119 # Run chrooted make
120 chmake() {
121   local port="/usr/ports/${1##/usr/ports/}"
122   shift
123   cheval "make -C ${port} ${*}"
124   return $?
125 }
126
127 ########
128 #
129 # Port Dependency Tracking
130 #
131 ########
132
133 # Translate port origin to package name
134 port2pkg() {
135   while [ "${1}" ]
136   do
137     chmake "${1}" -V PKGNAME
138     shift
139   done
140 }
141
142 # Build dependencies (shallow, recursive, package)
143 port_bdeps() {
144   while [ "${1}" ]
145   do
146     chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
147     shift
148   done
149 }
150 port_all_bdeps() {
151   # Clear cache if first value isn't '-r'
152   [ "${1}" = '-r' ] && shift || { : > "${_port_all_bdeps_cache}"; : > "${_port_all_rdeps_cache}"; }
153   # rdeps for rdeps are rdeps, bdeps for rdeps are bdeps, rdeps for bdeps are bdeps; thus:
154   (
155     port_bdeps "${@}" | while read port
156     do
157       [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(grep -q "${port}" \
158         "${_port_all_bdeps_cache}" && echo "hit" || echo "miss")"  "${port}" >&2
159       if ! grep -q "${port}" "${_port_all_bdeps_cache}"
160       then
161         echo "${port}" >> "${_port_all_bdeps_cache}"
162         echo "${port}"
163         port_all_bdeps -r "${port}"
164         port_all_rdeps -r "${port}"
165       fi
166     done
167     port_all_rdeps -r "${@}" | while read port
168     do
169       [ "${VERBOSE_CACHE}" ] && logf "**** bdep cache %s: '%s'\n" "$(grep -q "${port}" \
170         "${_port_all_bdeps_cache}" && echo "hit" || echo "miss")"  "${port}" >&2
171       if ! grep -q "${port}" "${_port_all_bdeps_cache}"
172       then
173         echo "${port}" >> "${_port_all_bdeps_cache}"
174         port_all_bdeps -r "${port}"
175       fi
176     done
177   ) | sort | uniq
178 }
179 pkg_bdeps() {
180   port2pkg $(port_all_bdeps "${@}")
181 }
182
183 # Runtime dependencies (shallow, recursive, package)
184 port_rdeps() {
185   while [ "${1}" ]
186   do
187     chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
188     shift
189   done
190 }
191 port_all_rdeps() {
192   # Clear cache if first value isn't '-r'
193   [ "${1}" = '-r' ] && shift || { : > "${_port_all_rdeps_cache}"; }
194   port_rdeps "${@}" | while read port
195   do
196     [ "${VERBOSE_CACHE}" ] && logf "**** rdep cache %s: '%s'\n" "$(grep -q "${port}" \
197       "${_port_all_rdeps_cache}" && echo "hit" || echo "miss")"  "${port}" >&2
198     if ! grep -q "${port}" "${_port_all_rdeps_cache}"
199     then
200       echo "${port}" >> "${_port_all_rdeps_cache}"
201       echo "${port}"
202       port_all_rdeps -r "${port}"
203     fi
204   done | sort | uniq
205 }
206 pkg_rdeps() {
207   port2pkg $(port_all_rdeps "${@}")
208 }
209
210 # All dependencies (shallow, recursive, package)
211 port_deps() {
212   while [ "${1}" ]
213   do
214     chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
215     shift
216   done
217 }
218 port_all_deps() {
219   port_deps "${@}" | while read port
220   do
221     echo "${port}"
222     port_deps "${port}"
223   done | sort | uniq
224 }
225 pkg_deps() {
226   port2pkg $(port_all_deps "${@}")
227 }
228
229 # Dump a list of leaf ports from a working system
230 # Leaf ports are ports that have nothing depending upon them
231 # These are generally the top-level ports; everything else should
232 # be pulled in by them; on a system that has had updates applied
233 # this may pick up orphaned packages as well, so try cutleaves?
234 leaf_ports() {
235   ( cd /var/db/pkg; ls -1 ) | while read pkg
236   do
237     pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
238   done | sort
239 }
240
241 # Display a tree of all build dependencies (and
242 # any runtime dependencies those build dependencies
243 # depend upon). Individual ports may appear more than
244 # once, as it is a tree and not a list.
245 port_bdep_tree() {
246   port="${1##/usr/ports/}"
247   printf "%${2}s%s\n" "" "${port}"
248   
249   ( chmake "${port}" build-depends-list
250     [ "${2:-0}" -gt 0 ] && chmake "${port}" run-depends-list
251   ) | sort -u | while read port
252   do
253     port_bdep_tree "${port}" $(( ${2:-0} + 1 ))
254   done
255 }
256
257 # Display a tree of all runtime dependencies. Individual
258 # ports may appear more than once, as it is a tree and
259 # not a list.
260 port_rdep_tree() {
261   port="${1##/usr/ports/}"
262   printf "%${2}s%s\n" "" "${port}"
263   
264   chmake "${port}" run-depends-list | sort -u | while read port
265   do
266     port_rdep_tree "${port}" $(( ${2:-0} + 1 ))
267   done
268 }
269
270 ########
271 #
272 # Port Configuration Handling
273 #
274 ########
275
276 # Run make config on a list of ports
277 port_config() {
278   while [ "${1}" ]
279   do
280     meh "port config ${1}"
281     chmake "${1}" config < /dev/tty
282     shift
283   done
284 }
285
286 # Make config-conditional for a list of ports, and all dependencies
287 port_config_recursive() {
288   # Clear cache if first value isn't '-r'
289   [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
290   while [ "${1}" ]
291   do
292     # Do not use config-recursive because it computes the depchain first;
293     # instead, manually compute the depchain after each config to ensure the
294     # proper depchain is followed. Use config-conditional to avoid dialog
295     # if the config is already complete. Also use a cache to avoid re-config
296     # and re-recurse on previously handled port branches.
297     if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
298     then
299       meh "port config-recursive ${1}"
300       chmake "${1}" config-conditional < /dev/tty
301       port_config_recursive -r $(port_deps "${1}")
302       _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
303     fi
304     shift
305   done
306 }
307 # Config cache
308 unset _port_config_recursive_cache
309
310 # Remove saved config for a list of ports
311 port_rmconfig() {
312   while [ "${1}" ]
313   do
314     meh "port rmconfig ${1}"
315     chmake "${1}" rmconfig
316     shift
317   done
318 }
319
320 # Remove saved config for a list of ports and all dependencies
321 port_rmconfig_recursive() {
322   meh "port rmconfig-recursive ${*}"
323   port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
324 }
325
326 # Obliterate saved configuration for all ports
327 port_rmconfig_all() {
328   meh "port rmconfig-all"
329   cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
330 }
331
332 # Restore port build options from cpio
333 port_load_config() {
334   [ -f "${conf_dir}/port_options.cpio" ] || return 0
335   meh "port load-config"
336   cheval "cd /var/db/ports; cpio -i" < "${conf_dir}/port_options.cpio"
337 }
338
339 # Dump port build options to cpio
340 port_save_config() {
341   meh "port save-config"
342   cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -oHnewc" > "${conf_dir}/port_options.cpio.tmp" || wtf "port save-config failed"
343   mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
344 }
345
346 ########
347 #
348 # Port distfile handling
349 #
350 ########
351
352 # Recursively retrieve distfiles
353 port_fetch_recursive() {
354   while [ "${1}" ]
355   do
356     meh "fetch-recursive ${1}"
357     chmake "${1}" fetch-recursive
358     shift
359   done
360 }
361
362 ########
363 #
364 # Port building and packaging
365 #
366 ########
367
368 # Copy in and install dependency packages
369 port_load_deps() {
370   local port="${1}"
371   for pkg in $(port2pkg $(port_all_deps "${port}"))
372   do
373     cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
374   done
375   if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
376   then
377     meh "Installing dependencies"
378     cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
379   fi
380 }
381
382 # Build and install a port
383 port_build() {
384   local port="${1}"
385   meh "Building ${port}"
386   chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
387 }
388
389 # Package a port
390 port_package() {
391   local port="${1}"
392   meh "Creating rdep package tree for ${port}"
393   cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
394 }
395
396 # Package port build dependencies, unless they're already run deps
397 port_stash_bdeps() {
398   meh "Stashing unsaved bdeps"
399   # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
400   #for pkg in $(pkg_bdeps "${1}")
401   #do
402   #  [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
403   #done
404   #
405   # Instead, just save everything that's not already in rdeps as bdeps
406   for pkg in $(cheval pkg_info | awk '{print $1}')
407   do
408     if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
409     then
410       cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
411     fi
412   done
413 }
414
415 # Copy generated packages out of tree
416 pkg_final() {
417   meh "Moving created packages to repo"
418   mkdir -p "${final_pkg_dir}"
419   ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
420   mkdir -p "${final_bdeps_dir}"
421   ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
422   # link everything into ${bdeps_dir} so we can find it easily later
423   ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
424 }
425
426 # Delete all installed packages (hope you saved them first)
427 pkg_delete_all() {
428   meh "Clearing out installed packages"
429   cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
430 }
431
432 ########
433 #
434 # All of the above?
435 #
436 ########
437
438 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
439 # Be sure to chsetup and populate your config before running!
440 chport() {
441   local port="${1}"
442   meh "config-recursive"
443   port_config_recursive "${port}"
444   meh "fetch-recursive"
445   port_fetch_recursive "${port}"
446   meh "load-deps"
447   port_load_deps "${port}"
448   meh "build"
449   port_build "${port}"
450   meh "package"
451   port_package "${port}"
452   meh "stash-deps"
453   port_stash_bdeps
454   meh "final"
455   pkg_final
456   meh "delete-all"
457   pkg_delete_all
458 }
459
460
461 ########
462 #
463 # Configuration variable setup
464 #
465 ########
466
467 TARGET="${TARGET:-i386}"
468 CONFIG="${CONFIG:-GENERIC}"
469
470 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
471
472 # Base directory for everything
473 base_dir="${ROOT}/${TARGET}/${CONFIG}"
474
475 # Directory holding configuration
476 conf_dir="${base_dir}/config"
477
478 # Root tree for chroot seeding
479 seed_dir="${base_dir}/root"
480
481 # Directory where distfiles will be stored between builds (common to all configs)
482 dist_dir="${ROOT}/seed/distfiles"
483
484 # Final directory for built packages (Outside chroot)
485 final_pkg_dir="${base_dir}/pkg"
486 final_bdeps_dir="${base_dir}/bdeps"
487
488 # Chroot directory
489 chroot_dir="${base_dir}/chroot"
490
491 # Package directories (must be under ${chroot_dir})
492 pkg_dir="${chroot_dir}/pkg"
493 bdeps_dir="${pkg_dir}/bdeps"
494
495 # Compute in-chroot pkg and bdeps dirs
496 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
497 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
498
499 # Cache files to speed up recursive bdep/rdep scanning
500 _port_all_bdeps_cache="${chroot_dir}/tmp/_port_all_bdeps_cache"
501 _port_all_rdeps_cache="${chroot_dir}/tmp/_port_all_rdeps_cache"
502
503 # Chroot environment
504 chroot_env="
505 USER=root
506 HOME=/root
507 LOGNAME=root
508 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
509 SHELL=/bin/sh
510 TERM=${TERM}
511 "
512
513 # Blind passthru for testing
514 [ "${#}" ] && "${@}"
515
516 # Todo: Clean up entrypoint to support proper options
517 # Todo: Add methods to autoprocess a ports.lst file to produce packages
518 # Todo: Unify chroot handling with makeworld