2002-03-21 00:19:00 +03:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: Insight Segmentation & Registration Toolkit
|
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
|
|
|
Copyright (c) 2002 Insight Consortium. All rights reserved.
|
|
|
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE. See the above copyright notices for more information.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
#include "cmCreateTestSourceList.h"
|
|
|
|
|
|
|
|
|
|
|
|
// cmCreateTestSourceList
|
|
|
|
bool cmCreateTestSourceList::InitialPass(std::vector<std::string> const& argsIn)
|
|
|
|
{
|
2002-03-26 20:37:25 +03:00
|
|
|
if (argsIn.size() < 3)
|
2002-03-21 00:19:00 +03:00
|
|
|
{
|
2002-03-27 01:53:07 +03:00
|
|
|
this->SetError("called with wrong number of arguments.");
|
|
|
|
return false;
|
2002-03-21 00:19:00 +03:00
|
|
|
}
|
2002-03-27 00:42:43 +03:00
|
|
|
|
2002-03-21 00:19:00 +03:00
|
|
|
std::vector<std::string> args;
|
2002-03-29 22:20:32 +03:00
|
|
|
cmSystemTools::ExpandListArguments(argsIn, args);
|
2002-03-21 00:19:00 +03:00
|
|
|
|
|
|
|
std::vector<std::string>::iterator i = args.begin();
|
2002-04-05 01:53:37 +04:00
|
|
|
std::string extraInclude;
|
|
|
|
std::string function;
|
|
|
|
std::vector<std::string> tests;
|
|
|
|
// extract extra include and function ot
|
|
|
|
for(; i != args.end(); i++)
|
|
|
|
{
|
|
|
|
if(*i == "EXTRA_INCLUDE")
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if(i == args.end())
|
|
|
|
{
|
|
|
|
this->SetError("incorrect arguments to EXTRA_INCLUDE");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
extraInclude = *i;
|
|
|
|
}
|
|
|
|
else if(*i == "FUNCTION")
|
|
|
|
{
|
|
|
|
++i;
|
|
|
|
if(i == args.end())
|
|
|
|
{
|
|
|
|
this->SetError("incorrect arguments to FUNCTION");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
function = *i;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tests.push_back(*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i = tests.begin();
|
|
|
|
|
2002-03-27 00:42:43 +03:00
|
|
|
// Name of the source list
|
|
|
|
|
2002-03-21 00:19:00 +03:00
|
|
|
const char* sourceList = i->c_str();
|
|
|
|
++i;
|
2002-03-27 00:42:43 +03:00
|
|
|
|
|
|
|
// Name of the test driver
|
2002-07-31 00:19:14 +04:00
|
|
|
// make sure they specified an extension
|
2002-07-31 19:07:09 +04:00
|
|
|
if (cmSystemTools::GetFilenameExtension(*i).size() < 2)
|
2002-07-31 00:19:14 +04:00
|
|
|
{
|
|
|
|
this->SetError("You must specify a file extenion for the test driver file.");
|
|
|
|
return false;
|
|
|
|
}
|
2002-03-21 00:19:00 +03:00
|
|
|
std::string driver = m_Makefile->GetCurrentOutputDirectory();
|
|
|
|
driver += "/";
|
|
|
|
driver += *i;
|
|
|
|
++i;
|
2002-03-27 00:42:43 +03:00
|
|
|
|
2002-03-21 00:19:00 +03:00
|
|
|
std::ofstream fout(driver.c_str());
|
2002-03-27 01:53:07 +03:00
|
|
|
if (!fout)
|
2002-03-21 00:19:00 +03:00
|
|
|
{
|
|
|
|
std::string err = "Could not create file ";
|
|
|
|
err += driver;
|
|
|
|
err += " for cmCreateTestSourceList command.";
|
|
|
|
this->SetError(err.c_str());
|
|
|
|
return false;
|
|
|
|
}
|
2002-03-27 00:42:43 +03:00
|
|
|
|
2002-03-21 00:19:00 +03:00
|
|
|
// Create the test driver file
|
2002-03-27 00:42:43 +03:00
|
|
|
|
2002-03-27 01:53:07 +03:00
|
|
|
fout <<
|
2002-03-29 16:42:48 +03:00
|
|
|
"#include <ctype.h>\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
"#include <stdio.h>\n"
|
2002-07-30 18:33:02 +04:00
|
|
|
"#include <string.h>\n"
|
2002-07-31 17:29:58 +04:00
|
|
|
"#include <stdlib.h>\n";
|
2002-04-05 01:53:37 +04:00
|
|
|
if(extraInclude.size())
|
|
|
|
{
|
|
|
|
fout << "#include \"" << extraInclude << "\"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
fout <<
|
2002-03-27 01:53:07 +03:00
|
|
|
"\n"
|
|
|
|
"// Forward declare test functions\n"
|
|
|
|
"\n";
|
2002-03-27 00:42:43 +03:00
|
|
|
|
|
|
|
std::vector<std::string>::iterator testsBegin = i;
|
2002-03-27 02:06:36 +03:00
|
|
|
std::vector<std::string> tests_func_name;
|
2002-03-27 00:42:43 +03:00
|
|
|
|
|
|
|
// The rest of the arguments consist of a list of test source files.
|
2002-03-27 02:06:36 +03:00
|
|
|
// 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.
|
2002-03-27 00:42:43 +03:00
|
|
|
// For the moment:
|
|
|
|
// - replace spaces ' ', ':' and '/' with underscores '_'
|
|
|
|
|
2002-04-05 01:53:37 +04:00
|
|
|
for(i = testsBegin; i != tests.end(); ++i)
|
2002-03-21 00:19:00 +03:00
|
|
|
{
|
2002-04-05 01:53:37 +04:00
|
|
|
if(*i == "EXTRA_INCLUDE")
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2002-07-31 00:19:14 +04:00
|
|
|
std::string func_name;
|
|
|
|
if (cmSystemTools::GetFilenamePath(*i).size() > 0)
|
|
|
|
{
|
|
|
|
func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
|
|
|
|
cmSystemTools::GetFilenameWithoutLastExtension(*i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
|
|
|
|
}
|
2002-03-27 02:06:36 +03:00
|
|
|
cmSystemTools::ConvertToUnixSlashes(func_name);
|
|
|
|
cmSystemTools::ReplaceString(func_name, " ", "_");
|
|
|
|
cmSystemTools::ReplaceString(func_name, "/", "_");
|
|
|
|
cmSystemTools::ReplaceString(func_name, ":", "_");
|
|
|
|
tests_func_name.push_back(func_name);
|
2002-07-31 00:19:14 +04:00
|
|
|
fout << "int " << func_name << "(int, char*[]);\n";
|
2002-03-21 00:19:00 +03:00
|
|
|
}
|
2002-03-27 00:42:43 +03:00
|
|
|
|
2002-03-27 01:53:07 +03:00
|
|
|
fout <<
|
|
|
|
"\n"
|
|
|
|
"// Create map\n"
|
|
|
|
"\n"
|
2002-07-31 00:19:14 +04:00
|
|
|
"typedef int (*MainFuncPointer)(int , char*[]);\n"
|
2002-07-31 19:07:09 +04:00
|
|
|
"typedef struct\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
"{\n"
|
|
|
|
" const char* name;\n"
|
|
|
|
" MainFuncPointer func;\n"
|
2002-07-31 19:07:09 +04:00
|
|
|
"} functionMapEntry;\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
"\n"
|
|
|
|
"functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
|
2002-03-27 00:42:43 +03:00
|
|
|
|
2002-03-21 00:19:00 +03:00
|
|
|
int numTests = 0;
|
2002-03-27 02:06:36 +03:00
|
|
|
std::vector<std::string>::iterator j;
|
2002-04-05 01:53:37 +04:00
|
|
|
for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
|
2002-03-21 00:19:00 +03:00
|
|
|
{
|
2002-07-31 00:19:14 +04:00
|
|
|
std::string func_name;
|
|
|
|
if (cmSystemTools::GetFilenamePath(*i).size() > 0)
|
|
|
|
{
|
|
|
|
func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
|
|
|
|
cmSystemTools::GetFilenameWithoutLastExtension(*i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
|
|
|
|
}
|
2002-03-27 01:53:07 +03:00
|
|
|
fout <<
|
|
|
|
" {\n"
|
2002-07-31 00:19:14 +04:00
|
|
|
" \"" << func_name << "\",\n"
|
2002-03-27 02:06:36 +03:00
|
|
|
" " << *j << "\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" },\n";
|
2002-03-21 00:19:00 +03:00
|
|
|
numTests++;
|
|
|
|
}
|
2002-03-27 00:42:43 +03:00
|
|
|
|
2002-03-27 01:53:07 +03:00
|
|
|
fout <<
|
|
|
|
"};\n"
|
|
|
|
"\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
"// Allocate and create a lowercased copy of string\n"
|
2002-07-30 18:33:02 +04:00
|
|
|
"// (note that it has to be free'd manually)\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
"\n"
|
|
|
|
"char* lowercase(const char *string)\n"
|
|
|
|
"{\n"
|
2002-07-30 18:23:39 +04:00
|
|
|
" char *new_string, *p;\n"
|
2002-07-30 18:33:02 +04:00
|
|
|
" new_string = (char *)malloc(sizeof(char) * (size_t)(strlen(string) + 1));\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
" if (!new_string)\n"
|
|
|
|
" {\n"
|
|
|
|
" return NULL;\n"
|
|
|
|
" }\n"
|
|
|
|
" strcpy(new_string, string);\n"
|
2002-07-30 18:23:39 +04:00
|
|
|
" p = new_string;\n"
|
2002-03-30 00:25:39 +03:00
|
|
|
" while (*p != 0)\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
" {\n"
|
2002-08-07 18:30:38 +04:00
|
|
|
" *p = (char)tolower(*p);\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
" ++p;\n"
|
|
|
|
" }\n"
|
|
|
|
" return new_string;\n"
|
|
|
|
"}\n"
|
|
|
|
"\n"
|
2002-07-31 00:19:14 +04:00
|
|
|
"int main(int ac, char *av[])\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
"{\n"
|
2002-08-07 19:01:06 +04:00
|
|
|
" int i, NumTests, testNum, partial_match;\n"
|
2002-07-30 18:23:39 +04:00
|
|
|
" char *arg, *test_name;\n"
|
|
|
|
" \n"
|
|
|
|
" NumTests = " << numTests << ";\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" \n"
|
2002-04-05 01:53:37 +04:00
|
|
|
" // If no test name was given\n";
|
|
|
|
if(function.size())
|
|
|
|
{
|
|
|
|
fout << " // process command line with user function\n"
|
|
|
|
<< " " << function << "(&ac, &av);\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
fout <<
|
2002-03-27 01:53:07 +03:00
|
|
|
" if (ac < 2)\n"
|
|
|
|
" {\n"
|
|
|
|
" // Ask for a test\n"
|
|
|
|
" printf(\"Available tests:\\n\");\n"
|
|
|
|
" for (i =0; i < NumTests; ++i)\n"
|
|
|
|
" {\n"
|
2002-03-27 23:52:52 +03:00
|
|
|
" printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" }\n"
|
|
|
|
" printf(\"To run a test, enter the test number: \");\n"
|
2002-07-30 18:23:39 +04:00
|
|
|
" testNum = 0;\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" scanf(\"%d\", &testNum);\n"
|
|
|
|
" if (testNum >= NumTests)\n"
|
|
|
|
" {\n"
|
2002-03-27 23:52:52 +03:00
|
|
|
" printf(\"%3d is an invalid test number.\\n\", testNum);\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" return -1;\n"
|
|
|
|
" }\n"
|
|
|
|
" return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
|
|
|
|
" }\n"
|
|
|
|
" \n"
|
|
|
|
" // If partial match is requested\n"
|
2002-07-30 18:23:39 +04:00
|
|
|
" partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" if (partial_match && ac < 3)\n"
|
|
|
|
" {\n"
|
|
|
|
" printf(\"-R needs an additional parameter.\\n\");\n"
|
|
|
|
" return -1;\n"
|
|
|
|
" }\n"
|
|
|
|
" \n"
|
2002-07-30 18:23:39 +04:00
|
|
|
" arg = lowercase(av[1 + partial_match]);\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" for (i =0; i < NumTests; ++i)\n"
|
|
|
|
" {\n"
|
2002-07-30 18:23:39 +04:00
|
|
|
" test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
" if (partial_match && strstr(test_name, arg) != NULL)\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" {\n"
|
2002-08-08 16:51:40 +04:00
|
|
|
" free(arg);\n"
|
2002-08-07 18:30:38 +04:00
|
|
|
" free(test_name);\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
" return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" }\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
" else if (!partial_match && strcmp(test_name, arg) == 0)\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" {\n"
|
2002-08-08 16:51:40 +04:00
|
|
|
" free(arg);\n"
|
2002-08-07 18:30:38 +04:00
|
|
|
" free(test_name);\n"
|
2002-03-28 19:43:53 +03:00
|
|
|
" return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" }\n"
|
2002-07-30 18:33:02 +04:00
|
|
|
" free(test_name);\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" }\n"
|
2002-07-30 18:33:02 +04:00
|
|
|
" free(arg);\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" \n"
|
|
|
|
" // Nothing was run, display the test names\n"
|
|
|
|
" printf(\"Available tests:\\n\");\n"
|
|
|
|
" for (i =0; i < NumTests; ++i)\n"
|
|
|
|
" {\n"
|
2002-03-27 23:52:52 +03:00
|
|
|
" printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
|
2002-03-27 01:53:07 +03:00
|
|
|
" }\n"
|
|
|
|
" printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
|
|
|
|
" \n"
|
|
|
|
" return -1;\n"
|
|
|
|
"}\n";
|
|
|
|
|
2002-03-21 00:19:00 +03:00
|
|
|
fout.close();
|
2002-03-27 00:42:43 +03:00
|
|
|
|
|
|
|
// Create the source list
|
2002-03-21 00:19:00 +03:00
|
|
|
cmSourceFile cfile;
|
2002-06-19 20:52:16 +04:00
|
|
|
std::string sourceListValue;
|
|
|
|
|
2002-08-16 19:20:18 +04:00
|
|
|
cfile.SetProperty("ABSTRACT","0");
|
2002-07-31 00:19:14 +04:00
|
|
|
cfile.SetName(cmSystemTools::GetFilenameWithoutExtension(args[1]).c_str(),
|
2002-03-27 00:42:43 +03:00
|
|
|
m_Makefile->GetCurrentOutputDirectory(),
|
2002-07-31 19:07:09 +04:00
|
|
|
cmSystemTools::GetFilenameExtension(args[1]).c_str()+1,
|
2002-03-27 00:42:43 +03:00
|
|
|
false);
|
2002-06-19 20:52:16 +04:00
|
|
|
m_Makefile->AddSource(cfile);
|
2002-07-31 00:19:14 +04:00
|
|
|
sourceListValue = args[1];
|
2002-06-19 20:52:16 +04:00
|
|
|
|
2002-04-05 01:53:37 +04:00
|
|
|
for(i = testsBegin; i != tests.end(); ++i)
|
2002-03-21 00:19:00 +03:00
|
|
|
{
|
|
|
|
cmSourceFile cfile;
|
2002-08-16 19:20:18 +04:00
|
|
|
cfile.SetProperty("ABSTRACT","0");
|
2002-03-27 00:42:43 +03:00
|
|
|
cfile.SetName(i->c_str(),
|
|
|
|
m_Makefile->GetCurrentDirectory(),
|
2002-03-28 00:19:44 +03:00
|
|
|
m_Makefile->GetSourceExtensions(),
|
|
|
|
m_Makefile->GetHeaderExtensions());
|
2002-06-19 20:52:16 +04:00
|
|
|
m_Makefile->AddSource(cfile);
|
|
|
|
sourceListValue += ";";
|
|
|
|
sourceListValue += *i;
|
2002-03-21 00:19:00 +03:00
|
|
|
}
|
|
|
|
|
2002-06-19 20:52:16 +04:00
|
|
|
m_Makefile->AddDefinition(sourceList, sourceListValue.c_str());
|
2002-03-21 00:19:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|