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