cmake/Source/CTest/cmCTestBuildAndTestHandler.cxx

458 lines
14 KiB
C++
Raw Normal View History

2009-10-04 10:30:41 +03:00
/*============================================================================
CMake - Cross Platform Makefile Generator
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
2009-10-04 10:30:41 +03:00
Distributed under the OSI-approved BSD License (the "License");
see accompanying file Copyright.txt for details.
2009-10-04 10:30:41 +03:00
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
============================================================================*/
#include "cmCTestBuildAndTestHandler.h"
#include "cmCTest.h"
2016-07-09 11:21:54 +02:00
#include "cmCTestTestHandler.h"
#include "cmGlobalGenerator.h"
2016-07-09 11:21:54 +02:00
#include "cmSystemTools.h"
#include "cmake.h"
#include <cmsys/Process.h>
cmCTestBuildAndTestHandler::cmCTestBuildAndTestHandler()
{
2016-07-09 11:21:54 +02:00
this->BuildTwoConfig = false;
this->BuildNoClean = false;
this->BuildNoCMake = false;
this->Timeout = 0;
}
void cmCTestBuildAndTestHandler::Initialize()
{
2015-08-17 11:37:30 +02:00
this->BuildTargets.clear();
this->Superclass::Initialize();
}
const char* cmCTestBuildAndTestHandler::GetOutput()
{
return this->Output.c_str();
}
int cmCTestBuildAndTestHandler::ProcessHandler()
{
this->Output = "";
std::string output;
cmSystemTools::ResetErrorOccuredFlag();
int retv = this->RunCMakeAndTest(&this->Output);
cmSystemTools::ResetErrorOccuredFlag();
return retv;
}
int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
2016-07-09 11:21:54 +02:00
std::ostringstream& out,
std::string& cmakeOutString,
std::string& cwd, cmake* cm)
{
unsigned int k;
std::vector<std::string> args;
2014-08-03 19:52:23 +02:00
args.push_back(cmSystemTools::GetCMakeCommand());
args.push_back(this->SourceDir);
2016-07-09 11:21:54 +02:00
if (!this->BuildGenerator.empty()) {
std::string generator = "-G";
generator += this->BuildGenerator;
args.push_back(generator);
2016-07-09 11:21:54 +02:00
}
if (!this->BuildGeneratorPlatform.empty()) {
2015-04-27 22:25:09 +02:00
std::string platform = "-A";
platform += this->BuildGeneratorPlatform;
args.push_back(platform);
2016-07-09 11:21:54 +02:00
}
if (!this->BuildGeneratorToolset.empty()) {
2013-03-16 19:13:01 +02:00
std::string toolset = "-T";
toolset += this->BuildGeneratorToolset;
args.push_back(toolset);
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
const char* config = 0;
2016-07-09 11:21:54 +02:00
if (!this->CTest->GetConfigType().empty()) {
config = this->CTest->GetConfigType().c_str();
2016-07-09 11:21:54 +02:00
}
#ifdef CMAKE_INTDIR
2016-07-09 11:21:54 +02:00
if (!config) {
config = CMAKE_INTDIR;
2016-07-09 11:21:54 +02:00
}
#endif
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (config) {
std::string btype = "-DCMAKE_BUILD_TYPE:STRING=" + std::string(config);
args.push_back(btype);
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
for (k = 0; k < this->BuildOptions.size(); ++k) {
args.push_back(this->BuildOptions[k]);
2016-07-09 11:21:54 +02:00
}
if (cm->Run(args) != 0) {
out << "Error: cmake execution failed\n";
out << cmakeOutString << "\n";
// return to the original directory
2015-04-27 22:25:09 +02:00
cmSystemTools::ChangeDirectory(cwd);
2016-07-09 11:21:54 +02:00
if (outstring) {
*outstring = out.str();
2016-07-09 11:21:54 +02:00
} else {
cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
}
2016-07-09 11:21:54 +02:00
return 1;
}
// do another config?
2016-07-09 11:21:54 +02:00
if (this->BuildTwoConfig) {
if (cm->Run(args) != 0) {
out << "Error: cmake execution failed\n";
out << cmakeOutString << "\n";
// return to the original directory
2015-04-27 22:25:09 +02:00
cmSystemTools::ChangeDirectory(cwd);
2016-07-09 11:21:54 +02:00
if (outstring) {
*outstring = out.str();
2016-07-09 11:21:54 +02:00
} else {
cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
}
2016-07-09 11:21:54 +02:00
return 1;
}
2016-07-09 11:21:54 +02:00
}
out << "======== CMake output ======\n";
out << cmakeOutString;
out << "======== End CMake output ======\n";
return 0;
}
void CMakeMessageCallback(const char* m, const char*, bool&, void* s)
{
std::string* out = (std::string*)s;
*out += m;
*out += "\n";
}
2016-07-09 11:21:54 +02:00
void CMakeProgressCallback(const char* msg, float, void* s)
{
std::string* out = (std::string*)s;
*out += msg;
*out += "\n";
}
2015-04-27 22:25:09 +02:00
void CMakeOutputCallback(const char* m, size_t len, void* s)
{
std::string* out = (std::string*)s;
out->append(m, len);
}
2015-04-27 22:25:09 +02:00
class cmCTestBuildAndTestCaptureRAII
2009-10-04 10:30:41 +03:00
{
2015-04-27 22:25:09 +02:00
cmake& CM;
2016-07-09 11:21:54 +02:00
2015-04-27 22:25:09 +02:00
public:
2016-07-09 11:21:54 +02:00
cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s)
: CM(cm)
{
2015-04-27 22:25:09 +02:00
cmSystemTools::SetMessageCallback(CMakeMessageCallback, &s);
cmSystemTools::SetStdoutCallback(CMakeOutputCallback, &s);
cmSystemTools::SetStderrCallback(CMakeOutputCallback, &s);
this->CM.SetProgressCallback(CMakeProgressCallback, &s);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
~cmCTestBuildAndTestCaptureRAII()
2016-07-09 11:21:54 +02:00
{
2015-04-27 22:25:09 +02:00
this->CM.SetProgressCallback(0, 0);
cmSystemTools::SetStderrCallback(0, 0);
2009-10-04 10:30:41 +03:00
cmSystemTools::SetStdoutCallback(0, 0);
2015-04-27 22:25:09 +02:00
cmSystemTools::SetMessageCallback(0, 0);
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
};
int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
{
// if the generator and make program are not specified then it is an error
2016-07-09 11:21:54 +02:00
if (this->BuildGenerator.empty()) {
if (outstring) {
*outstring = "--build-and-test requires that the generator "
"be provided using the --build-generator "
"command line option. ";
}
2016-07-09 11:21:54 +02:00
return 1;
}
2015-04-27 22:25:09 +02:00
cmake cm;
2015-08-17 11:37:30 +02:00
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
2015-04-27 22:25:09 +02:00
std::string cmakeOutString;
cmCTestBuildAndTestCaptureRAII captureRAII(cm, cmakeOutString);
static_cast<void>(captureRAII);
std::ostringstream out;
2016-07-09 11:21:54 +02:00
if (this->CTest->GetConfigType().empty() && !this->ConfigSample.empty()) {
// use the config sample to set the ConfigType
std::string fullPath;
std::string resultingConfig;
std::vector<std::string> extraPaths;
std::vector<std::string> failed;
2016-07-09 11:21:54 +02:00
fullPath = cmCTestTestHandler::FindExecutable(
this->CTest, this->ConfigSample.c_str(), resultingConfig, extraPaths,
failed);
if (!fullPath.empty() && !resultingConfig.empty()) {
this->CTest->SetConfigType(resultingConfig.c_str());
}
2016-07-09 11:21:54 +02:00
out << "Using config sample with results: " << fullPath << " and "
<< resultingConfig << std::endl;
}
2013-03-16 19:13:01 +02:00
// we need to honor the timeout specified, the timeout include cmake, build
// and test time
double clock_start = cmSystemTools::GetTime();
// make sure the binary dir is there
std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
2016-07-09 11:21:54 +02:00
out << "Internal cmake changing into directory: " << this->BinaryDir
<< std::endl;
if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) {
cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
cmSystemTools::ChangeDirectory(this->BinaryDir);
2016-07-09 11:21:54 +02:00
if (this->BuildNoCMake) {
2014-08-03 19:52:23 +02:00
// Make the generator available for the Build call below.
2016-07-09 11:21:54 +02:00
cm.SetGlobalGenerator(cm.CreateGlobalGenerator(this->BuildGenerator));
2015-04-27 22:25:09 +02:00
cm.SetGeneratorPlatform(this->BuildGeneratorPlatform);
2013-03-16 19:13:01 +02:00
cm.SetGeneratorToolset(this->BuildGeneratorToolset);
2014-08-03 19:52:23 +02:00
// Load the cache to make CMAKE_MAKE_PROGRAM available.
2015-08-17 11:37:30 +02:00
cm.LoadCache(this->BinaryDir);
2016-07-09 11:21:54 +02:00
} else {
// do the cmake step, no timeout here since it is not a sub process
2016-07-09 11:21:54 +02:00
if (this->RunCMake(outstring, out, cmakeOutString, cwd, &cm)) {
return 1;
}
2016-07-09 11:21:54 +02:00
}
// do the build
std::vector<std::string>::iterator tarIt;
2016-07-09 11:21:54 +02:00
if (this->BuildTargets.empty()) {
this->BuildTargets.push_back("");
2016-07-09 11:21:54 +02:00
}
for (tarIt = this->BuildTargets.begin(); tarIt != this->BuildTargets.end();
++tarIt) {
double remainingTime = 0;
2016-07-09 11:21:54 +02:00
if (this->Timeout > 0) {
remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
2016-07-09 11:21:54 +02:00
if (remainingTime <= 0) {
if (outstring) {
*outstring = "--build-and-test timeout exceeded. ";
}
2016-07-09 11:21:54 +02:00
return 1;
}
2016-07-09 11:21:54 +02:00
}
std::string output;
const char* config = 0;
2016-07-09 11:21:54 +02:00
if (!this->CTest->GetConfigType().empty()) {
config = this->CTest->GetConfigType().c_str();
2016-07-09 11:21:54 +02:00
}
#ifdef CMAKE_INTDIR
2016-07-09 11:21:54 +02:00
if (!config) {
config = CMAKE_INTDIR;
2016-07-09 11:21:54 +02:00
}
#endif
2016-07-09 11:21:54 +02:00
if (!config) {
config = "Debug";
2016-07-09 11:21:54 +02:00
}
int retVal = cm.GetGlobalGenerator()->Build(
2016-07-09 11:21:54 +02:00
this->SourceDir, this->BinaryDir, this->BuildProject, *tarIt, output,
this->BuildMakeProgram, config, !this->BuildNoClean, false, false,
remainingTime);
out << output;
// if the build failed then return
2016-07-09 11:21:54 +02:00
if (retVal) {
if (outstring) {
*outstring = out.str();
}
2016-07-09 11:21:54 +02:00
return 1;
}
2016-07-09 11:21:54 +02:00
}
if (outstring) {
*outstring = out.str();
}
// if no test was specified then we are done
2016-07-09 11:21:54 +02:00
if (this->TestCommand.empty()) {
return 0;
2016-07-09 11:21:54 +02:00
}
// now run the compiled test if we can find it
// store the final location in fullPath
std::string fullPath;
std::string resultingConfig;
std::vector<std::string> extraPaths;
// if this->ExecutableDirectory is set try that as well
2016-07-09 11:21:54 +02:00
if (!this->ExecutableDirectory.empty()) {
std::string tempPath = this->ExecutableDirectory;
tempPath += "/";
tempPath += this->TestCommand;
extraPaths.push_back(tempPath);
2016-07-09 11:21:54 +02:00
}
std::vector<std::string> failed;
2013-03-16 19:13:01 +02:00
fullPath =
2016-07-09 11:21:54 +02:00
cmCTestTestHandler::FindExecutable(this->CTest, this->TestCommand.c_str(),
resultingConfig, extraPaths, failed);
if (!cmSystemTools::FileExists(fullPath.c_str())) {
out << "Could not find path to executable, perhaps it was not built: "
2016-07-09 11:21:54 +02:00
<< this->TestCommand << "\n";
out << "tried to find it in these places:\n";
out << fullPath.c_str() << "\n";
2016-07-09 11:21:54 +02:00
for (unsigned int i = 0; i < failed.size(); ++i) {
out << failed[i] << "\n";
2016-07-09 11:21:54 +02:00
}
if (outstring) {
*outstring = out.str();
} else {
cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
2016-07-09 11:21:54 +02:00
}
// return to the original directory
2015-04-27 22:25:09 +02:00
cmSystemTools::ChangeDirectory(cwd);
return 1;
2016-07-09 11:21:54 +02:00
}
std::vector<const char*> testCommand;
testCommand.push_back(fullPath.c_str());
2016-07-09 11:21:54 +02:00
for (size_t k = 0; k < this->TestCommandArgs.size(); ++k) {
testCommand.push_back(this->TestCommandArgs[k].c_str());
2016-07-09 11:21:54 +02:00
}
testCommand.push_back(0);
std::string outs;
int retval = 0;
// run the test from the this->BuildRunDir if set
2016-07-09 11:21:54 +02:00
if (!this->BuildRunDir.empty()) {
out << "Run test in directory: " << this->BuildRunDir << "\n";
2015-04-27 22:25:09 +02:00
cmSystemTools::ChangeDirectory(this->BuildRunDir);
2016-07-09 11:21:54 +02:00
}
out << "Running test command: \"" << fullPath << "\"";
2016-07-09 11:21:54 +02:00
for (size_t k = 0; k < this->TestCommandArgs.size(); ++k) {
out << " \"" << this->TestCommandArgs[k] << "\"";
2016-07-09 11:21:54 +02:00
}
out << "\n";
// how much time is remaining
double remainingTime = 0;
2016-07-09 11:21:54 +02:00
if (this->Timeout > 0) {
remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
2016-07-09 11:21:54 +02:00
if (remainingTime <= 0) {
if (outstring) {
*outstring = "--build-and-test timeout exceeded. ";
}
2016-07-09 11:21:54 +02:00
return 1;
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
int runTestRes =
this->CTest->RunTest(testCommand, &outs, &retval, 0, remainingTime, 0);
2016-07-09 11:21:54 +02:00
if (runTestRes != cmsysProcess_State_Exited || retval != 0) {
out << "Test command failed: " << testCommand[0] << "\n";
retval = 1;
2016-07-09 11:21:54 +02:00
}
out << outs << "\n";
2016-07-09 11:21:54 +02:00
if (outstring) {
*outstring = out.str();
2016-07-09 11:21:54 +02:00
} else {
cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
2016-07-09 11:21:54 +02:00
}
return retval;
}
int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
const std::string& currentArg, size_t& idx,
const std::vector<std::string>& allArgs)
{
// --build-and-test options
2016-07-09 11:21:54 +02:00
if (currentArg.find("--build-and-test", 0) == 0 &&
idx < allArgs.size() - 1) {
if (idx + 2 < allArgs.size()) {
idx++;
this->SourceDir = allArgs[idx];
idx++;
this->BinaryDir = allArgs[idx];
// dir must exist before CollapseFullPath is called
cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
2016-07-09 11:21:54 +02:00
this->BinaryDir = cmSystemTools::CollapseFullPath(this->BinaryDir);
this->SourceDir = cmSystemTools::CollapseFullPath(this->SourceDir);
} else {
cmCTestLog(this->CTest, ERROR_MESSAGE,
2016-07-09 11:21:54 +02:00
"--build-and-test must have source and binary dir"
<< std::endl);
return 0;
}
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-target", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
this->BuildTargets.push_back(allArgs[idx]);
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-nocmake", 0) == 0) {
this->BuildNoCMake = true;
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-run-dir", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
this->BuildRunDir = allArgs[idx];
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-two-config", 0) == 0) {
this->BuildTwoConfig = true;
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-exe-dir", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
this->ExecutableDirectory = allArgs[idx];
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--test-timeout", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
this->Timeout = atof(allArgs[idx].c_str());
2016-07-09 11:21:54 +02:00
}
if (currentArg == "--build-generator" && idx < allArgs.size() - 1) {
idx++;
this->BuildGenerator = allArgs[idx];
2016-07-09 11:21:54 +02:00
}
if (currentArg == "--build-generator-platform" && idx < allArgs.size() - 1) {
2015-04-27 22:25:09 +02:00
idx++;
this->BuildGeneratorPlatform = allArgs[idx];
2016-07-09 11:21:54 +02:00
}
if (currentArg == "--build-generator-toolset" && idx < allArgs.size() - 1) {
2013-03-16 19:13:01 +02:00
idx++;
this->BuildGeneratorToolset = allArgs[idx];
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-project", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
this->BuildProject = allArgs[idx];
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-makeprogram", 0) == 0 &&
idx < allArgs.size() - 1) {
idx++;
this->BuildMakeProgram = allArgs[idx];
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-config-sample", 0) == 0 &&
idx < allArgs.size() - 1) {
idx++;
this->ConfigSample = allArgs[idx];
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-noclean", 0) == 0) {
this->BuildNoClean = true;
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--build-options", 0) == 0) {
while (idx + 1 < allArgs.size() && allArgs[idx + 1] != "--build-target" &&
allArgs[idx + 1] != "--test-command") {
2014-08-03 19:52:23 +02:00
++idx;
this->BuildOptions.push_back(allArgs[idx]);
}
2016-07-09 11:21:54 +02:00
}
if (currentArg.find("--test-command", 0) == 0 && idx < allArgs.size() - 1) {
++idx;
this->TestCommand = allArgs[idx];
2016-07-09 11:21:54 +02:00
while (idx + 1 < allArgs.size()) {
++idx;
this->TestCommandArgs.push_back(allArgs[idx]);
}
2016-07-09 11:21:54 +02:00
}
return 1;
}