cmTargetPropCommandBase.cxx 4.9 KB

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