X-Git-Url: https://git.ladys.computer/Shushe/blobdiff_plain/33cd26f85eec175028a9e7ced1562e34ff3d15d6..5e96540e69e1b5bd59753284b26439419745168a:/GNUmakefile?ds=sidebyside diff --git a/GNUmakefile b/GNUmakefile index 338c55d..f4161de 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -22,12 +22,12 @@ override define makefileinfo ║│ • cksum │║ ║│ • cp │║ ║│ • date │║ +║│ • diff │║ ║│ • file │║ ║│ • find │║ ║│ • grep │║ ║│ • git (optional) │║ ║│ • ln │║ -║│ • ls │║ ║│ • mkdir │║ ║│ • mv │║ ║│ • od │║ @@ -102,12 +102,12 @@ CAT := cat 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 @@ -193,7 +193,10 @@ EXTRATRANSFORMS := TRANSFORMS := $(sort $(patsubst ./%,%,$(wildcard $(THISDIR)/transforms/*.xslt)) $(EXTRATRANSFORMS)) # List of types which should be treated as X·M·L. -XMLTYPES := application/xml text/xml +# +# Types which start with a `+´ are suffixes and match any type which +# ends accordingly. +XMLTYPES := application/xml text/xml +xml ifdef GIT ifneq ($(wildcard $(THISDIR)/.git),) @@ -219,6 +222,9 @@ endif # 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 := @@ -247,25 +253,24 @@ override comma := , # (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 @@ -343,8 +348,10 @@ ifneq ($(wildcard $(BUILDDIR)/parser.xslt),) override plaintexttypes := $(filter-out $(XMLTYPES),$(shell $(XMLLINT) --xpath '/*/*[@name="书社:parsers"]//*[namespace-uri()="http://www.w3.org/1999/xhtml" and local-name()="dd"]/text()' $(call quote,$(BUILDDIR)/parser.xslt))) endif -# (callable) Get all of the files (source and includes) which have the given types. -override filesoftype = $(foreach type,$1,$(patsubst %|$(type),%,$(filter %|$(type),$(types)))) +# (callable) Get all of the files (source and includes) which have the provided types. +# +# If a provided type begins with a `+´, it is analysed as a suffix, and all matching types will be used. +override filesoftype = $(foreach typeorsuffix,$1,$(foreach type,$(if $(filter +%,$(typeorsuffix)),$(sort $(foreach suffixedtype,$(filter %$(typeorsuffix),$(types)),$(lastword $(subst |, ,$(suffixedtype))))),$(typeorsuffix)),$(patsubst %|$(type),%,$(filter %|$(type),$(types))))) # Build up collections of various file types. override xmlfiles := $(call filesoftype,$(XMLTYPES)) @@ -352,7 +359,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 = $(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))) @@ -502,7 +509,7 @@ list : # 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 : @@ -510,7 +517,7 @@ 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 : ; @@ -537,10 +544,10 @@ ifneq ($(typeupdates)$(wildcard $(BUILDDIR)/dependencies)$(wildcard $(BUILDDIR)/ $(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 @@ -553,7 +560,7 @@ ifeq ($(typeupdates),) $(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 @@ -570,18 +577,18 @@ $(call magicfile,$(MAGIC)) : $(BUILDDIR)/magic/% : $$(call magicsource,$$@) # 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) @@ -590,7 +597,7 @@ $(BUILDDIR)/parser.xslt : $(BUILDDIR)/parser.catalog $(THISDIR)/lib/catalog2pars # Even plain X·M·L files are parsed, because they may contain X·H·T·M·L `