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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCTestBuildAndTest.h"
#include <chrono>
#include <cstdint>
#include <iostream>
#include <ratio>
#include <utility>
#include <cm3p/uv.h>
#include "cmBuildOptions.h"
#include "cmCTest.h"
#include "cmCTestTestHandler.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmProcessOutput.h"
#include "cmState.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmUVHandlePtr.h"
#include "cmUVProcessChain.h"
#include "cmUVStream.h"
#include "cmWorkingDirectory.h"
#include "cmake.h"
struct cmMessageMetadata;
cmCTestBuildAndTest::cmCTestBuildAndTest(cmCTest* ctest)
: CTest(ctest)
{
}
bool cmCTestBuildAndTest::RunCMake(cmake* cm)
{
std::vector<std::string> args;
args.push_back(cmSystemTools::GetCMakeCommand());
args.push_back(this->SourceDir);
if (!this->BuildGenerator.empty()) {
args.push_back("-G" + this->BuildGenerator);
}
if (!this->BuildGeneratorPlatform.empty()) {
args.push_back("-A" + this->BuildGeneratorPlatform);
}
if (!this->BuildGeneratorToolset.empty()) {
args.push_back("-T" + this->BuildGeneratorToolset);
}
char const* config = nullptr;
if (!this->CTest->GetConfigType().empty()) {
config = this->CTest->GetConfigType().c_str();
}
if (config) {
args.push_back("-DCMAKE_BUILD_TYPE:STRING=" + std::string(config));
}
if (!this->BuildMakeProgram.empty() &&
(this->BuildGenerator.find("Make") != std::string::npos ||
this->BuildGenerator.find("Ninja") != std::string::npos ||
this->BuildGenerator.find("FASTBuild") != std::string::npos)) {
args.push_back("-DCMAKE_MAKE_PROGRAM:FILEPATH=" + this->BuildMakeProgram);
}
for (std::string const& opt : this->BuildOptions) {
args.push_back(opt);
}
std::cout << "======== CMake output ======\n";
if (cm->Run(args) != 0) {
std::cout << "======== End CMake output ======\n"
"Error: cmake execution failed\n";
return false;
}
// do another config?
if (this->BuildTwoConfig) {
if (cm->Run(args) != 0) {
std::cout << "======== End CMake output ======\n"
"Error: cmake execution failed\n";
return false;
}
}
std::cout << "======== End CMake output ======\n";
return true;
}
bool cmCTestBuildAndTest::RunTest(std::vector<std::string> const& argv,
int* retVal, cmDuration timeout)
{
cmUVProcessChainBuilder builder;
builder.AddCommand(argv).SetMergedBuiltinStreams();
auto chain = builder.Start();
cmProcessOutput processOutput(cmProcessOutput::Auto);
cm::uv_pipe_ptr outputStream;
outputStream.init(chain.GetLoop(), 0);
uv_pipe_open(outputStream, chain.OutputStream());
auto outputHandle = cmUVStreamRead(
outputStream,
[&processOutput](std::vector<char> data) {
std::string decoded;
processOutput.DecodeText(data.data(), data.size(), decoded);
std::cout << decoded << std::flush;
},
[]() {});
bool complete = chain.Wait(static_cast<uint64_t>(timeout.count() * 1000.0));
bool result = false;
if (complete) {
auto const& status = chain.GetStatus(0);
auto exception = status.GetException();
switch (exception.first) {
case cmUVProcessChain::ExceptionCode::None:
*retVal = static_cast<int>(status.ExitStatus);
result = true;
break;
case cmUVProcessChain::ExceptionCode::Spawn: {
std::cout << "\n*** ERROR executing: " << exception.second;
} break;
default: {
*retVal = status.TermSignal;
std::cout << "\n*** Exception executing: " << exception.second;
} break;
}
} else {
chain.Terminate();
}
return result;
}
class cmCTestBuildAndTestCaptureRAII
{
cmake& CM;
public:
cmCTestBuildAndTestCaptureRAII(cmake& cm)
: CM(cm)
{
cmSystemTools::SetMessageCallback(
[](std::string const& msg, cmMessageMetadata const& /* unused */) {
std::cout << msg << std::endl;
});
cmSystemTools::SetStdoutCallback(
[](std::string const& m) { std::cout << m << std::flush; });
cmSystemTools::SetStderrCallback(
[](std::string const& m) { std::cout << m << std::flush; });
this->CM.SetProgressCallback([](std::string const& msg, float prog) {
if (prog < 0) {
std::cout << msg << std::endl;
}
});
}
~cmCTestBuildAndTestCaptureRAII()
{
this->CM.SetProgressCallback(nullptr);
cmSystemTools::SetStderrCallback(nullptr);
cmSystemTools::SetStdoutCallback(nullptr);
cmSystemTools::SetMessageCallback(nullptr);
}
cmCTestBuildAndTestCaptureRAII(cmCTestBuildAndTestCaptureRAII const&) =
delete;
cmCTestBuildAndTestCaptureRAII& operator=(
cmCTestBuildAndTestCaptureRAII const&) = delete;
};
int cmCTestBuildAndTest::Run()
{
// if the generator and make program are not specified then it is an error
if (this->BuildGenerator.empty()) {
std::cout << "--build-and-test requires that the generator "
"be provided using the --build-generator "
"command line option.\n";
return 1;
}
cmake cm(cmake::RoleProject, cmState::Project);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cmCTestBuildAndTestCaptureRAII captureRAII(cm);
static_cast<void>(captureRAII);
if (this->CTest->GetConfigType().empty() && !this->ConfigSample.empty()) {
// use the config sample to set the ConfigType
std::string fullPath;
std::string resultingConfig;
std::vector<std::string> extraPaths;
std::vector<std::string> failed;
fullPath = cmCTestTestHandler::FindExecutable(
this->CTest, this->ConfigSample, resultingConfig, extraPaths, failed);
if (!fullPath.empty() && !resultingConfig.empty()) {
this->CTest->SetConfigType(resultingConfig);
}
std::cout << "Using config sample with results: " << fullPath << " and "
<< resultingConfig << std::endl;
}
// we need to honor the timeout specified, the timeout include cmake, build
// and test time
auto clock_start = std::chrono::steady_clock::now();
// make sure the binary dir is there
std::cout << "Internal cmake changing into directory: " << this->BinaryDir
<< std::endl;
if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) {
cmSystemTools::MakeDirectory(this->BinaryDir);
}
cmWorkingDirectory workdir(this->BinaryDir);
if (workdir.Failed()) {
std::cout << workdir.GetError() << '\n';
return 1;
}
if (this->BuildNoCMake) {
// Make the generator available for the Build call below.
cm.SetGlobalGenerator(cm.CreateGlobalGenerator(this->BuildGenerator));
if (!this->BuildGeneratorPlatform.empty()) {
cmMakefile mf(cm.GetGlobalGenerator(), cm.GetCurrentSnapshot());
if (!cm.GetGlobalGenerator()->SetGeneratorPlatform(
this->BuildGeneratorPlatform, &mf)) {
return 1;
}
}
// Load the cache to make CMAKE_MAKE_PROGRAM available.
cm.LoadCache(this->BinaryDir);
} else {
// do the cmake step, no timeout here since it is not a sub process
if (!this->RunCMake(&cm)) {
return 1;
}
}
// do the build
if (this->BuildTargets.empty()) {
this->BuildTargets.emplace_back();
}
for (std::string const& tar : this->BuildTargets) {
cmDuration remainingTime = std::chrono::seconds(0);
if (this->Timeout > cmDuration::zero()) {
remainingTime =
this->Timeout - (std::chrono::steady_clock::now() - clock_start);
if (remainingTime <= std::chrono::seconds(0)) {
std::cout << "--build-and-test timeout exceeded. ";
return 1;
}
}
char const* config = nullptr;
if (!this->CTest->GetConfigType().empty()) {
config = this->CTest->GetConfigType().c_str();
}
if (!config) {
config = "Debug";
}
cmBuildOptions buildOptions(!this->BuildNoClean, false,
PackageResolveMode::Disable);
int retVal = cm.GetGlobalGenerator()->Build(
cmake::NO_BUILD_PARALLEL_LEVEL, this->SourceDir, this->BinaryDir,
this->BuildProject, { tar }, std::cout, this->BuildMakeProgram, config,
buildOptions, false, remainingTime, cmSystemTools::OUTPUT_PASSTHROUGH);
// if the build failed then return
if (retVal) {
return 1;
}
}
// if no test was specified then we are done
if (this->TestCommand.empty()) {
return 0;
}
// now run the compiled test if we can find it
// store the final location in fullPath
std::string fullPath;
std::string resultingConfig;
std::vector<std::string> extraPaths;
// if this->ExecutableDirectory is set try that as well
if (!this->ExecutableDirectory.empty()) {
std::string tempPath =
cmStrCat(this->ExecutableDirectory, '/', this->TestCommand);
extraPaths.push_back(tempPath);
}
std::vector<std::string> failed;
fullPath = cmCTestTestHandler::FindExecutable(
this->CTest, this->TestCommand, resultingConfig, extraPaths, failed);
if (!cmSystemTools::FileExists(fullPath)) {
std::cout
<< "Could not find path to executable, perhaps it was not built: "
<< this->TestCommand << "\n"
<< "tried to find it in these places:\n"
<< fullPath << '\n';
for (std::string const& fail : failed) {
std::cout << fail << '\n';
}
return 1;
}
std::vector<std::string> testCommand;
testCommand.push_back(fullPath);
for (std::string const& testCommandArg : this->TestCommandArgs) {
testCommand.push_back(testCommandArg);
}
int retval = 0;
// run the test from the this->BuildRunDir if set
if (!this->BuildRunDir.empty()) {
std::cout << "Run test in directory: " << this->BuildRunDir << '\n';
if (!workdir.SetDirectory(this->BuildRunDir)) {
std::cout << workdir.GetError() << '\n';
return 1;
}
}
std::cout << "Running test command: \"" << fullPath << '"';
for (std::string const& testCommandArg : this->TestCommandArgs) {
std::cout << " \"" << testCommandArg << '"';
}
std::cout << '\n';
// how much time is remaining
cmDuration remainingTime = std::chrono::seconds(0);
if (this->Timeout > cmDuration::zero()) {
remainingTime =
this->Timeout - (std::chrono::steady_clock::now() - clock_start);
if (remainingTime <= std::chrono::seconds(0)) {
std::cout << "--build-and-test timeout exceeded. ";
return 1;
}
}
bool runTestRes = this->RunTest(testCommand, &retval, remainingTime);
if (!runTestRes || retval != 0) {
std::cout << "\nTest command failed: " << testCommand[0] << '\n';
retval = 1;
}
return retval;
}
|