diff options
| author | Mike Taves <mwtoews@gmail.com> | 2021-11-19 23:55:09 +1300 |
|---|---|---|
| committer | Mike Taves <mwtoews@gmail.com> | 2021-11-20 00:56:48 +1300 |
| commit | dd74dd05a055cb882388862f8a3fe6e3ba4b194b (patch) | |
| tree | d37a8707018c670852b53a99c462408625be37a6 /cmake | |
| parent | ac882266b57d04720bb645b8144901127f7427cf (diff) | |
| download | PROJ-dd74dd05a055cb882388862f8a3fe6e3ba4b194b.tar.gz PROJ-dd74dd05a055cb882388862f8a3fe6e3ba4b194b.zip | |
CMake: add option USE_CCACHE=OFF to use ccache to compile C/C++ objs
Diffstat (limited to 'cmake')
| -rw-r--r-- | cmake/Ccache.cmake | 66 | ||||
| -rw-r--r-- | cmake/Makefile.am | 1 |
2 files changed, 67 insertions, 0 deletions
diff --git a/cmake/Ccache.cmake b/cmake/Ccache.cmake new file mode 100644 index 00000000..09e81756 --- /dev/null +++ b/cmake/Ccache.cmake @@ -0,0 +1,66 @@ +# +# CMake module to support ccache +# +# see https://crascit.com/2016/04/09/using-ccache-with-cmake/ +# + +find_program(CCACHE_PROGRAM ccache) +# Note: REQUIRED keyword introduced in CMake 3.18 + +if(CCACHE_PROGRAM) + message(STATUS "Configuring ccache with ${CCACHE_PROGRAM}") + + set(C_LAUNCHER "${CCACHE_PROGRAM}") + set(CXX_LAUNCHER "${CCACHE_PROGRAM}") + + # Set up wrapper scripts + set(CCACHE_LAUNCH_C ${CMAKE_BINARY_DIR}/ccache-launch-c) + file(WRITE ${CCACHE_LAUNCH_C} "\ +#!/bin/sh + +# Xcode generator doesn't include the compiler as the +# first argument, Ninja and Makefiles do. Handle both cases. +if [ \"$1\" = \"${CMAKE_C_COMPILER}\" ] ; then + shift +fi + +export CCACHE_CPP2=true +exec \"${C_LAUNCHER}\" \"${CMAKE_C_COMPILER}\" \"$@\" +") + + set(CCACHE_LAUNCH_CXX ${CMAKE_BINARY_DIR}/ccache-launch-cxx) + file(WRITE ${CCACHE_LAUNCH_CXX} "\ +#!/bin/sh + +# Xcode generator doesn't include the compiler as the +# first argument, Ninja and Makefiles do. Handle both cases. +if [ \"$1\" = \"${CMAKE_CXX_COMPILER}\" ] ; then + shift +fi + +export CCACHE_CPP2=true +exec \"${CXX_LAUNCHER}\" \"${CMAKE_CXX_COMPILER}\" \"$@\" +") + + # Note: file(CHMOD) introduced in CMake 3.19 + execute_process( + COMMAND chmod a+rx + "${CCACHE_LAUNCH_C}" + "${CCACHE_LAUNCH_CXX}" + ) + + if(CMAKE_GENERATOR STREQUAL "Xcode") + # Set Xcode project attributes to route compilation and linking + # through our scripts + set(CMAKE_XCODE_ATTRIBUTE_CC "${CCACHE_LAUNCH_C}") + set(CMAKE_XCODE_ATTRIBUTE_CXX "${CCACHE_LAUNCH_CXX}") + set(CMAKE_XCODE_ATTRIBUTE_LD "${CCACHE_LAUNCH_C}") + set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CCACHE_LAUNCH_CXX}") + else() + # Support Unix Makefiles and Ninja + set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_LAUNCH_C}") + set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_LAUNCH_CXX}") + endif() +else() + message(SEND_ERROR "Ccache requested, but ccache was not found") +endif() diff --git a/cmake/Makefile.am b/cmake/Makefile.am index cdad1ff8..de982198 100644 --- a/cmake/Makefile.am +++ b/cmake/Makefile.am @@ -1,4 +1,5 @@ EXTRA_DIST = CMakeLists.txt \ + Ccache.cmake \ ProjInstallPath.cmake \ ProjUtilities.cmake \ proj_config.cmake.in \ |
