-
Notifications
You must be signed in to change notification settings - Fork 41
/
Makefile
426 lines (352 loc) · 13.3 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
ROOT := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
export K_BIN ?= $(ROOT)/.build/k/k-distribution/target/release/k/bin
export KOMPILE := $(K_BIN)/kompile
export KDEP := $(K_BIN)/kdep
# Appending to whatever the environment provided.
K_OPTS += -Xmx8g
K_OPTS += -Xss32m
export K_OPTS
PROFILE_ROOT := $(ROOT)/profiles
PROFILE_DIR := $(PROFILE_ROOT)/x86-gcc-limited-libc
PROFILE := $(shell basename $(PROFILE_DIR))
SUBPROFILE_DIRS :=
# Default build directory.
# Intended for overriding by the user, see below.
BUILD_DIR := dist
# Build directory used internally.
# `abspath` because the directory does not exist yet.
OUTPUT_DIR := $(abspath $(BUILD_DIR))
PROFILE_OUTPUT_DIR := $(OUTPUT_DIR)/profiles/$(PROFILE)
KCCFLAGS := -D_POSIX_C_SOURCE=200809 -nodefaultlibs -fno-native-compilation
# Appending to whatever the environment provided.
CFLAGS += -std=gnu11
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -Werror
CFLAGS += -pedantic
# Appending to whatever the environment provided.
# Do not export: `clang-tools` sets these things via `cmake`.
CXXFLAGS += -std=c++17
CXXFLAGS += -Wall
CXXFLAGS += -Wextra
CXXFLAGS += -Werror
CXXFLAGS += -pedantic
export LOGGER := $(ROOT)/scripts/build-logger.sh
CLANG_TOOLS_BUILD_DIR := $(OUTPUT_DIR)/clang-tools
CLANG_TOOLS_BIN := $(CLANG_TOOLS_BUILD_DIR)/bin
PARSER_BUILD_DIR := $(OUTPUT_DIR)/parser
PARSER_BIN := $(PARSER_BUILD_DIR)
CDECL_BUILD_DIR := $(OUTPUT_DIR)/cdecl-3.6
CDECL_BIN := $(CDECL_BUILD_DIR)/src
FILES_TO_DIST := \
scripts/kcc \
scripts/k++ \
scripts/kranlib \
scripts/merge-kcc-obj \
scripts/split-kcc-obj \
scripts/make-trampolines \
scripts/make-symbols \
scripts/gccsymdump \
scripts/globalize-syms \
scripts/ignored-flags \
scripts/program-runner \
scripts/histogram-csv \
$(PARSER_BIN)/cparser \
$(realpath $(K_BIN)/..) \
$(CLANG_TOOLS_BIN)/clang-kast \
$(CLANG_TOOLS_BIN)/call-sites \
$(CDECL_BIN)/cdecl \
LICENSE \
licenses
PROFILE_FILES := include src compiler-src native pp cpp-pp cc cxx
PROFILE_FILE_DEPS := $(foreach f, $(PROFILE_FILES), $(PROFILE_DIR)/$(f))
SUBPROFILE_FILE_DEPS := $(foreach d, $(SUBPROFILE_DIRS), \
$(foreach f, $(PROFILE_FILES), $(d)/$(f)))
PERL_MODULES := $(wildcard $(ROOT)/scripts/RV_Kcc/*.pm)
LIBC_SO := $(PROFILE_OUTPUT_DIR)/lib/libc.so
LIBSTDCXX_SO := $(PROFILE_OUTPUT_DIR)/lib/libstdc++.so
define timestamp_of
$(PROFILE_OUTPUT_DIR)/$(1)-kompiled/$(1)-kompiled/timestamp
endef
.PHONY: default
default: test-build
# Targets intended for direct invocation by the user,
# to check the installation of dependencies.
#
# No subsequent targets depend on these.
.PHONY: check-deps
check-deps: | check-ocaml check-cc check-cxx check-perl check-k
.PHONY: check-ocaml
check-ocaml:
@ocaml -version > /dev/null 2>&1 || { \
$(LOGGER) "ERROR: Missing OCaml installation. Please see INSTALL.md for more information." \
&& false; \
}
.PHONY: check-cc
check-cc:
@$(CC) -v > /dev/null 2>&1 || { \
$(LOGGER) "ERROR: Missing C compiler installation. Please see INSTALL.md for more information." \
&& false; \
}
.PHONY: check-cxx
check-cxx:
@$(CXX) -v > /dev/null 2>&1 || { \
$(LOGGER) "ERROR: Missing C++ compiler installation. Please see INSTALL.md for more information." \
&& false; \
}
.PHONY: check-perl
check-perl:
@export STR="$$(perl scripts/checkForModules.pl)" && test ! -z "$${STR}" && $(LOGGER) "$${STR}"
.PHONY: check-k
check-k:
@$(KOMPILE) --version > /dev/null 2>&1 || { \
$(LOGGER) "ERROR: Missing K installation. Please see INSTALL.md for more information." \
&& false; \
}
# Real targets.
# We rely on this rule to make
# nonexisting directories.
.DEFAULT: ;@mkdir -p $@
$(OUTPUT_DIR)/writelong: scripts/writelong.c | $(OUTPUT_DIR)
@$(LOGGER) "Building $@"
@$(CC) $(CFLAGS) $< -o $@
$(OUTPUT_DIR)/extract-references: scripts/extract-references.cpp | $(OUTPUT_DIR)
@$(LOGGER) "Building $@"
@$(CXX) $(CXXFLAGS) $< -lstdc++fs -o $@
$(OUTPUT_DIR)/kcc: scripts/getopt.pl \
$(PERL_MODULES) \
$(OUTPUT_DIR)/writelong \
$(FILES_TO_DIST) \
| $(OUTPUT_DIR)/RV_Kcc
cp -RLp $(FILES_TO_DIST) $(OUTPUT_DIR)
cp -RLp $(PERL_MODULES) $(OUTPUT_DIR)/RV_Kcc
cat scripts/RV_Kcc/Opts.pm | perl scripts/getopt.pl > $(OUTPUT_DIR)/RV_Kcc/Opts.pm
cp -Lp $(OUTPUT_DIR)/kcc $(OUTPUT_DIR)/kclang
.PHONY: pack
pack: FATPACK_COMMAND := PERL5LIB="$(OUTPUT_DIR):$(PERL5LIB)" fatpack
pack: $(OUTPUT_DIR)/kcc
@$(LOGGER) "Entering target $@"
cd $(OUTPUT_DIR) \
&& $(FATPACK_COMMAND) trace kcc
cd $(OUTPUT_DIR) \
&& $(FATPACK_COMMAND) packlists-for `cat fatpacker.trace` > packlists
$(LOGGER) "$$(cat $(OUTPUT_DIR)/packlists)"
cd $(OUTPUT_DIR) \
&& $(FATPACK_COMMAND) tree `cat packlists`
cp -RLp $(OUTPUT_DIR)/RV_Kcc $(OUTPUT_DIR)/fatlib/RV_Kcc
cd $(OUTPUT_DIR) \
&& $(FATPACK_COMMAND) file kcc > kcc.packed
chmod --reference=$(OUTPUT_DIR)/kcc $(OUTPUT_DIR)/kcc.packed
mv -f $(OUTPUT_DIR)/kcc.packed $(OUTPUT_DIR)/kcc
cp -Lp $(OUTPUT_DIR)/kcc $(OUTPUT_DIR)/kclang
rm -rf \
$(OUTPUT_DIR)/fatlib \
$(OUTPUT_DIR)/RV_Kcc \
$(OUTPUT_DIR)/packlists \
$(OUTPUT_DIR)/fatpacker.trace
.PHONY: PROFILE_DEPS
PROFILE_DEPS: $(OUTPUT_DIR)/kcc \
$(PROFILE_FILE_DEPS) \
$(SUBPROFILE_FILE_DEPS) \
$(PROFILE)-native \
| $(PROFILE_OUTPUT_DIR)/lib
@printf "%s" $(PROFILE) > $(OUTPUT_DIR)/current-profile
@printf "%s" $(PROFILE) > $(OUTPUT_DIR)/default-profile
@-$(foreach f, $(PROFILE_FILE_DEPS), \
cp -RLp $(f) $(PROFILE_OUTPUT_DIR);)
@true $(foreach d, $(SUBPROFILE_DIRS), \
&& mkdir -p $(OUTPUT_DIR)/profiles/$(shell basename $(d))/lib)
@-$(foreach d, $(SUBPROFILE_DIRS), \
$(foreach f, $(PROFILE_FILES), \
cp -RLp $(d)/$(f) \
$(OUTPUT_DIR)/profiles/$(shell basename $(d))/$(f);))
@-$(foreach d, $(SUBPROFILE_DIRS), \
cp -RLp $(PROFILE_OUTPUT_DIR)/native/* \
$(OUTPUT_DIR)/profiles/$(shell basename $(d))/native;)
$(LIBSTDCXX_SO): $(call timestamp_of,c-cpp-linking) \
$(call timestamp_of,cpp-translation) \
$(wildcard $(PROFILE_DIR)/compiler-src/*.C) \
$(foreach d,$(SUBPROFILE_DIRS),$(wildcard $(d)/compiler-src/*)) \
$(OUTPUT_DIR)/hardware-addresses.timestamp \
PROFILE_DEPS
@$(LOGGER) "$(PROFILE): Translating the C++ standard library..."
@cd $(PROFILE_DIR)/compiler-src && \
$(OUTPUT_DIR)/kcc \
-Wfatal-errors \
--use-profile $(PROFILE) \
-shared -o $@ *.C \
$(KCCFLAGS) -I .
@true $(foreach d,$(SUBPROFILE_DIRS), \
&& cd $(d)/compiler-src && \
$(OUTPUT_DIR)/kcc \
-Wfatal-errors \
--use-profile $(shell basename $(d)) \
-shared \
-o $(OUTPUT_DIR)/profiles/$(shell basename $(d))/lib/libstdc++.so \
*.C $(KCCFLAGS) -I .)
@$(LOGGER) "$(PROFILE): Done translating the C++ standard library."
$(LIBC_SO): $(call timestamp_of,c-cpp-linking) \
$(call timestamp_of,c-translation) \
$(wildcard $(PROFILE_DIR)/src/*.c) \
$(foreach d,$(SUBPROFILE_DIRS),$(wildcard $(d)/src/*.c)) \
$(OUTPUT_DIR)/hardware-addresses.timestamp \
PROFILE_DEPS
@$(LOGGER) "$(PROFILE): Translating the C standard library..."
@cd $(PROFILE_DIR)/src && \
$(OUTPUT_DIR)/kcc \
-Wfatal-errors \
--use-profile $(PROFILE) \
-shared -o $@ *.c \
$(KCCFLAGS) -I .
@true $(foreach d,$(SUBPROFILE_DIRS), \
&& cd $(d)/src && \
$(OUTPUT_DIR)/kcc \
-Wfatal-errors \
--use-profile $(shell basename $(d)) \
-shared \
-o $(OUTPUT_DIR)/profiles/$(shell basename $(d))/lib/libc.so \
*.c $(KCCFLAGS) -I .)
@$(LOGGER) "$(PROFILE): Done translating the C standard library."
$(OUTPUT_DIR)/hardware-addresses.timestamp: $(PROFILE_ROOT)/*-hwa $(PROFILE_ROOT)/no-hwa
@printf "no-hwa" > $(OUTPUT_DIR)/default-hardware-addresses
@printf "no-hwa" > $(OUTPUT_DIR)/current-hardware-addresses
@mkdir -p $(OUTPUT_DIR)/hardware-addresses
@cp -Lp $? $(OUTPUT_DIR)/hardware-addresses
@touch $@
.PHONY: $(PROFILE)-native
$(PROFILE)-native: CC := $(PROFILE_DIR)/cc
$(PROFILE)-native: CXX := $(PROFILE_DIR)/cxx
$(PROFILE)-native: $(PROFILE_OUTPUT_DIR)/native/main.o \
$(PROFILE_OUTPUT_DIR)/native/server.c \
$(PROFILE_OUTPUT_DIR)/native/builtins.o \
$(PROFILE_OUTPUT_DIR)/native/platform.o \
$(PROFILE_OUTPUT_DIR)/native/platform.h \
$(PROFILE_OUTPUT_DIR)/native/server.h
$(PROFILE_OUTPUT_DIR)/native/main.o: native-server/main.c \
native-server/server.h \
| $(PROFILE_OUTPUT_DIR)/native
$(CC) $(CFLAGS) -c $< -o $@ -g
$(PROFILE_OUTPUT_DIR)/native/%.h: native-server/%.h \
| $(PROFILE_OUTPUT_DIR)/native
cp -RLp $< $@
$(PROFILE_OUTPUT_DIR)/native/server.c: native-server/server.c \
| $(PROFILE_OUTPUT_DIR)/native
cp -RLp $< $@
$(PROFILE_OUTPUT_DIR)/native/%.o: $(PROFILE_DIR)/native/%.c \
$(wildcard native-server/*.h) \
| $(PROFILE_OUTPUT_DIR)/native
$(CC) $(CFLAGS) -c $< -o $@ -I native-server
.PHONY: test-build
test-build: $(LIBC_SO) \
$(LIBSTDCXX_SO) \
$(call timestamp_of,c-cpp) \
$(OUTPUT_DIR)/hardware-addresses.timestamp
$(PARSER_BUILD_DIR)/cparser: | $(PARSER_BUILD_DIR)
@$(LOGGER) "Building the C parser..."
@cp -RLp parser/* $(PARSER_BUILD_DIR)
@$(MAKE) -C $(PARSER_BUILD_DIR)
$(CLANG_TOOLS_BIN)/%: $(CLANG_TOOLS_BUILD_DIR)/Makefile
@$(LOGGER) "Entering target $@"
@$(MAKE) -C $(CLANG_TOOLS_BUILD_DIR) $*
$(CLANG_TOOLS_BUILD_DIR)/Makefile: clang-tools/CMakeLists.txt | $(CLANG_TOOLS_BUILD_DIR)
@$(LOGGER) "Entering target $@"
@cd $(CLANG_TOOLS_BUILD_DIR) \
&& test -f Makefile || cmake $(ROOT)/clang-tools
$(CDECL_BUILD_DIR)/Makefile.in: scripts/cdecl-3.6.tar.gz | $(OUTPUT_DIR)
@$(LOGGER) "Extracting $<"
-flock -w 120 $< \
tar xvf $< -C $(OUTPUT_DIR)
@touch $@
$(CDECL_BUILD_DIR)/Makefile: $(CDECL_BUILD_DIR)/Makefile.in
@$(LOGGER) "Configuring $@"
@cd $(CDECL_BUILD_DIR) && ./configure --without-readline
$(CDECL_BIN)/cdecl: $(CDECL_BUILD_DIR)/Makefile
@$(LOGGER) "Entering target $@"
@$(MAKE) -C $(CDECL_BUILD_DIR)
.PHONY: all-semantics
all-semantics:
@$(MAKE) -C semantics all BUILD_DIR=$(PROFILE_OUTPUT_DIR) PROFILE_DIR=$(PROFILE_DIR)
.PHONY: check
check: | pass fail fail-compile
.PHONY: pass
pass: | test-build
@$(MAKE) -C tests/unit-pass comparison
.PHONY: fail
fail: | test-build
@$(MAKE) -C tests/unit-fail comparison
.PHONY: fail-compile
fail-compile: | test-build
@$(MAKE) -C tests/unit-fail-compilation comparison
# Intended as a simple test to verify the build. If you invoke from the
# command line, do not forget to pass `BUILD_DIR=<dir>` if you specified one
# during the build.
.PHONY: simple-build-test
simple-build-test:
@$(LOGGER) "Testing kcc..."
printf "#include <stdio.h>\nint main(void) { printf(\"x\"); return 42; }" \
| $(OUTPUT_DIR)/kcc --use-profile $(PROFILE) -x c - -o $(OUTPUT_DIR)/testProgram.compiled
$(OUTPUT_DIR)/testProgram.compiled 2> /dev/null > $(OUTPUT_DIR)/testProgram.out; \
test $$? -eq 42
grep x $(OUTPUT_DIR)/testProgram.out > /dev/null 2>&1
@$(LOGGER) "Done."
@$(LOGGER) "Cleaning up..."
@rm -f $(OUTPUT_DIR)/testProgram.compiled
@rm -f $(OUTPUT_DIR)/testProgram.out
@$(LOGGER) "Done."
# Builds with `KOMPILE_FLAGS=--profile-rule-parsing`.
# Intended for direct user invocation.
# Produces a `timelogs.d` directory.
# Also produces a `timelogs.d/timelogs.csv` file
# suitable for importing into an SQL database.
.PHONY: profile-rule-parsing
profile-rule-parsing: export KOMPILE_FLAGS += --profile-rule-parsing
profile-rule-parsing: test-build
$(eval TIMELOGS_DIR := $(OUTPUT_DIR)/timelogs.d)
$(eval TIMELOGS_CSV := $(TIMELOGS_DIR)/timelogs.csv)
@$(LOGGER) "Moving timingXX.log files into $(TIMELOGS_DIR)..."
@cd $(OUTPUT_DIR)/profiles && \
find . \
! -path "*.build*" \
! -path "*.git*" \
-type f \
-name "timing*.log" \
-print | \
while IFS= read f; do \
mkdir -p $(TIMELOGS_DIR)/"$$(dirname $$f)"; \
mv $$f $(TIMELOGS_DIR)/"$$(dirname $$f)"; \
done
@$(LOGGER) "Done."
@$(LOGGER) "Generating $(TIMELOGS_CSV)..."
@cd $(TIMELOGS_DIR) && \
find . \
-type f \
-name "timing*.log" \
-exec \
perl -ne '/Source\((.*?)\):(\d+):(\d+)\s+(.*?)$$/ && print "{},$$1,$$2,$$3,$$4\n";' {} \; \
> $(TIMELOGS_CSV)
@$(LOGGER) "Done."
# This makefile does not need to be re-built.
# See https://www.gnu.org/software/make/manual/html_node/Remaking-Makefiles.html
Makefile: ;
.PHONY: clean
clean:
-$(MAKE) -C parser clean
-$(MAKE) -C semantics clean
-$(MAKE) -C tests clean
-rm -rf scripts/cdecl-3.6/
-rm -rf $(OUTPUT_DIR)
XYZ_SEMANTICS := $(addsuffix -semantics,c-translation cpp-translation c-cpp-linking c-cpp)
.PHONY: $(XYZ_SEMANTICS)
$(XYZ_SEMANTICS):
@$(MAKE) -C semantics $@ BUILD_DIR=$(PROFILE_OUTPUT_DIR) PROFILE_DIR=$(PROFILE_DIR)
# A) Move this to the end so that .SECONDEXPANSION does not
# affect the rest of the rules.
# B) The % sign matches '$(NAME)-kompiled/$(NAME)',
# e.g., c-cpp-kompiled/c-cpp'.
.SECONDEXPANSION:
$(PROFILE_OUTPUT_DIR)/%-kompiled/timestamp: PROFILE_DEPS \
$$(notdir $$*)-semantics
$(eval NAME := $(notdir $*))
@$(LOGGER) "Distributing $(NAME)"
@true $(foreach d,$(SUBPROFILE_DIRS), \
&& cp -RLp $(PROFILE_OUTPUT_DIR)/$(NAME)-kompiled $(OUTPUT_DIR)/profiles/$(shell basename $(d)))