From 316cbbe8d570775eb08683a72808ee07bddcd3e0 Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 28 May 2021 15:28:25 -0400 Subject: Makefiles: Fix CMAKE_EXPORT_COMPILE_COMMANDS crash with custom compile rule Extend the fix from commit 67e2130c96 (Makefiles: Fix CMAKE_EXPORT_COMPILE_COMMANDS crash with custom compile rule, 2020-11-20, v3.19.1~8^2) to work for the `` placeholder too. Also fix the test case to actually enable `EXPORT_COMPILE_COMMANDS` on the test target. Fixes: #22261 --- Source/cmMakefileTargetGenerator.cxx | 7 +++++-- Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Source/cmMakefileTargetGenerator.cxx b/Source/cmMakefileTargetGenerator.cxx index 4918bf6..5f7f3dc 100644 --- a/Source/cmMakefileTargetGenerator.cxx +++ b/Source/cmMakefileTargetGenerator.cxx @@ -926,8 +926,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) { diff --git a/Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake b/Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake index 12368a2..b1b120f 100644 --- a/Tests/RunCMake/ExportCompileCommands/CustomCompileRule.cmake +++ b/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 "" "" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}") string(REPLACE "" "" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}") -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +string(REPLACE "" "" CMAKE_C_COMPILE_OBJECT "${CMAKE_C_COMPILE_OBJECT}") -- cgit v0.12