Skip to content

Commit a2fadb6

Browse files
larskanissodabrew
authored andcommitted
Fix compat with RubyInstaller-2.4 on Windows (#875)
Since RubyInstaller-2.4+ is bundled with MSYS2 and the libmariadbclient can be installed per gemspec library dependency, it is easy to build the mysql2 gem in Windows. The MSYS2/MINGW dependency feature is documented here: https://github.com/oneclick/rubyinstaller2/wiki/For-gem-developers#msys2-library-dependency This also adds ruby-2.4 binaries, so that the mysql2 is still usabel as a binary gem. Fixes #861 The change in the spec is required for mariadbclient. It throws an error if no query was executed. Due to the stdcall convention on i686, the mysql_query() function check fails, so that it is omitted, now.
1 parent a50e081 commit a2fadb6

File tree

7 files changed

+29
-15
lines changed

7 files changed

+29
-15
lines changed

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ source 'https://rubygems.org'
33
gemspec
44

55
gem 'rake', '~> 10.4.2'
6-
gem 'rake-compiler', '~> 0.9.5'
6+
gem 'rake-compiler', '~> 1.0'
77

88
group :test do
99
gem 'eventmachine' unless RUBY_PLATFORM =~ /mswin|mingw/
@@ -22,7 +22,7 @@ end
2222

2323
group :development do
2424
gem 'pry'
25-
gem 'rake-compiler-dock', '~> 0.5.1'
25+
gem 'rake-compiler-dock', '~> 0.6.0'
2626
end
2727

2828
platforms :rbx do

appveyor.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
version: "{build}"
33
clone_depth: 10
44
install:
5+
- SET PATH=C:\MinGW\msys\1.0\bin;%PATH%
56
- SET PATH=C:\Ruby%ruby_version%\bin;%PATH%
67
- ruby --version
78
- gem --version
89
- gem install bundler --quiet --no-ri --no-rdoc
910
- bundler --version
1011
- bundle install --without benchmarks --path vendor/bundle
12+
- IF DEFINED MINGW_PACKAGE_PREFIX (ridk exec pacman -S --noconfirm --needed %MINGW_PACKAGE_PREFIX%-libmariadbclient)
1113
build_script:
1214
- bundle exec rake compile
1315
test_script:
@@ -20,11 +22,14 @@ test_script:
2022
FLUSH PRIVILEGES;
2123
"
2224
- bundle exec rake spec
23-
# Where do I get Unix find?
24-
#on_failure:
25-
# - find tmp -name "*.log" -exec cat {};
25+
on_failure:
26+
- find tmp -name "*.log" | xargs cat
2627
environment:
2728
matrix:
29+
- ruby_version: "24-x64"
30+
MINGW_PACKAGE_PREFIX: "mingw-w64-x86_64"
31+
- ruby_version: "24"
32+
MINGW_PACKAGE_PREFIX: "mingw-w64-i686"
2833
- ruby_version: "23-x64"
2934
- ruby_version: "23"
3035
- ruby_version: "22-x64"

ext/mysql2/extconf.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def add_ssl_defines(header)
8888
else
8989
_, usr_local_lib = dir_config('mysql', '/usr/local')
9090

91-
asplode("mysql client") unless find_library('mysqlclient', 'mysql_query', usr_local_lib, "#{usr_local_lib}/mysql")
91+
asplode("mysql client") unless find_library('mysqlclient', nil, usr_local_lib, "#{usr_local_lib}/mysql")
9292

9393
rpath_dir = usr_local_lib
9494
end
@@ -179,7 +179,7 @@ def add_ssl_defines(header)
179179
$CFLAGS << ' -g -fno-omit-frame-pointer'
180180
end
181181

182-
if RUBY_PLATFORM =~ /mswin|mingw/
182+
if RUBY_PLATFORM =~ /mswin|mingw/ && !defined?(RubyInstaller)
183183
# Build libmysql.a interface link library
184184
require 'rake'
185185

lib/mysql2.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
ENV['RUBY_MYSQL2_LIBMYSQL_DLL']
1313
elsif File.exist?(File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__)))
1414
# Use vendor/libmysql.dll if it exists, convert slashes for Win32 LoadLibrary
15-
File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__)).tr('/', '\\')
15+
File.expand_path('../vendor/libmysql.dll', File.dirname(__FILE__))
16+
elsif defined?(RubyInstaller)
17+
# RubyInstaller-2.4+ native build doesn't need DLL preloading
1618
else
1719
# This will use default / system library paths
1820
'libmysql.dll'
1921
end
2022

