Skip to content

Commit

Permalink
Merge pull request #2082 from sparklemotion/2079-flavorjones-universa…
Browse files Browse the repository at this point in the history
…l-darwin-gems

gem: attempt to build a darwin gem that works on multiple darwin versions
  • Loading branch information
flavorjones committed Dec 3, 2020
2 parents b54c3fb + a7e93f1 commit 609297d
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 15 deletions.
8 changes: 4 additions & 4 deletions .cross_rubies
Expand Up @@ -2,19 +2,19 @@
2.7.0:x86_64-w64-mingw32
2.7.0:i686-linux-gnu
2.7.0:x86_64-linux-gnu
2.7.0:x86_64-darwin19
2.7.0:x86_64-darwin
2.6.0:i686-w64-mingw32
2.6.0:x86_64-w64-mingw32
2.6.0:i686-linux-gnu
2.6.0:x86_64-linux-gnu
2.6.0:x86_64-darwin19
2.6.0:x86_64-darwin
2.5.0:i686-w64-mingw32
2.5.0:x86_64-w64-mingw32
2.5.0:i686-linux-gnu
2.5.0:x86_64-linux-gnu
2.5.0:x86_64-darwin19
2.5.0:x86_64-darwin
2.4.0:i686-w64-mingw32
2.4.0:x86_64-w64-mingw32
2.4.0:i686-linux-gnu
2.4.0:x86_64-linux-gnu
2.4.0:x86_64-darwin19
2.4.0:x86_64-darwin
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.DS_Store
*.gemspec
*.tmproj
*~
Expand Down
1 change: 1 addition & 0 deletions .hoerc
Expand Up @@ -6,6 +6,7 @@ exclude: !ruby/regexp '/
\.github|
\.vagrant|
\.yardoc|
\.DS_Store|
concourse|
gems|
patches|
Expand Down
12 changes: 6 additions & 6 deletions rakelib/cross-ruby.rake
Expand Up @@ -46,8 +46,8 @@ CrossRuby = Struct.new(:version, :host) do
"x86_64-linux"
when /\Ai[3-6]86.*linux/
"x86-linux"
when /\Ax86_64-darwin19/
"x86_64-darwin19"
when /\Ax86_64-darwin/
"x86_64-darwin"
else
raise "CrossRuby.platform: unsupported host: #{host}"
end
Expand All @@ -63,8 +63,8 @@ CrossRuby = Struct.new(:version, :host) do
"x86_64-linux-gnu-"
when "x86-linux"
"i686-linux-gnu-"
when /darwin/
""
# when /darwin/
# ""
else
raise "CrossRuby.tool: unmatched platform: #{platform}"
end) + name
Expand All @@ -80,8 +80,8 @@ CrossRuby = Struct.new(:version, :host) do
"elf64-x86-64"
when "x86-linux"
"elf32-i386"
when "x86_64-darwin19"
"Mach-O 64-bit x86-64"
when "x86_64-darwin"
"Mach-O 64-bit x86-64" # hmm
else
raise "CrossRuby.target_file_format: unmatched platform: #{platform}"
end
Expand Down
14 changes: 9 additions & 5 deletions scripts/setup-osx-native-builders
Expand Up @@ -4,6 +4,9 @@
# - https://github.com/rake-compiler/rake-compiler/blob/master/tasks/bin/cross-ruby.rake
# - https://github.com/apolcyn/grpc/blob/master/tools/distrib/build_ruby_environment_macos.sh

# see test/test_gem_platform.rb for context
gem_platform_name="x86_64-darwin"

# chosen so chruby will see it, should you be a user of chruby
RUBIES_DIR=${HOME}/.rubies

Expand All @@ -18,11 +21,12 @@ set -o pipefail

CROSS_FILE=".cross_rubies"
RUBIES=$(cat $CROSS_FILE | fgrep darwin | cut -d. -f1,2 | sort -u)

RAKE_COMPILER_CONFIG_DIR=${HOME}/.rake-compiler
RAKE_COMPILER_CONFIG=${RAKE_COMPILER_CONFIG_DIR}/config.yml
TMP_CONFIG=$(mktemp /tmp/rake-compiler-config.XXXXXXXX)

