From 299362b3cf7da38a4c0ecb09f94f79a5d0acfd3f Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Thu, 30 Jul 2020 22:27:21 +0000 Subject: [PATCH] Refactor cmake ccache support and support MSVC. Don't disable ccache for msys+ninja anymore, since the mingw ccache works now and there is no reason to use the msys ccache anymore. Use RULE_LAUNCH_COMPILE only on cmake versions < 3.4.0, because this currently breaks resource compilation with visual studio and the windows native ccache from chocolatey, this needs to be fixed in ccache. On cmake 3.4.0 and greater, set the variables CMAKE__COMPILER_LAUNCHER instead. This has the effect of using ccache for C, C++ and nasm, but not for the resource file, avoiding the problem with visual studio, which has a more recent cmake. This must be done before the project() call. TODO: Currently the Visual Studio build with ccache from chocolatey works correctly, ccache is being invoked from ninja, but no cache files are being created. This is being followed up with the chocolatey package maintainer and upstream if necessary. The resource compiler issue also needs to be fixed upstream. Signed-off-by: Rafael Kitover --- CMakeLists.txt | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b9993c1..0d2e6d63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,25 @@ if(VCPKG_TARGET_TRIPLET MATCHES -static OR CMAKE_TOOLCHAIN_FILE MATCHES "mxe|-st set(VBAM_STATIC_DEFAULT ON) endif() +# Use ccache if available and not already enabled on the command line. +# This has to be done before the project() call. +if(NOT (CMAKE_CXX_COMPILER_LAUNCHER OR CMAKE_CXX_COMPILER MATCHES ccache)) + find_program(CCACHE_EXECUTABLE ccache) + if(CCACHE_EXECUTABLE) + message(STATUS "Enabling ccache") + + if(CMAKE_VERSION VERSION_LESS 3.4.0) + # FIXME: This method currently breaks for native ccache on windows + # with the visual studio resource compiler. + set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_EXECUTABLE}) + else() + set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_EXECUTABLE} CACHE STRING "C compiler launcher" FORCE) + set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_EXECUTABLE} CACHE STRING "C++ compiler launcher" FORCE) + set(CMAKE_ASM_NASM_COMPILER_LAUNCHER ${CCACHE_EXECUTABLE} CACHE STRING "nasm assembler launcher" FORCE) + endif() + endif() +endif() + project(VBA-M C CXX) cmake_minimum_required(VERSION 2.8.12) @@ -92,18 +111,6 @@ if(VBAM_STATIC) endif() endif() -# use ccache if available, and not already enabled on the command line -# but not with ninja and msys ccache on msys2 -if(NOT (WIN32 AND NOT "$ENV{MSYSTEM_PREFIX}" STREQUAL "" AND CMAKE_GENERATOR STREQUAL Ninja)) - if(NOT CMAKE_CXX_COMPILER_LAUNCHER AND NOT CMAKE_C_COMPILER_LAUNCHER) - find_program(CCACHE_FOUND ccache) - if(CCACHE_FOUND) - set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) - set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) - endif(CCACHE_FOUND) - endif() -endif() - set(SSP_DEFAULT OFF) option(ENABLE_SSP "Enable gcc stack protector support" ${SSP_DEFAULT})