Skip to content

Commit 145b9bf

Browse files
authored
Merge pull request #41 from malept/static-build-support
Static build support
2 parents 981e551 + d7e226e commit 145b9bf

File tree

4 files changed

+72
-18
lines changed

4 files changed

+72
-18
lines changed

lib/thermite/cargo.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,16 @@ def cargo_recommended_msg
110110
private
111111

112112
def cargo_rustc_args
113-
args = []
114-
unless config.dynamic_linker_flags == '' || config.target_os == 'mingw32'
115-
args.push(
113+
if config.dynamic_linker_flags == '' || config.target_os == 'mingw32'
114+
[]
115+
else
116+
[
116117
'--lib',
117118
'--',
118119
'-C',
119120
"link-args=#{config.dynamic_linker_flags}"
120-
)
121+
]
121122
end
122-
123-
args
124123
end
125124
end
126125
end

lib/thermite/config.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,31 @@ def library_name
108108
end
109109

110110
#
111-
# The basename of the Rust shared library.
111+
# The basename of the shared library built by Cargo.
112112
#
113-
def shared_library
114-
@shared_library ||= begin
113+
def cargo_shared_library
114+
@cargo_shared_library ||= begin
115115
filename = "#{library_name}.#{shared_ext}"
116116
filename = "lib#{filename}" unless Gem.win_platform?
117117
filename
118118
end
119119
end
120120

121+
#
122+
# The basename of the Rust shared library, as installed in the {#ruby_extension_path}.
123+
#
124+
def shared_library
125+
@shared_library ||= "#{library_name}.so"
126+
end
127+
121128
#
122129
# Return the basename of the tarball generated by the `thermite:tarball` Rake task, given a
123130
# package `version`.
124131
#
125132
def tarball_filename(version)
126-
"#{library_name}-#{version}-#{ruby_version}-#{target_os}-#{target_arch}.tar.gz"
133+
static = static_extension? ? '-static' : ''
134+
135+
"#{library_name}-#{version}-#{ruby_version}-#{target_os}-#{target_arch}#{static}.tar.gz"
127136
end
128137

129138
#
@@ -261,6 +270,13 @@ def dynamic_linker_flags
261270
@dynamic_linker_flags ||= RbConfig::CONFIG['DLDFLAGS'].strip
262271
end
263272

273+
#
274+
# Whether to use a statically linked extension.
275+
#
276+
def static_extension?
277+
ENV.key?('RUBY_STATIC') || RbConfig::CONFIG['ENABLE_SHARED'] == 'no'
278+
end
279+
264280
private
265281

266282
def dlext

lib/thermite/tasks.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ def define_build_task
122122
if cargo
123123
profile = ENV.fetch('CARGO_PROFILE', 'release')
124124
run_cargo_rustc(profile)
125-
FileUtils.cp(config.cargo_target_path(profile, config.shared_library),
126-
config.ruby_path('lib'))
125+
FileUtils.cp(config.cargo_target_path(profile, config.cargo_shared_library),
126+
config.ruby_extension_path)
127127
elsif !download_binary_from_custom_uri && !download_binary_from_github_release
128128
inform_user_about_cargo
129129
end

test/lib/thermite/config_test.rb

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,40 @@ def test_shared_library
7474
config.stubs(:library_name).returns('foobar')
7575
config.stubs(:shared_ext).returns('ext')
7676
Gem.stubs(:win_platform?).returns(false)
77-
assert_equal 'libfoobar.ext', config.shared_library
77+
assert_equal 'foobar.so', config.shared_library
7878
end
7979

8080
def test_shared_library_windows
8181
config.stubs(:library_name).returns('foobar')
8282
config.stubs(:shared_ext).returns('ext')
8383
Gem.stubs(:win_platform?).returns(true)
84-
assert_equal 'foobar.ext', config.shared_library
84+
assert_equal 'foobar.so', config.shared_library
8585
end
8686

87-
def test_tarball_filename
87+
def test_cargo_shared_library
8888
config.stubs(:library_name).returns('foobar')
89-
config.stubs(:ruby_version).returns('ruby12')
90-
config.stubs(:target_os).returns('c64')
91-
config.stubs(:target_arch).returns('z80')
89+
config.stubs(:shared_ext).returns('ext')
90+
Gem.stubs(:win_platform?).returns(false)
91+
assert_equal 'libfoobar.ext', config.cargo_shared_library
92+
end
93+
94+
def test_cargo_shared_library_windows
95+
config.stubs(:library_name).returns('foobar')
96+
config.stubs(:shared_ext).returns('ext')
97+
Gem.stubs(:win_platform?).returns(true)
98+
assert_equal 'foobar.ext', config.cargo_shared_library
99+
end
100+
101+
def test_tarball_filename
102+
stub_tarball_filename_params(false)
92103
assert_equal 'foobar-0.1.2-ruby12-c64-z80.tar.gz', config.tarball_filename('0.1.2')
93104
end
94105

106+
def test_tarball_filename_with_static_extension
107+
stub_tarball_filename_params(true)
108+
assert_equal 'foobar-0.1.2-ruby12-c64-z80-static.tar.gz', config.tarball_filename('0.1.2')
109+
end
110+
95111
def test_default_ruby_toplevel_dir
96112
FileUtils.stubs(:pwd).returns('/tmp/foobar')
97113
assert_equal '/tmp/foobar', config.ruby_toplevel_dir
@@ -180,6 +196,21 @@ def test_toml_config
180196
assert_equal expected, config(cargo_project_path: fixtures_path('config')).toml_config
181197
end
182198

199+
def test_static_extension_sans_env_var
200+
ENV.stubs(:key?).with('RUBY_STATIC').returns(false)
201+
RbConfig::CONFIG.stubs(:[]).with('ENABLE_SHARED').returns('yes')
202+
refute config.static_extension?
203+
204+
RbConfig::CONFIG.stubs(:[]).with('ENABLE_SHARED').returns('no')
205+
assert config.static_extension?
206+
end
207+
208+
def test_static_extension_with_env_var
209+
ENV.stubs(:key?).with('RUBY_STATIC').returns(true)
210+
RbConfig::CONFIG.stubs(:[]).with('ENABLE_SHARED').returns('yes')
211+
assert config.static_extension?
212+
end
213+
183214
private
184215

185216
def config(options = {})
@@ -189,5 +220,13 @@ def config(options = {})
189220
def described_class
190221
Thermite::Config
191222
end
223+
224+
def stub_tarball_filename_params(static_extension)
225+
config.stubs(:library_name).returns('foobar')
226+
config.stubs(:ruby_version).returns('ruby12')
227+
config.stubs(:target_os).returns('c64')
228+
config.stubs(:target_arch).returns('z80')
229+
config.stubs(:static_extension?).returns(static_extension)
230+
end
192231
end
193232
end

0 commit comments

Comments
 (0)