cmGeneratorTarget_TransitiveProperty.cxx 11 KB

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