cmCTestBuildAndTestHandler.cxx 13.9 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.  */
3
4
5
#include "cmCTestBuildAndTestHandler.h"

#include "cmCTest.h"
6
#include "cmCTestTestHandler.h"
7
#include "cmGlobalGenerator.h"
8
#include "cmMakefile.h"
9
#include "cmState.h"
10
#include "cmSystemTools.h"
11
#include "cmWorkingDirectory.h"
12
#include "cmake.h"
13

14
#include "cmsys/Process.h"
15
#include <chrono>
16
#include <cstring>
17
#include <ratio>
18
#include <stdlib.h>
19
20
21

cmCTestBuildAndTestHandler::cmCTestBuildAndTestHandler()
{
22
23
24
  this->BuildTwoConfig = false;
  this->BuildNoClean = false;
  this->BuildNoCMake = false;
Wouter Klouwen's avatar
Wouter Klouwen committed
25
  this->Timeout = cmDuration::zero();
26
27
}

28
29
void cmCTestBuildAndTestHandler::Initialize()
{
30
  this->BuildTargets.clear();
31
  this->Superclass::Initialize();
32
33
}

34
35
const char* cmCTestBuildAndTestHandler::GetOutput()
{
Andy Cedilnik's avatar
Andy Cedilnik committed
36
  return this->Output.c_str();
37
38
39
}
int cmCTestBuildAndTestHandler::ProcessHandler()
{
40
  this->Output.clear();
41
42
  std::string output;
  cmSystemTools::ResetErrorOccuredFlag();
Andy Cedilnik's avatar
Andy Cedilnik committed
43
  int retv = this->RunCMakeAndTest(&this->Output);
44
45
46
47
  cmSystemTools::ResetErrorOccuredFlag();
  return retv;
}

Andy Cedilnik's avatar
Andy Cedilnik committed
48
int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
49
50
                                         std::ostringstream& out,
                                         std::string& cmakeOutString,
51
                                         cmake* cm)
52
53
{
  std::vector<std::string> args;
54
  args.push_back(cmSystemTools::GetCMakeCommand());
Andy Cedilnik's avatar
Andy Cedilnik committed
55
  args.push_back(this->SourceDir);
56
  if (!this->BuildGenerator.empty()) {
57
    args.push_back("-G" + this->BuildGenerator);
58
59
  }
  if (!this->BuildGeneratorPlatform.empty()) {
60
    args.push_back("-A" + this->BuildGeneratorPlatform);
61
62
  }
  if (!this->BuildGeneratorToolset.empty()) {
63
    args.push_back("-T" + this->BuildGeneratorToolset);
64
  }
65

Daniel Pfeifer's avatar
Daniel Pfeifer committed
66
  const char* config = nullptr;
67
  if (!this->CTest->GetConfigType().empty()) {
68
    config = this->CTest->GetConfigType().c_str();
69
  }
70
#ifdef CMAKE_INTDIR
71
  if (!config) {
72
    config = CMAKE_INTDIR;
73
  }
74
#endif
75

76
  if (config) {
77
    args.push_back("-DCMAKE_BUILD_TYPE:STRING=" + std::string(config));
78
  }
79

80
81
  for (std::string const& opt : this->BuildOptions) {
    args.push_back(opt);
82
83
  }
  if (cm->Run(args) != 0) {
84
85
    out << "Error: cmake execution failed\n";
    out << cmakeOutString << "\n";
86
    if (outstring) {
87
      *outstring = out.str();
88
    } else {
Andy Cedilnik's avatar
Andy Cedilnik committed
89
      cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
90
    }
91
92
    return 1;
  }
93
  // do another config?
94
95
  if (this->BuildTwoConfig) {
    if (cm->Run(args) != 0) {
96
97
      out << "Error: cmake execution failed\n";
      out << cmakeOutString << "\n";
98
      if (outstring) {
99
        *outstring = out.str();
100
      } else {
Andy Cedilnik's avatar
Andy Cedilnik committed
101
        cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
102
      }
103
      return 1;
Andy Cedilnik's avatar
Andy Cedilnik committed
104
    }
105
  }
106
107
108
  out << "======== CMake output     ======\n";
  out << cmakeOutString;
  out << "======== End CMake output ======\n";
109
110
111
  return 0;
}

