diff --git a/.github/actions/native-cache/action.yml b/.github/actions/native-cache/action.yml new file mode 100644 index 000000000..2c66d42e6 --- /dev/null +++ b/.github/actions/native-cache/action.yml @@ -0,0 +1,57 @@ +name: Native output cache +description: > + Restores a job's output when the files it is built from are unchanged, so the job can skip its + build; otherwise saves the output once the job succeeds. Releases (tags) always build. + +inputs: + name: + description: Cache entry; one per output (and matrix arch) + required: true + path: + description: The output, as the job uploads it + required: true + inputs: + description: > + Git pathspecs of what the output is built from. Each has to match a tracked file, so a + renamed input fails the job. `.github/actions` and build-native.yml always count. + required: true + restore-only: + description: "'true' when a later job builds and saves the output, under this action's `key`" + default: 'false' + +outputs: + cache-hit: + description: "'true' when the output was restored" + value: ${{ steps.cache.outputs.cache-hit || steps.restore.outputs.cache-hit }} + key: + description: The cache key + value: ${{ steps.key.outputs.key }} + +runs: + using: composite + steps: + # Git's hashes of the files, so every OS computes the same key. The week bounds how stale the + # crates.io versions and toolchains in a reused output can be. Bump v1 to drop every entry. + - id: key + shell: bash + env: + NAME: ${{ inputs.name }} + INPUTS: ${{ inputs.inputs }} + run: | + set -f + hash=$(git ls-files -s --error-unmatch -- .github/actions .github/workflows/build-native.yml $INPUTS | git hash-object --stdin) + echo "key=native-v1-$NAME-$(date -u +%G-W%V)-$hash" >> "$GITHUB_OUTPUT" + + - id: cache + if: github.ref_type != 'tag' && inputs.restore-only != 'true' + uses: actions/cache@v4 + with: + path: ${{ inputs.path }} + key: ${{ steps.key.outputs.key }} + + - id: restore + if: github.ref_type != 'tag' && inputs.restore-only == 'true' + uses: actions/cache/restore@v4 + with: + path: ${{ inputs.path }} + key: ${{ steps.key.outputs.key }} diff --git a/.github/workflows/build-native.yml b/.github/workflows/build-native.yml index 15255e57d..44a8be76b 100644 --- a/.github/workflows/build-native.yml +++ b/.github/workflows/build-native.yml @@ -42,10 +42,9 @@ # run the Node-API test suites on the shipped modules (on WARP: runners have no GPU; # audio renders offline and on the 'none' sink: they have no audio device either). # -# The napi-windows modules and the canvas-svg / audio-context mobile builds change rarely, so -# their jobs cache the final artifact keyed on a hash of their sources and skip the whole build -# on a hit. The other jobs that build Skia keep its built libraries per target in -# .github/actions/skia-cache, saved only by pushes to master. +# Each job restores its output from .github/actions/native-cache, and skips the build when the files +# it lists as `inputs` are unchanged. The jobs that build Skia also keep its built libraries per +# target in .github/actions/skia-cache, saved only by pushes to master. name: Build Native permissions: @@ -55,7 +54,7 @@ on: workflow_call: env: - # bump to invalidate the canvas-svg / audio-context / canvas-media artifact caches + # bump to invalidate the opus deps cache NATIVE_CACHE_VERSION: v1 jobs: @@ -64,25 +63,44 @@ jobs: runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 + - uses: ./.github/actions/native-cache + id: cache + with: + name: canvas-android + path: packages/canvas/platforms/android/canvas-release.aar + inputs: >- + Cargo.toml .cargo Makefile + crates :!crates/canvas-svg* :!crates/canvas-ios :!crates/*-napi/* + V8_RELEASE NATIVESCRIPT_IOS_RUNTIME_REF tools/scripts/download-v8.sh + tools/scripts/build-android-v8-stub.sh tools/scripts/build-android.sh + packages/canvas/src-native/canvas-android packages/canvas/platforms/ios/src/cpp - uses: ./.github/actions/setup-android-native + if: steps.cache.outputs.cache-hit != 'true' - uses: ./.github/actions/skia-cache + if: steps.cache.outputs.cache-hit != 'true' with: name: canvas-android - name: Fetch V8 + NativeScript runtime headers + if: steps.cache.outputs.cache-hit != 'true' run: ./tools/scripts/download-v8.sh - name: Build libNativeScript.so stub for linking + if: steps.cache.outputs.cache-hit != 'true' run: ./tools/scripts/build-android-v8-stub.sh - name: Build Rust libraries + if: steps.cache.outputs.cache-hit != 'true' run: make android # Saved right after the Rust build, so a later failing step cannot drop the cache. - uses: ./.github/actions/skia-cache + if: steps.cache.outputs.cache-hit != 'true' with: name: canvas-android save: 'true' - name: Build canvas AAR + if: steps.cache.outputs.cache-hit != 'true' working-directory: packages/canvas/src-native/canvas-android run: ./gradlew :canvas:assembleRelease - name: Copy AAR into platforms/android + if: steps.cache.outputs.cache-hit != 'true' run: cp packages/canvas/src-native/canvas-android/canvas/build/outputs/aar/canvas-release.aar packages/canvas/platforms/android/canvas-release.aar - uses: actions/upload-artifact@v4 with: @@ -90,10 +108,56 @@ jobs: path: packages/canvas/platforms/android/canvas-release.aar if-no-files-found: error + # The xcframeworks built by several jobs: a hit uploads one here, on Linux, and its macOS jobs never + # start. On a miss the last of them saves it under this job's key. + apple-cache: + name: Apple xcframeworks (cached) + runs-on: ubuntu-22.04 + outputs: + canvas-hit: ${{ steps.canvas.outputs.cache-hit }} + canvas-key: ${{ steps.canvas.outputs.key }} + svg-hit: ${{ steps.svg.outputs.cache-hit }} + svg-key: ${{ steps.svg.outputs.key }} + steps: + - uses: actions/checkout@v4 + - uses: ./.github/actions/native-cache + id: canvas + with: + name: canvas-ios + path: canvas-ios.tar.gz + restore-only: 'true' + inputs: >- + Cargo.toml .cargo Makefile + crates :!crates/canvas-svg* :!crates/canvas-android :!crates/*-napi/* + V8_RELEASE NATIVESCRIPT_IOS_RUNTIME_REF tools/scripts/download-v8.sh + tools/scripts/canvas-build.sh packages/canvas/src-native/canvas-ios + - uses: actions/upload-artifact@v4 + if: steps.canvas.outputs.cache-hit == 'true' + with: + name: canvas-ios + path: canvas-ios.tar.gz + - uses: ./.github/actions/native-cache + id: svg + with: + name: canvas-svg-ios + path: canvas-svg-ios.tar.gz + restore-only: 'true' + inputs: >- + Cargo.toml .cargo Makefile + crates :!crates/canvas-2d :!crates/canvas-c :!crates/canvas-webgl :!crates/canvas-android :!crates/canvas-ios :!crates/*-napi/* :!crates/canvas-svg-android + tools/scripts/canvas-svg-build.sh packages/canvas-svg/src-native/ios + - uses: actions/upload-artifact@v4 + if: steps.svg.outputs.cache-hit == 'true' + with: + name: canvas-svg-ios + path: canvas-svg-ios.tar.gz + # One runner per platform: its Rust targets, then its framework slices. The Xcode pre-build # phase reruns cargo for each slice, so the slices are built in the job that owns the target dir. canvas-apple: name: Canvas ${{ matrix.platform }} + needs: apple-cache + if: needs.apple-cache.outputs.canvas-hit != 'true' runs-on: macos-15 strategy: fail-fast: false @@ -130,7 +194,7 @@ jobs: canvas-ios: name: Canvas xcframework - needs: canvas-apple + needs: [apple-cache, canvas-apple] runs-on: macos-15 steps: - uses: actions/checkout@v4 @@ -151,6 +215,11 @@ jobs: run: sh canvas-build.sh --xcframework - name: Pack xcframework run: tar -czf canvas-ios.tar.gz -C packages/canvas/platforms/ios/CanvasNativeBinary CanvasNative.xcframework + - uses: actions/cache/save@v4 + if: github.ref_type != 'tag' + with: + path: canvas-ios.tar.gz + key: ${{ needs.apple-cache.outputs.canvas-key }} - uses: actions/upload-artifact@v4 with: name: canvas-ios @@ -162,12 +231,19 @@ jobs: runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - - name: Restore cached AAR + - uses: ./.github/actions/native-cache id: svg-aar - uses: actions/cache@v4 with: + name: canvas-svg-android path: packages/canvas-svg/platforms/android/canvassvg-release.aar - key: ${{ env.NATIVE_CACHE_VERSION }}-canvas-svg-android-${{ hashFiles('crates/canvas-svg/**', 'crates/canvas-svg-c/**', 'crates/canvas-svg-android/**', 'crates/canvas-utils/**', 'crates/canvas-core/**', 'crates/gl-bindings/**', 'packages/canvas-svg/src-native/android/**', '!packages/canvas-svg/src-native/android/**/build/**', '!packages/canvas-svg/src-native/android/**/jniLibs/**', 'Cargo.lock', 'tools/scripts/build-svg-android.sh', 'tools/scripts/copy-svg-android.sh', 'Makefile') }} + inputs: >- + Cargo.toml .cargo Makefile + crates :!crates/canvas-2d :!crates/canvas-c :!crates/canvas-webgl :!crates/canvas-android :!crates/canvas-ios :!crates/*-napi/* :!crates/canvas-svg-ios + V8_RELEASE NATIVESCRIPT_IOS_RUNTIME_REF tools/scripts/download-v8.sh + tools/scripts/build-android-v8-stub.sh + tools/scripts/build-svg-android.sh tools/scripts/copy-svg-android.sh + packages/canvas-svg/src-native/android packages/canvas-svg/platforms/ios/src/cpp + packages/canvas/src-native/canvas-android/canvas/src/main/cpp - uses: ./.github/actions/setup-android-native if: steps.svg-aar.outputs.cache-hit != 'true' - uses: ./.github/actions/skia-cache @@ -206,29 +282,10 @@ jobs: path: packages/canvas-svg/platforms/android/canvassvg-release.aar if-no-files-found: error - # canvas-svg changes rarely: on a hit for its xcframework the per-platform jobs never start. - # The lookup runs on macOS like the restore, so both compute the same cache version. - canvas-svg-cache: - name: Canvas SVG cache lookup - runs-on: macos-15 - outputs: - key: ${{ steps.key.outputs.key }} - hit: ${{ steps.lookup.outputs.cache-hit }} - steps: - - uses: actions/checkout@v4 - - id: key - run: echo "key=${{ env.NATIVE_CACHE_VERSION }}-canvas-svg-ios-${{ hashFiles('crates/canvas-svg/**', 'crates/canvas-svg-c/**', 'crates/canvas-svg-ios/**', 'crates/canvas-utils/**', 'crates/canvas-core/**', 'packages/canvas-svg/src-native/ios/**', '!packages/canvas-svg/src-native/ios/dist/**', 'Cargo.lock', 'tools/scripts/canvas-svg-build.sh', 'Makefile') }}" >> "$GITHUB_OUTPUT" - - id: lookup - uses: actions/cache/restore@v4 - with: - path: canvas-svg-ios.tar.gz - key: ${{ steps.key.outputs.key }} - lookup-only: true - canvas-svg-apple: name: Canvas SVG ${{ matrix.platform }} - needs: canvas-svg-cache - if: needs.canvas-svg-cache.outputs.hit != 'true' + needs: apple-cache + if: needs.apple-cache.outputs.svg-hit != 'true' runs-on: macos-15 strategy: fail-fast: false @@ -262,39 +319,32 @@ jobs: canvas-svg-ios: name: Canvas SVG xcframework - needs: [canvas-svg-cache, canvas-svg-apple] - if: ${{ !cancelled() && needs.canvas-svg-cache.result == 'success' && contains(fromJSON('["success","skipped"]'), needs.canvas-svg-apple.result) }} + needs: [apple-cache, canvas-svg-apple] runs-on: macos-15 steps: - uses: actions/checkout@v4 - - name: Restore cached xcframework - id: svg-xcframework - uses: actions/cache@v4 - with: - path: canvas-svg-ios.tar.gz - key: ${{ needs.canvas-svg-cache.outputs.key }} - uses: maxim-lobanov/setup-xcode@v1 - if: steps.svg-xcframework.outputs.cache-hit != 'true' with: xcode-version: '^16.0' - uses: actions/download-artifact@v4 - if: steps.svg-xcframework.outputs.cache-hit != 'true' with: pattern: canvas-svg-apple-* merge-multiple: true path: slices - name: Unpack slices - if: steps.svg-xcframework.outputs.cache-hit != 'true' run: | mkdir -p packages/canvas-svg/src-native/ios/dist for f in slices/*.tar.gz; do tar -xzf "$f" -C packages/canvas-svg/src-native/ios/dist; done - name: Build CanvasSVG.xcframework - if: steps.svg-xcframework.outputs.cache-hit != 'true' working-directory: tools/scripts run: sh canvas-svg-build.sh --xcframework - name: Pack xcframework - if: steps.svg-xcframework.outputs.cache-hit != 'true' run: tar -czf canvas-svg-ios.tar.gz -C packages/canvas-svg/platforms/ios/CanvasSVGBinary CanvasSVG.xcframework + - uses: actions/cache/save@v4 + if: github.ref_type != 'tag' + with: + path: canvas-svg-ios.tar.gz + key: ${{ needs.apple-cache.outputs.svg-key }} - uses: actions/upload-artifact@v4 with: name: canvas-svg-ios @@ -309,12 +359,13 @@ jobs: with: # Oboe submodules: true - - name: Restore cached AAR + - uses: ./.github/actions/native-cache id: audio-aar - uses: actions/cache@v4 with: + name: audio-context-android path: packages/audio-context/platforms/android/audiocontext-release.aar - key: ${{ env.NATIVE_CACHE_VERSION }}-audio-context-android-${{ hashFiles('packages/audio-context/src-native/android/**', '!packages/audio-context/src-native/android/**/build/**') }} + inputs: >- + packages/audio-context/src-native/android - uses: actions/setup-java@v4 if: steps.audio-aar.outputs.cache-hit != 'true' with: @@ -344,12 +395,13 @@ jobs: runs-on: macos-15 steps: - uses: actions/checkout@v4 - - name: Restore cached xcframework + - uses: ./.github/actions/native-cache id: audio-xcframework - uses: actions/cache@v4 with: + name: audio-context-ios path: audio-context-ios.tar.gz - key: ${{ env.NATIVE_CACHE_VERSION }}-audio-context-ios-${{ hashFiles('packages/audio-context/src-native/ios/**', 'tools/scripts/audio-context-build.sh') }} + inputs: >- + packages/audio-context/src-native/ios tools/scripts/audio-context-build.sh - uses: ./.github/actions/setup-apple-native if: steps.audio-xcframework.outputs.cache-hit != 'true' with: @@ -400,26 +452,41 @@ jobs: shell: bash steps: - uses: actions/checkout@v4 + - uses: ./.github/actions/native-cache + id: cache + with: + name: canvas-windows-${{ matrix.arch }} + path: packages/canvas/platforms/windows/${{ matrix.arch }}/ + inputs: >- + Cargo.toml .cargo + crates :!crates/canvas-svg* :!crates/canvas-android :!crates/canvas-ios + :!crates/audio-context-napi :!crates/canvas-media-napi :!crates/canvas-gamepad-napi + tools/scripts/build-napi.sh tools/scripts/download-angle.sh - uses: ./.github/actions/setup-windows-native + if: steps.cache.outputs.cache-hit != 'true' with: target: ${{ matrix.target }} - uses: ./.github/actions/skia-cache + if: steps.cache.outputs.cache-hit != 'true' with: name: canvas-windows-${{ matrix.arch }} - uses: actions/setup-node@v4 + if: steps.cache.outputs.cache-hit != 'true' with: node-version: '22' - name: Build canvasnative.node (and fetch ANGLE) + if: steps.cache.outputs.cache-hit != 'true' run: ./tools/scripts/build-napi.sh ${{ matrix.target }} release-napi # arm64 is cross-compiled, so only x64 runs them. The runners have no GPU: D3D12 and ANGLE # fall back to WARP, and the WebGPU suites skip without an adapter. # Saved right after the Rust build, so a later failing step cannot drop the cache. - uses: ./.github/actions/skia-cache + if: steps.cache.outputs.cache-hit != 'true' with: name: canvas-windows-${{ matrix.arch }} save: 'true' - name: Node-API tests - if: matrix.arch == 'x64' + if: steps.cache.outputs.cache-hit != 'true' && matrix.arch == 'x64' env: CANVAS_NAPI_ADDON: ${{ github.workspace }}\packages\canvas\platforms\windows\x64\canvasnative.node run: node --expose-gc --test "crates/canvas-napi/__test__/*.test.mjs" @@ -437,12 +504,12 @@ jobs: fail-fast: false matrix: module: - # `sources`: every tracked directory the module is built from (its cache key). + # `sources`: every crate the module is built from (its cache key). - name: canvas-svg crate: canvas-svg-napi file: canvassvg.node addon-env: CANVAS_SVG_NAPI_ADDON - sources: crates/canvas-svg crates/canvas-svg-c crates/canvas-svg-napi crates/canvas-utils + sources: crates/canvas-svg crates/canvas-svg-c crates/canvas-svg-napi crates/canvas-utils crates/canvas-core crates/gl-bindings skia: true # Runners have no audio device: its suite renders offline and on the 'none' sink. - name: audio-context @@ -473,18 +540,13 @@ jobs: MODULE_DIR: packages/${{ matrix.module.name }}/platforms/windows/${{ matrix.arch }} steps: - uses: actions/checkout@v4 - # The tree hashes of what the module is built from: equal hashes, equal module. - - name: Source key - id: sources - run: | - key=$(for path in ${{ matrix.module.sources }} crates/vendor/napi-sys Cargo.toml tools/scripts/build-napi.sh; do git rev-parse "HEAD:$path" || exit 1; done | sha256sum | cut -c1-32) - echo "key=$key" >> "$GITHUB_OUTPUT" - - name: Restore cached module + - uses: ./.github/actions/native-cache id: cache - uses: actions/cache@v4 with: + name: ${{ matrix.module.name }}-windows-${{ matrix.arch }} path: ${{ env.MODULE_DIR }}/${{ matrix.module.file }} - key: ${{ env.NATIVE_CACHE_VERSION }}-${{ matrix.module.name }}-windows-${{ matrix.arch }}-${{ steps.sources.outputs.key }} + inputs: >- + Cargo.toml .cargo crates/vendor tools/scripts/build-napi.sh ${{ matrix.module.sources }} - uses: ./.github/actions/setup-windows-native if: steps.cache.outputs.cache-hit != 'true' with: @@ -535,23 +597,38 @@ jobs: runner: ubuntu-22.04-arm steps: - uses: actions/checkout@v4 + - uses: ./.github/actions/native-cache + id: cache + with: + name: linux-${{ matrix.arch }} + path: packages/*/platforms/linux/lib/${{ matrix.arch }}/ + inputs: >- + Cargo.toml .cargo + crates :!crates/canvas-android :!crates/canvas-ios :!crates/canvas-svg-android :!crates/canvas-svg-ios + tools/scripts/build-linux.sh - uses: ./.github/actions/setup-linux-native + if: steps.cache.outputs.cache-hit != 'true' - uses: ./.github/actions/skia-cache + if: steps.cache.outputs.cache-hit != 'true' with: name: linux-${{ matrix.arch }} - uses: actions/setup-node@v4 + if: steps.cache.outputs.cache-hit != 'true' with: node-version: '22' - name: Build the Node-API modules + if: steps.cache.outputs.cache-hit != 'true' run: ./tools/scripts/build-linux.sh # Mesa's surfaceless EGL (llvmpipe) for 2D / WebGL and lavapipe for WebGPU: no GPU here. # canvas-media's suite drives Media Foundation, so it is Windows only. # Saved right after the Rust build, so a later failing step cannot drop the cache. - uses: ./.github/actions/skia-cache + if: steps.cache.outputs.cache-hit != 'true' with: name: linux-${{ matrix.arch }} save: 'true' - name: Node-API tests + if: steps.cache.outputs.cache-hit != 'true' env: CANVAS_NAPI_ADDON: ${{ github.workspace }}/packages/canvas/platforms/linux/lib/${{ matrix.arch }}/canvasnative.node CANVAS_SVG_NAPI_ADDON: ${{ github.workspace }}/packages/canvas-svg/platforms/linux/lib/${{ matrix.arch }}/canvassvg.node