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