diff --git a/Source/cmAddCustomCommandCommand.cxx b/Source/cmAddCustomCommandCommand.cxx index 46067d1dc6..85a807b049 100644 --- a/Source/cmAddCustomCommandCommand.cxx +++ b/Source/cmAddCustomCommandCommand.cxx @@ -9,6 +9,7 @@ #include #include +#include #include #include "cmCustomCommand.h" @@ -44,8 +45,7 @@ bool cmAddCustomCommandCommand(std::vector const& args, std::string depfile; std::string job_pool; std::string job_server_aware; - std::string comment_buffer; - char const* comment = nullptr; + cm::optional comment; std::vector depends; std::vector outputs; std::vector output; @@ -369,7 +369,7 @@ bool cmAddCustomCommandCommand(std::vector const& args, byproducts.push_back(filename); break; case doing_comment: - if (!comment_buffer.empty()) { + if (comment && !comment->empty()) { std::string const msg = "COMMENT requires exactly one argument, but multiple values " "or COMMENT keywords have been given."; @@ -381,8 +381,7 @@ bool cmAddCustomCommandCommand(std::vector const& args, mf.IssuePolicyWarning(cmPolicies::CMP0175, msg); } } - comment_buffer = copy; - comment = comment_buffer.c_str(); + comment = copy; break; default: status.SetError("Wrong syntax. Unknown type of argument."); @@ -477,7 +476,7 @@ bool cmAddCustomCommandCommand(std::vector const& args, cc->SetByproducts(byproducts); cc->SetCommandLines(commandLines); cc->SetComment(comment); - cc->SetWorkingDirectory(working.c_str()); + cc->SetWorkingDirectory(working); cc->SetEscapeOldStyle(!verbatim); cc->SetUsesTerminal(uses_terminal); cc->SetDepfile(depfile); diff --git a/Source/cmAddCustomTargetCommand.cxx b/Source/cmAddCustomTargetCommand.cxx index 92bc25e077..b2da4e0739 100644 --- a/Source/cmAddCustomTargetCommand.cxx +++ b/Source/cmAddCustomTargetCommand.cxx @@ -5,6 +5,7 @@ #include #include +#include #include "cmCustomCommand.h" #include "cmCustomCommandLines.h" @@ -50,8 +51,7 @@ bool cmAddCustomTargetCommand(std::vector const& args, bool verbatim = false; bool uses_terminal = false; bool command_expand_lists = false; - std::string comment_buffer; - char const* comment = nullptr; + cm::optional comment; std::vector sources; std::string job_pool; std::string job_server_aware; @@ -143,8 +143,7 @@ bool cmAddCustomTargetCommand(std::vector const& args, depends.push_back(std::move(dep)); } break; case doing_comment: - comment_buffer = copy; - comment = comment_buffer.c_str(); + comment = copy; break; case doing_source: sources.push_back(copy); @@ -209,7 +208,7 @@ bool cmAddCustomTargetCommand(std::vector const& args, // Add the utility target to the makefile. auto cc = cm::make_unique(); - cc->SetWorkingDirectory(working_directory.c_str()); + cc->SetWorkingDirectory(working_directory); cc->SetByproducts(byproducts); cc->SetDepends(depends); cc->SetCommandLines(commandLines); diff --git a/Source/cmCustomCommand.cxx b/Source/cmCustomCommand.cxx index 5ec451efbd..b7b40a9bf7 100644 --- a/Source/cmCustomCommand.cxx +++ b/Source/cmCustomCommand.cxx @@ -77,16 +77,14 @@ void cmCustomCommand::SetCommandLines(cmCustomCommandLines commandLines) this->CommandLines = std::move(commandLines); } -char const* cmCustomCommand::GetComment() const +cm::optional const& cmCustomCommand::GetComment() const { - char const* no_comment = nullptr; - return this->HaveComment ? this->Comment.c_str() : no_comment; + return this->Comment; } -void cmCustomCommand::SetComment(char const* comment) +void cmCustomCommand::SetComment(cm::optional comment) { - this->Comment = comment ? comment : ""; - this->HaveComment = (comment != nullptr); + this->Comment = std::move(comment); } void cmCustomCommand::AppendCommands(cmCustomCommandLines const& commandLines) diff --git a/Source/cmCustomCommand.h b/Source/cmCustomCommand.h index 927747e971..0a4a767bee 100644 --- a/Source/cmCustomCommand.h +++ b/Source/cmCustomCommand.h @@ -8,6 +8,8 @@ #include #include +#include + #include "cmCustomCommandLines.h" #include "cmListFileCache.h" #include "cmPolicies.h" @@ -50,9 +52,9 @@ public: return this->WorkingDirectory; } - void SetWorkingDirectory(char const* workingDirectory) + void SetWorkingDirectory(std::string const& workingDirectory) { - this->WorkingDirectory = (workingDirectory ? workingDirectory : ""); + this->WorkingDirectory = workingDirectory; } /** Get the list of command lines. */ @@ -60,8 +62,8 @@ public: void SetCommandLines(cmCustomCommandLines commandLines); /** Get the comment string for the command. */ - char const* GetComment() const; - void SetComment(char const* comment); + cm::optional const& GetComment() const; + void SetComment(cm::optional comment); /** Get a value indicating if the command uses UTF-8 output pipes. */ bool GetStdPipesUTF8() const { return this->StdPipesUTF8; } @@ -148,13 +150,12 @@ private: cmListFileBacktrace Backtrace; cmImplicitDependsList ImplicitDepends; std::string Target; - std::string Comment; + cm::optional Comment; std::string WorkingDirectory; std::string Depfile; std::string JobPool; std::string Role; bool JobserverAware = false; - bool HaveComment = false; bool EscapeAllowMakeVars = false; bool EscapeOldStyle = true; bool UsesTerminal = false; diff --git a/Source/cmCustomCommandGenerator.cxx b/Source/cmCustomCommandGenerator.cxx index 6bb4b72388..9805ad6c88 100644 --- a/Source/cmCustomCommandGenerator.cxx +++ b/Source/cmCustomCommandGenerator.cxx @@ -152,7 +152,7 @@ std::string EvaluateDepfile(std::string const& path, return cge->Evaluate(lg, config); } -std::string EvaluateComment(char const* comment, +std::string EvaluateComment(std::string const& comment, cmGeneratorExpression const& ge, cmLocalGenerator* lg, std::string const& config) { @@ -484,12 +484,12 @@ std::string cmCustomCommandGenerator::GetInternalDepfile() const cm::optional cmCustomCommandGenerator::GetComment() const { - char const* comment = this->CC->GetComment(); - if (!comment) { + if (!this->CC->GetComment()) { return cm::nullopt; } - if (!*comment) { - return std::string(); + std::string const& comment = this->CC->GetComment().value(); + if (comment.empty()) { + return comment; } cmGeneratorExpression ge(*this->LG->GetCMakeInstance(), diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 31ac2d715f..e83d10c093 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -3614,7 +3614,7 @@ void ModuleCompilationDatabaseCommandAction::operator()( cc->SetBacktrace(lfbt); cc->SetCommandLines(command_lines); - cc->SetWorkingDirectory(lg.GetBinaryDirectory().c_str()); + cc->SetWorkingDirectory(lg.GetBinaryDirectory()); cc->SetDependsExplicitOnly(true); cc->SetOutputs(this->Output); if (!inputs.empty()) { @@ -3646,7 +3646,7 @@ void ModuleCompilationDatabaseTargetAction::operator()( std::unique_ptr cc) { cc->SetBacktrace(lfbt); - cc->SetWorkingDirectory(lg.GetBinaryDirectory().c_str()); + cc->SetWorkingDirectory(lg.GetBinaryDirectory()); std::vector target_inputs; target_inputs.emplace_back(this->Output); cc->SetDepends(target_inputs); @@ -3698,7 +3698,7 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets() static cm::static_string_view TargetPrefix = "cmake_build_database"_s; auto AddMergeTarget = - [&mf](std::string const& name, char const* comment, + [&mf](std::string const& name, std::string const& comment, std::string const& output, std::function()> inputs) { // Add the custom command. @@ -3732,13 +3732,13 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets() lang, ".json"); mf->GetOrCreateGeneratedSource(output); AddMergeTarget( - cmStrCat(TargetPrefix, '-', lang), comment.c_str(), output, + cmStrCat(TargetPrefix, '-', lang), comment, output, [this, lang]() { return this->PerLanguageModuleDbs[lang]; }); all_lang_paths.emplace_back(std::move(output)); } // Add the overall target. - auto const* comment = "Combining module command databases"; + std::string comment{ "Combining module command databases" }; auto output = cmStrCat(mf->GetHomeOutputDirectory(), "/build_database.json"); mf->GetOrCreateGeneratedSource(output); @@ -3758,8 +3758,8 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets() auto output = cmStrCat(mf->GetHomeOutputDirectory(), "/build_database_", lang, '_', config, ".json"); mf->GetOrCreateGeneratedSource(output); - AddMergeTarget(cmStrCat(TargetPrefix, '-', lang, '-', config), - comment.c_str(), output, [this, config, lang]() { + AddMergeTarget(cmStrCat(TargetPrefix, '-', lang, '-', config), comment, + output, [this, config, lang]() { return this->PerConfigModuleDbs[config][lang]; }); all_config_paths.emplace_back(std::move(output)); @@ -3770,8 +3770,8 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets() auto output = cmStrCat(mf->GetHomeOutputDirectory(), "/build_database_", config, ".json"); mf->GetOrCreateGeneratedSource(output); - AddMergeTarget(cmStrCat(TargetPrefix, '-', config), comment.c_str(), - output, [all_config_paths]() { return all_config_paths; }); + AddMergeTarget(cmStrCat(TargetPrefix, '-', config), comment, output, + [all_config_paths]() { return all_config_paths; }); } // NMC considerations @@ -3783,13 +3783,13 @@ bool cmGlobalGenerator::AddBuildDatabaseTargets() lang, ".json"); mf->GetOrCreateGeneratedSource(output); AddMergeTarget( - cmStrCat(TargetPrefix, '-', lang), comment.c_str(), output, + cmStrCat(TargetPrefix, '-', lang), comment, output, [this, lang]() { return this->PerLanguageModuleDbs[lang]; }); all_config_paths.emplace_back(std::move(output)); } // Add the overall target. - auto const* comment = "Combining all module command databases"; + std::string comment{ "Combining all module command databases" }; auto output = cmStrCat(mf->GetHomeOutputDirectory(), "/build_database.json"); mf->GetOrCreateGeneratedSource(output); AddMergeTarget(std::string(TargetPrefix), comment, output, @@ -3844,7 +3844,7 @@ void cmGlobalGenerator::CreateGlobalTarget(GlobalTargetInfo const& gti, // Store the custom command in the target. cmCustomCommand cc; cc.SetCommandLines(gti.CommandLines); - cc.SetWorkingDirectory(gti.WorkingDir.c_str()); + cc.SetWorkingDirectory(gti.WorkingDir); cc.SetStdPipesUTF8(gti.StdPipesUTF8); cc.SetUsesTerminal(gti.UsesTerminal); cc.SetRole(gti.Role); diff --git a/Source/cmGlobalGhsMultiGenerator.cxx b/Source/cmGlobalGhsMultiGenerator.cxx index fe3f041337..d93d345445 100644 --- a/Source/cmGlobalGhsMultiGenerator.cxx +++ b/Source/cmGlobalGhsMultiGenerator.cxx @@ -9,6 +9,7 @@ #include #include +#include #include #include #include diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index 78d9cb1045..696a3e3ccb 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -829,7 +829,7 @@ void cmGlobalXCodeGenerator::AddExtraTargets( cc = cm::make_unique(); cc->SetCommandLines(legacyDependHelperCommandLines); cc->SetComment("Depend check for xcode"); - cc->SetWorkingDirectory(legacyDependHelperDir.c_str()); + cc->SetWorkingDirectory(legacyDependHelperDir); gen->AddCustomCommandToTarget( target->GetName(), cmCustomCommandType::POST_BUILD, std::move(cc), cmObjectLibraryCommands::Accept); diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index cfb5d27090..3c06484fa1 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -3069,7 +3069,7 @@ void cmLocalGenerator::CopyPchCompilePdb( auto cc = cm::make_unique(); cc->SetCommandLines(commandLines); - cc->SetComment(comment.c_str()); + cc->SetComment(comment); cc->SetStdPipesUTF8(true); cc->AppendDepends( { reuseTarget->GetPchFile(config, language), copy_script }); diff --git a/Source/cmLocalVisualStudio7Generator.cxx b/Source/cmLocalVisualStudio7Generator.cxx index acc3ba6b78..d9a7cf77c6 100644 --- a/Source/cmLocalVisualStudio7Generator.cxx +++ b/Source/cmLocalVisualStudio7Generator.cxx @@ -272,7 +272,7 @@ cmSourceFile* cmLocalVisualStudio7Generator::CreateVCProjBuildRule() cc->SetMainDependency(makefileIn); cc->SetDepends(listFiles); cc->SetCommandLines(commandLines); - cc->SetComment(comment.c_str()); + cc->SetComment(comment); cc->SetEscapeOldStyle(false); cc->SetStdPipesUTF8(true); cc->SetUsesTerminal(true); diff --git a/Source/cmNinjaNormalTargetGenerator.cxx b/Source/cmNinjaNormalTargetGenerator.cxx index 53f8902e30..8a569188a3 100644 --- a/Source/cmNinjaNormalTargetGenerator.cxx +++ b/Source/cmNinjaNormalTargetGenerator.cxx @@ -1520,7 +1520,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement( true, config); localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]); if (cc.GetComment()) { - auto cge = ge.Parse(cc.GetComment()); + auto cge = ge.Parse(cc.GetComment().value()); cmdComments[i]->emplace_back( cge->Evaluate(this->GetLocalGenerator(), config)); } diff --git a/Source/cmNinjaUtilityTargetGenerator.cxx b/Source/cmNinjaUtilityTargetGenerator.cxx index 08d6019d12..e4f3b270a2 100644 --- a/Source/cmNinjaUtilityTargetGenerator.cxx +++ b/Source/cmNinjaUtilityTargetGenerator.cxx @@ -11,6 +11,8 @@ #include #include +#include + #include "cmCustomCommand.h" #include "cmCustomCommandGenerator.h" #include "cmGeneratedFileStream.h" @@ -96,7 +98,7 @@ void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements( if (!commandDesc.empty()) { commandDesc += "; "; } - auto cge = ge.Parse(ci.GetComment()); + auto cge = ge.Parse(ci.GetComment().value()); commandDesc += cge->Evaluate(this->GetLocalGenerator(), config); } util_outputs.Add(ccg.GetByproducts()); diff --git a/Source/cmQTWrapCPPCommand.cxx b/Source/cmQTWrapCPPCommand.cxx index d1dcb7619b..26dafebd0a 100644 --- a/Source/cmQTWrapCPPCommand.cxx +++ b/Source/cmQTWrapCPPCommand.cxx @@ -5,6 +5,7 @@ #include #include +#include #include #include "cmCustomCommand.h" diff --git a/Source/cmQtAutoGenGlobalInitializer.cxx b/Source/cmQtAutoGenGlobalInitializer.cxx index a992123ad3..b9172befe9 100644 --- a/Source/cmQtAutoGenGlobalInitializer.cxx +++ b/Source/cmQtAutoGenGlobalInitializer.cxx @@ -6,6 +6,7 @@ #include #include +#include #include "cmCustomCommand.h" #include "cmDiagnostics.h" @@ -171,9 +172,9 @@ void cmQtAutoGenGlobalInitializer::GetOrCreateGlobalTarget( // Create utility target auto cc = cm::make_unique(); - cc->SetWorkingDirectory(makefile->GetHomeOutputDirectory().c_str()); + cc->SetWorkingDirectory(makefile->GetHomeOutputDirectory()); cc->SetEscapeOldStyle(false); - cc->SetComment(comment.c_str()); + cc->SetComment(comment); cmTarget* target = localGen->AddUtilityCommand(name, true, std::move(cc)); localGen->AddGeneratorTarget( cm::make_unique(target, localGen)); diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx index e24bd38735..afd68328a2 100644 --- a/Source/cmQtAutoGenInitializer.cxx +++ b/Source/cmQtAutoGenInitializer.cxx @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -1593,7 +1594,7 @@ bool cmQtAutoGenInitializer::InitAutogenTarget() cc->SetOutputs(timestampFileGenex); cc->SetDepends(uicDependencies); cc->SetComment(""); - cc->SetWorkingDirectory(this->Dir.Work.c_str()); + cc->SetWorkingDirectory(this->Dir.Work); cc->SetEscapeOldStyle(false); cc->SetStdPipesUTF8(stdPipesUTF8); this->LocalGen->AddCustomCommandToOutput(std::move(cc)); @@ -1607,9 +1608,9 @@ bool cmQtAutoGenInitializer::InitAutogenTarget() cmCustomCommand cc; cc.SetByproducts(autogenByproducts); cc.SetCommandLines(commandLines); - cc.SetComment(autogenComment.c_str()); + cc.SetComment(autogenComment); cc.SetBacktrace(this->Makefile->GetBacktrace()); - cc.SetWorkingDirectory(this->Dir.Work.c_str()); + cc.SetWorkingDirectory(this->Dir.Work); cc.SetStdPipesUTF8(stdPipesUTF8); cc.SetEscapeOldStyle(false); cc.SetEscapeAllowMakeVars(true); @@ -1671,7 +1672,7 @@ bool cmQtAutoGenInitializer::InitAutogenTarget() cmStrCat(this->GenTarget->GetName(), "_autogen_timestamp_deps"); auto cc = cm::make_unique(); - cc->SetWorkingDirectory(this->Dir.Work.c_str()); + cc->SetWorkingDirectory(this->Dir.Work); cc->SetDepends(dependencies); cc->SetEscapeOldStyle(false); timestampTarget = this->LocalGen->AddUtilityCommand( @@ -1742,8 +1743,8 @@ bool cmQtAutoGenInitializer::InitAutogenTarget() cc->SetByproducts(timestampByproducts); cc->SetDepends(dependencies); cc->SetCommandLines(commandLines); - cc->SetComment(autogenComment.c_str()); - cc->SetWorkingDirectory(this->Dir.Work.c_str()); + cc->SetComment(autogenComment); + cc->SetWorkingDirectory(this->Dir.Work); cc->SetEscapeOldStyle(false); cc->SetDepfile(depFile); cc->SetStdPipesUTF8(stdPipesUTF8); @@ -1763,12 +1764,12 @@ bool cmQtAutoGenInitializer::InitAutogenTarget() } else { // Create autogen target auto cc = cm::make_unique(); - cc->SetWorkingDirectory(this->Dir.Work.c_str()); + cc->SetWorkingDirectory(this->Dir.Work); cc->SetByproducts(autogenByproducts); cc->SetDepends(dependencies); cc->SetCommandLines(commandLines); cc->SetEscapeOldStyle(false); - cc->SetComment(autogenComment.c_str()); + cc->SetComment(autogenComment); cmTarget* autogenTarget = this->LocalGen->AddUtilityCommand( this->AutogenTarget.Name, true, std::move(cc)); // Create autogen generator target @@ -1878,9 +1879,9 @@ bool cmQtAutoGenInitializer::InitRccTargets() FileProjectRelativePath(this->Makefile, qrc.QrcFile)); auto cc = cm::make_unique(); - cc->SetWorkingDirectory(this->Dir.Work.c_str()); + cc->SetWorkingDirectory(this->Dir.Work); cc->SetCommandLines(commandLines); - cc->SetComment(ccComment.c_str()); + cc->SetComment(ccComment); cc->SetStdPipesUTF8(true); if (qrc.Generated || this->Rcc.GlobalTarget) {