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