cmAddExecutableCommand.cxx 6.6 KB

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