cmGeneratorTarget_TransitiveProperty.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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,
  103. this,
  104. prop,
  105. nullptr,
  106. dagCheckerParent,
  107. this->LocalGenerator,
  108. context->Config,
  109. };
  110. switch (dagChecker.Check()) {
  111. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  112. dagChecker.ReportError(
  113. context, "$<TARGET_PROPERTY:" + this->GetName() + "," + prop + ">");
  114. return result;
  115. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  116. // No error. We just skip cyclic references.
  117. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  118. // No error. We have already seen this transitive property.
  119. return result;
  120. case cmGeneratorExpressionDAGChecker::DAG:
  121. break;
  122. }
  123. cmGeneratorTarget const* headTarget =
  124. context->HeadTarget ? context->HeadTarget : this;
  125. if (cmValue p = this->GetProperty(prop)) {
  126. result = cmGeneratorExpressionNode::EvaluateDependentExpression(
  127. *p, context->LG, context, headTarget, &dagChecker, this);
  128. }
  129. if (cmLinkInterfaceLibraries const* iface =
  130. this->GetLinkInterfaceLibraries(context->Config, headTarget, usage)) {
  131. context->HadContextSensitiveCondition =
  132. context->HadContextSensitiveCondition ||
  133. iface->HadContextSensitiveCondition;
  134. for (cmLinkItem const& lib : iface->Libraries) {
  135. // Broken code can have a target in its own link interface.
  136. // Don't follow such link interface entries so as not to create a
  137. // self-referencing loop.
  138. if (lib.Target && lib.Target != this) {
  139. // Pretend $<TARGET_PROPERTY:lib.Target,prop> appeared in the
  140. // above property and hand-evaluate it as if it were compiled.
  141. // Create a context as cmCompiledGeneratorExpression::Evaluate does.
  142. cmGeneratorExpressionContext libContext(
  143. context->LG, context->Config, context->Quiet, headTarget, this,
  144. context->EvaluateForBuildsystem, context->Backtrace,
  145. context->Language);
  146. std::string libResult = cmGeneratorExpression::StripEmptyListElements(
  147. lib.Target->EvaluateInterfaceProperty(prop, &libContext, &dagChecker,
  148. usage));
  149. if (!libResult.empty()) {
  150. if (result.empty()) {
  151. result = std::move(libResult);
  152. } else {
  153. result.reserve(result.size() + 1 + libResult.size());
  154. result += ";";
  155. result += libResult;
  156. }
  157. }
  158. context->HadContextSensitiveCondition =
  159. context->HadContextSensitiveCondition ||
  160. libContext.HadContextSensitiveCondition;
  161. context->HadHeadSensitiveCondition =
  162. context->HadHeadSensitiveCondition ||
  163. libContext.HadHeadSensitiveCondition;
  164. }
  165. }
  166. }
  167. return result;
  168. }
  169. cm::optional<cmGeneratorTarget::TransitiveProperty>
  170. cmGeneratorTarget::IsTransitiveProperty(cm::string_view prop,
  171. cmLocalGenerator const* lg,
  172. std::string const& config,
  173. bool evaluatingLinkLibraries) const
  174. {
  175. cm::optional<TransitiveProperty> result;
  176. static const cm::string_view 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. result = i->second;
  186. if (result->Usage != cmGeneratorTarget::UseTo::Compile) {
  187. cmPolicies::PolicyStatus cmp0166 =
  188. lg->GetPolicyStatus(cmPolicies::CMP0166);
  189. if ((cmp0166 == cmPolicies::WARN || cmp0166 == cmPolicies::OLD) &&
  190. (prop == "LINK_DIRECTORIES"_s || prop == "LINK_DEPENDS"_s ||
  191. prop == "LINK_OPTIONS"_s)) {
  192. result->Usage = cmGeneratorTarget::UseTo::Compile;
  193. }
  194. }
  195. } else if (cmHasLiteralPrefix(prop, "COMPILE_DEFINITIONS_")) {
  196. cmPolicies::PolicyStatus cmp0043 =
  197. lg->GetPolicyStatus(cmPolicies::CMP0043);
  198. if (cmp0043 == cmPolicies::WARN || cmp0043 == cmPolicies::OLD) {
  199. result = TransitiveProperty{ "INTERFACE_COMPILE_DEFINITIONS"_s,
  200. UseTo::Compile };
  201. }
  202. } else if (!evaluatingLinkLibraries) {
  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 const cm::string_view 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. }