cmBuildCommand.cxx 3.6 KB

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