mkdir -p $RAKE_COMPILER_CONFIG_DIR
echo "---" > $RAKE_COMPILER_CONFIG
echo "---" > $TMP_CONFIG

for ruby in $RUBIES ; do
ruby_fullname="native-builder-${ruby}"
Expand All @@ -42,9 +46,9 @@ for ruby in $RUBIES ; do
echo "ruby $ruby_fullname is installed"
fi

platform=$(basename $(dirname $rbconfig))

echo "rbconfig-${platform}-${ruby_minor}: \"${rbconfig}\"" >> $RAKE_COMPILER_CONFIG
echo "rbconfig-${gem_platform_name}-${ruby_minor}: \"${rbconfig}\"" >> $TMP_CONFIG
done

mkdir -p $RAKE_COMPILER_CONFIG_DIR
cp $TMP_CONFIG $RAKE_COMPILER_CONFIG
cat $RAKE_COMPILER_CONFIG
69 changes: 69 additions & 0 deletions test/test_gem_platform.rb
@@ -0,0 +1,69 @@
require "helper"

class TestGemPlatform < Nokogiri::TestCase
# These tests were written to help me better understand the behavior
# of Rubygems's platform-matching behavior. They don't exercise code
# in Nokogiri, but do express the expectations we have of Rubygems
# and `gem install` behavior for our native darwin gem.
#
# More specifically, the grpc gem ships a native darwin gem with
# platform string `universal-darwin` which I *think* refers to the
# fact that it supports i386-and-x86_64 architectures (and not that
# it supports x86_64-and-arm64 architectures).
#
# With ARM support imminent [1] I'm a little worried about using
# `universal` until I understand better whether ARM support will be
# implied by `universal` or `universal2` or what. From
# https://en.wikipedia.org/wiki/Universal_binary:
#
# > The new Universal 2 binary format was introduced at the 2020
# > Worldwide Developers Conference. Universal 2 allows applications
# > to run on both Intel x86-64-based and ARM64-based Macintosh
# > computers, for the Mac transition to Apple Silicon.
#
# [1]: https://arstechnica.com/gadgets/2020/06/this-is-apples-roadmap-for-moving-the-first-macs-away-from-intel/
#
# There seems to be a lot of uncertainty right now, so I'm writing
# some tests to express my expectations, and we're gonig to
# conservatively use `x86_64-darwin` as the platform name for the
# native gem.
let(:darwin_gem_platform) { Gem::Platform.new("x86_64-darwin") }

describe "darwin" do
it "builds a gem that works on all darwin x86+64 platforms" do
assert_match darwin_gem_platform,
Gem::Platform.new(["universal", "darwin", "19"]),
"gem should match system-installed ruby on catalina"
assert_match darwin_gem_platform,
Gem::Platform.new(["x86_64", "darwin", "19"]),
"gem should match user-installed ruby on catalina"
assert_match darwin_gem_platform,
Gem::Platform.new(["x86_64", "darwin", "18"]),
"gem should match user-installed ruby on mojave"

# The intention here is to test that the x86_64 platform gems
# won't match a future ruby that is compiled on arm64/aarch64.
#
# I don't know what the future platform will call itself, but at
# least one of these should be right and none of them should
# match, so here are all the tests I can reasonably imagine.
#
# Feel free to delete assertions for clarity once we know more.
assert_no_match darwin_gem_platform,
Gem::Platform.new(["arm64", "darwin", "19"]),
"gem should not match an arm64 ruby"
assert_no_match darwin_gem_platform,
Gem::Platform.new(["arm", "darwin", "19"]),
"gem should not match an arm ruby"
assert_no_match darwin_gem_platform,
Gem::Platform.new(["aarch64", "darwin", "19"]),
"gem should not match an aarch64 ruby"
assert_no_match darwin_gem_platform,
Gem::Platform.new(["universal2", "darwin", "19"]),
"gem should not match an aarch64 ruby"
assert_no_match darwin_gem_platform,
Gem::Platform.new(["universal_2", "darwin", "19"]),
"gem should not match an aarch64 ruby"
end
end
end

0 comments on commit 609297d

Please sign in to comment.