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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#include "cmCTestLaunch.h"
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <utility>
#include <cm3p/uv.h>
#include "cmsys/FStream.hxx"
#include "cmsys/RegularExpression.hxx"
#include "cmCTestLaunchReporter.h"
#include "cmGlobalGenerator.h"
#include "cmInstrumentation.h"
#include "cmMakefile.h"
#include "cmProcessOutput.h"
#include "cmState.h"
#include "cmStateSnapshot.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
#include "cmUVHandlePtr.h"
#include "cmUVProcessChain.h"
#include "cmUVStream.h"
#include "cmake.h"
#ifdef _WIN32
# include <cstdio> // for std{out,err} and fileno
# include <fcntl.h> // for _O_BINARY
# include <io.h> // for _setmode
#endif
cmCTestLaunch::cmCTestLaunch(int argc, char const* const* argv, Op operation)
{
if (!this->ParseArguments(argc, argv)) {
return;
}
this->Reporter.RealArgs = this->RealArgs;
this->Reporter.ComputeFileNames();
this->ScrapeRulesLoaded = false;
this->HaveOut = false;
this->HaveErr = false;
this->Operation = operation;
}
cmCTestLaunch::~cmCTestLaunch() = default;
bool cmCTestLaunch::ParseArguments(int argc, char const* const* argv)
{
// Launcher options occur first and are separated from the real
// command line by a '--' option.
enum Doing
{
DoingNone,
DoingOutput,
DoingSource,
DoingLanguage,
DoingTargetLabels,
DoingTargetName,
DoingTargetType,
DoingCommandType,
DoingRole,
DoingBuildDir,
DoingCurrentBuildDir,
DoingCount,
DoingFilterPrefix,
DoingConfig
};
Doing doing = DoingNone;
int arg0 = 0;
for (int i = 1; !arg0 && i < argc; ++i) {
char const* arg = argv[i];
if (strcmp(arg, "--") == 0) {
arg0 = i + 1;
} else if (strcmp(arg, "--command-type") == 0) {
doing = DoingCommandType;
} else if (strcmp(arg, "--output") == 0) {
doing = DoingOutput;
} else if (strcmp(arg, "--source") == 0) {
doing = DoingSource;
} else if (strcmp(arg, "--language") == 0) {
doing = DoingLanguage;
} else if (strcmp(arg, "--target-labels") == 0) {
doing = DoingTargetLabels;
} else if (strcmp(arg, "--target-name") == 0) {
doing = DoingTargetName;
} else if (strcmp(arg, "--target-type") == 0) {
doing = DoingTargetType;
} else if (strcmp(arg, "--role") == 0) {
doing = DoingRole;
} else if (strcmp(arg, "--build-dir") == 0) {
doing = DoingBuildDir;
} else if (strcmp(arg, "--current-build-dir") == 0) {
doing = DoingCurrentBuildDir;
} else if (strcmp(arg, "--filter-prefix") == 0) {
doing = DoingFilterPrefix;
} else if (strcmp(arg, "--config") == 0) {
doing = DoingConfig;
} else if (doing == DoingOutput) {
this->Reporter.OptionOutput = arg;
doing = DoingNone;
} else if (doing == DoingSource) {
this->Reporter.OptionSource = arg;
doing = DoingNone;
} else if (doing == DoingLanguage) {
this->Reporter.OptionLanguage = arg;
if (this->Reporter.OptionLanguage == "CXX") {
this->Reporter.OptionLanguage = "C++";
}
doing = DoingNone;
} else if (doing == DoingTargetLabels) {
this->Reporter.OptionTargetLabels = arg;
doing = DoingNone;
} else if (doing == DoingTargetName) {
this->Reporter.OptionTargetName = arg;
doing = DoingNone;
} else if (doing == DoingTargetType) {
this->Reporter.OptionTargetType = arg;
doing = DoingNone;
} else if (doing == DoingBuildDir) {
this->Reporter.OptionBuildDir = arg;
doing = DoingNone;
} else if (doing == DoingCurrentBuildDir) {
this->Reporter.OptionCurrentBuildDir = arg;
doing = DoingNone;
} else if (doing == DoingFilterPrefix) {
this->Reporter.OptionFilterPrefix = arg;
doing = DoingNone;
} else if (doing == DoingCommandType) {
this->Reporter.OptionCommandType = arg;
doing = DoingNone;
} else if (doing == DoingRole) {
this->Reporter.OptionRole = arg;
doing = DoingNone;
} else if (doing == DoingConfig) {
this->Reporter.OptionConfig = arg;
doing = DoingNone;
}
}
// Extract the real command line.
if (arg0) {
for (int i = 0; i < argc - arg0; ++i) {
this->RealArgV.emplace_back((argv + arg0)[i]);
this->HandleRealArg((argv + arg0)[i]);
}
return true;
}
std::cerr << "No launch/command separator ('--') found!\n";
return false;
}
void cmCTestLaunch::HandleRealArg(char const* arg)
{
#ifdef _WIN32
// Expand response file arguments.
if (arg[0] == '@' && cmSystemTools::FileExists(arg + 1)) {
cmsys::ifstream fin(arg + 1);
std::string line;
while (cmSystemTools::GetLineFromStream(fin, line)) {
cmSystemTools::ParseWindowsCommandLine(line.c_str(), this->RealArgs);
}
return;
}
#endif
this->RealArgs.emplace_back(arg);
}
void cmCTestLaunch::RunChild()
{
// Ignore noopt make rules
if (this->RealArgs.empty() || this->RealArgs[0] == ":") {
this->Reporter.ExitCode = 0;
return;
}
// Prepare to run the real command.
cmUVProcessChainBuilder builder;
builder.AddCommand(this->RealArgV);
cmsys::ofstream fout;
cmsys::ofstream ferr;
if (this->Reporter.Passthru) {
// In passthru mode we just share the output pipes.
builder.SetExternalStream(cmUVProcessChainBuilder::Stream_OUTPUT, stdout)
.SetExternalStream(cmUVProcessChainBuilder::Stream_ERROR, stderr);
} else {
// In full mode we record the child output pipes to log files.
builder.SetBuiltinStream(cmUVProcessChainBuilder::Stream_OUTPUT)
.SetBuiltinStream(cmUVProcessChainBuilder::Stream_ERROR);
fout.open(this->Reporter.LogOut.c_str(), std::ios::out | std::ios::binary);
ferr.open(this->Reporter.LogErr.c_str(), std::ios::out | std::ios::binary);
}
#ifdef _WIN32
// Do this so that newline transformation is not done when writing to cout
// and cerr below.
_setmode(fileno(stdout), _O_BINARY);
_setmode(fileno(stderr), _O_BINARY);
#endif
// Run the real command.
auto chain = builder.Start();
// Record child stdout and stderr if necessary.
cm::uv_pipe_ptr outPipe;
cm::uv_pipe_ptr errPipe;
bool outFinished = true;
bool errFinished = true;
cmProcessOutput processOutput;
std::unique_ptr<cmUVStreamReadHandle> outputHandle;
std::unique_ptr<cmUVStreamReadHandle> errorHandle;
if (!this->Reporter.Passthru) {
auto beginRead = [&chain, &processOutput](
cm::uv_pipe_ptr& pipe, int stream, std::ostream& out,
cmsys::ofstream& file, bool& haveData, bool& finished,
int id) -> std::unique_ptr<cmUVStreamReadHandle> {
pipe.init(chain.GetLoop(), 0);
uv_pipe_open(pipe, stream);
finished = false;
return cmUVStreamRead(
pipe,
[&processOutput, &out, &file, id, &haveData](std::vector<char> data) {
std::string strdata;
processOutput.DecodeText(data.data(), data.size(), strdata, id);
file.write(strdata.c_str(), strdata.size());
out.write(strdata.c_str(), strdata.size());
haveData = true;
},
[&processOutput, &out, &file, &finished, id]() {
std::string strdata;
processOutput.DecodeText(std::string(), strdata, id);
if (!strdata.empty()) {
file.write(strdata.c_str(), strdata.size());
out.write(strdata.c_str(), strdata.size());
}
finished = true;
});
};
outputHandle = beginRead(outPipe, chain.OutputStream(), std::cout, fout,
this->HaveOut, outFinished, 1);
errorHandle = beginRead(errPipe, chain.ErrorStream(), std::cerr, ferr,
this->HaveErr, errFinished, 2);
}
// Wait for the real command to finish.
while (!(chain.Finished() && outFinished && errFinished)) {
uv_run(&chain.GetLoop(), UV_RUN_ONCE);
}
this->Reporter.Status = chain.GetStatus(0);
if (this->Reporter.Status.GetException().first ==
cmUVProcessChain::ExceptionCode::Spawn) {
this->Reporter.ExitCode = 1;
} else {
this->Reporter.ExitCode =
static_cast<int>(this->Reporter.Status.ExitStatus);
}
}
int cmCTestLaunch::Run()
{
auto instrumentation = cmInstrumentation(this->Reporter.OptionBuildDir);
std::map<std::string, std::string> options;
if (this->Reporter.OptionTargetName != "TARGET_NAME") {
options["target"] = this->Reporter.OptionTargetName;
}
options["source"] = this->Reporter.OptionSource;
options["language"] = this->Reporter.OptionLanguage;
options["targetType"] = this->Reporter.OptionTargetType;
options["role"] = this->Reporter.OptionRole;
options["config"] = this->Reporter.OptionConfig;
std::map<std::string, std::string> arrayOptions;
arrayOptions["outputs"] = this->Reporter.OptionOutput;
arrayOptions["targetLabels"] = this->Reporter.OptionTargetLabels;
instrumentation.InstrumentCommand(
this->Reporter.OptionCommandType, this->RealArgV,
[this]() -> int {
this->RunChild();
return 0;
},
options, arrayOptions);
if (this->Operation == Op::Normal) {
if (this->CheckResults()) {
return this->Reporter.ExitCode;
}
this->LoadConfig();
this->Reporter.WriteXML();
}
return this->Reporter.ExitCode;
}
bool cmCTestLaunch::CheckResults()
{
// Skip XML in passthru mode.
if (this->Reporter.Passthru) {
return true;
}
// We always report failure for error conditions.
if (this->Reporter.IsError()) {
return false;
}
// Scrape the output logs to look for warnings.
if ((this->HaveErr && this->ScrapeLog(this->Reporter.LogErr)) ||
(this->HaveOut && this->ScrapeLog(this->Reporter.LogOut))) {
return false;
}
return true;
}
void cmCTestLaunch::LoadScrapeRules()
{
if (this->ScrapeRulesLoaded) {
return;
}
this->ScrapeRulesLoaded = true;
// Load custom match rules given to us by CTest.
this->LoadScrapeRules("Warning", this->Reporter.RegexWarning);
this->LoadScrapeRules("WarningSuppress",
this->Reporter.RegexWarningSuppress);
}
void cmCTestLaunch::LoadScrapeRules(
char const* purpose, std::vector<cmsys::RegularExpression>& regexps) const
{
std::string fname =
cmStrCat(this->Reporter.LogDir, "Custom", purpose, ".txt");
cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
std::string line;
cmsys::RegularExpression rex;
while (cmSystemTools::GetLineFromStream(fin, line)) {
if (rex.compile(line)) {
regexps.push_back(rex);
}
}
}
bool cmCTestLaunch::ScrapeLog(std::string const& fname)
{
this->LoadScrapeRules();
// Look for log file lines matching warning expressions but not
// suppression expressions.
cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
std::string line;
while (cmSystemTools::GetLineFromStream(fin, line)) {
if (this->Reporter.MatchesFilterPrefix(line)) {
continue;
}
if (this->Reporter.Match(line, this->Reporter.RegexWarning) &&
!this->Reporter.Match(line, this->Reporter.RegexWarningSuppress)) {
return true;
}
}
return false;
}
int cmCTestLaunch::Main(int argc, char const* const argv[], Op operation)
{
if (argc == 2) {
std::cerr << "ctest --launch: this mode is for internal CTest use only"
<< std::endl;
return 1;
}
cmCTestLaunch self(argc, argv, operation);
return self.Run();
}
void cmCTestLaunch::LoadConfig()
{
cmake cm(cmake::RoleScript, cmState::CTest);
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
cm.GetCurrentSnapshot().SetDefaultDefinitions();
cmGlobalGenerator gg(&cm);
cmMakefile mf(&gg, cm.GetCurrentSnapshot());
std::string fname =
cmStrCat(this->Reporter.LogDir, "CTestLaunchConfig.cmake");
if (cmSystemTools::FileExists(fname) && mf.ReadListFile(fname)) {
this->Reporter.SourceDir = mf.GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
cmSystemTools::ConvertToUnixSlashes(this->Reporter.SourceDir);
}
}
|