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