jrmey-mysqlplus 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/ext/extconf.rb ADDED
@@ -0,0 +1,133 @@
1
+ require 'mkmf'
2
+
3
+ def exec_command(command, flag_raise=false)
4
+ output = `#{command}`
5
+ return output.chomp if $? == 0
6
+ msg = "failed: #{command}"
7
+ raise msg if flag_raise
8
+ die msg
9
+ end
10
+
11
+ def die(message)
12
+ $stderr.puts "*** ERROR: #{message}"
13
+ exit 1
14
+ end
15
+
16
+ if /mswin32/ =~ RUBY_PLATFORM
17
+ inc, lib = dir_config('mysql')
18
+ #exit 1 unless have_library("libmysql")
19
+ have_library("libmysql") or die "can't find libmysql."
20
+ elsif mc = with_config('mysql-config') then
21
+ mc = 'mysql_config' if mc == true
22
+ #cflags = `#{mc} --cflags`.chomp
23
+ #exit 1 if $? != 0
24
+ cflags = exec_command("#{mc} --cflags")
25
+ #libs = `#{mc} --libs`.chomp
26
+ #exit 1 if $? != 0
27
+ libs = exec_command("#{mc} --libs")
28
+ $CPPFLAGS += ' ' + cflags
29
+ $libs = libs + " " + $libs
30
+ else
31
+ puts "Trying to detect MySQL configuration with mysql_config command..."
32
+ begin
33
+ cflags = libs = nil
34
+
35
+ dirs = ENV['PATH'].split(':') + %w[
36
+ /opt
37
+ /opt/local
38
+ /opt/local/mysql
39
+ /opt/local/lib/mysql5
40
+ /usr
41
+ /usr/local
42
+ /usr/local/mysql
43
+ /usr/local/mysql-*
44
+ /usr/local/lib/mysql5
45
+ ].map{|dir| "#{dir}/bin" }
46
+
47
+ GLOB = "{#{dirs.join(',')}}/{mysql_config,mysql_config5}"
48
+
49
+ if /mswin32/ =~ RUBY_PLATFORM
50
+ inc, lib = dir_config('mysql')
51
+ exit 1 unless have_library("libmysql")
52
+ elsif mc = (with_config('mysql-config') || Dir[GLOB].first) then
53
+ mc = Dir[GLOB].first if mc == true
54
+ puts "Succeeded to detect MySQL configuration: #{mc}"
55
+ cflags = `#{mc} --cflags`.chomp
56
+ exit 1 if $? != 0
57
+ libs = `#{mc} --libs`.chomp
58
+ exit 1 if $? != 0
59
+ $CPPFLAGS += ' ' + cflags
60
+ $libs = libs + " " + $libs
61
+ else
62
+ puts "Failed to detect MySQL configuration with mysql_config command."
63
+ puts "Trying to detect MySQL client library..."
64
+ inc, lib = dir_config('mysql', '/usr/local')
65
+ libs = ['m', 'z', 'socket', 'nsl', 'mygcc']
66
+ while not find_library('mysqlclient', 'mysql_query', lib, "#{lib}/mysql") do
67
+ #exit 1 if libs.empty?
68
+ !libs.empty? or die "can't find mysql client library."
69
+ have_library(libs.shift)
70
+ end
71
+ end
72
+
73
+ end
74
+ end
75
+
76
+ have_func('mysql_ssl_set')
77
+ have_func('rb_str_set_len')
78
+
79
+ if have_header('mysql.h') then
80
+ src = "#include <errmsg.h>\n#include <mysqld_error.h>\n"
81
+ elsif have_header('mysql/mysql.h') then
82
+ src = "#include <mysql/errmsg.h>\n#include <mysql/mysqld_error.h>\n"
83
+ else
84
+ #exit 1
85
+ die "can't find 'mysql.h'."
86
+ end
87
+
88
+ # check for 1.9
89
+ if have_func('rb_thread_blocking_region') and have_macro('RUBY_UBF_IO', 'ruby.h')
90
+ $CFLAGS += " -DHAVE_TBR "
91
+ $CPPFLAGS << " -DHAVE_TBR "
92
+ end
93
+
94
+ # make mysql constant
95
+ File.open("conftest.c", "w") do |f|
96
+ f.puts src
97
+ end
98
+ if defined? cpp_command then
99
+ cpp = Config.expand(cpp_command(''))
100
+ else
101
+ cpp = Config.expand sprintf(CPP, $CPPFLAGS, $CFLAGS, '')
102
+ end
103
+ if /mswin32/ =~ RUBY_PLATFORM && !/-E/.match(cpp)
104
+ cpp << " -E"
105
+ end
106
+ #unless system "#{cpp} > confout" then
107
+ # exit 1
108
+ #end
109
+ exec_command("#{cpp} > confout")
110
+ File.unlink "conftest.c"
111
+
112
+ error_syms = []
113
+ IO.foreach('confout') do |l|
114
+ next unless l =~ /errmsg\.h|mysqld_error\.h/
115
+ fn = l.split(/\"/)[1]
116
+ IO.foreach(fn) do |m|
117
+ if m =~ /^#define\s+([CE]R_[0-9A-Z_]+)/ then
118
+ error_syms << $1
119
+ end
120
+ end
121
+ end
122
+ File.unlink 'confout'
123
+ error_syms.uniq!
124
+
125
+ File.open('error_const.h', 'w') do |f|
126
+ error_syms.each do |s|
127
+ f.puts " rb_define_mysql_const(#{s});"
128
+ end
129
+ end
130
+
131
+ $CPPFLAGS += " -DRUBY19" if RUBY_VERSION =~ /1.9/
132
+
133
+ create_makefile("mysql")