cmGeneratorTarget_TransitiveProperty.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "cmGenExEvaluation.h"
  16. #include "cmGeneratorExpression.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(std::string const& prop,
  52. cm::GenEx::Evaluation* eval,
  53. UseTo usage) const
  54. {
  55. std::string const key = prop + '@' + eval->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. eval->HeadTarget ? eval->HeadTarget : this;
  67. if (cmLinkInterfaceLibraries const* iface =
  68. this->GetLinkInterfaceLibraries(eval->Config, headTarget, 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, eval, 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, cm::GenEx::Evaluation* eval,
  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, eval, 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. this, prop, nullptr, dagCheckerParent,
  103. eval->LG, eval->Config, eval->Backtrace,
  104. };
  105. switch (dagChecker.Check()) {
  106. case cmGeneratorExpressionDAGChecker::SELF_REFERENCE:
  107. dagChecker.ReportError(
  108. eval, "$<TARGET_PROPERTY:" + this->GetName() + "," + prop + ">");
  109. return result;
  110. case cmGeneratorExpressionDAGChecker::CYCLIC_REFERENCE:
  111. // No error. We just skip cyclic references.
  112. case cmGeneratorExpressionDAGChecker::ALREADY_SEEN:
  113. // No error. We have already seen this transitive property.
  114. return result;
  115. case cmGeneratorExpressionDAGChecker::DAG:
  116. break;
  117. }
  118. cmGeneratorTarget const* headTarget =
  119. eval->HeadTarget ? eval->HeadTarget : this;
  120. if (cmValue p = this->GetProperty(prop)) {
  121. result = cmGeneratorExpressionNode::EvaluateDependentExpression(
  122. *p, eval, headTarget, &dagChecker, this);
  123. }
  124. if (cmLinkInterfaceLibraries const* iface =
  125. this->GetLinkInterfaceLibraries(eval->Config, headTarget, usage)) {
  126. eval->HadContextSensitiveCondition = eval->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. cm::GenEx::Evaluation libEval(
  137. eval->LG, eval->Config, eval->Quiet, headTarget, this,
  138. eval->EvaluateForBuildsystem, eval->Backtrace, eval->Language);
  139. std::string libResult = cmGeneratorExpression::StripEmptyListElements(
  140. lib.Target->EvaluateInterfaceProperty(prop, &libEval, &dagChecker,
  141. usage));
  142. if (!libResult.empty()) {
  143. if (result.empty()) {
  144. result = std::move(libResult);
  145. } else {
  146. result.reserve(result.size() + 1 + libResult.size());
  147. result += ";";
  148. result += libResult;
  149. }
  150. }
  151. eval->HadContextSensitiveCondition =
  152. eval->HadContextSensitiveCondition ||
  153. libEval.HadContextSensitiveCondition;
  154. eval->HadHeadSensitiveCondition =
  155. eval->HadHeadSensitiveCondition || libEval.HadHeadSensitiveCondition;
  156. }
  157. }
  158. }
  159. return result;
  160. }
  161. cm::optional<cmGeneratorTarget::TransitiveProperty>
  162. cmGeneratorTarget::IsTransitiveProperty(
  163. cm::string_view prop, cmLocalGenerator const* lg, std::string const& config,
  164. cmGeneratorExpressionDAGChecker const* dagChecker) const
  165. {
  166. cm::optional<TransitiveProperty> result;
  167. static cm::string_view const kINTERFACE_ = "INTERFACE_"_s;
  168. PropertyFor const propertyFor = cmHasPrefix(prop, kINTERFACE_)
  169. ? PropertyFor::Interface
  170. : PropertyFor::Build;
  171. if (propertyFor == PropertyFor::Interface) {
  172. prop = prop.substr(kINTERFACE_.length());
  173. }
  174. auto i = BuiltinTransitiveProperties.find(prop);
  175. if (i != BuiltinTransitiveProperties.end() &&
  176. // Look up CMP0189 in the context where evaluation occurs,
  177. // not where the target was created.
  178. lg->GetPolicyStatus(cmPolicies::CMP0189) != cmPolicies::NEW &&
  179. prop == "LINK_LIBRARIES"_s) {
  180. i = BuiltinTransitiveProperties.end();
  181. }
  182. if (i != BuiltinTransitiveProperties.end()) {
  183. result = i->second;
  184. if (result->Usage != cmGeneratorTarget::UseTo::Compile) {
  185. cmPolicies::PolicyStatus cmp0166 =
  186. lg->GetPolicyStatus(cmPolicies::CMP0166);
  187. if ((cmp0166 == cmPolicies::WARN || cmp0166 == cmPolicies::OLD) &&
  188. (prop == "LINK_DIRECTORIES"_s || prop == "LINK_DEPENDS"_s ||
  189. prop == "LINK_OPTIONS"_s)) {
  190. result->Usage = cmGeneratorTarget::UseTo::Compile;
  191. }
  192. }
  193. } else if (!dagChecker || !dagChecker->IsComputingLinkLibraries()) {
  194. // Honor TRANSITIVE_COMPILE_PROPERTIES and TRANSITIVE_LINK_PROPERTIES
  195. // from the link closure when we are not evaluating the closure itself.
  196. CustomTransitiveProperties const& ctp =
  197. this->GetCustomTransitiveProperties(config, propertyFor);
  198. auto ci = ctp.find(std::string(prop));
  199. if (ci != ctp.end()) {
  200. result = ci->second;
  201. }
  202. }
  203. return result;
  204. }
  205. cmGeneratorTarget::CustomTransitiveProperty::CustomTransitiveProperty(
  206. std::string interfaceName, UseTo usage)
  207. : CustomTransitiveProperty(
  208. cm::make_unique<std::string>(std::move(interfaceName)), usage)
  209. {
  210. }
  211. cmGeneratorTarget::CustomTransitiveProperty::CustomTransitiveProperty(
  212. std::unique_ptr<std::string> interfaceNameBuf, UseTo usage)
  213. : TransitiveProperty{ *interfaceNameBuf, usage }
  214. , InterfaceNameBuf(std::move(interfaceNameBuf))
  215. {
  216. }
  217. void cmGeneratorTarget::CustomTransitiveProperties::Add(cmValue props,
  218. UseTo usage)
  219. {
  220. if (props) {
  221. cmList propsList(*props);
  222. for (std::string p : propsList) {
  223. std::string ip;
  224. static cm::string_view const kINTERFACE_ = "INTERFACE_"_s;
  225. if (cmHasPrefix(p, kINTERFACE_)) {
  226. ip = std::move(p);
  227. p = ip.substr(kINTERFACE_.length());
  228. } else {
  229. ip = cmStrCat(kINTERFACE_, p);
  230. }
  231. this->emplace(std::move(p),
  232. CustomTransitiveProperty(std::move(ip), usage));
  233. }
  234. }
  235. }
  236. cmGeneratorTarget::CustomTransitiveProperties const&
  237. cmGeneratorTarget::GetCustomTransitiveProperties(std::string const& config,
  238. PropertyFor propertyFor) const
  239. {
  240. std::map<std::string, CustomTransitiveProperties>& ctpm =
  241. propertyFor == PropertyFor::Build
  242. ? this->CustomTransitiveBuildPropertiesMap
  243. : this->CustomTransitiveInterfacePropertiesMap;
  244. auto i = ctpm.find(config);
  245. if (i == ctpm.end()) {
  246. CustomTransitiveProperties ctp;
  247. auto addTransitiveProperties = [this, &config, propertyFor,
  248. &ctp](std::string const& tp, UseTo usage) {
  249. // Add transitive properties named by the target itself.
  250. ctp.Add(this->GetProperty(tp), usage);
  251. // Add transitive properties named by the target's link dependencies.
  252. if (propertyFor == PropertyFor::Build) {
  253. for (cmGeneratorTarget const* gt :
  254. this->GetLinkImplementationClosure(config, usage)) {
  255. ctp.Add(gt->GetProperty(tp), usage);
  256. }
  257. } else {
  258. // The set of custom transitive INTERFACE_ properties does not
  259. // depend on the consumer. Use the target as its own head.
  260. cmGeneratorTarget const* headTarget = this;
  261. for (cmGeneratorTarget const* gt :
  262. this->GetLinkInterfaceClosure(config, headTarget, usage)) {
  263. ctp.Add(gt->GetProperty(tp), usage);
  264. }
  265. }
  266. };
  267. addTransitiveProperties("TRANSITIVE_LINK_PROPERTIES", UseTo::Link);
  268. addTransitiveProperties("TRANSITIVE_COMPILE_PROPERTIES", UseTo::Compile);
  269. i = ctpm.emplace(config, std::move(ctp)).first;
  270. }
  271. return i->second;
  272. }