CMake/Source/cmNinjaNormalTargetGenerato...

687 lines
24 KiB
C++
Raw Normal View History

2011-11-11 09:00:49 +04: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"
#include "cmOSXBundleGenerator.h"
#include "cmGeneratorTarget.h"
#include "cmCustomCommandGenerator.h"
2011-11-11 09:00:49 +04:00
#include <assert.h>
#include <algorithm>
2011-11-11 09:00:49 +04:00
#ifndef _WIN32
#include <unistd.h>
#endif
2011-11-11 09:00:49 +04:00
cmNinjaNormalTargetGenerator::
cmNinjaNormalTargetGenerator(cmGeneratorTarget* target)
: cmNinjaTargetGenerator(target->Target)
2011-11-11 09:00:49 +04:00
, TargetNameOut()
, TargetNameSO()
, TargetNameReal()
, TargetNameImport()
, TargetNamePDB()
, TargetLinkLanguage("")
2011-11-11 09:00:49 +04:00
{
this->TargetLinkLanguage = target->Target
->GetLinkerLanguage(this->GetConfigName());
2011-11-11 09:00:49 +04:00
if (target->GetType() == cmTarget::EXECUTABLE)
target->Target->GetExecutableNames(this->TargetNameOut,
2011-11-11 09:00:49 +04:00
this->TargetNameReal,
this->TargetNameImport,
this->TargetNamePDB,
GetLocalGenerator()->GetConfigName());
else
target->Target->GetLibraryNames(this->TargetNameOut,
2011-11-11 09:00:49 +04:00
this->TargetNameSO,
this->TargetNameReal,
this->TargetNameImport,
this->TargetNamePDB,
GetLocalGenerator()->GetConfigName());
if(target->GetType() != cmTarget::OBJECT_LIBRARY)
{
// on Windows the output dir is already needed at compile time
// ensure the directory exists (OutDir test)
EnsureDirectoryExists(target->Target->GetDirectory(this->GetConfigName()));
}
this->OSXBundleGenerator = new cmOSXBundleGenerator(target,
this->GetConfigName());
this->OSXBundleGenerator->SetMacContentFolders(&this->MacContentFolders);
2011-11-11 09:00:49 +04:00
}
cmNinjaNormalTargetGenerator::~cmNinjaNormalTargetGenerator()
{
delete this->OSXBundleGenerator;
2011-11-11 09:00:49 +04:00
}
void cmNinjaNormalTargetGenerator::Generate()
{
if (this->TargetLinkLanguage.empty()) {
cmSystemTools::Error("CMake can not determine linker language for "
"target: ",
2014-02-07 02:31:47 +04:00
this->GetTarget()->GetName().c_str());
2011-11-11 09:00:49 +04:00
return;
}
// Write the rules for each language.
this->WriteLanguagesRules();
// Write the build statements
this->WriteObjectBuildStatements();
if(this->GetTarget()->GetType() == cmTarget::OBJECT_LIBRARY)
{
this->WriteObjectLibStatement();
}
else
{
this->WriteLinkRule(false); // write rule without rspfile support
this->WriteLinkRule(true); // write rule with rspfile support
this->WriteLinkStatement();
}
2011-11-11 09:00:49 +04:00
}
void cmNinjaNormalTargetGenerator::WriteLanguagesRules()
{
#ifdef NINJA_GEN_VERBOSE_FILES
2011-11-11 09:00:49 +04:00
cmGlobalNinjaGenerator::WriteDivider(this->GetRulesFileStream());
this->GetRulesFileStream()
<< "# Rules for each languages for "
<< cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
<< " target "
<< this->GetTargetName()
<< "\n\n";
#endif
2011-11-11 09:00:49 +04:00
std::set<std::string> languages;
this->GetTarget()->GetLanguages(languages,
this->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE"));
for(std::set<std::string>::const_iterator l = languages.begin();
2011-11-11 09:00:49 +04:00
l != languages.end();
++l)
this->WriteLanguageRules(*l);
}
const char *cmNinjaNormalTargetGenerator::GetVisibleTypeName() const
{
switch (this->GetTarget()->GetType()) {
case cmTarget::STATIC_LIBRARY:
return "static library";
case cmTarget::SHARED_LIBRARY:
return "shared library";
case cmTarget::MODULE_LIBRARY:
if (this->GetTarget()->IsCFBundleOnApple())
return "CFBundle shared module";
else
return "shared module";
2011-11-11 09:00:49 +04:00
case cmTarget::EXECUTABLE:
return "executable";
default:
return 0;
}
}
std::string
cmNinjaNormalTargetGenerator
::LanguageLinkerRule() const
{
return this->TargetLinkLanguage
2011-11-11 09:00:49 +04:00
+ "_"
+ cmTarget::GetTargetTypeName(this->GetTarget()->GetType())
+ "_LINKER";
}
void
cmNinjaNormalTargetGenerator
::WriteLinkRule(bool useResponseFile)
2011-11-11 09:00:49 +04:00
{
cmTarget::TargetType targetType = this->GetTarget()->GetType();
std::string ruleName = this->LanguageLinkerRule();
2012-06-06 01:40:42 +04:00
if (useResponseFile)
ruleName += "_RSP_FILE";
2011-11-11 09:00:49 +04:00
// Select whether to use a response file for objects.
std::string rspfile;
std::string rspcontent;
2011-11-11 09:00:49 +04:00
if (!this->GetGlobalGenerator()->HasRule(ruleName)) {
cmLocalGenerator::RuleVariables vars;
vars.RuleLauncher = "RULE_LAUNCH_LINK";
vars.CMTarget = this->GetTarget();
vars.Language = this->TargetLinkLanguage.c_str();
std::string responseFlag;
if (!useResponseFile) {
vars.Objects = "$in";
vars.LinkLibraries = "$LINK_PATH $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
rspcontent = "$in_newline $LINK_PATH $LINK_LIBRARIES";
vars.Objects = responseFlag.c_str();
vars.LinkLibraries = "";
}
2012-06-12 06:17:55 +04:00
vars.ObjectDir = "$OBJECT_DIR";
vars.Target = "$TARGET_FILE";
Support building shared libraries or modules without soname (#13155) Add a boolean target property NO_SONAME which may be used to disable soname for the specified shared library or module even if the platform supports it. This property should be useful for private shared libraries or various plugins which live in private directories and have not been designed to be found or loaded globally. Replace references to <CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG> and hard-coded -install_name flags with a conditional <SONAME_FLAG> which is expanded to the value of the CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG definition as long as soname supports is enabled for the target in question. Keep expanding CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG in rules in case third party projects still use it. Such projects would not yet use NO_SONAME so the adjacent <TARGET_SONAME> will always be expanded. Make <TARGET_INSTALLNAME_DIR> NO_SONAME aware as well. Since -install_name is soname on OS X, this should not be a problem if this variable is expanded only if soname is enabled. The Ninja generator performs rule variable substitution only once globally per rule to put its own placeholders. Final substitution is performed by ninja at build time. Therefore we cannot conditionally replace the soname placeholders on a per-target basis. Rather than omitting $SONAME from rules.ninja, simply do not write its contents for targets which have NO_SONAME. Since 3 variables are affected by NO_SONAME ($SONAME, $SONAME_FLAG, $INSTALLNAME_DIR), set them only if soname is enabled.
2012-04-22 17:42:55 +04:00
vars.SONameFlag = "$SONAME_FLAG";
2011-11-11 09:00:49 +04:00
vars.TargetSOName = "$SONAME";
vars.TargetInstallNameDir = "$INSTALLNAME_DIR";
vars.TargetPDB = "$TARGET_PDB";
2011-11-11 09:00:49 +04:00
// Setup the target version.
std::string targetVersionMajor;
std::string targetVersionMinor;
{
cmOStringStream majorStream;
cmOStringStream minorStream;
int major;
int minor;
this->GetTarget()->GetTargetVersion(major, minor);
majorStream << major;
minorStream << minor;
targetVersionMajor = majorStream.str();
targetVersionMinor = minorStream.str();
}
vars.TargetVersionMajor = targetVersionMajor.c_str();
vars.TargetVersionMinor = targetVersionMinor.c_str();
vars.Flags = "$FLAGS";
2011-11-11 09:00:49 +04:00
vars.LinkFlags = "$LINK_FLAGS";
std::string langFlags;
if (targetType != cmTarget::EXECUTABLE)
{
langFlags += "$LANGUAGE_COMPILE_FLAGS $ARCH_FLAGS";
vars.LanguageCompileFlags = langFlags.c_str();
}
2011-11-11 09:00:49 +04:00
// Rule for linking library/executable.
2011-11-11 09:00:49 +04: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);
// Write the linker rule with response file if needed.
cmOStringStream comment;
2011-11-11 09:00:49 +04:00
comment << "Rule for linking " << this->TargetLinkLanguage << " "
<< this->GetVisibleTypeName() << ".";
cmOStringStream description;
2011-11-11 09:00:49 +04:00
description << "Linking " << this->TargetLinkLanguage << " "
<< this->GetVisibleTypeName() << " $TARGET_FILE";
2011-11-11 09:00:49 +04:00
this->GetGlobalGenerator()->AddRule(ruleName,
linkCmd,
description.str(),
comment.str(),
/*depfile*/ "",
/*deptype*/ "",
rspfile,
rspcontent,
/*restat*/ "",
/*generator*/ false);
2011-11-11 09:00:49 +04:00
}
if (this->TargetNameOut != this->TargetNameReal &&
!this->GetTarget()->IsFrameworkOnApple()) {
2011-11-11 09:00:49 +04:00
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"),
cmLocalGenerator::SHELL);
2011-11-11 09:00:49 +04:00
if (targetType == cmTarget::EXECUTABLE)
this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_EXECUTABLE",
cmakeCommand +
" -E cmake_symlink_executable"
" $in $out && $POST_BUILD",
"Creating executable symlink $out",
"Rule for creating "
"executable symlink.",
/*depfile*/ "",
/*deptype*/ "",
/*rspfile*/ "",
/*rspcontent*/ "",
/*restat*/ "",
/*generator*/ false);
2011-11-11 09:00:49 +04:00
else
this->GetGlobalGenerator()->AddRule("CMAKE_SYMLINK_LIBRARY",
cmakeCommand +
" -E cmake_symlink_library"
" $in $SONAME $out && $POST_BUILD",
"Creating library symlink $out",
"Rule for creating "
"library symlink.",
/*depfile*/ "",
/*deptype*/ "",
/*rspfile*/ "",
/*rspcontent*/ "",
/*restat*/ "",
/*generator*/ false);
2011-11-11 09:00:49 +04:00
}
}
std::vector<std::string>
cmNinjaNormalTargetGenerator
::ComputeLinkCmd()
{
std::vector<std::string> linkCmds;
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;
}
}
switch (this->GetTarget()->GetType()) {
2011-11-11 09:00:49 +04:00
case cmTarget::STATIC_LIBRARY: {
// We have archive link commands set. First, delete the existing archive.
{
2011-11-11 09:00:49 +04:00
std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat(
mf->GetRequiredDefinition("CMAKE_COMMAND"),
cmLocalGenerator::SHELL);
linkCmds.push_back(cmakeCommand + " -E remove $TARGET_FILE");
}
2011-11-11 09:00:49 +04:00
// TODO: Use ARCHIVE_APPEND for archives over a certain size.
{
std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_ARCHIVE_CREATE";
const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
2011-11-11 09:00:49 +04:00
}
{
std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_ARCHIVE_FINISH";
const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
2011-11-11 09:00:49 +04:00
}
return linkCmds;
}
case cmTarget::SHARED_LIBRARY:
case cmTarget::MODULE_LIBRARY:
case cmTarget::EXECUTABLE:
break;
2011-11-11 09:00:49 +04:00
default:
assert(0 && "Unexpected target type");
}
2012-02-05 05:48:08 +04:00
return std::vector<std::string>();
2011-11-11 09:00:49 +04:00
}
static int calculateCommandLineLengthLimit(int linkRuleLength)
2011-11-11 09:00:49 +04:00
{
#ifdef _WIN32
return 8000 - linkRuleLength;
#elif defined(__linux) || defined(__APPLE__) || defined(__HAIKU__)
// for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
return ((int)sysconf(_SC_ARG_MAX)) - linkRuleLength - 1000;
#else
(void)linkRuleLength;
return -1;
#endif
}
2011-11-11 09:00:49 +04:00
void cmNinjaNormalTargetGenerator::WriteLinkStatement()
{
cmTarget& target = *this->GetTarget();
const std::string cfgName = this->GetConfigName();
std::string targetOutput = ConvertToNinjaPath(
target.GetFullPath(cfgName));
std::string targetOutputReal = ConvertToNinjaPath(
target.GetFullPath(cfgName,
/*implib=*/false,
/*realpath=*/true));
std::string targetOutputImplib = ConvertToNinjaPath(
target.GetFullPath(cfgName,
/*implib=*/true));
if (target.IsAppBundleOnApple())
{
// Create the app bundle
std::string outpath = target.GetDirectory(cfgName);
this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
// Calculate the output path
targetOutput = outpath;
targetOutput += "/";
targetOutput += this->TargetNameOut;
targetOutput = this->ConvertToNinjaPath(targetOutput);
targetOutputReal = outpath;
targetOutputReal += "/";
targetOutputReal += this->TargetNameReal;
targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
}
else if (target.IsFrameworkOnApple())
{
// Create the library framework.
this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
target.GetDirectory(cfgName));
}
else if(target.IsCFBundleOnApple())
{
2012-07-16 18:00:40 +04:00
// Create the core foundation bundle.
this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
target.GetDirectory(cfgName));
}
2011-11-11 09:00:49 +04:00
// Write comments.
cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
const cmTarget::TargetType targetType = target.GetType();
2011-11-11 09:00:49 +04:00
this->GetBuildFileStream()
<< "# Link build statements for "
<< cmTarget::GetTargetTypeName(targetType)
<< " target "
<< this->GetTargetName()
<< "\n\n";
cmNinjaDeps emptyDeps;
cmNinjaVars vars;
// Compute the comment.
cmOStringStream comment;
comment <<
"Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
2011-11-11 09:00:49 +04:00
// Compute outputs.
cmNinjaDeps outputs;
outputs.push_back(targetOutputReal);
// Compute specific libraries to link with.
cmNinjaDeps explicitDeps = this->GetObjects();
cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
2011-11-11 09:00:49 +04:00
cmMakefile* mf = this->GetMakefile();
std::string frameworkPath;
std::string linkPath;
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();
vars["TARGET_FILE"] =
localGen.ConvertToOutputFormat(targetOutputReal, cmLocalGenerator::SHELL);
localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
vars["FLAGS"],
vars["LINK_FLAGS"],
frameworkPath,
linkPath,
&genTarget,
useWatcomQuote);
2011-11-11 09:00:49 +04:00
this->addPoolNinjaVariable("JOB_POOL_LINK", &target, vars);
2012-03-10 15:19:18 +04:00
this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
::EncodeLiteral(vars["LINK_FLAGS"]);
vars["LINK_PATH"] = frameworkPath + linkPath;
2012-03-10 15:19:18 +04:00
2011-11-11 09:00:49 +04: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.
if (targetType == cmTarget::EXECUTABLE)
{
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;
}
if (target.HasSOName(cfgName))
{
vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
Support building shared libraries or modules without soname (#13155) Add a boolean target property NO_SONAME which may be used to disable soname for the specified shared library or module even if the platform supports it. This property should be useful for private shared libraries or various plugins which live in private directories and have not been designed to be found or loaded globally. Replace references to <CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG> and hard-coded -install_name flags with a conditional <SONAME_FLAG> which is expanded to the value of the CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG definition as long as soname supports is enabled for the target in question. Keep expanding CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG in rules in case third party projects still use it. Such projects would not yet use NO_SONAME so the adjacent <TARGET_SONAME> will always be expanded. Make <TARGET_INSTALLNAME_DIR> NO_SONAME aware as well. Since -install_name is soname on OS X, this should not be a problem if this variable is expanded only if soname is enabled. The Ninja generator performs rule variable substitution only once globally per rule to put its own placeholders. Final substitution is performed by ninja at build time. Therefore we cannot conditionally replace the soname placeholders on a per-target basis. Rather than omitting $SONAME from rules.ninja, simply do not write its contents for targets which have NO_SONAME. Since 3 variables are affected by NO_SONAME ($SONAME, $SONAME_FLAG, $INSTALLNAME_DIR), set them only if soname is enabled.
2012-04-22 17:42:55 +04:00
vars["SONAME"] = this->TargetNameSO;
if (targetType == cmTarget::SHARED_LIBRARY)
{
std::string install_dir = target.GetInstallNameDirForBuildTree(cfgName);
if (!install_dir.empty())
{
vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
cmLocalGenerator::NONE,
cmLocalGenerator::SHELL,
false);
}
Support building shared libraries or modules without soname (#13155) Add a boolean target property NO_SONAME which may be used to disable soname for the specified shared library or module even if the platform supports it. This property should be useful for private shared libraries or various plugins which live in private directories and have not been designed to be found or loaded globally. Replace references to <CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG> and hard-coded -install_name flags with a conditional <SONAME_FLAG> which is expanded to the value of the CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG definition as long as soname supports is enabled for the target in question. Keep expanding CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG in rules in case third party projects still use it. Such projects would not yet use NO_SONAME so the adjacent <TARGET_SONAME> will always be expanded. Make <TARGET_INSTALLNAME_DIR> NO_SONAME aware as well. Since -install_name is soname on OS X, this should not be a problem if this variable is expanded only if soname is enabled. The Ninja generator performs rule variable substitution only once globally per rule to put its own placeholders. Final substitution is performed by ninja at build time. Therefore we cannot conditionally replace the soname placeholders on a per-target basis. Rather than omitting $SONAME from rules.ninja, simply do not write its contents for targets which have NO_SONAME. Since 3 variables are affected by NO_SONAME ($SONAME, $SONAME_FLAG, $INSTALLNAME_DIR), set them only if soname is enabled.
2012-04-22 17:42:55 +04:00
}
2011-11-11 09:00:49 +04:00
}
if (!this->TargetNameImport.empty())
{
const std::string impLibPath = localGen.ConvertToOutputFormat(
targetOutputImplib,
cmLocalGenerator::SHELL);
2012-08-22 14:26:56 +04:00
vars["TARGET_IMPLIB"] = impLibPath;
EnsureParentDirectoryExists(impLibPath);
if(target.HasImportLibrary())
{
outputs.push_back(targetOutputImplib);
}
}
if (!this->SetMsvcTargetPdbVariable(vars))
2012-07-16 16:16:43 +04: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;
target.GetFullNameComponents(prefix, base, suffix);
std::string dbg_suffix = ".dbg";
// TODO: Where to document?
if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
{
dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
}
vars["TARGET_PDB"] = base + suffix + dbg_suffix;
2012-07-16 16:16:43 +04:00
}
2012-06-12 06:17:55 +04:00
if (mf->IsOn("CMAKE_COMPILER_IS_MINGW"))
{
2012-08-22 14:26:56 +04:00
const std::string objPath = GetTarget()->GetSupportDirectory();
vars["OBJECT_DIR"] = ConvertToNinjaPath(objPath);
2012-08-22 14:26:56 +04:00
EnsureDirectoryExists(objPath);
2012-11-07 20:13:09 +04:00
// 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(), '\\', '/');
2012-06-12 06:17:55 +04:00
}
const std::vector<cmCustomCommand> *cmdLists[3] = {
&target.GetPreBuildCommands(),
&target.GetPreLinkCommands(),
&target.GetPostBuildCommands()
2011-11-11 09:00:49 +04:00
};
std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
std::vector<std::string> *cmdLineLists[3] = {
&preLinkCmdLines,
&preLinkCmdLines,
&postBuildCmdLines
};
for (unsigned i = 0; i != 3; ++i)
{
2011-11-11 09:00:49 +04:00
for (std::vector<cmCustomCommand>::const_iterator
ci = cmdLists[i]->begin();
ci != cmdLists[i]->end(); ++ci)
{
cmCustomCommandGenerator ccg(*ci, cfgName, mf);
localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
}
2011-11-11 09:00:49 +04:00
}
// If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
// the link commands.
if (!preLinkCmdLines.empty())
{
const std::string homeOutDir = localGen.ConvertToOutputFormat(
mf->GetHomeOutputDirectory(),
cmLocalGenerator::SHELL);
2012-08-22 14:26:56 +04:00
preLinkCmdLines.push_back("cd " + homeOutDir);
}
2011-11-11 09:00:49 +04:00
vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
2011-11-11 09:00:49 +04:00
cmNinjaVars symlinkVars;
if (targetOutput == targetOutputReal)
{
2011-11-11 09:00:49 +04:00
vars["POST_BUILD"] = postBuildCmdLine;
}
else
{
2011-11-11 09:00:49 +04:00
vars["POST_BUILD"] = ":";
symlinkVars["POST_BUILD"] = postBuildCmdLine;
}
2011-11-11 09:00:49 +04:00
cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
int commandLineLengthLimit = 1;
const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
if (!mf->IsDefinitionSet(forceRspFile) &&
cmSystemTools::GetEnv(forceRspFile) == 0)
{
commandLineLengthLimit = calculateCommandLineLengthLimit(
globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
}
const std::string rspfile =
std::string(cmake::GetCMakeFilesDirectoryPostSlash())
+ target.GetName() + ".rsp";
// Gather order-only dependencies.
cmNinjaDeps orderOnlyDeps;
this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(),
orderOnlyDeps);
2011-11-11 09:00:49 +04:00
// Write the build statement for this target.
globalGen.WriteBuild(this->GetBuildFileStream(),
comment.str(),
this->LanguageLinkerRule(),
outputs,
explicitDeps,
implicitDeps,
orderOnlyDeps,
vars,
rspfile,
commandLineLengthLimit);
if (targetOutput != targetOutputReal && !target.IsFrameworkOnApple())
{
if (targetType == cmTarget::EXECUTABLE)
{
globalGen.WriteBuild(this->GetBuildFileStream(),
"Create executable symlink " + targetOutput,
"CMAKE_SYMLINK_EXECUTABLE",
cmNinjaDeps(1, targetOutput),
cmNinjaDeps(1, targetOutputReal),
emptyDeps,
emptyDeps,
symlinkVars);
}
else
{
2012-07-06 12:16:45 +04:00
cmNinjaDeps symlinks;
const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
// If one link has to be created.
if (targetOutputReal == soName || targetOutput == soName)
{
symlinkVars["SONAME"] = soName;
}
else
{
symlinkVars["SONAME"] = "";
2012-07-06 12:16:45 +04:00
symlinks.push_back(soName);
}
2012-07-06 12:16:45 +04:00
symlinks.push_back(targetOutput);
globalGen.WriteBuild(this->GetBuildFileStream(),
"Create library symlink " + targetOutput,
"CMAKE_SYMLINK_LIBRARY",
symlinks,
cmNinjaDeps(1, targetOutputReal),
emptyDeps,
emptyDeps,
symlinkVars);
}
2011-11-11 09:00:49 +04:00
}
2011-11-11 09:00:49 +04:00
// Add aliases for the file name and the target name.
globalGen.AddTargetAlias(this->TargetNameOut, &target);
globalGen.AddTargetAlias(this->GetTargetName(), &target);
2011-11-11 09:00:49 +04:00
}
//----------------------------------------------------------------------------
void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()
{
// Write a phony output that depends on all object files.
cmNinjaDeps outputs;
this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
cmNinjaDeps depends = this->GetObjects();
this->GetGlobalGenerator()->WritePhonyBuild(this->GetBuildFileStream(),
"Object library "
+ this->GetTargetName(),
outputs,
depends);
// Add aliases for the target name.
this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
this->GetTarget());
}