Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support $LLVM_VERSION, $LLVM_TARGETS, and $LLVM_LDFLAGS #15091

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -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 ))
Expand Down
36 changes: 25 additions & 11 deletions src/llvm/lib_llvm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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++")]
Expand All @@ -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) }}
Expand Down
Loading