diff --git a/CMakeLists.txt b/CMakeLists.txt index 3a3b1f7bfe..4c928e1e41 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) # MARK: - Start EXECUTORCH_H12025_BUILD_MIGRATION include(${PROJECT_SOURCE_DIR}/tools/cmake/common/preset.cmake) diff --git a/docs/source/using-executorch-building-from-source.md b/docs/source/using-executorch-building-from-source.md index 7680e7bb74..ba6b47bc6f 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 @@ -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: @@ -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 @@ -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 diff --git a/install_executorch.py b/install_executorch.py index 12452ef499..d453876ab0 100644 --- a/install_executorch.py +++ b/install_executorch.py @@ -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.")