Skip to content

Commit 4292f1b

Browse files
authored
Support for Apple M1. (#37)
Release pipeline prepares arm64-darwin gem. Alter rake tasks so that macOS can cross-compile arm64 or x86_64. We need to support cross-compile because we don't have access to arm64 macOS cloud machines.
1 parent 882b10d commit 4292f1b

File tree

7 files changed

+75
-28
lines changed

7 files changed

+75
-28
lines changed

.github/workflows/ci.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,20 @@ jobs:
8888
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
8989
chmod a+x builder
9090
./builder build -p ${{ env.PACKAGE_NAME }} downstream
91+
92+
# Cross-compile for Apple silicon.
93+
# It would be better to run tests natively on one of these machines, but we
94+
# don't currently have access to one in the cloud.
95+
osx-arm64-cross-compile:
96+
runs-on: macos-latest
97+
steps:
98+
- uses: ruby/setup-ruby@v1
99+
with:
100+
ruby-version: 2.7
101+
- uses: actions/checkout@v3
102+
with:
103+
submodules: recursive
104+
- name: Build ${{ env.PACKAGE_NAME }} + consumers
105+
run: |
106+
bundle install
107+
bundle exec rake "gem:aws-crt:platform[arm64]"

.github/workflows/release.yml

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ name: Release
33
on:
44
push:
55
branches:
6-
- '*' # TODO: Remove after testing...
7-
- 'master'
6+
- 'main'
87

98
env:
109
BUILDER_VERSION: v0.8.19
@@ -78,25 +77,29 @@ jobs:
7877
matrix:
7978
os: [macos]
8079
ruby: [2.7]
81-
arch: [x64]
80+
arch: [x86_64, arm64]
8281
runs-on: macos-latest
8382
steps:
8483
- uses: ruby/setup-ruby@v1
8584
with:
8685
ruby-version: ${{ matrix.ruby }}
86+
87+
- uses: actions/checkout@v3
88+
with:
89+
submodules: recursive
90+
8791
- name: Generate Gem artifacts
8892
run: |
89-
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder')"
90-
chmod a+x builder
91-
./builder build -p ${{ env.PACKAGE_NAME }} downstream
93+
bundle install
94+
bundle exec rake "gem:aws-crt:platform[${{ matrix.arch }}]"
9295
9396
- name: Upload artifacts
9497
uses: actions/upload-artifact@v2
9598
with:
9699
name: out_${{ matrix.os }}_${{ matrix.arch }}
97100
path: |
98-
aws-crt-ruby/gems/aws-crt/bin/
99-
aws-crt-ruby/gems/aws-crt/pkg/
101+
gems/aws-crt/bin/
102+
gems/aws-crt/pkg/
100103
101104
package:
102105
needs: [windows, osx, linux]
@@ -115,10 +118,15 @@ jobs:
115118
with:
116119
name: out_windows_x64
117120

118-
- name: Download OSX
121+
- name: Download OSX x64
122+
uses: actions/download-artifact@v2
123+
with:
124+
name: out_macos_x86_64
125+
126+
- name: Download OSX arm64
119127
uses: actions/download-artifact@v2
120128
with:
121-
name: out_macos_x64
129+
name: out_macos_arm64
122130

123131
- name: Download Linux x64
124132
uses: actions/download-artifact@v2

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ reported the issue. Please try to include as much information as you can. Detail
2323
## Contributing via Pull Requests
2424
Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that:
2525

26-
1. You are working against the latest source on the *master* branch.
26+
1. You are working against the latest source on the *main* branch.
2727
2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already.
2828
3. You open an issue to discuss any significant work - we would hate for your time to be wasted.
2929

gems/aws-crt/ext/compile.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,16 @@ def find_file(name, search_dirs, base_dir)
4141
end
4242

4343
# Compile bin to expected location
44-
def compile_bin
45-
platform = local_platform
44+
def compile_bin(cpu = host_cpu)
45+
platform = target_platform(cpu)
4646
native_dir = File.expand_path('../aws-crt-ffi', File.dirname(__FILE__))
47-
tmp_build_dir = File.expand_path('../tmp/build', File.dirname(__FILE__))
47+
tmp_dir = File.expand_path("../tmp/#{platform.cpu}", File.dirname(__FILE__))
48+
tmp_build_dir = File.expand_path('build', tmp_dir)
4849

4950
# We need cmake to "install" aws-crt-ffi so that the binaries end up in a
5051
# predictable location. But cmake still adds subdirectories we don't want,
5152
# so we'll "install" under tmp, and manually copy to bin/ after that.
52-
tmp_install_dir = File.expand_path('../tmp/install', File.dirname(__FILE__))
53+
tmp_install_dir = File.expand_path('install', tmp_dir)
5354

5455
build_type = 'RelWithDebInfo'
5556

@@ -59,8 +60,15 @@ def compile_bin
5960
"-B#{tmp_build_dir}",
6061
"-DCMAKE_INSTALL_PREFIX=#{tmp_install_dir}",
6162
"-DCMAKE_BUILD_TYPE=#{build_type}",
63+
'-DBUILD_TESTING=OFF',
6264
]
6365

