-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
185 lines (141 loc) · 5.54 KB
/
makefile
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
.SECONDARY:
HEADERS := $(shell find include -type f -name "*.h" -not -name ".*")
SOURCES := $(shell find src test -type f -iname "*.c")
SONAME = ml666
MAJOR = 0
MINOR = 0
PATCH = 0
prefix = /usr/local/
TYPE := release
ifdef debug
TYPE := debug
CFLAGS += -O0 -g
LDFLAGS += -g
else
CFLAGS += -O3
endif
ifdef asan
TYPE := $(TYPE)-asan
CFLAGS += -fsanitize=address,undefined
LDFLAGS += -fsanitize=address,undefined
endif
ifdef coverage
TYPE := $(TYPE)-coverage
CFLAGS += --coverage
LDFLAGS += --coverage
endif
CFLAGS += --std=c17
CFLAGS += -Iinclude
CFLAGS += -Wall -Wextra -pedantic -Werror
CFLAGS += -fstack-protector-all
CFLAGS += -Wno-missing-field-initializers
CFLAGS += -fvisibility=hidden -DML666_BUILD
ifndef debug
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS += -Wl,--gc-sections
endif
OBJECTS := $(patsubst %,build/$(TYPE)/o/%.o,$(SOURCES))
B-TS := bin/$(TYPE)/testsuite
BINS := $(patsubst src/main/%.c,bin/$(TYPE)/%,$(filter src/main/%.c,$(SOURCES)))
TESTS := $(patsubst test/%.c,%,$(filter test/%.c,$(SOURCES)))
.PHONY: all bin lib docs clean get//bin get//lib install uninstall shell test clean//docs install//docs uninstall//docs
all: bin lib docs
bin: $(BINS)
lib: lib/$(TYPE)/lib$(SONAME).a \
lib/$(TYPE)/lib$(SONAME).so
get//bin:
@echo bin/$(TYPE)/
get//lib:
@echo lib/$(TYPE)/
bin/$(TYPE)/%: build/$(TYPE)/o/src/main/%.c.o lib/$(TYPE)/lib$(SONAME).so
mkdir -p $(dir $@)
$(CC) -o $@ $(LDFLAGS) $< -Llib/$(TYPE)/ -l$(SONAME)
build/$(TYPE)/bin/%: build/$(TYPE)/o/test/%.c.o lib/$(TYPE)/lib$(SONAME).so
mkdir -p $(dir $@)
$(CC) -o $@ $(LDFLAGS) $< -Llib/$(TYPE)/ -l$(SONAME)
lib/$(TYPE)/lib$(SONAME).so: lib/$(TYPE)/lib$(SONAME).a
ln -sf "lib$(SONAME).so" "[email protected]"
$(CC) -o $@ -Wl,--no-undefined -Wl,-soname,lib$(SONAME).so.$(MAJOR) --shared -fPIC $(LDFLAGS) -Wl,--whole-archive $^ -Wl,--no-whole-archive
lib/$(TYPE)/lib$(SONAME).a: $(filter-out build/$(TYPE)/o/src/main/%,$(filter-out build/$(TYPE)/o/test/%,$(OBJECTS)))
mkdir -p $(dir $@)
rm -f $@
$(AR) q $@ $^
build/$(TYPE)/o/%.c.o: %.c makefile $(HEADERS)
mkdir -p $(dir $@)
$(CC) -fPIC -c -o $@ $(DFLAGS) $(CFLAGS) $<
build/$(TYPE)/test/tokenizer/%: test/%.ml666 test/%.tokenizer-result bin/$(TYPE)/ml666-tokenizer-example
mkdir -p $(dir $@)
LD_LIBRARY_PATH="$$PWD/lib/$(TYPE)/" \
bin/$(TYPE)/ml666-tokenizer-example <"$<" >"$@"
test//tokenizer/%: build/$(TYPE)/test/tokenizer/% test/%.tokenizer-result $(B-TS)
$(B-TS) "$(@:test//tokenizer//%=%)" diff -q "$<" "$(word 2,$^)"
test//tokenizer: $(B-TS)
$(B-TS) "tokenizer" $(MAKE) $(patsubst test/%.tokenizer-result,test//tokenizer//%,$(wildcard test/*.tokenizer-result))
build/$(TYPE)/test/json/%: test/%.ml666 test/%.json bin/$(TYPE)/ml666
mkdir -p $(dir $@)
# Normalizing json using jq. This way, changes to the output don't matter so long as it's still the same data
LD_LIBRARY_PATH="$$PWD/lib/$(TYPE)/" \
bin/$(TYPE)/ml666 <"$<" --output-format json | jq -c >"$@"
test//json//%: build/$(TYPE)/test/json/% test/%.json $(B-TS)
$(B-TS) "$(@:test//json//%=%)" diff -q "$<" "$(word 2,$^)"
test//json: $(B-TS)
$(B-TS) "JSON" $(MAKE) $(patsubst test/%.json,test//json//%,$(wildcard test/*.json test/**/*.json))
test//api//%: build/$(TYPE)/bin/% $(B-TS)
$(B-TS) "$(@:test//api//%=%)" sh -c ' \
"$<" | while read x; \
do $(B-TS) "$$x" "$<" "$$x"; \
done \
'
test//api: $(B-TS)
$(B-TS) "API" $(MAKE) $(TESTS:%=test//api//%)
clean: clean//docs
rm -rf build/$(TYPE)/ bin/$(TYPE)/ lib/$(TYPE)/
install:
cp "lib/$(TYPE)/lib$(SONAME).so" "$(DESTDIR)$(prefix)/lib/lib$(SONAME).so.$(MAJOR).$(MINOR).$(PATCH)"
ln -sf "lib$(SONAME).so.$(MAJOR).$(MINOR).$(PATCH)" "$(DESTDIR)$(prefix)/lib/lib$(SONAME).so.$(MAJOR).$(MINOR)"
ln -sf "lib$(SONAME).so.$(MAJOR).$(MINOR).$(PATCH)" "$(DESTDIR)$(prefix)/lib/lib$(SONAME).so.$(MAJOR)"
ln -sf "lib$(SONAME).so.$(MAJOR).$(MINOR).$(PATCH)" "$(DESTDIR)$(prefix)/lib/lib$(SONAME).so"
cp "bin/$(TYPE)/ml666" "$(DESTDIR)$(prefix)/bin/ml666"
cp "lib/$(TYPE)/lib$(SONAME).a" "$(DESTDIR)$(prefix)/lib/lib$(SONAME).a"
cp -a include/ml666/./ "$(DESTDIR)$(prefix)/include/ml666/"
ldconfig
uninstall:
rm -f "$(DESTDIR)$(prefix)/lib/lib$(SONAME).so.$(MAJOR).$(MINOR).$(PATCH)"
rm -f "$(DESTDIR)$(prefix)/lib/lib$(SONAME).so.$(MAJOR).$(MINOR)"
rm -f "$(DESTDIR)$(prefix)/lib/lib$(SONAME).so.$(MAJOR)"
rm -f "$(DESTDIR)$(prefix)/lib/lib$(SONAME).so"
rm -f "$(DESTDIR)$(prefix)/lib/lib$(SONAME).a"
rm -f "$(DESTDIR)$(prefix)/bin/ml666"
rm -rf "$(DESTDIR)$(prefix)/include/ml666/"
install//docs:
mkdir -p "$(DESTDIR)$(prefix)/share/man/man3/"
rm -f "$(DESTDIR)$(prefix)/share/man/man3/"ml666*.3
cp build/docs/api/man/man3/ml666*.3 "$(DESTDIR)$(prefix)/share/man/man3/"
uninstall//docs:
rm -f "$(DESTDIR)$(prefix)/share/man/man3/"ml666*.3
shell:
if [ -z "$$SHELL" ]; then SHELL="$$(getent passwd $$(id -u) | cut -d : -f 7)"; fi; \
if [ -z "$$SHELL" ]; then SHELL="/bin/sh"; fi; \
PROMPT_COMMAND='if [ -z "$$PS_SET" ]; then PS_SET=1; PS1="(ml666) $$PS1"; fi' \
LD_LIBRARY_PATH="$$PWD/lib/$(TYPE)/" \
PATH="$$PWD/bin/$(TYPE)/:scripts/:$$PATH" \
MANPATH="$$PWD/build/docs/api/man/:$$(man -w)" \
"$$SHELL"
test: $(B-TS)
$(B-TS) "ml666" $(MAKE) test//tokenizer test//json test//api
do-coverage:
-$(MAKE) test
gcov -n $$(find build/$(TYPE)/ -type f -iname '*.gcno')
coverage:
$(MAKE) coverage=1 do-coverage
docs: build/docs/api/.done
build/docs/api/.done: $(HEADERS) Doxyfile
rm -rf build/docs/api/
-doxygen -q
-cp -r docs/. build/docs/api/html/
-ln -sf . build/docs/api/html/docs
-ln -sf . build/docs/api/html/test
-cp test/example.ml666 build/docs/api/html/test/
-touch "$@"
clean//docs:
rm -rf build/docs/api/