X-Git-Url: https://git.ladys.computer/Shushe/blobdiff_plain/8b898cc1e883b2a5b3662a5c6b6913d5d0e7236d..687dbc6036a8611ce377621818f9f4112ec6c2b3:/GNUmakefile
diff --git a/GNUmakefile b/GNUmakefile
index 1cf21d2..d0e4346 100644
--- a/GNUmakefile
+++ b/GNUmakefile
@@ -6,15 +6,14 @@ SHELL = /bin/sh
# ━ § BASIC INFORMATION ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
override define makefileinfo
╭────────────────────────────╮
-╔╡ ⁌ ⛩️📰 书社 ∷ GNUmakefile ╞═════════════════════════════════╗
+╔╡ ⁌ ⛩📰 书社 ∷ GNUmakefile ╞═════════════════════════════════╗
║╰────────────────────────────╯ ║
╟┬ ¶ Prerequisites ───────────────────────────────────────────┬╢
║│ │║
-║│ Requires G·N·U Make, at least version 3.81, a version of │║
-║│ uuencode with base64 support, and the various programs │║
-║│ offered by libxml2 and libxslt. Beyond this, only programs │║
-║│ required by Posix are required, altho there is a chance of │║
-║│ version incompatibilities. The full list of program │║
+║│ Requires G·N·U Make, at least version 3.81, the Fine Free │║
+║│ File Command, and the various programs packaged with │║
+║│ libxml2 and libxslt. Beyond this, only programs specified │║
+║│ by Posix are required. The full list of program │║
║│ requirements is as follows :— │║
║│ │║
║│ • awk │║
@@ -23,16 +22,17 @@ override define makefileinfo
║│ • cksum │║
║│ • cp │║
║│ • date │║
-║│ • diff │║
-║│ • file │║
+║│ • diff (with -u) │║
+║│ • file (specifically the Fine Free File Command) │║
║│ • find │║
║│ • grep │║
║│ • git (optional) │║
║│ • ln │║
+║│ • make (specifically G·N·U Make, version 3.81 or later) │║
║│ • mkdir │║
║│ • mv │║
║│ • od │║
-║│ • pax (only when generating archives) │║
+║│ • pax (ustar format, when generating archives) │║
║│ • printf │║
║│ • rm │║
║│ • sed │║
@@ -83,7 +83,7 @@ override define makefileinfo
║╰────────────────────────────────────────────────────────────╯║
╟┬ ¶ Copyright & License ─────────────────────────────────────┬╢
║│ │║
-║│ Copyright © 2023–2024 Lady [@ Lady’s Computer]. │║
+║│ Copyright © 2023–2024 Lady [@ Ladys Computer]. │║
║│ │║
║│ This Source Code Form is subject to the terms of the │║
║│ Mozilla Public License, v 2.0. If a copy of the M·P·L was │║
@@ -149,11 +149,21 @@ SRCDIR := sources
# Multiple directories can be given so long as files with the same name do not exist in each.
#
# These can be inside of `SRCDIR´ directories if desired.
+#
+# This variable is also used by the two‐stage `MODE´, where it gives the include directory for the second stage.
INCLUDEDIR := $(foreach dir,$(SRCDIR),$(dir)/includes)
+# The directory which contains the data files.
+#
+# Only used in the two‐stage `MODE´, and defaults to that `MODE´ if this directory exists.
+# This directory is used as the include directory in the first stage.
+#
+# Multiple directories can be given so long as files with the same name do not exist in each.
+DATADIR :=
+
# The directory in which to generate temporary buildfiles.
#
-# This variable is also used by the archiving `MODE´.
+# This variable is also used by the archiving and two‐stage `MODE´s.
BUILDDIR := build
# The directory into which to output files on `make install´.
@@ -161,21 +171,32 @@ BUILDDIR := build
# This variable is also used by the archiving `MODE´.
DESTDIR := public
-# The location of this Makefile (and related ⛩️📰 书社 files), relative to the current working directory.
+# The location of this Makefile (and related ⛩📰 书社 files), relative to the current working directory.
#
# By default, this is inferred from the variable `MAKEFILE_LIST´.
#
-# This variable is also used by the archiving `MODE´.
+# This variable is also used by the archiving and two‐stage `MODE´s.
THISDIR := $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
# Configuration of `find´.
#
# By default, `find´ will ignore files which begin with a period and those which are likely to cause problems for `make´.
+#
+# These variables are also used by the two‐stage `MODE´.
EXTRAFINDRULES :=
EXTRAFINDINCLUDERULES :=
-FINDRULES := ! '(' '(' -name '[.-]*' -a ! -name '.' -o -name '*[][*?:|$$%\#\\; ]*' -o -name '*[)]' ')' -a -prune ')'$(if $(EXTRAFINDRULES), -a '(' $(EXTRAFINDRULES) ')',)
+FINDRULES := '!' '(' '(' -name '[.-]*' -a '!' -name '.' -o -name '*[][*?:|$$%\#\\; ]*' -o -name '*[)]' ')' -a -prune ')'$(if $(EXTRAFINDRULES), -a '(' $(EXTRAFINDRULES) ')',)
FINDINCLUDERULES := $(FINDRULES)$(if $(EXTRAFINDINCLUDERULES), -a '(' $(EXTRAFINDINCLUDERULES) ')',)
+# File extensions which indicate files in `SRCDIR´ which should be built as part of the first, rather than second, stage of the two‐stage `MODE´.
+DATAEXT := rdf
+
+# Rules for identifying files in `SRCDIR´ which should be built as part of the first, rather than second, stage of the two‐stage `MODE´.
+#
+# By default, this just constructs rules from `DATAEXT´.
+EXTRAFINDDATARULES :=
+FINDDATARULES := -name '.' $(foreach ext,$(DATAEXT), -o -name '$(subst ','"'"',[!.]*.$(ext))')$(if $(EXTRAFINDDATARULES), -a '(' $(EXTRAFINDDATARULES) ')',)
+
# The list of magic files to use when determining media types.
#
# Some are provided as part of this repository, but you can add more if you need different media type detection.
@@ -184,12 +205,24 @@ FINDINCLUDERULES := $(FINDRULES)$(if $(EXTRAFINDINCLUDERULES), -a '(' $(EXTRAFIN
EXTRAMAGIC :=
MAGIC := $(sort $(patsubst ./%,%,$(wildcard $(THISDIR)/magic/*)) $(EXTRAMAGIC))
+# The list of depedencies for parsers.
+#
+# When these files change, the assumption is that the result of the parsers will change as well, even if the parsers themselves have not.
+EXTRAPARSERLIBS :=
+PARSERLIBS := $(sort $(THISDIR)/lib/split.xslt $(EXTRAPARSERLIBS))
+
# The list of parsers for plaintext file types.
#
# Which parsers are provided will influence which kinds of files are recognized as plaintext.
EXTRAPARSERS :=
PARSERS := $(sort $(patsubst ./%,%,$(wildcard $(THISDIR)/parsers/*.xslt)) $(EXTRAPARSERS))
+# The list of depedencies for transforms.
+#
+# When these files change, the assumption is that the result of the transforms will change as well, even if the transforms themselves have not.
+EXTRATRANSFORMLIBS :=
+TRANSFORMLIBS := $(sort $(THISDIR)/lib/serialize.xslt $(EXTRATRANSFORMLIBS))
+
# The list of transforms.
EXTRATRANSFORMS :=
TRANSFORMS := $(sort $(patsubst ./%,%,$(wildcard $(THISDIR)/transforms/*.xslt)) $(EXTRATRANSFORMS))
@@ -205,7 +238,7 @@ FINALIZE := $(XMLLINT) --nonet --nsclean
ifdef GIT
ifneq ($(wildcard $(THISDIR)/.git),)
-# A description of the current git revision of ⛩️📰 书社.
+# A description of the current git revision of ⛩📰 书社.
THISREV := $(shell $(CD) $(THISDIR); $(GIT) describe 2>>/dev/null || $(GIT) rev-parse HEAD 2>>/dev/null || :)
endif
@@ -221,11 +254,14 @@ endif
# The following modes are available :—
#
# • ‹ urn:fdc:ladys.computer:20231231:Shu1She4:mode:default ›:
-# The default mode; typical Shushe behaviours.
+# The default mode; typical ⛩📰 书社 behaviours.
#
# • ‹ urn:fdc:ladys.computer:20231231:Shu1She4:mode:archive ›:
# Generates archive files from parse results.
-MODE := urn:fdc:ladys.computer:20231231:Shu1She4:mode:default
+#
+# • ‹ urn:fdc:ladys.computer:20231231:Shu1She4:mode:_2stage ›:
+# Two‐stage build; runs ⛩📰 书社 twice.
+MODE := urn:fdc:ladys.computer:20231231:Shu1She4:mode:$(if $(DATADIR),$(shell if $(TEST) -d $(call quote,$(DATADIR)); then $(PRINTF) '%s\n' '_2stage'; else $(PRINTF) '%s\n' 'default'; fi),default)
# Set to a non·empty value to silence informative messages.
QUIET :=
@@ -233,6 +269,14 @@ QUIET :=
# Set to a non·empty value to print all commands as they run.
VERBOSE :=
+# Posix locale information.
+LC_ALL := C
+export LC_ALL
+
+# Posix timezone information.
+TZ := UTC0
+export UTC0
+
# The default target for this makefile.
.DEFAULT_GOAL := all
@@ -261,19 +305,12 @@ override space := $(empty) $(empty)
# A variable which contains a single comma.
override comma := ,
+# (callable) Make empty strings non·empty, and non·empty strings empty.
+override not = $(if $1,,1)
+
# (callable) Quote the given string for use within shell calls.
override quote = '$(subst ','"'"',$1)'
-# (callable) Get the modified time of the provided file.
-#
-# This touches a file containing only a newline and then diffs it with `/dev/null´ and extracts the timestamp from the diff.
-# Interestingly, on Macintosh, the format of `diff -u´ is only Posixy (including a fractional component and timezone) when `COMMAND_MODE=legacy´; however, the timezone will always be zero and the fractional component is ignorable, so it’s not necessary to worry about that here.
-#
-# The diff will always have an exit status of 1, but this is ignored by piping into Sed.
-#
-# ☡ This variable creates a subshell every time it is computed.
-override modtime = $(shell if $(TEST) ! -f $(call quote,$(BUILDDIR)/.mtime); then $(PRINTF) '%b' '\n' > $(call quote,$(BUILDDIR)/.mtime); fi; $(TOUCH) -r $(call quote,$1) $(call quote,$(BUILDDIR)/.mtime); TZ=UTC0 $(DIFF) -u $(call quote,$(BUILDDIR)/.mtime) /dev/null | $(SED) '1!d;s/.* \([^ ]*\) \([^ ]*\).*$$/\1T\2Z/')
-
# ─ ¶ Recipe Variable Definitions ─────────────────────────────────────
# Outputs an `@´ to silence rules, unless `VERBOSE´ is non·empty.
@@ -297,7 +334,7 @@ override xargsmultiquote = $(SED) $(call quote,s/'/'"'"'/g;s/^/'/;s/$$/'/)
override xpath = $(XMLLINT) --noent --nonet --xpath $(call quote,$1) $(call quote,$2) >>/dev/null 2>>/dev/null
# (callable) Extract the value of the text nodes in the provided X·M·L document and print them to `stdout´.
-override extracttext = $(PRINTF) '%s' '' | $(XSLTPROC) --nonet --novalid - $(call quote,$1)
+override extracttext = $(PRINTF) '%s' '' | $(XSLTPROC) --nonet --novalid --nomkdir --nowrite - $(call quote,$1)
# (callable) Process the provided transformation result and output the result to the provided location, given the provided relative path.
override processresultto = if $(call xpath,/*[local-name()="raw-text" and namespace-uri()="urn:fdc:ladys.computer:20231231:Shu1She4"],$1); then $(call extracttext,$1) >|$(call quote,$2); elif $(call xpath,/*[local-name()="base64-binary" and namespace-uri()="urn:fdc:ladys.computer:20231231:Shu1She4"],$1); then { $(PRINTF) '%s\n' 'begin-base64 644 -'; $(call extracttext,$1) | $(TR) -d '\t\n\f\r '; $(PRINTF) '\n%s\n' '===='; } | $(UUDECODE) -o /dev/stdout >|$(call quote,$2); elif $(call xpath,/*[local-name()="archive" and namespace-uri()="urn:fdc:ladys.computer:20231231:Shu1She4"],$1); then $(MAKE) -f $(call quote,$(abspath $(THISDIR)/GNUmakefile)) NAME=$(call quote,$3) SRC=$(call quote,$1) BUILDDIR=$(call quote,$(BUILDDIR)/archive/$3) DESTDIR=$(call quote,$(patsubst %/,%,$(dir $2))) MODE='urn:fdc:ladys.computer:20231231:Shu1She4:mode:archive' $(call quote,$2); else $(FINALIZE) $(call quote,$1) >|$(call quote,$2); fi
@@ -308,14 +345,19 @@ ifeq ($(MODE),urn:fdc:ladys.computer:20231231:Shu1She4:mode:default)
# ─ ¶ Non‐Recipe Variable Definitions ─────────────────────────────────
-# (callable) Test to see if the prerequisites provided by the second argument matches the value in the file corresponding to the first argument in `$(BUILDDIR)/lastprereqs´.
-# If not, save the new value and then add FORCE.
-# Return them regardless.
+# Non·empty if `help´ or `clean´, and no targets other than `help´ or `clean´, were specified as goals on the commandline.
+override notbuilding := $(and $(filter help clean,$(MAKECMDGOALS)),$(call not,$(filter-out help clean,$(MAKECMDGOALS))))
+
+# (callable) Tests to see if the prerequisites provided by the second argument matches the value in the file corresponding to the first argument in `$(BUILDDIR)/lastprereqs´.
+# If not, saves the new value.
+# Returns the values plus the file in `$(BUILDDIR)/lastprereqs´, which will always be newer than the target if there was a change.
#
# Calling this variable is useful when a given target should be updated whenever its list of prerequisites changes in addition to whenever there is a change to one of its prerequisites.
#
+# If `$(notbuilding)´ is non·empty, this variable produces no result to avoid unnecessary work.
+#
# ☡ This variable creates at least one subshell every time it is computed.
-override diffprereqs = $(if $(subst $(shell $(CAT) $(call quote,$(BUILDDIR)/lastprereqs/$1) 2>>/dev/null || :),,$2),$2 FORCE$(and $(shell $(call ensuredirectory,$(BUILDDIR)/lastprereqs) && $(PRINTF) '%s\n' $(call quote,$2) >|$(BUILDDIR)/lastprereqs/$1),),$2)
+override diffprereqs = $(if $(notbuilding),,$(and $(subst $(shell $(CAT) $(call quote,$(BUILDDIR)/lastprereqs/$1) 2>>/dev/null || :),,$2),$(shell $(call ensuredirectory,$(BUILDDIR)/lastprereqs) && $(PRINTF) '%s\n' $(call quote,$2) >|$(BUILDDIR)/lastprereqs/$1),)$2 $(BUILDDIR)/lastprereqs/$1)
# (callable) Escape special characters for use in X·M·L.
override xmlesc = $(subst >,>,$(subst <,<,$(subst &,&,$1)))
@@ -381,7 +423,7 @@ override plaintextfiles := $(filter-out $(xmlfiles),$(call filesoftype,$(plainte
override assetfiles := $(filter-out $(xmlfiles) $(plaintextfiles),$(sourcefiles) $(sourceincludes))
# (callable) Get the types of the given files.
-override typeoffile = $(foreach file,$1,$(or $(patsubst $(file)|%,%,$(filter $(file)|%,$(types))),$(error Unable to get type of file `$(file)´)))
+override typeoffile = $(foreach file,$1,$(or $(patsubst $(file)|%,%,$(filter $(file)|%,$(types))),application/octet-stream))
# Pair each source magic file with its location in the build directory.
override magicpair := $(foreach magicfile,$(MAGIC),$(magicfile)|$(BUILDDIR)/magic/$(call namehash,$(magicfile)))
@@ -416,7 +458,7 @@ override sourcefile = $(foreach local,$1,$(patsubst %|$(local),%,$(filter %|$(lo
#
# This file is created before a reload due to type changes, and is removed after.
# This ensures that file classifications are up·to·date immediately after the reload.
-override typeupdates := $(if $(wildcard $(BUILDDIR)/.update-types),FORCE,)
+override typeupdates := $(and $(wildcard $(BUILDDIR)/.update-types),FORCE)
# Pair each source file and include with its metadata location.
override sourcemetadatapair := $(foreach file,$(sourcefiles) $(sourceincludes),$(file)|$(BUILDDIR)/$(if $(filter $(file),$(sourceincludes)),includes.metadata/$(call includepath,$(file)),sources.metadata/$(call sourcepath,$(file))))
@@ -436,10 +478,10 @@ override parsed = $(foreach file,$1,$(patsubst $(file)|%,%,$(filter $(file)|%,$(
# (callable) Get the source files for the given parsed file.
override unparsed = $(foreach file,$1,$(patsubst %|$(file),%,$(filter %|$(file),$(sourceparsedpair))))
-# Pair each parser, transform, source file, or parsed file with its file u·r·i.
-override fileuripairs := $(join $(patsubst %,%|,$(PARSERS) $(TRANSFORMS) $(sourcefiles) $(sourceincludes) $(call parsed,$(sourcefiles) $(sourceincludes))),$(call pathenc,$(foreach uriable,$(PARSERS) $(TRANSFORMS) $(sourcefiles) $(sourceincludes) $(call parsed,$(sourcefiles) $(sourceincludes)),file://$(abspath $(uriable)))))
+# Pair each build directory, parser, transform, source file, or parsed file with its file u·r·i.
+override fileuripairs := $(join $(patsubst %,%|,$(BUILDDIR) $(PARSERS) $(TRANSFORMS) $(sourcefiles) $(sourceincludes) $(call parsed,$(sourcefiles) $(sourceincludes))),$(call pathenc,$(foreach uriable,$(BUILDDIR) $(PARSERS) $(TRANSFORMS) $(sourcefiles) $(sourceincludes) $(call parsed,$(sourcefiles) $(sourceincludes)),file://$(abspath $(uriable)))))
-# (callable) Get the file u·r·is for the given parsers, transforms, or parsed files.
+# (callable) Get the file u·r·is for the given parsers, transforms, source file or parsed files.
override fileuri = $(foreach file,$1,$(or $(patsubst $(file)|%,%,$(filter $(file)|%,$(fileuripairs))),$(error Unable to get file u·r·i for `$(file)´)))
ifneq ($(wildcard $(BUILDDIR)/dependencies),)
@@ -480,12 +522,13 @@ ifneq ($(wildcard $(BUILDDIR)/destinations),)
override destinations := $(shell $(CAT) $(BUILDDIR)/destinations)
# Pair source files and their destinations.
-override sourcedestinationpair := $(foreach destination,$(destinations),$(call sourcefile,$(firstword $(subst |, ,$(destination))))|$(call perdec,$(subst $(space),|,$(wordlist 2,$(words $(subst |, ,$(destination))),$(subst |, ,$(destination))))))
+override sourcedestinationpair := $(foreach destination,$(destinations),$(call sourcefile,$(firstword $(subst |, ,$(destination))))|$(subst $(space),|,$(wordlist 2,$(words $(subst |, ,$(destination))),$(subst |, ,$(destination)))))
+endif
-# (callable) Get the destination for the given source files, or return `NOTDEF´.
+# (callable) Get the destination for the given source files, or return `.NOTDEF/$1´.
#
-# The fallback here is because destinations are used to generate targets and thus must always be non·empty, even when they haven’t been generated yet.
-override destination = $(foreach file,$1,$(or $(patsubst $(file)|%,%,$(filter $(file)|%,$(sourcedestinationpair))),NOTDEF))
+# The fallback here is because destinations are used to generate targets and thus must always be non·empty and should be unique, even when they haven’t been generated yet.
+override destination = $(foreach file,$1,$(or $(patsubst $(file)|%,%,$(filter $(file)|%,$(sourcedestinationpair))),.NOTDEF/$1))
# Pair each source file with its compiled location.
override sourcecompiledpair := $(foreach file,$(sourcefiles),$(file)|$(BUILDDIR)/results/$(call destination,$(file)))
@@ -504,7 +547,6 @@ override unbuilt = $(foreach file,$1,$(call uncompiled,$(patsubst $(BUILDDIR)/pu
# (callable) Get the installed locations for the given source files.
override installed = $(foreach file,$1,$(DESTDIR)/$(call destination,$(file)))
-endif
# ─ ¶ Recipe Variable Definitions ─────────────────────────────────────
@@ -521,6 +563,9 @@ override wrapplaintext = { $(PRINTF) '%s\n%s' '' '