Browse Source

Merge topic 'fix-compile-db-crash'

316cbbe8d5 Makefiles: Fix CMAKE_EXPORT_COMPILE_COMMANDS crash with custom compile rule

Acked-by: Kitware Robot <[email protected]>
Merge-request: !6173
Brad King 4 years ago
parent
commit
37cda77d16

+ 5 - 2
Source/cmMakefileTargetGenerator.cxx

@@ -909,8 +909,11 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles(
                                                    compileCommand, vars);
       std::string workingDirectory =
         this->LocalGenerator->GetCurrentBinaryDirectory();
-      compileCommand.replace(compileCommand.find(langFlags), langFlags.size(),
-                             this->GetFlags(lang, this->GetConfigName()));
+      std::string::size_type lfPos = compileCommand.find(langFlags);
+      if (lfPos != std::string::npos) {
+        compileCommand.replace(lfPos, langFlags.size(),
+                               this->GetFlags(lang, this->GetConfigName()));
+      }
       std::string langDefines = std::string("$(") + lang + "_DEFINES)";
       std::string::size_type ldPos = compileCommand.find(langDefines);
       if (ldPos != std::string::npos) {

+ 2 - 1
Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake

@@ -1,5 +1,6 @@
 enable_language(C)
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
 add_library(empty STATIC empty.c)
 string(REPLACE "<DEFINES>" "" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}")
 string(REPLACE "<INCLUDES>" "" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}")
-set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+string(REPLACE "<FLAGS>" "" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}")