This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
302 lines (215 loc) · 8.69 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# ============================================================================ #
#
#
#
# ---------------------------------------------------------------------------- #
MAKEFILE_DIR ?= $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
# ---------------------------------------------------------------------------- #
.PHONY: all clean FORCE
.DEFAULT_GOAL = bin
# ---------------------------------------------------------------------------- #
CXX ?= c++
RM ?= rm -f
CAT ?= cat
PKG_CONFIG ?= pkg-config
NIX ?= nix
UNAME ?= uname
MKDIR ?= mkdir
MKDIR_P ?= $(MKDIR) -p
CP ?= cp
TR ?= tr
SED ?= sed
TEST ?= test
DOXYGEN ?= doxygen
# ---------------------------------------------------------------------------- #
OS ?= $(shell $(UNAME))
OS := $(OS)
ifndef libExt
ifeq (Linux,$(OS))
libExt ?= .so
else
libExt ?= .dylib
endif # ifeq (Linux,$(OS))
endif # ifndef libExt
# ---------------------------------------------------------------------------- #
VERSION := $(file < $(MAKEFILE_DIR)/version)
# ---------------------------------------------------------------------------- #
PREFIX ?= $(MAKEFILE_DIR)/out
BINDIR ?= $(PREFIX)/bin
LIBDIR ?= $(PREFIX)/lib
INCLUDEDIR ?= $(PREFIX)/include
# ---------------------------------------------------------------------------- #
LIBFLOXRESOLVE = libflox-resolve$(libExt)
LIBS = $(LIBFLOXRESOLVE)
COMMON_HEADERS = $(wildcard include/*.hh) $(wildcard include/flox/*.hh)
TESTS = $(wildcard tests/*.cc)
SRCS = $(wildcard src/*.cc)
bin_SRCS = $(filter src/main%.cc, $(SRCS))
lib_SRCS = $(filter-out $(bin_SRCS),$(SRCS))
BINS = $(patsubst src/main-%.cc,%,$(bin_SRCS))
# ---------------------------------------------------------------------------- #
EXTRA_CXXFLAGS ?= -Wall -Wextra -Wpedantic
CXXFLAGS ?= $(EXTRA_CFLAGS) $(EXTRA_CXXFLAGS)
CXXFLAGS += '-I$(MAKEFILE_DIR)/include'
CXXFLAGS += '-DFLOX_RESOLVER_VERSION="$(VERSION)"'
LDFLAGS ?= $(EXTRA_LDFLAGS)
lib_CXXFLAGS ?= -shared -fPIC
ifeq (Linux,$(OS))
lib_LDFLAGS ?= -shared -fPIC -Wl,--no-undefined
else
lib_LDFLAGS ?= -shared -fPIC -Wl,-undefined,error
endif
bin_CXXFLAGS ?=
bin_LDFLAGS ?=
ifneq ($(DEBUG),)
CXXFLAGS += -ggdb3 -pg
LDFLAGS += -ggdb3 -pg
endif
# ---------------------------------------------------------------------------- #
nljson_CFLAGS ?= $(shell $(PKG_CONFIG) --cflags nlohmann_json)
nljson_CFLAGS := $(nljson_CFLAGS)
argparse_CFLAGS ?= $(shell $(PKG_CONFIG) --cflags argparse)
argparse_CFLAGS := $(argparse_CFLAGS)
boost_CFLAGS ?= \
-I$(shell $(NIX) build --no-link --print-out-paths 'nixpkgs#boost')/include
boost_CFLAGS := $(boost_CFLAGS)
sqlite3_CFLAGS ?= $(shell $(PKG_CONFIG) --cflags sqlite3)
sqlite3_CFLAGS := $(sqlite3_CFLAGS)
sqlite3_LDFLAGS ?= $(shell $(PKG_CONFIG) --libs sqlite3)
sqlite3_LDLAGS := $(sqlite3_LDLAGS)
sql_builder_CFLAGS ?= \
-I$(shell $(NIX) build --no-link --print-out-paths \
'$(MAKEFILE_DIR)#sql-builder')/include
sql_builder_CFLAGS := $(sql_builder_CFLAGS)
sqlite3pp_CFLAGS ?= $(shell $(PKG_CONFIG) --cflags sqlite3pp)
sqlite3pp_CFLAGS := $(sqlite3pp_CFLAGS)
nix_INCDIR ?= $(shell $(PKG_CONFIG) --variable=includedir nix-cmd)
nix_INCDIR := $(nix_INCDIR)
ifndef nix_CFLAGS
nix_CFLAGS = $(boost_CFLAGS)
nix_CFLAGS += $(shell $(PKG_CONFIG) --cflags nix-main nix-cmd nix-expr)
nix_CFLAGS += -isystem $(nix_INCDIR) -include $(nix_INCDIR)/nix/config.h
endif
nix_CFLAGS := $(nix_CFLAGS)
ifndef nix_LDFLAGS
nix_LDFLAGS = \
$(shell $(PKG_CONFIG) --libs nix-main nix-cmd nix-expr nix-store)
nix_LDFLAGS += -lnixfetchers
endif
nix_LDFLAGS := $(nix_LDFLAGS)
ifndef floxresolve_LDFLAGS
floxresolve_LDFLAGS = '-L$(MAKEFILE_DIR)/lib' -lflox-resolve
floxresolve_LDFLAGS += -Wl,--enable-new-dtags '-Wl,-rpath,$$ORIGIN/../lib'
endif
# ---------------------------------------------------------------------------- #
lib_CXXFLAGS += $(sqlite3_CFLAGS) $(sql_builder_CFLAGS) $(sqlite3pp_CFLAGS)
bin_CXXFLAGS += $(argparse_CFLAGS)
CXXFLAGS += $(nix_CFLAGS) $(nljson_CFLAGS)
ifeq (Linux,$(OS))
lib_LDFLAGS += -Wl,--as-needed
endif
lib_LDFLAGS += $(nix_LDFLAGS) $(sqlite3_LDFLAGS)
ifeq (Linux,$(OS))
lib_LDFLAGS += -Wl,--no-as-needed
endif
bin_LDFLAGS += $(nix_LDFLAGS) $(floxresolve_LDFLAGS)
# ---------------------------------------------------------------------------- #
SEMVER_PATH ?= \
$(shell $(NIX) build --no-link --print-out-paths \
'github:aakropotkin/floco#semver')/bin/semver
CXXFLAGS += -DSEMVER_PATH='$(SEMVER_PATH)'
# ---------------------------------------------------------------------------- #
.PHONY: bin lib include tests
bin: $(addprefix bin/,$(BINS))
lib: $(addprefix lib/,$(LIBS))
include: $(COMMON_HEADERS)
tests: $(TESTS:.cc=)
# ---------------------------------------------------------------------------- #
clean: FORCE
-$(RM) $(addprefix bin/,$(BINS))
-$(RM) $(addprefix lib/,$(LIBS))
-$(RM) src/*.o tests/*.o
-$(RM) result
-$(RM) -r $(PREFIX)
-$(RM) $(addprefix docs/,*.png *.html *.svg *.css *.js)
-$(RM) -r docs/search
-$(RM) $(TESTS:.cc=)
-$(RM) gmon.out *.log
# ---------------------------------------------------------------------------- #
%.o: %.cc $(COMMON_HEADERS)
$(CXX) $(CXXFLAGS) -c "$<" -o "$@"
lib/$(LIBFLOXRESOLVE): CXXFLAGS += $(lib_CXXFLAGS)
lib/$(LIBFLOXRESOLVE): LDFLAGS += $(lib_LDFLAGS)
lib/$(LIBFLOXRESOLVE): $(lib_SRCS:.cc=.o)
$(MKDIR_P) $(@D)
$(CXX) $^ $(LDFLAGS) -o "$@"
# ---------------------------------------------------------------------------- #
bin/%: CXXFLAGS += $(bin_CXXFLAGS)
bin/%: LDFLAGS += $(bin_LDFLAGS)
$(addprefix bin/,$(BINS)): bin/%: src/main-%.o lib/$(LIBFLOXRESOLVE)
$(MKDIR_P) $(@D)
$(CXX) $(CXXFLAGS) $(LDFLAGS) "$<" -o "$@"
# ---------------------------------------------------------------------------- #
.PHONY: install-dirs install-bin install-lib install-include install
install: install-dirs install-bin install-lib install-include
install-dirs: FORCE
$(MKDIR_P) $(BINDIR) $(LIBDIR) $(INCLUDEDIR)/flox
$(INCLUDEDIR)/%: include/% | install-dirs
$(CP) -- "$<" "$@"
$(LIBDIR)/%: lib/% | install-dirs
$(CP) -- "$<" "$@"
$(BINDIR)/%: bin/% | install-dirs
$(CP) -- "$<" "$@"
install-bin: $(addprefix $(BINDIR)/,$(BINS))
install-lib: $(addprefix $(LIBDIR)/,$(LIBS))
install-include: \
$(addprefix $(INCLUDEDIR)/,$(subst include/,,$(COMMON_HEADERS)))
# ---------------------------------------------------------------------------- #
.PHONY: check
tests/%: CXXFLAGS += $(sqlite3_CFLAGS) $(nljson_CFLAGS)
tests/%: CXXFLAGS += $(nix_CFLAGS) $(nljson_CFLAGS) $(bin_CXXFLAGS)
tests/%: LDFLAGS += $(floxresolve_LDFLAGS) $(bin_LDFLAGS)
tests/%: LDFLAGS += $(sqlite3_LDFLAGS) $(nix_LDFLAGS)
$(TESTS:.cc=): %: %.o lib/$(LIBFLOXRESOLVE)
$(CXX) $(CXXFLAGS) $(LDFLAGS) "$<" -o "$@"
check: $(TESTS:.cc=)
@_ec=0; \
echo ''; \
for t in $(TESTS:.cc=); do \
echo "Testing: $$t"; \
if "./$$t"; then \
echo "PASS: $$t"; \
else \
_ec=1; \
echo "FAIL: $$t"; \
fi; \
echo ''; \
done; \
exit "$$_ec"
# ---------------------------------------------------------------------------- #
all: bin lib tests
# ---------------------------------------------------------------------------- #
.PHONY: ccls
ccls: .ccls
.ccls: FORCE
echo 'clang' > "$@";
{ \
if [[ -n "$(NIX_CC)" ]]; then \
$(CAT) "$(NIX_CC)/nix-support/libc-cflags"; \
$(CAT) "$(NIX_CC)/nix-support/libcxx-cxxflags"; \
fi; \
echo $(CXXFLAGS) $(sqlite3_CFLAGS) $(nljson_CFLAGS) $(nix_CFLAGS); \
echo $(nljson_CFLAGS) $(argparse_CFLAGS) $(sql_builder_CFLAGS); \
echo $(sqlite3pp_CFLAGS); \
}|$(TR) ' ' '\n'|$(SED) 's/-std=/%cpp -std=/' >> "$@";
# ---------------------------------------------------------------------------- #
.PHONY: docs
docs: docs/index.html
docs/index.html: FORCE
$(DOXYGEN) ./Doxyfile
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
#
#
#
# ============================================================================ #