summaryrefslogtreecommitdiffstats
path: root/Makefile
blob: 11746cc9596eeeacb5b9d21c7569b23f541a0a98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
SRC_DIR = src
CMD_DIR = cmd
TST_DIR = test

OBJ_DIR = obj

PROG = git-authz

CMD_SRC = $(wildcard $(CMD_DIR)/*.c)
CMD_OBJ = $(CMD_SRC:%=$(OBJ_DIR)/%.o)

TST_SRC = $(wildcard $(TST_DIR)/*.c)
TST_RUN = $(TST_SRC:$(TST_DIR)/%.c=$(OBJ_DIR)/test_runner/%)
TST_OBJ = $(TST_SRC:$(TST_DIR)/%.c=$(OBJ_DIR)/$(TST_DIR)/%.c.o)

CC_SRC = $(wildcard $(SRC_DIR)/*.c)
CC_OBJ = $(CC_SRC:%=$(OBJ_DIR)/%.o)

# -MMD writes a makefile of the headers each object actually includes.  Without
# it, editing src/authz.h rebuilds nothing: the objects still match their .c
# files, so `make test' passes against a binary that is not the code on disk.
CFLAGS = -std=c99 -Wall -Wextra -Wpedantic -I$(SRC_DIR)
CFLAGS += -D_POSIX_C_SOURCE=200809L
CFLAGS += -MMD -MP
CFLAGS += -g
LDFLAGS =

DEPS = $(CC_OBJ:.o=.d) $(CMD_OBJ:.o=.d) $(TST_OBJ:.o=.d)

$(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 -MMD -MP
sanitizer: LDFLAGS := -fsanitize=address,undefined
sanitizer: test

# 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)
	@if [ -e $(DESTDIR)$(SYSCONFDIR)/authz.conf ]; then \
	  install -m 0644 authz.conf $(DESTDIR)$(SYSCONFDIR)/authz.conf.new; \
	  echo "  kept $(DESTDIR)$(SYSCONFDIR)/authz.conf; new example is authz.conf.new"; \
	else \
	  install -m 0644 authz.conf $(DESTDIR)$(SYSCONFDIR)/authz.conf; \
	fi
	link=`awk -v from='$(HOOKDIR)' -v to='$(BINDIR)/$(PROG)' -f build/relpath.awk`; \
	rm -f $(DESTDIR)$(HOOKDIR)/pre-receive; \
	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) -rf obj/
	@$(RM) -rf $(TST_DIR)/tmp
	@$(RM) $(PROG)

# Included last: the .d files only exist after something has been compiled, and
# a missing one is not an error.
-include $(DEPS)