123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635 |
- #include "cmCTestLaunch.h"
- #include "cmsys/FStream.hxx"
- #include "cmsys/Process.h"
- #include "cmsys/RegularExpression.hxx"
- #include <iostream>
- #include <memory> // IWYU pragma: keep
- #include <stdlib.h>
- #include <string.h>
- #include "cmCryptoHash.h"
- #include "cmGeneratedFileStream.h"
- #include "cmGlobalGenerator.h"
- #include "cmMakefile.h"
- #include "cmProcessOutput.h"
- #include "cmStateSnapshot.h"
- #include "cmSystemTools.h"
- #include "cmXMLWriter.h"
- #include "cmake.h"
- #ifdef _WIN32
- #include <fcntl.h> // for _O_BINARY
- #include <io.h> // for _setmode
- #include <stdio.h> // for std{out,err} and fileno
- #endif
- cmCTestLaunch::cmCTestLaunch(int argc, const char* const* argv)
- {
- this->Passthru = true;
- this->Process = nullptr;
- this->ExitCode = 1;
- this->CWD = cmSystemTools::GetCurrentWorkingDirectory();
- if (!this->ParseArguments(argc, argv)) {
- return;
- }
- this->ComputeFileNames();
- this->ScrapeRulesLoaded = false;
- this->HaveOut = false;
- this->HaveErr = false;
- this->Process = cmsysProcess_New();
- }
- cmCTestLaunch::~cmCTestLaunch()
- {
- cmsysProcess_Delete(this->Process);
- if (!this->Passthru) {
- cmSystemTools::RemoveFile(this->LogOut);
- cmSystemTools::RemoveFile(this->LogErr);
- }
- }
- bool cmCTestLaunch::ParseArguments(int argc, const char* const* argv)
- {
-
-
- enum Doing
- {
- DoingNone,
- DoingOutput,
- DoingSource,
- DoingLanguage,
- DoingTargetName,
- DoingTargetType,
- DoingBuildDir,
- DoingCount,
- DoingFilterPrefix
- };
- Doing doing = DoingNone;
- int arg0 = 0;
- for (int i = 1; !arg0 && i < argc; ++i) {
- const char* arg = argv[i];
- if (strcmp(arg, "--") == 0) {
- arg0 = i + 1;
- } 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-name") == 0) {
- doing = DoingTargetName;
- } else if (strcmp(arg, "--target-type") == 0) {
- doing = DoingTargetType;
- } else if (strcmp(arg, "--build-dir") == 0) {
- doing = DoingBuildDir;
- } else if (strcmp(arg, "--filter-prefix") == 0) {
- doing = DoingFilterPrefix;
- } else if (doing == DoingOutput) {
- this->OptionOutput = arg;
- doing = DoingNone;
- } else if (doing == DoingSource) {
- this->OptionSource = arg;
- doing = DoingNone;
- } else if (doing == DoingLanguage) {
- this->OptionLanguage = arg;
- if (this->OptionLanguage == "CXX") {
- this->OptionLanguage = "C++";
- }
- doing = DoingNone;
- } else if (doing == DoingTargetName) {
- this->OptionTargetName = arg;
- doing = DoingNone;
- } else if (doing == DoingTargetType) {
- this->OptionTargetType = arg;
- doing = DoingNone;
- } else if (doing == DoingBuildDir) {
- this->OptionBuildDir = arg;
- doing = DoingNone;
- } else if (doing == DoingFilterPrefix) {
- this->OptionFilterPrefix = arg;
- doing = DoingNone;
- }
- }
-
- if (arg0) {
- this->RealArgC = argc - arg0;
- this->RealArgV = argv + arg0;
- for (int i = 0; i < this->RealArgC; ++i) {
- this->HandleRealArg(this->RealArgV[i]);
- }
- return true;
- }
- this->RealArgC = 0;
- this->RealArgV = nullptr;
- std::cerr << "No launch/command separator ('--') found!\n";
- return false;
- }
- void cmCTestLaunch::HandleRealArg(const char* arg)
- {
- #ifdef _WIN32
-
- 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.push_back(arg);
- }
- void cmCTestLaunch::ComputeFileNames()
- {
-
-
- const char* d = getenv("CTEST_LAUNCH_LOGS");
- if (!(d && *d)) {
- return;
- }
- this->Passthru = false;
-
-
- this->LogDir = d;
- cmSystemTools::ConvertToUnixSlashes(this->LogDir);
- this->LogDir += "/";
-
-
- cmCryptoHash md5(cmCryptoHash::AlgoMD5);
- md5.Initialize();
- md5.Append(this->CWD);
- for (std::string const& realArg : this->RealArgs) {
- md5.Append(realArg);
- }
- this->LogHash = md5.FinalizeHex();
-
- this->LogOut = this->LogDir;
- this->LogOut += "launch-";
- this->LogOut += this->LogHash;
- this->LogOut += "-out.txt";
- this->LogErr = this->LogDir;
- this->LogErr += "launch-";
- this->LogErr += this->LogHash;
- this->LogErr += "-err.txt";
- }
- void cmCTestLaunch::RunChild()
- {
-
- if (this->RealArgs.empty() || this->RealArgs[0] == ":") {
- this->ExitCode = 0;
- return;
- }
-
- cmsysProcess* cp = this->Process;
- cmsysProcess_SetCommand(cp, this->RealArgV);
- cmsys::ofstream fout;
- cmsys::ofstream ferr;
- if (this->Passthru) {
-
- cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
- cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
- } else {
-
- fout.open(this->LogOut.c_str(), std::ios::out | std::ios::binary);
- ferr.open(this->LogErr.c_str(), std::ios::out | std::ios::binary);
- }
- #ifdef _WIN32
-
-
- _setmode(fileno(stdout), _O_BINARY);
- _setmode(fileno(stderr), _O_BINARY);
- #endif
-
- cmsysProcess_Execute(cp);
-
- if (!this->Passthru) {
- char* data = nullptr;
- int length = 0;
- cmProcessOutput processOutput;
- std::string strdata;
- while (int p = cmsysProcess_WaitForData(cp, &data, &length, nullptr)) {
- if (p == cmsysProcess_Pipe_STDOUT) {
- processOutput.DecodeText(data, length, strdata, 1);
- fout.write(strdata.c_str(), strdata.size());
- std::cout.write(strdata.c_str(), strdata.size());
- this->HaveOut = true;
- } else if (p == cmsysProcess_Pipe_STDERR) {
- processOutput.DecodeText(data, length, strdata, 2);
- ferr.write(strdata.c_str(), strdata.size());
- std::cerr.write(strdata.c_str(), strdata.size());
- this->HaveErr = true;
- }
- }
- 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());
- }
- }
-
- cmsysProcess_WaitForExit(cp, nullptr);
- this->ExitCode = cmsysProcess_GetExitValue(cp);
- }
- int cmCTestLaunch::Run()
- {
- if (!this->Process) {
- std::cerr << "Could not allocate cmsysProcess instance!\n";
- return -1;
- }
- this->RunChild();
- if (this->CheckResults()) {
- return this->ExitCode;
- }
- this->LoadConfig();
- this->WriteXML();
- return this->ExitCode;
- }
- void cmCTestLaunch::LoadLabels()
- {
- if (this->OptionBuildDir.empty() || this->OptionTargetName.empty()) {
- return;
- }
-
- std::string fname = this->OptionBuildDir;
- fname += cmake::GetCMakeFilesDirectory();
- fname += "/";
- fname += this->OptionTargetName;
- fname += ".dir/Labels.txt";
-
- std::string source = this->OptionSource;
- cmSystemTools::ConvertToUnixSlashes(source);
-
- cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
- if (!fin) {
- return;
- }
- bool inTarget = true;
- bool inSource = false;
- std::string line;
- while (cmSystemTools::GetLineFromStream(fin, line)) {
- if (line.empty() || line[0] == '#') {
-
- continue;
- }
- if (line[0] == ' ') {
-
- if (inTarget || inSource) {
- this->Labels.insert(line.c_str() + 1);
- }
- } else if (!this->OptionSource.empty() && !inSource) {
-
-
-
- inTarget = false;
- inSource = this->SourceMatches(line, source);
- } else {
- return;
- }
- }
- }
- bool cmCTestLaunch::SourceMatches(std::string const& lhs,
- std::string const& rhs)
- {
-
-
-
-
- return lhs == rhs;
- }
- bool cmCTestLaunch::IsError() const
- {
- return this->ExitCode != 0;
- }
- void cmCTestLaunch::WriteXML()
- {
-
- std::string logXML = this->LogDir;
- logXML += this->IsError() ? "error-" : "warning-";
- logXML += this->LogHash;
- logXML += ".xml";
-
- cmGeneratedFileStream fxml(logXML.c_str());
- cmXMLWriter xml(fxml, 2);
- xml.StartElement("Failure");
- xml.Attribute("type", this->IsError() ? "Error" : "Warning");
- this->WriteXMLAction(xml);
- this->WriteXMLCommand(xml);
- this->WriteXMLResult(xml);
- this->WriteXMLLabels(xml);
- xml.EndElement();
- }
- void cmCTestLaunch::WriteXMLAction(cmXMLWriter& xml)
- {
- xml.Comment("Meta-information about the build action");
- xml.StartElement("Action");
-
- if (!this->OptionTargetName.empty()) {
- xml.Element("TargetName", this->OptionTargetName);
- }
-
- if (!this->OptionLanguage.empty()) {
- xml.Element("Language", this->OptionLanguage);
- }
-
- if (!this->OptionSource.empty()) {
- std::string source = this->OptionSource;
- cmSystemTools::ConvertToUnixSlashes(source);
-
- if (cmSystemTools::FileIsFullPath(this->SourceDir) &&
- cmSystemTools::FileIsFullPath(source) &&
- cmSystemTools::IsSubDirectory(source, this->SourceDir)) {
- source = cmSystemTools::RelativePath(this->SourceDir, source);
- }
- xml.Element("SourceFile", source);
- }
-
- if (!this->OptionOutput.empty()) {
- xml.Element("OutputFile", this->OptionOutput);
- }
-
- const char* outputType = nullptr;
- if (!this->OptionTargetType.empty()) {
- if (this->OptionTargetType == "EXECUTABLE") {
- outputType = "executable";
- } else if (this->OptionTargetType == "SHARED_LIBRARY") {
- outputType = "shared library";
- } else if (this->OptionTargetType == "MODULE_LIBRARY") {
- outputType = "module library";
- } else if (this->OptionTargetType == "STATIC_LIBRARY") {
- outputType = "static library";
- }
- } else if (!this->OptionSource.empty()) {
- outputType = "object file";
- }
- if (outputType) {
- xml.Element("OutputType", outputType);
- }
- xml.EndElement();
- }
- void cmCTestLaunch::WriteXMLCommand(cmXMLWriter& xml)
- {
- xml.Comment("Details of command");
- xml.StartElement("Command");
- if (!this->CWD.empty()) {
- xml.Element("WorkingDirectory", this->CWD);
- }
- for (std::string const& realArg : this->RealArgs) {
- xml.Element("Argument", realArg);
- }
- xml.EndElement();
- }
- void cmCTestLaunch::WriteXMLResult(cmXMLWriter& xml)
- {
- xml.Comment("Result of command");
- xml.StartElement("Result");
-
- xml.StartElement("StdOut");
- this->DumpFileToXML(xml, this->LogOut);
- xml.EndElement();
-
- xml.StartElement("StdErr");
- this->DumpFileToXML(xml, this->LogErr);
- xml.EndElement();
-
- xml.StartElement("ExitCondition");
- cmsysProcess* cp = this->Process;
- switch (cmsysProcess_GetState(cp)) {
- case cmsysProcess_State_Starting:
- xml.Content("No process has been executed");
- break;
- case cmsysProcess_State_Executing:
- xml.Content("The process is still executing");
- break;
- case cmsysProcess_State_Disowned:
- xml.Content("Disowned");
- break;
- case cmsysProcess_State_Killed:
- xml.Content("Killed by parent");
- break;
- case cmsysProcess_State_Expired:
- xml.Content("Killed when timeout expired");
- break;
- case cmsysProcess_State_Exited:
- xml.Content(this->ExitCode);
- break;
- case cmsysProcess_State_Exception:
- xml.Content("Terminated abnormally: ");
- xml.Content(cmsysProcess_GetExceptionString(cp));
- break;
- case cmsysProcess_State_Error:
- xml.Content("Error administrating child process: ");
- xml.Content(cmsysProcess_GetErrorString(cp));
- break;
- };
- xml.EndElement();
- xml.EndElement();
- }
- void cmCTestLaunch::WriteXMLLabels(cmXMLWriter& xml)
- {
- this->LoadLabels();
- if (!this->Labels.empty()) {
- xml.Comment("Interested parties");
- xml.StartElement("Labels");
- for (std::string const& label : this->Labels) {
- xml.Element("Label", label);
- }
- xml.EndElement();
- }
- }
- void cmCTestLaunch::DumpFileToXML(cmXMLWriter& xml, std::string const& fname)
- {
- cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
- std::string line;
- const char* sep = "";
- while (cmSystemTools::GetLineFromStream(fin, line)) {
- if (MatchesFilterPrefix(line)) {
- continue;
- }
- if (this->Match(line, this->RegexWarningSuppress)) {
- line = "[CTest: warning suppressed] " + line;
- } else if (this->Match(line, this->RegexWarning)) {
- line = "[CTest: warning matched] " + line;
- }
- xml.Content(sep);
- xml.Content(line);
- sep = "\n";
- }
- }
- bool cmCTestLaunch::CheckResults()
- {
-
- if (this->Passthru) {
- return true;
- }
-
- if (this->IsError()) {
- return false;
- }
-
- if ((this->HaveErr && this->ScrapeLog(this->LogErr)) ||
- (this->HaveOut && this->ScrapeLog(this->LogOut))) {
- return false;
- }
- return true;
- }
- void cmCTestLaunch::LoadScrapeRules()
- {
- if (this->ScrapeRulesLoaded) {
- return;
- }
- this->ScrapeRulesLoaded = true;
-
-
-
- this->RegexWarning.push_back("(^|[ :])[Ww][Aa][Rr][Nn][Ii][Nn][Gg]");
- this->RegexWarning.push_back("(^|[ :])[Rr][Ee][Mm][Aa][Rr][Kk]");
- this->RegexWarning.push_back("(^|[ :])[Nn][Oo][Tt][Ee]");
-
- this->LoadScrapeRules("Warning", this->RegexWarning);
- this->LoadScrapeRules("WarningSuppress", this->RegexWarningSuppress);
- }
- void cmCTestLaunch::LoadScrapeRules(
- const char* purpose, std::vector<cmsys::RegularExpression>& regexps)
- {
- std::string fname = this->LogDir;
- fname += "Custom";
- fname += purpose;
- fname += ".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();
-
-
- cmsys::ifstream fin(fname.c_str(), std::ios::in | std::ios::binary);
- std::string line;
- while (cmSystemTools::GetLineFromStream(fin, line)) {
- if (MatchesFilterPrefix(line)) {
- continue;
- }
- if (this->Match(line, this->RegexWarning) &&
- !this->Match(line, this->RegexWarningSuppress)) {
- return true;
- }
- }
- return false;
- }
- bool cmCTestLaunch::Match(std::string const& line,
- std::vector<cmsys::RegularExpression>& regexps)
- {
- for (cmsys::RegularExpression& r : regexps) {
- if (r.find(line.c_str())) {
- return true;
- }
- }
- return false;
- }
- bool cmCTestLaunch::MatchesFilterPrefix(std::string const& line) const
- {
- return !this->OptionFilterPrefix.empty() &&
- cmSystemTools::StringStartsWith(line, this->OptionFilterPrefix.c_str());
- }
- int cmCTestLaunch::Main(int argc, const char* const argv[])
- {
- if (argc == 2) {
- std::cerr << "ctest --launch: this mode is for internal CTest use only"
- << std::endl;
- return 1;
- }
- cmCTestLaunch self(argc, argv);
- return self.Run();
- }
- void cmCTestLaunch::LoadConfig()
- {
- cmake cm(cmake::RoleScript);
- cm.SetHomeDirectory("");
- cm.SetHomeOutputDirectory("");
- cm.GetCurrentSnapshot().SetDefaultDefinitions();
- cmGlobalGenerator gg(&cm);
- cmMakefile mf(&gg, cm.GetCurrentSnapshot());
- std::string fname = this->LogDir;
- fname += "CTestLaunchConfig.cmake";
- if (cmSystemTools::FileExists(fname) && mf.ReadListFile(fname.c_str())) {
- this->SourceDir = mf.GetSafeDefinition("CTEST_SOURCE_DIRECTORY");
- cmSystemTools::ConvertToUnixSlashes(this->SourceDir);
- }
- }
|