Skip to content

Commit d1dad2d

Browse files
committed
Merge pull request #431 from sodabrew/with_dirconfig
Handle dir_config and with_config mysql more strictly, add rpath more often
2 parents 85d9cce + 0e6c618 commit d1dad2d

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,27 @@ gem install mysql2
2222

2323
This gem links against MySQL's `libmysqlclient` C shared library. You may need to install a package such as `libmysqlclient-dev`, `mysql-devel`, or other appropriate package for your system.
2424

25-
If you have installed MySQL to a non-standard location, add `gem install mysql2 --with-mysql-config=/some/random/path/bin/mysql_config`
25+
By default, the mysql2 gem will try to find a copy of MySQL in this order:
26+
27+
* Option `--with-mysql-dir`, if provided (see below).
28+
* Option `--with-mysql-config`, if provided (see below).
29+
* Several typical paths for `msyql_config` (default for the majority of users).
30+
* The directory `/usr/local`.
31+
32+
### Configuration options
33+
34+
Use these options by `gem install mysql2 -- [--optionA] [--optionB=argument]`.
35+
The following options are mutually exclusive.
36+
37+
* `--with-mysql-dir[=/path/to/mysqldir]` -
38+
Specify the directory where MySQL is installed. The mysql2 gem will not use
39+
`mysql_config`, but will instead look at `mysqldir/lib` and `mysqldir/include`
40+
for the library and header files.
41+
42+
* `--with-mysql-config[=/path/to/mysql_config]` -
43+
Specify a path to the `mysql_config` binary provided by your copy of MySQL. The
44+
mysql2 gem will ask this `mysql_config` binary about the compiler and linker
45+
arguments needed.
2646

2747
### Windows
2848
First, make sure you have the DevKit installed (http://rubyinstaller.org/downloads/) and its variables

ext/mysql2/extconf.rb

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,27 @@ def asplode lib
3131

3232
GLOB = "{#{dirs.join(',')}}/{mysql_config,mysql_config5}"
3333

34-
if RUBY_PLATFORM =~ /mswin|mingw/
35-
inc, lib = dir_config('mysql')
36-
34+
# If the user has provided a --with-mysql-dir argument, we must respect it or fail.
35+
inc, lib = dir_config('mysql')
36+
if inc && lib
3737
# Ruby versions not incorporating the mkmf fix at
3838
# https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/39717
3939
# do not properly search for lib directories, and must be corrected
4040
unless lib && lib[-3, 3] == 'lib'
4141
@libdir_basename = 'lib'
4242
inc, lib = dir_config('mysql')
4343
end
44-
exit 1 unless have_library("libmysql")
45-
elsif mc = (with_config('mysql-config') || Dir[GLOB].first) then
44+
abort "-----\nCannot find include dir at #{inc}\n-----" unless inc && File.directory?(inc)
45+
abort "-----\nCannot find library dir at #{lib}\n-----" unless lib && File.directory?(lib)
46+
warn "-----\nUsing --with-mysql-dir=#{File.dirname inc}\n-----"
47+
rpath_dir = lib
48+
elsif mc = (with_config('mysql-config') || Dir[GLOB].first)
49+
# If the user has provided a --with-mysql-config argument, we must respect it or fail.
50+
# If the user gave --with-mysql-config with no argument means we should try to find it.
4651
mc = Dir[GLOB].first if mc == true
52+
abort "-----\nCannot find mysql_config at #{mc}\n-----" unless mc && File.exists?(mc)
53+
abort "-----\nCannot execute mysql_config at #{mc}\n-----" unless File.executable?(mc)
54+
warn "-----\nUsing mysql_config at #{mc}\n-----"
4755
ver = `#{mc} --version`.chomp.to_f
4856
includes = `#{mc} --include`.chomp
4957
exit 1 if $? != 0
@@ -55,18 +63,24 @@ def asplode lib
5563
exit 1 if $? != 0
5664
$INCFLAGS += ' ' + includes
5765
$libs = libs + " " + $libs
66+
rpath_dir = libs
5867
else
5968
inc, lib = dir_config('mysql', '/usr/local')
6069
libs = ['m', 'z', 'socket', 'nsl', 'mygcc']
6170
while not find_library('mysqlclient', 'mysql_query', lib, "#{lib}/mysql") do
6271
exit 1 if libs.empty?
6372
have_library(libs.shift)
6473
end
74+
rpath_dir = lib
75+
end
76+
77+
if RUBY_PLATFORM =~ /mswin|mingw/
78+
exit 1 unless have_library('libmysql')
6579
end
6680

67-
if have_header('mysql.h') then
81+
if have_header('mysql.h')
6882
prefix = nil
69-
elsif have_header('mysql/mysql.h') then
83+
elsif have_header('mysql/mysql.h')
7084
prefix = 'mysql'
7185
else
7286
asplode 'mysql.h'
@@ -77,17 +91,27 @@ def asplode lib
7791
asplode h unless have_header h
7892
end
7993

80-
# GCC specific flags
81-
if RbConfig::MAKEFILE_CONFIG['CC'] =~ /gcc/
82-
$CFLAGS << ' -Wall -funroll-loops'
94+
# These gcc style flags are also supported by clang and xcode compilers,
95+
# so we'll use a does-it-work test instead of an is-it-gcc test.
96+
gcc_flags = ' -Wall -funroll-loops'
97+
if try_link('int main() {return 0;}', gcc_flags)
98+
$CFLAGS << gcc_flags
99+
end
83100

84-
if libdir = $libs[%r{-L(/[^ ]+)}, 1]
85-
# The following comment and test is borrowed from the Pg gem:
86-
# Try to use runtime path linker option, even if RbConfig doesn't know about it.
87-
# The rpath option is usually set implicit by dir_config(), but so far not on Mac OS X.
88-
if RbConfig::CONFIG["RPATHFLAG"].to_s.empty? && try_link('int main() {return 0;}', " -Wl,-rpath,#{libdir}")
89-
$LDFLAGS << " -Wl,-rpath,#{libdir}"
101+
if libdir = rpath_dir[%r{(-L)?(/[^ ]+)}, 2]
102+
rpath_flags = " -Wl,-rpath,#{libdir}"
103+
if RbConfig::CONFIG["RPATHFLAG"].to_s.empty? && try_link('int main() {return 0;}', rpath_flags)
104+
# Usually Ruby sets RPATHFLAG the right way for each system, but not on OS X.
105+
warn "-----\nSetting rpath to #{libdir}\n-----"
106+
$LDFLAGS << rpath_flags
107+
else
108+
if RbConfig::CONFIG["RPATHFLAG"].to_s.empty?
109+
# If we got here because try_link failed, warn the user
110+
warn "-----\nDon't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load\n-----"
90111
end
112+
# Make sure that LIBPATH gets set if we didn't explicitly set the rpath.
113+
warn "-----\nSetting libpath to #{libdir}\n-----"
114+
$LIBPATH << libdir unless $LIBPATH.include?(libdir)
91115
end
92116
end
93117

0 commit comments

Comments
 (0)