2017-09-12 07:52:23 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-13 05:46:16 -04:00
|
|
|
require 'test/unit'
|
2010-05-06 02:59:24 -04:00
|
|
|
require 'fiddle'
|
|
|
|
|
|
|
|
# FIXME: this is stolen from DL and needs to be refactored.
|
|
|
|
|
|
|
|
libc_so = libm_so = nil
|
|
|
|
|
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /cygwin/
|
|
|
|
libc_so = "cygwin1.dll"
|
|
|
|
libm_so = "cygwin1.dll"
|
2019-09-09 07:24:03 -04:00
|
|
|
when /android/
|
|
|
|
libdir = '/system/lib'
|
|
|
|
if [0].pack('L!').size == 8
|
|
|
|
libdir = '/system/lib64'
|
|
|
|
end
|
|
|
|
libc_so = File.join(libdir, "libc.so")
|
|
|
|
libm_so = File.join(libdir, "libm.so")
|
2020-01-22 21:59:37 -05:00
|
|
|
when /linux-musl/
|
|
|
|
Dir.glob('/lib/ld-musl-*.so.1') do |ld|
|
|
|
|
libc_so = libm_so = ld
|
|
|
|
end
|
2010-05-06 02:59:24 -04:00
|
|
|
when /linux/
|
|
|
|
libdir = '/lib'
|
2019-06-13 03:08:22 -04:00
|
|
|
case RbConfig::SIZEOF['void*']
|
2010-05-06 02:59:24 -04:00
|
|
|
when 4
|
|
|
|
# 32-bit ruby
|
2019-12-05 00:17:16 -05:00
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /armv\w+-linux/
|
|
|
|
# In the ARM 32-bit libc package such as libc6:armhf libc6:armel,
|
|
|
|
# libc.so and libm.so are installed to /lib/arm-linux-gnu*.
|
|
|
|
# It's not installed to /lib32.
|
2020-03-31 23:37:01 -04:00
|
|
|
dir, = Dir.glob('/lib/arm-linux-gnu*')
|
|
|
|
libdir = dir if dir && File.directory?(dir)
|
2019-12-05 00:17:16 -05:00
|
|
|
else
|
|
|
|
libdir = '/lib32' if File.directory? '/lib32'
|
|
|
|
end
|
2010-05-06 02:59:24 -04:00
|
|
|
when 8
|
|
|
|
# 64-bit ruby
|
|
|
|
libdir = '/lib64' if File.directory? '/lib64'
|
|
|
|
end
|
2020-03-31 21:18:23 -04:00
|
|
|
|
|
|
|
# Handle musl libc
|
2020-03-31 23:37:01 -04:00
|
|
|
libc_so, = Dir.glob(File.join(libdir, "libc.musl*.so*"))
|
|
|
|
if libc_so
|
|
|
|
libm_so = libc_so
|
2020-03-31 21:18:23 -04:00
|
|
|
else
|
|
|
|
# glibc
|
2021-03-10 16:48:00 -05:00
|
|
|
libc_so = "libc.so.6"
|
|
|
|
libm_so = "libm.so.6"
|
2020-03-31 21:18:23 -04:00
|
|
|
end
|
2010-05-06 02:59:24 -04:00
|
|
|
when /mingw/, /mswin/
|
|
|
|
require "rbconfig"
|
2015-11-21 01:36:05 -05:00
|
|
|
crtname = RbConfig::CONFIG["RUBY_SO_NAME"][/msvc\w+/] || 'ucrtbase'
|
2015-11-20 19:20:53 -05:00
|
|
|
libc_so = libm_so = "#{crtname}.dll"
|
2010-05-06 02:59:24 -04:00
|
|
|
when /darwin/
|
2020-09-08 07:38:01 -04:00
|
|
|
libc_so = libm_so = "/usr/lib/libSystem.B.dylib"
|
2010-05-06 02:59:24 -04:00
|
|
|
when /kfreebsd/
|
|
|
|
libc_so = "/lib/libc.so.0.1"
|
|
|
|
libm_so = "/lib/libm.so.1"
|
2013-09-28 01:43:50 -04:00
|
|
|
when /gnu/ #GNU/Hurd
|
|
|
|
libc_so = "/lib/libc.so.0.3"
|
|
|
|
libm_so = "/lib/libm.so.6"
|
2013-02-08 11:27:17 -05:00
|
|
|
when /mirbsd/
|
|
|
|
libc_so = "/usr/lib/libc.so.41.10"
|
|
|
|
libm_so = "/usr/lib/libm.so.7.0"
|
2014-01-14 20:43:29 -05:00
|
|
|
when /freebsd/
|
|
|
|
libc_so = "/lib/libc.so.7"
|
|
|
|
libm_so = "/lib/libm.so.5"
|
2010-05-06 02:59:24 -04:00
|
|
|
when /bsd|dragonfly/
|
|
|
|
libc_so = "/usr/lib/libc.so"
|
|
|
|
libm_so = "/usr/lib/libm.so"
|
2011-08-08 23:36:18 -04:00
|
|
|
when /solaris/
|
|
|
|
libdir = '/lib'
|
2019-06-13 03:08:22 -04:00
|
|
|
case RbConfig::SIZEOF['void*']
|
2011-08-08 23:36:18 -04:00
|
|
|
when 4
|
|
|
|
# 32-bit ruby
|
|
|
|
libdir = '/lib' if File.directory? '/lib'
|
|
|
|
when 8
|
|
|
|
# 64-bit ruby
|
|
|
|
libdir = '/lib/64' if File.directory? '/lib/64'
|
|
|
|
end
|
|
|
|
libc_so = File.join(libdir, "libc.so")
|
|
|
|
libm_so = File.join(libdir, "libm.so")
|
2010-08-25 21:16:53 -04:00
|
|
|
when /aix/
|
|
|
|
pwd=Dir.pwd
|
|
|
|
libc_so = libm_so = "#{pwd}/libaixdltest.so"
|
|
|
|
unless File.exist? libc_so
|
|
|
|
cobjs=%w!strcpy.o!
|
|
|
|
mobjs=%w!floats.o sin.o!
|
|
|
|
funcs=%w!sin sinf strcpy strncpy!
|
|
|
|
expfile='dltest.exp'
|
|
|
|
require 'tmpdir'
|
2020-11-16 21:12:09 -05:00
|
|
|
Dir.mktmpdir do |_dir|
|
2010-08-25 21:16:53 -04:00
|
|
|
begin
|
2020-11-16 21:12:09 -05:00
|
|
|
Dir.chdir _dir
|
2010-08-25 21:16:53 -04:00
|
|
|
%x!/usr/bin/ar x /usr/lib/libc.a #{cobjs.join(' ')}!
|
|
|
|
%x!/usr/bin/ar x /usr/lib/libm.a #{mobjs.join(' ')}!
|
|
|
|
%x!echo "#{funcs.join("\n")}\n" > #{expfile}!
|
|
|
|
require 'rbconfig'
|
|
|
|
if RbConfig::CONFIG["GCC"] = 'yes'
|
|
|
|
lflag='-Wl,'
|
|
|
|
else
|
|
|
|
lflag=''
|
|
|
|
end
|
|
|
|
flags="#{lflag}-bE:#{expfile} #{lflag}-bnoentry -lm"
|
|
|
|
%x!#{RbConfig::CONFIG["LDSHARED"]} -o #{libc_so} #{(cobjs+mobjs).join(' ')} #{flags}!
|
|
|
|
ensure
|
|
|
|
Dir.chdir pwd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-29 17:47:24 -04:00
|
|
|
when /haiku/
|
|
|
|
libdir = '/system/lib'
|
|
|
|
case [0].pack('L!').size
|
|
|
|
when 4
|
|
|
|
# 32-bit ruby
|
|
|
|
libdir = '/system/lib/x86' if File.directory? '/system/lib/x86'
|
|
|
|
when 8
|
|
|
|
# 64-bit ruby
|
|
|
|
libdir = '/system/lib/' if File.directory? '/system/lib/'
|
|
|
|
end
|
|
|
|
libc_so = File.join(libdir, "libroot.so")
|
|
|
|
libm_so = File.join(libdir, "libroot.so")
|
2010-05-06 02:59:24 -04:00
|
|
|
else
|
|
|
|
libc_so = ARGV[0] if ARGV[0] && ARGV[0][0] == ?/
|
|
|
|
libm_so = ARGV[1] if ARGV[1] && ARGV[1][0] == ?/
|
|
|
|
if( !(libc_so && libm_so) )
|
|
|
|
$stderr.puts("libc and libm not found: #{$0} <libc> <libm>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
libc_so = nil if !libc_so || (libc_so[0] == ?/ && !File.file?(libc_so))
|
|
|
|
libm_so = nil if !libm_so || (libm_so[0] == ?/ && !File.file?(libm_so))
|
|
|
|
|
2020-12-08 07:58:08 -05:00
|
|
|
# macOS 11.0+ removed libSystem.B.dylib from /usr/lib. But It works with dlopen.
|
|
|
|
if RUBY_PLATFORM =~ /darwin/
|
|
|
|
libc_so = libm_so = "/usr/lib/libSystem.B.dylib"
|
|
|
|
end
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
if !libc_so || !libm_so
|
|
|
|
ruby = EnvUtil.rubybin
|
2019-12-05 00:17:16 -05:00
|
|
|
# When the ruby binary is 32-bit and the host is 64-bit,
|
|
|
|
# `ldd ruby` outputs "not a dynamic executable" message.
|
|
|
|
# libc_so and libm_so are not set.
|
2010-05-06 02:59:24 -04:00
|
|
|
ldd = `ldd #{ruby}`
|
|
|
|
#puts ldd
|
|
|
|
libc_so = $& if !libc_so && %r{/\S*/libc\.so\S*} =~ ldd
|
|
|
|
libm_so = $& if !libm_so && %r{/\S*/libm\.so\S*} =~ ldd
|
|
|
|
#p [libc_so, libm_so]
|
|
|
|
end
|
|
|
|
|
|
|
|
Fiddle::LIBC_SO = libc_so
|
|
|
|
Fiddle::LIBM_SO = libm_so
|
|
|
|
|
|
|
|
module Fiddle
|
2017-07-13 05:46:16 -04:00
|
|
|
class TestCase < Test::Unit::TestCase
|
2010-05-06 02:59:24 -04:00
|
|
|
def setup
|
2012-11-27 14:54:50 -05:00
|
|
|
@libc = Fiddle.dlopen(LIBC_SO)
|
|
|
|
@libm = Fiddle.dlopen(LIBM_SO)
|
2010-05-06 02:59:24 -04:00
|
|
|
end
|
2013-04-11 02:35:43 -04:00
|
|
|
|
|
|
|
def teardown
|
|
|
|
if /linux/ =~ RUBY_PLATFORM
|
|
|
|
GC.start
|
2013-04-11 03:53:26 -04:00
|
|
|
end
|
2013-04-11 02:35:43 -04:00
|
|
|
end
|
2010-05-06 02:59:24 -04:00
|
|
|
end
|
|
|
|
end
|