1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-16 05:26:58 +08:00

cmCTestConfigureCommand: Refactor command line construction

This commit is contained in:
Daniel Pfeifer
2025-01-28 10:00:49 +01:00
parent fda055c260
commit e52eada2c2

View File

@@ -3,7 +3,6 @@
#include "cmCTestConfigureCommand.h" #include "cmCTestConfigureCommand.h"
#include <chrono> #include <chrono>
#include <cstring>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <utility> #include <utility>
@@ -44,122 +43,100 @@ cmCTestConfigureCommand::InitializeHandler(HandlerArguments& arguments,
{ {
cmMakefile& mf = status.GetMakefile(); cmMakefile& mf = status.GetMakefile();
auto const& args = static_cast<ConfigureArguments&>(arguments); auto const& args = static_cast<ConfigureArguments&>(arguments);
cmList options;
if (!args.Options.empty()) { std::string const buildDirectory = !args.Build.empty()
options.assign(args.Options); ? args.Build
: mf.GetDefinition("CTEST_BINARY_DIRECTORY");
if (buildDirectory.empty()) {
status.SetError("called with no build directory specified. "
"Either use the BUILD argument or set the "
"CTEST_BINARY_DIRECTORY variable.");
return nullptr;
} }
if (this->CTest->GetCTestConfiguration("BuildDirectory").empty()) { std::string configureCommand = mf.GetDefinition("CTEST_CONFIGURE_COMMAND");
if (configureCommand.empty()) {
cmValue cmakeGenerator = mf.GetDefinition("CTEST_CMAKE_GENERATOR");
if (!cmNonempty(cmakeGenerator)) {
status.SetError( status.SetError(
"Build directory not specified. Either use BUILD " "called with no configure command specified. "
"argument to CTEST_CONFIGURE command or set CTEST_BINARY_DIRECTORY " "If this is a \"built with CMake\" project, set "
"variable"); "CTEST_CMAKE_GENERATOR. If not, set CTEST_CONFIGURE_COMMAND.");
return nullptr; return nullptr;
} }
cmValue ctestConfigureCommand = mf.GetDefinition("CTEST_CONFIGURE_COMMAND"); std::string const sourceDirectory = !args.Source.empty()
? args.Source
if (cmNonempty(ctestConfigureCommand)) { : mf.GetDefinition("CTEST_SOURCE_DIRECTORY");
this->CTest->SetCTestConfiguration("ConfigureCommand", if (sourceDirectory.empty() ||
*ctestConfigureCommand, args.Quiet); !cmSystemTools::FileExists(sourceDirectory + "/CMakeLists.txt")) {
} else { status.SetError("called with invalid source directory. "
cmValue cmakeGeneratorName = mf.GetDefinition("CTEST_CMAKE_GENERATOR"); "CTEST_SOURCE_DIRECTORY must be set to a directory "
if (cmNonempty(cmakeGeneratorName)) { "that contains CMakeLists.txt.");
std::string const& source_dir =
this->CTest->GetCTestConfiguration("SourceDirectory");
if (source_dir.empty()) {
status.SetError(
"Source directory not specified. Either use SOURCE "
"argument to CTEST_CONFIGURE command or set CTEST_SOURCE_DIRECTORY "
"variable");
return nullptr; return nullptr;
} }
std::string const cmlName = mf.GetSafeDefinition("CMAKE_LIST_FILE_NAME"); bool const multiConfig = [&]() -> bool {
std::string const cmakelists_file = cmStrCat( cmake* cm = mf.GetCMakeInstance();
source_dir, "/", cmlName.empty() ? "CMakeLists.txt" : cmlName); auto gg = cm->CreateGlobalGenerator(cmakeGenerator);
if (!cmSystemTools::FileExists(cmakelists_file)) { return gg && gg->IsMultiConfig();
std::ostringstream e; }();
e << "CMakeLists.txt file does not exist [" << cmakelists_file << "]";
status.SetError(e.str());
return nullptr;
}
bool multiConfig = false; bool const buildTypeInOptions =
bool cmakeBuildTypeInOptions = false; args.Options.find("CMAKE_BUILD_TYPE=") != std::string::npos ||
args.Options.find("CMAKE_BUILD_TYPE:STRING=") != std::string::npos;
auto gg = configureCommand = cmStrCat('"', cmSystemTools::GetCMakeCommand(), '"');
mf.GetCMakeInstance()->CreateGlobalGenerator(*cmakeGeneratorName);
if (gg) {
multiConfig = gg->IsMultiConfig();
gg.reset();
}
std::string cmakeConfigureCommand =
cmStrCat('"', cmSystemTools::GetCMakeCommand(), '"');
auto const options = cmList(args.Options);
for (std::string const& option : options) { for (std::string const& option : options) {
cmakeConfigureCommand += " \""; configureCommand += " \"";
cmakeConfigureCommand += option; configureCommand += option;
cmakeConfigureCommand += "\""; configureCommand += "\"";
if ((nullptr != strstr(option.c_str(), "CMAKE_BUILD_TYPE=")) ||
(nullptr != strstr(option.c_str(), "CMAKE_BUILD_TYPE:STRING="))) {
cmakeBuildTypeInOptions = true;
}
} }
if (!multiConfig && !cmakeBuildTypeInOptions && cmValue cmakeBuildType = mf.GetDefinition("CTEST_CONFIGURATION_TYPE");
!this->CTest->GetConfigType().empty()) { if (!multiConfig && !buildTypeInOptions && cmNonempty(cmakeBuildType)) {
cmakeConfigureCommand += " \"-DCMAKE_BUILD_TYPE:STRING="; configureCommand += " \"-DCMAKE_BUILD_TYPE:STRING=";
cmakeConfigureCommand += this->CTest->GetConfigType(); configureCommand += cmakeBuildType;
cmakeConfigureCommand += "\""; configureCommand += "\"";
} }
if (mf.IsOn("CTEST_USE_LAUNCHERS")) { if (mf.IsOn("CTEST_USE_LAUNCHERS")) {
cmakeConfigureCommand += " \"-DCTEST_USE_LAUNCHERS:BOOL=TRUE\""; configureCommand += " \"-DCTEST_USE_LAUNCHERS:BOOL=TRUE\"";
} }
cmakeConfigureCommand += " \"-G"; configureCommand += " \"-G";
cmakeConfigureCommand += *cmakeGeneratorName; configureCommand += cmakeGenerator;
cmakeConfigureCommand += "\""; configureCommand += "\"";
cmValue cmakeGeneratorPlatform = cmValue cmakeGeneratorPlatform =
mf.GetDefinition("CTEST_CMAKE_GENERATOR_PLATFORM"); mf.GetDefinition("CTEST_CMAKE_GENERATOR_PLATFORM");
if (cmNonempty(cmakeGeneratorPlatform)) { if (cmNonempty(cmakeGeneratorPlatform)) {
cmakeConfigureCommand += " \"-A"; configureCommand += " \"-A";
cmakeConfigureCommand += *cmakeGeneratorPlatform; configureCommand += *cmakeGeneratorPlatform;
cmakeConfigureCommand += "\""; configureCommand += "\"";
} }
cmValue cmakeGeneratorToolset = cmValue cmakeGeneratorToolset =
mf.GetDefinition("CTEST_CMAKE_GENERATOR_TOOLSET"); mf.GetDefinition("CTEST_CMAKE_GENERATOR_TOOLSET");
if (cmNonempty(cmakeGeneratorToolset)) { if (cmNonempty(cmakeGeneratorToolset)) {
cmakeConfigureCommand += " \"-T"; configureCommand += " \"-T";
cmakeConfigureCommand += *cmakeGeneratorToolset; configureCommand += *cmakeGeneratorToolset;
cmakeConfigureCommand += "\""; configureCommand += "\"";
} }
cmakeConfigureCommand += " \"-S"; configureCommand += " \"-S";
cmakeConfigureCommand += source_dir; configureCommand += sourceDirectory;
cmakeConfigureCommand += "\""; configureCommand += "\"";
cmakeConfigureCommand += " \"-B"; configureCommand += " \"-B";
cmakeConfigureCommand += configureCommand += buildDirectory;
this->CTest->GetCTestConfiguration("BuildDirectory"); configureCommand += "\"";
cmakeConfigureCommand += "\""; }
this->CTest->SetCTestConfiguration("ConfigureCommand", this->CTest->SetCTestConfiguration("ConfigureCommand", configureCommand,
cmakeConfigureCommand, args.Quiet); args.Quiet);
} else {
status.SetError(
"Configure command is not specified. If this is a "
"\"built with CMake\" project, set CTEST_CMAKE_GENERATOR. If not, "
"set CTEST_CONFIGURE_COMMAND.");
return nullptr;
}
}
if (cmValue labelsForSubprojects = if (cmValue labelsForSubprojects =
mf.GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) { mf.GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {