Ninja: cmake formatting, make code more readable

This commit is contained in:
Peter Kümmel 2014-04-13 14:06:41 +02:00 committed by Brad King
parent b735c8cb43
commit 49fcffc6cc
1 changed files with 152 additions and 137 deletions

View File

@ -363,25 +363,39 @@ cmNinjaNormalTargetGenerator
return std::vector<std::string>(); return std::vector<std::string>();
} }
static int calculateCommandLineLengthLimit(int linkRuleLength)
{
#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
}
void cmNinjaNormalTargetGenerator::WriteLinkStatement() void cmNinjaNormalTargetGenerator::WriteLinkStatement()
{ {
cmTarget::TargetType targetType = this->GetTarget()->GetType(); cmTarget& target = *this->GetTarget();
const std::string cfgName = this->GetConfigName();
std::string targetOutput = ConvertToNinjaPath( std::string targetOutput = ConvertToNinjaPath(
this->GetTarget()->GetFullPath(this->GetConfigName()).c_str()); target.GetFullPath(cfgName).c_str());
std::string targetOutputReal = ConvertToNinjaPath( std::string targetOutputReal = ConvertToNinjaPath(
this->GetTarget()->GetFullPath(this->GetConfigName(), target.GetFullPath(cfgName,
/*implib=*/false, /*implib=*/false,
/*realpath=*/true).c_str()); /*realpath=*/true).c_str());
std::string targetOutputImplib = ConvertToNinjaPath( std::string targetOutputImplib = ConvertToNinjaPath(
this->GetTarget()->GetFullPath(this->GetConfigName(), target.GetFullPath(cfgName,
/*implib=*/true).c_str()); /*implib=*/true).c_str());
if (this->GetTarget()->IsAppBundleOnApple()) if (target.IsAppBundleOnApple())
{ {
// Create the app bundle // Create the app bundle
std::string outpath = std::string outpath = target.GetDirectory(cfgName);
this->GetTarget()->GetDirectory(this->GetConfigName());
this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath); this->OSXBundleGenerator->CreateAppBundle(this->TargetNameOut, outpath);
// Calculate the output path // Calculate the output path
@ -394,23 +408,22 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
targetOutputReal += this->TargetNameReal; targetOutputReal += this->TargetNameReal;
targetOutputReal = this->ConvertToNinjaPath(targetOutputReal.c_str()); targetOutputReal = this->ConvertToNinjaPath(targetOutputReal.c_str());
} }
else if (this->GetTarget()->IsFrameworkOnApple()) else if (target.IsFrameworkOnApple())
{ {
// Create the library framework. // Create the library framework.
std::string outpath = this->OSXBundleGenerator->CreateFramework(this->TargetNameOut,
this->GetTarget()->GetDirectory(this->GetConfigName()); target.GetDirectory(cfgName));
this->OSXBundleGenerator->CreateFramework(this->TargetNameOut, outpath);
} }
else if(this->GetTarget()->IsCFBundleOnApple()) else if(target.IsCFBundleOnApple())
{ {
// Create the core foundation bundle. // Create the core foundation bundle.
std::string outpath = this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut,
this->GetTarget()->GetDirectory(this->GetConfigName()); target.GetDirectory(cfgName));
this->OSXBundleGenerator->CreateCFBundle(this->TargetNameOut, outpath);
} }
// Write comments. // Write comments.
cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream()); cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
const cmTarget::TargetType targetType = target.GetType();
this->GetBuildFileStream() this->GetBuildFileStream()
<< "# Link build statements for " << "# Link build statements for "
<< cmTarget::GetTargetTypeName(targetType) << cmTarget::GetTargetTypeName(targetType)
@ -423,8 +436,8 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
// Compute the comment. // Compute the comment.
cmOStringStream comment; cmOStringStream comment;
comment << "Link the " << this->GetVisibleTypeName() << " " comment <<
<< targetOutputReal; "Link the " << this->GetVisibleTypeName() << " " << targetOutputReal;
// Compute outputs. // Compute outputs.
cmNinjaDeps outputs; cmNinjaDeps outputs;
@ -438,21 +451,21 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
std::string frameworkPath; std::string frameworkPath;
std::string linkPath; std::string linkPath;
cmGeneratorTarget* gtarget = this->GetGeneratorTarget(); cmGeneratorTarget& genTarget = *this->GetGeneratorTarget();
std::string createRule = "CMAKE_"; std::string createRule = "CMAKE_";
createRule += this->TargetLinkLanguage; createRule += this->TargetLinkLanguage + genTarget.GetCreateRuleVariable();
createRule += gtarget->GetCreateRuleVariable();
bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE"); bool useWatcomQuote = mf->IsOn(createRule+"_USE_WATCOM_QUOTE");
this->GetLocalGenerator()->GetTargetFlags(vars["LINK_LIBRARIES"], cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
localGen.GetTargetFlags(vars["LINK_LIBRARIES"],
vars["FLAGS"], vars["FLAGS"],
vars["LINK_FLAGS"], vars["LINK_FLAGS"],
frameworkPath, frameworkPath,
linkPath, linkPath,
gtarget, &genTarget,
useWatcomQuote); useWatcomQuote);
this->addPoolNinjaVariable("JOB_POOL_LINK", this->GetTarget(), vars); this->addPoolNinjaVariable("JOB_POOL_LINK", &target, vars);
this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]); this->AddModuleDefinitionFlag(vars["LINK_FLAGS"]);
vars["LINK_FLAGS"] = cmGlobalNinjaGenerator vars["LINK_FLAGS"] = cmGlobalNinjaGenerator
@ -466,35 +479,39 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
std::string flags = (targetType == cmTarget::EXECUTABLE std::string flags = (targetType == cmTarget::EXECUTABLE
? vars["FLAGS"] ? vars["FLAGS"]
: vars["ARCH_FLAGS"]); : vars["ARCH_FLAGS"]);
this->GetLocalGenerator()->AddArchitectureFlags(flags, localGen.AddArchitectureFlags(flags,
gtarget, &genTarget,
this->TargetLinkLanguage, this->TargetLinkLanguage,
this->GetConfigName()); cfgName);
if (targetType == cmTarget::EXECUTABLE) { if (targetType == cmTarget::EXECUTABLE)
{
vars["FLAGS"] = flags; vars["FLAGS"] = flags;
} else { }
else
{
vars["ARCH_FLAGS"] = flags; vars["ARCH_FLAGS"] = flags;
} }
if (this->GetTarget()->HasSOName(this->GetConfigName())) { if (target.HasSOName(cfgName))
vars["SONAME_FLAG"] = {
mf->GetSONameFlag(this->TargetLinkLanguage); vars["SONAME_FLAG"] = 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() {
->GetInstallNameDirForBuildTree(this->GetConfigName()); std::string install_dir = target.GetInstallNameDirForBuildTree(cfgName);
if (!install_dir.empty())
if (!install_name_dir.empty()) { {
vars["INSTALLNAME_DIR"] = vars["INSTALLNAME_DIR"] = localGen.Convert(install_dir,
this->GetLocalGenerator()->Convert(install_name_dir,
cmLocalGenerator::NONE, cmLocalGenerator::NONE,
cmLocalGenerator::SHELL, false); cmLocalGenerator::SHELL,
false);
} }
} }
} }
if (!this->TargetNameImport.empty()) { if (!this->TargetNameImport.empty())
const std::string impLibPath = this->GetLocalGenerator() {
->ConvertToOutputFormat(targetOutputImplib, const std::string impLibPath = localGen.ConvertToOutputFormat(
targetOutputImplib,
cmLocalGenerator::SHELL); cmLocalGenerator::SHELL);
vars["TARGET_IMPLIB"] = impLibPath; vars["TARGET_IMPLIB"] = impLibPath;
EnsureParentDirectoryExists(impLibPath); EnsureParentDirectoryExists(impLibPath);
@ -507,11 +524,13 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
std::string prefix; std::string prefix;
std::string base; std::string base;
std::string suffix; std::string suffix;
this->GetTarget()->GetFullNameComponents(prefix, base, suffix); target.GetFullNameComponents(prefix, base, suffix);
std::string dbg_suffix = ".dbg"; std::string dbg_suffix = ".dbg";
// TODO: Where to document? // TODO: Where to document?
if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX")) if (mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"))
{
dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX"); dbg_suffix = mf->GetDefinition("CMAKE_DEBUG_SYMBOL_SUFFIX");
}
vars["TARGET_PDB"] = base + suffix + dbg_suffix; vars["TARGET_PDB"] = base + suffix + dbg_suffix;
} }
@ -526,9 +545,9 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
} }
const std::vector<cmCustomCommand> *cmdLists[3] = { const std::vector<cmCustomCommand> *cmdLists[3] = {
&this->GetTarget()->GetPreBuildCommands(), &target.GetPreBuildCommands(),
&this->GetTarget()->GetPreLinkCommands(), &target.GetPreLinkCommands(),
&this->GetTarget()->GetPostBuildCommands() &target.GetPostBuildCommands()
}; };
std::vector<std::string> preLinkCmdLines, postBuildCmdLines; std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
@ -538,67 +557,58 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
&postBuildCmdLines &postBuildCmdLines
}; };
for (unsigned i = 0; i != 3; ++i) { for (unsigned i = 0; i != 3; ++i)
{
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(), mf); {
this->GetLocalGenerator()->AppendCustomCommandLines(ccg, cmCustomCommandGenerator ccg(*ci, cfgName, mf);
*cmdLineLists[i]); localGen.AppendCustomCommandLines(ccg, *cmdLineLists[i]);
} }
} }
// If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for // If we have any PRE_LINK commands, we need to go back to HOME_OUTPUT for
// the link commands. // the link commands.
if (!preLinkCmdLines.empty()) { if (!preLinkCmdLines.empty())
const std::string homeOutDir = this->GetLocalGenerator() {
->ConvertToOutputFormat(mf->GetHomeOutputDirectory(), const std::string homeOutDir = localGen.ConvertToOutputFormat(
mf->GetHomeOutputDirectory(),
cmLocalGenerator::SHELL); cmLocalGenerator::SHELL);
preLinkCmdLines.push_back("cd " + homeOutDir); preLinkCmdLines.push_back("cd " + homeOutDir);
} }
vars["PRE_LINK"] = vars["PRE_LINK"] = localGen.BuildCommandLine(preLinkCmdLines);
this->GetLocalGenerator()->BuildCommandLine(preLinkCmdLines); std::string postBuildCmdLine = localGen.BuildCommandLine(postBuildCmdLines);
std::string postBuildCmdLine =
this->GetLocalGenerator()->BuildCommandLine(postBuildCmdLines);
cmNinjaVars symlinkVars; cmNinjaVars symlinkVars;
if (targetOutput == targetOutputReal) { if (targetOutput == targetOutputReal)
{
vars["POST_BUILD"] = postBuildCmdLine; vars["POST_BUILD"] = postBuildCmdLine;
} else { }
else
{
vars["POST_BUILD"] = ":"; vars["POST_BUILD"] = ":";
symlinkVars["POST_BUILD"] = postBuildCmdLine; symlinkVars["POST_BUILD"] = postBuildCmdLine;
} }
int linkRuleLength = this->GetGlobalGenerator()-> cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
GetRuleCmdLength(this->LanguageLinkerRule());
int commandLineLengthLimit = 1; int commandLineLengthLimit = 1;
const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE"; const char* forceRspFile = "CMAKE_NINJA_FORCE_RESPONSE_FILE";
if (!mf->IsDefinitionSet(forceRspFile) && if (!mf->IsDefinitionSet(forceRspFile) &&
cmSystemTools::GetEnv(forceRspFile) == 0) { cmSystemTools::GetEnv(forceRspFile) == 0)
#ifdef _WIN32 {
commandLineLengthLimit = 8000 - linkRuleLength; commandLineLengthLimit = calculateCommandLineLengthLimit(
#elif defined(__linux) || defined(__APPLE__) || defined(__HAIKU__) globalGen.GetRuleCmdLength(this->LanguageLinkerRule()));
// for instance ARG_MAX is 2096152 on Ubuntu or 262144 on Mac
commandLineLengthLimit = ((int)sysconf(_SC_ARG_MAX))-linkRuleLength-1000;
#else
(void)linkRuleLength;
commandLineLengthLimit = -1;
#endif
} }
//Get the global generator as we are going to be call WriteBuild numerous const std::string rspfile =
//times in the following section std::string(cmake::GetCMakeFilesDirectoryPostSlash())
cmGlobalNinjaGenerator* globalGenerator = this->GetGlobalGenerator(); + target.GetName() + ".rsp";
const std::string rspfile = std::string
(cmake::GetCMakeFilesDirectoryPostSlash()) +
this->GetTarget()->GetName() + ".rsp";
// Write the build statement for this target. // Write the build statement for this target.
globalGenerator->WriteBuild(this->GetBuildFileStream(), globalGen.WriteBuild(this->GetBuildFileStream(),
comment.str(), comment.str(),
this->LanguageLinkerRule(), this->LanguageLinkerRule(),
outputs, outputs,
@ -609,10 +619,11 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
rspfile, rspfile,
commandLineLengthLimit); commandLineLengthLimit);
if (targetOutput != targetOutputReal && if (targetOutput != targetOutputReal && !target.IsFrameworkOnApple())
!this->GetTarget()->IsFrameworkOnApple()) { {
if (targetType == cmTarget::EXECUTABLE) { if (targetType == cmTarget::EXECUTABLE)
globalGenerator->WriteBuild(this->GetBuildFileStream(), {
globalGen.WriteBuild(this->GetBuildFileStream(),
"Create executable symlink " + targetOutput, "Create executable symlink " + targetOutput,
"CMAKE_SYMLINK_EXECUTABLE", "CMAKE_SYMLINK_EXECUTABLE",
cmNinjaDeps(1, targetOutput), cmNinjaDeps(1, targetOutput),
@ -620,18 +631,23 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
emptyDeps, emptyDeps,
emptyDeps, emptyDeps,
symlinkVars); symlinkVars);
} else { }
else
{
cmNinjaDeps symlinks; cmNinjaDeps symlinks;
const std::string soName = this->GetTargetFilePath(this->TargetNameSO); const std::string soName = this->GetTargetFilePath(this->TargetNameSO);
// If one link has to be created. // If one link has to be created.
if (targetOutputReal == soName || targetOutput == soName) { if (targetOutputReal == soName || targetOutput == soName)
{
symlinkVars["SONAME"] = soName; symlinkVars["SONAME"] = soName;
} else { }
else
{
symlinkVars["SONAME"] = ""; symlinkVars["SONAME"] = "";
symlinks.push_back(soName); symlinks.push_back(soName);
} }
symlinks.push_back(targetOutput); symlinks.push_back(targetOutput);
globalGenerator->WriteBuild(this->GetBuildFileStream(), globalGen.WriteBuild(this->GetBuildFileStream(),
"Create library symlink " + targetOutput, "Create library symlink " + targetOutput,
"CMAKE_SYMLINK_LIBRARY", "CMAKE_SYMLINK_LIBRARY",
symlinks, symlinks,
@ -642,20 +658,19 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
} }
} }
if (!this->TargetNameImport.empty()) { if (!this->TargetNameImport.empty())
{
// Since using multiple outputs would mess up the $out variable, use an // Since using multiple outputs would mess up the $out variable, use an
// alias for the import library. // alias for the import library.
globalGenerator->WritePhonyBuild(this->GetBuildFileStream(), globalGen.WritePhonyBuild(this->GetBuildFileStream(),
"Alias for import library.", "Alias for import library.",
cmNinjaDeps(1, targetOutputImplib), cmNinjaDeps(1, targetOutputImplib),
cmNinjaDeps(1, targetOutputReal)); cmNinjaDeps(1, targetOutputReal));
} }
// Add aliases for the file name and the target name. // Add aliases for the file name and the target name.
globalGenerator->AddTargetAlias(this->TargetNameOut, globalGen.AddTargetAlias(this->TargetNameOut, &target);
this->GetTarget()); globalGen.AddTargetAlias(this->GetTargetName(), &target);
globalGenerator->AddTargetAlias(this->GetTargetName(),
this->GetTarget());
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------