bin/chroot-maint: Program for maintaining chroots.
[distorted-chroot] / Makefile
CommitLineData
e36b4f25
MW
1### -*-makefile-*-
2###
3### Main maintenance script for chroots
4###
5### (c) 2018 Mark Wooding
6###
7
8###----- Licensing notice ---------------------------------------------------
9###
10### This file is part of the distorted.org.uk chroot maintenance tools.
11###
12### distorted-chroot is free software: you can redistribute it and/or
13### modify it under the terms of the GNU General Public License as
14### published by the Free Software Foundation; either version 2 of the
15### License, or (at your option) any later version.
16###
17### distorted-chroot is distributed in the hope that it will be useful,
18### but WITHOUT ANY WARRANTY; without even the implied warranty of
19### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20### General Public License for more details.
21###
22### You should have received a copy of the GNU General Public License
23### along with distorted-chroot. If not, write to the Free Software
24### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25### USA.
26
27all::
28clean::
3e5b03e2
MW
29check::
30.PHONY: all clean check
e36b4f25
MW
31.SECONDEXPANSION: #sorry
32
33###--------------------------------------------------------------------------
34### Configuration.
35
36CONFIG_VARS =
37
3e5b03e2
MW
38## Level of pickiness to aspire to.
39NOTIFY_FATAL = 1
40
41## Path to this working tree.
42CONFIG_VARS += HERE
43HERE = $(abspath .)
44
e36b4f25
MW
45## Volume group from which to allocate chroot volumes and snapshots.
46CONFIG_VARS += VG LVPREFIX
47VG = vg-$(shell hostname)
48LVPREFIX =
49
50## Logical volume size, as an LVM option.
3e5b03e2 51CONFIG_VARS += LVSZ SNAPOPT
e36b4f25 52LVSZ = -L8G
3e5b03e2 53SNAPOPT = -L8G
e36b4f25
MW
54
55## Debian mirror.
56CONFIG_VARS += DEBMIRROR
57DEBMIRROR = http://deb.debian.org/debian/
58
59## APT sources configurations. (Also, $($d_APTSRC) for each $d in $(DISTS).)
60CONFIG_VARS += APTSRC
61APTSRC = etc/aptsrc.conf $(wildcard etc/aptsrc.local.conf)
62
63## APT configuration fragment names. These will be linked into
64## `/etc/apt/apt.conf.d' in each chroot. To put a fragment f in a surprising
a98c9dba
MW
65## place, set $(_$f_APTCONFSRC).
66CONFIG_VARS += APTCONF $(foreach f,$(APTCONF),_$f_APTCONFSRC)
67APTCONF = $(notdir $(wildcard etc/apt-conf.d/[0-9]*[!~]))
e36b4f25
MW
68
69## Proxy setting.
70CONFIG_VARS += PROXY
71PROXY := $(shell \
a98c9dba 72 eval $$(apt-config $(foreach a,$(APTCONF),-cetc/apt-conf.d/$a) \
e36b4f25
MW
73 shell proxy Acquire::http::proxy); \
74 case $${proxy+t} in (t) echo "$$proxy" ;; (*) echo nil ;; esac)
75
76## Debian distributions to support.
77CONFIG_VARS += PRIMARY_DIST DISTS
78PRIMARY_DIST = stretch
79DISTS = $(PRIMARY_DIST) buster bullseye sid
80
81## Host's native architecture(s).
82CONFIG_VARS += MYARCH NATIVE_ARCHS
83MYARCH = $(shell dpkg --print-architecture)
84OTHERARCHS = $(shell dpkg --print-foreign-architectures)
85NATIVE_ARCHS = $(MYARCH) $(OTHERARCHS)
86
87## Foreign (emulated) architectures to support.
88CONFIG_VARS += FOREIGN_ARCHS
89FOREIGN_ARCHS =
90
91## Master lists of chroots to build and maintain.
a98c9dba 92CONFIG_VARS += NATIVE_CHROOTS FOREIGN_CHROOTS
e36b4f25
MW
93NATIVE_CHROOTS = $(foreach a,$(NATIVE_ARCHS), \
94 $(foreach d,$(or $($a_DISTS) $(DISTS)), \
95 $d-$a))
96FOREIGN_CHROOTS = $(foreach a,$(FOREIGN_ARCHS), \
97 $(foreach d,$(or $($a_DISTS) $(DISTS)), \
98 $d-$a))
99
a98c9dba
MW
100## Extra packages to be installed in chroots. `BASE_PACKAGES' are installed
101## through `debootstrap'; `EXTRA_PACKAGES' are installed later, using Apt,
102## which is faster in foreign chroots.
103CONFIG_VARS += BASE_PACKAGES EXTRA_PACKAGES
104BASE_PACKAGES = eatmydata
105EXTRA_PACKAGES = build-essential
106EXTRA_PACKAGES += ccache
107EXTRA_PACKAGES += fakeroot
108EXTRA_PACKAGES += libfile-fcntllock-perl
109EXTRA_PACKAGES += locales
110
111## Extra packages from which to install the cross tools.
112CONFIG_VARS += CROSS_PACKAGES
113CROSS_PACKAGES = bash coreutils dash findutils
114CROSS_PACKAGES += gzip m4 mawk sed tar xz-utils
115CROSS_PACKAGES += apt ccache eatmydata fakeroot make
116CROSS_PACKAGES += qemu-user-static
117CROSS_PACKAGES += $(foreach a,$(FOREIGN_GNUARCHS),\
118 gcc-$a g++-$a binutils-$a)
119
120## Native files to install in place of the foreign versions. `MULTI' here
121## stands for the donor's GNU multiarch name.
122CONFIG_VARS += CROSS_PATHS
123CROSS_PATHS += \
124 $(addprefix /usr/bin/, \
125 apt apt-cache apt-config apt-get apt-key apt-mark) \
126 $(addprefix /usr/lib/apt/, \
127 methods/ solvers/) \
128 $(addprefix /bin/, \
129 cat chgrp chown cp date dd df dir echo false ln ls mkdir \
130 mknod mktemp mv pwd readlink rm rmdir sleep stty sync touch \
131 true uname vdir) \
132 $(addprefix /usr/bin/, \
133 [ arch b2sum base32 base64 basename chcon cksum comm \
134 csplit cut dircolors dirname du env expand expr factor fmt \
135 fold groups head hostid id install join link logname md5sum \
136 mkfifo nice nl nohup nproc numfmt od paste pathchk pinky pr \
137 printenv printf ptx realpath runcon seq sha1sum sha224sum \
138 sha256sum sha384sum sha512sum shred shuf sort split stat \
139 stdbuf sum tac tail tee test timeout tr truncate tsort tty \
140 unexpand uniq unlink users wc who whoami yes) \
141 /usr/lib/MULTI/coreutils/ \
142 $(addprefix /lib/MULTI/, \
143 libnsl.so.* libnss_*.so.*) \
144 /usr/bin/gpgv \
145 /usr/bin/qemu-*-static \
146 $(addprefix /bin/, \
147 bash dash gzip sed tar) \
148 $(addprefix /usr/bin/, \
149 ccache find m4 make mawk xargs xz) \
150 $(addprefix /usr/lib/MULTI/, \
151 libeatmydata.so* libfakeroot/) \
152 $(addprefix /etc/ld.so.conf.d/, \
153 MULTI.conf fakeroot*.conf) \
154 $(foreach a,$(FOREIGN_GNUARCHS), \
155 $(addprefix /usr/bin/$a-, \
156 addr2line ar as c++filt dwp elfedit gprof ld ld.* \
157 nm objcopy objdump ranlib readelf size strings \
158 strip) \
159 $(addprefix /usr/bin/$a-, \
160 cpp gcc g++ gcov gcov-dump gcov-tool gprof \
161 gcc-ar gcc-nm gcc-ranlib) \
162 /usr/lib/gcc-cross/$a/)
3e5b03e2
MW
163
164## Local packages to be compiled and installed in chroots. Archives can be
165## found in `pkg/'.
a98c9dba 166CONFIG_VARS += LOCALPKGS $(foreach p,$(LOCALPKGS),$p_DEPS)
3e5b03e2 167LOCALPKGS = mLib checkpath
a98c9dba
MW
168mLib_DEPS =
169checkpath_DEPS = mLib
3e5b03e2 170
e36b4f25
MW
171## Which host architecture to use for foreign architectures. It turns out
172## that it's best to use a Qemu with the same host bitness as the target
173## architecture; otherwise it has to do a difficult job of converting
174## arguments and results between kernel ABI flavours.
17532BIT_QEMUHOST = $(or $(filter i386,$(NATIVE_ARCHS)),$(TOOLSARCH))
17664BIT_QEMUHOST = $(or $(filter amd64,$(NATIVE_ARCHS)),$(TOOLSARCH))
177
a98c9dba 178CONFIG_VARS += $(foreach a,$(FOREIGN_ARCHS),$a_QEMUHOST)
e36b4f25
MW
179armel_QEMUHOST = $(32BIT_QEMUHOST)
180armhf_QEMUHOST = $(32BIT_QEMUHOST)
181arm64_QEMUHOST = $(64BIT_QEMUHOST)
182i386_QEMUHOST = $(32BIT_QEMUHOST)
183amd64_QEMUHOST = $(64BIT_QEMUHOST)
184
185## Qemu architecture names. These tell us which Qemu binary to use for a
186## particular Debian architecture.
a98c9dba 187CONFIG_VARS += $(foreach a,$(FOREIGN_ARCHS),$a_QEMUARCH)
e36b4f25
MW
188armel_QEMUARCH = arm
189armhf_QEMUARCH = arm
190arm64_QEMUARCH = aarch64
191i386_QEMUARCH = i386
192amd64_QEMUARCH = x86_64
193
194## Alias mapping for chroots.
a98c9dba 195CONFIG_VARS += $(foreach d,$(DISTS),$d_ALIASES)
e36b4f25
MW
196stretch_ALIASES = oldstable
197buster_ALIASES = stable
a98c9dba 198bullseye_ALIASES = testing
e36b4f25
MW
199sid_ALIASES = unstable
200
201## Which host architecture to use for commonly used tools in foreign chroots.
202CONFIG_VARS += TOOLSARCH
203TOOLSARCH = $(MYARCH)
204
205## A directory in which we can maintain private files and state.
206CONFIG_VARS += STATE
207STATE = state
208
209## A directory which will be spliced into chroots as `/usr/local.schroot/'.
210## This will be our primary point of contact with the chroot.
211CONFIG_VARS += LOCAL
212LOCAL = local.schroot
213
214## How to run a command as a privileged user.
a98c9dba 215CONFIG_VARS += ROOTLY
e36b4f25
MW
216ROOTLY = sudo
217
218## Files to be copied into a chroot from the host.
219SCHROOT_COPYFILES = /etc/resolv.conf
220SCHROOT_NSSDATABASES = passwd shadow group gshadow
221
222## Local configuration hook.
223-include local.mk
224
225## All chroot names.
3e5b03e2
MW
226CONFIG_VARS += ALL_ARCHS ALL_CHROOTS
227ALL_ARCHS = $(NATIVE_ARCHS) $(FOREIGN_ARCHS)
e36b4f25
MW
228ALL_CHROOTS = $(NATIVE_CHROOTS) $(FOREIGN_CHROOTS)
229
a98c9dba
MW
230## GNU names for foreign architectures.
231CONFIG_VARS += FOREIGN_GNUARCHS
232FOREIGN_GNUARCHS := $(foreach a,$(FOREIGN_ARCHS),\
233 $(shell dpkg-architecture -A$a \
234 -qDEB_TARGET_GNU_TYPE))
235
e36b4f25
MW
236###--------------------------------------------------------------------------
237### Utilities.
238
3e5b03e2
MW
239## Hack to force rebuilding.
240_force:
241.PHONY: _force
242
243## Hack to not delimit function arguments. (Ugh!)
244comma = ,
245
246## Silent-rules machinery.
247V = 0
248V_AT = $(V_AT_$V)
249V_AT_0 = @
250v_print = $(call v_print_$V,$1,$2)
251v_print_0 = printf " %-8s %s\n" "$1" $(call squote,$2);
252v_tag = $(V_AT)$(call v_print,$1,$@)
253v_log = $(call v_log_$V,$1,$2)
254v_log_ = $(call v_log_1,$1,$2)
255v_log_0 = $2 >log/$1.log 2>&1
256v_log_1 = $(call catchrc,$(call throwrc,$2) 2>&1 | tee log/$1.log)
257v_echo = $(call v_echo_$V,$1)
258v_echo_0 = :
259v_echo_1 = printf "%s\n" $1
260CLEANFILES += log/*.log
261
262## Oh, shut up.
263SILENCE_LVM = \
264 LVM_SUPPRESS_FD_WARNINGS=1; export LVM_SUPPRESS_FD_WARNINGS
265
a98c9dba
MW
266## $(call definedp,VAR)
267##
268## Expand non-empty if and only if VAR is defined (but possibly empty).
269definedp = $(filter-out undefined,$(origin $1))
270
3e5b03e2
MW
271## $(call catchrc,...$(call throwrc,CMD)...)
272##
273## Catch the exit status of some subpart of a complicated shell rune.
274catchrc = (exec 3>&1; exit $$({ $1; } 4>&1 >&3 3>&-))
275throwrc = { $1; echo $$? >&4; }
276
277## $(call squote,TXT)
278##
279## Single-quote TXT.
280squote = '$(subst ','\\'',$1)'
281
e36b4f25
MW
282## $(call chroot-dist,D-A) -> D
283## $(call chroot-arch,D-A) -> A
284##
285## Parse chroot descriptions.
286chroot-dist = $(patsubst %/,%,$(dir $(subst -,/,$1)))
287chroot-arch = $(notdir $(subst -,/,$1))
288
3e5b03e2
MW
289## $(call package-dir-name,P-V) -> P
290## $(call package-dir-version,P-V) -> V
291##
292## Parse (source) package directory names.
293package-dir-name = $(sort $(foreach p,$(LOCALPKGS),$(if $(filter $p-$($p_VERSION),$1),$p)))
294package-dir-version = $($(call package-dir-name,$1)_VERSION)
295
296## $(call package-dir,P-V.A) -> P-V
297## $(call package-name,P-V.A) -> P
298## $(call package-version,P-V.A) -> V
299## $(call package-arch,P-V.A) -> A
300##
301## Parse package stamp names.
302package-dir = $(basename $1)
303package-name = $(call package-dir-name,$(call package-dir,$1))
304package-version = $(call package-dir-version,$(call package-dir,$1))
305package-arch = $(patsubst .%,%,$(suffix $1))
306
e36b4f25
MW
307## $(call native-chroot-p,D-A) -> D | <empty>
308##
309## Answer whether D-A is a native chroot.
3e5b03e2 310native-chroot-p = $(filter $(call chroot-arch,$1),$(NATIVE_ARCHS))
e36b4f25
MW
311
312## $(call chroot-qemuhost,D-A) -> AA
313##
314## Answer the apporopriate Qemu host architecture for foreign chroot D-A.
315chroot-qemuhost = $($(call chroot-arch,$1)_QEMUHOST)
316
3e5b03e2 317## $(call chroot-deps,PRE,D-A) -> PREDD-AA ... | <empty>
e36b4f25
MW
318##
319## Answer a list of additional dependencies for the chroot D-A: specifically,
320## if D-A is foreign then include PRE/DD-AA entries for the tools
321## architecture, and Qemu host architecture (if that's different).
322chroot-deps = $(if $(call native-chroot-p,$2),, \
3e5b03e2
MW
323 $(addprefix $1$(call chroot-dist,$2)-,\
324 $(sort $(TOOLSARCH) \
e36b4f25
MW
325 $(call chroot-qemuhost,$2))))
326
327## Substituting placeholders in files.
328SUBSTITUTIONS := local=$(shell realpath $(LOCAL))
329SUBSTITUTIONS += primary-dist=$(PRIMARY_DIST)
330subst-file = { \
331 echo "$1 GENERATED by distorted-chroot: do not edit"; \
332 substs=""; \
333 for v in $(SUBSTITUTIONS); do \
334 var=$${v%%=*} value=$${v\#*=}; \
335 substs="$$substs s\a@$$var@\a$$value\ag;"; \
336 done; \
337 sed "$$substs"; \
338}
339
3e5b03e2
MW
340### $(call symlink-ok-p,LINK,DEST)
341###
342### Expand to `t' if LINK is a symbolic link to DEST, and empty otherwise.
343symlink-ok-p = $(shell \
344 case $$(readlink 2>/dev/null $(patsubst %/,%,$1)) in ($2) echo t ;; esac)
345
346### $(call general-notify,SEV,COLOUR,PREFIX,MSG)
347###
348### Report a message, highlighted in the right way, and maybe fail
349general-notify = { \
350 echo "$$(tput bold; tput setaf $2)$3 "$4"$$(tput sgr0; tput op)"; \
351 if [ "$1" -le "$(NOTIFY_FATAL)" ]; then exit 2; fi; \
352}
353
354### $(call report/SEV,MSG)
355###
356### Report a notification of a particular severity.
357notify/INFO = $(call general-notify,3,6,---,$1)
358notify/WARN = $(call general-notify,2,5,???,$1)
359notify/ERR = $(call general-notify,1,1,!!!,$1)
360
361## $(call check,SEV,MSG,UNLESS)
362##
363## If UNLESS completes successfully, all is OK; otherwise print MSG to stderr
364## and fail.
365check = @{ \
366 $(call v_echo,'check: '$(call squote,$3)''); \
367 if ! { $3; }; then $(call notify/$1,$2); fi; \
368}
369
370## $(call check-executable,SEV,PATH)
371##
372## Verify that PATH is an executable program.
373check-executable = $(call check,$1,"\`$2' is not an executable", \
374 [ -x "$2" ])
375
376## $(call check-mountpoint,SEV,DIR)
377##
378## Verify that DIR is a mountpoint.
379check-mountpoint = $(call check,$1,"\`$2' is not a mount point", \
380 mountpoint -q "$2")
381
382## $(call check-symlink,SEV,LINK,DEST)
383##
384## Verify that LINK is a symbolic link pointing to DEST.
385check-symlink = $(call check,$1,"\`$2' is not a link to \`$3'", \
386 [ -L "$2" ] && [ "$$(readlink "$2")" = "$3" ])
387
388###--------------------------------------------------------------------------
a4c5d71a
MW
389### Python extensions.
390
391CC = gcc
392CFLAGS = -O2 -g -Wall
393
394LD = $(CC)
395LDFLAGS =
396
397c-source = $(foreach c,$1,src/$c)
398c-object = $(foreach c,$1,$(STATE)/obj/$(basename $c).o)
399
400PYEXT_PKGFLAGS := $(shell pkg-config --cflags python2)
401PYEXT_CFLAGS = -fPIC -fno-strict-aliasing $(PYEXT_PKGFLAGS)
402PYEXT_LDFLAGS = -shared
403
404PYEXTS += jobclient
405jobclient_SOURCES = jobclient.c
406
407PYEXT_ALLSRC = $(foreach x,$(PYEXTS),\
408 $(call c-source,$($x_SOURCES)))
409PYEXT_ALLOBJ = $(foreach x,$(PYEXTS),\
410 $(call c-object,$($x_SOURCES)))
411$(PYEXT_ALLOBJ): $(STATE)/obj/%.o: src/%.c
412 $(V_AT)mkdir -p $(dir $@)
413 $(call v_tag,CC)$(CC) -c $(CFLAGS) $(PYEXT_CFLAGS) -o$@ $<
414
415PYMODULES = $(foreach x,$(PYEXTS),$(STATE)/lib/python/$x.so)
416all:: $(PYMODULES)
417$(PYMODULES): $(STATE)/lib/python/%.so: $$(call c-object,$$($$*_SOURCES))
418 $(V_AT)mkdir -p $(dir $@)
419 $(call v_tag,LD)$(LD) $(LDFLAGS) $(PYEXT_LDFLAGS) -o$@ $^
420
421###--------------------------------------------------------------------------
3e5b03e2
MW
422### Scripts.
423
a98c9dba 424SCRIPTS += chroot-maint
3e5b03e2
MW
425SCRIPTS += mkbuildchroot
426SCRIPTS += mkchrootconf
427SCRIPTS += install-cross-tools update-cross-tools
428
429SUBST_SCRIPTS = $(addprefix $(STATE)/bin/,$(SCRIPTS))
430all:: $(SUBST_SCRIPTS)
431$(SUBST_SCRIPTS): $(STATE)/bin/%: bin/% $(STATE)/config.sh
432 $(V_AT)mkdir -p $(dir $@)
433 $(call v_tag,SUBST){ \
434 sed \
435 -e '2i### GENERATED by distorted-chroot: do not edit' \
436 -e '/@@@config@@@/ {' \
437 -e 'r $(STATE)/config.sh' \
438 -e 'd'\
439 -e '}' $<; \
440 } >$@.new && chmod +x $@.new && mv $@.new $@
e36b4f25
MW
441
442###--------------------------------------------------------------------------
443### APT configuration.
444
445## In a chroot, `/etc/apt/sources.list' links to
446## `/usr/local.schroot/etc/apt/sources.$d' for the appropriate distribution.
4004ed52 447APT_SOURCES = $(foreach d,$(DISTS),$(LOCAL)/etc/apt/sources.$d)
e36b4f25
MW
448all:: $(APT_SOURCES)
449
4004ed52 450$(foreach d,$(DISTS),$(STATE)/etc/apt/aptsrc.$d): $(STATE)/etc/apt/aptsrc.%:
e36b4f25
MW
451 $(V_AT)mkdir -p $(dir $@)
452 $(call v_tag,GEN){ \
453 echo "### -*-conf-*- GENERATED by distorted-chroot: do not edit"; \
454 echo "subscribe"; \
455 echo " debian : $*"; \
456 } >$@.new && mv $@.new $@
457
458$(APT_SOURCES): $(LOCAL)/etc/apt/sources.%: \
459 $$(APTSRC) $$($$*_APTSRC) $$(STATE)/etc/apt/aptsrc.$$*
460 $(V_AT)mkdir -p $(dir $@)
461 $(call v_tag,GEN)bin/mkaptsrc \
462 $(APTSRC) $($*_APTSRC) $(STATE)/etc/apt/aptsrc.$* \
463 >$@.new && mv $@.new $@
0539140c 464CLEANFILES += $(APT_SOURCES)
e36b4f25
MW
465
466## In a chroot, a link `/etc/apt/apt.conf.d/FOO' is created for each file in
467## `/usr/local.schroot/etc/apt/apt.conf.d/FOO'.
468APT_CONFIGS = $(addprefix $(LOCAL)/etc/apt/apt.conf.d/,$(APTCONF))
469all:: $(APT_CONFIGS)
470$(APT_CONFIGS): $(LOCAL)/etc/apt/apt.conf.d/%: \
a98c9dba 471 $$(or $$(_$$*_APTCONFSRC) etc/apt-conf.d/$$*)
e36b4f25
MW
472 $(V_AT)mkdir -p $(dir $@)
473 $(call v_tag,COPY)cp $< $@.new && mv $@.new $@
3e5b03e2
MW
474clean::; rm -f $(APT_CONFIGS)
475
476###--------------------------------------------------------------------------
477### Build hacks.
478
479check::; $(call check-executable,ERR,/usr/bin/eatmydata)
480
481EATMYDATA_HACKS += apt-get aptitude dpkg
482SYMLINK_EATMYDATA_HACKS = $(addprefix $(LOCAL)/hacks/,$(EATMYDATA_HACKS))
483all:: $(SYMLINK_EATMYDATA_HACKS)
484$(SYMLINK_EATMYDATA_HACKS): $(LOCAL)/hacks/%: \
485 $$(if $$(call symlink-ok-p,$$@,/usr/bin/eatmydata),,_force)
486 $(V_AT)mkdir -p $(dir $@)
487 $(call v_tag,SYMLINK)ln -sf /usr/bin/eatmydata $@.new && mv $@.new $@
488clean::; rm -f $(SYMLINK_EATMYDATA_HACKS)
489
490SCRIPT_HACKS += buildwrap
491COPY_SCRIPT_HACKS = $(addprefix $(LOCAL)/hacks/,$(SCRIPT_HACKS))
492all:: $(COPY_SCRIPT_HACKS)
493$(COPY_SCRIPT_HACKS): $(LOCAL)/hacks/%: bin/%
494 $(V_AT)mkdir -p $(dir $@)
495 $(call v_tag,COPY)cp $< $@.new && mv $@.new $@
496clean::; rm -f $(COPY_SCRIPT_HACKS)
e36b4f25
MW
497
498###--------------------------------------------------------------------------
499### `schroot' and `sbuild' configuration.
500
501all:: schroot-config
502schroot-config::
503.PHONY: schroot-config
504
3e5b03e2
MW
505check::; $(call check-mountpoint,WARN,/var/lib/sbuild/build)
506check::; $(call check-symlink,WARN,/build,/var/lib/sbuild/build)
507check::; $(call check-symlink,ERR,/schroot,/run/schroot/mount)
508
e36b4f25
MW
509%print-varlist = { \
510 echo "\#\#\# -*-sh-*- GENERATED by distorted-chroot: do not edit"; \
a98c9dba
MW
511 $(foreach v,$1,$(if $(call definedp,$v),\
512 echo $v=\'$(call squote,$($v))\';)) \
fb28f2a8 513}
e36b4f25
MW
514schroot-config_HASH := \
515 $(shell $(call %print-varlist,$(CONFIG_VARS)) | \
516 sha256sum | cut -c1-32)
517schroot-config_FILE = $(STATE)/config.sh-$(schroot-config_HASH)
518$(schroot-config_FILE):
519 $(V_AT)mkdir -p $(STATE)
520 $(V_AT)rm -f $(STATE)/config.sh-*
6ccac28c 521 $(call v_tag,GEN)$(call %print-varlist,$(CONFIG_VARS)) \
e36b4f25
MW
522 >$@.new && mv $@.new $@
523
524schroot-config:: $(STATE)/config.sh
525$(STATE)/config.sh: $(schroot-config_FILE)
526 $(call v_tag,SYMLINK)ln -sf $(notdir $<) $@
527
67a8479c
MW
528schroot-config:: $(LOCAL)/etc/schroot/sbuild.schroot
529$(LOCAL)/etc/schroot/sbuild.schroot: $(STATE)/bin/mkchrootconf
e36b4f25 530 $(V_AT)mkdir -p $(dir $@)
3e5b03e2
MW
531 $(call v_tag,GEN)$(STATE)/bin/mkchrootconf >$@.new && \
532 $(ROOTLY) chown root:root $@.new && mv $@.new $@
67a8479c
MW
533CLEANFILES += $(LOCAL)/etc/schroot/sbuild.schroot
534check::; $(call check-symlink,WARN,/etc/schroot/chroot.d/sbuild,$(HERE)/$(LOCAL)/etc/schroot/sbuild.schroot)
e36b4f25 535
67a8479c
MW
536schroot-config:: $(LOCAL)/etc/schroot/sbuild.profile/copyfiles
537$(LOCAL)/etc/schroot/sbuild.profile/copyfiles: $(schroot-config_STAMP)
e36b4f25
MW
538 $(V_AT)mkdir -p $(dir $@)
539 $(call v_tag,GEN){ \
540 echo "### -*-conf-*- GENERATED by distorted-chroot: do not edit"; \
541 for i in $(SCHROOT_COPYFILES); do echo "$$i"; done; \
542 } >$@.new && mv $@.new $@
67a8479c 543CLEANFILES += $(LOCAL)/etc/schroot/sbuild.profile/copyfiles
e36b4f25 544
67a8479c
MW
545schroot-config:: $(LOCAL)/etc/schroot/sbuild.profile/nssdatabases
546$(LOCAL)/etc/schroot/sbuild.profile/nssdatabases: $(schroot-config_STAMP)
e36b4f25
MW
547 $(V_AT)mkdir -p $(dir $@)
548 $(call v_tag,GEN){ \
549 echo "### -*-conf-*- GENERATED by distorted-chroot: do not edit"; \
550 for i in $(SCHROOT_NSSDATABASES); do echo "$$i"; done; \
551 } >$@.new && mv $@.new $@
67a8479c 552CLEANFILES += $(LOCAL)/etc/schroot/sbuild.profile/nssdatabases
e36b4f25 553
67a8479c
MW
554schroot-config:: $(LOCAL)/etc/schroot/sbuild.profile/fstab
555$(LOCAL)/etc/schroot/sbuild.profile/fstab: \
e36b4f25
MW
556 etc/sbuild.fstab.in $(schroot-config_STAMP)
557 $(V_AT)mkdir -p $(dir $@)
558 $(call v_tag,SUBST)$(call subst-file,### -*-conf-*-) \
559 <$< >$@.new && mv $@.new $@
67a8479c 560CLEANFILES += $(LOCAL)/etc/schroot/sbuild.profile/fstab
e36b4f25 561
67a8479c 562check::; $(call check-symlink,WARN,/etc/schroot/sbuild,$(HERE)/$(LOCAL)/etc/schroot/sbuild.profile)
3e5b03e2 563
67a8479c
MW
564schroot-config:: $(LOCAL)/etc/sbuild.conf
565$(LOCAL)/etc/sbuild.conf: etc/sbuild.conf.in $(schroot-config_STAMP)
e36b4f25
MW
566 $(V_AT)mkdir -p $(dir $@)
567 $(call v_tag,SUBST)$(call subst-file,### -*-perl-*-) \
568 <$< >$@.new && mv $@.new $@
67a8479c
MW
569CLEANFILES += $(LOCAL)/etc/sbuild.conf
570check::; $(call check-symlink,WARN,/etc/sbuild/sbuild.conf,$(HERE)/$(LOCAL)/etc/sbuild.conf)
d56d9258 571check::; $(call check-executable,WARN,/usr/local.schroot/hacks/apt-get)
e36b4f25 572
3e5b03e2
MW
573SCHROOT_SCRIPTS += 11private
574SCHROOT_SCRIPTS += 15binfmt
575SCHROOT_SCRIPTS += 51chrootenv
67a8479c 576COPY_SCHROOT_SCRIPTS = $(addprefix $(LOCAL)/etc/schroot/setup.d/,$(SCHROOT_SCRIPTS))
3e5b03e2
MW
577schroot-config:: $(COPY_SCHROOT_SCRIPTS)
578$(COPY_SCHROOT_SCRIPTS): \
67a8479c 579 $(LOCAL)/etc/schroot/setup.d/%: etc/schroot-scripts/%
3e5b03e2
MW
580 $(V_AT)mkdir -p $(dir $@)
581 $(call v_tag,COPY)cp $< $@.new && mv $@.new $@
67a8479c 582CLEANFILES += $(COPY_SCHROOT_SCRIPTS)
3e5b03e2
MW
583
584CHECK_SCHROOT_SCRIPTS = $(addprefix check-script/,$(SCHROOT_SCRIPTS))
585check:: $(CHECK_SCHROOT_SCRIPTS)
586$(CHECK_SCHROOT_SCRIPTS): check-script/%:
67a8479c 587 $(call check-symlink,WARN,/etc/schroot/setup.d/$*,$(HERE)/$(LOCAL)/etc/schroot/setup.d/$*)
3e5b03e2
MW
588.PHONY: $(addprefix check-script/,$(SCHROOT_SCRIPTS))
589
590###--------------------------------------------------------------------------
591### `/usr/local/' structure.
592
593LOCAL_COMMON_DIRS = share/ src/
27922408
MW
594LOCAL_EXTRA_DIRS = share/man/
595all:: $(foreach d,$(LOCAL_COMMON_DIRS) $(LOCAL_EXTRA_DIRS),$(LOCAL)/$d)
596$(foreach d,$(LOCAL_COMMON_DIRS) $(LOCAL_EXTRA_DIRS),$(LOCAL)/$d):
3e5b03e2
MW
597 $(V_AT)mkdir -p $(dir $(patsubst %/,%,$@))
598 $(call v_tag,MKDIR)mkdir $@
599
600LOCAL_ARCH_DIRS = bin/ etc/ games/ include/ lib/ libexec/ sbin/
601LOCAL_ARCH_LINKS = man
602man_LINKDEST = share/man
603all:: $(foreach a,$(ALL_ARCHS),\
604 $(LOCAL)/$a/ \
605 $(foreach d,$(LOCAL_ARCH_DIRS),$(LOCAL)/$a/$d) \
606 $(foreach d,$(LOCAL_ARCH_LINKS),$(LOCAL)/$a/$d) \
607 $(foreach d,$(LOCAL_COMMON_DIRS),$(LOCAL)/$a/$d))
608
609$(foreach a,$(ALL_ARCHS),$(LOCAL)/$a/):
610 $(call v_tag,MKDIR)mkdir $@
611$(foreach a,$(ALL_ARCHS),\
612 $(foreach d,$(LOCAL_ARCH_DIRS),$(LOCAL)/$a/$d)):
613 $(V_AT)mkdir -p $(dir $(patsubst %/,%,$@))
614 $(call v_tag,MKDIR)mkdir $@
615$(foreach a,$(ALL_ARCHS),\
616 $(foreach d,$(LOCAL_ARCH_LINKS),$(LOCAL)/$a/$d)): \
617 $$(if $$(call symlink-ok-p,$$@,$$($$(notdir $$@)_LINKDEST)),,_force)
618 $(V_AT)mkdir -p $(notdir $@)
619 $(call v_tag,SYMLINK)ln -sf $($(notdir $@)_LINKDEST) $@
620$(foreach a,$(ALL_ARCHS),\
621 $(foreach d,$(LOCAL_COMMON_DIRS),$(LOCAL)/$a/$d)): \
622 $$(if $$(call symlink-ok-p,$$@,../$$(notdir $$(patsubst %/,%,$$@))),,_force)
623 $(V_AT)mkdir -p $(dir $(patsubst %/,%,$@))
624 $(call v_tag,SYMLINK)ln -sf ../$(notdir $(patsubst %/,%,$@)) $(patsubst %/,%,$@)
625
e36b4f25 626###--------------------------------------------------------------------------
a98c9dba 627### Main chroot maintenance.
e36b4f25 628
a98c9dba
MW
629OPTS =
630FRESH = create
631JOBS = chroot cross-tools pkg-build
632
633MAINTQ_ = -q
634MAINTQ_0 = -q
635MAINT = +$(call v_tag,RUN)\
636 PYTHONPATH=$(STATE)/lib/python $(STATE)/bin/chroot-maint \
637 $(MAINTQ_$V) $(OPTS)
638
639maint: all check
640 $(MAINT) -f$(FRESH) $(JOBS)
641.PHONY: maint
e36b4f25
MW
642
643###--------------------------------------------------------------------------
644### Other maintenance targets.
645
646show:; : $x
647
648clean::
649 rm -f $(CLEANFILES)
650 rm -rf $(STATE)
651
652realclean:: clean
653 rm -rf $(LOCAL)
654
655###----- That's all, folks --------------------------------------------------