diff --git a/Makefile b/Makefile index d30db53464f7..9ab613c690e5 100644 --- a/Makefile +++ b/Makefile @@ -62,7 +62,7 @@ EXPORTS_BUILD := \ CRYSTAL_CONFIG_LIBRARY_PATH=$(CRYSTAL_CONFIG_LIBRARY_PATH) SHELL = sh LLVM_CONFIG := $(shell src/llvm/ext/find-llvm-config) -LLVM_VERSION := $(if $(LLVM_CONFIG),$(shell "$(LLVM_CONFIG)" --version 2> /dev/null)) +LLVM_VERSION ?= $(if $(LLVM_CONFIG),$(shell "$(LLVM_CONFIG)" --version 2> /dev/null)) LLVM_EXT_DIR = src/llvm/ext LLVM_EXT_OBJ = $(LLVM_EXT_DIR)/llvm_ext.o CXXFLAGS += $(if $(debug),-g -O0) diff --git a/Makefile.win b/Makefile.win index 0613acc8a207..43f7786e7ccc 100644 --- a/Makefile.win +++ b/Makefile.win @@ -68,7 +68,7 @@ SOURCE_DATE_EPOCH ?= $(or $(shell type src\SOURCE_DATE_EPOCH 2>NUL),$(shell git export_vars = $(eval export CRYSTAL_CONFIG_BUILD_COMMIT CRYSTAL_CONFIG_PATH SOURCE_DATE_EPOCH) export_build_vars = $(eval export CRYSTAL_CONFIG_LIBRARY_PATH) LLVM_CONFIG ?= -LLVM_VERSION := $(if $(LLVM_CONFIG),$(shell $(LLVM_CONFIG) --version)) +LLVM_VERSION ?= $(if $(LLVM_CONFIG),$(shell $(LLVM_CONFIG) --version)) LLVM_EXT_DIR = src\llvm\ext LLVM_EXT_OBJ = $(LLVM_EXT_DIR)\llvm_ext.obj CXXFLAGS += $(if $(static),$(if $(debug),/MTd /Od ,/MT ),$(if $(debug),/MDd /Od ,/MD )) diff --git a/src/llvm/lib_llvm.cr b/src/llvm/lib_llvm.cr index 8b6856631b55..a4b27bfb68b3 100644 --- a/src/llvm/lib_llvm.cr +++ b/src/llvm/lib_llvm.cr @@ -5,14 +5,10 @@ {% config ||= read_file?("#{dir.id}/llvm_VERSION") %} {% end %} - {% unless config %} - {% raise "Cannot determine LLVM configuration; ensure the file `llvm_VERSION` exists under `CRYSTAL_LIBRARY_PATH`" %} - {% end %} - - {% lines = config.lines.map(&.chomp) %} - {% llvm_version = lines[0] %} - {% llvm_targets = lines[1] %} - {% llvm_ldflags = lines[2] %} + {% lines = config ? config.lines.map(&.chomp) : nil %} + {% llvm_version = env("LLVM_VERSION") || (lines && lines[0]) %} + {% llvm_targets = env("LLVM_TARGETS") || (lines && lines[1]) %} + {% llvm_ldflags = env("LLVM_LDFLAGS") || (lines && lines[2]) %} @[Link("llvm")] {% if compare_versions(Crystal::VERSION, "1.11.0-dev") >= 0 %} @@ -22,9 +18,9 @@ end {% else %} {% llvm_config = env("LLVM_CONFIG") || `sh #{__DIR__}/ext/find-llvm-config`.stringify %} - {% llvm_version = `#{llvm_config.id} --version`.stringify %} + {% llvm_version = env("LLVM_VERSION") || `#{llvm_config.id} --version`.stringify %} {% llvm_targets = env("LLVM_TARGETS") || `#{llvm_config.id} --targets-built`.stringify %} - {% llvm_ldflags = "`#{llvm_config.id} --libs --system-libs --ldflags#{" --link-static".id if flag?(:static)}#{" 2> /dev/null".id unless flag?(:win32)}`" %} + {% llvm_ldflags = env("LLVM_LDFLAGS") || "`#{llvm_config.id} --libs --system-libs --ldflags#{" --link-static".id if flag?(:static)}#{" 2> /dev/null".id unless flag?(:win32)}`" %} {% unless flag?(:win32) %} @[Link("stdc++")] @@ -33,7 +29,25 @@ {% end %} {% end %} - @[Link(ldflags: {{ llvm_ldflags }})] + {% llvm_version ||= Crystal::DESCRIPTION.gsub(/.*LLVM: ([^\n]*).*/m, "\\1") %} + + {% unless llvm_targets %} + {% if flag?(:i386) || flag?(:x86_64) %} + {% llvm_targets = "X86" %} + {% elsif flag?(:arm) %} + {% llvm_targets = "ARM" %} + {% elsif flag?(:aarch64) %} + {% llvm_targets = "AArch64" %} + {% elsif flag?(:wasm32) %} + {% llvm_targets = "WebAssembly" %} + {% elsif flag?(:avr) %} + {% llvm_targets = "AVR" %} + {% end %} + {% end %} + + {% if llvm_ldflags %} + @[Link(ldflags: {{ llvm_ldflags }})] + {% end %} lib LibLLVM VERSION = {{ llvm_version.strip.gsub(/git/, "").gsub(/rc.*/, "") }} BUILT_TARGETS = {{ llvm_targets.strip.downcase.split(' ').map(&.id.symbolize) }}