cmLinkLineDeviceComputer.cxx 6.2 KB

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