From bc7f4da406fbd802b08985ba583e52f040bdba85 Mon Sep 17 00:00:00 2001 From: dev Date: Sat, 14 Nov 2026 00:06:35 +0000 Subject: Layout: cmd/, src/, test/, everything built into obj/ The tree had grown sideways: main.c living among the sources it dispatches to, one test binary covering everything, object files left in the source directory because the Makefile put them there and, worse, tracked them. Now the command line is cmd/main.c, the engine is src/ and nothing in it knows about the command line, and each area of the engine has its own program under test/ - glob, norm, parse, need, check - each linking the library and reporting its own count. A failure says which area broke instead of taking the whole run down with a single "unit: 171" line, which is the only thing a per-file split buys and it is worth the small duplication the shared test.h absorbs. Objects go to obj/, test runners to obj/test_runner/, so a build leaves the tree as it found it. The stale src/*.o and tests/unit that were committed earlier are gone; they were build output in a source tree and the .gitignore that should have stopped them arrived after they did. The Makefile follows the same shape as the other C trees here: directories in variables, wildcards for the sources, one pattern rule that makes its own directory, test runners built from the same objects as the binary so they cannot test a different build than ships, and `test` running every runner with a foreach so the first failure stops the line. `strict` is `all` with -Werror, which is what CI now calls instead of passing CFLAGS in from outside; the sanitizer and install targets are unchanged in what they do. Behaviour is unchanged: 171 direct checks and 154 behavioural ones, same as before the move, and make install still stages a working hook symlink. --- Makefile | 132 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 72 insertions(+), 60 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 1f93122..758b968 100644 --- a/Makefile +++ b/Makefile @@ -1,72 +1,83 @@ -# git-authz - who may move which refs. C99 and libc, nothing else. -# -# make build ./git-authz -# make test build, then run the unit suite and the behavioural suite -# make install install into $(DESTDIR)$(PREFIX) -# make clean -# -# No configure script, no cmake, no generated headers: the only portability -# knob this program needs is CFLAGS. Warnings are variables so that a strict -# build is a command line and not a build system: -# -# make WARN="-Wall -Wextra -Wpedantic -Werror" +SRC_DIR = src +CMD_DIR = cmd +TST_DIR = test -CC ?= cc -PREFIX ?= /usr/local -BINDIR ?= $(PREFIX)/bin -SHAREDIR ?= $(PREFIX)/share/git-host -SYSCONFDIR ?= /etc/git-host -HOOKDIR ?= $(SHAREDIR)/hooks +OBJ_DIR = obj -STD ?= -std=c99 -WARN ?= -Wall -Wextra -Wpedantic -CFLAGS ?= -O2 -g -CPPFLAGS += -D_POSIX_C_SOURCE=200809L +PROG = git-authz -PROG = git-authz -UNIT = tests/unit -OBJS = src/util.o src/glob.o src/policy.o src/hook.o src/validate.o src/edit.o \ - src/main.o +CMD_SRC = $(wildcard $(CMD_DIR)/*.c) +CMD_OBJ = $(CMD_SRC:%=$(OBJ_DIR)/%.o) -all: $(PROG) +TST_SRC = $(wildcard $(TST_DIR)/*.c) +TST_RUN = $(TST_SRC:$(TST_DIR)/%.c=$(OBJ_DIR)/test_runner/%) -$(PROG): $(OBJS) - $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) +CC_SRC = $(wildcard $(SRC_DIR)/*.c) +CC_OBJ = $(CC_SRC:%=$(OBJ_DIR)/%.o) -src/%.o: src/%.c src/authz.h - $(CC) $(STD) $(WARN) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< +CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -I$(SRC_DIR) +CFLAGS += -D_POSIX_C_SOURCE=200809L +CFLAGS += -g +LDFLAGS = -# AddressSanitizer/UBSan build of everything, so that the behavioural suite - -# which is what actually exercises the editing code paths - runs against them: -# -# make clean && make sanitizer -# -# Leaks are checked, which is why every command frees what it read: a program -# that exits in a millisecond does not need the memory back, but it does need -# the leak report to stay empty, or the one real leak hides among forty. -sanitizer: CFLAGS := -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer +$(OBJ_DIR)/%.c.o: %.c + @mkdir -p $(@D) + $(CC) $(CFLAGS) -o $@ -c $< + +$(PROG): $(CC_OBJ) $(CMD_OBJ) + @mkdir -p $(@D) + $(CC) $(LDFLAGS) $(CC_OBJ) $(CMD_OBJ) -o $@ + +$(OBJ_DIR)/test_runner/%: $(OBJ_DIR)/$(TST_DIR)/%.c.o $(CC_OBJ) + @mkdir -p $(@D) + $(CC) $(LDFLAGS) $(CC_OBJ) $< -o $@ + +# The test/*.c programs are the pieces tested directly: what a glob matches, +# what a ref normalises to, what a policy decides and what the refusal says. +# test/run.sh is the other half - the same binary driven the way a receive hook +# drives it, with an identity in the environment and "old new ref" on stdin, +# and no git anywhere in it. +.PHONY: test +test: $(PROG) $(TST_RUN) + @mkdir -p $(TST_DIR)/tmp + $(foreach var,$(TST_RUN),./$(var) &&) true + AUTHZ=./$(PROG) sh $(TST_DIR)/run.sh + +# Both suites again with AddressSanitizer and UBSan, which is the only reason +# every command frees what it read: a process that exits in a millisecond does +# not need the memory back, but it does need that report to stay empty, or the +# one real leak hides among forty. +.PHONY: sanitizer +sanitizer: CFLAGS := -std=c99 -Wall -Wextra -Wpedantic -I$(SRC_DIR) \ + -D_POSIX_C_SOURCE=200809L -O1 -g -fsanitize=address,undefined \ + -fno-omit-frame-pointer sanitizer: LDFLAGS := -fsanitize=address,undefined sanitizer: test -# The unit test links the parts it tests, not the command line, so that a -# broken argument parser cannot hide a working matcher (or the other way round). -$(UNIT): tests/unit.c src/util.o src/glob.o src/policy.o - $(CC) $(STD) $(WARN) $(CPPFLAGS) $(CFLAGS) -o $@ tests/unit.c \ - src/util.o src/glob.o src/policy.o - -test: $(PROG) $(UNIT) - mkdir -p tests/tmp - $(UNIT) - AUTHZ=./$(PROG) sh tests/run.sh - -# The hook is a symlink to the program, named 'pre-receive', because that name -# is what core.hooksPath looks for and what selects authorize mode inside the -# binary. The link is relative so that a staged install, an unpacked prefix and -# the image built from either all resolve to the binary that came with them -# rather than to whatever lives at the same absolute path on the reading side. +# The same build with the warnings turned into errors, so that CI proves the +# tree is clean rather than merely quiet. +.PHONY: strict +strict: CFLAGS += -Werror +strict: all + +.PHONY: all +all: $(PROG) $(TST_RUN) + +# The hook is a symlink named 'pre-receive', because that name is what +# core.hooksPath looks for and what selects authorize mode inside the binary. +# It is relative so that a staged install, an unpacked prefix or an image +# assembled elsewhere points at the binary that came with it rather than at +# whatever occupies that absolute path on the machine reading it. # # The policy is configuration: an existing one is an operator's file, so it is # kept and the new one arrives beside it as authz.conf.new. +PREFIX ?= /usr/local +BINDIR ?= $(PREFIX)/bin +SHAREDIR ?= $(PREFIX)/share/git-host +SYSCONFDIR ?= /etc/git-host +HOOKDIR ?= $(SHAREDIR)/hooks + +.PHONY: install install: $(PROG) install -d $(DESTDIR)$(BINDIR) $(DESTDIR)$(HOOKDIR) $(DESTDIR)$(SYSCONFDIR) install -m 0755 $(PROG) $(DESTDIR)$(BINDIR)/$(PROG) @@ -81,11 +92,12 @@ install: $(PROG) ln -s "$$link" $(DESTDIR)$(HOOKDIR)/pre-receive; \ echo " $(HOOKDIR)/pre-receive -> $$link" +.PHONY: uninstall uninstall: rm -f $(DESTDIR)$(BINDIR)/$(PROG) $(DESTDIR)$(HOOKDIR)/pre-receive +.PHONY: clean clean: - rm -f $(OBJS) $(PROG) $(UNIT) - rm -rf tests/tmp - -.PHONY: all test install uninstall clean + @$(RM) -rf obj/ + @$(RM) -rf $(TST_DIR)/tmp + @$(RM) $(PROG) -- cgit v1.2.3