cmAddExecutableCommand.cxx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 exist.";
  133. this->SetError(e.str());
  134. return false;
  135. }
  136. cmStateEnums::TargetType type = aliasedTarget->GetType();
  137. if (type != cmStateEnums::EXECUTABLE) {
  138. std::ostringstream e;
  139. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  140. << aliasedName << "\" is not an executable.";
  141. this->SetError(e.str());
  142. return false;
  143. }
  144. if (aliasedTarget->IsImported() &&
  145. !aliasedTarget->IsImportedGloballyVisible()) {
  146. std::ostringstream e;
  147. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  148. << aliasedName << "\" is imported but not globally visible.";
  149. this->SetError(e.str());
  150. return false;
  151. }
  152. this->Makefile->AddAlias(exename, aliasedName);
  153. return true;
  154. }
  155. // Handle imported target creation.
  156. if (importTarget) {
  157. // Make sure the target does not already exist.
  158. if (this->Makefile->FindTargetToUse(exename)) {
  159. std::ostringstream e;
  160. e << "cannot create imported target \"" << exename
  161. << "\" because another target with the same name already exists.";
  162. this->SetError(e.str());
  163. return false;
  164. }
  165. // Create the imported target.
  166. this->Makefile->AddImportedTarget(exename, cmStateEnums::EXECUTABLE,
  167. importGlobal);
  168. return true;
  169. }
  170. // Enforce name uniqueness.
  171. {
  172. std::string msg;
  173. if (!this->Makefile->EnforceUniqueName(exename, msg)) {
  174. this->SetError(msg);
  175. return false;
  176. }
  177. }
  178. std::vector<std::string> srclists(s, args.end());
  179. cmTarget* tgt =
  180. this->Makefile->AddExecutable(exename.c_str(), srclists, excludeFromAll);
  181. if (use_win32) {
  182. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  183. }
  184. if (use_macbundle) {
  185. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  186. }
  187. return true;
  188. }