summaryrefslogtreecommitdiff
path: root/Source/cmBuildCommand.cxx
blob: fd876004b0531a04d79630c2cce4e37352a9ebd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmBuildCommand.h"

#include <sstream>

#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
#include "cmake.h"

class cmExecutionStatus;

bool cmBuildCommand::InitialPass(std::vector<std::string> const& args,
                                 cmExecutionStatus&)
{
  // Support the legacy signature of the command:
  //
  if (2 == args.size()) {
    return this->TwoArgsSignature(args);
  }

  return this->MainSignature(args);
}

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

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

  // Parse remaining arguments.
  std::string configuration;
  std::string project_name;
  std::string target;
  enum Doing
  {
    DoingNone,
    DoingConfiguration,
    DoingProjectName,
    DoingTarget
  };
  Doing doing = DoingNone;
  for (unsigned int i = 1; i < args.size(); ++i) {
    if (args[i] == "CONFIGURATION") {
      doing = DoingConfiguration;
    } else if (args[i] == "PROJECT_NAME") {
      doing = DoingProjectName;
    } else if (args[i] == "TARGET") {
      doing = DoingTarget;
    } else if (doing == DoingConfiguration) {
      doing = DoingNone;
      configuration = args[i];
    } else if (doing == DoingProjectName) {
      doing = DoingNone;
      project_name = args[i];
    } else if (doing == DoingTarget) {
      doing = DoingNone;
      target = args[i];
    } else {
      std::ostringstream e;
      e << "unknown argument \"" << args[i] << "\"";
      this->SetError(e.str());
      return false;
    }
  }

  // If null/empty CONFIGURATION argument, cmake --build uses 'Debug'
  // 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:
  //
  if (configuration.empty()) {
    cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configuration);
  }
  if (configuration.empty()) {
    configuration = "Release";
  }

  if (!project_name.empty()) {
    this->Makefile->IssueMessage(
      cmake::AUTHOR_WARNING,
      "Ignoring PROJECT_NAME option because it has no effect.");
  }

  std::string makecommand =
    this->Makefile->GetGlobalGenerator()->GenerateCMakeBuildCommand(
      target, configuration, "", this->Makefile->IgnoreErrorsCMP0061());

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

  return true;
}

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

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

  std::string configType;
  if (!cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configType) ||
      configType.empty()) {
    configType = "Release";
  }

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

  if (cacheValue) {
    return true;
  }
  this->Makefile->AddCacheDefinition(define, makecommand.c_str(),
                                     "Command used to build entire project "
                                     "from the command line.",
                                     cmStateEnums::STRING);
  return true;
}