Module: GRCommons::GRLib
- Defined in:
- lib/gr_commons/gr_lib.rb
Overview
This module helps GR, GR3 and GRM to search the shared library.
The order of priority:
-
RubyInstaller ( for Windows only )
-
Environment variable GRDIR
-
pkg-config : github.com/ruby-gnome/pkg-config
The following packages (should) support pkg-config.
-
Linux
-
Red Data Tools github.com/red-data-tools/packages.red-data-tools.org
-
libgr-dev
-
libgr3-dev
-
libgrm-dev
-
-
-
Mac
-
Homebrew github.com/Homebrew/homebrew-core
-
libgr
-
-
-
Windows
-
MinGW github.com/msys2/MINGW-packages
-
mingw-w64-gr
-
-
Class Method Summary collapse
-
.default_lib_names(pkg_name) ⇒ Object
Return default shared library names for a GR package.
-
.get_grdir_from_env(lib_names) ⇒ Object
Return the directory path from the GRDIR environment variable.
-
.load_library(mod, pkg_name:, lib_names: nil, not_found_error: LoadError) ⇒ Object
Search the shared library and assign it to the given module.
-
.pkg_config_search(lib_name, pkg_name) ⇒ Object
Use pkg-config to search for shared libraries.
-
.recursive_search(name, base_dir) ⇒ Object
Recursive file search in directories.
-
.ruby_installer? ⇒ Boolean
Check if using RubyInstaller or not.
-
.search(lib_names, pkg_name) ⇒ Object
Search the shared library.
Class Method Details
.default_lib_names(pkg_name) ⇒ Object
Return default shared library names for a GR package.
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/gr_commons/gr_lib.rb', line 41 def default_lib_names(pkg_name) lib_base = "lib#{pkg_name.upcase}" case RbConfig::CONFIG['host_os'] when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ ["#{lib_base}.dll"] when /darwin|mac os/ ENV['GKS_WSTYPE'] ||= 'gksqt' ["#{lib_base}.dylib", "#{lib_base}.so"] else ["#{lib_base}.so"] end end |
.get_grdir_from_env(lib_names) ⇒ Object
Return the directory path from the GRDIR environment variable.
33 34 35 36 37 38 |
# File 'lib/gr_commons/gr_lib.rb', line 33 def get_grdir_from_env(lib_names) return nil unless ENV['GRDIR'] return ENV['GRDIR'] if Dir.exist?(ENV['GRDIR']) warn "#{lib_names} : Dir GRDIR=#{ENV['GRDIR']} not found." # return nil end |
.load_library(mod, pkg_name:, lib_names: nil, not_found_error: LoadError) ⇒ Object
Search the shared library and assign it to the given module.
55 56 57 58 59 60 61 62 |
# File 'lib/gr_commons/gr_lib.rb', line 55 def load_library(mod, pkg_name:, lib_names: nil, not_found_error: LoadError) lib_names ||= default_lib_names(pkg_name) lib_path = search(lib_names, pkg_name) raise not_found_error, "#{lib_names} not found" if lib_path.nil? mod.ffi_lib = lib_path end |
.pkg_config_search(lib_name, pkg_name) ⇒ Object
Use pkg-config to search for shared libraries
122 123 124 125 126 |
# File 'lib/gr_commons/gr_lib.rb', line 122 def pkg_config_search(lib_name, pkg_name) PKGConfig.variable(pkg_name, 'sopath') rescue PackageConfig::NotFoundError => e warn "#{e.} Cannot find #{lib_name}. " end |
.recursive_search(name, base_dir) ⇒ Object
Recursive file search in directories
112 113 114 115 116 117 118 119 |
# File 'lib/gr_commons/gr_lib.rb', line 112 def recursive_search(name, base_dir) Dir.chdir(base_dir) do paths = Dir["**/#{name}"].sort warn "More than one file found: #{paths}" if paths.size > 1 path = paths.first File.(path) if path end end |
.ruby_installer? ⇒ Boolean
Check if using RubyInstaller or not.
28 29 30 |
# File 'lib/gr_commons/gr_lib.rb', line 28 def ruby_installer? Object.const_defined?(:RubyInstaller) end |
.search(lib_names, pkg_name) ⇒ Object
This method does not detect the Operating System.
Search the shared library.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/gr_commons/gr_lib.rb', line 69 def search(lib_names, pkg_name) # FIXME: There may be a better way to do it... def lib_names.map_find(&block) lazy.map(&block).find { |path| path } end # ENV['GRDIR'] # Verify that the directory exists. grdir = get_grdir_from_env(lib_names) # Windows + RubyInstaller if ruby_installer? grdir ||= File.join(RubyInstaller::Runtime.msys2_installation.msys_path, RubyInstaller::Runtime.msys2_installation.mingwarch) end # Search grdir if grdir lib_path = lib_names.map_find do |lib_name| recursive_search(lib_name, grdir) end end # Search with pkg-config lib_path ||= lib_names.map_find do |lib_name| pkg_config_search(lib_name, pkg_name) end # Windows + RubyInstaller if ruby_installer? && lib_path RubyInstaller::Runtime.add_dll_directory(File.dirname(lib_path)) # FIXME: Where should I write this code? ENV['GKS_FONTPATH'] ||= grdir end lib_path end |