cmake/Source/cmBuildCommand.cxx

138 lines
3.8 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmBuildCommand.h"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
#include "cmGlobalGenerator.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
namespace {
2010-03-17 14:00:29 +02:00
2020-02-01 23:06:01 +01:00
bool MainSignature(std::vector<std::string> const& args,
cmExecutionStatus& status)
2010-03-17 14:00:29 +02:00
{
2016-10-30 18:24:19 +01:00
if (args.empty()) {
2020-02-01 23:06:01 +01:00
status.SetError("requires at least one argument naming a CMake variable");
2010-03-17 14:00:29 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
// The cmake variable in which to store the result.
2017-07-20 19:35:53 +02:00
std::string const& variable = args[0];
2010-03-17 14:00:29 +02:00
// Parse remaining arguments.
2016-10-30 18:24:19 +01:00
std::string configuration;
std::string project_name;
2015-04-27 22:25:09 +02:00
std::string target;
2021-09-14 00:13:48 +02:00
std::string parallel;
2016-07-09 11:21:54 +02:00
enum Doing
{
DoingNone,
DoingConfiguration,
DoingProjectName,
2021-09-14 00:13:48 +02:00
DoingTarget,
DoingParallel
2016-07-09 11:21:54 +02:00
};
2010-03-17 14:00:29 +02:00
Doing doing = DoingNone;
2016-07-09 11:21:54 +02:00
for (unsigned int i = 1; i < args.size(); ++i) {
if (args[i] == "CONFIGURATION") {
2010-03-17 14:00:29 +02:00
doing = DoingConfiguration;
2016-07-09 11:21:54 +02:00
} else if (args[i] == "PROJECT_NAME") {
2010-03-17 14:00:29 +02:00
doing = DoingProjectName;
2016-07-09 11:21:54 +02:00
} else if (args[i] == "TARGET") {
2010-03-17 14:00:29 +02:00
doing = DoingTarget;
2021-09-14 00:13:48 +02:00
} else if (args[i] == "PARALLEL_LEVEL") {
doing = DoingParallel;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingConfiguration) {
2010-03-17 14:00:29 +02:00
doing = DoingNone;
2016-10-30 18:24:19 +01:00
configuration = args[i];
2016-07-09 11:21:54 +02:00
} else if (doing == DoingProjectName) {
2010-03-17 14:00:29 +02:00
doing = DoingNone;
2016-10-30 18:24:19 +01:00
project_name = args[i];
2016-07-09 11:21:54 +02:00
} else if (doing == DoingTarget) {
2010-03-17 14:00:29 +02:00
doing = DoingNone;
2015-04-27 22:25:09 +02:00
target = args[i];
2021-09-14 00:13:48 +02:00
} else if (doing == DoingParallel) {
doing = DoingNone;
parallel = args[i];
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("unknown argument \"", args[i], "\""));
2010-03-17 14:00:29 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
2014-08-03 19:52:23 +02:00
// If null/empty CONFIGURATION argument, cmake --build uses 'Debug'
2010-03-17 14:00:29 +02:00
// 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:
//
2016-10-30 18:24:19 +01:00
if (configuration.empty()) {
cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configuration);
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
if (configuration.empty()) {
2010-03-17 14:00:29 +02:00
configuration = "Release";
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
2016-10-30 18:24:19 +01:00
if (!project_name.empty()) {
2020-02-01 23:06:01 +01:00
mf.IssueMessage(MessageType::AUTHOR_WARNING,
"Ignoring PROJECT_NAME option because it has no effect.");
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
2020-02-01 23:06:01 +01:00
std::string makecommand = mf.GetGlobalGenerator()->GenerateCMakeBuildCommand(
2021-09-14 00:13:48 +02:00
target, configuration, parallel, "", mf.IgnoreErrorsCMP0061());
2010-03-17 14:00:29 +02:00
2020-02-01 23:06:01 +01:00
mf.AddDefinition(variable, makecommand);
2010-03-17 14:00:29 +02:00
return true;
}
2020-02-01 23:06:01 +01:00
bool TwoArgsSignature(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-07-09 11:21:54 +02:00
if (args.size() < 2) {
2020-02-01 23:06:01 +01:00
status.SetError("called with less than two arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
2017-07-20 19:35:53 +02:00
std::string const& define = args[0];
2021-11-20 13:41:27 +01:00
cmValue cacheValue = mf.GetDefinition(define);
2010-03-17 14:00:29 +02:00
2016-10-30 18:24:19 +01:00
std::string configType;
if (!cmSystemTools::GetEnv("CMAKE_CONFIG_TYPE", configType) ||
configType.empty()) {
configType = "Release";
2016-07-09 11:21:54 +02:00
}
2010-03-17 14:00:29 +02:00
2020-02-01 23:06:01 +01:00
std::string makecommand = mf.GetGlobalGenerator()->GenerateCMakeBuildCommand(
2021-09-14 00:13:48 +02:00
"", configType, "", "", mf.IgnoreErrorsCMP0061());
2016-07-09 11:21:54 +02:00
if (cacheValue) {
return true;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
mf.AddCacheDefinition(define, makecommand,
2020-02-01 23:06:01 +01:00
"Command used to build entire project "
"from the command line.",
cmStateEnums::STRING);
return true;
}
2020-02-01 23:06:01 +01:00
} // namespace
bool cmBuildCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
// Support the legacy signature of the command:
if (args.size() == 2) {
return TwoArgsSignature(args, status);
}
return MainSignature(args, status);
}