cmGetTargetPropertyCommand.cxx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 "cmListFileCache.h"
  6. #include "cmMakefile.h"
  7. #include "cmPolicies.h"
  8. #include "cmTarget.h"
  9. #include "cmTargetPropertyComputer.h"
  10. #include "cmake.h"
  11. class cmExecutionStatus;
  12. class cmMessenger;
  13. // cmSetTargetPropertyCommand
  14. bool cmGetTargetPropertyCommand::InitialPass(
  15. std::vector<std::string> const& args, cmExecutionStatus&)
  16. {
  17. if (args.size() != 3) {
  18. this->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. if (cmTarget* tgt = this->Makefile->FindTargetToUse(targetName)) {
  26. if (args[2] == "ALIASED_TARGET") {
  27. if (this->Makefile->IsAlias(targetName)) {
  28. prop = tgt->GetName();
  29. prop_exists = true;
  30. }
  31. } else if (!args[2].empty()) {
  32. const char* prop_cstr = nullptr;
  33. cmListFileBacktrace bt = this->Makefile->GetBacktrace();
  34. cmMessenger* messenger = this->Makefile->GetMessenger();
  35. if (cmTargetPropertyComputer::PassesWhitelist(tgt->GetType(), args[2],
  36. messenger, bt)) {
  37. prop_cstr = tgt->GetComputedProperty(args[2], messenger, bt);
  38. if (!prop_cstr) {
  39. prop_cstr = tgt->GetProperty(args[2]);
  40. }
  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. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  51. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0045)) {
  52. case cmPolicies::WARN:
  53. issueMessage = true;
  54. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0045) << "\n";
  55. case cmPolicies::OLD:
  56. break;
  57. case cmPolicies::REQUIRED_IF_USED:
  58. case cmPolicies::REQUIRED_ALWAYS:
  59. case cmPolicies::NEW:
  60. issueMessage = true;
  61. messageType = cmake::FATAL_ERROR;
  62. }
  63. if (issueMessage) {
  64. e << "get_target_property() called with non-existent target \""
  65. << targetName << "\".";
  66. this->Makefile->IssueMessage(messageType, e.str());
  67. if (messageType == cmake::FATAL_ERROR) {
  68. return false;
  69. }
  70. }
  71. }
  72. if (prop_exists) {
  73. this->Makefile->AddDefinition(var, prop.c_str());
  74. return true;
  75. }
  76. this->Makefile->AddDefinition(var, (var + "-NOTFOUND").c_str());
  77. return true;
  78. }