cmAddExecutableCommand.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "cmExecutionStatus.h"
  6. #include "cmGeneratorExpression.h"
  7. #include "cmGlobalGenerator.h"
  8. #include "cmMakefile.h"
  9. #include "cmStateTypes.h"
  10. #include "cmTarget.h"
  11. bool cmAddExecutableCommand(std::vector<std::string> const& args,
  12. cmExecutionStatus& status)
  13. {
  14. if (args.empty()) {
  15. status.SetError("called with incorrect number of arguments");
  16. return false;
  17. }
  18. cmMakefile& mf = status.GetMakefile();
  19. auto 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 && !mf.CheckCMP0037(exename, cmStateEnums::EXECUTABLE)) {
  57. return false;
  58. }
  59. // Special modifiers are not allowed with IMPORTED signature.
  60. if (importTarget && (use_win32 || use_macbundle || excludeFromAll)) {
  61. if (use_win32) {
  62. status.SetError("may not be given WIN32 for an IMPORTED target.");
  63. } else if (use_macbundle) {
  64. status.SetError(
  65. "may not be given MACOSX_BUNDLE for an IMPORTED target.");
  66. } else // if(excludeFromAll)
  67. {
  68. status.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. status.SetError("Invalid name for ALIAS: " + exename);
  76. return false;
  77. }
  78. if (excludeFromAll) {
  79. status.SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  80. return false;
  81. }
  82. if (importTarget || importGlobal) {
  83. status.SetError("IMPORTED with ALIAS is not allowed.");
  84. return false;
  85. }
  86. if (args.size() != 3) {
  87. status.SetError("ALIAS requires exactly one target argument.");
  88. return false;
  89. }
  90. std::string const& aliasedName = *s;
  91. if (mf.IsAlias(aliasedName)) {
  92. std::ostringstream e;
  93. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  94. << aliasedName << "\" is itself an ALIAS.";
  95. status.SetError(e.str());
  96. return false;
  97. }
  98. cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
  99. if (!aliasedTarget) {
  100. std::ostringstream e;
  101. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  102. << aliasedName << "\" does not already exist.";
  103. status.SetError(e.str());
  104. return false;
  105. }
  106. cmStateEnums::TargetType type = aliasedTarget->GetType();
  107. if (type != cmStateEnums::EXECUTABLE) {
  108. std::ostringstream e;
  109. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  110. << aliasedName << "\" is not an executable.";
  111. status.SetError(e.str());
  112. return false;
  113. }
  114. if (aliasedTarget->IsImported() &&
  115. !aliasedTarget->IsImportedGloballyVisible()) {
  116. std::ostringstream e;
  117. e << "cannot create ALIAS target \"" << exename << "\" because target \""
  118. << aliasedName << "\" is imported but not globally visible.";
  119. status.SetError(e.str());
  120. return false;
  121. }
  122. mf.AddAlias(exename, aliasedName);
  123. return true;
  124. }
  125. // Handle imported target creation.
  126. if (importTarget) {
  127. // Make sure the target does not already exist.
  128. if (mf.FindTargetToUse(exename)) {
  129. std::ostringstream e;
  130. e << "cannot create imported target \"" << exename
  131. << "\" because another target with the same name already exists.";
  132. status.SetError(e.str());
  133. return false;
  134. }
  135. // Create the imported target.
  136. mf.AddImportedTarget(exename, cmStateEnums::EXECUTABLE, importGlobal);
  137. return true;
  138. }
  139. // Enforce name uniqueness.
  140. {
  141. std::string msg;
  142. if (!mf.EnforceUniqueName(exename, msg)) {
  143. status.SetError(msg);
  144. return false;
  145. }
  146. }
  147. std::vector<std::string> srclists(s, args.end());
  148. cmTarget* tgt = mf.AddExecutable(exename, srclists, excludeFromAll);
  149. if (use_win32) {
  150. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  151. }
  152. if (use_macbundle) {
  153. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  154. }
  155. return true;
  156. }