Tags: code
A Makefile is a script used by the make tool to automate tasks, typically building and compiling programs. It describes how to build targets (like .exe, .o, .html files) from their dependencies (like .c, .cpp, .md, .tex files).
The make
command is a build automation tool commonly
used in software development. It automatically builds executable
programs and libraries from source code by reading files called
Makefiles. These files contain rules and dependencies
that define how to compile and link the program.
What Make Does
Reads a Makefile β This file lists:
Targets (usually files to be built)
Dependencies (files the target depends on)
Commands (how to build the target)
Determines What Needs to Be Built β It only rebuilds files that have changed or whose dependencies have changed.
Runs the Necessary Commands β It executes shell commands to build or update targets.
Example Makefile
# Target: Dependencies
# Command
hello: hello.o
gcc -o hello hello.o
hello.o: hello.c
gcc -c hello.c
To build the hello executable, youβd run:
make
Why Use Make?
Efficiency: Only recompiles whatβs necessary.
Consistency: Standardizes build commands.
Convenience: Reduces typing and errors.
Common Use Cases
Compiling C/C++ projects.
Managing large codebases with many interdependent files.
Automating tasks (e.g., running tests, installing software).
New make
approach to build this website.
# Static Site Generator Makefile
# Variables
CONTENT_DIR := content
OUTPUT_DIR := pages
TAG_TEMP := tag_temp
TEMPLATE := layout/default.html
TAG_TEMPLATE := layout/tag.html
STYLE_SRC := layout/style.css
STYLE_DEST := $(OUTPUT_DIR)/style.css
REFS := refs.json
CSL := apa.csl
SITE_URL := https://continuum.codeberg.page
VERSION_DIR := versions
TIMESTAMP := $(shell date +"%Y%m%d-%H%M%S")
VERSION_OUTPUT := $(VERSION_DIR)/pages-$(TIMESTAMP)
# Collect markdown posts (excluding index.md)
POSTS := $(filter-out $(CONTENT_DIR)/index.md, $(wildcard $(CONTENT_DIR)/*.md))
POST_HTML := $(patsubst $(CONTENT_DIR)/%.md,$(OUTPUT_DIR)/%.html,$(POSTS))
# Tag pages (generated based on tag_temp/*.txt)
TAG_FILES := $(wildcard $(TAG_TEMP)/*.txt)
TAG_PAGES := $(patsubst $(TAG_TEMP)/%.txt,$(OUTPUT_DIR)/tag-%.html,$(TAG_FILES))
.PHONY: all clean
# Final build target
all: $(POST_HTML) $(TAG_PAGES) $(OUTPUT_DIR)/index.html $(OUTPUT_DIR)/rss.xml $(STYLE_DEST)
# Rule to convert each .md post to .html
$(OUTPUT_DIR)/%.html: $(CONTENT_DIR)/%.md | $(TAG_TEMP) $(OUTPUT_DIR)
./scripts/build-posts.sh
# Index page depends on generated post HTML and tag files
$(OUTPUT_DIR)/index.html: $(POST_HTML) $(TAG_FILES)
./scripts/build-index.sh
# Generate each tag page
$(OUTPUT_DIR)/tag-%.html: $(TAG_TEMP)/%.txt
./scripts/build-tag-pages.sh $*
# RSS feed generation
$(OUTPUT_DIR)/rss.xml: $(POST_HTML)
./scripts/build-rss.sh "$(OUTPUT_DIR)" "$(SITE_URL)"
# Copy static stylesheet
$(STYLE_DEST): $(STYLE_SRC) | $(OUTPUT_DIR)
cp $(STYLE_SRC) $(STYLE_DEST)
# Create output directories
$(OUTPUT_DIR):
mkdir -p $(OUTPUT_DIR)
$(TAG_TEMP):
mkdir -p $(TAG_TEMP)
# Clean build artifacts
clean:
find $(OUTPUT_DIR) -type f \( -name "*.html" -o -name "*.xml" -o -name "*.css" \) -delete
rm -rf $(TAG_TEMP)
# === Versioning Target ===
version: all
mkdir -p $(VERSION_OUTPUT)
cp -r $(OUTPUT_DIR)/* $(VERSION_OUTPUT)/
echo "Version created at: $(VERSION_OUTPUT)"
# Serve locally
serve: all
@echo "Starting development server at http://localhost:8000"
@cd $(OUTPUT_DIR) && python3 -m http.server 8000
# Force rebuild everything
rebuild: clean all
# Rebuild tag pages only
tags: $(TAG_PAGES)
# Generate RSS feed
rss: $(OUTPUT_DIR)/rss.xml
# Debugging info
debug:
@echo "CONTENT_DIR: $(CONTENT_DIR)"
@echo "OUTPUT_DIR: $(OUTPUT_DIR)"
@echo "TEMPLATE: $(TEMPLATE)"
@echo "TAG_TEMPLATE:$(TAG_TEMPLATE)"
@echo "POSTS: $(POSTS)"
@echo "POST_HTML: $(POST_HTML)"
@echo "TAG_FILES: $(TAG_FILES)"
@echo "TAG_PAGES: $(TAG_PAGES)"
# Help target
help:
@echo "Static Site Generator"
@echo ""
@echo "Targets:"
@echo " all - Build the entire site (default)"
@echo " clean - Remove all generated files"
@echo " serve - Build and serve the site locally"
@echo " rebuild - Clean and rebuild everything"
@echo " tags - Rebuild tag pages only"
@echo " rss - Generate RSS feed"
@echo " version - Save a snapshot of the current build output"
@echo " debug - Show debugging information"
@echo " help - Show this help message"