1
0

cmGeneratorTarget_TransitiveProperty.cxx 11 KB

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