-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add Crystal::Loader
#11434
Merged
straight-shoota
merged 6 commits into
crystal-lang:master
from
straight-shoota:feature/loader
Nov 25, 2021
Merged
Add Crystal::Loader
#11434
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f6a40a7
Add `Crystal::Loader`
straight-shoota ae69b68
Update src/compiler/crystal/loader/unix.cr
straight-shoota e5c5a4f
Add order spec
straight-shoota 8e398a6
Check in foo2.c
straight-shoota ce0952c
Apply suggestions from code review
straight-shoota bedbe1e
Move SHARED_LIBRARY_EXTENSION to unix implementation
straight-shoota File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
int foo(); | ||
|
||
int bar() { | ||
return foo() + 100; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
int foo() { | ||
return 12; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# comment | ||
|
||
foo/bar | ||
|
||
baz/qux |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foobar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include/before | ||
include basic*.conf | ||
include/after |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require "spec" | ||
|
||
SPEC_CRYSTAL_LOADER_LIB_PATH = File.join(SPEC_TEMPFILE_PATH, "loader") | ||
|
||
def build_c_dynlib(c_filename, target_dir = SPEC_CRYSTAL_LOADER_LIB_PATH) | ||
obj_ext = Crystal::Loader::SHARED_LIBRARY_EXTENSION | ||
o_filename = File.join(target_dir, "lib#{File.basename(c_filename).rchop(".c")}#{obj_ext}") | ||
|
||
{% if flag?(:msvc) %} | ||
`cl.exe /nologo /LD #{Process.quote(c_filename)} #{Process.quote("/Fo#{o_filename}")}`.should be_truthy | ||
{% else %} | ||
`#{ENV["CC"]? || "cc"} -shared #{Process.quote(c_filename)} -o #{Process.quote(o_filename)}`.should be_truthy | ||
{% end %} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
{% skip_file unless flag?(:unix) %} | ||
|
||
require "./spec_helper" | ||
require "../spec_helper" | ||
require "../../support/env" | ||
require "compiler/crystal/loader" | ||
|
||
describe Crystal::Loader do | ||
describe ".parse" do | ||
it "parses directory paths" do | ||
loader = Crystal::Loader.parse(["-L", "/foo/bar/baz", "--library-path", "qux"], search_paths: [] of String) | ||
loader.search_paths.should eq ["/foo/bar/baz", "qux"] | ||
end | ||
|
||
it "parses static" do | ||
expect_raises(Crystal::Loader::LoadError, "static libraries are not supported by Crystal's runtime loader") do | ||
Crystal::Loader.parse(["-static"], search_paths: [] of String) | ||
end | ||
end | ||
|
||
it "parses library names" do | ||
expect_raises(Crystal::Loader::LoadError, "cannot find -lfoobar") do | ||
Crystal::Loader.parse(["-l", "foobar"], search_paths: [] of String) | ||
end | ||
expect_raises(Crystal::Loader::LoadError, "cannot find -lfoobar") do | ||
Crystal::Loader.parse(["--library", "foobar"], search_paths: [] of String) | ||
end | ||
end | ||
|
||
it "parses file paths" do | ||
expect_raises(Crystal::Loader::LoadError, /#{Dir.current}\/foobar\.o.+(No such file or directory|image not found)/) do | ||
Crystal::Loader.parse(["foobar.o"], search_paths: [] of String) | ||
end | ||
expect_raises(Crystal::Loader::LoadError, /#{Dir.current}\/foo\/bar\.o.+(No such file or directory|image not found)/) do | ||
Crystal::Loader.parse(["-l", "foo/bar.o"], search_paths: [] of String) | ||
end | ||
end | ||
end | ||
|
||
describe ".default_search_paths" do | ||
it "LD_LIBRARY_PATH" do | ||
with_env "LD_LIBRARY_PATH": "ld1::ld2", "DYLD_LIBRARY_PATH": nil do | ||
search_paths = Crystal::Loader.default_search_paths | ||
{% if flag?(:darwin) %} | ||
search_paths.should eq ["/usr/lib", "/usr/local/lib"] | ||
{% else %} | ||
search_paths[0, 2].should eq ["ld1", "ld2"] | ||
search_paths[-2..].should eq ["/lib", "/usr/lib"] | ||
{% end %} | ||
end | ||
end | ||
|
||
it "DYLD_LIBRARY_PATH" do | ||
with_env "DYLD_LIBRARY_PATH": "ld1::ld2", "LD_LIBRARY_PATH": nil do | ||
search_paths = Crystal::Loader.default_search_paths | ||
{% if flag?(:darwin) %} | ||
search_paths[0, 2].should eq ["ld1", "ld2"] | ||
search_paths[-2..].should eq ["/usr/lib", "/usr/local/lib"] | ||
{% else %} | ||
search_paths[-2..].should eq ["/lib", "/usr/lib"] | ||
{% end %} | ||
end | ||
end | ||
end | ||
|
||
describe ".read_ld_conf" do | ||
it "basic" do | ||
ary = [] of String | ||
Crystal::Loader.read_ld_conf(ary, compiler_datapath("loader/ld.so/basic.conf")) | ||
ary.should eq ["foo/bar", "baz/qux"] | ||
end | ||
|
||
it "with include" do | ||
ary = [] of String | ||
Crystal::Loader.read_ld_conf(ary, compiler_datapath("loader/ld.so/include.conf")) | ||
ary[0].should eq "include/before" | ||
ary[-1].should eq "include/after" | ||
# the order between basic.conf and basic2.conf is system-dependent | ||
ary[1..-2].sort.should eq ["baz/qux", "foo/bar", "foobar"] | ||
end | ||
end | ||
|
||
describe "dynlib" do | ||
before_all do | ||
FileUtils.mkdir_p(SPEC_CRYSTAL_LOADER_LIB_PATH) | ||
build_c_dynlib(compiler_datapath("loader", "foo.c")) | ||
end | ||
|
||
after_all do | ||
FileUtils.rm_rf(SPEC_CRYSTAL_LOADER_LIB_PATH) | ||
end | ||
|
||
describe "#load_file" do | ||
it "finds function symbol" do | ||
loader = Crystal::Loader.new([] of String) | ||
lib_handle = loader.load_file(File.join(SPEC_CRYSTAL_LOADER_LIB_PATH, "libfoo#{Crystal::Loader::SHARED_LIBRARY_EXTENSION}")) | ||
lib_handle.should_not be_nil | ||
loader.find_symbol?("foo").should_not be_nil | ||
ensure | ||
loader.close_all if loader | ||
end | ||
end | ||
|
||
describe "#load_library" do | ||
it "library name" do | ||
loader = Crystal::Loader.new([SPEC_CRYSTAL_LOADER_LIB_PATH] of String) | ||
lib_handle = loader.load_library("foo") | ||
lib_handle.should_not be_nil | ||
loader.find_symbol?("foo").should_not be_nil | ||
ensure | ||
loader.close_all if loader | ||
end | ||
|
||
it "full path" do | ||
loader = Crystal::Loader.new([] of String) | ||
lib_handle = loader.load_library(File.join(SPEC_CRYSTAL_LOADER_LIB_PATH, "libfoo#{Crystal::Loader::SHARED_LIBRARY_EXTENSION}")) | ||
lib_handle.should_not be_nil | ||
loader.find_symbol?("foo").should_not be_nil | ||
ensure | ||
loader.close_all if loader | ||
end | ||
|
||
{% unless flag?(:darwin) %} | ||
# FIXME: bar.c doesn't compile on darwin | ||
it "does not implicitly find dependencies" do | ||
build_c_dynlib(compiler_datapath("loader", "bar.c")) | ||
loader = Crystal::Loader.new([SPEC_CRYSTAL_LOADER_LIB_PATH] of String) | ||
lib_handle = loader.load_library("bar") | ||
lib_handle.should_not be_nil | ||
loader.find_symbol?("bar").should_not be_nil | ||
loader.find_symbol?("foo").should be_nil | ||
ensure | ||
loader.close_all if loader | ||
end | ||
{% end %} | ||
end | ||
|
||
it "does not find global symbols" do | ||
loader = Crystal::Loader.new([] of String) | ||
loader.find_symbol?("__crystal_main").should be_nil | ||
end | ||
|
||
it "validate that lib handles are properly closed" do | ||
loader = Crystal::Loader.new([] of String) | ||
expect_raises(Crystal::Loader::LoadError, "undefined reference to `foo'") do | ||
loader.find_symbol("foo") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,115 @@ | ||||||||||
require "option_parser" | ||||||||||
|
||||||||||
# This loader component imitates the behaviour of `ld.so` for linking and loading | ||||||||||
# dynamic libraries at runtime. | ||||||||||
# | ||||||||||
# It provides a tool for interpreted mode, where the compiler does not generate | ||||||||||
# an object file that could be passed to the linker. Instead, `Crystal::Loader` | ||||||||||
# takes over the job of discovering libraries, loading them into memory and | ||||||||||
# finding symbols inside them. | ||||||||||
# | ||||||||||
# See system-specific implementations in ./loader for details. | ||||||||||
# | ||||||||||
# A Windows implementation is not yet available. | ||||||||||
class Crystal::Loader | ||||||||||
class LoadError < Exception | ||||||||||
end | ||||||||||
|
||||||||||
def self.new(search_paths : Array(String), libnames : Array(String), file_paths : Array(String)) : self | ||||||||||
loader = new(search_paths) | ||||||||||
|
||||||||||
file_paths.each do |path| | ||||||||||
loader.load_file(::Path[path].expand) | ||||||||||
end | ||||||||||
libnames.each do |libname| | ||||||||||
loader.load_library(libname) | ||||||||||
end | ||||||||||
loader | ||||||||||
end | ||||||||||
|
||||||||||
getter search_paths : Array(String) | ||||||||||
|
||||||||||
def initialize(@search_paths : Array(String)) | ||||||||||
@handles = [] of Handle | ||||||||||
end | ||||||||||
|
||||||||||
# def find_symbol?(name : String) : Handle? | ||||||||||
# raise NotImplementedError.new("find_symbol?") | ||||||||||
# end | ||||||||||
|
||||||||||
# def load_file(path : String | ::Path) : Handle | ||||||||||
# raise NotImplementedError.new("load_file") | ||||||||||
# end | ||||||||||
|
||||||||||
# private def open_library(path : String) : Handle | ||||||||||
# raise NotImplementedError.new("open_library") | ||||||||||
# end | ||||||||||
|
||||||||||
# def self.default_search_paths : Array(String) | ||||||||||
# raise NotImplementedError.new("close_all") | ||||||||||
# end | ||||||||||
|
||||||||||
def find_symbol(name : String) : Handle | ||||||||||
find_symbol?(name) || raise LoadError.new "undefined reference to `#{name}'" | ||||||||||
end | ||||||||||
|
||||||||||
def load_library(libname : String) : Handle | ||||||||||
load_library?(libname) || raise LoadError.new "cannot find -l#{libname}" | ||||||||||
end | ||||||||||
|
||||||||||
def load_library?(libname : String) : Handle? | ||||||||||
if ::Path::SEPARATORS.any? { |separator| libname.includes?(separator) } | ||||||||||
return load_file(::Path[libname].expand) | ||||||||||
end | ||||||||||
|
||||||||||
find_library_path(libname) do |library_path| | ||||||||||
handle = load_file?(library_path) | ||||||||||
return handle if handle | ||||||||||
end | ||||||||||
|
||||||||||
nil | ||||||||||
end | ||||||||||
|
||||||||||
def load_file?(path : String | ::Path) : Handle? | ||||||||||
handle = open_library(path.to_s) | ||||||||||
return nil unless handle | ||||||||||
|
||||||||||
@handles << handle | ||||||||||
handle | ||||||||||
end | ||||||||||
|
||||||||||
private def find_library_path(libname) | ||||||||||
each_library_path(libname) do |path| | ||||||||||
if File.exists?(path) | ||||||||||
yield path | ||||||||||
end | ||||||||||
Comment on lines
+83
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small stylistic suggestion:
Suggested change
|
||||||||||
end | ||||||||||
end | ||||||||||
|
||||||||||
private def each_library_path(libname) | ||||||||||
@search_paths.each do |directory| | ||||||||||
yield "#{directory}/lib#{libname}#{SHARED_LIBRARY_EXTENSION}" | ||||||||||
end | ||||||||||
end | ||||||||||
|
||||||||||
def close_all : Nil | ||||||||||
end | ||||||||||
|
||||||||||
def finalize | ||||||||||
close_all | ||||||||||
end | ||||||||||
|
||||||||||
SHARED_LIBRARY_EXTENSION = {% if flag?(:darwin) %} | ||||||||||
straight-shoota marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
".dylib" | ||||||||||
{% elsif flag?(:unix) %} | ||||||||||
".so" | ||||||||||
{% elsif flag?(:windows) %} | ||||||||||
".dll" | ||||||||||
{% else %} | ||||||||||
{% raise "Can't load dynamic libraries" %} | ||||||||||
{% end %} | ||||||||||
end | ||||||||||
|
||||||||||
{% if flag?(:unix) %} | ||||||||||
require "./loader/unix" | ||||||||||
{% end %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commented code should be removed, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added it there to define the interface that is expected to be implemented in the platform-specific files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do the same thing in with platform-specific implementations in
Crystal::System
.