cmLinkLineDeviceComputer.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. return std::any_of(
  52. items.begin(), items.end(),
  53. [](cmComputeLinkInformation::Item const& item) -> bool {
  54. return item.Target &&
  55. item.Target->GetType() == cmStateEnums::STATIC_LIBRARY &&
  56. // this dependency requires us to device link it
  57. !item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS") &&
  58. item.Target->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION");
  59. });
  60. }
  61. bool cmLinkLineDeviceComputer::ComputeRequiresDeviceLinkingIPOFlag(
  62. cmComputeLinkInformation& cli)
  63. {
  64. // Determine if this item might requires device linking.
  65. // For this we only consider targets
  66. using ItemVector = cmComputeLinkInformation::ItemVector;
  67. ItemVector const& items = cli.GetItems();
  68. std::string config = cli.GetConfig();
  69. return std::any_of(
  70. items.begin(), items.end(),
  71. [config](cmComputeLinkInformation::Item const& item) -> bool {
  72. return item.Target &&
  73. item.Target->GetType() == cmStateEnums::STATIC_LIBRARY &&
  74. // this dependency requires us to device link it
  75. !item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS") &&
  76. item.Target->GetPropertyAsBool("CUDA_SEPARABLE_COMPILATION") &&
  77. item.Target->IsIPOEnabled("CUDA", config);
  78. });
  79. }
  80. void cmLinkLineDeviceComputer::ComputeLinkLibraries(
  81. cmComputeLinkInformation& cli, std::string const& stdLibString,
  82. std::vector<BT<std::string>>& linkLibraries)
  83. {
  84. // Generate the unique set of link items when device linking.
  85. // The nvcc device linker is designed so that each static library
  86. // with device symbols only needs to be listed once as it doesn't
  87. // care about link order.
  88. std::set<std::string> emitted;
  89. using ItemVector = cmComputeLinkInformation::ItemVector;
  90. ItemVector const& items = cli.GetItems();
  91. std::string config = cli.GetConfig();
  92. bool skipItemAfterFramework = false;
  93. // Note:
  94. // Any modification of this algorithm should be reflected also in
  95. // cmVisualStudio10TargetGenerator::ComputeCudaLinkOptions
  96. for (auto const& item : items) {
  97. if (skipItemAfterFramework) {
  98. skipItemAfterFramework = false;
  99. continue;
  100. }
  101. if (item.Target) {
  102. bool skip = false;
  103. switch (item.Target->GetType()) {
  104. case cmStateEnums::SHARED_LIBRARY:
  105. case cmStateEnums::MODULE_LIBRARY:
  106. case cmStateEnums::OBJECT_LIBRARY:
  107. case cmStateEnums::INTERFACE_LIBRARY:
  108. skip = true;
  109. break;
  110. case cmStateEnums::STATIC_LIBRARY:
  111. skip = item.Target->GetPropertyAsBool("CUDA_RESOLVE_DEVICE_SYMBOLS");
  112. break;
  113. default:
  114. break;
  115. }
  116. if (skip) {
  117. continue;
  118. }
  119. }
  120. BT<std::string> linkLib;
  121. if (item.IsPath == cmComputeLinkInformation::ItemIsPath::Yes) {
  122. // nvcc understands absolute paths to libraries ending in '.a' or '.lib'.
  123. // These should be passed to nvlink. Other extensions need to be left
  124. // out because nvlink may not understand or need them. Even though it
  125. // can tolerate '.so' or '.dylib' it cannot tolerate '.so.1'.
  126. if (cmHasLiteralSuffix(item.Value.Value, ".a") ||
  127. cmHasLiteralSuffix(item.Value.Value, ".lib")) {
  128. linkLib.Value = item
  129. .GetFormattedItem(this->ConvertToOutputFormat(
  130. this->ConvertToLinkReference(item.Value.Value)))
  131. .Value;
  132. }
  133. } else if (item.Value == "-framework") {
  134. // This is the first part of '-framework Name' where the framework
  135. // name is specified as a following item. Ignore both.
  136. skipItemAfterFramework = true;
  137. continue;
  138. } else if (cmLinkItemValidForDevice(item.Value.Value)) {
  139. linkLib.Value = item.Value.Value;
  140. }
  141. if (emitted.insert(linkLib.Value).second) {
  142. linkLib.Value += " ";
  143. const cmLinkImplementation* linkImpl =
  144. cli.GetTarget()->GetLinkImplementation(
  145. cli.GetConfig(), cmGeneratorTarget::LinkInterfaceFor::Link);
  146. for (const cmLinkImplItem& iter : linkImpl->Libraries) {
  147. if (iter.Target != nullptr &&
  148. iter.Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  149. std::string libPath = iter.Target->GetLocation(cli.GetConfig());
  150. if (item.Value == libPath) {
  151. linkLib.Backtrace = iter.Backtrace;
  152. break;
  153. }
  154. }
  155. }
  156. linkLibraries.emplace_back(linkLib);
  157. }
  158. }
  159. if (!stdLibString.empty()) {
  160. linkLibraries.emplace_back(cmStrCat(stdLibString, ' '));
  161. }
  162. }
  163. std::string cmLinkLineDeviceComputer::GetLinkerLanguage(cmGeneratorTarget*,
  164. std::string const&)
  165. {
  166. return "CUDA";
  167. }
  168. bool requireDeviceLinking(cmGeneratorTarget& target, cmLocalGenerator& lg,
  169. const std::string& config)
  170. {
  171. if (!target.GetGlobalGenerator()->GetLanguageEnabled("CUDA")) {
  172. return false;
  173. }
  174. if (target.GetType() == cmStateEnums::OBJECT_LIBRARY) {
  175. return false;
  176. }
  177. if (!lg.GetMakefile()->IsOn("CMAKE_CUDA_COMPILER_HAS_DEVICE_LINK_PHASE")) {
  178. return false;
  179. }
  180. if (cmValue resolveDeviceSymbols =
  181. target.GetProperty("CUDA_RESOLVE_DEVICE_SYMBOLS")) {
  182. // If CUDA_RESOLVE_DEVICE_SYMBOLS has been explicitly set we need
  183. // to honor the value no matter what it is.
  184. return cmIsOn(*resolveDeviceSymbols);
  185. }
  186. // Determine if we have any dependencies that require
  187. // us to do a device link step
  188. cmGeneratorTarget::LinkClosure const* closure =
  189. target.GetLinkClosure(config);
  190. if (cm::contains(closure->Languages, "CUDA")) {
  191. if (cmIsOn(target.GetProperty("CUDA_SEPARABLE_COMPILATION"))) {
  192. bool doDeviceLinking = false;
  193. switch (target.GetType()) {
  194. case cmStateEnums::SHARED_LIBRARY:
  195. case cmStateEnums::MODULE_LIBRARY:
  196. case cmStateEnums::EXECUTABLE:
  197. doDeviceLinking = true;
  198. break;
  199. default:
  200. break;
  201. }
  202. return doDeviceLinking;
  203. }
  204. cmComputeLinkInformation* pcli = target.GetLinkInformation(config);
  205. if (pcli) {
  206. cmLinkLineDeviceComputer deviceLinkComputer(
  207. &lg, lg.GetStateSnapshot().GetDirectory());
  208. return deviceLinkComputer.ComputeRequiresDeviceLinking(*pcli);
  209. }
  210. return true;
  211. }
  212. return false;
  213. }