112
113
void CMakeMessageCallback(const char* m, const char* /*unused*/,
                          bool& /*unused*/, void* s)
114
{
115
  std::string* out = static_cast<std::string*>(s);
116
117
118
119
  *out += m;
  *out += "\n";
}

120
void CMakeProgressCallback(const char* msg, float /*unused*/, void* s)
121
{
122
  std::string* out = static_cast<std::string*>(s);
123
124
125
126
  *out += msg;
  *out += "\n";
}

127
void CMakeOutputCallback(const char* m, size_t len, void* s)
128
{
129
  std::string* out = static_cast<std::string*>(s);
130
131
  out->append(m, len);
}
132
133

class cmCTestBuildAndTestCaptureRAII
134
{
135
  cmake& CM;
136

137
public:
138
139
140
  cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s)
    : CM(cm)
  {
141
    cmSystemTools::SetMessageCallback(CMakeMessageCallback, &s);
142
143
    cmSystemTools::SetStdoutCallback(CMakeOutputCallback, &s);
    cmSystemTools::SetStderrCallback(CMakeOutputCallback, &s);
144
    this->CM.SetProgressCallback(CMakeProgressCallback, &s);
145
  }
146
  ~cmCTestBuildAndTestCaptureRAII()
147
  {
Daniel Pfeifer's avatar
Daniel Pfeifer committed
148
149
150
151
    this->CM.SetProgressCallback(nullptr, nullptr);
    cmSystemTools::SetStderrCallback(nullptr, nullptr);
    cmSystemTools::SetStdoutCallback(nullptr, nullptr);
    cmSystemTools::SetMessageCallback(nullptr, nullptr);
152
  }
153
};
154
155

int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
Andy Cedilnik's avatar
Andy Cedilnik committed
156
{
157
  // if the generator and make program are not specified then it is an error
158
159
160
161
162
  if (this->BuildGenerator.empty()) {
    if (outstring) {
      *outstring = "--build-and-test requires that the generator "
                   "be provided using the --build-generator "
                   "command line option. ";
163
    }
164
165
    return 1;
  }
Andy Cedilnik's avatar
Andy Cedilnik committed
166

167
  cmake cm(cmake::RoleProject, cmState::Project);
168
169
  cm.SetHomeDirectory("");
  cm.SetHomeOutputDirectory("");
170
171
172
  std::string cmakeOutString;
  cmCTestBuildAndTestCaptureRAII captureRAII(cm, cmakeOutString);
  static_cast<void>(captureRAII);
173
  std::ostringstream out;
174

175
  if (this->CTest->GetConfigType().empty() && !this->ConfigSample.empty()) {
176
177
178
179
180
    // use the config sample to set the ConfigType
    std::string fullPath;
    std::string resultingConfig;
    std::vector<std::string> extraPaths;
    std::vector<std::string> failed;
181
182
183
184
    fullPath = cmCTestTestHandler::FindExecutable(
      this->CTest, this->ConfigSample.c_str(), resultingConfig, extraPaths,
      failed);
    if (!fullPath.empty() && !resultingConfig.empty()) {
185
186
      this->CTest->SetConfigType(resultingConfig.c_str());
    }
187
188
189
    out << "Using config sample with results: " << fullPath << " and "
        << resultingConfig << std::endl;
  }
190

191
192
  // we need to honor the timeout specified, the timeout include cmake, build
  // and test time
193
  auto clock_start = std::chrono::steady_clock::now();
194

195
  // make sure the binary dir is there
196
197
198
  out << "Internal cmake changing into directory: " << this->BinaryDir
      << std::endl;
  if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) {
199
    cmSystemTools::MakeDirectory(this->BinaryDir);
200
  }
201
  cmWorkingDirectory workdir(this->BinaryDir);
202
203
204
205
206
207
208
209
210
211
  if (workdir.Failed()) {
    auto msg = "Failed to change working directory to " + this->BinaryDir +
      " : " + std::strerror(workdir.GetLastResult()) + "\n";
    if (outstring) {
      *outstring = msg;
    } else {
      cmCTestLog(this->CTest, ERROR_MESSAGE, msg);
    }
    return 1;
  }
