cmTargetPropCommandBase.cxx 4.1 KB

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