diff --git a/CMakeLists.txt b/CMakeLists.txt index 33ca9e1fd6e..3225f0fc715 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,6 +86,17 @@ announce_configured_options(CMAKE_TOOLCHAIN_FILE) load_build_preset() include(${PROJECT_SOURCE_DIR}/tools/cmake/preset/default.cmake) +# 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) + # Print all the configs that were called with announce_configured_options. print_configured_options() diff --git a/docs/source/using-executorch-building-from-source.md b/docs/source/using-executorch-building-from-source.md index 6438b9a869e..a5518818263 100644 --- a/docs/source/using-executorch-building-from-source.md +++ b/docs/source/using-executorch-building-from-source.md @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/install_executorch.py b/install_executorch.py index 844d3ab226b..22a9b8f3725 100644 --- a/install_executorch.py +++ b/install_executorch.py @@ -49,6 +49,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.")