-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
733 lines (659 loc) · 23.5 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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#=- Makefile -=#
#---------------------------------------------------------------
# Usage
#---------------------------------------------------------------
# The following generic Makefile is based on a specific project
# structure that makes the following conventions:
#
# The vanilla directory structure is in the form:
# - Root
# - include [1]
# - deps [2]
# - src [3]
# Where:
# [1] exists only for the library projects, and contains the header files
# [2] exists only for the projects with external dependencies,
# and each subfolder represents a dependency with a project
# in the form we are currently documenting
# [3] contains the source code for the project
#
# A working (built) project can also contain the following
# - Root
# - bin [5]
# - lib [6]
# - tmp [7]
# Where:
# [5] contains executable results
# [6] contains library results (static or shared)
# [7] contains intermediate object files of compiling processes
MAKEFLAGS += --no-builtin-rules
# Detect OS
ifeq ($(OS), Windows_NT)
HOST_OS := Windows
else
HOST_OS := $(shell uname -s)
endif
# CreateProcess NULL bug
ifeq ($(HOST_OS), Windows)
SHELL = cmd.exe
endif
# Set current makefile location
MKLOC ?= $(CURDIR)/$(firstword $(MAKEFILE_LIST))
export MKLOC
#---------------------------------------------------------------
# Build variable parameters
#---------------------------------------------------------------
# Variant = (Debug|Release)
VARIANT ?= Debug
TOOLCHAIN ?= GCC
SILENT ?=
VERBOSE ?=
# Default target OS is host if not provided
TARGET_OS ?= $(HOST_OS)
# Install location
LOCAL_REPO ?= $(HOME)/.local
#---------------------------------------------------------------
# Helpers
#---------------------------------------------------------------
# Recursive wildcard func
rwildcard = $(foreach d, $(wildcard $1*), $(call rwildcard, $d/, $2) $(filter $(subst *, %, $2), $d))
# Keeps only paths corresponding to files
filter-files = $(filter-out $(patsubst %/., %, $(wildcard $(addsuffix /., $(1)))), $(1))
# Suppress full command output
ifeq ($(HOST_OS), Windows)
suppress_out = > nul 2>&1
else
suppress_out = > /dev/null 2>&1
endif
# Ignore command error
ifeq ($(HOST_OS), Windows)
ignore_err = || (exit 0)
else
ignore_err = || true
endif
# Native paths
ifeq ($(HOST_OS), Windows)
native_path = $(subst /,\,$(1))
else
native_path = $(1)
endif
# Makedir command
ifeq ($(HOST_OS), Windows)
MKDIR_CMD = mkdir
else
MKDIR_CMD = mkdir -p
endif
mkdir = -$(if $(wildcard $(1)/.*), , $(MKDIR_CMD) $(call native_path, $(1)) $(suppress_out)$(ignore_err))
# Rmdir command
ifeq ($(HOST_OS), Windows)
RMDIR_CMD = rmdir /s /q
else
RMDIR_CMD = rm -rf
endif
rmdir = $(if $(wildcard $(1)/.*), $(RMDIR_CMD) $(call native_path, $(1)),)
# Copy command
ifeq ($(HOST_OS), Windows)
COPY_CMD = copy /Y
else
COPY_CMD = cp
endif
copy = $(COPY_CMD) $(call native_path, $(1)) $(call native_path, $(2)) $(suppress_out)
# Recursive folder copy command
ifeq ($(HOST_OS), Windows)
RCOPY_CMD = robocopy /S /E $(call native_path, $(1)) $(call native_path, $(2)) $(suppress_out)$(ignore_err)
else
RCOPY_CMD = cp -r $(call native_path, $(1))/* $(call native_path, $(2))
endif
rcopy = $(RCOPY_CMD)
# Path separator
pathsep = $(strip $(if $(filter $(HOST_OS), Windows), ;, :))
# Lowercase
lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,\
$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,\
$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,\
$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1))))))))))))))))))))))))))
# Quiet execution of command
quiet = $(if $(SILENT), $(suppress_out),)
# Command shown when executed
showcmd = $(if $(VERBOSE),,@)
# Newline macro
define \n
endef
# Space variable
space := $(subst ,, )
# Comma variable
comma := ,
# Canonical path (1 = base, 2 = path)
canonical_path = $(filter-out $(1), $(patsubst $(strip $(1))/%,%,$(abspath $(2))))
canonical_path_cur = $(call canonical_path, $(CURDIR), $(1))
# Gather given file from all subdirectories (1 = base, 2 = file)
findfile = $(foreach d, $(wildcard $1/*), $(filter %$(strip $2), $d))
# Gather given file from all subdirectories recursively (1 = base, 2 = file)
rfindfile = $(foreach d, $(wildcard $1/*), $(filter %$(strip $2), $d) $(call rfindfile, $d, $2))
# Gather all project configs
gatherprojs = $(strip $(patsubst ./%, %, $(patsubst %/config.mk, %, $(sort $(call rfindfile, $1, config.mk)))))
# Search for library in given paths
searchlibrary = $(foreach sd, $2, $(call findfile, $(sd), $1))
# Parse libraryname::version pairs
lib-from-extlib-pair = $(firstword $(subst ::, , $(1)))
ver-from-extlib-pair = $(strip $(or $(lastword $(subst ::, , $(1))), dev))
# Construct base path to repo dependency
extdep-pair = $(strip $(call lc,$(1)))::$(strip $(2))
extdep-path = $(LOCAL_REPO)/builds/$(subst ::,/,$(call extdep-pair, $(1), $(2)))
extdep-conf = $(call extdep-path, \
$(call lib-from-extlib-pair, $(1)), \
$(call ver-from-extlib-pair, $(1)))/pkg.mk
#---------------------------------------------------------------
# Global constants
#---------------------------------------------------------------
# Os executable extension
ifeq ($(TARGET_OS), Windows)
EXECEXT = .exe
else
EXECEXT = .out
endif
# Os dynamic library extension
ifeq ($(TARGET_OS), Windows)
DLEXT = .dll
else
DLEXT = .so
endif
# Object file extension
OBJEXT = .o
# Headers dependency file extension
HDEPEXT = .d
# Intermediate build directory
BUILDDIR := tmp
#---------------------------------------------------------------
# Header dependency generation helpers
#---------------------------------------------------------------
# Command chunks that help generate dependency files in each toolchain
sed-escape = $(subst /,\/,$(subst \,\\,$(1)))
msvc-dep-gen = $(1) /showIncludes >$(basename $@)$(HDEPEXT) & \
$(if $(strip $(2)),,sed -e "/^Note: including file:/d" $(basename $@)$(HDEPEXT) &&) \
sed -i \
-e "/: error /q1" \
-e "/^Note: including file:/!d" \
-e "s/^Note: including file:\s*\(.*\)$$/\1/" \
-e "s/\\/\//g" \
-e "s/ /\\ /g" \
-e "s/^\(.*\)$$/\t\1 \\/" \
-e "2 s/^.*$$/$(call sed-escape,$@)\: $(call sed-escape,$<) &/g" \
$(basename $@)$(HDEPEXT) || (echo. >$(basename $@)$(HDEPEXT) & exit 1)
gcc-dep-gen = -MMD -MT $@ -MF $(basename $@)$(HDEPEXT)
# Command wrapper that adds dependency generation functionality to given compile command
ifndef NO_INC_BUILD
dep-gen-wrapper = $(if $(filter $(TOOLCHAIN), MSVC), \
$(call msvc-dep-gen, $(1), $(2)), \
$(1) $(gcc-dep-gen))
else
dep-gen-wrapper = $(1)
endif
#---------------------------------------------------------------
# Colors
#---------------------------------------------------------------
ifneq ($(HOST_OS), Windows)
ESC := $(shell printf "\033")
endif
NO_COLOR := $(ESC)[0m
LGREEN_COLOR := $(ESC)[92m
LYELLOW_COLOR := $(ESC)[93m
LMAGENTA_COLOR := $(ESC)[95m
LRED_COLOR := $(ESC)[91m
DGREEN_COLOR := $(ESC)[32m
DYELLOW_COLOR := $(ESC)[33m
DCYAN_COLOR := $(ESC)[36m
#---------------------------------------------------------------
# Toolchain dependent values
#---------------------------------------------------------------
ifeq ($(TOOLCHAIN), MSVC)
# Compiler
CC := cl
CXX := cl
CFLAGS := /nologo /EHsc /W4 /c
CXXFLAGS :=
COUTFLAG := /Fo:
# Preprocessor
INCFLAG := /I
DEFINEFLAG := /D
# Archiver
AR := lib
ARFLAGS := /nologo
SLIBEXT := .lib
SLIBPREF :=
AROUTFLAG := /OUT:
# Linker
LD := link
LDFLAGS := /nologo /manifest
LIBFLAG :=
LIBSDIRFLAG := /LIBPATH:
LOUTFLAG := /OUT:
LSOFLAGS := /DLL
# Variant specific flags
ifeq ($(VARIANT), Debug)
CFLAGS += /MTd /Zi /Od /FS /Fd:$(BUILDDIR)/$(TARGETNAME).pdb
LDFLAGS += /debug
else
CFLAGS += /MT /O2
LDFLAGS += /incremental:NO
endif
else
# Compiler
CC := gcc
CXX := g++
CFLAGS := -Wall -Wextra -c
CXXFLAGS := -std=c++14
COUTFLAG := -o
CSOFLAGS := -fPIC
# Preprocessor
INCFLAG := -I
DEFINEFLAG := -D
# Archiver
AR := ar
ARFLAGS := rcs
SLIBEXT := .a
SLIBPREF := lib
AROUTFLAG :=
# Linker
LD := g++
LDFLAGS :=
ifeq ($(TARGET_OS), Windows)
LDFLAGS += -static-libgcc -static-libstdc++
endif
LIBFLAG := -l
LIBSDIRFLAG := -L
LOUTFLAG := -o
LSOFLAGS := -shared -fPIC
# Variant specific flags
ifeq ($(VARIANT), Debug)
CFLAGS += -g -O0
else
CFLAGS += -O2
endif
endif
ifdef CROSS_COMPILE
CC := $(CROSS_COMPILE)-$(CC)
CXX := $(CROSS_COMPILE)-$(CXX)
AR := $(CROSS_COMPILE)-$(AR)
LD := $(CROSS_COMPILE)-$(LD)
CFLAGS += --sysroot=$(SYSROOT)
CXXFLAGS += --sysroot=$(SYSROOT)
CPPFLAGS += --sysroot=$(SYSROOT)
LDFLAGS += --sysroot=$(SYSROOT)
endif
#---------------------------------------------------------------
# Rule generators
#---------------------------------------------------------------
# Compile rules (1 = extension, 2 = command generator, 3 = subproject path)
define compile-rule
$(BUILDDIR)/$(VARIANT)/$(strip $(3))%.$(strip $(1))$(OBJEXT): $(strip $(3))%.$(strip $(1))
@$$(info $(LGREEN_COLOR)[>] Compiling$(NO_COLOR) $(LYELLOW_COLOR)$$<$(NO_COLOR))
@$$(call mkdir, $$(@D))
$(showcmd)$$(call dep-gen-wrapper, $(2), $(SILENT)) $(quiet)
endef
#---------------------------------------------------------------
# Meta
#---------------------------------------------------------------
# Used by project generators to make $(D) and $(DP) variables available
define subproj-template-prologue
# Subproject name
$(eval D = $(strip $(1)))
# Subproject path
$(eval DP = $(if $(filter $(D),.),,$(D)/))
endef
#---------------------------------------------------------------
# Per project configuration
#---------------------------------------------------------------
# Should at least define:
# - PRJTYPE variable (Executable|StaticLib|DynLib)
# - LIBS variable (optional, Executable type only)
# Can optionally define:
# - TARGETNAME variable (project name, defaults to name of the root folder)
# - SRCDIR variable (source directory)
# - BUILDDIR variable (intermediate build directory)
# - SRC variable (list of the source files, defaults to every code file in SRCDIR)
# - SRCEXT variable (list of extensions used to match source files)
# - DEFINES variable (list defines in form of PROPERTY || PROPERTY=VALUE)
# - ADDINCS variable (list with additional include dirs)
# - MOREDEPS variable (list with additional dep dirs)
# Parses given subproject configuration to _$(subproj) postfixed variables.
# Fills in with defaults on some values if not given.
define parse-subproject-config
${subproj-template-prologue}
# Clear previous variables
$(foreach v, PRJTYPE VERSION DEFINES LIBS MOREDEPS EXTDEPS SRCDIR SRC ADDINCS ADDLIBDIR MCFLAGS MLDFLAGS, undefine $(v)${\n})
# Include configuration
-include $(DP)config.mk
# Gather variables from config
PRJTYPE_$(D) := $$(PRJTYPE)
VERSION_$(D) := $$(VERSION)
DEFINES_$(D) := $$(DEFINES)
LIBS_$(D) := $$(LIBS)
MOREDEPS_$(D) := $$(MOREDEPS)
EXTDEPS_$(D) := $$(EXTDEPS)
MCFLAGS_$(D) := $$(MCFLAGS)
MLDFLAGS_$(D) := $$(MLDFLAGS)
# Variables refering to local project paths,
# must be prefixed with subproject path
SRCDIR_$(D) := $$(addprefix $(DP), $$(SRCDIR))
SRC_$(D) := $$(addprefix $(DP), $$(SRC))
ADDINCS_$(D) := $$(addprefix $(DP), $$(ADDINCS))
ADDLIBDIR_$(D) := $$(addprefix $(DP), $$(ADDLIBDIR))
# Set defaults on unset variables
VERSION_$(D) := $$(strip $$(or $$(VERSION_$(D)), dev))
TARGETNAME_$(D) := $$(or $$(TARGETNAME_$(D)), $$(notdir $$(if $$(filter $(D),.), $$(CURDIR), $(D))))
SRCDIR_$(D) := $$(or $$(SRCDIR_$(D)), $(DP)src)
SRCEXT_$(D) := *.c *.cpp *.cc *.cxx
SRC_$(D) := $$(or $$(SRC_$(D)), $$(call rwildcard, $$(SRCDIR_$(D)), $$(SRCEXT_$(D))))
endef
#---------------------------------------------------------------
# Populated values
#---------------------------------------------------------------
#=- Populate core project target values
define populate-target-values
${subproj-template-prologue}
# Target directory
ifeq ($$(PRJTYPE_$(D)), Executable)
TARGETDIR_$(D) := $(DP)bin
else
TARGETDIR_$(D) := $(DP)lib
endif
# Output
ifeq ($$(PRJTYPE_$(D)), StaticLib)
TARGET_$(D) := $(SLIBPREF)$$(strip $$(call lc,$$(TARGETNAME_$(D))))$(SLIBEXT)
else ifeq ($$(PRJTYPE_$(D)), DynLib)
TARGET_$(D) := $(SLIBPREF)$$(strip $$(call lc,$$(TARGETNAME_$(D))))$(DLEXT)
else
TARGET_$(D) := $$(TARGETNAME_$(D))$(EXECEXT)
endif
# Master output full path
MASTEROUT_$(D) := $$(TARGETDIR_$(D))/$(VARIANT)/$$(TARGET_$(D))
# Objects
OBJ_$(D) := $$(addprefix $(BUILDDIR)/$(VARIANT)/, $$(SRC_$(D):=$(OBJEXT)))
# Header dependencies
HDEPS_$(D) := $$(OBJ_$(D):$(OBJEXT)=$(HDEPEXT))
# Install location
INSTALL_PREFIX_$(D) := $$(call extdep-path, $$(TARGETNAME_$(D)), $$(VERSION_$(D)))
# Install pair
INSTALL_PAIR_$(D) := $$(call extdep-pair, $$(TARGETNAME_$(D)), $$(VERSION_$(D)))
endef
#--------------------------------------------------
#- Read external pkg file for given dependency
define read-ext
-include $$(call extdep-conf, $(1))
PKGS += $$(PKGDEPS)
endef
#=- Populate project dependency values
define populate-dep-values
${subproj-template-prologue}
# Implicit dependencies directory
DEPSDIR_$(D) := $(DP)deps
# Implicit dependencies
DEPS_$(D) := $$(call gatherprojs, $$(DEPSDIR_$(D)))
# Explicit dependencies
DEPS_$(D) += $$(foreach md, $$(MOREDEPS_$(D)), $$(or $$(call canonical_path_cur, $(DP)/$$(md)), .))
# Append dependency pairs from local dependencies
undefine PKGS
PKGS :=
$$(foreach dep, $$(EXTDEPS_$(D)), $$(eval $$(call read-ext, $$(dep))))
EXTDEPS_$(D) += $$(PKGS)
endef
#--------------------------------------------------
#=- Populate project path values
define populate-path-values
${subproj-template-prologue}
# External (repo installed) dependency directories
EXTDEPPATHS_$(D) := $$(foreach ed, $$(EXTDEPS_$(D)), $$(call extdep-path, \
$$(call lib-from-extlib-pair, $$(ed)), \
$$(call ver-from-extlib-pair, $$(ed))))
# Include search paths
INCPATHS_$(D) := $$(strip $(DP)include \
$$(foreach dep, $$(DEPS_$(D)) \
$$(filter-out $$(DEPS_$(D)), $$(wildcard $$(DEPSDIR_$(D))/*)), \
$$(dep)/include) \
$$(ADDINCS_$(D)) \
$$(foreach extdep, $$(EXTDEPPATHS_$(D)), $$(extdep)/include))
# Library search paths
LIBPATHS_$(D) := $$(strip $$(foreach libdir,\
$$(foreach dep, $$(DEPS_$(D)), $$(dep)/lib) \
$$(ADDLIBDIR_$(D)),\
$$(libdir)/$(strip $(VARIANT))) \
$$(foreach extdep, $$(EXTDEPPATHS_$(D)), $$(extdep)/lib))
endef
#--------------------------------------------------
#=- Populate project flag values
define populate-flag-values
${subproj-template-prologue}
# Preprocessor flags
CPPFLAGS_$(D) := $$(addprefix $(DEFINEFLAG), $$(DEFINES_$(D)))
# Include path flags
INCDIR_$(D) := $$(addprefix $(INCFLAG), $$(INCPATHS_$(D)))
# Library path flags
LIBSDIR_$(D) := $$(addprefix $(LIBSDIRFLAG), $$(LIBPATHS_$(D)))
# Library flags
LIBFLAGS_$(D) := $$(strip $$(foreach lib, $$(LIBS_$(D)), $(LIBFLAG)$$(lib)$(if $(filter $(TOOLCHAIN), MSVC),.lib,)))
# Extra link flags when building shared libraries
ifeq ($$(PRJTYPE_$(D)), DynLib)
# Add shared library toggle
MORELFLAGS_$(D) := $(LSOFLAGS)
endif
# Setup rpath flag parameter for linux systems
ifeq ($$(PRJTYPE_$(D)), Executable)
ifneq ($(TARGET_OS), Windows)
# Path from executable location to project root
RELPPREFIX_$(D) := $$(subst $$(space),,$$(foreach dir, $$(subst /,$$(space),$$(dir $$(MASTEROUT_$(D)))),../))
# Library paths relative to the executable
LIBRELPATHS_$(D) := $$(addprefix $$(RELPPREFIX_$(D)), $$(LIBPATHS_$(D)))
# Add rpath param to search for dependent shared libraries relative to the executable location
MORELFLAGS_$(D) := '-Wl$$(comma)-rpath$$(comma)$$(subst $$(space),:,$$(addprefix $$$$ORIGIN/, $$(LIBRELPATHS_$(D))))'
endif
endif
endef
#--------------------------------------------------
#=- Populate project rule dependency values
define populate-rule-dep-values
${subproj-template-prologue}
# Build rule dependencies
BUILDDEPS_$(D) := $$(foreach dep, $$(DEPS_$(D)), $$(MASTEROUT_$$(dep)))
# Install dependencies for static library
ifeq ($$(PRJTYPE_$(D)), StaticLib)
INSTDEPS_$(D) := $$(addprefix install_, $$(DEPS_$(D)))
endif
endef
#--------------------------------------------------
#=- Populate project install values
# 1 = dst file 2 = src file
define copy-file-rule
$(1): $(2)
$$(info $(LGREEN_COLOR)[>] Copying$(NO_COLOR) $(LYELLOW_COLOR)$(strip $(2)) -> $$(subst \,/,$(strip $(1)))$(NO_COLOR))
$(showcmd)$$(call mkdir, $$(@D))
$(showcmd)$$(call copy, $(2), $$(@D))
INSTALL_FILES += $(1)
endef
# 1 = dst folder 2 = src folder
define copy-folder-rule
$(foreach f, $(call filter-files, $(call rwildcard, $(2), *)), \
$(call copy-file-rule, $(1)/$(call canonical_path, $(abspath $(2)), $(f)), $(f))${\n})
endef
define populate-install-values
${subproj-template-prologue}
# Reset install file list
$(eval undefine INSTALL_FILES)
ifneq ($$(PRJTYPE_$(D)), Executable)
# Copy header folder
$$(eval $$(call copy-folder-rule, $$(INSTALL_PREFIX_$(D))/include, $$(DP)include))
endif
# Copy master output folder
$$(eval $$(call copy-folder-rule, $$(INSTALL_PREFIX_$(D))/$$(lastword $$(subst /, , $$(TARGETDIR_$(D)))), $$(dir $$(MASTEROUT_$(D)))))
# Save install file list
INSTALL_FILES_$(D) := $$(INSTALL_FILES)
# Package description file
PKG_INFO_$(D) := $$(call extdep-conf, $$(INSTALL_PAIR_$(D)))
endef
#--------------------------------------------------
#=- Generate compilation database entries
# 1 = src file 2 = compilation command
define compilation-database-entry-template
{
"directory": "$(patsubst %/,%,$(dir $(MKLOC)))",
"file": "$(strip $(1))",
"command": "$(strip $(2))"
}
endef
define compile-database-entry
DB_ENTRY_$(strip $1) = $$(call compilation-database-entry-template, $(1), $(2))
DB_ENTRY_LIST += DB_ENTRY_$(strip $1)
endef
#---------------------------------------------------------------
# Rules
#---------------------------------------------------------------
# Generate rules for the given project
define gen-build-rules
${subproj-template-prologue}
# Dummy banner file for current project build
BANNERFILE_$(D) := $(BUILDDIR)/banners/banner_$(subst /,_,$(D))
# Main build rule
build_$(D): $$(MASTEROUT_$(D))
# Executes target
run_$(D): build_$(D)
@echo Executing $$(MASTEROUT_$(D)) ...
@$$(eval export PATH := $(PATH)$(pathsep)$$(subst $$(space),$(pathsep),$$(addprefix $$(CURDIR)/, $$(LIBPATHS_$(D)))))
@cd $(D) && $$(call native_path, $$(call canonical_path, $$(abspath $$(CURDIR)/$(D)), $$(MASTEROUT_$(D))))
# Pkg.mk file contents
define PCFG_$(D)
PKGDEPS := $$(strip $$(EXTDEPS_$(D)) $$(foreach dep, $$(DEPS_$(D)), $$(INSTALL_PAIR_$$(dep))))
endef
# Creates package info file
$$(PKG_INFO_$(D)): $(DP)config.mk
$$(info $(LGREEN_COLOR)[>] Pkginfo $(NO_COLOR)$(LYELLOW_COLOR)$$(@F) -> $$(@)$(NO_COLOR))
$(showcmd)echo $$(PCFG_$(D)) > $$(@)
# Installs files to repository
install_$(D): $$(INSTDEPS_$(D)) $$(INSTALL_FILES_$(D)) $$(PKG_INFO_$(D))
# Show banner for current build execution
$$(BANNERFILE_$(D)):
@$$(call mkdir, $$(@D))
$$(info $(LRED_COLOR)[o] Building$(NO_COLOR) $(LMAGENTA_COLOR)$$(TARGETNAME_$(D))$(NO_COLOR))
@echo Bum. > $$@
# Show banner if not shown
$$(OBJ_$(D)): | $$(BANNERFILE_$(D))
# Print build debug info
showvars_$(D): $$(BANNERFILE_$(D))
@echo MASTEROUT: $$(MASTEROUT_$(D))
@echo SRCDIR: $$(SRCDIR_$(D))
@echo SRC: $$(SRC_$(D))
@echo DEPS: $$(DEPS_$(D))
@echo BUILDDEPS: $$(BUILDDEPS_$(D))
@echo CFLAGS: $$(CFLAGS)
@echo CPPFLAGS: $$(CPPFLAGS_$(D))
@echo INCDIR: $$(INCDIR_$(D))
@echo LIBSDIR: $$(LIBSDIR_$(D))
@echo LIBFLAGS: $$(LIBFLAGS_$(D))
@echo HDEPS: $$(HDEPS_$(D))
@echo INSTALL: $$(INSTALL_PREFIX_$(D)): $$(INSTALL_FILES_$(D))
@echo EXTDEPS: $$(EXTDEPS_$(D))
@echo EXTPATHS: $$(EXTDEPPATHS_$(D))
# Show include search paths for current subproject
showincpaths_$(D):
$$(info $$(INCPATHS_$(D)))
# Show defines for current subproject
showdefines_$(D):
$$(info $$(DEFINES_$(D)))
# Include extra rules
-include $(DP)rules.mk
$$(MASTEROUT_$(D)): $$(BUILDDEPS_$(D)) $$(OBJ_$(D))
ifneq ($$(PRJTYPE_$(D)), StaticLib)
# Link rule
@$$(info $(DGREEN_COLOR)[+] Linking$(NO_COLOR) $(DYELLOW_COLOR)$$@$(NO_COLOR))
@$$(call mkdir, $$(@D))
$(showcmd)$(LD) $(LDFLAGS) $$(MLDFLAGS_$(D)) $$(LIBSDIR_$(D)) $(LOUTFLAG)$$@ $$(OBJ_$(D)) $$(LIBFLAGS_$(D)) $$(MORELFLAGS_$(D))
else
# Archive rule
@$$(info $(DCYAN_COLOR)[+] Archiving$(NO_COLOR) $(DYELLOW_COLOR)$$@$(NO_COLOR))
@$$(call mkdir, $$(@D))
$(showcmd)$(AR) $(ARFLAGS) $(AROUTFLAG)$$@ $$(OBJ_$(D))
endif
# Compile commands
CCOMPILE_$(D) = $(CC) $(CFLAGS) $$(MCFLAGS_$(D)) $$(CPPFLAGS_$(D)) $$(INCDIR_$(D)) $$< $(COUTFLAG) $$@ $$(MORECFLAGS_$(D))
CXXCOMPILE_$(D) = $(CXX) $(CFLAGS) $(CXXFLAGS) $$(MCFLAGS_$(D)) $$(CPPFLAGS_$(D)) $$(INCDIR_$(D)) $$< $(COUTFLAG) $$@ $$(MORECFLAGS_$(D))
# Generate compile rules
$(foreach ext, c m, $(call compile-rule, $(ext), $$(CCOMPILE_$(D)), $(DP))${\n})
$(foreach ext, cpp cxx cc mm, $(call compile-rule, $(ext), $$(CXXCOMPILE_$(D)), $(DP))${\n})
# Generate compilation database entries
$(foreach cu, $(SRC_$(D)), $(call compile-database-entry, $(cu), \
$$(subst $$@,$(BUILDDIR)/cdb/$(cu)$(OBJEXT), \
$$(subst $$<,$(cu), \
$$(if $$(filter c m, $$(patsubst .%,%,$$(suffix $(cu)))), \
$$(CCOMPILE_$(D)), \
$$(CXXCOMPILE_$(D)))))) \
${\n})
endef
#---------------------------------------------------------------
# Generation
#---------------------------------------------------------------
# Scan all directories for subprojects
SUBPROJS := $(call gatherprojs, .)
# Parse subproject configs
$(foreach subproj, $(SUBPROJS), $(eval $(call parse-subproject-config, $(subproj))))
# Generate subproject values
$(foreach generator, \
populate-target-values \
populate-dep-values \
populate-path-values \
populate-flag-values \
populate-install-values \
populate-rule-dep-values, \
$(foreach subproj, $(SUBPROJS), $(eval $(call $(generator), $(subproj)))))
# Create sublists with dependency and main projects
SILENT_SUBPROJS := $(foreach subproj, $(SUBPROJS), $(if $(findstring deps, $(subproj)), $(subproj)))
NONSILENT_SUBPROJS := $(filter-out $(SILENT_SUBPROJS), $(SUBPROJS))
# Generate subproject rules
SILENT := 1
$(foreach subproj, $(SILENT_SUBPROJS), $(eval $(call gen-build-rules, $(subproj))))
undefine SILENT
$(foreach subproj, $(NONSILENT_SUBPROJS), $(eval $(call gen-build-rules, $(subproj))))
# Aliases
build: build_.
run: run_.
install: install_.
showvars: showvars_.
showvars_all: $(addprefix showvars_, $(SUBPROJS))
# Track down Dynamic Library projects and find their dependencies
SO_PROJS := $(strip $(foreach subproj, $(SUBPROJS), $(if $(filter $(PRJTYPE_$(subproj)), DynLib), $(subproj),)))
SO_DEPS := $(strip $(sort $(foreach sop, $(SO_PROJS), $(foreach dep, $(DEPS_$(sop)), $(if $(filter $(PRJTYPE_$(dep)), StaticLib), $(dep),)))))
# Make these projects have position independent code
$(foreach pic, $(SO_PROJS) $(SO_DEPS), $(eval MORECFLAGS_$(pic) += $(CSOFLAGS)))
# Dump compilation database
compile_db:
$(info [)
$(foreach e, \
$(filter-out \
$(lastword $(DB_ENTRY_LIST)), \
$(DB_ENTRY_LIST)), \
$(info $($(e)),))
$(info $($(lastword $(DB_ENTRY_LIST))))
$(info ])
# Cleanup rule
clean:
@echo Cleaning...
@$(call rmdir, $(BUILDDIR))
# Don't create dependencies when we're cleaning
NOHDEPSGEN = clean
ifeq (0, $(words $(findstring $(MAKECMDGOALS), $(NOHDEPSGEN))))
# GNU Make attempts to (re)build the file it includes
HDEPS = $(foreach subproj, $(SUBPROJS), $(HDEPS_$(subproj)))
-include $(HDEPS)
endif
# Set default goal
.DEFAULT_GOAL := build
# Disable builtin rules
.SUFFIXES:
# Non file targets
PHONYRULETYPES := build run install showvars showincpaths showdefines
PHONYPREREQS := $(foreach ruletype, $(PHONYRULETYPES), $(addprefix $(ruletype)_, $(SUBPROJS))) \
run \
install \
showvars \
showvars_all \
compile_db \
clean
.PHONY: $(PHONYPREREQS)