cmake/Source/cmNinjaTargetGenerator.cxx

735 lines
27 KiB
C++
Raw Normal View History

2012-04-19 19:04:21 +03: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.
============================================================================*/
#include "cmNinjaTargetGenerator.h"
2016-07-09 11:21:54 +02:00
#include "cmAlgorithms.h"
#include "cmComputeLinkInformation.h"
#include "cmCustomCommandGenerator.h"
2012-04-19 19:04:21 +03:00
#include "cmGeneratedFileStream.h"
#include "cmGeneratorTarget.h"
2016-07-09 11:21:54 +02:00
#include "cmGlobalNinjaGenerator.h"
#include "cmLocalNinjaGenerator.h"
#include "cmMakefile.h"
2012-04-19 19:04:21 +03:00
#include "cmNinjaNormalTargetGenerator.h"
#include "cmNinjaUtilityTargetGenerator.h"
#include "cmSourceFile.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
2012-04-19 19:04:21 +03:00
#include <algorithm>
2016-07-09 11:21:54 +02:00
cmNinjaTargetGenerator* cmNinjaTargetGenerator::New(cmGeneratorTarget* target)
2012-04-19 19:04:21 +03:00
{
2016-07-09 11:21:54 +02:00
switch (target->GetType()) {
case cmState::EXECUTABLE:
case cmState::SHARED_LIBRARY:
case cmState::STATIC_LIBRARY:
case cmState::MODULE_LIBRARY:
case cmState::OBJECT_LIBRARY:
return new cmNinjaNormalTargetGenerator(target);
case cmState::UTILITY:
return new cmNinjaUtilityTargetGenerator(target);
;
case cmState::GLOBAL_TARGET: {
// We only want to process global targets that live in the home
// (i.e. top-level) directory. CMake creates copies of these targets
// in every directory, which we don't need.
if (strcmp(target->GetLocalGenerator()->GetCurrentSourceDirectory(),
target->GetLocalGenerator()->GetSourceDirectory()) == 0)
return new cmNinjaUtilityTargetGenerator(target);
// else fallthrough
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
default:
return 0;
}
2012-04-19 19:04:21 +03:00
}
2015-11-17 17:22:37 +01:00
cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
2016-07-09 11:21:54 +02:00
: cmCommonTargetGenerator(cmOutputConverter::HOME_OUTPUT, target)
, MacOSXContentGenerator(0)
, OSXBundleGenerator(0)
, MacContentFolders()
, LocalGenerator(
static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
, Objects()
2012-04-19 19:04:21 +03:00
{
2012-08-04 10:26:08 +03:00
MacOSXContentGenerator = new MacOSXContentGeneratorType(this);
2012-04-19 19:04:21 +03:00
}
cmNinjaTargetGenerator::~cmNinjaTargetGenerator()
{
2012-08-04 10:26:08 +03:00
delete this->MacOSXContentGenerator;
2012-04-19 19:04:21 +03:00
}
cmGeneratedFileStream& cmNinjaTargetGenerator::GetBuildFileStream() const
{
return *this->GetGlobalGenerator()->GetBuildFileStream();
}
cmGeneratedFileStream& cmNinjaTargetGenerator::GetRulesFileStream() const
{
return *this->GetGlobalGenerator()->GetRulesFileStream();
}
cmGlobalNinjaGenerator* cmNinjaTargetGenerator::GetGlobalGenerator() const
{
return this->LocalGenerator->GetGlobalNinjaGenerator();
}
2015-08-17 11:37:30 +02:00
std::string cmNinjaTargetGenerator::LanguageCompilerRule(
const std::string& lang) const
{
return lang + "_COMPILER__" +
2016-03-13 13:35:51 +01:00
cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName());
2015-08-17 11:37:30 +02:00
}
2016-07-09 11:21:54 +02:00
std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget()
2015-04-27 22:25:09 +02:00
{
return "cmake_order_depends_target_" + this->GetTargetName();
}
2012-04-19 19:04:21 +03:00
// TODO: Most of the code is picked up from
// void cmMakefileExecutableTargetGenerator::WriteExecutableRule(bool relink),
// void cmMakefileTargetGenerator::WriteTargetLanguageFlags()
// Refactor it.
2016-07-09 11:21:54 +02:00
std::string cmNinjaTargetGenerator::ComputeFlagsForObject(
cmSourceFile const* source, const std::string& language)
2012-04-19 19:04:21 +03:00
{
2015-11-17 17:22:37 +01:00
std::string flags = this->GetFlags(language);
2014-08-03 19:52:23 +02:00
2015-11-17 17:22:37 +01:00
// Add Fortran format flags.
2016-07-09 11:21:54 +02:00
if (language == "Fortran") {
2015-11-17 17:22:37 +01:00
this->AppendFortranFormatFlags(flags, *source);
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2014-08-03 19:52:23 +02:00
// Add source file specific flags.
this->LocalGenerator->AppendFlags(flags,
2016-07-09 11:21:54 +02:00
source->GetProperty("COMPILE_FLAGS"));
2012-04-19 19:04:21 +03:00
2014-08-03 19:52:23 +02:00
return flags;
}
2013-03-16 19:13:01 +02:00
2015-11-17 17:22:37 +01:00
void cmNinjaTargetGenerator::AddIncludeFlags(std::string& languageFlags,
std::string const& language)
{
std::vector<std::string> includes;
2016-07-09 11:21:54 +02:00
this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
language, this->GetConfigName());
2015-11-17 17:22:37 +01:00
// Add include directory flags.
2016-07-09 11:21:54 +02:00
std::string includeFlags = this->LocalGenerator->GetIncludeFlags(
includes, this->GeneratorTarget, language,
language == "RC" ? true : false, // full include paths for RC
// needed by cmcldeps
false, this->GetConfigName());
2015-11-17 17:22:37 +01:00
if (this->GetGlobalGenerator()->IsGCCOnWindows())
2016-07-09 11:21:54 +02:00
std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
2015-11-17 17:22:37 +01:00
this->LocalGenerator->AppendFlags(languageFlags, includeFlags);
}
2015-04-27 22:25:09 +02:00
bool cmNinjaTargetGenerator::NeedDepTypeMSVC(const std::string& lang) const
2014-08-03 19:52:23 +02:00
{
2016-07-09 11:21:54 +02:00
return strcmp(this->GetMakefile()->GetSafeDefinition("CMAKE_NINJA_DEPTYPE_" +
lang),
"msvc") == 0;
2012-04-19 19:04:21 +03:00
}
// TODO: Refactor with
// void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
2016-07-09 11:21:54 +02:00
std::string cmNinjaTargetGenerator::ComputeDefines(cmSourceFile const* source,
const std::string& language)
2012-04-19 19:04:21 +03:00
{
2013-03-16 19:13:01 +02:00
std::set<std::string> defines;
2016-07-09 11:21:54 +02:00
this->LocalGenerator->AppendDefines(
defines, source->GetProperty("COMPILE_DEFINITIONS"));
2012-04-19 19:04:21 +03:00
{
2016-07-09 11:21:54 +02:00
std::string defPropName = "COMPILE_DEFINITIONS_";
defPropName += cmSystemTools::UpperCase(this->GetConfigName());
this->LocalGenerator->AppendDefines(defines,
source->GetProperty(defPropName));
2012-04-19 19:04:21 +03:00
}
2015-11-17 17:22:37 +01:00
std::string definesString = this->GetDefines(language);
2016-07-09 11:21:54 +02:00
this->LocalGenerator->JoinDefines(defines, definesString, language);
2013-03-16 19:13:01 +02:00
return definesString;
2012-04-19 19:04:21 +03:00
}
cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps() const
{
// Static libraries never depend on other targets for linking.
2016-03-13 13:35:51 +01:00
if (this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
this->GeneratorTarget->GetType() == cmState::OBJECT_LIBRARY)
2012-04-19 19:04:21 +03:00
return cmNinjaDeps();
cmComputeLinkInformation* cli =
2015-11-17 17:22:37 +01:00
this->GeneratorTarget->GetLinkInformation(this->GetConfigName());
2016-07-09 11:21:54 +02:00
if (!cli)
2012-04-19 19:04:21 +03:00
return cmNinjaDeps();
2016-07-09 11:21:54 +02:00
const std::vector<std::string>& deps = cli->GetDepends();
2012-04-19 19:04:21 +03:00
cmNinjaDeps result(deps.size());
std::transform(deps.begin(), deps.end(), result.begin(), MapToNinjaPath());
// Add a dependency on the link definitions file, if any.
2016-07-09 11:21:54 +02:00
if (this->ModuleDefinitionFile) {
result.push_back(
this->ConvertToNinjaPath(this->ModuleDefinitionFile->GetFullPath()));
}
2015-11-17 17:22:37 +01:00
// Add a dependency on user-specified manifest files, if any.
std::vector<cmSourceFile const*> manifest_srcs;
this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
for (std::vector<cmSourceFile const*>::iterator mi = manifest_srcs.begin();
2016-07-09 11:21:54 +02:00
mi != manifest_srcs.end(); ++mi) {
2015-11-17 17:22:37 +01:00
result.push_back(this->ConvertToNinjaPath((*mi)->GetFullPath()));
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Add user-specified dependencies.
2016-03-13 13:35:51 +01:00
if (const char* linkDepends =
2016-07-09 11:21:54 +02:00
this->GeneratorTarget->GetProperty("LINK_DEPENDS")) {
2015-11-17 17:22:37 +01:00
std::vector<std::string> linkDeps;
cmSystemTools::ExpandListArgument(linkDepends, linkDeps);
std::transform(linkDeps.begin(), linkDeps.end(),
std::back_inserter(result), MapToNinjaPath());
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
return result;
}
2016-07-09 11:21:54 +02:00
std::string cmNinjaTargetGenerator::GetSourceFilePath(
cmSourceFile const* source) const
2012-04-19 19:04:21 +03:00
{
2015-04-27 22:25:09 +02:00
return ConvertToNinjaPath(source->GetFullPath());
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
std::string cmNinjaTargetGenerator::GetObjectFilePath(
cmSourceFile const* source) const
2012-04-19 19:04:21 +03:00
{
std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
2016-07-09 11:21:54 +02:00
if (!path.empty())
2012-04-19 19:04:21 +03:00
path += "/";
2016-07-09 11:21:54 +02:00
std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
2016-03-13 13:35:51 +01:00
path += this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget);
2012-04-19 19:04:21 +03:00
path += "/";
path += objectName;
return path;
}
std::string cmNinjaTargetGenerator::GetTargetOutputDir() const
{
2016-03-13 13:35:51 +01:00
std::string dir = this->GeneratorTarget->GetDirectory(this->GetConfigName());
2015-04-27 22:25:09 +02:00
return ConvertToNinjaPath(dir);
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
std::string cmNinjaTargetGenerator::GetTargetFilePath(
const std::string& name) const
2012-04-19 19:04:21 +03:00
{
std::string path = this->GetTargetOutputDir();
if (path.empty() || path == ".")
return name;
path += "/";
path += name;
return path;
}
std::string cmNinjaTargetGenerator::GetTargetName() const
{
2016-03-13 13:35:51 +01:00
return this->GeneratorTarget->GetName();
2012-04-19 19:04:21 +03:00
}
2013-03-16 19:13:01 +02:00
bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(cmNinjaVars& vars) const
2012-04-19 19:04:21 +03:00
{
2013-03-16 19:13:01 +02:00
cmMakefile* mf = this->GetMakefile();
if (mf->GetDefinition("MSVC_C_ARCHITECTURE_ID") ||
2016-07-09 11:21:54 +02:00
mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID")) {
2013-03-16 19:13:01 +02:00
std::string pdbPath;
2015-04-27 22:25:09 +02:00
std::string compilePdbPath;
2016-07-09 11:21:54 +02:00
if (this->GeneratorTarget->GetType() == cmState::EXECUTABLE ||
this->GeneratorTarget->GetType() == cmState::STATIC_LIBRARY ||
this->GeneratorTarget->GetType() == cmState::SHARED_LIBRARY ||
this->GeneratorTarget->GetType() == cmState::MODULE_LIBRARY) {
2016-03-13 13:35:51 +01:00
pdbPath = this->GeneratorTarget->GetPDBDirectory(this->GetConfigName());
2013-03-16 19:13:01 +02:00
pdbPath += "/";
2015-11-17 17:22:37 +01:00
pdbPath += this->GeneratorTarget->GetPDBName(this->GetConfigName());
2016-07-09 11:21:54 +02:00
}
if (this->GeneratorTarget->GetType() <= cmState::OBJECT_LIBRARY) {
2015-11-17 17:22:37 +01:00
compilePdbPath =
2016-07-09 11:21:54 +02:00
this->GeneratorTarget->GetCompilePDBPath(this->GetConfigName());
if (compilePdbPath.empty()) {
2016-03-13 13:35:51 +01:00
compilePdbPath = this->GeneratorTarget->GetSupportDirectory() + "/";
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2013-03-16 19:13:01 +02:00
vars["TARGET_PDB"] = this->GetLocalGenerator()->ConvertToOutputFormat(
2016-07-09 11:21:54 +02:00
ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
2015-04-27 22:25:09 +02:00
vars["TARGET_COMPILE_PDB"] =
this->GetLocalGenerator()->ConvertToOutputFormat(
2016-07-09 11:21:54 +02:00
ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
2015-04-27 22:25:09 +02:00
2013-03-16 19:13:01 +02:00
EnsureParentDirectoryExists(pdbPath);
2015-04-27 22:25:09 +02:00
EnsureParentDirectoryExists(compilePdbPath);
2013-03-16 19:13:01 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return false;
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language)
2012-04-19 19:04:21 +03:00
{
2012-06-27 20:52:58 +03:00
#ifdef NINJA_GEN_VERBOSE_FILES
2016-07-09 11:21:54 +02:00
this->GetRulesFileStream() << "# Rules for language " << language << "\n\n";
2012-06-27 20:52:58 +03:00
#endif
2012-04-19 19:04:21 +03:00
this->WriteCompileRule(language);
}
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang)
2012-04-19 19:04:21 +03:00
{
cmLocalGenerator::RuleVariables vars;
vars.RuleLauncher = "RULE_LAUNCH_COMPILE";
2016-03-13 13:35:51 +01:00
vars.CMTarget = this->GetGeneratorTarget();
2012-04-19 19:04:21 +03:00
vars.Language = lang.c_str();
vars.Source = "$in";
vars.Object = "$out";
vars.Defines = "$DEFINES";
2015-11-17 17:22:37 +01:00
vars.Includes = "$INCLUDES";
2012-04-19 19:04:21 +03:00
vars.TargetPDB = "$TARGET_PDB";
2015-04-27 22:25:09 +02:00
vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
2013-03-16 19:13:01 +02:00
vars.ObjectDir = "$OBJECT_DIR";
2015-04-27 22:25:09 +02:00
vars.ObjectFileDir = "$OBJECT_FILE_DIR";
2012-06-27 20:52:58 +03:00
cmMakefile* mf = this->GetMakefile();
2016-07-09 11:21:54 +02:00
std::string flags = "$FLAGS";
std::string rspfile;
std::string rspcontent;
std::string responseFlag;
if (lang != "RC" && this->ForceResponseFile()) {
rspfile = "$RSP_FILE";
responseFlag = "@" + rspfile;
rspcontent = " $DEFINES $INCLUDES $FLAGS";
flags = responseFlag;
vars.Defines = "";
vars.Includes = "";
}
2014-08-03 19:52:23 +02:00
// Tell ninja dependency format so all deps can be loaded into a database
std::string deptype;
std::string depfile;
std::string cldeps;
2016-07-09 11:21:54 +02:00
if (this->NeedDepTypeMSVC(lang)) {
2015-04-27 22:25:09 +02:00
deptype = "msvc";
depfile = "";
flags += " /showIncludes";
2016-07-09 11:21:54 +02:00
} else if (mf->IsOn("CMAKE_NINJA_CMCLDEPS_" + lang)) {
2015-04-27 22:25:09 +02:00
// For the MS resource compiler we need cmcldeps, but skip dependencies
// for source-file try_compile cases because they are always fresh.
2016-07-09 11:21:54 +02:00
if (!mf->GetIsSourceFileTryCompile()) {
2014-08-03 19:52:23 +02:00
deptype = "gcc";
depfile = "$DEP_FILE";
2016-07-09 11:21:54 +02:00
const std::string cl = mf->GetDefinition("CMAKE_C_COMPILER")
? mf->GetSafeDefinition("CMAKE_C_COMPILER")
: mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
cldeps = "\"";
2015-11-17 17:22:37 +01:00
cldeps += cmSystemTools::GetCMClDepsCommand();
2014-08-03 19:52:23 +02:00
cldeps += "\" " + lang + " $in \"$DEP_FILE\" $out \"";
cldeps += mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX");
cldeps += "\" \"" + cl + "\" ";
2012-06-27 20:52:58 +03:00
}
2016-07-09 11:21:54 +02:00
} else {
2014-08-03 19:52:23 +02:00
deptype = "gcc";
2015-04-27 22:25:09 +02:00
const char* langdeptype = mf->GetDefinition("CMAKE_NINJA_DEPTYPE_" + lang);
2016-07-09 11:21:54 +02:00
if (langdeptype) {
2015-04-27 22:25:09 +02:00
deptype = langdeptype;
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
depfile = "$DEP_FILE";
2014-08-03 19:52:23 +02:00
const std::string flagsName = "CMAKE_DEPFILE_FLAGS_" + lang;
2015-04-27 22:25:09 +02:00
std::string depfileFlags = mf->GetSafeDefinition(flagsName);
2016-07-09 11:21:54 +02:00
if (!depfileFlags.empty()) {
2014-08-03 19:52:23 +02:00
cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
2016-07-09 11:21:54 +02:00
cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
2014-08-03 19:52:23 +02:00
cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
mf->GetDefinition("CMAKE_C_COMPILER"));
flags += " " + depfileFlags;
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2014-08-03 19:52:23 +02:00
vars.Flags = flags.c_str();
vars.DependencyFile = depfile.c_str();
2012-04-19 19:04:21 +03:00
// Rule for compiling object file.
2014-08-03 19:52:23 +02:00
const std::string cmdVar = std::string("CMAKE_") + lang + "_COMPILE_OBJECT";
2015-04-27 22:25:09 +02:00
std::string compileCmd = mf->GetRequiredDefinition(cmdVar);
2012-04-19 19:04:21 +03:00
std::vector<std::string> compileCmds;
cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
2015-08-17 11:37:30 +02:00
// Maybe insert an include-what-you-use runner.
2016-07-09 11:21:54 +02:00
if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
2015-08-17 11:37:30 +02:00
std::string const iwyu_prop = lang + "_INCLUDE_WHAT_YOU_USE";
2016-07-09 11:21:54 +02:00
const char* iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
std::string const tidy_prop = lang + "_CLANG_TIDY";
const char* tidy = this->GeneratorTarget->GetProperty(tidy_prop);
if ((iwyu && *iwyu) || (tidy && *tidy)) {
std::string run_iwyu = this->GetLocalGenerator()->ConvertToOutputFormat(
cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
run_iwyu += " -E __run_iwyu";
if (iwyu && *iwyu) {
run_iwyu += " --iwyu=";
run_iwyu += this->GetLocalGenerator()->EscapeForShell(iwyu);
}
if (tidy && *tidy) {
run_iwyu += " --tidy=";
run_iwyu += this->GetLocalGenerator()->EscapeForShell(tidy);
run_iwyu += " --source=$in";
}
2015-08-17 11:37:30 +02:00
run_iwyu += " -- ";
compileCmds.front().insert(0, run_iwyu);
}
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
2015-11-17 17:22:37 +01:00
// Maybe insert a compiler launcher like ccache or distcc
2016-07-09 11:21:54 +02:00
if (!compileCmds.empty() && (lang == "C" || lang == "CXX")) {
2015-11-17 17:22:37 +01:00
std::string const clauncher_prop = lang + "_COMPILER_LAUNCHER";
2016-07-09 11:21:54 +02:00
const char* clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
if (clauncher && *clauncher) {
2015-11-17 17:22:37 +01:00
std::vector<std::string> launcher_cmd;
cmSystemTools::ExpandListArgument(clauncher, launcher_cmd, true);
for (std::vector<std::string>::iterator i = launcher_cmd.begin(),
2016-07-09 11:21:54 +02:00
e = launcher_cmd.end();
i != e; ++i) {
2015-11-17 17:22:37 +01:00
*i = this->LocalGenerator->EscapeForShell(*i);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
std::string const& run_launcher = cmJoin(launcher_cmd, " ") + " ";
compileCmds.front().insert(0, run_launcher);
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
2016-07-09 11:21:54 +02:00
if (!compileCmds.empty()) {
2015-04-27 22:25:09 +02:00
compileCmds.front().insert(0, cldeps);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2012-04-19 19:04:21 +03:00
for (std::vector<std::string>::iterator i = compileCmds.begin();
i != compileCmds.end(); ++i)
this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
std::string cmdLine =
this->GetLocalGenerator()->BuildCommandLine(compileCmds);
// Write the rule for compiling file of the given language.
2015-04-27 22:25:09 +02:00
std::ostringstream comment;
2014-08-03 19:52:23 +02:00
comment << "Rule for compiling " << lang << " files.";
2015-04-27 22:25:09 +02:00
std::ostringstream description;
2014-08-03 19:52:23 +02:00
description << "Building " << lang << " object $out";
2016-07-09 11:21:54 +02:00
this->GetGlobalGenerator()->AddRule(
this->LanguageCompilerRule(lang), cmdLine, description.str(),
comment.str(), depfile, deptype, rspfile, rspcontent,
/*restat*/ "",
/*generator*/ false);
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::WriteObjectBuildStatements()
2012-04-19 19:04:21 +03:00
{
// Write comments.
cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
this->GetBuildFileStream()
<< "# Object build statements for "
2016-03-13 13:35:51 +01:00
<< cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
2016-07-09 11:21:54 +02:00
<< " target " << this->GetTargetName() << "\n\n";
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
std::string config = this->Makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
std::vector<cmSourceFile const*> customCommands;
this->GeneratorTarget->GetCustomCommands(customCommands, config);
2016-07-09 11:21:54 +02:00
for (std::vector<cmSourceFile const*>::const_iterator si =
customCommands.begin();
si != customCommands.end(); ++si) {
cmCustomCommand const* cc = (*si)->GetCustomCommand();
this->GetLocalGenerator()->AddCustomCommandTarget(
cc, this->GetGeneratorTarget());
// Record the custom commands for this target. The container is used
// in WriteObjectBuildStatement when called in a loop below.
this->CustomCommands.push_back(cc);
}
2015-04-27 22:25:09 +02:00
std::vector<cmSourceFile const*> headerSources;
this->GeneratorTarget->GetHeaderSources(headerSources, config);
2012-08-04 10:26:08 +03:00
this->OSXBundleGenerator->GenerateMacOSXContentStatements(
2016-07-09 11:21:54 +02:00
headerSources, this->MacOSXContentGenerator);
2015-04-27 22:25:09 +02:00
std::vector<cmSourceFile const*> extraSources;
this->GeneratorTarget->GetExtraSources(extraSources, config);
2012-08-04 10:26:08 +03:00
this->OSXBundleGenerator->GenerateMacOSXContentStatements(
2016-07-09 11:21:54 +02:00
extraSources, this->MacOSXContentGenerator);
2015-04-27 22:25:09 +02:00
std::vector<cmSourceFile const*> externalObjects;
this->GeneratorTarget->GetExternalObjects(externalObjects, config);
2016-07-09 11:21:54 +02:00
for (std::vector<cmSourceFile const*>::const_iterator si =
externalObjects.begin();
si != externalObjects.end(); ++si) {
2012-04-19 19:04:21 +03:00
this->Objects.push_back(this->GetSourceFilePath(*si));
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
cmNinjaDeps orderOnlyDeps;
2016-03-13 13:35:51 +01:00
this->GetLocalGenerator()->AppendTargetDepends(this->GeneratorTarget,
orderOnlyDeps);
2015-04-27 22:25:09 +02:00
// Add order-only dependencies on custom command outputs.
2016-07-09 11:21:54 +02:00
for (std::vector<cmCustomCommand const*>::const_iterator cci =
this->CustomCommands.begin();
cci != this->CustomCommands.end(); ++cci) {
2015-04-27 22:25:09 +02:00
cmCustomCommand const* cc = *cci;
cmCustomCommandGenerator ccg(*cc, this->GetConfigName(),
2015-11-17 17:22:37 +01:00
this->GetLocalGenerator());
2015-04-27 22:25:09 +02:00
const std::vector<std::string>& ccoutputs = ccg.GetOutputs();
2016-07-09 11:21:54 +02:00
const std::vector<std::string>& ccbyproducts = ccg.GetByproducts();
2015-04-27 22:25:09 +02:00
std::transform(ccoutputs.begin(), ccoutputs.end(),
std::back_inserter(orderOnlyDeps), MapToNinjaPath());
std::transform(ccbyproducts.begin(), ccbyproducts.end(),
std::back_inserter(orderOnlyDeps), MapToNinjaPath());
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (!orderOnlyDeps.empty()) {
2015-04-27 22:25:09 +02:00
cmNinjaDeps orderOnlyTarget;
orderOnlyTarget.push_back(this->OrderDependsTargetForTarget());
2016-07-09 11:21:54 +02:00
this->GetGlobalGenerator()->WritePhonyBuild(
this->GetBuildFileStream(),
"Order-only phony target for " + this->GetTargetName(), orderOnlyTarget,
cmNinjaDeps(), cmNinjaDeps(), orderOnlyDeps);
}
2015-04-27 22:25:09 +02:00
std::vector<cmSourceFile const*> objectSources;
this->GeneratorTarget->GetObjectSources(objectSources, config);
2016-07-09 11:21:54 +02:00
for (std::vector<cmSourceFile const*>::const_iterator si =
objectSources.begin();
si != objectSources.end(); ++si) {
2015-04-27 22:25:09 +02:00
this->WriteObjectBuildStatement(*si, !orderOnlyDeps.empty());
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
this->GetBuildFileStream() << "\n";
}
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::WriteObjectBuildStatement(
2015-04-27 22:25:09 +02:00
cmSourceFile const* source, bool writeOrderDependsTargetForTarget)
2012-04-19 19:04:21 +03:00
{
2015-11-17 17:22:37 +01:00
std::string const language = source->GetLanguage();
std::string const sourceFileName =
2016-07-09 11:21:54 +02:00
language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
std::string const objectDir =
this->ConvertToNinjaPath(this->GeneratorTarget->GetSupportDirectory());
std::string const objectFileName =
this->ConvertToNinjaPath(this->GetObjectFilePath(source));
2015-11-17 17:22:37 +01:00
std::string const objectFileDir =
cmSystemTools::GetFilenamePath(objectFileName);
cmNinjaVars vars;
vars["FLAGS"] = this->ComputeFlagsForObject(source, language);
vars["DEFINES"] = this->ComputeDefines(source, language);
vars["INCLUDES"] = this->GetIncludes(language);
2016-07-09 11:21:54 +02:00
if (!this->NeedDepTypeMSVC(language)) {
2015-11-17 17:22:37 +01:00
vars["DEP_FILE"] =
cmGlobalNinjaGenerator::EncodeDepfileSpace(objectFileName + ".d");
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
this->ExportObjectCompileCommand(
2016-07-09 11:21:54 +02:00
language, sourceFileName, objectDir, objectFileName, objectFileDir,
vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"]);
2015-11-17 17:22:37 +01:00
2012-04-19 19:04:21 +03:00
std::string comment;
std::string rule = this->LanguageCompilerRule(language);
cmNinjaDeps outputs;
outputs.push_back(objectFileName);
// Add this object to the list of object files.
this->Objects.push_back(objectFileName);
cmNinjaDeps explicitDeps;
explicitDeps.push_back(sourceFileName);
2013-03-16 19:13:01 +02:00
cmNinjaDeps implicitDeps;
2016-07-09 11:21:54 +02:00
if (const char* objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
2012-04-19 19:04:21 +03:00
std::vector<std::string> depList;
cmSystemTools::ExpandListArgument(objectDeps, depList);
2016-07-09 11:21:54 +02:00
for (std::vector<std::string>::iterator odi = depList.begin();
odi != depList.end(); ++odi) {
if (cmSystemTools::FileIsFullPath(*odi)) {
2015-04-27 22:25:09 +02:00
*odi = cmSystemTools::CollapseFullPath(*odi);
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
std::transform(depList.begin(), depList.end(),
2013-03-16 19:13:01 +02:00
std::back_inserter(implicitDeps), MapToNinjaPath());
2012-04-19 19:04:21 +03:00
}
2015-04-27 22:25:09 +02:00
cmNinjaDeps orderOnlyDeps;
2016-07-09 11:21:54 +02:00
if (writeOrderDependsTargetForTarget) {
2015-04-27 22:25:09 +02:00
orderOnlyDeps.push_back(this->OrderDependsTargetForTarget());
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
// If the source file is GENERATED and does not have a custom command
// (either attached to this source file or another one), assume that one of
// the target dependencies, OBJECT_DEPENDS or header file custom commands
// will rebuild the file.
if (source->GetPropertyAsBool("GENERATED") && !source->GetCustomCommand() &&
!this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
this->GetGlobalGenerator()->AddAssumedSourceDependencies(sourceFileName,
orderOnlyDeps);
}
2012-06-27 20:52:58 +03:00
EnsureParentDirectoryExists(objectFileName);
2013-11-03 12:27:13 +02:00
vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
2016-07-09 11:21:54 +02:00
objectDir, cmOutputConverter::SHELL);
2015-04-27 22:25:09 +02:00
vars["OBJECT_FILE_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
2016-07-09 11:21:54 +02:00
objectFileDir, cmOutputConverter::SHELL);
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
vars);
2014-08-03 19:52:23 +02:00
2013-03-16 19:13:01 +02:00
this->SetMsvcTargetPdbVariable(vars);
2012-06-27 20:52:58 +03:00
2016-07-09 11:21:54 +02:00
bool const isRC = (language == "RC");
int const commandLineLengthLimit =
((!isRC && this->ForceResponseFile())) ? -1 : 0;
std::string const rspfile = objectFileName + ".rsp";
this->GetGlobalGenerator()->WriteBuild(
this->GetBuildFileStream(), comment, rule, outputs, explicitDeps,
implicitDeps, orderOnlyDeps, vars, rspfile, commandLineLengthLimit);
2012-04-19 19:04:21 +03:00
2016-07-09 11:21:54 +02:00
if (const char* objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
2012-04-19 19:04:21 +03:00
std::vector<std::string> outputList;
cmSystemTools::ExpandListArgument(objectOutputs, outputList);
std::transform(outputList.begin(), outputList.end(), outputList.begin(),
MapToNinjaPath());
2013-11-03 12:27:13 +02:00
this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
"Additional output files.",
2016-07-09 11:21:54 +02:00
outputList, outputs);
2012-04-19 19:04:21 +03:00
}
}
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::ExportObjectCompileCommand(
std::string const& language, std::string const& sourceFileName,
std::string const& objectDir, std::string const& objectFileName,
std::string const& objectFileDir, std::string const& flags,
std::string const& defines, std::string const& includes)
2012-04-19 19:04:21 +03:00
{
2016-07-09 11:21:54 +02:00
if (!this->Makefile->IsOn("CMAKE_EXPORT_COMPILE_COMMANDS")) {
2012-04-19 19:04:21 +03:00
return;
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2015-11-17 17:22:37 +01:00
cmLocalGenerator::RuleVariables compileObjectVars;
compileObjectVars.Language = language.c_str();
std::string escapedSourceFileName = sourceFileName;
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::FileIsFullPath(sourceFileName.c_str())) {
2015-11-17 17:22:37 +01:00
escapedSourceFileName = cmSystemTools::CollapseFullPath(
2016-07-09 11:21:54 +02:00
escapedSourceFileName, this->GetGlobalGenerator()
->GetCMakeInstance()
->GetHomeOutputDirectory());
}
2012-04-19 19:04:21 +03:00
2016-07-09 11:21:54 +02:00
escapedSourceFileName = this->LocalGenerator->ConvertToOutputFormat(
escapedSourceFileName, cmOutputConverter::SHELL);
2015-11-17 17:22:37 +01:00
compileObjectVars.Source = escapedSourceFileName.c_str();
compileObjectVars.Object = objectFileName.c_str();
compileObjectVars.ObjectDir = objectDir.c_str();
compileObjectVars.ObjectFileDir = objectFileDir.c_str();
compileObjectVars.Flags = flags.c_str();
compileObjectVars.Defines = defines.c_str();
compileObjectVars.Includes = includes.c_str();
// Rule for compiling object file.
std::string compileCmdVar = "CMAKE_";
compileCmdVar += language;
compileCmdVar += "_COMPILE_OBJECT";
std::string compileCmd =
this->GetMakefile()->GetRequiredDefinition(compileCmdVar);
std::vector<std::string> compileCmds;
cmSystemTools::ExpandListArgument(compileCmd, compileCmds);
for (std::vector<std::string>::iterator i = compileCmds.begin();
i != compileCmds.end(); ++i)
this->GetLocalGenerator()->ExpandRuleVariables(*i, compileObjectVars);
std::string cmdLine =
this->GetLocalGenerator()->BuildCommandLine(compileCmds);
2016-07-09 11:21:54 +02:00
this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName);
2012-04-19 19:04:21 +03:00
}
2012-06-27 20:52:58 +03:00
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::EnsureDirectoryExists(
const std::string& path) const
2012-06-27 20:52:58 +03:00
{
2016-07-09 11:21:54 +02:00
if (cmSystemTools::FileIsFullPath(path.c_str())) {
2013-03-16 19:13:01 +02:00
cmSystemTools::MakeDirectory(path.c_str());
2016-07-09 11:21:54 +02:00
} else {
cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
std::string fullPath =
std::string(gg->GetCMakeInstance()->GetHomeOutputDirectory());
// Also ensures their is a trailing slash.
gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
fullPath += path;
2013-03-16 19:13:01 +02:00
cmSystemTools::MakeDirectory(fullPath.c_str());
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
}
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::EnsureParentDirectoryExists(
const std::string& path) const
2012-06-27 20:52:58 +03:00
{
2015-04-27 22:25:09 +02:00
EnsureDirectoryExists(cmSystemTools::GetParentDirectory(path));
2012-06-27 20:52:58 +03:00
}
2012-08-04 10:26:08 +03:00
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::MacOSXContentGeneratorType::operator()(
2015-04-27 22:25:09 +02:00
cmSourceFile const& source, const char* pkgloc)
2012-08-04 10:26:08 +03:00
{
// Skip OS X content when not building a Framework or Bundle.
2016-07-09 11:21:54 +02:00
if (!this->Generator->GetGeneratorTarget()->IsBundleOnApple()) {
2012-08-04 10:26:08 +03:00
return;
2016-07-09 11:21:54 +02:00
}
2012-08-04 10:26:08 +03:00
std::string macdir =
this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc);
// Get the input file location.
std::string input = source.GetFullPath();
2016-07-09 11:21:54 +02:00
input = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(input);
2012-08-04 10:26:08 +03:00
// Get the output file location.
std::string output = macdir;
output += "/";
output += cmSystemTools::GetFilenameName(input);
2016-07-09 11:21:54 +02:00
output = this->Generator->GetGlobalGenerator()->ConvertToNinjaPath(output);
2012-08-04 10:26:08 +03:00
// Write a build statement to copy the content into the bundle.
this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(input,
output);
// Add as a dependency of all target so that it gets called.
this->Generator->GetGlobalGenerator()->AddDependencyToAll(output);
}
2014-08-03 19:52:23 +02:00
2015-04-27 22:25:09 +02:00
void cmNinjaTargetGenerator::addPoolNinjaVariable(
2016-07-09 11:21:54 +02:00
const std::string& pool_property, cmGeneratorTarget* target,
cmNinjaVars& vars)
2014-08-03 19:52:23 +02:00
{
2016-07-09 11:21:54 +02:00
const char* pool = target->GetProperty(pool_property);
if (pool) {
vars["pool"] = pool;
}
}
bool cmNinjaTargetGenerator::ForceResponseFile()
{
static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
cmSystemTools::GetEnv(forceRspFile) != 0);
2014-08-03 19:52:23 +02:00
}