Ninja: Simplify code for linker commands

Use GetCreateRuleVariable function instead of redundant code for Rule
variable name.  Use temporary variables to improve code.
This commit is contained in:
Jiri Malak 2014-03-31 00:25:11 +02:00 committed by Brad King
parent 521b930bf4
commit 2b89675256
1 changed files with 29 additions and 51 deletions

View File

@ -313,75 +313,52 @@ cmNinjaNormalTargetGenerator
::ComputeLinkCmd() ::ComputeLinkCmd()
{ {
std::vector<std::string> linkCmds; std::vector<std::string> linkCmds;
cmTarget::TargetType targetType = this->GetTarget()->GetType(); cmMakefile* mf = this->GetMakefile();
switch (targetType) {
case cmTarget::STATIC_LIBRARY: {
// Check if you have a non archive way to create the static library.
{ {
std::string linkCmdVar = "CMAKE_"; std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage; linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_CREATE_STATIC_LIBRARY"; linkCmdVar += this->GetGeneratorTarget()->GetCreateRuleVariable();
if (const char *linkCmd = const char *linkCmd = mf->GetDefinition(linkCmdVar);
this->GetMakefile()->GetDefinition(linkCmdVar)) if (linkCmd)
{ {
cmSystemTools::ExpandListArgument(linkCmd, linkCmds); cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
return linkCmds; return linkCmds;
} }
} }
switch (this->GetTarget()->GetType()) {
case cmTarget::STATIC_LIBRARY: {
// We have archive link commands set. First, delete the existing archive. // We have archive link commands set. First, delete the existing archive.
{
std::string cmakeCommand = std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat( this->GetLocalGenerator()->ConvertToOutputFormat(
this->GetMakefile()->GetRequiredDefinition("CMAKE_COMMAND"), mf->GetRequiredDefinition("CMAKE_COMMAND"),
cmLocalGenerator::SHELL); cmLocalGenerator::SHELL);
linkCmds.push_back(cmakeCommand + " -E remove $out"); linkCmds.push_back(cmakeCommand + " -E remove $out");
}
// TODO: Use ARCHIVE_APPEND for archives over a certain size. // TODO: Use ARCHIVE_APPEND for archives over a certain size.
{ {
std::string linkCmdVar = "CMAKE_"; std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage; linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_ARCHIVE_CREATE"; linkCmdVar += "_ARCHIVE_CREATE";
const char *linkCmd = const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
this->GetMakefile()->GetRequiredDefinition(linkCmdVar);
cmSystemTools::ExpandListArgument(linkCmd, linkCmds); cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
} }
{ {
std::string linkCmdVar = "CMAKE_"; std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage; linkCmdVar += this->TargetLinkLanguage;
linkCmdVar += "_ARCHIVE_FINISH"; linkCmdVar += "_ARCHIVE_FINISH";
const char *linkCmd = const char *linkCmd = mf->GetRequiredDefinition(linkCmdVar);
this->GetMakefile()->GetRequiredDefinition(linkCmdVar);
cmSystemTools::ExpandListArgument(linkCmd, linkCmds); cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
} }
return linkCmds; return linkCmds;
} }
case cmTarget::SHARED_LIBRARY: case cmTarget::SHARED_LIBRARY:
case cmTarget::MODULE_LIBRARY: case cmTarget::MODULE_LIBRARY:
case cmTarget::EXECUTABLE: {
std::string linkCmdVar = "CMAKE_";
linkCmdVar += this->TargetLinkLanguage;
switch (targetType) {
case cmTarget::SHARED_LIBRARY:
linkCmdVar += "_CREATE_SHARED_LIBRARY";
break;
case cmTarget::MODULE_LIBRARY:
linkCmdVar += "_CREATE_SHARED_MODULE";
break;
case cmTarget::EXECUTABLE: case cmTarget::EXECUTABLE:
linkCmdVar += "_LINK_EXECUTABLE";
break; break;
default: default:
assert(0 && "Unexpected target type"); assert(0 && "Unexpected target type");
} }
const char *linkCmd =
this->GetMakefile()->GetRequiredDefinition(linkCmdVar);
cmSystemTools::ExpandListArgument(linkCmd, linkCmds);
return linkCmds;
}
default:
assert(0 && "Unexpected target type");
}
return std::vector<std::string>(); return std::vector<std::string>();
} }
@ -456,14 +433,17 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
cmNinjaDeps explicitDeps = this->GetObjects(); cmNinjaDeps explicitDeps = this->GetObjects();
cmNinjaDeps implicitDeps = this->ComputeLinkDeps(); cmNinjaDeps implicitDeps = this->ComputeLinkDeps();
cmMakefile* mf = this->GetMakefile();
std::string frameworkPath; std::string frameworkPath;
std::string linkPath; std::string linkPath;
cmGeneratorTarget* gtarget = this->GetGeneratorTarget();
this->GetLocalGenerator()->GetTargetFlags(vars["LINK_LIBRARIES"], this->GetLocalGenerator()->GetTargetFlags(vars["LINK_LIBRARIES"],
vars["FLAGS"], vars["FLAGS"],
vars["LINK_FLAGS"], vars["LINK_FLAGS"],
frameworkPath, frameworkPath,
linkPath, linkPath,
this->GetGeneratorTarget()); gtarget);
this->addPoolNinjaVariable("JOB_POOL_LINK", this->GetTarget(), vars); this->addPoolNinjaVariable("JOB_POOL_LINK", this->GetTarget(), vars);
@ -480,7 +460,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
? vars["FLAGS"] ? vars["FLAGS"]
: vars["ARCH_FLAGS"]); : vars["ARCH_FLAGS"]);
this->GetLocalGenerator()->AddArchitectureFlags(flags, this->GetLocalGenerator()->AddArchitectureFlags(flags,
this->GetGeneratorTarget(), gtarget,
this->TargetLinkLanguage, this->TargetLinkLanguage,
this->GetConfigName()); this->GetConfigName());
if (targetType == cmTarget::EXECUTABLE) { if (targetType == cmTarget::EXECUTABLE) {
@ -490,7 +470,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
} }
if (this->GetTarget()->HasSOName(this->GetConfigName())) { if (this->GetTarget()->HasSOName(this->GetConfigName())) {
vars["SONAME_FLAG"] = vars["SONAME_FLAG"] =
this->GetMakefile()->GetSONameFlag(this->TargetLinkLanguage); mf->GetSONameFlag(this->TargetLinkLanguage);
vars["SONAME"] = this->TargetNameSO; vars["SONAME"] = this->TargetNameSO;
if (targetType == cmTarget::SHARED_LIBRARY) { if (targetType == cmTarget::SHARED_LIBRARY) {
std::string install_name_dir = this->GetTarget() std::string install_name_dir = this->GetTarget()
@ -513,7 +493,6 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
EnsureParentDirectoryExists(impLibPath); EnsureParentDirectoryExists(impLibPath);
} }
cmMakefile* mf = this->GetMakefile();
if (!this->SetMsvcTargetPdbVariable(vars)) if (!this->SetMsvcTargetPdbVariable(vars))
{ {
// It is common to place debug symbols at a specific place, // It is common to place debug symbols at a specific place,
@ -556,8 +535,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
for (std::vector<cmCustomCommand>::const_iterator for (std::vector<cmCustomCommand>::const_iterator
ci = cmdLists[i]->begin(); ci = cmdLists[i]->begin();
ci != cmdLists[i]->end(); ++ci) { ci != cmdLists[i]->end(); ++ci) {
cmCustomCommandGenerator ccg(*ci, this->GetConfigName(), cmCustomCommandGenerator ccg(*ci, this->GetConfigName(), mf);
this->GetMakefile());
this->GetLocalGenerator()->AppendCustomCommandLines(ccg, this->GetLocalGenerator()->AppendCustomCommandLines(ccg,
*cmdLineLists[i]); *cmdLineLists[i]);
} }
@ -567,7 +545,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
// the link commands. // the link commands.
if (!preLinkCmdLines.empty()) { if (!preLinkCmdLines.empty()) {
const std::string homeOutDir = this->GetLocalGenerator() const std::string homeOutDir = this->GetLocalGenerator()
->ConvertToOutputFormat(this->GetMakefile()->GetHomeOutputDirectory(), ->ConvertToOutputFormat(mf->GetHomeOutputDirectory(),
cmLocalGenerator::SHELL); cmLocalGenerator::SHELL);
preLinkCmdLines.push_back("cd " + homeOutDir); preLinkCmdLines.push_back("cd " + homeOutDir);
} }
@ -590,7 +568,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
int commandLineLengthLimit = 1; int commandLineLengthLimit = 1;
const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE"; const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
if (!this->GetMakefile()->IsDefinitionSet(forceRspFile) && if (!mf->IsDefinitionSet(forceRspFile) &&
cmSystemTools::GetEnv(forceRspFile) == 0) { cmSystemTools::GetEnv(forceRspFile) == 0) {
#ifdef _WIN32 #ifdef _WIN32
commandLineLengthLimit = 8000 - linkRuleLength; commandLineLengthLimit = 8000 - linkRuleLength;