cmLinkLineDeviceComputer.cxx 6.3 KB

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