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