Skip to content

Commit b1e14b3

Browse files
committed
Handle dir_config and with_config mysql more strictly, add rpath more often, add notices
1 parent 85d9cce commit b1e14b3

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

ext/mysql2/extconf.rb

Lines changed: 28 additions & 10 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,16 +91,20 @@ def asplode lib
7791
asplode h unless have_header h
7892
end
7993

80-
# GCC specific flags
81-
if RbConfig::MAKEFILE_CONFIG['CC'] =~ /gcc/
94+
# GCC | Clang | XCode specific flags
95+
if RbConfig::MAKEFILE_CONFIG['CC'] =~ /gcc|clang|xcrun/
8296
$CFLAGS << ' -Wall -funroll-loops'
8397

84-
if libdir = $libs[%r{-L(/[^ ]+)}, 1]
98+
if libdir = rpath_dir[%r{(-L)?(/[^ ]+)}, 2]
8599
# The following comment and test is borrowed from the Pg gem:
86100
# Try to use runtime path linker option, even if RbConfig doesn't know about it.
87101
# The rpath option is usually set implicit by dir_config(), but so far not on Mac OS X.
88102
if RbConfig::CONFIG["RPATHFLAG"].to_s.empty? && try_link('int main() {return 0;}', " -Wl,-rpath,#{libdir}")
103+
warn "-----\nSetting rpath to #{libdir}\n-----"
89104
$LDFLAGS << " -Wl,-rpath,#{libdir}"
105+
else
106+
# Make sure that LIBPATH gets set if we didn't explicitly set the rpath.
107+
$LIBPATH << libdir unless $LIBPATH.include?(libdir)
90108
end
91109
end
92110
end

0 commit comments

Comments
 (0)