cmake/Source/cmCreateTestSourceList.cxx

160 lines
5.0 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCreateTestSourceList.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include <algorithm>
#include "cmMakefile.h"
#include "cmSourceFile.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
class cmExecutionStatus;
// cmCreateTestSourceList
2016-07-09 11:21:54 +02:00
bool cmCreateTestSourceList::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
2016-07-09 11:21:54 +02:00
if (args.size() < 3) {
this->SetError("called with wrong number of arguments.");
return false;
2016-07-09 11:21:54 +02:00
}
std::vector<std::string>::const_iterator i = args.begin();
std::string extraInclude;
std::string function;
std::vector<std::string> tests;
// extract extra include and function ot
2016-07-09 11:21:54 +02:00
for (; i != args.end(); i++) {
if (*i == "EXTRA_INCLUDE") {
++i;
2016-07-09 11:21:54 +02:00
if (i == args.end()) {
this->SetError("incorrect arguments to EXTRA_INCLUDE");
return false;
2016-07-09 11:21:54 +02:00
}
extraInclude = "#include \"";
extraInclude += *i;
extraInclude += "\"\n";
2016-07-09 11:21:54 +02:00
} else if (*i == "FUNCTION") {
++i;
2016-07-09 11:21:54 +02:00
if (i == args.end()) {
this->SetError("incorrect arguments to FUNCTION");
return false;
2016-07-09 11:21:54 +02:00
}
function = *i;
function += "(&ac, &av);\n";
2016-07-09 11:21:54 +02:00
} else {
tests.push_back(*i);
}
2016-07-09 11:21:54 +02:00
}
i = tests.begin();
// Name of the source list
const char* sourceList = i->c_str();
++i;
// Name of the test driver
// make sure they specified an extension
2016-07-09 11:21:54 +02:00
if (cmSystemTools::GetFilenameExtension(*i).size() < 2) {
this->SetError(
2012-04-19 19:04:21 +03:00
"You must specify a file extension for the test driver file.");
return false;
2016-07-09 11:21:54 +02:00
}
2015-08-17 11:37:30 +02:00
std::string driver = this->Makefile->GetCurrentBinaryDirectory();
driver += "/";
driver += *i;
++i;
2016-07-09 11:21:54 +02:00
std::string configFile = cmSystemTools::GetCMakeRoot();
configFile += "/Templates/TestDriver.cxx.in";
// Create the test driver file
std::vector<std::string>::const_iterator testsBegin = i;
std::vector<std::string> tests_func_name;
// The rest of the arguments consist of a list of test source files.
// Sadly, they can be in directories. Let's find a unique function
// name for the corresponding test, and push it to the tests_func_name
// list.
// For the moment:
// - replace spaces ' ', ':' and '/' with underscores '_'
std::string forwardDeclareCode;
2016-07-09 11:21:54 +02:00
for (i = testsBegin; i != tests.end(); ++i) {
if (*i == "EXTRA_INCLUDE") {
break;
2016-07-09 11:21:54 +02:00
}
std::string func_name;
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::GetFilenamePath(*i).empty()) {
func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
cmSystemTools::GetFilenameWithoutLastExtension(*i);
2016-07-09 11:21:54 +02:00
} else {
func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
2016-07-09 11:21:54 +02:00
}
cmSystemTools::ConvertToUnixSlashes(func_name);
2016-07-09 11:21:54 +02:00
std::replace(func_name.begin(), func_name.end(), ' ', '_');
std::replace(func_name.begin(), func_name.end(), '/', '_');
std::replace(func_name.begin(), func_name.end(), ':', '_');
tests_func_name.push_back(func_name);
forwardDeclareCode += "int ";
forwardDeclareCode += func_name;
forwardDeclareCode += "(int, char*[]);\n";
2016-07-09 11:21:54 +02:00
}
std::string functionMapCode;
int numTests = 0;
std::vector<std::string>::iterator j;
2016-07-09 11:21:54 +02:00
for (i = testsBegin, j = tests_func_name.begin(); i != tests.end();
++i, ++j) {
std::string func_name;
2016-07-09 11:21:54 +02:00
if (!cmSystemTools::GetFilenamePath(*i).empty()) {
func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
cmSystemTools::GetFilenameWithoutLastExtension(*i);
2016-07-09 11:21:54 +02:00
} else {
func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
2016-07-09 11:21:54 +02:00
}
functionMapCode += " {\n"
2016-07-09 11:21:54 +02:00
" \"";
functionMapCode += func_name;
functionMapCode += "\",\n"
2016-07-09 11:21:54 +02:00
" ";
functionMapCode += *j;
functionMapCode += "\n"
2016-07-09 11:21:54 +02:00
" },\n";
numTests++;
2016-07-09 11:21:54 +02:00
}
if (!extraInclude.empty()) {
this->Makefile->AddDefinition("CMAKE_TESTDRIVER_EXTRA_INCLUDES",
extraInclude.c_str());
2016-07-09 11:21:54 +02:00
}
if (!function.empty()) {
this->Makefile->AddDefinition("CMAKE_TESTDRIVER_ARGVC_FUNCTION",
function.c_str());
2016-07-09 11:21:54 +02:00
}
this->Makefile->AddDefinition("CMAKE_FORWARD_DECLARE_TESTS",
2016-07-09 11:21:54 +02:00
forwardDeclareCode.c_str());
this->Makefile->AddDefinition("CMAKE_FUNCTION_TABLE_ENTIRES",
2016-07-09 11:21:54 +02:00
functionMapCode.c_str());
bool res = true;
2019-11-11 23:01:05 +01:00
if (!this->Makefile->ConfigureFile(configFile, driver, false, true, false)) {
res = false;
2016-07-09 11:21:54 +02:00
}
// Construct the source list.
std::string sourceListValue;
{
2016-07-09 11:21:54 +02:00
cmSourceFile* sf = this->Makefile->GetOrCreateSource(driver);
sf->SetProperty("ABSTRACT", "0");
sourceListValue = args[1];
}
2016-07-09 11:21:54 +02:00
for (i = testsBegin; i != tests.end(); ++i) {
2015-04-27 22:25:09 +02:00
cmSourceFile* sf = this->Makefile->GetOrCreateSource(*i);
2016-07-09 11:21:54 +02:00
sf->SetProperty("ABSTRACT", "0");
sourceListValue += ";";
sourceListValue += *i;
2016-07-09 11:21:54 +02:00
}
this->Makefile->AddDefinition(sourceList, sourceListValue.c_str());
return res;
}