cmTargetPropCommandBase.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #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. } else if ((flags & PROCESS_AFTER) && args[argIndex] == "AFTER") {
  82. if (args.size() < 3) {
  83. this->SetError("called with incorrect number of arguments");
  84. return false;
  85. }
  86. prepend = false;
  87. ++argIndex;
  88. }
  89. if ((flags & PROCESS_REUSE_FROM) && args[argIndex] == "REUSE_FROM") {
  90. if (args.size() != 3) {
  91. this->SetError("called with incorrect number of arguments");
  92. return false;
  93. }
  94. ++argIndex;
  95. this->Target->SetProperty("PRECOMPILE_HEADERS_REUSE_FROM", args[argIndex]);
  96. ++argIndex;
  97. }
  98. this->Property = prop;
  99. while (argIndex < args.size()) {
  100. if (!this->ProcessContentArgs(args, argIndex, prepend, system)) {
  101. return false;
  102. }
  103. }
  104. return true;
  105. }
  106. bool cmTargetPropCommandBase::ProcessContentArgs(
  107. std::vector<std::string> const& args, unsigned int& argIndex, bool prepend,
  108. bool system)
  109. {
  110. std::string const& scope = args[argIndex];
  111. if (scope != "PUBLIC" && scope != "PRIVATE" && scope != "INTERFACE") {
  112. this->SetError("called with invalid arguments");
  113. return false;
  114. }
  115. ++argIndex;
  116. std::vector<std::string> content;
  117. for (unsigned int i = argIndex; i < args.size(); ++i, ++argIndex) {
  118. if (args[i] == "PUBLIC" || args[i] == "PRIVATE" ||
  119. args[i] == "INTERFACE") {
  120. break;
  121. }
  122. content.push_back(args[i]);
  123. }
  124. if (!content.empty()) {
  125. if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
  126. scope != "INTERFACE" && this->Property != "SOURCES") {
  127. this->SetError("may only set INTERFACE properties on INTERFACE targets");
  128. return false;
  129. }
  130. if (this->Target->IsImported() && scope != "INTERFACE") {
  131. this->SetError("may only set INTERFACE properties on IMPORTED targets");
  132. return false;
  133. }
  134. if (this->Target->GetType() == cmStateEnums::UTILITY &&
  135. scope != "PRIVATE") {
  136. this->SetError("may only set PRIVATE properties on custom targets");
  137. return false;
  138. }
  139. }
  140. return this->PopulateTargetProperties(scope, content, prepend, system);
  141. }
  142. bool cmTargetPropCommandBase::PopulateTargetProperties(
  143. const std::string& scope, const std::vector<std::string>& content,
  144. bool prepend, bool system)
  145. {
  146. if (content.empty()) {
  147. return true;
  148. }
  149. if (scope == "PRIVATE" || scope == "PUBLIC") {
  150. if (!this->HandleDirectContent(this->Target, content, prepend, system)) {
  151. return false;
  152. }
  153. }
  154. if (scope == "INTERFACE" || scope == "PUBLIC") {
  155. this->HandleInterfaceContent(this->Target, content, prepend, system);
  156. }
  157. return true;
  158. }
  159. void cmTargetPropCommandBase::HandleInterfaceContent(
  160. cmTarget* tgt, const std::vector<std::string>& content, bool prepend, bool)
  161. {
  162. if (prepend) {
  163. const std::string propName = std::string("INTERFACE_") + this->Property;
  164. cmValue propValue = tgt->GetProperty(propName);
  165. const std::string totalContent =
  166. this->Join(content) + (propValue ? (";" + *propValue) : std::string());
  167. tgt->SetProperty(propName, totalContent);
  168. } else {
  169. tgt->AppendProperty("INTERFACE_" + this->Property, this->Join(content));
  170. }
  171. }