66+
# macOS can cross-compile for arm64 or x86_64.
67+
# This lets us prepare both types of gems from either type of machine.
68+
if platform.os == 'darwin'
69+
config_cmd.append("-DCMAKE_OSX_ARCHITECTURES=#{platform.cpu}")
70+
end
71+
6472
build_cmd = [
6573
CMAKE,
6674
'--build', tmp_build_dir,
@@ -87,5 +95,5 @@ def compile_bin
8795
'lib', # some unix variants
8896
]
8997
tmp_path = find_file(bin_name, search_dirs, tmp_install_dir)
90-
FileUtils.cp(tmp_path, bin_dir)
98+
FileUtils.cp(tmp_path, bin_dir, verbose: true)
9199
end

gems/aws-crt/lib/aws-crt/platforms.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99

1010
DEFAULT_BINARY = 'libaws-crt-ffi.so'
1111

12-
# @return [String] returns Gem::Platform style name for the current system
13-
# similar to Gem::Platform.local but will return systems host os/cpu
14-
# for Jruby
12+
# @return [Gem::Platform] similar to Gem::Platform.local but will return
13+
# host os/cpu for Jruby
1514
def local_platform
1615
Gem::Platform.new(host_string)
1716
end
1817

18+
# @return [Gem::Platform] return Gem::Platform for host os with target cpu
19+
def target_platform(cpu)
20+
Gem::Platform.new(target_string(cpu))
21+
end
22+
1923
# @return [String] return the file name for the CRT library for the platform
2024
def crt_bin_name(platform)
2125
OS_BINARIES[platform.os] || DEFAULT_BINARY
@@ -31,9 +35,14 @@ def crt_bin_path(platform)
3135
File.expand_path(crt_bin_name(platform), crt_bin_dir(platform))
3236
end
3337

34-
# @return [String] generate a string that be used with Gem::Platform
38+
# @return [String] generate a string that can be used with Gem::Platform
3539
def host_string
36-
"#{host_cpu}-#{host_os}"
40+
target_string(host_cpu)
41+
end
42+
43+
# @return [String] generate a string that can be used with Gem::Platform
44+
def target_string(cpu)
45+
"#{cpu}-#{host_os}"
3746
end
3847

3948
# @return [String] host cpu, even on jruby

gems/aws-crt/tasks/compile.rake

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# frozen_string_literal: true
22

3-
desc 'Compile CRT library and move to bin'
4-
task :bin do
3+
desc 'Compile CRT library and move to bin. Specify [cpu] to cross-compile.'
4+
task :bin, [:cpu] do |_, args|
5+
require_relative '../lib/aws-crt/platforms'
6+
args.with_defaults(:cpu => host_cpu)
7+
58
require_relative '../ext/compile'
6-
compile_bin
9+
compile_bin(args[:cpu])
710
end
811

912
desc 'Copies all binaries from the top level bin directory'

gems/aws-crt/tasks/gem.rake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# frozen_string_literal: true
22

3-
desc 'Build the aws-crt gem for the local platform'
4-
task 'gem:aws-crt:local' => [:bin] do
3+
desc 'Build aws-crt platform gem'
4+
task 'gem:aws-crt:platform', [:cpu] => [:bin] do |_, args|
55
require 'rubygems/package'
66
require 'fileutils'
77
require_relative '../lib/aws-crt/platforms'
88

9+
args.with_defaults(:cpu => host_cpu)
10+
911
FileUtils.chdir('gems/aws-crt') do
10-
platform = local_platform
12+
platform = target_platform(args[:cpu])
1113
binary_name = crt_bin_name(platform)
1214

1315
FileUtils.mkdir_p('pkg/', verbose: true)
@@ -27,7 +29,7 @@ task 'gem:aws-crt:local' => [:bin] do
2729
end
2830

2931
desc 'Build the aws-crt gem for the local platform'
30-
task 'gem:aws-crt' => 'gem:aws-crt:local'
32+
task 'gem:aws-crt' => 'gem:aws-crt:platform'
3133

3234
desc 'Build the aws-crt gem for pure-ruby'
3335
task 'gem:aws-crt:pure-ruby' => :clean do

0 commit comments

Comments
 (0)