cmLinkItem.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <map>
  6. #include <ostream>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <vector>
  10. #include <cm/optional>
  11. #include <cmext/algorithm>
  12. #include "cmListFileCache.h"
  13. #include "cmSystemTools.h"
  14. #include "cmTargetLinkLibraryType.h"
  15. class cmGeneratorTarget;
  16. class cmSourceFile;
  17. // Basic information about each link item.
  18. class cmLinkItem
  19. {
  20. std::string String;
  21. public:
  22. // default feature: link library without decoration
  23. static const std::string DEFAULT;
  24. cmLinkItem() = default;
  25. cmLinkItem(std::string s, bool c, cmListFileBacktrace bt,
  26. std::string feature = DEFAULT);
  27. cmLinkItem(cmGeneratorTarget const* t, bool c, cmListFileBacktrace bt,
  28. std::string feature = DEFAULT);
  29. std::string const& AsStr() const;
  30. cmGeneratorTarget const* Target = nullptr;
  31. // The source file representing the external object (used when linking
  32. // `$<TARGET_OBJECTS>`)
  33. cmSourceFile const* ObjectSource = nullptr;
  34. std::string Feature;
  35. bool Cross = false;
  36. cmListFileBacktrace Backtrace;
  37. friend bool operator<(cmLinkItem const& l, cmLinkItem const& r);
  38. friend bool operator==(cmLinkItem const& l, cmLinkItem const& r);
  39. friend std::ostream& operator<<(std::ostream& os, cmLinkItem const& item);
  40. };
  41. class cmLinkImplItem : public cmLinkItem
  42. {
  43. public:
  44. cmLinkImplItem() = default;
  45. cmLinkImplItem(cmLinkItem item, bool checkCMP0027);
  46. bool CheckCMP0027 = false;
  47. };
  48. /** The link implementation specifies the direct library
  49. dependencies needed by the object files of the target. */
  50. struct cmLinkImplementationLibraries
  51. {
  52. // Libraries linked directly in this configuration.
  53. std::vector<cmLinkImplItem> Libraries;
  54. // Object files linked directly in this configuration.
  55. std::vector<cmLinkItem> Objects;
  56. // Libraries linked directly in other configurations.
  57. // Needed only for OLD behavior of CMP0003.
  58. std::vector<cmLinkItem> WrongConfigLibraries;
  59. // Whether the list depends on a genex referencing the configuration.
  60. bool HadContextSensitiveCondition = false;
  61. };
  62. struct cmLinkInterfaceLibraries
  63. {
  64. // Libraries listed in the interface.
  65. std::vector<cmLinkItem> Libraries;
  66. // Object files listed in the interface.
  67. std::vector<cmLinkItem> Objects;
  68. // Items to be included as if directly linked by the head target.
  69. std::vector<cmLinkItem> HeadInclude;
  70. // Items to be excluded from direct linking by the head target.
  71. std::vector<cmLinkItem> HeadExclude;
  72. // Whether the list depends on a genex referencing the head target.
  73. bool HadHeadSensitiveCondition = false;
  74. // Whether the list depends on a genex referencing the configuration.
  75. bool HadContextSensitiveCondition = false;
  76. };
  77. struct cmLinkInterface : public cmLinkInterfaceLibraries
  78. {
  79. // Languages whose runtime libraries must be linked.
  80. std::vector<std::string> Languages;
  81. std::unordered_map<std::string, std::vector<cmLinkItem>>
  82. LanguageRuntimeLibraries;
  83. // Shared library dependencies needed for linking on some platforms.
  84. std::vector<cmLinkItem> SharedDeps;
  85. // Number of repetitions of a strongly connected component of two
  86. // or more static libraries.
  87. unsigned int Multiplicity = 0;
  88. // Libraries listed for other configurations.
  89. // Needed only for OLD behavior of CMP0003.
  90. std::vector<cmLinkItem> WrongConfigLibraries;
  91. bool ImplementationIsInterface = false;
  92. // Whether the list depends on a link language genex.
  93. bool HadLinkLanguageSensitiveCondition = false;
  94. };
  95. struct cmOptionalLinkInterface : public cmLinkInterface
  96. {
  97. bool LibrariesDone = false;
  98. bool AllDone = false;
  99. bool Exists = false;
  100. bool Explicit = false;
  101. bool CheckLinkLibraries = false;
  102. };
  103. struct cmHeadToLinkInterfaceMap
  104. : public std::map<cmGeneratorTarget const*, cmOptionalLinkInterface>
  105. {
  106. };
  107. struct cmLinkImplementation : public cmLinkImplementationLibraries
  108. {
  109. // Languages whose runtime libraries must be linked.
  110. std::vector<std::string> Languages;
  111. std::unordered_map<std::string, std::vector<cmLinkImplItem>>
  112. LanguageRuntimeLibraries;
  113. // Whether the list depends on a link language genex.
  114. bool HadLinkLanguageSensitiveCondition = false;
  115. };
  116. // Cache link implementation computation from each configuration.
  117. struct cmOptionalLinkImplementation : public cmLinkImplementation
  118. {
  119. bool LibrariesDone = false;
  120. bool LanguagesDone = false;
  121. bool HadHeadSensitiveCondition = false;
  122. bool CheckLinkLibraries = false;
  123. };
  124. /** Compute the link type to use for the given configuration. */
  125. inline cmTargetLinkLibraryType CMP0003_ComputeLinkType(
  126. const std::string& config, std::vector<std::string> const& debugConfigs)
  127. {
  128. // No configuration is always optimized.
  129. if (config.empty()) {
  130. return OPTIMIZED_LibraryType;
  131. }
  132. // Check if any entry in the list matches this configuration.
  133. std::string configUpper = cmSystemTools::UpperCase(config);
  134. if (cm::contains(debugConfigs, configUpper)) {
  135. return DEBUG_LibraryType;
  136. }
  137. // The current configuration is not a debug configuration.
  138. return OPTIMIZED_LibraryType;
  139. }
  140. // Parse LINK_LIBRARY genex markers.
  141. cm::optional<std::string> ParseLinkFeature(std::string const& item);