forked from dzaima/CBQN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makefile
377 lines (334 loc) · 10.7 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
SHELL = /usr/bin/env bash
MAKEFLAGS+= --no-print-directory --no-builtin-rules --no-builtin-variables
# note: do not manually define any i_… arguments, or incremental compiling will not work properly!
default: o3
# targets that only use the makefile
for-build: # for running the build system & Singeli
@"${MAKE}" i_t=forbuild i_CC=cc REPLXX=0 i_f="-O2 -DFOR_BUILD" i_FFI=0 i_SHARED=0 i_PIE= i_CC_PIE= i_EXPORT=0 i_OUTPUT_DEF=build/obj2/for_build2 run_incremental_0
for-bootstrap: # for bootstrapping bytecode
@"${MAKE}" i_t=for_bootstrap REPLXX=0 i_FFI=0 i_SHARED=0 i_PIE= i_CC_PIE= i_EXPORT=0 i_f='-DNATIVE_COMPILER -DONLY_NATIVE_COMP -DFORMATTER=0 -DNO_RT -DNO_EXPLAIN' run_incremental_0 i_USE_BC_SUBMODULE=0 BYTECODE_DIR=bytecodeNone
o3-makeonly:
@"${MAKE}" i_t=o3 i_f="-O3" run_incremental_0
c-makeonly:
@"${MAKE}" custom=1 run_incremental_0
i_notui=0
ifeq ($(origin notui),command line)
i_notui=1
endif
ifeq ($(origin REPLXX),command line)
i_REPLXX_1 = "$(REPLXX)"
else
i_REPLXX_1 = 1
endif
ifeq ($(origin singeli),command line)
i_singeli_1 = "$(singeli)"
else
i_singeli_1 = 1
endif
to-bqn-build:
ifeq ($(origin builddir),command line)
@echo "Error: 'builddir' unsupported"; false
endif
ifeq ($(origin clean),command line)
@echo "Error: build-specific 'clean' unsupported"; false
endif
@build/build from-makefile CC="$(CC)" CXX="$(CXX)" PIE="$(ENABLE_PIE)" OUTPUT="$(OUTPUT)" j="$(j)" \
verbose="$(verbose)" notui="$(i_notui)" v="$(version)" \
f="$(f)" lf="$(lf)" CCFLAGS="$(CCFLAGS)" LDFLAGS="$(LDFLAGS)" REPLXX_FLAGS="$(REPLXX_FLAGS)" CXXFLAGS="$(CXXFLAGS)" \
LD_LIBS="$(LD_LIBS)" NO_LDL="$(NO_LDL)" no_fPIC="$(no_fPIC)" \
c="$(build_c)" debug="$(debug)" $(i_build_opts) $(build_opts) \
os="$(target_os)" arch="$(target_arch)" has="$(has)" \
shared="$(i_SHARED)" singeli="$(i_singeli_1)" replxx="$(i_REPLXX_1)" FFI="$(FFI)"
# targets that go to build.bqn
o3:
@"${MAKE}" to-bqn-build
o3g:
@"${MAKE}" to-bqn-build i_build_opts="g"
o3n:
@"${MAKE}" to-bqn-build i_build_opts="native"
o3ng:
@"${MAKE}" to-bqn-build i_build_opts="native g"
c:
@"${MAKE}" to-bqn-build build_c=1
debug:
@"${MAKE}" to-bqn-build build_c=1 i_build_opts="debug"
heapverify:
@"${MAKE}" to-bqn-build build_c=1 i_build_opts="debug heapverify"
rtverify:
@"${MAKE}" to-bqn-build i_build_opts="debug rtverify"
debugn:
@"${MAKE}" to-bqn-build build_c=1 i_build_opts="native debug"
heapverifyn:
@"${MAKE}" to-bqn-build build_c=1 i_build_opts="native debug heapverify"
rtverifyn:
@"${MAKE}" to-bqn-build i_build_opts="native rtverify"
wasi-o3:
@"${MAKE}" to-bqn-build REPLXX=0 i_build_opts="wasi"
wasi-reactor-o3:
@"${MAKE}" to-bqn-build REPLXX=0 i_build_opts="wasi" i_SHARED=1
emcc-o3:
@"${MAKE}" to-bqn-build REPLXX=0 i_build_opts="emcc"
shared-o3:
@"${MAKE}" to-bqn-build REPLXX=0 i_SHARED=1
shared-c:
@"${MAKE}" to-bqn-build REPLXX=0 i_SHARED=1 i_build_opts=c
# mappings of old names
o3-singeli: o3
o3g-singeli: o3g
o3n-singeli: o3n
o3ng-singeli: o3ng
debugn-singeli: debugn
heapverifyn-singeli: heapverifyn
rtverifyn-singeli: rtverifyn
# compiler setup
i_CC := clang
i_PIE :=
i_LIBS_LD := -lm
i_LIBS_CC := -fvisibility=hidden
i_FFI := 2
i_OUTPUT := BQN
ifeq ($(origin i_OUTPUT_DEF),command line)
i_OUTPUT := $(i_OUTPUT_DEF)
endif
ifeq ($(origin OUTPUT),command line)
i_OUTPUT := $(OUTPUT)
endif
ifeq ($(i_emcc),1)
i_OUTPUT_FOLDER := $(i_OUTPUT)
i_OUTPUT_BIN := $(i_OUTPUT_FOLDER)/BQN.js
else
i_OUTPUT_BIN := $(i_OUTPUT)
endif
ifeq ($(origin CC),command line)
i_CC := $(CC)
custom = 1
endif
ifeq ($(origin FFI),command line)
i_FFI := $(FFI)
custom = 1
endif
ifeq ($(i_SHARED),1)
i_PIE := -shared
i_EXPORT := 1
i_LIBS_CC += -DCBQN_SHARED
ifneq ($(no_fPIC),1)
i_LIBS_CC += -fPIC
endif
endif
ifneq ($(i_FFI),0)
i_EXPORT := 1
ifeq ($(shell command -v pkg-config 2>&1 > /dev/null && pkg-config --exists libffi && echo $$?),0)
i_LIBS_LD += $(shell pkg-config --libs libffi)
i_LIBS_CC += $(shell pkg-config --cflags libffi)
else
i_LIBS_LD += -lffi
endif
ifneq ($(NO_LDL),1)
i_LIBS_LD += -ldl
endif
endif
ifeq ($(i_EXPORT),1)
i_LIBS_LD += -rdynamic
i_LIBS_CC += -DCBQN_EXPORT
endif
ifeq ($(origin LD_LIBS),command line)
i_LIBS_LD := $(LD_LIBS)
custom = 1
endif
ifeq ($(i_REPLXX_1),1)
i_PIE += -fPIE
i_CC_PIE := -fPIE
endif
ifeq ($(origin PIE),command line)
i_PIE := $(PIE)
custom = 1
endif
ifeq ($(origin f),command line)
custom = 1
else
f:=
endif
ifeq ($(origin lf),command line)
custom = 1
else
lf:=
endif
ifeq ($(origin CCFLAGS),command line)
custom = 1
endif
ifeq ($(origin LDFLAGS),command line)
custom = 1
endif
ifeq ($(i_REPLXX_1),1)
i_f+= -DREPLXX_STATIC=1
custom = 1
REPLXX_DIR = $(shell if [ -d build/replxxLocal ]; then echo build/replxxLocal; else echo build/replxxSubmodule; fi)
endif
BYTECODE_DIR = $(shell if [ -d build/bytecodeLocal ]; then echo bytecodeLocal; else echo bytecodeSubmodule; fi)
ifeq ($(BYTECODE_DIR),bytecodeLocal)
custom = 1
endif
ifeq ($(BYTECODE_DIR),bytecodeSubmodule)
ifeq ($(i_USE_BC_SUBMODULE),)
i_USE_BC_SUBMODULE=1
endif
endif
i_LD = $(i_CC)
CC_IS_CLANG = $(shell $(i_CC) --version | head -n1 | grep -m 1 -c "clang")
ifeq (${CC_IS_CLANG}, 1)
NOWARN = -Wno-microsoft-anon-tag -Wno-bitwise-instead-of-logical -Wno-unknown-warning-option
else
NOWARN = -Wno-parentheses
endif
ifeq ($(WINDOWS), 1)
i_f+= -DNO_MMAP
i_lf+= -lpthread
ifeq ($(i_REPLXX_1), 1)
i_f+= -DUSE_REPLXX_IO
endif
endif
ALL_CC_FLAGS = -std=gnu11 -Wall -Wno-unused-function -fms-extensions -ffp-contract=off -fno-math-errno -fno-strict-aliasing $(CCFLAGS) $(f) $(i_f) $(NOWARN) -DBYTECODE_DIR=$(BYTECODE_DIR) -DFFI=$(i_FFI) $(i_LIBS_CC) $(i_CC_PIE)
ALL_LD_FLAGS = $(LDFLAGS) $(lf) $(i_lf) $(i_PIE) $(i_LIBS_LD)
j=4
ifneq (${manualJobs},1)
ifeq (${MAKECMDGOALS},run_incremental_1)
MAKEFLAGS+= -j${j} manualJobs=1
endif
endif
builddir:
ifeq ($(force_build_dir),)
@printf 'build/obj/'
ifeq ($(custom),)
@echo "def_$(i_t)"
else
@[ -x "$$(command -v sha256sum)" ] && hashInput="sha256sum"; \
[ -x "$$(command -v shasum)" ] && hashInput="shasum -a 256"; \
printf "%s\0%s\0%s\0%s\0%s\0%s\0%s\0%s\0%s" "${i_CC}" "${ALL_CC_FLAGS}" "${ALL_LD_FLAGS}" "${i_REPLXX_1}" "${REPLXX_FLAGS}" "${CXXFLAGS}" "${i_CXX}" "${BYTECODE_DIR}" "${REPLXX_DIR}" | $$hashInput | grep -oE '[0-9a-z]{64}' | head -c32
endif
else
@printf "%s" "$(force_build_dir)"
endif
# simple non-incremental builds
single-o3:
$(i_CC) $(ALL_CC_FLAGS) -DSINGLE_BUILD -O3 -o ${i_OUTPUT_BIN} src/opt/single.c $(ALL_LD_FLAGS)
single-o3g:
$(i_CC) $(ALL_CC_FLAGS) -DSINGLE_BUILD -O3 -g -o ${i_OUTPUT_BIN} src/opt/single.c $(ALL_LD_FLAGS)
single-debug:
$(i_CC) $(ALL_CC_FLAGS) -DSINGLE_BUILD -DDEBUG -g -o ${i_OUTPUT_BIN} src/opt/single.c $(ALL_LD_FLAGS)
single-c:
$(i_CC) $(ALL_CC_FLAGS) -DSINGLE_BUILD -o ${i_OUTPUT_BIN} src/opt/single.c $(ALL_LD_FLAGS)
# actual build
run_incremental_0:
ifeq ($(i_t),forbuild)
@mkdir -p build/obj2
endif
ifeq ($(verbose),1)
@echo "build directory: $$("${MAKE}" builddir)"
@echo " bytecode: build/$(BYTECODE_DIR)"
ifeq ($(i_REPLXX_1),1)
@echo " replxx: $(REPLXX_DIR)"
else
@echo " replxx: not used"
endif
@echo " cc invocation: $(CC_INC) \[email protected] -o \$$@ -c \$$<"
@echo " ld invocation: $(i_LD) ${CCFLAGS} -o [build_dir]/BQN [build_dir]/*.o $(ALL_LD_FLAGS)"
endif
ifeq ($(singeli),1)
@echo "makefile-only builds don't support Singeli"; false
endif
ifeq ($(origin clean),command line)
@"${MAKE}" clean-specific bd="$$("${MAKE}" builddir)"
else ifeq ($(origin builddir),command line)
@echo "$$("${MAKE}" builddir)"
else # run build
ifeq ($(REPLXX_DIR),build/replxxSubmodule)
@git submodule update --init build/replxxSubmodule || (echo 'Failed to initialize submodule; clone CBQN as a git repo, or place local copies in build/ (see README.md#submodules).' && false)
endif
ifeq ($(i_USE_BC_SUBMODULE),1)
@git submodule update --init build/bytecodeSubmodule || (echo 'Failed to initialize submodule; clone CBQN as a git repo, or place local copies in build/ (see README.md#submodules).' && false)
endif
@export bd=$$("${MAKE}" builddir); \
[ "build/obj/" = "$$bd" ] && echo "Neither shasum nor sha256sum was found; cannot use custom configurations" && exit 1; \
mkdir -p "$$bd"; \
"${MAKE}" run_incremental_1 bd="$$bd"
endif # run build
run_incremental_1: ${bd}/BQN
ifneq (${bd}/BQN,${i_OUTPUT_BIN})
ifeq ($(i_emcc),1)
@cp -f ${bd}/BQN.wasm "${i_OUTPUT_FOLDER}/BQN.wasm"
endif
ifeq ($(WINDOWS),1)
@cp -f ${bd}/BQN.exe "${i_OUTPUT_BIN}"
else
@cp -f ${bd}/BQN "${i_OUTPUT_BIN}"
endif
endif
@echo ${postmsg}
${bd}/BQN: builtins core base jit utils # build the final binary
@$(i_LD) ${CCFLAGS} -o ${bd}/BQN ${bd}/*.o $(ALL_LD_FLAGS)
CC_INC = $(i_CC) $(ALL_CC_FLAGS) -MMD -MP -MF
# build individual object files
core: ${addprefix ${bd}/, tyarr.o harr.o fillarr.o stuff.o derv.o mm.o heap.o}
${bd}/%.o: src/core/%.c
@echo $< | cut -c 5-
@$(CC_INC) [email protected] -o $@ -c $<
base: ${addprefix ${bd}/, load.o main.o rtwrap.o vm.o ns.o nfns.o ffi.o}
${bd}/%.o: src/%.c
@echo $< | cut -c 5-
@$(CC_INC) [email protected] -o $@ -c $<
utils: ${addprefix ${bd}/, ryu.o utf.o hash.o file.o mut.o each.o bits.o}
${bd}/%.o: src/utils/%.c
@echo $< | cut -c 5-
@$(CC_INC) [email protected] -o $@ -c $<
jit: ${addprefix ${bd}/, nvm.o}
${bd}/%.o: src/jit/%.c
@echo $< | cut -c 5-
@$(CC_INC) [email protected] -o $@ -c $<
builtins: ${addprefix ${bd}/, arithm.o arithd.o cmp.o sfns.o squeeze.o select.o slash.o group.o sort.o search.o selfsearch.o transpose.o fold.o scan.o md1.o md2.o compare.o cells.o fns.o sysfn.o internal.o inverse.o}
${bd}/%.o: src/builtins/%.c
@echo $< | cut -c 5-
@$(CC_INC) [email protected] -o $@ -c $<
.INTERMEDIATE: core base utils jit builtins bytecodeMessage
$(bd)/load.o: bytecodeMessage
bytecodeMessage:
ifeq ($(i_USE_BC_SUBMODULE),1)
@echo "Using precompiled bytecode; see readme for how to build your own"
endif
# replxx
ifeq ($(i_REPLXX_1),1)
i_CXX := c++
ifeq ($(origin CXX),command line)
i_CXX := $(CXX)
endif
i_LD = $(i_CXX)
REPLXX_FLAGS = -std=c++11 -Os
ALL_CC_FLAGS += -DUSE_REPLXX -I$(REPLXX_DIR)/include $(i_CC_PIE)
CXX_INC = $(i_CXX) $(CCFLAGS) $(REPLXX_FLAGS) $(CXXFLAGS) -DREPLXX_STATIC=1 -I$(REPLXX_DIR)/include -MMD -MP -MF
replxx_obj: ${addprefix ${bd}/, ConvertUTF.cpp.o wcwidth.cpp.o conversion.cxx.o escape.cxx.o history.cxx.o prompt.cxx.o replxx.cxx.o replxx_impl.cxx.o terminal.cxx.o util.cxx.o windows.cxx.o}
${bd}/%.o: $(REPLXX_DIR)/src/%
@echo $<
@$(CXX_INC) [email protected] -o $@ -c $<
${bd}/BQN: replxx_obj
endif # replxx
# dependency files
-include $(bd)/*.d
DESTDIR =
PREFIX = /usr/local
install: uninstall
cp -f BQN "$(DESTDIR)$(PREFIX)/bin/bqn"
uninstall:
rm -f "$(DESTDIR)$(PREFIX)/bin/bqn"
clean-build:
rm -f build/obj/*/*.o
rm -f build/obj/*/*.d
rm -f build/obj/*/BQN
clean-specific:
rm -f $(bd)/*.o
rm -f $(bd)/*.d
rm -f $(bd)/BQN
rmdir $(bd); true
clean-submodules:
git submodule deinit build/singeliSubmodule/ build/replxxSubmodule/ build/bytecodeSubmodule/
clean-obj2:
rm -rf build/obj2
clean: clean-build clean-obj2