-
Notifications
You must be signed in to change notification settings - Fork 2
/
compileAndLink.mk
94 lines (76 loc) · 3.36 KB
/
compileAndLink.mk
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
#===============================================================================
# Dependecy list processing
#===============================================================================
# Generate lists of objects and corresponding .h-files directories
depObjects = $(foreach dir, $(projectPaths), $(wildcard $(dir)/$(objDir)/*.o))
depIncludePaths = $(addsuffix /$(srcDir),$(projectPaths))
# Add the g++ flag '-I ' before every include path
includePaths=$(addprefix -I , $(depIncludePaths))
includePaths+=$(addprefix -I , $(libIncludePaths))
# Add the g++ flag '-L ' before every library path
libPaths =$(addprefix -L , $(_libPaths))
# The prefix 'lib' and suffix '.a' is removed and the prefix '-l' is be added.
# This is the g++ syntax for linking with static libraries
_libs = $(patsubst lib%.a,%, $(__libs))
libs = $(addprefix -l,$(_libs))
#===============================================================================
# Create lists for this project
#===============================================================================
# Find all cpp files and put them in a list.
sources = $(wildcard $(srcDir)/*.cpp)
# Convert suffix to .o/.d and use to list all corresponding objects files
__objects = $(sources:.cpp=.o)
__deps = $(sources:.cpp=.d)
# Remove dir from file path. File path -> file name
_objects = $(notdir ${__objects})
_deps = $(notdir ${__deps})
# Add Object/Dependency file dir to file name
objects = $(addprefix ${objDir}/, ${_objects})
deps = $(addprefix ${depDir}/, ${_deps})
#===============================================================================
# Build
#===============================================================================
# Build all dependencies
#include $(deps)
all : createDirs $(outFile)
# g++ -c -o obj/main.o src/main.cpp $(includePaths)
# g++ -o bin/main.out obj/main.o $(depObjects) $(libPaths) $(libs)
# Compile all .cpp files to .o files.
# $< = actual cpp file. $@ .o file, the target file
$(objDir)/%.o : $(srcDir)/%.cpp
g++ $(cFlags) -o $@ $< $(includePaths)
# Generate dependencies
# -MF write the generated dependency rule to a file
# -MG assume missing headers will be generated and don't stop with an error
# -MM generate dependency rule for prerequisite, skipping system headers
# -MP add phony target for each header to prevent errors when header is missing
# -MT add a target to the generated dependency
$(depDir)/%.d : $(srcDir)/%.cpp
g++ $(cFlags) -MF"$@" -MG -MM -MP -MT"$@" -MT"$(<:.c=.o)" "$<"
# Compile object files to executable
$(outFile) : $(objects) $(deps)
ifeq ($(link),true)
g++ $(objects) $(lFlags) -o $(binDir)/$(outFile) $(depObjects) $(libPaths) $(libs)
endif
createDirs :
mkdir $(masDir) $(objDir) $(binDir) $(depDir)
# Debug info if anything goes wrong
info:
@echo
@echo Dependency info:
@echo =======================================================
@echo Include Paths: $(includePaths)
@echo Library Paths: $(libPaths)
@echo Libraries: $(libs)
@echo Dependency folders: $(foreach dir, $(projectPaths), $(dir)/$(objDir))
@echo Dependencies: $(depObjects)
@echo =======================================================
@echo
@echo Compile info
@echo =======================================================
@echo Sources: $(sources)
@echo Objects: $(objects)
@echo Dependencies: $(deps)
@echo =======================================================
clean :
rm -rf $(masDir) $(binDir)