From d51d121c0c52205393ae607ebfab5e50b5c9a491 Mon Sep 17 00:00:00 2001 From: "Nicolas \"Pixel\" Noble" Date: Sun, 31 Jan 2016 11:33:19 +0100 Subject: And the Windows extension finally loads. --- Rakefile | 7 +++ src/ruby/ext/grpc/extconf.rb | 22 ++------ src/ruby/ext/grpc/rb_byte_buffer.c | 4 +- src/ruby/ext/grpc/rb_call.c | 2 + src/ruby/ext/grpc/rb_call_credentials.c | 2 + src/ruby/ext/grpc/rb_channel.c | 4 +- src/ruby/ext/grpc/rb_channel_args.c | 4 +- src/ruby/ext/grpc/rb_channel_credentials.c | 4 +- src/ruby/ext/grpc/rb_completion_queue.c | 4 +- src/ruby/ext/grpc/rb_event_thread.c | 2 + src/ruby/ext/grpc/rb_grpc.c | 10 +++- src/ruby/ext/grpc/rb_grpc_imports.generated.c | 1 + src/ruby/ext/grpc/rb_loader.c | 72 +++++++++++++++++++++++++++ src/ruby/ext/grpc/rb_loader.h | 40 +++++++++++++++ src/ruby/ext/grpc/rb_server.c | 4 +- src/ruby/ext/grpc/rb_server_credentials.c | 4 +- 16 files changed, 160 insertions(+), 26 deletions(-) create mode 100644 src/ruby/ext/grpc/rb_loader.c create mode 100644 src/ruby/ext/grpc/rb_loader.h diff --git a/Rakefile b/Rakefile index 543d79061d..2c2fb1fcf3 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 84ee3f8364..b259f8146f 100644 --- a/src/ruby/ext/grpc/extconf.rb +++ b/src/ruby/ext/grpc/extconf.rb @@ -54,6 +54,8 @@ LIB_DIRS = [ 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 @@ else 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 @@ unless File.exist?(File.join(grpc_lib_dir, 'libgrpc.a')) 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 << ' -pedantic ' $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 0aa34c844e..db7cac363a 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 07864ebf59..af05ddf6e7 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 ebcc6592c2..2426f106a9 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 2fb8a5cdf0..d1e48753f2 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 37dd981925..69827cea1c 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 3530081c6b..8c6fc3b7eb 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 a7de96d718..1eb5a28750 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 516f0bdad2..2649a1087f 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 9514d54c17..eefdb93d67 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 a278b2ba99..23fc51b4e8 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 0000000000..242535f164 --- /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 0000000000..d0c1613a64 --- /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 ebdd7e1a34..37cc55a651 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 de57585e0b..f866aecd28 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 -- cgit v1.2.3