cmAddExecutableCommand.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. bool nameOk = cmGeneratorExpression::IsValidTargetName(exename) &&
  67. !cmGlobalGenerator::IsReservedTarget(exename);
  68. if (nameOk && !importTarget && !isAlias)
  69. {
  70. nameOk = exename.find(":") == std::string::npos;
  71. }
  72. if (!nameOk)
  73. {
  74. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  75. std::ostringstream e;
  76. bool issueMessage = false;
  77. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0037))
  78. {
  79. case cmPolicies::WARN:
  80. e << (this->Makefile->GetPolicies()
  81. ->GetPolicyWarning(cmPolicies::CMP0037)) << "\n";
  82. issueMessage = true;
  83. case cmPolicies::OLD:
  84. break;
  85. case cmPolicies::NEW:
  86. case cmPolicies::REQUIRED_IF_USED:
  87. case cmPolicies::REQUIRED_ALWAYS:
  88. issueMessage = true;
  89. messageType = cmake::FATAL_ERROR;
  90. }
  91. if (issueMessage)
  92. {
  93. e << "The target name \"" << exename <<
  94. "\" is reserved or not valid for certain "
  95. "CMake features, such as generator expressions, and may result "
  96. "in undefined behavior.";
  97. this->Makefile->IssueMessage(messageType, e.str());
  98. if (messageType == cmake::FATAL_ERROR)
  99. {
  100. return false;
  101. }
  102. }
  103. }
  104. // Special modifiers are not allowed with IMPORTED signature.
  105. if(importTarget
  106. && (use_win32 || use_macbundle || excludeFromAll))
  107. {
  108. if(use_win32)
  109. {
  110. this->SetError("may not be given WIN32 for an IMPORTED target.");
  111. }
  112. else if(use_macbundle)
  113. {
  114. this->SetError(
  115. "may not be given MACOSX_BUNDLE for an IMPORTED target.");
  116. }
  117. else // if(excludeFromAll)
  118. {
  119. this->SetError(
  120. "may not be given EXCLUDE_FROM_ALL for an IMPORTED target.");
  121. }
  122. return false;
  123. }
  124. if (isAlias)
  125. {
  126. if(!cmGeneratorExpression::IsValidTargetName(exename))
  127. {
  128. this->SetError("Invalid name for ALIAS: " + exename);
  129. return false;
  130. }
  131. if(excludeFromAll)
  132. {
  133. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  134. return false;
  135. }
  136. if(importTarget || importGlobal)
  137. {
  138. this->SetError("IMPORTED with ALIAS is not allowed.");
  139. return false;
  140. }
  141. if(args.size() != 3)
  142. {
  143. std::ostringstream e;
  144. e << "ALIAS requires exactly one target argument.";
  145. this->SetError(e.str());
  146. return false;
  147. }
  148. const char *aliasedName = s->c_str();
  149. if(this->Makefile->IsAlias(aliasedName))
  150. {
  151. std::ostringstream e;
  152. e << "cannot create ALIAS target \"" << exename
  153. << "\" because target \"" << aliasedName << "\" is itself an ALIAS.";
  154. this->SetError(e.str());
  155. return false;
  156. }
  157. cmTarget *aliasedTarget =
  158. this->Makefile->FindTargetToUse(aliasedName, true);
  159. if(!aliasedTarget)
  160. {
  161. std::ostringstream e;
  162. e << "cannot create ALIAS target \"" << exename
  163. << "\" because target \"" << aliasedName << "\" does not already "
  164. "exist.";
  165. this->SetError(e.str());
  166. return false;
  167. }
  168. cmTarget::TargetType type = aliasedTarget->GetType();
  169. if(type != cmTarget::EXECUTABLE)
  170. {
  171. std::ostringstream e;
  172. e << "cannot create ALIAS target \"" << exename
  173. << "\" because target \"" << aliasedName << "\" is not an "
  174. "executable.";
  175. this->SetError(e.str());
  176. return false;
  177. }
  178. if(aliasedTarget->IsImported())
  179. {
  180. std::ostringstream e;
  181. e << "cannot create ALIAS target \"" << exename
  182. << "\" because target \"" << aliasedName << "\" is IMPORTED.";
  183. this->SetError(e.str());
  184. return false;
  185. }
  186. this->Makefile->AddAlias(exename, aliasedTarget);
  187. return true;
  188. }
  189. // Handle imported target creation.
  190. if(importTarget)
  191. {
  192. // Make sure the target does not already exist.
  193. if(this->Makefile->FindTargetToUse(exename))
  194. {
  195. std::ostringstream e;
  196. e << "cannot create imported target \"" << exename
  197. << "\" because another target with the same name already exists.";
  198. this->SetError(e.str());
  199. return false;
  200. }
  201. // Create the imported target.
  202. this->Makefile->AddImportedTarget(exename, cmTarget::EXECUTABLE,
  203. importGlobal);
  204. return true;
  205. }
  206. // Enforce name uniqueness.
  207. {
  208. std::string msg;
  209. if(!this->Makefile->EnforceUniqueName(exename, msg))
  210. {
  211. this->SetError(msg);
  212. return false;
  213. }
  214. }
  215. if (s == args.end())
  216. {
  217. this->SetError
  218. ("called with incorrect number of arguments, no sources provided");
  219. return false;
  220. }
  221. std::vector<std::string> srclists(s, args.end());
  222. cmTarget* tgt = this->Makefile->AddExecutable(exename.c_str(), srclists,
  223. excludeFromAll);
  224. if ( use_win32 )
  225. {
  226. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  227. }
  228. if ( use_macbundle)
  229. {
  230. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  231. }
  232. return true;
  233. }