cmTargetPropCommandBase.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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::EXECUTABLE) &&
  33. (this->Target->GetType() != cmStateEnums::STATIC_LIBRARY) &&
  34. (this->Target->GetType() != cmStateEnums::SHARED_LIBRARY) &&
  35. (this->Target->GetType() != cmStateEnums::MODULE_LIBRARY) &&
  36. (this->Target->GetType() != cmStateEnums::OBJECT_LIBRARY) &&
  37. (this->Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) &&
  38. (this->Target->GetType() != cmStateEnums::UNKNOWN_LIBRARY)) {
  39. this->SetError("called with non-compilable target type");
  40. return false;
  41. }
  42. bool system = false;
  43. unsigned int argIndex = 1;
  44. if ((flags & PROCESS_SYSTEM) && args[argIndex] == "SYSTEM") {
  45. if (args.size() < 3) {
  46. this->SetError("called with incorrect number of arguments");
  47. return false;
  48. }
  49. system = true;
  50. ++argIndex;
  51. }
  52. bool prepend = false;
  53. if ((flags & PROCESS_BEFORE) && args[argIndex] == "BEFORE") {
  54. if (args.size() < 3) {
  55. this->SetError("called with incorrect number of arguments");
  56. return false;
  57. }
  58. prepend = true;
  59. ++argIndex;
  60. }
  61. if ((flags & PROCESS_REUSE_FROM) && args[argIndex] == "REUSE_FROM") {
  62. if (args.size() != 3) {
  63. this->SetError("called with incorrect number of arguments");
  64. return false;
  65. }
  66. ++argIndex;
  67. this->Target->SetProperty("PRECOMPILE_HEADERS_REUSE_FROM",
  68. args[argIndex].c_str());
  69. ++argIndex;
  70. }
  71. this->Property = prop;
  72. while (argIndex < args.size()) {
  73. if (!this->ProcessContentArgs(args, argIndex, prepend, system)) {
  74. return false;
  75. }
  76. }
  77. return true;
  78. }
  79. bool cmTargetPropCommandBase::ProcessContentArgs(
  80. std::vector<std::string> const& args, unsigned int& argIndex, bool prepend,
  81. bool system)
  82. {
  83. std::string const& scope = args[argIndex];
  84. if (scope != "PUBLIC" && scope != "PRIVATE" && scope != "INTERFACE") {
  85. this->SetError("called with invalid arguments");
  86. return false;
  87. }
  88. ++argIndex;
  89. std::vector<std::string> content;
  90. for (unsigned int i = argIndex; i < args.size(); ++i, ++argIndex) {
  91. if (args[i] == "PUBLIC" || args[i] == "PRIVATE" ||
  92. args[i] == "INTERFACE") {
  93. break;
  94. }
  95. content.push_back(args[i]);
  96. }
  97. if (!content.empty()) {
  98. if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
  99. scope != "INTERFACE") {
  100. this->SetError("may only set INTERFACE properties on INTERFACE targets");
  101. return false;
  102. }
  103. if (this->Target->IsImported() && scope != "INTERFACE") {
  104. this->SetError("may only set INTERFACE properties on IMPORTED targets");
  105. return false;
  106. }
  107. }
  108. return this->PopulateTargetProperies(scope, content, prepend, system);
  109. }
  110. bool cmTargetPropCommandBase::PopulateTargetProperies(
  111. const std::string& scope, const std::vector<std::string>& content,
  112. bool prepend, bool system)
  113. {
  114. if (content.empty()) {
  115. return true;
  116. }
  117. if (scope == "PRIVATE" || scope == "PUBLIC") {
  118. if (!this->HandleDirectContent(this->Target, content, prepend, system)) {
  119. return false;
  120. }
  121. }
  122. if (scope == "INTERFACE" || scope == "PUBLIC") {
  123. this->HandleInterfaceContent(this->Target, content, prepend, system);
  124. }
  125. return true;
  126. }
  127. void cmTargetPropCommandBase::HandleInterfaceContent(
  128. cmTarget* tgt, const std::vector<std::string>& content, bool prepend, bool)
  129. {
  130. if (prepend) {
  131. const std::string propName = std::string("INTERFACE_") + this->Property;
  132. const char* propValue = tgt->GetProperty(propName);
  133. const std::string totalContent = this->Join(content) +
  134. (propValue ? std::string(";") + propValue : std::string());
  135. tgt->SetProperty(propName, totalContent.c_str());
  136. } else {
  137. tgt->AppendProperty("INTERFACE_" + this->Property,
  138. this->Join(content).c_str());
  139. }
  140. }