cmake/Source/CTest/cmCTestLaunch.cxx

329 lines
9.1 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. */
2009-10-04 10:30:41 +03:00
#include "cmCTestLaunch.h"
2020-02-01 23:06:01 +01:00
#include <cstring>
#include <iostream>
2017-07-20 19:35:53 +02:00
#include "cmsys/FStream.hxx"
#include "cmsys/Process.h"
#include "cmsys/RegularExpression.hxx"
2009-10-04 10:30:41 +03:00
2021-09-14 00:13:48 +02:00
#include "cmCTestLaunchReporter.h"
2017-04-14 19:02:05 +02:00
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmProcessOutput.h"
2019-11-11 23:01:05 +01:00
#include "cmState.h"
2017-04-14 19:02:05 +02:00
#include "cmStateSnapshot.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
#include "cmake.h"
2015-04-27 22:25:09 +02:00
#ifdef _WIN32
2018-08-09 18:06:22 +02:00
# include <fcntl.h> // for _O_BINARY
# include <io.h> // for _setmode
# include <stdio.h> // for std{out,err} and fileno
2015-04-27 22:25:09 +02:00
#endif
2009-10-04 10:30:41 +03:00
cmCTestLaunch::cmCTestLaunch(int argc, const char* const* argv)
{
2018-01-26 17:06:56 +01:00
this->Process = nullptr;
2009-10-04 10:30:41 +03:00
2016-07-09 11:21:54 +02:00
if (!this->ParseArguments(argc, argv)) {
2009-10-04 10:30:41 +03:00
return;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2021-09-14 00:13:48 +02:00
this->Reporter.RealArgs = this->RealArgs;
this->Reporter.ComputeFileNames();
2009-10-04 10:30:41 +03:00
this->ScrapeRulesLoaded = false;
this->HaveOut = false;
this->HaveErr = false;
this->Process = cmsysProcess_New();
}
cmCTestLaunch::~cmCTestLaunch()
{
cmsysProcess_Delete(this->Process);
}
bool cmCTestLaunch::ParseArguments(int argc, const char* const* argv)
{
// Launcher options occur first and are separated from the real
// command line by a '--' option.
2016-07-09 11:21:54 +02:00
enum Doing
{
DoingNone,
DoingOutput,
DoingSource,
DoingLanguage,
DoingTargetName,
DoingTargetType,
DoingBuildDir,
DoingCount,
DoingFilterPrefix
};
2009-10-04 10:30:41 +03:00
Doing doing = DoingNone;
int arg0 = 0;
2016-07-09 11:21:54 +02:00
for (int i = 1; !arg0 && i < argc; ++i) {
2009-10-04 10:30:41 +03:00
const char* arg = argv[i];
2016-07-09 11:21:54 +02:00
if (strcmp(arg, "--") == 0) {
arg0 = i + 1;
} else if (strcmp(arg, "--output") == 0) {
2009-10-04 10:30:41 +03:00
doing = DoingOutput;
2016-07-09 11:21:54 +02:00
} else if (strcmp(arg, "--source") == 0) {
2009-10-04 10:30:41 +03:00
doing = DoingSource;
2016-07-09 11:21:54 +02:00
} else if (strcmp(arg, "--language") == 0) {
2009-10-04 10:30:41 +03:00
doing = DoingLanguage;
2016-07-09 11:21:54 +02:00
} else if (strcmp(arg, "--target-name") == 0) {
2009-10-04 10:30:41 +03:00
doing = DoingTargetName;
2016-07-09 11:21:54 +02:00
} else if (strcmp(arg, "--target-type") == 0) {
2009-10-04 10:30:41 +03:00
doing = DoingTargetType;
2016-07-09 11:21:54 +02:00
} else if (strcmp(arg, "--build-dir") == 0) {
2009-10-04 10:30:41 +03:00
doing = DoingBuildDir;
2016-07-09 11:21:54 +02:00
} else if (strcmp(arg, "--filter-prefix") == 0) {
2014-08-03 19:52:23 +02:00
doing = DoingFilterPrefix;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingOutput) {
2021-09-14 00:13:48 +02:00
this->Reporter.OptionOutput = arg;
2009-10-04 10:30:41 +03:00
doing = DoingNone;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingSource) {
2021-09-14 00:13:48 +02:00
this->Reporter.OptionSource = arg;
2009-10-04 10:30:41 +03:00
doing = DoingNone;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingLanguage) {
2021-09-14 00:13:48 +02:00
this->Reporter.OptionLanguage = arg;
if (this->Reporter.OptionLanguage == "CXX") {
this->Reporter.OptionLanguage = "C++";
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
doing = DoingNone;
} else if (doing == DoingTargetName) {
2021-09-14 00:13:48 +02:00
this->Reporter.OptionTargetName = arg;
2009-10-04 10:30:41 +03:00
doing = DoingNone;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingTargetType) {
2021-09-14 00:13:48 +02:00
this->Reporter.OptionTargetType = arg;
2009-10-04 10:30:41 +03:00
doing = DoingNone;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingBuildDir) {
2021-09-14 00:13:48 +02:00
this->Reporter.OptionBuildDir = arg;
2009-10-04 10:30:41 +03:00
doing = DoingNone;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingFilterPrefix) {
2021-09-14 00:13:48 +02:00
this->Reporter.OptionFilterPrefix = arg;
2014-08-03 19:52:23 +02:00
doing = DoingNone;
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// Extract the real command line.
2016-07-09 11:21:54 +02:00
if (arg0) {
2009-10-04 10:30:41 +03:00
this->RealArgC = argc - arg0;
this->RealArgV = argv + arg0;
2016-07-09 11:21:54 +02:00
for (int i = 0; i < this->RealArgC; ++i) {
2009-10-04 10:30:41 +03:00
this->HandleRealArg(this->RealArgV[i]);
}
2016-07-09 11:21:54 +02:00
return true;
}
2016-10-30 18:24:19 +01:00
this->RealArgC = 0;
2018-01-26 17:06:56 +01:00
this->RealArgV = nullptr;
2016-10-30 18:24:19 +01:00
std::cerr << "No launch/command separator ('--') found!\n";
return false;
2009-10-04 10:30:41 +03:00
}
void cmCTestLaunch::HandleRealArg(const char* arg)
{
#ifdef _WIN32
// Expand response file arguments.
2016-07-09 11:21:54 +02:00
if (arg[0] == '@' && cmSystemTools::FileExists(arg + 1)) {
cmsys::ifstream fin(arg + 1);
2009-10-04 10:30:41 +03:00
std::string line;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(fin, line)) {
2009-10-04 10:30:41 +03:00
cmSystemTools::ParseWindowsCommandLine(line.c_str(), this->RealArgs);
}
2016-07-09 11:21:54 +02:00
return;
}
2009-10-04 10:30:41 +03:00
#endif
2019-11-11 23:01:05 +01:00
this->RealArgs.emplace_back(arg);
2009-10-04 10:30:41 +03:00
}
void cmCTestLaunch::RunChild()
{
2011-01-16 11:35:12 +01:00
// Ignore noopt make rules
2016-07-09 11:21:54 +02:00
if (this->RealArgs.empty() || this->RealArgs[0] == ":") {
2021-09-14 00:13:48 +02:00
this->Reporter.ExitCode = 0;
2011-01-16 11:35:12 +01:00
return;
2016-07-09 11:21:54 +02:00
}
2011-01-16 11:35:12 +01:00
2009-10-04 10:30:41 +03:00
// Prepare to run the real command.
cmsysProcess* cp = this->Process;
cmsysProcess_SetCommand(cp, this->RealArgV);
2014-08-03 19:52:23 +02:00
cmsys::ofstream fout;
cmsys::ofstream ferr;
2021-09-14 00:13:48 +02:00
if (this->Reporter.Passthru) {
2009-10-04 10:30:41 +03:00
// In passthru mode we just share the output pipes.
cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
2016-07-09 11:21:54 +02:00
} else {
2009-10-04 10:30:41 +03:00
// In full mode we record the child output pipes to log files.
2021-09-14 00:13:48 +02:00
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);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2015-04-27 22:25:09 +02:00
#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
2009-10-04 10:30:41 +03:00
// Run the real command.
cmsysProcess_Execute(cp);
// Record child stdout and stderr if necessary.
2021-09-14 00:13:48 +02:00
if (!this->Reporter.Passthru) {
2018-01-26 17:06:56 +01:00
char* data = nullptr;
2009-10-04 10:30:41 +03:00
int length = 0;
2017-04-14 19:02:05 +02:00
cmProcessOutput processOutput;
std::string strdata;
2018-01-26 17:06:56 +01:00
while (int p = cmsysProcess_WaitForData(cp, &data, &length, nullptr)) {
2016-07-09 11:21:54 +02:00
if (p == cmsysProcess_Pipe_STDOUT) {
2017-04-14 19:02:05 +02:00
processOutput.DecodeText(data, length, strdata, 1);
fout.write(strdata.c_str(), strdata.size());
std::cout.write(strdata.c_str(), strdata.size());
2009-10-04 10:30:41 +03:00
this->HaveOut = true;
2016-07-09 11:21:54 +02:00
} else if (p == cmsysProcess_Pipe_STDERR) {
2017-04-14 19:02:05 +02:00
processOutput.DecodeText(data, length, strdata, 2);
ferr.write(strdata.c_str(), strdata.size());
std::cerr.write(strdata.c_str(), strdata.size());
2009-10-04 10:30:41 +03:00
this->HaveErr = true;
}
}
2017-04-14 19:02:05 +02:00
processOutput.DecodeText(std::string(), strdata, 1);
if (!strdata.empty()) {
fout.write(strdata.c_str(), strdata.size());
std::cout.write(strdata.c_str(), strdata.size());
}
processOutput.DecodeText(std::string(), strdata, 2);
if (!strdata.empty()) {
ferr.write(strdata.c_str(), strdata.size());
std::cerr.write(strdata.c_str(), strdata.size());
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// Wait for the real command to finish.
2018-01-26 17:06:56 +01:00
cmsysProcess_WaitForExit(cp, nullptr);
2021-09-14 00:13:48 +02:00
this->Reporter.ExitCode = cmsysProcess_GetExitValue(cp);
2009-10-04 10:30:41 +03:00
}
int cmCTestLaunch::Run()
{
2016-07-09 11:21:54 +02:00
if (!this->Process) {
2009-10-04 10:30:41 +03:00
std::cerr << "Could not allocate cmsysProcess instance!\n";
return -1;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
this->RunChild();
2016-07-09 11:21:54 +02:00
if (this->CheckResults()) {
2021-09-14 00:13:48 +02:00
return this->Reporter.ExitCode;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
this->LoadConfig();
2021-09-14 00:13:48 +02:00
this->Reporter.Process = this->Process;
this->Reporter.WriteXML();
2009-10-04 10:30:41 +03:00
2021-09-14 00:13:48 +02:00
return this->Reporter.ExitCode;
2009-10-04 10:30:41 +03:00
}
bool cmCTestLaunch::CheckResults()
{
// Skip XML in passthru mode.
2021-09-14 00:13:48 +02:00
if (this->Reporter.Passthru) {
2009-10-04 10:30:41 +03:00
return true;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// We always report failure for error conditions.
2021-09-14 00:13:48 +02:00
if (this->Reporter.IsError()) {
2009-10-04 10:30:41 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
// Scrape the output logs to look for warnings.
2021-09-14 00:13:48 +02:00
if ((this->HaveErr && this->ScrapeLog(this->Reporter.LogErr)) ||
(this->HaveOut && this->ScrapeLog(this->Reporter.LogOut))) {
2009-10-04 10:30:41 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return true;
}
void cmCTestLaunch::LoadScrapeRules()
{
2016-07-09 11:21:54 +02:00
if (this->ScrapeRulesLoaded) {
2009-10-04 10:30:41 +03:00
return;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
this->ScrapeRulesLoaded = true;
// Load custom match rules given to us by CTest.
2021-09-14 00:13:48 +02:00
this->LoadScrapeRules("Warning", this->Reporter.RegexWarning);
this->LoadScrapeRules("WarningSuppress",
this->Reporter.RegexWarningSuppress);
2009-10-04 10:30:41 +03:00
}
2016-07-09 11:21:54 +02:00
void cmCTestLaunch::LoadScrapeRules(
2021-09-14 00:13:48 +02:00
const char* purpose, std::vector<cmsys::RegularExpression>& regexps) const
2009-10-04 10:30:41 +03:00
{
2021-09-14 00:13:48 +02:00
std::string fname =
cmStrCat(this->Reporter.LogDir, "Custom", purpose, ".txt");
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
2009-10-04 10:30:41 +03:00
std::string line;
cmsys::RegularExpression rex;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(fin, line)) {
2018-04-23 21:13:27 +02:00
if (rex.compile(line)) {
2009-10-04 10:30:41 +03:00
regexps.push_back(rex);
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}
bool cmCTestLaunch::ScrapeLog(std::string const& fname)
{
this->LoadScrapeRules();
// Look for log file lines matching warning expressions but not
// suppression expressions.
2014-08-03 19:52:23 +02:00
cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
2009-10-04 10:30:41 +03:00
std::string line;
2016-07-09 11:21:54 +02:00
while (cmSystemTools::GetLineFromStream(fin, line)) {
2021-09-14 00:13:48 +02:00
if (this->Reporter.MatchesFilterPrefix(line)) {
2014-08-03 19:52:23 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2021-09-14 00:13:48 +02:00
if (this->Reporter.Match(line, this->Reporter.RegexWarning) &&
!this->Reporter.Match(line, this->Reporter.RegexWarningSuppress)) {
2009-10-04 10:30:41 +03:00
return true;
}
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
return false;
}
int cmCTestLaunch::Main(int argc, const char* const argv[])
{
2016-07-09 11:21:54 +02:00
if (argc == 2) {
2009-10-04 10:30:41 +03:00
std::cerr << "ctest --launch: this mode is for internal CTest use only"
<< std::endl;
return 1;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
cmCTestLaunch self(argc, argv);
return self.Run();
}
void cmCTestLaunch::LoadConfig()
{
2019-11-11 23:01:05 +01:00
cmake cm(cmake::RoleScript, cmState::CTest);
2015-08-17 11:37:30 +02:00
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
2016-03-13 13:35:51 +01:00
cm.GetCurrentSnapshot().SetDefaultDefinitions();
2015-08-17 11:37:30 +02:00
cmGlobalGenerator gg(&cm);
2018-01-26 17:06:56 +01:00
cmMakefile mf(&gg, cm.GetCurrentSnapshot());
2021-09-14 00:13:48 +02:00
std::string fname =
cmStrCat(this->Reporter.LogDir, "CTestLaunchConfig.cmake");
2019-11-11 23:01:05 +01:00
if (cmSystemTools::FileExists(fname) && mf.ReadListFile(fname)) {
2021-09-14 00:13:48 +02:00
this->Reporter.SourceDir = mf.GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
cmSystemTools::ConvertToUnixSlashes(this->Reporter.SourceDir);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
}