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: | diff --git a/CMakeLists.txt b/CMakeLists.txt index f04a3a2c5..32e78e58d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,28 +79,31 @@ 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 REQUIRED) +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")) - 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 @@ -266,19 +269,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) 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", 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