cmLinkLineDeviceComputer.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 = true;
  83. if (item.Target->GetType() == cmStateEnums::STATIC_LIBRARY) {
  84. if ((!item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS")) &&
  85. item.Target->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION")) {
  86. skip = false;
  87. }
  88. }
  89. if (skip) {
  90. continue;
  91. }
  92. }
  93. std::string out;
  94. if (item.IsPath) {
  95. // nvcc understands absolute paths to libraries ending in '.a' or '.lib'.
  96. // These should be passed to nvlink. Other extensions need to be left
  97. // out because nvlink may not understand or need them. Even though it
  98. // can tolerate '.so' or '.dylib' it cannot tolerate '.so.1'.
  99. if (cmHasLiteralSuffix(item.Value, ".a") ||
  100. cmHasLiteralSuffix(item.Value, ".lib")) {
  101. out += this->ConvertToOutputFormat(
  102. this->ConvertToLinkReference(item.Value));
  103. }
  104. } else if (item.Value == "-framework") {
  105. // This is the first part of '-framework Name' where the framework
  106. // name is specified as a following item. Ignore both.
  107. skipItemAfterFramework = true;
  108. continue;
  109. } else if (cmLinkItemValidForDevice(item.Value)) {
  110. out += item.Value;
  111. }
  112. if (emitted.insert(out).second) {
  113. fout << out << " ";
  114. }
  115. }
  116. if (!stdLibString.empty()) {
  117. fout << stdLibString << " ";
  118. }
  119. return fout.str();
  120. }
  121. std::string cmLinkLineDeviceComputer::GetLinkerLanguage(cmGeneratorTarget*,
  122. std::string const&)
  123. {
  124. return "CUDA";
  125. }
  126. bool requireDeviceLinking(cmGeneratorTarget& target, cmLocalGenerator& lg,
  127. const std::string& config)
  128. {
  129. if (!target.GetGlobalGenerator()->GetLanguageEnabled("CUDA")) {
  130. return false;
  131. }
  132. if (target.GetType() == cmStateEnums::OBJECT_LIBRARY) {
  133. return false;
  134. }
  135. if (const char* resolveDeviceSymbols =
  136. target.GetProperty("CUDA_RESOLVE_DEVICE_SYMBOLS")) {
  137. // If CUDA_RESOLVE_DEVICE_SYMBOLS has been explicitly set we need
  138. // to honor the value no matter what it is.
  139. return cmSystemTools::IsOn(resolveDeviceSymbols);
  140. }
  141. if (const char* separableCompilation =
  142. target.GetProperty("CUDA_SEPARABLE_COMPILATION")) {
  143. if (cmSystemTools::IsOn(separableCompilation)) {
  144. bool doDeviceLinking = false;
  145. switch (target.GetType()) {
  146. case cmStateEnums::SHARED_LIBRARY:
  147. case cmStateEnums::MODULE_LIBRARY:
  148. case cmStateEnums::EXECUTABLE:
  149. doDeviceLinking = true;
  150. break;
  151. default:
  152. break;
  153. }
  154. return doDeviceLinking;
  155. }
  156. }
  157. // Determine if we have any dependencies that require
  158. // us to do a device link step
  159. const std::string cuda_lang("CUDA");
  160. cmGeneratorTarget::LinkClosure const* closure =
  161. target.GetLinkClosure(config);
  162. bool closureHasCUDA =
  163. (std::find(closure->Languages.begin(), closure->Languages.end(),
  164. cuda_lang) != closure->Languages.end());
  165. if (closureHasCUDA) {
  166. cmComputeLinkInformation* pcli = target.GetLinkInformation(config);
  167. if (pcli) {
  168. cmLinkLineDeviceComputer deviceLinkComputer(
  169. &lg, lg.GetStateSnapshot().GetDirectory());
  170. return deviceLinkComputer.ComputeRequiresDeviceLinking(*pcli);
  171. }
  172. return true;
  173. }
  174. return false;
  175. }