# 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)))))))
$(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.
.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 ($(typeupdates)$(wildcard $(BUILDDIR)/dependencies)$(wildcard $(BUILDDIR)/destinations),)
# Reload this make·file if the dependency graph or output destinations have changed.
# 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)$(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))
# Generate a catalog of all parsed files, for use when processing includes.
#
# This does not depend on actually transforming the files.
-$(BUILDDIR)/catalog : $(sourcefiles) $(sourceincludes) $(typeupdates)
+$(BUILDDIR)/catalog : $(call diffprereqs,sources,$(sort $(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,$(call fileuri,$(call parsed,$(source)))#$(if $(filter $(source),$(assetfiles)),asset,xml)) --noout $(call quote,$@)$(newline))
$(silent)$(XSLTPROC) -o $(call quote,$@) $(call quote,$(THISDIR)/lib/catalog2destinations.xslt) $(call quote,$<)
# Generate the main transform.
-$(BUILDDIR)/transform.catalog : $(TRANSFORMS)
+$(BUILDDIR)/transform.catalog : $(call diffprereqs,transforms,$(sort $(TRANSFORMS)))
@$(ECHO) "Generating catalog of transforms…"
$(silent)$(XMLCATALOG) --create --noout $(call quote,$@)
$(foreach transform,$(TRANSFORMS),$(silent)( $(call id,$(transform)) ) | $(XARGS) -I %% $(XMLCATALOG) --add uri %% $(call quote,$(call fileuri,$(transform))) --noout $(call quote,$@)$(newline))