cmTargetPropCommandBase.cxx 5.4 KB

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