cmLinkLineDeviceComputer.cxx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "cmLinkLineDeviceComputer.h"
  4. #include <algorithm>
  5. #include <set>
  6. #include <sstream>
  7. #include <utility>
  8. #include <vector>
  9. #include "cmAlgorithms.h"
  10. #include "cmComputeLinkInformation.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmStateDirectory.h"
  14. #include "cmStateSnapshot.h"
  15. #include "cmStateTypes.h"
  16. #include "cmSystemTools.h"
  17. class cmOutputConverter;
  18. cmLinkLineDeviceComputer::cmLinkLineDeviceComputer(
  19. cmOutputConverter* outputConverter, cmStateDirectory const& stateDir)
  20. : cmLinkLineComputer(outputConverter, stateDir)
  21. {
  22. }
  23. cmLinkLineDeviceComputer::~cmLinkLineDeviceComputer() = default;
  24. static bool cmLinkItemValidForDevice(std::string const& item)
  25. {
  26. // Valid items are:
  27. // * Non-flags (does not start in '-')
  28. // * Specific flags --library, --library-path, -l, -L
  29. // For example:
  30. // * 'cublas_device' => pass-along
  31. // * '--library pthread' => pass-along
  32. // * '-lpthread' => pass-along
  33. // * '-pthread' => drop
  34. // * '-a' => drop
  35. // * '-framework Name' (as one string) => drop
  36. return (!cmHasLiteralPrefix(item, "-") || //
  37. cmHasLiteralPrefix(item, "-l") || //
  38. cmHasLiteralPrefix(item, "-L") || //
  39. cmHasLiteralPrefix(item, "--library"));
  40. }
  41. bool cmLinkLineDeviceComputer::ComputeRequiresDeviceLinking(
  42. cmComputeLinkInformation& cli)
  43. {
  44. // Determine if this item might requires device linking.
  45. // For this we only consider targets
  46. typedef cmComputeLinkInformation::ItemVector ItemVector;
  47. ItemVector const& items = cli.GetItems();
  48. std::string config = cli.GetConfig();
  49. for (auto const& item : items) {
  50. if (item.Target &&
  51. item.Target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  52. if ((!item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS")) &&
  53. item.Target->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) {
  54. // this dependency requires us to device link it
  55. return true;
  56. }
  57. }
  58. }
  59. return false;
  60. }
  61. std::string cmLinkLineDeviceComputer::ComputeLinkLibraries(
  62. cmComputeLinkInformation& cli, std::string const& stdLibString)
  63. {
  64. // Write the library flags to the build rule.
  65. std::ostringstream fout;
  66. // Generate the unique set of link items when device linking.
  67. // The nvcc device linker is designed so that each static library
  68. // with device symbols only needs to be listed once as it doesn't
  69. // care about link order.
  70. std::set<std::string> emitted;
  71. typedef cmComputeLinkInformation::ItemVector ItemVector;
  72. ItemVector const& items = cli.GetItems();
  73. std::string config = cli.GetConfig();
  74. bool skipItemAfterFramework = false;
  75. for (auto const& item : items) {
  76. if (skipItemAfterFramework) {
  77. skipItemAfterFramework = false;
  78. continue;
  79. }
  80. if (item.Target) {
  81. bool skip = true;
  82. if (item.Target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  83. if ((!item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS")) &&
  84. item.Target->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) {
  85. skip = false;
  86. }
  87. }
  88. if (skip) {
  89. continue;
  90. }
  91. }
  92. std::string out;
  93. if (item.IsPath) {
  94. // nvcc understands absolute paths to libraries ending in '.a' or '.lib'.
  95. // These should be passed to nvlink. Other extensions need to be left
  96. // out because nvlink may not understand or need them. Even though it
  97. // can tolerate '.so' or '.dylib' it cannot tolerate '.so.1'.
  98. if (cmHasLiteralSuffix(item.Value, ".a") ||
  99. cmHasLiteralSuffix(item.Value, ".lib")) {
  100. out += this->ConvertToOutputFormat(
  101. this->ConvertToLinkReference(item.Value));
  102. }
  103. } else if (item.Value == "-framework") {
  104. // This is the first part of '-framework Name' where the framework
  105. // name is specified as a following item. Ignore both.
  106. skipItemAfterFramework = true;
  107. continue;
  108. } else if (cmLinkItemValidForDevice(item.Value)) {
  109. out += item.Value;
  110. }
  111. if (emitted.insert(out).second) {
  112. fout << out << " ";
  113. }
  114. }
  115. if (!stdLibString.empty()) {
  116. fout << stdLibString << " ";
  117. }
  118. return fout.str();
  119. }
  120. std::string cmLinkLineDeviceComputer::GetLinkerLanguage(cmGeneratorTarget*,
  121. std::string const&)
  122. {
  123. return "CUDA";
  124. }
  125. bool requireDeviceLinking(cmGeneratorTarget& target, cmLocalGenerator& lg,
  126. const std::string& config)
  127. {
  128. if (target.GetType() == cmStateEnums::OBJECT_LIBRARY) {
  129. return false;
  130. }
  131. if (const char* resolveDeviceSymbols =
  132. target.GetProperty("CUDA_RESOLVE_DEVICE_SYMBOLS")) {
  133. // If CUDA_RESOLVE_DEVICE_SYMBOLS has been explicitly set we need
  134. // to honor the value no matter what it is.
  135. return cmSystemTools::IsOn(resolveDeviceSymbols);
  136. }
  137. if (const char* separableCompilation =
  138. target.GetProperty("CUDA_SEPARABLE_COMPILATION")) {
  139. if (cmSystemTools::IsOn(separableCompilation)) {
  140. bool doDeviceLinking = false;
  141. switch (target.GetType()) {
  142. case cmStateEnums::SHARED_LIBRARY:
  143. case cmStateEnums::MODULE_LIBRARY:
  144. case cmStateEnums::EXECUTABLE:
  145. doDeviceLinking = true;
  146. break;
  147. default:
  148. break;
  149. }
  150. return doDeviceLinking;
  151. }
  152. }
  153. // Determine if we have any dependencies that require
  154. // us to do a device link step
  155. const std::string cuda_lang("CUDA");
  156. cmGeneratorTarget::LinkClosure const* closure =
  157. target.GetLinkClosure(config);
  158. bool closureHasCUDA =
  159. (std::find(closure->Languages.begin(), closure->Languages.end(),
  160. cuda_lang) != closure->Languages.end());
  161. if (closureHasCUDA) {
  162. cmComputeLinkInformation* pcli = target.GetLinkInformation(config);
  163. if (pcli) {
  164. cmLinkLineDeviceComputer deviceLinkComputer(
  165. &lg, lg.GetStateSnapshot().GetDirectory());
  166. return deviceLinkComputer.ComputeRequiresDeviceLinking(*pcli);
  167. }
  168. return true;
  169. }
  170. return false;
  171. }