X-Git-Url: https://git.ladys.computer/Shushe/blobdiff_plain/d5d9313e299c91913ebc7b24742107068af9a5ce..eb3b42b3ee2ac34f01624d360b2ae3195151cfd4:/GNUmakefile diff --git a/GNUmakefile b/GNUmakefile index 9eb4fd4..d0989c1 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -3,7 +3,7 @@ SHELL = /bin/sh # ━ § BASIC INFORMATION ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ override define makefileinfo ╭────────────────────────────╮ -╔╡ ⁌ ⛩️📰 书社 ∷ GNUmakefile ╞════════════════════════════════╗ +╔╡ ⁌ ⛩️📰 书社 ∷ GNUmakefile ╞═════════════════════════════════╗ ║╰────────────────────────────╯ ║ ╟┬ ¶ Prerequisites ───────────────────────────────────────────┬╢ ║│ │║ @@ -46,13 +46,14 @@ override define makefileinfo ║╰────────────────────────────────────────────────────────────╯║ ╟┬ ¶ Usage ───────────────────────────────────────────────────┬╢ ║│ │║ -║│ • `make all´: Compile, but do not install, all files. │║ +║│ • `make all´ (default): Compile, but do not install, all │║ +║│ files. │║ ║│ │║ ║│ • `make clean´: Remove `BUILDDIR´. │║ ║│ │║ ║│ • `make gone´: Remove `BUILDDIR´ and installed files. │║ ║│ │║ -║│ • `make help´ (default): Print this message. │║ +║│ • `make help´: Print this message. │║ ║│ │║ ║│ • `make install´: Compile all files and install in │║ ║│ `DESTDIR´. │║ @@ -165,13 +166,26 @@ TRANSFORMS := $(sort $(patsubst ./%,%,$(wildcard $(THISDIR)/transforms/*.xslt)) # List of types which should be treated as X·M·L. XMLTYPES := application/xml text/xml +# The name of the generator program. +GENERATOR := ⛩️📰 书社 + +ifdef GIT +ifneq ($(wildcard $(THISDIR)/.git),) +# A description of the current git revision of ⛩️📰 书社. +VERSION := $(shell cd $(THISDIR); $(GIT) describe 2> /dev/null || $(GIT) rev-parse HEAD 2> /dev/null || true) +endif + +ifneq ($(wildcard .git),) +# A description of the current git revision of the working directory. +SRCREV := $(shell $(GIT) describe 2> /dev/null || $(GIT) rev-parse HEAD 2> /dev/null || true) +endif +endif + # Set to a non·empty value to print all commands as they run. VERBOSE := # The default target for this makefile. -# -# Print help and exit. -.DEFAULT_GOAL := help +.DEFAULT_GOAL := all # ━ § BEGIN MAKE·FILE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ @@ -201,19 +215,28 @@ override silent := $(if $(VERBOSE),,@) # (callable) Escape special characters for use in sed regular expressions. override sedesc = $(subst /,[/],$(subst $$,\$$,$(subst *,\*,$(subst .,\.,$(subst [,\[,$(subst ^,\^,$(subst \,\\,$1))))))) +# The command to use for percent‐decoding. +override perdeccmd := $(SED) 's/|/%7C/g;s/[\]/%5C/g;s/%[0-9A-Fa-f]\{2\}/|&|/g' | $(TR) '|' '\n' | $(SED) '/^%[0-9A-Fa-f]\{2\}$$/!s/%/|%25|/' | $(TR) '|' '\n' | $(AWK) '$$0!~/%/{printf "%s",$$0}/%/{sub("%","0x");cmd="$(XARGS) $(PRINTF) \"%04o\"";printf "%s","\\";printf "%s",$$0|cmd;close(cmd)}' | $(XARGS) -0 $(PRINTF) '%b' + # (callable) Percent‐decode the given strings. -override perdec = $(foreach encoded,$1,$(shell $(PRINTF) '%s\n' $(call quote,$(encoded)) | $(SED) 's/|/%7C/g;s/[\]/%5C/g;s/%[0-9A-Fa-f]\{2\}/|&|/g' | $(TR) '|' '\n' | $(SED) '/^%[0-9A-Fa-f]\{2\}$$/!s/%/|%25|/' | $(TR) '|' '\n' | $(AWK) '$$0!~/%/{printf "%s",$$0}/%/{sub("%","0x");cmd="$(XARGS) $(PRINTF) \"%04o\"";printf "%s","\\";printf "%s",$$0|cmd;close(cmd)}' | $(XARGS) -0 $(PRINTF) '%b')) +# +# ☡ This variable creates a subshell every time it is computed. +override perdec = $(shell $(PRINTF) '%s\0450A' $(foreach encoded,$1,$(call quote,$(encoded))) | $(perdeccmd)) # (callable) Percent‐encode the given strings. # # This singly‐encodes u·r·i characters and doubly‐encodes other characters, then calls `perdec´ to decode back to a single encoding. # The encoding assumes the input is a “u·r·i component”; e·g that the resulting string should only contain `pchar´ (but can contain any `sub-delim´). -override perenc = $(foreach unencoded,$1,$(call perdec,$(shell $(PRINTF) '%s' $(call quote,$(unencoded)) | $(OD) -t x1 | $(SED) 's/^[0-9]*//;s/2[146-9A-Ea-e]/%&/g;s/3[0-9AaBbDd]/%&/g;s/40/%&/g;s/[46][1-9A-Fa-f]/%&/g;s/[57][0-9Aa]/%&/g;s/5[Ff]/%&/g;s/7[Ee]/%&/g;s/[ ][0-9A-Fa-f]\{2\}/%25&/g' | $(TR) -d ' \n' | $(TR) 'abcdef' 'ABCDEF'))) +# +# It is assumed that the given strings do not contain newlines. +# +# ☡ This variable creates a subshell every time it is computed. +override perenc = $(shell $(PRINTF) '%s\n' $(foreach unencoded,$1,$(call quote,$(unencoded))) | $(OD) -t x1 | $(SED) 's/^[0-9]*//;s/0[Aa]/%&/g;s/2[146-9A-Ea-e]/%&/g;s/3[0-9AaBbDd]/%&/g;s/40/%&/g;s/[46][1-9A-Fa-f]/%&/g;s/[57][0-9Aa]/%&/g;s/5[Ff]/%&/g;s/7[Ee]/%&/g;s/[ ][0-9A-Fa-f]\{2\}/%25&/g' | $(TR) -d ' \n' | $(TR) 'abcdef' 'ABCDEF' | $(perdeccmd)) # (callable) Percent‐encode each component in the given paths. # -# ☡ This will remove leading and trailing slashes. -override pathenc = $(foreach path,$1,$(subst $(space),/,$(call perenc,$(subst /, ,$(path))))) +# ☡ This variable creates a subshell every time it is computed. +override pathenc = $(subst %2F,/,$(call perenc,$1)) # (overridable) Collect all of the applicable includes from the includes directory. sourceincludes := $(shell $(FIND) $(foreach dir,$(INCLUDEDIR),$(call quote,$(dir))) '(' $(FINDRULES) ')' -a -type f) @@ -221,18 +244,6 @@ sourceincludes := $(shell $(FIND) $(foreach dir,$(INCLUDEDIR),$(call quote,$(dir # (overridable) Collect all of the applicable source files from the source directory, removing any which are also includes. sourcefiles := $(filter-out $(sourceincludes),$(shell $(FIND) $(foreach dir,$(SRCDIR),$(call quote,$(dir))) '(' $(FINDRULES) ')' -a -type f)) -ifdef GIT -ifneq ($(wildcard $(THISDIR)/.git),) -# (overridable) The name of the current revision of ⛩️📰 书社, or its hash if the current revision is not a tag. -thisrev = $(shell cd $(THISDIR); $(GIT) describe 2> /dev/null || $(GIT) rev-parse HEAD 2> /dev/null || true) -endif - -ifneq ($(wildcard .git),) -# (overridable) The name of the current revision of the working directory, or its hash if the current revision is not a tag. -srcrev = $(shell $(GIT) describe 2> /dev/null || $(GIT) rev-parse HEAD 2> /dev/null || true) -endif -endif - # Figure out the file type of each source file and source include. ifneq ($(wildcard $(BUILDDIR)/magic.mgc),) override types := $(shell $(SED) 's/^ *//;s/ *$$//;s/ {2,}/ /g' <<< $(call quote,$(sourcefiles) $(sourceincludes)) | $(TR) ' ' '\n' | $(FILE) -m $(call quote,$(BUILDDIR)/magic.mgc) --mime-type --separator '|' --files-from - | $(SED) 's/| */|/g') @@ -254,9 +265,6 @@ override assetfiles := $(filter-out $(xmlfiles) $(plaintextfiles),$(sourcefiles) # (callable) Get the types of the given files. override typeoffile = $(patsubst $(foreach file,$1,$(file)|%),%,$(filter $(foreach file,$1,$(file)|%),$(types))) -# (callable) Get the identifier for the given parser or transform. -override id = $(or $(shell $(XMLLINT) --xpath '/*/*[local-name()="id" and namespace-uri()="urn:fdc:ladys.computer:20231231:Shu1She4"]/text()[1]' $1 2> /dev/null),about:shushe?$(or $2,unknown)=$(call pathenc,$(basename $(notdir $1)))) - # Pair each source magic file with its location in the build directory. override magicpair := $(foreach magicfile,$(MAGIC),$(magicfile)|$(BUILDDIR)/magic/$(notdir $(magicfile))) @@ -273,6 +281,8 @@ override sourcepath = $(firstword $(foreach directory,$(SRCDIR),$(if $(filter $( override includepath = $(firstword $(foreach directory,$(INCLUDEDIR),$(if $(filter $(directory)/%,$1),$(patsubst $(directory)/%,%,$1),))) # (callable) Get base64 data u·r·i’s for the given files. +# +# ☡ This variable creates a subshell every time it is computed. override datauri = $(foreach file,$1,data:$(call typeoffile,$(file));base64,$(shell $(UUENCODE) -m -r $(call quote,$(file)) _ | tr -d ' \n')) # Pair each source file and include with its local u·r·i. @@ -299,9 +309,15 @@ 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, or parsed file with its file u·r·i. +override fileuripairs := $(join $(patsubst %,%|,$(PARSERS) $(TRANSFORMS) $(call parsed,$(sourcefiles) $(sourceincludes))),$(call pathenc,$(foreach uriable,$(PARSERS) $(TRANSFORMS) $(call parsed,$(sourcefiles) $(sourceincludes)),file://$(abspath $(uriable))))) + +# (callable) Get the file u·r·is for the given parsers, transforms, or parsed files. +override fileuri = $(foreach file,$1,$(patsubst $(file)|%,%,$(filter $(file)|%,$(fileuripairs)))) + ifneq ($(wildcard $(BUILDDIR)/dependencies),) # Pair each file with a list of dependencies for it. -override dependenciesforfile := $(foreach file,$(sourcefiles),$(file)|$(subst $(space),|,$(shell $(CAT) $(call quote,$(BUILDDIR)/dependencies) | $(SED) $(call quote,/^$(call sedesc,$(call localuri,$(file)))$$/$(comma)/^[^ ]/!d;/^ /!d;s/^ //)))) +override dependenciesforfile := $(foreach file,$(filter-out $(assetfiles),$(sourcefiles) $(sourceincludes)),$(file)|$(subst $(space),|,$(shell $(CAT) $(call quote,$(BUILDDIR)/dependencies) | $(SED) $(call quote,/^$(call sedesc,$(call localuri,$(file)))$$/$(comma)/^[^ ]/!d;/^ /!d;s/^ //)))) # (callable) Get the list of dependency leiris for the given source files. # @@ -356,15 +372,14 @@ endif # (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 +# (callable) Get the identifier for the given parser or transform. +override id = $(XMLLINT) --xpath '/*/*[local-name()="id" and namespace-uri()="urn:fdc:ladys.computer:20231231:Shu1She4"]/text()[1]' $(call quote,$1) 2> /dev/null || $(PRINTF) '%s\n' $(call quote,about:shushe?$(or $2,unknown)=$(call pathenc,$(basename $(notdir $1)))) + # (callable) Sanitize and wrap the provided plaintext file in X·M·L, printing to `stdout´. override wrapplaintext = $(PRINTF) '%s\n' "$$($(PRINTF) '%b' '\n')" # ─ ¶ Phony Targets ─────────────────────────────────────────────────── -# Provide help. -help : - @$(PRINTF) '%b' '$(subst $(newline),\n,$(makefileinfo))' - # Compile all files, or error if any are recursive. all : $(call compiled,$(recursivefiles) $(compilablefiles)) ; @@ -375,6 +390,10 @@ clean : # Destroy build directory and installed files. gone : clean uninstall ; +# Provide help. +help : + @$(PRINTF) '%b' '$(subst $(newline),\n,$(makefileinfo))' + # Install the compiled files into `DESTDIR´, or error if any are recursive. install : $(call installed,$(recursivefiles) $(installablefiles)) ; @@ -382,7 +401,7 @@ install : $(call installed,$(recursivefiles) $(installablefiles)) ; list : @$(PRINTF) '%b' $(call quote,$(foreach file,$(sort $(sourcefiles)) $(sort $(sourceincludes)),\0033[1m$(file)\0033[22m|$(call typeoffile,$(file))|[\0033[3m$(if $(filter $(file),$(xmlfiles)),xml,$(if $(filter $(file),$(plaintextfiles)),text,asset))$(if $(filter $(file),$(sourceincludes)),|include,)\0033[23m]$(if $(call dependencies,$(file))$(call recursives,$(file)), $(strip $(foreach recursive,$(call recursives,$(file)),\0033[93;41m•|Recursive|Dependency|\0033[39;49m|$(recursive)) $(foreach dependency,$(call dependencies,$(file)),\0033[2m•|Dependency|\0033[22m|$(dependency))))$(if $(filter $(file),$(sourcefiles)), →|<\0033[4m/$(call destination,$(file))\0033[24m>,) )) | $(TR) ' |' '\n ' -# Destroy build directory and installed files. +# Destroy installed files. uninstall : $(foreach file,$(installablefiles),$(if $(wildcard $(call installed,$(file))),$(silent)$(PRINTF) '%s\n' $(call quote,Removing …)$(newline)$(silent)$(RM) -f $(call quote,$(call installed,$(file)))$(newline),)) @@ -449,7 +468,7 @@ $(BUILDDIR)/magic.mgc : $(call magicfile,$(MAGIC)) $(BUILDDIR)/parser.catalog : $(PARSERS) @$(ECHO) "Generating catalog of parsers…" $(silent)$(XMLCATALOG) --create --noout $(call quote,$@) - $(foreach parser,$(PARSERS),$(silent)$(XMLCATALOG) --add uri $(call quote,$(call id,$(parser),parser)) $(call quote,file:///$(call pathenc,$(abspath $(parser)))) --noout $(call quote,$@)$(newline)) + $(foreach parser,$(PARSERS),$(silent)( $(call id,$(parser)) ) | $(XARGS) -I %% $(XMLCATALOG) --add uri %% $(call quote,$(call fileuri,$(parser))) --noout $(call quote,$@)$(newline)) $(BUILDDIR)/parser.xslt: $(BUILDDIR)/parser.catalog $(THISDIR)/lib/catalog2parser.xslt @$(ECHO) "Generating main parser…" $(silent)$(XSLTPROC) -o $(call quote,$@) $(call quote,$(THISDIR)/lib/catalog2parser.xslt) $(call quote,$<) @@ -461,7 +480,7 @@ $(BUILDDIR)/parser.xslt: $(BUILDDIR)/parser.catalog $(THISDIR)/lib/catalog2parse $(call parsed,$(sourcefiles) $(sourceincludes)) : % : $$(call unparsed,$$@) $(typeupdates) @$(PRINTF) '%s\n' $(call quote,Processing `$<´…) $(silent)$(call ensuredirectory,$(dir $@)) - $(silent)$(if $(filter $<,$(assetfiles)),$(PRINTF) '%s\n' $(call quote,) > $(call quote,$@),$(if $(filter $<,$(plaintextfiles)),$(call wrapplaintext,$<),$(CAT) $(call quote,$<)) | $(XSLTPROC) -o $(call quote,$@) --stringparam BUILDTIME '$(shell TZ= $(DATE) '+%Y-%m-%dT%H:%M:%SZ')' --stringparam SRCTIME '$(shell TZ= $(STAT) -f '%Sm' -t '%Y-%m-%dT%H:%M:%SZ' $(call quote,$(call unparsed,$@)))'$(if $(thisrev), --stringparam VERSION $(call quote,$(thisrev)),)$(if $(srcrev), --stringparam SRCREV $(call quote,$(srcrev)),) $(call quote,$(BUILDDIR)/parser.xslt) -) + $(silent)$(if $(filter $<,$(assetfiles)),$(PRINTF) '%s\n' $(call quote,) > $(call quote,$@),$(if $(filter $<,$(plaintextfiles)),$(call wrapplaintext,$<),$(CAT) $(call quote,$<)) | $(XSLTPROC) -o $(call quote,$@) --stringparam BUILDTIME $$(TZ= $(DATE) '+%Y-%m-%dT%H:%M:%SZ') --stringparam SRCTIME $$(TZ= $(STAT) -f '%Sm' -t '%Y-%m-%dT%H:%M:%SZ' $(call quote,$(call unparsed,$@)))$(if $(GENERATOR), --stringparam GENERATOR $(call quote,$(GENERATOR)))$(if $(VERSION), --stringparam VERSION $(call quote,$(VERSION)),)$(if $(SRCREV), --stringparam SRCREV $(call quote,$(SRCREV)),) $(call quote,$(BUILDDIR)/parser.xslt) -) # Generate a catalog of all parsed files, for use when processing includes. # @@ -469,7 +488,7 @@ $(call parsed,$(sourcefiles) $(sourceincludes)) : % : $$(call unparsed,$$@) $(ty $(BUILDDIR)/catalog : $(sourcefiles) $(sourceincludes) $(typeupdates) @$(ECHO) "Generating catalog of parsed files…" $(silent)$(XMLCATALOG) --create --noout $(call quote,$@) - $(foreach source,$(sourcefiles) $(sourceincludes),$(silent)$(XMLCATALOG) --add uri $(call quote,$(call localuri,$(source))) $(call quote,file:///$(call pathenc,$(abspath $(call parsed,$(source))))#$(if $(filter $(source),$(assetfiles)),asset,xml)) --noout $(call quote,$@)$(newline)) + $(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 @@ -488,7 +507,7 @@ $(BUILDDIR)/destinations : $(BUILDDIR)/catalog $(call parsed,$(filter-out $(asse $(BUILDDIR)/transform.catalog : $(TRANSFORMS) @$(ECHO) "Generating catalog of transforms…" $(silent)$(XMLCATALOG) --create --noout $(call quote,$@) - $(foreach transform,$(TRANSFORMS),$(silent)$(XMLCATALOG) --add uri $(call quote,$(call id,$(transform),transform)) $(call quote,file:///$(call pathenc,$(abspath $(transform)))) --noout $(call quote,$@)$(newline)) + $(foreach transform,$(TRANSFORMS),$(silent)( $(call id,$(transform)) ) | $(XARGS) -I %% $(XMLCATALOG) --add uri %% $(call quote,$(call fileuri,$(transform))) --noout $(call quote,$@)$(newline)) $(BUILDDIR)/transform.xslt : $(BUILDDIR)/transform.catalog $(THISDIR)/lib/catalog2transform.xslt @$(ECHO) "Generating main transform…" $(silent)$(XSLTPROC) -o $(call quote,$@) $(call quote,$(THISDIR)/lib/catalog2transform.xslt) $(call quote,$<) @@ -497,7 +516,7 @@ $(BUILDDIR)/transform.xslt : $(BUILDDIR)/transform.catalog $(THISDIR)/lib/catalo $(call compiled,$(compilablefiles)) : $(BUILDDIR)/public/% : $$(call parsed,$$(call uncompiled,$$@)) $(BUILDDIR)/transform.xslt $$(call parsed,$$(call dependencies,$$(call uncompiled,$$@))) $(silent)$(call ensuredirectory,$(dir $@)) @$(PRINTF) '%s\n' $(call quote,Compiling …) - $(silent)$(XSLTPROC) -o $(call quote,$@) --stringparam CATALOG 'catalog' --stringparam BUILDTIME '$(shell TZ= $(DATE) '+%Y-%m-%dT%H:%M:%SZ')' --stringparam SRCTIME '$(shell TZ= $(STAT) -f '%Sm' -t '%Y-%m-%dT%H:%M:%SZ' $(call quote,$(call uncompiled,$@)))' --stringparam PATH $(call quote,/$*)$(if $(thisrev), --stringparam VERSION $(call quote,$(thisrev)),)$(if $(srcrev), --stringparam SRCREV $(call quote,$(srcrev)),) $(call quote,$(BUILDDIR)/transform.xslt) $(call quote,$<) + $(silent)$(XSLTPROC) -o $(call quote,$@) --stringparam CATALOG 'catalog' --stringparam BUILDTIME $$(TZ= $(DATE) '+%Y-%m-%dT%H:%M:%SZ') --stringparam SRCTIME $$(TZ= $(STAT) -f '%Sm' -t '%Y-%m-%dT%H:%M:%SZ' $(call quote,$(call uncompiled,$@))) --stringparam PATH $(call quote,/$*)$(if $(GENERATOR), --stringparam GENERATOR $(call quote,$(GENERATOR)))$(if $(VERSION), --stringparam VERSION $(call quote,$(VERSION)),)$(if $(SRCREV), --stringparam SRCREV $(call quote,$(SRCREV)),) $(call quote,$(BUILDDIR)/transform.xslt) $(call quote,$<) $(call compiled,$(filter $(assetfiles),$(sourcefiles))) : $(BUILDDIR)/public/% : $$(call uncompiled,$$@) @$(PRINTF) '%s\n' $(call quote,Compiling …) $(silent)$(call ensuredirectory,$(dir $@))