-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMakefile
More file actions
222 lines (176 loc) · 6.46 KB
/
Copy pathMakefile
File metadata and controls
222 lines (176 loc) · 6.46 KB
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
#
# @file Makefile
# Makefile for compiler-prototype project
# @author Boris Shurygin
#
# Main targets are:
# all - build all targets in debug and release modes
# debug (not yet implemented) - build debug vesion of all targets
# release - build release version all targets
# doc - run doxygen to generate documentation from source code
#
# targets are buit in two steps:
# 1. Generate additional .dep files
# 2. Compile all .cpp to .o
# 3. Link .o files into targets
#
# Tools
export CC = gcc
export CXX = g++
export PERL = perl
export MKDIR = mkdir
export RM = rm
export GREP = grep
export TOUCH = touch
export DOXYGEN = doxygen
export LEX = flex++
#Directories
export BIN_DIR := bin
export BUILD_DIR := build
export OBJECT_DIR := $(BUILD_DIR)/objects
export SOURCES := sources
export DEBUG_OBJECTS_DIR := $(OBJECT_DIR)/debug
export RELEASE_OBJECTS_DIR := $(OBJECT_DIR)/release
export LEX_TARGET_DIR = $(BUILD_DIR)/lex_targets
#Source files excluded from build.
#Use carefully each file name matches as a substring, specify directory for better results
EXCLUDED_CPP= Interpreter/spirit-parser.cpp
#Macros for fitering, example $(call FILTER_OUT,g, seven eight nine ten)
FIND_SOURCES = $(shell find $(SOURCES) $(1) | grep -v "\#")
FILTER = $(foreach filter,$(1),$(foreach substr,$(2),$(if $(findstring $(filter),$(substr)),$(substr),)))
FILTER_OUT = $(filter-out $(call FILTER,$(1),$(2)),$(2))
#Includes
DEFAULT_INCLUDE_DIRS := -I. -Isources
RELEASE_INCLUDE_FLAGS = $(DEFAULT_INCLUDE_DIRS)
DEBUG_INCLUDE_FLAGS = $(DEFAULT_INCLUDE_DIRS)
# Final debug and release flags
DEBUG_OPT_FLAGS = -g -O0 -D_DEBUG -MMD -MP
RELEASE_OPT_FLAGS = -O3 -MMD -MP
DEBUG_CPPFLAGS = $(DEBUG_OPT_FLAGS) $(DEBUG_INCLUDE_FLAGS)
RELEASE_CPPFLAGS = $(RELEASE_OPT_FLAGS) $(RELEASE_INCLUDE_FLAGS)
# Library sets for debug and release
DEBUG_LIB_NAMES =
RELEASE_LIB_NAMES =
DEBUG_LIB_DIRS = -L/usr/lib
RELEASE_LIB_DIRS = -L/usr/lib
# Needed for unit testing targets only
BOOST_UTF_LIB = -lboost_unit_test_framework
DEBUG_LIBS = $(addprefix -l, $(DEBUG_LIB_NAMES))
DEBUG_LIB_FLAGS =
RELEASE_LIBS = $(addprefix -l, $(RELEASE_LIB_NAMES))
RELEASE_LIB_FLAGS =
# Contains all names of .lex files
LEX_FILES:=$(wildcard $(SOURCES)/**/*.lex)
# Change suffix .lex to _lex.cpp, this creates .cpp targets so flex-generated
# sources could be updated once we modify .lex files
LEX_TARGETS=$(LEX_FILES:.lex=_lex.cpp)
# Uses wildecard to create a string containing all the project headers with their relative paths
HEADERS:= $(call FIND_SOURCES,-name *.hpp -or -name *.h)
# Obtain string with all the *.cpp/*.c source files in project
SOURCES_CPP_ALL:= $(call FIND_SOURCES,-name *.cpp -or -name *.c)
SOURCES_CPP_WO_LEX:=$(call FILTER_OUT,$(LEX_TARGETS),$(SOURCES_CPP_ALL))
SOURCES_CPP:= $(call FILTER_OUT,$(EXCLUDED_CPP),$(SOURCES_CPP_WO_LEX)) $(LEX_TARGETS)
# Target directories
TARGET_DIRS:= UnitTest interpreter
DEBUG_SRC_NAMES= $(patsubst $(SOURCES)/%,$(DEBUG_OBJECTS_DIR)/%,$(SOURCES_CPP))
DEBUG_OBJS = $(DEBUG_SRC_NAMES:.cpp=.o)
DEBUG_DEPS = $(DEBUG_SRC_NAMES:.cpp=.d)
DEBUG_LIB_OBJS = $(call FILTER_OUT,$(TARGET_DIRS),$(DEBUG_OBJS))
RELEASE_SRC_NAMES= $(patsubst $(SOURCES)/%,$(RELEASE_OBJECTS_DIR)/%,$(SOURCES_CPP))
RELEASE_OBJS = $(RELEASE_SRC_NAMES:.cpp=.o)
RELEASE_DEPS = $(RELEASE_SRC_NAMES:.cpp=.d)
RELEASE_LIB_OBJS = $(call FILTER_OUT,$(TARGET_DIRS),$(RELEASE_OBJS))
# All build targets
all: debug release
# Run interpreter in debug mode
#run: interpreterd
# ./$(BIN_DIR)/interpreterd
#
# Cleanup routines
#
.PHONY: clean clean_lex clean_bin clean_objs
clean: clean_lex clean_bin clean_objs
clean_lex:
$(eval EXISTING_LEX_CPP = $(call FIND_SOURCES, -name *_lex.cpp))
-$(RM) -f $(EXISTING_LEX_CPP)
clean_bin:
-$(RM) -rf $(BIN_DIR)
clean_objs:
-$(RM) -rf $(OBJECT_DIR)
# Debug targets
debug: utestd interpreterd
utestd: gen utestd_link
interpreterd: gen interpreterd_link
# Additional generation target (if ever needed)
gen: lex
# Release targets
release: utest interpreter
utest: gen utest_link
interpreter: gen interpreter_link
#
# Linking targets for debug and release modes
#
utestd_link: $(call FILTER,UnitTest,$(DEBUG_OBJS)) $(DEBUG_LIB_OBJS)
@echo [linking] $(BIN_DIR)/utestd
@$(MKDIR) -p $(BIN_DIR)
@$(CXX) $(DEBUG_LIB_FLAGS) -o $(BIN_DIR)/utestd $(DEBUG_LIB_OBJS) $(call FILTER,UnitTest,$(DEBUG_OBJS)) $(DEBUG_LIB_DIRS) $(DEBUG_LIBS) $(BOOST_UTF_LIB)
utest_link: $(RELEASE_LIB_OBJS) $(call FILTER,UnitTest,$(RELEASE_OBJS))
@echo [linking] $(BIN_DIR)/utest
@$(MKDIR) -p $(BIN_DIR)
@$(CXX) $(RELEASE_LIB_FLAGS) -o $(BIN_DIR)/utest $(RELEASE_LIB_OBJS) $(call FILTER,UnitTest,$(RELEASE_OBJS)) $(RELEASE_LIB_DIRS) $(RELEASE_LIBS) $(BOOST_UTF_LIB)
interpreterd_link: $(call FILTER,interpreter,$(DEBUG_OBJS)) $(DEBUG_LIB_OBJS)
@echo [linking] $(BIN_DIR)/interpreterd
@$(MKDIR) -p $(BIN_DIR)
@$(CXX) $(DEBUG_LIB_FLAGS) -o $(BIN_DIR)/interpreterd $^ $(DEBUG_LIB_DIRS) $(DEBUG_LIBS) $(BOOST_UTF_LIB)
interpreter_link: $(call FILTER,interpreter,$(RELEASE_OBJS)) $(RELEASE_LIB_OBJS)
@echo [linking] $(BIN_DIR)/interpreter
@$(MKDIR) -p $(BIN_DIR)
@$(CXX) $(DEBUG_LIB_FLAGS) -o $(BIN_DIR)/interpreter $^ $(RELEASE_LIB_DIRS) $(RELEASE_LIBS) $(BOOST_UTF_LIB)
#
# Generation of cpp files with flex
#
lex: $(LEX_TARGETS)
# Rule for flex++ run
$(SOURCES)/%_lex.cpp: $(SOURCES)/%.lex
@echo [flex++] $*_lex.cpp
@$(LEX) -o $(SOURCES)/$*_lex.cpp $<
@$(TOUCH) $@
#
# Rules that run CPP compiler
#
#Dependences generation for debug mode
#$(DEBUG_OBJECTS_DIR)/%.d: $(SOURCES)/%.cpp
# @echo [generating deps] from $*.cpp to $@
# @$(MKDIR) -p $(dir $@)
# @$(CXX) -MM $(DEBUG_CPPFLAGS) $< -MF $@ -MT "$@ $(@:.d=.o)"
# @$(TOUCH) $@
-include $(DEBUG_DEPS)
#Objects generation for debug mode
$(DEBUG_OBJECTS_DIR)/%.o: $(SOURCES)/%.cpp
@echo [compiling] $*.cpp to $@
@$(MKDIR) -p $(dir $@)
@$(CXX) -c $(DEBUG_CPPFLAGS) $< -o $@
@$(TOUCH) $@
#Dependences generation for release mode
#$(RELEASE_OBJECTS_DIR)/%.d: $(SOURCES)/%.cpp
# @echo [generating deps] from $*.cpp to $@
# @$(MKDIR) -p $(dir $@)
# @$(CXX) -MM $(RELEASE_CPPFLAGS) $< -MF $@ -MT "$@ $(@:.d=.o)"
# @$(TOUCH) $@
-include $(RELEASE_DEPS)
#Objects generation for release mode
$(RELEASE_OBJECTS_DIR)/%.o: $(SOURCES)/%.cpp
@echo [compiling] $*.cpp to $@
@$(MKDIR) -p $(dir $@)
@$(CXX) -c $(RELEASE_CPPFLAGS) $< -o $@
@$(TOUCH) $@
#
# Documentation
#
doc:
@echo [doxygen]
@cd autodoc;$(DOXYGEN) Doxyfile
#$(eval EXISTING_MOCS = $(wildcard $(SOURCES)/*/*_moc.cpp))
#$(eval EXISTING_RESOURCES = $(RESOURCES:.qrc=.cpp))
#-$(RM) -f $(EXISTING_RESOURCES)
#-$(RM) -f $(EXISTING_MOCS)