cmBuildCommand.cxx 3.6 KB

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