cmCTestBuildHandler.cxx 39.4 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
Ken Martin's avatar
Ken Martin committed
3 4
#include "cmCTestBuildHandler.h"

5
#include "cmAlgorithms.h"
Ken Martin's avatar
Ken Martin committed
6
#include "cmCTest.h"
Wouter Klouwen's avatar
Wouter Klouwen committed
7
#include "cmDuration.h"
8
#include "cmFileTimeCache.h"
9
#include "cmGeneratedFileStream.h"
10
#include "cmMakefile.h"
11
#include "cmProcessOutput.h"
12
#include "cmSystemTools.h"
13
#include "cmXMLWriter.h"
Ken Martin's avatar
Ken Martin committed
14

15 16 17
#include "cmsys/Directory.hxx"
#include "cmsys/FStream.hxx"
#include "cmsys/Process.h"
18
#include <set>
Andy Cedilnik's avatar
Andy Cedilnik committed
19
#include <stdlib.h>
20
#include <string.h>
21
#include <utility>
Ken Martin's avatar
Ken Martin committed
22 23 24 25 26

static const char* cmCTestErrorMatches[] = {
  "^[Bb]us [Ee]rror",
  "^[Ss]egmentation [Vv]iolation",
  "^[Ss]egmentation [Ff]ault",
David Cole's avatar
David Cole committed
27
  ":.*[Pp]ermission [Dd]enied",
Ken Martin's avatar
Ken Martin committed
28 29 30 31 32 33 34
  "([^ :]+):([0-9]+): ([^ \\t])",
  "([^:]+): error[ \\t]*[0-9]+[ \\t]*:",
  "^Error ([0-9]+):",
  "^Fatal",
  "^Error: ",
  "^Error ",
  "[0-9] ERROR: ",
35
  R"(^"[^"]+", line [0-9]+: [^Ww])",
Ken Martin's avatar
Ken Martin committed
36 37
  "^cc[^C]*CC: ERROR File = ([^,]+), Line = ([0-9]+)",
  "^ld([^:])*:([ \\t])*ERROR([^:])*:",
38
  R"(^ild:([ \t])*\(undefined symbol\))",
Ken Martin's avatar
Ken Martin committed
39 40
  "([^ :]+) : (error|fatal error|catastrophic error)",
  "([^:]+): (Error:|error|undefined reference|multiply defined)",
41
  R"(([^:]+)\(([^\)]+)\) ?: (error|fatal error|catastrophic error))",
Ken Martin's avatar
Ken Martin committed
42 43 44
  "^fatal error C[0-9]+:",
  ": syntax error ",
  "^collect2: ld returned 1 exit status",
45
  "ld terminated with signal",
David Cole's avatar
David Cole committed
46
  "Unsatisfied symbol",
Ken Martin's avatar
Ken Martin committed
47
  "^Unresolved:",
David Cole's avatar
David Cole committed
48
  "Undefined symbol",
Ken Martin's avatar
Ken Martin committed
49
  "^Undefined[ \\t]+first referenced",
50
  "^CMake Error.*:",
Ken Martin's avatar
Ken Martin committed
51 52
  ":[ \\t]cannot find",
  ":[ \\t]can't find",
53 54
  R"(: \*\*\* No rule to make target [`'].*\'.  Stop)",
  R"(: \*\*\* No targets specified and no makefile found)",
Ken Martin's avatar
Ken Martin committed
55
  ": Invalid loader fixup for symbol",
56 57
  ": Invalid fixups exist",
  ": Can't find library for",
Ken Martin's avatar
Ken Martin committed
58
  ": internal link edit command failed",
59
  ": Unrecognized option [`'].*\\'",
60
  R"(", line [0-9]+\.[0-9]+: [0-9]+-[0-9]+ \([^WI]\))",
Ken Martin's avatar
Ken Martin committed
61 62 63 64 65 66
  "ld: 0706-006 Cannot find or open library file: -l ",
  "ild: \\(argument error\\) can't find library argument ::",
  "^could not be found and will not be loaded.",
  "s:616 string too big",
  "make: Fatal error: ",
  "ld: 0711-993 Error occurred while writing to the output file:",
67
  "ld: fatal: ",
Andy Cedilnik's avatar
Andy Cedilnik committed
68
  "final link failed:",
69 70 71
  R"(make: \*\*\*.*Error)",
  R"(make\[.*\]: \*\*\*.*Error)",
  R"(\*\*\* Error code)",
Andy Cedilnik's avatar
Andy Cedilnik committed
72
  "nternal error:",
73
  R"(Makefile:[0-9]+: \*\*\* .*  Stop\.)",
Andy Cedilnik's avatar
Andy Cedilnik committed
74 75
  ": No such file or directory",
  ": Invalid argument",
Brad King's avatar
Brad King committed
76
  "^The project cannot be built\\.",
77
  "^\\[ERROR\\]",
78
  "^Command .* failed with exit code",
Daniel Pfeifer's avatar
Daniel Pfeifer committed
79
  nullptr
Ken Martin's avatar
Ken Martin committed
80 81 82 83 84 85
};

