X-Git-Url: https://git.ladys.computer/Shushe/blobdiff_plain/e7c797ffb120c0dac9be13b28d533d49d164b6d0..630f5bf06a645dbaad5f05a50526328fafad87ca:/GNUmakefile?ds=inline diff --git a/GNUmakefile b/GNUmakefile index e0cb8e1..085b4c2 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -16,6 +16,7 @@ override define makefileinfo ║│ │║ ║│ • cat │║ ║│ • cp │║ +║│ • date │║ ║│ • echo │║ ║│ • file │║ ║│ • find │║ @@ -25,6 +26,7 @@ override define makefileinfo ║│ • rm │║ ║│ • sed │║ ║│ • sleep │║ +║│ • stat │║ ║│ • test │║ ║│ • touch │║ ║│ • tr (requires support for `-d´) │║ @@ -79,6 +81,7 @@ endef # different implementation, you can override the appropriate variable. CAT := cat CP := cp +DATE := date ECHO := echo FILE := file FIND := find @@ -88,6 +91,7 @@ PRINTF := printf RM := rm SED := sed SLEEP := sleep +STAT := stat TEST := test TOUCH := touch TR := tr @@ -97,12 +101,18 @@ XMLLINT := xmllint XSLTPROC := xsltproc # The directory which contains the source files. +# +# Multiple directories can be given so long as files with the same name +# do not exist in each. SRCDIR := sources # The directory which contains “includes”: Files which may be included # in other files but for which no final output will be generated. # -# This can be inside of `SOURCES_DIRECTORY´ if desired. +# Multiple directories can be given so long as files with the same name +# do not exist in each. +# +# These can be inside of `SRCDIR´ directories if desired. INCLUDEDIR := sources/includes # The directory in which to generate temporary buildfiles. @@ -130,11 +140,13 @@ MAGICDIR := $(patsubst ./%,%,$(THISDIR)/magic) # Configuration of `find´. # -# By default, `find´ will follow symlinks and use extended regular -# expressions, ignoring hidden files and those which begin with a -# period. -FINDOPTS := -LE +# By default, `find´ will not follow symlinks and will use extended +# regular expressions, ignoring hidden files and those which begin with +# a period. +FINDOPTS := -PE FINDRULES := -flags -nohidden -and -not -name '.*' +FINDINCLUDEOPTS := $(FINDOPTS) +FINDINCLUDERULES := $(FINDRULES) # The list of parsers for plaintext file types. # @@ -189,13 +201,14 @@ override silent := $(if $(VERBOSE),,@) # (callable) Escape special characters for use in sed regular # expressions. -override sedesc = $(subst $$,\$$,$(subst *,\*,$(subst .,\.,$(subst [,\[,$(subst ^,\^,$(subst \,\\,$1)))))) +override sedesc = $(subst /,[/],$(subst $$,\$$,$(subst *,\*,$(subst .,\.,$(subst [,\[,$(subst ^,\^,$(subst \,\\,$1))))))) -# Collect all of the applicable includes from the includes directory. +# (overridable) Collect all of the applicable includes from the +# includes directory. sourceincludes := $(shell $(FIND) $(FINDOPTS) $(INCLUDEDIR) -type f '(' $(FINDRULES) ')') -# Collect all of the applicable source files from the source directory, -# removing any which are also includes. +# (overridable) Collect all of the applicable source files from the +# source directory, removing any which are also includes. sourcefiles := $(filter-out $(sourceincludes),$(shell $(FIND) $(FINDOPTS) $(SRCDIR) -type f '(' $(FINDRULES) ')')) # Figure out the file type of each source file and source include. @@ -205,33 +218,41 @@ endif # Get the list of supported plaintext file types from the parser. ifneq ($(wildcard $(BUILDDIR)/parser.xslt),) -override plaintexttypes := $(shell $(XSLTPROC) $(call quote,$(THISDIR)/lib/parser2types.xslt) $(call quote,$(BUILDDIR)/parser.xslt)) +override plaintexttypes := $(filter-out $(XMLTYPES),$(shell $(XSLTPROC) $(call quote,$(THISDIR)/lib/parser2types.xslt) $(call quote,$(BUILDDIR)/parser.xslt))) endif -# Simplify the file type by only taking the first component (image, -# text, ⁊·c). -override simpletypes := $(shell $(TR) ' ' '\n' <<< $(call quote,$(types)) | $(SED) 's`/[^/]*$$``g') - # (callable) Get all of the files (source and includes) which have the # given types. override filesoftype = $(foreach type,$1,$(patsubst %?type=$(type),%,$(filter %?type=$(type),$(types)))) # Build up collections of various file types. -override plaintextfiles := $(call filesoftype,$(plaintexttypes)) -override xmlfiles := $(filter-out $(plaintextfiles),$(call filesoftype,$(XMLTYPES))) +override xmlfiles := $(call filesoftype,$(XMLTYPES)) +override plaintextfiles := $(filter-out $(xmlfiles),$(call filesoftype,$(plaintexttypes))) override assetfiles := $(filter-out $(xmlfiles) $(plaintextfiles),$(sourcefiles) $(sourceincludes)) # (callable) Get the types of the given files. override typeoffile = $(patsubst $(foreach file,$1,$(file)?type=%),%,$(filter $(foreach file,$1,$(file)?type=%),$(types))) +# (callable) Get the identifier for the given transform. +override id = $(or $(shell $(XMLLINT) --xpath '/*/*[local-name()="id" and namespace-uri()="urn:fdc:ladys.computer:20231231:Shu1She4"]/text()[1]' $1),$(basename $(notdir $1)) | $(SED)) + +# (callable) Get the local path for the given source file. +override sourcepath = $(firstword $(foreach directory,$(SRCDIR),$(if $(filter $(directory)/%,$1),$(patsubst $(directory)/%,%,$1),))) + +# (callable) Get the local path for the given include. +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. override datauri = $(foreach file,$1,data:$(call typeoffile,$(file));base64,$(shell $(UUENCODE) -m -r $(call quote,$(file)) _ | tr -d ' \n')) # (callable) Get local leiris for the given files. -override localuri = $(foreach file,$1,$(if $(filter $(file),$(sourceincludes)),$(patsubst $(INCLUDEDIR)/%,about:shushe?include=%,$(file)),$(patsubst $(SRCDIR)/%,about:shushe?source=%,$(file)))) +override localuri = $(foreach file,$1,$(if $(filter $(file),$(sourceincludes)),about:shushe?include=$(call includepath,$(file)),about:shushe?source=$(call sourcepath,$(file)))) + +# Pair each source file and include with its local u·r·i. +override sourcelocalpair = $(foreach file,$(sourcefiles) $(sourceincludes),$(file)`$(call localuri,$(file))) # (callable) Get the source files for the given local leiris. -override sourcefile = $(foreach file,$1,$(if $(filter about:shushe?include=%,$(file)),$(patsubst about:shushe?include=%,$(INCLUDEDIR)/%,$(file)),$(patsubst about:shushe?source=%,$(SRCDIR)/%,$(file)))) +override sourcefile = $(foreach local,$1,$(patsubst %`$(local),%,$(filter %`$(local),$(sourcelocalpair)))) # Adds a requirement on `$(BUILDDIR)/.update-types´ if the file is # present. @@ -239,11 +260,17 @@ override sourcefile = $(foreach file,$1,$(if $(filter about:shushe?include=%,$(f # 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 := $(if $(wildcard $(BUILDDIR)/.update-types),$(BUILDDIR)/.update-types,) +override typeupdates := $(wildcard $(BUILDDIR)/.update-types) # (callable) Get the location of the transformed X·M·L files for the # given source files. -override parsed = $(foreach file,$1,$(if $(filter $(file),$(sourceincludes)),$(patsubst $(INCLUDEDIR)/%,$(BUILDDIR)/includes/%,$(file)),$(patsubst $(SRCDIR)/%,$(BUILDDIR)/sources/%,$(file)))) +override parsed = $(foreach file,$1,$(if $(filter $(file),$(sourceincludes)),$(BUILDDIR)/includes/$(call includepath,$(file)),$(BUILDDIR)/sources/$(call sourcepath,$(file)))) + +# Pair each source file and include with its parsed location. +override sourceparsedpair = $(foreach file,$(sourcefiles) $(sourceincludes),$(file)`$(call parsed,$(file))) + +# (callable) Get the source files for the given parsed file. +override unparsed = $(foreach file,$1,$(patsubst %`$(file),%,$(filter %`$(file),$(sourceparsedpair)))) ifneq ($(wildcard $(BUILDDIR)/dependencies),) # Pair each file with a list of dependencies for it. @@ -273,10 +300,16 @@ override recursivefiles := $(foreach file,$(filter-out $(assetfiles),$(sourcefil override compilablefiles := $(filter-out $(assetfiles) $(recursivefiles),$(sourcefiles)) # (callable) Get the compiled locations for the given source files. -override compiled = $(patsubst $(SRCDIR)/%,$(BUILDDIR)/public/%,$(1)) +override compiled = $(foreach file,$1,$(BUILDDIR)/public/$(call sourcepath,$(file))) + +# Pair each source file and include with its compiled location. +override sourcecompiledpair = $(foreach file,$(sourcefiles) $(sourceincludes),$(file)`$(call compiled,$(file))) + +# (callable) Get the source files for the given compiled file. +override uncompiled = $(foreach file,$1,$(patsubst %`$(file),%,$(filter %`$(file),$(sourcecompiledpair)))) # (callable) Get the installed locations for the given source files. -override installed = $(patsubst $(SRCDIR)/%,$(DESTDIR)/%,$(1)) +override installed = $(foreach file,$1,$(DESTDIR)/$(call sourcepath,$(file))) # ─ ¶ Recipe Variable Definitions ───────────────────────────────────── @@ -295,7 +328,7 @@ help: $(silent)$(PRINTF) '%b' '$(subst $(newline),\n,$(makefileinfo))' # Compile all files, or error if any are recursive. -all: $(call compiled,$(recursivefiles) $(compilablefiles) $(assetfiles)) ; +all: $(call compiled,$(recursivefiles) $(compilablefiles) $(filter $(sourcefiles),$(assetfiles))) ; # Destroy buildfiles. clean: @@ -306,7 +339,7 @@ gone: $(silent)$(RM) -rf $(BUILDDIR)/ $(call compiled,$(recursivefiles) $(compilablefiles)) # Install the compiled files into `DESTDIR´. -install: $(call installed,$(recursivefiles) $(compilablefiles) $(assetfiles)) ; +install: $(call installed,$(recursivefiles) $(compilablefiles) $(filter $(sourcefiles),$(assetfiles))) ; # List all source files and includes and their computed types. list: @@ -329,6 +362,21 @@ $(call compiled,$(recursivefiles)): $(BUILDDIR)/public/%: # Phony rules; always consider these out‐of‐date. .PHONY: all default clean gone info install list $(call compiled,$(recursivefiles)); +ifneq ($(wildcard $(BUILDDIR)/.update-types)$(wildcard $(BUILDDIR)/dependencies),) +# Reload this make·file if the dependency graph has changed. +# +# This graph is a dependency for some of the variables that this +# make·file uses, 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 already exists. +$(THISDIR)/GNUmakefile:: $(BUILDDIR)/dependencies + $(silent)$(TOUCH) $(THISDIR)/GNUmakefile + $(silent)$(RM) -f $(BUILDDIR)/.update-types + @$(PRINTF) '%b\n' '\0033[1mDependency graph updated. Restarting…\0033[22m' +endif + ifeq ($(wildcard $(BUILDDIR)/.update-types),) # Reload this make·file if any of the magic files or parsers have # changed. @@ -340,25 +388,10 @@ ifeq ($(wildcard $(BUILDDIR)/.update-types),) # the restart have a more current time·stamp. $(THISDIR)/GNUmakefile:: $(BUILDDIR)/magic.mgc $(BUILDDIR)/parser.xslt $(THISDIR)/lib/parser2types.xslt $(silent)$(TOUCH) $(THISDIR)/GNUmakefile - $(silent)$(TOUCH) $(call quote,$(BUILDDIR)/.update-types) $(silent)$(RM) -f $(call quote,$(BUILDDIR)/dependencies) @$(PRINTF) '%b\n' '\0033[1mMagic file or parsers have updated. Restarting…\0033[22m' $(silent)$(SLEEP) 1 -endif - -ifneq ($(wildcard $(BUILDDIR)/.update-types)$(wildcard $(BUILDDIR)/dependencies),) -# Reload this make·file if the dependency graph has changed. -# -# This graph is a dependency for some of the variables that this -# make·file uses, 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 already exists. -$(THISDIR)/GNUmakefile:: $(BUILDDIR)/dependencies - $(silent)$(TOUCH) $(THISDIR)/GNUmakefile - $(silent)$(RM) -f $(BUILDDIR)/.update-types - @$(PRINTF) '%b\n' '\0033[1mDependency graph updated. Restarting…\0033[22m' + $(silent)$(TOUCH) $(call quote,$(BUILDDIR)/.update-types) endif # ─ ¶ Build Targets ─────────────────────────────────────────────────── @@ -371,37 +404,26 @@ endif $(BUILDDIR)/magic.mgc: $(wildcard $(MAGICDIR)/*) @$(ECHO) "Compiling new magic…" $(silent)$(call ensuredirectory,$(dir $@)) - $(silent)$(FILE) -C -m $(call quote,$(MAGICDIR)) - $(silent)$(MV) $(call quote,$(MAGICDIR).mgc) $(call quote,$(BUILDDIR)/magic.mgc) + $(silent)cd $(call quote,$(BUILDDIR)) && $(FILE) -C -m $(call quote,$(realpath $(MAGICDIR))) # Generate the main parser. $(BUILDDIR)/parser.catalog: $(PARSERS) @$(ECHO) "Generating catalog of parsers…" $(silent)$(XMLCATALOG) --create --noout $(call quote,$@) - $(foreach parser,$(PARSERS),$(silent)$(XMLCATALOG) --add uri $(call quote,$(basename $(notdir $(parser)))) $(call quote,../$(parser)) --noout $(call quote,$@)$(newline)) + $(foreach parser,$(PARSERS),$(silent)$(XMLCATALOG) --add uri $(call quote,$(call id,$(parser))) $(call quote,../$(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,$<) -# Note updates to parsers or magic. -# -# This file is actually generated as part of the make·file restart -# process, so it doesn’t need any recipes. -$(BUILDDIR)/.update-types: $(BUILDDIR)/magic.mgc $(BUILDDIR)/parser.xslt $(THISDIR)/lib/parser2types.xslt ; - # Parse the files. # # Even plain X·M·L files are parsed, because they may contain X·H·T·M·L # `