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
15- ZIG_VERSION := $(shell $(ZIG ) version)
4+ ZIG ?= $(shell which zig || echo ~/.local/share/zig/0.14.1/zig)
165BUILD_TYPE ?= Debug
176BUILD_OPTS = -Doptimize=$(BUILD_TYPE )
187JOBS ?= $(shell nproc || echo 2)
198SRC_DIR := src
20- TEST_DIR := tests
9+ EXAMPLES_DIR := examples
2110BUILD_DIR := zig-out
2211CACHE_DIR := .zig-cache
23- DOC_SRC := src/root .zig
12+ DOC_SRC := src/lib .zig
2413DOC_OUT := docs/api/
2514COVERAGE_DIR := coverage
26- BINARY_NAME := template-zig-project
27- BINARY_PATH := $(BUILD_DIR ) /bin/$(BINARY_NAME )
28- TEST_EXECUTABLE := $(BUILD_DIR ) /bin/test
29- PREFIX ?= /usr/local
15+ BINARY_NAME := example
3016RELEASE_MODE := ReleaseSmall
17+ TEST_FLAGS := --summary all --verbose
18+
19+ # Automatically find all example names (e.g., btree_map, trie, etc.)
20+ EXAMPLES := $(patsubst % .zig,% ,$(notdir $(wildcard examples/* .zig) ) )
21+ # CHANGED: Default is now "all"
22+ EXAMPLE ?= all
3123
3224SHELL := /usr/bin/env bash
3325.SHELLFLAGS := -eu -o pipefail -c
@@ -36,68 +28,82 @@ SHELL := /usr/bin/env bash
3628# Targets
3729# ###############################################################################
3830
39- .PHONY : all build rebuild run test cov lint format doc clean install-deps release help coverage
31+ .PHONY : all help build rebuild run test release clean lint format doc install-deps coverage setup-hooks test-hooks
4032.DEFAULT_GOAL := help
4133
4234help : # # Show the help messages for all targets
43- @grep -E ' ^[a-zA-Z0-9_-]+:.*?## ' Makefile | awk ' BEGIN {FS = ":.*?## "}; {printf " %-10s %s\n", $$1, $$2}'
35+ @echo " Usage: make <target>"
36+ @echo " "
37+ @echo " Targets:"
38+ @grep -E ' ^[a-zA-Z_-]+:.*## .*$$' Makefile | \
39+ awk ' BEGIN {FS = ":.*## "}; {printf " \033[36m%-15s\033[0m %s\n", $$1, $$2}'
4440
4541all : build test lint doc # # build, test, lint, and doc
4642
4743build : # # Build project (Mode=$(BUILD_TYPE))
4844 @echo " Building project in $( BUILD_TYPE) mode with $( JOBS) concurrent jobs..."
49- $(ZIG ) build $(BUILD_OPTS ) -j$(JOBS )
45+ @ $(ZIG ) build $(BUILD_OPTS ) -j$(JOBS )
5046
5147rebuild : clean build # # clean and build
5248
53- run : build # # Run the main application
54- @echo " Running $( BINARY_NAME) ..."
55- $(ZIG ) build run $(BUILD_OPTS ) --
49+ run : # # Run an example (e.g. 'make run EXAMPLE=trie' or 'make run' for all)
50+ @if [ " $( EXAMPLE) " = " all" ]; then \
51+ echo " --> Running all examples..." ; \
52+ for ex in $( EXAMPLES) ; do \
53+ echo " " ; \
54+ echo " --> Running example: $$ ex" ; \
55+ $(ZIG ) build run-$$ ex $(BUILD_OPTS ) ; \
56+ done ; \
57+ else \
58+ echo " --> Running example: $( EXAMPLE) " ; \
59+ $(ZIG ) build run-$(EXAMPLE ) $(BUILD_OPTS ) ; \
60+ fi
5661
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 )
62+ test : # # Run tests
63+ @echo " Running tests..."
64+ @ $(ZIG ) build test $(BUILD_OPTS ) -j$(JOBS ) $( TEST_FLAGS )
6065
6166release : # # Build in Release mode
6267 @echo " Building the project in Release mode..."
6368 @$(MAKE ) BUILD_TYPE=$(RELEASE_MODE ) build
6469
6570clean : # # Remove docs, build artifacts, and cache directories
6671 @echo " Removing build artifacts, cache, generated docs, and coverage files..."
67- rm -rf $(BUILD_DIR ) $(CACHE_DIR ) $(DOC_OUT ) * .profraw $(COVERAGE_DIR )
72+ @ rm -rf $(BUILD_DIR ) $(CACHE_DIR ) $(DOC_OUT ) * .profraw $(COVERAGE_DIR ) public
6873
6974lint : # # Check code style and formatting of Zig files
7075 @echo " Running code style checks..."
71- $(ZIG ) fmt --check $(SRC_DIR ) $(TEST_DIR )
76+ @ $(ZIG ) fmt --check $(SRC_DIR ) $(EXAMPLES_DIR )
7277
7378format : # # Format Zig files
7479 @echo " Formatting Zig files..."
75- $(ZIG ) fmt .
80+ @ $(ZIG ) fmt .
7681
7782doc : # # Generate API documentation
7883 @echo " Generating documentation from $( DOC_SRC) to $( DOC_OUT) ..."
79- mkdir -p $(DOC_OUT )
80- @if $(ZIG ) doc --help > /dev/null 2>&1 ; then \
81- $(ZIG ) doc $(DOC_SRC ) --output-dir $(DOC_OUT ) ; \
82- else \
83- $(ZIG ) test -femit-docs $(DOC_SRC ) ; \
84- for f in docs/* ; do \
85- base=$$(basename "$$f") ; \
86- if [ " $$ base" = " assets" ] || [ " $$ base" = " api" ]; then \
87- continue ; \
88- fi ; \
89- mv " $$ f" $(DOC_OUT ) /; \
90- done ; \
91- fi
84+ @mkdir -p $(DOC_OUT )
85+ @$(ZIG ) test $(DOC_SRC ) -femit-docs=$(DOC_OUT )
9286
9387install-deps : # # Install system dependencies (for Debian-based systems)
9488 @echo " Installing system dependencies..."
95- sudo apt-get update
96- sudo apt-get install -y make llvm snapd
97- sudo snap install zig --beta --classic # Use `--edge --classic` to install the latest version
89+ @ sudo apt-get update
90+ @ sudo apt-get install -y make llvm snapd
91+ @ sudo snap install zig --beta --classic
9892
99- coverage : # # Generate code coverage report
100- @echo " Building tests with coverage instrumentation..."
101- @zig build test -Denable-coverage=true
93+ coverage : test # # Generate code coverage report
10294 @echo " Generating coverage report..."
103- @kcov --include-pattern=src --verify coverage-out zig-out/bin/test-root
95+ @kcov --include-pattern=src --verify coverage-out-btree-map zig-out/bin/btree_map
96+
97+ setup-hooks : # # Install Git hooks (pre-commit and pre-push)
98+ @echo " Setting up Git hooks..."
99+ @if ! command -v pre-commit & > /dev/null; then \
100+ echo " pre-commit not found. Please install it using 'pip install pre-commit'" ; \
101+ exit 1; \
102+ fi
103+ @pre-commit install --hook-type pre-commit
104+ @pre-commit install --hook-type pre-push
105+ @pre-commit install-hooks
106+
107+ test-hooks : # # Test Git hooks on all files
108+ @echo " Testing Git hooks..."
109+ @pre-commit run --all-files --show-diff-on-failure
0 commit comments