cmLinkLineDeviceComputer.cxx 7.1 KB

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