aboutsummaryrefslogtreecommitdiff
path: root/Makefile
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-14 16:02:56 +0330
committerGravatar zinc <zinc@downsta.ir> 2026-10-14 16:02:56 +0330
commitfb2679438835fc6bad2dd135887957d7642c8023 (patch)
tree671398389c2b44af21f374e20b61afe5c8e4fb02 /Makefile
parentmerge line tables (diff)
parentmake: keep the x86-64 jit by default (LLVM=1 opts in), record the llvm mode s... (diff)
merge: llvm auto-build (opt-in on x86-64), mode stamp
Diffstat (limited to 'Makefile')
-rw-r--r--Makefile50
1 files changed, 47 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 8779cc3..b3b7d56 100644
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,9 @@
# (src/tools/xref.sh): build/xref/report.txt and report.json;
# make xref XREF_ARGS=--check compares with tests/xref.baseline
# make genome build/core/genome.bin from src/, with the fixpoint check
+# make LLVM=1 on x86_64, build zy with the llvm backend instead of the x86-64 jit
+# make NOLLVM=1 on other hosts, leave out the llvm backend (in by default when a
+# usable llvm-config is on PATH, or LLVM_CONFIG names one)
# make build/bin/zyi the interpreter alone (zy has a jit on arm64 and on x86-64 linux)
# make lsp build/bin/zclsp, the language server
# make web build/web/: the page, its script and zc.wasm
@@ -95,9 +98,50 @@ $(B)/core/genome.bin: $(ZC_DEPS) $(BIN)/zc0$(EXE) $(BIN)/mkgenome$(EXE) src/core
cp $(B)/s1/genome.bin $@
@echo "fixpoint: $$(( $$(wc -c < $@) / 8 )) words, $@ updated"
-# the vm core with the compiler genome inside: a jit on arm64 and x86-64 linux, else the interpreter
-$(BIN)/zy$(EXE): src/core/core.c $(IO) $(B)/core/genome.bin $(CORE_H) | $(BIN)
- $(CC) $(CFLAGS) -DJTYPE=int src/core/core.c $(IO) $(COREINC) -o $@
+# the llvm backend (-DZYLLVM) in zy. -DZYLLVM replaces the x86-64 jit, so on x86_64 hosts
+# (uname -m) it is built only when LLVM=1 is given; on other hosts it is in whenever an llvm
+# is found, and NOLLVM=1 leaves it out. the llvm is LLVM_CONFIG when given, else llvm-config,
+# else llvm-config-19 on PATH; it is used only when llvm-c/Core.h exists, --libs is not
+# empty and a tiny program links against it (llvm-config can exist without headers or libs).
+# otherwise the build goes on without it and prints a one-line note.
+HOST_ARCH := $(shell uname -m 2>/dev/null)
+ifeq ($(HOST_ARCH),x86_64)
+ WANT_LLVM := $(if $(filter 1,$(LLVM)),1,)
+else
+ WANT_LLVM := $(if $(filter 1,$(NOLLVM)),,1)
+endif
+ifeq ($(WANT_LLVM),1)
+ ifeq ($(origin LLVM_CONFIG),undefined)
+ LLVM_CONFIG := $(firstword $(shell command -v llvm-config llvm-config-19 2>/dev/null))
+ endif
+ ifneq ($(LLVM_CONFIG),)
+ ifneq ($(wildcard $(shell $(LLVM_CONFIG) --includedir 2>/dev/null)/llvm-c/Core.h),)
+ llvm_try_libs := $(shell $(LLVM_CONFIG) --libs 2>/dev/null)
+ ifneq ($(strip $(llvm_try_libs)),)
+ llvm_try_cflags := $(shell $(LLVM_CONFIG) --cflags 2>/dev/null)
+ llvm_try_ldflags := $(shell $(LLVM_CONFIG) --ldflags 2>/dev/null)
+ llvm_link_ok := $(shell d=$$(mktemp -d) && printf '\043include <llvm-c/Core.h>\nint main(void){LLVMContextDispose(LLVMContextCreate());return 0;}\n' > $$d/t.c && $(CC) $(llvm_try_cflags) $$d/t.c -o $$d/t $(llvm_try_ldflags) $(llvm_try_libs) >/dev/null 2>&1 && echo yes; rm -rf $$d)
+ ifeq ($(llvm_link_ok),yes)
+ LLVM_CFLAGS := -DZYLLVM $(llvm_try_cflags)
+ LLVM_LIBS := $(llvm_try_ldflags) $(llvm_try_libs)
+ endif
+ endif
+ endif
+ endif
+ ifeq ($(LLVM_CFLAGS),)
+ $(info note: no usable llvm (llvm-config with headers, libs and a test link), building zy without the llvm backend)
+ endif
+endif
+# the mode zy was built in; rewritten only when it changes, so switching rebuilds zy
+LLVM_MODE := $(if $(LLVM_CFLAGS),llvm,nollvm)
+ifneq ($(MAKECMDGOALS),clean)
+ $(shell mkdir -p $(B); [ "$$(cat $(B)/llvm.mode 2>/dev/null)" = "$(LLVM_MODE)" ] || echo $(LLVM_MODE) > $(B)/llvm.mode)
+endif
+
+# the vm core with the compiler genome inside: a jit on arm64 and x86-64 linux, else the
+# interpreter; plus the llvm backend when found (above)
+$(BIN)/zy$(EXE): src/core/core.c $(IO) $(B)/core/genome.bin $(CORE_H) $(B)/llvm.mode | $(BIN)
+ $(CC) $(CFLAGS) -DJTYPE=int $(LLVM_CFLAGS) src/core/core.c $(IO) $(COREINC) -o $@ $(LLVM_LIBS)
# the interpreter alone, even on a host with a jit: make build/bin/zyi, to compare against zy
$(BIN)/zyi$(EXE): src/core/core.c $(IO) $(B)/core/genome.bin $(CORE_H) | $(BIN)
$(CC) $(CFLAGS) -DZYINTERP -DJTYPE=int src/core/core.c $(IO) $(COREINC) -o $@