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