Skip to content

Commit

Permalink
And the Windows extension finally loads.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasnoble committed Jan 31, 2016
1 parent 763cc08 commit d51d121
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 26 deletions.
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 4 additions & 18 deletions src/ruby/ext/grpc/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/ruby/ext/grpc/rb_byte_buffer.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_byte_buffer.h"

#include <ruby/ruby.h>
Expand Down
2 changes: 2 additions & 0 deletions src/ruby/ext/grpc/rb_call.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_call.h"

#include <ruby/ruby.h>
Expand Down
2 changes: 2 additions & 0 deletions src/ruby/ext/grpc/rb_call_credentials.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_call_credentials.h"

#include <ruby/ruby.h>
Expand Down
4 changes: 3 additions & 1 deletion src/ruby/ext/grpc/rb_channel.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_channel.h"

#include <ruby/ruby.h>
Expand Down
4 changes: 3 additions & 1 deletion src/ruby/ext/grpc/rb_channel_args.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_channel_args.h"

#include <ruby/ruby.h>
Expand Down
4 changes: 3 additions & 1 deletion src/ruby/ext/grpc/rb_channel_credentials.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_channel_credentials.h"

#include <ruby/ruby.h>
Expand Down
4 changes: 3 additions & 1 deletion src/ruby/ext/grpc/rb_completion_queue.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_completion_queue.h"

#include <ruby/ruby.h>
Expand Down
2 changes: 2 additions & 0 deletions src/ruby/ext/grpc/rb_event_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_event_thread.h"

#include <stdbool.h>
Expand Down
10 changes: 9 additions & 1 deletion src/ruby/ext/grpc/rb_grpc.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_grpc.h"

#include <math.h>
Expand All @@ -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"

Expand Down Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions src/ruby/ext/grpc/rb_grpc_imports.generated.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
*/

#include <ruby/ruby.h>
#include <grpc/support/port_platform.h>

#ifdef GPR_WIN32
Expand Down
72 changes: 72 additions & 0 deletions src/ruby/ext/grpc/rb_loader.c
Original file line number Diff line number Diff line change
@@ -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 <tchar.h>

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
40 changes: 40 additions & 0 deletions src/ruby/ext/grpc/rb_loader.h
Original file line number Diff line number Diff line change
@@ -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_ */
4 changes: 3 additions & 1 deletion src/ruby/ext/grpc/rb_server.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_server.h"

#include <ruby/ruby.h>
Expand Down
4 changes: 3 additions & 1 deletion src/ruby/ext/grpc/rb_server_credentials.c
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,6 +31,8 @@
*
*/

#include <ruby/ruby.h>
#include "rb_grpc_imports.generated.h"
#include "rb_server_credentials.h"

#include <ruby/ruby.h>
Expand Down

0 comments on commit d51d121

Please sign in to comment.