cmAddExecutableCommand.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "cmExecutionStatus.h"
  5. #include "cmGeneratorExpression.h"
  6. #include "cmGlobalGenerator.h"
  7. #include "cmMakefile.h"
  8. #include "cmStateTypes.h"
  9. #include "cmStringAlgorithms.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. if (importTarget && !importGlobal) {
  52. importGlobal = mf.IsImportedTargetGlobalScope();
  53. }
  54. bool nameOk = cmGeneratorExpression::IsValidTargetName(exename) &&
  55. !cmGlobalGenerator::IsReservedTarget(exename);
  56. if (nameOk && !importTarget && !isAlias) {
  57. nameOk = exename.find(':') == std::string::npos;
  58. }
  59. if (!nameOk && !mf.CheckCMP0037(exename, cmStateEnums::EXECUTABLE)) {
  60. return false;
  61. }
  62. // Special modifiers are not allowed with IMPORTED signature.
  63. if (importTarget && (use_win32 || use_macbundle || excludeFromAll)) {
  64. if (use_win32) {
  65. status.SetError("may not be given WIN32 for an IMPORTED target.");
  66. } else if (use_macbundle) {
  67. status.SetError(
  68. "may not be given MACOSX_BUNDLE for an IMPORTED target.");
  69. } else // if(excludeFromAll)
  70. {
  71. status.SetError(
  72. "may not be given EXCLUDE_FROM_ALL for an IMPORTED target.");
  73. }
  74. return false;
  75. }
  76. if (isAlias) {
  77. if (!cmGeneratorExpression::IsValidTargetName(exename)) {
  78. status.SetError("Invalid name for ALIAS: " + exename);
  79. return false;
  80. }
  81. if (excludeFromAll) {
  82. status.SetError("EXCLUDE_FROM_ALL with ALIAS makes no sense.");
  83. return false;
  84. }
  85. if (importTarget || importGlobal) {
  86. status.SetError("IMPORTED with ALIAS is not allowed.");
  87. return false;
  88. }
  89. if (args.size() != 3) {
  90. status.SetError("ALIAS requires exactly one target argument.");
  91. return false;
  92. }
  93. std::string const& aliasedName = *s;
  94. if (mf.IsAlias(aliasedName)) {
  95. status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
  96. "\" because target \"", aliasedName,
  97. "\" is itself an ALIAS."));
  98. return false;
  99. }
  100. cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
  101. if (!aliasedTarget) {
  102. status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
  103. "\" because target \"", aliasedName,
  104. "\" does not already exist."));
  105. return false;
  106. }
  107. cmStateEnums::TargetType type = aliasedTarget->GetType();
  108. if (type != cmStateEnums::EXECUTABLE) {
  109. status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
  110. "\" because target \"", aliasedName,
  111. "\" is not an executable."));
  112. return false;
  113. }
  114. mf.AddAlias(exename, aliasedName,
  115. !aliasedTarget->IsImported() ||
  116. aliasedTarget->IsImportedGloballyVisible());
  117. return true;
  118. }
  119. // Handle imported target creation.
  120. if (importTarget) {
  121. // Make sure the target does not already exist.
  122. if (mf.FindTargetToUse(exename)) {
  123. status.SetError(cmStrCat(
  124. "cannot create imported target \"", exename,
  125. "\" because another target with the same name already exists."));
  126. return false;
  127. }
  128. // Create the imported target.
  129. mf.AddImportedTarget(exename, cmStateEnums::EXECUTABLE, importGlobal);
  130. return true;
  131. }
  132. // Enforce name uniqueness.
  133. {
  134. std::string msg;
  135. if (!mf.EnforceUniqueName(exename, msg)) {
  136. status.SetError(msg);
  137. return false;
  138. }
  139. }
  140. std::vector<std::string> srclists(s, args.end());
  141. cmTarget* tgt = mf.AddExecutable(exename, srclists, excludeFromAll);
  142. if (use_win32) {
  143. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  144. }
  145. if (use_macbundle) {
  146. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  147. }
  148. return true;
  149. }