]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/makepkg
script/makepkg: import cheval alternate framework
[CDN/Mosi.git] / script / makepkg
1 # Dump a list of leaf ports from a working system
2 # Leaf ports are ports that have nothing depending upon them
3 # These are generally the top-level ports; everything else should
4 # be pulled in by them
5 leaf_ports() {
6   ( cd /var/db/pkg; ls -1 ) | while read pkg
7   do
8     pkg_info -Rq "${pkg}" | grep -q '' || pkg_info -oq "${pkg}"
9   done | sort
10 }
11
12 ########
13 #
14 # Configuration variable setup
15 #
16 ########
17
18 # Base directory for everything
19 base_dir="$(realpath "$(dirname "${0}")/../worlds")"
20
21 # Directory where distfiles will be stored between builds
22 dist_dir="${base_dir}/distfiles"
23
24 # Final directory for built packages (Outside chroot)
25 final_pkg_dir="${base_dir}/pkg"
26 final_bdeps_dir="${base_dir}/bdeps"
27
28 # Chroot directory
29 chroot_dir="${base_dir}/root"
30
31 # Package directories, must be under ${chroot_dir}
32 pkg_dir="${chroot_dir}/pkg"
33 bdeps_dir="${pkg_dir}/bdeps"
34
35 # Compute in-chroot pkg and bdeps dirs
36 chroot_pkg_dir="${pkg_dir##${chroot_dir}}"
37 chroot_bdeps_dir="${bdeps_dir##${chroot_dir}}"
38
39 # Chroot environment
40 chroot_env="
41 USER=root
42 HOME=/root
43 LOGNAME=root
44 PATH=:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
45 SHELL=/bin/sh
46 TERM=${TERM}
47 "
48
49 # Tempvars
50 unset _port_config_recursive_cache
51
52 ########
53 #
54 # Boilerplate functions
55 #
56 ########
57
58 meh() { printf " \033[1;32m*\033[0m %s\n" "${*}" >&2; }
59 omg() { printf " \033[1;33m*\033[0m %s\n" "${*}" >&2; }
60 wtf() { printf " \033[1;31m*\033[0m %s\n" "${*}" >&2; exit 1; }
61
62 ########
63 #
64 # Chroot handling
65 #
66 ########
67
68 # Setup a chroot
69 chsetup() {
70   # Necessary mountpoints
71   mkdir -p "${chroot_dir}/dev" || return 1
72   mount -t devfs devfs "${chroot_dir}/dev" || return 1
73   mkdir -p "${chroot_dir}/usr/ports" || return 1
74   mount -t nullfs -r /usr/ports "${chroot_dir}/usr/ports" || return 1
75   mkdir -p "${chroot_dir}/var/ports/distfiles" || return 1
76   mount -t nullfs -w "${dist_dir}" "${chroot_dir}/var/ports/distfiles" || return 1
77
78   # Chroot configuration
79   cp -f /etc/resolv.conf "${chroot_dir}/etc/resolv.conf" || return 1
80
81   # Target package directories
82   mkdir -p "${pkg_dir}" || return 1
83   mkdir -p "${bdeps_dir}" || return 1
84 }
85
86 # Tear down a chroot
87 chteardown() {
88   umount "${chroot_dir}/var/ports/distfiles"
89   umount "${chroot_dir}/usr/ports"
90   umount "${chroot_dir}/dev"
91 }
92
93 # Evaluate a command line within the chroot
94 cheval() {
95   chroot "${chroot_dir}" env -i ${chroot_env} /bin/sh -c "cd ${chroot_pkg_dir}; ${*}"
96 }
97
98 # Run chrooted make
99 chmake() {
100   local port="/usr/ports/${1##/usr/ports/}"
101   shift
102   cheval "make -C ${port} ${*}"
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   while [ "${1}" ]
197   do
198     # Do not use config-recursive because it computes the depchain first;
199     # instead, manually compute the depchain after each config to ensure the
200     # proper depchain is followed. Use config-conditional to avoid dialog
201     # if the config is already complete. Also use a cache to avoid re-config
202     # and re-recurse on previously handled port branches.
203     if echo "${_port_config_recursive_cache}" | grep -qv " ${1} "
204     then
205       meh "port config-recursive ${1}"
206       chmake "${1}" config-conditional < /dev/tty
207       port_config_recursive $(port_deps "${1}")
208       _port_config_recursive_cache="${_port_config_recursive_cache} ${1} "
209     fi
210     shift
211   done
212 }
213
214 # Remove saved config for a list of ports
215 port_rmconfig() {
216   while [ "${1}" ]
217   do
218     meh "port rmconfig ${1}"
219     chmake "${1}" rmconfig
220     shift
221   done
222 }
223
224 # Remove saved config for a list of ports and all dependencies
225 port_rmconfig_recursive() {
226   meh "port rmconfig-recursive ${*}"
227   port_rmconfig $(echo "${@}" $(port_all_deps "${@}") | sort | uniq)
228 }
229
230 # Obliterate saved configuration for all ports
231 port_rmconfig_all() {
232   meh "port rmconfig-all"
233   cheval "cd /var/db/ports; find . -name 'options' -delete; find . -type d | xargs rmdir 2>/dev/null"
234 }
235
236 # Restore port build options from cpio
237 port_load_config() {
238   meh "port load-config"
239   cheval "cd /var/db/ports; cpio -iv"
240 }
241
242 # Dump port build options to cpio
243 port_save_config() {
244   meh "port save-config"
245   cheval "cd /var/db/ports; find . -type d -o -type f -name options | cpio -ovHnewc"
246 }
247
248 ########
249 #
250 # Port distfile handling
251 #
252 ########
253
254 # Recursively retrieve distfiles
255 port_fetch_recursive() {
256   while [ "${1}" ]
257   do
258     meh "fetch-recursive ${1}"
259     chmake "${1}" fetch-recursive
260   done
261 }
262
263 ########
264 #
265 # Port building and packaging
266 #
267 ########
268
269 # Copy in and install dependency packages
270 port_load_deps() {
271   local port="${1}"
272   for pkg in $(port2pkg $(port_all_deps "${port}"))
273   do
274     cp -f "${final_bdeps_dir}/${pkg}.tbz" "${bdeps_dir}" 2>/dev/null && meh "Loading dependent ${pkg}"
275   done
276   if ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1
277   then
278     meh "Installing dependencies"
279     cheval "cd ${chroot_bdeps_dir}; pkg_add -F *" || wtf "port_load_deps ${port} failed"
280   fi
281 }
282
283 # Build and install a port
284 port_build() {
285   local port="${1}"
286   meh "Building ${port}"
287   chmake "${port}" clean build install clean || wtf "port_build ${port} failed"
288 }
289
290 # Package a port
291 port_package() {
292   local port="${1}"
293   meh "Creating rdep package tree for ${port}"
294   cheval "pkg_create -Rvb $(port2pkg "${port}")" || wtf "port_package ${port} failed"
295 }
296
297 # Package port build dependencies, unless they're already run deps
298 port_stash_bdeps() {
299   meh "Stashing unsaved bdeps"
300   # This doesn't work well, because there can be bdeps that aren't listed as bdeps (bison)
301   #for pkg in $(pkg_bdeps "${1}")
302   #do
303   #  [ ! -f "${pkg_dir}/${pkg}.tbz" ] && cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}"
304   #done
305   #
306   # Instead, just save everything that's not already in rdeps as bdeps
307   for pkg in $(cheval pkg_info | awk '{print $1}')
308   do
309     if [ ! -f "${pkg_dir}/${pkg}.tbz" -a ! -f "${bdeps_dir}/${pkg}.tbz" ]
310     then
311       cheval "cd ${chroot_bdeps_dir}; pkg_create -vb ${pkg}" || wtf "port_stash_bdeps failed"
312     fi
313   done
314 }
315
316 # Copy generated packages out of tree
317 pkg_final() {
318   meh "Moving created packages to repo"
319   ls "${pkg_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${pkg_dir}"/*.tbz "${final_pkg_dir}"
320   ls "${bdeps_dir}"/*.tbz >/dev/null 2>&1 && mv -f "${bdeps_dir}"/*.tbz "${final_bdeps_dir}"
321   # link everything into ${bdeps_dir} so we can find it easily later
322   ( cd "${final_pkg_dir}"; find . -type f | cpio -plu --quiet "${final_bdeps_dir}" )
323 }
324
325 # Delete all installed packages (hope you saved them first)
326 pkg_delete_all() {
327   meh "Clearing out installed packages"
328   cheval "pkg_delete -f \*" || wtf "pkg_delete_all failed"
329 }
330
331 ########
332 #
333 # All of the above?
334 #
335 ########
336
337 # Execute a complete port build, using prebuilt packages to fulfill dependencies when available
338 # Be sure to chsetup and populate your config before running!
339 chport() {
340   local port="${1}"
341   port_load_deps "${port}"
342   port_build "${port}"
343   port_package "${port}"
344   port_stash_bdeps
345   pkg_final
346   pkg_delete_all
347 }