cmLinkLineComputer.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <vector>
  6. #include "cmComputeLinkInformation.h"
  7. #include "cmGeneratorTarget.h"
  8. #include "cmOutputConverter.h"
  9. #include "cmStateDirectory.h"
  10. #include "cmStateTypes.h"
  11. #include "cmSystemTools.h"
  12. cmLinkLineComputer::cmLinkLineComputer(cmOutputConverter* outputConverter,
  13. cmStateDirectory const& stateDir)
  14. : StateDir(stateDir)
  15. , OutputConverter(outputConverter)
  16. , ForResponse(false)
  17. , UseWatcomQuote(false)
  18. , Relink(false)
  19. {
  20. }
  21. cmLinkLineComputer::~cmLinkLineComputer()
  22. {
  23. }
  24. void cmLinkLineComputer::SetUseWatcomQuote(bool useWatcomQuote)
  25. {
  26. this->UseWatcomQuote = useWatcomQuote;
  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. std::string relLib = lib;
  40. if (cmOutputConverter::ContainedInDirectory(
  41. this->StateDir.GetCurrentBinary(), lib, this->StateDir)) {
  42. relLib = cmSystemTools::ForceToRelativePath(
  43. this->StateDir.GetCurrentBinary(), lib);
  44. }
  45. return relLib;
  46. }
  47. std::string cmLinkLineComputer::ComputeLinkLibs(cmComputeLinkInformation& cli)
  48. {
  49. std::string linkLibs;
  50. typedef cmComputeLinkInformation::ItemVector ItemVector;
  51. ItemVector const& items = cli.GetItems();
  52. for (auto const& item : items) {
  53. if (item.Target &&
  54. item.Target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  55. continue;
  56. }
  57. if (item.IsPath) {
  58. linkLibs +=
  59. this->ConvertToOutputFormat(this->ConvertToLinkReference(item.Value));
  60. } else {
  61. linkLibs += item.Value;
  62. }
  63. linkLibs += " ";
  64. }
  65. return linkLibs;
  66. }
  67. std::string cmLinkLineComputer::ConvertToOutputFormat(std::string const& input)
  68. {
  69. cmOutputConverter::OutputFormat shellFormat = (this->ForResponse)
  70. ? cmOutputConverter::RESPONSE
  71. : ((this->UseWatcomQuote) ? cmOutputConverter::WATCOMQUOTE
  72. : cmOutputConverter::SHELL);
  73. return this->OutputConverter->ConvertToOutputFormat(input, shellFormat);
  74. }
  75. std::string cmLinkLineComputer::ConvertToOutputForExisting(
  76. std::string const& input)
  77. {
  78. cmOutputConverter::OutputFormat shellFormat = (this->ForResponse)
  79. ? cmOutputConverter::RESPONSE
  80. : ((this->UseWatcomQuote) ? cmOutputConverter::WATCOMQUOTE
  81. : cmOutputConverter::SHELL);
  82. return this->OutputConverter->ConvertToOutputForExisting(input, shellFormat);
  83. }
  84. std::string cmLinkLineComputer::ComputeLinkPath(
  85. cmComputeLinkInformation& cli, std::string const& libPathFlag,
  86. std::string const& libPathTerminator)
  87. {
  88. std::string linkPath;
  89. std::vector<std::string> const& libDirs = cli.GetDirectories();
  90. for (std::string const& libDir : libDirs) {
  91. std::string libpath = this->ConvertToOutputForExisting(libDir);
  92. linkPath += " " + libPathFlag;
  93. linkPath += libpath;
  94. linkPath += libPathTerminator;
  95. linkPath += " ";
  96. }
  97. return linkPath;
  98. }
  99. std::string cmLinkLineComputer::ComputeRPath(cmComputeLinkInformation& cli)
  100. {
  101. std::string rpath;
  102. // Check what kind of rpath flags to use.
  103. if (cli.GetRuntimeSep().empty()) {
  104. // Each rpath entry gets its own option ("-R a -R b -R c")
  105. std::vector<std::string> runtimeDirs;
  106. cli.GetRPath(runtimeDirs, this->Relink);
  107. for (std::string const& rd : runtimeDirs) {
  108. rpath += cli.GetRuntimeFlag();
  109. rpath += this->ConvertToOutputFormat(rd);
  110. rpath += " ";
  111. }
  112. } else {
  113. // All rpath entries are combined ("-Wl,-rpath,a:b:c").
  114. std::string rpathString = cli.GetRPathString(this->Relink);
  115. // Store the rpath option in the stream.
  116. if (!rpathString.empty()) {
  117. rpath += cli.GetRuntimeFlag();
  118. rpath +=
  119. this->OutputConverter->EscapeForShell(rpathString, !this->ForResponse);
  120. rpath += " ";
  121. }
  122. }
  123. return rpath;
  124. }
  125. std::string cmLinkLineComputer::ComputeFrameworkPath(
  126. cmComputeLinkInformation& cli, std::string const& fwSearchFlag)
  127. {
  128. std::string frameworkPath;
  129. if (!fwSearchFlag.empty()) {
  130. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  131. for (std::string const& fd : fwDirs) {
  132. frameworkPath += fwSearchFlag;
  133. frameworkPath += this->ConvertToOutputFormat(fd);
  134. frameworkPath += " ";
  135. }
  136. }
  137. return frameworkPath;
  138. }
  139. std::string cmLinkLineComputer::ComputeLinkLibraries(
  140. cmComputeLinkInformation& cli, std::string const& stdLibString)
  141. {
  142. std::ostringstream fout;
  143. fout << this->ComputeRPath(cli);
  144. // Write the library flags to the build rule.
  145. fout << this->ComputeLinkLibs(cli);
  146. // Add the linker runtime search path if any.
  147. std::string rpath_link = cli.GetRPathLinkString();
  148. if (!cli.GetRPathLinkFlag().empty() && !rpath_link.empty()) {
  149. fout << cli.GetRPathLinkFlag();
  150. fout << this->OutputConverter->EscapeForShell(rpath_link,
  151. !this->ForResponse);
  152. fout << " ";
  153. }
  154. if (!stdLibString.empty()) {
  155. fout << stdLibString << " ";
  156. }
  157. return fout.str();
  158. }
  159. std::string cmLinkLineComputer::GetLinkerLanguage(cmGeneratorTarget* target,
  160. std::string const& config)
  161. {
  162. return target->GetLinkerLanguage(config);
  163. }