2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2011-11-11 05:00:49 +00:00
|
|
|
#include "cmNinjaUtilityTargetGenerator.h"
|
2016-04-29 13:40:20 +00:00
|
|
|
|
2011-11-11 05:00:49 +00:00
|
|
|
#include "cmCustomCommand.h"
|
2016-04-29 14:53:13 +00:00
|
|
|
#include "cmCustomCommandGenerator.h"
|
2011-11-11 05:00:49 +00:00
|
|
|
#include "cmGeneratedFileStream.h"
|
2016-08-23 22:29:15 +00:00
|
|
|
#include "cmGeneratorTarget.h"
|
2011-11-11 05:00:49 +00:00
|
|
|
#include "cmGlobalNinjaGenerator.h"
|
2016-08-23 22:29:15 +00:00
|
|
|
#include "cmLocalNinjaGenerator.h"
|
2011-11-11 05:00:49 +00:00
|
|
|
#include "cmMakefile.h"
|
2016-08-23 22:29:15 +00:00
|
|
|
#include "cmNinjaTypes.h"
|
|
|
|
#include "cmOutputConverter.h"
|
2011-11-11 05:00:49 +00:00
|
|
|
#include "cmSourceFile.h"
|
2016-10-18 19:28:48 +00:00
|
|
|
#include "cmStateTypes.h"
|
2016-08-23 22:29:15 +00:00
|
|
|
#include "cmSystemTools.h"
|
|
|
|
#include "cmake.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2011-11-11 05:00:49 +00:00
|
|
|
|
2012-10-10 19:32:37 +00:00
|
|
|
cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator(
|
2016-05-16 14:34:04 +00:00
|
|
|
cmGeneratorTarget* target)
|
|
|
|
: cmNinjaTargetGenerator(target)
|
|
|
|
{
|
|
|
|
}
|
2011-11-11 05:00:49 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator()
|
|
|
|
{
|
|
|
|
}
|
2011-11-11 05:00:49 +00:00
|
|
|
|
|
|
|
void cmNinjaUtilityTargetGenerator::Generate()
|
|
|
|
{
|
2016-08-05 19:55:32 +00:00
|
|
|
std::string utilCommandName =
|
|
|
|
this->GetLocalGenerator()->GetCurrentBinaryDirectory();
|
|
|
|
utilCommandName += cmake::GetCMakeFilesDirectory();
|
|
|
|
utilCommandName += "/";
|
2014-11-13 23:54:52 +00:00
|
|
|
utilCommandName += this->GetTargetName() + ".util";
|
2016-08-05 19:55:32 +00:00
|
|
|
utilCommandName = this->ConvertToNinjaPath(utilCommandName);
|
2014-11-13 23:54:52 +00:00
|
|
|
|
2011-11-11 05:00:49 +00:00
|
|
|
std::vector<std::string> commands;
|
2014-11-13 23:54:52 +00:00
|
|
|
cmNinjaDeps deps, outputs, util_outputs(1, utilCommandName);
|
2011-11-11 05:00:49 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
const std::vector<cmCustomCommand>* cmdLists[2] = {
|
2015-10-22 22:42:58 +00:00
|
|
|
&this->GetGeneratorTarget()->GetPreBuildCommands(),
|
|
|
|
&this->GetGeneratorTarget()->GetPostBuildCommands()
|
2011-11-11 05:00:49 +00:00
|
|
|
};
|
|
|
|
|
2014-11-05 20:42:18 +00:00
|
|
|
bool uses_terminal = false;
|
|
|
|
|
2011-11-11 05:00:49 +00:00
|
|
|
for (unsigned i = 0; i != 2; ++i) {
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<cmCustomCommand>::const_iterator ci =
|
|
|
|
cmdLists[i]->begin();
|
|
|
|
ci != cmdLists[i]->end(); ++ci) {
|
2014-03-10 19:47:19 +00:00
|
|
|
cmCustomCommandGenerator ccg(*ci, this->GetConfigName(),
|
2015-07-25 15:52:10 +00:00
|
|
|
this->GetLocalGenerator());
|
2014-03-10 19:47:19 +00:00
|
|
|
this->GetLocalGenerator()->AppendCustomCommandDeps(ccg, deps);
|
|
|
|
this->GetLocalGenerator()->AppendCustomCommandLines(ccg, commands);
|
2014-11-13 23:54:52 +00:00
|
|
|
std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
|
|
|
|
std::transform(ccByproducts.begin(), ccByproducts.end(),
|
|
|
|
std::back_inserter(util_outputs), MapToNinjaPath());
|
2016-06-10 16:36:24 +00:00
|
|
|
if (ci->GetUsesTerminal()) {
|
2014-11-05 20:42:18 +00:00
|
|
|
uses_terminal = true;
|
2016-06-10 16:36:24 +00:00
|
|
|
}
|
2011-11-11 05:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:22:57 +00:00
|
|
|
std::vector<cmSourceFile*> sources;
|
2016-05-16 14:34:04 +00:00
|
|
|
std::string config =
|
|
|
|
this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
|
2015-08-29 15:51:15 +00:00
|
|
|
this->GetGeneratorTarget()->GetSourceFiles(sources, config);
|
2016-05-16 14:34:04 +00:00
|
|
|
for (std::vector<cmSourceFile*>::const_iterator source = sources.begin();
|
|
|
|
source != sources.end(); ++source) {
|
|
|
|
if (cmCustomCommand* cc = (*source)->GetCustomCommand()) {
|
2014-03-10 19:47:19 +00:00
|
|
|
cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
|
2015-07-25 15:52:10 +00:00
|
|
|
this->GetLocalGenerator());
|
2016-05-16 14:34:04 +00:00
|
|
|
this->GetLocalGenerator()->AddCustomCommandTarget(
|
|
|
|
cc, this->GetGeneratorTarget());
|
2011-11-11 05:00:49 +00:00
|
|
|
|
|
|
|
// Depend on all custom command outputs.
|
2014-03-10 19:47:19 +00:00
|
|
|
const std::vector<std::string>& ccOutputs = ccg.GetOutputs();
|
2014-11-13 23:54:52 +00:00
|
|
|
const std::vector<std::string>& ccByproducts = ccg.GetByproducts();
|
2012-02-05 01:48:08 +00:00
|
|
|
std::transform(ccOutputs.begin(), ccOutputs.end(),
|
2011-11-11 05:00:49 +00:00
|
|
|
std::back_inserter(deps), MapToNinjaPath());
|
2014-11-13 23:54:52 +00:00
|
|
|
std::transform(ccByproducts.begin(), ccByproducts.end(),
|
|
|
|
std::back_inserter(deps), MapToNinjaPath());
|
2011-11-11 05:00:49 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2011-11-11 05:00:49 +00:00
|
|
|
|
2015-10-18 21:13:50 +00:00
|
|
|
this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(),
|
|
|
|
outputs);
|
|
|
|
this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
|
|
|
|
deps);
|
2011-11-11 05:00:49 +00:00
|
|
|
|
|
|
|
if (commands.empty()) {
|
2016-05-16 14:34:04 +00:00
|
|
|
this->GetGlobalGenerator()->WritePhonyBuild(
|
|
|
|
this->GetBuildFileStream(),
|
|
|
|
"Utility command for " + this->GetTargetName(), outputs, deps);
|
2011-11-11 05:00:49 +00:00
|
|
|
} else {
|
|
|
|
std::string command =
|
|
|
|
this->GetLocalGenerator()->BuildCommandLine(commands);
|
2016-05-16 14:34:04 +00:00
|
|
|
const char* echoStr =
|
|
|
|
this->GetGeneratorTarget()->GetProperty("EchoString");
|
2011-11-11 05:00:49 +00:00
|
|
|
std::string desc;
|
2016-06-10 16:36:24 +00:00
|
|
|
if (echoStr) {
|
2011-11-11 05:00:49 +00:00
|
|
|
desc = echoStr;
|
2016-06-10 16:36:24 +00:00
|
|
|
} else {
|
2011-11-11 05:00:49 +00:00
|
|
|
desc = "Running utility command for " + this->GetTargetName();
|
2016-06-10 16:36:24 +00:00
|
|
|
}
|
2011-11-11 05:00:49 +00:00
|
|
|
|
|
|
|
// TODO: fix problematic global targets. For now, search and replace the
|
|
|
|
// makefile vars.
|
2012-03-17 20:55:09 +00:00
|
|
|
cmSystemTools::ReplaceString(
|
2016-05-16 14:34:04 +00:00
|
|
|
command, "$(CMAKE_SOURCE_DIR)",
|
|
|
|
this->GetLocalGenerator()
|
|
|
|
->ConvertToOutputFormat(
|
|
|
|
this->GetLocalGenerator()->GetSourceDirectory(),
|
2016-05-19 21:11:40 +00:00
|
|
|
cmOutputConverter::SHELL)
|
2016-05-16 14:34:04 +00:00
|
|
|
.c_str());
|
2012-03-17 20:55:09 +00:00
|
|
|
cmSystemTools::ReplaceString(
|
2016-05-16 14:34:04 +00:00
|
|
|
command, "$(CMAKE_BINARY_DIR)",
|
|
|
|
this->GetLocalGenerator()
|
|
|
|
->ConvertToOutputFormat(
|
|
|
|
this->GetLocalGenerator()->GetBinaryDirectory(),
|
2016-05-19 21:11:40 +00:00
|
|
|
cmOutputConverter::SHELL)
|
2016-05-16 14:34:04 +00:00
|
|
|
.c_str());
|
2011-11-11 05:00:49 +00:00
|
|
|
cmSystemTools::ReplaceString(command, "$(ARGS)", "");
|
|
|
|
|
2016-06-10 16:36:24 +00:00
|
|
|
if (command.find('$') != std::string::npos) {
|
2011-11-11 05:00:49 +00:00
|
|
|
return;
|
2016-06-10 16:36:24 +00:00
|
|
|
}
|
2011-11-11 05:00:49 +00:00
|
|
|
|
2016-05-16 14:34:04 +00:00
|
|
|
for (cmNinjaDeps::const_iterator oi = util_outputs.begin(),
|
|
|
|
oe = util_outputs.end();
|
|
|
|
oi != oe; ++oi) {
|
2014-11-13 23:54:52 +00:00
|
|
|
this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2011-11-11 05:00:49 +00:00
|
|
|
|
|
|
|
this->GetGlobalGenerator()->WriteCustomCommandBuild(
|
2016-05-16 14:34:04 +00:00
|
|
|
command, desc, "Utility command for " + this->GetTargetName(),
|
2016-08-05 12:39:31 +00:00
|
|
|
/*depfile*/ "", uses_terminal,
|
2016-05-16 14:34:04 +00:00
|
|
|
/*restat*/ true, util_outputs, deps);
|
|
|
|
|
|
|
|
this->GetGlobalGenerator()->WritePhonyBuild(
|
|
|
|
this->GetBuildFileStream(), "", outputs,
|
|
|
|
cmNinjaDeps(1, utilCommandName));
|
2011-11-11 05:00:49 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 19:55:32 +00: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.
|
2016-10-18 19:28:46 +00:00
|
|
|
if (this->GetGeneratorTarget()->GetType() != cmStateEnums::GLOBAL_TARGET) {
|
2016-08-05 19:55:32 +00:00
|
|
|
this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
|
|
|
|
this->GetGeneratorTarget());
|
|
|
|
}
|
2011-11-11 05:00:49 +00:00
|
|
|
}
|