Skip to content

Commit 348a911

Browse files
committed
fix(native tracer): handle cross-platform extension
1 parent eeb89ee commit 348a911

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

gems/native-tracer/lib/native_trace.rb

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Simple utility loading the native tracer extension and executing a program.
44

55
require 'optparse'
6+
require 'fileutils'
7+
require 'rbconfig'
68

79
options = {}
810
parser = OptionParser.new do |opts|
@@ -27,10 +29,20 @@
2729

2830
# Path to the compiled native extension
2931
ext_dir = File.expand_path('../ext/native_tracer/target/release', __dir__)
30-
target_path = File.join(ext_dir, 'codetracer_ruby_recorder.so')
32+
dlext = RbConfig::CONFIG['DLEXT']
33+
target_path = File.join(ext_dir, "codetracer_ruby_recorder.#{dlext}")
3134
unless File.exist?(target_path)
32-
alt_path = File.join(ext_dir, 'libcodetracer_ruby_recorder.so')
33-
target_path = alt_path if File.exist?(alt_path)
35+
extensions = %w[so bundle dylib dll]
36+
alt_path = extensions
37+
.map { |ext| File.join(ext_dir, "libcodetracer_ruby_recorder.#{ext}") }
38+
.find { |path| File.exist?(path) }
39+
if alt_path
40+
begin
41+
File.symlink(alt_path, target_path)
42+
rescue StandardError
43+
FileUtils.cp(alt_path, target_path)
44+
end
45+
end
3446
end
3547

3648
recorder = nil

test/test_tracer.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,18 @@ def program_args(base)
5050
base = File.basename(fixture, '_trace.json')
5151
define_method("test_#{base}") do
5252
pure_trace, pure_out = run_trace('gems/pure-ruby-tracer/lib/trace.rb', "#{base}.rb", *program_args(base))
53-
_native_trace, native_out = run_trace('gems/native-tracer/lib/native_trace.rb', "#{base}.rb", *program_args(base))
53+
native_trace, native_out = run_trace('gems/native-tracer/lib/native_trace.rb', "#{base}.rb", *program_args(base))
54+
5455
assert_equal expected_trace("#{base}.rb"), pure_trace
5556
expected = expected_output("#{base}.rb")
5657
assert_equal expected, pure_out
5758
assert_equal expected, native_out
59+
60+
# The native tracer doesn't yet match the pure Ruby tracer, but it should
61+
# still generate some trace output. Fail early if the trace is empty to
62+
# ensure the extension was loaded correctly.
63+
refute_nil native_trace, 'native tracer did not produce a trace file'
64+
refute_empty native_trace, 'native tracer produced an empty trace'
5865
end
5966
end
6067
end

0 commit comments

Comments
 (0)