Skip to content

Commit 2ce804d

Browse files
committed
Merge branch 'compiler_flags'
2 parents 066b665 + 434eb31 commit 2ce804d

File tree

6 files changed

+120
-129
lines changed

6 files changed

+120
-129
lines changed

ext/mysql2/client.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
VALUE cMysql2Client;
1818
extern VALUE mMysql2, cMysql2Error;
19-
static VALUE sym_id, sym_version, sym_async, sym_symbolize_keys, sym_as, sym_array, sym_stream;
19+
static VALUE sym_id, sym_version, sym_header_version, sym_async, sym_symbolize_keys, sym_as, sym_array, sym_stream;
2020
static ID intern_merge, intern_merge_bang, intern_error_number_eql, intern_sql_state_eql;
2121
static ID intern_brackets, intern_new;
2222

@@ -349,7 +349,7 @@ static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE po
349349
/* avoid an early timeout due to time truncating milliseconds off the start time */
350350
if (elapsed_time > 0)
351351
elapsed_time--;
352-
if (elapsed_time >= wrapper->connect_timeout)
352+
if (elapsed_time >= (time_t)wrapper->connect_timeout)
353353
break;
354354
connect_timeout = wrapper->connect_timeout - elapsed_time;
355355
mysql_options(wrapper->client, MYSQL_OPT_CONNECT_TIMEOUT, &connect_timeout);
@@ -824,30 +824,23 @@ static VALUE _mysql_client_options(VALUE self, int opt, VALUE value) {
824824
*
825825
* Returns a string that represents the client library version.
826826
*/
827-
static VALUE rb_mysql_client_info(VALUE self) {
828-
VALUE version, client_info;
829-
#ifdef HAVE_RUBY_ENCODING_H
830-
rb_encoding *default_internal_enc;
831-
rb_encoding *conn_enc;
832-
GET_CLIENT(self);
833-
#endif
834-
version = rb_hash_new();
827+
static VALUE rb_mysql_client_info(RB_MYSQL_UNUSED VALUE klass) {
828+
VALUE version_info, version, header_version;
829+
version_info = rb_hash_new();
835830

836-
#ifdef HAVE_RUBY_ENCODING_H
837-
default_internal_enc = rb_default_internal_encoding();
838-
conn_enc = rb_to_encoding(wrapper->encoding);
839-
#endif
831+
version = rb_str_new2(mysql_get_client_info());
832+
header_version = rb_str_new2(MYSQL_LINK_VERSION);
840833

841-
rb_hash_aset(version, sym_id, LONG2NUM(mysql_get_client_version()));
842-
client_info = rb_str_new2(mysql_get_client_info());
843834
#ifdef HAVE_RUBY_ENCODING_H
844-
rb_enc_associate(client_info, conn_enc);
845-
if (default_internal_enc) {
846-
client_info = rb_str_export_to_enc(client_info, default_internal_enc);
847-
}
835+
rb_enc_associate(version, rb_usascii_encoding());
836+
rb_enc_associate(header_version, rb_usascii_encoding());
848837
#endif
849-
rb_hash_aset(version, sym_version, client_info);
850-
return version;
838+
839+
rb_hash_aset(version_info, sym_id, LONG2NUM(mysql_get_client_version()));
840+
rb_hash_aset(version_info, sym_version, version);
841+
rb_hash_aset(version_info, sym_header_version, header_version);
842+
843+
return version_info;
851844
}
852845

853846
/* call-seq:
@@ -1244,11 +1237,11 @@ void init_mysql2_client() {
12441237
rb_define_alloc_func(cMysql2Client, allocate);
12451238

12461239
rb_define_singleton_method(cMysql2Client, "escape", rb_mysql_client_escape, 1);
1240+
rb_define_singleton_method(cMysql2Client, "info", rb_mysql_client_info, 0);
12471241

12481242
rb_define_method(cMysql2Client, "close", rb_mysql_client_close, 0);
12491243
rb_define_method(cMysql2Client, "abandon_results!", rb_mysql_client_abandon_results, 0);
12501244
rb_define_method(cMysql2Client, "escape", rb_mysql_client_real_escape, 1);
1251-
rb_define_method(cMysql2Client, "info", rb_mysql_client_info, 0);
12521245
rb_define_method(cMysql2Client, "server_info", rb_mysql_client_server_info, 0);
12531246
rb_define_method(cMysql2Client, "socket", rb_mysql_client_socket, 0);
12541247
rb_define_method(cMysql2Client, "async_result", rb_mysql_client_async_result, 0);
@@ -1284,6 +1277,7 @@ void init_mysql2_client() {
12841277

12851278
sym_id = ID2SYM(rb_intern("id"));
12861279
sym_version = ID2SYM(rb_intern("version"));
1280+
sym_header_version = ID2SYM(rb_intern("header_version"));
12871281
sym_async = ID2SYM(rb_intern("async"));
12881282
sym_symbolize_keys = ID2SYM(rb_intern("symbolize_keys"));
12891283
sym_as = ID2SYM(rb_intern("as"));

ext/mysql2/extconf.rb

Lines changed: 86 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -76,109 +76,106 @@ def asplode lib
7676
libs = ['m', 'z', 'socket', 'nsl', 'mygcc']
7777
found = false
7878
while not find_library('mysqlclient', 'mysql_query', lib, "#{lib}/mysql") do
79-
exit 1 if libs.empty?
80-
found ||= have_library(libs.shift)
81-
end
79+
exit 1 if libs.empty?
80+
found ||= have_library(libs.shift)
81+
end
8282

83-
asplode("mysql client") unless found
83+
asplode("mysql client") unless found
8484

85-
rpath_dir = lib
86-
end
85+
rpath_dir = lib
86+
end
8787

88-
if have_header('mysql.h')
89-
prefix = nil
90-
elsif have_header('mysql/mysql.h')
91-
prefix = 'mysql'
92-
else
93-
asplode 'mysql.h'
94-
end
88+
if have_header('mysql.h')
89+
prefix = nil
90+
elsif have_header('mysql/mysql.h')
91+
prefix = 'mysql'
92+
else
93+
asplode 'mysql.h'
94+
end
9595

96-
%w{ errmsg.h mysqld_error.h }.each do |h|
97-
header = [prefix, h].compact.join '/'
98-
asplode h unless have_header h
99-
end
96+
%w{ errmsg.h mysqld_error.h }.each do |h|
97+
header = [prefix, h].compact.join '/'
98+
asplode h unless have_header h
99+
end
100100

101-
# This is our wishlist. We use whichever flags work on the host.
102-
# -Wall and -Wextra are included by default.
103-
# TODO: fix statement.c and remove -Wno-error=declaration-after-statement
104-
%w(
105-
-Werror
106-
-Weverything
107-
-fsanitize=address
108-
-fsanitize=integer
109-
-fsanitize=thread
110-
-fsanitize=memory
111-
-fsanitize=undefined
112-
-fsanitize=cfi
113-
-Wno-error=declaration-after-statement
114-
).each do |flag|
115-
if try_link('int main() {return 0;}', flag)
116-
$CFLAGS << ' ' << flag
117-
end
118-
end
101+
# This is our wishlist. We use whichever flags work on the host.
102+
# TODO: fix statement.c and remove -Wno-declaration-after-statement
103+
# TODO: fix gperf mysql_enc_name_to_ruby.h and remove -Wno-missing-field-initializers
104+
%w(
105+
-Wall
106+
-Wextra
107+
-Werror
108+
-Wno-unused-function
109+
-Wno-declaration-after-statement
110+
-Wno-missing-field-initializers
111+
).select do |flag|
112+
try_link('int main() {return 0;}', flag)
113+
end.each do |flag|
114+
$CFLAGS << ' ' << flag
115+
end
119116

120-
if RUBY_PLATFORM =~ /mswin|mingw/
121-
# Build libmysql.a interface link library
122-
require 'rake'
123-
124-
# Build libmysql.a interface link library
125-
# Use rake to rebuild only if these files change
126-
deffile = File.expand_path('../../../support/libmysql.def', __FILE__)
127-
libfile = File.expand_path(File.join(rpath_dir, 'libmysql.lib'))
128-
file 'libmysql.a' => [deffile, libfile] do |t|
129-
when_writing 'building libmysql.a' do
130-
# Ruby kindly shows us where dllwrap is, but that tool does more than we want.
131-
# Maybe in the future Ruby could provide RbConfig::CONFIG['DLLTOOL'] directly.
132-
dlltool = RbConfig::CONFIG['DLLWRAP'].gsub('dllwrap', 'dlltool')
133-
sh dlltool, '--kill-at',
134-
'--dllname', 'libmysql.dll',
135-
'--output-lib', 'libmysql.a',
136-
'--input-def', deffile, libfile
137-
end
117+
if RUBY_PLATFORM =~ /mswin|mingw/
118+
# Build libmysql.a interface link library
119+
require 'rake'
120+
121+
# Build libmysql.a interface link library
122+
# Use rake to rebuild only if these files change
123+
deffile = File.expand_path('../../../support/libmysql.def', __FILE__)
124+
libfile = File.expand_path(File.join(rpath_dir, 'libmysql.lib'))
125+
file 'libmysql.a' => [deffile, libfile] do |t|
126+
when_writing 'building libmysql.a' do
127+
# Ruby kindly shows us where dllwrap is, but that tool does more than we want.
128+
# Maybe in the future Ruby could provide RbConfig::CONFIG['DLLTOOL'] directly.
129+
dlltool = RbConfig::CONFIG['DLLWRAP'].gsub('dllwrap', 'dlltool')
130+
sh dlltool, '--kill-at',
131+
'--dllname', 'libmysql.dll',
132+
'--output-lib', 'libmysql.a',
133+
'--input-def', deffile, libfile
138134
end
135+
end
139136

140-
Rake::Task['libmysql.a'].invoke
141-
$LOCAL_LIBS << ' ' << 'libmysql.a'
137+
Rake::Task['libmysql.a'].invoke
138+
$LOCAL_LIBS << ' ' << 'libmysql.a'
142139

143-
# Make sure the generated interface library works (if cross-compiling, trust without verifying)
144-
unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
145-
abort "-----\nCannot find libmysql.a\n----" unless have_library('libmysql')
146-
abort "-----\nCannot link to libmysql.a (my_init)\n----" unless have_func('my_init')
147-
end
140+
# Make sure the generated interface library works (if cross-compiling, trust without verifying)
141+
unless RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
142+
abort "-----\nCannot find libmysql.a\n----" unless have_library('libmysql')
143+
abort "-----\nCannot link to libmysql.a (my_init)\n----" unless have_func('my_init')
144+
end
148145

149-
# Vendor libmysql.dll
150-
vendordir = File.expand_path('../../../vendor/', __FILE__)
151-
directory vendordir
146+
# Vendor libmysql.dll
147+
vendordir = File.expand_path('../../../vendor/', __FILE__)
148+
directory vendordir
152149

153-
vendordll = File.join(vendordir, 'libmysql.dll')
154-
dllfile = File.expand_path(File.join(rpath_dir, 'libmysql.dll'))
155-
file vendordll => [dllfile, vendordir] do |t|
156-
when_writing 'copying libmysql.dll' do
157-
cp dllfile, vendordll
158-
end
150+
vendordll = File.join(vendordir, 'libmysql.dll')
151+
dllfile = File.expand_path(File.join(rpath_dir, 'libmysql.dll'))
152+
file vendordll => [dllfile, vendordir] do |t|
153+
when_writing 'copying libmysql.dll' do
154+
cp dllfile, vendordll
159155
end
156+
end
160157

161-
# Copy libmysql.dll to the local vendor directory by default
162-
if arg_config('--no-vendor-libmysql')
163-
# Fine, don't.
164-
puts "--no-vendor-libmysql"
165-
else # Default: arg_config('--vendor-libmysql')
166-
# Let's do it!
167-
Rake::Task[vendordll].invoke
168-
end
158+
# Copy libmysql.dll to the local vendor directory by default
159+
if arg_config('--no-vendor-libmysql')
160+
# Fine, don't.
161+
puts "--no-vendor-libmysql"
162+
else # Default: arg_config('--vendor-libmysql')
163+
# Let's do it!
164+
Rake::Task[vendordll].invoke
165+
end
166+
else
167+
case explicit_rpath = with_config('mysql-rpath')
168+
when true
169+
abort "-----\nOption --with-mysql-rpath must have an argument\n-----"
170+
when false
171+
warn "-----\nOption --with-mysql-rpath has been disabled at your request\n-----"
172+
when String
173+
# The user gave us a value so use it
174+
rpath_flags = " -Wl,-rpath,#{explicit_rpath}"
175+
warn "-----\nSetting mysql rpath to #{explicit_rpath}\n-----"
176+
$LDFLAGS << rpath_flags
169177
else
170-
case explicit_rpath = with_config('mysql-rpath')
171-
when true
172-
abort "-----\nOption --with-mysql-rpath must have an argument\n-----"
173-
when false
174-
warn "-----\nOption --with-mysql-rpath has been disabled at your request\n-----"
175-
when String
176-
# The user gave us a value so use it
177-
rpath_flags = " -Wl,-rpath,#{explicit_rpath}"
178-
warn "-----\nSetting mysql rpath to #{explicit_rpath}\n-----"
179-
$LDFLAGS << rpath_flags
180-
else
181-
if libdir = rpath_dir[%r{(-L)?(/[^ ]+)}, 2]
178+
if libdir = rpath_dir[%r{(-L)?(/[^ ]+)}, 2]
182179
rpath_flags = " -Wl,-rpath,#{libdir}"
183180
if RbConfig::CONFIG["RPATHFLAG"].to_s.empty? && try_link('int main() {return 0;}', rpath_flags)
184181
# Usually Ruby sets RPATHFLAG the right way for each system, but not on OS X.

ext/mysql2/result.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ static VALUE rb_mysql_result_fetch_fields(VALUE self) {
747747
wrapper->fields = rb_ary_new2(wrapper->numberOfFields);
748748
}
749749

750-
if (RARRAY_LEN(wrapper->fields) != wrapper->numberOfFields) {
750+
if ((unsigned)RARRAY_LEN(wrapper->fields) != wrapper->numberOfFields) {
751751
for (i=0; i<wrapper->numberOfFields; i++) {
752752
rb_mysql_result_fetch_field(self, i, symbolizeKeys);
753753
}

ext/mysql2/statement.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ static void rb_raise_mysql2_stmt_error(VALUE self) {
5353
rb_encoding *conn_enc;
5454
#endif
5555
GET_STATEMENT(self);
56+
57+
#ifdef HAVE_RUBY_ENCODING_H
5658
{
5759
GET_CLIENT(stmt_wrapper->client);
58-
#ifdef HAVE_RUBY_ENCODING_H
5960
conn_enc = rb_to_encoding(wrapper->encoding);
60-
#endif
6161
}
62+
#endif
6263

6364
rb_raise_mysql2_stmt_error2(stmt_wrapper->stmt
6465
#ifdef HAVE_RUBY_ENCODING_H

lib/mysql2/client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def query_info
9494
info_hash
9595
end
9696

97+
def info
98+
self.class.info
99+
end
100+
97101
private
98102
def self.local_offset
99103
::Time.local(2010).utc_offset.to_r / 86400

spec/mysql2/client_spec.rb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -711,23 +711,18 @@ def run_gc
711711
context "strings returned by #info" do
712712
before { pending('Encoding is undefined') unless defined?(Encoding) }
713713

714-
it "should default to the connection's encoding if Encoding.default_internal is nil" do
715-
with_internal_encoding nil do
716-
expect(@client.info[:version].encoding).to eql(Encoding::UTF_8)
717-
718-
client2 = Mysql2::Client.new(DatabaseCredentials['root'].merge(:encoding => 'ascii'))
719-
expect(client2.info[:version].encoding).to eql(Encoding::ASCII)
720-
end
714+
it "should be tagged as ascii" do
715+
expect(@client.info[:version].encoding).to eql(Encoding::US_ASCII)
716+
expect(@client.info[:header_version].encoding).to eql(Encoding::US_ASCII)
721717
end
718+
end
722719

723-
it "should use Encoding.default_internal" do
724-
with_internal_encoding Encoding::UTF_8 do
725-
expect(@client.info[:version].encoding).to eql(Encoding.default_internal)
726-
end
720+
context "strings returned by .info" do
721+
before { pending('Encoding is undefined') unless defined?(Encoding) }
727722

728-
with_internal_encoding Encoding::ASCII do
729-
expect(@client.info[:version].encoding).to eql(Encoding.default_internal)
730-
end
723+
it "should be tagged as ascii" do
724+
expect(Mysql2::Client.info[:version].encoding).to eql(Encoding::US_ASCII)
725+
expect(Mysql2::Client.info[:header_version].encoding).to eql(Encoding::US_ASCII)
731726
end
732727
end
733728

0 commit comments

Comments
 (0)