cmLinkLineComputer.cxx 5.3 KB

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