Skip to content

Commit

Permalink
Add LLVM.version (#15354)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored Jan 21, 2025
1 parent 5a245d9 commit b103c28
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
4 changes: 4 additions & 0 deletions spec/std/llvm/llvm_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ require "spec"
require "llvm"

describe LLVM do
it ".version" do
LLVM.version.should eq LibLLVM::VERSION
end

describe ".normalize_triple" do
it "works" do
LLVM.normalize_triple("x86_64-apple-macos").should eq("x86_64-apple-macos")
Expand Down
11 changes: 1 addition & 10 deletions src/compiler/crystal/config.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ module Crystal
{{ read_file("#{__DIR__}/../../VERSION").chomp }}
end

def self.llvm_version
{% if LibLLVM.has_method?(:get_version) %}
LibLLVM.get_version(out major, out minor, out patch)
"#{major}.#{minor}.#{patch}"
{% else %}
LibLLVM::VERSION
{% end %}
end

def self.description
String.build do |io|
io << "Crystal " << version
Expand All @@ -27,7 +18,7 @@ module Crystal

io << "\n\nThe compiler was not built in release mode." unless release_mode?

io << "\n\nLLVM: " << llvm_version
io << "\n\nLLVM: " << LLVM.version
io << "\nDefault target: " << host_target
io << "\n"
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/program.cr
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ module Crystal
define_crystal_string_constant "VERSION", Crystal::Config.version, <<-MD
The version of the Crystal compiler.
MD
define_crystal_string_constant "LLVM_VERSION", Crystal::Config.llvm_version, <<-MD
define_crystal_string_constant "LLVM_VERSION", LLVM.version, <<-MD
The version of LLVM used by the Crystal compiler.
MD
define_crystal_string_constant "HOST_TRIPLE", Crystal::Config.host_target.to_s, <<-MD
Expand Down
16 changes: 16 additions & 0 deletions src/llvm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ require "c/string"
module LLVM
@@initialized = false

# Returns the runtime version of LLVM.
#
# Starting with LLVM 16, this method returns the version as reported by
# `LLVMGetVersion` at runtime. Older versions of LLVM do not expose this
# information, so the value falls back to `LibLLVM::VERSION` which is
# determined at compile time and might slightly be out of sync to the
# dynamic library loaded at runtime.
def self.version
{% if LibLLVM.has_method?(:get_version) %}
LibLLVM.get_version(out major, out minor, out patch)
"#{major}.#{minor}.#{patch}"
{% else %}
LibLLVM::VERSION
{% end %}
end

def self.init_x86 : Nil
return if @@initialized_x86
@@initialized_x86 = true
Expand Down

0 comments on commit b103c28

Please sign in to comment.