cmAddExecutableCommand.cxx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmAddExecutableCommand.h"
  4. // cmExecutableCommand
  5. bool cmAddExecutableCommand::InitialPass(std::vector<std::string> const& args,
  6. cmExecutionStatus&)
  7. {
  8. if (args.size() < 2) {
  9. this->SetError("called with incorrect number of arguments");
  10. return false;
  11. }
  12. std::vector<std::string>::const_iterator s = args.begin();
  13. std::string exename = *s;
  14. ++s;
  15. bool use_win32 = false;
  16. bool use_macbundle = false;
  17. bool excludeFromAll = false;
  18. bool importTarget = false;
  19. bool importGlobal = false;
  20. bool isAlias = false;
  21. while (s != args.end()) {
  22. if (*s == "WIN32") {
  23. ++s;
  24. use_win32 = true;
  25. } else if (*s == "MACOSX_BUNDLE") {
  26. ++s;
  27. use_macbundle = true;
  28. } else if (*s == "EXCLUDE_FROM_ALL") {
  29. ++s;
  30. excludeFromAll = true;
  31. } else if (*s == "IMPORTED") {
  32. ++s;
  33. importTarget = true;
  34. } else if (importTarget && *s == "GLOBAL") {
  35. ++s;
  36. importGlobal = true;
  37. } else if (*s == "ALIAS") {
  38. ++s;
  39. isAlias = true;
  40. } else {
  41. break;
  42. }
  43. }
  44. bool nameOk = cmGeneratorExpression::IsValidTargetName(exename) &&
  45. !cmGlobalGenerator::IsReservedTarget(exename);
  46. if (nameOk && !importTarget && !isAlias) {
  47. nameOk = exename.find(":") == std::string::npos;
  48. }
  49. if (!nameOk) {
  50. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  51. std::ostringstream e;
  52. bool issueMessage = false;
  53. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0037)) {
  54. case cmPolicies::WARN:
  55. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0037) << "\n";
  56. issueMessage = true;
  57. case cmPolicies::OLD:
  58. break;
  59. case cmPolicies::NEW:
  60. case cmPolicies::REQUIRED_IF_USED:
  61. case cmPolicies::REQUIRED_ALWAYS:
  62. issueMessage = true;
  63. messageType = cmake::FATAL_ERROR;
  64. }
  65. if (issueMessage) {
  66. /* clang-format off */
  67. e << "The target name \"" << exename <<
  68. "\" is reserved or not valid for certain "
  69. "CMake features, such as generator expressions, and may result "
  70. "in undefined behavior.";
  71. /* clang-format on */
  72. this->Makefile->IssueMessage(messageType, e.str());
  73. if (messageType == cmake::FATAL_ERROR) {
  74. return false;
  75. }
  76. }
  77. }
  78. // Special modifiers are not allowed with IMPORTED signature.
  79. if (importTarget && (use_win32 || use_macbundle || excludeFromAll)) {
  80. if (use_win32) {
  81. this->SetError("may not be given WIN32 for an IMPORTED target.");
  82. } else if (use_macbundle) {
  83. this->SetError("may not be given MACOSX_BUNDLE for an IMPORTED target.");
  84. } else // if(excludeFromAll)
  85. {
  86. this->SetError(
  87. "may not be given EXCLUDE_FROM_ALL for an IMPORTED target.");
  88. }
  89. return false;
  90. }
  91. if (isAlias) {
  92. if (!cmGeneratorExpression::IsValidTargetName(exename)) {
  93. this->SetError("Invalid name for ALIAS: " + exename);
  94. return false;
  95. }
  96. if (excludeFromAll) {
  97. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  98. return false;
  99. }
  100. if (importTarget || importGlobal) {
  101. this->SetError("IMPORTED with ALIAS is not allowed.");
  102. return false;
  103. }
  104. if (args.size() != 3) {
  105. std::ostringstream e;
  106. e << "ALIAS requires exactly one target argument.";
  107. this->SetError(e.str());
  108. return false;
  109. }
  110. const char* aliasedName = s->c_str();
  111. if (this->Makefile->IsAlias(aliasedName)) {
  112. std::ostringstream e;
  113. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  114. << aliasedName << "\" is itself an ALIAS.";
  115. this->SetError(e.str());
  116. return false;
  117. }
  118. cmTarget* aliasedTarget =
  119. this->Makefile->FindTargetToUse(aliasedName, true);
  120. if (!aliasedTarget) {
  121. std::ostringstream e;
  122. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  123. << aliasedName << "\" does not already "
  124. "exist.";
  125. this->SetError(e.str());
  126. return false;
  127. }
  128. cmState::TargetType type = aliasedTarget->GetType();
  129. if (type != cmState::EXECUTABLE) {
  130. std::ostringstream e;
  131. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  132. << aliasedName << "\" is not an "
  133. "executable.";
  134. this->SetError(e.str());
  135. return false;
  136. }
  137. if (aliasedTarget->IsImported()) {
  138. std::ostringstream e;
  139. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  140. << aliasedName << "\" is IMPORTED.";
  141. this->SetError(e.str());
  142. return false;
  143. }
  144. this->Makefile->AddAlias(exename, aliasedName);
  145. return true;
  146. }
  147. // Handle imported target creation.
  148. if (importTarget) {
  149. // Make sure the target does not already exist.
  150. if (this->Makefile->FindTargetToUse(exename)) {
  151. std::ostringstream e;
  152. e << "cannot create imported target \"" << exename
  153. << "\" because another target with the same name already exists.";
  154. this->SetError(e.str());
  155. return false;
  156. }
  157. // Create the imported target.
  158. this->Makefile->AddImportedTarget(exename, cmState::EXECUTABLE,
  159. importGlobal);
  160. return true;
  161. }
  162. // Enforce name uniqueness.
  163. {
  164. std::string msg;
  165. if (!this->Makefile->EnforceUniqueName(exename, msg)) {
  166. this->SetError(msg);
  167. return false;
  168. }
  169. }
  170. if (s == args.end()) {
  171. this->SetError(
  172. "called with incorrect number of arguments, no sources provided");
  173. return false;
  174. }
  175. std::vector<std::string> srclists(s, args.end());
  176. cmTarget* tgt =
  177. this->Makefile->AddExecutable(exename.c_str(), srclists, excludeFromAll);
  178. if (use_win32) {
  179. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  180. }
  181. if (use_macbundle) {
  182. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  183. }
  184. return true;
  185. }