diff --git a/Rakefile b/Rakefile index 543d79061d6a9..2c2fb1fcf3ea9 100755 --- a/Rakefile +++ b/Rakefile @@ -24,6 +24,13 @@ Rake::ExtensionTask.new('grpc_c', spec) do |ext| ext.lib_dir = File.join('src', 'ruby', 'lib', 'grpc') ext.cross_compile = true ext.cross_platform = ['x86-mingw32', 'x64-mingw32'] + ext.cross_compiling do |spec| + spec.files = %w( etc/roots.pem grpc_c.32.ruby grpc_c.64.ruby ) + spec.files += Dir.glob('src/ruby/bin/**/*') + spec.files += Dir.glob('src/ruby/ext/**/*') + spec.files += Dir.glob('src/ruby/lib/**/*') + spec.files += Dir.glob('src/ruby/pb/**/*') + end end # Define the test suites diff --git a/src/ruby/ext/grpc/extconf.rb b/src/ruby/ext/grpc/extconf.rb index 84ee3f8364a73..b259f8146fdd2 100644 --- a/src/ruby/ext/grpc/extconf.rb +++ b/src/ruby/ext/grpc/extconf.rb @@ -54,6 +54,8 @@ LIBDIR ] +windows = RUBY_PLATFORM =~ /mingw|mswin/ + grpc_root = File.expand_path(File.join(File.dirname(__FILE__), '../../../..')) grpc_config = ENV['GRPC_CONFIG'] || 'opt' @@ -64,27 +66,16 @@ grpc_lib_dir = File.join(grpc_root, 'libs', grpc_config) end -unless File.exist?(File.join(grpc_lib_dir, 'libgrpc.a')) +unless File.exist?(File.join(grpc_lib_dir, 'libgrpc.a')) or windows for var in %w( CC AR ) do ENV[var] = RbConfig::CONFIG[var] end ENV['LD'] = ENV['CC'] - if RUBY_PLATFORM =~ /mingw|mswin/ - ENV['SYSTEM'] = 'MINGW32' - end - ENV['EMBED_OPENSSL'] = 'true' ENV['EMBED_ZLIB'] = 'true' - grpc_cppflags = '' - grpc_cppflags << ' -D_WIN32_WINNT=0x600 ' - grpc_cppflags << ' -DUNICODE ' - grpc_cppflags << ' -D_UNICODE ' - - ENV['CPPFLAGS'] = grpc_cppflags - output_dir = File.expand_path(RbConfig::CONFIG['topdir']) grpc_lib_dir = File.join(output_dir, 'libs', grpc_config) ENV['BUILDDIR'] = output_dir @@ -95,7 +86,7 @@ end $CFLAGS << ' -I' + File.join(grpc_root, 'include') -$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') +$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows if grpc_config == 'gcov' $CFLAGS << ' -O0 -fprofile-arcs -ftest-coverage' $LDFLAGS << ' -fprofile-arcs -ftest-coverage -rdynamic' @@ -108,11 +99,6 @@ $CFLAGS << ' -Werror ' $CFLAGS << ' -Wno-format ' -case RUBY_PLATFORM -when /mingw|mswin/ - $LDFLAGS << ' -static ' -end - output = File.join('grpc', 'grpc_c') puts 'Generating Makefile for ' + output create_makefile(output) diff --git a/src/ruby/ext/grpc/rb_byte_buffer.c b/src/ruby/ext/grpc/rb_byte_buffer.c index 0aa34c844e461..db7cac363a371 100644 --- a/src/ruby/ext/grpc/rb_byte_buffer.c +++ b/src/ruby/ext/grpc/rb_byte_buffer.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_byte_buffer.h" #include diff --git a/src/ruby/ext/grpc/rb_call.c b/src/ruby/ext/grpc/rb_call.c index 07864ebf599ee..af05ddf6e7869 100644 --- a/src/ruby/ext/grpc/rb_call.c +++ b/src/ruby/ext/grpc/rb_call.c @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_call.h" #include diff --git a/src/ruby/ext/grpc/rb_call_credentials.c b/src/ruby/ext/grpc/rb_call_credentials.c index ebcc6592c2d96..2426f106a99e0 100644 --- a/src/ruby/ext/grpc/rb_call_credentials.c +++ b/src/ruby/ext/grpc/rb_call_credentials.c @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_call_credentials.h" #include diff --git a/src/ruby/ext/grpc/rb_channel.c b/src/ruby/ext/grpc/rb_channel.c index 2fb8a5cdf006a..d1e48753f27b1 100644 --- a/src/ruby/ext/grpc/rb_channel.c +++ b/src/ruby/ext/grpc/rb_channel.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_channel.h" #include diff --git a/src/ruby/ext/grpc/rb_channel_args.c b/src/ruby/ext/grpc/rb_channel_args.c index 37dd981925ad5..69827cea1c426 100644 --- a/src/ruby/ext/grpc/rb_channel_args.c +++ b/src/ruby/ext/grpc/rb_channel_args.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_channel_args.h" #include diff --git a/src/ruby/ext/grpc/rb_channel_credentials.c b/src/ruby/ext/grpc/rb_channel_credentials.c index 3530081c6b29d..8c6fc3b7eb683 100644 --- a/src/ruby/ext/grpc/rb_channel_credentials.c +++ b/src/ruby/ext/grpc/rb_channel_credentials.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_channel_credentials.h" #include diff --git a/src/ruby/ext/grpc/rb_completion_queue.c b/src/ruby/ext/grpc/rb_completion_queue.c index a7de96d71893f..1eb5a28750086 100644 --- a/src/ruby/ext/grpc/rb_completion_queue.c +++ b/src/ruby/ext/grpc/rb_completion_queue.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_completion_queue.h" #include diff --git a/src/ruby/ext/grpc/rb_event_thread.c b/src/ruby/ext/grpc/rb_event_thread.c index 516f0bdad2ce1..2649a1087f205 100644 --- a/src/ruby/ext/grpc/rb_event_thread.c +++ b/src/ruby/ext/grpc/rb_event_thread.c @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_event_thread.h" #include diff --git a/src/ruby/ext/grpc/rb_grpc.c b/src/ruby/ext/grpc/rb_grpc.c index 9514d54c173fb..eefdb93d6734b 100644 --- a/src/ruby/ext/grpc/rb_grpc.c +++ b/src/ruby/ext/grpc/rb_grpc.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_grpc.h" #include @@ -45,6 +47,7 @@ #include "rb_channel.h" #include "rb_channel_credentials.h" #include "rb_completion_queue.h" +#include "rb_loader.h" #include "rb_server.h" #include "rb_server_credentials.h" @@ -298,6 +301,11 @@ VALUE sym_details = Qundef; VALUE sym_metadata = Qundef; void Init_grpc_c() { + if (!grpc_rb_load_core()) { + rb_raise(rb_eLoadError, "Couldn't find or load gRPC's dynamic C core"); + return; + } + grpc_init(); /* TODO: find alternative to ruby_vm_at_exit that is ok in Ruby 2.0 */ diff --git a/src/ruby/ext/grpc/rb_grpc_imports.generated.c b/src/ruby/ext/grpc/rb_grpc_imports.generated.c index a278b2ba997e6..23fc51b4e8e3e 100644 --- a/src/ruby/ext/grpc/rb_grpc_imports.generated.c +++ b/src/ruby/ext/grpc/rb_grpc_imports.generated.c @@ -31,6 +31,7 @@ * */ +#include #include #ifdef GPR_WIN32 diff --git a/src/ruby/ext/grpc/rb_loader.c b/src/ruby/ext/grpc/rb_loader.c new file mode 100644 index 0000000000000..242535f164ef6 --- /dev/null +++ b/src/ruby/ext/grpc/rb_loader.c @@ -0,0 +1,72 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include "rb_grpc_imports.generated.h" + +#if GPR_WIN32 +#include + +int grpc_rb_load_core() { +#if GPR_ARCH_64 + TCHAR fname[] = _T("grpc_c.64.ruby"); +#else + TCHAR fname[] = _T("grpc_c.32.ruby"); +#endif + HMODULE module = GetModuleHandle(_T("grpc_c.so")); + TCHAR path[2048 + 32] = _T(""); + LPTSTR seek_back = NULL; + GetModuleFileName(module, path, 2048); + + seek_back = _tcsrchr(path, _T('\\')); + + while (seek_back) { + HMODULE grpc_c; + _tcscpy(seek_back + 1, fname); + grpc_c = LoadLibrary(path); + if (grpc_c) { + grpc_rb_load_imports(grpc_c); + return 1; + } else { + *seek_back = _T('\0'); + seek_back = _tcsrchr(path, _T('\\')); + } + } + + return 0; +} + +#else + +int grpc_rb_load_core() { return 1; } + +#endif diff --git a/src/ruby/ext/grpc/rb_loader.h b/src/ruby/ext/grpc/rb_loader.h new file mode 100644 index 0000000000000..d0c1613a64596 --- /dev/null +++ b/src/ruby/ext/grpc/rb_loader.h @@ -0,0 +1,40 @@ +/* + * + * Copyright 2016, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#ifndef GRPC_RB_LOADER_H_ +#define GRPC_RB_LOADER_H_ + +/* Attempts to load the core if necessary, and return non-zero upon succes. */ +int grpc_rb_load_core(); + +#endif /* GRPC_RB_BYTE_BUFFER_H_ */ diff --git a/src/ruby/ext/grpc/rb_server.c b/src/ruby/ext/grpc/rb_server.c index ebdd7e1a346a2..37cc55a6514e3 100644 --- a/src/ruby/ext/grpc/rb_server.c +++ b/src/ruby/ext/grpc/rb_server.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_server.h" #include diff --git a/src/ruby/ext/grpc/rb_server_credentials.c b/src/ruby/ext/grpc/rb_server_credentials.c index de57585e0baec..f866aecd289ab 100644 --- a/src/ruby/ext/grpc/rb_server_credentials.c +++ b/src/ruby/ext/grpc/rb_server_credentials.c @@ -1,6 +1,6 @@ /* * - * Copyright 2015, Google Inc. + * Copyright 2015-2016, Google Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -31,6 +31,8 @@ * */ +#include +#include "rb_grpc_imports.generated.h" #include "rb_server_credentials.h" #include