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