From 4b6ba9411e9861458f50d751792bf597d7f7d19c Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Wed, 2 Oct 2024 08:56:08 +0200 Subject: [PATCH] feat(cli): add Ccache support (#3375) --- .changeset/bright-mayflies-hide.md | 5 +++++ .github/actions/setup-toolchain/action.yml | 15 -------------- .github/workflows/pr.yml | 9 ++++++++- packages/cli/src/build.ts | 11 +++++++++++ packages/cli/src/build/ccache.ts | 23 ++++++++++++++++++++++ 5 files changed, 47 insertions(+), 16 deletions(-) create mode 100644 .changeset/bright-mayflies-hide.md create mode 100644 packages/cli/src/build/ccache.ts diff --git a/.changeset/bright-mayflies-hide.md b/.changeset/bright-mayflies-hide.md new file mode 100644 index 000000000..f9f4c9ebb --- /dev/null +++ b/.changeset/bright-mayflies-hide.md @@ -0,0 +1,5 @@ +--- +"@rnx-kit/cli": patch +--- + +Add Ccache support diff --git a/.github/actions/setup-toolchain/action.yml b/.github/actions/setup-toolchain/action.yml index 94449f777..144af2a6a 100644 --- a/.github/actions/setup-toolchain/action.yml +++ b/.github/actions/setup-toolchain/action.yml @@ -24,21 +24,6 @@ runs: run: | podfile_lock="packages/test-app/${{ inputs.platform }}/Podfile.lock" if [[ -f $(git rev-parse --show-toplevel)/.ccache/ccache.conf ]] && [[ -f "$podfile_lock" ]]; then - if ! command -v ccache 1> /dev/null; then - brew install ccache - fi - - CCACHE_HOME=$(dirname $(dirname $(which ccache)))/opt/ccache - - echo "CCACHE_DIR=$(git rev-parse --show-toplevel)/.ccache" >> $GITHUB_ENV - - echo "CC=${CCACHE_HOME}/libexec/clang" >> $GITHUB_ENV - echo "CXX=${CCACHE_HOME}/libexec/clang++" >> $GITHUB_ENV - echo "CMAKE_C_COMPILER_LAUNCHER=$(which ccache)" >> $GITHUB_ENV - echo "CMAKE_CXX_COMPILER_LAUNCHER=$(which ccache)" >> $GITHUB_ENV - - ccache --zero-stats 1> /dev/null - clang --version > .clang-version input=$(find . -maxdepth 1 -name .clang-version -o -name .yarnrc.yml | sort) echo "cache-key=$(cat "$podfile_lock" $input | shasum -a 256 | awk '{ print $1 }')" >> $GITHUB_OUTPUT diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 0ca366f87..7f4d1ad88 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -138,6 +138,10 @@ jobs: if [[ "$(yarn show-affected --base origin/${{ github.base_ref }})" = *"@rnx-kit/test-app"* ]]; then echo 'ios=true' >> $GITHUB_OUTPUT fi + - name: Install Ccache + if: ${{ steps.affected-projects.outputs.ios != '' }} + run: | + brew install ccache - name: Build @rnx-kit/cli if: ${{ steps.affected-projects.outputs.ios != '' }} run: | @@ -151,7 +155,10 @@ jobs: - name: Build iOS app if: ${{ steps.affected-projects.outputs.ios != '' }} run: | - yarn build:ios | xcbeautify + export CCACHE_DIR="$(git rev-parse --show-toplevel)/.ccache" + ccache --zero-stats 1> /dev/null + yarn build:ios --ccache-dir "$CCACHE_DIR" --ccache-home /opt/homebrew/opt/ccache | xcbeautify + ccache --show-stats --verbose working-directory: packages/test-app build-website: name: "Build the website" diff --git a/packages/cli/src/build.ts b/packages/cli/src/build.ts index a511ab5fe..d2266aac2 100644 --- a/packages/cli/src/build.ts +++ b/packages/cli/src/build.ts @@ -1,6 +1,7 @@ import type { Config } from "@react-native-community/cli-types"; import { InvalidArgumentError } from "commander"; import { buildAndroid } from "./build/android"; +import { setCcacheDir, setCcacheHome } from "./build/ccache"; import { buildIOS } from "./build/ios"; import { buildMacOS } from "./build/macos"; import type { @@ -100,5 +101,15 @@ export const rnxBuildCommand = { default: "simulator", parse: asDestination, }, + { + name: "--ccache-dir ", + description: "Path to Ccache config", + parse: setCcacheDir, + }, + { + name: "--ccache-home ", + description: "Path to Ccache installation", + parse: setCcacheHome, + }, ], }; diff --git a/packages/cli/src/build/ccache.ts b/packages/cli/src/build/ccache.ts new file mode 100644 index 000000000..d4208b496 --- /dev/null +++ b/packages/cli/src/build/ccache.ts @@ -0,0 +1,23 @@ +import * as fs from "node:fs"; +import * as path from "node:path"; + +export function setCcacheDir(dir: string): string | undefined { + if (!fs.existsSync(dir)) { + return undefined; + } + + process.env["CCACHE_DIR"] = dir; + return dir; +} + +export function setCcacheHome(dir: string): string | undefined { + if (!fs.existsSync(dir)) { + return undefined; + } + + process.env["CC"] = path.join(dir, "libexec", "clang"); + process.env["CXX"] = path.join(dir, "libexec", "clang++"); + process.env["CMAKE_C_COMPILER_LAUNCHER"] = path.join(dir, "bin", "ccache"); + process.env["CMAKE_CXX_COMPILER_LAUNCHER"] = path.join(dir, "bin", "ccache"); + return dir; +}