-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
257 lines (225 loc) · 6.49 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
#===============================================================================
#
# === Available build rules/targets ===
#
# - all (default):
# $ make
# Same as debug.
#
# - debug:
# $ make debug
# Enables debug symbols (-g) asserts and
# a few other more costly runtime checks.
#
# - release:
# $ make release
# Build with optimizations turned on (-O3), no assertions,
# no debug symbols and no additional runtime checks.
#
# - static_check:
# $ make static_check
# Runs Clang static analyzer on the source code.
# This will not generate an executable or lib,
# so the linking stage will fail.
#
# - asan:
# $ make asan
# Build in debug mode and with Clang's address sanitizer.
# See: http://clang.llvm.org/docs/AddressSanitizer.html
#
# - test:
# $ make test
# Build the static lib, CLI and test applications in debug mode.
#
#
# === Output ===
#
# - lavascript executable: The Command Line Interpreter (CLI).
# - libLavaScript.a: Static library with the compiler and VM.
# - test_XYZ: Each of the test executables.
#
#
# === Other global flags ===
#
# - Setting the 'VERBOSE' variable causes the whole
# commands to be printed. If this is not set,
# a short summary is printed for each command
# instead.
#
#===============================================================================
#
# Macros / env:
#
CLI_BIN = lavascript
STATIC_LIB = libLavaScript.a
GENERATED_DIR = generated
OBJ_DIR = obj
SOURCE_DIR = source/lib
CLI_SRC = $(wildcard source/cli/*.cpp)
TEST_SRC = $(wildcard tests/cpp/*.cpp)
MKDIR_CMD = mkdir -p
AR_CMD = ar rcs
STRIP_CMD = strip
LEX_SRC = $(wildcard $(SOURCE_DIR)/*.lxx)
BISON_SRC = $(wildcard $(SOURCE_DIR)/*.yxx)
CPP_SRC = $(wildcard $(SOURCE_DIR)/*.cpp)
LEX_GENERATED = $(addprefix $(GENERATED_DIR)/, $(notdir $(patsubst %.lxx, %.cpp, $(LEX_SRC))))
BISON_GENERATED = $(addprefix $(GENERATED_DIR)/, $(notdir $(patsubst %.yxx, %.cpp, $(BISON_SRC))))
SRC_FILES = $(BISON_GENERATED) $(LEX_GENERATED) $(CPP_SRC)
OBJ_FILES = $(addprefix $(OBJ_DIR)/, $(patsubst %.cpp, %.o, $(SRC_FILES)))
TEST_OBJ_FILES = $(addprefix $(OBJ_DIR)/, $(patsubst %.cpp, %.o, $(TEST_SRC)))
#
# Define 'VERBOSE' to get the full console output.
# Otherwise print a short message for each rule.
#
ifndef VERBOSE
QUIET = @
endif # VERBOSE
#
# Additional release settings:
#
RELEASE_FLAGS = -O3 \
-DNDEBUG=1 \
-DLAVASCRIPT_DEBUG=0 \
-DLAVASCRIPT_ENABLE_ASSERT=0
#
# Additional debug settings:
#
DEBUG_FLAGS = -g \
-DDEBUG=1 \
-D_DEBUG=1 \
-D_LIBCPP_DEBUG=0 \
-D_LIBCPP_DEBUG2=0 \
-DLAVASCRIPT_DEBUG=1 \
-DLAVASCRIPT_ENABLE_ASSERT=1
#
# Paranoid warning flags:
#
WARNS_USED = -Wall \
-Wextra \
-Wsequence-point \
-Wdisabled-optimization \
-Wuninitialized \
-Wshadow \
-Wformat=2 \
-Winit-self \
-Wwrite-strings
#
# Some warnings generated by the Flex and Bison generated code that we can't fix:
#
WARNS_IGNORED = -Wno-deprecated-register \
-Wno-unused-function \
-Wno-sign-compare
#
# Additional include commands:
#
INCLUDE_DIRS = -I. \
-I/usr/local/include \
-I$(GENERATED_DIR)/ \
-I$(SOURCE_DIR)/
#
# Static analysis with Clang:
#
STATIC_CHECK_FLAGS = --analyze -Xanalyzer -analyzer-output=text
#
# Clang address sanitizer (use it with the debug target):
#
ASAN_FLAGS = -O1 -fno-omit-frame-pointer -fsanitize=address
#
# Macro define system operation:
#
MACROS = -DLAVASCRIPT_PRINT_USE_ANSI_COLOR_CODES="0"
#
# All flags combined:
#
CXXFLAGS += -std=c++1z \
$(MACROS) \
$(WARNS_USED) \
$(WARNS_IGNORED) \
$(INCLUDE_DIRS)
#================================================
# CPlusPlus rules:
#
#
# Default rule. Same as debug.
#
all: CXXFLAGS += $(DEBUG_FLAGS)
all: common_rule
@echo "Note: Built with debug settings (default)."
#
# DEBUG:
#
debug: CXXFLAGS += $(DEBUG_FLAGS)
debug: common_rule
@echo "Note: Built with debug settings."
#
# RELEASE:
#
release: CXXFLAGS += $(RELEASE_FLAGS)
release: common_rule
@echo "Note: Built with release settings."
#
# Clang static check:
#
static_check: CXXFLAGS += $(DEBUG_FLAGS) $(STATIC_CHECK_FLAGS)
static_check: common_rule
@echo "Note: Compiled for static analysis only. No code generated."
#
# Clang address sanitizer (ASan):
#
asan: CXXFLAGS += $(DEBUG_FLAGS) $(ASAN_FLAGS)
asan: common_rule
@echo "Note: Built with address sanitizer enabled and debug settings."
#
# Static lib + CLI + tests, in debug mode.
#
test: CXXFLAGS += $(DEBUG_FLAGS) $(ASAN_FLAGS)
test: common_rule $(TEST_OBJ_FILES)
$(TEST_OBJ_FILES): $(OBJ_DIR)/%.o: %.cpp
@echo "-> Building test" $< "..."
$(QUIET) $(MKDIR_CMD) $(dir $@)
$(QUIET) $(CXX) $(CXXFLAGS) -c $< -o $@
$(QUIET) $(CXX) $(CXXFLAGS) $@ -o $(addprefix test_, $(basename $(@F))) $(STATIC_LIB)
#
# Base rules shared by all the above:
#
common_rule: $(CLI_BIN)
$(QUIET) $(STRIP_CMD) $(CLI_BIN)
$(STATIC_LIB): $(OBJ_FILES)
@echo "-> Creating static library ..."
$(QUIET) $(AR_CMD) $@ $^
$(CLI_BIN): $(STATIC_LIB) $(CLI_SRC)
@echo "-> Linking executable ..."
$(QUIET) $(CXX) $(CXXFLAGS) $(CLI_SRC) -o $@ $(STATIC_LIB)
$(OBJ_FILES): $(OBJ_DIR)/%.o: %.cpp
@echo "-> Compiling" $< "..."
$(QUIET) $(MKDIR_CMD) $(dir $@)
$(QUIET) $(CXX) $(CXXFLAGS) -c $< -o $@
#================================================
# Bison rules:
#
# Preserves the generated source files
#
.PRECIOUS: $(GENERATED_DIR)/%.cpp
$(GENERATED_DIR)/%.cpp: $(SOURCE_DIR)/%.yxx
@echo "-> Running Bison for" $< "..."
$(QUIET) bison -o $@ -d $<
#================================================
# Flex rules:
#
# Preserves the generated source files
#
.PRECIOUS: $(GENERATED_DIR)/%.cpp
$(GENERATED_DIR)/%.cpp: $(SOURCE_DIR)/%.lxx
@echo "-> Running Flex for" $< "..."
$(QUIET) flex -t $< > $@
#================================================
#
# make clean:
#
clean:
@echo "-> Cleaning ..."
$(QUIET) rm -f $(CLI_BIN) $(STATIC_LIB) $(LEX_GENERATED) $(BISON_GENERATED)
$(QUIET) rm -f $(addprefix test_, $(basename $(notdir $(TEST_SRC))))
$(QUIET) rm -f $(GENERATED_DIR)/*.hh $(GENERATED_DIR)/*.hpp
$(QUIET) rm -rf $(OBJ_DIR) *.dSYM