Skip to content

Commit 1a1886a

Browse files
mergennachinjathu
andauthored
Add instructions to enable ccache (#11838)
cc @larryliu0820 @jathu --------- Co-authored-by: jathu <[email protected]>
1 parent 7adffe6 commit 1a1886a

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ announce_configured_options(CMAKE_TOOLCHAIN_FILE)
8686
load_build_preset()
8787
include(${PROJECT_SOURCE_DIR}/tools/cmake/preset/default.cmake)
8888

89+
# Enable ccache if available
90+
find_program(CCACHE_PROGRAM ccache)
91+
if(CCACHE_PROGRAM)
92+
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
93+
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
94+
message(STATUS "ccache found and enabled for faster builds")
95+
else()
96+
message(STATUS "ccache not found, builds will not be cached")
97+
endif()
98+
announce_configured_options(CCACHE_PROGRAM)
99+
89100
# Print all the configs that were called with announce_configured_options.
90101
print_configured_options()
91102

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

Lines changed: 28 additions & 0 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
@@ -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

install_executorch.py

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

5467

0 commit comments

Comments
 (0)