2016-09-27 19:01:08 +00:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2002-03-20 21:19:00 +00:00
|
|
|
#include "cmCreateTestSourceList.h"
|
2016-04-29 13:40:20 +00:00
|
|
|
|
2016-11-05 20:40:14 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2016-10-25 18:35:04 +00:00
|
|
|
#include "cmMakefile.h"
|
2002-09-27 20:24:10 +00:00
|
|
|
#include "cmSourceFile.h"
|
2016-10-19 19:59:14 +00:00
|
|
|
#include "cmSystemTools.h"
|
2002-03-20 21:19:00 +00:00
|
|
|
|
2016-11-05 20:40:14 +00:00
|
|
|
class cmExecutionStatus;
|
|
|
|
|
2002-03-20 21:19:00 +00:00
|
|
|
// cmCreateTestSourceList
|
2016-05-16 14:34:04 +00:00
|
|
|
bool cmCreateTestSourceList::InitialPass(std::vector<std::string> const& args,
|
|
|
|
cmExecutionStatus&)
|
2002-03-20 21:19:00 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
if (args.size() < 3) {
|
2002-03-26 22:53:07 +00:00
|
|
|
this->SetError("called with wrong number of arguments.");
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2002-12-11 23:13:33 +00:00
|
|
|
std::vector<std::string>::const_iterator i = args.begin();
|
2002-04-04 21:53:37 +00:00
|
|
|
std::string extraInclude;
|
|
|
|
std::string function;
|
|
|
|
std::vector<std::string> tests;
|
|
|
|
// extract extra include and function ot
|
2016-05-16 14:34:04 +00:00
|
|
|
for (; i != args.end(); i++) {
|
|
|
|
if (*i == "EXTRA_INCLUDE") {
|
2002-04-04 21:53:37 +00:00
|
|
|
++i;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (i == args.end()) {
|
2002-04-04 21:53:37 +00:00
|
|
|
this->SetError("incorrect arguments to EXTRA_INCLUDE");
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2004-04-28 16:31:18 +00:00
|
|
|
extraInclude = "#include \"";
|
|
|
|
extraInclude += *i;
|
|
|
|
extraInclude += "\"\n";
|
2016-05-16 14:34:04 +00:00
|
|
|
} else if (*i == "FUNCTION") {
|
2002-04-04 21:53:37 +00:00
|
|
|
++i;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (i == args.end()) {
|
2002-04-04 21:53:37 +00:00
|
|
|
this->SetError("incorrect arguments to FUNCTION");
|
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2002-04-04 21:53:37 +00:00
|
|
|
function = *i;
|
2004-04-28 16:31:18 +00:00
|
|
|
function += "(&ac, &av);\n";
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2002-04-04 21:53:37 +00:00
|
|
|
tests.push_back(*i);
|
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2002-04-04 21:53:37 +00:00
|
|
|
i = tests.begin();
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2002-03-26 21:42:43 +00:00
|
|
|
// Name of the source list
|
|
|
|
|
2002-03-20 21:19:00 +00:00
|
|
|
const char* sourceList = i->c_str();
|
|
|
|
++i;
|
2002-03-26 21:42:43 +00:00
|
|
|
|
|
|
|
// Name of the test driver
|
2002-07-30 20:19:14 +00:00
|
|
|
// make sure they specified an extension
|
2016-05-16 14:34:04 +00:00
|
|
|
if (cmSystemTools::GetFilenameExtension(*i).size() < 2) {
|
2006-03-10 18:06:26 +00:00
|
|
|
this->SetError(
|
2012-02-26 18:44:20 +00:00
|
|
|
"You must specify a file extension for the test driver file.");
|
2002-07-30 20:19:14 +00:00
|
|
|
return false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2015-04-16 19:33:09 +00:00
|
|
|
std::string driver = this->Makefile->GetCurrentBinaryDirectory();
|
2002-03-20 21:19:00 +00:00
|
|
|
driver += "/";
|
|
|
|
driver += *i;
|
|
|
|
++i;
|
2002-03-26 21:42:43 +00:00
|
|
|
|
2016-03-14 14:38:19 +00:00
|
|
|
std::string configFile = cmSystemTools::GetCMakeRoot();
|
2006-03-15 16:02:08 +00:00
|
|
|
|
2004-04-28 16:31:18 +00:00
|
|
|
configFile += "/Templates/TestDriver.cxx.in";
|
2002-03-20 21:19:00 +00:00
|
|
|
// Create the test driver file
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2002-12-11 23:13:33 +00:00
|
|
|
std::vector<std::string>::const_iterator testsBegin = i;
|
2002-03-26 23:06:36 +00:00
|
|
|
std::vector<std::string> tests_func_name;
|
2002-03-26 21:42:43 +00:00
|
|
|
|
|
|
|
// The rest of the arguments consist of a list of test source files.
|
2006-03-10 18:06:26 +00:00
|
|
|
// Sadly, they can be in directories. Let's find a unique function
|
2002-03-26 23:06:36 +00:00
|
|
|
// name for the corresponding test, and push it to the tests_func_name
|
2006-03-10 18:06:26 +00:00
|
|
|
// list.
|
2002-03-26 21:42:43 +00:00
|
|
|
// For the moment:
|
|
|
|
// - replace spaces ' ', ':' and '/' with underscores '_'
|
2004-04-28 16:31:18 +00:00
|
|
|
std::string forwardDeclareCode;
|
2016-05-16 14:34:04 +00:00
|
|
|
for (i = testsBegin; i != tests.end(); ++i) {
|
|
|
|
if (*i == "EXTRA_INCLUDE") {
|
2002-04-04 21:53:37 +00:00
|
|
|
break;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2002-07-30 20:19:14 +00:00
|
|
|
std::string func_name;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!cmSystemTools::GetFilenamePath(*i).empty()) {
|
2006-03-10 18:06:26 +00:00
|
|
|
func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
|
2002-07-30 20:19:14 +00:00
|
|
|
cmSystemTools::GetFilenameWithoutLastExtension(*i);
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2002-07-30 20:19:14 +00:00
|
|
|
func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2002-03-26 23:06:36 +00:00
|
|
|
cmSystemTools::ConvertToUnixSlashes(func_name);
|
Use std::replace for replacing chars in strings.
Find uses of `cmSystemTools::ReplaceString` where both `replace` and
`with` are string literals with a size of one.
Automate with:
git grep -l ReplaceString | xargs sed -i "s|cmSystemTools::ReplaceString(\([^,]*\), \"\(.\)\", \"\(.\)\");|std::replace(\1.begin(), \1.end(), '\2', '\3');|g"
git grep -l ReplaceString | xargs sed -i "s|cmSystemTools::ReplaceString(\([^,]*\), \"\(.\)\", \"\\\\\\\\\");|std::replace(\1.begin(), \1.end(), '\2', '\\\\\\\\');|g"
git grep -l ReplaceString | xargs sed -i "s|cmSystemTools::ReplaceString(\([^,]*\), \"\\\\\\\\\", \"\(.\)\");|std::replace(\1.begin(), \1.end(), '\\\\\\\\', '\2');|g"
2016-05-24 20:58:11 +00: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(), ':', '_');
|
2002-03-26 23:06:36 +00:00
|
|
|
tests_func_name.push_back(func_name);
|
2004-04-28 16:31:18 +00:00
|
|
|
forwardDeclareCode += "int ";
|
|
|
|
forwardDeclareCode += func_name;
|
|
|
|
forwardDeclareCode += "(int, char*[]);\n";
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2006-03-10 18:06:26 +00:00
|
|
|
|
2004-04-28 16:31:18 +00:00
|
|
|
std::string functionMapCode;
|
2002-03-20 21:19:00 +00:00
|
|
|
int numTests = 0;
|
2002-03-26 23:06:36 +00:00
|
|
|
std::vector<std::string>::iterator j;
|
2016-05-16 14:34:04 +00:00
|
|
|
for (i = testsBegin, j = tests_func_name.begin(); i != tests.end();
|
|
|
|
++i, ++j) {
|
2002-07-30 20:19:14 +00:00
|
|
|
std::string func_name;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!cmSystemTools::GetFilenamePath(*i).empty()) {
|
2006-03-10 18:06:26 +00:00
|
|
|
func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
|
2002-07-30 20:19:14 +00:00
|
|
|
cmSystemTools::GetFilenameWithoutLastExtension(*i);
|
2016-05-16 14:34:04 +00:00
|
|
|
} else {
|
2002-07-30 20:19:14 +00:00
|
|
|
func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2004-04-28 16:31:18 +00:00
|
|
|
functionMapCode += " {\n"
|
2016-05-16 14:34:04 +00:00
|
|
|
" \"";
|
2004-04-28 16:31:18 +00:00
|
|
|
functionMapCode += func_name;
|
|
|
|
functionMapCode += "\",\n"
|
2016-05-16 14:34:04 +00:00
|
|
|
" ";
|
|
|
|
functionMapCode += *j;
|
2004-04-28 16:31:18 +00:00
|
|
|
functionMapCode += "\n"
|
2016-05-16 14:34:04 +00:00
|
|
|
" },\n";
|
2002-03-20 21:19:00 +00:00
|
|
|
numTests++;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (!extraInclude.empty()) {
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile->AddDefinition("CMAKE_TESTDRIVER_EXTRA_INCLUDES",
|
|
|
|
extraInclude.c_str());
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
|
|
|
if (!function.empty()) {
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile->AddDefinition("CMAKE_TESTDRIVER_ARGVC_FUNCTION",
|
|
|
|
function.c_str());
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile->AddDefinition("CMAKE_FORWARD_DECLARE_TESTS",
|
2016-05-16 14:34:04 +00:00
|
|
|
forwardDeclareCode.c_str());
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile->AddDefinition("CMAKE_FUNCTION_TABLE_ENTIRES",
|
2016-05-16 14:34:04 +00:00
|
|
|
functionMapCode.c_str());
|
2004-05-27 16:56:52 +00:00
|
|
|
bool res = true;
|
2016-05-16 14:34:04 +00:00
|
|
|
if (!this->Makefile->ConfigureFile(configFile.c_str(), driver.c_str(), false,
|
|
|
|
true, false)) {
|
2004-05-27 16:56:52 +00:00
|
|
|
res = false;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2002-03-26 21:42:43 +00:00
|
|
|
|
2007-06-18 15:59:23 +00:00
|
|
|
// Construct the source list.
|
2002-06-19 16:52:16 +00:00
|
|
|
std::string sourceListValue;
|
2007-06-18 15:59:23 +00:00
|
|
|
{
|
2016-05-16 14:34:04 +00:00
|
|
|
cmSourceFile* sf = this->Makefile->GetOrCreateSource(driver);
|
|
|
|
sf->SetProperty("ABSTRACT", "0");
|
|
|
|
sourceListValue = args[1];
|
2007-06-18 15:59:23 +00:00
|
|
|
}
|
2016-05-16 14:34:04 +00:00
|
|
|
for (i = testsBegin; i != tests.end(); ++i) {
|
2014-03-10 23:04:11 +00:00
|
|
|
cmSourceFile* sf = this->Makefile->GetOrCreateSource(*i);
|
2016-05-16 14:34:04 +00:00
|
|
|
sf->SetProperty("ABSTRACT", "0");
|
2002-06-19 16:52:16 +00:00
|
|
|
sourceListValue += ";";
|
|
|
|
sourceListValue += *i;
|
2016-05-16 14:34:04 +00:00
|
|
|
}
|
2002-03-20 21:19:00 +00:00
|
|
|
|
2006-03-15 16:02:08 +00:00
|
|
|
this->Makefile->AddDefinition(sourceList, sourceListValue.c_str());
|
2004-05-27 16:56:52 +00:00
|
|
|
return res;
|
2002-03-20 21:19:00 +00:00
|
|
|
}
|