Skip to content

Commit

Permalink
ruby: build native Darwin gems using rake-compiler-dock
Browse files Browse the repository at this point in the history
Please note that this work simplifies the Darwin rubygem build,
and addresses inconsistencies in packaging that result in issues
like grpc#25060, but introduces some notable changes:

- ships "x86_64-darwin" and "arm64-darwin" platform gems
- stops shipping "universal-darwin" platform gem
- drop support for i386 darwin

It's unclear to me whether grpc should continue to support i386
Darwin, and so I'm open to learning more about this.

Related to:

- grpc#25429
- grpc#25755
- grpc#25756
  • Loading branch information
flavorjones committed Mar 23, 2021
1 parent a540a4a commit ea8b452
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 55 deletions.
69 changes: 28 additions & 41 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,15 @@ end

# Add the extension compiler task
Rake::ExtensionTask.new('grpc_c', spec) do |ext|
unless RUBY_PLATFORM =~ /darwin/
# TODO: also set "no_native to true" for mac if possible. As is,
# "no_native" can only be set if the RUBY_PLATFORM doing
# cross-compilation is contained in the "ext.cross_platform" array.
ext.no_native = true
end
ext.no_native = true
ext.source_pattern = '**/*.{c,h}'
ext.ext_dir = File.join('src', 'ruby', 'ext', 'grpc')
ext.lib_dir = File.join('src', 'ruby', 'lib', 'grpc')
ext.cross_compile = true
ext.cross_platform = [
'x86-mingw32', 'x64-mingw32',
'x86_64-linux', 'x86-linux',
'universal-darwin'
'x86_64-darwin', 'arm64-darwin',
]
ext.cross_compiling do |spec|
spec.files = %w( etc/roots.pem grpc_c.32.ruby grpc_c.64.ruby )
Expand Down Expand Up @@ -123,40 +118,32 @@ task 'gem:native' do
grpc_config = ENV['GRPC_CONFIG'] || 'opt'
ruby_cc_versions = ['3.0.0', '2.7.0', '2.6.0', '2.5.0', '2.4.0'].join(':')

if RUBY_PLATFORM =~ /darwin/
FileUtils.touch 'grpc_c.32.ruby'
FileUtils.touch 'grpc_c.64.ruby'
unless '2.5' == /(\d+\.\d+)/.match(RUBY_VERSION).to_s
fail "rake gem:native (the rake task to build the binary packages) is being " \
"invoked on macos with ruby #{RUBY_VERSION}. The ruby macos artifact " \
"build should be running on ruby 2.5."
end
system "rake cross native gem RUBY_CC_VERSION=#{ruby_cc_versions} V=#{verbose} GRPC_CONFIG=#{grpc_config}"
else
Rake::Task['dlls'].execute
['x86-mingw32', 'x64-mingw32'].each do |plat|
run_rake_compiler plat, <<-EOT
gem update --system --no-document && \
bundle && \
rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem pkg/#{spec.full_name}.gem \
RUBY_CC_VERSION=#{ruby_cc_versions} \
V=#{verbose} \
GRPC_CONFIG=#{grpc_config}
EOT
end
# Truncate grpc_c.*.ruby files because they're for Windows only.
File.truncate('grpc_c.32.ruby', 0)
File.truncate('grpc_c.64.ruby', 0)
['x86_64-linux', 'x86-linux'].each do |plat|
run_rake_compiler plat, <<-EOT
gem update --system --no-document && \
bundle && \
rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem pkg/#{spec.full_name}.gem \
RUBY_CC_VERSION=#{ruby_cc_versions} \
V=#{verbose} \
GRPC_CONFIG=#{grpc_config}
EOT
end
FileUtils.touch 'grpc_c.32.ruby'
FileUtils.touch 'grpc_c.64.ruby'
Rake::Task['dlls'].execute
['x86-mingw32', 'x64-mingw32'].each do |plat|
run_rake_compiler plat, <<-EOT
gem update --system --no-document && \
bundle && \
rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem pkg/#{spec.full_name}.gem \
RUBY_CC_VERSION=#{ruby_cc_versions} \
V=#{verbose} \
GRPC_CONFIG=#{grpc_config}
EOT
end

# Truncate grpc_c.*.ruby files because they're for Windows only.
File.truncate('grpc_c.32.ruby', 0)
File.truncate('grpc_c.64.ruby', 0)
['x86_64-linux', 'x86-linux', 'x86_64-darwin', 'arm64-darwin'].each do |plat|
run_rake_compiler plat, <<-EOT
gem update --system --no-document && \
bundle && \
rake native:#{plat} pkg/#{spec.full_name}-#{plat}.gem pkg/#{spec.full_name}.gem \
RUBY_CC_VERSION=#{ruby_cc_versions} \
V=#{verbose} \
GRPC_CONFIG=#{grpc_config}
EOT
end
end

Expand Down
15 changes: 1 addition & 14 deletions src/ruby/ext/grpc/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,11 @@
ENV['LD'] = ENV['CC']
end

if RUBY_PLATFORM =~ /darwin/
ENV['AR'] = 'libtool'
ENV['ARFLAGS'] = '-o'
end

ENV['EMBED_OPENSSL'] = 'true'
ENV['EMBED_ZLIB'] = 'true'
ENV['EMBED_CARES'] = 'true'

ENV['ARCH_FLAGS'] = RbConfig::CONFIG['ARCH_FLAG']
if RUBY_PLATFORM =~ /darwin/
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'

output_dir = File.expand_path(RbConfig::CONFIG['topdir'])
Expand Down Expand Up @@ -100,7 +87,7 @@
create_makefile(output)

strip_tool = RbConfig::CONFIG['STRIP']
strip_tool = 'strip -x' if RUBY_PLATFORM =~ /darwin/
strip_tool += ' -x' if RUBY_PLATFORM =~ /darwin/

if grpc_config == 'opt'
File.open('Makefile.new', 'w') do |o|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM larskanis/rake-compiler-dock-mri-arm64-darwin:1.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM larskanis/rake-compiler-dock-mri-x86_64-darwin:1.1.0

0 comments on commit ea8b452

Please sign in to comment.