cmake/Source/CTest/cmCTestBuildCommand.cxx

156 lines
5.5 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 "cmCTestBuildCommand.h"
2020-02-01 23:06:01 +01:00
#include <cstring>
#include <sstream>
2020-08-30 11:54:41 +02:00
#include <cmext/string_view>
2020-02-01 23:06:01 +01:00
#include "cmCTest.h"
2009-10-04 10:30:41 +03:00
#include "cmCTestBuildHandler.h"
#include "cmGlobalGenerator.h"
2016-10-30 18:24:19 +01:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmSystemTools.h"
2016-07-09 11:21:54 +02:00
#include "cmake.h"
2016-10-30 18:24:19 +01:00
class cmExecutionStatus;
2020-02-01 23:06:01 +01:00
void cmCTestBuildCommand::BindArguments()
{
2020-02-01 23:06:01 +01:00
this->cmCTestHandlerCommand::BindArguments();
this->Bind("NUMBER_ERRORS"_s, this->NumberErrors);
this->Bind("NUMBER_WARNINGS"_s, this->NumberWarnings);
this->Bind("TARGET"_s, this->Target);
this->Bind("CONFIGURATION"_s, this->Configuration);
this->Bind("FLAGS"_s, this->Flags);
this->Bind("PROJECT_NAME"_s, this->ProjectName);
}
2020-08-30 11:54:41 +02:00
cmCTestBuildCommand::~cmCTestBuildCommand() = default;
cmCTestGenericHandler* cmCTestBuildCommand::InitializeHandler()
{
2019-11-11 23:01:05 +01:00
cmCTestBuildHandler* handler = this->CTest->GetBuildHandler();
handler->Initialize();
this->Handler = handler;
2010-03-17 14:00:29 +02:00
2016-07-09 11:21:54 +02:00
const char* ctestBuildCommand =
this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
if (ctestBuildCommand && *ctestBuildCommand) {
2015-08-17 11:37:30 +02:00
this->CTest->SetCTestConfiguration("MakeCommand", ctestBuildCommand,
2016-07-09 11:21:54 +02:00
this->Quiet);
} else {
const char* cmakeGeneratorName =
this->Makefile->GetDefinition("CTEST_CMAKE_GENERATOR");
2010-03-17 14:00:29 +02:00
// Build configuration is determined by: CONFIGURATION argument,
// or CTEST_BUILD_CONFIGURATION script variable, or
// CTEST_CONFIGURATION_TYPE script variable, or ctest -C command
// line argument... in that order.
//
2016-07-09 11:21:54 +02:00
const char* ctestBuildConfiguration =
this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
2020-02-01 23:06:01 +01:00
const char* cmakeBuildConfiguration = !this->Configuration.empty()
? this->Configuration.c_str()
2010-03-17 14:00:29 +02:00
: ((ctestBuildConfiguration && *ctestBuildConfiguration)
2016-07-09 11:21:54 +02:00
? ctestBuildConfiguration
: this->CTest->GetConfigType().c_str());
2010-03-17 14:00:29 +02:00
2020-02-01 23:06:01 +01:00
const char* cmakeBuildAdditionalFlags = !this->Flags.empty()
? this->Flags.c_str()
2010-03-17 14:00:29 +02:00
: this->Makefile->GetDefinition("CTEST_BUILD_FLAGS");
2020-02-01 23:06:01 +01:00
const char* cmakeBuildTarget = !this->Target.empty()
? this->Target.c_str()
2010-03-17 14:00:29 +02:00
: this->Makefile->GetDefinition("CTEST_BUILD_TARGET");
2019-11-11 23:01:05 +01:00
if (cmakeGeneratorName && *cmakeGeneratorName) {
2016-07-09 11:21:54 +02:00
if (!cmakeBuildConfiguration) {
cmakeBuildConfiguration = "Release";
2016-07-09 11:21:54 +02:00
}
if (this->GlobalGenerator) {
if (this->GlobalGenerator->GetName() != cmakeGeneratorName) {
2020-08-30 11:54:41 +02:00
this->GlobalGenerator.reset();
}
2016-07-09 11:21:54 +02:00
}
if (!this->GlobalGenerator) {
this->GlobalGenerator =
this->Makefile->GetCMakeInstance()->CreateGlobalGenerator(
cmakeGeneratorName);
2016-07-09 11:21:54 +02:00
if (!this->GlobalGenerator) {
2020-02-01 23:06:01 +01:00
std::string e = cmStrCat("could not create generator named \"",
cmakeGeneratorName, '"');
2019-11-11 23:01:05 +01:00
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
2014-08-03 19:52:23 +02:00
cmSystemTools::SetFatalErrorOccured();
2018-01-26 17:06:56 +01:00
return nullptr;
}
2016-07-09 11:21:54 +02:00
}
if (strlen(cmakeBuildConfiguration) == 0) {
2018-01-26 17:06:56 +01:00
const char* config = nullptr;
#ifdef CMAKE_INTDIR
config = CMAKE_INTDIR;
#endif
2016-07-09 11:21:54 +02:00
if (!config) {
config = "Debug";
}
2016-07-09 11:21:54 +02:00
cmakeBuildConfiguration = config;
}
2010-03-17 14:00:29 +02:00
2013-11-03 12:27:13 +02:00
std::string dir = this->CTest->GetCTestConfiguration("BuildDirectory");
2016-07-09 11:21:54 +02:00
std::string buildCommand =
this->GlobalGenerator->GenerateCMakeBuildCommand(
cmakeBuildTarget ? cmakeBuildTarget : "", cmakeBuildConfiguration,
2015-08-17 11:37:30 +02:00
cmakeBuildAdditionalFlags ? cmakeBuildAdditionalFlags : "",
this->Makefile->IgnoreErrorsCMP0061());
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2016-07-09 11:21:54 +02:00
"SetMakeCommand:" << buildCommand << "\n",
this->Quiet);
2015-08-17 11:37:30 +02:00
this->CTest->SetCTestConfiguration("MakeCommand", buildCommand.c_str(),
2016-07-09 11:21:54 +02:00
this->Quiet);
} else {
2015-04-27 22:25:09 +02:00
std::ostringstream ostr;
2016-07-09 11:21:54 +02:00
/* clang-format off */
2010-03-17 14:00:29 +02:00
ostr << "has no project to build. If this is a "
"\"built with CMake\" project, verify that CTEST_CMAKE_GENERATOR "
2019-11-11 23:01:05 +01:00
"is set. Otherwise, set CTEST_BUILD_COMMAND to build the project "
2010-03-17 14:00:29 +02:00
"with a custom command line.";
2016-07-09 11:21:54 +02:00
/* clang-format on */
2015-04-27 22:25:09 +02:00
this->SetError(ostr.str());
2018-01-26 17:06:56 +01:00
return nullptr;
}
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (const char* useLaunchers =
this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
2015-08-17 11:37:30 +02:00
this->CTest->SetCTestConfiguration("UseLaunchers", useLaunchers,
2016-07-09 11:21:54 +02:00
this->Quiet);
}
2009-10-04 10:30:41 +03:00
2018-01-26 17:06:56 +01:00
if (const char* labelsForSubprojects =
this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
this->CTest->SetCTestConfiguration("LabelsForSubprojects",
labelsForSubprojects, this->Quiet);
}
2015-08-17 11:37:30 +02:00
handler->SetQuiet(this->Quiet);
return handler;
}
2009-10-04 10:30:41 +03:00
bool cmCTestBuildCommand::InitialPass(std::vector<std::string> const& args,
2016-07-09 11:21:54 +02:00
cmExecutionStatus& status)
2009-10-04 10:30:41 +03:00
{
2016-07-09 11:21:54 +02:00
bool ret = cmCTestHandlerCommand::InitialPass(args, status);
2020-02-01 23:06:01 +01:00
if (!this->NumberErrors.empty()) {
this->Makefile->AddDefinition(
this->NumberErrors, std::to_string(this->Handler->GetTotalErrors()));
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
if (!this->NumberWarnings.empty()) {
this->Makefile->AddDefinition(
this->NumberWarnings, std::to_string(this->Handler->GetTotalWarnings()));
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return ret;
}