21-
require 'Win32API'
22-
LoadLibrary = Win32API.new('Kernel32', 'LoadLibrary', ['P'], 'I')
23-
if 0 == LoadLibrary.call(dll_path)
24-
abort "Failed to load libmysql.dll from #{dll_path}"
23+
if dll_path
24+
require 'Win32API'
25+
LoadLibrary = Win32API.new('Kernel32', 'LoadLibrary', ['P'], 'I')
26+
if 0 == LoadLibrary.call(dll_path)
27+
abort "Failed to load libmysql.dll from #{dll_path}"
28+
end
2529
end
2630
end
2731

mysql2.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ Mysql2::GEMSPEC = Gem::Specification.new do |s|
1414

1515
s.files = `git ls-files README.md CHANGELOG.md LICENSE ext lib support`.split
1616
s.test_files = `git ls-files spec examples`.split
17+
18+
s.metadata['msys2_mingw_dependencies'] = 'libmariadbclient'
1719
end

spec/mysql2/statement_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,6 @@ def stmt_count
686686

687687
it 'should return number of rows affected by an insert' do
688688
stmt = @client.prepare 'INSERT INTO lastIdTest (blah) VALUES (?)'
689-
expect(stmt.affected_rows).to eq 0
690689
stmt.execute 1
691690
expect(stmt.affected_rows).to eq 1
692691
end

tasks/compile.rake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Rake::ExtensionTask.new("mysql2", Mysql2::GEMSPEC) do |ext|
99
# clean compiled extension
1010
CLEAN.include "#{ext.lib_dir}/*.#{RbConfig::CONFIG['DLEXT']}"
1111

12-
if RUBY_PLATFORM =~ /mswin|mingw/
12+
if RUBY_PLATFORM =~ /mswin|mingw/ && !defined?(RubyInstaller)
1313
# Expand the path because the build dir is 3-4 levels deep in tmp/platform/version/
1414
connector_dir = File.expand_path("../../vendor/#{vendor_mysql_dir}", __FILE__)
1515
ext.config_options = ["--with-mysql-dir=#{connector_dir}"]
@@ -26,6 +26,10 @@ Rake::ExtensionTask.new("mysql2", Mysql2::GEMSPEC) do |ext|
2626
Rake::Task['lib/mysql2/mysql2.rb'].invoke
2727
# vendor/libmysql.dll is invoked from extconf.rb
2828
Rake::Task['vendor/README'].invoke
29+
30+
# only the source gem has a package dependency - the binary gem ships it's own DLL version
31+
spec.metadata.delete('msys2_mingw_dependencies')
32+
2933
spec.files << 'lib/mysql2/mysql2.rb'
3034
spec.files << 'vendor/libmysql.dll'
3135
spec.files << 'vendor/README'
@@ -77,7 +81,7 @@ task :devkit do
7781
end
7882

7983
if RUBY_PLATFORM =~ /mingw|mswin/
80-
Rake::Task['compile'].prerequisites.unshift 'vendor:mysql'
84+
Rake::Task['compile'].prerequisites.unshift 'vendor:mysql' unless defined?(RubyInstaller)
8185
Rake::Task['compile'].prerequisites.unshift 'devkit'
8286
else
8387
if Rake::Task.tasks.map(&:name).include? 'cross'

0 commit comments

Comments
 (0)