cmTargetPropCommandBase.cxx 5.2 KB

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