XCRemoteCache is a remote cache tool for Xcode projects. It reuses target artifacts generated on a remote machine, served from a simple REST server.
- How and Why?
- How to integrate XCRemoteCache with your Xcode project?
- 1. Download XCRemoteCache
- A. Automatic integration
- B. Manual integration
- A full list of configuration parameters:
- Backend cache server
- CocoaPods plugin
- Code of conduct
- Security Issues?
How and Why?
The caching mechanism is based on remote artifacts that should be generated and uploaded to the cache server for each commit on a
master branch, preferably as a part of CI/CD step. Xcode products are not portable between different Xcode versions, each XCRemoteCache artifact is linked with a specific Xcode build number that generated it. To support multiple Xcode versions, artifacts generation should happen for each Xcode version.
The artifact resue flow is as follows: XCRemoteCache performs a target precheck (aka prebuild) and if a fingerprint for local sources matches the one computed on a generation side, several compilation steps wrappers (e.g.
xclibtool) mock corresponding compilation step(s) and linking (or archiving) moves the cached build artifact to the expected location.
Multiple commits that have the same target sources reuse artifact package on a remote server.
Accurate target input files
Finding a precise list of input and dependency files is a non-trivial task as Xcode heavily relies on implicit target dependencies. It means that Xcode is trying to use required dependencies from provided search paths and looks up
DerivedData's product dir. To find a narrow list of files to compute fingerprint hash, XCRemoteCache fetches a
meta.json file from a server which contains remotelly generated fingerprint hash and a list of input files that should constitute a fingerprint. That list of input files was produced during the artifact generation process, based on
.d output from
Before building a project in Xcode, XCRemoteCache needs to find the best git commit sha for which artifacts will be used. This happens as a part of the
xcprepare execution, which should be called after each merge or switching a branch.
xcprepare finds a list of 10 most recent common sha with the remote repo branch using git's first-parent strategy and selects the newest one for which all artifacts have been uploaded.
The generation side is responsible to call
xcprepare mark subcommand after each successful build. Marking process creates an empty marker file on a remote cache server with a given format:
HEAD requests for all identified shas, picks the newest one for which a marker file exists remotely, and saves it in the text form to the
arc.rc file. That file informs the prebuild phase which meta file should be fetched to get a list of target dependency files.
New file added to the target
Considering in the hash fingerprint only a list of previously observed files can give invalid results if a build contains a new source file as it isn't considered in the hash.
For a new
.swift file in a swift-only target,
xcswiftc automatically recognizes that case and forces local compilation of the entire target. For Objective-C or mixed targets, fallbacking to the local compilation is more difficult as some previous invocations (either
xcswiftc) could already be finished with no-operation. To mitigate that, each wrapper appends invocation call to a side file (
history.compile) just in case some other process would need to compile the entire target locally. If that happens, compilation of the newly added file acquires a target-wide lock that stops other wrapper invocations, executes already mocked steps one by one to backfill already skipped compilation steps.
Binaries built with "debug symbols: enabled" embed source file absolute paths so compilation products cannot be directly ported between two machines with different source roots. Otherwise, LLDB debugger is not able to correlate a set of currently executing machine instructions with a local file that produced it. To mitigate that, XCRemoteCache recommends adding a custom C and Swift debug flags
prefix-map for all XCRemoteCache builds. These flags ensure that all binaries, generated locally and downloaded from a remote server, have the same debug symbols absolute paths which are translated to an actual local path at the beginning of the LLDB session.
XCRemoteCache involves several optimization techniques:
- Local HTTP cache stores all responses from the remote server at
- Prebuild and postbuild steps leverage Xcode's discovered dependency file to avoid recomputing fingerprint hashes if none of the input files has changed
- A wrapper for the
clangcompilation is a C program, generated and compiled during the
xcpreparestep. It is called many times to compile each
*.(m|c)file and accessing a disk to read a configuration would introduce a significant slowdown, especially if a project contains a lot of Objective-C files. As a remedy,
xcpreparereads the XCRemoteCache configuration only once and embeds all configurable fields directly into the
arc.rc, generated by
xcprepare, gets file modification equal to the commit date if refers.
arc.rcis included in the discovered dependency file, touching it in the
xcpreparewould automatically invalidate previous XCRemoteCache prebuild step and force redundant fingerprint checks. By syncing the
mdatewith a git commit, Xcode avoids prebuild steps unless the remote cache commit has changed
- If a target cache miss happens, XCRemoteCache disables cache for that target until a commit sha in
arc.rcchanges. That bypasses a fingerprint computation for incremental builds
If a list of targets that can have dirty sources is limited, XCRemoteCache can be configured with focused targets, specified in
By default, all targets are focused and these compare local fingerprint with one available remotely and fallbacks to the local compilation if it doesn't match. Non-focused targets, called 'thin' targets, always use cached artifacts what eliminates a fingerprint computation. Thin targets should contain only a single compilation file with
How to integrate XCRemoteCache with your Xcode project?
To enable XCRemoteCache in the existing
.xcodeproj you need to add extra build settings and build phases to targets that you want to cache.
You can do that in an automatic way, using the XCRemoteCache-provided integration command, or manually modify your Xcode project.
1. Download XCRemoteCache
From the Github Releases page, download the XCRemoteCache bundle zip. Unzip the bundle to a directory next to your
The following steps will assume the bundle has been unzipped to
xcremotecache dir, placed next to the
A. Automatic integration
2. Create a minimal XCRemoteCache configuration
.rcinfo yaml file next to the
.xcodeproj with a minimum set of configuration entries, like:
primary_repo: https://yourRepo.git cache_addresses: - https://xcremotecacheserver.com
3. Run automatic integration script
3a. Producer side
Execute a command that modifies
xcremotecache/xcprepare integrate --input <yourProject.xcodeproj> --mode producer --final-producer-target <YourMainTarget>
3b. Consumer side
Execute a command that modifies
xcremotecache/xcprepare integrate --input <yourProject.xcodeproj> --mode consumer
A full list of
xcprepare integrate supported options
||mode. Supported values:
||comma-separated list of targets to integrate XCRemoteCache.||
||comma-separated list of targets to not integrate XCRemoteCache. Takes priority over --targets-include.||
||comma-separated list of configurations to integrate XCRemoteCache.||
||comma-separated list of configurations to not integrate XCRemoteCache. Takes priority over --configurations-include.||
||[Producer only] The final target that generates cache artifacts. Once this targets is finished, no other targets are allowed to upload artifacts to the remote server for a given sha, configuration and platform context.||
||[Consumer only] comma-separated list of configurations that need to have all artifacts uploaded to the remote site before using given sha.||
||[Consumer only] comma-separated list of platforms that need to have all artifacts uploaded to the remote site before using given sha||
||LLDBInit mode. Appends to .lldbinit a command required for debugging. Supported values: 'none' (do not append to .lldbinit), 'user' (append to ~/.lldbinit)||
||An arbitrary source location shared between producers and consumers. Should be unique for a project.||
||Save the project with integrated XCRemoteCache to a separate location.||N/A|
B. Manual integration
2. Configure XCRemoteCache
Create yaml configuration file
.rcinfo, next to the
.xcodeproj, with your full XCRemoteCache configuration, according to the parameters list e.g.:
primary_repo: https://yourRepo.git cache_addresses: - https://xcremotecacheserver.com repo_root: "." remote_commit_file: arc.rc xccc_file: xcremotecache/xccc
3. Call xcprepare
xcprepare --configuration #Configuration# --platform #platform# command after each merge or rebase with the primary branch. Otherwise, the remote cache artifacts may be outdated and final hit rate may be poor.
xcprepare application saves
arc.rc file on a disk and prints a summary to the standard output. The printed
recommended_remote_address is just a recommendation which cache remote server use. It is up to the integration tooling to decide if it makes sense. If so, the project's
.rcinfo should define that value as
$ xcremotecache/xcprepare --configuration Debug --platform iphonesimulator result: true commit: aabbccc00 age: 0 recommended_remote_address: https://xcremotecacheserver.com
4. Integrate with the Xcode project
Configure Xcode targets that should use XCRemoteCache:
- Override Build Settings:
SWIFT_EXEC- location of
LIBTOOL- location of
LD- location of
- Add a
Prebuildbuild phase (before compilation):
- input files: location of
- output files:
- discovery dependency file:
Postbuildbuild phase (after compilation):
- input files: location of
- output files:
- discovery dependency file:
5. Configure LLDB source-map (Optional)
Rewriting source-map is required to support debugging and hit breakpoints, see Debug symbols.
- Ooverride the following Build Settings for all targets:
/xxxxxxxxxx(or any other arbitrary string for your project)
OTHER_SWIFT_FLAGS. If it doesn't exist, define it as
$(inherited) -debug-prefix-map $(SRCROOT)=$(XCRC_SRCROOT)
OTHER_CFLAGS. If it doesn't exists, define it as
settings set target.source-map /xxxxxxxxxx /Users/account/src/PathToTheProjectto
~/.lldbiniton end machine that builds a project with XCRemoteCache
XCRC_SRCROOTarbitrary path should be project-exclusive to avoid clashing.
Tip: In some rare cases, Xcode caches
~/.lldbinit content so make sure to restart Xcode after the modification.
6. Producer mode - Artifacts generation
XCRemoteCache can operate in two main modes:
consumer (default) tries to reuse artifacts available on the remote server and
producer is used to generate all artifacts - it builds all targets locally and uploads meta and artifact files to the remote cache server.
6a. Configure producer mode
To enable the
producer mode, configure it directly in the
Optionally, you can define
.rcinfowith a path to the other yaml file that will override the default configuration in
.rcinfo. That approach can be useful if you want to track main
.rcinfoand keep your local configuration out of git.
6b. Fill the cache
Build the project from Xcode or using
6c. Mark commit sha
Once all artifacts have been uploaded, "mark a build" using
xcprepare mark command:
$ xcremotecache/xcprepare mark --configuration Debug --platform iphonesimulator
That command creates an empty file on a remote server which informs that for given sha, configuration, platform, Xcode versions etc. all artifacts are available.
Note that for the
producer mode, the prebuild build phase and
xclibtool wrappers become no-op, so it is recommended to not add them for the
A full list of configuration parameters:
||build mode. Possible values:
||Addresses of all remote cache replicas. Required to be a non-empty array of strings||N/A|
||Address of the best remote cache to use in the consumer mode. If not specified, the first item in
||Probe request path to the
||Filepath to the file with the remote commit sha||
||Path to the xccc wrapper||
||Path, relative to
||Path, relative to
||Path, relative to
||Command for a standard C compilation fallback||
||Command for a standard Swift compilation fallback||
||Address of the primary git repository that produces cache artifacts (case-sensitive)||N/A|
||The main (primary) branch on the
||The path to the git repo root||
||Number of historical git commits to look for cache artifacts||
||Source root of the Xcode project||
||Fingerprint override extension (sample override
||Configuration file that overrides project configuration||
||Custom commit sha to publish artifact (producer only)||
||Maximum age in days HTTP response should be locally cached before being evicted||
||Extra ENV keys that should be convoluted into the environment fingerprint||
||Directory where all XCRemoteCache statistics (e.g. counters) are stored||
||Number of retries for download requests||
||Number of retries for upload requests||
||Dictionary of extra HTTP headers for all remote server requests||
||Filename (without an extension) of the compilation input file that is used as a fake compilation for the forced-cached target (aka thin target)||
||A list of all targets that are not thinned. If empty, all targets are meant to be non-thin||
||Disable cache for http requests to fetch metadata and download artifacts||
||Path, relative to $TARGET_TEMP_DIR which gathers all compilation commands that should be executed if a target switches to local compilation. Example: A new
||Timeout for remote response data interval (in seconds). If an interval between data chunks is longer than a timeout, a request fails.||
||If true, any observed request timeout switches off remote cache for all targets||
||List of all extensions that should carry over source fingerprints. Extensions of all product files that contain non-deterministic content (absolute paths, timestamp, etc) should be included.||
||If true, support for thin projects is enabled||
||Module name of a target that works as a helper for thinned targets||
||Secret key for AWS V4 Signature Authorization. If this is set to a non-empty String - an Authentication Header will be added based on this and the other
||Access key for AWS V4 Signature Authorization.||
||Region for AWS V4 Signature Authorization. E.g.
||Service for AWS V4 Signature Authorization. E.g.
Backend cache server
As a cache server, XCRemoteCache may use any REST server that supports PUT, GET and HEAD methods.
For the development phase, you can try the simplest cache server available as a docker image in backend-example. For the production environment, it is recommended to configure a reliable, fast server, preferrably located in a close proximity to developer's machines.
Out-of-the-box, XCRemoteCache supports V4 Signature Authorization used by Amazon's S3 and Google's GCS. Altenatively, if your server has a customized authentication procedure, you can add extra HTTP request headers with
request_custom_headers configuration property.
Sample REST cache server from a docker image
To run a local instance of a server, use a snippet which exposes a cache endpoint under
docker build -t xcremotecache-demo-server backend-example docker run -it --rm -d -p 8080:8080 --name xcremotecache xcremotecache-demo-server
As the docker image saves all files in a container non-persistent storage, to reset cache's content, just restart it:
# stop the container docker kill xcremotecache # run a new instance of the image docker run -it --rm -d -p 8080:8080 --name xcremotecache xcremotecache-demo-server
To review all files stored in the cache server, navigate to the container's cache root directory:
docker exec -w /tmp/cache -it xcremotecache /bin/bash
Amazon S3 and Google Cloud Storage
XCRemoteCache supports Amazon S3 and Google Cloud Storage buckets to be used as cache servers using the Amazon v4 Signature Authorization.
To set it up use the configuration parameters
aws_service in the
.rcinfo file. Specify the URL to the bucket in cache-addresses field in the same file.
... cache_addresses: - https://bucketname.s3.eu-central-1.amazonaws.com/ aws_secret_key: <SECRET_KEY> aws_access_key: <ACCESS_KEY> aws_region: eu-central-1 aws_service: s3 ...
Retention Policy: Buckets usually have a retention policy option which ensures objects are retained for a certain amount of time and won't be modified or deleted. Keep this option short or disable it to avoid errors in case multiple builds are done consecutively on the producer side for the same configuration.
Head over to our cocoapods-plugin docs to see how to integrate XCRemoteCache in your CocoaPods project.
- The repo under
- Xcode 11.4+
- Xcode New Build System
- Current Xcode location set by
- Using the default Xcode Toolchain
- Recommended: multi-targets Xcode project
- Recommended: do not use fast-forward PR strategy (use merge or squash instead)
- Recommended: avoid
DWARF with dSYM File"Debug Information Format" build setting. Use
- Swift Package Manager (SPM) projects are not supported
- Filenames with
_vers.csuffix are reserved and cannot be used as a source file
- All compilation files should be referenced via the git repo root. Referencing
../../someOther.swiftthat resolve to the location outside of the git repo root is prohibited.
Follow the FAQ page.
Follow the Development guide. It has all the information on how to get started.
Building release package
To build a release zip package for a single platform (e.g.
rake 'build[release, x86_64-apple-macosx]'
The zip package will be generated at
We feel that a welcoming community is important and we ask that you follow Spotify's Open Source Code of Conduct in all interactions with the community.
Code of conduct
This project adheres to the Open Code of Conduct. By participating, you are expected to honor this code.
Copyright 2021 Spotify AB Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Please report sensitive security issues via Spotify's bug-bounty program (https://hackerone.com/spotify) rather than GitHub.