cmTargetPropCommandBase.cxx 5.6 KB

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