cmBuildCommand.cxx 3.6 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3 4
#include "cmBuildCommand.h"

5 6
#include <sstream>

7
#include "cmGlobalGenerator.h"
8
#include "cmMakefile.h"
9
#include "cmMessageType.h"
10 11 12 13
#include "cmStateTypes.h"
#include "cmSystemTools.h"

class cmExecutionStatus;
14

15 16
bool cmBuildCommand::InitialPass(std::vector<std::string> const& args,
                                 cmExecutionStatus&)
17 18 19
{
  // Support the legacy signature of the command:
  //
20
  if (2 == args.size()) {
21
    return this->TwoArgsSignature(args);
22
  }
23 24 25 26

  return this->MainSignature(args);
}

27
bool cmBuildCommand::MainSignature(std::vector<std::string> const& args)
28
{
29
  if (args.empty()) {
30 31
    this->SetError("requires at least one argument naming a CMake variable");
    return false;
32
  }
33 34

  // The cmake variable in which to store the result.
35
  std::string const& variable = args[0];
36 37

  // Parse remaining arguments.
38 39
  std::string configuration;
  std::string project_name;
40
  std::string target;
41 42 43 44 45 46 47
  enum Doing
  {
    DoingNone,
    DoingConfiguration,
    DoingProjectName,
    DoingTarget
  };
48
  Doing doing = DoingNone;
49 50
  for (unsigned int i = 1; i < args.size(); ++i) {
    if (args[i] == "CONFIGURATION") {
51
      doing = DoingConfiguration;
52
    } else if (args[i] == "PROJECT_NAME") {
53
      doing = DoingProjectName;
54
    } else if (args[i] == "TARGET") {
55
      doing = DoingTarget;
56
    } else if (doing == DoingConfiguration) {
57
      doing = DoingNone;
58
      configuration = args[i];
59
    } else if (doing == DoingProjectName) {
60
      doing = DoingNone;
61
      project_name = args[i];
62
    } else if (doing == DoingTarget) {
63
      doing = DoingNone;
64
      target = args[i];
65
    } else {
66
      std::ostringstream e;
67
      e << "unknown argument \"" << args[i] << "\"";
68
      this->SetError(e.str());
69 70
      return false;
    }
71
  }
72

73
  // If null/empty CONFIGURATION argument, cmake --build uses 'Debug'
74 75 76 77
  // in the currently implemented multi-configuration global generators...
  // so we put this code here to end up with the same default configuration
  // as the original 2-arg build_command signature:
  //
78 79
  if (configuration.empty()) {
    cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configuration);
80
  }
81
  if (configuration.empty()) {
82
    configuration = "Release";
83
  }
84

85
  if (!project_name.empty()) {
86
    this->Makefile->IssueMessage(
87
      MessageType::AUTHOR_WARNING,
88
      "Ignoring PROJECT_NAME option because it has no effect.");
89
  }
90

91 92
  std::string makecommand =
    this->Makefile->GetGlobalGenerator()->GenerateCMakeBuildCommand(
93
      target, configuration, "", this->Makefile->IgnoreErrorsCMP0061());
94 95 96 97 98 99

  this->Makefile->AddDefinition(variable, makecommand.c_str());

  return true;
}

100
bool cmBuildCommand::TwoArgsSignature(std::vector<std::string> const& args)
101
{
102
  if (args.size() < 2) {
103
    this->SetError("called with less than two arguments");
104
    return false;
105
  }
106

107
  std::string const& define = args[0];
108
  const char* cacheValue = this->Makefile->GetDefinition(define);
109

110 111 112 113
  std::string configType;
  if (!cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configType) ||
      configType.empty()) {
    configType = "Release";
114
  }
115

116 117 118
  std::string makecommand =
    this->Makefile->GetGlobalGenerator()->GenerateCMakeBuildCommand(
      "", configType, "", this->Makefile->IgnoreErrorsCMP0061());
119

120
  if (cacheValue) {
121
    return true;
122 123 124 125
  }
  this->Makefile->AddCacheDefinition(define, makecommand.c_str(),
                                     "Command used to build entire project "
                                     "from the command line.",
126
                                     cmStateEnums::STRING);
127 128
  return true;
}