X-Git-Url: https://git.ladys.computer/Shushe/blobdiff_plain/eb3b42b3ee2ac34f01624d360b2ae3195151cfd4..c00fbad125caaac8be191801ca47a6971007fbad:/GNUmakefile diff --git a/GNUmakefile b/GNUmakefile index d0989c1..1697903 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -16,6 +16,7 @@ override define makefileinfo ║│ │║ ║│ • awk │║ ║│ • cat │║ +║│ • cksum │║ ║│ • cp │║ ║│ • date │║ ║│ • echo │║ @@ -89,6 +90,7 @@ endef # If these are not installed on your computer, or you need to use a different implementation, you can override the appropriate variable. AWK := awk CAT := cat +CKSUM := cksum CP := cp DATE := date ECHO := echo @@ -166,13 +168,10 @@ 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) +THISREV := $(shell cd $(THISDIR); $(GIT) describe 2> /dev/null || $(GIT) rev-parse HEAD 2> /dev/null || true) endif ifneq ($(wildcard .git),) @@ -212,11 +211,20 @@ override quote = '$(subst ','"'"',$1)' # Outputs an `@´ to silence rules, unless `VERBOSE´ is nonempty. override silent := $(if $(VERBOSE),,@) +# (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. +# +# 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. +# +# ☡ 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 || true),,$2),$2 FORCE$(and $(shell $(call ensuredirectory,$(BUILDDIR)/lastprereqs) && $(PRINTF) '%s\n' $(call quote,$2) > $(BUILDDIR)/lastprereqs/$1),),$2) + # (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' +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}/%/{d="0123456789ABCDEF";v=substr(toupper($$0),2,2);printf "\\%04o",(index(d,substr(v,1,1))-1)*16+index(d,substr(v,2,1))-1}' | $(XARGS) -0 $(PRINTF) '%b' # (callable) Percent‐decode the given strings. # @@ -409,6 +417,9 @@ uninstall : $(call compiled,$(recursivefiles)) : @$(PRINTF) '%b\n' $(call quote,\0033[93;41mError:\0033[39;49m `$(call uncompiled,$@)´ has recursive dependencies:\n$(subst |, ,$(subst $(space),$(newline),$(foreach recursive,$(call recursives,$(call uncompiled,$@)),•|$(recursive))))) && false +# Add as a prerequisite to treat the target as tho it were phony. +FORCE : ; + # ─ ¶ Special Targets ───────────────────────────────────────────────── # Perform secondary expansion; this enables pattern rules to determine their prerequisites based on the matched pattern. @@ -418,21 +429,27 @@ $(call compiled,$(recursivefiles)) : .SUFFIXES : ; # Phony rules; always consider these out·of·date. -.PHONY : all default clean gone info install list uninstall $(call compiled,$(recursivefiles)) ; +.PHONY : FORCE all default clean gone info install list uninstall $(call compiled,$(recursivefiles)) ; -ifneq ($(wildcard $(BUILDDIR)/.update-types)$(wildcard $(BUILDDIR)/dependencies)$(wildcard $(BUILDDIR)/destinations),) +ifneq ($(typeupdates)$(wildcard $(BUILDDIR)/dependencies)$(wildcard $(BUILDDIR)/destinations),) # Reload this make·file if the dependency graph or output destinations have changed. # # The dependency graph and output destinations are used to set the values of variables in this make·file, so it’s important to ensure that they are actually up·to·date prior to executing any later rules. # # This recipe only exists after types have been updated or when the dependency graph or destinations file already exists. +# +# ※ There is a chance that generating the dependencies will also update the parsers. $(THISDIR)/GNUmakefile :: $(BUILDDIR)/dependencies $(BUILDDIR)/destinations $(silent)$(TOUCH) $(THISDIR)/GNUmakefile - $(silent)$(RM) -f $(BUILDDIR)/.update-types +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 +else + $(silent)$(RM) $(BUILDDIR)/.update-types @$(PRINTF) '%b\n' '\0033[1mDependency graph and output destinations updated. Restarting…\0033[22m' endif +endif -ifeq ($(wildcard $(BUILDDIR)/.update-types),) +ifeq ($(typeupdates),) # Reload this make·file if any of the magic files or parsers have changed. # # These are used to classify source files, so if they have changed then the make·file must be reloaded. @@ -448,44 +465,44 @@ endif # ─ ¶ Build Targets ─────────────────────────────────────────────────── -# Create symbolic links from the build directory’s store of magic files -# to their corresponding sources. -$(call magicfile,$(MAGIC)) : $(BUILDDIR)/magic/%: $$(call magicsource,$$@) +# Create symbolic links from the build directory’s store of magic files to their corresponding sources. +$(call magicfile,$(MAGIC)) : $(BUILDDIR)/magic/% : $$(call magicsource,$$@) $(silent)$(call ensuredirectory,$(dir $@)) $(silent)$(LN) -sf $(call quote,$(realpath $<)) $(call quote,$@) # Generate the compiled magic file from its sources. # # It must be updated if any of the files in the magic directory change. -# It ⁜also⁜ should be updated if any of the files in the magic directory are deleted, but this isn’t tracked presently. -$(BUILDDIR)/magic.mgc : $(call magicfile,$(MAGIC)) +$(BUILDDIR)/magic.mgc : $(call diffprereqs,magic,$(sort $(call magicfile,$(MAGIC)))) $(foreach outdated,$(filter-out $^,$(wildcard $(BUILDDIR)/magic/*)),$(silent)$(RM) $(call quote,$(outdated))$(newline)) @$(ECHO) "Compiling new magic…" $(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 : $(PARSERS) +$(BUILDDIR)/parser.catalog : $(call diffprereqs,parsers,$(sort $(PARSERS))) @$(ECHO) "Generating catalog of parsers…" $(silent)$(XMLCATALOG) --create --noout $(call quote,$@) $(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 +$(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,$<) + $(silent)$(TOUCH) $(call quote,$(BUILDDIR)/.update-types) # Parse the files. # # Even plain X·M·L files are parsed, because they may contain X·H·T·M·L `