cmLinkLineComputer.cxx 7.9 KB

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