cmake/Source/CTest/cmCTestBuildCommand.cxx

152 lines
5.4 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 <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"
2021-11-20 13:41:27 +01:00
#include "cmValue.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);
2021-09-14 00:13:48 +02:00
this->Bind("PARALLEL_LEVEL"_s, this->ParallelLevel);
}
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
2021-11-20 13:41:27 +01:00
cmValue ctestBuildCommand =
2016-07-09 11:21:54 +02:00
this->Makefile->GetDefinition("CTEST_BUILD_COMMAND");
2021-09-14 00:13:48 +02:00
if (cmNonempty(ctestBuildCommand)) {
this->CTest->SetCTestConfiguration("MakeCommand", *ctestBuildCommand,
2016-07-09 11:21:54 +02:00
this->Quiet);
} else {
2021-11-20 13:41:27 +01:00
cmValue cmakeGeneratorName =
2016-07-09 11:21:54 +02:00
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.
//
2021-11-20 13:41:27 +01:00
cmValue ctestBuildConfiguration =
2016-07-09 11:21:54 +02:00
this->Makefile->GetDefinition("CTEST_BUILD_CONFIGURATION");
2021-09-14 00:13:48 +02:00
std::string cmakeBuildConfiguration = cmNonempty(this->Configuration)
? this->Configuration
: cmNonempty(ctestBuildConfiguration) ? *ctestBuildConfiguration
: this->CTest->GetConfigType();
const std::string& cmakeBuildAdditionalFlags = cmNonempty(this->Flags)
? this->Flags
: this->Makefile->GetSafeDefinition("CTEST_BUILD_FLAGS");
const std::string& cmakeBuildTarget = cmNonempty(this->Target)
? this->Target
: this->Makefile->GetSafeDefinition("CTEST_BUILD_TARGET");
if (cmNonempty(cmakeGeneratorName)) {
if (cmakeBuildConfiguration.empty()) {
cmakeBuildConfiguration = "Release";
2016-07-09 11:21:54 +02:00
}
if (this->GlobalGenerator) {
2021-09-14 00:13:48 +02:00
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(
2021-09-14 00:13:48 +02:00
*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 \"",
2021-09-14 00:13:48 +02:00
*cmakeGeneratorName, '"');
2019-11-11 23:01:05 +01:00
this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e);
2022-08-04 22:12:04 +02:00
cmSystemTools::SetFatalErrorOccurred();
2018-01-26 17:06:56 +01:00
return nullptr;
}
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
if (cmakeBuildConfiguration.empty()) {
#ifdef CMAKE_INTDIR
2021-09-14 00:13:48 +02:00
cmakeBuildConfiguration = CMAKE_INTDIR;
#else
cmakeBuildConfiguration = "Debug";
#endif
2016-07-09 11:21:54 +02:00
}
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(
2021-09-14 00:13:48 +02:00
cmakeBuildTarget, cmakeBuildConfiguration, this->ParallelLevel,
cmakeBuildAdditionalFlags, this->Makefile->IgnoreErrorsCMP0061());
2015-08-17 11:37:30 +02:00
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
2016-07-09 11:21:54 +02:00
"SetMakeCommand:" << buildCommand << "\n",
this->Quiet);
2021-09-14 00:13:48 +02:00
this->CTest->SetCTestConfiguration("MakeCommand", buildCommand,
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
}
2021-11-20 13:41:27 +01:00
if (cmValue useLaunchers =
2016-07-09 11:21:54 +02:00
this->Makefile->GetDefinition("CTEST_USE_LAUNCHERS")) {
2021-09-14 00:13:48 +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
2021-11-20 13:41:27 +01:00
if (cmValue labelsForSubprojects =
2018-01-26 17:06:56 +01:00
this->Makefile->GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
this->CTest->SetCTestConfiguration("LabelsForSubprojects",
2021-09-14 00:13:48 +02:00
*labelsForSubprojects, this->Quiet);
2018-01-26 17:06:56 +01:00
}
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
{
2021-09-14 00:13:48 +02:00
bool ret = this->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;
}