Skip to content

Commit 05ba33e

Browse files
committed
Use env var RUBY_MYSQL2_LIBMYSQL_DLL to find libmysql.dll out of path
1 parent 7366f9c commit 05ba33e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/mysql2.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@
33
require 'bigdecimal'
44
require 'rational' unless RUBY_VERSION >= '1.9.2'
55

6+
# Load libmysql.dll before requiring mysql2/mysql2.so
7+
# This gives a chance to be flexible about the load path
8+
# Or to bomb out with a clear error message instead of a linker crash
9+
if RUBY_PLATFORM =~ /mswin|mingw/
10+
dll_search = [
11+
File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__)).gsub('/', '\\'),
12+
File.expand_path('../libmysql.dll', File.dirname(__FILE__)).gsub('/', '\\'),
13+
'libmysql.dll' # This will use default / system library paths
14+
]
15+
16+
# If this environment variable is set, it overrides any other search paths
17+
dll_search = [ ENV['RUBY_MYSQL2_LIBMYSQL_DLL'].dup ] if ENV['RUBY_MYSQL2_LIBMYSQL_DLL']
18+
19+
require 'Win32API'
20+
LoadLibrary = Win32API.new('Kernel32', 'LoadLibrary', ['P'], 'I')
21+
unless dll_search.any? { |dll| 0 != LoadLibrary.call(dll) }
22+
abort "Failed to load libmysql.dll from any of #{dll_search.inspect}"
23+
end
24+
end
25+
626
require 'mysql2/version' unless defined? Mysql2::VERSION
727
require 'mysql2/error'
828
require 'mysql2/mysql2'

0 commit comments

Comments
 (0)