cmAddExecutableCommand.cxx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. 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. status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
  93. "\" because target \"", aliasedName,
  94. "\" is itself an ALIAS."));
  95. return false;
  96. }
  97. cmTarget* aliasedTarget = mf.FindTargetToUse(aliasedName, true);
  98. if (!aliasedTarget) {
  99. status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
  100. "\" because target \"", aliasedName,
  101. "\" does not already exist."));
  102. return false;
  103. }
  104. cmStateEnums::TargetType type = aliasedTarget->GetType();
  105. if (type != cmStateEnums::EXECUTABLE) {
  106. status.SetError(cmStrCat("cannot create ALIAS target \"", exename,
  107. "\" because target \"", aliasedName,
  108. "\" is not an executable."));
  109. return false;
  110. }
  111. mf.AddAlias(exename, aliasedName,
  112. !aliasedTarget->IsImported() ||
  113. aliasedTarget->IsImportedGloballyVisible());
  114. return true;
  115. }
  116. // Handle imported target creation.
  117. if (importTarget) {
  118. // Make sure the target does not already exist.
  119. if (mf.FindTargetToUse(exename)) {
  120. status.SetError(cmStrCat(
  121. "cannot create imported target \"", exename,
  122. "\" because another target with the same name already exists."));
  123. return false;
  124. }
  125. // Create the imported target.
  126. mf.AddImportedTarget(exename, cmStateEnums::EXECUTABLE, importGlobal);
  127. return true;
  128. }
  129. // Enforce name uniqueness.
  130. {
  131. std::string msg;
  132. if (!mf.EnforceUniqueName(exename, msg)) {
  133. status.SetError(msg);
  134. return false;
  135. }
  136. }
  137. std::vector<std::string> srclists(s, args.end());
  138. cmTarget* tgt = mf.AddExecutable(exename, srclists, excludeFromAll);
  139. if (use_win32) {
  140. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  141. }
  142. if (use_macbundle) {
  143. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  144. }
  145. return true;
  146. }