Ninja: Use cmNinjaBuild class for WriteBuild

This commit is contained in:
Sebastian Holtermann 2019-05-30 16:33:30 +02:00
parent cba8584517
commit 4c9e99e1f9

View File

@ -716,18 +716,21 @@ void cmNinjaNormalTargetGenerator::WriteDeviceLinkStatement()
void cmNinjaNormalTargetGenerator::WriteLinkStatement() void cmNinjaNormalTargetGenerator::WriteLinkStatement()
{ {
cmGeneratorTarget& gt = *this->GetGeneratorTarget(); cmMakefile* mf = this->GetMakefile();
const std::string cfgName = this->GetConfigName(); cmGlobalNinjaGenerator* globalGen = this->GetGlobalGenerator();
std::string targetOutput = ConvertToNinjaPath(gt.GetFullPath(cfgName)); cmGeneratorTarget* gt = this->GetGeneratorTarget();
std::string targetOutputReal = ConvertToNinjaPath(
gt.GetFullPath(cfgName, cmStateEnums::RuntimeBinaryArtifact,
/*realname=*/true));
std::string targetOutputImplib = ConvertToNinjaPath(
gt.GetFullPath(cfgName, cmStateEnums::ImportLibraryArtifact));
if (gt.IsAppBundleOnApple()) { const std::string cfgName = this->GetConfigName();
std::string targetOutput = ConvertToNinjaPath(gt->GetFullPath(cfgName));
std::string targetOutputReal = ConvertToNinjaPath(
gt->GetFullPath(cfgName, cmStateEnums::RuntimeBinaryArtifact,
/*realname=*/true));
std::string targetOutputImplib = ConvertToNinjaPath(
gt->GetFullPath(cfgName, cmStateEnums::ImportLibraryArtifact));
if (gt->IsAppBundleOnApple()) {
// Create the app bundle // Create the app bundle
std::string outpath = gt.GetDirectory(cfgName); std::string outpath = gt->GetDirectory(cfgName);
this->OSXBundleGenerator->CreateAppBundle(this->TargetNames.Output, this->OSXBundleGenerator->CreateAppBundle(this->TargetNames.Output,
outpath); outpath);
@ -740,33 +743,34 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
targetOutputReal += "/"; targetOutputReal += "/";
targetOutputReal += this->TargetNames.Real; targetOutputReal += this->TargetNames.Real;
targetOutputReal = this->ConvertToNinjaPath(targetOutputReal); targetOutputReal = this->ConvertToNinjaPath(targetOutputReal);
} else if (gt.IsFrameworkOnApple()) { } else if (gt->IsFrameworkOnApple()) {
// Create the library framework. // Create the library framework.
this->OSXBundleGenerator->CreateFramework(this->TargetNames.Output, this->OSXBundleGenerator->CreateFramework(this->TargetNames.Output,
gt.GetDirectory(cfgName)); gt->GetDirectory(cfgName));
} else if (gt.IsCFBundleOnApple()) { } else if (gt->IsCFBundleOnApple()) {
// Create the core foundation bundle. // Create the core foundation bundle.
this->OSXBundleGenerator->CreateCFBundle(this->TargetNames.Output, this->OSXBundleGenerator->CreateCFBundle(this->TargetNames.Output,
gt.GetDirectory(cfgName)); gt->GetDirectory(cfgName));
} }
// Write comments. // Write comments.
cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream()); cmGlobalNinjaGenerator::WriteDivider(this->GetBuildFileStream());
const cmStateEnums::TargetType targetType = gt.GetType(); const cmStateEnums::TargetType targetType = gt->GetType();
this->GetBuildFileStream() this->GetBuildFileStream()
<< "# Link build statements for " << cmState::GetTargetTypeName(targetType) << "# Link build statements for " << cmState::GetTargetTypeName(targetType)
<< " target " << this->GetTargetName() << "\n\n"; << " target " << this->GetTargetName() << "\n\n";
cmNinjaVars vars; cmNinjaBuild linkBuild(this->LanguageLinkerRule());
cmNinjaVars& vars = linkBuild.Variables;
// Compute the comment. // Compute the comment.
std::ostringstream comment; linkBuild.Comment = "Link the ";
comment << "Link the " << this->GetVisibleTypeName() << " " linkBuild.Comment += this->GetVisibleTypeName();
<< targetOutputReal; linkBuild.Comment += " ";
linkBuild.Comment += targetOutputReal;
// Compute outputs. // Compute outputs.
cmNinjaDeps outputs; linkBuild.Outputs.push_back(targetOutputReal);
outputs.push_back(targetOutputReal);
if (this->TargetLinkLanguage == "Swift") { if (this->TargetLinkLanguage == "Swift") {
vars["SWIFT_LIBRARY_NAME"] = [this]() -> std::string { vars["SWIFT_LIBRARY_NAME"] = [this]() -> std::string {
@ -775,12 +779,11 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
return targetNames.Base; return targetNames.Base;
}(); }();
vars["SWIFT_MODULE_NAME"] = [this]() -> std::string { vars["SWIFT_MODULE_NAME"] = [gt]() -> std::string {
if (const char* name = if (const char* name = gt->GetProperty("Swift_MODULE_NAME")) {
this->GetGeneratorTarget()->GetProperty("Swift_MODULE_NAME")) {
return name; return name;
} }
return this->GetGeneratorTarget()->GetName(); return gt->GetName();
}(); }();
vars["SWIFT_MODULE"] = [this](const std::string& module) -> std::string { vars["SWIFT_MODULE"] = [this](const std::string& module) -> std::string {
@ -804,7 +807,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
vars["SWIFT_OUTPUT_FILE_MAP"] = vars["SWIFT_OUTPUT_FILE_MAP"] =
this->GetLocalGenerator()->ConvertToOutputFormat( this->GetLocalGenerator()->ConvertToOutputFormat(
this->ConvertToNinjaPath(gt.GetSupportDirectory() + this->ConvertToNinjaPath(gt->GetSupportDirectory() +
"/output-file-map.json"), "/output-file-map.json"),
cmOutputConverter::SHELL); cmOutputConverter::SHELL);
@ -833,35 +836,31 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
} }
// Compute specific libraries to link with. // Compute specific libraries to link with.
cmNinjaDeps explicitDeps;
if (this->TargetLinkLanguage == "Swift") { if (this->TargetLinkLanguage == "Swift") {
std::vector<cmSourceFile const*> sources; std::vector<cmSourceFile const*> sources;
this->GetGeneratorTarget()->GetObjectSources(sources, gt->GetObjectSources(sources, this->GetConfigName());
this->GetConfigName());
for (const auto& source : sources) { for (const auto& source : sources) {
outputs.push_back( linkBuild.Outputs.push_back(
this->ConvertToNinjaPath(this->GetObjectFilePath(source))); this->ConvertToNinjaPath(this->GetObjectFilePath(source)));
explicitDeps.push_back( linkBuild.ExplicitDeps.push_back(
this->ConvertToNinjaPath(this->GetSourceFilePath(source))); this->ConvertToNinjaPath(this->GetSourceFilePath(source)));
} }
outputs.push_back(vars["SWIFT_MODULE"]); linkBuild.Outputs.push_back(vars["SWIFT_MODULE"]);
} else { } else {
explicitDeps = this->GetObjects(); linkBuild.ExplicitDeps = this->GetObjects();
} }
cmNinjaDeps implicitDeps = this->ComputeLinkDeps(this->TargetLinkLanguage); linkBuild.ImplicitDeps = this->ComputeLinkDeps(this->TargetLinkLanguage);
if (!this->DeviceLinkObject.empty()) { if (!this->DeviceLinkObject.empty()) {
explicitDeps.push_back(this->DeviceLinkObject); linkBuild.ExplicitDeps.push_back(this->DeviceLinkObject);
} }
cmMakefile* mf = this->GetMakefile();
std::string frameworkPath; std::string frameworkPath;
std::string linkPath; std::string linkPath;
std::string createRule = std::string createRule =
gt.GetCreateRuleVariable(this->TargetLinkLanguage, this->GetConfigName()); gt->GetCreateRuleVariable(this->TargetLinkLanguage, this->GetConfigName());
bool useWatcomQuote = mf->IsOn(createRule + "_USE_WATCOM_QUOTE"); bool useWatcomQuote = mf->IsOn(createRule + "_USE_WATCOM_QUOTE");
cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator(); cmLocalNinjaGenerator& localGen = *this->GetLocalGenerator();
@ -869,14 +868,14 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
localGen.ConvertToOutputFormat(targetOutputReal, cmOutputConverter::SHELL); localGen.ConvertToOutputFormat(targetOutputReal, cmOutputConverter::SHELL);
std::unique_ptr<cmLinkLineComputer> linkLineComputer( std::unique_ptr<cmLinkLineComputer> linkLineComputer(
this->GetGlobalGenerator()->CreateLinkLineComputer( globalGen->CreateLinkLineComputer(
this->GetLocalGenerator(), this->GetLocalGenerator(),
this->GetLocalGenerator()->GetStateSnapshot().GetDirectory())); this->GetLocalGenerator()->GetStateSnapshot().GetDirectory()));
linkLineComputer->SetUseWatcomQuote(useWatcomQuote); linkLineComputer->SetUseWatcomQuote(useWatcomQuote);
localGen.GetTargetFlags(linkLineComputer.get(), this->GetConfigName(), localGen.GetTargetFlags(linkLineComputer.get(), this->GetConfigName(),
vars["LINK_LIBRARIES"], vars["FLAGS"], vars["LINK_LIBRARIES"], vars["FLAGS"],
vars["LINK_FLAGS"], frameworkPath, linkPath, &gt); vars["LINK_FLAGS"], frameworkPath, linkPath, gt);
// Add OS X version flags, if any. // Add OS X version flags, if any.
if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY || if (this->GeneratorTarget->GetType() == cmStateEnums::SHARED_LIBRARY ||
@ -887,7 +886,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
"CURRENT", false); "CURRENT", false);
} }
this->addPoolNinjaVariable("JOB_POOL_LINK", &gt, vars); this->addPoolNinjaVariable("JOB_POOL_LINK", gt, vars);
this->AddModuleDefinitionFlag(linkLineComputer.get(), vars["LINK_FLAGS"]); this->AddModuleDefinitionFlag(linkLineComputer.get(), vars["LINK_FLAGS"]);
vars["LINK_FLAGS"] = vars["LINK_FLAGS"] =
@ -897,7 +896,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
vars["LINK_PATH"] = frameworkPath + linkPath; vars["LINK_PATH"] = frameworkPath + linkPath;
std::string lwyuFlags; std::string lwyuFlags;
if (gt.GetPropertyAsBool("LINK_WHAT_YOU_USE")) { if (gt->GetPropertyAsBool("LINK_WHAT_YOU_USE")) {
lwyuFlags = " -Wl,--no-as-needed"; lwyuFlags = " -Wl,--no-as-needed";
} }
@ -906,24 +905,23 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
// code between the Makefile executable and library generators. // code between the Makefile executable and library generators.
if (targetType == cmStateEnums::EXECUTABLE) { if (targetType == cmStateEnums::EXECUTABLE) {
std::string t = vars["FLAGS"]; std::string t = vars["FLAGS"];
localGen.AddArchitectureFlags(t, &gt, TargetLinkLanguage, cfgName); localGen.AddArchitectureFlags(t, gt, TargetLinkLanguage, cfgName);
t += lwyuFlags; t += lwyuFlags;
vars["FLAGS"] = t; vars["FLAGS"] = t;
} else { } else {
std::string t = vars["ARCH_FLAGS"]; std::string t = vars["ARCH_FLAGS"];
localGen.AddArchitectureFlags(t, &gt, TargetLinkLanguage, cfgName); localGen.AddArchitectureFlags(t, gt, TargetLinkLanguage, cfgName);
vars["ARCH_FLAGS"] = t; vars["ARCH_FLAGS"] = t;
t.clear(); t.clear();
t += lwyuFlags; t += lwyuFlags;
localGen.AddLanguageFlagsForLinking(t, &gt, TargetLinkLanguage, cfgName); localGen.AddLanguageFlagsForLinking(t, gt, TargetLinkLanguage, cfgName);
vars["LANGUAGE_COMPILE_FLAGS"] = t; vars["LANGUAGE_COMPILE_FLAGS"] = t;
} }
if (this->GetGeneratorTarget()->HasSOName(cfgName)) { if (gt->HasSOName(cfgName)) {
vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage); vars["SONAME_FLAG"] = mf->GetSONameFlag(this->TargetLinkLanguage);
vars["SONAME"] = this->TargetNames.SharedObject; vars["SONAME"] = this->TargetNames.SharedObject;
if (targetType == cmStateEnums::SHARED_LIBRARY) { if (targetType == cmStateEnums::SHARED_LIBRARY) {
std::string install_dir = std::string install_dir = gt->GetInstallNameDirForBuildTree(cfgName);
this->GetGeneratorTarget()->GetInstallNameDirForBuildTree(cfgName);
if (!install_dir.empty()) { if (!install_dir.empty()) {
vars["INSTALLNAME_DIR"] = localGen.ConvertToOutputFormat( vars["INSTALLNAME_DIR"] = localGen.ConvertToOutputFormat(
install_dir, cmOutputConverter::SHELL); install_dir, cmOutputConverter::SHELL);
@ -938,7 +936,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
targetOutputImplib, cmOutputConverter::SHELL); targetOutputImplib, cmOutputConverter::SHELL);
vars["TARGET_IMPLIB"] = impLibPath; vars["TARGET_IMPLIB"] = impLibPath;
EnsureParentDirectoryExists(impLibPath); EnsureParentDirectoryExists(impLibPath);
if (gt.HasImportLibrary(cfgName)) { if (gt->HasImportLibrary(cfgName)) {
byproducts.push_back(targetOutputImplib); byproducts.push_back(targetOutputImplib);
} }
} }
@ -949,7 +947,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
std::string prefix; std::string prefix;
std::string base; std::string base;
std::string suffix; std::string suffix;
this->GetGeneratorTarget()->GetFullNameComponents(prefix, base, suffix); gt->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")) {
@ -958,12 +956,12 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
vars["TARGET_PDB"] = base + suffix + dbg_suffix; vars["TARGET_PDB"] = base + suffix + dbg_suffix;
} }
const std::string objPath = GetGeneratorTarget()->GetSupportDirectory(); const std::string objPath = gt->GetSupportDirectory();
vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat( vars["OBJECT_DIR"] = this->GetLocalGenerator()->ConvertToOutputFormat(
this->ConvertToNinjaPath(objPath), cmOutputConverter::SHELL); this->ConvertToNinjaPath(objPath), cmOutputConverter::SHELL);
EnsureDirectoryExists(objPath); EnsureDirectoryExists(objPath);
if (this->GetGlobalGenerator()->IsGCCOnWindows()) { if (globalGen->IsGCCOnWindows()) {
// ar.exe can't handle backslashes in rsp files (implicitly used by gcc) // ar.exe can't handle backslashes in rsp files (implicitly used by gcc)
std::string& linkLibraries = vars["LINK_LIBRARIES"]; std::string& linkLibraries = vars["LINK_LIBRARIES"];
std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/'); std::replace(linkLibraries.begin(), linkLibraries.end(), '\\', '/');
@ -972,8 +970,8 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
} }
const std::vector<cmCustomCommand>* cmdLists[3] = { const std::vector<cmCustomCommand>* cmdLists[3] = {
&gt.GetPreBuildCommands(), &gt.GetPreLinkCommands(), &gt->GetPreBuildCommands(), &gt->GetPreLinkCommands(),
&gt.GetPostBuildCommands() &gt->GetPostBuildCommands()
}; };
std::vector<std::string> preLinkCmdLines, postBuildCmdLines; std::vector<std::string> preLinkCmdLines, postBuildCmdLines;
@ -993,7 +991,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
// maybe create .def file from list of objects // maybe create .def file from list of objects
cmGeneratorTarget::ModuleDefinitionInfo const* mdi = cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
gt.GetModuleDefinitionInfo(this->GetConfigName()); gt->GetModuleDefinitionInfo(this->GetConfigName());
if (mdi && mdi->DefFileGenerated) { if (mdi && mdi->DefFileGenerated) {
std::string cmakeCommand = std::string cmakeCommand =
this->GetLocalGenerator()->ConvertToOutputFormat( this->GetLocalGenerator()->ConvertToOutputFormat(
@ -1025,8 +1023,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
} }
} }
// If we have any PRE_LINK commands, we need to go back to CMAKE_BINARY_DIR // If we have any PRE_LINK commands, we need to go back to CMAKE_BINARY_DIR
// for // for the link commands.
// the link commands.
if (!preLinkCmdLines.empty()) { if (!preLinkCmdLines.empty()) {
const std::string homeOutDir = localGen.ConvertToOutputFormat( const std::string homeOutDir = localGen.ConvertToOutputFormat(
localGen.GetBinaryDirectory(), cmOutputConverter::SHELL); localGen.GetBinaryDirectory(), cmOutputConverter::SHELL);
@ -1040,14 +1037,13 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
cmNinjaVars symlinkVars; cmNinjaVars symlinkVars;
bool const symlinkNeeded = bool const symlinkNeeded =
(targetOutput != targetOutputReal && !gt.IsFrameworkOnApple()); (targetOutput != targetOutputReal && !gt->IsFrameworkOnApple());
if (!symlinkNeeded) { if (!symlinkNeeded) {
vars["POST_BUILD"] = postBuildCmdLine; vars["POST_BUILD"] = postBuildCmdLine;
} else { } else {
vars["POST_BUILD"] = cmGlobalNinjaGenerator::SHELL_NOOP; vars["POST_BUILD"] = cmGlobalNinjaGenerator::SHELL_NOOP;
symlinkVars["POST_BUILD"] = postBuildCmdLine; symlinkVars["POST_BUILD"] = postBuildCmdLine;
} }
cmGlobalNinjaGenerator& globalGen = *this->GetGlobalGenerator();
bool const lang_supports_response = bool const lang_supports_response =
!(this->TargetLinkLanguage == "RC" || this->TargetLinkLanguage == "CUDA"); !(this->TargetLinkLanguage == "RC" || this->TargetLinkLanguage == "CUDA");
@ -1055,32 +1051,27 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
if (!lang_supports_response || !this->ForceResponseFile()) { if (!lang_supports_response || !this->ForceResponseFile()) {
commandLineLengthLimit = commandLineLengthLimit =
static_cast<int>(cmSystemTools::CalculateCommandLineLengthLimit()) - static_cast<int>(cmSystemTools::CalculateCommandLineLengthLimit()) -
globalGen.GetRuleCmdLength(this->LanguageLinkerRule()); globalGen->GetRuleCmdLength(linkBuild.Rule);
} }
const std::string rspfile = this->ConvertToNinjaPath( linkBuild.RspFile = this->ConvertToNinjaPath(std::string("CMakeFiles/") +
std::string("CMakeFiles/") + gt.GetName() + ".rsp"); gt->GetName() + ".rsp");
// Gather order-only dependencies. // Gather order-only dependencies.
cmNinjaDeps orderOnlyDeps; this->GetLocalGenerator()->AppendTargetDepends(gt, linkBuild.OrderOnlyDeps);
this->GetLocalGenerator()->AppendTargetDepends(this->GetGeneratorTarget(),
orderOnlyDeps);
// Ninja should restat after linking if and only if there are byproducts. // Ninja should restat after linking if and only if there are byproducts.
vars["RESTAT"] = byproducts.empty() ? "" : "1"; vars["RESTAT"] = byproducts.empty() ? "" : "1";
for (std::string const& o : byproducts) { for (std::string const& o : byproducts) {
this->GetGlobalGenerator()->SeenCustomCommandOutput(o); globalGen->SeenCustomCommandOutput(o);
outputs.push_back(o); linkBuild.Outputs.push_back(o);
} }
// Write the build statement for this target. // Write the build statement for this target.
bool usedResponseFile = false; bool usedResponseFile = false;
globalGen.WriteBuild(this->GetBuildFileStream(), comment.str(), globalGen->WriteBuild(this->GetBuildFileStream(), linkBuild,
this->LanguageLinkerRule(), outputs, commandLineLengthLimit, &usedResponseFile);
/*implicitOuts=*/cmNinjaDeps(), explicitDeps,
implicitDeps, orderOnlyDeps, vars, rspfile,
commandLineLengthLimit, &usedResponseFile);
this->WriteLinkRule(usedResponseFile); this->WriteLinkRule(usedResponseFile);
if (symlinkNeeded) { if (symlinkNeeded) {
@ -1090,7 +1081,7 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
build.Outputs.push_back(targetOutput); build.Outputs.push_back(targetOutput);
build.ExplicitDeps.push_back(targetOutputReal); build.ExplicitDeps.push_back(targetOutputReal);
build.Variables = std::move(symlinkVars); build.Variables = std::move(symlinkVars);
globalGen.WriteBuild(this->GetBuildFileStream(), build); globalGen->WriteBuild(this->GetBuildFileStream(), build);
} else { } else {
cmNinjaBuild build("CMAKE_SYMLINK_LIBRARY"); cmNinjaBuild build("CMAKE_SYMLINK_LIBRARY");
build.Comment = "Create library symlink " + targetOutput; build.Comment = "Create library symlink " + targetOutput;
@ -1110,13 +1101,13 @@ void cmNinjaNormalTargetGenerator::WriteLinkStatement()
build.ExplicitDeps.push_back(targetOutputReal); build.ExplicitDeps.push_back(targetOutputReal);
build.Variables = std::move(symlinkVars); build.Variables = std::move(symlinkVars);
globalGen.WriteBuild(this->GetBuildFileStream(), build); globalGen->WriteBuild(this->GetBuildFileStream(), build);
} }
} }
// Add aliases for the file name and the target name. // Add aliases for the file name and the target name.
globalGen.AddTargetAlias(this->TargetNames.Output, &gt); globalGen->AddTargetAlias(this->TargetNames.Output, gt);
globalGen.AddTargetAlias(this->GetTargetName(), &gt); globalGen->AddTargetAlias(this->GetTargetName(), gt);
} }
void cmNinjaNormalTargetGenerator::WriteObjectLibStatement() void cmNinjaNormalTargetGenerator::WriteObjectLibStatement()