Skip to content

Commit

Permalink
Detect ccache and provide a default configuration (#42051)
Browse files Browse the repository at this point in the history
Summary:
Building native modules from source, may take a long time. Xcode already helps bring this down, by providing incremental builds, as long as the user doesn't delete their `ios/build` directory. But in some situations, i.e. when iterating the native code of an app or library or when the developer need to delete that `ios/build` directory, it's advantageous to use a compiler cache, such as ccache. This is already outlined in our ["Speeding up your Build phase"](https://reactnative.dev/docs/build-speed#xcode-specific-setup) guide.

But setting up an Xcode project to use Ccache with the correct configuration, isn't trivial in a way that doesn't require symlinking `clang` and `clang++` or passing configuration via environment variables on every `npm run ios` invokation.

This PR takes its inspiration from the existing guide on [setting up Ccache for Xcode](https://reactnative.dev/docs/build-speed#xcode-specific-setup), but applies the build settings only if an installation of `ccache` is detected and the feature is explicitly opted into via an argument to the `react_native_post_install` function or a `USE_CCACHE` environment variable. It uses two shell scripts to wrap the call to `ccache`, which both injects a default `CCACHE_CONFIGPATH` environment variable (i.e. it won't override this if already provided, to allow for customisations on CI), pointing to a `ccache.config` which works well with React Native projects (it has the same values as the guide mentions).

For context, I posted about this change in the ios channel of the contributors Discord server, where I discussed it with cipolleschi and saadnajmi

### Additional output printed when running `pod install`

#### When `ccache_available and ccache_enabled`

```
[Ccache]: Ccache found at /opt/homebrew/bin/ccache
[Ccache]: Setting CC, LD, CXX & LDPLUSPLUS build settings
```

#### When `ccache_available and !ccache_enabled`

```
[Ccache]: Ccache found at /opt/homebrew/bin/ccache
[Ccache]: Pass ':ccache_enabled => true' to 'react_native_post_install' in your Podfile or set environment variable 'USE_CCACHE=1' to increase the speed of subsequent builds
```

#### When `!ccache_available and ccache_enabled`

```
[!] [Ccache]: Install ccache or ensure your neither passing ':ccache_enabled => true' nor setting environment variable 'USE_CCACHE=1'
```

#### Otherwise

If the user doesn't have ccache installed and doesn't explicitly opt into this feature, nothing will be printed.

bypass-github-export-checks

## Changelog:

[IOS] [ADDED] - Added better support for `ccache`, to speed up subsequent builds of native code. After installing `ccache` and running `pod install`, the Xcode project is injected with compiler and linker build settings pointing scripts that loads a default  Ccache configuration and invokes the `ccache` executable.

Pull Request resolved: facebook/react-native#42051

Test Plan:
I've tested this manually - would love some inspiration on how to automate this, if the reviewer deem it needed.
To test this locally:
1. Install Ccache and make sure the `ccache` executable is in your `PATH` (verify by running `ccache --version`)
2. Create a new template app instance and apply the changes of this PR to the `node_modules/react-native` package.
3. Set the `USE_CCACHE` environment variable using `export USE_CCACHE=1`.
4. Run `pod install` in the `ios` directory.
5. Check the stats of Ccache (running `ccache -s`).
6. Run `npm run ios` or build the project from Xcode.
7. Check the Ccache stats again to verify ccache is intercepting compilation ("Cacheable calls" should ideally be 100%).
8. To check the speed gain:
  a. Delete the `ios/builds` directory
  b. Zero out the ccache stats (by running `ccache -z`)
  c. Run `pod install` again (only needed if you ran the initial `pod install` with new architecture enabled `RCT_NEW_ARCH_ENABLED=1`).
  d. Run `npm run ios` or build the project from Xcode.
  e. This last step should be significantly faster and you should see "Hits" under "Local storage" in the ccache stats approach 100%.

Reviewed By: huntie

Differential Revision: D52431507

Pulled By: cipolleschi

fbshipit-source-id: 6cfe39acd6250fae03959f0ee74d1f2fc46b0827

Original: facebook/react-native@e85d51c
  • Loading branch information
kraenhansen authored and facebook-github-bot committed Dec 28, 2023
1 parent 267a553 commit 357d2a5
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion template/template/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ target 'HelloWorld' do
react_native_post_install(
installer,
config[:reactNativePath],
:mac_catalyst_enabled => false
:mac_catalyst_enabled => false,
# :ccache_enabled => true
)
end
end

0 comments on commit 357d2a5

Please sign in to comment.