cmBuildCommand.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "cmBuildCommand.h"
  4. #include "cmExecutionStatus.h"
  5. #include "cmGlobalGenerator.h"
  6. #include "cmMakefile.h"
  7. #include "cmMessageType.h"
  8. #include "cmStateTypes.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. #include "cmValue.h"
  12. namespace {
  13. bool MainSignature(std::vector<std::string> const& args,
  14. cmExecutionStatus& status)
  15. {
  16. if (args.empty()) {
  17. status.SetError("requires at least one argument naming a CMake variable");
  18. return false;
  19. }
  20. // The cmake variable in which to store the result.
  21. std::string const& variable = args[0];
  22. // Parse remaining arguments.
  23. std::string configuration;
  24. std::string project_name;
  25. std::string target;
  26. std::string parallel;
  27. enum Doing
  28. {
  29. DoingNone,
  30. DoingConfiguration,
  31. DoingProjectName,
  32. DoingTarget,
  33. DoingParallel
  34. };
  35. Doing doing = DoingNone;
  36. for (unsigned int i = 1; i < args.size(); ++i) {
  37. if (args[i] == "CONFIGURATION") {
  38. doing = DoingConfiguration;
  39. } else if (args[i] == "PROJECT_NAME") {
  40. doing = DoingProjectName;
  41. } else if (args[i] == "TARGET") {
  42. doing = DoingTarget;
  43. } else if (args[i] == "PARALLEL_LEVEL") {
  44. doing = DoingParallel;
  45. } else if (doing == DoingConfiguration) {
  46. doing = DoingNone;
  47. configuration = args[i];
  48. } else if (doing == DoingProjectName) {
  49. doing = DoingNone;
  50. project_name = args[i];
  51. } else if (doing == DoingTarget) {
  52. doing = DoingNone;
  53. target = args[i];
  54. } else if (doing == DoingParallel) {
  55. doing = DoingNone;
  56. parallel = args[i];
  57. } else {
  58. status.SetError(cmStrCat("unknown argument \"", args[i], "\""));
  59. return false;
  60. }
  61. }
  62. // If null/empty CONFIGURATION argument, cmake --build uses 'Debug'
  63. // in the currently implemented multi-configuration global generators...
  64. // so we put this code here to end up with the same default configuration
  65. // as the original 2-arg build_command signature:
  66. //
  67. if (configuration.empty()) {
  68. cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configuration);
  69. }
  70. if (configuration.empty()) {
  71. configuration = "Release";
  72. }
  73. cmMakefile& mf = status.GetMakefile();
  74. if (!project_name.empty()) {
  75. mf.IssueMessage(MessageType::AUTHOR_WARNING,
  76. "Ignoring PROJECT_NAME option because it has no effect.");
  77. }
  78. std::string makecommand = mf.GetGlobalGenerator()->GenerateCMakeBuildCommand(
  79. target, configuration, parallel, "", mf.IgnoreErrorsCMP0061());
  80. mf.AddDefinition(variable, makecommand);
  81. return true;
  82. }
  83. bool TwoArgsSignature(std::vector<std::string> const& args,
  84. cmExecutionStatus& status)
  85. {
  86. if (args.size() < 2) {
  87. status.SetError("called with less than two arguments");
  88. return false;
  89. }
  90. cmMakefile& mf = status.GetMakefile();
  91. std::string const& define = args[0];
  92. cmValue cacheValue = mf.GetDefinition(define);
  93. std::string configType;
  94. if (!cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configType) ||
  95. configType.empty()) {
  96. configType = "Release";
  97. }
  98. std::string makecommand = mf.GetGlobalGenerator()->GenerateCMakeBuildCommand(
  99. "", configType, "", "", mf.IgnoreErrorsCMP0061());
  100. if (cacheValue) {
  101. return true;
  102. }
  103. mf.AddCacheDefinition(define, makecommand,
  104. "Command used to build entire project "
  105. "from the command line.",
  106. cmStateEnums::STRING);
  107. return true;
  108. }
  109. } // namespace
  110. bool cmBuildCommand(std::vector<std::string> const& args,
  111. cmExecutionStatus& status)
  112. {
  113. // Support the legacy signature of the command:
  114. if (args.size() == 2) {
  115. return TwoArgsSignature(args, status);
  116. }
  117. return MainSignature(args, status);
  118. }