Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix library loading #131

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ Mittsu depends on Ruby 2.x, OpenGL 3.3+, and GLFW 3.1.x
$ brew install glfw3

# Ubuntu
$ sudo apt-get install libglfw3
$ sudo apt install libglfw3

# Fedora
$ sudo dnf install glfw
```

**NOTE:** On Windows, you will have to manually specify the glfw3.dll path in an environment variable
Expand Down
24 changes: 7 additions & 17 deletions lib/mittsu/renderers/generic_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,15 @@ def discover

class Base
def path; nil; end
def file; nil; end
end

class Linux < Base
def path
return nil if file_path.nil?
File.dirname file_path
end

def file
return nil if file_path.nil?
File.basename file_path
@_path ||= begin
ldconfig_lib || driver_specific_lib || sixtyfour_bit_lib || fallback_lib || give_up
end.tap do |result|
print_debug_info(result) if DEBUG
end
end

class << self
Expand All @@ -41,7 +38,7 @@ def kernel_module_in_use
''
end

def libgl_paths
def lib_paths
Dir.glob('/usr/lib*/**/libGL.so*')
rescue
[]
Expand All @@ -59,13 +56,6 @@ def ldconfig
end

private
def file_path
@_file_path ||= begin
ldconfig_lib || driver_specific_lib || sixtyfour_bit_lib || fallback_lib || give_up
end.tap do |result|
print_debug_info(result) if DEBUG
end
end

def ldconfig_lib
return nil if ldconfig.empty?
Expand Down Expand Up @@ -107,7 +97,7 @@ def kernel_module
end

def libs
@_libs ||= @loader.libgl_paths.sort_by(&:length)
@_libs ||= @loader.lib_paths.sort_by(&:length)
end

def ldconfig
Expand Down
14 changes: 3 additions & 11 deletions lib/mittsu/renderers/glfw_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(loader = Linux)
end

class << self
def libgl_paths
def lib_paths
Dir.glob('/usr/lib*/**/libglfw*.so*')
rescue
[]
Expand All @@ -27,7 +27,7 @@ def ldconfig
end

class Windows < GenericLib::Base
def file
def path
'glfw3.dll'
end
end
Expand All @@ -38,19 +38,11 @@ class MacOS < GenericLib::Base
'/opt/homebrew/**']

def path
File.dirname(match.to_s)
end

def file
File.basename(match.to_s)
@_path ||= find_match&.to_s
end

private

def match
@match ||= find_match
end

def find_match
SEARCH_GLOBS.each do |glob|
matches = Dir.glob("#{glob}/libglfw*.dylib")
Expand Down
2 changes: 1 addition & 1 deletion lib/mittsu/renderers/glfw_window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'mittsu/utils'
require 'mittsu/renderers/glfw_lib'
glfw_lib = Mittsu::GLFWLib.discover
::GLFW.load_lib(ENV["MITTSU_LIBGLFW_FILE"] || glfw_lib.file, ENV["MITTSU_LIBGLFW_PATH"] || glfw_lib.path) unless Mittsu.test?
::GLFW.load_lib(ENV["MITTSU_LIBGLFW_PATH"] || glfw_lib.path, Mittsu.debug?) unless Mittsu.test?

module Mittsu
module GLFW
Expand Down
2 changes: 1 addition & 1 deletion lib/mittsu/renderers/opengl_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require 'mittsu/renderers/opengl/opengl_lib'
opengl_lib = Mittsu::OpenGLLib.discover
GL.load_lib(ENV["MITTSU_LIBGL_FILE"] || opengl_lib.file, ENV["MITTSU_LIBGL_PATH"] || opengl_lib.path)
GL.load_lib(ENV["MITTSU_LIBGL_PATH"] || opengl_lib.path, Mittsu.debug?)

require 'mittsu/renderers/glfw_window'
require 'mittsu/renderers/opengl/opengl_debug' if ENV['DEBUG']
Expand Down
25 changes: 10 additions & 15 deletions test/renderers/opengl/test_opengl_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class TestOpenGLLib < Minitest::Test
def test_linux_64_nvidia_1
fake_loader = Struct.new(:libgl_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
fake_loader = Struct.new(:lib_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
[
'/usr/lib/x86_64-linux-gnu/mesa/libGL.so',
'/usr/lib/x86_64-linux-gnu/libGL.so',
Expand All @@ -13,13 +13,12 @@ def test_linux_64_nvidia_1
)

linux_lib = Mittsu::OpenGLLib::Linux.new(fake_loader)
assert_equal 'libGL.so', linux_lib.file
assert_equal '/usr/lib/x86_64-linux-gnu', linux_lib.path
assert_equal '/usr/lib/x86_64-linux-gnu/libGL.so', linux_lib.path
end

# TODO: get real test data from my nvidia PC when I get home
def test_linux_64_nvidia_2
fake_loader = Struct.new(:libgl_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
fake_loader = Struct.new(:lib_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
[
'/usr/lib/x86_64-linux-gnu/libGL.so',
'/usr/lib/x86_64-linux-gnu/mesa/libGL.so',
Expand All @@ -32,12 +31,11 @@ def test_linux_64_nvidia_2
)

linux_lib = Mittsu::OpenGLLib::Linux.new(fake_loader)
assert_equal 'libGL.so', linux_lib.file
assert_equal '/usr/lib/nvidia-367', linux_lib.path
assert_equal '/usr/lib/nvidia-367/libGL.so', linux_lib.path
end

def test_linux_64_vboxvideo
fake_loader = Struct.new(:libgl_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
fake_loader = Struct.new(:lib_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
[
'/usr/lib/x86_64-linux-gnu/mesa/libGL.so',
'/usr/lib/x86_64-linux-gnu/libGL.so'
Expand All @@ -48,12 +46,11 @@ def test_linux_64_vboxvideo
)

linux_lib = Mittsu::OpenGLLib::Linux.new(fake_loader)
assert_equal 'libGL.so', linux_lib.file
assert_equal '/usr/lib/x86_64-linux-gnu', linux_lib.path
assert_equal '/usr/lib/x86_64-linux-gnu/libGL.so', linux_lib.path
end

def test_linux_64_ldconfig
fake_loader = Struct.new(:libgl_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
fake_loader = Struct.new(:lib_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
[],
'',
true,
Expand All @@ -67,12 +64,11 @@ def test_linux_64_ldconfig
)

linux_lib = Mittsu::OpenGLLib::Linux.new(fake_loader)
assert_equal 'libGL.so.1', linux_lib.file
assert_equal '/usr/lib/nvidia-367', linux_lib.path
assert_equal '/usr/lib/nvidia-367/libGL.so.1', linux_lib.path
end

def test_linux_32_ldconfig
fake_loader = Struct.new(:libgl_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
fake_loader = Struct.new(:lib_paths, :kernel_module_in_use, :sixtyfour_bits?, :ldconfig).new(
[],
'',
false,
Expand All @@ -86,8 +82,7 @@ def test_linux_32_ldconfig
)

linux_lib = Mittsu::OpenGLLib::Linux.new(fake_loader)
assert_equal 'libGL.so.1', linux_lib.file
assert_equal '/usr/lib32/nvidia-367', linux_lib.path
assert_equal '/usr/lib32/nvidia-367/libGL.so.1', linux_lib.path
end

# TODO: get real-world data for 32-bit machines, as well as neuvaux, fglrx, radeon, and intel configs
Expand Down