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