CMake/Source/CTest/cmCTestBuildAndTestHandler.cxx

451 lines
14 KiB
C++
Raw Normal View History

Simplify CMake per-source license notices Per-source copyright/license notice headers that spell out copyright holder names and years are hard to maintain and often out-of-date or plain wrong. Precise contributor information is already maintained automatically by the version control tool. Ultimately it is the receiver of a file who is responsible for determining its licensing status, and per-source notices are merely a convenience. Therefore it is simpler and more accurate for each source to have a generic notice of the license name and references to more detailed information on copyright holders and full license terms. Our `Copyright.txt` file now contains a list of Contributors whose names appeared source-level copyright notices. It also references version control history for more precise information. Therefore we no longer need to spell out the list of Contributors in each source file notice. Replace CMake per-source copyright/license notice headers with a short description of the license and links to `Copyright.txt` and online information available from "https://cmake.org/licensing". The online URL also handles cases of modules being copied out of our source into other projects, so we can drop our notices about replacing links with full license text. Run the `Utilities/Scripts/filter-notices.bash` script to perform the majority of the replacements mechanically. Manually fix up shebang lines and trailing newlines in a few files. Manually update the notices in a few files that the script does not handle.
2016-09-27 22:01:08 +03:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCTestBuildAndTestHandler.h"
#include "cmCTest.h"
#include "cmCTestTestHandler.h"
#include "cmGlobalGenerator.h"
#include "cmSystemTools.h"
#include "cmake.h"
#include <cmsys/Process.h>
#include <stdlib.h>
cmCTestBuildAndTestHandler::cmCTestBuildAndTestHandler()
{
this->BuildTwoConfig = false;
this->BuildNoClean = false;
this->BuildNoCMake = false;
2007-01-25 19:16:16 +03:00
this->Timeout = 0;
}
void cmCTestBuildAndTestHandler::Initialize()
{
2015-02-15 15:42:17 +03:00
this->BuildTargets.clear();
this->Superclass::Initialize();
}
const char* cmCTestBuildAndTestHandler::GetOutput()
{
2006-03-10 23:03:09 +03:00
return this->Output.c_str();
}
int cmCTestBuildAndTestHandler::ProcessHandler()
{
2006-03-10 23:03:09 +03:00
this->Output = "";
std::string output;
cmSystemTools::ResetErrorOccuredFlag();
2006-03-10 23:03:09 +03:00
int retv = this->RunCMakeAndTest(&this->Output);
cmSystemTools::ResetErrorOccuredFlag();
return retv;
}
2006-03-09 19:17:10 +03:00
int cmCTestBuildAndTestHandler::RunCMake(std::string* outstring,
std::ostringstream& out,
std::string& cmakeOutString,
std::string& cwd, cmake* cm)
{
unsigned int k;
std::vector<std::string> args;
args.push_back(cmSystemTools::GetCMakeCommand());
2006-03-10 23:03:09 +03:00
args.push_back(this->SourceDir);
if (!this->BuildGenerator.empty()) {
std::string generator = "-G";
2006-03-10 23:03:09 +03:00
generator += this->BuildGenerator;
args.push_back(generator);
}
if (!this->BuildGeneratorPlatform.empty()) {
std::string platform = "-A";
platform += this->BuildGeneratorPlatform;
args.push_back(platform);
}
if (!this->BuildGeneratorToolset.empty()) {
std::string toolset = "-T";
toolset += this->BuildGeneratorToolset;
args.push_back(toolset);
}
2016-06-27 23:44:16 +03:00
const char* config = CM_NULLPTR;
if (!this->CTest->GetConfigType().empty()) {
config = this->CTest->GetConfigType().c_str();
}
#ifdef CMAKE_INTDIR
if (!config) {
config = CMAKE_INTDIR;
}
#endif
if (config) {
std::string btype = "-DCMAKE_BUILD_TYPE:STRING=" + std::string(config);
args.push_back(btype);
}
for (k = 0; k < this->BuildOptions.size(); ++k) {
2006-03-10 23:03:09 +03:00
args.push_back(this->BuildOptions[k]);
}
if (cm->Run(args) != 0) {
out << "Error: cmake execution failed\n";
out << cmakeOutString << "\n";
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
if (outstring) {
*outstring = out.str();
} else {
2006-03-10 23:03:09 +03:00
cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
}
return 1;
}
// do another config?
if (this->BuildTwoConfig) {
if (cm->Run(args) != 0) {
out << "Error: cmake execution failed\n";
out << cmakeOutString << "\n";
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
if (outstring) {
*outstring = out.str();
} else {
2006-03-10 23:03:09 +03:00
cmCTestLog(this->CTest, ERROR_MESSAGE, out.str() << std::endl);
}
return 1;
2006-03-09 19:17:10 +03:00
}
}
out << "======== CMake output ======\n";
out << cmakeOutString;
out << "======== End CMake output ======\n";
return 0;
}
void CMakeMessageCallback(const char* m, const char* /*unused*/,
bool& /*unused*/, void* s)
{
std::string* out = (std::string*)s;
*out += m;
*out += "\n";
}
void CMakeProgressCallback(const char* msg, float /*unused*/, void* s)
{
std::string* out = (std::string*)s;
*out += msg;
*out += "\n";
}
void CMakeOutputCallback(const char* m, size_t len, void* s)
{
std::string* out = (std::string*)s;
out->append(m, len);
}
class cmCTestBuildAndTestCaptureRAII
{
cmake& CM;
public:
cmCTestBuildAndTestCaptureRAII(cmake& cm, std::string& s)
: CM(cm)
{
cmSystemTools::SetMessageCallback(CMakeMessageCallback, &s);
cmSystemTools::SetStdoutCallback(CMakeOutputCallback, &s);
cmSystemTools::SetStderrCallback(CMakeOutputCallback, &s);
this->CM.SetProgressCallback(CMakeProgressCallback, &s);
}
~cmCTestBuildAndTestCaptureRAII()
{
2016-06-27 23:44:16 +03:00
this->CM.SetProgressCallback(CM_NULLPTR, CM_NULLPTR);
cmSystemTools::SetStderrCallback(CM_NULLPTR, CM_NULLPTR);
cmSystemTools::SetStdoutCallback(CM_NULLPTR, CM_NULLPTR);
cmSystemTools::SetMessageCallback(CM_NULLPTR, CM_NULLPTR);
}
};
int cmCTestBuildAndTestHandler::RunCMakeAndTest(std::string* outstring)
2006-03-09 19:17:10 +03:00
{
// if the generator and make program are not specified then it is an error
if (this->BuildGenerator.empty()) {
if (outstring) {
*outstring = "--build-and-test requires that the generator "
"be provided using the --build-generator "
"command line option. ";
}
return 1;
}
2006-03-09 19:17:10 +03:00
cmake cm;
cm.SetHomeDirectory("");
cm.SetHomeOutputDirectory("");
std::string cmakeOutString;
cmCTestBuildAndTestCaptureRAII captureRAII(cm, cmakeOutString);
static_cast<void>(captureRAII);
std::ostringstream out;
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;
fullPath = cmCTestTestHandler::FindExecutable(
this->CTest, this->ConfigSample.c_str(), resultingConfig, extraPaths,
failed);
if (!fullPath.empty() && !resultingConfig.empty()) {
this->CTest->SetConfigType(resultingConfig.c_str());
}
out << "Using config sample with results: " << fullPath << " and "
<< resultingConfig << std::endl;
}
// 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();
out << "Internal cmake changing into directory: " << this->BinaryDir
<< std::endl;
if (!cmSystemTools::FileIsDirectory(this->BinaryDir)) {
2006-03-10 23:03:09 +03:00
cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
}
cmSystemTools::ChangeDirectory(this->BinaryDir);
if (this->BuildNoCMake) {
// Make the generator available for the Build call below.
cm.SetGlobalGenerator(cm.CreateGlobalGenerator(this->BuildGenerator));
cm.SetGeneratorPlatform(this->BuildGeneratorPlatform);
cm.SetGeneratorToolset(this->BuildGeneratorToolset);
// Load the cache to make CMAKE_MAKE_PROGRAM available.
cm.LoadCache(this->BinaryDir);
} else {
// do the cmake step, no timeout here since it is not a sub process
if (this->RunCMake(outstring, out, cmakeOutString, cwd, &cm)) {
return 1;
}
}
// do the build
std::vector<std::string>::iterator tarIt;
if (this->BuildTargets.empty()) {
2006-03-10 23:03:09 +03:00
this->BuildTargets.push_back("");
}
for (tarIt = this->BuildTargets.begin(); tarIt != this->BuildTargets.end();
++tarIt) {
double remainingTime = 0;
if (this->Timeout > 0) {
remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
if (remainingTime <= 0) {
if (outstring) {
*outstring = "--build-and-test timeout exceeded. ";
}
return 1;
}
}
std::string output;
2016-06-27 23:44:16 +03:00
const char* config = CM_NULLPTR;
if (!this->CTest->GetConfigType().empty()) {
config = this->CTest->GetConfigType().c_str();
}
#ifdef CMAKE_INTDIR
if (!config) {
config = CMAKE_INTDIR;
}
#endif
if (!config) {
config = "Debug";
}
2006-03-23 23:35:03 +03:00
int retVal = cm.GetGlobalGenerator()->Build(
this->SourceDir, this->BinaryDir, this->BuildProject, *tarIt, output,
this->BuildMakeProgram, config, !this->BuildNoClean, false, false,
remainingTime);
out << output;
// if the build failed then return
if (retVal) {
if (outstring) {
*outstring = out.str();
}
return 1;
}
}
if (outstring) {
*outstring = out.str();
}
2006-03-09 19:17:10 +03:00
2007-01-25 19:16:16 +03:00
// if no test was specified then we are done
if (this->TestCommand.empty()) {
return 0;
}
2006-03-09 19:17:10 +03: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;
2006-03-10 23:03:09 +03:00
// if this->ExecutableDirectory is set try that as well
if (!this->ExecutableDirectory.empty()) {
std::string tempPath = this->ExecutableDirectory;
tempPath += "/";
2006-03-10 23:03:09 +03:00
tempPath += this->TestCommand;
extraPaths.push_back(tempPath);
}
std::vector<std::string> failed;
fullPath =
cmCTestTestHandler::FindExecutable(this->CTest, this->TestCommand.c_str(),
resultingConfig, extraPaths, failed);
if (!cmSystemTools::FileExists(fullPath.c_str())) {
2006-03-10 23:03:09 +03:00
out << "Could not find path to executable, perhaps it was not built: "
<< this->TestCommand << "\n";
out << "tried to find it in these places:\n";
out << fullPath << "\n";
for (unsigned int i = 0; i < failed.size(); ++i) {
out << failed[i] << "\n";
}
if (outstring) {
*outstring = out.str();
} else {
2006-03-10 23:03:09 +03:00
cmCTestLog(this->CTest, ERROR_MESSAGE, out.str());
}
// return to the original directory
cmSystemTools::ChangeDirectory(cwd);
return 1;
}
std::vector<const char*> testCommand;
testCommand.push_back(fullPath.c_str());
for (size_t k = 0; k < this->TestCommandArgs.size(); ++k) {
2006-03-10 23:03:09 +03:00
testCommand.push_back(this->TestCommandArgs[k].c_str());
}
2016-06-27 23:44:16 +03:00
testCommand.push_back(CM_NULLPTR);
std::string outs;
int retval = 0;
2006-03-10 23:03:09 +03:00
// run the test from the this->BuildRunDir if set
if (!this->BuildRunDir.empty()) {
2006-03-10 23:03:09 +03:00
out << "Run test in directory: " << this->BuildRunDir << "\n";
cmSystemTools::ChangeDirectory(this->BuildRunDir);
}
out << "Running test command: \"" << fullPath << "\"";
for (size_t k = 0; k < this->TestCommandArgs.size(); ++k) {
out << " \"" << this->TestCommandArgs[k] << "\"";
}
out << "\n";
// how much time is remaining
double remainingTime = 0;
if (this->Timeout > 0) {
remainingTime = this->Timeout - cmSystemTools::GetTime() + clock_start;
if (remainingTime <= 0) {
if (outstring) {
*outstring = "--build-and-test timeout exceeded. ";
}
return 1;
}
}
2016-06-27 23:44:16 +03:00
int runTestRes = this->CTest->RunTest(testCommand, &outs, &retval,
CM_NULLPTR, remainingTime, CM_NULLPTR);
if (runTestRes != cmsysProcess_State_Exited || retval != 0) {
out << "Test command failed: " << testCommand[0] << "\n";
retval = 1;
}
out << outs << "\n";
if (outstring) {
*outstring = out.str();
} else {
2006-03-10 23:03:09 +03:00
cmCTestLog(this->CTest, OUTPUT, out.str() << std::endl);
}
return retval;
}
int cmCTestBuildAndTestHandler::ProcessCommandLineArguments(
const std::string& currentArg, size_t& idx,
const std::vector<std::string>& allArgs)
{
// --build-and-test options
if (currentArg.find("--build-and-test", 0) == 0 &&
idx < allArgs.size() - 1) {
if (idx + 2 < allArgs.size()) {
idx++;
2006-03-10 23:03:09 +03:00
this->SourceDir = allArgs[idx];
idx++;
2006-03-10 23:03:09 +03:00
this->BinaryDir = allArgs[idx];
// dir must exist before CollapseFullPath is called
2006-03-10 23:03:09 +03:00
cmSystemTools::MakeDirectory(this->BinaryDir.c_str());
this->BinaryDir = cmSystemTools::CollapseFullPath(this->BinaryDir);
this->SourceDir = cmSystemTools::CollapseFullPath(this->SourceDir);
} else {
2006-03-10 23:03:09 +03:00
cmCTestLog(this->CTest, ERROR_MESSAGE,
"--build-and-test must have source and binary dir"
<< std::endl);
return 0;
}
}
if (currentArg.find("--build-target", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
2006-03-10 23:03:09 +03:00
this->BuildTargets.push_back(allArgs[idx]);
}
if (currentArg.find("--build-nocmake", 0) == 0) {
2006-03-10 23:03:09 +03:00
this->BuildNoCMake = true;
}
if (currentArg.find("--build-run-dir", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
2006-03-10 23:03:09 +03:00
this->BuildRunDir = allArgs[idx];
}
if (currentArg.find("--build-two-config", 0) == 0) {
2006-03-10 23:03:09 +03:00
this->BuildTwoConfig = true;
}
if (currentArg.find("--build-exe-dir", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
2006-03-10 23:03:09 +03:00
this->ExecutableDirectory = allArgs[idx];
}
if (currentArg.find("--test-timeout", 0) == 0 && idx < allArgs.size() - 1) {
2007-01-25 19:16:16 +03:00
idx++;
this->Timeout = atof(allArgs[idx].c_str());
}
if (currentArg == "--build-generator" && idx < allArgs.size() - 1) {
idx++;
2006-03-10 23:03:09 +03:00
this->BuildGenerator = allArgs[idx];
}
if (currentArg == "--build-generator-platform" && idx < allArgs.size() - 1) {
idx++;
this->BuildGeneratorPlatform = allArgs[idx];
}
if (currentArg == "--build-generator-toolset" && idx < allArgs.size() - 1) {
idx++;
this->BuildGeneratorToolset = allArgs[idx];
}
if (currentArg.find("--build-project", 0) == 0 && idx < allArgs.size() - 1) {
idx++;
2006-03-10 23:03:09 +03:00
this->BuildProject = allArgs[idx];
}
if (currentArg.find("--build-makeprogram", 0) == 0 &&
idx < allArgs.size() - 1) {
idx++;
2006-03-10 23:03:09 +03:00
this->BuildMakeProgram = allArgs[idx];
}
if (currentArg.find("--build-config-sample", 0) == 0 &&
idx < allArgs.size() - 1) {
idx++;
this->ConfigSample = allArgs[idx];
}
if (currentArg.find("--build-noclean", 0) == 0) {
2006-03-10 23:03:09 +03:00
this->BuildNoClean = true;
}
if (currentArg.find("--build-options", 0) == 0) {
while (idx + 1 < allArgs.size() && allArgs[idx + 1] != "--build-target" &&
allArgs[idx + 1] != "--test-command") {
++idx;
2006-03-10 23:03:09 +03:00
this->BuildOptions.push_back(allArgs[idx]);
}
}
if (currentArg.find("--test-command", 0) == 0 && idx < allArgs.size() - 1) {
++idx;
2006-03-10 23:03:09 +03:00
this->TestCommand = allArgs[idx];
while (idx + 1 < allArgs.size()) {
++idx;
2006-03-10 23:03:09 +03:00
this->TestCommandArgs.push_back(allArgs[idx]);
}
}
return 1;
}