1- # Generic Makefile for Zig projects
2-
3- # Load environment variables from .env file
4- ifneq (,$(wildcard ./.env) )
5- include .env
6- export $(shell sed 's/=.*//' .env)
7- else
8- $(warning `.env` file not found. Environment variables not loaded.)
9- endif
10-
111# ###############################################################################
122# Configuration and Variables
133# ###############################################################################
14- ZIG ?= zig
4+ ZIG ?= $( shell which zig || echo ~/.local/share/zig/0.15.2/zig)
155ZIG_VERSION := $(shell $(ZIG ) version)
166BUILD_TYPE ?= Debug
177BUILD_OPTS = -Doptimize=$(BUILD_TYPE )
@@ -20,43 +10,55 @@ SRC_DIR := src
2010TEST_DIR := tests
2111BUILD_DIR := zig-out
2212CACHE_DIR := .zig-cache
23- DOC_SRC := src/root .zig
13+ DOC_SRC := src/lib .zig
2414DOC_OUT := docs/api/
2515COVERAGE_DIR := coverage
26- BINARY_NAME := template-zig-project
16+ BINARY_NAME := zodd
2717BINARY_PATH := $(BUILD_DIR ) /bin/$(BINARY_NAME )
2818TEST_EXECUTABLE := $(BUILD_DIR ) /bin/test
2919PREFIX ?= /usr/local
3020RELEASE_MODE := ReleaseSmall
3121
22+ # Get all .zig files in the examples directory and extract their stem names
23+ EXAMPLES := $(patsubst % .zig,% ,$(notdir $(wildcard examples/* .zig) ) )
24+ EXAMPLE ?= all
25+
3226SHELL := /usr/bin/env bash
3327.SHELLFLAGS := -eu -o pipefail -c
3428
3529# ###############################################################################
3630# Targets
3731# ###############################################################################
3832
39- .PHONY : all build rebuild run test cov lint format docs clean install-deps release help coverage setup-hooks test-hooks
33+ .PHONY : all build rebuild example test cov lint format docs docs-serve clean install-deps release help coverage \
34+ setup-hooks test-hooks
4035.DEFAULT_GOAL := help
4136
4237help : # # Show the help messages for all targets
4338 @grep -E ' ^[a-zA-Z0-9_-]+:.*?## ' Makefile | awk ' BEGIN {FS = ":.*?## "}; {printf " %-10s %s\n", $$1, $$2}'
4439
45- all : build test lint docs # # build, test, lint, and doc
40+ all : build test lint docs # # build, test, lint, and docs
4641
4742build : # # Build project (Mode=$(BUILD_TYPE))
4843 @echo " Building project in $( BUILD_TYPE) mode with $( JOBS) concurrent jobs..."
4944 $(ZIG ) build $(BUILD_OPTS ) -j$(JOBS )
5045
5146rebuild : clean build # # clean and build
5247
53- run : build # # Run the main application
54- @echo " Running $( BINARY_NAME) ..."
55- $(ZIG ) build run $(BUILD_OPTS ) --
48+ example : # # Run examples (default: all tests, or 'make example EXAMPLE=e1_transitive_closure')
49+ ifeq ($(EXAMPLE ) ,all)
50+ @for ex in $(EXAMPLES); do \
51+ echo "--> Running example: $$ex"; \
52+ $(ZIG) build run-$$ex $(BUILD_OPTS); \
53+ done
54+ else
55+ @echo "--> Running example: $(EXAMPLE)"
56+ $(ZIG) build run-$(EXAMPLE) $(BUILD_OPTS)
57+ endif
5658
57- test : # # Run tests and generate coverage data
58- @echo " Running tests with coverage enabled ..."
59- $(ZIG ) build test $(BUILD_OPTS ) -Denable-coverage=true - j$(JOBS )
59+ test : # # Run tests
60+ @echo " Running tests..."
61+ $(ZIG ) build test $(BUILD_OPTS ) -j$(JOBS ) --summary all
6062
6163release : # # Build in Release mode
6264 @echo " Building the project in Release mode..."
@@ -90,18 +92,16 @@ docs: ## Generate API documentation
9092 done ; \
9193 fi
9294
95+ docs-serve : # # Serve API documentation locally
96+ @echo " Serving documentation at http://localhost:8000/..."
97+ cd $(DOC_OUT ) && python3 -m http.server 8000
98+
9399install-deps : # # Install system dependencies (for Debian-based systems)
94100 @echo " Installing system dependencies..."
95101 sudo apt-get update
96102 sudo apt-get install -y make llvm snapd
97103 sudo snap install zig --beta --classic # Use `--edge --classic` to install the latest version
98104
99- coverage : # # Generate code coverage report
100- @echo " Building tests with coverage instrumentation..."
101- @zig build test -Denable-coverage=true
102- @echo " Generating coverage report..."
103- @kcov --include-pattern=src --verify coverage-out zig-out/bin/test-root
104-
105105setup-hooks : # # Install Git hooks (pre-commit and pre-push)
106106 @echo " Installing Git hooks..."
107107 @pre-commit install --hook-type pre-commit
0 commit comments