From 67e2130c963bb3d9d437f00f5d6b8e95d2f5fb18 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 20 Nov 2020 09:45:14 -0500 Subject: Makefiles: Fix CMAKE_EXPORT_COMPILE_COMMANDS crash with custom compile rule Fixes: #21471 --- Source/cmMakefileTargetGenerator.cxx | 16 ++++++++++------ .../ExportCompileCommands/CustomCompileRule.cmake | 5 +++++ Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 5f97d86..6e6c8df 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -838,13 +838,17 @@ void cmMakefileTargetGenerator::WriteObjectRuleFiles( compileCommand.replace(compileCommand.find(langFlags), langFlags.size(), this->GetFlags(lang, this->GetConfigName())); std::string langDefines = std::string("$(") + lang + "_DEFINES)"; - compileCommand.replace(compileCommand.find(langDefines), - langDefines.size(), - this->GetDefines(lang, this->GetConfigName())); + std::string::size_type ldPos = compileCommand.find(langDefines); + if (ldPos != std::string::npos) { + compileCommand.replace(ldPos, langDefines.size(), + this->GetDefines(lang, this->GetConfigName())); + } std::string langIncludes = std::string("$(") + lang + "_INCLUDES)"; - compileCommand.replace(compileCommand.find(langIncludes), - langIncludes.size(), - this->GetIncludes(lang, this->GetConfigName())); + std::string::size_type liPos = compileCommand.find(langIncludes); + if (liPos != std::string::npos) { + compileCommand.replace(liPos, langIncludes.size(), + this->GetIncludes(lang, this->GetConfigName())); + } cmProp eliminate[] = { this->Makefile->GetDefinition("CMAKE_START_TEMP_FILE"), diff --git a/Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake b/Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake new file mode 100644 index 0000000..12368a2 --- /dev/null +++ b/Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake @@ -0,0 +1,5 @@ +enable_language(C) +add_library(empty STATIC empty.c) +string(REPLACE "" "" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}") +string(REPLACE "" "" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}") +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) diff --git a/Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake b/Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake index b540a04..9e7e732 100644 --- a/Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake +++ b/Tests/RunCMake/ExportCompileCommands/RunCMakeTest.cmake @@ -1,3 +1,4 @@ include(RunCMake) run_cmake_with_options(BeforeProject -DCMAKE_PROJECT_INCLUDE_BEFORE=BeforeProjectBEFORE.cmake) +run_cmake(CustomCompileRule) -- cgit v0.12