Skip to content

Commit

Permalink
Enable cross-compilation in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
lloeki committed Jul 9, 2021
1 parent 2034b2e commit b633599
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 27 deletions.
90 changes: 69 additions & 21 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,42 +134,78 @@ jobs:
matrix:
platform:
- amd64
# arm64
- arm64
# arm
# ppc64le
# s390x
libc:
- gnu
- musl
include:
- platform: amd64
libc: musl
name: Build (linux)
outputs:
GEM_VERSION: ${{ steps.set-metadata.outputs.GEM_VERSION }}
runs-on: ubuntu-20.04
steps:
- name: Enable ${{ matrix.platform }} platform
id: qemu
if: ${{ matrix.platform != 'amd64' }}
- name: Set cross-compilation platform
id: platform
run: |
docker run --privileged --rm tonistiigi/binfmt:latest --install ${{ matrix.platform }} | tee platforms.json
echo "::set-output name=platforms::$(cat platforms.json)"
case ${{ matrix.platform }} in
amd64)
case ${{ matrix.libc }} in
gnu)
echo 'x86_64-linux-gnu'
;;
musl)
echo 'x86_64-alpine-linux-musl'
;;
esac
;;
arm64)
echo 'aarch64-linux-gnu'
;;
esac | tee target_platform
case ${{ matrix.platform }} in
amd64)
case ${{ matrix.libc }} in
gnu)
echo 'x86_64-linux'
;;
musl)
echo 'x86_64-linux-musl'
;;
esac
;;
arm64)
echo 'aarch64-linux'
;;
esac | tee ruby_target_platform
echo "::set-output name=target_platform::$(cat target_platform)"
echo "::set-output name=ruby_target_platform::$(cat ruby_target_platform)"
- name: Start container
id: container
run: |
case ${{ matrix.libc }} in
gnu)
echo 'ruby:2.4'
echo 'ruby:3.0'
;;
musl)
echo 'ruby:2.4-alpine'
echo 'ruby:3.0-alpine'
;;
esac > container_image
esac | tee container_image
echo "::set-output name=image::$(cat container_image)"
docker run --rm -d -v "${PWD}":"${PWD}" -w "${PWD}" --platform linux/${{ matrix.platform }} $(cat container_image) /bin/sleep 64d | tee container_id
docker run --rm -d -v "${PWD}":"${PWD}" -w "${PWD}" --platform linux/amd64 -e RUBY_TARGET_PLATFORM=${{ steps.platform.outputs.ruby_target_platform }} -e TARGET_PLATFORM=${{ steps.platform.outputs.target_platform }} $(cat container_image) /bin/sleep 64d | tee container_id
docker exec -w "${PWD}" $(cat container_id) uname -a
echo "::set-output name=id::$(cat container_id)"
- name: Install Alpine system dependencies
if: ${{ matrix.libc == 'musl' }}
run: docker exec -w "${PWD}" ${{ steps.container.outputs.id }} apk add --no-cache build-base linux-headers bash python2 python3 git curl tar
run: docker exec -w "${PWD}" ${{ steps.container.outputs.id }} apk add --no-cache build-base linux-headers bash python2 python3 git curl tar clang
- name: Install Debian cross-compiler
if: ${{ matrix.libc == 'gnu' && matrix.platform != 'amd64' }}
run: |
docker exec -w "${PWD}" ${{ steps.container.outputs.id }} apt-get update
docker exec -w "${PWD}" ${{ steps.container.outputs.id }} apt-get install -y binutils-aarch64-linux-gnu gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
- name: Checkout
uses: actions/checkout@v2
- name: Bundle
Expand Down Expand Up @@ -208,22 +244,22 @@ jobs:
docker exec -w "${PWD}" ${{ steps.container.outputs.id }} ./libexec/inject-libv8 ${{ steps.set-metadata.outputs.NODE_VERSION }}
- name: Build binary gem
run: |
docker exec -w "${PWD}" ${{ steps.container.outputs.id }} bundle exec rake binary
docker exec -w "${PWD}" ${{ steps.container.outputs.id }} bundle exec rake binary[${{ steps.platform.outputs.ruby_target_platform }}]
- name: Upload V8
uses: actions/upload-artifact@v1
with:
name: v8-${{ steps.set-metadata.outputs.LIBV8_VERSION }}-${{ steps.set-metadata.outputs.GEM_PLATFORM }}
name: v8-${{ steps.set-metadata.outputs.LIBV8_VERSION }}-${{ steps.platform.outputs.ruby_target_platform }}
path: vendor
- name: Upload gem
uses: actions/upload-artifact@v1
with:
name: gem-${{ steps.set-metadata.outputs.GEM_VERSION }}-${{ steps.set-metadata.outputs.GEM_PLATFORM }}
name: gem-${{ steps.set-metadata.outputs.GEM_VERSION }}-${{ steps.platform.outputs.ruby_target_platform }}
path: pkg
test-ruby:
name: Test (ruby)
needs: build-ruby
runs-on: ubuntu-20.04
container: ruby:2.4
container: ruby:2.5
steps:
- name: Set metadata
id: set-metadata
Expand All @@ -241,7 +277,7 @@ jobs:
run: |
git clone https://github.com/rubyjs/mini_racer.git test/mini_racer --depth 1
cd test/mini_racer
git fetch origin refs/pull/186/head
git fetch origin refs/pull/210/head
git checkout FETCH_HEAD
git reset --hard
git clean -f -d -x
Expand Down Expand Up @@ -276,7 +312,7 @@ jobs:
run: |
git clone https://github.com/rubyjs/mini_racer.git test/mini_racer --depth 1
cd test/mini_racer
git fetch origin refs/pull/186/head
git fetch origin refs/pull/210/head
git checkout FETCH_HEAD
git reset --hard
git clean -f -d -x
Expand All @@ -289,7 +325,6 @@ jobs:
fail-fast: false
matrix:
version:
- '2.4'
- '2.5'
- '2.6'
- '2.7'
Expand All @@ -303,6 +338,19 @@ jobs:
libc:
- gnu
- musl
include:
- version: '2.5'
platform: 'arm64'
libc: 'gnu'
- version: '2.6'
platform: 'arm64'
libc: 'gnu'
- version: '2.7'
platform: 'arm64'
libc: 'gnu'
- version: '3.0'
platform: 'arm64'
libc: 'gnu'
name: Test (linux)
needs: build-linux
runs-on: ubuntu-20.04
Expand All @@ -323,7 +371,7 @@ jobs:
musl)
echo 'ruby:${{ matrix.version }}-alpine'
;;
esac > container_image
esac | tee container_image
echo "::set-output name=image::$(cat container_image)"
docker run --rm -d -v "${PWD}":"${PWD}" -w "${PWD}" --platform linux/${{ matrix.platform }} $(cat container_image) /bin/sleep 64d | tee container_id
docker exec -w "${PWD}" $(cat container_id) uname -a
Expand All @@ -347,7 +395,7 @@ jobs:
run: |
git clone https://github.com/rubyjs/mini_racer.git test/mini_racer --depth 1
cd test/mini_racer
git fetch origin refs/pull/186/head
git fetch origin refs/pull/210/head
git checkout FETCH_HEAD
git reset --hard
git clean -f -d -x
Expand Down
9 changes: 5 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Helpers
module_function

