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