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