1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmCreateTestSourceList.cxx,v $
6 Date: $Date: 2002-06-27 19:57:09 $
7 Version: $Revision: 1.15 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmCreateTestSourceList.h"
20 // cmCreateTestSourceList
21 bool cmCreateTestSourceList::InitialPass(std::vector
<std::string
> const& argsIn
)
23 if (argsIn
.size() < 3)
25 this->SetError("called with wrong number of arguments.");
29 std::vector
<std::string
> args
;
30 cmSystemTools::ExpandListArguments(argsIn
, args
);
32 std::vector
<std::string
>::iterator i
= args
.begin();
33 std::string extraInclude
;
35 std::vector
<std::string
> tests
;
36 // extract extra include and function ot
37 for(; i
!= args
.end(); i
++)
39 if(*i
== "EXTRA_INCLUDE")
44 this->SetError("incorrect arguments to EXTRA_INCLUDE");
49 else if(*i
== "FUNCTION")
54 this->SetError("incorrect arguments to FUNCTION");
66 // Name of the source list
68 const char* sourceList
= i
->c_str();
71 // Name of the test driver
73 std::string driver
= m_Makefile
->GetCurrentOutputDirectory();
79 std::ofstream
fout(driver
.c_str());
82 std::string err
= "Could not create file ";
84 err
+= " for cmCreateTestSourceList command.";
85 this->SetError(err
.c_str());
89 // Create the test driver file
92 "#include <ctype.h>\n"
93 "#include <stdio.h>\n"
94 "#include <string.h>\n";
95 if(extraInclude
.size())
97 fout
<< "#include \"" << extraInclude
<< "\"\n";
102 "// Forward declare test functions\n"
105 std::vector
<std::string
>::iterator testsBegin
= i
;
106 std::vector
<std::string
> tests_func_name
;
108 // The rest of the arguments consist of a list of test source files.
109 // Sadly, they can be in directories. Let's find a unique function
110 // name for the corresponding test, and push it to the tests_func_name
113 // - replace spaces ' ', ':' and '/' with underscores '_'
115 for(i
= testsBegin
; i
!= tests
.end(); ++i
)
117 if(*i
== "EXTRA_INCLUDE")
121 std::string func_name
= *i
;
122 cmSystemTools::ConvertToUnixSlashes(func_name
);
123 cmSystemTools::ReplaceString(func_name
, " ", "_");
124 cmSystemTools::ReplaceString(func_name
, "/", "_");
125 cmSystemTools::ReplaceString(func_name
, ":", "_");
126 tests_func_name
.push_back(func_name
);
127 fout
<< "int " << func_name
<< "(int, char**);\n";
134 "typedef int (*MainFuncPointer)(int , char**);\n"
135 "struct functionMapEntry\n"
137 " const char* name;\n"
138 " MainFuncPointer func;\n"
141 "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
144 std::vector
<std::string
>::iterator j
;
145 for(i
= testsBegin
, j
= tests_func_name
.begin(); i
!= tests
.end(); ++i
, ++j
)
149 " \"" << *i
<< "\",\n"
158 "// Allocate and create a lowercased copy of string\n"
160 "char* lowercase(const char *string)\n"
162 " char *new_string = new char[strlen(string) + 1];\n"
163 " if (!new_string)\n"
167 " strcpy(new_string, string);\n"
168 " char *p = new_string;\n"
171 " *p = tolower(*p);\n"
174 " return new_string;\n"
177 "int main(int ac, char** av)\n"
179 " int NumTests = " << numTests
<< ";\n"
182 " // If no test name was given\n";
185 fout
<< " // process command line with user function\n"
186 << " " << function
<< "(&ac, &av);\n";
192 " // If there is only one test, then run it with the arguments\n"
193 " if (NumTests == 1)\n"
195 " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
198 " // Ask for a test\n"
199 " printf(\"Available tests:\\n\");\n"
200 " for (i =0; i < NumTests; ++i)\n"
202 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
204 " printf(\"To run a test, enter the test number: \");\n"
205 " int testNum = 0;\n"
206 " scanf(\"%d\", &testNum);\n"
207 " if (testNum >= NumTests)\n"
209 " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
212 " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
215 " // If partial match is requested\n"
216 " int partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
217 " if (partial_match && ac < 3)\n"
219 " printf(\"-R needs an additional parameter.\\n\");\n"
223 " char *arg = lowercase(av[1 + partial_match]);\n"
224 " for (i =0; i < NumTests; ++i)\n"
226 " char *test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
227 " if (partial_match && strstr(test_name, arg) != NULL)\n"
229 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
231 " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
233 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
235 " delete [] test_name;\n"
239 " // If the test was not found but there is only one test, then\n"
240 " // run it with the arguments\n"
241 " if (NumTests == 1)\n"
243 " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
246 " // Nothing was run, display the test names\n"
247 " printf(\"Available tests:\\n\");\n"
248 " for (i =0; i < NumTests; ++i)\n"
250 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
252 " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
259 // Create the source list
261 std::string sourceListValue
;
263 cfile
.SetIsAnAbstractClass(false);
264 cfile
.SetName(args
[1].c_str(),
265 m_Makefile
->GetCurrentOutputDirectory(),
268 m_Makefile
->AddSource(cfile
);
269 sourceListValue
= args
[1] + ".cxx";
271 for(i
= testsBegin
; i
!= tests
.end(); ++i
)
274 cfile
.SetIsAnAbstractClass(false);
275 cfile
.SetName(i
->c_str(),
276 m_Makefile
->GetCurrentDirectory(),
277 m_Makefile
->GetSourceExtensions(),
278 m_Makefile
->GetHeaderExtensions());
279 m_Makefile
->AddSource(cfile
);
280 sourceListValue
+= ";";
281 sourceListValue
+= *i
;
284 m_Makefile
->AddDefinition(sourceList
, sourceListValue
.c_str());