]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makepkg
script/makepkg, todo: todo maintenance
[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   ( cd /usr/ports/${port}
250     ( make build-depends-list
251       [ "${2:-0}" -gt 0 ] && make run-depends-list
252     ) | sort -u | while read port
253     do
254       bdep_tree "${port}" $(( ${2:-0} + 1 ))
255     done
256   )
257 }
258
259 # Display a tree of all runtime dependencies. Individual
260 # ports may appear more than once, as it is a tree and
261 # not a list.
262 port_rdep_tree() {
263   port="${1##/usr/ports/}"
264   printf "%${2}s%s\n" "" "${port}"
265   
266   ( cd /usr/ports/${port}
267     make run-depends-list | sort -u | while read port
268     do
269       rdep_tree "${port}" $(( ${2:-0} + 1 ))
270     done
271   )
272 }
273
274 ########
275 #
276 # Port Configuration Handling
277 #
278 ########
279
280 # Run make config on a list of ports
281 port_config() {
282   while [ "${1}" ]
283   do
284     meh "port config ${1}"
285     chmake "${1}" config < /dev/tty
286     shift
287   done
288 }
289
290 # Make config-conditional for a list of ports, and all dependencies
291 port_config_recursive() {
292   # Clear cache if first value isn't '-r'
293   [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
294   while [ "${1}" ]
295   do
296     # Do not use config-recursive because it computes the depchain first;
297     # instead, manually compute the depchain after each config to ensure the
298     # proper depchain is followed. Use config-conditional to avoid dialog
299     # if the config is already complete. Also use a cache to avoid re-config
300     # and re-recurse on previously handled port branches.
301     if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
302     then
303       meh "port config-recursive ${1}"
304       chmake "${1}" config-conditional < /dev/tty
305       port_config_recursive -r $(port_deps "${1}")
306       _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
307     fi
308     shift
309   done
310 }
311 # Config cache
312 unset _port_config_recursive_cache
313
314 # Remove saved config for a list of ports
315 port_rmconfig() {
316   while [ "${1}" ]
317   do
318     meh "port rmconfig ${1}"
319     chmake "${1}" rmconfig
320     shift
321   done
322 }
323
324 # Remove saved config for a list of ports and all dependencies
325 port_rmconfig_recursive() {
326   meh "port rmconfig-recursive ${*}"
327   port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
328 }
329
330 # Obliterate saved configuration for all ports
331 port_rmconfig_all() {
332   meh "port rmconfig-all"
333   cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
334 }
335
336 # Restore port build options from cpio
337 port_load_config() {
338   [ -f "${conf_dir}/port_options.cpio" ] || return 0
339   meh "port load-config"
340   cheval "cd /var/db/ports; cpio -i" < "${conf_dir}/port_options.cpio"
341 }
342
343 # Dump port build options to cpio
344 port_save_config() {
345   meh "port save-config"
346   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"
347   mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
348 }
349
350 ########
351 #
352 # Port distfile handling
353 #
354 ########
355
356 # Recursively retrieve distfiles
357 port_fetch_recursive() {
358   while [ "${1}" ]
359   do
360     meh "fetch-recursive ${1}"
361     chmake "${1}" fetch-recursive
362     shift
363   done
364 }
365
366 ########
367 #
368 # Port building and packaging
369 #
370 ########
371
372 # Copy in and install dependency packages
373 port_load_deps() {
374   local port="${1}"
375   for pkg in $(port2pkg $(port_all_deps "${port}"))
376   do
377     cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
378   done
379   if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
380   then
381     meh "Installing dependencies"
382     cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
383   fi
384 }
385
386 # Build and install a port
387 port_build() {
388   local port="${1}"
389   meh "Building ${port}"
390   chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
391 }
392
393 # Package a port
394 port_package() {
395   local port="${1}"
396   meh "Creating rdep package tree for ${port}"
397   cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
398 }
399
400 # Package port build dependencies, unless they're already run deps
401 port_stash_bdeps() {
402   meh "Stashing unsaved bdeps"
403   # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
404   #for pkg in $(pkg_bdeps "${1}")
405   #do
406   #  [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
407   #done
408   #
409   # Instead, just save everything that's not already in rdeps as bdeps
410   for pkg in $(cheval pkg_info | awk '{print $1}')
411   do
412     if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
413     then
414       cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
415     fi
416   done
417 }
418
419 # Copy generated packages out of tree
420 pkg_final() {
421   meh "Moving created packages to repo"
422   mkdir -p "${final_pkg_dir}"
423   ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
424   mkdir -p "${final_bdeps_dir}"
425   ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
426   # link everything into ${bdeps_dir} so we can find it easily later
427   ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
428 }
429
430 # Delete all installed packages (hope you saved them first)
431 pkg_delete_all() {
432   meh "Clearing out installed packages"
433   cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
434 }
435
436 ########
437 #
438 # All of the above?
439 #
440 ########
441
442 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
443 # Be sure to chsetup and populate your config before running!
444 chport() {
445   local port="${1}"
446   meh "config-recursive"
447   port_config_recursive "${port}"
448   meh "fetch-recursive"
449   port_fetch_recursive "${port}"
450   meh "load-deps"
451   port_load_deps "${port}"
452   meh "build"
453   port_build "${port}"
454   meh "package"
455   port_package "${port}"
456   meh "stash-deps"
457   port_stash_bdeps
458   meh "final"
459   pkg_final
460   meh "delete-all"
461   pkg_delete_all
462 }
463
464
465 ########
466 #
467 # Configuration variable setup
468 #
469 ########
470
471 TARGET="${TARGET:-i386}"
472 CONFIG="${CONFIG:-GENERIC}"
473
474 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
475
476 # Base directory for everything
477 base_dir="${ROOT}/${TARGET}/${CONFIG}"
478
479 # Directory holding configuration
480 conf_dir="${base_dir}/config"
481
482 # Root tree for chroot seeding
483 seed_dir="${base_dir}/root"
484
485 # Directory where distfiles will be stored between builds (common to all configs)
486 dist_dir="${ROOT}/seed/distfiles"
487
488 # Final directory for built packages (Outside chroot)
489 final_pkg_dir="${base_dir}/pkg"
490 final_bdeps_dir="${base_dir}/bdeps"
491
492 # Chroot directory
493 chroot_dir="${base_dir}/chroot"
494
495 # Package directories (must be under ${chroot_dir})
496 pkg_dir="${chroot_dir}/pkg"
497 bdeps_dir="${pkg_dir}/bdeps"
498
499 # Compute in-chroot pkg and bdeps dirs
500 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
501 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
502
503 # Cache files to speed up recursive bdep/rdep scanning
504 _port_all_bdeps_cache="${chroot_dir}/tmp/_port_all_bdeps_cache"
505 _port_all_rdeps_cache="${chroot_dir}/tmp/_port_all_rdeps_cache"
506
507 # Chroot environment
508 chroot_env="
509 USER=root
510 HOME=/root
511 LOGNAME=root
512 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
513 SHELL=/bin/sh
514 TERM=${TERM}
515 "
516
517 # Blind passthru for testing
518 [ "${#}" ] && "${@}"
519
520 # Todo: Clean up entrypoint to support proper options
521 # Todo: Add methods to autoprocess a ports.lst file to produce packages
522 # Todo: Unify chroot handling with makeworld