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