cmGeneratorTarget_TransitiveProperty.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. /* clang-format off */
  4. #include "cmGeneratorTarget.h"
  5. /* clang-format on */
  6. #include <map>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include <vector>
  11. #include <cm/memory>
  12. #include <cm/optional>
  13. #include <cm/string_view>
  14. #include <cmext/string_view>
  15. #include "cmGeneratorExpression.h"
  16. #include "cmGeneratorExpressionContext.h"
  17. #include "cmGeneratorExpressionDAGChecker.h"
  18. #include "cmGeneratorExpressionNode.h"
  19. #include "cmLinkItem.h"
  20. #include "cmList.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmPolicies.h"
  23. #include "cmStringAlgorithms.h"
  24. #include "cmValue.h"
  25. namespace {
  26. using UseTo = cmGeneratorTarget::UseTo;
  27. using TransitiveProperty = cmGeneratorTarget::TransitiveProperty;
  28. }
  29. const std::map<cm::string_view, TransitiveProperty>
  30. cmGeneratorTarget::BuiltinTransitiveProperties = {
  31. { "AUTOMOC_MACRO_NAMES"_s,
  32. { "INTERFACE_AUTOMOC_MACRO_NAMES"_s, UseTo::Compile } },
  33. { "AUTOUIC_OPTIONS"_s, { "INTERFACE_AUTOUIC_OPTIONS"_s, UseTo::Compile } },
  34. { "COMPILE_DEFINITIONS"_s,
  35. { "INTERFACE_COMPILE_DEFINITIONS"_s, UseTo::Compile } },
  36. { "COMPILE_FEATURES"_s,
  37. { "INTERFACE_COMPILE_FEATURES"_s, UseTo::Compile } },
  38. { "COMPILE_OPTIONS"_s, { "INTERFACE_COMPILE_OPTIONS"_s, UseTo::Compile } },
  39. { "INCLUDE_DIRECTORIES"_s,
  40. { "INTERFACE_INCLUDE_DIRECTORIES"_s, UseTo::Compile } },
  41. { "LINK_DEPENDS"_s, { "INTERFACE_LINK_DEPENDS"_s, UseTo::Link } },
  42. { "LINK_DIRECTORIES"_s, { "INTERFACE_LINK_DIRECTORIES"_s, UseTo::Link } },
  43. { "LINK_OPTIONS"_s, { "INTERFACE_LINK_OPTIONS"_s, UseTo::Link } },
  44. { "PRECOMPILE_HEADERS"_s,
  45. { "INTERFACE_PRECOMPILE_HEADERS"_s, UseTo::Compile } },
  46. { "SOURCES"_s, { "INTERFACE_SOURCES"_s, UseTo::Compile } },
  47. { "SYSTEM_INCLUDE_DIRECTORIES"_s,
  48. { "INTERFACE_SYSTEM_INCLUDE_DIRECTORIES"_s, UseTo::Compile } },
  49. };
  50. bool cmGeneratorTarget::MaybeHaveInterfaceProperty(
  51. std::string const& prop, cmGeneratorExpressionContext* context,
  52. UseTo usage) const
  53. {
  54. std::string const key = prop + '@' + context->Config;
  55. auto i = this->MaybeInterfacePropertyExists.find(key);
  56. if (i == this->MaybeInterfacePropertyExists.end()) {
  57. // Insert an entry now in case there is a cycle.
  58. i = this->MaybeInterfacePropertyExists.emplace(key, false).first;
  59. bool& maybeInterfaceProp = i->second;
  60. // If this target itself has a non-empty property value, we are done.
  61. maybeInterfaceProp = cmNonempty(this->GetProperty(prop));
  62. // Otherwise, recurse to interface dependencies.
  63. if (!maybeInterfaceProp) {
  64. cmGeneratorTarget const* headTarget =
  65. context->HeadTarget ? context->HeadTarget : this;
  66. if (cmLinkInterfaceLibraries const* iface =
  67. this->GetLinkInterfaceLibraries(context->Config, headTarget,
  68. usage)) {
  69. if (iface->HadHeadSensitiveCondition) {
  70. // With a different head target we may get to a library with
  71. // this interface property.
  72. maybeInterfaceProp = true;
  73. } else {
  74. // The transitive interface libraries do not depend on the
  75. // head target, so we can follow them.
  76. for (cmLinkItem const& lib : iface->Libraries) {
  77. if (lib.Target &&
  78. lib.Target->MaybeHaveInterfaceProperty(prop, context, usage)) {
  79. maybeInterfaceProp = true;
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. return i->second;
  88. }
  89. std::string cmGeneratorTarget::EvaluateInterfaceProperty(
  90. std::string const& prop, cmGeneratorExpressionContext* context,
  91. cmGeneratorExpressionDAGChecker* dagCheckerParent, UseTo usage) const
  92. {
  93. std::string result;
  94. // If the property does not appear transitively at all, we are done.
  95. if (!this->MaybeHaveInterfaceProperty(prop, context, usage)) {
  96. return result;
  97. }
  98. // Evaluate $<TARGET_PROPERTY:this,prop> as if it were compiled. This is
  99. // a subset of TargetPropertyNode::Evaluate without stringify/parse steps
  100. // but sufficient for transitive interface properties.
  101. cmGeneratorExpressionDAGChecker dagChecker(
  102. context->Backtrace, this, prop, nullptr, dagCheckerParent,
  103. this->LocalGenerator, context->Config);
  104. switch (dagChecker.Check()) {
  105. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  106. dagChecker.ReportError(
  107. context, "$<TARGET_PROPERTY:" + this->GetName() + "," + prop + ">");
  108. return result;
  109. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  110. // No error. We just skip cyclic references.
  111. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  112. // No error. We have already seen this transitive property.
  113. return result;
  114. case cmGeneratorExpressionDAGChecker::DAG:
  115. break;
  116. }
  117. cmGeneratorTarget const* headTarget =
  118. context->HeadTarget ? context->HeadTarget : this;
  119. if (cmValue p = this->GetProperty(prop)) {
  120. result = cmGeneratorExpressionNode::EvaluateDependentExpression(
  121. *p, context->LG, context, headTarget, &dagChecker, this);
  122. }
  123. if (cmLinkInterfaceLibraries const* iface =
  124. this->GetLinkInterfaceLibraries(context->Config, headTarget, usage)) {
  125. context->HadContextSensitiveCondition =
  126. context->HadContextSensitiveCondition ||
  127. iface->HadContextSensitiveCondition;
  128. for (cmLinkItem const& lib : iface->Libraries) {
  129. // Broken code can have a target in its own link interface.
  130. // Don't follow such link interface entries so as not to create a
  131. // self-referencing loop.
  132. if (lib.Target && lib.Target != this) {
  133. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in the
  134. // above property and hand-evaluate it as if it were compiled.
  135. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  136. cmGeneratorExpressionContext libContext(
  137. context->LG, context->Config, context->Quiet, headTarget, this,
  138. context->EvaluateForBuildsystem, context->Backtrace,
  139. context->Language);
  140. std::string libResult = cmGeneratorExpression::StripEmptyListElements(
  141. lib.Target->EvaluateInterfaceProperty(prop, &libContext, &dagChecker,
  142. usage));
  143. if (!libResult.empty()) {
  144. if (result.empty()) {
  145. result = std::move(libResult);
  146. } else {
  147. result.reserve(result.size() + 1 + libResult.size());
  148. result += ";";
  149. result += libResult;
  150. }
  151. }
  152. context->HadContextSensitiveCondition =
  153. context->HadContextSensitiveCondition ||
  154. libContext.HadContextSensitiveCondition;
  155. context->HadHeadSensitiveCondition =
  156. context->HadHeadSensitiveCondition ||
  157. libContext.HadHeadSensitiveCondition;
  158. }
  159. }
  160. }
  161. return result;
  162. }
  163. cm::optional<cmGeneratorTarget::TransitiveProperty>
  164. cmGeneratorTarget::IsTransitiveProperty(cm::string_view prop,
  165. cmLocalGenerator const* lg,
  166. std::string const& config,
  167. bool evaluatingLinkLibraries) const
  168. {
  169. cm::optional<TransitiveProperty> result;
  170. static const cm::string_view kINTERFACE_ = "INTERFACE_"_s;
  171. PropertyFor const propertyFor = cmHasPrefix(prop, kINTERFACE_)
  172. ? PropertyFor::Interface
  173. : PropertyFor::Build;
  174. if (propertyFor == PropertyFor::Interface) {
  175. prop = prop.substr(kINTERFACE_.length());
  176. }
  177. auto i = BuiltinTransitiveProperties.find(prop);
  178. if (i != BuiltinTransitiveProperties.end()) {
  179. result = i->second;
  180. if (result->Usage != cmGeneratorTarget::UseTo::Compile) {
  181. cmPolicies::PolicyStatus cmp0166 =
  182. lg->GetPolicyStatus(cmPolicies::CMP0166);
  183. if ((cmp0166 == cmPolicies::WARN || cmp0166 == cmPolicies::OLD) &&
  184. (prop == "LINK_DIRECTORIES"_s || prop == "LINK_DEPENDS"_s ||
  185. prop == "LINK_OPTIONS"_s)) {
  186. result->Usage = cmGeneratorTarget::UseTo::Compile;
  187. }
  188. }
  189. } else if (!evaluatingLinkLibraries) {
  190. // Honor TRANSITIVE_COMPILE_PROPERTIES and TRANSITIVE_LINK_PROPERTIES
  191. // from the link closure when we are not evaluating the closure itself.
  192. CustomTransitiveProperties const& ctp =
  193. this->GetCustomTransitiveProperties(config, propertyFor);
  194. auto ci = ctp.find(std::string(prop));
  195. if (ci != ctp.end()) {
  196. result = ci->second;
  197. }
  198. }
  199. return result;
  200. }
  201. cmGeneratorTarget::CustomTransitiveProperty::CustomTransitiveProperty(
  202. std::string interfaceName, UseTo usage)
  203. : CustomTransitiveProperty(
  204. cm::make_unique<std::string>(std::move(interfaceName)), usage)
  205. {
  206. }
  207. cmGeneratorTarget::CustomTransitiveProperty::CustomTransitiveProperty(
  208. std::unique_ptr<std::string> interfaceNameBuf, UseTo usage)
  209. : TransitiveProperty{ *interfaceNameBuf, usage }
  210. , InterfaceNameBuf(std::move(interfaceNameBuf))
  211. {
  212. }
  213. void cmGeneratorTarget::CustomTransitiveProperties::Add(cmValue props,
  214. UseTo usage)
  215. {
  216. if (props) {
  217. cmList propsList(*props);
  218. for (std::string p : propsList) {
  219. std::string ip;
  220. static const cm::string_view kINTERFACE_ = "INTERFACE_"_s;
  221. if (cmHasPrefix(p, kINTERFACE_)) {
  222. ip = std::move(p);
  223. p = ip.substr(kINTERFACE_.length());
  224. } else {
  225. ip = cmStrCat(kINTERFACE_, p);
  226. }
  227. this->emplace(std::move(p),
  228. CustomTransitiveProperty(std::move(ip), usage));
  229. }
  230. }
  231. }
  232. cmGeneratorTarget::CustomTransitiveProperties const&
  233. cmGeneratorTarget::GetCustomTransitiveProperties(std::string const& config,
  234. PropertyFor propertyFor) const
  235. {
  236. std::map<std::string, CustomTransitiveProperties>& ctpm =
  237. propertyFor == PropertyFor::Build
  238. ? this->CustomTransitiveBuildPropertiesMap
  239. : this->CustomTransitiveInterfacePropertiesMap;
  240. auto i = ctpm.find(config);
  241. if (i == ctpm.end()) {
  242. CustomTransitiveProperties ctp;
  243. auto addTransitiveProperties = [this, &config, propertyFor,
  244. &ctp](std::string const& tp, UseTo usage) {
  245. // Add transitive properties named by the target itself.
  246. ctp.Add(this->GetProperty(tp), usage);
  247. // Add transitive properties named by the target's link dependencies.
  248. if (propertyFor == PropertyFor::Build) {
  249. for (cmGeneratorTarget const* gt :
  250. this->GetLinkImplementationClosure(config, usage)) {
  251. ctp.Add(gt->GetProperty(tp), usage);
  252. }
  253. } else {
  254. // The set of custom transitive INTERFACE_ properties does not
  255. // depend on the consumer. Use the target as its own head.
  256. cmGeneratorTarget const* headTarget = this;
  257. for (cmGeneratorTarget const* gt :
  258. this->GetLinkInterfaceClosure(config, headTarget, usage)) {
  259. ctp.Add(gt->GetProperty(tp), usage);
  260. }
  261. }
  262. };
  263. addTransitiveProperties("TRANSITIVE_LINK_PROPERTIES", UseTo::Link);
  264. addTransitiveProperties("TRANSITIVE_COMPILE_PROPERTIES", UseTo::Compile);
  265. i = ctpm.emplace(config, std::move(ctp)).first;
  266. }
  267. return i->second;
  268. }