Skip to content

Commit a88216b

Browse files
committed
Add instructions to enable ccache
1 parent ba9254a commit a88216b

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@
4848
cmake_minimum_required(VERSION 3.24)
4949
project(executorch)
5050

51+
# Enable ccache if available
52+
find_program(CCACHE_PROGRAM ccache)
53+
if(CCACHE_PROGRAM)
54+
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
55+
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
56+
message(STATUS "ccache found and enabled for faster builds")
57+
else()
58+
message(STATUS "ccache not found, builds will not be cached")
59+
endif()
60+
5161
# MARK: - Start EXECUTORCH_H12025_BUILD_MIGRATION
5262

5363
include(${PROJECT_SOURCE_DIR}/tools/cmake/common/preset.cmake)

docs/source/using-executorch-building-from-source.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Windows (x86_64)
3030
* `g++` version 7 or higher, `clang++` version 5 or higher, or another
3131
C++17-compatible toolchain.
3232
* `python` version 3.10-3.12
33+
* `ccache` (optional) - A compiler cache that speeds up recompilation
3334

3435
Note that the cross-compilable core runtime code supports a wider range of
3536
toolchains, down to C++17. See the [Runtime Overview](runtime-overview.md) for
@@ -64,8 +65,8 @@ Or alternatively, [install conda on your machine](https://conda.io/projects/cond
6465
# Intel-based macOS systems require building PyTorch from source (see below)
6566
./install_executorch.sh
6667
```
67-
68-
See the [PyTorch instructions](https://github.com/pytorch/pytorch#installation) on how to build PyTorch from source.
68+
69+
See the [PyTorch instructions](https://github.com/pytorch/pytorch#installation) on how to build PyTorch from source.
6970

7071
Use the [`--use-pt-pinned-commit` flag](../../install_executorch.py) to install ExecuTorch with an existing PyTorch build:
7172

@@ -119,6 +120,8 @@ Or alternatively, [install conda on your machine](https://conda.io/projects/cond
119120
> git submodule sync
120121
> git submodule update --init --recursive
121122
> ```
123+
>
124+
> The `--clean` command removes build artifacts, pip outputs, and also clears the ccache if it's installed, ensuring a completely fresh build environment.
122125
123126
## Build ExecuTorch C++ runtime from source
124127
@@ -171,6 +174,29 @@ To further optimize the release build for size, use both:
171174
-DEXECUTORCH_OPTIMIZE_SIZE=ON
172175
```
173176
177+
#### Compiler Cache (ccache)
178+
179+
ExecuTorch automatically detects and enables [ccache](https://ccache.dev/) if it's installed on your system. This significantly speeds up recompilation by caching previously compiled objects:
180+
181+
- If ccache is detected, you'll see: `ccache found and enabled for faster builds`
182+
- If ccache is not installed, you'll see: `ccache not found, builds will not be cached`
183+
184+
To install ccache:
185+
```bash
186+
# Ubuntu/Debian
187+
sudo apt install ccache
188+
189+
# macOS
190+
brew install ccache
191+
192+
# CentOS/RHEL
193+
sudo yum install ccache
194+
# or
195+
sudo dnf install ccache
196+
```
197+
198+
No additional configuration is needed - the build system will automatically use ccache when available.
199+
174200
See [CMakeLists.txt](https://github.com/pytorch/executorch/blob/main/CMakeLists.txt)
175201
176202
### Build the runtime components
@@ -190,6 +216,8 @@ cd executorch
190216
cmake --build cmake-out -j9
191217
```
192218
219+
> **_TIP:_** For faster rebuilds, consider installing ccache (see [Compiler Cache section](#compiler-cache-ccache) above). On first builds, ccache populates its cache. Subsequent builds with the same compiler flags can be significantly faster.
220+
193221
## Use an example binary `executor_runner` to execute a .pte file
194222
195223
First, generate a .pte file, either by exporting an example model or following
@@ -243,7 +271,7 @@ Install ClangCL for Windows from the [official website](https://learn.microsoft.
243271
To check if conda is detected by the powershell prompt, try `conda list` or `conda --version`
244272
245273
If conda is not detected, you could run the powershell script for conda named `conda-hook.ps1`.
246-
To verify that Conda is available in the in the powershell environment, run try `conda list` or `conda --version`.
274+
To verify that Conda is available in the in the powershell environment, run try `conda list` or `conda --version`.
247275
If Conda is not available, run conda-hook.ps1 as follows:
248276
```bash
249277
$miniconda_dir\\shell\\condabin\\conda-hook.ps1

install_executorch.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@ def clean():
5050
shutil.rmtree(d, ignore_errors=True)
5151
print("Cleaning buck-out/...")
5252
shutil.rmtree("buck-out/", ignore_errors=True)
53+
54+
# Clean ccache if available
55+
try:
56+
result = subprocess.run(["ccache", "--version"], capture_output=True, text=True)
57+
if result.returncode == 0:
58+
print("Cleaning ccache...")
59+
subprocess.run(["ccache", "--clear"], check=True)
60+
print("ccache cleared successfully.")
61+
else:
62+
print("ccache not found, skipping ccache cleanup.")
63+
except (subprocess.CalledProcessError, FileNotFoundError):
64+
print("ccache not found, skipping ccache cleanup.")
65+
5366
print("Done cleaning build artifacts.")
5467

5568

0 commit comments

Comments
 (0)