cmake/Source/cmNinjaTargetGenerator.cxx

1656 lines
61 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 "cmNinjaTargetGenerator.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include <algorithm>
2020-02-01 23:06:01 +01:00
#include <cassert>
2017-04-14 19:02:05 +02:00
#include <iterator>
#include <map>
2019-11-11 23:01:05 +01:00
#include <ostream>
2020-08-30 11:54:41 +02:00
#include <unordered_map>
#include <unordered_set>
2019-11-11 23:01:05 +01:00
#include <utility>
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
#include <cm/memory>
2020-08-30 11:54:41 +02:00
#include <cmext/algorithm>
2020-02-01 23:06:01 +01:00
2020-08-30 11:54:41 +02:00
#include <cm3p/json/value.h>
#include <cm3p/json/writer.h>
2020-02-01 23:06:01 +01:00
2016-07-09 11:21:54 +02:00
#include "cmComputeLinkInformation.h"
#include "cmCustomCommandGenerator.h"
2012-04-19 19:04:21 +03:00
#include "cmGeneratedFileStream.h"
2017-04-14 19:02:05 +02:00
#include "cmGeneratorExpression.h"
2012-04-19 19:04:21 +03:00
#include "cmGeneratorTarget.h"
2016-07-09 11:21:54 +02:00
#include "cmGlobalNinjaGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmLocalGenerator.h"
2016-07-09 11:21:54 +02:00
#include "cmLocalNinjaGenerator.h"
#include "cmMakefile.h"
2012-04-19 19:04:21 +03:00
#include "cmNinjaNormalTargetGenerator.h"
#include "cmNinjaUtilityTargetGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmOutputConverter.h"
2020-08-30 11:54:41 +02:00
#include "cmProperty.h"
2019-11-11 23:01:05 +01:00
#include "cmRange.h"
2017-04-14 19:02:05 +02:00
#include "cmRulePlaceholderExpander.h"
2012-04-19 19:04:21 +03:00
#include "cmSourceFile.h"
2016-10-30 18:24:19 +01:00
#include "cmState.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
2016-10-30 18:24:19 +01:00
#include "cmake.h"
2019-11-11 23:01:05 +01:00
std::unique_ptr<cmNinjaTargetGenerator> cmNinjaTargetGenerator::New(
cmGeneratorTarget* target)
2012-04-19 19:04:21 +03:00
{
2016-07-09 11:21:54 +02:00
switch (target->GetType()) {
2017-04-14 19:02:05 +02:00
case cmStateEnums::EXECUTABLE:
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::STATIC_LIBRARY:
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::OBJECT_LIBRARY:
2019-11-11 23:01:05 +01:00
return cm::make_unique<cmNinjaNormalTargetGenerator>(target);
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
case cmStateEnums::UTILITY:
case cmStateEnums::GLOBAL_TARGET:
2019-11-11 23:01:05 +01:00
return cm::make_unique<cmNinjaUtilityTargetGenerator>(target);
2016-07-09 11:21:54 +02:00
default:
2019-11-11 23:01:05 +01:00
return std::unique_ptr<cmNinjaTargetGenerator>();
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
}
2015-11-17 17:22:37 +01:00
cmNinjaTargetGenerator::cmNinjaTargetGenerator(cmGeneratorTarget* target)
2016-10-30 18:24:19 +01:00
: cmCommonTargetGenerator(target)
2018-01-26 17:06:56 +01:00
, OSXBundleGenerator(nullptr)
2016-07-09 11:21:54 +02:00
, LocalGenerator(
static_cast<cmLocalNinjaGenerator*>(target->GetLocalGenerator()))
2012-04-19 19:04:21 +03:00
{
2020-08-30 11:54:41 +02:00
for (auto const& fileConfig : target->Makefile->GetGeneratorConfigs()) {
this->Configs[fileConfig].MacOSXContentGenerator =
cm::make_unique<MacOSXContentGeneratorType>(this, fileConfig);
}
2012-04-19 19:04:21 +03:00
}
2019-11-11 23:01:05 +01:00
cmNinjaTargetGenerator::~cmNinjaTargetGenerator() = default;
2012-04-19 19:04:21 +03:00
2020-08-30 11:54:41 +02:00
cmGeneratedFileStream& cmNinjaTargetGenerator::GetImplFileStream(
const std::string& config) const
2012-04-19 19:04:21 +03:00
{
2020-08-30 11:54:41 +02:00
return *this->GetGlobalGenerator()->GetImplFileStream(config);
}
cmGeneratedFileStream& cmNinjaTargetGenerator::GetCommonFileStream() const
{
return *this->GetGlobalGenerator()->GetCommonFileStream();
2012-04-19 19:04:21 +03:00
}
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(
2020-08-30 11:54:41 +02:00
const std::string& lang, const std::string& config) const
2015-08-17 11:37:30 +02:00
{
2020-08-30 11:54:41 +02:00
return cmStrCat(
lang, "_COMPILER__",
cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()),
'_', config);
2015-08-17 11:37:30 +02:00
}
2016-10-30 18:24:19 +01:00
std::string cmNinjaTargetGenerator::LanguagePreprocessRule(
2020-08-30 11:54:41 +02:00
std::string const& lang, const std::string& config) const
2016-10-30 18:24:19 +01:00
{
2020-08-30 11:54:41 +02:00
return cmStrCat(
lang, "_PREPROCESS_SCAN__",
cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()),
'_', config);
}
std::string cmNinjaTargetGenerator::LanguageDependencyRule(
std::string const& lang, const std::string& config) const
{
return cmStrCat(
lang, "_SCAN__",
cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()),
'_', config);
2016-10-30 18:24:19 +01:00
}
bool cmNinjaTargetGenerator::NeedExplicitPreprocessing(
std::string const& lang) const
{
return lang == "Fortran";
}
2019-11-11 23:01:05 +01:00
bool cmNinjaTargetGenerator::UsePreprocessedSource(
std::string const& lang) const
{
return lang == "Fortran";
}
2020-02-01 23:06:01 +01:00
bool cmNinjaTargetGenerator::CompilePreprocessedSourceWithDefines(
std::string const& lang) const
{
return this->Makefile->IsOn(
cmStrCat("CMAKE_", lang, "_COMPILE_WITH_DEFINES"));
}
2016-10-30 18:24:19 +01:00
std::string cmNinjaTargetGenerator::LanguageDyndepRule(
2020-08-30 11:54:41 +02:00
const std::string& lang, const std::string& config) const
2016-10-30 18:24:19 +01:00
{
2020-08-30 11:54:41 +02:00
return cmStrCat(
lang, "_DYNDEP__",
cmGlobalNinjaGenerator::EncodeRuleName(this->GeneratorTarget->GetName()),
'_', config);
2016-10-30 18:24:19 +01:00
}
bool cmNinjaTargetGenerator::NeedDyndep(std::string const& lang) const
{
return lang == "Fortran";
}
2020-08-30 11:54:41 +02:00
std::string cmNinjaTargetGenerator::OrderDependsTargetForTarget(
const std::string& config)
2015-04-27 22:25:09 +02:00
{
2020-08-30 11:54:41 +02:00
return this->GetGlobalGenerator()->OrderDependsTargetForTarget(
this->GeneratorTarget, config);
2015-04-27 22:25:09 +02:00
}
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(
2020-08-30 11:54:41 +02:00
cmSourceFile const* source, const std::string& language,
const std::string& config)
2012-04-19 19:04:21 +03:00
{
2020-08-30 11:54:41 +02:00
std::vector<std::string> architectures;
std::unordered_map<std::string, std::string> pchSources;
this->GeneratorTarget->GetAppleArchs(config, architectures);
if (architectures.empty()) {
architectures.emplace_back();
}
std::string filterArch;
for (const std::string& arch : architectures) {
const std::string pchSource =
this->GeneratorTarget->GetPchSource(config, language, arch);
if (pchSource == source->GetFullPath()) {
filterArch = arch;
}
if (!pchSource.empty()) {
pchSources.insert(std::make_pair(pchSource, arch));
}
}
std::string flags = this->GetFlags(language, config, filterArch);
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);
2020-08-30 11:54:41 +02:00
this->AppendFortranPreprocessFlags(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.
2018-04-23 21:13:27 +02:00
cmGeneratorExpressionInterpreter genexInterpreter(
2020-08-30 11:54:41 +02:00
this->LocalGenerator, config, this->GeneratorTarget, language);
2018-04-23 21:13:27 +02:00
const std::string COMPILE_FLAGS("COMPILE_FLAGS");
2020-08-30 11:54:41 +02:00
if (cmProp cflags = source->GetProperty(COMPILE_FLAGS)) {
2018-04-23 21:13:27 +02:00
this->LocalGenerator->AppendFlags(
2020-08-30 11:54:41 +02:00
flags, genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS));
2018-04-23 21:13:27 +02:00
}
const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
2020-08-30 11:54:41 +02:00
if (cmProp coptions = source->GetProperty(COMPILE_OPTIONS)) {
2018-04-23 21:13:27 +02:00
this->LocalGenerator->AppendCompileOptions(
2020-08-30 11:54:41 +02:00
flags, genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS));
2017-04-14 19:02:05 +02:00
}
2012-04-19 19:04:21 +03:00
2020-02-01 23:06:01 +01:00
// Add precompile headers compile options.
2020-08-30 11:54:41 +02:00
if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
2020-02-01 23:06:01 +01:00
std::string pchOptions;
2020-08-30 11:54:41 +02:00
auto pchIt = pchSources.find(source->GetFullPath());
if (pchIt != pchSources.end()) {
2020-02-01 23:06:01 +01:00
pchOptions = this->GeneratorTarget->GetPchCreateCompileOptions(
2020-08-30 11:54:41 +02:00
config, language, pchIt->second);
2020-02-01 23:06:01 +01:00
} else {
pchOptions =
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetPchUseCompileOptions(config, language);
2020-02-01 23:06:01 +01:00
}
this->LocalGenerator->AppendCompileOptions(
flags, genexInterpreter.Evaluate(pchOptions, COMPILE_OPTIONS));
}
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,
2020-08-30 11:54:41 +02:00
std::string const& language,
const std::string& config)
2015-11-17 17:22:37 +01:00
{
std::vector<std::string> includes;
2016-07-09 11:21:54 +02:00
this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
2020-08-30 11:54:41 +02:00
language, config);
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,
2016-10-30 18:24:19 +01:00
language == "RC", // full include paths for RC needed by cmcldeps
2020-08-30 11:54:41 +02:00
false, config);
2016-10-30 18:24:19 +01:00
if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
2016-07-09 11:21:54 +02:00
std::replace(includeFlags.begin(), includeFlags.end(), '\\', '/');
2016-10-30 18:24:19 +01:00
}
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
{
2020-08-30 11:54:41 +02:00
std::string const& deptype = this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_NINJA_DEPTYPE_", lang));
2019-11-11 23:01:05 +01:00
if (deptype == "msvc") {
return true;
}
if (deptype == "intel") {
// Ninja does not really define "intel", but we use it to switch based
// on whether this environment supports "gcc" or "msvc" deptype.
if (!this->GetGlobalGenerator()->SupportsMultilineDepfile()) {
// This ninja version is too old to support the Intel depfile format.
// Fall back to msvc deptype.
return true;
}
if ((this->Makefile->GetHomeDirectory().find(' ') != std::string::npos) ||
(this->Makefile->GetHomeOutputDirectory().find(' ') !=
std::string::npos)) {
// The Intel compiler does not properly escape spaces in a depfile.
// Fall back to msvc deptype.
return true;
}
}
return false;
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,
2020-08-30 11:54:41 +02:00
const std::string& language,
const std::string& config)
2012-04-19 19:04:21 +03:00
{
2013-03-16 19:13:01 +02:00
std::set<std::string> defines;
2018-04-23 21:13:27 +02:00
cmGeneratorExpressionInterpreter genexInterpreter(
2018-10-28 12:09:07 +01:00
this->LocalGenerator, config, this->GeneratorTarget, language);
2018-04-23 21:13:27 +02:00
2020-08-30 11:54:41 +02:00
// Seriously??
if (this->GetGlobalGenerator()->IsMultiConfig()) {
defines.insert(cmStrCat("CMAKE_INTDIR=\"", config, '"'));
}
2018-04-23 21:13:27 +02:00
const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
2020-08-30 11:54:41 +02:00
if (cmProp compile_defs = source->GetProperty(COMPILE_DEFINITIONS)) {
2018-04-23 21:13:27 +02:00
this->LocalGenerator->AppendDefines(
2020-08-30 11:54:41 +02:00
defines, genexInterpreter.Evaluate(*compile_defs, COMPILE_DEFINITIONS));
2018-04-23 21:13:27 +02:00
}
2020-02-01 23:06:01 +01:00
std::string defPropName =
cmStrCat("COMPILE_DEFINITIONS_", cmSystemTools::UpperCase(config));
2020-08-30 11:54:41 +02:00
if (cmProp config_compile_defs = source->GetProperty(defPropName)) {
2018-04-23 21:13:27 +02:00
this->LocalGenerator->AppendDefines(
defines,
2020-08-30 11:54:41 +02:00
genexInterpreter.Evaluate(*config_compile_defs, COMPILE_DEFINITIONS));
2012-04-19 19:04:21 +03:00
}
2020-08-30 11:54:41 +02:00
std::string definesString = this->GetDefines(language, config);
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
}
2018-04-23 21:13:27 +02:00
std::string cmNinjaTargetGenerator::ComputeIncludes(
2020-08-30 11:54:41 +02:00
cmSourceFile const* source, const std::string& language,
const std::string& config)
2018-04-23 21:13:27 +02:00
{
std::vector<std::string> includes;
cmGeneratorExpressionInterpreter genexInterpreter(
2018-10-28 12:09:07 +01:00
this->LocalGenerator, config, this->GeneratorTarget, language);
2018-04-23 21:13:27 +02:00
const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
2020-08-30 11:54:41 +02:00
if (cmProp cincludes = source->GetProperty(INCLUDE_DIRECTORIES)) {
2018-04-23 21:13:27 +02:00
this->LocalGenerator->AppendIncludeDirectories(
2020-08-30 11:54:41 +02:00
includes, genexInterpreter.Evaluate(*cincludes, INCLUDE_DIRECTORIES),
2018-04-23 21:13:27 +02:00
*source);
}
std::string includesString = this->LocalGenerator->GetIncludeFlags(
includes, this->GeneratorTarget, language, true, false, config);
this->LocalGenerator->AppendFlags(includesString,
2020-08-30 11:54:41 +02:00
this->GetIncludes(language, config));
2018-04-23 21:13:27 +02:00
return includesString;
}
2018-10-28 12:09:07 +01:00
cmNinjaDeps cmNinjaTargetGenerator::ComputeLinkDeps(
2020-08-30 11:54:41 +02:00
const std::string& linkLanguage, const std::string& config) const
2012-04-19 19:04:21 +03:00
{
// Static libraries never depend on other targets for linking.
2017-04-14 19:02:05 +02:00
if (this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
this->GeneratorTarget->GetType() == cmStateEnums::OBJECT_LIBRARY) {
2012-04-19 19:04:21 +03:00
return cmNinjaDeps();
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
cmComputeLinkInformation* cli =
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetLinkInformation(config);
2016-10-30 18:24:19 +01:00
if (!cli) {
2012-04-19 19:04:21 +03:00
return cmNinjaDeps();
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
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.
2017-07-20 19:35:53 +02:00
if (cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetModuleDefinitionInfo(config)) {
2018-01-26 17:06:56 +01:00
for (cmSourceFile const* src : mdi->Sources) {
result.push_back(this->ConvertToNinjaPath(src->GetFullPath()));
2017-07-20 19:35:53 +02:00
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Add a dependency on user-specified manifest files, if any.
std::vector<cmSourceFile const*> manifest_srcs;
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetManifests(manifest_srcs, config);
2018-01-26 17:06:56 +01:00
for (cmSourceFile const* manifest_src : manifest_srcs) {
result.push_back(this->ConvertToNinjaPath(manifest_src->GetFullPath()));
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Add user-specified dependencies.
2018-10-28 12:09:07 +01:00
std::vector<std::string> linkDeps;
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetLinkDepends(linkDeps, config, linkLanguage);
2018-10-28 12:09:07 +01:00
std::transform(linkDeps.begin(), linkDeps.end(), std::back_inserter(result),
MapToNinjaPath());
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(
2020-08-30 11:54:41 +02:00
cmSourceFile const* source, const std::string& config) const
2012-04-19 19:04:21 +03:00
{
std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
2016-10-30 18:24:19 +01:00
if (!path.empty()) {
2020-08-30 11:54:41 +02:00
path += '/';
2016-10-30 18:24:19 +01:00
}
2016-07-09 11:21:54 +02:00
std::string const& objectName = this->GeneratorTarget->GetObjectName(source);
2020-08-30 11:54:41 +02:00
path += cmStrCat(
this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
this->GetGlobalGenerator()->ConfigDirectory(config), '/', objectName);
2012-04-19 19:04:21 +03:00
return path;
}
2016-10-30 18:24:19 +01:00
std::string cmNinjaTargetGenerator::GetPreprocessedFilePath(
2020-08-30 11:54:41 +02:00
cmSourceFile const* source, const std::string& config) const
2016-10-30 18:24:19 +01:00
{
// Choose an extension to compile already-preprocessed source.
std::string ppExt = source->GetExtension();
if (cmHasLiteralPrefix(ppExt, "F")) {
// Some Fortran compilers automatically enable preprocessing for
// upper-case extensions. Since the source is already preprocessed,
// use a lower-case extension.
ppExt = cmSystemTools::LowerCase(ppExt);
}
if (ppExt == "fpp") {
// Some Fortran compilers automatically enable preprocessing for
// the ".fpp" extension. Since the source is already preprocessed,
// use the ".f" extension.
ppExt = "f";
}
// Take the object file name and replace the extension.
std::string const& objName = this->GeneratorTarget->GetObjectName(source);
std::string const& objExt =
this->GetGlobalGenerator()->GetLanguageOutputExtension(*source);
assert(objName.size() >= objExt.size());
std::string const ppName =
2020-08-30 11:54:41 +02:00
cmStrCat(objName.substr(0, objName.size() - objExt.size()), "-pp.", ppExt);
2016-10-30 18:24:19 +01:00
std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
2017-04-14 19:02:05 +02:00
if (!path.empty()) {
2020-08-30 11:54:41 +02:00
path += '/';
2017-04-14 19:02:05 +02:00
}
2020-08-30 11:54:41 +02:00
path +=
cmStrCat(this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
this->GetGlobalGenerator()->ConfigDirectory(config), '/', ppName);
2016-10-30 18:24:19 +01:00
return path;
}
std::string cmNinjaTargetGenerator::GetDyndepFilePath(
2020-08-30 11:54:41 +02:00
std::string const& lang, const std::string& config) const
2016-10-30 18:24:19 +01:00
{
std::string path = this->LocalGenerator->GetHomeRelativeOutputPath();
2017-04-14 19:02:05 +02:00
if (!path.empty()) {
2020-08-30 11:54:41 +02:00
path += '/';
2017-04-14 19:02:05 +02:00
}
2020-08-30 11:54:41 +02:00
path += cmStrCat(
this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
this->GetGlobalGenerator()->ConfigDirectory(config), '/', lang, ".dd");
2016-10-30 18:24:19 +01:00
return path;
}
std::string cmNinjaTargetGenerator::GetTargetDependInfoPath(
2020-08-30 11:54:41 +02:00
std::string const& lang, const std::string& config) const
2016-10-30 18:24:19 +01:00
{
2020-02-01 23:06:01 +01:00
std::string path =
cmStrCat(this->Makefile->GetCurrentBinaryDirectory(), '/',
this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
2020-08-30 11:54:41 +02:00
this->GetGlobalGenerator()->ConfigDirectory(config), '/', lang,
"DependInfo.json");
2016-10-30 18:24:19 +01:00
return path;
}
2020-08-30 11:54:41 +02:00
std::string cmNinjaTargetGenerator::GetTargetOutputDir(
const std::string& config) const
2012-04-19 19:04:21 +03:00
{
2020-08-30 11:54:41 +02:00
std::string dir = this->GeneratorTarget->GetDirectory(config);
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(
2020-08-30 11:54:41 +02:00
const std::string& name, const std::string& config) const
2012-04-19 19:04:21 +03:00
{
2020-08-30 11:54:41 +02:00
std::string path = this->GetTargetOutputDir(config);
2016-10-30 18:24:19 +01:00
if (path.empty() || path == ".") {
2012-04-19 19:04:21 +03:00
return name;
2016-10-30 18:24:19 +01:00
}
2020-08-30 11:54:41 +02:00
path += cmStrCat('/', name);
2012-04-19 19:04:21 +03:00
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
}
2020-08-30 11:54:41 +02:00
bool cmNinjaTargetGenerator::SetMsvcTargetPdbVariable(
cmNinjaVars& vars, const std::string& config) 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") ||
2017-04-14 19:02:05 +02:00
mf->GetDefinition("MSVC_CXX_ARCHITECTURE_ID") ||
mf->GetDefinition("MSVC_CUDA_ARCHITECTURE_ID")) {
2013-03-16 19:13:01 +02:00
std::string pdbPath;
2020-08-30 11:54:41 +02:00
std::string compilePdbPath = this->ComputeTargetCompilePDB(config);
2017-04-14 19:02:05 +02:00
if (this->GeneratorTarget->GetType() == cmStateEnums::EXECUTABLE ||
this->GeneratorTarget->GetType() == cmStateEnums::STATIC_LIBRARY ||
this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
2020-08-30 11:54:41 +02:00
pdbPath = cmStrCat(this->GeneratorTarget->GetPDBDirectory(config), '/',
this->GeneratorTarget->GetPDBName(config));
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
}
2020-08-30 11:54:41 +02:00
void cmNinjaTargetGenerator::WriteLanguageRules(const std::string& language,
const std::string& config)
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
2020-08-30 11:54:41 +02:00
this->WriteCompileRule(language, config);
}
namespace {
// Create the command to run the dependency scanner
std::string GetScanCommand(const std::string& cmakeCmd, const std::string& tdi,
const std::string& lang, const std::string& ppFile,
bool needDyndep, const std::string& ddiFile)
{
std::string ccmd =
cmStrCat(cmakeCmd, " -E cmake_ninja_depends --tdi=", tdi, " --lang=", lang,
" --pp=", ppFile, " --dep=$DEP_FILE");
if (needDyndep) {
ccmd = cmStrCat(ccmd, " --obj=$OBJ_FILE --ddi=", ddiFile);
}
return ccmd;
}
// Helper function to create dependency scanning rule, with optional
// explicit preprocessing step if preprocessCommand is non-empty
cmNinjaRule GetPreprocessScanRule(
const std::string& ruleName, cmRulePlaceholderExpander::RuleVariables& vars,
const std::string& responseFlag, const std::string& flags,
const std::string& launcher,
cmRulePlaceholderExpander* const rulePlaceholderExpander,
std::string scanCommand, cmLocalNinjaGenerator* generator,
const std::string& preprocessCommand = "")
{
cmNinjaRule rule(ruleName);
// Explicit preprocessing always uses a depfile.
rule.DepType = ""; // no deps= for multiple outputs
rule.DepFile = "$DEP_FILE";
cmRulePlaceholderExpander::RuleVariables ppVars;
ppVars.CMTargetName = vars.CMTargetName;
ppVars.CMTargetType = vars.CMTargetType;
ppVars.Language = vars.Language;
ppVars.Object = "$out"; // for RULE_LAUNCH_COMPILE
ppVars.PreprocessedSource = "$out";
ppVars.DependencyFile = rule.DepFile.c_str();
// Preprocessing uses the original source, compilation uses
// preprocessed output or original source
ppVars.Source = vars.Source;
vars.Source = "$in";
// Copy preprocessor definitions to the preprocessor rule.
ppVars.Defines = vars.Defines;
// Copy include directories to the preprocessor rule. The Fortran
// compilation rule still needs them for the INCLUDE directive.
ppVars.Includes = vars.Includes;
// Preprocessing and compilation use the same flags.
std::string ppFlags = flags;
// If using a response file, move defines, includes, and flags into it.
if (!responseFlag.empty()) {
rule.RspFile = "$RSP_FILE";
rule.RspContent =
cmStrCat(' ', ppVars.Defines, ' ', ppVars.Includes, ' ', ppFlags);
ppFlags = cmStrCat(responseFlag, rule.RspFile);
ppVars.Defines = "";
ppVars.Includes = "";
}
ppVars.Flags = ppFlags.c_str();
// Rule for preprocessing source file.
std::vector<std::string> ppCmds;
if (!preprocessCommand.empty()) {
// Lookup the explicit preprocessing rule.
cmExpandList(preprocessCommand, ppCmds);
for (std::string& i : ppCmds) {
i = cmStrCat(launcher, i);
rulePlaceholderExpander->ExpandRuleVariables(generator, i, ppVars);
}
}
// Run CMake dependency scanner on either preprocessed output or source file
ppCmds.emplace_back(std::move(scanCommand));
rule.Command = generator->BuildCommandLine(ppCmds);
return rule;
}
2012-04-19 19:04:21 +03:00
}
2020-08-30 11:54:41 +02:00
void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang,
const std::string& config)
2012-04-19 19:04:21 +03:00
{
2017-04-14 19:02:05 +02:00
cmRulePlaceholderExpander::RuleVariables vars;
vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
vars.CMTargetType =
2020-08-30 11:54:41 +02:00
cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()).c_str();
2012-04-19 19:04:21 +03:00
vars.Language = lang.c_str();
2017-04-14 19:02:05 +02:00
vars.Source = "$in";
2012-04-19 19:04:21 +03:00
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
2020-02-01 23:06:01 +01:00
cmMakefile* mf = this->GetMakefile();
2016-10-30 18:24:19 +01:00
// For some cases we do an explicit preprocessor invocation.
bool const explicitPP = this->NeedExplicitPreprocessing(lang);
2020-02-01 23:06:01 +01:00
bool const compilePPWithDefines = this->UsePreprocessedSource(lang) &&
this->CompilePreprocessedSourceWithDefines(lang);
2016-10-30 18:24:19 +01:00
bool const needDyndep = this->NeedDyndep(lang);
2016-07-09 11:21:54 +02:00
std::string flags = "$FLAGS";
2018-08-09 18:06:22 +02:00
std::string responseFlag;
2019-11-11 23:01:05 +01:00
bool const lang_supports_response = lang != "RC";
2017-04-14 19:02:05 +02:00
if (lang_supports_response && this->ForceResponseFile()) {
2018-04-23 21:13:27 +02:00
std::string const responseFlagVar =
2020-08-30 11:54:41 +02:00
cmStrCat("CMAKE_", lang, "_RESPONSE_FILE_FLAG");
2018-08-09 18:06:22 +02:00
responseFlag = this->Makefile->GetSafeDefinition(responseFlagVar);
2019-11-11 23:01:05 +01:00
if (responseFlag.empty() && lang != "CUDA") {
2018-04-23 21:13:27 +02:00
responseFlag = "@";
}
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
2017-04-14 19:02:05 +02:00
this->GetLocalGenerator()->CreateRulePlaceholderExpander());
2016-10-30 18:24:19 +01:00
std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
2020-08-30 11:54:41 +02:00
ConvertToNinjaPath(this->GetTargetDependInfoPath(lang, config)),
2016-10-30 18:24:19 +01:00
cmLocalGenerator::SHELL);
2017-04-14 19:02:05 +02:00
std::string launcher;
const char* val = this->GetLocalGenerator()->GetRuleLauncher(
this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE");
if (val && *val) {
2020-02-01 23:06:01 +01:00
launcher = cmStrCat(val, ' ');
2017-04-14 19:02:05 +02:00
}
2019-11-11 23:01:05 +01:00
std::string const cmakeCmd =
this->GetLocalGenerator()->ConvertToOutputFormat(
cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
2016-10-30 18:24:19 +01:00
2019-11-11 23:01:05 +01:00
if (explicitPP) {
2020-08-30 11:54:41 +02:00
// Combined preprocessing and dependency scanning
const auto ppScanCommand = GetScanCommand(
cmakeCmd, tdi, lang, "$out", needDyndep, "$DYNDEP_INTERMEDIATE_FILE");
const auto ppVar = cmStrCat("CMAKE_", lang, "_PREPROCESS_SOURCE");
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
auto ppRule = GetPreprocessScanRule(
this->LanguagePreprocessRule(lang, config), vars, responseFlag, flags,
launcher, rulePlaceholderExpander.get(), ppScanCommand,
this->GetLocalGenerator(), mf->GetRequiredDefinition(ppVar));
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
// Write the rule for preprocessing file of the given language.
ppRule.Comment = cmStrCat("Rule for preprocessing ", lang, " files.");
ppRule.Description = cmStrCat("Building ", lang, " preprocessed $out");
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
this->GetGlobalGenerator()->AddRule(ppRule);
2016-10-30 18:24:19 +01:00
2020-02-01 23:06:01 +01:00
if (!compilePPWithDefines) {
2020-08-30 11:54:41 +02:00
// Remove preprocessor definitions from compilation step
2020-02-01 23:06:01 +01:00
vars.Defines = "";
}
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
// Just dependency scanning for files that have preprocessing turned off
const auto scanCommand =
GetScanCommand(cmakeCmd, tdi, lang, "$in", needDyndep, "$out");
2018-08-09 18:06:22 +02:00
2020-08-30 11:54:41 +02:00
auto scanRule = GetPreprocessScanRule(
this->LanguageDependencyRule(lang, config), vars, "", flags, launcher,
rulePlaceholderExpander.get(), scanCommand, this->GetLocalGenerator());
2018-08-09 18:06:22 +02:00
2020-08-30 11:54:41 +02:00
// Write the rule for generating dependencies for the given language.
scanRule.Comment = cmStrCat("Rule for generating ", lang,
" dependencies on non-preprocessed files.");
scanRule.Description =
cmStrCat("Generating ", lang, " dependencies for $in");
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
this->GetGlobalGenerator()->AddRule(scanRule);
2016-10-30 18:24:19 +01:00
}
if (needDyndep) {
// Write the rule for ninja dyndep file generation.
2020-08-30 11:54:41 +02:00
cmNinjaRule rule(this->LanguageDyndepRule(lang, config));
2018-01-26 17:06:56 +01:00
// Command line length is almost always limited -> use response file for
// dyndep rules
2019-11-11 23:01:05 +01:00
rule.RspFile = "$out.rsp";
rule.RspContent = "$in";
// Run CMake dependency scanner on the source file (using the preprocessed
// source if that was performed).
{
std::vector<std::string> ddCmds;
{
2020-02-01 23:06:01 +01:00
std::string ccmd =
cmStrCat(cmakeCmd, " -E cmake_ninja_dyndep --tdi=", tdi,
" --lang=", lang, " --dd=$out @", rule.RspFile);
2019-11-11 23:01:05 +01:00
ddCmds.emplace_back(std::move(ccmd));
}
rule.Command = this->GetLocalGenerator()->BuildCommandLine(ddCmds);
}
2020-02-01 23:06:01 +01:00
rule.Comment =
cmStrCat("Rule to generate ninja dyndep files for ", lang, '.');
rule.Description = cmStrCat("Generating ", lang, " dyndep file $out");
2019-11-11 23:01:05 +01:00
this->GetGlobalGenerator()->AddRule(rule);
2016-10-30 18:24:19 +01:00
}
2020-08-30 11:54:41 +02:00
cmNinjaRule rule(this->LanguageCompilerRule(lang, config));
2018-08-09 18:06:22 +02:00
// If using a response file, move defines, includes, and flags into it.
if (!responseFlag.empty()) {
2019-11-11 23:01:05 +01:00
rule.RspFile = "$RSP_FILE";
2020-02-01 23:06:01 +01:00
rule.RspContent =
cmStrCat(' ', vars.Defines, ' ', vars.Includes, ' ', flags);
2020-08-30 11:54:41 +02:00
flags = cmStrCat(responseFlag, rule.RspFile);
2018-08-09 18:06:22 +02:00
vars.Defines = "";
vars.Includes = "";
}
// Tell ninja dependency format so all deps can be loaded into a database
std::string cldeps;
if (explicitPP) {
// The explicit preprocessing step will handle dependency scanning.
} else if (this->NeedDepTypeMSVC(lang)) {
2019-11-11 23:01:05 +01:00
rule.DepType = "msvc";
rule.DepFile.clear();
2018-08-09 18:06:22 +02:00
flags += " /showIncludes";
2020-08-30 11:54:41 +02:00
} else if (mf->IsOn(cmStrCat("CMAKE_NINJA_CMCLDEPS_", lang))) {
2018-08-09 18:06:22 +02:00
// For the MS resource compiler we need cmcldeps, but skip dependencies
// for source-file try_compile cases because they are always fresh.
if (!mf->GetIsSourceFileTryCompile()) {
2019-11-11 23:01:05 +01:00
rule.DepType = "gcc";
rule.DepFile = "$DEP_FILE";
2020-08-30 11:54:41 +02:00
auto d = mf->GetDefinition("CMAKE_C_COMPILER");
const std::string cl =
d ? d : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
2020-02-01 23:06:01 +01:00
cldeps = cmStrCat('"', cmSystemTools::GetCMClDepsCommand(), "\" ", lang,
' ', vars.Source, " $DEP_FILE $out \"",
mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX"),
"\" \"", cl, "\" ");
2018-08-09 18:06:22 +02:00
}
} else {
2019-11-11 23:01:05 +01:00
rule.DepType = "gcc";
rule.DepFile = "$DEP_FILE";
2020-08-30 11:54:41 +02:00
const std::string flagsName = cmStrCat("CMAKE_DEPFILE_FLAGS_", lang);
2018-08-09 18:06:22 +02:00
std::string depfileFlags = mf->GetSafeDefinition(flagsName);
if (!depfileFlags.empty()) {
cmSystemTools::ReplaceString(depfileFlags, "<DEPFILE>", "$DEP_FILE");
cmSystemTools::ReplaceString(depfileFlags, "<OBJECT>", "$out");
cmSystemTools::ReplaceString(depfileFlags, "<CMAKE_C_COMPILER>",
mf->GetDefinition("CMAKE_C_COMPILER"));
2020-08-30 11:54:41 +02:00
flags += cmStrCat(' ', depfileFlags);
2018-08-09 18:06:22 +02:00
}
}
vars.Flags = flags.c_str();
2019-11-11 23:01:05 +01:00
vars.DependencyFile = rule.DepFile.c_str();
2018-08-09 18:06:22 +02:00
2012-04-19 19:04:21 +03:00
// Rule for compiling object file.
std::vector<std::string> compileCmds;
2017-04-14 19:02:05 +02:00
if (lang == "CUDA") {
std::string cmdVar;
if (this->GeneratorTarget->GetPropertyAsBool(
"CUDA_SEPARABLE_COMPILATION")) {
2019-11-11 23:01:05 +01:00
cmdVar = "CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION";
2017-07-20 19:35:53 +02:00
} else if (this->GeneratorTarget->GetPropertyAsBool(
"CUDA_PTX_COMPILATION")) {
2019-11-11 23:01:05 +01:00
cmdVar = "CMAKE_CUDA_COMPILE_PTX_COMPILATION";
2017-04-14 19:02:05 +02:00
} else {
2019-11-11 23:01:05 +01:00
cmdVar = "CMAKE_CUDA_COMPILE_WHOLE_COMPILATION";
2017-04-14 19:02:05 +02:00
}
2019-11-11 23:01:05 +01:00
const std::string& compileCmd = mf->GetRequiredDefinition(cmdVar);
2020-02-01 23:06:01 +01:00
cmExpandList(compileCmd, compileCmds);
2017-04-14 19:02:05 +02:00
} else {
2020-08-30 11:54:41 +02:00
const std::string cmdVar = cmStrCat("CMAKE_", lang, "_COMPILE_OBJECT");
2019-11-11 23:01:05 +01:00
const std::string& compileCmd = mf->GetRequiredDefinition(cmdVar);
2020-02-01 23:06:01 +01:00
cmExpandList(compileCmd, compileCmds);
2017-04-14 19:02:05 +02:00
}
2012-04-19 19:04:21 +03:00
2018-08-09 18:06:22 +02:00
// See if we need to use a compiler launcher like ccache or distcc
std::string compilerLauncher;
if (!compileCmds.empty() &&
2020-02-01 23:06:01 +01:00
(lang == "C" || lang == "CXX" || lang == "Fortran" || lang == "CUDA" ||
lang == "OBJC" || lang == "OBJCXX")) {
2020-08-30 11:54:41 +02:00
std::string const clauncher_prop = cmStrCat(lang, "_COMPILER_LAUNCHER");
cmProp clauncher = this->GeneratorTarget->GetProperty(clauncher_prop);
if (clauncher && !clauncher->empty()) {
compilerLauncher = *clauncher;
2018-08-09 18:06:22 +02:00
}
}
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")) {
2020-08-30 11:54:41 +02:00
std::string const iwyu_prop = cmStrCat(lang, "_INCLUDE_WHAT_YOU_USE");
cmProp iwyu = this->GeneratorTarget->GetProperty(iwyu_prop);
std::string const tidy_prop = cmStrCat(lang, "_CLANG_TIDY");
cmProp tidy = this->GeneratorTarget->GetProperty(tidy_prop);
std::string const cpplint_prop = cmStrCat(lang, "_CPPLINT");
cmProp cpplint = this->GeneratorTarget->GetProperty(cpplint_prop);
std::string const cppcheck_prop = cmStrCat(lang, "_CPPCHECK");
cmProp cppcheck = this->GeneratorTarget->GetProperty(cppcheck_prop);
if ((iwyu && !iwyu->empty()) || (tidy && !tidy->empty()) ||
(cpplint && !cpplint->empty()) || (cppcheck && !cppcheck->empty())) {
2020-02-01 23:06:01 +01:00
std::string run_iwyu = cmStrCat(cmakeCmd, " -E __run_co_compile");
2018-08-09 18:06:22 +02:00
if (!compilerLauncher.empty()) {
// In __run_co_compile case the launcher command is supplied
// via --launcher=<maybe-list> and consumed
2020-08-30 11:54:41 +02:00
run_iwyu +=
cmStrCat(" --launcher=",
this->LocalGenerator->EscapeForShell(compilerLauncher));
2018-08-09 18:06:22 +02:00
compilerLauncher.clear();
}
2020-08-30 11:54:41 +02:00
if (iwyu && !iwyu->empty()) {
run_iwyu += cmStrCat(" --iwyu=",
this->GetLocalGenerator()->EscapeForShell(*iwyu));
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
if (tidy && !tidy->empty()) {
2016-07-09 11:21:54 +02:00
run_iwyu += " --tidy=";
2020-08-30 11:54:41 +02:00
const char* driverMode = this->Makefile->GetDefinition(
cmStrCat("CMAKE_", lang, "_CLANG_TIDY_DRIVER_MODE"));
if (!(driverMode && *driverMode)) {
driverMode = lang == "C" ? "gcc" : "g++";
}
run_iwyu += this->GetLocalGenerator()->EscapeForShell(
cmStrCat(*tidy, ";--extra-arg-before=--driver-mode=", driverMode));
2017-04-14 19:02:05 +02:00
}
2020-08-30 11:54:41 +02:00
if (cpplint && !cpplint->empty()) {
run_iwyu += cmStrCat(
" --cpplint=", this->GetLocalGenerator()->EscapeForShell(*cpplint));
2017-04-14 19:02:05 +02:00
}
2020-08-30 11:54:41 +02:00
if (cppcheck && !cppcheck->empty()) {
run_iwyu +=
cmStrCat(" --cppcheck=",
this->GetLocalGenerator()->EscapeForShell(*cppcheck));
2018-01-26 17:06:56 +01:00
}
2020-08-30 11:54:41 +02:00
if ((tidy && !tidy->empty()) || (cpplint && !cpplint->empty()) ||
(cppcheck && !cppcheck->empty())) {
2016-07-09 11:21:54 +02:00
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
2018-08-09 18:06:22 +02:00
// If compiler launcher was specified and not consumed above, it
// goes to the beginning of the command line.
if (!compileCmds.empty() && !compilerLauncher.empty()) {
2020-02-01 23:06:01 +01:00
std::vector<std::string> args = cmExpandedList(compilerLauncher, true);
2019-11-11 23:01:05 +01:00
if (!args.empty()) {
args[0] = this->LocalGenerator->ConvertToOutputFormat(
args[0], cmOutputConverter::SHELL);
for (std::string& i : cmMakeRange(args.begin() + 1, args.end())) {
i = this->LocalGenerator->EscapeForShell(i);
}
2015-11-17 17:22:37 +01:00
}
2020-08-30 11:54:41 +02:00
compileCmds.front().insert(0, cmStrCat(cmJoin(args, " "), ' '));
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
2018-01-26 17:06:56 +01:00
for (std::string& i : compileCmds) {
2020-02-01 23:06:01 +01:00
i = cmStrCat(launcher, i);
2018-01-26 17:06:56 +01:00
rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
2017-04-14 19:02:05 +02:00
vars);
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
2019-11-11 23:01:05 +01:00
rule.Command = this->GetLocalGenerator()->BuildCommandLine(compileCmds);
2012-04-19 19:04:21 +03:00
// Write the rule for compiling file of the given language.
2020-02-01 23:06:01 +01:00
rule.Comment = cmStrCat("Rule for compiling ", lang, " files.");
rule.Description = cmStrCat("Building ", lang, " object $out");
2019-11-11 23:01:05 +01:00
this->GetGlobalGenerator()->AddRule(rule);
2012-04-19 19:04:21 +03:00
}
2020-08-30 11:54:41 +02:00
void cmNinjaTargetGenerator::WriteObjectBuildStatements(
const std::string& config, const std::string& fileConfig,
bool firstForConfig)
2012-04-19 19:04:21 +03:00
{
// Write comments.
2020-08-30 11:54:41 +02:00
cmGlobalNinjaGenerator::WriteDivider(this->GetImplFileStream(fileConfig));
this->GetImplFileStream(fileConfig)
2012-04-19 19:04:21 +03:00
<< "# 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
2019-11-11 23:01:05 +01:00
{
std::vector<cmSourceFile const*> customCommands;
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetCustomCommands(customCommands, config);
2019-11-11 23:01:05 +01:00
for (cmSourceFile const* sf : customCommands) {
cmCustomCommand const* cc = sf->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.
2020-08-30 11:54:41 +02:00
this->Configs[config].CustomCommands.push_back(cc);
2019-11-11 23:01:05 +01:00
}
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
{
std::vector<cmSourceFile const*> headerSources;
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetHeaderSources(headerSources, config);
2019-11-11 23:01:05 +01:00
this->OSXBundleGenerator->GenerateMacOSXContentStatements(
2020-08-30 11:54:41 +02:00
headerSources, this->Configs[fileConfig].MacOSXContentGenerator.get(),
config);
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
{
std::vector<cmSourceFile const*> extraSources;
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetExtraSources(extraSources, config);
2019-11-11 23:01:05 +01:00
this->OSXBundleGenerator->GenerateMacOSXContentStatements(
2020-08-30 11:54:41 +02:00
extraSources, this->Configs[fileConfig].MacOSXContentGenerator.get(),
config);
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
if (firstForConfig) {
2020-02-01 23:06:01 +01:00
const char* pchExtension =
GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
2019-11-11 23:01:05 +01:00
std::vector<cmSourceFile const*> externalObjects;
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetExternalObjects(externalObjects, config);
2019-11-11 23:01:05 +01:00
for (cmSourceFile const* sf : externalObjects) {
2020-08-30 11:54:41 +02:00
auto objectFileName = this->GetGlobalGenerator()->ExpandCFGIntDir(
this->GetSourceFilePath(sf), config);
2020-02-01 23:06:01 +01:00
if (!cmSystemTools::StringEndsWith(objectFileName, pchExtension)) {
2020-08-30 11:54:41 +02:00
this->Configs[config].Objects.push_back(objectFileName);
2020-02-01 23:06:01 +01:00
}
2019-11-11 23:01:05 +01:00
}
2018-08-09 18:06:22 +02:00
}
2017-07-20 19:35:53 +02:00
{
2019-11-11 23:01:05 +01:00
cmNinjaBuild build("phony");
2020-08-30 11:54:41 +02:00
build.Comment =
cmStrCat("Order-only phony target for ", this->GetTargetName());
build.Outputs.push_back(this->OrderDependsTargetForTarget(config));
2019-11-11 23:01:05 +01:00
cmNinjaDeps& orderOnlyDeps = build.OrderOnlyDeps;
this->GetLocalGenerator()->AppendTargetDepends(
2020-08-30 11:54:41 +02:00
this->GeneratorTarget, orderOnlyDeps, config, fileConfig,
DependOnTargetOrdering);
2019-11-11 23:01:05 +01:00
// Add order-only dependencies on other files associated with the target.
2020-08-30 11:54:41 +02:00
cm::append(orderOnlyDeps, this->Configs[config].ExtraFiles);
2019-11-11 23:01:05 +01:00
// Add order-only dependencies on custom command outputs.
2020-08-30 11:54:41 +02:00
for (cmCustomCommand const* cc : this->Configs[config].CustomCommands) {
cmCustomCommandGenerator ccg(*cc, config, this->GetLocalGenerator());
2019-11-11 23:01:05 +01:00
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(orderOnlyDeps), MapToNinjaPath());
std::transform(ccbyproducts.begin(), ccbyproducts.end(),
std::back_inserter(orderOnlyDeps), MapToNinjaPath());
}
std::sort(orderOnlyDeps.begin(), orderOnlyDeps.end());
orderOnlyDeps.erase(
std::unique(orderOnlyDeps.begin(), orderOnlyDeps.end()),
orderOnlyDeps.end());
// The phony target must depend on at least one input or ninja will explain
// that "output ... of phony edge with no inputs doesn't exist" and
// consider the phony output "dirty".
if (orderOnlyDeps.empty()) {
// Any path that always exists will work here. It would be nice to
// use just "." but that is not supported by Ninja < 1.7.
2020-02-01 23:06:01 +01:00
std::string tgtDir = cmStrCat(
this->LocalGenerator->GetCurrentBinaryDirectory(), '/',
this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget));
2019-11-11 23:01:05 +01:00
orderOnlyDeps.push_back(this->ConvertToNinjaPath(tgtDir));
}
2020-08-30 11:54:41 +02:00
this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
build);
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
{
std::vector<cmSourceFile const*> objectSources;
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetObjectSources(objectSources, config);
2019-11-11 23:01:05 +01:00
for (cmSourceFile const* sf : objectSources) {
2020-08-30 11:54:41 +02:00
this->WriteObjectBuildStatement(sf, config, fileConfig, firstForConfig);
2019-11-11 23:01:05 +01: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
for (auto const& langDDIFiles : this->Configs[config].DDIFiles) {
2019-11-11 23:01:05 +01:00
std::string const& language = langDDIFiles.first;
cmNinjaDeps const& ddiFiles = langDDIFiles.second;
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
cmNinjaBuild build(this->LanguageDyndepRule(language, config));
build.Outputs.push_back(this->GetDyndepFilePath(language, config));
2019-11-11 23:01:05 +01:00
build.ExplicitDeps = ddiFiles;
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
this->WriteTargetDependInfo(language, config);
2016-10-30 18:24:19 +01:00
2017-07-20 19:35:53 +02:00
// Make sure dyndep files for all our dependencies have already
2019-11-11 23:01:05 +01:00
// been generated so that the '<LANG>Modules.json' files they
2017-07-20 19:35:53 +02:00
// produced as side-effects are available for us to read.
2019-11-11 23:01:05 +01:00
// Ideally we should depend on the '<LANG>Modules.json' files
2017-07-20 19:35:53 +02:00
// from our dependencies directly, but we don't know which of
// our dependencies produces them. Fixing this will require
// refactoring the Ninja generator to generate targets in
// dependency order so that we can collect the needed information.
this->GetLocalGenerator()->AppendTargetDepends(
2020-08-30 11:54:41 +02:00
this->GeneratorTarget, build.OrderOnlyDeps, config, fileConfig,
DependOnTargetArtifact);
2017-07-20 19:35:53 +02:00
2020-08-30 11:54:41 +02:00
this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
build);
2016-10-30 18:24:19 +01:00
}
2020-08-30 11:54:41 +02:00
this->GetImplFileStream(fileConfig) << "\n";
2019-11-11 23:01:05 +01:00
2020-08-30 11:54:41 +02:00
if (!this->Configs[config].SwiftOutputMap.empty()) {
2020-02-01 23:06:01 +01:00
std::string const mapFilePath =
2020-08-30 11:54:41 +02:00
cmStrCat(this->GeneratorTarget->GetSupportDirectory(), '/', config, '/',
"output-file-map.json");
std::string const targetSwiftDepsPath = [this, config]() -> std::string {
2019-11-11 23:01:05 +01:00
cmGeneratorTarget const* target = this->GeneratorTarget;
2020-08-30 11:54:41 +02:00
if (cmProp name = target->GetProperty("Swift_DEPENDENCIES_FILE")) {
return *name;
2019-11-11 23:01:05 +01:00
}
2020-08-30 11:54:41 +02:00
return this->ConvertToNinjaPath(
cmStrCat(target->GetSupportDirectory(), '/', config, '/',
target->GetName(), ".swiftdeps"));
2019-11-11 23:01:05 +01:00
}();
// build the global target dependencies
// https://github.com/apple/swift/blob/master/docs/Driver.md#output-file-maps
Json::Value deps(Json::objectValue);
deps["swift-dependencies"] = targetSwiftDepsPath;
2020-08-30 11:54:41 +02:00
this->Configs[config].SwiftOutputMap[""] = deps;
2019-11-11 23:01:05 +01:00
cmGeneratedFileStream output(mapFilePath);
2020-08-30 11:54:41 +02:00
output << this->Configs[config].SwiftOutputMap;
2019-11-11 23:01:05 +01:00
}
2012-04-19 19:04:21 +03:00
}
2020-08-30 11:54:41 +02:00
namespace {
cmNinjaBuild GetPreprocessOrScanBuild(
const std::string& ruleName, const std::string& ppFileName, bool compilePP,
bool compilePPWithDefines, cmNinjaBuild& objBuild, cmNinjaVars& vars,
const std::string& depFileName, bool needDyndep,
const std::string& objectFileName)
{
// Explicit preprocessing and dependency
cmNinjaBuild ppBuild(ruleName);
if (!ppFileName.empty()) {
ppBuild.Outputs.push_back(ppFileName);
ppBuild.RspFile = cmStrCat(ppFileName, ".rsp");
} else {
ppBuild.RspFile = "$out.rsp";
}
if (compilePP) {
// Move compilation dependencies to the preprocessing build statement.
std::swap(ppBuild.ExplicitDeps, objBuild.ExplicitDeps);
std::swap(ppBuild.ImplicitDeps, objBuild.ImplicitDeps);
std::swap(ppBuild.OrderOnlyDeps, objBuild.OrderOnlyDeps);
std::swap(ppBuild.Variables["IN_ABS"], vars["IN_ABS"]);
// The actual compilation will now use the preprocessed source.
objBuild.ExplicitDeps.push_back(ppFileName);
} else {
// Copy compilation dependencies to the preprocessing build statement.
ppBuild.ExplicitDeps = objBuild.ExplicitDeps;
ppBuild.ImplicitDeps = objBuild.ImplicitDeps;
ppBuild.OrderOnlyDeps = objBuild.OrderOnlyDeps;
ppBuild.Variables["IN_ABS"] = vars["IN_ABS"];
}
// Preprocessing and compilation generally use the same flags.
ppBuild.Variables["FLAGS"] = vars["FLAGS"];
if (compilePP && !compilePPWithDefines) {
// Move preprocessor definitions to the preprocessor build statement.
std::swap(ppBuild.Variables["DEFINES"], vars["DEFINES"]);
} else {
// Copy preprocessor definitions to the preprocessor build statement.
ppBuild.Variables["DEFINES"] = vars["DEFINES"];
}
// Copy include directories to the preprocessor build statement. The
// Fortran compilation build statement still needs them for the INCLUDE
// directive.
ppBuild.Variables["INCLUDES"] = vars["INCLUDES"];
// Explicit preprocessing always uses a depfile.
ppBuild.Variables["DEP_FILE"] = depFileName;
if (compilePP) {
// The actual compilation does not need a depfile because it
// depends on the already-preprocessed source.
vars.erase("DEP_FILE");
}
if (needDyndep) {
// Tell dependency scanner the object file that will result from
// compiling the source.
ppBuild.Variables["OBJ_FILE"] = objectFileName;
// Tell dependency scanner where to store dyndep intermediate results.
std::string const ddiFile = cmStrCat(objectFileName, ".ddi");
if (ppFileName.empty()) {
ppBuild.Outputs.push_back(ddiFile);
} else {
ppBuild.Variables["DYNDEP_INTERMEDIATE_FILE"] = ddiFile;
ppBuild.ImplicitOuts.push_back(ddiFile);
}
}
return ppBuild;
}
}
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::WriteObjectBuildStatement(
2020-08-30 11:54:41 +02:00
cmSourceFile const* source, const std::string& config,
const std::string& fileConfig, bool firstForConfig)
2012-04-19 19:04:21 +03:00
{
2015-11-17 17:22:37 +01:00
std::string const language = source->GetLanguage();
2017-04-14 19:02:05 +02:00
std::string const sourceFileName =
language == "RC" ? source->GetFullPath() : this->GetSourceFilePath(source);
2020-08-30 11:54:41 +02:00
std::string const objectDir = this->ConvertToNinjaPath(
cmStrCat(this->GeneratorTarget->GetSupportDirectory(),
this->GetGlobalGenerator()->ConfigDirectory(config)));
2016-07-09 11:21:54 +02:00
std::string const objectFileName =
2020-08-30 11:54:41 +02:00
this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
2015-11-17 17:22:37 +01:00
std::string const objectFileDir =
cmSystemTools::GetFilenamePath(objectFileName);
2020-02-01 23:06:01 +01:00
std::string cmakeVarLang = cmStrCat("CMAKE_", language);
2019-11-11 23:01:05 +01:00
// build response file name
2020-08-30 11:54:41 +02:00
std::string cmakeLinkVar = cmStrCat(cmakeVarLang, "_RESPONSE_FILE_FLAG");
2019-11-11 23:01:05 +01:00
const char* flag = GetMakefile()->GetDefinition(cmakeLinkVar);
2018-08-09 18:06:22 +02:00
bool const lang_supports_response =
2019-11-11 23:01:05 +01:00
!(language == "RC" || (language == "CUDA" && !flag));
2018-08-09 18:06:22 +02:00
int const commandLineLengthLimit =
((lang_supports_response && this->ForceResponseFile())) ? -1 : 0;
2020-08-30 11:54:41 +02:00
cmNinjaBuild objBuild(this->LanguageCompilerRule(language, config));
2019-11-11 23:01:05 +01:00
cmNinjaVars& vars = objBuild.Variables;
2020-08-30 11:54:41 +02:00
vars["FLAGS"] = this->ComputeFlagsForObject(source, language, config);
vars["DEFINES"] = this->ComputeDefines(source, language, config);
vars["INCLUDES"] = this->ComputeIncludes(source, language, config);
2018-08-09 18:06:22 +02:00
2016-07-09 11:21:54 +02:00
if (!this->NeedDepTypeMSVC(language)) {
2018-08-09 18:06:22 +02:00
bool replaceExt(false);
if (!language.empty()) {
2020-02-01 23:06:01 +01:00
std::string repVar =
cmStrCat("CMAKE_", language, "_DEPFILE_EXTENSION_REPLACE");
2018-08-09 18:06:22 +02:00
replaceExt = this->Makefile->IsOn(repVar);
}
if (!replaceExt) {
// use original code
vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
2020-08-30 11:54:41 +02:00
cmStrCat(objectFileName, ".d"), cmOutputConverter::SHELL);
2018-08-09 18:06:22 +02:00
} else {
// Replace the original source file extension with the
// depend file extension.
2020-08-30 11:54:41 +02:00
std::string dependFileName = cmStrCat(
cmSystemTools::GetFilenameWithoutLastExtension(objectFileName), ".d");
2018-08-09 18:06:22 +02:00
vars["DEP_FILE"] = this->GetLocalGenerator()->ConvertToOutputFormat(
2020-08-30 11:54:41 +02:00
cmStrCat(objectFileDir, '/', dependFileName),
cmOutputConverter::SHELL);
2018-08-09 18:06:22 +02:00
}
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
2019-11-11 23:01:05 +01:00
objBuild.Outputs.push_back(objectFileName);
2020-08-30 11:54:41 +02:00
if (firstForConfig) {
const char* pchExtension =
this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
if (!cmSystemTools::StringEndsWith(objectFileName, pchExtension)) {
// Add this object to the list of object files.
this->Configs[config].Objects.push_back(objectFileName);
}
2020-02-01 23:06:01 +01:00
}
2012-04-19 19:04:21 +03:00
2019-11-11 23:01:05 +01:00
objBuild.ExplicitDeps.push_back(sourceFileName);
2012-04-19 19:04:21 +03:00
2020-02-01 23:06:01 +01:00
// Add precompile headers dependencies
std::vector<std::string> depList;
2020-08-30 11:54:41 +02:00
std::vector<std::string> architectures;
this->GeneratorTarget->GetAppleArchs(config, architectures);
if (architectures.empty()) {
architectures.emplace_back();
}
std::unordered_set<std::string> pchSources;
for (const std::string& arch : architectures) {
const std::string pchSource =
this->GeneratorTarget->GetPchSource(config, language, arch);
if (!pchSource.empty()) {
pchSources.insert(pchSource);
}
}
if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
for (const std::string& arch : architectures) {
2020-02-01 23:06:01 +01:00
depList.push_back(
2020-08-30 11:54:41 +02:00
this->GeneratorTarget->GetPchHeader(config, language, arch));
if (pchSources.find(source->GetFullPath()) == pchSources.end()) {
depList.push_back(
this->GeneratorTarget->GetPchFile(config, language, arch));
}
2020-02-01 23:06:01 +01:00
}
}
2020-08-30 11:54:41 +02:00
if (cmProp objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
std::vector<std::string> objDepList = cmExpandedList(*objectDeps);
2020-02-01 23:06:01 +01:00
std::copy(objDepList.begin(), objDepList.end(),
std::back_inserter(depList));
}
if (!depList.empty()) {
2018-01-26 17:06:56 +01:00
for (std::string& odi : depList) {
if (cmSystemTools::FileIsFullPath(odi)) {
odi = cmSystemTools::CollapseFullPath(odi);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
std::transform(depList.begin(), depList.end(),
2019-11-11 23:01:05 +01:00
std::back_inserter(objBuild.ImplicitDeps),
MapToNinjaPath());
2012-04-19 19:04:21 +03:00
}
2020-08-30 11:54:41 +02:00
objBuild.OrderOnlyDeps.push_back(this->OrderDependsTargetForTarget(config));
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.
2019-11-11 23:01:05 +01:00
if (source->GetIsGenerated() &&
2018-05-19 10:45:57 +02:00
!source->GetPropertyAsBool("__CMAKE_GENERATED_BY_CMAKE") &&
!source->GetCustomCommand() &&
2012-04-19 19:04:21 +03:00
!this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFileName)) {
2019-11-11 23:01:05 +01:00
this->GetGlobalGenerator()->AddAssumedSourceDependencies(
sourceFileName, objBuild.OrderOnlyDeps);
2012-04-19 19:04:21 +03:00
}
2016-10-30 18:24:19 +01:00
// For some cases we need to generate a ninja dyndep file.
bool const needDyndep = this->NeedDyndep(language);
// For some cases we do an explicit preprocessor invocation.
bool const explicitPP = this->NeedExplicitPreprocessing(language);
if (explicitPP) {
2020-08-30 11:54:41 +02:00
// If source/target has preprocessing turned off, we still need to
// generate an explicit dependency step
const auto srcpp = source->GetSafeProperty("Fortran_PREPROCESS");
cmOutputConverter::FortranPreprocess preprocess =
cmOutputConverter::GetFortranPreprocess(srcpp);
if (preprocess == cmOutputConverter::FortranPreprocess::Unset) {
const auto& tgtpp =
this->GeneratorTarget->GetSafeProperty("Fortran_PREPROCESS");
preprocess = cmOutputConverter::GetFortranPreprocess(tgtpp);
}
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
bool const compilePP = this->UsePreprocessedSource(language) &&
(preprocess != cmOutputConverter::FortranPreprocess::NotNeeded);
2020-02-01 23:06:01 +01:00
bool const compilePPWithDefines =
compilePP && this->CompilePreprocessedSourceWithDefines(language);
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
std::string const ppFileName = compilePP
? this->ConvertToNinjaPath(this->GetPreprocessedFilePath(source, config))
: "";
std::string const buildName = compilePP
? this->LanguagePreprocessRule(language, config)
: this->LanguageDependencyRule(language, config);
const auto depExtension = compilePP ? ".pp.d" : ".d";
const std::string depFileName =
this->GetLocalGenerator()->ConvertToOutputFormat(
cmStrCat(objectFileName, depExtension), cmOutputConverter::SHELL);
cmNinjaBuild ppBuild = GetPreprocessOrScanBuild(
buildName, ppFileName, compilePP, compilePPWithDefines, objBuild, vars,
depFileName, needDyndep, objectFileName);
2019-11-11 23:01:05 +01:00
if (compilePP) {
// In case compilation requires flags that are incompatible with
// preprocessing, include them here.
std::string const& postFlag = this->Makefile->GetSafeDefinition(
2020-08-30 11:54:41 +02:00
cmStrCat("CMAKE_", language, "_POSTPROCESS_FLAG"));
2019-11-11 23:01:05 +01:00
this->LocalGenerator->AppendFlags(vars["FLAGS"], postFlag);
2016-10-30 18:24:19 +01:00
2019-11-11 23:01:05 +01:00
// Prepend source file's original directory as an include directory
// so e.g. Fortran INCLUDE statements can look for files in it.
std::vector<std::string> sourceDirectory;
sourceDirectory.push_back(
cmSystemTools::GetParentDirectory(source->GetFullPath()));
2016-10-30 18:24:19 +01:00
2019-11-11 23:01:05 +01:00
std::string sourceDirectoryFlag = this->LocalGenerator->GetIncludeFlags(
sourceDirectory, this->GeneratorTarget, language, false, false,
2020-08-30 11:54:41 +02:00
config);
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
vars["INCLUDES"] = cmStrCat(sourceDirectoryFlag, ' ', vars["INCLUDES"]);
2019-11-11 23:01:05 +01:00
}
2016-10-30 18:24:19 +01:00
2020-08-30 11:54:41 +02:00
if (firstForConfig && needDyndep) {
std::string const ddiFile = cmStrCat(objectFileName, ".ddi");
this->Configs[config].DDIFiles[language].push_back(ddiFile);
2016-10-30 18:24:19 +01:00
}
this->addPoolNinjaVariable("JOB_POOL_COMPILE", this->GetGeneratorTarget(),
2019-11-11 23:01:05 +01:00
ppBuild.Variables);
2018-08-09 18:06:22 +02:00
2020-08-30 11:54:41 +02:00
this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
ppBuild, commandLineLengthLimit);
2016-10-30 18:24:19 +01:00
}
if (needDyndep) {
2020-08-30 11:54:41 +02:00
std::string const dyndep = this->GetDyndepFilePath(language, config);
2019-11-11 23:01:05 +01:00
objBuild.OrderOnlyDeps.push_back(dyndep);
2016-10-30 18:24:19 +01:00
vars["dyndep"] = dyndep;
}
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
2020-08-30 11:54:41 +02:00
if (!pchSources.empty() && !source->GetProperty("SKIP_PRECOMPILE_HEADERS")) {
auto pchIt = pchSources.find(source->GetFullPath());
if (pchIt != pchSources.end()) {
this->addPoolNinjaVariable("JOB_POOL_PRECOMPILE_HEADER",
this->GetGeneratorTarget(), vars);
}
}
this->SetMsvcTargetPdbVariable(vars, config);
2012-06-27 20:52:58 +03:00
2020-08-30 11:54:41 +02:00
objBuild.RspFile = cmStrCat(objectFileName, ".rsp");
2016-07-09 11:21:54 +02:00
2019-11-11 23:01:05 +01:00
if (language == "Swift") {
2020-08-30 11:54:41 +02:00
this->EmitSwiftDependencyInfo(source, config);
2019-11-11 23:01:05 +01:00
} else {
2020-08-30 11:54:41 +02:00
this->GetGlobalGenerator()->WriteBuild(this->GetImplFileStream(fileConfig),
2019-11-11 23:01:05 +01:00
objBuild, commandLineLengthLimit);
}
2012-04-19 19:04:21 +03:00
2020-08-30 11:54:41 +02:00
if (cmProp objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
std::string evaluatedObjectOutputs = cmGeneratorExpression::Evaluate(
*objectOutputs, this->LocalGenerator, config);
if (!evaluatedObjectOutputs.empty()) {
cmNinjaBuild build("phony");
build.Comment = "Additional output files.";
build.Outputs = cmExpandedList(evaluatedObjectOutputs);
std::transform(build.Outputs.begin(), build.Outputs.end(),
build.Outputs.begin(), MapToNinjaPath());
build.ExplicitDeps = objBuild.Outputs;
this->GetGlobalGenerator()->WriteBuild(
this->GetImplFileStream(fileConfig), build);
}
2012-04-19 19:04:21 +03:00
}
}
2020-08-30 11:54:41 +02:00
void cmNinjaTargetGenerator::WriteTargetDependInfo(std::string const& lang,
const std::string& config)
2016-10-30 18:24:19 +01:00
{
Json::Value tdi(Json::objectValue);
tdi["language"] = lang;
2020-08-30 11:54:41 +02:00
tdi["compiler-id"] = this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_COMPILER_ID"));
2016-10-30 18:24:19 +01:00
if (lang == "Fortran") {
std::string mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
this->Makefile->GetHomeOutputDirectory());
if (mod_dir.empty()) {
mod_dir = this->Makefile->GetCurrentBinaryDirectory();
}
tdi["module-dir"] = mod_dir;
2019-11-11 23:01:05 +01:00
tdi["submodule-sep"] =
this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_SEP");
tdi["submodule-ext"] =
this->Makefile->GetSafeDefinition("CMAKE_Fortran_SUBMODULE_EXT");
2016-10-30 18:24:19 +01:00
}
tdi["dir-cur-bld"] = this->Makefile->GetCurrentBinaryDirectory();
tdi["dir-cur-src"] = this->Makefile->GetCurrentSourceDirectory();
tdi["dir-top-bld"] = this->Makefile->GetHomeOutputDirectory();
tdi["dir-top-src"] = this->Makefile->GetHomeDirectory();
Json::Value& tdi_include_dirs = tdi["include-dirs"] = Json::arrayValue;
std::vector<std::string> includes;
this->LocalGenerator->GetIncludeDirectories(includes, this->GeneratorTarget,
2020-08-30 11:54:41 +02:00
lang, config);
2018-01-26 17:06:56 +01:00
for (std::string const& i : includes) {
2017-04-14 19:02:05 +02:00
// Convert the include directories the same way we do for -I flags.
// See upstream ninja issue 1251.
2018-01-26 17:06:56 +01:00
tdi_include_dirs.append(this->ConvertToNinjaPath(i));
2016-10-30 18:24:19 +01:00
}
Json::Value& tdi_linked_target_dirs = tdi["linked-target-dirs"] =
Json::arrayValue;
2020-08-30 11:54:41 +02:00
for (std::string const& l : this->GetLinkedTargetDirectories(config)) {
2018-01-26 17:06:56 +01:00
tdi_linked_target_dirs.append(l);
2016-10-30 18:24:19 +01:00
}
2020-08-30 11:54:41 +02:00
std::string const tdin = this->GetTargetDependInfoPath(lang, config);
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream tdif(tdin);
2016-10-30 18:24:19 +01:00
tdif << tdi;
}
2019-11-11 23:01:05 +01:00
void cmNinjaTargetGenerator::EmitSwiftDependencyInfo(
2020-08-30 11:54:41 +02:00
cmSourceFile const* source, const std::string& config)
2019-11-11 23:01:05 +01:00
{
std::string const sourceFilePath =
this->ConvertToNinjaPath(this->GetSourceFilePath(source));
std::string const objectFilePath =
2020-08-30 11:54:41 +02:00
this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
2019-11-11 23:01:05 +01:00
std::string const swiftDepsPath = [source, objectFilePath]() -> std::string {
2020-08-30 11:54:41 +02:00
if (cmProp name = source->GetProperty("Swift_DEPENDENCIES_FILE")) {
return *name;
2019-11-11 23:01:05 +01:00
}
2020-08-30 11:54:41 +02:00
return cmStrCat(objectFilePath, ".swiftdeps");
2019-11-11 23:01:05 +01:00
}();
std::string const swiftDiaPath = [source, objectFilePath]() -> std::string {
2020-08-30 11:54:41 +02:00
if (cmProp name = source->GetProperty("Swift_DIAGNOSTICS_FILE")) {
return *name;
2019-11-11 23:01:05 +01:00
}
2020-08-30 11:54:41 +02:00
return cmStrCat(objectFilePath, ".dia");
2019-11-11 23:01:05 +01:00
}();
2020-08-30 11:54:41 +02:00
std::string const makeDepsPath = [this, source, config]() -> std::string {
2019-11-11 23:01:05 +01:00
cmLocalNinjaGenerator const* local = this->GetLocalGenerator();
std::string const objectFileName =
2020-08-30 11:54:41 +02:00
this->ConvertToNinjaPath(this->GetObjectFilePath(source, config));
2019-11-11 23:01:05 +01:00
std::string const objectFileDir =
cmSystemTools::GetFilenamePath(objectFileName);
if (this->Makefile->IsOn("CMAKE_Swift_DEPFLE_EXTNSION_REPLACE")) {
2020-08-30 11:54:41 +02:00
std::string dependFileName = cmStrCat(
cmSystemTools::GetFilenameWithoutLastExtension(objectFileName), ".d");
return local->ConvertToOutputFormat(
cmStrCat(objectFileDir, '/', dependFileName),
cmOutputConverter::SHELL);
2019-11-11 23:01:05 +01:00
}
2020-08-30 11:54:41 +02:00
return local->ConvertToOutputFormat(cmStrCat(objectFileName, ".d"),
2019-11-11 23:01:05 +01:00
cmOutputConverter::SHELL);
}();
// build the source file mapping
// https://github.com/apple/swift/blob/master/docs/Driver.md#output-file-maps
Json::Value entry = Json::Value(Json::objectValue);
entry["object"] = objectFilePath;
entry["dependencies"] = makeDepsPath;
entry["swift-dependencies"] = swiftDepsPath;
entry["diagnostics"] = swiftDiaPath;
2020-08-30 11:54:41 +02:00
this->Configs[config].SwiftOutputMap[sourceFilePath] = entry;
2019-11-11 23:01:05 +01: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
2017-04-14 19:02:05 +02:00
cmRulePlaceholderExpander::RuleVariables compileObjectVars;
2015-11-17 17:22:37 +01:00
compileObjectVars.Language = language.c_str();
std::string escapedSourceFileName = sourceFileName;
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileIsFullPath(sourceFileName)) {
2018-08-09 18:06:22 +02:00
escapedSourceFileName =
cmSystemTools::CollapseFullPath(escapedSourceFileName,
this->GetGlobalGenerator()
->GetCMakeInstance()
->GetHomeOutputDirectory());
2016-07-09 11:21:54 +02:00
}
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::vector<std::string> compileCmds;
2017-07-20 19:35:53 +02:00
if (language == "CUDA") {
std::string cmdVar;
if (this->GeneratorTarget->GetPropertyAsBool(
"CUDA_SEPARABLE_COMPILATION")) {
2019-11-11 23:01:05 +01:00
cmdVar = "CMAKE_CUDA_COMPILE_SEPARABLE_COMPILATION";
2017-07-20 19:35:53 +02:00
} else if (this->GeneratorTarget->GetPropertyAsBool(
"CUDA_PTX_COMPILATION")) {
2019-11-11 23:01:05 +01:00
cmdVar = "CMAKE_CUDA_COMPILE_PTX_COMPILATION";
2017-07-20 19:35:53 +02:00
} else {
2019-11-11 23:01:05 +01:00
cmdVar = "CMAKE_CUDA_COMPILE_WHOLE_COMPILATION";
2017-07-20 19:35:53 +02:00
}
2019-11-11 23:01:05 +01:00
const std::string& compileCmd =
2017-07-20 19:35:53 +02:00
this->GetMakefile()->GetRequiredDefinition(cmdVar);
2020-02-01 23:06:01 +01:00
cmExpandList(compileCmd, compileCmds);
2017-07-20 19:35:53 +02:00
} else {
2020-08-30 11:54:41 +02:00
const std::string cmdVar = cmStrCat("CMAKE_", language, "_COMPILE_OBJECT");
2019-11-11 23:01:05 +01:00
const std::string& compileCmd =
2017-07-20 19:35:53 +02:00
this->GetMakefile()->GetRequiredDefinition(cmdVar);
2020-02-01 23:06:01 +01:00
cmExpandList(compileCmd, compileCmds);
2017-07-20 19:35:53 +02:00
}
2015-11-17 17:22:37 +01:00
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
2017-04-14 19:02:05 +02:00
this->GetLocalGenerator()->CreateRulePlaceholderExpander());
2018-01-26 17:06:56 +01:00
for (std::string& i : compileCmds) {
2017-04-14 19:02:05 +02:00
// no launcher for CMAKE_EXPORT_COMPILE_COMMANDS
2018-01-26 17:06:56 +01:00
rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(), i,
2017-04-14 19:02:05 +02:00
compileObjectVars);
2016-10-30 18:24:19 +01:00
}
2015-11-17 17:22:37 +01:00
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
2020-08-30 11:54:41 +02:00
void cmNinjaTargetGenerator::AdditionalCleanFiles(const std::string& config)
2019-11-11 23:01:05 +01:00
{
2020-08-30 11:54:41 +02:00
if (cmProp prop_value =
2019-11-11 23:01:05 +01:00
this->GeneratorTarget->GetProperty("ADDITIONAL_CLEAN_FILES")) {
cmLocalNinjaGenerator* lg = this->LocalGenerator;
std::vector<std::string> cleanFiles;
2020-08-30 11:54:41 +02:00
cmExpandList(cmGeneratorExpression::Evaluate(*prop_value, lg, config,
this->GeneratorTarget),
2020-02-01 23:06:01 +01:00
cleanFiles);
2019-11-11 23:01:05 +01:00
std::string const& binaryDir = lg->GetCurrentBinaryDirectory();
cmGlobalNinjaGenerator* gg = lg->GetGlobalNinjaGenerator();
for (std::string const& cleanFile : cleanFiles) {
// Support relative paths
gg->AddAdditionalCleanFile(
2020-08-30 11:54:41 +02:00
cmSystemTools::CollapseFullPath(cleanFile, binaryDir), config);
2019-11-11 23:01:05 +01:00
}
}
}
2020-08-30 11:54:41 +02:00
cmNinjaDeps cmNinjaTargetGenerator::GetObjects(const std::string& config) const
{
auto const it = this->Configs.find(config);
if (it != this->Configs.end()) {
return it->second.Objects;
}
return {};
}
2016-07-09 11:21:54 +02:00
void cmNinjaTargetGenerator::EnsureDirectoryExists(
const std::string& path) const
2012-06-27 20:52:58 +03:00
{
2018-04-23 21:13:27 +02:00
if (cmSystemTools::FileIsFullPath(path)) {
cmSystemTools::MakeDirectory(path);
2016-07-09 11:21:54 +02:00
} else {
cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator();
2019-11-11 23:01:05 +01:00
std::string fullPath = gg->GetCMakeInstance()->GetHomeOutputDirectory();
2016-07-09 11:21:54 +02:00
// Also ensures their is a trailing slash.
gg->StripNinjaOutputPathPrefixAsSuffix(fullPath);
fullPath += path;
2018-04-23 21:13:27 +02:00
cmSystemTools::MakeDirectory(fullPath);
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()(
2020-08-30 11:54:41 +02:00
cmSourceFile const& source, const char* pkgloc, const std::string& config)
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 =
2020-08-30 11:54:41 +02:00
this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(pkgloc,
config);
// Reject files that collide with files from the Ninja file's native config.
if (config != this->FileConfig) {
std::string nativeMacdir =
this->Generator->OSXBundleGenerator->InitMacOSXContentDirectory(
pkgloc, this->FileConfig);
if (macdir == nativeMacdir) {
return;
}
}
2012-08-04 10:26:08 +03:00
// 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.
2020-02-01 23:06:01 +01:00
std::string output =
cmStrCat(macdir, '/', 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.
2020-08-30 11:54:41 +02:00
this->Generator->GetGlobalGenerator()->WriteMacOSXContentBuild(
input, output, this->FileConfig);
2012-08-04 10:26:08 +03:00
2016-10-30 18:24:19 +01:00
// Add as a dependency to the target so that it gets called.
2020-08-30 11:54:41 +02:00
this->Generator->Configs[config].ExtraFiles.push_back(std::move(output));
2012-08-04 10:26:08 +03:00
}
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
{
2020-08-30 11:54:41 +02:00
cmProp pool = target->GetProperty(pool_property);
2016-07-09 11:21:54 +02:00
if (pool) {
2020-08-30 11:54:41 +02:00
vars["pool"] = *pool;
2016-07-09 11:21:54 +02:00
}
}
bool cmNinjaTargetGenerator::ForceResponseFile()
{
static std::string const forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
return (this->GetMakefile()->IsDefinitionSet(forceRspFile) ||
2016-10-30 18:24:19 +01:00
cmSystemTools::HasEnv(forceRspFile));
2014-08-03 19:52:23 +02:00
}