cmake/Source/cmLocalNinjaGenerator.cxx

941 lines
31 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2012-04-19 19:04:21 +03:00
#include "cmLocalNinjaGenerator.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cassert>
#include <cstdio>
#include <memory>
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <utility>
2023-07-02 19:51:09 +02:00
#include <cm/unordered_set>
2021-09-14 00:13:48 +02:00
#include <cmext/string_view>
2020-02-01 23:06:01 +01:00
#include "cmsys/FStream.hxx"
2023-12-07 09:12:54 +01:00
#include "cm_codecvt_Encoding.hxx"
2018-08-09 18:06:22 +02:00
#include "cmCryptoHash.h"
2016-10-30 18:24:19 +01:00
#include "cmCustomCommand.h"
2012-04-19 19:04:21 +03:00
#include "cmCustomCommandGenerator.h"
2016-07-09 11:21:54 +02:00
#include "cmGeneratedFileStream.h"
2019-11-11 23:01:05 +01:00
#include "cmGeneratorExpression.h"
2016-10-30 18:24:19 +01:00
#include "cmGeneratorTarget.h"
#include "cmGlobalGenerator.h"
2012-04-19 19:04:21 +03:00
#include "cmGlobalNinjaGenerator.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2019-11-11 23:01:05 +01:00
#include "cmLocalGenerator.h"
2016-07-09 11:21:54 +02:00
#include "cmMakefile.h"
2021-09-14 00:13:48 +02:00
#include "cmMessageType.h"
2012-04-19 19:04:21 +03:00
#include "cmNinjaTargetGenerator.h"
2023-07-02 19:51:09 +02:00
#include "cmNinjaTypes.h"
2021-09-14 00:13:48 +02:00
#include "cmPolicies.h"
2017-04-14 19:02:05 +02:00
#include "cmRulePlaceholderExpander.h"
2012-04-19 19:04:21 +03:00
#include "cmSourceFile.h"
2015-08-17 11:37:30 +02:00
#include "cmState.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2020-08-30 11:54:41 +02:00
#include "cmTarget.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2016-07-09 11:21:54 +02:00
#include "cmake.h"
2012-04-19 19:04:21 +03:00
2015-08-17 11:37:30 +02:00
cmLocalNinjaGenerator::cmLocalNinjaGenerator(cmGlobalGenerator* gg,
2015-11-17 17:22:37 +01:00
cmMakefile* mf)
2023-07-02 19:51:09 +02:00
: cmLocalCommonGenerator(gg, mf)
2012-04-19 19:04:21 +03:00
{
}
// Virtual public methods.
2023-07-02 19:51:09 +02:00
std::unique_ptr<cmRulePlaceholderExpander>
2017-04-14 19:02:05 +02:00
cmLocalNinjaGenerator::CreateRulePlaceholderExpander() const
{
2023-07-02 19:51:09 +02:00
auto ret = this->cmLocalGenerator::CreateRulePlaceholderExpander();
2017-04-14 19:02:05 +02:00
ret->SetTargetImpLib("$TARGET_IMPLIB");
2023-07-02 19:51:09 +02:00
return std::unique_ptr<cmRulePlaceholderExpander>(std::move(ret));
2017-04-14 19:02:05 +02:00
}
2019-11-11 23:01:05 +01:00
cmLocalNinjaGenerator::~cmLocalNinjaGenerator() = default;
2012-04-19 19:04:21 +03:00
void cmLocalNinjaGenerator::Generate()
{
2015-11-17 17:22:37 +01:00
// Compute the path to use when referencing the current output
// directory from the top output directory.
2021-09-14 00:13:48 +02:00
this->HomeRelativeOutputPath =
this->MaybeRelativeToTopBinDir(this->GetCurrentBinaryDirectory());
2016-07-09 11:21:54 +02:00
if (this->HomeRelativeOutputPath == ".") {
2018-01-26 17:06:56 +01:00
this->HomeRelativeOutputPath.clear();
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2020-08-30 11:54:41 +02:00
if (this->GetGlobalGenerator()->IsMultiConfig()) {
for (auto const& config : this->GetConfigNames()) {
this->WriteProcessedMakefile(this->GetImplFileStream(config));
}
}
this->WriteProcessedMakefile(this->GetCommonFileStream());
2012-06-27 20:52:58 +03:00
#ifdef NINJA_GEN_VERBOSE_FILES
2012-04-19 19:04:21 +03:00
this->WriteProcessedMakefile(this->GetRulesFileStream());
2012-06-27 20:52:58 +03:00
#endif
2012-04-19 19:04:21 +03:00
2014-08-03 19:52:23 +02:00
// We do that only once for the top CMakeLists.txt file.
2016-07-09 11:21:54 +02:00
if (this->IsRootMakefile()) {
2014-08-03 19:52:23 +02:00
this->WriteBuildFileTop();
this->WritePools(this->GetRulesFileStream());
2019-11-11 23:01:05 +01:00
const std::string& showIncludesPrefix =
2016-07-09 11:21:54 +02:00
this->GetMakefile()->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
if (!showIncludesPrefix.empty()) {
2014-08-03 19:52:23 +02:00
cmGlobalNinjaGenerator::WriteComment(this->GetRulesFileStream(),
"localized /showIncludes string");
2021-09-14 00:13:48 +02:00
this->GetRulesFileStream() << "msvc_deps_prefix = ";
2022-11-16 20:14:03 +01:00
// 'cl /showIncludes' encodes output in the console output code page.
// It may differ from the encoding used for file paths in 'build.ninja'.
// Ninja matches the showIncludes prefix using its raw byte sequence.
this->GetRulesFileStream().WriteAltEncoding(
showIncludesPrefix, cmGeneratedFileStream::Encoding::ConsoleOutput);
2021-09-14 00:13:48 +02:00
this->GetRulesFileStream() << "\n\n";
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2020-08-30 11:54:41 +02:00
for (const auto& target : this->GetGeneratorTargets()) {
2021-09-14 00:13:48 +02:00
if (!target->IsInBuildSystem()) {
2014-08-03 19:52:23 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
auto tg = cmNinjaTargetGenerator::New(target.get());
2016-07-09 11:21:54 +02:00
if (tg) {
2020-08-30 11:54:41 +02:00
if (target->Target->IsPerConfig()) {
for (auto const& config : this->GetConfigNames()) {
tg->Generate(config);
if (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
this->GetGlobalGenerator()->IsMultiConfig()) {
cmNinjaBuild phonyAlias("phony");
this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
2021-09-14 00:13:48 +02:00
target.get(), phonyAlias.Outputs, "", DependOnTargetArtifact);
2020-08-30 11:54:41 +02:00
this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
2021-09-14 00:13:48 +02:00
target.get(), phonyAlias.ExplicitDeps, config,
DependOnTargetArtifact);
2020-08-30 11:54:41 +02:00
this->GetGlobalNinjaGenerator()->WriteBuild(
*this->GetGlobalNinjaGenerator()->GetConfigFileStream(config),
phonyAlias);
}
}
if (target->GetType() == cmStateEnums::GLOBAL_TARGET &&
this->GetGlobalGenerator()->IsMultiConfig()) {
if (!this->GetGlobalNinjaGenerator()->GetDefaultConfigs().empty()) {
cmNinjaBuild phonyAlias("phony");
this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
2021-09-14 00:13:48 +02:00
target.get(), phonyAlias.Outputs, "", DependOnTargetArtifact);
2020-08-30 11:54:41 +02:00
for (auto const& config :
this->GetGlobalNinjaGenerator()->GetDefaultConfigs()) {
this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
2021-09-14 00:13:48 +02:00
target.get(), phonyAlias.ExplicitDeps, config,
DependOnTargetArtifact);
2020-08-30 11:54:41 +02:00
}
this->GetGlobalNinjaGenerator()->WriteBuild(
*this->GetGlobalNinjaGenerator()->GetDefaultFileStream(),
phonyAlias);
}
cmNinjaBuild phonyAlias("phony");
this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
2021-09-14 00:13:48 +02:00
target.get(), phonyAlias.Outputs, "all", DependOnTargetArtifact);
2020-08-30 11:54:41 +02:00
for (auto const& config : this->GetConfigNames()) {
this->GetGlobalNinjaGenerator()->AppendTargetOutputs(
2021-09-14 00:13:48 +02:00
target.get(), phonyAlias.ExplicitDeps, config,
DependOnTargetArtifact);
2020-08-30 11:54:41 +02:00
}
this->GetGlobalNinjaGenerator()->WriteBuild(
*this->GetGlobalNinjaGenerator()->GetDefaultFileStream(),
phonyAlias);
}
} else {
tg->Generate("");
}
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2020-08-30 11:54:41 +02:00
for (auto const& config : this->GetConfigNames()) {
this->WriteCustomCommandBuildStatements(config);
this->AdditionalCleanFiles(config);
}
2012-04-19 19:04:21 +03:00
}
// TODO: Picked up from cmLocalUnixMakefileGenerator3. Refactor it.
2016-07-09 11:21:54 +02:00
std::string cmLocalNinjaGenerator::GetTargetDirectory(
cmGeneratorTarget const* target) const
2012-04-19 19:04:21 +03:00
{
2020-02-01 23:06:01 +01:00
std::string dir = cmStrCat("CMakeFiles/", target->GetName());
2012-04-19 19:04:21 +03:00
#if defined(__VMS)
dir += "_dir";
#else
dir += ".dir";
#endif
return dir;
}
// Non-virtual public methods.
2016-07-09 11:21:54 +02:00
const cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
const
2012-04-19 19:04:21 +03:00
{
2016-07-09 11:21:54 +02:00
return static_cast<const cmGlobalNinjaGenerator*>(
this->GetGlobalGenerator());
2012-04-19 19:04:21 +03:00
}
cmGlobalNinjaGenerator* cmLocalNinjaGenerator::GetGlobalNinjaGenerator()
{
return static_cast<cmGlobalNinjaGenerator*>(this->GetGlobalGenerator());
}
2023-07-02 19:51:09 +02:00
std::string const& cmLocalNinjaGenerator::GetWorkingDirectory() const
{
return this->GetState()->GetBinaryDirectory();
}
std::string cmLocalNinjaGenerator::MaybeRelativeToWorkDir(
std::string const& path) const
{
return this->GetGlobalNinjaGenerator()->NinjaOutputPath(
this->MaybeRelativeToTopBinDir(path));
}
std::string cmLocalNinjaGenerator::GetLinkDependencyFile(
cmGeneratorTarget* target, std::string const& config) const
{
return cmStrCat(target->GetSupportDirectory(),
this->GetGlobalNinjaGenerator()->ConfigDirectory(config),
"/link.d");
}
2012-04-19 19:04:21 +03:00
// Virtual protected methods.
2016-07-09 11:21:54 +02:00
std::string cmLocalNinjaGenerator::ConvertToIncludeReference(
2022-03-29 21:10:50 +02:00
std::string const& path, cmOutputConverter::OutputFormat format)
2012-04-19 19:04:21 +03:00
{
2021-09-14 00:13:48 +02:00
return this->ConvertToOutputFormat(path, format);
2012-04-19 19:04:21 +03:00
}
// Private methods.
2020-08-30 11:54:41 +02:00
cmGeneratedFileStream& cmLocalNinjaGenerator::GetImplFileStream(
const std::string& config) const
2012-04-19 19:04:21 +03:00
{
2020-08-30 11:54:41 +02:00
return *this->GetGlobalNinjaGenerator()->GetImplFileStream(config);
}
cmGeneratedFileStream& cmLocalNinjaGenerator::GetCommonFileStream() const
{
return *this->GetGlobalNinjaGenerator()->GetCommonFileStream();
2012-04-19 19:04:21 +03:00
}
cmGeneratedFileStream& cmLocalNinjaGenerator::GetRulesFileStream() const
{
return *this->GetGlobalNinjaGenerator()->GetRulesFileStream();
}
const cmake* cmLocalNinjaGenerator::GetCMakeInstance() const
{
return this->GetGlobalGenerator()->GetCMakeInstance();
}
cmake* cmLocalNinjaGenerator::GetCMakeInstance()
{
return this->GetGlobalGenerator()->GetCMakeInstance();
}
void cmLocalNinjaGenerator::WriteBuildFileTop()
{
2020-08-30 11:54:41 +02:00
this->WriteProjectHeader(this->GetCommonFileStream());
if (this->GetGlobalGenerator()->IsMultiConfig()) {
for (auto const& config : this->GetConfigNames()) {
auto& stream = this->GetImplFileStream(config);
this->WriteProjectHeader(stream);
this->WriteNinjaRequiredVersion(stream);
this->WriteNinjaConfigurationVariable(stream, config);
this->WriteNinjaFilesInclusionConfig(stream);
}
} else {
this->WriteNinjaRequiredVersion(this->GetCommonFileStream());
this->WriteNinjaConfigurationVariable(this->GetCommonFileStream(),
this->GetConfigNames().front());
}
this->WriteNinjaFilesInclusionCommon(this->GetCommonFileStream());
2021-09-14 00:13:48 +02:00
this->WriteNinjaWorkDir(this->GetCommonFileStream());
2012-04-19 19:04:21 +03:00
// For the rule file.
this->WriteProjectHeader(this->GetRulesFileStream());
}
void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
2020-08-30 11:54:41 +02:00
os << "# Project: " << this->GetProjectName() << '\n'
<< "# Configurations: " << cmJoin(this->GetConfigNames(), ", ") << '\n';
2012-04-19 19:04:21 +03:00
cmGlobalNinjaGenerator::WriteDivider(os);
}
2015-04-27 22:25:09 +02:00
void cmLocalNinjaGenerator::WriteNinjaRequiredVersion(std::ostream& os)
{
// Default required version
2019-11-11 23:01:05 +01:00
std::string requiredVersion = cmGlobalNinjaGenerator::RequiredNinjaVersion();
2015-04-27 22:25:09 +02:00
// Ninja generator uses the 'console' pool if available (>= 1.5)
2021-11-20 13:41:27 +01:00
if (this->GetGlobalNinjaGenerator()->SupportsDirectConsole()) {
2015-11-17 17:22:37 +01:00
requiredVersion =
2019-11-11 23:01:05 +01:00
cmGlobalNinjaGenerator::RequiredNinjaVersionForConsolePool();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2018-08-09 18:06:22 +02:00
// The Ninja generator writes rules which require support for restat
// when rebuilding build.ninja manifest (>= 1.8)
if (this->GetGlobalNinjaGenerator()->SupportsManifestRestat() &&
this->GetCMakeInstance()->DoWriteGlobVerifyTarget() &&
!this->GetGlobalNinjaGenerator()->GlobalSettingIsOn(
"CMAKE_SUPPRESS_REGENERATION")) {
requiredVersion =
2019-11-11 23:01:05 +01:00
cmGlobalNinjaGenerator::RequiredNinjaVersionForManifestRestat();
2018-08-09 18:06:22 +02:00
}
2016-07-09 11:21:54 +02:00
cmGlobalNinjaGenerator::WriteComment(
os, "Minimal version of Ninja required by this file");
2020-08-30 11:54:41 +02:00
os << "ninja_required_version = " << requiredVersion << "\n\n";
}
void cmLocalNinjaGenerator::WriteNinjaConfigurationVariable(
std::ostream& os, const std::string& config)
{
cmGlobalNinjaGenerator::WriteVariable(
os, "CONFIGURATION", config,
"Set configuration variable for custom commands.");
2015-04-27 22:25:09 +02:00
}
2014-08-03 19:52:23 +02:00
void cmLocalNinjaGenerator::WritePools(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
2021-11-20 13:41:27 +01:00
cmValue jobpools =
2016-07-09 11:21:54 +02:00
this->GetCMakeInstance()->GetState()->GetGlobalProperty("JOB_POOLS");
2018-04-23 21:13:27 +02:00
if (!jobpools) {
2021-09-14 00:13:48 +02:00
jobpools = this->GetMakefile()->GetDefinition("CMAKE_JOB_POOLS");
2018-04-23 21:13:27 +02:00
}
2016-07-09 11:21:54 +02:00
if (jobpools) {
cmGlobalNinjaGenerator::WriteComment(
os, "Pools defined by global property JOB_POOLS");
2023-07-02 19:51:09 +02:00
cmList pools{ *jobpools };
2018-01-26 17:06:56 +01:00
for (std::string const& pool : pools) {
2016-07-09 11:21:54 +02:00
const std::string::size_type eq = pool.find('=');
2014-08-03 19:52:23 +02:00
unsigned int jobs;
if (eq != std::string::npos &&
2016-07-09 11:21:54 +02:00
sscanf(pool.c_str() + eq, "=%u", &jobs) == 1) {
2020-08-30 11:54:41 +02:00
os << "pool " << pool.substr(0, eq) << "\n depth = " << jobs
<< "\n\n";
2016-07-09 11:21:54 +02:00
} else {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': " +
pool);
2014-08-03 19:52:23 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2020-08-30 11:54:41 +02:00
void cmLocalNinjaGenerator::WriteNinjaFilesInclusionConfig(std::ostream& os)
2012-04-19 19:04:21 +03:00
{
cmGlobalNinjaGenerator::WriteDivider(os);
2020-08-30 11:54:41 +02:00
os << "# Include auxiliary files.\n\n";
cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
std::string const ninjaCommonFile =
ng->NinjaOutputPath(cmGlobalNinjaMultiGenerator::NINJA_COMMON_FILE);
std::string const commonFilePath = ng->EncodePath(ninjaCommonFile);
cmGlobalNinjaGenerator::WriteInclude(os, commonFilePath,
"Include common file.");
os << "\n";
}
void cmLocalNinjaGenerator::WriteNinjaFilesInclusionCommon(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
os << "# Include auxiliary files.\n\n";
2016-07-09 11:21:54 +02:00
cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
std::string const ninjaRulesFile =
ng->NinjaOutputPath(cmGlobalNinjaGenerator::NINJA_RULES_FILE);
2018-08-09 18:06:22 +02:00
std::string const rulesFilePath = ng->EncodePath(ninjaRulesFile);
2016-07-09 11:21:54 +02:00
cmGlobalNinjaGenerator::WriteInclude(os, rulesFilePath,
2012-04-19 19:04:21 +03:00
"Include rules file.");
os << "\n";
}
2021-09-14 00:13:48 +02:00
void cmLocalNinjaGenerator::WriteNinjaWorkDir(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
cmGlobalNinjaGenerator::WriteComment(
os, "Logical path to working directory; prefix for absolute paths.");
cmGlobalNinjaGenerator* ng = this->GetGlobalNinjaGenerator();
std::string ninja_workdir = this->GetBinaryDirectory();
ng->StripNinjaOutputPathPrefixAsSuffix(ninja_workdir); // Also appends '/'.
os << "cmake_ninja_workdir = " << ng->EncodePath(ninja_workdir) << "\n";
}
2012-04-19 19:04:21 +03:00
void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
2020-08-30 11:54:41 +02:00
os << "# Write statements declared in CMakeLists.txt:\n"
2021-09-14 00:13:48 +02:00
<< "# " << this->Makefile->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE")
2020-08-30 11:54:41 +02:00
<< '\n';
2016-10-30 18:24:19 +01:00
if (this->IsRootMakefile()) {
2020-08-30 11:54:41 +02:00
os << "# Which is the root file.\n";
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
cmGlobalNinjaGenerator::WriteDivider(os);
2020-08-30 11:54:41 +02:00
os << '\n';
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,
2020-08-30 11:54:41 +02:00
cmNinjaDeps& outputs,
const std::string& config)
2012-04-19 19:04:21 +03:00
{
2021-09-14 00:13:48 +02:00
this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs, config,
DependOnTargetArtifact);
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
void cmLocalNinjaGenerator::AppendTargetDepends(cmGeneratorTarget* target,
2017-07-20 19:35:53 +02:00
cmNinjaDeps& outputs,
2020-08-30 11:54:41 +02:00
const std::string& config,
const std::string& fileConfig,
2017-07-20 19:35:53 +02:00
cmNinjaTargetDepends depends)
2012-04-19 19:04:21 +03:00
{
2020-08-30 11:54:41 +02:00
this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs, config,
fileConfig, depends);
2012-04-19 19:04:21 +03:00
}
2015-04-27 22:25:09 +02:00
void cmLocalNinjaGenerator::AppendCustomCommandDeps(
2020-08-30 11:54:41 +02:00
cmCustomCommandGenerator const& ccg, cmNinjaDeps& ninjaDeps,
const std::string& config)
2012-04-19 19:04:21 +03:00
{
2019-11-11 23:01:05 +01:00
for (std::string const& i : ccg.GetDepends()) {
2012-04-19 19:04:21 +03:00
std::string dep;
2020-08-30 11:54:41 +02:00
if (this->GetRealDependency(i, config, dep)) {
2015-11-17 17:22:37 +01:00
ninjaDeps.push_back(
this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(dep));
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
}
}
2018-08-09 18:06:22 +02:00
std::string cmLocalNinjaGenerator::WriteCommandScript(
2021-09-14 00:13:48 +02:00
std::vector<std::string> const& cmdLines, std::string const& outputConfig,
std::string const& commandConfig, std::string const& customStep,
2018-08-09 18:06:22 +02:00
cmGeneratorTarget const* target) const
{
std::string scriptPath;
if (target) {
scriptPath = target->GetSupportDirectory();
} else {
2020-02-01 23:06:01 +01:00
scriptPath = cmStrCat(this->GetCurrentBinaryDirectory(), "/CMakeFiles");
2018-08-09 18:06:22 +02:00
}
2021-09-14 00:13:48 +02:00
scriptPath += this->GetGlobalNinjaGenerator()->ConfigDirectory(outputConfig);
2018-08-09 18:06:22 +02:00
cmSystemTools::MakeDirectory(scriptPath);
scriptPath += '/';
scriptPath += customStep;
2021-09-14 00:13:48 +02:00
if (this->GlobalGenerator->IsMultiConfig()) {
scriptPath += cmStrCat('-', commandConfig);
}
2018-08-09 18:06:22 +02:00
#ifdef _WIN32
scriptPath += ".bat";
#else
scriptPath += ".sh";
#endif
cmsys::ofstream script(scriptPath.c_str());
2019-11-11 23:01:05 +01:00
#ifdef _WIN32
script << "@echo off\n";
int line = 1;
#else
2018-08-09 18:06:22 +02:00
script << "set -e\n\n";
#endif
for (auto const& i : cmdLines) {
std::string cmd = i;
// The command line was built assuming it would be written to
// the build.ninja file, so it uses '$$' for '$'. Remove this
// for the raw shell script.
cmSystemTools::ReplaceString(cmd, "$$", "$");
#ifdef _WIN32
2019-11-11 23:01:05 +01:00
script << cmd << " || (set FAIL_LINE=" << ++line << "& goto :ABORT)"
<< '\n';
2018-08-09 18:06:22 +02:00
#else
script << cmd << '\n';
#endif
}
2019-11-11 23:01:05 +01:00
#ifdef _WIN32
script << "goto :EOF\n\n"
":ABORT\n"
"set ERROR_CODE=%ERRORLEVEL%\n"
"echo Batch file failed at line %FAIL_LINE% "
"with errorcode %ERRORLEVEL%\n"
"exit /b %ERROR_CODE%";
#endif
2018-08-09 18:06:22 +02:00
return scriptPath;
}
2023-05-23 16:38:00 +02:00
#ifdef _WIN32
namespace {
bool RuleNeedsCMD(std::string const& cmd)
{
std::vector<std::string> args;
cmSystemTools::ParseWindowsCommandLine(cmd.c_str(), args);
auto it = std::find_if(args.cbegin(), args.cend(),
[](std::string const& arg) -> bool {
// FIXME: Detect more windows shell operators.
return cmHasLiteralPrefix(arg, ">");
});
return it != args.cend();
}
}
#endif
2012-04-19 19:04:21 +03:00
std::string cmLocalNinjaGenerator::BuildCommandLine(
2021-09-14 00:13:48 +02:00
std::vector<std::string> const& cmdLines, std::string const& outputConfig,
std::string const& commandConfig, std::string const& customStep,
2018-08-09 18:06:22 +02:00
cmGeneratorTarget const* target) const
2012-04-19 19:04:21 +03:00
{
2017-04-14 19:02:05 +02:00
// If we have no commands but we need to build a command anyway, use noop.
2012-04-19 19:04:21 +03:00
// This happens when building a POST_BUILD value for link targets that
// don't use POST_BUILD.
2016-10-30 18:24:19 +01:00
if (cmdLines.empty()) {
2017-04-14 19:02:05 +02:00
return cmGlobalNinjaGenerator::SHELL_NOOP;
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
2018-08-09 18:06:22 +02:00
// If this is a custom step then we will have no '$VAR' ninja placeholders.
// This means we can deal with long command sequences by writing to a script.
// Do this if the command lines are on the scale of the OS limit.
if (!customStep.empty()) {
size_t cmdLinesTotal = 0;
for (std::string const& cmd : cmdLines) {
cmdLinesTotal += cmd.length() + 6;
}
if (cmdLinesTotal > cmSystemTools::CalculateCommandLineLengthLimit() / 2) {
2021-09-14 00:13:48 +02:00
std::string const scriptPath = this->WriteCommandScript(
cmdLines, outputConfig, commandConfig, customStep, target);
2018-08-09 18:06:22 +02:00
std::string cmd
#ifndef _WIN32
= "/bin/sh "
#endif
;
cmd += this->ConvertToOutputFormat(
this->GetGlobalNinjaGenerator()->ConvertToNinjaPath(scriptPath),
cmOutputConverter::SHELL);
// Add an unused argument based on script content so that Ninja
// knows when the command lines change.
cmd += " ";
cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
cmd += hash.HashFile(scriptPath).substr(0, 16);
return cmd;
}
}
2015-04-27 22:25:09 +02:00
std::ostringstream cmd;
2012-06-27 20:52:58 +03:00
#ifdef _WIN32
2023-12-07 09:12:54 +01:00
cmGlobalNinjaGenerator const* gg = this->GetGlobalNinjaGenerator();
2023-05-23 16:38:00 +02:00
bool const needCMD =
cmdLines.size() > 1 || (customStep.empty() && RuleNeedsCMD(cmdLines[0]));
for (auto li = cmdLines.begin(); li != cmdLines.end(); ++li) {
2016-07-09 11:21:54 +02:00
if (li != cmdLines.begin()) {
2014-08-03 19:52:23 +02:00
cmd << " && ";
2023-05-23 16:38:00 +02:00
} else if (needCMD) {
2023-12-07 09:12:54 +01:00
cmd << gg->GetComspec() << " /C \"";
2012-06-27 20:52:58 +03:00
}
2017-07-20 19:35:53 +02:00
// Put current cmdLine in brackets if it contains "||" because it has
// higher precedence than "&&" in cmd.exe
if (li->find("||") != std::string::npos) {
cmd << "( " << *li << " )";
} else {
cmd << *li;
}
2016-07-09 11:21:54 +02:00
}
2023-05-23 16:38:00 +02:00
if (needCMD) {
2014-08-03 19:52:23 +02:00
cmd << "\"";
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
#else
2023-05-23 16:38:00 +02:00
for (auto li = cmdLines.begin(); li != cmdLines.end(); ++li) {
2016-07-09 11:21:54 +02:00
if (li != cmdLines.begin()) {
2014-08-03 19:52:23 +02:00
cmd << " && ";
}
2016-07-09 11:21:54 +02:00
cmd << *li;
}
2014-08-03 19:52:23 +02:00
#endif
2012-04-19 19:04:21 +03:00
return cmd.str();
}
2015-04-27 22:25:09 +02:00
void cmLocalNinjaGenerator::AppendCustomCommandLines(
2016-07-09 11:21:54 +02:00
cmCustomCommandGenerator const& ccg, std::vector<std::string>& cmdLines)
2012-04-19 19:04:21 +03:00
{
2020-08-30 11:54:41 +02:00
auto* gg = this->GetGlobalNinjaGenerator();
2012-04-19 19:04:21 +03:00
if (ccg.GetNumberOfCommands() > 0) {
2015-04-27 22:25:09 +02:00
std::string wd = ccg.GetWorkingDirectory();
2016-10-30 18:24:19 +01:00
if (wd.empty()) {
2016-03-13 13:35:51 +01:00
wd = this->GetCurrentBinaryDirectory();
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
std::ostringstream cdCmd;
2013-11-03 12:27:13 +02:00
#ifdef _WIN32
2016-07-09 11:21:54 +02:00
std::string cdStr = "cd /D ";
2013-11-03 12:27:13 +02:00
#else
2016-07-09 11:21:54 +02:00
std::string cdStr = "cd ";
2013-11-03 12:27:13 +02:00
#endif
2016-07-09 11:21:54 +02:00
cdCmd << cdStr
<< this->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
2012-04-19 19:04:21 +03:00
cmdLines.push_back(cdCmd.str());
}
2014-08-03 19:52:23 +02:00
2015-04-27 22:25:09 +02:00
std::string launcher = this->MakeCustomLauncher(ccg);
2014-08-03 19:52:23 +02:00
2012-04-19 19:04:21 +03:00
for (unsigned i = 0; i != ccg.GetNumberOfCommands(); ++i) {
2021-09-14 00:13:48 +02:00
std::string c = ccg.GetCommand(i);
if (c.empty()) {
continue;
}
2014-08-03 19:52:23 +02:00
cmdLines.push_back(launcher +
2020-08-30 11:54:41 +02:00
this->ConvertToOutputFormat(
2021-09-14 00:13:48 +02:00
c,
2020-08-30 11:54:41 +02:00
gg->IsMultiConfig() ? cmOutputConverter::NINJAMULTI
: cmOutputConverter::SHELL));
2014-08-03 19:52:23 +02:00
2012-04-19 19:04:21 +03:00
std::string& cmd = cmdLines.back();
ccg.AppendArguments(i, cmd);
}
}
2016-07-09 11:21:54 +02:00
void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement(
2021-09-14 00:13:48 +02:00
cmCustomCommand const* cc, const std::set<cmGeneratorTarget*>& targets,
const std::string& fileConfig)
2012-04-19 19:04:21 +03:00
{
2019-11-11 23:01:05 +01:00
cmGlobalNinjaGenerator* gg = this->GetGlobalNinjaGenerator();
2021-09-14 00:13:48 +02:00
if (gg->SeenCustomCommand(cc, fileConfig)) {
2012-04-19 19:04:21 +03:00
return;
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
2021-09-14 00:13:48 +02:00
auto ccgs = this->MakeCustomCommandGenerators(*cc, fileConfig);
for (cmCustomCommandGenerator const& ccg : ccgs) {
if (ccg.GetOutputs().empty() && ccg.GetByproducts().empty()) {
// Generator expressions evaluate to no output for this config.
continue;
}
2015-04-27 22:25:09 +02:00
2023-07-02 19:51:09 +02:00
std::unordered_set<std::string> orderOnlyDeps;
if (!cc->GetDependsExplicitOnly()) {
// A custom command may appear on multiple targets. However, some build
// systems exist where the target dependencies on some of the targets are
// overspecified, leading to a dependency cycle. If we assume all target
// dependencies are a superset of the true target dependencies for this
// custom command, we can take the set intersection of all target
// dependencies to obtain a correct dependency list.
//
// FIXME: This won't work in certain obscure scenarios involving indirect
// dependencies.
auto j = targets.begin();
assert(j != targets.end());
2021-09-14 00:13:48 +02:00
this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(
2023-07-02 19:51:09 +02:00
*j, orderOnlyDeps, ccg.GetOutputConfig(), fileConfig, ccgs.size() > 1);
++j;
for (; j != targets.end(); ++j) {
std::unordered_set<std::string> jDeps;
this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(
*j, jDeps, ccg.GetOutputConfig(), fileConfig, ccgs.size() > 1);
cm::erase_if(orderOnlyDeps, [&jDeps](std::string const& dep) {
return jDeps.find(dep) == jDeps.end();
});
}
2021-09-14 00:13:48 +02:00
}
const std::vector<std::string>& outputs = ccg.GetOutputs();
const std::vector<std::string>& byproducts = ccg.GetByproducts();
bool symbolic = false;
for (std::string const& output : outputs) {
if (cmSourceFile* sf = this->Makefile->GetSource(output)) {
if (sf->GetPropertyAsBool("SYMBOLIC")) {
symbolic = true;
break;
}
}
}
cmGlobalNinjaGenerator::CCOutputs ccOutputs(gg);
ccOutputs.Add(outputs);
ccOutputs.Add(byproducts);
std::string mainOutput = ccOutputs.ExplicitOuts[0];
cmNinjaDeps ninjaDeps;
this->AppendCustomCommandDeps(ccg, ninjaDeps, fileConfig);
std::vector<std::string> cmdLines;
this->AppendCustomCommandLines(ccg, cmdLines);
2023-07-02 19:51:09 +02:00
cmNinjaDeps sortedOrderOnlyDeps(orderOnlyDeps.begin(),
orderOnlyDeps.end());
std::sort(sortedOrderOnlyDeps.begin(), sortedOrderOnlyDeps.end());
2021-09-14 00:13:48 +02:00
if (cmdLines.empty()) {
cmNinjaBuild build("phony");
build.Comment = cmStrCat("Phony custom command for ", mainOutput);
build.Outputs = std::move(ccOutputs.ExplicitOuts);
build.WorkDirOuts = std::move(ccOutputs.WorkDirOuts);
build.ExplicitDeps = std::move(ninjaDeps);
2023-07-02 19:51:09 +02:00
build.OrderOnlyDeps = std::move(sortedOrderOnlyDeps);
2021-09-14 00:13:48 +02:00
gg->WriteBuild(this->GetImplFileStream(fileConfig), build);
} else {
std::string customStep = cmSystemTools::GetFilenameName(mainOutput);
if (this->GlobalGenerator->IsMultiConfig()) {
customStep += '-';
customStep += fileConfig;
customStep += '-';
customStep += ccg.GetOutputConfig();
2019-11-11 23:01:05 +01:00
}
2021-09-14 00:13:48 +02:00
// Hash full path to make unique.
customStep += '-';
cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
customStep += hash.HashString(mainOutput).substr(0, 7);
std::string depfile = ccg.GetDepfile();
if (!depfile.empty()) {
switch (cc->GetCMP0116Status()) {
case cmPolicies::WARN:
if (this->GetCurrentBinaryDirectory() !=
this->GetBinaryDirectory() ||
this->Makefile->PolicyOptionalWarningEnabled(
"CMAKE_POLICY_WARNING_CMP0116")) {
this->GetCMakeInstance()->IssueMessage(
MessageType::AUTHOR_WARNING,
cmPolicies::GetPolicyWarning(cmPolicies::CMP0116),
cc->GetBacktrace());
}
CM_FALLTHROUGH;
case cmPolicies::OLD:
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
depfile = ccg.GetInternalDepfile();
break;
}
}
std::string comment = cmStrCat("Custom command for ", mainOutput);
gg->WriteCustomCommandBuild(
this->BuildCommandLine(cmdLines, ccg.GetOutputConfig(), fileConfig,
customStep),
this->ConstructComment(ccg), comment, depfile, cc->GetJobPool(),
cc->GetUsesTerminal(),
/*restat*/ !symbolic || !byproducts.empty(), fileConfig,
2023-07-02 19:51:09 +02:00
std::move(ccOutputs), std::move(ninjaDeps),
std::move(sortedOrderOnlyDeps));
2016-03-13 13:35:51 +01:00
}
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
}
2016-03-13 13:35:51 +01:00
2021-09-14 00:13:48 +02:00
bool cmLocalNinjaGenerator::HasUniqueByproducts(
std::vector<std::string> const& byproducts, cmListFileBacktrace const& bt)
{
2023-05-23 16:38:00 +02:00
cmGeneratorExpression ge(*this->GetCMakeInstance(), bt);
2021-09-14 00:13:48 +02:00
for (std::string const& p : byproducts) {
if (cmGeneratorExpression::Find(p) == std::string::npos) {
return false;
}
std::set<std::string> seen;
std::unique_ptr<cmCompiledGeneratorExpression> cge = ge.Parse(p);
2024-07-09 14:46:46 +02:00
for (std::string const& config : this->GetConfigNames()) {
2021-09-14 00:13:48 +02:00
for (std::string const& b :
this->ExpandCustomCommandOutputPaths(*cge, config)) {
if (!seen.insert(b).second) {
return false;
}
}
}
}
return true;
}
namespace {
bool HasUniqueOutputs(std::vector<cmCustomCommandGenerator> const& ccgs)
{
std::set<std::string> allOutputs;
std::set<std::string> allByproducts;
for (cmCustomCommandGenerator const& ccg : ccgs) {
for (std::string const& output : ccg.GetOutputs()) {
if (!allOutputs.insert(output).second) {
return false;
}
}
for (std::string const& byproduct : ccg.GetByproducts()) {
if (!allByproducts.insert(byproduct).second) {
return false;
}
}
}
return true;
}
}
std::string cmLocalNinjaGenerator::CreateUtilityOutput(
std::string const& targetName, std::vector<std::string> const& byproducts,
cmListFileBacktrace const& bt)
{
// In Ninja Multi-Config, we can only produce cross-config utility
// commands if all byproducts are per-config.
if (!this->GetGlobalGenerator()->IsMultiConfig() ||
!this->HasUniqueByproducts(byproducts, bt)) {
return this->cmLocalGenerator::CreateUtilityOutput(targetName, byproducts,
bt);
}
std::string const base = cmStrCat(this->GetCurrentBinaryDirectory(),
"/CMakeFiles/", targetName, '-');
// The output is not actually created so mark it symbolic.
2024-07-09 14:46:46 +02:00
for (std::string const& config : this->GetConfigNames()) {
2021-09-14 00:13:48 +02:00
std::string const force = cmStrCat(base, config);
if (cmSourceFile* sf = this->Makefile->GetOrCreateGeneratedSource(force)) {
sf->SetProperty("SYMBOLIC", "1");
} else {
cmSystemTools::Error("Could not get source file entry for " + force);
}
}
this->GetGlobalNinjaGenerator()->AddPerConfigUtilityTarget(targetName);
return cmStrCat(base, "$<CONFIG>"_s);
}
2012-04-19 19:04:21 +03:00
2021-09-14 00:13:48 +02:00
std::vector<cmCustomCommandGenerator>
cmLocalNinjaGenerator::MakeCustomCommandGenerators(
cmCustomCommand const& cc, std::string const& fileConfig)
{
cmGlobalNinjaGenerator const* gg = this->GetGlobalNinjaGenerator();
bool transformDepfile = false;
switch (cc.GetCMP0116Status()) {
case cmPolicies::WARN:
2021-11-20 13:41:27 +01:00
CM_FALLTHROUGH;
case cmPolicies::OLD:
2021-09-14 00:13:48 +02:00
break;
case cmPolicies::REQUIRED_IF_USED:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::NEW:
transformDepfile = true;
break;
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
2021-09-14 00:13:48 +02:00
// Start with the build graph's configuration.
std::vector<cmCustomCommandGenerator> ccgs;
ccgs.emplace_back(cc, fileConfig, this, transformDepfile);
2019-11-11 23:01:05 +01:00
2021-09-14 00:13:48 +02:00
// Consider adding cross configurations.
if (!gg->EnableCrossConfigBuild()) {
return ccgs;
}
2012-04-19 19:04:21 +03:00
2021-09-14 00:13:48 +02:00
// Outputs and byproducts must be expressed using generator expressions.
for (std::string const& output : cc.GetOutputs()) {
if (cmGeneratorExpression::Find(output) == std::string::npos) {
return ccgs;
}
}
for (std::string const& byproduct : cc.GetByproducts()) {
if (cmGeneratorExpression::Find(byproduct) == std::string::npos) {
return ccgs;
}
}
// Tentatively add the other cross configurations.
for (std::string const& config : gg->GetCrossConfigs(fileConfig)) {
if (fileConfig != config) {
ccgs.emplace_back(cc, fileConfig, this, transformDepfile, config);
}
}
2018-08-09 18:06:22 +02:00
2021-09-14 00:13:48 +02:00
// If outputs and byproducts are not unique to each configuration,
// drop the cross configurations.
if (!HasUniqueOutputs(ccgs)) {
ccgs.erase(ccgs.begin() + 1, ccgs.end());
2012-04-19 19:04:21 +03:00
}
2021-09-14 00:13:48 +02:00
return ccgs;
2012-04-19 19:04:21 +03:00
}
void cmLocalNinjaGenerator::AddCustomCommandTarget(cmCustomCommand const* cc,
2016-03-13 13:35:51 +01:00
cmGeneratorTarget* target)
2012-04-19 19:04:21 +03:00
{
2016-07-09 11:21:54 +02:00
CustomCommandTargetMap::value_type v(cc, std::set<cmGeneratorTarget*>());
std::pair<CustomCommandTargetMap::iterator, bool> ins =
this->CustomCommandTargets.insert(v);
if (ins.second) {
this->CustomCommands.push_back(cc);
}
ins.first->second.insert(target);
2012-04-19 19:04:21 +03:00
}
2020-08-30 11:54:41 +02:00
void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements(
2021-09-14 00:13:48 +02:00
const std::string& fileConfig)
2012-04-19 19:04:21 +03:00
{
2018-01-26 17:06:56 +01:00
for (cmCustomCommand const* customCommand : this->CustomCommands) {
2020-02-01 23:06:01 +01:00
auto i = this->CustomCommandTargets.find(customCommand);
2016-07-09 11:21:54 +02:00
assert(i != this->CustomCommandTargets.end());
2021-09-14 00:13:48 +02:00
this->WriteCustomCommandBuildStatement(i->first, i->second, fileConfig);
2012-04-19 19:04:21 +03:00
}
}
2014-08-03 19:52:23 +02:00
std::string cmLocalNinjaGenerator::MakeCustomLauncher(
2015-04-27 22:25:09 +02:00
cmCustomCommandGenerator const& ccg)
2014-08-03 19:52:23 +02:00
{
2021-11-20 13:41:27 +01:00
cmValue property_value = this->Makefile->GetProperty("RULE_LAUNCH_CUSTOM");
2014-08-03 19:52:23 +02:00
2021-09-14 00:13:48 +02:00
if (!cmNonempty(property_value)) {
2014-08-03 19:52:23 +02:00
return std::string();
}
2018-10-28 12:09:07 +01:00
// Expand rule variables referenced in the given launcher command.
2017-04-14 19:02:05 +02:00
cmRulePlaceholderExpander::RuleVariables vars;
2014-08-03 19:52:23 +02:00
std::string output;
2015-04-27 22:25:09 +02:00
const std::vector<std::string>& outputs = ccg.GetOutputs();
2016-07-09 11:21:54 +02:00
if (!outputs.empty()) {
2017-04-14 19:02:05 +02:00
output = outputs[0];
2016-10-30 18:24:19 +01:00
if (ccg.GetWorkingDirectory().empty()) {
2021-09-14 00:13:48 +02:00
output = this->MaybeRelativeToCurBinDir(output);
2016-10-30 18:24:19 +01:00
}
2017-04-14 19:02:05 +02:00
output = this->ConvertToOutputFormat(output, cmOutputConverter::SHELL);
2014-08-03 19:52:23 +02:00
}
vars.Output = output.c_str();
2023-07-02 19:51:09 +02:00
auto rulePlaceholderExpander = this->CreateRulePlaceholderExpander();
2017-04-14 19:02:05 +02:00
2020-08-30 11:54:41 +02:00
std::string launcher = *property_value;
2017-04-14 19:02:05 +02:00
rulePlaceholderExpander->ExpandRuleVariables(this, launcher, vars);
2016-07-09 11:21:54 +02:00
if (!launcher.empty()) {
2014-08-03 19:52:23 +02:00
launcher += " ";
}
return launcher;
}
2019-11-11 23:01:05 +01:00
2020-08-30 11:54:41 +02:00
void cmLocalNinjaGenerator::AdditionalCleanFiles(const std::string& config)
2019-11-11 23:01:05 +01:00
{
2021-11-20 13:41:27 +01:00
if (cmValue prop_value =
2019-11-11 23:01:05 +01:00
this->Makefile->GetProperty("ADDITIONAL_CLEAN_FILES")) {
2023-07-02 19:51:09 +02:00
cmList cleanFiles{ cmGeneratorExpression::Evaluate(*prop_value, this,
config) };
2019-11-11 23:01:05 +01:00
std::string const& binaryDir = this->GetCurrentBinaryDirectory();
cmGlobalNinjaGenerator* gg = this->GetGlobalNinjaGenerator();
2023-07-02 19:51:09 +02:00
for (auto const& cleanFile : cleanFiles) {
2019-11-11 23:01:05 +01:00
// Support relative paths
gg->AddAdditionalCleanFile(
2020-08-30 11:54:41 +02:00
cmSystemTools::CollapseFullPath(cleanFile, binaryDir), config);
2019-11-11 23:01:05 +01:00
}
}
}