# suit: no configure step. Output goes in build/. See docs/quality.md.
CC ?= cc
LLVM_BIN ?= $(if $(wildcard /opt/homebrew/opt/llvm/bin/clang),/opt/homebrew/opt/llvm/bin,$(patsubst %/,%,$(dir $(shell command -v clang))))
UNAME := $(shell uname -s)
# C23 under whichever name this compiler knows it by (GCC 13 has only c2x).
STD := $(firstword $(foreach s,c23 c2x,$(if $(shell echo 'constexpr int x = 1; [[maybe_unused]] static bool b = nullptr == nullptr;' | $(CC) -std=$(s) -x c - -fsyntax-only 2>/dev/null && echo ok),$(s))))
ifeq ($(STD),)
$(error $(CC) does not accept -std=c23 or -std=c2x with constexpr, nullptr and bool; need GCC 13 or Clang 18 or newer)
endif
W := -std=$(STD) -Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wshadow \
-Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wcast-align -Wformat=2 \
-Wundef -Wvla -Wnull-dereference -Wdouble-promotion -Wimplicit-fallthrough \
-Werror -D_DEFAULT_SOURCE -Ibuild/gen
SAN := -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined \
-fno-sanitize-recover=all -DSUIT_TEST
ifeq ($(UNAME),Darwin)
STRIP := -Wl,-x -Wl,-S
SDK := -isysroot $(shell xcrun --show-sdk-path)
else
STRIP := -s
SDK :=
endif
REL := -O2 -flto $(STRIP)
COV := -O0 -g -D_FORTIFY_SOURCE=0 -fprofile-instr-generate -fcoverage-mapping -DSUIT_TEST -DSUIT_COVER
# The parts that touch no store: the config parser, the stamp, the bench and the
# platform layer. The fuzz targets link only these.
CORE := src/conf.c src/stamp.c src/bench.c src/platform.c src/ledger.c src/remote.c src/project.c src/lint.c src/need.c src/usage.c src/curate_text.c src/anchors.c
STORE := src/upstream.c src/wardrobes.c src/store.c src/store_fs.c src/resolve.c src/place.c src/commands.c src/machine.c src/attention.c src/join.c src/scan.c src/exclude.c src/install.c src/hook.c src/tally.c src/assets.c src/curate.c src/curate_apply.c src/tls.c
LIB := $(CORE) $(STORE)
MAIN := src/main.c
# Vendored code: built with warnings off, linked in, outside the 100% rule.
BRI := -isystem vendor/bearssl/inc
LF := -isystem vendor/libfossil -DHAVE_LIBFOSSIL_CONFIG_H -DWITHOUT_ICONV $(BRI)
VFLAGS := -w -D_DEFAULT_SOURCE -DHAVE_LIBFOSSIL_CONFIG_H -DWITHOUT_ICONV \
-DSQLITE_OMIT_LOAD_EXTENSION -DZ_HAVE_UNISTD_H -Ivendor/libfossil -Ivendor/zlib
VOBJ := libfossil.o sqlite3.o adler32.o compress.o crc32.o deflate.o inffast.o inflate.o \
inftrees.o trees.o uncompr.o zutil.o
vpath %.c vendor/libfossil vendor/zlib
ifeq ($(UNAME),Linux)
LDLIBS := -lm -lpthread
else
LDLIBS :=
endif
# BearSSL makes no system call of its own: no RDRAND, no /dev/urandom, no
# time(). Suit seeds it and gives it the time (src/tls.c).
BRFLAGS := -w -Ivendor/bearssl/inc -Ivendor/bearssl/src -DBR_RDRAND=0 -DBR_USE_URANDOM=0 \
-DBR_USE_WIN32_RAND=0 -DBR_USE_UNIX_TIME=0 -DBR_USE_WIN32_TIME=0
BR_SRC := $(sort $(wildcard vendor/bearssl/src/*.c vendor/bearssl/src/*/*.c))
BR_OBJ := $(patsubst vendor/bearssl/src/%.c,%.o,$(BR_SRC))
# vendor_rules(VARIANT, COMPILER, FLAGS): build/VARIANT/vendor/*.o and
# build/VARIANT/vendor/libbearssl.a
define vendor_rules
build/$(1)/vendor/%.o: %.c Makefile
@mkdir -p build/$(1)/vendor
$(2) $(3) $$(VFLAGS) -c -o $$@ $$<
build/$(1)/vendor/bearssl/%.o: vendor/bearssl/src/%.c Makefile
@mkdir -p $$(@D)
@$(2) $(3) $$(BRFLAGS) -c -o $$@ $$<
build/$(1)/vendor/libbearssl.a: $$(addprefix build/$(1)/vendor/bearssl/,$$(BR_OBJ))
rm -f $$@
$$(AR) rcs $$@ $$^
endef
VSAN := -O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined -fno-sanitize-recover=undefined
$(eval $(call vendor_rules,dev,$(CC),$(VSAN)))
$(eval $(call vendor_rules,release,$(CC),-O2 -flto))
$(eval $(call vendor_rules,cover,$(CC),-O1))
$(eval $(call vendor_rules,fuzz,$(LLVM_BIN)/clang,-O1 -g -fsanitize=address,undefined))
# Fil-C has no x86 intrinsics: BearSSL's AES-NI and SSE2 code stays out.
$(eval $(call vendor_rules,filc,$(FILCC),-O2 -DBR_AES_X86NI=0 -DBR_SSE2=0 -DBR_POWER8=0))
# Helpers test/https.sh runs: built with the compiler as it is, no sanitizers.
$(eval $(call vendor_rules,helpers,$(CC),-O1))
HELPERS := build/helpers/deadline build/helpers/badsrv
HFLAGS := -std=$(STD) -O1 -Wall -Wextra -Werror -D_DEFAULT_SOURCE
build/helpers/deadline: test/deadline.c Makefile
@mkdir -p build/helpers
$(CC) $(HFLAGS) -o $@ test/deadline.c
build/helpers/badsrv: test/badsrv.c build/helpers/vendor/libbearssl.a Makefile
@mkdir -p build/helpers
$(CC) $(HFLAGS) $(BRI) -o $@ test/badsrv.c build/helpers/vendor/libbearssl.a
DEV_BR := build/dev/vendor/libbearssl.a
REL_BR := build/release/vendor/libbearssl.a
COV_BR := build/cover/vendor/libbearssl.a
FUZZ_BR := build/fuzz/vendor/libbearssl.a
FILC_BR := build/filc/vendor/libbearssl.a
DEV_V := $(addprefix build/dev/vendor/,$(VOBJ)) $(DEV_BR)
REL_V := $(addprefix build/release/vendor/,$(VOBJ)) $(REL_BR)
COV_V := $(addprefix build/cover/vendor/,$(VOBJ)) $(COV_BR)
FILC_V := $(addprefix build/filc/vendor/,$(VOBJ)) $(FILC_BR)
HDR := $(wildcard src/*.h) build/gen/curate_page.inc
# How many test suites run side by side (JOBS=1: one after another). Four or
# more cores: up to six; fewer: one, as on the small Linux runner.
NCPU := $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
JOBS ?= $(shell n=$(NCPU); if [ $$n -ge 6 ]; then echo 6; elif [ $$n -ge 4 ]; then echo $$n; else echo 1; fi)
export TEST_JOBS = $(JOBS)
FILCC ?=
FUZZ_TIME ?= 30
MUTATE_ITER ?= 200000
.PHONY: suit release check cover analyze check-budget fuzz corpus filc ci all clean live quick gate
suit: build/dev/suit build/dev/suit-run build/dev/empty
# The page `suit curate` writes is src/curate.html, compiled in as a C array with
# POSIX tools only (od and awk): the release build needs nothing but a C compiler.
build/gen/curate_page.inc: src/curate.html Makefile
@mkdir -p build/gen
{ echo '// Generated from src/curate.html by the Makefile. Do not edit.'; \
echo 'static const unsigned char curate_page[] = {'; \
od -An -v -tu1 src/curate.html | awk '{ for (i = 1; i <= NF; i++) printf "%s,", $$i; printf "\n" }'; \
echo '};'; } > [email protected] && mv [email protected] $@
# ---- dev: sanitizers and fault injection -----------------------------------
build/dev/suit: $(LIB) $(MAIN) $(HDR) $(DEV_V) Makefile
@mkdir -p build/dev
$(CC) $(W) $(SAN) $(LF) -o $@ $(LIB) $(MAIN) $(DEV_V) $(LDLIBS)
build/dev/suit-run: src/run_main.c src/hook.c src/stamp.c src/platform.c $(HDR) Makefile
@mkdir -p build/dev
$(CC) $(W) $(SAN) -DSUIT_NO_NET -Isrc -o $@ src/run_main.c src/hook.c src/stamp.c src/platform.c
build/dev/empty: test/empty.c Makefile
@mkdir -p build/dev
$(CC) $(W) -O2 -o $@ test/empty.c
build/dev/unit: test/unit.c $(LIB) $(HDR) $(DEV_V) Makefile
@mkdir -p build/dev
$(CC) $(W) $(SAN) $(LF) -Isrc -o $@ test/unit.c $(LIB) $(DEV_V) $(LDLIBS)
# Everything check and quick run, built and not run: what the gate's first stage compiles.
CHECK_BUILD := $(HELPERS) build/dev/suit build/dev/suit-run build/dev/empty build/dev/unit build/dev/replay-conf build/dev/replay-stamp build/dev/replay-ledger build/dev/replay-remote build/dev/replay-project build/dev/replay-lintrules build/dev/replay-lintline build/dev/replay-need build/dev/replay-usage build/dev/replay-curate build/dev/replay-choices build/dev/replay-anchors
check-build: $(CHECK_BUILD)
check: $(CHECK_BUILD)
ASAN_OPTIONS=detect_leaks=0 JOBS=$(JOBS) sh tools/suites.sh build/dev
ASAN_OPTIONS=detect_leaks=0 sh test/replay.sh build/dev
SUIT=build/dev/suit sh test/docs.sh
# One suite, three ways (plain, no USER/LOGNAME, umask 027), on the dev build as it is.
quick: $(CHECK_BUILD)
@sh tools/quick.sh $(SUITE)
# The whole local gate, stopping at the first failure (tools/gate.sh).
gate:
@sh tools/gate.sh
build/dev/replay-%: test/fuzz_%.c test/replay.c $(CORE) $(HDR) $(DEV_BR) Makefile
@mkdir -p build/dev
$(CC) $(W) $(SAN) $(BRI) -Isrc -o $@ $< test/replay.c $(CORE) $(DEV_BR)
# ---- release: fast, no test code ------------------------------------------
release: build/release/suit build/release/suit-run build/release/empty
sh test/release_clean.sh build/release/suit build/dev/suit
build/release/suit: $(LIB) $(MAIN) $(HDR) $(REL_V) Makefile
@mkdir -p build/release
$(CC) $(W) $(REL) $(LF) -o $@ $(LIB) $(MAIN) $(REL_V) $(LDLIBS)
build/release/suit-run: src/run_main.c src/hook.c src/stamp.c src/platform.c $(HDR) Makefile
@mkdir -p build/release
$(CC) $(W) $(REL) -DSUIT_NO_NET -Isrc -o $@ src/run_main.c src/hook.c src/stamp.c src/platform.c
build/release/empty: test/empty.c Makefile
@mkdir -p build/release
$(CC) $(W) $(REL) -o $@ test/empty.c
check-budget: build/release/suit build/release/suit-run build/release/empty
SUIT=build/release/suit sh test/budget.sh
# ---- coverage: 100% of src/, or fail ---------------------------------------
build/cover/suit: $(LIB) $(MAIN) $(HDR) $(COV_V) Makefile
@mkdir -p build/cover
$(LLVM_BIN)/clang $(W) $(COV) $(LF) -o $@ $(LIB) $(MAIN) $(COV_V) $(LDLIBS)
build/cover/unit: test/unit.c $(LIB) $(HDR) $(COV_V) Makefile
@mkdir -p build/cover
$(LLVM_BIN)/clang $(W) $(COV) $(LF) -Isrc -o $@ test/unit.c $(LIB) $(COV_V) $(LDLIBS)
build/cover/replay-%: test/fuzz_%.c test/replay.c $(CORE) $(HDR) $(COV_BR) Makefile
@mkdir -p build/cover
$(LLVM_BIN)/clang $(W) $(COV) $(BRI) -Isrc -o $@ $< test/replay.c $(CORE) $(COV_BR)
build/cover/suit-run: src/run_main.c src/hook.c src/stamp.c src/platform.c $(HDR) Makefile
@mkdir -p build/cover
$(LLVM_BIN)/clang $(W) $(COV) -DSUIT_NO_NET -Isrc -o $@ src/run_main.c src/hook.c src/stamp.c src/platform.c
build/cover/empty: test/empty.c Makefile
@mkdir -p build/cover
$(CC) $(W) -O2 -o $@ test/empty.c
cover: $(HELPERS) build/cover/suit build/cover/suit-run build/cover/empty build/cover/unit build/cover/replay-conf build/cover/replay-stamp build/cover/replay-ledger build/cover/replay-remote build/cover/replay-project build/cover/replay-lintrules build/cover/replay-lintline build/cover/replay-need build/cover/replay-usage build/cover/replay-curate build/cover/replay-choices build/cover/replay-anchors
rm -rf build/cover/prof build/cover/all.profdata
mkdir -p build/cover/prof
LLVM_PROFILE_FILE=$(CURDIR)/build/cover/prof/%p-%m.profraw JOBS=$(JOBS) sh tools/suites.sh build/cover
LLVM_PROFILE_FILE=$(CURDIR)/build/cover/prof/%p-%m.profraw sh test/replay.sh build/cover
$(LLVM_BIN)/llvm-profdata merge -sparse build/cover/prof/*.profraw -o build/cover/all.profdata
python3 tools/cover_gate.py $(LLVM_BIN)/llvm-cov build/cover/all.profdata build/cover/suit build/cover/suit-run build/cover/unit build/cover/replay-conf build/cover/replay-stamp build/cover/replay-ledger build/cover/replay-remote build/cover/replay-project build/cover/replay-lintrules build/cover/replay-lintline build/cover/replay-need build/cover/replay-usage build/cover/replay-curate build/cover/replay-choices build/cover/replay-anchors
# ---- static analysis -------------------------------------------------------
analyze: build/gen/curate_page.inc
$(LLVM_BIN)/clang --analyze -Xclang -analyzer-output=text -Xclang -analyzer-werror $(SDK) $(W) -DSUIT_TEST $(LF) -Isrc $(LIB) $(MAIN) src/run_main.c
$(LLVM_BIN)/clang --analyze -Xclang -analyzer-output=text -Xclang -analyzer-werror $(SDK) $(W) -DSUIT_TEST -DSUIT_COVER $(LF) -Isrc $(LIB) $(MAIN) src/run_main.c
# ---- fuzzing ---------------------------------------------------------------
# libFuzzer from Homebrew's llvm when it works; otherwise a fixed-seed mutation
# loop. Both fuzz targets, seeds in test/corpus/NAME, findings kept in
# build/fuzz/corpus-NAME (replayed by check and cover).
build/fuzz/%: test/fuzz_%.c $(CORE) $(HDR) $(FUZZ_BR) Makefile
@mkdir -p build/fuzz
$(LLVM_BIN)/clang $(W) -O1 -g -fsanitize=fuzzer,address,undefined \
-fno-sanitize-recover=all -DSUIT_TEST $(BRI) -Isrc -o $@ $< $(CORE) $(FUZZ_BR)
fuzz: build/dev/replay-conf build/dev/replay-stamp build/dev/replay-ledger build/dev/replay-remote build/dev/replay-project build/dev/replay-lintrules build/dev/replay-lintline build/dev/replay-need build/dev/replay-usage build/dev/replay-curate build/dev/replay-choices build/dev/replay-anchors
@mkdir -p build/fuzz
@if printf 'int LLVMFuzzerTestOneInput(const unsigned char *d, unsigned long n) { (void)d; (void)n; return 0; }' \
| "$(LLVM_BIN)/clang" -fsanitize=fuzzer -x c - -o build/fuzz/probe >/dev/null 2>&1; then \
for t in conf stamp ledger remote project lintrules lintline need usage curate choices anchors; do \
$(MAKE) --no-print-directory build/fuzz/$$t && \
mkdir -p build/fuzz/corpus-$$t && \
{ echo "fuzz $$t: libFuzzer, $(FUZZ_TIME) seconds"; \
ASAN_OPTIONS=detect_leaks=0 build/fuzz/$$t build/fuzz/corpus-$$t test/corpus/$$t \
-max_total_time=$(FUZZ_TIME) -max_len=16384 -timeout=10 -close_fd_mask=3 \
-artifact_prefix=build/fuzz/ -print_final_stats=1 > build/fuzz/$$t.log 2>&1 \
|| { tail -30 build/fuzz/$$t.log; exit 1; }; \
grep number_of_executed_units build/fuzz/$$t.log | sed "s/^stat::/fuzz $$t: /"; } || exit 1; \
done; \
else \
for t in conf stamp ledger remote project lintrules lintline need usage curate choices anchors; do \
echo "fuzz $$t: mutation loop (no libFuzzer), fixed seed"; \
ASAN_OPTIONS=detect_leaks=0 build/dev/replay-$$t --mutate $(MUTATE_ITER) test/corpus/$$t || exit 1; \
done; \
fi
# Minimize each target's grown corpus (build/fuzz/corpus-NAME, from `make fuzz`)
# with libFuzzer's merge mode and keep it in test/corpus/NAME as grown-* files
# next to the hand-written seeds, so every push replays it under the sanitizers
# and Fil-C. At most 300 files or 1 MiB per target (tools/corpus.py).
corpus:
@for t in conf stamp ledger remote project lintrules lintline need usage curate choices anchors; do \
$(MAKE) --no-print-directory build/fuzz/$$t || exit 1; \
rm -rf build/fuzz/merged-$$t; mkdir -p build/fuzz/merged-$$t build/fuzz/corpus-$$t; \
ASAN_OPTIONS=detect_leaks=0 build/fuzz/$$t -merge=1 -close_fd_mask=3 -max_len=16384 \
build/fuzz/merged-$$t build/fuzz/corpus-$$t test/corpus/$$t > build/fuzz/$$t.merge.log 2>&1 \
|| { tail -20 build/fuzz/$$t.merge.log; exit 1; }; \
python3 tools/corpus.py $$t build/fuzz/merged-$$t test/corpus/$$t || exit 1; \
done
# ---- Fil-C (Linux only; tools/filc.sh sets up the toolchain) ---------------
filc:
sh tools/filc.sh
# Run by tools/filc.sh with FILCC pointing at the Fil-C compiler.
FILC_FLAGS := -O2 -g -DSUIT_TEST
filc-run: $(FILC_V) $(HELPERS) build/gen/curate_page.inc
@test -n "$(FILCC)" || { echo "no Fil-C compiler; set FILCC" >&2; exit 1; }
mkdir -p build/filc
$(FILCC) $(W) $(FILC_FLAGS) $(LF) -o build/filc/suit $(LIB) $(MAIN) $(FILC_V) $(LDLIBS)
$(FILCC) $(W) $(FILC_FLAGS) -DSUIT_NO_NET -Isrc -o build/filc/suit-run src/run_main.c src/hook.c src/stamp.c src/platform.c
$(FILCC) $(W) -O2 -o build/filc/empty test/empty.c
$(FILCC) $(W) $(FILC_FLAGS) $(LF) -Isrc -o build/filc/unit test/unit.c $(LIB) $(FILC_V) $(LDLIBS)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-conf test/fuzz_conf.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-stamp test/fuzz_stamp.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-ledger test/fuzz_ledger.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-remote test/fuzz_remote.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-lintrules test/fuzz_lintrules.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-lintline test/fuzz_lintline.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-need test/fuzz_need.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-usage test/fuzz_usage.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-curate test/fuzz_curate.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-choices test/fuzz_choices.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-project test/fuzz_project.c test/replay.c $(CORE) $(FILC_BR)
$(FILCC) $(W) $(FILC_FLAGS) $(BRI) -Isrc -o build/filc/replay-anchors test/fuzz_anchors.c test/replay.c $(CORE) $(FILC_BR)
build/filc/unit
SUIT=build/filc/suit sh test/run.sh
SUIT=build/filc/suit sh test/store.sh
SUIT=build/filc/suit sh test/place.sh
SUIT=build/filc/suit sh test/project.sh
SUIT=build/filc/suit sh test/usage.sh
SUIT=build/filc/suit sh test/curate.sh
SUIT=build/filc/suit sh test/curate_apply.sh
SUIT=build/filc/suit sh test/https.sh
sh test/replay.sh build/filc
# ---- CI: what the Linux runner runs (system cc, no clang, no coverage) -----
ci:
@echo "ci: $(CC) with -std=$(STD)"
$(MAKE) check
$(MAKE) filc
# ---- live: real harnesses through the local proxy (not part of all or ci) ---
live: build/release/suit
SUIT=build/release/suit sh test/live.sh
# ---- everything ------------------------------------------------------------
# Every gate, in order, stopping at the first failure.
all:
$(MAKE) suit
$(MAKE) release
$(MAKE) check
$(MAKE) cover
$(MAKE) analyze
$(MAKE) check-budget
$(MAKE) fuzz
$(MAKE) filc
clean:
rm -rf build