cmTargetPropCommandBase.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2013 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmTargetPropCommandBase.h"
  11. #include "cmGlobalGenerator.h"
  12. //----------------------------------------------------------------------------
  13. bool cmTargetPropCommandBase
  14. ::HandleArguments(std::vector<std::string> const& args, const char *prop,
  15. ArgumentFlags flags)
  16. {
  17. if(args.size() < 2)
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. // Lookup the target for which libraries are specified.
  23. if (this->Makefile->IsAlias(args[0]))
  24. {
  25. this->SetError("can not be used on an ALIAS target.");
  26. return false;
  27. }
  28. this->Target =
  29. this->Makefile->GetCMakeInstance()
  30. ->GetGlobalGenerator()->FindTarget(0, args[0].c_str());
  31. if(!this->Target)
  32. {
  33. this->Target = this->Makefile->FindTargetToUse(args[0]);
  34. }
  35. if(!this->Target)
  36. {
  37. this->HandleMissingTarget(args[0]);
  38. return false;
  39. }
  40. if ((this->Target->GetType() != cmTarget::SHARED_LIBRARY)
  41. && (this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  42. && (this->Target->GetType() != cmTarget::OBJECT_LIBRARY)
  43. && (this->Target->GetType() != cmTarget::MODULE_LIBRARY)
  44. && (this->Target->GetType() != cmTarget::INTERFACE_LIBRARY)
  45. && (this->Target->GetType() != cmTarget::EXECUTABLE))
  46. {
  47. this->SetError("called with non-compilable target type");
  48. return false;
  49. }
  50. bool system = false;
  51. unsigned int argIndex = 1;
  52. if ((flags & PROCESS_SYSTEM) && args[argIndex] == "SYSTEM")
  53. {
  54. if (args.size() < 3)
  55. {
  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. {
  65. if (args.size() < 3)
  66. {
  67. this->SetError("called with incorrect number of arguments");
  68. return false;
  69. }
  70. prepend = true;
  71. ++argIndex;
  72. }
  73. this->Property = prop;
  74. while (argIndex < args.size())
  75. {
  76. if (!this->ProcessContentArgs(args, argIndex, prepend, system))
  77. {
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. //----------------------------------------------------------------------------
  84. bool cmTargetPropCommandBase
  85. ::ProcessContentArgs(std::vector<std::string> const& args,
  86. unsigned int &argIndex, bool prepend, bool system)
  87. {
  88. const std::string scope = args[argIndex];
  89. if(scope != "PUBLIC"
  90. && scope != "PRIVATE"
  91. && scope != "INTERFACE" )
  92. {
  93. this->SetError("called with invalid arguments");
  94. return false;
  95. }
  96. if(this->Target->IsImported())
  97. {
  98. this->HandleImportedTarget(args[0]);
  99. return false;
  100. }
  101. if (this->Target->GetType() == cmTarget::INTERFACE_LIBRARY
  102. && scope != "INTERFACE")
  103. {
  104. this->SetError("may only be set INTERFACE properties on INTERFACE "
  105. "targets");
  106. return false;
  107. }
  108. ++argIndex;
  109. std::vector<std::string> content;
  110. for(unsigned int i=argIndex; i < args.size(); ++i, ++argIndex)
  111. {
  112. if(args[i] == "PUBLIC"
  113. || args[i] == "PRIVATE"
  114. || args[i] == "INTERFACE" )
  115. {
  116. this->PopulateTargetProperies(scope, content, prepend, system);
  117. return true;
  118. }
  119. content.push_back(args[i]);
  120. }
  121. this->PopulateTargetProperies(scope, content, prepend, system);
  122. return true;
  123. }
  124. //----------------------------------------------------------------------------
  125. void cmTargetPropCommandBase
  126. ::PopulateTargetProperies(const std::string &scope,
  127. const std::vector<std::string> &content,
  128. bool prepend, bool system)
  129. {
  130. if (scope == "PRIVATE" || scope == "PUBLIC")
  131. {
  132. this->HandleDirectContent(this->Target, content, prepend, system);
  133. }
  134. if (scope == "INTERFACE" || scope == "PUBLIC")
  135. {
  136. this->HandleInterfaceContent(this->Target, content, prepend, system);
  137. }
  138. }
  139. //----------------------------------------------------------------------------
  140. void cmTargetPropCommandBase::HandleInterfaceContent(cmTarget *tgt,
  141. const std::vector<std::string> &content,
  142. bool prepend, bool)
  143. {
  144. if (prepend)
  145. {
  146. const std::string propName = std::string("INTERFACE_") + this->Property;
  147. const char *propValue = tgt->GetProperty(propName.c_str());
  148. const std::string totalContent = this->Join(content) + (propValue
  149. ? std::string(";") + propValue
  150. : std::string());
  151. tgt->SetProperty(propName.c_str(), totalContent.c_str());
  152. }
  153. else
  154. {
  155. tgt->AppendProperty(("INTERFACE_" + this->Property).c_str(),
  156. this->Join(content).c_str());
  157. }
  158. }