1
0

cmAddExecutableCommand.cxx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  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 "cmAddExecutableCommand.h"
  11. // cmExecutableCommand
  12. bool cmAddExecutableCommand
  13. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  14. {
  15. if(args.size() < 2 )
  16. {
  17. this->SetError("called with incorrect number of arguments");
  18. return false;
  19. }
  20. std::vector<std::string>::const_iterator s = args.begin();
  21. std::string exename = *s;
  22. ++s;
  23. bool use_win32 = false;
  24. bool use_macbundle = false;
  25. bool excludeFromAll = false;
  26. bool importTarget = false;
  27. bool importGlobal = false;
  28. bool isAlias = false;
  29. while ( s != args.end() )
  30. {
  31. if (*s == "WIN32")
  32. {
  33. ++s;
  34. use_win32 = true;
  35. }
  36. else if ( *s == "MACOSX_BUNDLE" )
  37. {
  38. ++s;
  39. use_macbundle = true;
  40. }
  41. else if(*s == "EXCLUDE_FROM_ALL")
  42. {
  43. ++s;
  44. excludeFromAll = true;
  45. }
  46. else if(*s == "IMPORTED")
  47. {
  48. ++s;
  49. importTarget = true;
  50. }
  51. else if(importTarget && *s == "GLOBAL")
  52. {
  53. ++s;
  54. importGlobal = true;
  55. }
  56. else if(*s == "ALIAS")
  57. {
  58. ++s;
  59. isAlias = true;
  60. }
  61. else
  62. {
  63. break;
  64. }
  65. }
  66. // Special modifiers are not allowed with IMPORTED signature.
  67. if(importTarget
  68. && (use_win32 || use_macbundle || excludeFromAll))
  69. {
  70. if(use_win32)
  71. {
  72. this->SetError("may not be given WIN32 for an IMPORTED target.");
  73. }
  74. else if(use_macbundle)
  75. {
  76. this->SetError(
  77. "may not be given MACOSX_BUNDLE for an IMPORTED target.");
  78. }
  79. else // if(excludeFromAll)
  80. {
  81. this->SetError(
  82. "may not be given EXCLUDE_FROM_ALL for an IMPORTED target.");
  83. }
  84. return false;
  85. }
  86. if (isAlias)
  87. {
  88. if(!cmGeneratorExpression::IsValidTargetName(exename.c_str()))
  89. {
  90. this->SetError(("Invalid name for ALIAS: " + exename).c_str());
  91. return false;
  92. }
  93. if(excludeFromAll)
  94. {
  95. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  96. return false;
  97. }
  98. if(importTarget || importGlobal)
  99. {
  100. this->SetError("IMPORTED with ALIAS is not allowed.");
  101. return false;
  102. }
  103. if(args.size() != 3)
  104. {
  105. cmOStringStream e;
  106. e << "ALIAS requires exactly one target argument.";
  107. this->SetError(e.str().c_str());
  108. return false;
  109. }
  110. const char *aliasedName = s->c_str();
  111. if(this->Makefile->IsAlias(aliasedName))
  112. {
  113. cmOStringStream e;
  114. e << "cannot create ALIAS target \"" << exename
  115. << "\" because target \"" << aliasedName << "\" is itself an ALIAS.";
  116. this->SetError(e.str().c_str());
  117. return false;
  118. }
  119. cmTarget *aliasedTarget =
  120. this->Makefile->FindTargetToUse(aliasedName, true);
  121. if(!aliasedTarget)
  122. {
  123. cmOStringStream e;
  124. e << "cannot create ALIAS target \"" << exename
  125. << "\" because target \"" << aliasedName << "\" does not already "
  126. "exist.";
  127. this->SetError(e.str().c_str());
  128. return false;
  129. }
  130. cmTarget::TargetType type = aliasedTarget->GetType();
  131. if(type != cmTarget::EXECUTABLE)
  132. {
  133. cmOStringStream e;
  134. e << "cannot create ALIAS target \"" << exename
  135. << "\" because target \"" << aliasedName << "\" is not an "
  136. "executable.";
  137. this->SetError(e.str().c_str());
  138. return false;
  139. }
  140. if(aliasedTarget->IsImported())
  141. {
  142. cmOStringStream e;
  143. e << "cannot create ALIAS target \"" << exename
  144. << "\" because target \"" << aliasedName << "\" is IMPORTED.";
  145. this->SetError(e.str().c_str());
  146. return false;
  147. }
  148. this->Makefile->AddAlias(exename.c_str(), aliasedTarget);
  149. return true;
  150. }
  151. // Handle imported target creation.
  152. if(importTarget)
  153. {
  154. // Make sure the target does not already exist.
  155. if(this->Makefile->FindTargetToUse(exename.c_str()))
  156. {
  157. cmOStringStream e;
  158. e << "cannot create imported target \"" << exename
  159. << "\" because another target with the same name already exists.";
  160. this->SetError(e.str().c_str());
  161. return false;
  162. }
  163. // Create the imported target.
  164. this->Makefile->AddImportedTarget(exename.c_str(), cmTarget::EXECUTABLE,
  165. importGlobal);
  166. return true;
  167. }
  168. // Enforce name uniqueness.
  169. {
  170. std::string msg;
  171. if(!this->Makefile->EnforceUniqueName(exename, msg))
  172. {
  173. this->SetError(msg.c_str());
  174. return false;
  175. }
  176. }
  177. if (s == args.end())
  178. {
  179. this->SetError
  180. ("called with incorrect number of arguments, no sources provided");
  181. return false;
  182. }
  183. std::vector<std::string> srclists(s, args.end());
  184. cmTarget* tgt = this->Makefile->AddExecutable(exename.c_str(), srclists,
  185. excludeFromAll);
  186. if ( use_win32 )
  187. {
  188. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  189. }
  190. if ( use_macbundle)
  191. {
  192. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  193. }
  194. return true;
  195. }