From 7b0275003406ebe1aabaa5e9a96bb5554d54c9e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Thor=C3=A9n?= Date: Wed, 27 Dec 2023 21:10:56 +0100 Subject: [PATCH 1/6] ENABLE_CCACHE option is available for all platforms --- CMakeLists.txt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f04a3a2c5..5d2ce2a29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,10 +79,19 @@ if(NOT APPLE_IOS AND NOT ANDROID) option(ENABLE_MONOLITHIC_INSTALL "Install everything in single directory on Linux and Mac" OFF) endif() -# On Linux, use -DCMAKE_CXX_COMPILER_LAUNCHER=ccache instead. -# The XCode and MSVC builds each require some more configuration, which is enabled by the following option: -if(MSVC OR (CMAKE_GENERATOR STREQUAL "Xcode")) - option(ENABLE_CCACHE "Speed up recompilation by caching previous compilations" OFF) +option(ENABLE_CCACHE "Speed up recompilation by caching previous compilations" OFF) +if(ENABLE_CCACHE) + find_program(CCACHE ccache) + if(CCACHE-NOTFOUND) + message(FATAL_ERROR "'ccache' could not be found; install it or set ENABLE_CCACHE=OFF.") + endif() +endif() + +# On Linux, use ccache via CMAKE_CXX_COMPILER_LAUNCHER. +# The XCode and MSVC builds each require some more configuration further down. +if(ENABLE_CCACHE AND LINUX) + set(CMAKE_C_COMPILER_LAUNCHER "ccache") + set(CMAKE_CXX_COMPILER_LAUNCHER "ccache") endif() if(ENABLE_CCACHE AND (CMAKE_GENERATOR STREQUAL "Xcode")) From fce3b5b83c634cf8c16185e18d71dcb72bd8a9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Thor=C3=A9n?= Date: Wed, 27 Dec 2023 21:12:52 +0100 Subject: [PATCH 2/6] Remove redundant find_program and if(CCACHE) - if ENABLE_CCACHE is set and the generation hasn't already failed then CCACHE is guaranteed to be set and point to a ccache binary. --- CMakeLists.txt | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d2ce2a29..8827a968e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -95,21 +95,18 @@ if(ENABLE_CCACHE AND LINUX) endif() if(ENABLE_CCACHE AND (CMAKE_GENERATOR STREQUAL "Xcode")) - find_program(CCACHE ccache REQUIRED) - if(CCACHE) - # https://stackoverflow.com/a/36515503/2278742 - # Set up wrapper scripts - configure_file(xcode/launch-c.in xcode/launch-c) - configure_file(xcode/launch-cxx.in xcode/launch-cxx) - execute_process(COMMAND chmod a+rx - "${CMAKE_BINARY_DIR}/xcode/launch-c" - "${CMAKE_BINARY_DIR}/xcode/launch-cxx") - # Set Xcode project attributes to route compilation through our scripts - set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_BINARY_DIR}/xcode/launch-c") - set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/xcode/launch-cxx") - set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_BINARY_DIR}/xcode/launch-c") - set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_BINARY_DIR}/xcode/launch-cxx") - endif() + # https://stackoverflow.com/a/36515503/2278742 + # Set up wrapper scripts + configure_file(xcode/launch-c.in xcode/launch-c) + configure_file(xcode/launch-cxx.in xcode/launch-cxx) + execute_process(COMMAND chmod a+rx + "${CMAKE_BINARY_DIR}/xcode/launch-c" + "${CMAKE_BINARY_DIR}/xcode/launch-cxx") + # Set Xcode project attributes to route compilation through our scripts + set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_BINARY_DIR}/xcode/launch-c") + set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/xcode/launch-cxx") + set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_BINARY_DIR}/xcode/launch-c") + set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_BINARY_DIR}/xcode/launch-cxx") endif() # Allow to pass package name from Travis CI @@ -275,19 +272,16 @@ if(MINGW OR MSVC) if(MSVC) if(ENABLE_CCACHE) # https://github.com/ccache/ccache/discussions/1154#discussioncomment-3611387 - find_program(CCACHE ccache REQUIRED) - if (CCACHE) - file(COPY_FILE - ${CCACHE} ${CMAKE_BINARY_DIR}/cl.exe - ONLY_IF_DIFFERENT) + file(COPY_FILE + ${CCACHE} ${CMAKE_BINARY_DIR}/cl.exe + ONLY_IF_DIFFERENT) - set(CMAKE_VS_GLOBALS - "CLToolExe=cl.exe" - "CLToolPath=${CMAKE_BINARY_DIR}" - "TrackFileAccess=false" - "UseMultiToolTask=true" - ) - endif() + set(CMAKE_VS_GLOBALS + "CLToolExe=cl.exe" + "CLToolPath=${CMAKE_BINARY_DIR}" + "TrackFileAccess=false" + "UseMultiToolTask=true" + ) endif() add_definitions(-DBOOST_ALL_NO_LIB) From 2569e1c661f4d2992ef598d334c60c059bd6661e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Thor=C3=A9n?= Date: Wed, 27 Dec 2023 21:13:41 +0100 Subject: [PATCH 3/6] Update documentation to suggest using option ENABLE_CCACHE instead of directly setting CMAKE_C(XX)_COMPILER_LAUNCHER --- docs/developers/Building_Android.md | 2 +- docs/developers/Building_Linux.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/developers/Building_Android.md b/docs/developers/Building_Android.md index ef0c2c909..fee0024a4 100644 --- a/docs/developers/Building_Android.md +++ b/docs/developers/Building_Android.md @@ -62,7 +62,7 @@ Building for Android is a 2-step process. First, native C++ code is compiled to This is a traditional CMake project, you can build it from command line or some IDE. You're not required to pass any custom options (except Conan toolchain file), defaults are already good. If you wish to use your own CMake presets, inherit them from our `build-with-conan` preset. Example: ``` -cmake -S . -B ../build -G Ninja -D CMAKE_BUILD_TYPE=Debug -D CMAKE_CXX_COMPILER_LAUNCHER=ccache -D CMAKE_C_COMPILER_LAUNCHER=ccache --toolchain ... +cmake -S . -B ../build -G Ninja -D CMAKE_BUILD_TYPE=Debug -D ENABLE_CCACHE:BOOL=ON --toolchain ... cmake --build ../build ``` diff --git a/docs/developers/Building_Linux.md b/docs/developers/Building_Linux.md index 5bd2f871c..4190ad49b 100644 --- a/docs/developers/Building_Linux.md +++ b/docs/developers/Building_Linux.md @@ -76,7 +76,7 @@ cmake ../vcmi **Notice**: The ../vcmi/ is not a typo, it will place makefile scripts into the build dir as the build dir is your working dir when calling CMake. ## To use ccache: -`cmake ../vcmi -D CMAKE_CXX_COMPILER_LAUNCHER=ccache -D CMAKE_C_COMPILER_LAUNCHER=ccache` +`cmake ../vcmi -D ENABLE_CCACHE:BOOL=ON` ## Trigger build From 9001124c3ad2690d6a3d00c3d7559eea0673e1f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Thor=C3=A9n?= Date: Wed, 27 Dec 2023 21:14:00 +0100 Subject: [PATCH 4/6] Add linux-gcc-release-ccache and linux-clang-release-ccache --- CMakePresets.json | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/CMakePresets.json b/CMakePresets.json index d96a905b0..255347cd6 100644 --- a/CMakePresets.json +++ b/CMakePresets.json @@ -65,6 +65,15 @@ "CMAKE_CXX_COMPILER": "/usr/bin/clang++" } }, + { + "name": "linux-clang-release-ccache", + "displayName": "Clang x86_64-pc-linux-gnu with ccache", + "description": "VCMI Linux Clang with ccache", + "inherits": "linux-release", + "cacheVariables": { + "ENABLE_CCACHE": "ON" + } + }, { "name": "linux-gcc-release", "displayName": "GCC x86_64-pc-linux-gnu", @@ -76,6 +85,15 @@ "CMAKE_CXX_COMPILER": "/usr/bin/g++" } }, + { + "name": "linux-gcc-release-ccache", + "displayName": "GCC x86_64-pc-linux-gnu with ccache", + "description": "VCMI Linux GCC with ccache", + "inherits": "linux-release", + "cacheVariables": { + "ENABLE_CCACHE": "ON" + } + }, { "name": "linux-gcc-debug", "displayName": "GCC x86_64-pc-linux-gnu (debug)", @@ -284,6 +302,11 @@ "configurePreset": "linux-clang-release", "inherits": "default-release" }, + { + "name": "linux-clang-release-ccache", + "configurePreset": "linux-clang-release-ccache", + "inherits": "linux-clang-release" + }, { "name": "linux-clang-test", "configurePreset": "linux-clang-test", @@ -299,6 +322,11 @@ "configurePreset": "linux-gcc-release", "inherits": "default-release" }, + { + "name": "linux-gcc-release-ccache", + "configurePreset": "linux-gcc-release-ccache", + "inherits": "linux-gcc-release" + }, { "name": "linux-gcc-debug", "configurePreset": "linux-gcc-debug", From 721c189b2020abaa509a6720df4dabe087cb6559 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Thor=C3=A9n?= Date: Wed, 27 Dec 2023 21:17:56 +0100 Subject: [PATCH 5/6] Replace direct usage of CMAKE_CXX_COMPILER_LAUNCHER with ENABLE_CCACHE in CI --- .github/workflows/github.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/github.yml b/.github/workflows/github.yml index ac20a6aea..219754038 100644 --- a/.github/workflows/github.yml +++ b/.github/workflows/github.yml @@ -244,7 +244,7 @@ jobs: - name: CMake Preset with ccache run: | - cmake -DCMAKE_CXX_COMPILER_LAUNCHER=ccache --preset ${{ matrix.preset }} + cmake -DENABLE_CCACHE:BOOL=ON --preset ${{ matrix.preset }} - name: Build Preset run: | From 25be15a0231b7e7a179c701f52151cc9dbc6e520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Thor=C3=A9n?= Date: Fri, 29 Dec 2023 21:54:58 +0100 Subject: [PATCH 6/6] Use 'REQUIRED' instead of custom error message when ccache is not found --- CMakeLists.txt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8827a968e..32e78e58d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,10 +81,7 @@ endif() option(ENABLE_CCACHE "Speed up recompilation by caching previous compilations" OFF) if(ENABLE_CCACHE) - find_program(CCACHE ccache) - if(CCACHE-NOTFOUND) - message(FATAL_ERROR "'ccache' could not be found; install it or set ENABLE_CCACHE=OFF.") - endif() + find_program(CCACHE ccache REQUIRED) endif() # On Linux, use ccache via CMAKE_CXX_COMPILER_LAUNCHER.