cmGeneratorTarget_TransitiveProperty.cxx 11 KB

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