cmGetTargetPropertyCommand.cxx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "cmMakefile.h"
  8. #include "cmMessageType.h"
  9. #include "cmPolicies.h"
  10. #include "cmTarget.h"
  11. #include "cmValue.h"
  12. bool cmGetTargetPropertyCommand(std::vector<std::string> const& args,
  13. cmExecutionStatus& status)
  14. {
  15. if (args.size() != 3) {
  16. status.SetError("called with incorrect number of arguments");
  17. return false;
  18. }
  19. std::string const& var = args[0];
  20. std::string const& targetName = args[1];
  21. std::string prop;
  22. bool prop_exists = false;
  23. cmMakefile& mf = status.GetMakefile();
  24. if (cmTarget* tgt = mf.FindTargetToUse(targetName)) {
  25. if (args[2] == "ALIASED_TARGET" || args[2] == "ALIAS_GLOBAL") {
  26. if (mf.IsAlias(targetName)) {
  27. prop_exists = true;
  28. if (args[2] == "ALIASED_TARGET") {
  29. prop = tgt->GetName();
  30. }
  31. if (args[2] == "ALIAS_GLOBAL") {
  32. prop =
  33. mf.GetGlobalGenerator()->IsAlias(targetName) ? "TRUE" : "FALSE";
  34. }
  35. }
  36. } else if (!args[2].empty()) {
  37. cmValue prop_cstr = nullptr;
  38. prop_cstr = tgt->GetComputedProperty(args[2], mf);
  39. if (!prop_cstr) {
  40. prop_cstr = tgt->GetProperty(args[2]);
  41. }
  42. if (prop_cstr) {
  43. prop = *prop_cstr;
  44. prop_exists = true;
  45. }
  46. }
  47. } else {
  48. bool issueMessage = false;
  49. std::ostringstream e;
  50. MessageType messageType = MessageType::AUTHOR_WARNING;
  51. switch (mf.GetPolicyStatus(cmPolicies::CMP0045)) {
  52. case cmPolicies::WARN:
  53. issueMessage = true;
  54. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0045) << "\n";
  55. CM_FALLTHROUGH;
  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. break;
  64. }
  65. if (issueMessage) {
  66. e << "get_target_property() called with non-existent target \""
  67. << targetName << "\".";
  68. mf.IssueMessage(messageType, e.str());
  69. if (messageType == MessageType::FATAL_ERROR) {
  70. return false;
  71. }
  72. }
  73. }
  74. if (prop_exists) {
  75. mf.AddDefinition(var, prop);
  76. return true;
  77. }
  78. mf.AddDefinition(var, var + "-NOTFOUND");
  79. return true;
  80. }