cmake/Source/cmNinjaTargetGenerator.cxx

1876 lines
69 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>
2023-07-02 19:51:09 +02:00
#include <array>
2020-02-01 23:06:01 +01:00
#include <cassert>
2023-05-23 16:38:00 +02:00
#include <functional>
2017-04-14 19:02:05 +02:00
#include <iterator>
#include <map>
2019-11-11 23:01:05 +01:00
#include <ostream>
2023-07-02 19:51:09 +02:00
#include <type_traits>
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>
2021-09-14 00:13:48 +02:00
#include <cm/string_view>
2020-08-30 11:54:41 +02:00
#include <cmext/algorithm>
2021-09-14 00:13:48 +02:00
#include <cmext/string_view>
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"
2023-05-23 16:38:00 +02:00
#include "cmDyndepCollation.h"
2022-08-04 22:12:04 +02:00
#include "cmFileSet.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"
2023-07-02 19:51:09 +02:00
#include "cmList.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"
2022-08-04 22:12:04 +02:00
#include "cmMessageType.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"
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"
2022-08-04 22:12:04 +02:00
#include "cmTarget.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.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:
2021-09-14 00:13:48 +02:00
case cmStateEnums::INTERFACE_LIBRARY:
2017-04-14 19:02:05 +02:00
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
{
2021-09-14 00:13:48 +02:00
for (auto const& fileConfig :
target->Makefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig)) {
2020-08-30 11:54:41 +02:00
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(
2023-05-23 16:38:00 +02:00
const std::string& lang, const std::string& config,
WithScanning withScanning) 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()),
2023-05-23 16:38:00 +02:00
withScanning == WithScanning::Yes ? "_scanned_" : "_unscanned_", config);
2015-08-17 11:37:30 +02:00
}
2021-09-14 00:13:48 +02:00
std::string cmNinjaTargetGenerator::LanguagePreprocessAndScanRule(
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);
}
2021-09-14 00:13:48 +02:00
std::string cmNinjaTargetGenerator::LanguageScanRule(
2020-08-30 11:54:41 +02:00
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";
}
2021-09-14 00:13:48 +02:00
bool cmNinjaTargetGenerator::CompileWithDefines(std::string const& lang) const
2020-02-01 23:06:01 +01:00
{
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
}
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::unordered_map<std::string, std::string> pchSources;
2023-07-02 19:51:09 +02:00
std::vector<std::string> architectures =
this->GeneratorTarget->GetAppleArchs(config, language);
2020-08-30 11:54:41 +02:00
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));
}
}
2021-09-14 00:13:48 +02:00
std::string flags;
// Explicitly add the explicit language flag before any other flag
// so user flags can override it.
this->GeneratorTarget->AddExplicitLanguageFlags(flags, *source);
if (!flags.empty()) {
flags += " ";
}
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);
2022-03-29 21:10:50 +02:00
this->AppendFortranPreprocessFlags(flags, *source,
PreprocessFlagsRequired::NO);
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");
2021-11-20 13:41:27 +01:00
if (cmValue 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");
2021-11-20 13:41:27 +01:00
if (cmValue 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));
}
2023-05-23 16:38:00 +02:00
auto const* fs = this->GeneratorTarget->GetFileSetForSource(config, source);
2023-07-02 19:51:09 +02:00
if (fs && fs->GetType() == "CXX_MODULES"_s) {
2023-05-23 16:38:00 +02:00
if (source->GetLanguage() != "CXX"_s) {
2022-11-16 20:14:03 +01:00
this->GetMakefile()->IssueMessage(
2023-05-23 16:38:00 +02:00
MessageType::FATAL_ERROR,
cmStrCat("Target \"", this->GeneratorTarget->Target->GetName(),
"\" contains the source\n ", source->GetFullPath(),
"\nin a file set of type \"", fs->GetType(),
R"(" but the source is not classified as a "CXX" source.)"));
2022-08-04 22:12:04 +02: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,
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(
2022-03-29 21:10:50 +02:00
includes, this->GeneratorTarget, language, config, false);
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);
}
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");
2021-11-20 13:41:27 +01:00
if (cmValue 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));
2021-11-20 13:41:27 +01:00
if (cmValue 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");
2021-11-20 13:41:27 +01:00
if (cmValue 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(
2022-03-29 21:10:50 +02:00
includes, this->GeneratorTarget, language, config, false);
2018-04-23 21:13:27 +02:00
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(
2021-09-14 00:13:48 +02:00
const std::string& linkLanguage, const std::string& config,
bool ignoreType) const
2012-04-19 19:04:21 +03:00
{
// Static libraries never depend on other targets for linking.
2021-09-14 00:13:48 +02:00
if (!ignoreType &&
(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());
2021-09-14 00:13:48 +02:00
std::transform(deps.begin(), deps.end(), result.begin(),
this->MapToNinjaPath());
2012-04-19 19:04:21 +03:00
// 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),
2021-09-14 00:13:48 +02:00
this->MapToNinjaPath());
2012-04-19 19:04:21 +03:00
return result;
}
2021-09-14 00:13:48 +02:00
std::string cmNinjaTargetGenerator::GetCompiledSourceNinjaPath(
2016-07-09 11:21:54 +02:00
cmSourceFile const* source) const
2012-04-19 19:04:21 +03:00
{
2021-09-14 00:13:48 +02:00
// Pass source files to the compiler by absolute path.
return this->ConvertToNinjaAbsPath(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;
}
2023-05-23 16:38:00 +02:00
std::string cmNinjaTargetGenerator::GetClangTidyReplacementsFilePath(
2023-07-02 19:51:09 +02:00
std::string const& directory, cmSourceFile const& source,
std::string const& config) const
2023-05-23 16:38:00 +02:00
{
2023-07-02 19:51:09 +02:00
auto path = this->LocalGenerator->GetHomeRelativeOutputPath();
2023-05-23 16:38:00 +02:00
if (!path.empty()) {
path += '/';
}
path = cmStrCat(directory, '/', path);
2023-07-02 19:51:09 +02:00
auto const& objectName = this->GeneratorTarget->GetObjectName(&source);
2023-05-23 16:38:00 +02:00
path =
cmStrCat(std::move(path),
this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
this->GetGlobalGenerator()->ConfigDirectory(config), '/',
objectName, ".yaml");
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);
2021-09-14 00:13:48 +02:00
return this->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(
2021-09-14 00:13:48 +02:00
this->ConvertToNinjaPath(pdbPath), cmOutputConverter::SHELL);
2015-04-27 22:25:09 +02:00
vars["TARGET_COMPILE_PDB"] =
this->GetLocalGenerator()->ConvertToOutputFormat(
2021-09-14 00:13:48 +02:00
this->ConvertToNinjaPath(compilePdbPath), cmOutputConverter::SHELL);
2015-04-27 22:25:09 +02:00
2021-09-14 00:13:48 +02:00
this->EnsureParentDirectoryExists(pdbPath);
this->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
2023-07-02 19:51:09 +02:00
std::string GetScanCommand(cm::string_view cmakeCmd, cm::string_view tdi,
cm::string_view lang, cm::string_view srcFile,
cm::string_view ddiFile)
2020-08-30 11:54:41 +02:00
{
2021-09-14 00:13:48 +02:00
return cmStrCat(cmakeCmd, " -E cmake_ninja_depends --tdi=", tdi,
2023-07-02 19:51:09 +02:00
" --lang=", lang, " --src=", srcFile, " --out=$out",
2021-09-14 00:13:48 +02:00
" --dep=$DEP_FILE --obj=$OBJ_FILE --ddi=", ddiFile);
2020-08-30 11:54:41 +02:00
}
2021-09-14 00:13:48 +02:00
// Helper function to create dependency scanning rule that may or may
// not perform explicit preprocessing too.
cmNinjaRule GetScanRule(
std::string const& ruleName, std::string const& ppFileName,
2022-08-04 22:12:04 +02:00
std::string const& deptype,
2021-09-14 00:13:48 +02:00
cmRulePlaceholderExpander::RuleVariables const& vars,
2020-08-30 11:54:41 +02:00
const std::string& responseFlag, const std::string& flags,
cmRulePlaceholderExpander* const rulePlaceholderExpander,
2021-09-14 00:13:48 +02:00
cmLocalNinjaGenerator* generator, std::vector<std::string> scanCmds,
const std::string& outputConfig)
2020-08-30 11:54:41 +02:00
{
cmNinjaRule rule(ruleName);
2021-09-14 00:13:48 +02:00
// Scanning always uses a depfile for preprocessor dependencies.
2022-08-04 22:12:04 +02:00
if (deptype == "msvc"_s) {
rule.DepType = deptype;
2023-05-23 16:38:00 +02:00
rule.DepFile.clear();
2022-08-04 22:12:04 +02:00
} else {
2023-05-23 16:38:00 +02:00
rule.DepType.clear(); // no deps= for multiple outputs
2022-08-04 22:12:04 +02:00
rule.DepFile = "$DEP_FILE";
}
2020-08-30 11:54:41 +02:00
2021-09-14 00:13:48 +02:00
cmRulePlaceholderExpander::RuleVariables scanVars;
scanVars.CMTargetName = vars.CMTargetName;
scanVars.CMTargetType = vars.CMTargetType;
scanVars.Language = vars.Language;
scanVars.Object = "$OBJ_FILE";
scanVars.PreprocessedSource = ppFileName.c_str();
scanVars.DynDepFile = "$DYNDEP_INTERMEDIATE_FILE";
scanVars.DependencyFile = rule.DepFile.c_str();
scanVars.DependencyTarget = "$out";
2020-08-30 11:54:41 +02:00
2021-09-14 00:13:48 +02:00
// Scanning needs the same preprocessor settings as direct compilation would.
scanVars.Source = vars.Source;
scanVars.Defines = vars.Defines;
scanVars.Includes = vars.Includes;
2020-08-30 11:54:41 +02:00
2021-09-14 00:13:48 +02:00
// Scanning needs the compilation flags too.
std::string scanFlags = flags;
2020-08-30 11:54:41 +02:00
// If using a response file, move defines, includes, and flags into it.
if (!responseFlag.empty()) {
rule.RspFile = "$RSP_FILE";
rule.RspContent =
2021-09-14 00:13:48 +02:00
cmStrCat(' ', scanVars.Defines, ' ', scanVars.Includes, ' ', scanFlags);
scanFlags = cmStrCat(responseFlag, rule.RspFile);
scanVars.Defines = "";
scanVars.Includes = "";
2020-08-30 11:54:41 +02:00
}
2021-09-14 00:13:48 +02:00
scanVars.Flags = scanFlags.c_str();
2020-08-30 11:54:41 +02:00
2021-09-14 00:13:48 +02:00
// Rule for scanning a source file.
for (std::string& scanCmd : scanCmds) {
rulePlaceholderExpander->ExpandRuleVariables(generator, scanCmd, scanVars);
2020-08-30 11:54:41 +02:00
}
2021-09-14 00:13:48 +02:00
rule.Command =
generator->BuildCommandLine(scanCmds, outputConfig, outputConfig);
2020-08-30 11:54:41 +02:00
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)
2023-05-23 16:38:00 +02:00
{
// For some cases we scan to dynamically discover dependencies.
bool const needDyndep = this->GetGeneratorTarget()->NeedDyndep(lang, config);
if (needDyndep) {
this->WriteCompileRule(lang, config, WithScanning::Yes);
}
this->WriteCompileRule(lang, config, WithScanning::No);
}
void cmNinjaTargetGenerator::WriteCompileRule(const std::string& lang,
const std::string& config,
WithScanning withScanning)
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";
2022-03-29 21:10:50 +02:00
vars.CudaCompileMode = "$CUDA_COMPILE_MODE";
2021-09-14 00:13:48 +02:00
vars.ISPCHeader = "$ISPC_HEADER_FILE";
2012-06-27 20:52:58 +03:00
2020-02-01 23:06:01 +01:00
cmMakefile* mf = this->GetMakefile();
2021-09-14 00:13:48 +02:00
// For some cases we scan to dynamically discover dependencies.
bool const compilationPreprocesses = !this->NeedExplicitPreprocessing(lang);
2016-10-30 18:24:19 +01:00
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
}
2021-09-14 00:13:48 +02:00
std::string const modmapFormatVar =
cmStrCat("CMAKE_EXPERIMENTAL_", lang, "_MODULE_MAP_FORMAT");
std::string const modmapFormat =
this->Makefile->GetSafeDefinition(modmapFormatVar);
2016-07-09 11:21:54 +02:00
2023-07-02 19:51:09 +02:00
auto rulePlaceholderExpander =
this->GetLocalGenerator()->CreateRulePlaceholderExpander();
2017-04-14 19:02:05 +02:00
2016-10-30 18:24:19 +01:00
std::string const tdi = this->GetLocalGenerator()->ConvertToOutputFormat(
2021-09-14 00:13:48 +02:00
this->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;
2023-07-02 19:51:09 +02:00
std::string val = this->GetLocalGenerator()->GetRuleLauncher(
this->GetGeneratorTarget(), "RULE_LAUNCH_COMPILE", config);
2021-09-14 00:13:48 +02:00
if (cmNonempty(val)) {
2023-07-02 19:51:09 +02: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
2023-05-23 16:38:00 +02:00
if (withScanning == WithScanning::Yes) {
2022-08-04 22:12:04 +02:00
const auto& scanDepType = this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_EXPERIMENTAL_", lang, "_SCANDEP_DEPFILE_FORMAT"));
2021-09-14 00:13:48 +02:00
// Rule to scan dependencies of sources that need preprocessing.
{
2023-07-02 19:51:09 +02:00
cmList scanCommands;
2021-09-14 00:13:48 +02:00
std::string scanRuleName;
std::string ppFileName;
if (compilationPreprocesses) {
scanRuleName = this->LanguageScanRule(lang, config);
ppFileName = "$PREPROCESSED_OUTPUT_FILE";
std::string const& scanCommand = mf->GetRequiredDefinition(
cmStrCat("CMAKE_EXPERIMENTAL_", lang, "_SCANDEP_SOURCE"));
2023-07-02 19:51:09 +02:00
scanCommands.assign(scanCommand);
for (auto& i : scanCommands) {
2021-09-14 00:13:48 +02:00
i = cmStrCat(launcher, i);
}
} else {
scanRuleName = this->LanguagePreprocessAndScanRule(lang, config);
ppFileName = "$out";
std::string const& ppCommmand = mf->GetRequiredDefinition(
cmStrCat("CMAKE_", lang, "_PREPROCESS_SOURCE"));
2023-07-02 19:51:09 +02:00
scanCommands.assign(ppCommmand);
for (auto& i : scanCommands) {
2021-09-14 00:13:48 +02:00
i = cmStrCat(launcher, i);
}
scanCommands.emplace_back(GetScanCommand(cmakeCmd, tdi, lang, "$out",
"$DYNDEP_INTERMEDIATE_FILE"));
}
2016-10-30 18:24:19 +01:00
2022-08-04 22:12:04 +02:00
auto scanRule = GetScanRule(
scanRuleName, ppFileName, scanDepType, vars, responseFlag, flags,
rulePlaceholderExpander.get(), this->GetLocalGenerator(),
std::move(scanCommands), config);
2021-09-14 00:13:48 +02:00
scanRule.Comment =
cmStrCat("Rule for generating ", lang, " dependencies.");
if (compilationPreprocesses) {
scanRule.Description =
cmStrCat("Scanning $in for ", lang, " dependencies");
} else {
scanRule.Description =
cmStrCat("Building ", lang, " preprocessed $out");
}
2016-10-30 18:24:19 +01:00
2021-09-14 00:13:48 +02:00
this->GetGlobalGenerator()->AddRule(scanRule);
2020-02-01 23:06:01 +01:00
}
2016-10-30 18:24:19 +01:00
2021-09-14 00:13:48 +02:00
if (!compilationPreprocesses) {
// Compilation will not preprocess, so it does not need the defines
// unless the compiler wants them for some other purpose.
if (!this->CompileWithDefines(lang)) {
vars.Defines = "";
}
2018-08-09 18:06:22 +02:00
2021-09-14 00:13:48 +02:00
// Rule to scan dependencies of sources that do not need preprocessing.
std::string const& scanRuleName = this->LanguageScanRule(lang, config);
std::vector<std::string> scanCommands;
scanCommands.emplace_back(
GetScanCommand(cmakeCmd, tdi, lang, "$in", "$out"));
2018-08-09 18:06:22 +02:00
2022-08-04 22:12:04 +02:00
auto scanRule =
GetScanRule(scanRuleName, "", scanDepType, vars, "", flags,
rulePlaceholderExpander.get(), this->GetLocalGenerator(),
std::move(scanCommands), config);
2016-10-30 18:24:19 +01:00
2021-09-14 00:13:48 +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");
this->GetGlobalGenerator()->AddRule(scanRule);
}
2016-10-30 18:24:19 +01:00
// 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).
2021-09-14 00:13:48 +02:00
std::string ddModmapArg;
if (!modmapFormat.empty()) {
ddModmapArg += cmStrCat(" --modmapfmt=", modmapFormat);
}
2019-11-11 23:01:05 +01:00
{
std::vector<std::string> ddCmds;
{
2021-09-14 00:13:48 +02:00
std::string ccmd = cmStrCat(
cmakeCmd, " -E cmake_ninja_dyndep --tdi=", tdi, " --lang=", lang,
ddModmapArg, " --dd=$out @", rule.RspFile);
2019-11-11 23:01:05 +01:00
ddCmds.emplace_back(std::move(ccmd));
}
2021-09-14 00:13:48 +02:00
rule.Command =
this->GetLocalGenerator()->BuildCommandLine(ddCmds, config, config);
2019-11-11 23:01:05 +01:00
}
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
}
2023-05-23 16:38:00 +02:00
cmNinjaRule rule(this->LanguageCompilerRule(lang, config, withScanning));
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;
2021-09-14 00:13:48 +02:00
if (!compilationPreprocesses) {
// The compiler will not do preprocessing, so it has no such dependencies.
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";
2021-11-20 13:41:27 +01:00
cmValue d = mf->GetDefinition("CMAKE_C_COMPILER");
2020-08-30 11:54:41 +02:00
const std::string cl =
2021-09-14 00:13:48 +02:00
d ? *d : mf->GetSafeDefinition("CMAKE_CXX_COMPILER");
2021-11-20 13:41:27 +01:00
std::string cmcldepsPath;
cmSystemTools::GetShortPath(cmSystemTools::GetCMClDepsCommand(),
cmcldepsPath);
cldeps = cmStrCat(cmcldepsPath, ' ', lang, ' ', vars.Source,
" $DEP_FILE $out \"",
2020-02-01 23:06:01 +01:00
mf->GetSafeDefinition("CMAKE_CL_SHOWINCLUDES_PREFIX"),
"\" \"", cl, "\" ");
2018-08-09 18:06:22 +02:00
}
} else {
2021-09-14 00:13:48 +02:00
const auto& depType = this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_DEPFILE_FORMAT"));
if (depType == "msvc"_s) {
rule.DepType = "msvc";
rule.DepFile.clear();
} else {
rule.DepType = "gcc";
rule.DepFile = "$DEP_FILE";
}
vars.DependencyFile = rule.DepFile.c_str();
vars.DependencyTarget = "$out";
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()) {
2021-09-14 00:13:48 +02:00
rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
depfileFlags, vars);
2020-08-30 11:54:41 +02:00
flags += cmStrCat(' ', depfileFlags);
2018-08-09 18:06:22 +02:00
}
}
2023-05-23 16:38:00 +02:00
if (withScanning == WithScanning::Yes && !modmapFormat.empty()) {
2021-09-14 00:13:48 +02:00
std::string modmapFlags = mf->GetRequiredDefinition(
cmStrCat("CMAKE_EXPERIMENTAL_", lang, "_MODULE_MAP_FLAG"));
cmSystemTools::ReplaceString(modmapFlags, "<MODULE_MAP_FILE>",
"$DYNDEP_MODULE_MAP_FILE");
flags += cmStrCat(' ', modmapFlags);
}
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
2022-03-29 21:10:50 +02:00
std::string cudaCompileMode;
2017-04-14 19:02:05 +02:00
if (lang == "CUDA") {
if (this->GeneratorTarget->GetPropertyAsBool(
"CUDA_SEPARABLE_COMPILATION")) {
2022-03-29 21:10:50 +02:00
const std::string& rdcFlag =
this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG");
cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, " ");
}
2023-07-02 19:51:09 +02:00
static std::array<cm::string_view, 4> const compileModes{
{ "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s }
};
bool useNormalCompileMode = true;
for (cm::string_view mode : compileModes) {
auto propName = cmStrCat("CUDA_", mode, "_COMPILATION");
auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG");
if (this->GeneratorTarget->GetPropertyAsBool(propName)) {
const std::string& flag =
this->Makefile->GetRequiredDefinition(defName);
cudaCompileMode = cmStrCat(cudaCompileMode, flag);
useNormalCompileMode = false;
break;
}
}
if (useNormalCompileMode) {
2022-03-29 21:10:50 +02:00
const std::string& wholeFlag =
this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG");
cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag);
2017-04-14 19:02:05 +02:00
}
2022-03-29 21:10:50 +02:00
vars.CudaCompileMode = cudaCompileMode.c_str();
2017-04-14 19:02:05 +02:00
}
2012-04-19 19:04:21 +03:00
2022-03-29 21:10:50 +02:00
// Rule for compiling object file.
const std::string cmdVar = cmStrCat("CMAKE_", lang, "_COMPILE_OBJECT");
const std::string& compileCmd = mf->GetRequiredDefinition(cmdVar);
2023-07-02 19:51:09 +02:00
cmList compileCmds(compileCmd);
2018-08-09 18:06:22 +02:00
2023-07-02 19:51:09 +02:00
if (!compileCmds.empty()) {
compileCmds.front().insert(0, "${CODE_CHECK}");
compileCmds.front().insert(0, "${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
2021-09-14 00:13:48 +02:00
const auto& extraCommands = this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_DEPENDS_EXTRA_COMMANDS"));
if (!extraCommands.empty()) {
2023-07-02 19:51:09 +02:00
compileCmds.append(extraCommands);
2021-09-14 00:13:48 +02:00
}
2023-07-02 19:51:09 +02:00
for (auto& 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
2021-09-14 00:13:48 +02:00
rule.Command =
this->GetLocalGenerator()->BuildCommandLine(compileCmds, config, config);
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
{
2022-11-16 20:14:03 +01:00
this->GeneratorTarget->CheckCxxModuleStatus(config);
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) {
2021-11-20 13:41:27 +01:00
cmValue pchExtension =
2021-09-14 00:13:48 +02:00
this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
2020-02-01 23:06:01 +01:00
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(
2021-09-14 00:13:48 +02:00
this->ConvertToNinjaPath(sf->GetFullPath()), config);
2021-11-20 13:41:27 +01:00
if (!cmHasSuffix(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(),
2021-09-14 00:13:48 +02:00
std::back_inserter(orderOnlyDeps),
this->MapToNinjaPath());
2019-11-11 23:01:05 +01:00
std::transform(ccbyproducts.begin(), ccbyproducts.end(),
2021-09-14 00:13:48 +02:00
std::back_inserter(orderOnlyDeps),
this->MapToNinjaPath());
2019-11-11 23:01:05 +01:00
}
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);
2021-09-14 00:13:48 +02:00
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
2023-07-02 19:51:09 +02:00
for (auto const& langScanningFiles : this->Configs[config].ScanningInfo) {
std::string const& language = langScanningFiles.first;
std::vector<ScanningFiles> const& scanningFiles = langScanningFiles.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));
2023-07-02 19:51:09 +02:00
build.ImplicitOuts.push_back(
cmStrCat(this->Makefile->GetCurrentBinaryDirectory(), '/',
this->LocalGenerator->GetTargetDirectory(this->GeneratorTarget),
this->GetGlobalGenerator()->ConfigDirectory(config), '/',
language, "Modules.json"));
for (auto const& scanFiles : scanningFiles) {
if (!scanFiles.ScanningOutput.empty()) {
build.ExplicitDeps.push_back(scanFiles.ScanningOutput);
}
if (!scanFiles.ModuleMapFile.empty()) {
build.ImplicitOuts.push_back(scanFiles.ModuleMapFile);
}
}
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
2023-07-02 19:51:09 +02:00
for (std::string const& l :
this->GetLinkedTargetDirectories(language, config)) {
build.ImplicitDeps.push_back(cmStrCat(l, '/', language, "Modules.json"));
}
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";
2023-05-23 16:38:00 +02:00
}
2019-11-11 23:01:05 +01:00
2023-05-23 16:38:00 +02:00
void cmNinjaTargetGenerator::GenerateSwiftOutputFileMap(
const std::string& config, std::string& flags)
{
if (this->Configs[config].SwiftOutputMap.empty()) {
return;
}
std::string const targetSwiftDepsPath = [this, config]() -> std::string {
cmGeneratorTarget const* target = this->GeneratorTarget;
if (cmValue name = target->GetProperty("Swift_DEPENDENCIES_FILE")) {
return *name;
}
return this->ConvertToNinjaPath(cmStrCat(target->GetSupportDirectory(),
'/', config, '/',
target->GetName(), ".swiftdeps"));
}();
2019-11-11 23:01:05 +01:00
2023-05-23 16:38:00 +02:00
std::string mapFilePath =
cmStrCat(this->GeneratorTarget->GetSupportDirectory(), '/', config, '/',
"output-file-map.json");
2019-11-11 23:01:05 +01:00
2023-05-23 16:38:00 +02: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;
this->Configs[config].SwiftOutputMap[""] = deps;
cmGeneratedFileStream output(mapFilePath);
output << this->Configs[config].SwiftOutputMap;
// Add flag
this->LocalGenerator->AppendFlags(flags, "-output-file-map");
this->LocalGenerator->AppendFlagEscape(flags,
ConvertToNinjaPath(mapFilePath));
2012-04-19 19:04:21 +03:00
}
2020-08-30 11:54:41 +02:00
namespace {
2021-09-14 00:13:48 +02:00
cmNinjaBuild GetScanBuildStatement(const std::string& ruleName,
const std::string& ppFileName,
bool compilePP, bool compilePPWithDefines,
2023-07-02 19:51:09 +02:00
bool compilationPreprocesses,
2021-09-14 00:13:48 +02:00
cmNinjaBuild& objBuild, cmNinjaVars& vars,
const std::string& objectFileName,
cmLocalGenerator* lg)
2020-08-30 11:54:41 +02:00
{
2021-09-14 00:13:48 +02:00
cmNinjaBuild scanBuild(ruleName);
2020-08-30 11:54:41 +02:00
2021-09-14 00:13:48 +02:00
scanBuild.RspFile = "$out.rsp";
2020-08-30 11:54:41 +02:00
if (compilePP) {
2021-09-14 00:13:48 +02:00
// Move compilation dependencies to the scan/preprocessing build statement.
std::swap(scanBuild.ExplicitDeps, objBuild.ExplicitDeps);
std::swap(scanBuild.ImplicitDeps, objBuild.ImplicitDeps);
std::swap(scanBuild.OrderOnlyDeps, objBuild.OrderOnlyDeps);
std::swap(scanBuild.Variables["IN_ABS"], vars["IN_ABS"]);
2020-08-30 11:54:41 +02:00
// The actual compilation will now use the preprocessed source.
objBuild.ExplicitDeps.push_back(ppFileName);
} else {
2021-09-14 00:13:48 +02:00
// Copy compilation dependencies to the scan/preprocessing build statement.
scanBuild.ExplicitDeps = objBuild.ExplicitDeps;
scanBuild.ImplicitDeps = objBuild.ImplicitDeps;
scanBuild.OrderOnlyDeps = objBuild.OrderOnlyDeps;
scanBuild.Variables["IN_ABS"] = vars["IN_ABS"];
2020-08-30 11:54:41 +02:00
}
2021-09-14 00:13:48 +02:00
// Scanning and compilation generally use the same flags.
scanBuild.Variables["FLAGS"] = vars["FLAGS"];
2020-08-30 11:54:41 +02:00
if (compilePP && !compilePPWithDefines) {
2021-09-14 00:13:48 +02:00
// Move preprocessor definitions to the scan/preprocessor build statement.
std::swap(scanBuild.Variables["DEFINES"], vars["DEFINES"]);
2020-08-30 11:54:41 +02:00
} else {
2021-09-14 00:13:48 +02:00
// Copy preprocessor definitions to the scan/preprocessor build statement.
scanBuild.Variables["DEFINES"] = vars["DEFINES"];
2020-08-30 11:54:41 +02:00
}
// Copy include directories to the preprocessor build statement. The
// Fortran compilation build statement still needs them for the INCLUDE
// directive.
2021-09-14 00:13:48 +02:00
scanBuild.Variables["INCLUDES"] = vars["INCLUDES"];
// Tell dependency scanner the object file that will result from
// compiling the source.
scanBuild.Variables["OBJ_FILE"] = objectFileName;
// Tell dependency scanner where to store dyndep intermediate results.
std::string const& ddiFile = cmStrCat(objectFileName, ".ddi");
scanBuild.Variables["DYNDEP_INTERMEDIATE_FILE"] = ddiFile;
// Outputs of the scan/preprocessor build statement.
if (compilePP) {
scanBuild.Outputs.push_back(ppFileName);
scanBuild.ImplicitOuts.push_back(ddiFile);
} else {
scanBuild.Outputs.push_back(ddiFile);
scanBuild.Variables["PREPROCESSED_OUTPUT_FILE"] = ppFileName;
2023-07-02 19:51:09 +02:00
if (!compilationPreprocesses) {
// Compilation does not preprocess and we are not compiling an
// already-preprocessed source. Make compilation depend on the scan
// results to honor implicit dependencies discovered during scanning
// (such as Fortran INCLUDE directives).
objBuild.ImplicitDeps.emplace_back(ddiFile);
}
2021-09-14 00:13:48 +02:00
}
2020-08-30 11:54:41 +02:00
2022-08-04 22:12:04 +02:00
// Scanning always provides a depfile for preprocessor dependencies. This
// variable is unused in `msvc`-deptype scanners.
2021-09-14 00:13:48 +02:00
std::string const& depFileName = cmStrCat(scanBuild.Outputs.front(), ".d");
scanBuild.Variables["DEP_FILE"] =
lg->ConvertToOutputFormat(depFileName, cmOutputConverter::SHELL);
2020-08-30 11:54:41 +02:00
if (compilePP) {
// The actual compilation does not need a depfile because it
// depends on the already-preprocessed source.
vars.erase("DEP_FILE");
}
2021-09-14 00:13:48 +02:00
return scanBuild;
2020-08-30 11:54:41 +02:00
}
}
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();
2021-09-14 00:13:48 +02:00
std::string const sourceFilePath = this->GetCompiledSourceNinjaPath(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
2021-11-20 13:41:27 +01:00
cmValue flag = this->GetMakefile()->GetDefinition(cmakeLinkVar);
2019-11-11 23:01:05 +01:00
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;
2023-05-23 16:38:00 +02:00
bool const needDyndep =
this->GeneratorTarget->NeedDyndepForSource(language, config, source);
2018-08-09 18:06:22 +02:00
2023-05-23 16:38:00 +02:00
cmNinjaBuild objBuild(this->LanguageCompilerRule(
language, config, needDyndep ? WithScanning::Yes : WithScanning::No));
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
2023-07-02 19:51:09 +02:00
auto compilerLauncher = this->GetCompilerLauncher(language, config);
cmValue const skipCodeCheck = source->GetProperty("SKIP_LINTING");
if (!skipCodeCheck.IsOn()) {
auto const cmakeCmd = this->GetLocalGenerator()->ConvertToOutputFormat(
cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
vars["CODE_CHECK"] =
this->GenerateCodeCheckRules(*source, compilerLauncher, cmakeCmd, config,
[this](const std::string& path) {
return this->ConvertToNinjaPath(path);
});
}
// If compiler launcher was specified and not consumed above, it
// goes to the beginning of the command line.
if (!compilerLauncher.empty()) {
cmList args{ compilerLauncher, cmList::EmptyElements::Yes };
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);
}
vars["LAUNCHER"] = args.join(" ") + " ";
}
}
2021-09-14 00:13:48 +02:00
if (this->GetMakefile()->GetSafeDefinition(
cmStrCat("CMAKE_", language, "_DEPFILE_FORMAT")) != "msvc"_s) {
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
2022-11-16 20:14:03 +01:00
if (firstForConfig) {
this->ExportObjectCompileCommand(
language, sourceFilePath, objectDir, objectFileName, objectFileDir,
vars["FLAGS"], vars["DEFINES"], vars["INCLUDES"], config);
}
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) {
2021-11-20 13:41:27 +01:00
cmValue pchExtension =
2020-08-30 11:54:41 +02:00
this->GetMakefile()->GetDefinition("CMAKE_PCH_EXTENSION");
2021-11-20 13:41:27 +01:00
if (!cmHasSuffix(objectFileName, pchExtension)) {
2020-08-30 11:54:41 +02:00
// 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
2021-09-14 00:13:48 +02:00
objBuild.ExplicitDeps.push_back(sourceFilePath);
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;
2023-07-02 19:51:09 +02:00
std::vector<std::string> architectures =
this->GeneratorTarget->GetAppleArchs(config, language);
2020-08-30 11:54:41 +02:00
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
}
}
2021-11-20 13:41:27 +01:00
if (cmValue objectDeps = source->GetProperty("OBJECT_DEPENDS")) {
2023-07-02 19:51:09 +02:00
cmList objDepList{ *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),
2021-09-14 00:13:48 +02:00
this->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() &&
2021-09-14 00:13:48 +02:00
!this->GetGlobalGenerator()->HasCustomCommandOutput(sourceFilePath)) {
2019-11-11 23:01:05 +01:00
this->GetGlobalGenerator()->AddAssumedSourceDependencies(
2021-09-14 00:13:48 +02:00
sourceFilePath, objBuild.OrderOnlyDeps);
2012-04-19 19:04:21 +03:00
}
2021-09-14 00:13:48 +02:00
// For some cases we scan to dynamically discover dependencies.
bool const compilationPreprocesses =
!this->NeedExplicitPreprocessing(language);
2016-10-30 18:24:19 +01:00
2021-09-14 00:13:48 +02:00
std::string modmapFormat;
if (needDyndep) {
std::string const modmapFormatVar =
cmStrCat("CMAKE_EXPERIMENTAL_", language, "_MODULE_MAP_FORMAT");
modmapFormat = this->Makefile->GetSafeDefinition(modmapFormatVar);
}
2016-10-30 18:24:19 +01:00
2021-09-14 00:13:48 +02:00
if (needDyndep) {
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
2021-09-14 00:13:48 +02:00
bool const compilePP = !compilationPreprocesses &&
2020-08-30 11:54:41 +02:00
(preprocess != cmOutputConverter::FortranPreprocess::NotNeeded);
2020-02-01 23:06:01 +01:00
bool const compilePPWithDefines =
2021-09-14 00:13:48 +02:00
compilePP && this->CompileWithDefines(language);
2020-08-30 11:54:41 +02:00
2021-09-14 00:13:48 +02:00
std::string scanRuleName;
std::string ppFileName;
if (compilePP) {
scanRuleName = this->LanguagePreprocessAndScanRule(language, config);
ppFileName = this->ConvertToNinjaPath(
this->GetPreprocessedFilePath(source, config));
} else {
scanRuleName = this->LanguageScanRule(language, config);
ppFileName = cmStrCat(objectFileName, ".ddi.i");
}
2020-08-30 11:54:41 +02:00
2021-09-14 00:13:48 +02:00
cmNinjaBuild ppBuild = GetScanBuildStatement(
2023-07-02 19:51:09 +02:00
scanRuleName, ppFileName, compilePP, compilePPWithDefines,
compilationPreprocesses, objBuild, vars, objectFileName,
this->LocalGenerator);
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(
2022-03-29 21:10:50 +02:00
sourceDirectory, this->GeneratorTarget, language, config, false);
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
2023-07-02 19:51:09 +02:00
ScanningFiles scanningFiles;
2021-09-14 00:13:48 +02:00
if (firstForConfig) {
2023-07-02 19:51:09 +02:00
scanningFiles.ScanningOutput = cmStrCat(objectFileName, ".ddi");
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);
2021-09-14 00:13:48 +02:00
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;
2021-09-14 00:13:48 +02:00
if (!modmapFormat.empty()) {
2023-07-02 19:51:09 +02:00
// XXX(modmap): If changing this path construction, change
2023-07-14 19:10:31 +02:00
// `cmGlobalNinjaGenerator::WriteDyndep` and
// `cmNinjaTargetGenerator::ExportObjectCompileCommand` to expect the
// corresponding file path.
2023-07-02 19:51:09 +02:00
std::string ddModmapFile = cmStrCat(objectFileName, ".modmap");
2021-09-14 00:13:48 +02:00
vars["DYNDEP_MODULE_MAP_FILE"] = ddModmapFile;
objBuild.OrderOnlyDeps.push_back(ddModmapFile);
2023-07-02 19:51:09 +02:00
scanningFiles.ModuleMapFile = std::move(ddModmapFile);
}
if (!scanningFiles.IsEmpty()) {
this->Configs[config].ScanningInfo[language].emplace_back(scanningFiles);
2021-09-14 00:13:48 +02:00
}
2016-10-30 18:24:19 +01:00
}
2021-09-14 00:13:48 +02:00
this->EnsureParentDirectoryExists(objectFileName);
2012-06-27 20:52:58 +03:00
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
2021-09-14 00:13:48 +02:00
if (language == "ISPC") {
std::string const& objectName =
this->GeneratorTarget->GetObjectName(source);
std::string ispcSource =
cmSystemTools::GetFilenameWithoutLastExtension(objectName);
ispcSource = cmSystemTools::GetFilenameWithoutLastExtension(ispcSource);
2021-11-20 13:41:27 +01:00
cmValue ispcSuffixProp =
2021-09-14 00:13:48 +02:00
this->GeneratorTarget->GetProperty("ISPC_HEADER_SUFFIX");
assert(ispcSuffixProp);
std::string ispcHeaderDirectory =
this->GeneratorTarget->GetObjectDirectory(config);
2021-11-20 13:41:27 +01:00
if (cmValue prop =
2021-09-14 00:13:48 +02:00
this->GeneratorTarget->GetProperty("ISPC_HEADER_DIRECTORY")) {
ispcHeaderDirectory =
cmStrCat(this->LocalGenerator->GetBinaryDirectory(), '/', *prop);
}
std::string ispcHeader =
cmStrCat(ispcHeaderDirectory, '/', ispcSource, *ispcSuffixProp);
ispcHeader = this->ConvertToNinjaPath(ispcHeader);
// Make sure ninja knows what command generates the header
objBuild.ImplicitOuts.push_back(ispcHeader);
// Make sure ninja knows how to clean the generated header
this->GetGlobalGenerator()->AddAdditionalCleanFile(ispcHeader, config);
auto ispcSuffixes =
detail::ComputeISPCObjectSuffixes(this->GeneratorTarget);
if (ispcSuffixes.size() > 1) {
std::string rootObjectDir =
this->GeneratorTarget->GetObjectDirectory(config);
auto ispcSideEfffectObjects = detail::ComputeISPCExtraObjects(
objectName, rootObjectDir, ispcSuffixes);
for (auto sideEffect : ispcSideEfffectObjects) {
sideEffect = this->ConvertToNinjaPath(sideEffect);
objBuild.ImplicitOuts.emplace_back(sideEffect);
this->GetGlobalGenerator()->AddAdditionalCleanFile(sideEffect, config);
}
}
vars["ISPC_HEADER_FILE"] =
this->GetLocalGenerator()->ConvertToOutputFormat(
ispcHeader, cmOutputConverter::SHELL);
} else {
auto headers = this->GeneratorTarget->GetGeneratedISPCHeaders(config);
if (!headers.empty()) {
std::transform(headers.begin(), headers.end(), headers.begin(),
this->MapToNinjaPath());
objBuild.OrderOnlyDeps.insert(objBuild.OrderOnlyDeps.end(),
headers.begin(), headers.end());
}
}
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
2021-11-20 13:41:27 +01:00
if (cmValue objectOutputs = source->GetProperty("OBJECT_OUTPUTS")) {
2020-08-30 11:54:41 +02:00
std::string evaluatedObjectOutputs = cmGeneratorExpression::Evaluate(
*objectOutputs, this->LocalGenerator, config);
if (!evaluatedObjectOutputs.empty()) {
cmNinjaBuild build("phony");
build.Comment = "Additional output files.";
2023-07-02 19:51:09 +02:00
build.Outputs = cmList{ evaluatedObjectOutputs }.data();
2020-08-30 11:54:41 +02:00
std::transform(build.Outputs.begin(), build.Outputs.end(),
2021-09-14 00:13:48 +02:00
build.Outputs.begin(), this->MapToNinjaPath());
2020-08-30 11:54:41 +02:00
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"));
2023-05-23 16:38:00 +02:00
tdi["compiler-simulate-id"] = this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_SIMULATE_ID"));
tdi["compiler-frontend-variant"] = this->Makefile->GetSafeDefinition(
cmStrCat("CMAKE_", lang, "_COMPILER_FRONTEND_VARIANT"));
2016-10-30 18:24:19 +01:00
2021-09-14 00:13:48 +02:00
std::string mod_dir;
2016-10-30 18:24:19 +01:00
if (lang == "Fortran") {
2021-09-14 00:13:48 +02:00
mod_dir = this->GeneratorTarget->GetFortranModuleDirectory(
2016-10-30 18:24:19 +01:00
this->Makefile->GetHomeOutputDirectory());
2021-09-14 00:13:48 +02:00
} else if (lang == "CXX") {
2022-11-16 20:14:03 +01:00
mod_dir = this->GetGlobalGenerator()->ExpandCFGIntDir(
cmSystemTools::CollapseFullPath(this->GeneratorTarget->ObjectDirectory),
config);
2021-09-14 00:13:48 +02:00
}
if (mod_dir.empty()) {
mod_dir = this->Makefile->GetCurrentBinaryDirectory();
}
tdi["module-dir"] = mod_dir;
if (lang == "Fortran") {
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");
2021-09-14 00:13:48 +02:00
} else if (lang == "CXX") {
// No extra information necessary.
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;
2023-05-23 16:38:00 +02:00
for (std::string const& l : this->GetLinkedTargetDirectories(lang, config)) {
2018-01-26 17:06:56 +01:00
tdi_linked_target_dirs.append(l);
2016-10-30 18:24:19 +01:00
}
2023-05-23 16:38:00 +02:00
cmDyndepGeneratorCallbacks cb;
cb.ObjectFilePath = [this](cmSourceFile const* sf, std::string const& cnf) {
return this->GetObjectFilePath(sf, cnf);
};
2022-11-16 20:14:03 +01:00
2023-05-23 16:38:00 +02:00
#if !defined(CMAKE_BOOTSTRAP)
cmDyndepCollation::AddCollationInformation(tdi, this->GeneratorTarget,
config, cb);
#endif
2022-11-16 20:14:03 +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
{
2021-09-14 00:13:48 +02:00
std::string const sourceFilePath = this->GetCompiledSourceNinjaPath(source);
2019-11-11 23:01:05 +01:00
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 {
2021-11-20 13:41:27 +01:00
if (cmValue name = source->GetProperty("Swift_DEPENDENCIES_FILE")) {
2020-08-30 11:54:41 +02:00
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 {
2021-11-20 13:41:27 +01:00
if (cmValue name = source->GetProperty("Swift_DIAGNOSTICS_FILE")) {
2020-08-30 11:54:41 +02:00
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,
2021-09-14 00:13:48 +02:00
std::string const& defines, std::string const& includes,
std::string const& outputConfig)
2012-04-19 19:04:21 +03:00
{
2021-09-14 00:13:48 +02:00
if (!this->GeneratorTarget->GetPropertyAsBool("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
2023-07-14 19:10:31 +02:00
std::string fullFlags = flags;
{
bool const needDyndep =
this->GetGeneratorTarget()->NeedDyndep(language, outputConfig);
std::string const modmapFormatVar =
cmStrCat("CMAKE_EXPERIMENTAL_", language, "_MODULE_MAP_FORMAT");
std::string const modmapFormat =
this->Makefile->GetSafeDefinition(modmapFormatVar);
if (needDyndep && !modmapFormat.empty()) {
std::string modmapFlags = this->GetMakefile()->GetRequiredDefinition(
cmStrCat("CMAKE_EXPERIMENTAL_", language, "_MODULE_MAP_FLAG"));
// XXX(modmap): If changing this path construction, change
// `cmGlobalNinjaGenerator::WriteDyndep` and
// `cmNinjaTargetGenerator::WriteObjectBuildStatement` to expect the
// corresponding file path.
cmSystemTools::ReplaceString(modmapFlags, "<MODULE_MAP_FILE>",
cmStrCat(objectFileName, ".modmap"));
fullFlags += cmStrCat(' ', modmapFlags);
}
}
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();
2023-07-14 19:10:31 +02:00
compileObjectVars.Flags = fullFlags.c_str();
2015-11-17 17:22:37 +01:00
compileObjectVars.Defines = defines.c_str();
compileObjectVars.Includes = includes.c_str();
// Rule for compiling object file.
2022-04-13 00:32:21 +02:00
std::string cudaCompileMode;
2017-07-20 19:35:53 +02:00
if (language == "CUDA") {
if (this->GeneratorTarget->GetPropertyAsBool(
"CUDA_SEPARABLE_COMPILATION")) {
2022-04-13 00:32:21 +02:00
const std::string& rdcFlag =
this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_RDC_FLAG");
cudaCompileMode = cmStrCat(cudaCompileMode, rdcFlag, " ");
}
2023-07-02 19:51:09 +02:00
static std::array<cm::string_view, 4> const compileModes{
{ "PTX"_s, "CUBIN"_s, "FATBIN"_s, "OPTIX"_s }
};
bool useNormalCompileMode = true;
for (cm::string_view mode : compileModes) {
auto propName = cmStrCat("CUDA_", mode, "_COMPILATION");
auto defName = cmStrCat("_CMAKE_CUDA_", mode, "_FLAG");
if (this->GeneratorTarget->GetPropertyAsBool(propName)) {
const std::string& flag =
this->Makefile->GetRequiredDefinition(defName);
cudaCompileMode = cmStrCat(cudaCompileMode, flag);
useNormalCompileMode = false;
break;
}
}
if (useNormalCompileMode) {
2022-04-13 00:32:21 +02:00
const std::string& wholeFlag =
this->Makefile->GetRequiredDefinition("_CMAKE_CUDA_WHOLE_FLAG");
cudaCompileMode = cmStrCat(cudaCompileMode, wholeFlag);
2017-07-20 19:35:53 +02:00
}
2022-04-13 00:32:21 +02:00
compileObjectVars.CudaCompileMode = cudaCompileMode.c_str();
2017-07-20 19:35:53 +02:00
}
2015-11-17 17:22:37 +01:00
2022-04-13 00:32:21 +02:00
const std::string cmdVar = cmStrCat("CMAKE_", language, "_COMPILE_OBJECT");
const std::string& compileCmd =
this->Makefile->GetRequiredDefinition(cmdVar);
2023-07-02 19:51:09 +02:00
cmList compileCmds(compileCmd);
2022-04-13 00:32:21 +02:00
2023-07-02 19:51:09 +02:00
auto rulePlaceholderExpander =
this->GetLocalGenerator()->CreateRulePlaceholderExpander();
2017-04-14 19:02:05 +02:00
2023-07-02 19:51:09 +02:00
for (auto& 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
2021-09-14 00:13:48 +02:00
std::string cmdLine = this->GetLocalGenerator()->BuildCommandLine(
compileCmds, outputConfig, outputConfig);
2015-11-17 17:22:37 +01:00
2023-05-23 16:38:00 +02:00
this->GetGlobalGenerator()->AddCXXCompileCommand(cmdLine, sourceFileName,
objectFileName);
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
{
2021-11-20 13:41:27 +01:00
if (cmValue prop_value =
2019-11-11 23:01:05 +01:00
this->GeneratorTarget->GetProperty("ADDITIONAL_CLEAN_FILES")) {
cmLocalNinjaGenerator* lg = this->LocalGenerator;
2023-07-02 19:51:09 +02:00
cmList cleanFiles(cmGeneratorExpression::Evaluate(*prop_value, lg, config,
this->GeneratorTarget));
2019-11-11 23:01:05 +01:00
std::string const& binaryDir = lg->GetCurrentBinaryDirectory();
cmGlobalNinjaGenerator* gg = lg->GetGlobalNinjaGenerator();
2023-07-02 19:51:09 +02:00
for (auto const& cleanFile : cleanFiles) {
2019-11-11 23:01:05 +01:00
// 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();
2021-11-20 13:41:27 +01:00
// Also ensures there is a trailing slash.
2016-07-09 11:21:54 +02:00
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
{
2021-09-14 00:13:48 +02:00
this->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
{
2021-11-20 13:41:27 +01:00
cmValue 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
}