cmTargetPropCommandBase.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "cmTargetPropCommandBase.h"
  4. #include "cmGlobalGenerator.h"
  5. #include "cmMakefile.h"
  6. #include "cmStateTypes.h"
  7. #include "cmTarget.h"
  8. #include "cmake.h"
  9. bool cmTargetPropCommandBase::HandleArguments(
  10. std::vector<std::string> const& args, const std::string& prop,
  11. ArgumentFlags flags)
  12. {
  13. if (args.size() < 2) {
  14. this->SetError("called with incorrect number of arguments");
  15. return false;
  16. }
  17. if (this->Makefile->IsAlias(args[0])) {
  18. this->SetError("can not be used on an ALIAS target.");
  19. return false;
  20. }
  21. // Lookup the target for which property-values are specified.
  22. this->Target =
  23. this->Makefile->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(
  24. args[0]);
  25. if (!this->Target) {
  26. this->Target = this->Makefile->FindTargetToUse(args[0]);
  27. }
  28. if (!this->Target) {
  29. this->HandleMissingTarget(args[0]);
  30. return false;
  31. }
  32. if ((this->Target->GetType() != cmStateEnums::SHARED_LIBRARY) &&
  33. (this->Target->GetType() != cmStateEnums::STATIC_LIBRARY) &&
  34. (this->Target->GetType() != cmStateEnums::OBJECT_LIBRARY) &&
  35. (this->Target->GetType() != cmStateEnums::MODULE_LIBRARY) &&
  36. (this->Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) &&
  37. (this->Target->GetType() != cmStateEnums::EXECUTABLE)) {
  38. this->SetError("called with non-compilable target type");
  39. return false;
  40. }
  41. bool system = false;
  42. unsigned int argIndex = 1;
  43. if ((flags & PROCESS_SYSTEM) && args[argIndex] == "SYSTEM") {
  44. if (args.size() < 3) {
  45. this->SetError("called with incorrect number of arguments");
  46. return false;
  47. }
  48. system = true;
  49. ++argIndex;
  50. }
  51. bool prepend = false;
  52. if ((flags & PROCESS_BEFORE) && args[argIndex] == "BEFORE") {
  53. if (args.size() < 3) {
  54. this->SetError("called with incorrect number of arguments");
  55. return false;
  56. }
  57. prepend = true;
  58. ++argIndex;
  59. }
  60. this->Property = prop;
  61. while (argIndex < args.size()) {
  62. if (!this->ProcessContentArgs(args, argIndex, prepend, system)) {
  63. return false;
  64. }
  65. }
  66. return true;
  67. }
  68. bool cmTargetPropCommandBase::ProcessContentArgs(
  69. std::vector<std::string> const& args, unsigned int& argIndex, bool prepend,
  70. bool system)
  71. {
  72. std::string const& scope = args[argIndex];
  73. if (scope != "PUBLIC" && scope != "PRIVATE" && scope != "INTERFACE") {
  74. this->SetError("called with invalid arguments");
  75. return false;
  76. }
  77. if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
  78. scope != "INTERFACE") {
  79. this->SetError("may only set INTERFACE properties on INTERFACE targets");
  80. return false;
  81. }
  82. if (this->Target->IsImported() && scope != "INTERFACE") {
  83. this->SetError("may only set INTERFACE properties on IMPORTED targets");
  84. return false;
  85. }
  86. ++argIndex;
  87. std::vector<std::string> content;
  88. for (unsigned int i = argIndex; i < args.size(); ++i, ++argIndex) {
  89. if (args[i] == "PUBLIC" || args[i] == "PRIVATE" ||
  90. args[i] == "INTERFACE") {
  91. return this->PopulateTargetProperies(scope, content, prepend, system);
  92. }
  93. content.push_back(args[i]);
  94. }
  95. return this->PopulateTargetProperies(scope, content, prepend, system);
  96. }
  97. bool cmTargetPropCommandBase::PopulateTargetProperies(
  98. const std::string& scope, const std::vector<std::string>& content,
  99. bool prepend, bool system)
  100. {
  101. if (scope == "PRIVATE" || scope == "PUBLIC") {
  102. if (!this->HandleDirectContent(this->Target, content, prepend, system)) {
  103. return false;
  104. }
  105. }
  106. if (scope == "INTERFACE" || scope == "PUBLIC") {
  107. this->HandleInterfaceContent(this->Target, content, prepend, system);
  108. }
  109. return true;
  110. }
  111. void cmTargetPropCommandBase::HandleInterfaceContent(
  112. cmTarget* tgt, const std::vector<std::string>& content, bool prepend, bool)
  113. {
  114. if (prepend) {
  115. const std::string propName = std::string("INTERFACE_") + this->Property;
  116. const char* propValue = tgt->GetProperty(propName);
  117. const std::string totalContent = this->Join(content) +
  118. (propValue ? std::string(";") + propValue : std::string());
  119. tgt->SetProperty(propName, totalContent.c_str());
  120. } else {
  121. tgt->AppendProperty("INTERFACE_" + this->Property,
  122. this->Join(content).c_str());
  123. }
  124. }