static const char* cmCTestErrorExceptions[] = {
  "instantiated from ",
  "candidates are:",
  ": warning",
86
  ": WARNING",
87
  ": \\(Warning\\)",
88
  ": note",
89
  "Note:",
Ken Martin's avatar
Ken Martin committed
90 91 92 93
  "makefile:",
  "Makefile:",
  ":[ \\t]+Where:",
  "([^ :]+):([0-9]+): Warning",
Andy Cedilnik's avatar
Andy Cedilnik committed
94
  "------ Build started: .* ------",
Daniel Pfeifer's avatar
Daniel Pfeifer committed
95
  nullptr
Ken Martin's avatar
Ken Martin committed
96 97 98 99
};

static const char* cmCTestWarningMatches[] = {
  "([^ :]+):([0-9]+): warning:",
100
  "([^ :]+):([0-9]+): note:",
Ken Martin's avatar
Ken Martin committed
101 102 103
  "^cc[^C]*CC: WARNING File = ([^,]+), Line = ([0-9]+)",
  "^ld([^:])*:([ \\t])*WARNING([^:])*:",
  "([^:]+): warning ([0-9]+):",
104
  R"(^"[^"]+", line [0-9]+: [Ww](arning|arnung))",
Ken Martin's avatar
Ken Martin committed
105
  "([^:]+): warning[ \\t]*[0-9]+[ \\t]*:",
106
  "^(Warning|Warnung) ([0-9]+):",
107
  "^(Warning|Warnung)[ :]",
Ken Martin's avatar
Ken Martin committed
108 109 110
  "WARNING: ",
  "([^ :]+) : warning",
  "([^:]+): warning",
111
  R"(", line [0-9]+\.[0-9]+: [0-9]+-[0-9]+ \([WI]\))",
Ken Martin's avatar
Ken Martin committed
112 113
  "^cxx: Warning:",
  ".*file: .* has no symbols",
114
  "([^ :]+):([0-9]+): (Warning|Warnung)",
115
  "\\([0-9]*\\): remark #[0-9]*",
116
  R"(".*", line [0-9]+: remark\([0-9]*\):)",
117
  "cc-[0-9]* CC: REMARK File = .*, Line = [0-9]*",
118
  "^CMake Warning.*:",
119
  "^\\[WARNING\\]",
Daniel Pfeifer's avatar
Daniel Pfeifer committed
120
  nullptr
Ken Martin's avatar
Ken Martin committed
121 122 123
};

static const char* cmCTestWarningExceptions[] = {
124 125 126
  R"(/usr/.*/X11/Xlib\.h:[0-9]+: war.*: ANSI C\+\+ forbids declaration)",
  R"(/usr/.*/X11/Xutil\.h:[0-9]+: war.*: ANSI C\+\+ forbids declaration)",
  R"(/usr/.*/X11/XResource\.h:[0-9]+: war.*: ANSI C\+\+ forbids declaration)",
Ken Martin's avatar
Ken Martin committed
127 128 129 130 131 132 133 134 135 136 137 138 139
  "WARNING 84 :",
  "WARNING 47 :",
  "makefile:",
  "Makefile:",
  "warning:  Clock skew detected.  Your build may be incomplete.",
  "/usr/openwin/include/GL/[^:]+:",
  "bind_at_load",
  "XrmQGetResource",
  "IceFlush",
  "warning LNK4089: all references to [^ \\t]+ discarded by .OPT:REF",
  "ld32: WARNING 85: definition of dataKey in",
  "cc: warning 422: Unknown option \"\\+b",
  "_with_warning_C",
Daniel Pfeifer's avatar
Daniel Pfeifer committed
140
  nullptr
Ken Martin's avatar
Ken Martin committed
141 142
};

143 144
struct cmCTestBuildCompileErrorWarningRex
{
Andy Cedilnik's avatar
Andy Cedilnik committed
145 146 147
  const char* RegularExpressionString;
  int FileIndex;
  int LineIndex;
148 149
};

150 151 152
static cmCTestBuildCompileErrorWarningRex cmCTestWarningErrorFileLine[] = {
  { "^Warning W[0-9]+ ([a-zA-Z.\\:/0-9_+ ~-]+) ([0-9]+):", 1, 2 },
  { "^([a-zA-Z./0-9_+ ~-]+):([0-9]+):", 1, 2 },
153 154
  { R"(^([a-zA-Z.\:/0-9_+ ~-]+)\(([0-9]+)\))", 1, 2 },
  { R"(^[0-9]+>([a-zA-Z.\:/0-9_+ ~-]+)\(([0-9]+)\))", 1, 2 },
155 156 157
  { "^([a-zA-Z./0-9_+ ~-]+)\\(([0-9]+)\\)", 1, 2 },
  { "\"([a-zA-Z./0-9_+ ~-]+)\", line ([0-9]+)", 1, 2 },
  { "File = ([a-zA-Z./0-9_+ ~-]+), Line = ([0-9]+)", 1, 2 },
Daniel Pfeifer's avatar
Daniel Pfeifer committed
158
  { nullptr, 0, 0 }
159 160 161 162
};

cmCTestBuildHandler::cmCTestBuildHandler()
{
163 164
  this->MaxPreContext = 10;
  this->MaxPostContext = 10;
165

Andy Cedilnik's avatar
Andy Cedilnik committed
166 167
  this->MaxErrors = 50;
  this->MaxWarnings = 50;
168

Andy Cedilnik's avatar
Andy Cedilnik committed
169
  this->LastErrorOrWarning = this->ErrorsAndWarnings.end();
170

171
  this->UseCTestLaunch = false;
172 173
}

