cmAddExecutableCommand.cxx 4.8 KB

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