Skip to content

Commit a2454be

Browse files
authored
Merge pull request #42 from mrkn/autoloader_windows
Tweak autoloader for Windows
2 parents 3cd5042 + a6d03ca commit a2454be

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

lib/numo/linalg/autoloader.rb

+47-5
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,30 @@ def load_library
6464
end
6565

6666
def detect_library_extension
67-
case RbConfig::CONFIG['host_os']
68-
when /mswin|msys|mingw|cygwin/
69-
'dll'
67+
# Ruby >= 2.5 provides SOEXT in rbconfig
68+
so_ext = RbConfig::CONFIG["SOEXT"]
69+
return so_ext if so_ext
70+
71+
return 'dll' if windows?
72+
73+
# For Ruby < 2.5, we use RUBY_PLATFORM
74+
case RUBY_PLATFORM
7075
when /darwin|mac os/
7176
'dylib'
7277
else
7378
'so'
7479
end
7580
end
7681

82+
def windows?
83+
case RUBY_PLATFORM
84+
when /mswin|msys|mingw|cygwin/
85+
true
86+
else
87+
false
88+
end
89+
end
90+
7791
def select_dirs(dirs)
7892
dirs.select!{|d| Dir.exist?(d)}
7993
end
@@ -82,8 +96,36 @@ def find_libs(lib_names, lib_dirs)
8296
lib_ext = detect_library_extension
8397
lib_arr = lib_names.map do |l|
8498
x = nil
85-
lib_dirs.each do |d|
86-
break if x = Dir.glob("#{d}/lib#{l}{,64}.#{lib_ext}{,.*}").last
99+
if windows?
100+
# On Windows, try to search the default DLL search path at first
101+
[
102+
"lib#{l}.#{lib_ext}",
103+
"lib#{l}64.#{lib_ext}"
104+
].each do |filename|
105+
begin
106+
Fiddle.dlopen(filename).close
107+
rescue Fiddle::DLError
108+
x = nil
109+
else
110+
x = filename
111+
break
112+
end
113+
end
114+
end
115+
if x.nil?
116+
# Search in the candidate directories
117+
lib_dirs.find do |d|
118+
x = Dir.glob("#{d}/lib#{l}{,64}.#{lib_ext}{,.*}").find do |lib|
119+
begin
120+
Fiddle.dlopen(lib).close
121+
rescue Fiddle::DLError
122+
false
123+
else
124+
true
125+
end
126+
end
127+
break if x
128+
end
87129
end
88130
[l.to_sym, x]
89131
end

0 commit comments

Comments
 (0)