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