174 175
void cmCTestBuildHandler::Initialize()
{
176
  this->Superclass::Initialize();
177 178
  this->StartBuild.clear();
  this->EndBuild.clear();
Andy Cedilnik's avatar
Andy Cedilnik committed
179 180 181 182
  this->CustomErrorMatches.clear();
  this->CustomErrorExceptions.clear();
  this->CustomWarningMatches.clear();
  this->CustomWarningExceptions.clear();
183 184
  this->ReallyCustomWarningMatches.clear();
  this->ReallyCustomWarningExceptions.clear();
Andy Cedilnik's avatar
Andy Cedilnik committed
185 186 187 188 189 190 191 192 193 194 195
  this->ErrorWarningFileLineRegex.clear();

  this->ErrorMatchRegex.clear();
  this->ErrorExceptionRegex.clear();
  this->WarningMatchRegex.clear();
  this->WarningExceptionRegex.clear();
  this->BuildProcessingQueue.clear();
  this->BuildProcessingErrorQueue.clear();
  this->BuildOutputLogSize = 0;
  this->CurrentProcessingLine.clear();

196 197
  this->SimplifySourceDir.clear();
  this->SimplifyBuildDir.clear();
Andy Cedilnik's avatar
Andy Cedilnik committed
198 199 200 201
  this->OutputLineCounter = 0;
  this->ErrorsAndWarnings.clear();
  this->LastErrorOrWarning = this->ErrorsAndWarnings.end();
  this->PostContextCount = 0;
202 203
  this->MaxPreContext = 10;
  this->MaxPostContext = 10;
Andy Cedilnik's avatar
Andy Cedilnik committed
204 205 206 207 208 209 210 211 212 213 214
  this->PreContext.clear();

  this->TotalErrors = 0;
  this->TotalWarnings = 0;
  this->LastTickChar = 0;

  this->ErrorQuotaReached = false;
  this->WarningQuotaReached = false;

  this->MaxErrors = 50;
  this->MaxWarnings = 50;
215 216

  this->UseCTestLaunch = false;
217
}
Ken Martin's avatar
Ken Martin committed
218

219
void cmCTestBuildHandler::PopulateCustomVectors(cmMakefile* mf)
Ken Martin's avatar
Ken Martin committed
220
{
221
  this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_MATCH",
222
                                    this->CustomErrorMatches);
223
  this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_ERROR_EXCEPTION",
224
                                    this->CustomErrorExceptions);
225
  this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_MATCH",
226
                                    this->CustomWarningMatches);
227
  this->CTest->PopulateCustomVector(mf, "CTEST_CUSTOM_WARNING_EXCEPTION",
228 229 230 231 232
                                    this->CustomWarningExceptions);
  this->CTest->PopulateCustomInteger(
    mf, "CTEST_CUSTOM_MAXIMUM_NUMBER_OF_ERRORS", this->MaxErrors);
  this->CTest->PopulateCustomInteger(
    mf, "CTEST_CUSTOM_MAXIMUM_NUMBER_OF_WARNINGS", this->MaxWarnings);
233

234 235
  int n = -1;
  this->CTest->PopulateCustomInteger(mf, "CTEST_CUSTOM_ERROR_PRE_CONTEXT", n);
236
  if (n != -1) {
237
    this->MaxPreContext = static_cast<size_t>(n);
238
  }
239 240 241

  n = -1;
  this->CTest->PopulateCustomInteger(mf, "CTEST_CUSTOM_ERROR_POST_CONTEXT", n);
242
  if (n != -1) {
243
    this->MaxPostContext = static_cast<size_t>(n);
244
  }
245

246
  // Record the user-specified custom warning rules.
247 248
  if (const char* customWarningMatchers =
        mf->GetDefinition("CTEST_CUSTOM_WARNING_MATCH")) {
249 250
    cmSystemTools::ExpandListArgument(customWarningMatchers,
                                      this->ReallyCustomWarningMatches);
251 252 253
  }
  if (const char* customWarningExceptions =
        mf->GetDefinition("CTEST_CUSTOM_WARNING_EXCEPTION")) {
254 255
    cmSystemTools::ExpandListArgument(customWarningExceptions,
                                      this->ReallyCustomWarningExceptions);
256
  }
Ken Martin's avatar
Ken Martin committed
257 258
}

259 260
std::string cmCTestBuildHandler::GetMakeCommand()
{
261 262 263
  std::string makeCommand = this->CTest->GetCTestConfiguration("MakeCommand");
  cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
                     "MakeCommand:" << makeCommand << "\n", this->Quiet);
264 265

  std::string configType = this->CTest->GetConfigType();
266
  if (configType.empty()) {
267 268 269
    configType =
      this->CTest->GetCTestConfiguration("DefaultCTestConfigurationType");
  }
270
  if (configType.empty()) {
271
    configType = "Release";
272
  }
273

274 275
  cmSystemTools::ReplaceString(makeCommand, "${CTEST_CONFIGURATION_TYPE}",
                               configType.c_str());
276 277 278 279

  return makeCommand;
}

