-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.make
514 lines (405 loc) · 15.2 KB
/
common.make
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
#==============================================================================#
# #
# Diatom Build Framework v1.2 #
# . #
#==============================================================================#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# #
# This build framework has been designed to support quite large projects, as #
# well as a variety of languages. (C, C++, Java, as well as Lex/Yacc files.) #
# #
# For full documentation on this Makefile, see the document README.diatom in #
# this directory, or online at: http://diatom.com/build #
# #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# ------------------------------------ #
# (0) Zn specific globals #
# ------------------------------------ #
NAMESPACE := zn
C_STD := gnu99
# MULTIPROJECT := 1 not for zinc!
APPLE_LINK := -flat_namespace
# ------------------------------------ #
# (1) Supported Platforms. #
# ------------------------------------ #
APPLE := APPLE
LINUX := linux
PLATFORM := $(strip \
$(findstring $(APPLE), \
$(shell cpp -dM < /dev/null | awk '/__APPLE__/ {print $$2}' ) ) \
$(findstring $(LINUX), \
$(shell cpp -dM < /dev/null | awk '/__LINUX__/ {print $$2}' ) ) )
# ------------------------------------ #
# (2) Directory Configration #
# ------------------------------------ #
SHELL := /bin/bash
MODULE := $(shell basename `pwd`)
SRC_DIR := $(shell pwd)
TMP_PAR := $(SRC_DIR:%/$(MODULE)=%)
FAMILY := $(shell basename $(TMP_PAR))
TMP_SRC := $(TMP_PAR:%/$(FAMILY)=%)
SRC_ROOT := $(shell basename $(TMP_SRC))
ROOT_DIR := $(TMP_SRC:%/$(SRC_ROOT)=%)
BUILD_ROOT := $(ROOT_DIR)/build
BUILD_DIR := $(BUILD_ROOT)/$(MODULE)
ARCHIVE_DIR := $(ROOT_DIR)/lib/
INCLUDE_DIR := $(ROOT_DIR)/include
ifdef NAMESPACE
EXPORT_DIR := $(INCLUDE_DIR)/$(NAMESPACE)
else
EXPORT_DIR := $(INCLUDE_DIR)
endif
BIN_DIR := $(ROOT_DIR)/bin
INSTALL_BASE := /opt/bin
CLASSPATH := $(BUILD_ROOT)
ifdef JAVA_PKG
JAVA_PATH := $(subst .,/,$(JAVA_PKG))
endif
ifdef MULTIPROJECT
APP_DIR := $(BIN_DIR)/$(PROJECT)
INSTALL_DIR := $(INSTALL_BASE)/$(PROJECT)
else
APP_DIR := $(BIN_DIR)
INSTALL_DIR := $(INSTALL_BASE)
endif
# ------------------------------------ #
# (3) Application Configuration #
# ------------------------------------ #
JXX := javac -classpath $(CLASSPATH)
CC := gcc
CXX := g++
AR := ar
JAR := jar
LEX := flex
YACC := bison
# ------------------------------------ #
# (4) Target Conversion #
# ------------------------------------ #
ifdef LYOBJ
OBJECT_SOURCES := $(OBJECT_SOURCES) $(LYOBJ).tab.c $(LYOBJ).lex.c
endif
OBJECTS := $(patsubst %.cpp,%.o,$(filter %.cpp,$(OBJECT_SOURCES))) \
$(patsubst %.c,%.o,$(filter %.c,$(OBJECT_SOURCES)))
JCLASSES := $(patsubst %.java,%.class,$(filter %.java,$(OBJECT_SOURCES)))
APPS := $(patsubst %.cpp,%.cppapp,$(filter %.cpp,$(APP_SOURCES))) \
$(patsubst %.c,%.capp,$(filter %.c,$(APP_SOURCES))) \
$(patsubst %.java,%.jbin,$(filter %.java,$(APP_SOURCES)))
ifdef LYAPP
APPS := $(APPS) $(LYAPP).lyapp
endif
TEST_APPS := $(patsubst %.cpp,%.cpptapp,$(filter %.cpp,$(TEST_SOURCES))) \
$(patsubst %.c,%.ctapp,$(filter %.c,$(TEST_SOURCES)))
JTEST_APPS := $(patsubst %.java,%.jtbin,$(filter %.java,$(TEST_SOURCES)))
BUILT_OBJS := $(OBJECTS:%=$(BUILD_DIR)/%)
HEADER_TARG := $(PUBLIC_HEADERS:%.h=$(EXPORT_DIR)/%.h)
# ------------------------------------ #
# (4) Compile Mode Configuration #
# ------------------------------------ #
ifeq ($(COMPILE_MODE), lib)
ifneq ($(ONLY_SHARED), 1)
AR_TARG := $(if $(strip $(OBJECTS)),lib$(MODULE).a)
endif
ifneq ($(ONLY_ARCHIVE), 1)
SO_TARG := $(if $(strip $(OBJECTS)),lib$(MODULE).so)
endif
JAR_TARG := $(if $(strip $(JCLASSES)),$(MODULE).jar)
UNITTEST := $(MODULE)-test
ifeq ($(PLATFORM),$(APPLE))
LIB_FLAGS := -shared -undefined suppress $(APPLE_LINK)
else
LIB_FLAGS := -shared
endif
endif
ifeq ($(PLATFORM),$(APPLE))
LINK_FLAGS := $(APPLE_LINK)
endif
ifdef AR_TARG
OBJECT_ARCHIVE := $(BUILD_DIR)/$(AR_TARG)
endif
ifdef SO_TARG
SHARED_LIBRARY := $(BUILD_DIR)/$(SO_TARG)
endif
ifdef JAR_TARG
JARCHIVE := $(ARCHIVE_DIR)/$(JAR_TARG)
endif
# ------------------------------------ #
# (5) Convenience Replacements #
# ------------------------------------ #
CPPAPP = $(patsubst %.cppapp,%,$@)
CAPP = $(patsubst %.capp,%,$@)
CPPTAPP = $(patsubst %.cpptapp,%,$@)
CTAPP = $(patsubst %.ctapp,%,$@)
JAVAPP = $(patsubst %.jbin,%,$@)
JTAPP = $(patsubst %.jtbin,%,$@)
# ------------------------------------ #
# (6) Flags and other Makefile #
# ------------------------------------ #
ifndef THIRD_PARTY_INCLUDES
THIRD_PARTY_INCLUDES :=
else
THIRD_PARTY_INCLUDES := $(THIRD_PARTY_INCLUDES)
endif
ifndef THIRD_PARTY_LIBS
THIRD_PARTY_LIBS := -lpthread
else
THIRD_PARTY_LIBS := -lpthread $(THIRD_PARTY_LIBS)
endif
# Some platforms want -lpthread as a comp flag.
ifndef COMP_FLAGS
COMP_FLAGS := -W -Wall -Wshadow -Wpointer-arith -Wcast-qual $(ADDTL_FLAGS)
endif
ifndef CPP_FLAGS
CPP_FLAGS := -Wreorder -Woverloaded-virtual
endif
ifndef C_FLAGS
C_FLAGS := $(strip -fPIC -std=$(C_STD) -funsigned-char \
-Wbad-function-cast -Wmissing-prototypes )
endif
ifndef LINK_DYNAMIC
LINK_LOCAL :=
LINK_LOCAL_END :=
endif
# Debugging memory? dmalloc isn't bad...
# Use for mem debugging: -DDMALLOC
ifdef DEBUG
DBG_FLAGS := -ggdb
D_FLAGS := $(D_FLAGS) -D DEBUG
else
COMP_FLAGS := $(COMP_FLAGS) -O2
D_FLAGS := $(D_FLAGS) -D NDEBUG
endif
ifdef PARSER_PREFIX
FLEX_OPTS = -P$(PARSER_PREFIX)
YACC_OPTS = -p $(PARSER_PREFIX)
endif
# Need any additional common libraries? -lc? -lz?
STD_LIBS := -lc
AR_FLAGS := -rcs
VPATH := $(SRC_DIR):$(BUILD_DIR)
# ------------------------------------ #
# (7) Pre-assemble command lines #
# ------------------------------------ #
APP_FILES := $(APP_SOURCES) $(BUILT_OBJS)
PREP_LINE := $(D_FLAGS) -I$(INCLUDE_DIR) $(THIRD_PARTY_INCLUDES)
LINK_LINE := $(LINK_FLAGS) $(LINK_LOCAL) -L$(ARCHIVE_DIR) $(LOCAL_LIBS) \
$(EXTRA_LINK_DIRS) $(THIRD_PARTY_LIBS) $(LINK_LOCAL_END) \
$(STD_LIBS)
CC_EXEC := $(CC) $(COMP_FLAGS) $(DBG_FLAGS) $(C_FLAGS)
CXX_EXEC := $(CXX) $(COMP_FLAGS) $(DBG_FLAGS) $(CPP_FLAGS)
JXX_EXEC := $(JXX)
ifdef REQUIRE_C
TARG_EXEC := $(CC_EXEC)
else
TARG_EXEC := $(CXX_EXEC)
endif
# ------------------------------------ #
# (8) Targets #
# ------------------------------------ #
.PHONY: all quiet env libs apps scripts export unittest install clean br
all: env export libs apps scripts unittest post
quiet: env export libs apps scripts post
apps: $(OBJECTS) $(JCLASSES) $(APPS)
libs: $(OBJECT_ARCHIVE) $(SHARED_LIBRARY) $(JARCHIVE)
unittest: $(OBJECTS) $(JCLASSES) $(OBJECT_ARCHIVE) $(SHARED_LIBRARY) $(JARCHIVE) $(TEST_APPS) $(JTEST_APPS)
post:
@echo
# ------------------------------------ #
# (9) Set Up Environment #
# ------------------------------------ #
env:
@if [ ! -d $(INCLUDE_DIR) ]; then mkdir -p $(INCLUDE_DIR); fi;
@if [ ! -d $(BIN_DIR) ]; then mkdir -p $(BIN_DIR); fi;
@if [ ! -d $(BUILD_ROOT) ]; then mkdir -p $(BUILD_ROOT); fi;
@if [ ! -d $(ARCHIVE_DIR) ]; then mkdir -p $(ARCHIVE_DIR); fi;
@if [ ! -d $(BUILD_DIR) ]; then mkdir -p $(BUILD_DIR); fi;
ifdef PUBLIC_HEADERS
@if [ ! -d $(EXPORT_DIR) ]; then mkdir -p $(EXPORT_DIR); fi;
endif
ifdef APP_DIR
@if [ ! -d $(APP_DIR) ]; then mkdir -p $(APP_DIR); fi;
endif
ifdef JAVA_PATH
@if [ ! -d $(BUILD_ROOT)/$(JAVA_PATH) ]; \
then mkdir -p $(BUILD_ROOT)/$(JAVA_PATH); fi;
endif
# ------------------------------------ #
# (10) Export Headers #
# ------------------------------------ #
export: env $(PUBLIC_HEADERS)
ifdef PUBLIC_HEADERS
@echo
@echo ... EXPORTING: Public Header Files to $(EXPORT_DIR) : $(NAMESPACE) :
cp $(PUBLIC_HEADERS) $(EXPORT_DIR)
endif
# ------------------------------------ #
# (11) Installation #
# ------------------------------------ #
install:
@echo
ifdef APP_DIR
@echo ... INSTALLING APPLICATIONS AND SCRIPTS
@if [ ! -d $(INSTALL_DIR) ]; then mkdir -p $(INSTALL_DIR); fi;
cp $(APP_DIR)/* $(INSTALL_DIR)
else
@echo Nothing to Install!
endif
# ------------------------------------ #
# (12) Scripts #
# ------------------------------------ #
scripts:
ifdef SCRIPTS
@echo
@echo ... PUBLISHING SCRIPTS
cp $(SCRIPTS) $(APP_DIR)
endif
# ------------------------------------ #
# (13) Clean #
# ------------------------------------ #
clean:
@rm -f *.tab.c;
@rm -f *.tab.h;
@rm -f *.lex.c;
@rm -f *~;
@rm -f \#*;
@if [ -d $(BUILD_DIR) ]; then rm -r $(BUILD_DIR); fi;
ifdef APPS
@for h in $(APPS); do if [ -f $(APP_DIR)/$$h ]; then rm $$h; fi; done
endif
ifdef AR_TARG
@if [ -f $(ARCHIVE_DIR)/$(AR_TARG) ]; \
then rm $(ARCHIVE_DIR)/$(AR_TARG); fi;
endif
ifdef SO_TARG
@if [ -f $(ARCHIVE_DIR)/$(SO_TARG) ]; \
then rm $(ARCHIVE_DIR)/$(SO_TARG); fi;
endif
ifdef JAR_TARG
@if [ -f $(ARCHIVE_DIR)/$(JAR_TARG) ]; \
then rm $(ARCHIVE_DIR)/$(JAR_TARG); fi;
endif
ifdef JAVA_PATH
@if [ -d $(BUILD_ROOT)/$(JAVA_PATH) ]; \
then rm -r $(BUILD_ROOT)/$(JAVA_PATH); fi;
endif
ifdef HEADER_TARG
@for h in $(HEADER_TARG) ; do if [ -f $$h ]; then rm $$h; fi; done;
endif
@printf " : cleaned: %-25s :\n" $(MODULE)
# ------------------------------------ #
# (14) File Rules #
# ------------------------------------ #
## Pretty print for bash:
## @printf "\033[40m\033[1;37m%-4s\033[1;32m%-76s\033[0m\n" '...' '$@'
%.o : %.cpp %.h
@echo
@printf "%-4s %-76s" '...' '$@'
$(strip $(CXX_EXEC) -c $(PREP_LINE) $< -o $(BUILD_DIR)/$@ )
%.o : %.cpp
@echo
@printf "%-4s %-76s" '...' '$@'c
$(strip $(CXX_EXEC) -c $(PREP_LINE) $< -o $(BUILD_DIR)/$@ )
%.o : %.c %.h
@echo
@printf "%-4s %-76s" '...' '$@'
$(strip $(CC_EXEC) -c $(PREP_LINE) $< -o $(BUILD_DIR)/$@ )
%.o : %.c
@echo
@printf "%-4s %-76s" '...' '$@'
$(strip $(CC_EXEC) -c $(PREP_LINE) $< -o $(BUILD_DIR)/$@ )
%.class : %.java
@echo
@printf "%-4s %-76s" '...' '$@'
$(strip $(JXX_EXEC) $(patsubst %.class,%.java,$@) -d $(BUILD_ROOT) )
%.lex.o : %.l
@echo
@printf "%-4s %-76s" '...' '$@'
$(LEX) $(FLEX_OPTS) -o$(LYOBJ).lex.c $(LYOBJ).l
$(strip $(CC_EXEC) -c $(PREP_LINE) $(LYOBJ).lex.c -o $(BUILD_DIR)/$@ )
%.tab.o : %.y
@echo
@printf "%-4s %-76s" '...' '$@'
$(YACC) $(YACC_OPTS) -d $(LYOBJ).y
$(strip $(CC_EXEC) -c $(PREP_LINE) $(LYOBJ).tab.c -o $(BUILD_DIR)/$@ )
# ------------------------------------ #
# (15) Application & Test Rules #
# ------------------------------------ #
%.capp : %.c
@echo
@printf "%-4s%-22s%-51s\n" '...' 'BUILDING C Application' '< $(CAPP) >'
$(strip $(CC_EXEC) $(patsubst %.capp,%.c,$@) $(BUILT_OBJS) \
$(PREP_LINE) $(LINK_LINE) -o $(BUILD_DIR)/$(CAPP) )
cp $(BUILD_DIR)/$(CAPP) $(APP_DIR)/$(CAPP)
%.jbin : %.java
@echo
@printf "\033[40m\033[1;37m%-4s\033[0;35m\033[40m%-22s \033[40;37m %-51s \033[0m\n" '...' 'BUILDING Java Application' '< $(JAVAPP) >'
$(strip $(JXX_EXEC) $(patsubst %.jbin,%.java,$@) -d $(BUILD_ROOT) )
$(JAR) cf $(APP_DIR)/$(JAVAPP).jar -C $(BUILD_ROOT) $(JAVA_PATH)/$(JAVAPP).class
# touch $@
%.cppapp : %.cpp
@echo
@printf "\033[40m\033[1;37m%-4s\033[0;35m\033[40m%-22s \033[40;37m %-51s \033[0m\n" '...' 'BUILDING C++ Application' '< $(CPPAPP) >'
$(strip $(CXX_EXEC) $(patsubst %.cppapp,%.cpp,$@) $(BUILT_OBJS) \
$(PREP_LINE) $(LINK_LINE) -o \
$(BUILD_DIR)/$(patsubst %.cppapp,%,$@) )
cp $(BUILD_DIR)/$(CPPAPP) $(APP_DIR)/$(CPPAPP)
%.ctapp : %.c
@echo
@printf "\033[40m\033[1;37m%-4s\033[0;35m\033[40m%-19s \033[40;37m %-54s \033[0m\n" '...' 'BUILDING: Unit Test' '< $(CTAPP) >'
$(strip $(CC_EXEC) $(patsubst %.ctapp,%.c,$@) $(BUILT_OBJS) \
$(PREP_LINE) $(LINK_LINE) -o $(BUILD_DIR)/$(CTAPP) )
@echo
@printf "\033[40m\033[1;37m... \033[1;34m\033[40m%-76s\033[0m\n" 'EXECUTING TEST'
@$(BUILD_DIR)/$(CTAPP)
@echo
%.cpptapp : %.cpp
@echo
@printf "\033[40m\033[1;37m%-4s\033[0;35m\033[40m%-19s \033[40;37m %-54s \033[0m\n" '...' 'BUILDING: Unit Test' '< $(CPPTAPP) >'
$(strip $(CXX_EXEC) $(patsubst %.cpptapp,%.cpp,$@) $(BUILT_OBJS) \
$(PREP_LINE) $(LINK_LINE) -o $(BUILD_DIR)/$(CPPTAPP) )
@echo
@echo "... \033[1;37mEXECUTING TEST\033[0m ..."
$(BUILD_DIR)/$(CPPTAPP)
%.jtbin : %.java
@echo
@printf "\033[40m\033[1;37m%-4s\033[0;35m\033[40m%-22s \033[40;37m %-51s \033[0m\n" '...' 'BUILDING Unit Test' '< $(JTAPP) >'
$(strip $(JXX_EXEC) $(patsubst %.jtbin,%.java,$@) -d $(BUILD_ROOT) )
@echo
@echo "... \033[1;37mEXECUTING TEST\033[0m ..."
java $(BUILD_DIR)/$(JAVA_PKG)/$(JTAPP)
touch $@
# ------------------------------------ #
# (16) Library Rules #
# ------------------------------------ #
$(SHARED_LIBRARY): $(OBJECTS)
ifdef SO_TARG
@echo
@printf "%s%-66s\n" 'BUILDING:' '$(SO_TARG)'
$(strip $(TARG_EXEC) $(LIB_FLAGS) $(BUILT_OBJS) $(PREP_LINE) -o $@ )
@if [ ! -d $(ARCHIVE_DIR) ]; then mkdir -p $(ARCHIVE_DIR); fi;
@cp $(SHARED_LIBRARY) $(ARCHIVE_DIR)/$(SO_TARG)
endif
$(OBJECT_ARCHIVE): $(OBJECTS)
ifdef AR_TARG
@echo
@printf "%s%-66s\n" 'BUILDING:' '$(AR_TARG)'
@if [ ! -d $(ARCHIVE_DIR) ]; then mkdir -p $(ARCHIVE_DIR); fi;
ar $(AR_FLAGS) $(ARCHIVE_DIR)/$(AR_TARG) $(filter %.o,$(BUILT_OBJS))
endif
$(JARCHIVE): $(JCLASSES)
ifdef JAR_TARG
@echo
@printf "\033[40;1;37m... \033[0;36m\033[40m%s \033[1;36m%-66s\033[0m\n" 'BUILDING:' '$(JAR_TARG)'
@if [ ! -d $(ARCHIVE_DIR) ]; then mkdir -p $(ARCHIVE_DIR); fi;
$(JAR) cvf $(ARCHIVE_DIR)/$(JAR_TARG) $(addprefix -C $(BUILD_ROOT) $(JAVA_PATH)/,$(filter %.class,$(JCLASSES)))
endif
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# #
# Honestly, this thing is a dinosaur--and this is only the beginning! Future #
# functionality such as dependency tracking and a better install system will #
# make it even more insane! #
# #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
# . #
# ooh and wouldn't it be cool if it could identify external dependencies and #
# install them locally like maven does? that way people wouldn't have to put #
# all kinds of crazy crap on their machines just to build. i like it... #