-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
132 lines (96 loc) · 3.08 KB
/
makefile
File metadata and controls
132 lines (96 loc) · 3.08 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
library_name := approvals
##### folders
src_dir := ./src
test_dir := ./tests
cov_dir := ./gcov
lib_dir := ./lib
example_dir := ./example
##### files
src_files := $(wildcard $(src_dir)/*.c)
obj_files := $(src_files:.c=.o)
ifeq ($(OS),Windows_NT)
exec_extension := .exe
else
exec_extension := .
endif
test_src_files := $(wildcard $(test_dir)/*Test.c)
test_run_files := $(test_src_files:.c=$(exec_extension))
cov_sources := $(src_files:.c=.c.gcov)
cov_files := $(cov_sources:$(src_dir)/%=$(cov_dir)/%)
ifeq ($(OS),Windows_NT)
library := ./bin/$(library_name).dll
else
library := ${lib_dir}/lib$(library_name).so
endif
example_src_files := $(wildcard $(example_dir)/*Test.c)
example_run_files := $(example_src_files:.c=$(exec_extension))
##### tools
CC := @gcc
COV := gcov
##### flags
CFLAGS := -g
# user can override with -O
STD := c99
C_BASE_FLAGS := -std=$(STD) -pedantic -pedantic-errors
C_BASE_FLAGS := -Werror -Wall -Wextra
C_BASE_FLAGS += -Wbad-function-cast -Wconversion -Wdeprecated -Wdiv-by-zero -Wfloat-equal -Wformat=2 -Wint-to-pointer-cast -Wjump-misses-init -Wlogical-op -Woverflow -Wpointer-to-int-cast -Wshadow -Wswitch-default
C_BASE_FLAGS += -Wno-error=format -Wno-error=unused-variable -Wno-error=format-nonliteral
ifneq ($(OS),Windows_NT)
C_BASE_FLAGS += -fPIC
endif
C_COMPILE_FLAGS := $(C_BASE_FLAGS) -c ${CFLAGS}
C_TEST_FLAGS := $(C_BASE_FLAGS) ${CFLAGS}
C_LIBRARY_FLAGS := $(C_BASE_FLAGS) ${CFLAGS} -shared
ifeq ($(OS),Windows_NT)
C_LIBRARY_FLAGS += -Wl,--out-implib,${lib_dir}/lib$(library_name).a
# https://stackoverflow.com/questions/17601949/building-a-shared-library-using-gcc-on-linux-and-mingw-on-windows
endif
LD_FLAGS := -lcmocka
LD_APPROVALS := -l$(library_name)
##### compile targets
all: build test example
$(src_dir)/%.o: $(src_dir)/%.c
$(CC) $(C_COMPILE_FLAGS) $< -o $@
##### test targets
$(test_dir)/%$(exec_extension): $(test_dir)/%.c ${obj_files}
$(CC) $(C_TEST_FLAGS) ${obj_files} $< $(LD_FLAGS) -o $@
.PHONY: test
test: ${test_run_files}
for exe in ${test_run_files}; do $$exe || exit; done
.PHONY: check
check: test
##### coverage targets
$(cov_dir):
mkdir $(cov_dir)
$(cov_dir)/%.c.gcov: $(src_dir)/%.c $(cov_dir)
${COV} $<
mv *.c.gcov ${cov_dir}/
coverage: C_COMPILE_FLAGS += --coverage
coverage: LD_FLAGS += -lgcov
coverage: clean test ${cov_files}
rm -f $(src_dir)/*.gcno $(src_dir)/*.gcda
##### library targets
${library}: ${obj_files}
$(CC) $(C_LIBRARY_FLAGS) $^ $(LD_FLAGS) -o ${library}
build: very-clean ${library}
lib: build
##### clean targets
.PHONY: clean
clean:
rm -f $(src_dir)/*.o
rm -f $(test_dir)/*$(exec_extension) $(test_dir)/*.received.*
rm -f ${example_dir}/*$(exec_extension) ${example_dir}/*.received.*
rm -f -r ${cov_dir}
# Eclipse folder
rm -f -r ./Default
.PHONY: very-clean
very-clean: clean
rm -f ${library}
rm -f $(lib_dir)/*.a
##### example targets
# example build needs approvals to be "installed"
$(example_dir)/%$(exec_extension): $(example_dir)/%.c ${library}
$(CC) $(C_TEST_FLAGS) $< $(LD_FLAGS) $(LD_APPROVALS) -o $@
.PHONY: example
example: ${example_run_files}
for exe in ${example_run_files}; do $$exe || exit; done