Skip to content

Add instructions to enable ccache #11838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
cmake_minimum_required(VERSION 3.24)
project(executorch)

# Enable ccache if available
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
message(STATUS "ccache found and enabled for faster builds")
else()
message(STATUS "ccache not found, builds will not be cached")
endif()
announce_configured_options(CCACHE_PROGRAM)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops you need to move this below line 66 cause we import this function later. I'd suggest to move this down to line 98 after we've configured some common requirements like python and buck

# MARK: - Start EXECUTORCH_H12025_BUILD_MIGRATION

include(${PROJECT_SOURCE_DIR}/tools/cmake/common/preset.cmake)
Expand Down
34 changes: 31 additions & 3 deletions docs/source/using-executorch-building-from-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Windows (x86_64)
* `g++` version 7 or higher, `clang++` version 5 or higher, or another
C++17-compatible toolchain.
* `python` version 3.10-3.12
* `ccache` (optional) - A compiler cache that speeds up recompilation

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

See the [PyTorch instructions](https://github.com/pytorch/pytorch#installation) on how to build PyTorch from source.

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

Expand Down Expand Up @@ -119,6 +120,8 @@ Or alternatively, [install conda on your machine](https://conda.io/projects/cond
> git submodule sync
> git submodule update --init --recursive
> ```
>
> The `--clean` command removes build artifacts, pip outputs, and also clears the ccache if it's installed, ensuring a completely fresh build environment.

## Build ExecuTorch C++ runtime from source

Expand Down Expand Up @@ -171,6 +174,29 @@ To further optimize the release build for size, use both:
-DEXECUTORCH_OPTIMIZE_SIZE=ON
```

#### Compiler Cache (ccache)

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:

- If ccache is detected, you'll see: `ccache found and enabled for faster builds`
- If ccache is not installed, you'll see: `ccache not found, builds will not be cached`

To install ccache:
```bash
# Ubuntu/Debian
sudo apt install ccache

# macOS
brew install ccache

# CentOS/RHEL
sudo yum install ccache
# or
sudo dnf install ccache
```

No additional configuration is needed - the build system will automatically use ccache when available.

See [CMakeLists.txt](https://github.com/pytorch/executorch/blob/main/CMakeLists.txt)

### Build the runtime components
Expand All @@ -190,6 +216,8 @@ cd executorch
cmake --build cmake-out -j9
```

> **_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.

## Use an example binary `executor_runner` to execute a .pte file

First, generate a .pte file, either by exporting an example model or following
Expand Down Expand Up @@ -243,7 +271,7 @@ Install ClangCL for Windows from the [official website](https://learn.microsoft.
To check if conda is detected by the powershell prompt, try `conda list` or `conda --version`

If conda is not detected, you could run the powershell script for conda named `conda-hook.ps1`.
To verify that Conda is available in the in the powershell environment, run try `conda list` or `conda --version`.
To verify that Conda is available in the in the powershell environment, run try `conda list` or `conda --version`.
If Conda is not available, run conda-hook.ps1 as follows:
```bash
$miniconda_dir\\shell\\condabin\\conda-hook.ps1
Expand Down
13 changes: 13 additions & 0 deletions install_executorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ def clean():
shutil.rmtree(d, ignore_errors=True)
print("Cleaning buck-out/...")
shutil.rmtree("buck-out/", ignore_errors=True)

# Clean ccache if available
try:
result = subprocess.run(["ccache", "--version"], capture_output=True, text=True)
if result.returncode == 0:
print("Cleaning ccache...")
subprocess.run(["ccache", "--clear"], check=True)
print("ccache cleared successfully.")
else:
print("ccache not found, skipping ccache cleanup.")
except (subprocess.CalledProcessError, FileNotFoundError):
print("ccache not found, skipping ccache cleanup.")

print("Done cleaning build artifacts.")


Expand Down
Loading