cmLinkLineComputer.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. item.Target->GetType() == cmStateEnums::OBJECT_LIBRARY)) {
  58. continue;
  59. }
  60. BT<std::string> linkLib;
  61. if (item.IsPath == cmComputeLinkInformation::ItemIsPath::Yes) {
  62. linkLib = item.GetFormattedItem(this->ConvertToOutputFormat(
  63. this->ConvertToLinkReference(item.Value.Value)));
  64. } else {
  65. linkLib = item.Value;
  66. }
  67. linkLib.Value += " ";
  68. linkLibraries.emplace_back(linkLib);
  69. }
  70. }
  71. std::string cmLinkLineComputer::ConvertToOutputFormat(std::string const& input)
  72. {
  73. cmOutputConverter::OutputFormat shellFormat = cmOutputConverter::SHELL;
  74. if (this->ForResponse) {
  75. shellFormat = cmOutputConverter::RESPONSE;
  76. } else if (this->UseNinjaMulti) {
  77. shellFormat = cmOutputConverter::NINJAMULTI;
  78. }
  79. return this->OutputConverter->ConvertToOutputFormat(input, shellFormat,
  80. this->UseWatcomQuote);
  81. }
  82. std::string cmLinkLineComputer::ConvertToOutputForExisting(
  83. std::string const& input)
  84. {
  85. cmOutputConverter::OutputFormat shellFormat = cmOutputConverter::SHELL;
  86. if (this->ForResponse) {
  87. shellFormat = cmOutputConverter::RESPONSE;
  88. } else if (this->UseNinjaMulti) {
  89. shellFormat = cmOutputConverter::NINJAMULTI;
  90. }
  91. return this->OutputConverter->ConvertToOutputForExisting(
  92. input, shellFormat, this->UseWatcomQuote);
  93. }
  94. std::string cmLinkLineComputer::ComputeLinkPath(
  95. cmComputeLinkInformation& cli, std::string const& libPathFlag,
  96. std::string const& libPathTerminator)
  97. {
  98. std::string linkPath;
  99. std::vector<BT<std::string>> linkPathList;
  100. this->ComputeLinkPath(cli, libPathFlag, libPathTerminator, linkPathList);
  101. cli.AppendValues(linkPath, linkPathList);
  102. return linkPath;
  103. }
  104. void cmLinkLineComputer::ComputeLinkPath(
  105. cmComputeLinkInformation& cli, std::string const& libPathFlag,
  106. std::string const& libPathTerminator, std::vector<BT<std::string>>& linkPath)
  107. {
  108. if (cli.GetLinkLanguage() == "Swift") {
  109. std::string linkPathNoBT;
  110. for (const cmComputeLinkInformation::Item& item : cli.GetItems()) {
  111. const cmGeneratorTarget* target = item.Target;
  112. if (!target) {
  113. continue;
  114. }
  115. if (target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  116. target->GetType() == cmStateEnums::SHARED_LIBRARY) {
  117. cmStateEnums::ArtifactType type = cmStateEnums::RuntimeBinaryArtifact;
  118. if (target->HasImportLibrary(cli.GetConfig())) {
  119. type = cmStateEnums::ImportLibraryArtifact;
  120. }
  121. linkPathNoBT +=
  122. cmStrCat(" ", libPathFlag,
  123. this->ConvertToOutputForExisting(
  124. item.Target->GetDirectory(cli.GetConfig(), type)),
  125. libPathTerminator, " ");
  126. }
  127. }
  128. if (!linkPathNoBT.empty()) {
  129. linkPath.emplace_back(std::move(linkPathNoBT));
  130. }
  131. }
  132. for (BT<std::string> libDir : cli.GetDirectoriesWithBacktraces()) {
  133. libDir.Value = cmStrCat(" ", libPathFlag,
  134. this->ConvertToOutputForExisting(libDir.Value),
  135. libPathTerminator, " ");
  136. linkPath.emplace_back(libDir);
  137. }
  138. }
  139. std::string cmLinkLineComputer::ComputeRPath(cmComputeLinkInformation& cli)
  140. {
  141. std::string rpath;
  142. // Check what kind of rpath flags to use.
  143. if (cli.GetRuntimeSep().empty()) {
  144. // Each rpath entry gets its own option ("-R a -R b -R c")
  145. std::vector<std::string> runtimeDirs;
  146. cli.GetRPath(runtimeDirs, this->Relink);
  147. for (std::string const& rd : runtimeDirs) {
  148. rpath += cli.GetRuntimeFlag();
  149. rpath += this->ConvertToOutputFormat(rd);
  150. rpath += " ";
  151. }
  152. } else {
  153. // All rpath entries are combined ("-Wl,-rpath,a:b:c").
  154. std::string rpathString = cli.GetRPathString(this->Relink);
  155. // Store the rpath option in the stream.
  156. if (!rpathString.empty()) {
  157. rpath += cli.GetRuntimeFlag();
  158. rpath +=
  159. this->OutputConverter->EscapeForShell(rpathString, !this->ForResponse);
  160. rpath += " ";
  161. }
  162. }
  163. return rpath;
  164. }
  165. std::string cmLinkLineComputer::ComputeFrameworkPath(
  166. cmComputeLinkInformation& cli, cmValue fwSearchFlag, cmValue sysFwSearchFlag)
  167. {
  168. if (!fwSearchFlag && !sysFwSearchFlag) {
  169. return std::string{};
  170. }
  171. std::string frameworkPath;
  172. auto const& fwDirs = cli.GetFrameworkPaths();
  173. for (auto const& fd : fwDirs) {
  174. if (sysFwSearchFlag &&
  175. cli.GetTarget()->IsSystemIncludeDirectory(fd, cli.GetConfig(),
  176. cli.GetLinkLanguage())) {
  177. frameworkPath += sysFwSearchFlag;
  178. } else {
  179. frameworkPath += fwSearchFlag;
  180. }
  181. frameworkPath += this->ConvertToOutputFormat(fd);
  182. frameworkPath += " ";
  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. }