[flang] [llvm] [CMake] Fix using precompiled headers with ccache (PR #131397)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 17 10:03:19 PDT 2025


================
@@ -291,7 +291,7 @@ if(LLVM_CCACHE_BUILD)
       set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM})
     else()
       if(LLVM_CCACHE_MAXSIZE OR LLVM_CCACHE_DIR OR
-         NOT LLVM_CCACHE_PARAMS MATCHES "CCACHE_CPP2=yes CCACHE_HASHDIR=yes")
+         NOT LLVM_CCACHE_PARAMS MATCHES "CCACHE_CPP2=yes CCACHE_HASHDIR=yes CCACHE_SLOPPINESS=pch_defines,time_macros")
----------------
Meinersbur wrote:

```diff
diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index c5d3e23a47f0..2ecd3092f9d9 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -280,25 +280,16 @@ if(LLVM_CCACHE_BUILD)
     set(LLVM_CCACHE_PARAMS "CCACHE_CPP2=yes CCACHE_HASHDIR=yes"
         CACHE STRING "Parameters to pass through to ccache")

-    if(NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
-      set(CCACHE_PROGRAM "${LLVM_CCACHE_PARAMS} ${CCACHE_PROGRAM}")
-      if (LLVM_CCACHE_MAXSIZE)
-        set(CCACHE_PROGRAM "CCACHE_MAXSIZE=${LLVM_CCACHE_MAXSIZE} ${CCACHE_PROGRAM}")
-      endif()
-      if (LLVM_CCACHE_DIR)
-        set(CCACHE_PROGRAM "CCACHE_DIR=${LLVM_CCACHE_DIR} ${CCACHE_PROGRAM}")
-      endif()
-      set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM})
-    else()
-      if(LLVM_CCACHE_MAXSIZE OR LLVM_CCACHE_DIR OR
-         NOT LLVM_CCACHE_PARAMS MATCHES "CCACHE_CPP2=yes CCACHE_HASHDIR=yes")
-        message(FATAL_ERROR "Ccache configuration through CMake is not supported on Windows. Please use environment variables.")
-      endif()
-      # RULE_LAUNCH_COMPILE should work with Ninja but currently has issues
-      # with cmd.exe and some MSVC tools other than cl.exe
-      set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
-      set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE_PROGRAM})
-    endif()
+    set(launcher_env "${LLVM_CCACHE_PARAMS}")
+    if (CCACHE_MAXSIZE)
+      set(launcher_env "CCACHE_MAXSIZE=${LLVM_CCACHE_MAXSIZE} ${launcher_env}")
+    endif ()
+    if (CCACHE_DIR)
+      set(launcher_env "CCACHE_DIR=${LLVM_CCACHE_DIR} ${launcher_env}")
+    endif ()
+    set(launcher "${CMAKE_COMMAND} -E env ${launcher_env} -- ${CCACHE_PROGRAM}")
+    set(CMAKE_C_COMPILER_LAUNCHER "${launcher}")
+    set(CMAKE_CXX_COMPILER_LAUNCHER "${launcher}")
   else()
     message(FATAL_ERROR "Unable to find the program ccache. Set LLVM_CCACHE_BUILD to OFF")
   endif()
```
NOT tested

Previous code used some antipatterns:
1. [RULE_LAUNCH_COMPILE](https://cmake.org/cmake/help/latest/prop_gbl/RULE_LAUNCH_COMPILE.html) is a CMake-internal implementation detail.
2. `CCACHE_PROGRAM` a configuration variable and should not be modified within the script. Leads to the confusion situation that its value in CMakeCache.txt is different.

https://github.com/llvm/llvm-project/pull/131397


More information about the llvm-commits mailing list