cmLinkLineDeviceComputer.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <set>
  5. #include <utility>
  6. #include <cmext/algorithm>
  7. #include "cmComputeLinkInformation.h"
  8. #include "cmGeneratorTarget.h"
  9. #include "cmGlobalGenerator.h"
  10. #include "cmLinkItem.h"
  11. #include "cmListFileCache.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmProperty.h"
  15. #include "cmStateDirectory.h"
  16. #include "cmStateSnapshot.h"
  17. #include "cmStateTypes.h"
  18. #include "cmStringAlgorithms.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. using ItemVector = cmComputeLinkInformation::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. void cmLinkLineDeviceComputer::ComputeLinkLibraries(
  64. cmComputeLinkInformation& cli, std::string const& stdLibString,
  65. std::vector<BT<std::string>>& linkLibraries)
  66. {
  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. using ItemVector = cmComputeLinkInformation::ItemVector;
  73. ItemVector const& items = cli.GetItems();
  74. std::string config = cli.GetConfig();
  75. bool skipItemAfterFramework = false;
  76. // Note:
  77. // Any modification of this algorithm should be reflected also in
  78. // cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions
  79. for (auto const& item : items) {
  80. if (skipItemAfterFramework) {
  81. skipItemAfterFramework = false;
  82. continue;
  83. }
  84. if (item.Target) {
  85. bool skip = false;
  86. switch (item.Target->GetType()) {
  87. case cmStateEnums::SHARED_LIBRARY:
  88. case cmStateEnums::MODULE_LIBRARY:
  89. case cmStateEnums::INTERFACE_LIBRARY:
  90. skip = true;
  91. break;
  92. case cmStateEnums::STATIC_LIBRARY:
  93. skip = item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS");
  94. break;
  95. default:
  96. break;
  97. }
  98. if (skip) {
  99. continue;
  100. }
  101. }
  102. BT<std::string> linkLib;
  103. if (item.IsPath) {
  104. // nvcc understands absolute paths to libraries ending in '.a' or '.lib'.
  105. // These should be passed to nvlink. Other extensions need to be left
  106. // out because nvlink may not understand or need them. Even though it
  107. // can tolerate '.so' or '.dylib' it cannot tolerate '.so.1'.
  108. if (cmHasLiteralSuffix(item.Value.Value, ".a") ||
  109. cmHasLiteralSuffix(item.Value.Value, ".lib")) {
  110. linkLib.Value += this->ConvertToOutputFormat(
  111. this->ConvertToLinkReference(item.Value.Value));
  112. }
  113. } else if (item.Value == "-framework") {
  114. // This is the first part of '-framework Name' where the framework
  115. // name is specified as a following item. Ignore both.
  116. skipItemAfterFramework = true;
  117. continue;
  118. } else if (cmLinkItemValidForDevice(item.Value.Value)) {
  119. linkLib.Value += item.Value.Value;
  120. }
  121. if (emitted.insert(linkLib.Value).second) {
  122. linkLib.Value += " ";
  123. const cmLinkImplementation* linkImpl =
  124. cli.GetTarget()->GetLinkImplementation(cli.GetConfig());
  125. for (const cmLinkImplItem& iter : linkImpl->Libraries) {
  126. if (iter.Target != nullptr &&
  127. iter.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  128. std::string libPath = iter.Target->GetLocation(cli.GetConfig());
  129. if (item.Value == libPath) {
  130. linkLib.Backtrace = iter.Backtrace;
  131. break;
  132. }
  133. }
  134. }
  135. linkLibraries.emplace_back(linkLib);
  136. }
  137. }
  138. if (!stdLibString.empty()) {
  139. linkLibraries.emplace_back(cmStrCat(stdLibString, ' '));
  140. }
  141. }
  142. std::string cmLinkLineDeviceComputer::GetLinkerLanguage(cmGeneratorTarget*,
  143. std::string const&)
  144. {
  145. return "CUDA";
  146. }
  147. bool requireDeviceLinking(cmGeneratorTarget& target, cmLocalGenerator& lg,
  148. const std::string& config)
  149. {
  150. if (!target.GetGlobalGenerator()->GetLanguageEnabled("CUDA")) {
  151. return false;
  152. }
  153. if (target.GetType() == cmStateEnums::OBJECT_LIBRARY) {
  154. return false;
  155. }
  156. if (!lg.GetMakefile()->IsOn("CMAKE_CUDA_COMPILER_HAS_DEVICE_LINK_PHASE")) {
  157. return false;
  158. }
  159. if (cmProp resolveDeviceSymbols =
  160. target.GetProperty("CUDA_RESOLVE_DEVICE_SYMBOLS")) {
  161. // If CUDA_RESOLVE_DEVICE_SYMBOLS has been explicitly set we need
  162. // to honor the value no matter what it is.
  163. return cmIsOn(*resolveDeviceSymbols);
  164. }
  165. // Determine if we have any dependencies that require
  166. // us to do a device link step
  167. cmGeneratorTarget::LinkClosure const* closure =
  168. target.GetLinkClosure(config);
  169. if (cm::contains(closure->Languages, "CUDA")) {
  170. if (cmProp separableCompilation =
  171. target.GetProperty("CUDA_SEPARABLE_COMPILATION")) {
  172. if (cmIsOn(*separableCompilation)) {
  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. }
  186. cmComputeLinkInformation* pcli = target.GetLinkInformation(config);
  187. if (pcli) {
  188. cmLinkLineDeviceComputer deviceLinkComputer(
  189. &lg, lg.GetStateSnapshot().GetDirectory());
  190. return deviceLinkComputer.ComputeRequiresDeviceLinking(*pcli);
  191. }
  192. return true;
  193. }
  194. return false;
  195. }