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

Static build support #41

Merged
merged 4 commits into from
Oct 5, 2017
Merged
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
11 changes: 5 additions & 6 deletions lib/thermite/cargo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,16 @@ def cargo_recommended_msg
private

def cargo_rustc_args
args = []
unless config.dynamic_linker_flags == '' || config.target_os == 'mingw32'
args.push(
if config.dynamic_linker_flags == '' || config.target_os == 'mingw32'
[]
else
[
'--lib',
'--',
'-C',
"link-args=#{config.dynamic_linker_flags}"
)
]
end

args
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less variable mutation.

end
end
end
24 changes: 20 additions & 4 deletions lib/thermite/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,31 @@ def library_name
end

#
# The basename of the Rust shared library.
# The basename of the shared library built by Cargo.
#
def shared_library
@shared_library ||= begin
def cargo_shared_library
@cargo_shared_library ||= begin
filename = "#{library_name}.#{shared_ext}"
filename = "lib#{filename}" unless Gem.win_platform?
filename
end
end

#
# The basename of the Rust shared library, as installed in the {#ruby_extension_path}.
#
def shared_library
@shared_library ||= "#{library_name}.so"
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently Ruby always expects the file extension to be .so regardless of OS. I guess that makes it simpler.

end

#
# Return the basename of the tarball generated by the `thermite:tarball` Rake task, given a
# package `version`.
#
def tarball_filename(version)
"#{library_name}-#{version}-#{ruby_version}-#{target_os}-#{target_arch}.tar.gz"
static = static_extension? ? '-static' : ''

"#{library_name}-#{version}-#{ruby_version}-#{target_os}-#{target_arch}#{static}.tar.gz"
end

#
Expand Down Expand Up @@ -261,6 +270,13 @@ def dynamic_linker_flags
@dynamic_linker_flags ||= RbConfig::CONFIG['DLDFLAGS'].strip
end

#
# Whether to use a statically linked extension.
#
def static_extension?
ENV.key?('RUBY_STATIC') || RbConfig::CONFIG['ENABLE_SHARED'] == 'no'
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end

private

def dlext
Expand Down
4 changes: 2 additions & 2 deletions lib/thermite/tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def define_build_task
if cargo
profile = ENV.fetch('CARGO_PROFILE', 'release')
run_cargo_rustc(profile)
FileUtils.cp(config.cargo_target_path(profile, config.shared_library),
config.ruby_path('lib'))
FileUtils.cp(config.cargo_target_path(profile, config.cargo_shared_library),
config.ruby_extension_path)
elsif !download_binary_from_custom_uri && !download_binary_from_github_release
inform_user_about_cargo
end
Expand Down
51 changes: 45 additions & 6 deletions test/lib/thermite/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,40 @@ def test_shared_library
config.stubs(:library_name).returns('foobar')
config.stubs(:shared_ext).returns('ext')
Gem.stubs(:win_platform?).returns(false)
assert_equal 'libfoobar.ext', config.shared_library
assert_equal 'foobar.so', config.shared_library
end

def test_shared_library_windows
config.stubs(:library_name).returns('foobar')
config.stubs(:shared_ext).returns('ext')
Gem.stubs(:win_platform?).returns(true)
assert_equal 'foobar.ext', config.shared_library
assert_equal 'foobar.so', config.shared_library
end

def test_tarball_filename
def test_cargo_shared_library
config.stubs(:library_name).returns('foobar')
config.stubs(:ruby_version).returns('ruby12')
config.stubs(:target_os).returns('c64')
config.stubs(:target_arch).returns('z80')
config.stubs(:shared_ext).returns('ext')
Gem.stubs(:win_platform?).returns(false)
assert_equal 'libfoobar.ext', config.cargo_shared_library
end

def test_cargo_shared_library_windows
config.stubs(:library_name).returns('foobar')
config.stubs(:shared_ext).returns('ext')
Gem.stubs(:win_platform?).returns(true)
assert_equal 'foobar.ext', config.cargo_shared_library
end

def test_tarball_filename
stub_tarball_filename_params(false)
assert_equal 'foobar-0.1.2-ruby12-c64-z80.tar.gz', config.tarball_filename('0.1.2')
end

def test_tarball_filename_with_static_extension
stub_tarball_filename_params(true)
assert_equal 'foobar-0.1.2-ruby12-c64-z80-static.tar.gz', config.tarball_filename('0.1.2')
end

def test_default_ruby_toplevel_dir
FileUtils.stubs(:pwd).returns('/tmp/foobar')
assert_equal '/tmp/foobar', config.ruby_toplevel_dir
Expand Down Expand Up @@ -180,6 +196,21 @@ def test_toml_config
assert_equal expected, config(cargo_project_path: fixtures_path('config')).toml_config
end

def test_static_extension_sans_env_var
ENV.stubs(:key?).with('RUBY_STATIC').returns(false)
RbConfig::CONFIG.stubs(:[]).with('ENABLE_SHARED').returns('yes')
refute config.static_extension?

RbConfig::CONFIG.stubs(:[]).with('ENABLE_SHARED').returns('no')
assert config.static_extension?
end

def test_static_extension_with_env_var
ENV.stubs(:key?).with('RUBY_STATIC').returns(true)
RbConfig::CONFIG.stubs(:[]).with('ENABLE_SHARED').returns('yes')
assert config.static_extension?
end

private

def config(options = {})
Expand All @@ -189,5 +220,13 @@ def config(options = {})
def described_class
Thermite::Config
end

def stub_tarball_filename_params(static_extension)
config.stubs(:library_name).returns('foobar')
config.stubs(:ruby_version).returns('ruby12')
config.stubs(:target_os).returns('c64')
config.stubs(:target_arch).returns('z80')
config.stubs(:static_extension?).returns(static_extension)
end
end
end