]> git.rocketbowman.com Git - jetsam.git/commitdiff
feat: make Makefile more flexible and straightforward.
authorKyle Bowman <kyle+github@rocketbowman.com>
Sun, 1 Dec 2024 18:43:19 +0000 (13:43 -0500)
committerKyle Bowman <kyle+github@rocketbowman.com>
Sun, 1 Dec 2024 18:43:19 +0000 (13:43 -0500)
Makefile

index d18df02bcec5bf0ec9f90c4f8975750511d5710c..26fdbbc6e16aa0cc394c39aab44468c1e154910c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,39 +1,31 @@
 SRC_DIR := src
 BUILD_DIR := build
+BUILD_LIST := $(shell find -L . -path './$(SRC_DIR)/**/*.md' | sed 's:./$(SRC_DIR):$(BUILD_DIR):g' | sed 's:\.md:\.html:g')
 
-IGNORE_VCS=*.git*
+.PHONY: clean help index
 
-.PHONY: all clean index
+help:
+       @echo "Builds HTML files from MD files. Targets include:"
+       @echo
+       @echo "clean            Removes build artifacts."
+       @echo "help             Prints this help message."
+       @echo "index            Builds an index of all the files in the $(BUILD_DIR) directory. (Top-level)"
+       @echo "$(BUILD_DIR)             Builds all the files defined in .buildlist."
+       @echo ".buildlist       Defines a list of all files to build. Paths may be relative."
+       @echo "         to this Makefile's directory. Paths should be on their own lines. You"
+       @echo "         may create this file manually or override the BUILD_LIST variable when"
+       @echo "         you invoke the make command. The default BUILD_LIST is created from MD files"
+       @echo "         found anywhere under the $(SRC_DIR) directory."
 
-# Pro/con - this builds *everything* due to recursion
-# Pro - simple
-# Con - I can't do make build/demo; make index to get a partial build.
-# Idea -
-#   I think I could use mindepth/maxdepth in the subdirs find.
-#   Then, make index wouldn't recurse. 
 index: $(BUILD_DIR)
        scripts/index.sh > build/index.html
 
-# This pattern defines a multi-target rule; it defines a target 
-# build/DIR for each DIR in src/DIR.
-subdirs := $(shell find -L $(SRC_DIR) -type d -not -path $(IGNORE_VCS)) 
-builddirs := $(patsubst $(SRC_DIR)%, $(BUILD_DIR)%, $(subdirs))
+$(BUILD_DIR): .buildlist
+       xargs -n 1 make --no-print-directory < .buildlist
 
-# USAGE: `make $(BUILD_DIR)/arbitary-dir`
-# BEHAVIOR: For each file in the build dir, invoke the markdown rule.
-# UNDERSTANDING THE APPROACH:
-#   1. From build target $@,
-#   2. Get a list of corresponding md files in SRC_DIR $(shell find ...), and
-#   3. Rewrite the result as a build target $(patsubstr $(SRC_DIR)%.md, ...)
-# WHY $$ and .SECONDEXPANSION?
-#   To let users specify arbitrary directories, I wanted to use the target $@ 
-#   in the prerequisites. To get scoping to work for $@ in the prerequisites, 
-#   you must go through a second phase of expansion. The $$ defers expansion 
-#   until the second phase. Single $ can be expanded on the first phase.
-.SECONDEXPANSION:
-$(builddirs): $$(patsubst $(SRC_DIR)%.md, $(BUILD_DIR)%.html, $$(shell find -L $$(patsubst $(BUILD_DIR)%, $(SRC_DIR)%, $$@) -name '*.md'))
+.buildlist:
+       @echo $(BUILD_LIST) | tr ' ' '\n' > .buildlist
 
-# For a given md file in src, make the corresponding html in build.
 $(BUILD_DIR)/%.html: $(SRC_DIR)/%.md 
        @mkdir -p $(dir $@)
        @pandoc \
@@ -44,6 +36,6 @@ $(BUILD_DIR)/%.html: $(SRC_DIR)/%.md
                --lua-filter=scripts/links-to-html.lua
        @echo "Building $@ from $<..."
 
-# Clean up generated HTML files
 clean:
+       rm .buildlist
        rm -rf $(BUILD_DIR)
\ No newline at end of file