[llvm] e8dc8ad - [CMake] Fix using precompiled headers with ccache (#131397)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 04:09:56 PDT 2025


Author: Kajetan Puchalski
Date: 2025-04-08T12:09:52+01:00
New Revision: e8dc8add3c04517e673d9dff342a60001c85dc1a

URL: https://github.com/llvm/llvm-project/commit/e8dc8add3c04517e673d9dff342a60001c85dc1a
DIFF: https://github.com/llvm/llvm-project/commit/e8dc8add3c04517e673d9dff342a60001c85dc1a.diff

LOG: [CMake] Fix using precompiled headers with ccache (#131397)

Using precompiled headers with ccache requires special accommodations.
Add the required ccache options, clang and gcc compiler flags to CMake.
Refactor ccache configuration to pass options directly on the command line for versions of ccache that support it.

---------

Signed-off-by: Kajetan Puchalski <kajetan.puchalski at arm.com>

Added: 
    

Modified: 
    flang/CMakeLists.txt
    llvm/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index 76eb13295eb07..a2f59214aaf8d 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -452,6 +452,10 @@ if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -fno-semantic-interposition")
   endif()
 
+  # GCC requires this flag in order for precompiled headers to work with ccache
+  if (CMAKE_CXX_COMPILER_ID MATCHES GCC AND NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fpch-preprocess")
+  endif()
 endif()
 
 # Clang on Darwin enables non-POSIX extensions by default, which allows the
@@ -462,6 +466,11 @@ if (APPLE)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_POSIX_C_SOURCE=200809")
 endif()
 
+# Clang requires this flag in order for precompiled headers to work with ccache
+if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND NOT CMAKE_DISABLE_PRECOMPILE_HEADERS)
+  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -fno-pch-timestamp")
+endif()
+
 list(REMOVE_DUPLICATES CMAKE_CXX_FLAGS)
 
 # Determine HOST_LINK_VERSION on Darwin.

diff  --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt
index cfd1a086c0fc2..2efb96bcd4470 100644
--- a/llvm/CMakeLists.txt
+++ b/llvm/CMakeLists.txt
@@ -271,30 +271,43 @@ set(LLVM_CCACHE_BUILD OFF CACHE BOOL "Set to ON for a ccache enabled build")
 if(LLVM_CCACHE_BUILD)
   find_program(CCACHE_PROGRAM ccache)
   if(CCACHE_PROGRAM)
+    # ccache --version example output: "ccache version 4.9.1\n(..)"
+    execute_process(COMMAND ${CCACHE_PROGRAM} --version OUTPUT_VARIABLE CCACHE_VERSION_STR)
+    string(REGEX MATCH "[0-9]+\.[0-9]+\.?[0-9]*" CCACHE_VERSION "${CCACHE_VERSION_STR}")
+
     set(LLVM_CCACHE_MAXSIZE "" CACHE STRING "Size of ccache")
     set(LLVM_CCACHE_DIR "" CACHE STRING "Directory to keep ccached data")
-    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}")
+    # ccache only supports passing options on the command line from version 4.8.0
+    # use a workaround with ad-hoc environment variables for older versions
+    if (CCACHE_VERSION VERSION_LESS "4.8.0")
+      set(LLVM_CCACHE_PARAMS "CCACHE_CPP2=yes;CCACHE_HASHDIR=yes;CCACHE_SLOPPINESS=pch_defines,time_macros"
+          CACHE STRING "Parameters to pass through to ccache")
+
+      set(launcher_params ${LLVM_CCACHE_PARAMS})
+      if (CCACHE_MAXSIZE)
+        set(launcher_params "CCACHE_MAXSIZE=${CCACHE_MAXSIZE};${launcher_params}")
       endif()
-      if (LLVM_CCACHE_DIR)
-        set(CCACHE_PROGRAM "CCACHE_DIR=${LLVM_CCACHE_DIR} ${CCACHE_PROGRAM}")
+      if (CCACHE_DIR)
+        set(launcher_params "CCACHE_DIR=${CCACHE_DIR};${launcher_params}")
       endif()
-      set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ${CCACHE_PROGRAM})
+      set(launcher "${launcher_params};${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.")
+      set(LLVM_CCACHE_PARAMS "run_second_cpp=true;hash_dir=true;sloppiness=pch_defines,time_macros"
+          CACHE STRING "Parameters to pass through to ccache")
+
+      set(launcher_params ${LLVM_CCACHE_PARAMS})
+      if (CCACHE_MAXSIZE)
+        set(launcher_params "max_size=${CCACHE_MAXSIZE};${launcher_params}")
       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})
+      if (CCACHE_DIR)
+        set(launcher_params "cache_dir=${CCACHE_DIR};${launcher_params}")
+      endif()
+      set(launcher "${CCACHE_PROGRAM};${launcher_params}")
     endif()
+
+    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()


        


More information about the llvm-commits mailing list