212

213
  if (this->BuildNoCMake) {
214
    // Make the generator available for the Build call below.
215
216
217
218
219
220
221
222
    cmGlobalGenerator* gen = cm.CreateGlobalGenerator(this->BuildGenerator);
    cm.SetGlobalGenerator(gen);
    if (!this->BuildGeneratorPlatform.empty()) {
      cmMakefile mf(gen, cm.GetCurrentSnapshot());
      if (!gen->SetGeneratorPlatform(this->BuildGeneratorPlatform, &mf)) {
        return 1;
      }
    }
223
224

    // Load the cache to make CMAKE_MAKE_PROGRAM available.
225
    cm.LoadCache(this->BinaryDir);
226
  } else {
227
    // do the cmake step, no timeout here since it is not a sub process
228
    if (this->RunCMake(outstring, out, cmakeOutString, &cm)) {
229
230
      return 1;
    }
231
  }
232
233

  // do the build
234
  if (this->BuildTargets.empty()) {
Andy Cedilnik's avatar
Andy Cedilnik committed
235
    this->BuildTargets.push_back("");
236
  }
237
  for (std::string const& tar : this->BuildTargets) {
Wouter Klouwen's avatar
Wouter Klouwen committed
238
239
    cmDuration remainingTime = std::chrono::seconds(0);
    if (this->Timeout > cmDuration::zero()) {
240
241
      remainingTime =
        this->Timeout - (std::chrono::steady_clock::now() - clock_start);
242
      if (remainingTime <= std::chrono::seconds(0)) {
243
        if (outstring) {
244
245
          *outstring = "--build-and-test timeout exceeded. ";
        }
246
        return 1;
247
      }
248
    }
249
    std::string output;
Daniel Pfeifer's avatar
Daniel Pfeifer committed
250
    const char* config = nullptr;
251
    if (!this->CTest->GetConfigType().empty()) {
252
      config = this->CTest->GetConfigType().c_str();
253
    }
254
#ifdef CMAKE_INTDIR
255
    if (!config) {
256
      config = CMAKE_INTDIR;
257
    }
258
#endif
259
    if (!config) {
260
      config = "Debug";
261
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
262
    int retVal = cm.GetGlobalGenerator()->Build(
263
264
265
      cmake::NO_BUILD_PARALLEL_LEVEL, this->SourceDir, this->BinaryDir,
      this->BuildProject, tar, output, this->BuildMakeProgram, config,
      !this->BuildNoClean, false, false, remainingTime);
266
267
    out << output;
    // if the build failed then return
268
269
270
    if (retVal) {
      if (outstring) {
        *outstring = out.str();
271
      }
272
      return 1;
273
    }
274
275
276
277
  }
  if (outstring) {
    *outstring = out.str();
  }
Andy Cedilnik's avatar
Andy Cedilnik committed
278

279
  // if no test was specified then we are done
280
  if (this->TestCommand.empty()) {
281
    return 0;
282
  }
Andy Cedilnik's avatar
Andy Cedilnik committed
283

284
  // now run the compiled test if we can find it
285
286
287
288
  // store the final location in fullPath
  std::string fullPath;
  std::string resultingConfig;
  std::vector<std::string> extraPaths;
Andy Cedilnik's avatar
Andy Cedilnik committed
289
  // if this->ExecutableDirectory is set try that as well
290
  if (!this->ExecutableDirectory.empty()) {
291
    std::string tempPath = this->ExecutableDirectory;
292
    tempPath += "/";
Andy Cedilnik's avatar
Andy Cedilnik committed
293
    tempPath += this->TestCommand;
294
    extraPaths.push_back(tempPath);
295
  }
296
  std::vector<std::string> failed;
297
  fullPath =
298
299
300
    cmCTestTestHandler::FindExecutable(this->CTest, this->TestCommand.c_str(),
                                       resultingConfig, extraPaths, failed);

301
  if (!cmSystemTools::FileExists(fullPath)) {
Andy Cedilnik's avatar
Andy Cedilnik committed
302
    out << "Could not find path to executable, perhaps it was not built: "
303
        << this->TestCommand << "\n";
304
    out << "tried to find it in these places:\n";
305
    out << fullPath << "\n";
306
307
    for (std::string const& fail : failed) {
      out << fail << "\n";
308
309
310
311
    }
    if (outstring) {
      *outstring = out.str();
    } else {
Andy Cedilnik's avatar
Andy Cedilnik committed
312
      cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
313
    }
314
    return 1;
315
  }
316
317
318

  std::vector<const char*> testCommand;
  testCommand.push_back(fullPath.c_str());
319
320
  for (std::string const& testCommandArg : this->TestCommandArgs) {
    testCommand.push_back(testCommandArg.c_str());
321
  }
Daniel Pfeifer's avatar
Daniel Pfeifer committed
322
  testCommand.push_back(nullptr);
323
324
  std::string outs;
  int retval = 0;
Andy Cedilnik's avatar
Andy Cedilnik committed
325
  // run the test from the this->BuildRunDir if set
326
  if (!this->BuildRunDir.empty()) {
Andy Cedilnik's avatar
Andy Cedilnik committed
327
    out << "Run test in directory: " << this->BuildRunDir << "\n";
328
329
330
331
332
333
334
335
336
337
    if (!workdir.SetDirectory(this->BuildRunDir)) {
      out << "Failed to change working directory : "
          << std::strerror(workdir.GetLastResult()) << "\n";
      if (outstring) {
        *outstring = out.str();
      } else {
        cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
      }
      return 1;
    }
338
  }
339
  out << "Running test command: \"" << fullPath << "\"";
340
341
  for (std::string const& testCommandArg : this->TestCommandArgs) {
    out << " \"" << testCommandArg << "\"";
342
  }
343
  out << "\n";
344
345

  // how much time is remaining
Wouter Klouwen's avatar
Wouter Klouwen committed
346
347
  cmDuration remainingTime = std::chrono::seconds(0);
  if (this->Timeout > cmDuration::zero()) {
348
349
    remainingTime =
      this->Timeout - (std::chrono::steady_clock::now() - clock_start);
350
    if (remainingTime <= std::chrono::seconds(0)) {
351
      if (outstring) {
352
        *outstring = "--build-and-test timeout exceeded. ";
353
      }
354
      return 1;
355
    }
356
  }
357

Daniel Pfeifer's avatar
Daniel Pfeifer committed
358
  int runTestRes = this->CTest->RunTest(testCommand, &outs, &retval, nullptr,
359
                                        remainingTime, nullptr);
360

361
  if (runTestRes != cmsysProcess_State_Exited || retval != 0) {
362
    out << "Test command failed: " << testCommand[0] << "\n";
363
    retval = 1;
364
  }
365
366

  out << outs << "\n";
367
  if (outstring) {
368
    *outstring = out.str();
369
  } else {
Andy Cedilnik's avatar
Andy Cedilnik committed
370
    cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
371
  }
372
373
374
375
376
377
378
379
  return retval;
}

