tzpfms/Makefile
2024-03-05 22:48:30 +01:00

164 lines
6.9 KiB
Makefile

# SPDX-License-Identifier: MIT
-include Makefile.local
TZPFMS_VERSION ?= "$(patsubst v%,%,$(shell git describe))"
SOURCE_DATE_EPOCH ?=
TZPFMS_DATE ?=
AWK ?= awk
SED ?= sed
MANDOC ?= mandoc
PKG_CONFIG ?= pkgconf
SHELLCHECK ?= shellcheck
NOLOCREGEN ?=
OUTDIR := out/
BLDDIR := out/build/
SYSTEMD_SYSTEM_UNITDIR := $(shell $(PKG_CONFIG) --variable=systemd_system_unit_dir systemd 2>/dev/null || echo /usr/lib/systemd/system)
ifneq "$(TZPFMS_DATE)" ""
MANUAL_DATE := $(TZPFMS_DATE)
else ifneq "$(SOURCE_DATE_EPOCH)" ""
MANUAL_DATE := $(shell LC_ALL=C date -d@$(SOURCE_DATE_EPOCH) '+%B %e, %Y')
else
MANUAL_DATE = $(shell LC_ALL=C date -d@$(shell git log -1 --no-show-signature --format=%at $(1)) '+%B %e, %Y')
endif
PKGS := libzfs libzfs_core tss2-esys tss2-rc
PKG_CONFIG_LIBS := $(shell $(PKG_CONFIG) --libs $(PKGS))
PKG_CONFIG_CFLAGS := $(shell $(PKG_CONFIG) --cflags $(PKGS))
LDFLAGS += -Wl,--as-needed
LDLIBS += -lrt -ltspi -lcrypto $(PKG_CONFIG_LIBS)
CXXFLAGS += -g -O3 -std=c++17 -fno-exceptions -fno-rtti -Wall -Wextra -pipe -fPIC $(PKG_CONFIG_CFLAGS)
CPPFLAGS += -MD -DTZPFMS_VERSION='$(TZPFMS_VERSION)'
BINARY_SOURCES := $(sort $(wildcard src/bin/*.cpp src/bin/**/*.cpp))
COMMON_SOURCES := $(filter-out $(BINARY_SOURCES),$(sort $(wildcard src/*.cpp src/**/*.cpp src/**/**/*.cpp src/**/**/**/*.cpp)))
MANPAGE_HEADERS := $(sort $(wildcard man/*.h))
MANPAGE_SOURCES := $(sort $(wildcard man/*.[0-8].pp))
INITRD_HEADERS := $(sort $(wildcard initrd/*.h))
ifneq (,$(shell command -v msgfmt))
LOCALES := $(wildcard po/*.po)
endif
ifdef TZPFMS_PASSPHRASE_HELPER
CPPFLAGS += -DTZPFMS_PASSPHRASE_HELPER='$(TZPFMS_PASSPHRASE_HELPER)'
endif
ifdef TZPFMS_PASSPHRASE_HELPER_MAN
DEF_TPH_MAN := .Pp\nDefault:\n.No '\'' Ns $(TZPFMS_PASSPHRASE_HELPER_MAN) Ns '\'' .
else
DEF_TPH_MAN ?= .
endif
ifneq "$(findstring clang,$(shell $(CXX) --version))" ""
# GCC doesn't have this granularity
CXXFLAGS += -flto=full -pedantic -Wno-gnu-statement-expression -Wno-gnu-include-next -Wno-gnu-conditional-omitted-operand -Wno-c++20-designator -Wno-variadic-macros -Wno-c99-extensions
ifeq "$(AR)" "ar"
# Need this because -flto=full produces objects FSF ranlib (ar s) can't index
override AR := llvm-ar
endif
else
CXXFLAGS += -flto
endif
.PHONY : all locales clean build shellcheck i-t dracut init.d-systemd manpages htmlpages
.SECONDARY :
all : build locales manpages htmlpages shellcheck i-t init.d-systemd dracut
shellcheck : i-t dracut
$(SHELLCHECK) --exclude SC1091,SC2093 $(foreach l,$(OUTDIR)initramfs-tools/ $(OUTDIR)dracut/ init.d/,$(wildcard $(l)*.sh $(l)**/*.sh $(l)**/**/*.sh $(l)**/**/**/*.sh $(l)**/**/**/**/*.sh $(l)**/**/**/**/**/*.sh $(l)**/**/**/**/**/**/*.sh $(l)**/**/**/**/**/**/**/*.sh $(l)**/**/**/**/**/**/**/**/*.sh))
clean :
rm -rf $(OUTDIR)
build : $(subst src/bin/,$(OUTDIR),$(subst .cpp,,$(BINARY_SOURCES)))
manpages : $(patsubst man/%.pp,$(OUTDIR)man/%,$(MANPAGE_SOURCES))
htmlpages : $(patsubst man/%.pp,$(OUTDIR)man/%.html,$(MANPAGE_SOURCES)) $(OUTDIR)man/style.css
locales : $(patsubst po/%.po,$(OUTDIR)locale/%/LC_MESSAGES/tzpfms.mo,$(LOCALES))
i-t : $(OUTDIR)initramfs-tools/usr/share/initramfs-tools/hooks/tzpfms $(OUTDIR)initramfs-tools/usr/share/tzpfms/initramfs-tools-zfs-patch.sh
dracut : $(patsubst initrd/dracut/%,$(OUTDIR)dracut/usr/lib/dracut/modules.d/91tzpfms/%,$(sort $(wildcard initrd/dracut/*.sh)))
init.d-systemd : $(OUTDIR)systemd/$(SYSTEMD_SYSTEM_UNITDIR)/zfs-load-key@.service.d/tzpfms.conf $(OUTDIR)systemd/usr/libexec/tzpfms-zfs-load-key@ $(foreach l,$(subst init.d/systemd/usr-share-tzpfms/,,$(wildcard init.d/systemd/usr-share-tzpfms/*)),$(OUTDIR)systemd/usr/share/tzpfms/$(l))
$(BLDDIR)tzpfms.pot: src/*.[ch]pp src/bin/*.[ch]pp
@mkdir -p $(@D)
$(NOLOCREGEN)xgettext --check=ellipsis-unicode --from-code=UTF-8 -c -io- $^ | sed -n '/^#[:.]/,$$p' > $@
$(NOLOCREGEN)$(foreach l,$(LOCALES),msgmerge --backup=off --no-wrap -Uiq $(l) $@ &&) :
@>> $@
$(OUTDIR)locale/%/LC_MESSAGES/tzpfms.mo : po/%.po $(BLDDIR)tzpfms.pot
@mkdir -p $(@D)
msgfmt --statistics --check-format --check-domain -o $@ $<
$(OUTDIR)initramfs-tools/usr/share/initramfs-tools/hooks/tzpfms: initrd/initramfs-tools/hook $(INITRD_HEADERS)
@mkdir -p $(dir $@)
$(AWK) -f pp.awk $< > $@
chmod --reference $< $@
$(OUTDIR)initramfs-tools/usr/share/tzpfms/initramfs-tools-zfs-patch.sh: initrd/initramfs-tools/zfs-patch.sh $(INITRD_HEADERS)
@mkdir -p $(dir $@)
$(AWK) -f pp.awk $< > $@
chmod --reference $< $@
$(OUTDIR)systemd/$(SYSTEMD_SYSTEM_UNITDIR)/zfs-load-key@.service.d/tzpfms.conf : init.d/systemd/zfs-load-key@.service.d-tzpfms.conf
@mkdir -p $(dir $@)
ln -f $< $@ || cp $< $@
$(OUTDIR)systemd/usr/libexec/tzpfms-zfs-load-key@ : init.d/systemd/libexec-tzpfms-zfs-load-key@.sh
@mkdir -p $(dir $@)
ln -f $< $@ || cp $< $@
$(OUTDIR)systemd/usr/share/tzpfms/% : init.d/systemd/usr-share-tzpfms/%
@mkdir -p $(dir $@)
ln -f $< $@ || cp $< $@
# The d-v-o-s string starts at "BSD" (hence the "BSD General Commands Manual" default); we're not BSD, so hide it
# Can't put it at the very top, since man(1) only loads mdoc *after* the first mdoc macro (.Dd in our case)
$(OUTDIR)man/% : man/%.pp $(MANPAGE_HEADERS)
@mkdir -p $(dir $@)
$(AWK) -f pp.awk $< TZPFMS_PASSPHRASE_HELPER_MAN='$(DEF_TPH_MAN)' | $(AWK) '/^$$/ {prev_empty=1; next} $$1 == "#" && $$2 ~ /^[0-9]*$$/ {prev_empty=0; next} {if(prev_empty) print ""; prev_empty=0; print}' | $(AWK) '$$0 == ".Dd" {$$2 = "$(call MANUAL_DATE,$<)"} $$1 == ".Dt" { print ".ds doc-volume-operating-system" } $$0 == ".Os" {$$2 = "tzpfms"; $$3 = $(TZPFMS_VERSION)} {print}' > $@
! $(MANDOC) -Tlint $@ 2>&1 | grep -vE -e 'mandoc: outdated mandoc.db' -e 'STYLE: referenced manual not found' -e 'STYLE: operating system explicitly specified: Os tzpfms' -e 'WARNING: cross reference to self: Xr zfs-tpm.*-change-key 8' -e 'STYLE: input text line longer than 80 bytes'
# The "WARNING: unknown font, skipping request: TS.+fC[RBI]" one: see https://bugs.debian.org/992002
$(OUTDIR)man/%.html : $(OUTDIR)man/%
@mkdir -p $(dir $@)
( cd $(OUTDIR)man/ && $(MANDOC) -Thtml -Ostyle="style.css",man="%N.%S.html;https://manpages.debian.org/bookworm/%N.%S" ) < $< | \
$(AWK) '/^<h1/ {in_syn = $$0 ~ /id="SYNOPSIS"/} /^<br/ {if(in_syn) next} {print}' | \
$(SED) -Ee 's/ title=".."//g' -e 's/<a class="permalink" href="#([^"]*)"><span class="No" id="([^"]*)">/<a><span class="No">/g' -e 's#manpages.debian.org/[^/]*/ESYS_CONTEXT.3#mankier.com/3/ESYS_CONTEXT#g' > $@
$(OUTDIR)man/style.css : man/style.css
@mkdir -p $(dir $@)
cp $^ $@
$(BLDDIR)libtzpfms.a : $(subst src/,$(BLDDIR)obj/,$(subst .cpp,.o,$(COMMON_SOURCES)))
$(AR) --thin crs $@ $^
$(OUTDIR)% : $(BLDDIR)obj/bin/%.o $(BLDDIR)libtzpfms.a
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BLDDIR)obj/%.o : src/%.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
$(OUTDIR)dracut/usr/lib/dracut/modules.d/91tzpfms/% : initrd/dracut/% $(INITRD_HEADERS)
@mkdir -p $(dir $@)
$(AWK) -f pp.awk $< > $@
chmod --reference $< $@
include $(wildcard $(BLDDIR)*/*.d $(BLDDIR)*/*/*.d)