cmAddExecutableCommand.cxx 6.0 KB

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