-
Notifications
You must be signed in to change notification settings - Fork 24
/
Makefile
533 lines (394 loc) · 14 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# Grand Unified Makefile
# File naming conventions:
# project/module.cpp = Part of the library
# project/module-test.cpp = Unit test for part of the library
# project/app-module.cpp = Part of the application
# If the current directory ends in -lib or -engine, the library is installable;
# otherwise, if any application modules exist, the application is installable;
# otherwise, nothing is installable.
SHELL=bash
# Project identity
LIBROOT ?= ..
project_name := $(shell ls */*.{c,h,cpp,hpp} 2>/dev/null | sed -E 's!/.*!!'| uniq -c | sort | tail -n 1 | sed -E 's!^[ 0-9]+!!')
project_tag := $(shell echo "$$PWD" | sed -E 's!^([^/]*/)*([^/]*-)?!!')
dependency_file := dependencies.make
install_prefix := /usr/local
scripts_dir = $(LIBROOT)/unicorn-lib/scripts
# Toolset identity
build_host := $(shell uname | tr A-Z a-z | sed -E 's/[^a-z].*//')
ifeq ($(TOOLS),msvc)
build_target := $(shell cl.exe 2>&1 | head -n 1 | sed -E 's/^.*Version ([0-9]+)\.([0-9]+)\.[0-9.]+ for (.+)$$/msvc-\1\2-\3/')
cross_target := msvc
else
build_target := $(shell gcc -v 2>&1 | grep '^Target:' | sed -E -e 's/^Target: //' -e 's/[.].*$$//' | tr A-Z a-z)
cross_target := $(shell echo $(build_target) | tr A-Z a-z | sed -E -e 's/-gnu$$//' -e 's/.*-//' -e 's/[^a-z].*//')
endif
# Toolset configuration
# Start with generic Unix settings, then modify based on the toolset.
# Empty comments are a makefile trick to create a variable value with a trailing space.
BUILD := build/$(build_target)
LIBTAG := $(cross_target)
CXX := g++
common_flags := -finput-charset=UTF-8 -march=ivybridge -mfpmath=sse
diagnostic_flags := -fdiagnostics-color=always -Wall -Wextra -Werror
cc_specific_flags :=
cxx_specific_flags :=
objc_specific_flags :=
cc_defines := -DNDEBUG=1
opt_release := -O2
opt_test := -O1
cc_output := -o #
include_path := -I.
AR := ar
ar_specific_flags := -rsu
ar_output :=
LDLIBS :=
ld_specific_flags :=
ld_output := -o #
library_path := -L$(BUILD)
lib_prefix := lib
lib_suffix := .a
exe_suffix :=
ifeq ($(build_host),cygwin)
exe_suffix := .exe
endif
ifeq ($(cross_target),msvc)
install_subpath := /Dropbox/Lib/msvc
install_prefix := $(HOME)$(install_subpath)
windows_prefix := $(WINDOWS_PROFILE)$(install_subpath)
vcpkg_prefix := $(VCPKG_ROOT)/installed/x64-windows
CXX := cl.exe
common_flags := /EHc /EHs /fp:precise /Gy /MD /MP /nologo /sdl /utf-8 /Y-
diagnostic_flags := /W4 /WX
cc_specific_flags :=
cxx_specific_flags := /permissive- /std:c++latest
cc_defines := /D_CRT_SECURE_NO_WARNINGS=1 /DNDEBUG=1 /DNOMINMAX=1 /DUNICODE=1 /D_UNICODE=1 /DWINVER=0x601 /D_WIN32_WINNT=0x601
opt_release := /O2
opt_test := /Od
cc_output := /Fo
include_path := /I. /I$(windows_prefix)/include /I$(vcpkg_prefix)/include
AR := lib.exe
ar_specific_flags := /NOLOGO
ar_output := /OUT:
LD := link.exe
LDLIBS :=
ld_specific_flags := /CGTHREADS:4 /MACHINE:X64 /NOLOGO /NXCOMPAT /SUBSYSTEM:CONSOLE
ld_output := /OUT:
library_path := /LIBPATH:$(BUILD) /LIBPATH:$(windows_prefix)/lib /LIBPATH:$(vcpkg_prefix)/lib
lib_prefix :=
lib_suffix := .lib
RC := rc.exe
RCFLAGS :=
rc_output := /fo
endif
ifeq ($(cross_target),cygwin)
cc_defines += -D_REENTRANT=1 -D_XOPEN_SOURCE=700
cxx_specific_flags += -std=gnu++2a
ld_specific_flags += -Wl,--enable-auto-import
endif
ifeq ($(cross_target),darwin)
LIBTAG := apple
CXX := clang++
cc_defines += -D_DARWIN_C_SOURCE=1 -D_REENTRANT=1 -D_XOPEN_SOURCE=700
cxx_specific_flags += -std=c++2a -stdlib=libc++
ld_specific_flags += -framework Cocoa
endif
ifeq ($(cross_target),linux)
cc_defines += -D_REENTRANT=1 -D_XOPEN_SOURCE=700
cxx_specific_flags += -std=gnu++2a
endif
ifneq ($(shell grep -Fo sdl $(dependency_file)),)
cc_defines += -DSDL_MAIN_HANDLED=1
endif
# Collect the build tools and their options
CC := $(CXX)
OBJC := $(CXX)
OBJCXX := $(CXX)
CFLAGS += $(common_flags) $(diagnostic_flags) $(include_path) $(cc_specific_flags) $(cc_defines)
CXXFLAGS += $(common_flags) $(diagnostic_flags) $(include_path) $(cxx_specific_flags) $(cc_defines)
OBJCFLAGS += $(common_flags) $(diagnostic_flags) $(include_path) $(objc_specific_flags) $(cc_defines)
OBJCXXFLAGS += $(common_flags) $(diagnostic_flags) $(include_path) $(objc_specific_flags) $(cxx_specific_flags) $(cc_defines)
ifeq ($(cross_target),msvc)
ARFLAGS := $(ar_specific_flags) $(library_path)
LDFLAGS += $(ld_specific_flags) $(library_path)
else
ARFLAGS := $(ar_specific_flags)
LD := $(CXX)
LDFLAGS += $(common_flags) $(diagnostic_flags) $(cc_defines) $(ld_specific_flags) $(library_path)
endif
# File names
all_headers := $(shell find $(project_name) -name *.hpp)
library_headers := $(filter-out $(project_name)/library.hpp Makefile,$(shell grep -EL '// NOT INSTALLED' $(all_headers) Makefile)) # Dummy entry to avoid empty list
all_sources := $(shell find $(project_name) -name *.c -or -name *.cpp -or -name *.m -or -name *.mm)
app_sources := $(shell echo "$(all_sources)" | tr ' ' '\n' | grep -E '(^|/)app-')
test_sources := $(shell echo "$(all_sources)" | tr ' ' '\n' | grep -E '[-]test\.')
library_sources := $(filter-out $(app_sources) $(test_sources),$(all_sources))
ifneq ($(test_sources),)
test_sources := $(sort $(test_sources) $(project_name)/unit-test.cpp)
endif
app_objects := $(shell sed -E 's!$(project_name)/([^ ]+)\.[a-z]+!$(BUILD)/\1.o!g' <<< '$(app_sources)')
test_objects := $(shell sed -E 's!$(project_name)/([^ ]+)\.[a-z]+!$(BUILD)/\1.o!g' <<< '$(test_sources)')
library_objects := $(shell sed -E 's!$(project_name)/([^ ]+)\.[a-z]+!$(BUILD)/\1.o!g' <<< '$(library_sources)')
doc_index := $(wildcard $(project_name)/index.md)
doc_sources := $(shell find $(project_name) -name '*.md' | sort)
doc_pages := doc/style.css doc/index.html $(patsubst $(project_name)/%.md,doc/%.html,$(doc_sources))
static_name := $(lib_prefix)$(project_name)$(lib_suffix)
static_library := $(BUILD)/$(static_name)
ifeq ($(cross_target),msvc)
resource_files := $(wildcard resources/*.rc) $(wildcard resources/*.ico)
resource_object := $(patsubst resources/%.rc,$(BUILD)/%.o,$(firstword $(resource_files)))
app_objects += $(resource_object)
endif
# Work out which kind of installable target we want
ifeq ($(project_tag),lib)
ifeq ($(library_sources),)
install_type := header-lib
else
install_type := object-lib
endif
else ifeq ($(project_tag),engine)
install_type := object-lib
else ifneq ($(app_sources),)
install_type := application
else
install_type := nothing
endif
app_name := $(project_name)$(exe_suffix)
app_target := $(BUILD)/$(app_name)
test_name := test-$(project_name)$(exe_suffix)
test_target := $(BUILD)/$(test_name)
static_target :=
ifneq ($(library_sources),)
static_target := $(static_library)
endif
install_app :=
install_include :=
install_static :=
ifneq ($(install_prefix),)
install_app := $(install_prefix)/bin/$(app_name)
install_include := $(install_prefix)/include/$(project_name)
ifneq ($(static_library),)
install_static := $(install_prefix)/lib/$(static_name)
endif
endif
# Common build targets
.DELETE_ON_ERROR:
.PHONY: all static app run tests check check-% install symlinks uninstall doc unlink undoc clean clean-all dep
.PHONY: help help-prefix help-app help-test help-install help-suffix print-%
all: static app tests doc
unlink:
rm -f $(static_library) $(app_target) $(test_target)
undoc:
rm -f doc/*.html
clean:
rm -rf $(BUILD) *.stackdump __test_*
clean-all:
rm -rf build doc *.stackdump __test_*
dep:
$(scripts_dir)/make-dependencies
help: help-suffix
help-prefix:
@echo
@echo "Make targets:"
@echo
@echo " all = Build everything (default)"
@echo " static = Build the static library"
help-suffix: help-install
@echo " doc = Build the documentation"
@echo " unlink = Delete link targets but not object modules"
@echo " undoc = Delete the documentation"
@echo " clean = Delete all targets for this toolset"
@echo " clean-all = Delete all targets for all toolsets and docs"
@echo " dep = Rebuild makefile dependencies"
@echo " help = List make targets"
@echo " print-* = Query a makefile variable"
@echo
print-%:
@echo "$* = $($*)"
# Targets that depend on whether we're building an app
ifneq ($(app_sources),)
app: $(app_target)
run: app
@rm -f *.stackdump
$(app_target)
help-app: help-prefix
@echo " app = Build the application"
@echo " run = Build and run the application"
else
app:
run:
help-app: help-prefix
endif
# Targets that depend on whether we have any unit tests
ifneq ($(test_sources),)
tests: $(test_target)
check: all
@rm -rf __test_* *.stackdump
$(test_target)
check-%: all
@rm -rf __test_* *.stackdump
UNIT='$*' $(test_target)
help-test: help-app
@echo " tests = Build the unit tests"
@echo " check = Build everything and run the unit tests"
@echo " check-* = Build everything and run specific unit tests"
else
tests:
check:
check-%:
help-test: help-app
endif
# Targets that depend on whether we have any documentation
ifneq ($(doc_sources),)
doc: $(doc_pages)
else
doc:
endif
# Targets specific to building an application
ifeq ($(install_type),application)
static: $(static_target)
install: uninstall app
@mkdir -p $(dir $(install_app))
cp $(app_target) $(install_app)
uninstall:
if [ -f "$(install_app)" ]; then rm $(install_app); fi
help-install: help-test
@echo " install = Install the library"
@echo " symlinks = Install the library using symlinks"
@echo " uninstall = Uninstall the library"
ifeq ($(cross_target),msvc)
symlinks:
$(error make symlinks is not supported on $(cross_target), use make install)
else
symlinks: uninstall app
@mkdir -p $(dir $(install_app))
ln -s $(abspath $(app_target)) $(install_app)
endif
endif
# Targets specific to building a header-only library
ifeq ($(install_type),header-lib)
static: $(static_target) $(project_name)/library.hpp
install: uninstall static
@mkdir -p $(install_include)
cp $(library_headers) $(project_name)/library.hpp $(install_include)
uninstall:
if [ -h "$(install_include)" ]; then rm $(install_include); fi
if [ -d "$(install_include)" ]; then rm -rf $(install_include); fi
help-install: help-test
@echo " install = Install the application"
@echo " symlinks = Install the application using symlinks"
@echo " uninstall = Uninstall the application"
ifeq ($(cross_target),msvc)
symlinks:
$(error make symlinks is not supported on $(cross_target), use make install)
else
symlinks: uninstall static
@mkdir -p $(dir $(install_include))
ln -s $(abspath $(project_name)) $(install_include)
endif
endif
# Targets specific to building a non-header library
ifeq ($(install_type),object-lib)
static: $(static_target) $(project_name)/library.hpp
install: uninstall static
@mkdir -p $(install_include) $(dir $(install_static))
cp $(library_headers) $(project_name)/library.hpp $(install_include)
cp $(static_library) $(install_static)
uninstall:
if [ -h "$(install_include)" ]; then rm $(install_include); fi
if [ -d "$(install_include)" ]; then rm -rf $(install_include); fi
if [ -f "$(install_static)" ] || [ -h "$(install_static)" ]; then rm $(install_static); fi
help-install: help-test
@echo " install = Install the application"
@echo " symlinks = Install the application using symlinks"
@echo " uninstall = Uninstall the application"
ifeq ($(cross_target),msvc)
symlinks:
$(error make symlinks is not supported on $(cross_target), use make install)
else
symlinks: uninstall static
@mkdir -p $(dir $(install_include)) $(dir $(install_static))
ln -s $(abspath $(project_name)) $(install_include)
ln -s $(abspath $(static_library)) $(install_static)
endif
endif
# Fallback targets when none of the above apply
ifeq ($(install_type),nothing)
static: $(static_target)
install:
symlinks:
uninstall:
help-install: help-test
endif
# Include the dependency list
-include $(dependency_file)
# System specific link libraries
ifneq ($(cross_target),msvc)
LDLIBS += -lpthread -lz
ifneq ($(cross_target),linux)
LDLIBS += -liconv
endif
endif
# Rules for building objects from source
$(BUILD)/%-test.o: $(project_name)/%-test.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(opt_test) -c $< $(cc_output)$@
$(BUILD)/%-test.o: $(project_name)/%-test.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(opt_test) -c $< $(cc_output)$@
$(BUILD)/%-test.o: $(project_name)/%-test.m
@mkdir -p $(dir $@)
$(OBJC) $(OBJCFLAGS) $(opt_test) -c $< $(cc_output)$@
$(BUILD)/%-test.o: $(project_name)/%-test.mm
@mkdir -p $(dir $@)
$(OBJCXX) $(OBJCXXFLAGS) $(opt_test) -c $< $(cc_output)$@
$(BUILD)/%.o: $(project_name)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) $(opt_release) -c $< $(cc_output)$@
$(BUILD)/%.o: $(project_name)/%.cpp
@mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) $(opt_release) -c $< $(cc_output)$@
$(BUILD)/%.o: $(project_name)/%.m
@mkdir -p $(dir $@)
$(OBJC) $(OBJCFLAGS) $(opt_release) -c $< $(cc_output)$@
$(BUILD)/%.o: $(project_name)/%.mm
@mkdir -p $(dir $@)
$(OBJCXX) $(OBJCXXFLAGS) $(opt_release) -c $< $(cc_output)$@
ifneq ($(resource_object),)
$(resource_object): $(resource_files)
@mkdir -p $(dir $@)
$(RC) $(RCFLAGS) $(rc_output)$@ $<
endif
# Rules for building the final target from objects
$(static_library): $(library_objects)
@mkdir -p $(dir $@)
@rm -f $@
$(AR) $(ARFLAGS) $(ar_output)$@ $^
$(app_target): $(app_objects) $(static_target)
@mkdir -p $(dir $@)
@rm -f $@
$(LD) $(LDFLAGS) $(opt_release) $^ $(LDLIBS) $(ld_output)$@
$(test_target): $(test_objects) $(static_target)
@mkdir -p $(dir $@)
@rm -f $@
$(LD) $(LDFLAGS) $(opt_test) $^ $(LDLIBS) $(ld_output)$@
# Other build rules
$(project_name)/library.hpp: $(library_headers)
echo "#pragma once" > $@
echo >> $@
echo $(sort $(library_headers)) | tr ' ' '\n' | sed -E 's/.+/#include "&"/' >> $@
$(project_name)/unit-test.cpp: $(filter-out $(project_name)/unit-test.cpp,$(test_sources))
$(scripts_dir)/make-tests
ifeq ($(doc_index),)
doc/index.html: $(doc_sources) $(scripts_dir)/make-doc $(scripts_dir)/make-index
@mkdir -p $(dir $@)
$(scripts_dir)/make-index > __index__.md
$(scripts_dir)/make-doc __index__.md $@
rm -f __index__.md
endif
doc/%.html: $(project_name)/%.md $(scripts_dir)/make-doc
@mkdir -p $(dir $@)
$(scripts_dir)/make-doc $< $@
doc/style.css: $(scripts_dir)/style.css
@mkdir -p $(dir $@)
cp $< $@