Module: Tiletanic::GeosBootstrap

Defined in:
lib/tiletanic/geos_bootstrap.rb

Class Method Summary collapse

Class Method Details

.associated_base_library(geos_c_library) ⇒ Object



20
21
22
23
# File 'lib/tiletanic/geos_bootstrap.rb', line 20

def associated_base_library(geos_c_library)
  directory = File.dirname(geos_c_library)
  library_files(directory, 'libgeos').find { |path| path && File.file?(path) } || common_base_library
end

.callObject



7
8
9
10
11
12
13
14
# File 'lib/tiletanic/geos_bootstrap.rb', line 7

def call
  geos_c_library = ENV.fetch('GEOS_LIBRARY_PATH', nil)
  geos_c_library = discover_library_path if geos_c_library.nil? || geos_c_library.empty?
  return unless geos_c_library && File.file?(geos_c_library)

  ENV['GEOS_LIBRARY_PATH'] = geos_c_library
  preload_base_library(associated_base_library(geos_c_library))
end

.candidate_pathsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tiletanic/geos_bootstrap.rb', line 25

def candidate_paths
  executable = find_executable('geos-config')

  candidates = []
  if executable
    bin_dir = File.dirname(executable)
    candidates.concat(library_files(File.expand_path('../lib', bin_dir)))
    candidates.concat(library_files(File.expand_path('../../lib', bin_dir)))
  end

  candidates.concat(common_library_files)
  candidates.compact.uniq
end

.common_base_libraryObject



48
49
50
51
52
53
54
55
# File 'lib/tiletanic/geos_bootstrap.rb', line 48

def common_base_library
  %w[
    /usr/local/lib
    /opt/local/lib
    /opt/homebrew/lib
    /usr/lib
  ].flat_map { |directory| library_files(directory, 'libgeos') }.find { |path| path && File.file?(path) }
end

.common_library_filesObject



39
40
41
42
43
44
45
46
# File 'lib/tiletanic/geos_bootstrap.rb', line 39

def common_library_files
  %w[
    /usr/local/lib
    /opt/local/lib
    /opt/homebrew/lib
    /usr/lib
  ].flat_map { |directory| library_files(directory) }
end

.discover_library_pathObject



16
17
18
# File 'lib/tiletanic/geos_bootstrap.rb', line 16

def discover_library_path
  candidate_paths.find { |path| path && File.file?(path) }
end

.find_executable(name) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/tiletanic/geos_bootstrap.rb', line 61

def find_executable(name)
  ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).each do |directory|
    executable = File.join(directory, name)
    return executable if File.executable?(executable) && !File.directory?(executable)
  end

  nil
end

.library_files(directory, basename = 'libgeos_c') ⇒ Object



57
58
59
# File 'lib/tiletanic/geos_bootstrap.rb', line 57

def library_files(directory, basename = 'libgeos_c')
  Dir.glob(File.join(directory, "#{basename}.{dylib,so,so.*,dll}"))
end

.preload_base_library(path) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/tiletanic/geos_bootstrap.rb', line 70

def preload_base_library(path)
  return unless path && File.file?(path)

  require 'ffi'
  FFI::DynamicLibrary.open(path, FFI::DynamicLibrary::RTLD_LAZY | FFI::DynamicLibrary::RTLD_GLOBAL)
rescue LoadError, StandardError
  nil
end