+### $(call symlink-ok-p,LINK,DEST)
+###
+### Expand to `t' if LINK is a symbolic link to DEST, and empty otherwise.
+symlink-ok-p = $(shell \
+ case $$(readlink 2>/dev/null $(patsubst %/,%,$1)) in ($2) echo t ;; esac)
+
+### $(call general-notify,SEV,COLOUR,PREFIX,MSG)
+###
+### Report a message, highlighted in the right way, and maybe fail
+general-notify = { \
+ echo "$$(tput bold; tput setaf $2)$3 "$4"$$(tput sgr0; tput op)"; \
+ if [ "$1" -le "$(NOTIFY_FATAL)" ]; then exit 2; fi; \
+}
+
+### $(call report/SEV,MSG)
+###
+### Report a notification of a particular severity.
+notify/INFO = $(call general-notify,3,6,---,$1)
+notify/WARN = $(call general-notify,2,5,???,$1)
+notify/ERR = $(call general-notify,1,1,!!!,$1)
+
+## $(call check,SEV,MSG,UNLESS)
+##
+## If UNLESS completes successfully, all is OK; otherwise print MSG to stderr
+## and fail.
+check = @{ \
+ $(call v_echo,'check: '$(call squote,$3)''); \
+ if ! { $3; }; then $(call notify/$1,$2); fi; \
+}
+
+## $(call check-executable,SEV,PATH)
+##
+## Verify that PATH is an executable program.
+check-executable = $(call check,$1,"\`$2' is not an executable", \
+ [ -x "$2" ])
+
+## $(call check-mountpoint,SEV,DIR)
+##
+## Verify that DIR is a mountpoint.
+check-mountpoint = $(call check,$1,"\`$2' is not a mount point", \
+ mountpoint -q "$2")
+
+## $(call check-symlink,SEV,LINK,DEST)
+##
+## Verify that LINK is a symbolic link pointing to DEST.
+check-symlink = $(call check,$1,"\`$2' is not a link to \`$3'", \
+ [ -L "$2" ] && [ "$$(readlink "$2")" = "$3" ])
+
+###--------------------------------------------------------------------------
+### Python extensions.
+
+CC = gcc
+CFLAGS = -O2 -g -Wall
+
+LD = $(CC)
+LDFLAGS =
+
+c-source = $(foreach c,$1,src/$c)
+c-object = $(foreach c,$1,$(STATE)/obj/$(basename $c).o)
+
+PYEXT_PKGFLAGS := $(shell pkg-config --cflags python2)
+PYEXT_CFLAGS = -fPIC -fno-strict-aliasing $(PYEXT_PKGFLAGS)
+PYEXT_LDFLAGS = -shared
+
+PYEXTS += jobclient
+jobclient_SOURCES = jobclient.c
+
+PYEXT_ALLSRC = $(foreach x,$(PYEXTS),\
+ $(call c-source,$($x_SOURCES)))
+PYEXT_ALLOBJ = $(foreach x,$(PYEXTS),\
+ $(call c-object,$($x_SOURCES)))
+$(PYEXT_ALLOBJ): $(STATE)/obj/%.o: src/%.c
+ $(V_AT)mkdir -p $(dir $@)
+ $(call v_tag,CC)$(CC) -c $(CFLAGS) $(PYEXT_CFLAGS) -o$@ $<
+
+PYMODULES = $(foreach x,$(PYEXTS),$(STATE)/lib/python/$x.so)
+all:: $(PYMODULES)
+$(PYMODULES): $(STATE)/lib/python/%.so: $$(call c-object,$$($$*_SOURCES))
+ $(V_AT)mkdir -p $(dir $@)
+ $(call v_tag,LD)$(LD) $(LDFLAGS) $(PYEXT_LDFLAGS) -o$@ $^
+
+###--------------------------------------------------------------------------
+### Scripts.
+
+SCRIPTS += chroot-maint
+SCRIPTS += mkchrootconf
+
+SUBST_SCRIPTS = $(addprefix $(STATE)/bin/,$(SCRIPTS))
+all:: $(SUBST_SCRIPTS)
+$(SUBST_SCRIPTS): $(STATE)/bin/%: bin/% $(STATE)/config.sh
+ $(V_AT)mkdir -p $(dir $@)
+ $(call v_tag,SUBST){ \
+ sed \
+ -e '2i### GENERATED by distorted-chroot: do not edit' \
+ -e '/@@@config@@@/ {' \
+ -e 'r $(STATE)/config.sh' \
+ -e 'd'\
+ -e '}' $<; \
+ } >$@.new && chmod +x $@.new && mv $@.new $@