-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakefile
More file actions
260 lines (215 loc) · 6.89 KB
/
Copy pathmakefile
File metadata and controls
260 lines (215 loc) · 6.89 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/make -f
CC = gcc
BUILD := build
BIN := $(BUILD)/bin
OBJ := $(BUILD)/obj
SOURCES := $(patsubst src/%.c,%,$(wildcard src/*.c))
TESTS := $(patsubst test/%.c,%,$(wildcard test/*.c))
CFLAGS += -std=gnu11
CFLAGS += -O3 -march=native -fdata-sections -ffunction-sections
CFLAGS += -Wall -Wextra -Wpedantic
CFLAGS += -Wno-unused-result -Wno-unused-value -Wno-unused-function
CFLAGS += -Iincl -I/usr/local/lib
LDLIBS += -lm -pthread
#CFLAGS += $(shell pkg-config --cflags libcurl)
#LDLIBS += $(shell pkg-config --libs libcurl)
###BEGIN-UPDATE
UPDATE := 'https://raw.githubusercontent.com/mpwaser/make-ide/master/makefile'
VERSION := 'v0.1.0'
ifeq ($(DEBUG),true)
CFLAGS := -g3 -O0 $(filter-out -O3,$(CFLAGS))
endif
ifeq ($(PROFILE),true)
CFLAGS := -pg -g3 -O0 $(filter-out -O3,$(CFLAGS))
endif
NAME := foo
NAME_UPPER = $(shell echo $(NAME) | tr a-z A-Z)
TEST := main
DEBUG := false
PROFILE := false
main: $(SOURCES:%=$(OBJ)/%.o) ## Compile main
$(CC) $^ $(CFLAGS) -MMD $(LDLIBS) -o $@
$(OBJ)/%.o: src/%.c | $(OBJ)
$(CC) -c $(CFLAGS) -MMD -MT $@ -MF $(OBJ)/$*.d $< -o $@
check: $(TESTS:%=$(BIN)/%) ## Run all or specifi unit tests, e.g. make check TEST=foo
ifneq ($(PROFILE),true)
ifneq ($(DEBUG),true)
ifeq ($(TEST),main)
@for name in $^; do ./$$name || exit; done
else
./build/bin/test_$(TEST)
endif
endif
endif
$(BIN)/%: $(OBJ)/%.o $(filter-out $(OBJ)/main.o,$(SOURCES:%=$(OBJ)/%.o)) | $(BIN)
@$(CC) $^ $(CFLAGS) -MMD $(LDLIBS) -o $@
$(OBJ)/%.o: test/%.c | $(OBJ)
@$(CC) -c $(CFLAGS) -MMD -MT $@ -MF $(OBJ)/$*.d $< -o $@
$(BIN) $(OBJ):
@mkdir -p $@
clean: ## Remove build folder
rm -rf build
.PHONY: run
run: ## Compile main and run executable
make main -s && ./main
.PHONY: profile
profile: ## Generate main or unit test profiling information, e.g. make profile TEST=foo
ifneq ($(TEST),main)
@make check PROFILE=true TEST=$(TEST) && ./build/bin/test_$(TEST)
@gprof build/bin/test_$(TEST) gmon.out > PROFILE && rm gmon.out
@cat PROFILE
else
@make PROFILE=true && ./main
@gprof ./main gmon.out > PROFILE && rm gmon.out && cat PROFILE
endif
.PHONY: debug
debug: ## Debug main or module unit test, e.g. make debug TEST=foo
ifeq ($(TEST),main)
make main DEBUG=true && gdb ./$(TEST)
else
make check DEBUG=true && gdb ./build/bin/test_$(TEST)
endif
.PHONY: ide
ide: ## Open all project related files in Vim
ctags -R . && vim -c "set list nu et sta sts=2 ts=2 sw=2 tag \
| vsp | args **/*.c **/*.h <CR> "
.PHONY: module
module: ## Create templates for new modules, e.g. make module NAME=foo
@printf '%s\n' '#include <stdlib.h>' '' '#include "$(NAME).h"' '' '' \
'void' 'function(void)' '{' '' '}' >> src/$(NAME).c
@printf '%s\n' '#ifndef $(NAME_UPPER)_H_' '' '' '/* description */' \
'void' 'function(void);' '' '' '#endif' >> incl/$(NAME).h
@printf '%s\n' '#include <stdlib.h>' '#include <stdbool.h>' '' \
'#include "check.h"' '#include "$(NAME).h"' '' '' 'static void' \
'test_feature(void)' '{' ' check_assert(0);' '}' '' '' \
'int main(void)' '{' ' check_init("test_$(NAME)");' \
' check_run(test_feature);' ' exit(EXIT_SUCCESS);' '}' \
>> test/test_$(NAME).c
.PHONY: project
project: ## Set up folder structure for new project in empty root folder
@mkdir -p test src incl
$(file > check.h,$(CHECK))
@mv check.h incl/check.h
@printf '%s\n' '#include <stdlib.h>' '' '' 'int main(void)' \
'{' '' '}' >> src/main.c
.PHONY: update
update: ## Replace local makefile between UPDATE markers with repo content
@curl -s $(UPDATE) > update
@sed -i '1,/^###BEGIN-UPDATE$$/d' update
@sed -i '/^###BEGIN-UPDATE$$/,$$d' makefile
@cat update >> makefile && rm update && echo 'Update make-ide to $(VERSION)'
.PHONY: help
help:
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
| sed -n 's/^\(.*\): \(.*\)##\(.*\)/\1\t\3/p'
define CHECK =
#ifndef CHECK_H_
#define CHECK_H_
#include <unistd.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define CHECK_SHOW_MAX 20
#define CHECK_EXPR_LENGTH 100
struct check_info {
unsigned num_tests, num_failed;
char fail_msg[CHECK_SHOW_MAX][CHECK_EXPR_LENGTH];
int main_pid;
};
static struct check_info* ci;
#define PASSED "\x1B[1;32mok\x1B[0m"
#define FAILED "\x1B[1;31mfailed\x1B[0m"
#define PASS_DOT "\x1B[1;32m.\x1B[0m"
#define FAIL_DOT "\x1B[1;31mF\x1B[0m"
#define EXIT_DOT "\x1B[1;31mE\x1B[0m"
#define SIGNAL_DOT "\x1B[1;31mS\x1B[0m"
#define CHECK__STR(n) #n
#define CHECK_STR(n) CHECK__STR(n)
#define check__assert(expr, ...) do { \
if (!(expr)) { \
snprintf(ci->fail_msg[ci->num_failed++], CHECK_EXPR_LENGTH, \
__FILE__ ": " CHECK_STR(__LINE__) ": " __VA_ARGS__); \
return; \
} \
} while (0)
#define check_assert(expr) check__assert(expr, "'%s' failed\n", #expr)
static void
check_exit(void)
{
if (getpid() == ci->main_pid)
{
if (!ci->num_failed)
{
fprintf(stderr, " %s %u test%s\n", PASSED, ci->num_tests,
ci->num_tests == 1 ? "" : "s");
}
else
{
fprintf(stderr, " %s %u of %u test%s\n", FAILED, ci->num_failed,
ci->num_tests, ci->num_tests == 1 ? "" : "s");
size_t k = ci->num_failed > CHECK_SHOW_MAX ? CHECK_SHOW_MAX : ci->num_failed;
for (size_t i = 0; i < k; ++i)
{
printf("%s", ci->fail_msg[i]);
}
}
}
fflush(stderr);
munmap(ci, sizeof(*ci));
}
void
check_init(const char* name)
{
ci = mmap(NULL, sizeof(*ci), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
ci->num_tests = ci->num_failed = 0;
ci->main_pid = getpid();
atexit(check_exit);
fprintf(stderr, "%8s: ", name);
}
void
check_error(const char* file, int line, const char* fname, int stat)
{
int sig = 0;
if ((sig = WEXITSTATUS(stat)))
{
fprintf(stderr, "%s", EXIT_DOT);
snprintf(ci->fail_msg[ci->num_failed++], CHECK_EXPR_LENGTH,
"%s:%d: %s non-zero exit (%d)\n", file, line, fname, sig);
}
else if ((sig = WTERMSIG(stat)))
{
fprintf(stderr, "%s", SIGNAL_DOT);
snprintf(ci->fail_msg[ci->num_failed++], CHECK_EXPR_LENGTH,
"%s:%d: %s recieved signal %d (%s)\n", file, line, fname,
sig, strsignal(sig));
}
fflush(stderr);
}
#define check_run(fn) check__run(__FILE__, __LINE__, #fn, fn)
static void __attribute__ ((unused))
check__run(const char* file, int line, const char* fname, void(*fn)(void))
{
int stat = 0;
unsigned fails = ci->num_failed;
++ci->num_tests;
pid_t pid = fork();
if (pid == 0)
{
fn();
fprintf(stderr, "%s", ci->num_failed > fails ? FAIL_DOT : PASS_DOT);
fflush(stderr);
exit(ci->num_failed ? EXIT_FAILURE : EXIT_SUCCESS);
}
else
{
waitpid(pid, &stat, 0);
check_error(file, line, fname, stat);
exit(EXIT_FAILURE);
}
}
#endif
endef