cmGeneratorTarget_TransitiveProperty.cxx 11 KB

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