cmake/Source/cmLocalNinjaGenerator.cxx

593 lines
19 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>
#include <assert.h>
#include <iterator>
2018-01-26 17:06:56 +01:00
#include <memory> // IWYU pragma: keep
2017-04-14 19:02:05 +02:00
#include <sstream>
#include <stdio.h>
#include <utility>
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"
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"
2016-07-09 11:21:54 +02:00
#include "cmMakefile.h"
2012-04-19 19:04:21 +03:00
#include "cmNinjaTargetGenerator.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"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2016-07-09 11:21:54 +02:00
#include "cmake.h"
2018-08-09 18:06:22 +02:00
#include "cmsys/FStream.hxx"
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)
2016-10-30 18:24:19 +01:00
: cmLocalCommonGenerator(gg, mf, mf->GetState()->GetBinaryDirectory())
2012-04-19 19:04:21 +03:00
, HomeRelativeOutputPath("")
{
}
// Virtual public methods.
2017-04-14 19:02:05 +02:00
cmRulePlaceholderExpander*
cmLocalNinjaGenerator::CreateRulePlaceholderExpander() const
{
2017-07-20 19:35:53 +02:00
cmRulePlaceholderExpander* ret =
new cmRulePlaceholderExpander(this->Compilers, this->VariableMappings,
this->CompilerSysroot, this->LinkerSysroot);
2017-04-14 19:02:05 +02:00
ret->SetTargetImpLib("$TARGET_IMPLIB");
return ret;
}
2012-04-19 19:04:21 +03:00
cmLocalNinjaGenerator::~cmLocalNinjaGenerator()
{
}
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.
2016-10-30 18:24:19 +01:00
this->HomeRelativeOutputPath = this->ConvertToRelativePath(
this->GetBinaryDirectory(), 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
2012-04-19 19:04:21 +03:00
this->WriteProcessedMakefile(this->GetBuildFileStream());
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());
2016-07-09 11:21:54 +02:00
const std::string showIncludesPrefix =
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");
2018-08-09 18:06:22 +02:00
this->GetRulesFileStream()
<< "msvc_deps_prefix = " << showIncludesPrefix << "\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
2018-01-26 17:06:56 +01:00
const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
for (cmGeneratorTarget* target : targets) {
if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
2014-08-03 19:52:23 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
cmNinjaTargetGenerator* tg = cmNinjaTargetGenerator::New(target);
2016-07-09 11:21:54 +02:00
if (tg) {
2012-04-19 19:04:21 +03:00
tg->Generate();
// Add the target to "all" if required.
if (!this->GetGlobalNinjaGenerator()->IsExcluded(
2018-01-26 17:06:56 +01:00
this->GetGlobalNinjaGenerator()->GetLocalGenerators()[0],
target)) {
this->GetGlobalNinjaGenerator()->AddDependencyToAll(target);
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
delete tg;
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
this->WriteCustomCommandBuildStatements();
}
// 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
{
std::string dir = cmake::GetCMakeFilesDirectoryPostSlash();
2016-03-13 13:35:51 +01:00
dir += 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());
}
// Virtual protected methods.
2016-07-09 11:21:54 +02:00
std::string cmLocalNinjaGenerator::ConvertToIncludeReference(
std::string const& path, cmOutputConverter::OutputFormat format,
bool forceFullPaths)
2012-04-19 19:04:21 +03:00
{
2016-10-30 18:24:19 +01:00
if (forceFullPaths) {
return this->ConvertToOutputFormat(cmSystemTools::CollapseFullPath(path),
format);
}
return this->ConvertToOutputFormat(
this->ConvertToRelativePath(this->GetBinaryDirectory(), path), format);
2012-04-19 19:04:21 +03:00
}
// Private methods.
cmGeneratedFileStream& cmLocalNinjaGenerator::GetBuildFileStream() const
{
return *this->GetGlobalNinjaGenerator()->GetBuildFileStream();
}
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()
{
// For the build file.
this->WriteProjectHeader(this->GetBuildFileStream());
2015-04-27 22:25:09 +02:00
this->WriteNinjaRequiredVersion(this->GetBuildFileStream());
2012-04-19 19:04:21 +03:00
this->WriteNinjaFilesInclusion(this->GetBuildFileStream());
// For the rule file.
this->WriteProjectHeader(this->GetRulesFileStream());
}
void cmLocalNinjaGenerator::WriteProjectHeader(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
2016-07-09 11:21:54 +02:00
os << "# Project: " << this->GetProjectName() << std::endl
<< "# Configuration: " << this->ConfigName << std::endl;
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
2015-11-17 17:22:37 +01:00
std::string requiredVersion =
2016-07-09 11:21:54 +02:00
this->GetGlobalNinjaGenerator()->RequiredNinjaVersion();
2015-04-27 22:25:09 +02:00
// Ninja generator uses the 'console' pool if available (>= 1.5)
2016-07-09 11:21:54 +02:00
if (this->GetGlobalNinjaGenerator()->SupportsConsolePool()) {
2015-11-17 17:22:37 +01:00
requiredVersion =
this->GetGlobalNinjaGenerator()->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 =
this->GetGlobalNinjaGenerator()->RequiredNinjaVersionForManifestRestat();
}
2016-07-09 11:21:54 +02:00
cmGlobalNinjaGenerator::WriteComment(
os, "Minimal version of Ninja required by this file");
os << "ninja_required_version = " << requiredVersion << std::endl
<< std::endl;
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);
2016-07-09 11:21:54 +02:00
const char* jobpools =
this->GetCMakeInstance()->GetState()->GetGlobalProperty("JOB_POOLS");
2018-04-23 21:13:27 +02:00
if (!jobpools) {
jobpools = this->GetMakefile()->GetDefinition("CMAKE_JOB_POOLS");
}
2016-07-09 11:21:54 +02:00
if (jobpools) {
cmGlobalNinjaGenerator::WriteComment(
os, "Pools defined by global property JOB_POOLS");
2014-08-03 19:52:23 +02:00
std::vector<std::string> pools;
cmSystemTools::ExpandListArgument(jobpools, pools);
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) {
2014-08-03 19:52:23 +02:00
os << "pool " << pool.substr(0, eq) << std::endl;
os << " depth = " << jobs << std::endl;
os << std::endl;
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
cmSystemTools::Error("Invalid pool defined by property 'JOB_POOLS': ",
pool.c_str());
}
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2012-04-19 19:04:21 +03:00
void cmLocalNinjaGenerator::WriteNinjaFilesInclusion(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
2016-07-09 11:21:54 +02:00
os << "# Include auxiliary files.\n"
<< "\n";
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";
}
void cmLocalNinjaGenerator::WriteProcessedMakefile(std::ostream& os)
{
cmGlobalNinjaGenerator::WriteDivider(os);
2016-07-09 11:21:54 +02:00
os << "# Write statements declared in CMakeLists.txt:" << std::endl
<< "# " << this->Makefile->GetDefinition("CMAKE_CURRENT_LIST_FILE")
<< std::endl;
2016-10-30 18:24:19 +01:00
if (this->IsRootMakefile()) {
2012-04-19 19:04:21 +03:00
os << "# Which is the root file." << std::endl;
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
cmGlobalNinjaGenerator::WriteDivider(os);
os << std::endl;
}
2016-07-09 11:21:54 +02:00
void cmLocalNinjaGenerator::AppendTargetOutputs(cmGeneratorTarget* target,
cmNinjaDeps& outputs)
2012-04-19 19:04:21 +03:00
{
this->GetGlobalNinjaGenerator()->AppendTargetOutputs(target, outputs);
}
2016-07-09 11:21:54 +02:00
void cmLocalNinjaGenerator::AppendTargetDepends(cmGeneratorTarget* target,
2017-07-20 19:35:53 +02:00
cmNinjaDeps& outputs,
cmNinjaTargetDepends depends)
2012-04-19 19:04:21 +03:00
{
2017-07-20 19:35:53 +02:00
this->GetGlobalNinjaGenerator()->AppendTargetDepends(target, outputs,
depends);
2012-04-19 19:04:21 +03:00
}
2015-04-27 22:25:09 +02:00
void cmLocalNinjaGenerator::AppendCustomCommandDeps(
2016-07-09 11:21:54 +02:00
cmCustomCommandGenerator const& ccg, cmNinjaDeps& ninjaDeps)
2012-04-19 19:04:21 +03:00
{
2016-07-09 11:21:54 +02:00
const std::vector<std::string>& deps = ccg.GetDepends();
2018-01-26 17:06:56 +01:00
for (std::string const& i : deps) {
2012-04-19 19:04:21 +03:00
std::string dep;
2018-01-26 17:06:56 +01:00
if (this->GetRealDependency(i, this->GetConfigName(), 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(
std::vector<std::string> const& cmdLines, std::string const& customStep,
cmGeneratorTarget const* target) const
{
std::string scriptPath;
if (target) {
scriptPath = target->GetSupportDirectory();
} else {
scriptPath = this->GetCurrentBinaryDirectory();
scriptPath += cmake::GetCMakeFilesDirectory();
}
cmSystemTools::MakeDirectory(scriptPath);
scriptPath += '/';
scriptPath += customStep;
#ifdef _WIN32
scriptPath += ".bat";
#else
scriptPath += ".sh";
#endif
cmsys::ofstream script(scriptPath.c_str());
#ifndef _WIN32
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
script << cmd << " || exit /b" << '\n';
#else
script << cmd << '\n';
#endif
}
return scriptPath;
}
2012-04-19 19:04:21 +03:00
std::string cmLocalNinjaGenerator::BuildCommandLine(
2018-08-09 18:06:22 +02:00
std::vector<std::string> const& cmdLines, std::string const& customStep,
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) {
std::string const scriptPath =
this->WriteCommandScript(cmdLines, customStep, target);
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-04-19 19:04:21 +03:00
for (std::vector<std::string>::const_iterator li = cmdLines.begin();
2014-08-03 19:52:23 +02:00
li != cmdLines.end(); ++li)
2012-06-27 20:52:58 +03:00
#ifdef _WIN32
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
} else if (cmdLines.size() > 1) {
2014-08-03 19:52:23 +02:00
cmd << "cmd.exe /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
}
if (cmdLines.size() > 1) {
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
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
{
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) {
2014-08-03 19:52:23 +02:00
cmdLines.push_back(launcher +
2016-07-09 11:21:54 +02:00
this->ConvertToOutputFormat(ccg.GetCommand(i),
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(
cmCustomCommand const* cc, const cmNinjaDeps& orderOnlyDeps)
2012-04-19 19:04:21 +03:00
{
2016-10-30 18:24:19 +01:00
if (this->GetGlobalNinjaGenerator()->SeenCustomCommand(cc)) {
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
2015-11-17 17:22:37 +01:00
cmCustomCommandGenerator ccg(*cc, this->GetConfigName(), this);
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
const std::vector<std::string>& outputs = ccg.GetOutputs();
const std::vector<std::string>& byproducts = ccg.GetByproducts();
cmNinjaDeps ninjaOutputs(outputs.size() + byproducts.size()), ninjaDeps;
2012-04-19 19:04:21 +03:00
2016-03-13 13:35:51 +01:00
bool symbolic = false;
for (std::vector<std::string>::const_iterator o = outputs.begin();
2016-07-09 11:21:54 +02:00
!symbolic && o != outputs.end(); ++o) {
if (cmSourceFile* sf = this->Makefile->GetSource(*o)) {
2016-03-13 13:35:51 +01:00
symbolic = sf->GetPropertyAsBool("SYMBOLIC");
}
2016-07-09 11:21:54 +02:00
}
2016-03-13 13:35:51 +01:00
2015-04-27 22:25:09 +02:00
#if 0
2018-08-09 18:06:22 +02:00
# error TODO: Once CC in an ExternalProject target must provide the \
2015-04-27 22:25:09 +02:00
file of each imported target that has an add_dependencies pointing \
at us. How to know which ExternalProject step actually provides it?
#endif
2016-07-09 11:21:54 +02:00
std::transform(outputs.begin(), outputs.end(), ninjaOutputs.begin(),
2015-11-17 17:22:37 +01:00
this->GetGlobalNinjaGenerator()->MapToNinjaPath());
2015-04-27 22:25:09 +02:00
std::transform(byproducts.begin(), byproducts.end(),
2015-11-17 17:22:37 +01:00
ninjaOutputs.begin() + outputs.size(),
this->GetGlobalNinjaGenerator()->MapToNinjaPath());
2015-04-27 22:25:09 +02:00
this->AppendCustomCommandDeps(ccg, ninjaDeps);
2012-04-19 19:04:21 +03:00
2018-01-26 17:06:56 +01:00
for (std::string const& ninjaOutput : ninjaOutputs) {
this->GetGlobalNinjaGenerator()->SeenCustomCommandOutput(ninjaOutput);
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
std::vector<std::string> cmdLines;
2015-04-27 22:25:09 +02:00
this->AppendCustomCommandLines(ccg, cmdLines);
2012-04-19 19:04:21 +03:00
if (cmdLines.empty()) {
2013-11-03 12:27:13 +02:00
this->GetGlobalNinjaGenerator()->WritePhonyBuild(
this->GetBuildFileStream(),
2016-07-09 11:21:54 +02:00
"Phony custom command for " + ninjaOutputs[0], ninjaOutputs, ninjaDeps,
cmNinjaDeps(), orderOnlyDeps, cmNinjaVars());
2012-04-19 19:04:21 +03:00
} else {
2018-08-09 18:06:22 +02:00
std::string customStep = cmSystemTools::GetFilenameName(ninjaOutputs[0]);
// Hash full path to make unique.
customStep += '-';
cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
customStep += hash.HashString(ninjaOutputs[0]).substr(0, 7);
2012-04-19 19:04:21 +03:00
this->GetGlobalNinjaGenerator()->WriteCustomCommandBuild(
2018-08-09 18:06:22 +02:00
this->BuildCommandLine(cmdLines, customStep),
this->ConstructComment(ccg), "Custom command for " + ninjaOutputs[0],
cc->GetDepfile(), cc->GetUsesTerminal(),
2016-07-09 11:21:54 +02:00
/*restat*/ !symbolic || !byproducts.empty(), ninjaOutputs, ninjaDeps,
2012-04-19 19:04:21 +03:00
orderOnlyDeps);
}
}
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
}
void cmLocalNinjaGenerator::WriteCustomCommandBuildStatements()
{
2018-01-26 17:06:56 +01:00
for (cmCustomCommand const* customCommand : this->CustomCommands) {
CustomCommandTargetMap::iterator i =
this->CustomCommandTargets.find(customCommand);
2016-07-09 11:21:54 +02:00
assert(i != this->CustomCommandTargets.end());
2012-04-19 19:04:21 +03:00
// 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.
2016-03-13 13:35:51 +01:00
std::set<cmGeneratorTarget*>::iterator j = i->second.begin();
2012-04-19 19:04:21 +03:00
assert(j != i->second.end());
std::vector<std::string> ccTargetDeps;
2016-10-30 18:24:19 +01:00
this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(*j,
ccTargetDeps);
2012-04-19 19:04:21 +03:00
std::sort(ccTargetDeps.begin(), ccTargetDeps.end());
++j;
for (; j != i->second.end(); ++j) {
std::vector<std::string> jDeps, depsIntersection;
2016-10-30 18:24:19 +01:00
this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure(*j, jDeps);
2012-04-19 19:04:21 +03:00
std::sort(jDeps.begin(), jDeps.end());
std::set_intersection(ccTargetDeps.begin(), ccTargetDeps.end(),
jDeps.begin(), jDeps.end(),
std::back_inserter(depsIntersection));
ccTargetDeps = depsIntersection;
}
this->WriteCustomCommandBuildStatement(i->first, ccTargetDeps);
}
}
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
{
2017-04-14 19:02:05 +02:00
const char* property_value =
this->Makefile->GetProperty("RULE_LAUNCH_CUSTOM");
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
if (!property_value || !*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()) {
output =
2017-04-14 19:02:05 +02:00
this->ConvertToRelativePath(this->GetCurrentBinaryDirectory(), 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();
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
2017-04-14 19:02:05 +02:00
this->CreateRulePlaceholderExpander());
2018-10-28 12:09:07 +01: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;
}