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