cmake/Source/cmNinjaNormalTargetGenerator.cxx

1105 lines
40 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 "cmNinjaNormalTargetGenerator.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include <algorithm>
#include <assert.h>
#include <iterator>
#include <map>
2018-01-26 17:06:56 +01:00
#include <memory> // IWYU pragma: keep
2017-04-14 19:02:05 +02:00
#include <set>
#include <sstream>
2016-07-09 11:21:54 +02:00
#include "cmAlgorithms.h"
#include "cmCustomCommandGenerator.h"
2012-04-19 19:04:21 +03:00
#include "cmGeneratedFileStream.h"
2016-07-09 11:21:54 +02:00
#include "cmGeneratorTarget.h"
#include "cmGlobalNinjaGenerator.h"
2017-04-14 19:02:05 +02:00
#include "cmLinkLineComputer.h"
#include "cmLinkLineDeviceComputer.h"
2016-10-30 18:24:19 +01:00
#include "cmLocalGenerator.h"
2016-07-09 11:21:54 +02:00
#include "cmLocalNinjaGenerator.h"
2012-04-19 19:04:21 +03:00
#include "cmMakefile.h"
2016-10-30 18:24:19 +01:00
#include "cmNinjaTypes.h"
2012-08-04 10:26:08 +03:00
#include "cmOSXBundleGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmOutputConverter.h"
2017-04-14 19:02:05 +02:00
#include "cmRulePlaceholderExpander.h"
2016-07-09 11:21:54 +02:00
#include "cmSourceFile.h"
2016-10-30 18:24:19 +01:00
#include "cmState.h"
2017-04-14 19:02:05 +02:00
#include "cmStateDirectory.h"
#include "cmStateSnapshot.h"
#include "cmStateTypes.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
#include "cmake.h"
2012-04-19 19:04:21 +03:00
2018-01-26 17:06:56 +01:00
class cmCustomCommand;
2016-07-09 11:21:54 +02:00
cmNinjaNormalTargetGenerator::cmNinjaNormalTargetGenerator(
cmGeneratorTarget* target)
2015-11-17 17:22:37 +01:00
: cmNinjaTargetGenerator(target)
2012-04-19 19:04:21 +03:00
, TargetNameOut()
, TargetNameSO()
, TargetNameReal()
, TargetNameImport()
, TargetNamePDB()
2015-04-27 22:25:09 +02:00
, TargetLinkLanguage("")
2017-04-14 19:02:05 +02:00
, DeviceLinkObject()
2012-04-19 19:04:21 +03:00
{
2015-11-17 17:22:37 +01:00
this->TargetLinkLanguage = target->GetLinkerLanguage(this->GetConfigName());
2017-04-14 19:02:05 +02:00
if (target->GetType() == cmStateEnums::EXECUTABLE) {
2016-07-09 11:21:54 +02:00
this->GetGeneratorTarget()->GetExecutableNames(
this->TargetNameOut, this->TargetNameReal, this->TargetNameImport,
this->TargetNamePDB, GetLocalGenerator()->GetConfigName());
2016-10-30 18:24:19 +01:00
} else {
2016-07-09 11:21:54 +02:00
this->GetGeneratorTarget()->GetLibraryNames(
this->TargetNameOut, this->TargetNameSO, this->TargetNameReal,
this->TargetNameImport, this->TargetNamePDB,
GetLocalGenerator()->GetConfigName());
2016-10-30 18:24:19 +01:00
}
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
if (target->GetType() != cmStateEnums::OBJECT_LIBRARY) {
2012-04-19 19:04:21 +03:00
// on Windows the output dir is already needed at compile time
// ensure the directory exists (OutDir test)
2016-03-13 13:35:51 +01:00
EnsureDirectoryExists(target->GetDirectory(this->GetConfigName()));
2016-07-09 11:21:54 +02:00
}
2012-08-04 10:26:08 +03:00
2016-07-09 11:21:54 +02:00
this->OSXBundleGenerator =
new cmOSXBundleGenerator(target, this->GetConfigName());
2012-08-04 10:26:08 +03:00
this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
2012-04-19 19:04:21 +03:00
}
cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
{
2012-08-04 10:26:08 +03:00
delete this->OSXBundleGenerator;
2012-04-19 19:04:21 +03:00
}
void cmNinjaNormalTargetGenerator::Generate()
{
2015-04-27 22:25:09 +02:00
if (this->TargetLinkLanguage.empty()) {
2013-11-03 12:27:13 +02:00
cmSystemTools::Error("CMake can not determine linker language for "
"target: ",
2016-03-13 13:35:51 +01:00
this->GetGeneratorTarget()->GetName().c_str());
2012-04-19 19:04:21 +03:00
return;
}
// Write the rules for each language.
this->WriteLanguagesRules();
// Write the build statements
this->WriteObjectBuildStatements();
2017-04-14 19:02:05 +02:00
if (this->GetGeneratorTarget()->GetType() == cmStateEnums::OBJECT_LIBRARY) {
2012-04-19 19:04:21 +03:00
this->WriteObjectLibStatement();
2016-07-09 11:21:54 +02:00
} else {
2017-04-14 19:02:05 +02:00
// If this target has cuda language link inputs, and we need to do
// device linking
this->WriteDeviceLinkStatement();
2012-04-19 19:04:21 +03:00
this->WriteLinkStatement();
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
}
void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
{
2012-06-27 20:52:58 +03:00
#ifdef NINJA_GEN_VERBOSE_FILES
2012-04-19 19:04:21 +03:00
cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
this->GetRulesFileStream()
<< "# Rules for each languages 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-06-27 20:52:58 +03:00
#endif
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
// Write rules for languages compiled in this target.
std::set<std::string> languages;
2017-04-14 19:02:05 +02:00
std::vector<cmSourceFile const*> sourceFiles;
this->GetGeneratorTarget()->GetObjectSources(
2016-07-09 11:21:54 +02:00
sourceFiles, this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
2018-01-26 17:06:56 +01:00
for (cmSourceFile const* sf : sourceFiles) {
std::string const lang = sf->GetLanguage();
2016-07-09 11:21:54 +02:00
if (!lang.empty()) {
2015-04-27 22:25:09 +02:00
languages.insert(lang);
}
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
for (std::string const& language : languages) {
this->WriteLanguageRules(language);
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
const char* cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
2012-04-19 19:04:21 +03:00
{
2016-03-13 13:35:51 +01:00
switch (this->GetGeneratorTarget()->GetType()) {
2017-04-14 19:02:05 +02:00
case cmStateEnums::STATIC_LIBRARY:
2012-04-19 19:04:21 +03:00
return "static library";
2017-04-14 19:02:05 +02:00
case cmStateEnums::SHARED_LIBRARY:
2012-04-19 19:04:21 +03:00
return "shared library";
2017-04-14 19:02:05 +02:00
case cmStateEnums::MODULE_LIBRARY:
2016-10-30 18:24:19 +01:00
if (this->GetGeneratorTarget()->IsCFBundleOnApple()) {
2012-08-04 10:26:08 +03:00
return "CFBundle shared module";
2016-10-30 18:24:19 +01:00
} else {
2012-08-04 10:26:08 +03:00
return "shared module";
2016-10-30 18:24:19 +01:00
}
2017-04-14 19:02:05 +02:00
case cmStateEnums::EXECUTABLE:
2012-04-19 19:04:21 +03:00
return "executable";
default:
2018-01-26 17:06:56 +01:00
return nullptr;
2012-04-19 19:04:21 +03:00
}
}
2016-07-09 11:21:54 +02:00
std::string cmNinjaNormalTargetGenerator::LanguageLinkerRule() const
2012-04-19 19:04:21 +03:00
{
2016-07-09 11:21:54 +02:00
return this->TargetLinkLanguage + "_" +
cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()) +
"_LINKER__" + cmGlobalNinjaGenerator::EncodeRuleName(
this->GetGeneratorTarget()->GetName());
2012-04-19 19:04:21 +03:00
}
2017-04-14 19:02:05 +02:00
std::string cmNinjaNormalTargetGenerator::LanguageLinkerDeviceRule() const
{
return this->TargetLinkLanguage + "_" +
cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType()) +
"_DEVICE_LINKER__" + cmGlobalNinjaGenerator::EncodeRuleName(
this->GetGeneratorTarget()->GetName());
}
2016-10-30 18:24:19 +01:00
struct cmNinjaRemoveNoOpCommands
{
bool operator()(std::string const& cmd)
{
return cmd.empty() || cmd[0] == ':';
}
};
2017-04-14 19:02:05 +02:00
void cmNinjaNormalTargetGenerator::WriteDeviceLinkRule(bool useResponseFile)
{
cmStateEnums::TargetType targetType = this->GetGeneratorTarget()->GetType();
std::string ruleName = this->LanguageLinkerDeviceRule();
// Select whether to use a response file for objects.
std::string rspfile;
std::string rspcontent;
if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
cmRulePlaceholderExpander::RuleVariables vars;
vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
vars.CMTargetType =
cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
vars.Language = "CUDA";
std::string responseFlag;
if (!useResponseFile) {
vars.Objects = "$in";
vars.LinkLibraries = "$LINK_LIBRARIES";
} else {
std::string cmakeVarLang = "CMAKE_";
cmakeVarLang += this->TargetLinkLanguage;
// build response file name
std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
const char* flag = GetMakefile()->GetDefinition(cmakeLinkVar);
if (flag) {
responseFlag = flag;
} else {
responseFlag = "@";
}
rspfile = "$RSP_FILE";
responseFlag += rspfile;
// build response file content
if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
rspcontent = "$in";
} else {
rspcontent = "$in_newline";
}
rspcontent += " $LINK_LIBRARIES";
vars.Objects = responseFlag.c_str();
vars.LinkLibraries = "";
}
vars.ObjectDir = "$OBJECT_DIR";
vars.Target = "$TARGET_FILE";
vars.SONameFlag = "$SONAME_FLAG";
vars.TargetSOName = "$SONAME";
vars.TargetPDB = "$TARGET_PDB";
vars.TargetCompilePDB = "$TARGET_COMPILE_PDB";
vars.Flags = "$FLAGS";
vars.LinkFlags = "$LINK_FLAGS";
vars.Manifests = "$MANIFESTS";
std::string langFlags;
if (targetType != cmStateEnums::EXECUTABLE) {
langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
vars.LanguageCompileFlags = langFlags.c_str();
}
std::string launcher;
const char* val = this->GetLocalGenerator()->GetRuleLauncher(
this->GetGeneratorTarget(), "RULE_LAUNCH_LINK");
if (val && *val) {
launcher = val;
launcher += " ";
}
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
2017-04-14 19:02:05 +02:00
this->GetLocalGenerator()->CreateRulePlaceholderExpander());
// Rule for linking library/executable.
std::vector<std::string> linkCmds = this->ComputeDeviceLinkCmd();
2018-01-26 17:06:56 +01:00
for (std::string& linkCmd : linkCmds) {
linkCmd = launcher + linkCmd;
2017-04-14 19:02:05 +02:00
rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
2018-01-26 17:06:56 +01:00
linkCmd, vars);
2017-04-14 19:02:05 +02:00
}
2017-07-20 19:35:53 +02:00
// If there is no ranlib the command will be ":". Skip it.
cmEraseIf(linkCmds, cmNinjaRemoveNoOpCommands());
2017-04-14 19:02:05 +02:00
std::string linkCmd =
this->GetLocalGenerator()->BuildCommandLine(linkCmds);
// Write the linker rule with response file if needed.
std::ostringstream comment;
comment << "Rule for linking " << this->TargetLinkLanguage << " "
<< this->GetVisibleTypeName() << ".";
std::ostringstream description;
description << "Linking " << this->TargetLinkLanguage << " "
<< this->GetVisibleTypeName() << " $TARGET_FILE";
this->GetGlobalGenerator()->AddRule(ruleName, linkCmd, description.str(),
comment.str(),
/*depfile*/ "",
/*deptype*/ "", rspfile, rspcontent,
/*restat*/ "$RESTAT",
/*generator*/ false);
}
}
2016-07-09 11:21:54 +02:00
void cmNinjaNormalTargetGenerator::WriteLinkRule(bool useResponseFile)
2012-04-19 19:04:21 +03:00
{
2017-04-14 19:02:05 +02:00
cmStateEnums::TargetType targetType = this->GetGeneratorTarget()->GetType();
2012-04-19 19:04:21 +03:00
std::string ruleName = this->LanguageLinkerRule();
2012-06-27 20:52:58 +03:00
// Select whether to use a response file for objects.
std::string rspfile;
2012-08-04 10:26:08 +03:00
std::string rspcontent;
2012-04-19 19:04:21 +03:00
if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
2017-04-14 19:02:05 +02:00
cmRulePlaceholderExpander::RuleVariables vars;
vars.CMTargetName = this->GetGeneratorTarget()->GetName().c_str();
vars.CMTargetType =
cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType());
2015-04-27 22:25:09 +02:00
vars.Language = this->TargetLinkLanguage.c_str();
2012-06-27 20:52:58 +03:00
std::string responseFlag;
if (!useResponseFile) {
vars.Objects = "$in";
2013-03-16 19:13:01 +02:00
vars.LinkLibraries = "$LINK_PATH $LINK_LIBRARIES";
2012-06-27 20:52:58 +03:00
} else {
2016-07-09 11:21:54 +02:00
std::string cmakeVarLang = "CMAKE_";
cmakeVarLang += this->TargetLinkLanguage;
// build response file name
std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
const char* flag = GetMakefile()->GetDefinition(cmakeLinkVar);
if (flag) {
responseFlag = flag;
} else {
responseFlag = "@";
}
rspfile = "$RSP_FILE";
responseFlag += rspfile;
// build response file content
if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
rspcontent = "$in";
} else {
rspcontent = "$in_newline";
}
rspcontent += " $LINK_PATH $LINK_LIBRARIES";
vars.Objects = responseFlag.c_str();
vars.LinkLibraries = "";
2012-06-27 20:52:58 +03:00
}
vars.ObjectDir = "$OBJECT_DIR";
2012-08-04 10:26:08 +03:00
2015-04-27 22:25:09 +02:00
vars.Target = "$TARGET_FILE";
2012-08-04 10:26:08 +03:00
2012-06-27 20:52:58 +03:00
vars.SONameFlag = "$SONAME_FLAG";
2012-04-19 19:04:21 +03:00
vars.TargetSOName = "$SONAME";
vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
vars.TargetPDB = "$TARGET_PDB";
// Setup the target version.
std::string targetVersionMajor;
std::string targetVersionMinor;
{
2016-07-09 11:21:54 +02:00
std::ostringstream majorStream;
std::ostringstream minorStream;
int major;
int minor;
this->GetGeneratorTarget()->GetTargetVersion(major, minor);
majorStream << major;
minorStream << minor;
targetVersionMajor = majorStream.str();
targetVersionMinor = minorStream.str();
2012-04-19 19:04:21 +03:00
}
vars.TargetVersionMajor = targetVersionMajor.c_str();
vars.TargetVersionMinor = targetVersionMinor.c_str();
vars.Flags = "$FLAGS";
vars.LinkFlags = "$LINK_FLAGS";
2015-11-17 17:22:37 +01:00
vars.Manifests = "$MANIFESTS";
2012-04-19 19:04:21 +03:00
std::string langFlags;
2017-04-14 19:02:05 +02:00
if (targetType != cmStateEnums::EXECUTABLE) {
2015-04-27 22:25:09 +02:00
langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
2012-06-27 20:52:58 +03:00
vars.LanguageCompileFlags = langFlags.c_str();
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2017-04-14 19:02:05 +02:00
std::string launcher;
const char* val = this->GetLocalGenerator()->GetRuleLauncher(
this->GetGeneratorTarget(), "RULE_LAUNCH_LINK");
if (val && *val) {
launcher = val;
launcher += " ";
}
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmRulePlaceholderExpander> rulePlaceholderExpander(
2017-04-14 19:02:05 +02:00
this->GetLocalGenerator()->CreateRulePlaceholderExpander());
2012-06-27 20:52:58 +03:00
// Rule for linking library/executable.
2012-04-19 19:04:21 +03:00
std::vector<std::string> linkCmds = this->ComputeLinkCmd();
2018-01-26 17:06:56 +01:00
for (std::string& linkCmd : linkCmds) {
linkCmd = launcher + linkCmd;
2017-04-14 19:02:05 +02:00
rulePlaceholderExpander->ExpandRuleVariables(this->GetLocalGenerator(),
2018-01-26 17:06:56 +01:00
linkCmd, vars);
2016-07-09 11:21:54 +02:00
}
2017-07-20 19:35:53 +02:00
// If there is no ranlib the command will be ":". Skip it.
cmEraseIf(linkCmds, cmNinjaRemoveNoOpCommands());
2016-10-30 18:24:19 +01:00
2012-04-19 19:04:21 +03:00
linkCmds.insert(linkCmds.begin(), "$PRE_LINK");
linkCmds.push_back("$POST_BUILD");
std::string linkCmd =
this->GetLocalGenerator()->BuildCommandLine(linkCmds);
2012-06-27 20:52:58 +03:00
// Write the linker rule with response file if needed.
2015-04-27 22:25:09 +02:00
std::ostringstream comment;
2012-04-19 19:04:21 +03:00
comment << "Rule for linking " << this->TargetLinkLanguage << " "
<< this->GetVisibleTypeName() << ".";
2015-04-27 22:25:09 +02:00
std::ostringstream description;
2012-04-19 19:04:21 +03:00
description << "Linking " << this->TargetLinkLanguage << " "
2015-04-27 22:25:09 +02:00
<< this->GetVisibleTypeName() << " $TARGET_FILE";
2016-07-09 11:21:54 +02:00
this->GetGlobalGenerator()->AddRule(ruleName, linkCmd, description.str(),
2012-06-27 20:52:58 +03:00
comment.str(),
/*depfile*/ "",
2016-07-09 11:21:54 +02:00
/*deptype*/ "", rspfile, rspcontent,
2015-04-27 22:25:09 +02:00
/*restat*/ "$RESTAT",
2014-08-03 19:52:23 +02:00
/*generator*/ false);
2012-04-19 19:04:21 +03:00
}
2013-11-03 12:27:13 +02:00
if (this->TargetNameOut != this->TargetNameReal &&
2016-07-09 11:21:54 +02:00
!this->GetGeneratorTarget()->IsFrameworkOnApple()) {
2012-04-19 19:04:21 +03:00
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
2016-07-09 11:21:54 +02:00
cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
2017-04-14 19:02:05 +02:00
if (targetType == cmStateEnums::EXECUTABLE) {
2018-01-26 17:06:56 +01:00
std::vector<std::string> commandLines;
commandLines.push_back(cmakeCommand +
" -E cmake_symlink_executable $in $out");
commandLines.push_back("$POST_BUILD");
2016-07-09 11:21:54 +02:00
this->GetGlobalGenerator()->AddRule(
"CMAKE_SYMLINK_EXECUTABLE",
2018-01-26 17:06:56 +01:00
this->GetLocalGenerator()->BuildCommandLine(commandLines),
2016-07-09 11:21:54 +02:00
"Creating executable symlink $out", "Rule for creating "
"executable symlink.",
/*depfile*/ "",
/*deptype*/ "",
/*rspfile*/ "",
/*rspcontent*/ "",
/*restat*/ "",
/*generator*/ false);
2016-10-30 18:24:19 +01:00
} else {
2018-01-26 17:06:56 +01:00
std::vector<std::string> commandLines;
commandLines.push_back(cmakeCommand +
" -E cmake_symlink_library $in $SONAME $out");
commandLines.push_back("$POST_BUILD");
2016-07-09 11:21:54 +02:00
this->GetGlobalGenerator()->AddRule(
"CMAKE_SYMLINK_LIBRARY",
2018-01-26 17:06:56 +01:00
this->GetLocalGenerator()->BuildCommandLine(commandLines),
2016-07-09 11:21:54 +02:00
"Creating library symlink $out", "Rule for creating "
"library symlink.",
/*depfile*/ "",
/*deptype*/ "",
/*rspfile*/ "",
/*rspcontent*/ "",
/*restat*/ "",
/*generator*/ false);
2016-10-30 18:24:19 +01:00
}
2012-04-19 19:04:21 +03:00
}
}
2017-04-14 19:02:05 +02:00
std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeDeviceLinkCmd()
{
std::vector<std::string> linkCmds;
// this target requires separable cuda compilation
// now build the correct command depending on if the target is
// an executable or a dynamic library.
std::string linkCmd;
switch (this->GetGeneratorTarget()->GetType()) {
2017-07-20 19:35:53 +02:00
case cmStateEnums::STATIC_LIBRARY:
2017-04-14 19:02:05 +02:00
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::MODULE_LIBRARY: {
const std::string cudaLinkCmd(
this->GetMakefile()->GetDefinition("CMAKE_CUDA_DEVICE_LINK_LIBRARY"));
cmSystemTools::ExpandListArgument(cudaLinkCmd, linkCmds);
} break;
case cmStateEnums::EXECUTABLE: {
const std::string cudaLinkCmd(this->GetMakefile()->GetDefinition(
"CMAKE_CUDA_DEVICE_LINK_EXECUTABLE"));
cmSystemTools::ExpandListArgument(cudaLinkCmd, linkCmds);
} break;
default:
break;
}
return linkCmds;
}
2016-07-09 11:21:54 +02:00
std::vector<std::string> cmNinjaNormalTargetGenerator::ComputeLinkCmd()
2012-04-19 19:04:21 +03:00
{
std::vector<std::string> linkCmds;
2015-04-27 22:25:09 +02:00
cmMakefile* mf = this->GetMakefile();
{
2017-04-14 19:02:05 +02:00
// If we have a rule variable prefer it. In the case of static libraries
// this occurs when things like IPO is enabled, and we need to use the
// CMAKE_<lang>_CREATE_STATIC_LIBRARY_IPO define instead.
2016-07-09 11:21:54 +02:00
std::string linkCmdVar = this->GetGeneratorTarget()->GetCreateRuleVariable(
this->TargetLinkLanguage, this->GetConfigName());
const char* linkCmd = mf->GetDefinition(linkCmdVar);
if (linkCmd) {
2018-01-26 17:06:56 +01:00
std::string linkCmdStr = linkCmd;
if (this->GetGeneratorTarget()->HasImplibGNUtoMS()) {
std::string ruleVar = "CMAKE_";
ruleVar += this->GeneratorTarget->GetLinkerLanguage(this->ConfigName);
ruleVar += "_GNUtoMS_RULE";
if (const char* rule = this->Makefile->GetDefinition(ruleVar)) {
linkCmdStr += rule;
}
}
cmSystemTools::ExpandListArgument(linkCmdStr, linkCmds);
2016-10-30 18:24:19 +01:00
if (this->GetGeneratorTarget()->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
2018-01-26 17:06:56 +01:00
cmakeCommand += " -E __run_co_compile --lwyu=";
2016-10-30 18:24:19 +01:00
cmGeneratorTarget& gt = *this->GetGeneratorTarget();
const std::string cfgName = this->GetConfigName();
std::string targetOutput = ConvertToNinjaPath(gt.GetFullPath(cfgName));
2017-07-20 19:35:53 +02:00
std::string targetOutputReal = this->ConvertToNinjaPath(
gt.GetFullPath(cfgName, cmStateEnums::RuntimeBinaryArtifact,
/*realname=*/true));
2016-10-30 18:24:19 +01:00
cmakeCommand += targetOutputReal;
cmakeCommand += " || true";
linkCmds.push_back(cmakeCommand);
}
2016-07-09 11:21:54 +02:00
return linkCmds;
2015-04-27 22:25:09 +02:00
}
}
2016-03-13 13:35:51 +01:00
switch (this->GetGeneratorTarget()->GetType()) {
2017-04-14 19:02:05 +02:00
case cmStateEnums::STATIC_LIBRARY: {
2012-04-19 19:04:21 +03:00
// We have archive link commands set. First, delete the existing archive.
2015-04-27 22:25:09 +02:00
{
2016-07-09 11:21:54 +02:00
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
linkCmds.push_back(cmakeCommand + " -E remove $TARGET_FILE");
2015-04-27 22:25:09 +02:00
}
2012-04-19 19:04:21 +03:00
// TODO: Use ARCHIVE_APPEND for archives over a certain size.
{
2016-07-09 11:21:54 +02:00
std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_ARCHIVE_CREATE";
2017-07-20 19:35:53 +02:00
linkCmdVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
linkCmdVar, this->TargetLinkLanguage, this->GetConfigName());
2016-07-09 11:21:54 +02:00
const char* linkCmd = mf->GetRequiredDefinition(linkCmdVar);
cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
2012-04-19 19:04:21 +03:00
}
{
2016-07-09 11:21:54 +02:00
std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_ARCHIVE_FINISH";
2017-07-20 19:35:53 +02:00
linkCmdVar = this->GeneratorTarget->GetFeatureSpecificLinkRuleVariable(
linkCmdVar, this->TargetLinkLanguage, this->GetConfigName());
2016-07-09 11:21:54 +02:00
const char* linkCmd = mf->GetRequiredDefinition(linkCmdVar);
cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
2012-04-19 19:04:21 +03:00
}
return linkCmds;
}
2017-04-14 19:02:05 +02:00
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::EXECUTABLE:
2015-04-27 22:25:09 +02:00
break;
2012-04-19 19:04:21 +03:00
default:
2017-04-14 19:02:05 +02:00
assert(false && "Unexpected target type");
2012-04-19 19:04:21 +03:00
}
return std::vector<std::string>();
}
2017-04-14 19:02:05 +02:00
void cmNinjaNormalTargetGenerator::WriteDeviceLinkStatement()
{
cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
// determine if we need to do any device linking for this target
const std::string cuda_lang("CUDA");
cmGeneratorTarget::LinkClosure const* closure =
genTarget.GetLinkClosure(this->GetConfigName());
const bool hasCUDA =
(std::find(closure->Languages.begin(), closure->Languages.end(),
cuda_lang) != closure->Languages.end());
bool shouldHaveDeviceLinking = false;
switch (genTarget.GetType()) {
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::EXECUTABLE:
shouldHaveDeviceLinking = true;
break;
2017-07-20 19:35:53 +02:00
case cmStateEnums::STATIC_LIBRARY:
shouldHaveDeviceLinking =
genTarget.GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS");
break;
2017-04-14 19:02:05 +02:00
default:
break;
}
2017-07-20 19:35:53 +02:00
if (!(shouldHaveDeviceLinking && hasCUDA)) {
2017-04-14 19:02:05 +02:00
return;
}
// Now we can do device linking
// First and very important step is to make sure while inside this
// step our link language is set to CUDA
std::string cudaLinkLanguage = "CUDA";
std::string const objExt =
this->Makefile->GetSafeDefinition("CMAKE_CUDA_OUTPUT_EXTENSION");
std::string const cfgName = this->GetConfigName();
std::string const targetOutputReal = ConvertToNinjaPath(
genTarget.ObjectDirectory + "cmake_device_link" + objExt);
2017-07-20 19:35:53 +02:00
std::string const targetOutputImplib = ConvertToNinjaPath(
genTarget.GetFullPath(cfgName, cmStateEnums::ImportLibraryArtifact));
2017-04-14 19:02:05 +02:00
this->DeviceLinkObject = targetOutputReal;
// Write comments.
cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
const cmStateEnums::TargetType targetType = genTarget.GetType();
this->GetBuildFileStream() << "# Device Link build statements for "
<< cmState::GetTargetTypeName(targetType)
<< " target " << this->GetTargetName() << "\n\n";
// Compute the comment.
std::ostringstream comment;
comment << "Link the " << this->GetVisibleTypeName() << " "
<< targetOutputReal;
cmNinjaDeps emptyDeps;
cmNinjaVars vars;
// Compute outputs.
cmNinjaDeps outputs;
outputs.push_back(targetOutputReal);
// Compute specific libraries to link with.
cmNinjaDeps explicitDeps = this->GetObjects();
cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
std::string frameworkPath;
std::string linkPath;
std::string createRule = genTarget.GetCreateRuleVariable(
this->TargetLinkLanguage, this->GetConfigName());
const bool useWatcomQuote =
this->GetMakefile()->IsOn(createRule + "_USE_WATCOM_QUOTE");
cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
vars["TARGET_FILE"] =
localGen.ConvertToOutputFormat(targetOutputReal, cmOutputConverter::SHELL);
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmLinkLineComputer> linkLineComputer(
2017-04-14 19:02:05 +02:00
new cmNinjaLinkLineDeviceComputer(
this->GetLocalGenerator(),
this->GetLocalGenerator()->GetStateSnapshot().GetDirectory(),
this->GetGlobalGenerator()));
linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
localGen.GetTargetFlags(
linkLineComputer.get(), this->GetConfigName(), vars["LINK_LIBRARIES"],
vars["FLAGS"], vars["LINK_FLAGS"], frameworkPath, linkPath, &genTarget);
this->addPoolNinjaVariable("JOB_POOL_LINK", &genTarget, vars);
vars["LINK_FLAGS"] =
cmGlobalNinjaGenerator::EncodeLiteral(vars["LINK_FLAGS"]);
vars["MANIFESTS"] = this->GetManifests();
vars["LINK_PATH"] = frameworkPath + linkPath;
// Compute architecture specific link flags. Yes, these go into a different
// variable for executables, probably due to a mistake made when duplicating
// code between the Makefile executable and library generators.
if (targetType == cmStateEnums::EXECUTABLE) {
std::string t = vars["FLAGS"];
localGen.AddArchitectureFlags(t, &genTarget, cudaLinkLanguage, cfgName);
vars["FLAGS"] = t;
} else {
std::string t = vars["ARCH_FLAGS"];
localGen.AddArchitectureFlags(t, &genTarget, cudaLinkLanguage, cfgName);
vars["ARCH_FLAGS"] = t;
2018-01-26 17:06:56 +01:00
t.clear();
2017-07-20 19:35:53 +02:00
localGen.AddLanguageFlagsForLinking(t, &genTarget, cudaLinkLanguage,
cfgName);
2017-04-14 19:02:05 +02:00
vars["LANGUAGE_COMPILE_FLAGS"] = t;
}
if (this->GetGeneratorTarget()->HasSOName(cfgName)) {
vars["SONAME_FLAG"] =
this->GetMakefile()->GetSONameFlag(this->TargetLinkLanguage);
vars["SONAME"] = this->TargetNameSO;
if (targetType == cmStateEnums::SHARED_LIBRARY) {
std::string install_dir =
this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(cfgName);
if (!install_dir.empty()) {
vars["INSTALLNAME_DIR"] = localGen.ConvertToOutputFormat(
install_dir, cmOutputConverter::SHELL);
}
}
}
cmNinjaDeps byproducts;
if (!this->TargetNameImport.empty()) {
const std::string impLibPath = localGen.ConvertToOutputFormat(
targetOutputImplib, cmOutputConverter::SHELL);
vars["TARGET_IMPLIB"] = impLibPath;
EnsureParentDirectoryExists(impLibPath);
if (genTarget.HasImportLibrary()) {
byproducts.push_back(targetOutputImplib);
}
}
const std::string objPath = GetGeneratorTarget()->GetSupportDirectory();
vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
this->ConvertToNinjaPath(objPath), cmOutputConverter::SHELL);
EnsureDirectoryExists(objPath);
this->SetMsvcTargetPdbVariable(vars);
if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
// ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
std::string& linkLibraries = vars["LINK_LIBRARIES"];
std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
std::string& link_path = vars["LINK_PATH"];
std::replace(link_path.begin(), link_path.end(), '\\', '/');
}
const std::vector<cmCustomCommand>* cmdLists[3] = {
&genTarget.GetPreBuildCommands(), &genTarget.GetPreLinkCommands(),
&genTarget.GetPostBuildCommands()
};
std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
vars["POST_BUILD"] = localGen.BuildCommandLine(postBuildCmdLines);
std::vector<std::string>* cmdLineLists[3] = { &preLinkCmdLines,
&preLinkCmdLines,
&postBuildCmdLines };
for (unsigned i = 0; i != 3; ++i) {
2018-01-26 17:06:56 +01:00
for (cmCustomCommand const& cc : *cmdLists[i]) {
cmCustomCommandGenerator ccg(cc, cfgName, this->GetLocalGenerator());
2017-04-14 19:02:05 +02:00
localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
std::transform(ccByproducts.begin(), ccByproducts.end(),
std::back_inserter(byproducts), MapToNinjaPath());
}
}
cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
// Device linking currently doesn't support response files so
// do not check if the user has explicitly forced a response file.
2017-07-20 19:35:53 +02:00
int const commandLineLengthLimit =
static_cast<int>(cmSystemTools::CalculateCommandLineLengthLimit()) -
globalGen.GetRuleCmdLength(this->LanguageLinkerDeviceRule());
2017-04-14 19:02:05 +02:00
const std::string rspfile =
std::string(cmake::GetCMakeFilesDirectoryPostSlash()) +
genTarget.GetName() + ".rsp";
// Gather order-only dependencies.
cmNinjaDeps orderOnlyDeps;
this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
orderOnlyDeps);
// Write the build statement for this target.
bool usedResponseFile = false;
globalGen.WriteBuild(this->GetBuildFileStream(), comment.str(),
this->LanguageLinkerDeviceRule(), outputs,
/*implicitOuts=*/cmNinjaDeps(), explicitDeps,
implicitDeps, orderOnlyDeps, vars, rspfile,
commandLineLengthLimit, &usedResponseFile);
this->WriteDeviceLinkRule(usedResponseFile);
}
2015-04-27 22:25:09 +02:00
void cmNinjaNormalTargetGenerator::WriteLinkStatement()
{
2015-11-17 17:22:37 +01:00
cmGeneratorTarget& gt = *this->GetGeneratorTarget();
2015-04-27 22:25:09 +02:00
const std::string cfgName = this->GetConfigName();
2016-07-09 11:21:54 +02:00
std::string targetOutput = ConvertToNinjaPath(gt.GetFullPath(cfgName));
2017-07-20 19:35:53 +02:00
std::string targetOutputReal = ConvertToNinjaPath(
gt.GetFullPath(cfgName, cmStateEnums::RuntimeBinaryArtifact,
/*realname=*/true));
std::string targetOutputImplib = ConvertToNinjaPath(
gt.GetFullPath(cfgName, cmStateEnums::ImportLibraryArtifact));
2012-08-04 10:26:08 +03:00
2016-07-09 11:21:54 +02:00
if (gt.IsAppBundleOnApple()) {
2012-08-04 10:26:08 +03:00
// Create the app bundle
2016-03-13 13:35:51 +01:00
std::string outpath = gt.GetDirectory(cfgName);
2012-08-04 10:26:08 +03:00
this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
// Calculate the output path
2013-11-03 12:27:13 +02:00
targetOutput = outpath;
targetOutput += "/";
targetOutput += this->TargetNameOut;
2015-04-27 22:25:09 +02:00
targetOutput = this->ConvertToNinjaPath(targetOutput);
2013-11-03 12:27:13 +02:00
targetOutputReal = outpath;
targetOutputReal += "/";
targetOutputReal += this->TargetNameReal;
2015-04-27 22:25:09 +02:00
targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
2016-07-09 11:21:54 +02:00
} else if (gt.IsFrameworkOnApple()) {
2012-08-04 10:26:08 +03:00
// Create the library framework.
2015-04-27 22:25:09 +02:00
this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
2016-03-13 13:35:51 +01:00
gt.GetDirectory(cfgName));
2016-07-09 11:21:54 +02:00
} else if (gt.IsCFBundleOnApple()) {
2012-08-04 10:26:08 +03:00
// Create the core foundation bundle.
2015-04-27 22:25:09 +02:00
this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
2016-03-13 13:35:51 +01:00
gt.GetDirectory(cfgName));
2016-07-09 11:21:54 +02:00
}
2012-08-04 10:26:08 +03:00
2012-04-19 19:04:21 +03:00
// Write comments.
cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
2017-04-14 19:02:05 +02:00
const cmStateEnums::TargetType targetType = gt.GetType();
2016-07-09 11:21:54 +02:00
this->GetBuildFileStream() << "# Link build statements for "
<< cmState::GetTargetTypeName(targetType)
<< " target " << this->GetTargetName() << "\n\n";
2012-04-19 19:04:21 +03:00
cmNinjaDeps emptyDeps;
cmNinjaVars vars;
// Compute the comment.
2015-04-27 22:25:09 +02:00
std::ostringstream comment;
2016-07-09 11:21:54 +02:00
comment << "Link the " << this->GetVisibleTypeName() << " "
<< targetOutputReal;
2012-04-19 19:04:21 +03:00
// Compute outputs.
cmNinjaDeps outputs;
outputs.push_back(targetOutputReal);
// Compute specific libraries to link with.
2012-08-04 10:26:08 +03:00
cmNinjaDeps explicitDeps = this->GetObjects();
cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
2012-04-19 19:04:21 +03:00
2017-04-14 19:02:05 +02:00
if (!this->DeviceLinkObject.empty()) {
explicitDeps.push_back(this->DeviceLinkObject);
}
2015-04-27 22:25:09 +02:00
cmMakefile* mf = this->GetMakefile();
2013-03-16 19:13:01 +02:00
std::string frameworkPath;
std::string linkPath;
2015-04-27 22:25:09 +02:00
cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
2016-07-09 11:21:54 +02:00
std::string createRule = genTarget.GetCreateRuleVariable(
this->TargetLinkLanguage, this->GetConfigName());
bool useWatcomQuote = mf->IsOn(createRule + "_USE_WATCOM_QUOTE");
2015-04-27 22:25:09 +02:00
cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
vars["TARGET_FILE"] =
2016-07-09 11:21:54 +02:00
localGen.ConvertToOutputFormat(targetOutputReal, cmOutputConverter::SHELL);
2018-01-26 17:06:56 +01:00
std::unique_ptr<cmLinkLineComputer> linkLineComputer(
2017-04-14 19:02:05 +02:00
this->GetGlobalGenerator()->CreateLinkLineComputer(
this->GetLocalGenerator(),
this->GetLocalGenerator()->GetStateSnapshot().GetDirectory()));
linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
localGen.GetTargetFlags(
linkLineComputer.get(), this->GetConfigName(), vars["LINK_LIBRARIES"],
vars["FLAGS"], vars["LINK_FLAGS"], frameworkPath, linkPath, &genTarget);
2016-07-09 11:21:54 +02:00
// Add OS X version flags, if any.
2017-04-14 19:02:05 +02:00
if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
this->GeneratorTarget->GetType() == cmStateEnums::MODULE_LIBRARY) {
2016-07-09 11:21:54 +02:00
this->AppendOSXVerFlag(vars["LINK_FLAGS"], this->TargetLinkLanguage,
"COMPATIBILITY", true);
this->AppendOSXVerFlag(vars["LINK_FLAGS"], this->TargetLinkLanguage,
"CURRENT", false);
}
2015-04-27 22:25:09 +02:00
2016-03-13 13:35:51 +01:00
this->addPoolNinjaVariable("JOB_POOL_LINK", &gt, vars);
2014-08-03 19:52:23 +02:00
2017-04-14 19:02:05 +02:00
this->AddModuleDefinitionFlag(linkLineComputer.get(), vars["LINK_FLAGS"]);
2016-07-09 11:21:54 +02:00
vars["LINK_FLAGS"] =
cmGlobalNinjaGenerator::EncodeLiteral(vars["LINK_FLAGS"]);
2013-03-16 19:13:01 +02:00
2015-11-17 17:22:37 +01:00
vars["MANIFESTS"] = this->GetManifests();
2013-03-16 19:13:01 +02:00
vars["LINK_PATH"] = frameworkPath + linkPath;
2016-10-30 18:24:19 +01:00
std::string lwyuFlags;
if (genTarget.GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
lwyuFlags = " -Wl,--no-as-needed";
}
2012-04-19 19:04:21 +03:00
// Compute architecture specific link flags. Yes, these go into a different
// variable for executables, probably due to a mistake made when duplicating
// code between the Makefile executable and library generators.
2017-04-14 19:02:05 +02:00
if (targetType == cmStateEnums::EXECUTABLE) {
2015-04-27 22:25:09 +02:00
std::string t = vars["FLAGS"];
localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
2016-10-30 18:24:19 +01:00
t += lwyuFlags;
2015-04-27 22:25:09 +02:00
vars["FLAGS"] = t;
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
std::string t = vars["ARCH_FLAGS"];
localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
vars["ARCH_FLAGS"] = t;
2018-01-26 17:06:56 +01:00
t.clear();
2016-10-30 18:24:19 +01:00
t += lwyuFlags;
2017-07-20 19:35:53 +02:00
localGen.AddLanguageFlagsForLinking(t, &genTarget, TargetLinkLanguage,
cfgName);
2015-04-27 22:25:09 +02:00
vars["LANGUAGE_COMPILE_FLAGS"] = t;
2016-07-09 11:21:54 +02:00
}
if (this->GetGeneratorTarget()->HasSOName(cfgName)) {
2015-04-27 22:25:09 +02:00
vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
2012-06-27 20:52:58 +03:00
vars["SONAME"] = this->TargetNameSO;
2017-04-14 19:02:05 +02:00
if (targetType == cmStateEnums::SHARED_LIBRARY) {
2015-11-17 17:22:37 +01:00
std::string install_dir =
2016-07-09 11:21:54 +02:00
this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(cfgName);
if (!install_dir.empty()) {
2016-10-30 18:24:19 +01:00
vars["INSTALLNAME_DIR"] = localGen.ConvertToOutputFormat(
install_dir, cmOutputConverter::SHELL);
2012-06-27 20:52:58 +03:00
}
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2015-11-17 17:22:37 +01:00
cmNinjaDeps byproducts;
2016-07-09 11:21:54 +02:00
if (!this->TargetNameImport.empty()) {
2015-04-27 22:25:09 +02:00
const std::string impLibPath = localGen.ConvertToOutputFormat(
2016-07-09 11:21:54 +02:00
targetOutputImplib, cmOutputConverter::SHELL);
2013-03-16 19:13:01 +02:00
vars["TARGET_IMPLIB"] = impLibPath;
EnsureParentDirectoryExists(impLibPath);
2016-07-09 11:21:54 +02:00
if (genTarget.HasImportLibrary()) {
2015-11-17 17:22:37 +01:00
byproducts.push_back(targetOutputImplib);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2016-07-09 11:21:54 +02:00
if (!this->SetMsvcTargetPdbVariable(vars)) {
2012-08-04 10:26:08 +03:00
// It is common to place debug symbols at a specific place,
// so we need a plain target name in the rule available.
std::string prefix;
std::string base;
std::string suffix;
2015-11-17 17:22:37 +01:00
this->GetGeneratorTarget()->GetFullNameComponents(prefix, base, suffix);
2012-08-04 10:26:08 +03:00
std::string dbg_suffix = ".dbg";
// TODO: Where to document?
2016-07-09 11:21:54 +02:00
if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX")) {
2012-08-04 10:26:08 +03:00
dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
}
2016-07-09 11:21:54 +02:00
vars["TARGET_PDB"] = base + suffix + dbg_suffix;
}
2012-06-27 20:52:58 +03:00
2016-03-13 13:35:51 +01:00
const std::string objPath = GetGeneratorTarget()->GetSupportDirectory();
2016-07-09 11:21:54 +02:00
vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
this->ConvertToNinjaPath(objPath), cmOutputConverter::SHELL);
2015-11-17 17:22:37 +01:00
EnsureDirectoryExists(objPath);
2016-07-09 11:21:54 +02:00
if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
2013-11-03 12:27:13 +02:00
// ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
2012-08-04 10:26:08 +03:00
std::string& linkLibraries = vars["LINK_LIBRARIES"];
std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
2015-04-27 22:25:09 +02:00
std::string& link_path = vars["LINK_PATH"];
std::replace(link_path.begin(), link_path.end(), '\\', '/');
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2016-07-09 11:21:54 +02:00
const std::vector<cmCustomCommand>* cmdLists[3] = {
&gt.GetPreBuildCommands(), &gt.GetPreLinkCommands(),
2016-03-13 13:35:51 +01:00
&gt.GetPostBuildCommands()
2012-04-19 19:04:21 +03:00
};
std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
2016-07-09 11:21:54 +02:00
std::vector<std::string>* cmdLineLists[3] = { &preLinkCmdLines,
&preLinkCmdLines,
&postBuildCmdLines };
for (unsigned i = 0; i != 3; ++i) {
2018-01-26 17:06:56 +01:00
for (cmCustomCommand const& cc : *cmdLists[i]) {
cmCustomCommandGenerator ccg(cc, cfgName, this->GetLocalGenerator());
2015-04-27 22:25:09 +02:00
localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
std::vector<std::string> const& ccByproducts = ccg.GetByproducts();
std::transform(ccByproducts.begin(), ccByproducts.end(),
std::back_inserter(byproducts), MapToNinjaPath());
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2015-11-17 17:22:37 +01:00
// maybe create .def file from list of objects
2017-07-20 19:35:53 +02:00
cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
gt.GetModuleDefinitionInfo(this->GetConfigName());
if (mdi && mdi->DefFileGenerated) {
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
cmSystemTools::GetCMakeCommand(), cmOutputConverter::SHELL);
std::string cmd = cmakeCommand;
cmd += " -E __create_def ";
cmd += this->GetLocalGenerator()->ConvertToOutputFormat(
mdi->DefFile, cmOutputConverter::SHELL);
cmd += " ";
std::string obj_list_file = mdi->DefFile + ".objs";
cmd += this->GetLocalGenerator()->ConvertToOutputFormat(
obj_list_file, cmOutputConverter::SHELL);
preLinkCmdLines.push_back(cmd);
// create a list of obj files for the -E __create_def to read
cmGeneratedFileStream fout(obj_list_file.c_str());
if (mdi->WindowsExportAllSymbols) {
2015-11-17 17:22:37 +01:00
cmNinjaDeps objs = this->GetObjects();
2018-01-26 17:06:56 +01:00
for (std::string const& obj : objs) {
if (cmHasLiteralSuffix(obj, ".obj")) {
fout << obj << "\n";
2015-11-17 17:22:37 +01:00
}
}
}
2017-07-20 19:35:53 +02:00
2018-01-26 17:06:56 +01:00
for (cmSourceFile const* src : mdi->Sources) {
fout << src->GetFullPath() << "\n";
2017-07-20 19:35:53 +02:00
}
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
// If we have any PRE_LINK commands, we need to go back to CMAKE_BINARY_DIR
// for
2012-04-19 19:04:21 +03:00
// the link commands.
2016-07-09 11:21:54 +02:00
if (!preLinkCmdLines.empty()) {
2015-04-27 22:25:09 +02:00
const std::string homeOutDir = localGen.ConvertToOutputFormat(
2016-07-09 11:21:54 +02:00
localGen.GetBinaryDirectory(), cmOutputConverter::SHELL);
2013-03-16 19:13:01 +02:00
preLinkCmdLines.push_back("cd " + homeOutDir);
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
2012-04-19 19:04:21 +03:00
cmNinjaVars symlinkVars;
2016-10-30 18:24:19 +01:00
bool const symlinkNeeded =
(targetOutput != targetOutputReal && !gt.IsFrameworkOnApple());
if (!symlinkNeeded) {
2012-04-19 19:04:21 +03:00
vars["POST_BUILD"] = postBuildCmdLine;
2016-07-09 11:21:54 +02:00
} else {
2017-04-14 19:02:05 +02:00
vars["POST_BUILD"] = cmGlobalNinjaGenerator::SHELL_NOOP;
2012-04-19 19:04:21 +03:00
symlinkVars["POST_BUILD"] = postBuildCmdLine;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
2013-03-16 19:13:01 +02:00
2017-04-14 19:02:05 +02:00
bool const lang_supports_response =
!(this->TargetLinkLanguage == "RC" || this->TargetLinkLanguage == "CUDA");
2016-07-09 11:21:54 +02:00
int commandLineLengthLimit = -1;
2017-04-14 19:02:05 +02:00
if (!lang_supports_response || !this->ForceResponseFile()) {
2017-07-20 19:35:53 +02:00
commandLineLengthLimit =
static_cast<int>(cmSystemTools::CalculateCommandLineLengthLimit()) -
globalGen.GetRuleCmdLength(this->LanguageLinkerRule());
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
const std::string rspfile =
2016-07-09 11:21:54 +02:00
std::string(cmake::GetCMakeFilesDirectoryPostSlash()) + gt.GetName() +
".rsp";
2013-11-03 12:27:13 +02:00
2015-04-27 22:25:09 +02:00
// Gather order-only dependencies.
cmNinjaDeps orderOnlyDeps;
2016-03-13 13:35:51 +01:00
this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
2016-07-09 11:21:54 +02:00
orderOnlyDeps);
2013-11-03 12:27:13 +02:00
2015-04-27 22:25:09 +02:00
// Ninja should restat after linking if and only if there are byproducts.
2016-07-09 11:21:54 +02:00
vars["RESTAT"] = byproducts.empty() ? "" : "1";
2015-04-27 22:25:09 +02:00
2018-01-26 17:06:56 +01:00
for (std::string const& o : byproducts) {
this->GetGlobalGenerator()->SeenCustomCommandOutput(o);
outputs.push_back(o);
2016-07-09 11:21:54 +02:00
}
2012-06-27 20:52:58 +03:00
2012-04-19 19:04:21 +03:00
// Write the build statement for this target.
2015-08-17 11:37:30 +02:00
bool usedResponseFile = false;
2016-07-09 11:21:54 +02:00
globalGen.WriteBuild(this->GetBuildFileStream(), comment.str(),
2016-10-30 18:24:19 +01:00
this->LanguageLinkerRule(), outputs,
/*implicitOuts=*/cmNinjaDeps(), explicitDeps,
2016-07-09 11:21:54 +02:00
implicitDeps, orderOnlyDeps, vars, rspfile,
commandLineLengthLimit, &usedResponseFile);
2015-08-17 11:37:30 +02:00
this->WriteLinkRule(usedResponseFile);
2015-04-27 22:25:09 +02:00
2016-10-30 18:24:19 +01:00
if (symlinkNeeded) {
2017-04-14 19:02:05 +02:00
if (targetType == cmStateEnums::EXECUTABLE) {
2016-07-09 11:21:54 +02:00
globalGen.WriteBuild(
this->GetBuildFileStream(),
"Create executable symlink " + targetOutput,
"CMAKE_SYMLINK_EXECUTABLE", cmNinjaDeps(1, targetOutput),
2016-10-30 18:24:19 +01:00
/*implicitOuts=*/cmNinjaDeps(), cmNinjaDeps(1, targetOutputReal),
emptyDeps, emptyDeps, symlinkVars);
2016-07-09 11:21:54 +02:00
} else {
2012-08-04 10:26:08 +03:00
cmNinjaDeps symlinks;
2016-07-09 11:21:54 +02:00
std::string const soName =
this->ConvertToNinjaPath(this->GetTargetFilePath(this->TargetNameSO));
2012-08-04 10:26:08 +03:00
// If one link has to be created.
2016-07-09 11:21:54 +02:00
if (targetOutputReal == soName || targetOutput == soName) {
2012-08-04 10:26:08 +03:00
symlinkVars["SONAME"] = soName;
2016-07-09 11:21:54 +02:00
} else {
2018-01-26 17:06:56 +01:00
symlinkVars["SONAME"].clear();
2012-08-04 10:26:08 +03:00
symlinks.push_back(soName);
2015-04-27 22:25:09 +02:00
}
2016-07-09 11:21:54 +02:00
symlinks.push_back(targetOutput);
globalGen.WriteBuild(
this->GetBuildFileStream(), "Create library symlink " + targetOutput,
2016-10-30 18:24:19 +01:00
"CMAKE_SYMLINK_LIBRARY", symlinks,
/*implicitOuts=*/cmNinjaDeps(), cmNinjaDeps(1, targetOutputReal),
2016-07-09 11:21:54 +02:00
emptyDeps, emptyDeps, symlinkVars);
2012-04-19 19:04:21 +03:00
}
2016-07-09 11:21:54 +02:00
}
2012-04-19 19:04:21 +03:00
// Add aliases for the file name and the target name.
2016-03-13 13:35:51 +01:00
globalGen.AddTargetAlias(this->TargetNameOut, &gt);
globalGen.AddTargetAlias(this->GetTargetName(), &gt);
2012-04-19 19:04:21 +03:00
}
void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
{
// Write a phony output that depends on all object files.
cmNinjaDeps outputs;
2016-03-13 13:35:51 +01:00
this->GetLocalGenerator()->AppendTargetOutputs(this->GetGeneratorTarget(),
outputs);
2012-04-19 19:04:21 +03:00
cmNinjaDeps depends = this->GetObjects();
2016-07-09 11:21:54 +02:00
this->GetGlobalGenerator()->WritePhonyBuild(
this->GetBuildFileStream(), "Object library " + this->GetTargetName(),
outputs, depends);
2012-04-19 19:04:21 +03:00
// Add aliases for the target name.
this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
2016-03-13 13:35:51 +01:00
this->GetGeneratorTarget());
2012-04-19 19:04:21 +03:00
}