Browse Source

cmGeneratorTarget: shorten PCH filenames

John Parent 5 months ago
parent
commit
526a6f2a81

+ 16 - 8
Source/cmGeneratorTarget.cxx

@@ -2994,6 +2994,7 @@ std::string cmGeneratorTarget::GetPchHeader(std::string const& config,
       filename = cmStrCat(filename, '/', config);
     }
 
+    // This is acceptable as its the source file, won't have a rename/hash
     filename =
       cmStrCat(filename, "/cmake_pch", arch.empty() ? "" : cmStrCat('_', arch),
                languageToExtension.at(language));
@@ -3135,12 +3136,9 @@ std::string cmGeneratorTarget::GetPchFileObject(std::string const& config,
 
     auto* pchSf = this->Makefile->GetOrCreateSource(
       pchSource, false, cmSourceFileLocationKind::Known);
-
-    filename = cmStrCat(this->ObjectDirectory, this->GetObjectName(pchSf));
-    if (this->GetGlobalGenerator()->IsMultiConfig()) {
-      cmSystemTools::ReplaceString(
-        filename, this->GetGlobalGenerator()->GetCMakeCFGIntDir(), config);
-    }
+    pchSf->ResolveFullPath();
+    filename = cmStrCat(this->GetObjectDirectory(config), '/',
+                        this->GetObjectName(pchSf));
   }
   return inserted.first->second;
 }
@@ -3181,8 +3179,18 @@ std::string cmGeneratorTarget::GetPchFile(std::string const& config,
         pchFile = replaceExtension(pchFileObject, pchExtension);
       }
     } else {
-      pchFile = this->GetPchHeader(config, language, arch);
-      pchFile += pchExtension;
+      if (this->GetUseShortObjectNames()) {
+        auto pchSource = this->GetPchSource(config, language, arch);
+        auto* pchSf = this->Makefile->GetOrCreateSource(
+          pchSource, false, cmSourceFileLocationKind::Known);
+        pchSf->ResolveFullPath();
+        pchFile = cmStrCat(
+          this->GetSupportDirectory(), '/',
+          this->LocalGenerator->GetShortObjectFileName(*pchSf), pchExtension);
+      } else {
+        pchFile =
+          cmStrCat(this->GetPchHeader(config, language, arch), pchExtension);
+      }
     }
   }
   return inserted.first->second;

+ 1 - 1
Source/cmLocalGenerator.cxx

@@ -4241,6 +4241,7 @@ std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
       objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
     }
   }
+  bool const isPchObject = objectName.find("cmake_pch") != std::string::npos;
 
   // Short object path policy selected, use as little info as necessary to
   // select an object name
@@ -4255,7 +4256,6 @@ std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
   // CMakeFiles/<target>.dir/CMakeFiles/<target>.dir/generated_source_file.obj
   cmValue unitySourceFile = source.GetProperty("UNITY_SOURCE_FILE");
   cmValue pchExtension = source.GetProperty("PCH_EXTENSION");
-  bool const isPchObject = objectName.find("cmake_pch") != std::string::npos;
   if (unitySourceFile || pchExtension || isPchObject) {
     if (pchExtension) {
       customOutputExtension = pchExtension->c_str();

+ 9 - 0
Tests/RunCMake/PrecompileHeaders/PchInterfaceShort-check.cmake

@@ -0,0 +1,9 @@
+set(foobar_pch_header "${RunCMake_TEST_BINARY_DIR}/.o/ff32d702/cmake_pch.h")
+if (RunCMake_GENERATOR_IS_MULTI_CONFIG)
+  set(foobar_pch_header "${RunCMake_TEST_BINARY_DIR}/.o/ff32d702/Debug/cmake_pch.h")
+endif()
+
+if (NOT EXISTS ${foobar_pch_header})
+  set(RunCMake_TEST_FAILED "Generated foobar pch header ${foobar_pch_header} does not exist")
+  return()
+endif()

+ 22 - 0
Tests/RunCMake/PrecompileHeaders/PchInterfaceShort.cmake

@@ -0,0 +1,22 @@
+set(CMAKE_INTERMEDIATE_DIR_STRATEGY SHORT CACHE STRING "" FORCE)
+
+enable_language(C)
+
+add_library(foo foo.c)
+target_include_directories(foo PUBLIC include)
+target_precompile_headers(foo PUBLIC
+  include/foo.h
+  \"foo2.h\"
+  <stdio.h>
+  \"string.h\"
+)
+if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
+  set_property(SOURCE foo.c APPEND PROPERTY COMPILE_OPTIONS "-WX-")
+endif()
+
+add_library(bar INTERFACE)
+target_include_directories(bar INTERFACE include)
+target_precompile_headers(bar INTERFACE include/bar.h)
+
+add_executable(foobar foobar.c)
+target_link_libraries(foobar foo bar)

+ 3 - 0
Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake

@@ -18,6 +18,9 @@ endfunction()
 run_cmake(DisabledPch)
 run_cmake(PchDebugGenex)
 run_test(PchInterface)
+if (CMAKE_GENERATOR MATCHES "NONE")
+  run_test(PchInterfaceShort)
+endif ()
 run_test(PchInterfaceUnity)
 run_cmake(PchPrologueEpilogue)
 run_test(SkipPrecompileHeaders)