cmLinkItem.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. // Whether the list depends on a genex referencing the configuration.
  57. bool HadContextSensitiveCondition = false;
  58. };
  59. struct cmLinkInterfaceLibraries
  60. {
  61. // Libraries listed in the interface.
  62. std::vector<cmLinkItem> Libraries;
  63. // Object files listed in the interface.
  64. std::vector<cmLinkItem> Objects;
  65. // Items to be included as if directly linked by the head target.
  66. std::vector<cmLinkItem> HeadInclude;
  67. // Items to be excluded from direct linking by the head target.
  68. std::vector<cmLinkItem> HeadExclude;
  69. // Whether the list depends on a genex referencing the head target.
  70. bool HadHeadSensitiveCondition = false;
  71. // Whether the list depends on a genex referencing the configuration.
  72. bool HadContextSensitiveCondition = false;
  73. };
  74. struct cmLinkInterface : public cmLinkInterfaceLibraries
  75. {
  76. // Languages whose runtime libraries must be linked.
  77. std::vector<std::string> Languages;
  78. std::unordered_map<std::string, std::vector<cmLinkItem>>
  79. LanguageRuntimeLibraries;
  80. // Shared library dependencies needed for linking on some platforms.
  81. std::vector<cmLinkItem> SharedDeps;
  82. // Number of repetitions of a strongly connected component of two
  83. // or more static libraries.
  84. unsigned int Multiplicity = 0;
  85. // Whether the list depends on a link language genex.
  86. bool HadLinkLanguageSensitiveCondition = false;
  87. };
  88. struct cmOptionalLinkInterface : public cmLinkInterface
  89. {
  90. bool LibrariesDone = false;
  91. bool AllDone = false;
  92. bool Exists = false;
  93. bool CheckLinkLibraries = false;
  94. };
  95. struct cmHeadToLinkInterfaceMap
  96. : public std::map<cmGeneratorTarget const*, cmOptionalLinkInterface>
  97. {
  98. };
  99. struct cmLinkImplementation : public cmLinkImplementationLibraries
  100. {
  101. // Languages whose runtime libraries must be linked.
  102. std::vector<std::string> Languages;
  103. std::unordered_map<std::string, std::vector<cmLinkImplItem>>
  104. LanguageRuntimeLibraries;
  105. // Whether the list depends on a link language genex.
  106. bool HadLinkLanguageSensitiveCondition = false;
  107. };
  108. // Cache link implementation computation from each configuration.
  109. struct cmOptionalLinkImplementation : public cmLinkImplementation
  110. {
  111. bool LibrariesDone = false;
  112. bool LanguagesDone = false;
  113. bool HadHeadSensitiveCondition = false;
  114. bool CheckLinkLibraries = false;
  115. };
  116. /** Compute the link type to use for the given configuration. */
  117. inline cmTargetLinkLibraryType ComputeLinkType(
  118. const std::string& config, std::vector<std::string> const& debugConfigs)
  119. {
  120. // No configuration is always optimized.
  121. if (config.empty()) {
  122. return OPTIMIZED_LibraryType;
  123. }
  124. // Check if any entry in the list matches this configuration.
  125. std::string configUpper = cmSystemTools::UpperCase(config);
  126. if (cm::contains(debugConfigs, configUpper)) {
  127. return DEBUG_LibraryType;
  128. }
  129. // The current configuration is not a debug configuration.
  130. return OPTIMIZED_LibraryType;
  131. }
  132. // Parse LINK_LIBRARY genex markers.
  133. cm::optional<std::string> ParseLinkFeature(std::string const& item);