lib/pycall/libpython.rb in pycall-0.1.0.alpha.20170317 vs lib/pycall/libpython.rb in pycall-0.1.0.alpha.20170329

- old
+ new

@@ -29,28 +29,32 @@ python ||= 'python' python_config = investigate_python_config(python) v = python_config[:VERSION] libprefix = FFI::Platform::LIBPREFIX - libs = [ "#{libprefix}python#{v}", "#{libprefix}python" ] - lib = python_config[:LIBRARY] - libs.unshift(File.basename(lib, File.extname(lib))) if lib - lib = python_config[:LDLIBRARY] - libs.unshift(lib, File.basename(lib)) if lib + 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#{v}" << "#{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[:exec_prefix] libpaths << exec_prefix << File.join(exec_prefix, 'lib') + libpaths.compact! unless ENV['PYTHONHOME'] # PYTHONHOME tells python where to look for both pure python and binary modules. # When it is set, it replaces both `prefix` and `exec_prefix` # and we thus need to set it to both in case they differ. @@ -67,22 +71,42 @@ unless system(python, '-c', 'import site', out: File::NULL, err: File::NULL) ENV['PYTHONHOME'] = nil end end + # Try LIBPYTHON environment variable first. + 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 + # Find libpython (we hope): libsuffix = FFI::Platform::LIBSUFFIX + multiarch = python_config[:MULTIARCH] || python_config[:multiarch] + dir_sep = File::ALT_SEPARATOR || File::SEPARATOR libs.each do |lib| libpaths.each do |libpath| - next unless libpath # NOTE: File.join doesn't use File::ALT_SEPARATOR - libpath_lib = [libpath, lib].join(File::ALT_SEPARATOR || File::SEPARATOR) - if File.file?("#{libpath_lib}.#{libsuffix}") - begin - libs = ffi_lib("#{libpath_lib}.#{libsuffix}") - return libs.first - rescue LoadError - # skip load error + 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 + # skip load error + end end end end end end