From dea4434250e7a07f1acb3ab1c73294d2a6e334d4 Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 23 Mar 2021 10:06:49 +0100
Subject: [PATCH] [CMake] Use ccache setup from TheLartians/Ccache.cmake

https://github.com/TheLartians/Ccache.cmake

No need to configure anything for ccache.
---
 CMakeLists.txt                                |  1 -
 scripts/cmake/CCacheSetup.cmake               | 61 -------------------
 scripts/cmake/Dependencies.cmake              | 14 +++++
 .../docs/devguide/advanced/using-ccache.md    | 14 -----
 4 files changed, 14 insertions(+), 76 deletions(-)
 delete mode 100644 scripts/cmake/CCacheSetup.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index f2326ba1e0a..7a44c4a7ab6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -80,7 +80,6 @@ include(ConanSetup)
 include(CompilerSetup)
 include(Find)
 include(Dependencies)
-include(CCacheSetup)
 include(CLCacheSetup)
 include(DocumentationSetup)
 include(test/Test)
diff --git a/scripts/cmake/CCacheSetup.cmake b/scripts/cmake/CCacheSetup.cmake
deleted file mode 100644
index 5dee6d01a87..00000000000
--- a/scripts/cmake/CCacheSetup.cmake
+++ /dev/null
@@ -1,61 +0,0 @@
-if(WIN32 OR NOT CCACHE_TOOL_PATH OR OGS_DISABLE_CCACHE)
-    return()
-endif()
-
-# Check ccache version
-set(CCACHE_VERSION_REQUIRED 3.2.0)
-execute_process(
-    COMMAND ${CCACHE_TOOL_PATH} --version OUTPUT_VARIABLE CCACHE_VERSION
-)
-set(CCACHE_VERSION_GREATER_EQUAL_3_2_5 ON)
-if("${CCACHE_VERSION}" MATCHES "ccache version ([0-9]\\.[0-9]\\.[0-9])")
-    if(${CMAKE_MATCH_1} VERSION_LESS ${CCACHE_VERSION_REQUIRED})
-        message(STATUS "CCache outdated. Installed: ${CMAKE_MATCH_1}, \
-            required: ${CCACHE_VERSION_REQUIRED}. Caching disabled."
-        )
-        return()
-    endif()
-    if(${CMAKE_MATCH_1} VERSION_LESS 3.2.5)
-        set(CCACHE_VERSION_GREATER_EQUAL_3_2_5 OFF)
-    endif()
-endif()
-
-# Set ccache as the compiler launcher
-set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
-set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
-
-if(COMPILER_IS_CLANG)
-    add_compile_options(-Qunused-arguments)
-endif()
-
-# Check ccache pre-compiled headers config
-execute_process(
-    COMMAND ${CCACHE_TOOL_PATH} -p
-    OUTPUT_VARIABLE CCACHE_CONFIG
-    ERROR_VARIABLE CCACHE_CONFIG
-    OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE
-)
-
-string(REGEX MATCH ".*time_macros.*" COTIRE_CCACHE_CONFIG_TIME_MACROS
-             ${CCACHE_CONFIG}
-)
-
-string(REGEX MATCH ".*pch_defines.*" COTIRE_CCACHE_CONFIG_PCH_DEFINES
-             ${CCACHE_CONFIG}
-)
-
-# Regex should be "sloppiness.*time_macros.*pch_defines.*" but due to bug fixed
-# in https://ccache.samba.org/releasenotes.html#_ccache_3_2_5 we have to leave
-# out pch_defines if ccache version is older than 3.2.5. Ubuntu 16.04 comes with
-# ccache 3.2.4 ...
-if(NOT COTIRE_CCACHE_CONFIG_TIME_MACROS
-   OR (CCACHE_VERSION_GREATER_EQUAL_3_2_5 AND NOT
-                                              COTIRE_CCACHE_CONFIG_PCH_DEFINES)
-)
-    message(
-        FATAL_ERROR
-            "CCache configuration does not set sloppiness to pch_defines,time_macros. \
-    Current options are: '${CCACHE_CONFIG}'. \
-    See https://docs.opengeosys.org/docs/devguide/advanced/using-ccache"
-    )
-endif()
diff --git a/scripts/cmake/Dependencies.cmake b/scripts/cmake/Dependencies.cmake
index b2ed384942a..7b89d7765a2 100644
--- a/scripts/cmake/Dependencies.cmake
+++ b/scripts/cmake/Dependencies.cmake
@@ -421,3 +421,17 @@ cpm_licenses_create_disclaimer_target(
     write-licenses "${PROJECT_BINARY_DIR}/third_party_licenses.txt"
     "${CPM_PACKAGES}"
 )
+
+# ccache
+if(NOT WIN32 AND CCACHE_TOOL_PATH AND NOT OGS_DISABLE_CCACHE)
+    set(CCACHE_OPTIONS "CCACHE_SLOPPINESS=pch_defines,time_macros")
+    if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang|AppleClang")
+        list(APPEND CCACHE_OPTIONS "CCACHE_CPP2=true")
+    endif()
+    CPMAddPackage(
+        NAME Ccache.cmake
+        GITHUB_REPOSITORY TheLartians/Ccache.cmake
+        VERSION 1.2.2
+        OPTIONS "USE_CCACHE ON"
+    )
+endif()
diff --git a/web/content/docs/devguide/advanced/using-ccache.md b/web/content/docs/devguide/advanced/using-ccache.md
index 872d3b94aa3..79a17b8ea73 100644
--- a/web/content/docs/devguide/advanced/using-ccache.md
+++ b/web/content/docs/devguide/advanced/using-ccache.md
@@ -20,26 +20,12 @@ Tested on GCC and Clang.
 
 [ccache](https://ccache.samba.org) speeds up compilation times by caching object files and reusing them on subsequent builds. This even works for complete rebuilds (i.e. deleting the full build-directory). If ccache is found on the system it is automatically used.
 
-## Configuration
-
-Add the following line to your [ccache config file](https://ccache.samba.org/manual.html#_configuration) which is required for pre-compiled headers:
-
-```bash
-sloppiness = pch_defines,time_macros
-```
-
-See the [ccache docs](https://ccache.samba.org/manual.html#_configuration_settings) for other available options.
-
 To disable caching with ccache:
 
 ```bash
 cmake . -DOGS_DISABLE_CCACHE=ON
 ```
 
-### ccache and Clang
-
-Set the option `run_second_cpp = true` or `export CCACHE_CPP2=true` to suppress lots of [false positive compiler warnings](http://peter.eisentraut.org/blog/2014/12/01/ccache-and-clang-part-3/).
-
 ## Usage on eve
 
 Just load the module:
-- 
GitLab