Skip to content

Commit 8b71775

Browse files
authored
Allow LLVM_VERSION override inside Makefile (#15765)
Setting `LLVM_VERSION` as an environment variable or a command-line argument to `make` will now inhibit LLVM version detection via `LLVM_CONFIG`. This is mainly to support cross-compilation from Linux to Windows MSVC, i.e. `make crystal target=x86_64-windows-msvc LLVM_VERSION=19.1.7` should now just work on WSL. It is also advisable to set the `LLVM_TARGETS` and `LLVM_LDFLAGS` environment variables, as implemented in #15091. Using LLVM 17 or below will still fail if `LLVM_CONFIG` is not set, since the Makefiles have never supported cross-compiling `llvm_ext.o`, and that object file needs `llvm-config --cxxflags`. A side benefit is that Crystal can now somewhat target future LLVM versions by simply doing something like `make LLVM_VERSION=21.0.0`, since it is only `find-llvm-config.sh` that imposes the version check.
1 parent 1887c81 commit 8b71775

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ all:
2222
## $ make -B generate_data
2323

2424
CRYSTAL ?= crystal## which previous crystal compiler use
25-
LLVM_CONFIG ?= ## llvm-config command path to use
2625

2726
release ?= ## Compile in release mode
2827
stats ?= ## Enable statistics output
@@ -62,8 +61,12 @@ override EXPORTS_BUILD += \
6261
$(EXPORT_CC) \
6362
CRYSTAL_CONFIG_LIBRARY_PATH=$(CRYSTAL_CONFIG_LIBRARY_PATH)
6463
SHELL = sh
65-
LLVM_CONFIG := $(shell src/llvm/ext/find-llvm-config.sh)
66-
LLVM_VERSION ?= $(if $(LLVM_CONFIG),$(shell "$(LLVM_CONFIG)" --version 2> /dev/null))
64+
65+
ifeq ($(LLVM_VERSION),)
66+
LLVM_CONFIG ?= $(shell src/llvm/ext/find-llvm-config.sh)
67+
LLVM_VERSION ?= $(if $(LLVM_CONFIG),$(shell "$(LLVM_CONFIG)" --version 2> /dev/null))
68+
endif
69+
6770
LLVM_EXT_DIR = src/llvm/ext
6871
LLVM_EXT_OBJ = $(LLVM_EXT_DIR)/llvm_ext.o
6972
CXXFLAGS += $(if $(debug),-g -O0)
@@ -101,8 +104,8 @@ endif
101104

102105
check_llvm_config = $(eval \
103106
check_llvm_config := $(if $(LLVM_VERSION),\
104-
$(call colorize,Using $(LLVM_CONFIG) [version=$(LLVM_VERSION)]),\
105-
$(error "Could not locate compatible llvm-config, make sure it is installed and in your PATH, or set LLVM_CONFIG. Compatible versions: $(shell cat src/llvm/ext/llvm-versions.txt)))\
107+
$(call colorize,Using $(or $(LLVM_CONFIG),externally configured LLVM) [version=$(LLVM_VERSION)]),\
108+
$(error "Could not locate compatible llvm-config, make sure it is installed and in your PATH, or set LLVM_VERSION / LLVM_CONFIG. Compatible versions: $(shell cat src/llvm/ext/llvm-versions.txt)))\
106109
)
107110

108111
.PHONY: all
@@ -258,7 +261,7 @@ $(O)/$(CRYSTAL_BIN): $(DEPS) $(SOURCES)
258261

259262
$(LLVM_EXT_OBJ): $(LLVM_EXT_DIR)/llvm_ext.cc
260263
$(call check_llvm_config)
261-
$(CXX) -c $(CXXFLAGS) -o $@ $< $(shell $(LLVM_CONFIG) --cxxflags)
264+
$(CXX) -c $(CXXFLAGS) -o $@ $< $(if $(LLVM_CONFIG),$(shell $(LLVM_CONFIG) --cxxflags))
262265

263266
man/: $(MAN1PAGES)
264267

Makefile.win

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ all:
2222
## $ make -B generate_data
2323

2424
CRYSTAL ?= crystal ## which previous crystal compiler use
25-
LLVM_CONFIG ?= ## llvm-config command path to use
2625

2726
release ?= ## Compile in release mode
2827
stats ?= ## Enable statistics output
@@ -67,8 +66,12 @@ CRYSTAL_VERSION ?= $(shell type src\VERSION)
6766
SOURCE_DATE_EPOCH ?= $(or $(shell type src\SOURCE_DATE_EPOCH 2>NUL),$(shell git show -s --format=%ct HEAD))
6867
export_vars = $(eval export CRYSTAL_CONFIG_BUILD_COMMIT CRYSTAL_CONFIG_PATH SOURCE_DATE_EPOCH)
6968
export_build_vars = $(eval export CRYSTAL_CONFIG_LIBRARY_PATH)
70-
LLVM_CONFIG ?=
71-
LLVM_VERSION ?= $(if $(LLVM_CONFIG),$(shell $(LLVM_CONFIG) --version))
69+
70+
ifeq ($(LLVM_VERSION),)
71+
LLVM_CONFIG ?=
72+
LLVM_VERSION ?= $(if $(LLVM_CONFIG),$(shell "$(LLVM_CONFIG)" --version))
73+
endif
74+
7275
LLVM_EXT_DIR = src\llvm\ext
7376
LLVM_EXT_OBJ = $(LLVM_EXT_DIR)\llvm_ext.obj
7477
CXXFLAGS += $(if $(static),$(if $(debug),/MTd /Od ,/MT ),$(if $(debug),/MDd /Od ,/MD ))
@@ -79,7 +82,7 @@ LIBDIR ?= $(prefix)\lib
7982
SRCDIR ?= $(prefix)\src
8083
DATADIR ?= $(prefix)
8184

82-
colorize = $1
85+
colorize = $(info $1)
8386

8487
DEPS = $(LLVM_EXT_OBJ)
8588
ifneq ($(LLVM_VERSION),)
@@ -90,8 +93,8 @@ endif
9093

9194
check_llvm_config = $(eval \
9295
check_llvm_config := $(if $(LLVM_VERSION),\
93-
$(info $(call colorize,Using $(LLVM_CONFIG) [version=$(LLVM_VERSION)])),\
94-
$(error "Could not locate compatible llvm-config, make sure it is installed and in your PATH, or set LLVM_CONFIG. Compatible versions: $(shell type src\llvm\ext\llvm-versions.txt)))\
96+
$(call colorize,Using $(or $(LLVM_CONFIG),externally configured LLVM) [version=$(LLVM_VERSION)]),\
97+
$(error "Could not locate compatible llvm-config, make sure it is installed and in your PATH, or set LLVM_VERSION / LLVM_CONFIG. Compatible versions: $(shell type src\llvm\ext\llvm-versions.txt)))\
9598
)
9699

97100
.PHONY: all
@@ -232,7 +235,7 @@ $(LLVM_EXT_OBJ): $(LLVM_EXT_DIR)\llvm_ext.cc
232235
@rem - warning C4244: 'initializing': conversion from '_Ty' to '_Ty2', possible loss of data
233236
@rem - warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
234237
@rem - warning C4624: '...': destructor was implicitly defined as deleted
235-
$(CXX) /nologo /c $(CXXFLAGS) /WX /wd4244 /wd4624 /wd4530 "/Fo$@" "$<" $(shell $(LLVM_CONFIG) --cxxflags)
238+
$(CXX) /nologo /c $(CXXFLAGS) /WX /wd4244 /wd4624 /wd4530 "/Fo$@" "$<" $(if $(LLVM_CONFIG),$(shell $(LLVM_CONFIG) --cxxflags))
236239

237240
.PHONY: clean
238241
clean: clean_crystal ## Clean up built directories and files

0 commit comments

Comments
 (0)