forked from MoserMichael/cstuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.make
executable file
·544 lines (354 loc) · 15.1 KB
/
rules.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#--------------------------
# Setup variables and stuff
#--------------------------
ifeq "$(strip $(MODE))" ""
MODE:=debug
endif
# check if MODE is an allowed mode
VALID_CFG="debug release release2 releaseBuild install"
ifeq "$(strip $(findstring $(MODE),$(VALID_CFG)))" ""
$(error "MODE must be either $(VALID_CFG)")
endif
#-
# ROOT_DIR - where the rules.make file is (in most projects it is the top directory of the project; but this may be overriden by setting
# the root directory from the environment)
#-
ifeq "$(strip $(ROOT_DIR))" ""
ROOT_DIR=$(TOPDIR)
endif
ifeq "$(OS_TYPE)" ""
OS_TYPE:=$(shell uname -o | sed -e 's\#/\#-\#g' -e 's\#(\#-\#g' -e 's\#)\#-\#g' )
export OS_TYPE
endif
ifeq "$(OS_VERSION)" ""
OS_VERSION:=$(shell uname -r | sed -e 's\#/\#-\#g' -e 's\#(\#-\#g' -e 's\#)\#-\#g' )
export OS_VERSION
endif
ifeq "$(ARCH)" ""
ARCH=$(shell uname -m)
export ARCH
endif
#-
# Name of directory where built results are put in.
#-
PLATFORM_NAME:=$(OS_TYPE)-$(OS_VERSION)-$(ARCH)
#-
# Path of directory where built results are put in
#-
BIN_ROOT_DIR:=$(ROOT_DIR)/$(PLATFORM_NAME)
export BIN_ROOT_DIR
#
# search path for dependent path
#-
DEPENDENCY_PATH+=$(BIN_ROOT_DIR)/lib
#-
# What is the current directory ?
# $(PWD) - can't use with cygwin, as PWD we get from the calling shell.
# $(shell pwd) - don't want to run another shell
# $(CURDIR) - built in of gmake. well enough. Not all versions have it though.
#-
CURWD=$(CURDIR)
ifeq "$(CURWD)" ""
CURWD=$(shell pwd)
endif
#-
# object directory. Each target will place objects into subdirectory of this one.
#-
OBJECT_DIR=$(BIN_ROOT_DIR)/obj/$(MODE)$(CURWD)
# Uncomment in order to show generated rules.
#SHOW_RULES=1
# default installation prefix, set value if it is not defined.
INSTALL_PREFIX?=/usr/local
###
# This is an obscure exercise:
# Each target gets defined again, but in the way of doing
# so we copy its name into the OP global variable, when we have to recurse into a subdirectory, then
# the recursion rule knows, what the current target is, by looking at the OP variable, and so
# the current build target gets passed to the invoked makefile that sits in the subdirectory.
###
clean:OP:=clean
cleanall:OP:=cleanall
test:OP:=test
install:OP:=install
ctags:OP:=ctags
#-
# Include platform specific override script (file must not exist, is optional)
#-
-include $(ROOT_DIR)/rules-$(OS_TYPE).make
-include $(ROOT_DIR)/rules-$(OS_TYPE)-$(OS_VERSION).make
-include $(ROOT_DIR)/rules-$(OS_TYPE)-$(OS_VERSION)-$(ARCH).make
ifneq "$(FULL_ROOT_DIR)" ""
FULL_ROOT_DIR=$(abspath $(TOPDIR))
endif
#$(subst $FULL_ROOT_DIR,,$(abspath $(TOPDIR)))
#------------------
# Macro definitions
#------------------
# --- dependency creation ---
define make-depend
gcc -MM \
-MF $3 \
-MP \
-MT $2 \
$4 \
-I. -I$(ROOT_DIR) \
$1
endef
# --- check if directory exists, if not create it.
# --- this trick does fewer shell invocations, shell is run only when the directory does not exist - that is important for cygwin.
define make_dir_iff
$$(if $$(subst $(1),,$$(wildcard $(1))),,$$(shell mkdir -p $(1)))
endef
#---
# Show a message when starting to build a target.
#---
define display_build_banner
$(info )
$(info ****)
$(info * Target: $(1))
$(info * Type: $(2))
$(info * Mode: $(MODE))
$(info * Host: os: $(OS_TYPE) os-version: $(OS_VERSION) architecture: $(ARCH))
$(info * Directory: $(3))
$(info ****)
endef
define PROGRAM_target_setup_template
#---
# Generated common rules $(1)
#---
# - check for valid project type
ifeq "$(call $(1)_TYPE)" "shlib"
else
ifeq "$(strip $(call $(1)_TYPE))" "exe"
else
ifeq "$(strip $(call $(1)_TYPE))" "lib"
else
$$(error the value of $(1)_TYPE most be one of the following shlib , exe, lib. Current value is $(call $(1)_TYPE) )
endif
endif
endif
# - creation of subdirectory for object files (each target has its own)
.PHONY: $(1)_objdir_create
$(1)_objdir_create :
$$(eval $$(call make_dir_iff,$$(OBJECT_DIR)/$(1)))
# - define implicit rules that place targets into subdirectory per target.
$$(OBJECT_DIR)/$(1)/%.o: %.c
$$(call make-depend,$$<,$$@,$$(subst .o,.d,$$@),$$(CFLAGS) $$($(1)_CFLAGS) $$($$<_CFLAGS) $$(EXTERNAL_CFLAGS))
ifneq "$(strip $(CPP_LISTING))" ""
$$(CC) $$(PREPROCESSOR_FLAG) $$(CFLAGS) $$($(1)_CFLAGS) $$($$<_CFLAGS) $$(EXTERNAL_CFLAGS) -c -I. -I$(ROOT_DIR) $$< -o $$(addsuffix .$(1).i,$$(basename $$<))
endif
$$(CC) $$(CFLAGS) $$($(1)_CFLAGS) $$($$<_CFLAGS) $$(EXTERNAL_CFLAGS) -c $$(OUTPUT_OPTION) -I. -I$(ROOT_DIR) $$<
ifneq "$(strip $(ASM_LISTING))" ""
$$(CC) $$(ASM_LISTING_FLAG) $$(CFLAGS) $$($(1)_CFLAGS) $$($$<_CFLAGS) $$(EXTERNAL_CFLAGS) -c -I. -I$(ROOT_DIR) $$< -o $$(addsuffix .$(1).S,$$(basename $$<))
endif
$$(OBJECT_DIR)/$(1)/%.o: %.cpp
$$(call make-depend,$$<,$$@,$$(subst .o,.d,$$@),$$(CPPFLAGS) $$(CXXFLAGS) $$($(1)_CFLAGS) $$($(1)_CXXFLAGS) $$($$<_CFLAGS) $$($$<_CXXFLAGS) $$(EXTERNAL_CFLAGS) $$(EXTERNAL_CXXFLAGS) )
ifneq "$(strip $(CPP_LISTING))" ""
$$(CXX) $$(PREPROCESSOR_FLAG) $$(CPPFLAGS) $$(CXXFLAGS) $$($(1)_CXXFLAGS) $$($$<_CFLAGS) $$($$<_CXXFLAGS) $$(EXTERNAL_CFLAGS) $$(EXTERNAL_CXXFLAGS) -c -I. -I$(ROOT_DIR) $$< -o $$(addsuffix .$(1).i,$$(basename $$<))
endif
$$(CXX) $$(CPPFLAGS) $$(CXXFLAGS) $$($(1)_CFLAGS) $$($(1)_CXXFLAGS) $$($$<_CFLAGS) $$($$<_CXXFLAGS) $$(EXTERNAL_CFLAGS) $$(EXTERNAL_CXXFLAGS) -c $$(OUTPUT_OPTION) -I. -I$(ROOT_DIR) $$<
ifneq "$(strip $(ASM_LISTING))" ""
$$(CXX) $$(ASM_LISTING_FLAG) $$(CPPFLAGS) $$(CXXFLAGS) $$($(1)_CFLAGS) $$($(1)_CXXFLAGS) $$($$<_CFLAGS) $$($$<_CXXFLAGS) $$(EXTERNAL_CFLAGS) $$(EXTERNAL_CXXFLAGS) -c -I. -I$(ROOT_DIR) $$< -o $$(addsuffix .$(1).S,$$(basename $$<))
endif
# - list of source files -
ifneq "$(strip $(call $(1)_SRC))" ""
sources_$(1)=$$($(1)_SRC)
else
ifneq "$(strip $(call $(1)_SRC_EXTENSION))" ""
sources_$(1)=$$(shell ls *.$(call $(1)_SRC_EXTENSION))
else
$$(error Either list source files $(1)_SRC or specify source extension in $(1)_SRC_EXTENSION)
endif
endif
# - define prebuild target - executed before build. Prebuild step gets source file names as argument.
$(1)_pre_build :
ifneq "$$(strip $$($(1)_PREBUILD))" ""
$$($(1)_PREBUILD) $$(sources_$(1))
endif
# - define postbuild target - executed before build. Prebuild step gets following arguments: path of resulting executable, source file names
$(1)_post_build :
ifneq "$$(strip $$($(1)_POSTBUILD))" ""
$$($(1)_POSTBUILD) $$(PRJ_RESULT_FILE) $$(sources_$(1))
endif
objects_$(1):=$$(addprefix $$(BIN_ROOT_DIR)/obj/$(MODE)$$(CURWD)/$(1)/, $$(addsuffix .o, $$(basename $$(sources_$(1)) ) ) )
-include $$(addprefix $$(BIN_ROOT_DIR)/obj/$(MODE)$$(CURWD)/$(1)/, $$(addsuffix .d, $$(basename $$(sources_$(1)) ) ) )
.PHONY: banner_xxx_$(1)
banner_xxx_$(1) :
$$(eval $$(call display_build_banner,$(1),$$(PRJ_TYPE_NAME),$$(CURWD)))
# rule tha cleans this target
$(1)__clean_xxx :
rm -rf $$(OBJECT_DIR)/$(1)
rm -f $$(PRJ_RESULT_FILE)
.DEFAULT: $(1)_INSTALL
$(1)_INSTALL:
endef
# --- define shared library rules ---
define PROGRAM_shared_library_template
#---
# Generated shared library rules $(1)
#---
.PHONY: build_xxx__$(1)
build_xxx__$(1) : PRJ_NAME:=$(1)
build_xxx__$(1) : $(call add-solib-compiler-flags)
build_xxx__$(1) : $(call add-solib-compiler-flags-cpp)
build_xxx__$(1) : PRJ_TYPE_NAME:=Shared library
build_xxx__$(1) : PRJ_RESULT_FILE=$(call make_shared_lib_name,$(1),$(2))
build_xxx__$(1) : directory_setup $(1)_objdir_create banner_xxx_$(1) $(call make_shared_lib_name,$(1),$(2))
$(call make_shared_lib_name,$(1),$(2)) : $$(objects_$(1))
$$(eval $$(call display_build_banner,$(1),$$(PRJ_TYPE_NAME),$$(CURWD)))
$(CC) -shared -Wl,-soname,lib$(1).so -o $$@ $(LDFLAGS) $$(EXTERNAL_LDFLAGS) $$($(1)_LDFLAGS) $$(objects_$(1)) $$(addprefix -L,$$(DEPENDENCY_PATH)) $$(addprefix -l,$$($(1)_LIBS))
.PHONY: $(1)__clean_xxx
$(1)__clean_xxx : PRJ_RESULT_FILE=$(BIN_ROOT_DIR)/$(2)/lib$(1).so
.PHONY: $(1)__run_xxx
$(1)__run_xxx :
.PHONY: $(1)__install_target_xx
$(1)__install_target_xx : $($(1)_INSTALL)
$$(eval $$(call install-a-file,$$(call make_shared_lib_name,$(1),$(2)),$$(PREFIX)/lib))
endef
$
# --- define static library rules ---
define PROGRAM_static_library_template
#---
# Generated static library $(1)
#---
.PHONY: build_xxx__$(1)
build_xxx__$(1) : PRJ_NAME:=$(1)
build_xxx__$(1) : PRJ_TYPE_NAME:=Static library
build_xxx__$(1) : PRJ_RESULT_FILE = $(BIN_ROOT_DIR)/$(2)/lib$(1).a
build_xxx__$(1) : directory_setup $(1)_objdir_create banner_xxx_$(1) $$($(1)_DEPENDENCIES) $(1)_pre_build $$(BIN_ROOT_DIR)/$(2)/lib$(1).a $(1)_post_build
$(BIN_ROOT_DIR)/$(2)/lib$(1).a : $(objects_$(1))
$$(eval $$(call display_build_banner,$(1),$$(PRJ_TYPE_NAME),$$(CURWD)))
rm -f $(BIN_ROOT_DIR)/$(2)/lib$(1).a ; $(AR) $(ARFLAGS) $(BIN_ROOT_DIR)/$(2)/lib$(1).a $$(objects_$(1))
.PHONY: $(1)__clean_xxx
$(1)__clean_xxx : PRJ_RESULT_FILE = $(BIN_ROOT_DIR)/$(2)/lib$(1).a
.PHONY: $(1)__run_xxx
$(1)__run_xxx :
.PHONY: $(1)__install_target_xx
$(1)__install_target_xx : $($(1)_INSTALL)
$$(eval $$(call install-a-file,$$(BIN_ROOT_DIR)/$(2)/lib$(1).a,$$(PREFIX)/lib))
endef
# --- define executable library rules ---
define PROGRAM_exe_template
#---
# Generated executable rules $(1)
#---
# - rule for target -
.PHONY: build_xxx__$(1)
build_xxx__$(1) : PRJ_NAME:=$(1)
build_xxx__$(1) : PRJ_TYPE_NAME:=Executable
build_xxx__$(1) : PRJ_RESULT_FILE=$(BIN_ROOT_DIR)/bin/$(1)
build_xxx__$(1) : directory_setup $(1)_objdir_create banner_xxx_$(1) $$($(1)_DEPENDENCIES) $(1)_pre_build $(BIN_ROOT_DIR)/bin/$(1) $(1)_post_build
$(BIN_ROOT_DIR)/bin/$(1) : $$(objects_$(1))
$(CC) -o $(BIN_ROOT_DIR)/bin/$(1) $(LDFLAGS) $$($(1)_LDFLAGS) $$(EXTERNAL_LDFLAGS) $$(objects_$(1)) $$(addprefix -L,$$(DEPENDENCY_PATH)) $$(addprefix -l,$$($(1)_LIBS))
.PHONY: $(1)__clean_xxx
$(1)__clean_xxx : PRJ_RESULT_FILE=$(BIN_ROOT_DIR)/bin/$(1)
.PHONY: $(1)__run_xxx
$(1)__run_xxx :
.PHONY: $(1)__install_target_xx
$(1)__install_target_xx : $($(1)_INSTALL)
$$(eval $$(call install-a-file,$$(BIN_ROOT_DIR)/bin/$(1),$$(PREFIX)/bin))
endef
# --- define executable library rules ---
define TEST_exe_template
#---
# Generated executable rules $(1)
#---
# - rule for target -
.PHONY: build_xxx__$(1)
build_xxx__$(1) : PRJ_NAME:=$(1)
build_xxx__$(1) : PRJ_TYPE_NAME:=Executable test program
build_xxx__$(1) : DEPENDENCY_PATH+=$$(BIN_ROOT_DIR)/test
build_xxx__$(1) : PRJ_RESULT_FILE=$(BIN_ROOT_DIR)/test/$(1)
build_xxx__$(1) : $(1)_objdir_create directory_setup banner_xxx_$(1) $$($(1)_DEPENDENCIES) $(1)_pre_build $(BIN_ROOT_DIR)/test/$(1) $(1)_post_build
$(BIN_ROOT_DIR)/test/$(1) : $$(objects_$(1))
$$(CC) -o $$(BIN_ROOT_DIR)/test/$(1) $$(LDFLAGS) $$($(1)_LDFLAGS) $$(EXTERNAL_LDFLAGS) $$(objects_$(1)) $$(addprefix -L,$$(DEPENDENCY_PATH)) $$(addprefix -l,$$($(1)_LIBS))
.PHONY: $(1)__clean_xxx
$(1)__clean_xxx : PRJ_RESULT_FILE=$(BIN_ROOT_DIR)/test/$(1)
.PHONY: $(1)__run_xxx
$(1)__run_xxx :
$$(info )
$$(info *** running test $(1) ***)
$$(info )
$$(TEST_TOOL) $(BIN_ROOT_DIR)/test/$(1)
endef
#----------------------
# Top level targets
#----------------------
# -- make build target --
.PHONY: all_do
all_do: build_pre_subdirs $(addprefix build_xxx__,$(TARGETS))
.PHONY: all
all: all_do build_post_subdirs
ifneq "$(SHOW_RULES)" ""
$(foreach prog, $(TARGETS), $(info $(call PROGRAM_target_setup_template,$(prog))))
$(foreach prog, $(TARGETS), $(if $(subst shlib,,$($(prog)_TYPE)),,$(info $(call PROGRAM_shared_library_template,$(prog),lib))))
$(foreach prog, $(TARGETS), $(if $(subst lib,,$($(prog)_TYPE)),,$(info $(call PROGRAM_static_library_template,$(prog),lib))))
$(foreach prog, $(TARGETS), $(if $(subst exe,,$($(prog)_TYPE)),,$(info $(call PROGRAM_exe_template,$(prog)))))
endif
$(foreach prog, $(TARGETS), $(eval $(call PROGRAM_target_setup_template,$(prog))))
$(foreach prog, $(TARGETS), $(if $(subst shlib,,$($(prog)_TYPE)),,$(eval $(call PROGRAM_shared_library_template,$(prog),lib))))
$(foreach prog, $(TARGETS), $(if $(subst lib,,$($(prog)_TYPE)),,$(eval $(call PROGRAM_static_library_template,$(prog),lib))))
$(foreach prog, $(TARGETS), $(if $(subst exe,,$($(prog)_TYPE)),,$(eval $(call PROGRAM_exe_template,$(prog)))))
# -- make main target --
.PHONY: test_do
test_do: all_do build_pre_subdirs $(addprefix build_xxx__,$(TESTS)) $(addsuffix __run_xxx,$(TESTS))
.PHONY: test
test: test_do build_post_subdirs
ifneq "$(SHOW_RULES)" ""
$(foreach prog, $(TESTS), $(info $(call PROGRAM_target_setup_template,$(prog))))
$(foreach prog, $(TESTS), $(if $(subst shlib,,$($(prog)_TYPE)),,$(info $(call PROGRAM_shared_library_template,$(prog),test))))
$(foreach prog, $(TESTS), $(if $(subst lib,,$($(prog)_TYPE)),,$(info $(call PROGRAM_static_library_template,$(prog),test))))
$(foreach prog, $(TESTS), $(if $(subst exe,,$($(prog)_TYPE)),,$(info $(call TEST_exe_template,$(prog)))))
endif
$(foreach prog, $(TESTS), $(eval $(call PROGRAM_target_setup_template,$(prog))))
$(foreach prog, $(TESTS), $(if $(subst shlib,,$($(prog)_TYPE)),,$(eval $(call PROGRAM_shared_library_template,$(prog),test))))
$(foreach prog, $(TESTS), $(if $(subst lib,,$($(prog)_TYPE)),,$(eval $(call PROGRAM_static_library_template,$(prog),test))))
$(foreach prog, $(TESTS), $(if $(subst exe,,$($(prog)_TYPE)),,$(eval $(call TEST_exe_template,$(prog)))))
# -- make clean target --
.PHONY: clean
clean: build_pre_subdirs $(addsuffix __clean_xxx,$(TARGETS) $(TESTS)) build_post_subdirs
# -- make install target --
.PHONY: install
install: PREFIX?=$(INSTALL_PREFIX)
.PHONY: install
install: OP=install
.PHONY: install
install: test_do install_create_all_install_dirs $(addsuffix __install_target_xx,$(TARGETS)) $(addsuffix _INSTALL,$(TARGETS)) build_post_subdirs
.PHONY: install_create_all_install_dirs
install_create_all_install_dirs :
$(eval $(call install-mkdir,$(PREFIX)/bin))
$(eval $(call install-mkdir,$(PREFIX)/lib))
$(eval $(call install-mkdir,$(PREFIX)/etc))
#----------------------
# common rules
#----------------------
# --- make root directories ---
.PHONY: directory_setup
directory_setup :
$(eval $(call make_dir_iff,$(BIN_ROOT_DIR)/lib))
$(eval $(call make_dir_iff,$(BIN_ROOT_DIR)/bin))
$(eval $(call make_dir_iff,$(BIN_ROOT_DIR)/test))
# --- recurse into subdirectory (prebuild) ---
.PHONY: build_pre_subdirs
build_pre_subdirs : $(addsuffix _build_pre_subdirs_dir, $(PREBUILD_SUBDIRS))
.PHONY: %_build_pre_subdirs_dir
%_build_pre_subdirs_dir :
$(if $(wildcard $*),,$(error "Can't recurse into directory $* wrong value assigned to PREBUILD_SUBDIRS check your make file"))
@set -e; cd $* ; $(MAKE) $(OP)
.PHONY: build_post_subdirs
build_post_subdirs : $(addsuffix _build_post_subdirs_dir, $(POSTBUILD_SUBDIRS))
.PHONY: %_build_post_subdirs_dir
%_build_post_subdirs_dir :
$(if $(wildcard $*),,$(error "Can't recurse into directory $* wrong value assigned to POSTBUILD_SUBDIRS check your make file"))
@set -e; cd $* ; $(MAKE) $(OP)
.PHONY: cleanall
cleanall :
rm -rf $(BIN_ROOT_DIR)
OMNICPP_OPTS=--c++-kinds=+p --fields=+iaS --extra=+q
.PHONY: ctags
ctags : build_pre_subdirs build_post_subdirs
ifeq "$(realpath $(PWD))" "$(realpath $(ROOT_DIR))"
ctags --file-scope=no -R $(OMNICPP_OPTS) $(realpath $(ROOT_DIR))
else
ctags $(OMNICPP_OPTS) *
endif