║│ • cksum │║
║│ • cp │║
║│ • date │║
+║│ • diff │║
║│ • file │║
║│ • find │║
║│ • grep │║
║│ • git (optional) │║
║│ • ln │║
-║│ • ls │║
║│ • mkdir │║
║│ • mv │║
║│ • od │║
CKSUM := cksum
CP := cp
DATE := date
+DIFF := diff
FILE := file
FIND := find
GIT := git
GREP := grep
LN := ln
-LS := ls
MKDIR := mkdir
MV := mv
OD := od
# Generates archive files from parse results.
MODE := urn:fdc:ladys.computer:20231231:Shu1She4:mode:default
+# Set to a non·empty value to silence informative messages.
+QUIET :=
+
# Set to a non·empty value to print all commands as they run.
VERBOSE :=
# (callable) Quote the given string for use within shell calls.
override quote = '$(subst ','"'"',$1)'
-# Outputs an `@´ to silence rules, unless `VERBOSE´ is nonempty.
-override silent := $(if $(VERBOSE),,@)
-
# (callable) Get the modified time of the provided file.
#
-# Only the date·time information available via `ls -l´ is supported; that is, only minute precision for files newer than six months, and only day precision for files which are older.
-# This is, unfortunately, the best that can be done in pure Posix as of now.
-#
-# There is an easier implementation which provides the full date·time by archiving an empty file and then reading it back out of the archive :—
+# 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.
#
-# $(shell LC_TIME=POSIX TZ=UTC0 $(TOUCH) -m -r $(callquote,$1) $(BUILDDIR)/.mtime && $(PAX) -w -x ustar $(BUILDDIR)/.mtime | $(PAX) -v -o 'listopt=%(mtime=%Y-%m-%dT%H:%M:%SZ)T')
-#
-# However, it depends on the `listopt´ option in `pax´, which (despite being specified in Posix) no implementations actually support.
+# 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 { $(DATE) -u '+%Y-%m'; LC_TIME=POSIX TZ=UTC0 ls -q -A -l $(call quote,$1) | $(SED) 's/\([^ ]* \{1,\}\)\{5\}\(\([^ ]* \{1,\}\)\{3\}\).*/\2/;s/ \([0123456789]\) / 0\1 /;s/Jan/01/;s/Feb/02/;s/Mar/03/;s/Apr/04/;s/May/05/;s/Jun/06/;s/Jul/07/;s/Aug/08/;s/Sep/09/;s/Oct/10/;s/Nov/11/;s/Dec/12/;s/\([0123456789]\{2\}\) *\([0123456789]\{2\}\) *\([0123456789]\{4\}\)/\3-\1-\2T00:00:00Z/;s/\([0123456789]\{2\}\) *\([0123456789]\{2\}\) *\([0123456789:]\{5\}\)/XXXX-\1-\2T\3:00Z/'; } | $(TR) -d ' ' | $(AWK) -F '-' 'NR==1{year=$$1;month=$$2}$$1=="XXXX"&&$$2<=month{print year"-"$$2"-"$$3}$$1=="XXXX"&&$$2>month{print year-1"-"$$2"-"$$3}NR>1&&$$1!="XXXX"')
+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.
+override silent := $(if $(VERBOSE),,@)
+
+# (callable) Only perform the rule if `QUIET` is not set.
+override inform = $(if $(QUIET),,@$1)
+
# (callable) Check to see if the given directory exists and create it if not.
override ensuredirectory = if $(TEST) ! -d $(call quote,$1); then $(MKDIR) -p $(call quote,$1); fi
override assetfiles := $(filter-out $(xmlfiles) $(plaintextfiles),$(sourcefiles) $(sourceincludes))
# (callable) Get the types of the given files.
-override typeoffile = $(patsubst $(foreach file,$1,$(file)|%),%,$(filter $(foreach file,$1,$(file)|%),$(types)))
+override typeoffile = $(foreach file,$1,$(patsubst $(file)|%,%,$(filter $(file)|%,$(types))))
# Pair each source magic file with its location in the build directory.
override magicpair := $(foreach magicfile,$(MAGIC),$(magicfile)|$(BUILDDIR)/magic/$(notdir $(magicfile)))
# (callable) Get the source files for the given local uris.
override sourcefile = $(foreach local,$1,$(patsubst %|$(local),%,$(filter %|$(local),$(sourcelocalpair))))
-# Adds a requirement on `$(BUILDDIR)/.update-types´ if the file is present.
+# Resolves to the `FORCE´ target if `$(BUILDDIR)/.update-types´ is present.
#
-# This file is created after a reload due to type changes, and is removed after.
-# Requiring it ensures that file classifications are up·to·date immediately after the reload.
-override typeupdates := $(wildcard $(BUILDDIR)/.update-types)
+# 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,)
# Pair each source file and include with its parsed location.
override sourceparsedpair := $(foreach file,$(sourcefiles) $(sourceincludes),$(file)|$(BUILDDIR)/$(if $(filter $(file),$(sourceincludes)),includes/$(call includepath,$(file)),sources/$(call sourcepath,$(file))))
# Lists out the destinations of all resulting files (relative to `DESTDIR´).
listout :
- @$(PRINTF) '⛩️📰 书社 Output: %s\n' $(call quote,$(foreach file,$(sort $(sourcefiles)),$(call destination,$(file))))
+ @$(PRINTF) '%s\n' $(call quote,$(foreach file,$(sort $(sourcefiles)),$(call destination,$(file))))
# Destroy installed files.
uninstall :
# Raise an error when attempting to build any files with recursive dependencies.
$(call built,$(recursivefiles)) :
- @$(PRINTF) '%b\n' $(call quote,\0033[93;41mError:\0033[39;49m `$(call unbuilt,$@)´ has recursive dependencies:\n$(subst |, ,$(subst $(space),$(newline),$(foreach recursive,$(call recursives,$(call unbuilt,$@)),•|$(recursive))))) && false
+ @$(PRINTF) '%b\n' $(call quote,\0033[93;41mError:\0033[39;49m `$(call unbuilt,$@)´ has recursive dependencies:\n$(subst |, ,$(subst $(space),$(newline),$(foreach recursive,$(call recursives,$(call unbuilt,$@)),•|$(recursive))))) >&2 && false
# Add as a prerequisite to treat the target as tho it were phony.
FORCE : ;
$(THISDIR)/GNUmakefile :: $(BUILDDIR)/dependencies $(BUILDDIR)/destinations
$(silent)$(TOUCH) $(THISDIR)/GNUmakefile
ifeq ($(typeupdates),)
- @if $(TEST) ! -f $(call quote,$(BUILDDIR)/.update-types); then $(PRINTF) '%b\n' '\0033[1mDependency graph and output destinations updated. Restarting…\0033[22m'; fi
+ $(call inform,if $(TEST) ! -f $(call quote,$(BUILDDIR)/.update-types); then $(PRINTF) '%b\n' '\0033[1mDependency graph and output destinations updated. Restarting…\0033[22m' >&2; fi)
else
$(silent)$(RM) $(call quote,$(BUILDDIR)/.update-types)
- @$(PRINTF) '%b\n' '\0033[1mDependency graph and output destinations updated. Restarting…\0033[22m'
+ $(call inform,$(PRINTF) '%b\n' '\0033[1mDependency graph and output destinations updated. Restarting…\0033[22m' >&2)
endif
endif
$(THISDIR)/GNUmakefile :: $(BUILDDIR)/magic.mgc $(BUILDDIR)/parser.xslt
$(silent)$(TOUCH) $(THISDIR)/GNUmakefile
$(silent)$(RM) -f $(call quote,$(BUILDDIR)/dependencies) $(call quote,$(BUILDDIR)/destinations)
- @$(PRINTF) '%b\n' '\0033[1mMagic file or parsers have updated. Restarting…\0033[22m'
+ $(call inform,$(PRINTF) '%b\n' '\0033[1mMagic file or parsers have updated. Restarting…\0033[22m' >&2)
$(silent)$(SLEEP) 1
$(silent)$(TOUCH) $(call quote,$(BUILDDIR)/.update-types)
endif
# It must be updated if any of the files in the magic directory change.
$(BUILDDIR)/magic.mgc : $(call diffprereqs,magic,$(sort $(call magicfile,$(MAGIC))))
$(foreach outdated,$(filter-out $^,$(wildcard $(BUILDDIR)/magic/*)),$(silent)$(RM) $(call quote,$(outdated))$(newline))
- @$(PRINTF) '%s\n' 'Compiling new magic…'
+ $(call inform,$(PRINTF) '%s\n' 'Compiling new magic…' >&2)
$(silent)$(call ensuredirectory,$(dir $@))
$(silent)cd $(call quote,$(BUILDDIR)) && $(FILE) -C -m $(call quote,$(realpath $(BUILDDIR)/magic))
$(silent)$(TOUCH) $(call quote,$(BUILDDIR)/.update-types)
# Generate the main parser.
$(BUILDDIR)/parser.catalog : $(call diffprereqs,parsers,$(sort $(PARSERS)))
- @$(PRINTF) '%s\n' 'Generating catalog of parsers…'
+ $(call inform,$(PRINTF) '%s\n' 'Generating catalog of parsers…' >&2)
$(silent)$(XMLCATALOG) --create --noout $(call quote,$@)
$(foreach parser,$(PARSERS),$(silent){ $(call id,$(parser)); $(PRINTF) '%s\n' $(call quote,$(call fileuri,$(parser))) '--noout' $(call quote,$@); } | $(xargsmultiquote) | $(XARGS) -E '' $(XMLCATALOG) --add uri$(newline))
$(BUILDDIR)/parser.xslt : $(BUILDDIR)/parser.catalog $(THISDIR)/lib/catalog2parser.xslt
- @$(PRINTF) '%s\n' 'Generating main parser…'
+ $(call inform,$(PRINTF) '%s\n' 'Generating main parser…' >&2)
$(silent)$(XSLTPROC) --nonet --novalid -o $(call quote,$@) $(call quote,$(THISDIR)/lib/catalog2parser.xslt) $(call quote,$<)
$(silent)$(TOUCH) $(call quote,$(BUILDDIR)/.update-types)
# Even plain X·M·L files are parsed, because they may contain X·H·T·M·L `<script>´ elements which contain other kinds of data.
# Asset files are turned into H·T·M·L embeds pointing to `data:´ U·R·I’s.
$(call parsed,$(sourcefiles) $(sourceincludes)) : % : $$(call unparsed,$$@) $(BUILDDIR)/parser.xslt $(typeupdates)
- @$(PRINTF) '%s\n' $(call quote,Processing `$<´…)
+ $(call inform,$(PRINTF) '%s\n' $(call quote,Processing `$<´…) >&2)
$(silent)$(call ensuredirectory,$(dir $@))
$(silent)$(if $(filter $<,$(assetfiles)),$(PRINTF) '%s\n' $(call quote,<object xmlns="http://www.w3.org/1999/xhtml" type="$(call typeoffile,$<)" data="$(call datauri,$<)"/>) > $(call quote,$@),$(if $(filter $<,$(plaintextfiles)),$(call wrapplaintext,$<),$(call serializexml,$<)) | $(XSLTPROC) --nonet --novalid -o $(call quote,$@) --stringparam BUILDTIME $$($(DATE) -u '+%Y-%m-%dT%H:%M:%SZ') --stringparam IDENTIFIER $(call quote,$(call localuri,$<)) --stringparam SRCTIME '$(call modtime,$<)' --stringparam CKSUM $$($(CKSUM) $(call quote,$<) | $(SED) 's/[ ].*//')$(if $(THISREV), --stringparam THISREV $(call quote,$(THISREV)),)$(if $(SRCREV), --stringparam SRCREV $(call quote,$(SRCREV)),) $(call quote,$(BUILDDIR)/parser.xslt) -)
#
# This does not depend on actually transforming the files.
$(BUILDDIR)/catalog : $(call diffprereqs,sources,$(sort $(sourcefiles) $(sourceincludes))) $(typeupdates)
- @$(PRINTF) '%s\n' 'Generating catalog of parsed files…'
+ $(call inform,$(PRINTF) '%s\n' 'Generating catalog of parsed files…' >&2)
$(silent)$(XMLCATALOG) --create --noout $(call quote,$@)
$(foreach source,$(sourcefiles) $(sourceincludes),$(silent)$(XMLCATALOG) --add uri $(call quote,$(call localuri,$(source))) $(call quote,$(call fileuri,$(call parsed,$(source)))#$(if $(filter $(source),$(assetfiles)),asset,xml)) --noout $(call quote,$@)$(newline))
# Build a list of dependencies for each parsed file.
$(BUILDDIR)/dependencies : $(BUILDDIR)/catalog $(call parsed,$(plaintextfiles) $(xmlfiles)) $(THISDIR)/lib/catalog2dependencies.xslt
- @$(PRINTF) '%s\n' 'Identifying dependencies…'
+ $(call inform,$(PRINTF) '%s\n' 'Identifying dependencies…' >&2)
$(silent)$(TOUCH) $(call quote,$@)
$(silent)$(XSLTPROC) --nonet --novalid -o $(call quote,$@) $(call quote,$(THISDIR)/lib/catalog2dependencies.xslt) $(call quote,$<)
# This depends on parsing non·asset source files, but not assets or includes.
# It does not require knowing the dependencies.
$(BUILDDIR)/destinations : $(BUILDDIR)/catalog $(call parsed,$(filter-out $(assetfiles),$(sourcefiles))) $(THISDIR)/lib/catalog2destinations.xslt
- @$(PRINTF) '%s\n' 'Identifying output destinations…'
+ $(call inform,$(PRINTF) '%s\n' 'Identifying output destinations…' >&2)
$(silent)$(TOUCH) $(call quote,$@)
$(silent)$(XSLTPROC) --nonet --novalid -o $(call quote,$@) $(call quote,$(THISDIR)/lib/catalog2destinations.xslt) $(call quote,$<)
# Generate the main transform.
$(BUILDDIR)/transform.catalog : $(call diffprereqs,transforms,$(sort $(TRANSFORMS)))
- @$(PRINTF) '%s\n' 'Generating catalog of transforms…'
+ $(call inform,$(PRINTF) '%s\n' 'Generating catalog of transforms…' >&2)
$(silent)$(XMLCATALOG) --create --noout $(call quote,$@)
$(foreach transform,$(TRANSFORMS),$(silent){ $(call id,$(transform)); $(PRINTF) '%s\n' $(call quote,$(call fileuri,$(transform))) '--noout' $(call quote,$@); } | $(xargsmultiquote) | $(XARGS) -E '' $(XMLCATALOG) --add uri$(newline))
$(BUILDDIR)/transform.xslt : $(BUILDDIR)/transform.catalog $(THISDIR)/lib/catalog2transform.xslt
- @$(PRINTF) '%s\n' 'Generating main transform…'
+ $(call inform,$(PRINTF) '%s\n' 'Generating main transform…' >&2)
$(silent)$(XSLTPROC) --nonet --novalid -o $(call quote,$@) $(call quote,$(THISDIR)/lib/catalog2transform.xslt) $(call quote,$<)
# Compile the result files using the dependencies as necessary.
$(call compiled,$(compilablefiles)) : $(BUILDDIR)/results/% : $$(call parsed,$$(call uncompiled,$$@)) $(BUILDDIR)/transform.xslt $$(call parsed,$$(call dependencies,$$(call uncompiled,$$@)))
- @$(PRINTF) '%s\n' $(call quote,Compiling </$*>…)
+ $(call inform,$(PRINTF) '%s\n' $(call quote,Compiling </$*>…) >&2)
$(silent)$(call ensuredirectory,$(dir $@))
$(silent)$(XSLTPROC) --nonet --novalid -o $(call quote,$@) --stringparam CATALOG 'catalog' --stringparam BUILDTIME $$($(DATE) -u '+%Y-%m-%dT%H:%M:%SZ') --stringparam SRCTIME '$(call modtime,$(call uncompiled,$@))' --stringparam IDENTIFIER $(call quote,$(call localuri,$(call uncompiled,$@))) --stringparam PATH $(call quote,/$*) --stringparam CKSUM $$($(CKSUM) $(call quote,$(call uncompiled,$@)) | $(SED) 's/[ ].*//')$(if $(THISREV), --stringparam THISREV $(call quote,$(THISREV)),)$(if $(SRCREV), --stringparam SRCREV $(call quote,$(SRCREV)),) $(call quote,$(BUILDDIR)/transform.xslt) $(call quote,$<)
# Create the final files from the compiled results (or error in the case of recursive ones).
$(call built,$(compilablefiles)) : $(BUILDDIR)/public/% : $(BUILDDIR)/results/%
- @$(PRINTF) '%s\n' $(call quote,Building </$*>…)
+ $(call inform,$(PRINTF) '%s\n' $(call quote,Building </$*>…) >&2)
$(silent)$(call ensuredirectory,$(dir $@))
$(silent)$(RM) -f -R $(call quote,$@)
$(silent)$(call processresultto,$<,$@,$*)
$(call built,$(filter $(assetfiles),$(sourcefiles))) : $(BUILDDIR)/public/% : $$(call unbuilt,$$@)
- @$(PRINTF) '%s\n' $(call quote,Building </$*>…)
+ $(call inform,$(PRINTF) '%s\n' $(call quote,Building </$*>…) >&2)
$(silent)$(call ensuredirectory,$(dir $@))
$(silent)$(RM) -f -R $(call quote,$@)
$(silent)$(CP) $(call quote,$<) $(call quote,$@)
# Install compiled files (or error in the case of recursive ones).
$(call installed,$(recursivefiles) $(installablefiles)) : $(DESTDIR)/% : $(BUILDDIR)/public/%
- @$(PRINTF) '%s\n' $(call quote,Installing </$*>…)
+ $(call inform,$(PRINTF) '%s\n' $(call quote,Installing </$*>…) >&2)
$(silent)$(call ensuredirectory,$(dir $@))
$(silent)$(RM) -f -R $(call quote,$@)
$(silent)$(CP) -R $(call quote,$<) $(call quote,$@)
# Reload this make·file if the archive components have changed.
$(THISDIR)/GNUmakefile : $(BUILDDIR)/index
$(silent)$(TOUCH) $(THISDIR)/GNUmakefile
- @$(PRINTF) '%b\n' $(call quote,\0033[1mArchive components for </$(NAME)> updated. Restarting…\0033[22m)
+ $(call inform,$(PRINTF) '%b\n' $(call quote,\0033[1mArchive components for </$(NAME)> updated. Restarting…\0033[22m) >&2)
# ─ ¶ Build Targets ───────────────────────────────────────────────────
#
# This target sleeps for 1 second to ensure the resulting index will always be newer than this make·file.
$(BUILDDIR)/index : $(BUILDDIR)/extractor.xslt $(SRC)
- @$(PRINTF) '%s\n' $(call quote,Extracting components for </$(NAME)>…)
+ $(call inform,$(PRINTF) '%s\n' $(call quote,Extracting components for </$(NAME)>…) >&2)
$(silent)$(SLEEP) 1
$(silent)$(XSLTPROC) --nonet --novalid -o $(call quote,$@) --writesubtree $(call quote,$(dir $@)) $(call quote,$<) $(call quote,$(SRC))
# Process each extractor component into its final form.
$(foreach file,$(archivefiles),$(BUILDDIR)/files/$(file)) : $(BUILDDIR)/files/% : $(BUILDDIR)/extracted/%
- @$(PRINTF) '%s\n' $(call quote,Building </$(NAME)/$*>…)
+ $(call inform,$(PRINTF) '%s\n' $(call quote,Building </$(NAME)/$*>…) >&2)
$(silent)$(call ensuredirectory,$(dir $@))
$(silent)$(RM) -f -R $(call quote,$@)
$(silent)$(call processresultto,$<,$@,$*)
#
# This is a match‐anything target, with the assumption that this make·file is being called recursively from the default mode.
$(DESTDIR)/% : $(SRC) $(foreach file,$(archivefiles),$(BUILDDIR)/files/$(file))
- @$(PRINTF) '%s\n' $(call quote,Archiving </$(NAME)>…)
+ $(call inform,$(PRINTF) '%s\n' $(call quote,Archiving </$(NAME)>…) >&2)
$(silent)$(call ensuredirectory,$(dir $@))
$(silent)$(RM) -f -R $(call quote,$@)
$(silent)cd $(call quote,$(BUILDDIR)/files); if $(call xpath,/*/@*[local-name()="expanded" and namespace-uri()="urn:fdc:ladys.computer:20231231:Shu1She4"],$(abspath $<)); then $(MKDIR) -p $(call quote,$(abspath $@)); $(PRINTF) '%s\n' $(foreach file,$(archivefiles),$(call quote,$(file))) | $(PAX) -r -w $(call quote,$(abspath $@)); else $(PRINTF) '%s\n' $(foreach file,$(archivefiles),$(call quote,$(file))) | $(PAX) -w -x ustar > $(call quote,$(abspath $@)); fi