Skip to content

Commit

Permalink
Merge pull request #1 from TheLartians/add-options
Browse files Browse the repository at this point in the history
add Ccache options
  • Loading branch information
TheLartians authored Oct 16, 2019
2 parents db2a359 + 39c40e2 commit d22575d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
68 changes: 38 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion launch-c.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ if [[ "$1" = "${CMAKE_C_COMPILER}" ]] ; then
shift
fi

export CCACHE_CPP2=true
${CCCACHE_EXPORTS}

exec "${C_LAUNCHER}" "${CMAKE_C_COMPILER}" "$@"
3 changes: 2 additions & 1 deletion launch-cxx.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ if [[ "$1" = "${CMAKE_CXX_COMPILER}" ]] ; then
shift
fi

export CCACHE_CPP2=true
${CCCACHE_EXPORTS}

exec "${CXX_LAUNCHER}" "${CMAKE_CXX_COMPILER}" "$@"

0 comments on commit d22575d

Please sign in to comment.