aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 4165c73e934b1955995d6807c9d42f807601f5bc (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
CC              = clang
CFLAGS          = -Wall -std=c89 -D_DEFAULT_SOURCE -D_GNU_SOURCE -Wvla \
		  -Wdeclaration-after-statement -Wstrict-prototypes \
		  -Wunreachable-code -Wsign-compare -Wimplicit-int-conversion \
		  -Wsign-conversion
LDFLAGS         = -lm
BUILDDIR        = build
SOURCEDIR       = src
OBJECTDIR       = obj

TESTDIR         = tests
TESTCFLAGS      = -D_TEST -g -Wall -std=c89 -D_DEFAULT_SOURCE -D_GNU_SOURCE -Wvla \
		  -Wdeclaration-after-statement -Wstrict-prototypes \
		  -Wunreachable-code -Wsign-compare -Wimplicit-int-conversion \
		  -Wsign-conversion
TESTLDFLAGS     = $(LDFLAGS) -lcheck

COVCFLAGS       = -fprofile-instr-generate -fcoverage-mapping

OUTPUT          = dnsd

SRCS = $(wildcard $(SOURCEDIR)/*.c)
OBJS = $(SRCS:.c=.o)
OBJ  = $(OBJS:$(SOURCEDIR)/%=$(OBJECTDIR)/%)

TESTS = $(wildcard $(TESTDIR)/*.c)
TOBJS = $(TESTS:.c=.o)
TSUBS = $(filter-out $(OBJECTDIR)/main.o,$(OBJ))

RUNARGS = -p 5333 -z tests/zone.file

# default build target
DEFAULT = debug

default: $(DEFAULT)

build: dir $(OBJ)
	@echo [ LD ] $(OBJ)
	@$(CC) $(CFLAGS) -o $(BUILDDIR)/$(OUTPUT) $(OBJ) $(LDFLAGS)

debug: CFLAGS += -g -D_DEBUG
debug: build;

build_test: dir $(TOBJS) $(TSUBS)
	@echo [ LD ] $(TOBJS) $(TSUBS)
	@$(CC) $(TESTCFLAGS) -o $(TESTDIR)/run $(TOBJS) $(TSUBS) $(TESTLDFLAGS)

test: build_test
	@echo [EXEC] $(TESTDIR)/run
	@$(TESTDIR)/run

valgrind_test: build_test
	@CK_FORK=no valgrind --leak-check=full $(TESTDIR)/run

coverage: CFLAGS += $(COVCFLAGS)
coverage: TESTLDFLAGS += -fprofile-instr-generate
coverage: clean build_test
	@LLVM_PROFILE_FILE="coverage.profraw" $(TESTDIR)/run
	@llvm-profdata merge -sparse coverage.profraw -o coverage.profdata
	@llvm-cov show $(TESTDIR)/run -instr-profile=coverage.profdata -format=html \
		> coverage.html
	@llvm-cov report $(TESTDIR)/run -instr-profile=coverage.profdata

dir:
	@mkdir -p $(OBJECTDIR)
	@mkdir -p $(BUILDDIR)

$(OBJECTDIR)/%.o: $(SOURCEDIR)/%.c
	@echo [ CC ] $<
	@$(CC) $(CFLAGS) -c $< -o $@

$(TESTDIR)/%.o: $(TESTDIR)/%.c
	@echo [TEST] $<
	@$(CC) $(TESTCFLAGS) -c $< -o $@

#sudo setcap 'cap_net_bind_service=+ep' /path/to/prog
#to allow port access
run: $(DEFAULT)
	@echo [EXEC] $(BUILDDIR)/$(OUTPUT) $(RUNARGS)
	$(BUILDDIR)/$(OUTPUT) $(RUNARGS)

.PHONY: clean
clean:
	@echo [ RM ] $(OBJ) $(TOBJS)
	@echo [ RM ] $(BUILDDIR)/$(OUTPUT) $(TESTDIR)/run
	@rm -df  $(OBJ) $(TOBJS) $(TESTDIR)/run
	@rm -Rdf $(BUILDDIR) $(OBJECTDIR)
	@rm -f {$(SOURCEDIR),$(TESTDIR)}/*.gc{da,no}
	@rm -f *.prof{raw,data}
	@rm -f coverage.html

.PHONY: all
all: clean $(DEFAULT)

devsetup:
	@echo "[" > compile_commands.json
	@for file in $(SRCS); do \
		echo "{\"directory\":\"$(PWD)\",\"command\":\"$(shell which $(CC)) $(CFLAGS) -c $$file\",\"file\":\"$(PWD)/$$file\"}," >> compile_commands.json;\
	done
	@for file in $(TESTS); do \
		echo "{\"directory\":\"$(PWD)\",\"command\":\"$(shell which $(CC)) $(TESTCFLAGS) -c $$file\",\"file\":\"$(PWD)/$$file\"}," >> compile_commands.json;\
	done
	@echo "]" >> compile_commands.json