7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/pybind/libpython.rb', line 7
def self.find_libpython(python = nil)
python ||= ENV['PYTHON'] || 'python'
python_config = investigate_python_config(python)
version = python_config[:VERSION]
libprefix = FFI::Platform::LIBPREFIX
libs = []
i(INSTSONAME LDLIBRARY).each do |key|
lib = python_config[key]
libs << lib << File.basename(lib) if lib
end
if (lib = python_config[:LIBRARY])
libs << File.basename(lib, File.extname(lib))
end
libs << "#{libprefix}python#{version}" << "#{libprefix}python"
libs.uniq!
executable = python_config[:EXECUTABLE]
libpaths = [ python_config[:LIBDIR] ]
if FFI::Platform.windows?
libpaths << File.dirname(executable)
else
libpaths << File.expand_path('../../lib', executable)
end
libpaths << python_config[:PYTHONFRAMEWORKPREFIX] if FFI::Platform.mac?
exec_prefix = python_config[:EXECPREFIX]
libpaths << exec_prefix << File.join(exec_prefix, 'lib')
libpaths.compact!
unless ENV['PYTHONHOME']
if FFI::Platform.windows?
ENV['PYTHONHOME'] = exec_prefix
else
ENV['PYTHONHOME'] = [python_config[:PREFIX], exec_prefix].join(':')
end
unless system(python, '-c', 'import site', out: File::NULL, err: File::NULL)
ENV['PYTHONHOME'] = nil
end
end
if ENV['LIBPYTHON']
if File.file?(ENV['LIBPYTHON'])
begin
libs = ffi_lib(ENV['LIBPYTHON'])
return libs.first
rescue LoadError
end
end
$stderr.puts '[WARN] Ignore the wrong libpython location specified in LIBPYTHON environment variable.'
end
libsuffix = FFI::Platform::LIBSUFFIX
multiarch = python_config[:MULTIARCH] || python_config[:IMPLEMENTATIONMULTIARCH]
dir_sep = File::ALT_SEPARATOR || File::SEPARATOR
libs.each do |lib|
libpaths.each do |libpath|
libpath_libs = [ [libpath, lib].join(dir_sep) ]
libpath_libs << [libpath, multiarch, lib].join(dir_sep) if multiarch
libpath_libs.each do |libpath_lib|
[
libpath_lib,
"#{libpath_lib}.#{libsuffix}"
].each do |fullname|
next unless File.file?(fullname)
begin
libs = ffi_lib(fullname)
return libs.first
rescue LoadError
end
end
end
end
end
end
|