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