280 281
// clearly it would be nice if this were broken up into a few smaller
// functions and commented...
282
int cmCTestBuildHandler::ProcessHandler()
Ken Martin's avatar
Ken Martin committed
283
{
284
  cmCTestOptionalLog(this->CTest, HANDLER_OUTPUT, "Build project" << std::endl,
285
                     this->Quiet);
286

287
  // do we have time for this
288
  if (this->CTest->GetRemainingTimeAllowed() < std::chrono::minutes(2)) {
289
    return 0;
290
  }
291

292
  int entry;
293 294
  for (entry = 0; cmCTestWarningErrorFileLine[entry].RegularExpressionString;
       ++entry) {
295
    cmCTestBuildHandler::cmCTestCompileErrorWarningRex r;
296 297
    if (r.RegularExpression.compile(
          cmCTestWarningErrorFileLine[entry].RegularExpressionString)) {
Andy Cedilnik's avatar
Andy Cedilnik committed
298 299
      r.FileIndex = cmCTestWarningErrorFileLine[entry].FileIndex;
      r.LineIndex = cmCTestWarningErrorFileLine[entry].LineIndex;
300
      this->ErrorWarningFileLineRegex.push_back(std::move(r));
301 302
    } else {
      cmCTestLog(
303 304
        this->CTest, ERROR_MESSAGE,
        "Problem Compiling regular expression: "
305 306
          << cmCTestWarningErrorFileLine[entry].RegularExpressionString
          << std::endl);
307
    }
308
  }
309

310
  // Determine build command and build directory
311
  std::string makeCommand = this->GetMakeCommand();
312
  if (makeCommand.empty()) {
Andy Cedilnik's avatar
Andy Cedilnik committed
313
    cmCTestLog(this->CTest, ERROR_MESSAGE,
314 315
               "Cannot find MakeCommand key in the DartConfiguration.tcl"
                 << std::endl);
316
    return -1;
317
  }
318

319 320 321
  const std::string& buildDirectory =
    this->CTest->GetCTestConfiguration("BuildDirectory");
  if (buildDirectory.empty()) {
Andy Cedilnik's avatar
Andy Cedilnik committed
322
    cmCTestLog(this->CTest, ERROR_MESSAGE,
323 324
               "Cannot find BuildDirectory  key in the DartConfiguration.tcl"
                 << std::endl);
325
    return -1;
326
  }
Ken Martin's avatar
Ken Martin committed
327

328 329
  std::string const& useLaunchers =
    this->CTest->GetCTestConfiguration("UseLaunchers");
330
  this->UseCTestLaunch = cmSystemTools::IsOn(useLaunchers);
331

332
  // Create a last build log
333
  cmGeneratedFileStream ofs;
334
  auto elapsed_time_start = std::chrono::steady_clock::now();
335
  if (!this->StartLogFile("Build", ofs)) {
336 337
    cmCTestLog(this->CTest, ERROR_MESSAGE,
               "Cannot create build log file" << std::endl);
338
  }
339

340 341
  // Create lists of regular expression strings for errors, error exceptions,
  // warnings and warning exceptions.
342
  std::vector<std::string>::size_type cc;
343
  for (cc = 0; cmCTestErrorMatches[cc]; cc++) {
wahikihiki's avatar
wahikihiki committed
344
    this->CustomErrorMatches.emplace_back(cmCTestErrorMatches[cc]);
345 346
  }
  for (cc = 0; cmCTestErrorExceptions[cc]; cc++) {
wahikihiki's avatar
wahikihiki committed
347
    this->CustomErrorExceptions.emplace_back(cmCTestErrorExceptions[cc]);
348 349
  }
  for (cc = 0; cmCTestWarningMatches[cc]; cc++) {
wahikihiki's avatar
wahikihiki committed
350
    this->CustomWarningMatches.emplace_back(cmCTestWarningMatches[cc]);
351
  }
352

353
  for (cc = 0; cmCTestWarningExceptions[cc]; cc++) {
wahikihiki's avatar
wahikihiki committed
354
    this->CustomWarningExceptions.emplace_back(cmCTestWarningExceptions[cc]);
355
  }
Ken Martin's avatar
Ken Martin committed
356

357
  // Pre-compile regular expressions objects for all regular expressions
358

359
#define cmCTestBuildHandlerPopulateRegexVector(strings, regexes)              \
360 361
  do {                                                                        \
    regexes.clear();                                                          \
362
    cmCTestOptionalLog(this->CTest, DEBUG,                                    \
363 364 365 366 367
                       this << "Add " #regexes << std::endl, this->Quiet);    \
    for (std::string const& s : (strings)) {                                  \
      cmCTestOptionalLog(this->CTest, DEBUG,                                  \
                         "Add " #strings ": " << s << std::endl,              \
                         this->Quiet);                                        \
wahikihiki's avatar
wahikihiki committed
368
      (regexes).emplace_back(s);                                              \
369 370 371
    }                                                                         \
  } while (false)

372 373 374 375 376 377 378 379
  cmCTestBuildHandlerPopulateRegexVector(this->CustomErrorMatches,
                                         this->ErrorMatchRegex);
  cmCTestBuildHandlerPopulateRegexVector(this->CustomErrorExceptions,
                                         this->ErrorExceptionRegex);
  cmCTestBuildHandlerPopulateRegexVector(this->CustomWarningMatches,
                                         this->WarningMatchRegex);
  cmCTestBuildHandlerPopulateRegexVector(this->CustomWarningExceptions,
                                         this->WarningExceptionRegex);
380 381

  // Determine source and binary tree substitutions to simplify the output.
382 383
  this->SimplifySourceDir.clear();
  this->SimplifyBuildDir.clear();
384 385 386
  if (this->CTest->GetCTestConfiguration("SourceDirectory").size() > 20) {
    std::string srcdir =
      this->CTest->GetCTestConfiguration("SourceDirectory") + "/";
Ken Martin's avatar
Ken Martin committed
387
    std::string srcdirrep;
388 389
    for (cc = srcdir.size() - 2; cc > 0; cc--) {
      if (srcdir[cc] == '/') {
390
        srcdirrep = srcdir.substr(cc);
Ken Martin's avatar
Ken Martin committed
391
        srcdirrep = "/..." + srcdirrep;
392
        srcdir = srcdir.substr(0, cc + 1);
Ken Martin's avatar
Ken Martin committed
393 394
        break;
      }
395
    }
396 397 398 399 400
    this->SimplifySourceDir = srcdir;
  }
  if (this->CTest->GetCTestConfiguration("BuildDirectory").size() > 20) {
    std::string bindir =
      this->CTest->GetCTestConfiguration("BuildDirectory") + "/";
401
    std::string bindirrep;
402 403
    for (cc = bindir.size() - 2; cc > 0; cc--) {
      if (bindir[cc] == '/') {
404
        bindirrep = bindir.substr(cc);
Ken Martin's avatar
Ken Martin committed
405
        bindirrep = "/..." + bindirrep;
406
        bindir = bindir.substr(0, cc + 1);
Ken Martin's avatar
Ken Martin committed
407 408 409
        break;
      }
    }
410 411
    this->SimplifyBuildDir = bindir;
  }
Ken Martin's avatar
Ken Martin committed
412

413
  // Ok, let's do the build
Andy Cedilnik's avatar
Andy Cedilnik committed
414

415
  // Remember start build time
Andy Cedilnik's avatar
Andy Cedilnik committed
416
  this->StartBuild = this->CTest->CurrentTime();
417
  this->StartBuildTime = std::chrono::system_clock::now();
418 419
  int retVal = 0;
  int res = cmsysProcess_State_Exited;
420
  if (!this->CTest->GetShowOnly()) {
421 422
    res = this->RunMakeCommand(makeCommand, &retVal, buildDirectory.c_str(), 0,
                               ofs);
423 424 425 426 427
  } else {
    cmCTestOptionalLog(this->CTest, DEBUG,
                       "Build with command: " << makeCommand << std::endl,
                       this->Quiet);
  }
Ken Martin's avatar
Ken Martin committed
428

429
  // Remember end build time and calculate elapsed time
Andy Cedilnik's avatar
Andy Cedilnik committed
430
  this->EndBuild = this->CTest->CurrentTime();
431
  this->EndBuildTime = std::chrono::system_clock::now();
432 433
  auto elapsed_build_time =
    std::chrono::steady_clock::now() - elapsed_time_start;
434 435

  // Cleanups strings in the errors and warnings list.
436
  if (!this->SimplifySourceDir.empty()) {
437 438
    for (cmCTestBuildErrorWarning& evit : this->ErrorsAndWarnings) {
      cmSystemTools::ReplaceString(evit.Text, this->SimplifySourceDir.c_str(),
439
                                   "/.../");
440
      cmSystemTools::ReplaceString(evit.PreContext,
441
                                   this->SimplifySourceDir.c_str(), "/.../");
442
      cmSystemTools::ReplaceString(evit.PostContext,
443
                                   this->SimplifySourceDir.c_str(), "/.../");
Ken Martin's avatar
Ken Martin committed
444
    }
445 446 447
  }

  if (!this->SimplifyBuildDir.empty()) {
448 449
    for (cmCTestBuildErrorWarning& evit : this->ErrorsAndWarnings) {
      cmSystemTools::ReplaceString(evit.Text, this->SimplifyBuildDir.c_str(),
450
                                   "/.../");
451
      cmSystemTools::ReplaceString(evit.PreContext,
452
                                   this->SimplifyBuildDir.c_str(), "/.../");
453
      cmSystemTools::ReplaceString(evit.PostContext,
454
                                   this->SimplifyBuildDir.c_str(), "/.../");
Ken Martin's avatar
Ken Martin committed
455
    }
456
  }
Ken Martin's avatar
Ken Martin committed
457

458
  // Generate XML output
459
  cmGeneratedFileStream xofs;
460
  if (!this->StartResultingXML(cmCTest::PartBuild, "Build", xofs)) {
461 462
    cmCTestLog(this->CTest, ERROR_MESSAGE,
               "Cannot create build XML file" << std::endl);
463
    return -1;
464
  }
465 466
  cmXMLWriter xml(xofs);
  this->GenerateXMLHeader(xml);
467
  if (this->UseCTestLaunch) {
468
    this->GenerateXMLLaunched(xml);
469
  } else {
470
    this->GenerateXMLLogScraped(xml);
471
  }
472
  this->GenerateXMLFooter(xml, elapsed_build_time);
473

474
  if (res != cmsysProcess_State_Exited || retVal || this->TotalErrors > 0) {
475 476
    cmCTestLog(this->CTest, ERROR_MESSAGE,
               "Error(s) when building project" << std::endl);
477
  }
478

479
  // Display message about number of errors and warnings
480 481 482 483 484 485 486 487 488
  cmCTestLog(this->CTest, HANDLER_OUTPUT,
             "   " << this->TotalErrors
                   << (this->TotalErrors >= this->MaxErrors ? " or more" : "")
                   << " Compiler errors" << std::endl);
  cmCTestLog(
    this->CTest, HANDLER_OUTPUT,
    "   " << this->TotalWarnings
          << (this->TotalWarnings >= this->MaxWarnings ? " or more" : "")
          << " Compiler warnings" << std::endl);
489

490
  return retVal;
Ken Martin's avatar
Ken Martin committed
491 492
}

493
void cmCTestBuildHandler::GenerateXMLHeader(cmXMLWriter& xml)
Ken Martin's avatar
Ken Martin committed
494
{
495
  this->CTest->StartXML(xml, this->AppendXML);
496
  this->CTest->GenerateSubprojectsOutput(xml);
497 498
  xml.StartElement("Build");
  xml.Element("StartDateTime", this->StartBuild);
499
  xml.Element("StartBuildTime", this->StartBuildTime);
500
  xml.Element("BuildCommand", this->GetMakeCommand());
501
}
Andy Cedilnik's avatar
Andy Cedilnik committed
502

503 504 505
class cmCTestBuildHandler::FragmentCompare
{
public:
506
  FragmentCompare(cmFileTimeCache* ftc)
507 508 509
    : FTC(ftc)
  {
  }
wahikihiki's avatar
wahikihiki committed
510
  FragmentCompare() = default;
511
  bool operator()(std::string const& l, std::string const& r) const
512
  {
513 514
    // Order files by modification time.  Use lexicographic order
    // among files with the same time.
515
    int result;
516
    if (this->FTC->Compare(l, r, &result) && result != 0) {
517 518
      return result < 0;
    }
519
    return l < r;
520 521
  }

522
private:
523
  cmFileTimeCache* FTC = nullptr;
524 525
};

526
void cmCTestBuildHandler::GenerateXMLLaunched(cmXMLWriter& xml)
527
{
528
  if (this->CTestLaunchDir.empty()) {
529
    return;
530
  }
531 532

  // Sort XML fragments in chronological order.
533
  cmFileTimeCache ftc;
534
  FragmentCompare fragmentCompare(&ftc);
535
  typedef std::set<std::string, FragmentCompare> Fragments;
536 537
  Fragments fragments(fragmentCompare);

538 539 540
  // only report the first 50 warnings and first 50 errors
  int numErrorsAllowed = this->MaxErrors;
  int numWarningsAllowed = this->MaxWarnings;
541 542
  // Identify fragments on disk.
  cmsys::Directory launchDir;
543
  launchDir.Load(this->CTestLaunchDir);
544
  unsigned long n = launchDir.GetNumberOfFiles();
545
  for (unsigned long i = 0; i < n; ++i) {
546
    const char* fname = launchDir.GetFile(i);
547
    if (this->IsLaunchedErrorFile(fname) && numErrorsAllowed) {
548
      numErrorsAllowed--;
549 550
      fragments.insert(this->CTestLaunchDir + "/" + fname);
      ++this->TotalErrors;
551
    } else if (this->IsLaunchedWarningFile(fname) && numWarningsAllowed) {
552
      numWarningsAllowed--;
553 554 555
      fragments.insert(this->CTestLaunchDir + "/" + fname);
      ++this->TotalWarnings;
    }
556
  }
557 558

  // Copy the fragments into the final XML file.
559 560
  for (std::string const& f : fragments) {
    xml.FragmentFile(f.c_str());
561
  }
562 563
}

564
void cmCTestBuildHandler::GenerateXMLLogScraped(cmXMLWriter& xml)
565 566
{
  std::vector<cmCTestBuildErrorWarning>& ew = this->ErrorsAndWarnings;
Ken Martin's avatar
Ken Martin committed
567
  std::vector<cmCTestBuildErrorWarning>::iterator it;
Andy Cedilnik's avatar
Andy Cedilnik committed
568

569
  // only report the first 50 warnings and first 50 errors
570 571
  int numErrorsAllowed = this->MaxErrors;
  int numWarningsAllowed = this->MaxWarnings;
Andy Cedilnik's avatar
Andy Cedilnik committed
572
  std::string srcdir = this->CTest->GetCTestConfiguration("SourceDirectory");
573 574
  // make sure the source dir is in the correct case on windows
  // via a call to collapse full path.
575
  srcdir = cmSystemTools::CollapseFullPath(srcdir);
576
  srcdir += "/";
577 578 579
  for (it = ew.begin();
       it != ew.end() && (numErrorsAllowed || numWarningsAllowed); it++) {
    cmCTestBuildErrorWarning* cm = &(*it);
580
    if ((cm->Error && numErrorsAllowed) ||
581 582
        (!cm->Error && numWarningsAllowed)) {
      if (cm->Error) {
583
        numErrorsAllowed--;
584
      } else {
585
        numWarningsAllowed--;
586
      }
587 588 589
      xml.StartElement(cm->Error ? "Error" : "Warning");
      xml.Element("BuildLogLine", cm->LogLine);
      xml.Element("Text", cm->Text);
590 591 592
      for (cmCTestCompileErrorWarningRex& rit :
           this->ErrorWarningFileLineRegex) {
        cmsys::RegularExpression* re = &rit.RegularExpression;
593
        if (re->find(cm->Text)) {
594
          cm->SourceFile = re->match(rit.FileIndex);
Andy Cedilnik's avatar
Andy Cedilnik committed
595
          // At this point we need to make this->SourceFile relative to
596
          // the source root of the project, so cvs links will work
Andy Cedilnik's avatar
Andy Cedilnik committed
597
          cmSystemTools::ConvertToUnixSlashes(cm->SourceFile);
598
          if (cm->SourceFile.find("/.../") != std::string::npos) {
Andy Cedilnik's avatar
Andy Cedilnik committed
599
            cmSystemTools::ReplaceString(cm->SourceFile, "/.../", "");
600
            std::string::size_type p = cm->SourceFile.find('/');
601
            if (p != std::string::npos) {
602 603
              cm->SourceFile =
                cm->SourceFile.substr(p + 1, cm->SourceFile.size() - p);
604
            }
605
          } else {
606
            // make sure it is a full path with the correct case
607 608 609
            cm->SourceFile = cmSystemTools::CollapseFullPath(cm->SourceFile);
            cmSystemTools::ReplaceString(cm->SourceFile, srcdir.c_str(), "");
          }
610
          cm->LineNumber = atoi(re->match(rit.LineIndex).c_str());
611 612
          break;
        }
613 614 615
      }
      if (!cm->SourceFile.empty() && cm->LineNumber >= 0) {
        if (!cm->SourceFile.empty()) {
616
          xml.Element("SourceFile", cm->SourceFile);
617 618
        }
        if (!cm->SourceFileTail.empty()) {
619
          xml.Element("SourceFileTail", cm->SourceFileTail);
620 621
        }
        if (cm->LineNumber >= 0) {
622
          xml.Element("SourceLineNumber", cm->LineNumber);
623
        }
624
      }
625 626 627
      xml.Element("PreContext", cm->PreContext);
      xml.StartElement("PostContext");
      xml.Content(cm->PostContext);
628
      // is this the last warning or error, if so notify
629
      if ((cm->Error && !numErrorsAllowed) ||
630
          (!cm->Error && !numWarningsAllowed)) {
631
        xml.Content("\nThe maximum number of reported warnings or errors "
632 633
                    "has been reached!!!\n");
      }
634 635 636
      xml.EndElement(); // PostContext
      xml.Element("RepeatCount", "0");
      xml.EndElement(); // "Error" / "Warning"
Ken Martin's avatar
Ken Martin committed
637
    }
638
  }
639 640
}

Wouter Klouwen's avatar
Wouter Klouwen committed
641 642
void cmCTestBuildHandler::GenerateXMLFooter(cmXMLWriter& xml,
                                            cmDuration elapsed_build_time)
643
{
644 645 646 647 648 649
  xml.StartElement("Log");
  xml.Attribute("Encoding", "base64");
  xml.Attribute("Compression", "bin/gzip");
  xml.EndElement(); // Log

  xml.Element("EndDateTime", this->EndBuild);
650
  xml.Element("EndBuildTime", this->EndBuildTime);
651 652 653 654
  xml.Element(
    "ElapsedMinutes",
    std::chrono::duration_cast<std::chrono::minutes>(elapsed_build_time)
      .count());
655 656
  xml.EndElement(); // Build
  this->CTest->EndXML(xml);
657 658 659 660 661
}

bool cmCTestBuildHandler::IsLaunchedErrorFile(const char* fname)
{
  // error-{hash}.xml
662
  return (cmHasLiteralPrefix(fname, "error-") &&
663
          strcmp(fname + strlen(fname) - 4, ".xml") == 0);
664 665 666 667 668
}

bool cmCTestBuildHandler::IsLaunchedWarningFile(const char* fname)
{
  // warning-{hash}.xml
669
  return (cmHasLiteralPrefix(fname, "warning-") &&
670
          strcmp(fname + strlen(fname) - 4, ".xml") == 0);
671 672
}

Andy Cedilnik's avatar
Andy Cedilnik committed
673 674 675 676 677
//######################################################################
//######################################################################
//######################################################################
//######################################################################

678 679 680 681 682
class cmCTestBuildHandler::LaunchHelper
{
public:
  LaunchHelper(cmCTestBuildHandler* handler);
  ~LaunchHelper();
683 684
  LaunchHelper(const LaunchHelper&) = delete;
  LaunchHelper& operator=(const LaunchHelper&) = delete;
685

686 687 688 689
private:
  cmCTestBuildHandler* Handler;
  cmCTest* CTest;

690
  void WriteLauncherConfig();
691 692 693 694
  void WriteScrapeMatchers(const char* purpose,
                           std::vector<std::string> const& matchers);
};

695 696 697
cmCTestBuildHandler::LaunchHelper::LaunchHelper(cmCTestBuildHandler* handler)
  : Handler(handler)
  , CTest(handler->CTest)
698 699
{
  std::string tag = this->CTest->GetCurrentTag();
700
  if (tag.empty()) {
701 702 703
    // This is not for a dashboard submission, so there is no XML.
    // Skip enabling the launchers.
    this->Handler->UseCTestLaunch = false;
704
  } else {
705 706 707 708 709 710 711 712
    // Compute a directory in which to store launcher fragments.
    std::string& launchDir = this->Handler->CTestLaunchDir;
    launchDir = this->CTest->GetBinaryDir();
    launchDir += "/Testing/";
    launchDir += tag;
    launchDir += "/Build";

    // Clean out any existing launcher fragments.
713
    cmSystemTools::RemoveADirectory(launchDir);
714

715
    if (this->Handler->UseCTestLaunch) {
716
      // Enable launcher fragments.
717
      cmSystemTools::MakeDirectory(launchDir);
718
      this->WriteLauncherConfig();
719 720
      std::string launchEnv = "CTEST_LAUNCH_LOGS=";
      launchEnv += launchDir;
721
      cmSystemTools::PutEnv(launchEnv);
722
    }
723
  }
724 725

  // If not using launchers, make sure they passthru.
726
  if (!this->Handler->UseCTestLaunch) {
727
    cmSystemTools::UnsetEnv("CTEST_LAUNCH_LOGS");
728
  }
729 730 731 732
}

cmCTestBuildHandler::LaunchHelper::~LaunchHelper()
{
733
  if (this->Handler->UseCTestLaunch) {
734
    cmSystemTools::UnsetEnv("CTEST_LAUNCH_LOGS");
735
  }
736 737
}

738
void cmCTestBuildHandler::LaunchHelper::WriteLauncherConfig()
739 740 741 742 743
{
  this->WriteScrapeMatchers("Warning",
                            this->Handler->ReallyCustomWarningMatches);
  this->WriteScrapeMatchers("WarningSuppress",
                            this->Handler->ReallyCustomWarningExceptions);
744 745 746 747

  // Give some testing configuration information to the launcher.
  std::string fname = this->Handler->CTestLaunchDir;
  fname += "/CTestLaunchConfig.cmake";
748
  cmGeneratedFileStream fout(fname);
749 750
  std::string srcdir = this->CTest->GetCTestConfiguration("SourceDirectory");
  fout << "set(CTEST_SOURCE_DIRECTORY \"" << srcdir << "\")\n";
751 752
}

753 754
void cmCTestBuildHandler::LaunchHelper::WriteScrapeMatchers(
  const char* purpose, std::vector<std::string> const& matchers)
755
{
756
  if (matchers.empty()) {
757
    return;
758
  }
759 760 761 762
  std::string fname = this->Handler->CTestLaunchDir;
  fname += "/Custom";
  fname += purpose;
  fname += ".txt";
763
  cmGeneratedFileStream fout(fname);
764 765
  for (std::string const& m : matchers) {
    fout << m << "\n";
766
  }
767 768
}

769 770 771 772
int cmCTestBuildHandler::RunMakeCommand(const std::string& command,
                                        int* retVal, const char* dir,
                                        int timeout, std::ostream& ofs,
                                        Encoding encoding)
773 774
{
  // First generate the command and arguments
775
  std::vector<std::string> args = cmSystemTools::ParseArguments(command);
776

777
  if (args.empty()) {
778
    return false;
779
  }
780 781

  std::vector<const char*> argv;
782
  argv.reserve(args.size() + 1);
783 784
  for (std::string const& arg : args) {
    argv.push_back(arg.c_str());
785
  }
Daniel Pfeifer's avatar
Daniel Pfeifer committed
786
  argv.push_back(nullptr);
787

788 789
  cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
                     "Run command:", this->Quiet);
790 791 792 793
  for (char const* arg : argv) {
    if (!arg) {
      break;
    }
794
    cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT,
795
                       " \"" << arg << "\"", this->Quiet);
796
  }
