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