cmGetTargetPropertyCommand.cxx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmGetTargetPropertyCommand.h"
  4. #include <sstream>
  5. #include "cmExecutionStatus.h"
  6. #include "cmListFileCache.h"
  7. #include "cmMakefile.h"
  8. #include "cmMessageType.h"
  9. #include "cmPolicies.h"
  10. #include "cmProperty.h"
  11. #include "cmTarget.h"
  12. #include "cmTargetPropertyComputer.h"
  13. class cmMessenger;
  14. bool cmGetTargetPropertyCommand(std::vector<std::string> const& args,
  15. cmExecutionStatus& status)
  16. {
  17. if (args.size() != 3) {
  18. status.SetError("called with incorrect number of arguments");
  19. return false;
  20. }
  21. std::string const& var = args[0];
  22. std::string const& targetName = args[1];
  23. std::string prop;
  24. bool prop_exists = false;
  25. cmMakefile& mf = status.GetMakefile();
  26. if (cmTarget* tgt = mf.FindTargetToUse(targetName)) {
  27. if (args[2] == "ALIASED_TARGET") {
  28. if (mf.IsAlias(targetName)) {
  29. prop = tgt->GetName();
  30. prop_exists = true;
  31. }
  32. } else if (!args[2].empty()) {
  33. cmProp prop_cstr = nullptr;
  34. cmListFileBacktrace bt = mf.GetBacktrace();
  35. cmMessenger* messenger = mf.GetMessenger();
  36. if (cmTargetPropertyComputer::PassesWhitelist(tgt->GetType(), args[2],
  37. messenger, bt)) {
  38. prop_cstr = tgt->GetComputedProperty(args[2], messenger, bt);
  39. if (!prop_cstr) {
  40. prop_cstr = tgt->GetProperty(args[2]);
  41. }
  42. }
  43. if (prop_cstr) {
  44. prop = *prop_cstr;
  45. prop_exists = true;
  46. }
  47. }
  48. } else {
  49. bool issueMessage = false;
  50. std::ostringstream e;
  51. MessageType messageType = MessageType::AUTHOR_WARNING;
  52. switch (mf.GetPolicyStatus(cmPolicies::CMP0045)) {
  53. case cmPolicies::WARN:
  54. issueMessage = true;
  55. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0045) << "\n";
  56. case cmPolicies::OLD:
  57. break;
  58. case cmPolicies::REQUIRED_IF_USED:
  59. case cmPolicies::REQUIRED_ALWAYS:
  60. case cmPolicies::NEW:
  61. issueMessage = true;
  62. messageType = MessageType::FATAL_ERROR;
  63. }
  64. if (issueMessage) {
  65. e << "get_target_property() called with non-existent target \""
  66. << targetName << "\".";
  67. mf.IssueMessage(messageType, e.str());
  68. if (messageType == MessageType::FATAL_ERROR) {
  69. return false;
  70. }
  71. }
  72. }
  73. if (prop_exists) {
  74. mf.AddDefinition(var, prop);
  75. return true;
  76. }
  77. mf.AddDefinition(var, var + "-NOTFOUND");
  78. return true;
  79. }