int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
  const std::string& currentArg, size_t& idx,
  const std::vector<std::string>& allArgs)
{
  // --build-and-test options
380
381
382
  if (currentArg.find("--build-and-test", 0) == 0 &&
      idx < allArgs.size() - 1) {
    if (idx + 2 < allArgs.size()) {
383
      idx++;
Andy Cedilnik's avatar
Andy Cedilnik committed
384
      this->SourceDir = allArgs[idx];
385
      idx++;
Andy Cedilnik's avatar
Andy Cedilnik committed
386
      this->BinaryDir = allArgs[idx];
387
      // dir must exist before CollapseFullPath is called
388
      cmSystemTools::MakeDirectory(this->BinaryDir);
389
390
391
      this->BinaryDir = cmSystemTools::CollapseFullPath(this->BinaryDir);
      this->SourceDir = cmSystemTools::CollapseFullPath(this->SourceDir);
    } else {
Andy Cedilnik's avatar
Andy Cedilnik committed
392
      cmCTestLog(this->CTest, ERROR_MESSAGE,
393
394
                 "--build-and-test must have source and binary dir"
                   << std::endl);
395
396
      return 0;
    }
397
398
  }
  if (currentArg.find("--build-target", 0) == 0 && idx < allArgs.size() - 1) {
399
    idx++;
Andy Cedilnik's avatar
Andy Cedilnik committed
400
    this->BuildTargets.push_back(allArgs[idx]);
401
402
  }
  if (currentArg.find("--build-nocmake", 0) == 0) {
Andy Cedilnik's avatar
Andy Cedilnik committed
403
    this->BuildNoCMake = true;
404
405
  }
  if (currentArg.find("--build-run-dir", 0) == 0 && idx < allArgs.size() - 1) {
406
    idx++;
Andy Cedilnik's avatar
Andy Cedilnik committed
407
    this->BuildRunDir = allArgs[idx];
408
409
  }
  if (currentArg.find("--build-two-config", 0) == 0) {
Andy Cedilnik's avatar
Andy Cedilnik committed
410
    this->BuildTwoConfig = true;
411
412
  }
  if (currentArg.find("--build-exe-dir", 0) == 0 && idx < allArgs.size() - 1) {
413
    idx++;
Andy Cedilnik's avatar
Andy Cedilnik committed
414
    this->ExecutableDirectory = allArgs[idx];
415
416
  }
  if (currentArg.find("--test-timeout", 0) == 0 && idx < allArgs.size() - 1) {
417
    idx++;
Wouter Klouwen's avatar
Wouter Klouwen committed
418
    this->Timeout = cmDuration(atof(allArgs[idx].c_str()));
419
420
  }
  if (currentArg == "--build-generator" && idx < allArgs.size() - 1) {
421
    idx++;
Andy Cedilnik's avatar
Andy Cedilnik committed
422
    this->BuildGenerator = allArgs[idx];
423
424
  }
  if (currentArg == "--build-generator-platform" && idx < allArgs.size() - 1) {
425
426
    idx++;
    this->BuildGeneratorPlatform = allArgs[idx];
427
428
  }
  if (currentArg == "--build-generator-toolset" && idx < allArgs.size() - 1) {
429
430
    idx++;
    this->BuildGeneratorToolset = allArgs[idx];
431
432
  }
  if (currentArg.find("--build-project", 0) == 0 && idx < allArgs.size() - 1) {
433
    idx++;
Andy Cedilnik's avatar
Andy Cedilnik committed
434
    this->BuildProject = allArgs[idx];
435
436
437
  }
  if (currentArg.find("--build-makeprogram", 0) == 0 &&
      idx < allArgs.size() - 1) {
438
    idx++;
Andy Cedilnik's avatar
Andy Cedilnik committed
439
    this->BuildMakeProgram = allArgs[idx];
440
441
442
  }
  if (currentArg.find("--build-config-sample", 0) == 0 &&
      idx < allArgs.size() - 1) {
443
444
    idx++;
    this->ConfigSample = allArgs[idx];
445
446
  }
  if (currentArg.find("--build-noclean", 0) == 0) {
Andy Cedilnik's avatar
Andy Cedilnik committed
447
    this->BuildNoClean = true;
448
449
450
451
  }
  if (currentArg.find("--build-options", 0) == 0) {
    while (idx + 1 < allArgs.size() && allArgs[idx + 1] != "--build-target" &&
           allArgs[idx + 1] != "--test-command") {
452
      ++idx;
Andy Cedilnik's avatar
Andy Cedilnik committed
453
      this->BuildOptions.push_back(allArgs[idx]);
454
    }
455
456
  }
  if (currentArg.find("--test-command", 0) == 0 && idx < allArgs.size() - 1) {
457
    ++idx;
Andy Cedilnik's avatar
Andy Cedilnik committed
458
    this->TestCommand = allArgs[idx];
459
    while (idx + 1 < allArgs.size()) {
460
      ++idx;
Andy Cedilnik's avatar
Andy Cedilnik committed
461
      this->TestCommandArgs.push_back(allArgs[idx]);
462
    }
463
  }
464
465
  return 1;
}