1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmLinkLineComputer.h"
#include <sstream>
#include <utility>
#include <vector>
#include "cmComputeLinkInformation.h"
#include "cmGeneratorTarget.h"
#include "cmList.h"
#include "cmListFileCache.h"
#include "cmOutputConverter.h"
#include "cmStateTypes.h"
#include "cmStringAlgorithms.h"
cmLinkLineComputer::cmLinkLineComputer(cmOutputConverter* outputConverter,
cmStateDirectory const& stateDir)
: StateDir(stateDir)
, OutputConverter(outputConverter)
{
}
cmLinkLineComputer::~cmLinkLineComputer() = default;
void cmLinkLineComputer::SetUseWatcomQuote(bool useWatcomQuote)
{
this->UseWatcomQuote = useWatcomQuote;
}
void cmLinkLineComputer::SetUseNinjaMulti(bool useNinjaMulti)
{
this->UseNinjaMulti = useNinjaMulti;
}
void cmLinkLineComputer::SetForResponse(bool forResponse)
{
this->ForResponse = forResponse;
}
void cmLinkLineComputer::SetRelink(bool relink)
{
this->Relink = relink;
}
std::string cmLinkLineComputer::ConvertToLinkReference(
std::string const& lib) const
{
return this->OutputConverter->MaybeRelativeToCurBinDir(lib);
}
std::string cmLinkLineComputer::ComputeLinkLibs(cmComputeLinkInformation& cli)
{
std::string linkLibs;
std::vector<BT<std::string>> linkLibsList;
this->ComputeLinkLibs(cli, linkLibsList);
cli.AppendValues(linkLibs, linkLibsList);
return linkLibs;
}
void cmLinkLineComputer::ComputeLinkLibs(
cmComputeLinkInformation& cli, std::vector<BT<std::string>>& linkLibraries)
{
using ItemVector = cmComputeLinkInformation::ItemVector;
ItemVector const& items = cli.GetItems();
for (auto const& item : items) {
if (item.Target &&
(item.Target->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
item.Target->GetType() == cmStateEnums::OBJECT_LIBRARY)) {
continue;
}
BT<std::string> linkLib;
if (item.IsPath == cmComputeLinkInformation::ItemIsPath::Yes) {
linkLib = item.GetFormattedItem(this->ConvertToOutputFormat(
this->ConvertToLinkReference(item.Value.Value)));
} else {
linkLib = item.Value;
}
linkLib.Value += " ";
linkLibraries.emplace_back(linkLib);
}
}
std::string cmLinkLineComputer::ConvertToOutputFormat(std::string const& input)
{
cmOutputConverter::OutputFormat shellFormat = cmOutputConverter::SHELL;
if (this->ForResponse) {
shellFormat = cmOutputConverter::RESPONSE;
} else if (this->UseNinjaMulti) {
shellFormat = cmOutputConverter::NINJAMULTI;
}
return this->OutputConverter->ConvertToOutputFormat(input, shellFormat,
this->UseWatcomQuote);
}
std::string cmLinkLineComputer::ConvertToOutputForExisting(
std::string const& input)
{
cmOutputConverter::OutputFormat shellFormat = cmOutputConverter::SHELL;
if (this->ForResponse) {
shellFormat = cmOutputConverter::RESPONSE;
} else if (this->UseNinjaMulti) {
shellFormat = cmOutputConverter::NINJAMULTI;
}
return this->OutputConverter->ConvertToOutputForExisting(
input, shellFormat, this->UseWatcomQuote);
}
std::string cmLinkLineComputer::ComputeLinkPath(
cmComputeLinkInformation& cli, std::string const& libPathFlag,
std::string const& libPathTerminator, std::string const& stdLinkDirString)
{
std::string linkPath;
std::vector<BT<std::string>> linkPathList;
this->ComputeLinkPath(cli, libPathFlag, libPathTerminator, stdLinkDirString,
linkPathList);
cli.AppendValues(linkPath, linkPathList);
return linkPath;
}
void cmLinkLineComputer::ComputeLinkPath(
cmComputeLinkInformation& cli, std::string const& libPathFlag,
std::string const& libPathTerminator, std::string const& stdLinkDirString,
std::vector<BT<std::string>>& linkPath)
{
if (cli.GetLinkLanguage() == "Swift") {
std::string linkPathNoBT;
for (cmComputeLinkInformation::Item const& item : cli.GetItems()) {
cmGeneratorTarget const* target = item.Target;
if (!target) {
continue;
}
if (target->GetType() == cmStateEnums::STATIC_LIBRARY ||
target->GetType() == cmStateEnums::SHARED_LIBRARY) {
cmStateEnums::ArtifactType type = cmStateEnums::RuntimeBinaryArtifact;
if (target->HasImportLibrary(cli.GetConfig())) {
type = cmStateEnums::ImportLibraryArtifact;
}
linkPathNoBT +=
cmStrCat(' ', libPathFlag,
this->ConvertToOutputForExisting(
item.Target->GetDirectory(cli.GetConfig(), type)),
libPathTerminator, ' ');
}
}
if (!linkPathNoBT.empty()) {
linkPath.emplace_back(std::move(linkPathNoBT));
}
}
for (BT<std::string> libDir : cli.GetDirectoriesWithBacktraces()) {
libDir.Value = cmStrCat(' ', libPathFlag,
this->ConvertToOutputForExisting(libDir.Value),
libPathTerminator, ' ');
linkPath.emplace_back(libDir);
}
for (auto& linkDir : cmList(stdLinkDirString)) {
linkPath.emplace_back(cmStrCat(' ', libPathFlag,
this->ConvertToOutputForExisting(linkDir),
libPathTerminator, ' '));
}
}
std::string cmLinkLineComputer::ComputeRPath(cmComputeLinkInformation& cli)
{
std::string rpath;
// Check what kind of rpath flags to use.
if (cli.GetRuntimeSep().empty()) {
// Each rpath entry gets its own option ("-R a -R b -R c")
std::vector<std::string> runtimeDirs;
cli.GetRPath(runtimeDirs, this->Relink);
for (std::string const& rd : runtimeDirs) {
rpath += cli.GetRuntimeFlag();
rpath += this->ConvertToOutputFormat(rd);
rpath += " ";
}
} else {
// All rpath entries are combined ("-Wl,-rpath,a:b:c").
std::string rpathString = cli.GetRPathString(this->Relink);
// Store the rpath option in the stream.
if (!rpathString.empty()) {
rpath += cli.GetRuntimeFlag();
rpath +=
this->OutputConverter->EscapeForShell(rpathString, !this->ForResponse);
rpath += " ";
}
}
return rpath;
}
std::string cmLinkLineComputer::ComputeFrameworkPath(
cmComputeLinkInformation& cli, cmValue fwSearchFlag)
{
if (!fwSearchFlag) {
return std::string{};
}
std::string frameworkPath;
for (auto const& fd : cli.GetFrameworkPaths()) {
frameworkPath +=
cmStrCat(fwSearchFlag, this->ConvertToOutputFormat(fd), ' ');
}
return frameworkPath;
}
std::string cmLinkLineComputer::ComputeLinkLibraries(
cmComputeLinkInformation& cli, std::string const& stdLibString)
{
std::string linkLibraries;
std::vector<BT<std::string>> linkLibrariesList;
this->ComputeLinkLibraries(cli, stdLibString, linkLibrariesList);
cli.AppendValues(linkLibraries, linkLibrariesList);
return linkLibraries;
}
void cmLinkLineComputer::ComputeLinkLibraries(
cmComputeLinkInformation& cli, std::string const& stdLibString,
std::vector<BT<std::string>>& linkLibraries)
{
std::ostringstream rpathOut;
rpathOut << this->ComputeRPath(cli);
std::string rpath = rpathOut.str();
if (!rpath.empty()) {
linkLibraries.emplace_back(std::move(rpath));
}
// Write the library flags to the build rule.
this->ComputeLinkLibs(cli, linkLibraries);
// Add the linker runtime search path if any.
std::ostringstream fout;
std::string rpath_link = cli.GetRPathLinkString();
if (!cli.GetRPathLinkFlag().empty() && !rpath_link.empty()) {
fout << cli.GetRPathLinkFlag();
fout << this->OutputConverter->EscapeForShell(rpath_link,
!this->ForResponse);
fout << " ";
}
if (!stdLibString.empty()) {
fout << stdLibString << " ";
}
std::string remainingLibs = fout.str();
if (!remainingLibs.empty()) {
linkLibraries.emplace_back(remainingLibs);
}
}
std::string cmLinkLineComputer::GetLinkerLanguage(cmGeneratorTarget* target,
std::string const& config)
{
return target->GetLinkerLanguage(config);
}
|