-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmakefile
More file actions
278 lines (237 loc) · 8.09 KB
/
makefile
File metadata and controls
278 lines (237 loc) · 8.09 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
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
MAKEFLAGS := --silent
MAKE_CONC := $(MAKE) -j 128 CONC=true clear=$(or $(clear),false)
CLEAR ?= $(if $(filter false,$(clear)),, )
HERE ?= $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
CC := clang
PROD ?=
STRICT ?=
DEBUG_FLAGS_0 ?= -g3 -fsanitize=undefined,address,integer,nullability -fstack-protector
DEBUG_FLAGS_1 ?= -g3 -Wno-unused-parameter -Wno-unused-variable
DEBUG_FLAGS_PROD ?= -g3 -O2 -Wno-unused-parameter -Wno-unused-variable
DEBUG_FLAGS ?= $(if $(DEBUG),$(DEBUG_FLAGS_0),$(if $(PROD),$(DEBUG_FLAGS_PROD),$(DEBUG_FLAGS_1)))
CRASH_FLAGS ?= $(and $(FAST_CRASH),-DFAST_CRASH)
STRICT_FLAGS ?= $(and $(STRICT),-Werror)
COMPILE_FLAGS ?= $(strip $(shell cat $(HERE)/compile_flags.txt))
CFLAGS ?= $(and $(PROD),-DPROD) $(COMPILE_FLAGS) $(STRICT_FLAGS) $(DEBUG_FLAGS) $(CRASH_FLAGS)
LOCAL ?= local
CLIB_DIR ?= clib
COMP_DIR ?= comp
GEN_DIR ?= generated
ASM_GEN_SRC ?= $(COMP_DIR)/asm_gen.c
ASM_GEN_OUT ?= $(COMP_DIR)/asm_generated.s
MACH_GEN_SRC ?= mach/mach_exc.defs
MACH_GEN_OUT ?= $(GEN_DIR)/mach_exc.c
CLIB_SRC ?= $(shell find $(CLIB_DIR) -type f \( -name '*.c' -or -name '*.h' -or -name '*.s' \) )
COMP_SRC ?= $(filter-out $(ASM_GEN_OUT),$(shell find $(COMP_DIR) -type f \( -name '*.c' -or -name '*.h' -or -name '*.s' \) ))
ALL_SRC ?= $(CLIB_SRC) $(COMP_SRC)
MAIN_SRC ?= $(COMP_DIR)/main.c
MAIN_S ?= astil_s.exe
MAIN ?= astil.exe
TEST_EXE ?= test.exe
FILE_EXE ?= $(and $(file),$(basename $(file)).exe)
DISASM ?= --disassemble-all --headers --private-headers --reloc --dynamic-reloc --syms --dynamic-syms
WATCH_IGNORE ?= -i=$(GEN_DIR) -i=$(ASM_GEN_OUT)
WATCH ?= watchexec $(and $(CLEAR),-c) $(WATCH_IGNORE) -r -d=1ms -n -q
# WATCH ?= watchexec $(and $(CLEAR),-c) $(WATCH_IGNORE) -r -d=1ms -n -q --no-vcs-ignore
WATCH_COMP ?= $(WATCH) -e=c,h,s
WATCH_PROG ?= $(WATCH) -e=af
WATCH_ALL ?= $(WATCH) -e=c,h,s,af
WATCH_IMM ?= $(WATCH) --no-vcs-ignore -w=forth -w=$(MAIN) -w=$(MAIN_S)
ARTIF ?= $(shell find . \( -type d -name '*.dSYM' \) -or \( -type f \( -name '.DS_Store' -or -name '*.o' -or -name '*.exe' \) \))
ifeq ($(verb),true)
OK = echo [$@] ok
endif
# Disables some dangerous behaviors. Without this, `$@` sometimes changes from
# the intended target name to something surprising, like `makefile`, resulting
# in weird `cc` build commands that don't work and delete the wrong files.
.SUFFIXES:
# Auto-delete intermediary executables if any.
# Automatically affects `run_c`.
.INTERMEDIATE: $(FILE_EXE)
.PHONY: all
all: vet build
$(OK)
.PHONY: all_w
all_w:
$(WATCH_COMP) -- $(MAKE) all
.PHONY: build
build: $(MAIN_S) $(MAIN)
$(OK)
.PHONY: build_w
build_w:
$(WATCH_COMP) -- $(MAKE) build
.PHONY: vet
vet:
clang-tidy --quiet $(MAIN_SRC) -- $(CFLAGS) -ferror-limit=1
$(OK)
.PHONY: vet_w
vet_w:
$(WATCH_COMP) -- $(MAKE) vet
# We use sandboxing to prevent buggy JIT-ted code from accidentally
# deleting the entire filesystem, launching nuclear missiles, etc..
# However, we whitelist a few child executables used for debugging.
EXE0 = $(shell realpath $$(which llvm-mc))
EXE1 = $(shell realpath $$(xcrun --find llvm-symbolizer))
EXE2 = $(shell realpath $$(xcrun --find atos))
.PHONY: run_boxed
run_boxed:
rlwrap --no-warnings \
sandbox-exec \
-f sandbox.sb \
-D MAIN="$(PWD)/$(file)" \
-D OUT="$(PWD)/$(TEST_EXE)" \
-D EXE0=$(EXE0) \
-D EXE1=$(EXE1) \
-D EXE2=$(EXE2) \
$(abspath $(file)) $(args)
# Register-CC version.
.PHONY: run
run:
$(MAKE) run_boxed file=$(MAIN)
# Stack-CC version.
.PHONY: run_s
run_s:
$(MAKE) run_boxed file=$(MAIN_S)
# Usage example:
#
# make run_w args='forth/test.af -'
.PHONY: run_w
run_w:
$(WATCH_IMM) -- $(MAKE) run
.PHONY: run_s_w
run_s_w:
$(WATCH_IMM) -- $(MAKE) run_s
$(MAIN): $(ALL_SRC)
$(CC) $(CFLAGS) $(MAIN_SRC) -o $@
$(MAIN_S): $(ALL_SRC) $(ASM_GEN_OUT)
$(CC) $(CFLAGS) -DCALL_CONV_STACK $(MAIN_SRC) -o $@
# Usage example:
#
# make run_c file=some_file.c
.PHONY: run_c
run_c: $(FILE_EXE) $(ALL_SRC)
$(abspath $(FILE_EXE)) $(args)
.PHONY: run_c_w
run_c_w:
$(WATCH_COMP) -- $(MAKE) run_c
.PHONY: repl
repl:
$(MAKE) run args='std:lang.af -'
.PHONY: repl_s
repl_s:
$(MAKE) run_s args='std:lang_s.af -'
.PHONY: test
test:
make run args='forth/test.af --build=$(TEST_EXE)'
./$(TEST_EXE)
.PHONY: test_w
test_w:
$(WATCH_IMM) -- $(MAKE) test
.PHONY: test_s
test_s:
make run_s args=forth/test_s.af
.PHONY: test_s_w
test_s_w:
$(WATCH_IMM) -- $(MAKE) test_s
# For executables from arbitrary C files. This is possible because our C files
# specify all their dependencies with `#include`, without needing the build
# system to describe the dependencies.
#
# Even on Unix, using `.exe` is convenient. It makes this recipe
# possible, allows to use `.INTERMEDIATE` for auto-cleanup, and
# allows `make clean` to delete these executables by wildcard.
%.exe: %.c $(ALL_SRC)
$(CC) $(CFLAGS) -x c $< -o $@
**/%.exe: %.c $(ALL_SRC)
$(CC) $(CFLAGS) -x c $< -o $@
# This is easier to define with `make run_c`, but in that case,
# removing `--silent` or adding any logging breaks the recipe.
$(ASM_GEN_OUT): $(ASM_GEN_SRC) $(ALL_SRC) $(COMP_DIR)/asm_gen.exe
$(COMP_DIR)/asm_gen.exe > $(ASM_GEN_OUT)
.PHONY: debug
debug:
lldb \
--source-quietly \
--one-line "settings set show-statusline false" \
--one-line "settings set target.disable-aslr true" \
$(and $(DEBUG),--one-line "settings set target.env-vars DEBUG=true") \
--file $(file) -- $(args)
# .PHONY: debug_run
# debug_run:
# lldb --batch \
# --source-quietly \
# --one-line "settings set show-statusline false" \
# --one-line "process handle SIGSEGV --notify true --pass true --stop false" \
# --one-line "process handle SIGBUS --notify true --pass true --stop false" \
# --one-line "process handle SIGILL --notify true --pass true --stop false" \
# --one-line "process handle SIGABRT --notify true --pass true --stop false" \
# --one-line "settings set target.disable-aslr true" \
# $(and $(DEBUG),--one-line "settings set target.env-vars DEBUG=true") \
# --one-line "run" \
# --one-line "quit" \
# --file $(file) -- $(args)
.PHONY: debug_run
debug_run:
lldb --batch \
--source-quietly \
--one-line "settings set show-statusline false" \
--one-line "settings set target.disable-aslr true" \
$(and $(DEBUG),--one-line "settings set target.env-vars DEBUG=true") \
--one-line "run" \
--one-line "quit" \
--file $(file) -- $(args)
.PHONY: debug_run_w
debug_run_w:
$(WATCH_IMM) -- $(MAKE) debug_run
.PHONY: prepro
prepro:
mkdir -p $(LOCAL)
$(CC) -E $(CFLAGS) $(or $(file),$(MAIN_SRC)) -o $(LOCAL)/out.c
.PHONY: asm
asm:
mkdir -p $(LOCAL)
$(CC) -S $(CFLAGS) $(or $(file),$(MAIN_SRC)) -o $(LOCAL)/out.s
.PHONY: disasm
disasm:
mkdir -p $(LOCAL)
llvm-objdump $(DISASM) $(or $(file),$(MAIN)) > $(LOCAL)/out.s
.PHONY: clean
clean:
rm -rf $(GEN_DIR) $(wildcard $(ARTIF))
# The MIG's output is much worse than this.
$(MACH_GEN_OUT): $(MACH_GEN_SRC)
mkdir -p $(GEN_DIR)
xcrun mig -server $(GEN_DIR)/tmp.c -user /dev/null -header /dev/null $(MACH_GEN_SRC) \
&& cat mach/mach_pre.txt $(GEN_DIR)/tmp.c mach/mach_suf.txt \
| sed -e 's/__attribute__((unused))//g' -e 's/__attribute__((__unused__))//g' \
| clang-format \
> $(MACH_GEN_OUT) \
; rm -rf $(GEN_DIR)/tmp.c
.PHONY: bench
bench:
bench/bench.sh
# Non-configurable for now; usage of `~/.local`
# is also hardcoded in the interpreter.
# SYNC[install_path].
INSTALL_ROOT := $(HOME)/.local
INSTALL_BIN := $(INSTALL_ROOT)/bin
INSTALL_DAT := $(INSTALL_ROOT)/share
# Assumes that the "bin" path is already in the `$PATH`.
# By default uses `~/.local/bin`, which is cleaner than
# `/usr/...`, but `~/.local/bin` is NOT in the `$PATH`
# by default, and needs to be added there manually.
.PHONY: install
install: build
mkdir -p "$(INSTALL_BIN)"
mkdir -p "$(INSTALL_DAT)"
ln -sf $(shell realpath $(MAIN)) "$(INSTALL_BIN)/astil"
ln -sf $(shell realpath $(MAIN_S)) "$(INSTALL_BIN)/astil_s"
ln -sfn $(shell realpath ./forth) "$(INSTALL_DAT)/astil"
@echo "Symlinked the reg-CC executable to: \"$(INSTALL_BIN)/astil\"."
@echo "Symlinked the stack-CC executable to: \"$(INSTALL_BIN)/astil_s\"."
@echo "Symlinked the built-in modules to: \"$(INSTALL_DAT)/astil\"."
@echo "Hint: make sure \"$(INSTALL_BIN)\" is in your PATH."
.PHONY: uninstall
uninstall:
rm -f "$(INSTALL_BIN)/astil"
rm -f "$(INSTALL_DAT)/astil"