cmAddExecutableCommand.cxx 6.5 KB

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