797
  cmCTestOptionalLog(this->CTest, HANDLER_VERBOSE_OUTPUT, std::endl,
798
                     this->Quiet);
Andy Cedilnik's avatar
Andy Cedilnik committed
799

800 801 802 803
  // Optionally use make rule launchers to record errors and warnings.
  LaunchHelper launchHelper(this);
  static_cast<void>(launchHelper);

804 805
  // Now create process object
  cmsysProcess* cp = cmsysProcess_New();
806
  cmsysProcess_SetCommand(cp, argv.data());
807 808 809 810 811 812 813 814 815 816 817
  cmsysProcess_SetWorkingDirectory(cp, dir);
  cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  cmsysProcess_SetTimeout(cp, timeout);
  cmsysProcess_Execute(cp);

  // Initialize tick's
  std::string::size_type tick = 0;
  const std::string::size_type tick_len = 1024;

  char* data;
  int length;
818
  cmProcessOutput processOutput(encoding);
819
  std::string strdata;
820
  cmCTestOptionalLog(
821 822
    this->CTest, HANDLER_PROGRESS_OUTPUT,
    "   Each symbol represents "
823 824 825 826 827 828
      << tick_len << " bytes of output." << std::endl
      << (this->UseCTestLaunch
            ? ""
            : "   '!' represents an error and '*' a warning.\n")
      << "    " << std::flush,
    this->Quiet);
829 830

  // Initialize building structures
Andy Cedilnik's avatar
Andy Cedilnik committed
831 832 833 834 835 836 837 838 839
  this->BuildProcessingQueue.clear();
  this->OutputLineCounter = 0;
  this->ErrorsAndWarnings.clear();
  this->TotalErrors = 0;
  this->TotalWarnings = 0;
  this->BuildOutputLogSize = 0;
  this->LastTickChar = '.';
  this->WarningQuotaReached = false;
  this->ErrorQuotaReached = false;
840 841

  // For every chunk of data
842
  int res;
Daniel Pfeifer's avatar
Daniel Pfeifer committed
843
  while ((res = cmsysProcess_WaitForData(cp, &data, &length, nullptr))) {
844 845
    // Replace '\0' with '\n', since '\0' does not really make sense. This is
    // for Visual Studio output
846 847
    for (int cc = 0; cc < length; ++cc) {
      if (data[cc] == 0) {
848 849
        data[cc] = '\n';
      }