cmAddExecutableCommand.cxx 6.0 KB

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