cmake/Source/cmCreateTestSourceList.cxx

156 lines
4.8 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>
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmSourceFile.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2020-02-01 23:06:01 +01:00
bool cmCreateTestSourceList(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-07-09 11:21:54 +02:00
if (args.size() < 3) {
2020-02-01 23:06:01 +01:00
status.SetError("called with wrong number of arguments.");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
auto 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()) {
2020-02-01 23:06:01 +01:00
status.SetError("incorrect arguments to EXTRA_INCLUDE");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
extraInclude = cmStrCat("#include \"", *i, "\"\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()) {
2020-02-01 23:06:01 +01:00
status.SetError("incorrect arguments to FUNCTION");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
function = cmStrCat(*i, "(&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) {
2020-02-01 23:06:01 +01:00
status.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
}
2020-02-01 23:06:01 +01:00
cmMakefile& mf = status.GetMakefile();
std::string driver = cmStrCat(mf.GetCurrentBinaryDirectory(), '/', *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
2020-02-01 23:06:01 +01:00
auto 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(), ':', '_');
2021-09-14 00:13:48 +02:00
bool already_declared =
std::find(tests_func_name.begin(), tests_func_name.end(), func_name) !=
tests_func_name.end();
tests_func_name.push_back(func_name);
2021-09-14 00:13:48 +02:00
if (!already_declared) {
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()) {
2020-02-01 23:06:01 +01:00
mf.AddDefinition("CMAKE_TESTDRIVER_EXTRA_INCLUDES", extraInclude);
2016-07-09 11:21:54 +02:00
}
if (!function.empty()) {
2020-02-01 23:06:01 +01:00
mf.AddDefinition("CMAKE_TESTDRIVER_ARGVC_FUNCTION", function);
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
mf.AddDefinition("CMAKE_FORWARD_DECLARE_TESTS", forwardDeclareCode);
2021-09-14 00:13:48 +02:00
mf.AddDefinition("CMAKE_FUNCTION_TABLE_ENTRIES", functionMapCode);
bool res = true;
2020-02-01 23:06:01 +01:00
if (!mf.ConfigureFile(configFile, driver, false, true, false)) {
res = false;
2016-07-09 11:21:54 +02:00
}
// Construct the source list.
std::string sourceListValue;
{
2020-02-01 23:06:01 +01:00
cmSourceFile* sf = mf.GetOrCreateSource(driver);
2016-07-09 11:21:54 +02:00
sf->SetProperty("ABSTRACT", "0");
sourceListValue = args[1];
}
2016-07-09 11:21:54 +02:00
for (i = testsBegin; i != tests.end(); ++i) {
2020-02-01 23:06:01 +01:00
cmSourceFile* sf = mf.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
}
2020-02-01 23:06:01 +01:00
mf.AddDefinition(sourceList, sourceListValue);
return res;
}