]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makepkg
script/makepkg: move configuration beneath function definitions
[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   while [ "${1}" ]
153   do
154     # Do not use config-recursive because it computes the depchain first;
155     # instead, manually compute the depchain after each config to ensure the
156     # proper depchain is followed. Use config-conditional to avoid dialog
157     # if the config is already complete. Also use a cache to avoid re-config
158     # and re-recurse on previously handled port branches.
159     if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
160     then
161       meh "port config-recursive ${1}"
162       chmake "${1}" config-conditional < /dev/tty
163       port_config_recursive $(port_deps "${1}")
164       _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
165     fi
166     shift
167   done
168 }
169 # Config cache
170 unset _port_config_recursive_cache
171
172 # Remove saved config for a list of ports
173 port_rmconfig() {
174   while [ "${1}" ]
175   do
176     meh "port rmconfig ${1}"
177     chmake "${1}" rmconfig
178     shift
179   done
180 }
181
182 # Remove saved config for a list of ports and all dependencies
183 port_rmconfig_recursive() {
184   meh "port rmconfig-recursive ${*}"
185   port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
186 }
187
188 # Obliterate saved configuration for all ports
189 port_rmconfig_all() {
190   meh "port rmconfig-all"
191   cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
192 }
193
194 # Restore port build options from cpio
195 port_load_config() {
196   meh "port load-config"
197   cheval "cd /var/db/ports; cpio -iv"
198 }
199
200 # Dump port build options to cpio
201 port_save_config() {
202   meh "port save-config"
203   cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
204 }
205
206 ########
207 #
208 # Port distfile handling
209 #
210 ########
211
212 # Recursively retrieve distfiles
213 port_fetch_recursive() {
214   while [ "${1}" ]
215   do
216     meh "fetch-recursive ${1}"
217     chmake "${1}" fetch-recursive
218   done
219 }
220
221 ########
222 #
223 # Port building and packaging
224 #
225 ########
226
227 # Copy in and install dependency packages
228 port_load_deps() {
229   local port="${1}"
230   for pkg in $(port2pkg $(port_all_deps "${port}"))
231   do
232     cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
233   done
234   if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
235   then
236     meh "Installing dependencies"
237     cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
238   fi
239 }
240
241 # Build and install a port
242 port_build() {
243   local port="${1}"
244   meh "Building ${port}"
245   chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
246 }
247
248 # Package a port
249 port_package() {
250   local port="${1}"
251   meh "Creating rdep package tree for ${port}"
252   cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
253 }
254
255 # Package port build dependencies, unless they're already run deps
256 port_stash_bdeps() {
257   meh "Stashing unsaved bdeps"
258   # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
259   #for pkg in $(pkg_bdeps "${1}")
260   #do
261   #  [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
262   #done
263   #
264   # Instead, just save everything that's not already in rdeps as bdeps
265   for pkg in $(cheval pkg_info | awk '{print $1}')
266   do
267     if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
268     then
269       cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
270     fi
271   done
272 }
273
274 # Copy generated packages out of tree
275 pkg_final() {
276   meh "Moving created packages to repo"
277   ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
278   ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
279   # link everything into ${bdeps_dir} so we can find it easily later
280   ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
281 }
282
283 # Delete all installed packages (hope you saved them first)
284 pkg_delete_all() {
285   meh "Clearing out installed packages"
286   cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
287 }
288
289 ########
290 #
291 # All of the above?
292 #
293 ########
294
295 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
296 # Be sure to chsetup and populate your config before running!
297 chport() {
298   local port="${1}"
299   port_load_deps "${port}"
300   port_build "${port}"
301   port_package "${port}"
302   port_stash_bdeps
303   pkg_final
304   pkg_delete_all
305 }
306
307
308 ########
309 #
310 # Configuration variable setup
311 #
312 ########
313
314 # Base directory for everything
315 base_dir="$(realpath "$(dirname "${0}")/../worlds")"
316
317 # Directory where distfiles will be stored between builds
318 dist_dir="${base_dir}/distfiles"
319
320 # Final directory for built packages (Outside chroot)
321 final_pkg_dir="${base_dir}/pkg"
322 final_bdeps_dir="${base_dir}/bdeps"
323
324 # Chroot directory
325 chroot_dir="${base_dir}/root"
326
327 # Package directories, must be under ${chroot_dir}
328 pkg_dir="${chroot_dir}/pkg"
329 bdeps_dir="${pkg_dir}/bdeps"
330
331 # Compute in-chroot pkg and bdeps dirs
332 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
333 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
334
335 # Chroot environment
336 chroot_env="
337 USER=root
338 HOME=/root
339 LOGNAME=root
340 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
341 SHELL=/bin/sh
342 TERM=${TERM}
343 "