1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCTestConfigureCommand.h"
#include <chrono>
#include <cstdlib>
#include <sstream>
#include <string>
#include <vector>
#include <cm/memory>
#include <cmext/string_view>
#include "cmArgumentParser.h"
#include "cmCTest.h"
#include "cmDuration.h"
#include "cmExecutionStatus.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h"
#include "cmInstrumentation.h"
#include "cmInstrumentationQuery.h"
#include "cmList.h"
#include "cmMakefile.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmValue.h"
#include "cmXMLWriter.h"
#include "cmake.h"
bool cmCTestConfigureCommand::ExecuteConfigure(ConfigureArguments const& args,
cmExecutionStatus& status) const
{
cmMakefile& mf = status.GetMakefile();
std::string const buildDirectory = !args.Build.empty()
? 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 false;
}
std::string configureCommand = mf.GetDefinition("CTEST_CONFIGURE_COMMAND");
if (configureCommand.empty()) {
cmValue cmakeGenerator = mf.GetDefinition("CTEST_CMAKE_GENERATOR");
if (!cmNonempty(cmakeGenerator)) {
status.SetError(
"called with no configure command specified. "
"If this is a \"built with CMake\" project, set "
"CTEST_CMAKE_GENERATOR. If not, set CTEST_CONFIGURE_COMMAND.");
return false;
}
std::string const sourceDirectory = !args.Source.empty()
? args.Source
: mf.GetDefinition("CTEST_SOURCE_DIRECTORY");
if (sourceDirectory.empty() ||
!cmSystemTools::FileExists(sourceDirectory + "/CMakeLists.txt")) {
status.SetError("called with invalid source directory. "
"CTEST_SOURCE_DIRECTORY must be set to a directory "
"that contains CMakeLists.txt.");
return false;
}
bool const multiConfig = [&]() -> bool {
cmake* cm = mf.GetCMakeInstance();
auto gg = cm->CreateGlobalGenerator(cmakeGenerator);
return gg && gg->IsMultiConfig();
}();
bool const buildTypeInOptions =
args.Options.find("CMAKE_BUILD_TYPE=") != std::string::npos ||
args.Options.find("CMAKE_BUILD_TYPE:STRING=") != std::string::npos;
configureCommand = cmStrCat('"', cmSystemTools::GetCMakeCommand(), '"');
auto const options = cmList(args.Options);
for (std::string const& option : options) {
configureCommand += " \"";
configureCommand += option;
configureCommand += "\"";
}
cmValue cmakeBuildType = mf.GetDefinition("CTEST_CONFIGURATION_TYPE");
if (!multiConfig && !buildTypeInOptions && cmNonempty(cmakeBuildType)) {
configureCommand += " \"-DCMAKE_BUILD_TYPE:STRING=";
configureCommand += cmakeBuildType;
configureCommand += "\"";
}
if (mf.IsOn("CTEST_USE_LAUNCHERS")) {
configureCommand += " \"-DCTEST_USE_LAUNCHERS:BOOL=TRUE\"";
}
configureCommand += " \"-G";
configureCommand += cmakeGenerator;
configureCommand += "\"";
cmValue cmakeGeneratorPlatform =
mf.GetDefinition("CTEST_CMAKE_GENERATOR_PLATFORM");
if (cmNonempty(cmakeGeneratorPlatform)) {
configureCommand += " \"-A";
configureCommand += *cmakeGeneratorPlatform;
configureCommand += "\"";
}
cmValue cmakeGeneratorToolset =
mf.GetDefinition("CTEST_CMAKE_GENERATOR_TOOLSET");
if (cmNonempty(cmakeGeneratorToolset)) {
configureCommand += " \"-T";
configureCommand += *cmakeGeneratorToolset;
configureCommand += "\"";
}
configureCommand += " \"-S";
configureCommand += cmSystemTools::CollapseFullPath(sourceDirectory);
configureCommand += "\"";
configureCommand += " \"-B";
configureCommand += cmSystemTools::CollapseFullPath(buildDirectory);
configureCommand += "\"";
}
cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "Configure project\n",
args.Quiet);
if (this->CTest->GetShowOnly()) {
cmCTestOptionalLog(this->CTest, DEBUG,
"Configure with command: " << configureCommand << '\n',
args.Quiet);
if (!args.ReturnValue.empty()) {
mf.AddDefinition(args.ReturnValue, "0");
}
return true;
}
if (!cmSystemTools::MakeDirectory(buildDirectory)) {
status.SetError(cmStrCat("cannot create directory ", buildDirectory));
return false;
}
cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
"Configure with command: " << configureCommand << '\n',
args.Quiet);
int const submitIndex =
args.SubmitIndex.empty() ? 0 : std::atoi(args.SubmitIndex.c_str());
cmGeneratedFileStream logFile;
this->CTest->StartLogFile("Configure", submitIndex, logFile);
auto const startTime = std::chrono::system_clock::now();
auto const startDateTime = this->CTest->CurrentTime();
std::string output;
int retVal = 0;
bool const res = this->CTest->RunMakeCommand(configureCommand, output,
&retVal, buildDirectory.c_str(),
cmDuration::zero(), logFile);
auto const endTime = std::chrono::system_clock::now();
auto const endDateTime = this->CTest->CurrentTime();
auto const elapsedMinutes =
std::chrono::duration_cast<std::chrono::minutes>(endTime - startTime);
cmCTestOptionalLog(this->CTest, DEBUG, "End\n", args.Quiet);
if (!res || retVal) {
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Error(s) when configuring the project\n");
}
if (!args.ReturnValue.empty()) {
mf.AddDefinition(args.ReturnValue, std::to_string(retVal));
}
if (cmValue value = mf.GetDefinition("CTEST_CHANGE_ID")) {
this->CTest->SetCTestConfiguration("ChangeId", *value, args.Quiet);
}
if (cmValue value = mf.GetDefinition("CTEST_LABELS_FOR_SUBPROJECTS")) {
this->CTest->SetCTestConfiguration("LabelsForSubprojects", *value,
args.Quiet);
}
cmGeneratedFileStream xmlFile;
if (!this->CTest->StartResultingXML(cmCTest::PartConfigure, "Configure",
submitIndex, xmlFile)) {
cmCTestLog(this->CTest, ERROR_MESSAGE,
"Cannot open configure file" << std::endl);
return false;
}
cmXMLWriter xml(xmlFile);
this->CTest->StartXML(xml, mf.GetCMakeInstance(), args.Append);
this->CTest->GenerateSubprojectsOutput(xml);
xml.StartElement("Configure");
xml.Element("StartDateTime", startDateTime);
xml.Element("StartConfigureTime", startTime);
xml.Element("ConfigureCommand", configureCommand);
xml.Element("Log", output);
xml.Element("ConfigureStatus", retVal);
xml.Element("EndDateTime", endDateTime);
xml.Element("EndConfigureTime", endTime);
xml.Element("ElapsedMinutes", elapsedMinutes.count());
this->CTest->GetInstrumentation().CollectTimingData(
cmInstrumentationQuery::Hook::PrepareForCDash);
this->CTest->ConvertInstrumentationSnippetsToXML(xml, "configure");
xml.EndElement(); // Configure
this->CTest->EndXML(xml);
return res;
}
bool cmCTestConfigureCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus& status) const
{
using Args = ConfigureArguments;
static auto const parser =
cmArgumentParser<Args>{ MakeHandlerParser<Args>() } //
.Bind("OPTIONS"_s, &ConfigureArguments::Options);
return this->Invoke(parser, args, status, [&](ConfigureArguments& a) {
return this->ExecuteConfigure(a, status);
});
}
|