cmake/Source/cmNinjaNormalTargetGenerator.cxx

796 lines
27 KiB
C++
Raw Normal View History

2012-04-19 19:04:21 +03:00
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2011 Peter Collingbourne <peter@pcc.me.uk>
Copyright 2011 Nicolas Despres <nicolas.despres@gmail.com>
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmNinjaNormalTargetGenerator.h"
#include "cmLocalNinjaGenerator.h"
#include "cmGlobalNinjaGenerator.h"
#include "cmSourceFile.h"
#include "cmGeneratedFileStream.h"
#include "cmMakefile.h"
2012-08-04 10:26:08 +03:00
#include "cmOSXBundleGenerator.h"
2014-08-03 19:52:23 +02:00
#include "cmGeneratorTarget.h"
2015-04-27 22:25:09 +02:00
#include "cmCustomCommandGenerator.h"
2015-08-17 11:37:30 +02:00
#include "cmAlgorithms.h"
2012-04-19 19:04:21 +03:00
#include <assert.h>
2012-08-04 10:26:08 +03:00
#include <algorithm>
2015-04-27 22:25:09 +02:00
#include <limits>
2012-08-04 10:26:08 +03:00
#ifndef _WIN32
#include <unistd.h>
#endif
2012-04-19 19:04:21 +03:00
cmNinjaNormalTargetGenerator::
2014-08-03 19:52:23 +02:00
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("")
2012-04-19 19:04:21 +03:00
{
2015-11-17 17:22:37 +01:00
this->TargetLinkLanguage = target->GetLinkerLanguage(this->GetConfigName());
2016-03-13 13:35:51 +01:00
if (target->GetType() == cmState::EXECUTABLE)
2015-11-17 17:22:37 +01:00
this->GetGeneratorTarget()->GetExecutableNames(this->TargetNameOut,
2012-04-19 19:04:21 +03:00
this->TargetNameReal,
this->TargetNameImport,
this->TargetNamePDB,
GetLocalGenerator()->GetConfigName());
else
2015-11-17 17:22:37 +01:00
this->GetGeneratorTarget()->GetLibraryNames(this->TargetNameOut,
2012-04-19 19:04:21 +03:00
this->TargetNameSO,
this->TargetNameReal,
this->TargetNameImport,
this->TargetNamePDB,
GetLocalGenerator()->GetConfigName());
2016-03-13 13:35:51 +01:00
if(target->GetType() != cmState::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()));
2012-04-19 19:04:21 +03:00
}
2012-08-04 10:26:08 +03:00
this->OSXBundleGenerator = new cmOSXBundleGenerator(target,
this->GetConfigName());
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();
2016-03-13 13:35:51 +01:00
if(this->GetGeneratorTarget()->GetType() == cmState::OBJECT_LIBRARY)
2012-04-19 19:04:21 +03:00
{
this->WriteObjectLibStatement();
}
else
{
this->WriteLinkStatement();
}
}
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())
2012-04-19 19:04:21 +03: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;
std::vector<cmSourceFile*> sourceFiles;
2016-03-13 13:35:51 +01:00
this->GetGeneratorTarget()->GetSourceFiles(sourceFiles,
2015-04-27 22:25:09 +02:00
this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
for(std::vector<cmSourceFile*>::const_iterator
i = sourceFiles.begin(); i != sourceFiles.end(); ++i)
{
const std::string& lang = (*i)->GetLanguage();
if(!lang.empty())
{
languages.insert(lang);
}
}
for(std::set<std::string>::const_iterator l = languages.begin();
2012-04-19 19:04:21 +03:00
l != languages.end();
++l)
2015-04-27 22:25:09 +02:00
{
2012-04-19 19:04:21 +03:00
this->WriteLanguageRules(*l);
2015-04-27 22:25:09 +02:00
}
2012-04-19 19:04:21 +03:00
}
const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
{
2016-03-13 13:35:51 +01:00
switch (this->GetGeneratorTarget()->GetType()) {
case cmState::STATIC_LIBRARY:
2012-04-19 19:04:21 +03:00
return "static library";
2016-03-13 13:35:51 +01:00
case cmState::SHARED_LIBRARY:
2012-04-19 19:04:21 +03:00
return "shared library";
2016-03-13 13:35:51 +01:00
case cmState::MODULE_LIBRARY:
if (this->GetGeneratorTarget()->IsCFBundleOnApple())
2012-08-04 10:26:08 +03:00
return "CFBundle shared module";
else
return "shared module";
2016-03-13 13:35:51 +01:00
case cmState::EXECUTABLE:
2012-04-19 19:04:21 +03:00
return "executable";
default:
return 0;
}
}
std::string
cmNinjaNormalTargetGenerator
::LanguageLinkerRule() const
{
2015-04-27 22:25:09 +02:00
return this->TargetLinkLanguage
2012-04-19 19:04:21 +03:00
+ "_"
2016-03-13 13:35:51 +01:00
+ cmState::GetTargetTypeName(this->GetGeneratorTarget()->GetType())
2015-08-17 11:37:30 +02:00
+ "_LINKER__"
2016-03-13 13:35:51 +01:00
+ cmGlobalNinjaGenerator::EncodeRuleName(
this->GetGeneratorTarget()->GetName())
2015-08-17 11:37:30 +02:00
;
2012-04-19 19:04:21 +03:00
}
void
cmNinjaNormalTargetGenerator
2012-06-27 20:52:58 +03:00
::WriteLinkRule(bool useResponseFile)
2012-04-19 19:04:21 +03:00
{
2016-03-13 13:35:51 +01:00
cmState::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)) {
cmLocalGenerator::RuleVariables vars;
vars.RuleLauncher = "RULE_LAUNCH_LINK";
2016-03-13 13:35:51 +01:00
vars.CMTarget = this->GetGeneratorTarget();
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 {
2013-03-16 19:13:01 +02:00
std::string cmakeVarLang = "CMAKE_";
cmakeVarLang += this->TargetLinkLanguage;
// build response file name
std::string cmakeLinkVar = cmakeVarLang + "_RESPONSE_FILE_LINK_FLAG";
2015-04-27 22:25:09 +02:00
const char * flag = GetMakefile()->GetDefinition(cmakeLinkVar);
2012-06-27 20:52:58 +03:00
if(flag) {
responseFlag = flag;
} else {
responseFlag = "@";
}
2013-03-16 19:13:01 +02:00
rspfile = "$RSP_FILE";
2012-06-27 20:52:58 +03:00
responseFlag += rspfile;
2013-03-16 19:13:01 +02:00
// build response file content
2015-08-17 11:37:30 +02:00
if (this->GetGlobalGenerator()->IsGCCOnWindows()) {
rspcontent = "$in";
} else {
rspcontent = "$in_newline";
}
rspcontent += " $LINK_PATH $LINK_LIBRARIES";
2012-06-27 20:52:58 +03:00
vars.Objects = responseFlag.c_str();
2012-08-04 10:26:08 +03:00
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;
{
2015-04-27 22:25:09 +02:00
std::ostringstream majorStream;
std::ostringstream minorStream;
2012-04-19 19:04:21 +03:00
int major;
int minor;
2016-03-13 13:35:51 +01:00
this->GetGeneratorTarget()->GetTargetVersion(major, minor);
2012-04-19 19:04:21 +03:00
majorStream << major;
minorStream << minor;
targetVersionMajor = majorStream.str();
targetVersionMinor = minorStream.str();
}
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;
2016-03-13 13:35:51 +01:00
if (targetType != cmState::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();
2015-04-27 22:25:09 +02:00
}
2012-04-19 19:04:21 +03:00
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();
for(std::vector<std::string>::iterator i = linkCmds.begin();
i != linkCmds.end();
++i)
{
this->GetLocalGenerator()->ExpandRuleVariables(*i, vars);
}
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";
2012-04-19 19:04:21 +03:00
this->GetGlobalGenerator()->AddRule(ruleName,
linkCmd,
description.str(),
2012-06-27 20:52:58 +03:00
comment.str(),
/*depfile*/ "",
2014-08-03 19:52:23 +02:00
/*deptype*/ "",
2012-08-04 10:26:08 +03:00
rspfile,
2014-08-03 19:52:23 +02:00
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-03-13 13:35:51 +01:00
!this->GetGeneratorTarget()->IsFrameworkOnApple()) {
2012-04-19 19:04:21 +03:00
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
2015-08-17 11:37:30 +02:00
cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
2016-03-13 13:35:51 +01:00
if (targetType == cmState::EXECUTABLE)
2012-04-19 19:04:21 +03:00
this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
cmakeCommand +
" -E cmake_symlink_executable"
" $in $out && $POST_BUILD",
"Creating executable symlink $out",
2014-08-03 19:52:23 +02:00
"Rule for creating "
"executable symlink.",
/*depfile*/ "",
/*deptype*/ "",
/*rspfile*/ "",
/*rspcontent*/ "",
2015-04-27 22:25:09 +02:00
/*restat*/ "",
2014-08-03 19:52:23 +02:00
/*generator*/ false);
2012-04-19 19:04:21 +03:00
else
this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
cmakeCommand +
" -E cmake_symlink_library"
" $in $SONAME $out && $POST_BUILD",
"Creating library symlink $out",
2014-08-03 19:52:23 +02:00
"Rule for creating "
"library symlink.",
/*depfile*/ "",
/*deptype*/ "",
/*rspfile*/ "",
/*rspcontent*/ "",
2015-04-27 22:25:09 +02:00
/*restat*/ "",
2014-08-03 19:52:23 +02:00
/*generator*/ false);
2012-04-19 19:04:21 +03:00
}
}
std::vector<std::string>
cmNinjaNormalTargetGenerator
::ComputeLinkCmd()
{
std::vector<std::string> linkCmds;
2015-04-27 22:25:09 +02:00
cmMakefile* mf = this->GetMakefile();
{
std::string linkCmdVar = this->GetGeneratorTarget()
->GetCreateRuleVariable(this->TargetLinkLanguage, this->GetConfigName());
const char *linkCmd = mf->GetDefinition(linkCmdVar);
if (linkCmd)
{
cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
return linkCmds;
}
}
2016-03-13 13:35:51 +01:00
switch (this->GetGeneratorTarget()->GetType()) {
case cmState::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
{
2012-04-19 19:04:21 +03:00
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
2015-08-17 11:37:30 +02:00
cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
2015-04-27 22:25:09 +02:00
linkCmds.push_back(cmakeCommand + " -E remove $TARGET_FILE");
}
2012-04-19 19:04:21 +03:00
// TODO: Use ARCHIVE_APPEND for archives over a certain size.
{
std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_ARCHIVE_CREATE";
2015-04-27 22:25:09 +02:00
const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
2012-04-19 19:04:21 +03:00
cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
}
{
std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_ARCHIVE_FINISH";
2015-04-27 22:25:09 +02:00
const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
2012-04-19 19:04:21 +03:00
cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
}
return linkCmds;
}
2016-03-13 13:35:51 +01:00
case cmState::SHARED_LIBRARY:
case cmState::MODULE_LIBRARY:
case cmState::EXECUTABLE:
2015-04-27 22:25:09 +02:00
break;
2012-04-19 19:04:21 +03:00
default:
assert(0 && "Unexpected target type");
}
return std::vector<std::string>();
}
2015-04-27 22:25:09 +02:00
static int calculateCommandLineLengthLimit(int linkRuleLength)
2012-04-19 19:04:21 +03:00
{
2015-04-27 22:25:09 +02:00
static int const limits[] = {
#ifdef _WIN32
8000,
#endif
#if defined(__APPLE__) || defined(__HAIKU__) || defined(__linux)
// for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
((int)sysconf(_SC_ARG_MAX)) - 1000,
#endif
#if defined(__linux)
// #define MAX_ARG_STRLEN (PAGE_SIZE * 32) in Linux's binfmts.h
((int)sysconf(_SC_PAGESIZE) * 32) - 1000,
#endif
std::numeric_limits<int>::max()
};
size_t const arrSz = cmArraySize(limits);
int const sz = *std::min_element(limits, limits + arrSz);
if (sz == std::numeric_limits<int>::max())
{
return -1;
}
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
return sz - linkRuleLength;
}
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();
2012-08-04 10:26:08 +03:00
std::string targetOutput = ConvertToNinjaPath(
2015-11-17 17:22:37 +01:00
gt.GetFullPath(cfgName));
2012-08-04 10:26:08 +03:00
std::string targetOutputReal = ConvertToNinjaPath(
2015-11-17 17:22:37 +01:00
gt.GetFullPath(cfgName,
2015-04-27 22:25:09 +02:00
/*implib=*/false,
/*realpath=*/true));
2012-08-04 10:26:08 +03:00
std::string targetOutputImplib = ConvertToNinjaPath(
2015-11-17 17:22:37 +01:00
gt.GetFullPath(cfgName,
2015-04-27 22:25:09 +02:00
/*implib=*/true));
2012-08-04 10:26:08 +03:00
2016-03-13 13:35:51 +01: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);
2012-08-04 10:26:08 +03:00
}
2016-03-13 13:35:51 +01: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));
2012-08-04 10:26:08 +03:00
}
2016-03-13 13:35:51 +01: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));
2012-08-04 10:26:08 +03:00
}
2012-04-19 19:04:21 +03:00
// Write comments.
cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
2016-03-13 13:35:51 +01:00
const cmState::TargetType targetType = gt.GetType();
2012-04-19 19:04:21 +03:00
this->GetBuildFileStream()
<< "# Link build statements for "
2016-03-13 13:35:51 +01:00
<< cmState::GetTargetTypeName(targetType)
2012-04-19 19:04:21 +03:00
<< " target "
<< this->GetTargetName()
<< "\n\n";
cmNinjaDeps emptyDeps;
cmNinjaVars vars;
// Compute the comment.
2015-04-27 22:25:09 +02:00
std::ostringstream comment;
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
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();
std::string createRule =
genTarget.GetCreateRuleVariable(this->TargetLinkLanguage,
this->GetConfigName());
bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
vars["TARGET_FILE"] =
localGen.ConvertToOutputFormat(targetOutputReal, cmLocalGenerator::SHELL);
localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
vars["FLAGS"],
vars["LINK_FLAGS"],
frameworkPath,
linkPath,
&genTarget,
useWatcomQuote);
2015-11-17 17:22:37 +01:00
if(this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS")
2016-03-13 13:35:51 +01:00
&& gt.GetType() == cmState::SHARED_LIBRARY)
2015-11-17 17:22:37 +01:00
{
2016-03-13 13:35:51 +01:00
if(gt.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
2015-11-17 17:22:37 +01:00
{
std::string name_of_def_file
2016-03-13 13:35:51 +01:00
= gt.GetSupportDirectory();
name_of_def_file += "/" + gt.GetName();
2015-11-17 17:22:37 +01:00
name_of_def_file += ".def ";
vars["LINK_FLAGS"] += " /DEF:";
vars["LINK_FLAGS"] += this->GetLocalGenerator()
->ConvertToOutputFormat(name_of_def_file.c_str(),
cmLocalGenerator::SHELL);
}
}
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
2012-04-19 19:04:21 +03:00
this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
2013-03-16 19:13:01 +02:00
vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
::EncodeLiteral(vars["LINK_FLAGS"]);
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;
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.
2016-03-13 13:35:51 +01:00
if (targetType == cmState::EXECUTABLE)
2015-04-27 22:25:09 +02:00
{
std::string t = vars["FLAGS"];
localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
vars["FLAGS"] = t;
}
else
{
std::string t = vars["ARCH_FLAGS"];
localGen.AddArchitectureFlags(t, &genTarget, TargetLinkLanguage, cfgName);
vars["ARCH_FLAGS"] = t;
t = "";
localGen.AddLanguageFlags(t, TargetLinkLanguage, cfgName);
vars["LANGUAGE_COMPILE_FLAGS"] = t;
}
2015-11-17 17:22:37 +01: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;
2016-03-13 13:35:51 +01:00
if (targetType == cmState::SHARED_LIBRARY)
2015-04-27 22:25:09 +02:00
{
2015-11-17 17:22:37 +01:00
std::string install_dir =
this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(cfgName);
2015-04-27 22:25:09 +02:00
if (!install_dir.empty())
{
vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
cmLocalGenerator::NONE,
2015-08-17 11:37:30 +02:00
cmLocalGenerator::SHELL);
2015-04-27 22:25:09 +02:00
}
2012-06-27 20:52:58 +03:00
}
2012-04-19 19:04:21 +03:00
}
2015-11-17 17:22:37 +01:00
cmNinjaDeps byproducts;
2015-04-27 22:25:09 +02:00
if (!this->TargetNameImport.empty())
{
const std::string impLibPath = localGen.ConvertToOutputFormat(
targetOutputImplib,
cmLocalGenerator::SHELL);
2013-03-16 19:13:01 +02:00
vars["TARGET_IMPLIB"] = impLibPath;
EnsureParentDirectoryExists(impLibPath);
2016-03-13 13:35:51 +01:00
if(genTarget.HasImportLibrary())
2015-04-27 22:25:09 +02:00
{
2015-11-17 17:22:37 +01:00
byproducts.push_back(targetOutputImplib);
2015-04-27 22:25:09 +02:00
}
}
2012-04-19 19:04:21 +03:00
2013-03-16 19:13:01 +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?
if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
2015-04-27 22:25:09 +02:00
{
2012-08-04 10:26:08 +03:00
dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
2015-04-27 22:25:09 +02:00
}
2012-08-04 10:26:08 +03: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();
2015-11-17 17:22:37 +01:00
vars["OBJECT_DIR"] =
this->GetLocalGenerator()->ConvertToOutputFormat(
this->ConvertToNinjaPath(objPath), cmLocalGenerator::SHELL);
EnsureDirectoryExists(objPath);
2015-08-17 11:37:30 +02:00
if (this->GetGlobalGenerator()->IsGCCOnWindows())
2012-06-27 20:52:58 +03:00
{
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(), '\\', '/');
2012-06-27 20:52:58 +03:00
}
2012-04-19 19:04:21 +03:00
2014-08-03 19:52:23 +02:00
const std::vector<cmCustomCommand> *cmdLists[3] = {
2016-03-13 13:35:51 +01:00
&gt.GetPreBuildCommands(),
&gt.GetPreLinkCommands(),
&gt.GetPostBuildCommands()
2012-04-19 19:04:21 +03:00
};
std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
std::vector<std::string> *cmdLineLists[3] = {
&preLinkCmdLines,
&preLinkCmdLines,
&postBuildCmdLines
};
2015-04-27 22:25:09 +02:00
for (unsigned i = 0; i != 3; ++i)
{
2012-04-19 19:04:21 +03:00
for (std::vector<cmCustomCommand>::const_iterator
ci = cmdLists[i]->begin();
2015-04-27 22:25:09 +02:00
ci != cmdLists[i]->end(); ++ci)
{
2015-11-17 17:22:37 +01:00
cmCustomCommandGenerator ccg(*ci, 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
}
2015-11-17 17:22:37 +01:00
// maybe create .def file from list of objects
2016-03-13 13:35:51 +01:00
if (gt.GetType() == cmState::SHARED_LIBRARY &&
2015-11-17 17:22:37 +01:00
this->GetMakefile()->IsOn("CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS"))
{
2016-03-13 13:35:51 +01:00
if(gt.GetPropertyAsBool("WINDOWS_EXPORT_ALL_SYMBOLS"))
2015-11-17 17:22:37 +01:00
{
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
cmSystemTools::GetCMakeCommand(), cmLocalGenerator::SHELL);
std::string name_of_def_file
2016-03-13 13:35:51 +01:00
= gt.GetSupportDirectory();
name_of_def_file += "/" + gt.GetName();
2015-11-17 17:22:37 +01:00
name_of_def_file += ".def";
std::string cmd = cmakeCommand;
cmd += " -E __create_def ";
cmd += this->GetLocalGenerator()
->ConvertToOutputFormat(name_of_def_file.c_str(),
cmLocalGenerator::SHELL);
cmd += " ";
cmNinjaDeps objs = this->GetObjects();
std::string obj_list_file = name_of_def_file;
obj_list_file += ".objs";
cmd += this->GetLocalGenerator()
->ConvertToOutputFormat(obj_list_file.c_str(),
cmLocalGenerator::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());
for(cmNinjaDeps::iterator i=objs.begin(); i != objs.end(); ++i)
{
if(cmHasLiteralSuffix(*i, ".obj"))
{
fout << *i << "\n";
}
}
}
}
2012-04-19 19:04:21 +03:00
// If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
// the link commands.
2015-04-27 22:25:09 +02:00
if (!preLinkCmdLines.empty())
{
const std::string homeOutDir = localGen.ConvertToOutputFormat(
2016-03-13 13:35:51 +01:00
localGen.GetBinaryDirectory(),
cmLocalGenerator::SHELL);
2013-03-16 19:13:01 +02:00
preLinkCmdLines.push_back("cd " + homeOutDir);
2015-04-27 22:25:09 +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;
2015-04-27 22:25:09 +02:00
if (targetOutput == targetOutputReal)
{
2012-04-19 19:04:21 +03:00
vars["POST_BUILD"] = postBuildCmdLine;
2015-04-27 22:25:09 +02:00
}
else
{
2012-04-19 19:04:21 +03:00
vars["POST_BUILD"] = ":";
symlinkVars["POST_BUILD"] = postBuildCmdLine;
2015-04-27 22:25:09 +02:00
}
2012-04-19 19:04:21 +03:00
2015-04-27 22:25:09 +02:00
cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
2013-03-16 19:13:01 +02:00
int commandLineLengthLimit = 1;
const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
2015-04-27 22:25:09 +02:00
if (!mf->IsDefinitionSet(forceRspFile) &&
cmSystemTools::GetEnv(forceRspFile) == 0)
{
commandLineLengthLimit = calculateCommandLineLengthLimit(
globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
}
2013-03-16 19:13:01 +02:00
2015-04-27 22:25:09 +02:00
const std::string rspfile =
std::string(cmake::GetCMakeFilesDirectoryPostSlash())
2016-03-13 13:35:51 +01:00
+ 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(),
2015-04-27 22:25:09 +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.
vars["RESTAT"] = byproducts.empty()? "" : "1";
for (cmNinjaDeps::const_iterator oi = byproducts.begin(),
oe = byproducts.end();
oi != oe; ++oi)
{
this->GetGlobalGenerator()->SeenCustomCommandOutput(*oi);
outputs.push_back(*oi);
}
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;
2015-04-27 22:25:09 +02:00
globalGen.WriteBuild(this->GetBuildFileStream(),
comment.str(),
this->LanguageLinkerRule(),
outputs,
explicitDeps,
implicitDeps,
orderOnlyDeps,
vars,
rspfile,
2015-08-17 11:37:30 +02:00
commandLineLengthLimit,
&usedResponseFile);
this->WriteLinkRule(usedResponseFile);
2015-04-27 22:25:09 +02:00
2016-03-13 13:35:51 +01:00
if (targetOutput != targetOutputReal && !gt.IsFrameworkOnApple())
2015-04-27 22:25:09 +02:00
{
2016-03-13 13:35:51 +01:00
if (targetType == cmState::EXECUTABLE)
2015-04-27 22:25:09 +02:00
{
globalGen.WriteBuild(this->GetBuildFileStream(),
"Create executable symlink " + targetOutput,
"CMAKE_SYMLINK_EXECUTABLE",
cmNinjaDeps(1, targetOutput),
cmNinjaDeps(1, targetOutputReal),
emptyDeps,
emptyDeps,
symlinkVars);
}
else
{
2012-08-04 10:26:08 +03:00
cmNinjaDeps symlinks;
const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
// If one link has to be created.
2015-04-27 22:25:09 +02:00
if (targetOutputReal == soName || targetOutput == soName)
{
2012-08-04 10:26:08 +03:00
symlinkVars["SONAME"] = soName;
2015-04-27 22:25:09 +02:00
}
else
{
2012-08-04 10:26:08 +03:00
symlinkVars["SONAME"] = "";
symlinks.push_back(soName);
2015-04-27 22:25:09 +02:00
}
2012-08-04 10:26:08 +03:00
symlinks.push_back(targetOutput);
2015-04-27 22:25:09 +02:00
globalGen.WriteBuild(this->GetBuildFileStream(),
2013-11-03 12:27:13 +02:00
"Create library symlink " + targetOutput,
"CMAKE_SYMLINK_LIBRARY",
symlinks,
cmNinjaDeps(1, targetOutputReal),
emptyDeps,
emptyDeps,
symlinkVars);
2015-04-27 22:25:09 +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();
2013-11-03 12:27:13 +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
}