cmAddExecutableCommand.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "cmStateTypes.h"
  9. #include "cmTarget.h"
  10. class cmExecutionStatus;
  11. // cmExecutableCommand
  12. bool cmAddExecutableCommand::InitialPass(std::vector<std::string> const& args,
  13. cmExecutionStatus&)
  14. {
  15. if (args.empty()) {
  16. this->SetError("called with incorrect number of arguments");
  17. return false;
  18. }
  19. std::vector<std::string>::const_iterator s = args.begin();
  20. std::string const& exename = *s;
  21. ++s;
  22. bool use_win32 = false;
  23. bool use_macbundle = false;
  24. bool excludeFromAll = false;
  25. bool importTarget = false;
  26. bool importGlobal = false;
  27. bool isAlias = false;
  28. while (s != args.end()) {
  29. if (*s == "WIN32") {
  30. ++s;
  31. use_win32 = true;
  32. } else if (*s == "MACOSX_BUNDLE") {
  33. ++s;
  34. use_macbundle = true;
  35. } else if (*s == "EXCLUDE_FROM_ALL") {
  36. ++s;
  37. excludeFromAll = true;
  38. } else if (*s == "IMPORTED") {
  39. ++s;
  40. importTarget = true;
  41. } else if (importTarget && *s == "GLOBAL") {
  42. ++s;
  43. importGlobal = true;
  44. } else if (*s == "ALIAS") {
  45. ++s;
  46. isAlias = true;
  47. } else {
  48. break;
  49. }
  50. }
  51. bool nameOk = cmGeneratorExpression::IsValidTargetName(exename) &&
  52. !cmGlobalGenerator::IsReservedTarget(exename);
  53. if (nameOk && !importTarget && !isAlias) {
  54. nameOk = exename.find(':') == std::string::npos;
  55. }
  56. if (!nameOk &&
  57. !this->Makefile->CheckCMP0037(exename, cmStateEnums::EXECUTABLE)) {
  58. return false;
  59. }
  60. // Special modifiers are not allowed with IMPORTED signature.
  61. if (importTarget && (use_win32 || use_macbundle || excludeFromAll)) {
  62. if (use_win32) {
  63. this->SetError("may not be given WIN32 for an IMPORTED target.");
  64. } else if (use_macbundle) {
  65. this->SetError("may not be given MACOSX_BUNDLE for an IMPORTED target.");
  66. } else // if(excludeFromAll)
  67. {
  68. this->SetError(
  69. "may not be given EXCLUDE_FROM_ALL for an IMPORTED target.");
  70. }
  71. return false;
  72. }
  73. if (isAlias) {
  74. if (!cmGeneratorExpression::IsValidTargetName(exename)) {
  75. this->SetError("Invalid name for ALIAS: " + exename);
  76. return false;
  77. }
  78. if (excludeFromAll) {
  79. this->SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  80. return false;
  81. }
  82. if (importTarget || importGlobal) {
  83. this->SetError("IMPORTED with ALIAS is not allowed.");
  84. return false;
  85. }
  86. if (args.size() != 3) {
  87. std::ostringstream e;
  88. e << "ALIAS requires exactly one target argument.";
  89. this->SetError(e.str());
  90. return false;
  91. }
  92. std::string const& aliasedName = *s;
  93. if (this->Makefile->IsAlias(aliasedName)) {
  94. std::ostringstream e;
  95. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  96. << aliasedName << "\" is itself an ALIAS.";
  97. this->SetError(e.str());
  98. return false;
  99. }
  100. cmTarget* aliasedTarget =
  101. this->Makefile->FindTargetToUse(aliasedName, true);
  102. if (!aliasedTarget) {
  103. std::ostringstream e;
  104. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  105. << aliasedName << "\" does not already exist.";
  106. this->SetError(e.str());
  107. return false;
  108. }
  109. cmStateEnums::TargetType type = aliasedTarget->GetType();
  110. if (type != cmStateEnums::EXECUTABLE) {
  111. std::ostringstream e;
  112. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  113. << aliasedName << "\" is not an executable.";
  114. this->SetError(e.str());
  115. return false;
  116. }
  117. if (aliasedTarget->IsImported() &&
  118. !aliasedTarget->IsImportedGloballyVisible()) {
  119. std::ostringstream e;
  120. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  121. << aliasedName << "\" is imported but not globally visible.";
  122. this->SetError(e.str());
  123. return false;
  124. }
  125. this->Makefile->AddAlias(exename, aliasedName);
  126. return true;
  127. }
  128. // Handle imported target creation.
  129. if (importTarget) {
  130. // Make sure the target does not already exist.
  131. if (this->Makefile->FindTargetToUse(exename)) {
  132. std::ostringstream e;
  133. e << "cannot create imported target \"" << exename
  134. << "\" because another target with the same name already exists.";
  135. this->SetError(e.str());
  136. return false;
  137. }
  138. // Create the imported target.
  139. this->Makefile->AddImportedTarget(exename, cmStateEnums::EXECUTABLE,
  140. importGlobal);
  141. return true;
  142. }
  143. // Enforce name uniqueness.
  144. {
  145. std::string msg;
  146. if (!this->Makefile->EnforceUniqueName(exename, msg)) {
  147. this->SetError(msg);
  148. return false;
  149. }
  150. }
  151. std::vector<std::string> srclists(s, args.end());
  152. cmTarget* tgt =
  153. this->Makefile->AddExecutable(exename, srclists, excludeFromAll);
  154. if (use_win32) {
  155. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  156. }
  157. if (use_macbundle) {
  158. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  159. }
  160. return true;
  161. }