def binary_gemspec(platform: Gem::Platform.local, str: RUBY_PLATFORM)
# TODO: old rubygems and cross compile
platform.instance_eval { @version = 'musl' } if str =~ /-musl/ && platform.version.nil?

gemspec = eval(File.read('libv8-node.gemspec')) # rubocop:disable Security/Eval
Expand All @@ -20,12 +21,12 @@ end

task :compile, [:platform] => [] do |_, args|
local_platform = Gem::Platform.local.to_s
target_platform = ENV['GEM_TARGET_PLATFORM'] || args.to_h[:platform] || Gem::Platform.local.to_s
target_platform = ENV['RUBY_TARGET_PLATFORM'] || args.to_h[:platform] || Gem::Platform.local.to_s

puts "local platform: #{local_platform}"
puts "target platform: #{target_platform}"

ENV['GEM_TARGET_PLATFORM'] = target_platform
ENV['RUBY_TARGET_PLATFORM'] = target_platform

if (libs = Dir["vendor/v8/#{target_platform}/**/*.a"]).any?
puts "found: #{libs.inspect}"
Expand All @@ -38,8 +39,8 @@ task :compile, [:platform] => [] do |_, args|
end

task :binary, [:platform] => [:compile] do |_, args|
local_platform = Gem::Platform.local.to_s
target_platform = ENV['GEM_TARGET_PLATFORM'] || args.to_h[:platform] || Gem::Platform.local.to_s
local_platform = Gem::Platform.local
target_platform = Gem::Platform.new(ENV['RUBY_TARGET_PLATFORM']) || Gem::Platform.new(args.to_h[:platform]) || Gem::Platform.local

puts "local platform: #{local_platform}"
puts "target platform: #{target_platform}"
Expand Down
4 changes: 2 additions & 2 deletions libexec/platform
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ case "${host_platform}" in
esac

if [ "${host_platform}" != "${target_platform}" ]; then
echo "cross compiling host: ${host_platform} target: ${target_platform}"
echo "# cross compiling host: ${host_platform} target: ${target_platform}"
case "${target_platform}" in
aarch64-*linux*)
CC='aarch64-linux-gnu-gcc'
Expand Down Expand Up @@ -121,6 +121,6 @@ if [ -n "${RUBY_TARGET_PLATFORM:-}" ]; then cat <<EOF; fi
RUBY_TARGET_PLATFORM='${RUBY_TARGET_PLATFORM}'
EOF

if [ -n "${CC_host:-}" ]; then cat <<EOF; fi
if [ -n "${configure_flags:-}" ]; then cat <<EOF; fi
configure_flags="\${configure_flags:-} ${configure_flags}"
EOF

0 comments on commit b633599

Please sign in to comment.