cmGetTargetPropertyCommand.cxx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 "cmGlobalGenerator.h"
  7. #include "cmListFileCache.h"
  8. #include "cmMakefile.h"
  9. #include "cmMessageType.h"
  10. #include "cmPolicies.h"
  11. #include "cmProperty.h"
  12. #include "cmTarget.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" || args[2] == "ALIAS_GLOBAL") {
  28. if (mf.IsAlias(targetName)) {
  29. prop_exists = true;
  30. if (args[2] == "ALIASED_TARGET") {
  31. prop = tgt->GetName();
  32. }
  33. if (args[2] == "ALIAS_GLOBAL") {
  34. prop =
  35. mf.GetGlobalGenerator()->IsAlias(targetName) ? "TRUE" : "FALSE";
  36. }
  37. }
  38. } else if (!args[2].empty()) {
  39. cmProp prop_cstr = nullptr;
  40. cmListFileBacktrace bt = mf.GetBacktrace();
  41. cmMessenger* messenger = mf.GetMessenger();
  42. prop_cstr = tgt->GetComputedProperty(args[2], messenger, bt);
  43. if (!prop_cstr) {
  44. prop_cstr = tgt->GetProperty(args[2]);
  45. }
  46. if (prop_cstr) {
  47. prop = *prop_cstr;
  48. prop_exists = true;
  49. }
  50. }
  51. } else {
  52. bool issueMessage = false;
  53. std::ostringstream e;
  54. MessageType messageType = MessageType::AUTHOR_WARNING;
  55. switch (mf.GetPolicyStatus(cmPolicies::CMP0045)) {
  56. case cmPolicies::WARN:
  57. issueMessage = true;
  58. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0045) << "\n";
  59. case cmPolicies::OLD:
  60. break;
  61. case cmPolicies::REQUIRED_IF_USED:
  62. case cmPolicies::REQUIRED_ALWAYS:
  63. case cmPolicies::NEW:
  64. issueMessage = true;
  65. messageType = MessageType::FATAL_ERROR;
  66. }
  67. if (issueMessage) {
  68. e << "get_target_property() called with non-existent target \""
  69. << targetName << "\".";
  70. mf.IssueMessage(messageType, e.str());
  71. if (messageType == MessageType::FATAL_ERROR) {
  72. return false;
  73. }
  74. }
  75. }
  76. if (prop_exists) {
  77. mf.AddDefinition(var, prop);
  78. return true;
  79. }
  80. mf.AddDefinition(var, var + "-NOTFOUND");
  81. return true;
  82. }