cmTargetPropCommandBase.cxx 4.9 KB

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