From b45cc5b3e44b0d5d26cfbe21b8c7427a234ecc80 Mon Sep 17 00:00:00 2001 From: Kyle Bowman Date: Sun, 1 Dec 2024 13:43:19 -0500 Subject: [PATCH] feat: make Makefile more flexible and straightforward. --- Makefile | 46 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index d18df02..26fdbbc 100644 --- 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 -- 2.39.5