diff options
Diffstat (limited to 'Makefile')
| -rw-r--r-- | Makefile | 45 |
1 files changed, 33 insertions, 12 deletions
@@ -13,8 +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 NOLLVM=1 leave the llvm backend out of zy (it is in by default when an -# llvm-config with headers is on PATH, or LLVM_CONFIG names one) +# 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 @@ -97,29 +98,49 @@ $(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 llvm backend (-DZYLLVM) goes into zy whenever an llvm with headers is found: -# LLVM_CONFIG when given, else llvm-config, else llvm-config-19 on PATH. llvm-config can -# exist without the headers (a runtime-only install), so the gate is llvm-c/Core.h. -# NOLLVM=1 leaves it out. zy does not record which way it was built: after changing -# NOLLVM or the llvm, rebuild it (rm build/bin/zy, or make clean). -ifneq ($(NOLLVM),1) +# 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_CFLAGS := -DZYLLVM $(shell $(LLVM_CONFIG) --cflags) - LLVM_LIBS := $(shell $(LLVM_CONFIG) --ldflags --libs) + 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 llvm-config with headers found, building zy without the llvm backend (NOLLVM=1 silences this)) + $(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) | $(BIN) +$(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) |
