diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cdeae4d..1085cf9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,13 +15,13 @@ jobs: run: brew install ccache - name: Configure - run: cmake -Htest -Bbuild -DUSE_CCACHE=YES + run: cmake -Htest -Bbuild -DUSE_CCACHE=YES -DCCACHE_OPTIONS="CCACHE_CPP2=true;CCACHE_SLOPPINESS=clang_index_store" - name: Build run: "cmake --build build" - name: Configure for XCode - run: cmake -Htest -Bbuildx -GXcode -DUSE_CCACHE=YES + run: cmake -Htest -Bbuildx -GXcode -DUSE_CCACHE=YES -DCCACHE_OPTIONS="CCACHE_CPP2=true;CCACHE_SLOPPINESS=clang_index_store" - name: Build for XCode run: "cmake --build buildx" diff --git a/CMakeLists.txt b/CMakeLists.txt index 3797328..627cf8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,38 +3,46 @@ cmake_minimum_required(VERSION 3.4) option(USE_CCACHE "" OFF) +set(CCACHE_OPTIONS "" CACHE STRING "options for ccache") if (USE_CCACHE) - find_program(CCACHE_PROGRAM ccache) - - if(CCACHE_PROGRAM) - # Set up wrapper scripts - set(C_LAUNCHER "${CCACHE_PROGRAM}") - set(CXX_LAUNCHER "${CCACHE_PROGRAM}") - configure_file(launch-c.in launch-c) - configure_file(launch-cxx.in launch-cxx) - execute_process(COMMAND chmod a+rx - "${CMAKE_CURRENT_BINARY_DIR}/launch-c" - "${CMAKE_CURRENT_BINARY_DIR}/launch-cxx" - ) - - if(CMAKE_GENERATOR STREQUAL "Xcode") - # Set Xcode project attributes to route compilation and linking - # through our scripts - set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_CURRENT_BINARY_DIR}/launch-c" CACHE INTERNAL "") - set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_CURRENT_BINARY_DIR}/launch-cxx" CACHE INTERNAL "") - set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_CURRENT_BINARY_DIR}/launch-c" CACHE INTERNAL "") - set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_CURRENT_BINARY_DIR}/launch-cxx" CACHE INTERNAL "") - else() - # Support Unix Makefiles and Ninja - set(CMAKE_C_COMPILER_LAUNCHER "${CMAKE_CURRENT_BINARY_DIR}/launch-c" CACHE INTERNAL "") - set(CMAKE_CXX_COMPILER_LAUNCHER "${CMAKE_CURRENT_BINARY_DIR}/launch-cxx" CACHE INTERNAL "") - endif() - - message(STATUS "Using ccache: ${CCACHE_PROGRAM}") - else() - message(WARNING "Could not enable Ccache: could not find program") - endif() + find_program(CCACHE_PROGRAM ccache) + + if(CCACHE_PROGRAM) + # Set up wrapper scripts + set(C_LAUNCHER "${CCACHE_PROGRAM}") + set(CXX_LAUNCHER "${CCACHE_PROGRAM}") + set(CCCACHE_EXPORTS "") + + foreach(option ${CCACHE_OPTIONS}) + set(CCCACHE_EXPORTS "${CCCACHE_EXPORTS}\nexport ${option}") + endforeach() + + configure_file(launch-c.in launch-c) + configure_file(launch-cxx.in launch-cxx) + + execute_process(COMMAND chmod a+rx + "${CMAKE_CURRENT_BINARY_DIR}/launch-c" + "${CMAKE_CURRENT_BINARY_DIR}/launch-cxx" + ) + + if(CMAKE_GENERATOR STREQUAL "Xcode") + # Set Xcode project attributes to route compilation and linking + # through our scripts + set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_CURRENT_BINARY_DIR}/launch-c" CACHE INTERNAL "") + set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_CURRENT_BINARY_DIR}/launch-cxx" CACHE INTERNAL "") + set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_CURRENT_BINARY_DIR}/launch-c" CACHE INTERNAL "") + set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_CURRENT_BINARY_DIR}/launch-cxx" CACHE INTERNAL "") + else() + # Support Unix Makefiles and Ninja + set(CMAKE_C_COMPILER_LAUNCHER "${CMAKE_CURRENT_BINARY_DIR}/launch-c" CACHE INTERNAL "") + set(CMAKE_CXX_COMPILER_LAUNCHER "${CMAKE_CURRENT_BINARY_DIR}/launch-cxx" CACHE INTERNAL "") + endif() + + message(STATUS "Using ccache: ${CCACHE_PROGRAM}") + else() + message(WARNING "Could not enable Ccache: could not find program") + endif() endif() diff --git a/README.md b/README.md index 7e4004d..3e81c1b 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,12 @@ A simple, Xcode compatible _Ccache_ integration for _CMake_, based on [this](htt [Ccache](https://ccache.dev) is a compiler cache that can drastically improve build times for C and C++ projects. This script makes it easy to configure a CMake project to use Ccache by adding the configuration option `USE_CCACHE` which will active Ccache support when enabled. +Build-specific environmental variables can be set with the `CCACHE_OPTIONS` configuration parameter. It is currently compatible with _Makefile_, _Ninja_ and _Xcode_ generators. Example usage: ```bash -cmake . -DUSE_CCACHE=YES +cmake . -DUSE_CCACHE=YES -DCCACHE_OPTIONS="CCACHE_CPP2=true;CCACHE_SLOPPINESS=clang_index_store" ``` ## How to integrate diff --git a/launch-c.in b/launch-c.in index 7107bc9..68b8c10 100644 --- a/launch-c.in +++ b/launch-c.in @@ -6,5 +6,6 @@ if [[ "$1" = "${CMAKE_C_COMPILER}" ]] ; then shift fi -export CCACHE_CPP2=true +${CCCACHE_EXPORTS} + exec "${C_LAUNCHER}" "${CMAKE_C_COMPILER}" "$@" \ No newline at end of file diff --git a/launch-cxx.in b/launch-cxx.in index 457660f..1c1381c 100644 --- a/launch-cxx.in +++ b/launch-cxx.in @@ -6,5 +6,6 @@ if [[ "$1" = "${CMAKE_CXX_COMPILER}" ]] ; then shift fi -export CCACHE_CPP2=true +${CCCACHE_EXPORTS} + exec "${CXX_LAUNCHER}" "${CMAKE_CXX_COMPILER}" "$@"