]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makepkg
script/makepkg: add chup? check to flag when chroot is ready for use
[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 -iv ) < "${conf_dir}/port_options.cpio"
59
60   trap "" exit hup int term kill
61   meh "Chroot tree set up in ${chroot_dir}"
62
63   return 0
64 }
65
66 # Set up chroot mounts and runtime configuration
67 chstartup() {
68   [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -d "${chroot_dir}/dev" ] || wtf "Chroot not prepared"
69   chup? && return 0
70
71   # Rollback if a problem occurs during startup
72   trap "chshutdown" exit hup int term kill
73
74   meh "Starting up chroot"
75
76   # Necessary mountpoints
77   mount -t devfs devfs "${chroot_dir}/dev" || wtf "mount /dev failed"
78   mount -t nullfs -r /usr/src "${chroot_dir}/usr/src" || wtf "mount /usr/src failed"
79   mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || wtf "mount /usr/ports failed"
80   mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || wtf "mount /var/ports/distfiles failed"
81
82   # Chroot configuration
83   cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || wtf "seeding /etc/resolv.conf failed"
84
85   trap "" exit hup int term kill
86   meh "Chroot up and running in ${chroot_dir}"
87
88   return 0
89 }
90
91 # Check if the chroot is probably ready for use
92 chup?() {
93   [ -d "${chroot_dir}" -a -f "${chroot_dir}/COPYRIGHT" -a -c "${chroot_dir}/dev/null" ]
94   return $?
95 }
96
97 # Unmount all chroot directories
98 chshutdown() {
99   # Short-circuit if nothing is mounted
100   mount | grep -q "${chroot_dir}" || return 0
101   meh "Shutting down chroot"
102   umount "${chroot_dir}/var/ports/distfiles"
103   umount "${chroot_dir}/usr/ports"
104   umount "${chroot_dir}/usr/src"
105   umount "${chroot_dir}/dev"
106   return 0
107 }
108
109 # Remove all chroot contents
110 chdestroy() {
111   chshutdown
112   meh "Destroying chroot"
113   chflags -R noschg "${chroot_dir}" || wtf "noschg failed"
114   rm -Rf "${chroot_dir}" || wtf "chroot removal failed"
115   return 0
116 }
117
118 # Evaluate a command line within the chroot
119 cheval() {
120   chup? || wtf "Chroot not ready"
121   chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
122   return $?
123 }
124
125 # Run chrooted make
126 chmake() {
127   local port="/usr/ports/${1##/usr/ports/}"
128   shift
129   cheval "make -C ${port} ${*}"
130   return $?
131 }
132
133 ########
134 #
135 # Port Dependency Tracking
136 #
137 ########
138
139 # Translate port origin to package name
140 port2pkg() {
141   while [ "${1}" ]
142   do
143     chmake "${1}" -V PKGNAME
144     shift
145   done
146 }
147
148 # Build dependencies (shallow, recursive, package)
149 port_bdeps() {
150   while [ "${1}" ]
151   do
152     chmake "${1}" build-depends-list | sed -e 's#^/usr/ports/##'
153     shift
154   done
155 }
156 port_all_bdeps() {
157   # rdeps for rdeps are rdeps, rdeps for bdeps are bdeps; thus:
158   ( port_bdeps "${@}"; port_all_rdeps "${@}" ) | while read port
159   do
160     port_all_bdeps "${port}"
161     port_all_rdeps "${port}"
162   done | sort | uniq
163 }
164 pkg_bdeps() {
165   port2pkg $(port_all_bdeps "${@}")
166 }
167
168 # Runtime dependencies (shallow, recursive, package)
169 port_rdeps() {
170   while [ "${1}" ]
171   do
172     chmake "${1}" run-depends-list | sed -e 's#^/usr/ports/##'
173     shift
174   done
175 }
176 port_all_rdeps() {
177   port_rdeps "${@}" | while read port
178   do
179     echo "${port}"
180     port_all_rdeps "${port}"
181   done | sort | uniq
182 }
183 pkg_rdeps() {
184   port2pkg $(port_all_rdeps "${@}")
185 }
186
187 # All dependencies (shallow, recursive, package)
188 port_deps() {
189   while [ "${1}" ]
190   do
191     chmake "${1}" all-depends-list | sed -e 's#^/usr/ports/##'
192     shift
193   done
194 }
195 port_all_deps() {
196   port_deps "${@}" | while read port
197   do
198     echo "${port}"
199     port_deps "${port}"
200   done | sort | uniq
201 }
202 pkg_deps() {
203   port2pkg $(port_all_deps "${@}")
204 }
205
206 ########
207 #
208 # Port Configuration Handling
209 #
210 ########
211
212 # Run make config on a list of ports
213 port_config() {
214   while [ "${1}" ]
215   do
216     meh "port config ${1}"
217     chmake "${1}" config < /dev/tty
218     shift
219   done
220 }
221
222 # Make config-conditional for a list of ports, and all dependencies
223 port_config_recursive() {
224   # Clear cache if first value isn't '-r'
225   [ "${1}" = '-r' ] && shift || _port_config_recursive_cache=""
226   while [ "${1}" ]
227   do
228     # Do not use config-recursive because it computes the depchain first;
229     # instead, manually compute the depchain after each config to ensure the
230     # proper depchain is followed. Use config-conditional to avoid dialog
231     # if the config is already complete. Also use a cache to avoid re-config
232     # and re-recurse on previously handled port branches.
233     if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
234     then
235       meh "port config-recursive ${1}"
236       chmake "${1}" config-conditional < /dev/tty
237       port_config_recursive -r $(port_deps "${1}")
238       _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
239     fi
240     shift
241   done
242 }
243 # Config cache
244 unset _port_config_recursive_cache
245
246 # Remove saved config for a list of ports
247 port_rmconfig() {
248   while [ "${1}" ]
249   do
250     meh "port rmconfig ${1}"
251     chmake "${1}" rmconfig
252     shift
253   done
254 }
255
256 # Remove saved config for a list of ports and all dependencies
257 port_rmconfig_recursive() {
258   meh "port rmconfig-recursive ${*}"
259   port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
260 }
261
262 # Obliterate saved configuration for all ports
263 port_rmconfig_all() {
264   meh "port rmconfig-all"
265   cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
266 }
267
268 # Restore port build options from cpio
269 port_load_config() {
270   [ -f "${conf_dir}/port_options.cpio" ] || return 0
271   meh "port load-config"
272   cheval "cd /var/db/ports; cpio -iv" < "${conf_dir}/port_options.cpio"
273 }
274
275 # Dump port build options to cpio
276 port_save_config() {
277   meh "port save-config"
278   cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc" > "${conf_dir}/port_options.cpio.tmp" || wtf "port save-config failed"
279   mv -f "${conf_dir}/port_options.cpio.tmp" "${conf_dir}/port_options.cpio" || wtf "port save-config atomic commit failed"
280 }
281
282 ########
283 #
284 # Port distfile handling
285 #
286 ########
287
288 # Recursively retrieve distfiles
289 port_fetch_recursive() {
290   while [ "${1}" ]
291   do
292     meh "fetch-recursive ${1}"
293     chmake "${1}" fetch-recursive
294     shift
295   done
296 }
297
298 ########
299 #
300 # Port building and packaging
301 #
302 ########
303
304 # Copy in and install dependency packages
305 port_load_deps() {
306   local port="${1}"
307   for pkg in $(port2pkg $(port_all_deps "${port}"))
308   do
309     cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
310   done
311   if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
312   then
313     meh "Installing dependencies"
314     cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
315   fi
316 }
317
318 # Build and install a port
319 port_build() {
320   local port="${1}"
321   meh "Building ${port}"
322   chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
323 }
324
325 # Package a port
326 port_package() {
327   local port="${1}"
328   meh "Creating rdep package tree for ${port}"
329   cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
330 }
331
332 # Package port build dependencies, unless they're already run deps
333 port_stash_bdeps() {
334   meh "Stashing unsaved bdeps"
335   # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
336   #for pkg in $(pkg_bdeps "${1}")
337   #do
338   #  [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
339   #done
340   #
341   # Instead, just save everything that's not already in rdeps as bdeps
342   for pkg in $(cheval pkg_info | awk '{print $1}')
343   do
344     if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
345     then
346       cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
347     fi
348   done
349 }
350
351 # Copy generated packages out of tree
352 pkg_final() {
353   meh "Moving created packages to repo"
354   mkdir -p "${final_pkg_dir}"
355   ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
356   mkdir -p "${final_bdeps_dir}"
357   ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
358   # link everything into ${bdeps_dir} so we can find it easily later
359   ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
360 }
361
362 # Delete all installed packages (hope you saved them first)
363 pkg_delete_all() {
364   meh "Clearing out installed packages"
365   cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
366 }
367
368 ########
369 #
370 # All of the above?
371 #
372 ########
373
374 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
375 # Be sure to chsetup and populate your config before running!
376 chport() {
377   local port="${1}"
378   port_load_deps "${port}"
379   port_build "${port}"
380   port_package "${port}"
381   port_stash_bdeps
382   pkg_final
383   pkg_delete_all
384 }
385
386
387 ########
388 #
389 # Configuration variable setup
390 #
391 ########
392
393 TARGET="${TARGET:-i386}"
394 CONFIG="${CONFIG:-GENERIC}"
395
396 ROOT="$(realpath "$(dirname "${0}")/../worlds")"
397
398 # Base directory for everything
399 base_dir="${ROOT}/${TARGET}/${CONFIG}"
400
401 # Directory holding configuration
402 conf_dir="${base_dir}/config"
403
404 # Root tree for chroot seeding
405 seed_dir="${base_dir}/root"
406
407 # Directory where distfiles will be stored between builds (common to all configs)
408 dist_dir="${ROOT}/seed/distfiles"
409
410 # Final directory for built packages (Outside chroot)
411 final_pkg_dir="${base_dir}/pkg"
412 final_bdeps_dir="${base_dir}/bdeps"
413
414 # Chroot directory
415 chroot_dir="${base_dir}/chroot"
416
417 # Package directories (must be under ${chroot_dir})
418 pkg_dir="${chroot_dir}/pkg"
419 bdeps_dir="${pkg_dir}/bdeps"
420
421 # Compute in-chroot pkg and bdeps dirs
422 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
423 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
424
425 # Chroot environment
426 chroot_env="
427 USER=root
428 HOME=/root
429 LOGNAME=root
430 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
431 SHELL=/bin/sh
432 TERM=${TERM}
433 "
434
435 # Blind passthru for testing
436 [ "${#}" ] && "${@}"