cmake/Source/cmNinjaUtilityTargetGenerator.cxx

168 lines
5.9 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 "cmNinjaUtilityTargetGenerator.h"
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
2012-04-19 19:04:21 +03:00
#include "cmCustomCommand.h"
2016-07-09 11:21:54 +02:00
#include "cmCustomCommandGenerator.h"
2012-04-19 19:04:21 +03:00
#include "cmGeneratedFileStream.h"
2016-10-30 18:24:19 +01:00
#include "cmGeneratorTarget.h"
2012-04-19 19:04:21 +03:00
#include "cmGlobalNinjaGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmLocalNinjaGenerator.h"
#include "cmNinjaTypes.h"
#include "cmOutputConverter.h"
2020-08-30 11:54:41 +02:00
#include "cmProperty.h"
2012-04-19 19:04:21 +03:00
#include "cmSourceFile.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"
2016-10-30 18:24:19 +01:00
2014-08-03 19:52:23 +02:00
cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator(
2016-07-09 11:21:54 +02:00
cmGeneratorTarget* target)
: cmNinjaTargetGenerator(target)
{
}
2012-04-19 19:04:21 +03:00
2019-11-11 23:01:05 +01:00
cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator() = default;
2012-04-19 19:04:21 +03:00
2020-08-30 11:54:41 +02:00
void cmNinjaUtilityTargetGenerator::Generate(const std::string& config)
2012-04-19 19:04:21 +03:00
{
2019-11-11 23:01:05 +01:00
cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
cmLocalNinjaGenerator* lg = this->GetLocalGenerator();
cmGeneratorTarget* genTarget = this->GetGeneratorTarget();
2020-08-30 11:54:41 +02:00
std::string configDir;
if (genTarget->Target->IsPerConfig()) {
configDir = gg->ConfigDirectory(config);
}
2020-02-01 23:06:01 +01:00
std::string utilCommandName =
2020-08-30 11:54:41 +02:00
cmStrCat(lg->GetCurrentBinaryDirectory(), "/CMakeFiles", configDir, "/",
2020-02-01 23:06:01 +01:00
this->GetTargetName(), ".util");
2016-10-30 18:24:19 +01:00
utilCommandName = this->ConvertToNinjaPath(utilCommandName);
2015-04-27 22:25:09 +02:00
2019-11-11 23:01:05 +01:00
cmNinjaBuild phonyBuild("phony");
2012-04-19 19:04:21 +03:00
std::vector<std::string> commands;
2020-02-01 23:06:01 +01:00
cmNinjaDeps deps;
cmNinjaDeps util_outputs(1, utilCommandName);
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
bool uses_terminal = false;
2019-11-11 23:01:05 +01:00
{
std::array<std::vector<cmCustomCommand> const*, 2> const cmdLists = {
{ &genTarget->GetPreBuildCommands(), &genTarget->GetPostBuildCommands() }
};
for (std::vector<cmCustomCommand> const* cmdList : cmdLists) {
for (cmCustomCommand const& ci : *cmdList) {
2020-08-30 11:54:41 +02:00
cmCustomCommandGenerator ccg(ci, config, lg);
lg->AppendCustomCommandDeps(ccg, deps, config);
2019-11-11 23:01:05 +01:00
lg->AppendCustomCommandLines(ccg, commands);
std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
std::transform(ccByproducts.begin(), ccByproducts.end(),
std::back_inserter(util_outputs), MapToNinjaPath());
if (ci.GetUsesTerminal()) {
uses_terminal = true;
}
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
}
}
2019-11-11 23:01:05 +01:00
{
std::vector<cmSourceFile*> sources;
genTarget->GetSourceFiles(sources, config);
for (cmSourceFile const* source : sources) {
if (cmCustomCommand const* cc = source->GetCustomCommand()) {
2020-08-30 11:54:41 +02:00
cmCustomCommandGenerator ccg(*cc, config, lg);
2019-11-11 23:01:05 +01:00
lg->AddCustomCommandTarget(cc, genTarget);
// Depend on all custom command outputs.
const std::vector<std::string>& ccOutputs = ccg.GetOutputs();
const std::vector<std::string>& ccByproducts = ccg.GetByproducts();
std::transform(ccOutputs.begin(), ccOutputs.end(),
std::back_inserter(deps), MapToNinjaPath());
std::transform(ccByproducts.begin(), ccByproducts.end(),
std::back_inserter(deps), MapToNinjaPath());
}
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
std::string outputConfig;
if (genTarget->Target->IsPerConfig()) {
outputConfig = config;
}
lg->AppendTargetOutputs(genTarget, phonyBuild.Outputs, outputConfig);
if (genTarget->Target->GetType() != cmStateEnums::GLOBAL_TARGET) {
lg->AppendTargetOutputs(genTarget, gg->GetByproductsForCleanTarget(),
config);
2020-10-15 20:05:27 +02:00
std::copy(util_outputs.begin(), util_outputs.end(),
std::back_inserter(gg->GetByproductsForCleanTarget()));
2020-08-30 11:54:41 +02:00
}
lg->AppendTargetDepends(genTarget, deps, config, config);
2012-04-19 19:04:21 +03:00
if (commands.empty()) {
2019-11-11 23:01:05 +01:00
phonyBuild.Comment = "Utility command for " + this->GetTargetName();
phonyBuild.ExplicitDeps = std::move(deps);
2020-08-30 11:54:41 +02:00
gg->WriteBuild(this->GetCommonFileStream(), phonyBuild);
2012-04-19 19:04:21 +03:00
} else {
2019-11-11 23:01:05 +01:00
std::string command =
lg->BuildCommandLine(commands, "utility", this->GeneratorTarget);
2012-04-19 19:04:21 +03:00
std::string desc;
2020-08-30 11:54:41 +02:00
cmProp echoStr = genTarget->GetProperty("EchoString");
2016-10-30 18:24:19 +01:00
if (echoStr) {
2020-08-30 11:54:41 +02:00
desc = *echoStr;
2016-10-30 18:24:19 +01:00
} else {
2012-04-19 19:04:21 +03:00
desc = "Running utility command for " + this->GetTargetName();
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
// TODO: fix problematic global targets. For now, search and replace the
// makefile vars.
cmSystemTools::ReplaceString(
2016-07-09 11:21:54 +02:00
command, "$(CMAKE_SOURCE_DIR)",
2019-11-11 23:01:05 +01:00
lg->ConvertToOutputFormat(lg->GetSourceDirectory(),
cmOutputConverter::SHELL));
2012-04-19 19:04:21 +03:00
cmSystemTools::ReplaceString(
2016-07-09 11:21:54 +02:00
command, "$(CMAKE_BINARY_DIR)",
2019-11-11 23:01:05 +01:00
lg->ConvertToOutputFormat(lg->GetBinaryDirectory(),
cmOutputConverter::SHELL));
2012-04-19 19:04:21 +03:00
cmSystemTools::ReplaceString(command, "$(ARGS)", "");
2020-08-30 11:54:41 +02:00
command = gg->ExpandCFGIntDir(command, config);
2012-04-19 19:04:21 +03:00
2016-10-30 18:24:19 +01:00
if (command.find('$') != std::string::npos) {
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
2018-01-26 17:06:56 +01:00
for (std::string const& util_output : util_outputs) {
2019-11-11 23:01:05 +01:00
gg->SeenCustomCommandOutput(util_output);
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2020-08-30 11:54:41 +02:00
std::string ccConfig;
if (genTarget->Target->IsPerConfig() &&
genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) {
ccConfig = config;
}
2019-11-11 23:01:05 +01:00
gg->WriteCustomCommandBuild(command, desc,
"Utility command for " + this->GetTargetName(),
/*depfile*/ "", /*job_pool*/ "", uses_terminal,
2020-08-30 11:54:41 +02:00
/*restat*/ true, util_outputs, ccConfig, deps);
2016-07-09 11:21:54 +02:00
2019-11-11 23:01:05 +01:00
phonyBuild.ExplicitDeps.push_back(utilCommandName);
2020-08-30 11:54:41 +02:00
gg->WriteBuild(this->GetCommonFileStream(), phonyBuild);
2012-04-19 19:04:21 +03:00
}
2019-11-11 23:01:05 +01:00
// Find ADDITIONAL_CLEAN_FILES
2020-08-30 11:54:41 +02:00
this->AdditionalCleanFiles(config);
2019-11-11 23:01:05 +01:00
2016-10-30 18:24:19 +01:00
// Add an alias for the logical target name regardless of what directory
// contains it. Skip this for GLOBAL_TARGET because they are meant to
// be per-directory and have one at the top-level anyway.
2019-11-11 23:01:05 +01:00
if (genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) {
2020-08-30 11:54:41 +02:00
gg->AddTargetAlias(this->GetTargetName(), genTarget, config);
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
}