Skip to content

Commit

Permalink
Make the gem build on TruffleRuby (#27660)
Browse files Browse the repository at this point in the history
* replace darwin checks in extconf.rb to exclude TruffleRuby

* inherit RANLIB and STRIP from RbConfig, set LDXX

* enable overriding ranlib command in top-level makefile

* ensure the -no_warning_for_no_symbols flag is only used with Apple's ranlib

* don't embed openssl & zlib on truffleruby

* add RbConfig's cppflag to CPPFLAGS when using TruffleRuby

* this ensure the paths to find the system's OpenSSL are set up correctly with TruffleRuby (includes being able to find an OpenSSL installed via Homebrew etc)

* don't statically link standard libraries on Linux with Truffleruby

* This does not work when compiling to bitcode.

* Prefer SIGTERM to SIGQUIT for graceful shutdown in examples

* Overriding SIGQUIT is suboptimal, for example on JVM where it is very
  useful to dump the thread stacktraces.

* Keep the rb_tr_abi_version symbol for TruffleRuby in grpc_c.so

* Otherwise TruffleRuby cannot verify the ABI version is correct.
* See oracle/truffleruby#2386

* Use RbConfig::CONFIG['STRIP'] instead of just `strip`

* Use a local variable for apple_toolchain for consistency

* Add a comment about -static-libgcc -static-libstdc++ and TruffleRuby

* Split comment into two for openssl/zlib

Co-authored-by: Nicolas Laurent <nicolas.laurent@oracle.com>
  • Loading branch information
eregon and Nicolas Laurent committed Jun 28, 2022
1 parent eb5ae61 commit 0a5d982
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 33 deletions.
27 changes: 17 additions & 10 deletions Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/ruby/greeter_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ def main
s = GRPC::RpcServer.new
s.add_http2_port('0.0.0.0:50051', :this_port_is_insecure)
s.handle(GreeterServer)
# Runs the server with SIGHUP, SIGINT and SIGQUIT signal handlers to
# Runs the server with SIGHUP, SIGINT and SIGTERM signal handlers to
# gracefully shutdown.
# User could also choose to run server via call to run_till_terminated
s.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
s.run_till_terminated_or_interrupted([1, 'int', 'SIGTERM'])
end

main
4 changes: 2 additions & 2 deletions examples/ruby/route_guide/route_guide_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ def main
s.add_http2_port(port, :this_port_is_insecure)
GRPC.logger.info("... running insecurely on #{port}")
s.handle(ServerImpl.new(feature_db))
# Runs the server with SIGHUP, SIGINT and SIGQUIT signal handlers to
# Runs the server with SIGHUP, SIGINT and SIGTERM signal handlers to
# gracefully shutdown.
# User could also choose to run server via call to run_till_terminated
s.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
s.run_till_terminated_or_interrupted([1, 'int', 'SIGTERM'])
end

main
2 changes: 2 additions & 0 deletions src/ruby/ext/grpc/ext-export-truffleruby.clang
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_Init_grpc_c
_rb_tr_abi_version
7 changes: 7 additions & 0 deletions src/ruby/ext/grpc/ext-export-truffleruby.gcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
grpc_1.0 {
global:
Init_grpc_c;
rb_tr_abi_version;
local:
*;
};
65 changes: 47 additions & 18 deletions src/ruby/ext/grpc/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,74 @@
darwin = RUBY_PLATFORM =~ /darwin/
linux = RUBY_PLATFORM =~ /linux/
cross_compiling = ENV['RCD_HOST_RUBY_VERSION'] # set by rake-compiler-dock in build containers
# TruffleRuby uses the Sulong LLVM runtime, which is different from Apple's.
apple_toolchain = darwin && RUBY_ENGINE != 'truffleruby'

grpc_root = File.expand_path(File.join(File.dirname(__FILE__), '../../../..'))

grpc_config = ENV['GRPC_CONFIG'] || 'opt'

ENV['MACOSX_DEPLOYMENT_TARGET'] = '10.10'

if ENV['AR'].nil? || ENV['AR'].size == 0
ENV['AR'] = RbConfig::CONFIG['AR']
def env_unset?(name)
ENV[name].nil? || ENV[name].size == 0
end
if ENV['CC'].nil? || ENV['CC'].size == 0
ENV['CC'] = RbConfig::CONFIG['CC']

def rbconfig_set?(name)
RbConfig::CONFIG[name] && RbConfig::CONFIG[name].size > 0
end
if ENV['CXX'].nil? || ENV['CXX'].size == 0
ENV['CXX'] = RbConfig::CONFIG['CXX']

def inherit_rbconfig(name)
ENV[name] = RbConfig::CONFIG[name] if env_unset?(name) && rbconfig_set?(name)
end
if ENV['LD'].nil? || ENV['LD'].size == 0
ENV['LD'] = ENV['CC']

def env_append(name, string)
ENV[name] ||= ''
ENV[name] += ' ' + string
end

if darwin && !cross_compiling
inherit_rbconfig 'AR'
inherit_rbconfig 'CC'
inherit_rbconfig 'CXX'
inherit_rbconfig 'RANLIB'
inherit_rbconfig 'STRIP'
inherit_rbconfig 'CPPFLAGS'
inherit_rbconfig 'LDFLAGS'

ENV['LD'] = ENV['CC'] if env_unset?('LD')
ENV['LDXX'] = ENV['CXX'] if env_unset?('LDXX')

if RUBY_ENGINE == 'truffleruby'
# ensure we can find the system's OpenSSL
env_append 'CPPFLAGS', RbConfig::CONFIG['cppflags']
end

if apple_toolchain && !cross_compiling
ENV['AR'] = 'libtool'
ENV['ARFLAGS'] = '-o'
end

ENV['EMBED_OPENSSL'] = 'true'
ENV['EMBED_ZLIB'] = 'true'
# Don't embed on TruffleRuby (constant-time crypto is unsafe with Sulong, slow build times)
ENV['EMBED_OPENSSL'] = (RUBY_ENGINE != 'truffleruby').to_s
# Don't embed on TruffleRuby (the system zlib is already linked for the zlib C extension, slow build times)
ENV['EMBED_ZLIB'] = (RUBY_ENGINE != 'truffleruby').to_s

ENV['EMBED_CARES'] = 'true'

ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG']
if darwin && !cross_compiling
if apple_toolchain && !cross_compiling
if RUBY_PLATFORM =~ /arm64/
ENV['ARCH_FLAGS'] = '-arch arm64'
else
ENV['ARCH_FLAGS'] = '-arch i386 -arch x86_64'
end
end

ENV['CPPFLAGS'] = '-DGPR_BACKWARDS_COMPATIBILITY_MODE'
ENV['CPPFLAGS'] += ' -DGRPC_XDS_USER_AGENT_NAME_SUFFIX="\"RUBY\"" '
env_append 'CPPFLAGS', '-DGPR_BACKWARDS_COMPATIBILITY_MODE'
env_append 'CPPFLAGS', '-DGRPC_XDS_USER_AGENT_NAME_SUFFIX="\"RUBY\""'

require_relative '../../lib/grpc/version'
ENV['CPPFLAGS'] += ' -DGRPC_XDS_USER_AGENT_VERSION_SUFFIX="\"' + GRPC::VERSION + '\"" '
env_append 'CPPFLAGS', '-DGRPC_XDS_USER_AGENT_VERSION_SUFFIX="\"' + GRPC::VERSION + '\""'

output_dir = File.expand_path(RbConfig::CONFIG['topdir'])
grpc_lib_dir = File.join(output_dir, 'libs', grpc_config)
Expand All @@ -87,8 +112,9 @@
$CFLAGS << ' -I' + File.join(grpc_root, 'include')

ext_export_file = File.join(grpc_root, 'src', 'ruby', 'ext', 'grpc', 'ext-export')
ext_export_file += '-truffleruby' if RUBY_ENGINE == 'truffleruby'
$LDFLAGS << ' -Wl,--version-script="' + ext_export_file + '.gcc"' if linux
$LDFLAGS << ' -Wl,-exported_symbols_list,"' + ext_export_file + '.clang"' if darwin
$LDFLAGS << ' -Wl,-exported_symbols_list,"' + ext_export_file + '.clang"' if apple_toolchain

$LDFLAGS << ' ' + File.join(grpc_lib_dir, 'libgrpc.a') unless windows
if grpc_config == 'gcov'
Expand All @@ -101,7 +127,10 @@
end

$LDFLAGS << ' -Wl,-wrap,memcpy' if linux
$LDFLAGS << ' -static-libgcc -static-libstdc++' if linux
# Do not statically link standard libraries on TruffleRuby as this does not work when compiling to bitcode
if linux && RUBY_ENGINE != 'truffleruby'
$LDFLAGS << ' -static-libgcc -static-libstdc++'
end
$LDFLAGS << ' -static' if windows

$CFLAGS << ' -std=c11 '
Expand All @@ -114,7 +143,7 @@
create_makefile(output)

strip_tool = RbConfig::CONFIG['STRIP']
strip_tool += ' -x' if darwin
strip_tool += ' -x' if apple_toolchain

if grpc_config == 'opt'
File.open('Makefile.new', 'w') do |o|
Expand Down
9 changes: 8 additions & 1 deletion templates/Makefile.template
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@
INSTALL ?= install
RM ?= rm -f
PKG_CONFIG ?= pkg-config
RANLIB ?= ranlib
ifeq ($(SYSTEM),Darwin)
APPLE_RANLIB = $(shell [[ "`$(RANLIB) -V 2>/dev/null`" == "Apple Inc."* ]]; echo $$?)
ifeq ($(APPLE_RANLIB),0)
RANLIBFLAGS = -no_warning_for_no_symbols
endif
endif

ifndef VALID_CONFIG_$(CONFIG)
$(error Invalid CONFIG value '$(CONFIG)')
Expand Down Expand Up @@ -941,7 +948,7 @@
% endif

ifeq ($(SYSTEM),Darwin)
$(Q) ranlib -no_warning_for_no_symbols $(LIBDIR)/$(CONFIG)/lib${lib.name}.a
$(Q) $(RANLIB) $(RANLIBFLAGS) $(LIBDIR)/$(CONFIG)/lib${lib.name}.a
endif

<%
Expand Down

0 comments on commit 0a5d982

Please sign in to comment.