From 60ac82a3d153ab6bb5a3337f2efb449a08a63fa9 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 15:43:20 +0900 Subject: [PATCH 1/9] Use ccache only for host build --- build_config.rb | 55 ++++++++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/build_config.rb b/build_config.rb index 04b9a47..43f2df2 100644 --- a/build_config.rb +++ b/build_config.rb @@ -11,21 +11,31 @@ def debug_config(conf) conf.enable_debug end -def linux_build_config(conf, target = nil, strip: false) - commands = %w[zig cc] - commands << '-s' if strip - commands += ['-target', target] if target - commands = commands.shelljoin - - conf.cc.command = commands - conf.linker.command = commands +def cc_command(target = nil) + # Only host build is enabled because otherwise the wrong cache may be used + if target || ENV['CCACHE_DISABLE'] + 'zig cc' + else + ENV['USE_CCACHE'] = '1' + ENV['CCACHE_DIR'] = File.expand_path('build/ccache') + 'ccache zig cc' + end +end + +def build_config(conf, target = nil, strip: false) + [conf.cc, conf.linker].each do |cc| + cc.command = cc_command(target) + cc.flags += ['-target', target] if target + end + conf.cc.flags << '-s' if strip + conf.archiver.command = 'zig ar' conf.cc.defines += %w[MRB_STR_LENGTH_MAX=0 MRB_UTF8_STRING] conf.host_target = target if target end MRuby::Build.new do |conf| - linux_build_config(conf) + build_config(conf) debug_config(conf) gem_config(conf) end @@ -39,7 +49,7 @@ def linux_build_config(conf, target = nil, strip: false) next unless build_targets.include?(arch) MRuby::CrossBuild.new(arch) do |conf| - linux_build_config(conf, target, strip: true) + build_config(conf, target, strip: true) debug_config(conf) gem_config(conf) end @@ -49,11 +59,12 @@ def linux_build_config(conf, target = nil, strip: false) MRuby::CrossBuild.new('darwin-amd64') do |conf| macos_sdk = ENV.fetch('MACOSX_SDK_PATH').shellescape - command = ['zig', 'cc', '-target', 'x86_64-macos', '-Wno-overriding-t-option', '-mmacosx-version-min=10.14'] - conf.cc.command = (command + ['-isysroot', macos_sdk, '-iwithsysroot', - '/usr/include', '-iframeworkwithsysroot', - '/System/Library/Frameworks']).join(' ') - conf.linker.command = (command + ['--sysroot', macos_sdk, '-F/System/Library/Frameworks', '-L/usr/lib']).shelljoin + build_config(conf, 'x86_64-macos', strip: true) + conf.cc.flags += ['-Wno-overriding-t-option', '-mmacosx-version-min=10.14', + '-isysroot', macos_sdk, '-iwithsysroot', '/usr/include', + '-iframeworkwithsysroot', '/System/Library/Frameworks'] + conf.linker.flags += ['-Wno-overriding-t-option', '-mmacosx-version-min=10.14', + '--sysroot', macos_sdk, '-F/System/Library/Frameworks', '-L/usr/lib'] conf.archiver.command = 'zig ar' ENV['RANLIB'] ||= 'zig ranlib' conf.host_target = 'x86_64-darwin' @@ -67,14 +78,16 @@ def linux_build_config(conf, target = nil, strip: false) MRuby::CrossBuild.new('darwin-arm64') do |conf| macos_sdk = ENV.fetch('MACOSX_SDK_PATH').shellescape - command = ['zig', 'cc', '-target', 'aarch64-macos', '-Wno-overriding-t-option', '-mmacosx-version-min=11.1'] - conf.cc.command = (command + ['-isysroot', macos_sdk, '-iwithsysroot', - '/usr/include', '-iframeworkwithsysroot', - '/System/Library/Frameworks']).join(' ') - conf.linker.command = (command + ['--sysroot', macos_sdk, '-F/System/Library/Frameworks', '-L/usr/lib']).shelljoin + build_config(conf, 'aarch64-macos', strip: true) + conf.cc.flags += ['-Wno-overriding-t-option', '-mmacosx-version-min=11.1', + '-isysroot', macos_sdk, '-iwithsysroot', '/usr/include', + '-iframeworkwithsysroot', '/System/Library/Frameworks'] + conf.linker.flags += ['-Wno-overriding-t-option', '-mmacosx-version-min=11.1', + '--sysroot', macos_sdk, '-F/System/Library/Frameworks', '-L/usr/lib'] + conf.archiver.command = 'zig ar' ENV['RANLIB'] ||= 'zig ranlib' - conf.host_target = 'x86_64-darwin' + conf.host_target = 'aarch64-darwin' debug_config(conf) gem_config(conf) From 7d7f7cba0cb851fe4417e304dbde46d063a99000 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 17:11:23 +0900 Subject: [PATCH 2/9] Enable ccache by building for each architecture --- Rakefile | 7 +++++-- build_config.rb | 15 +++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Rakefile b/Rakefile index 153b45c..db4cb21 100644 --- a/Rakefile +++ b/Rakefile @@ -47,8 +47,11 @@ end namespace :build do desc 'Build the project for all targets' task 'all' do - env = ["MRUBY_BUILD_TARGETS=#{build_targets.join(',')}"] - docker_run(env:) + build_targets.each do |target| + env = ["MRUBY_BUILD_TARGETS=#{target}"] + env += ['USE_CCACHE=1', "CCACHE_DIR=build/ccache/#{target}"] unless ENV['CCACHE_DISABLE'] + docker_run(env:) + end end desc 'Build the project for CI' diff --git a/build_config.rb b/build_config.rb index 43f2df2..3489550 100644 --- a/build_config.rb +++ b/build_config.rb @@ -11,20 +11,15 @@ def debug_config(conf) conf.enable_debug end -def cc_command(target = nil) - # Only host build is enabled because otherwise the wrong cache may be used - if target || ENV['CCACHE_DISABLE'] - 'zig cc' - else - ENV['USE_CCACHE'] = '1' - ENV['CCACHE_DIR'] = File.expand_path('build/ccache') - 'ccache zig cc' - end +def cc_command + command = %w[zig cc] + command.unshift 'ccache' if ENV['USE_CCACHE'] + command.join(' ') end def build_config(conf, target = nil, strip: false) [conf.cc, conf.linker].each do |cc| - cc.command = cc_command(target) + cc.command = cc_command cc.flags += ['-target', target] if target end conf.cc.flags << '-s' if strip From 05847941eeb31b2ff282d222aa5403bfefaab605 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 17:14:35 +0900 Subject: [PATCH 3/9] Build all targets on CI --- .github/workflows/build-and-test.yml | 2 +- Rakefile | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 79bf248..561f3fe 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -18,7 +18,7 @@ jobs: with: ruby-version: 3.2 - name: build - run: rake build:ci + run: rake clean build:all - name: Upload test binary (linux-amd64) uses: actions/upload-artifact@v3 with: diff --git a/Rakefile b/Rakefile index db4cb21..4aa2c9f 100644 --- a/Rakefile +++ b/Rakefile @@ -19,10 +19,6 @@ def build_targets ] end -def ci_build_targets - %w[linux-amd64 darwin-amd64] -end - def archive_binary_file(targets, version) FileUtils.mkdir_p 'release' targets.each do |target| @@ -48,17 +44,18 @@ namespace :build do desc 'Build the project for all targets' task 'all' do build_targets.each do |target| + Rake::Task["build:#{target}"].invoke + end + end + + build_targets.each do |target| + desc "Build the project for #{target}" + task target do env = ["MRUBY_BUILD_TARGETS=#{target}"] env += ['USE_CCACHE=1', "CCACHE_DIR=build/ccache/#{target}"] unless ENV['CCACHE_DISABLE'] docker_run(env:) end end - - desc 'Build the project for CI' - task 'ci' do - env = ["MRUBY_BUILD_TARGETS=#{ci_build_targets.join(',')}"] - docker_run(env:) - end end desc 'Cleanup build cache' From d7b1de0087a16b86c6a97e9bfe1339e4a4011101 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 17:29:13 +0900 Subject: [PATCH 4/9] Cache ccache's build cache --- .github/workflows/build-and-test.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 561f3fe..44bbc5a 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -1,6 +1,7 @@ name: build and test on: + workflow_dispatch: pull_request: jobs: @@ -17,6 +18,11 @@ jobs: uses: ruby/setup-ruby@v1 with: ruby-version: 3.2 + - name: Build cache + uses: actions/cache@v3 + with: + path: build/ccache + key: ${{ runner.os }}-ccache - name: build run: rake clean build:all - name: Upload test binary (linux-amd64) From 2218a4b0429e811b831f2ca34f9da1069ed4d020 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 17:44:37 +0900 Subject: [PATCH 5/9] typo --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 4aa2c9f..96546b6 100644 --- a/Rakefile +++ b/Rakefile @@ -70,7 +70,7 @@ end desc 'Release the project' task release: %w[clean build:all] do - archive_binary_file(build_targets, "v#{RF::VERSION}") + archive_binary_file(build_targets, "v#{Rf::VERSION}") end desc 'Run RSpec with parallel_rspec' From 9b5373bb4f78054ba481e07191066741b9507e95 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 17:57:16 +0900 Subject: [PATCH 6/9] Add build directory to cache --- .github/workflows/build-and-test.yml | 18 ++++++++++++++++-- Rakefile | 3 ++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 44bbc5a..120a48d 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -21,20 +21,34 @@ jobs: - name: Build cache uses: actions/cache@v3 with: - path: build/ccache + path: build/ key: ${{ runner.os }}-ccache - name: build - run: rake clean build:all + run: rake build:all release + - name: Upload test binary (linux-amd64) uses: actions/upload-artifact@v3 with: name: rf-${{ github.sha }}-linux-amd64 path: build/linux-amd64/bin/rf + - name: Upload test binary (linux-arm64) + uses: actions/upload-artifact@v3 + with: + name: rf-${{ github.sha }}-linux-arm64 + path: build/linux-arm64/bin/rf - name: Upload test binary (darwin-amd64) uses: actions/upload-artifact@v3 with: name: rf-${{ github.sha }}-darwin-amd64 path: build/darwin-amd64/bin/rf + - name: Upload test binary (darwin-arm64) + uses: actions/upload-artifact@v3 + with: + name: rf-${{ github.sha }}-darwin-arm64 + path: build/darwin-arm64/bin/rf + + - name: Cleanup build directory + run: rake clean test-on-linux: needs: build diff --git a/Rakefile b/Rakefile index 96546b6..36dc218 100644 --- a/Rakefile +++ b/Rakefile @@ -65,7 +65,8 @@ end desc 'Deep cleanup build cache' task 'deep_clean' do - docker_run(cmd: 'deep_clean') + env = ["MRUBY_BUILD_TARGETS=#{build_targets.join(',')}"] + docker_run(cmd: 'deep_clean', env:) end desc 'Release the project' From 1e9f352865aca02d9f3bdbfc075fac6bda4966f8 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 18:12:07 +0900 Subject: [PATCH 7/9] Use ruby:3.2 container --- .github/workflows/build-and-test.yml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 120a48d..c7231b8 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -53,16 +53,13 @@ jobs: test-on-linux: needs: build runs-on: ubuntu-latest + container: ruby:3.2 steps: - uses: actions/checkout@v3 - uses: actions/download-artifact@v3 with: name: rf-${{ github.sha }}-linux-amd64 path: build/bin - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 3.2 - name: Install dependencies run: bundle install - name: Run test @@ -73,16 +70,13 @@ jobs: test-on-macos: needs: build runs-on: macos-latest + container: ruby:3.2 steps: - uses: actions/checkout@v3 - uses: actions/download-artifact@v3 with: name: rf-${{ github.sha }}-darwin-amd64 path: build/bin - - name: Set up Ruby - uses: ruby/setup-ruby@v1 - with: - ruby-version: 3.2 - name: Install dependencies run: bundle install - name: Run test From 822e189866865f09ff1181300b7debe78d701862 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 18:13:39 +0900 Subject: [PATCH 8/9] Rename job name --- .github/workflows/rubocop.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rubocop.yaml b/.github/workflows/rubocop.yaml index 6f949c3..503a676 100644 --- a/.github/workflows/rubocop.yaml +++ b/.github/workflows/rubocop.yaml @@ -1,10 +1,10 @@ name: rubocop on: - pull_request: + pull_request: jobs: - build: + lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@master From b35dae121037e8080df8dd54cca8153622a45c14 Mon Sep 17 00:00:00 2001 From: buty4649 Date: Sat, 27 May 2023 18:25:45 +0900 Subject: [PATCH 9/9] Revert "Use ruby:3.2 container" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 1e9f352865aca02d9f3bdbfc075fac6bda4966f8. dockerが入ってなくてエラーになった --- .github/workflows/build-and-test.yml | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index c7231b8..120a48d 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -53,13 +53,16 @@ jobs: test-on-linux: needs: build runs-on: ubuntu-latest - container: ruby:3.2 steps: - uses: actions/checkout@v3 - uses: actions/download-artifact@v3 with: name: rf-${{ github.sha }}-linux-amd64 path: build/bin + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.2 - name: Install dependencies run: bundle install - name: Run test @@ -70,13 +73,16 @@ jobs: test-on-macos: needs: build runs-on: macos-latest - container: ruby:3.2 steps: - uses: actions/checkout@v3 - uses: actions/download-artifact@v3 with: name: rf-${{ github.sha }}-darwin-amd64 path: build/bin + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.2 - name: Install dependencies run: bundle install - name: Run test