1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmCreateTestSourceList.cxx,v $
6 Date: $Date: 2002-07-31 15:07:09 $
7 Version: $Revision: 1.20 $
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
72 // make sure they specified an extension
73 if (cmSystemTools::GetFilenameExtension(*i
).size() < 2)
75 this->SetError("You must specify a file extenion for the test driver file.");
78 std::string driver
= m_Makefile
->GetCurrentOutputDirectory();
83 std::ofstream
fout(driver
.c_str());
86 std::string err
= "Could not create file ";
88 err
+= " for cmCreateTestSourceList command.";
89 this->SetError(err
.c_str());
93 // Create the test driver file
96 "#include <ctype.h>\n"
97 "#include <stdio.h>\n"
98 "#include <string.h>\n"
99 "#include <stdlib.h>\n";
100 if(extraInclude
.size())
102 fout
<< "#include \"" << extraInclude
<< "\"\n";
107 "// Forward declare test functions\n"
110 std::vector
<std::string
>::iterator testsBegin
= i
;
111 std::vector
<std::string
> tests_func_name
;
113 // The rest of the arguments consist of a list of test source files.
114 // Sadly, they can be in directories. Let's find a unique function
115 // name for the corresponding test, and push it to the tests_func_name
118 // - replace spaces ' ', ':' and '/' with underscores '_'
120 for(i
= testsBegin
; i
!= tests
.end(); ++i
)
122 if(*i
== "EXTRA_INCLUDE")
126 std::string func_name
;
127 if (cmSystemTools::GetFilenamePath(*i
).size() > 0)
129 func_name
= cmSystemTools::GetFilenamePath(*i
) + "/" +
130 cmSystemTools::GetFilenameWithoutLastExtension(*i
);
134 func_name
= cmSystemTools::GetFilenameWithoutLastExtension(*i
);
136 cmSystemTools::ConvertToUnixSlashes(func_name
);
137 cmSystemTools::ReplaceString(func_name
, " ", "_");
138 cmSystemTools::ReplaceString(func_name
, "/", "_");
139 cmSystemTools::ReplaceString(func_name
, ":", "_");
140 tests_func_name
.push_back(func_name
);
141 fout
<< "int " << func_name
<< "(int, char*[]);\n";
148 "typedef int (*MainFuncPointer)(int , char*[]);\n"
151 " const char* name;\n"
152 " MainFuncPointer func;\n"
153 "} functionMapEntry;\n"
155 "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
158 std::vector
<std::string
>::iterator j
;
159 for(i
= testsBegin
, j
= tests_func_name
.begin(); i
!= tests
.end(); ++i
, ++j
)
161 std::string func_name
;
162 if (cmSystemTools::GetFilenamePath(*i
).size() > 0)
164 func_name
= cmSystemTools::GetFilenamePath(*i
) + "/" +
165 cmSystemTools::GetFilenameWithoutLastExtension(*i
);
169 func_name
= cmSystemTools::GetFilenameWithoutLastExtension(*i
);
173 " \"" << func_name
<< "\",\n"
182 "// Allocate and create a lowercased copy of string\n"
183 "// (note that it has to be free'd manually)\n"
185 "char* lowercase(const char *string)\n"
187 " char *new_string, *p;\n"
188 " new_string = (char *)malloc(sizeof(char) * (size_t)(strlen(string) + 1));\n"
189 " if (!new_string)\n"
193 " strcpy(new_string, string);\n"
197 " *p = tolower(*p);\n"
200 " return new_string;\n"
203 "int main(int ac, char *av[])\n"
205 " int NumTests, i, testNum, partial_match;\n"
206 " char *arg, *test_name;\n"
208 " NumTests = " << numTests
<< ";\n"
210 " // If no test name was given\n";
213 fout
<< " // process command line with user function\n"
214 << " " << function
<< "(&ac, &av);\n";
220 " // If there is only one test, then run it with the arguments\n"
221 " if (NumTests == 1)\n"
223 " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
226 " // Ask for a test\n"
227 " printf(\"Available tests:\\n\");\n"
228 " for (i =0; i < NumTests; ++i)\n"
230 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
232 " printf(\"To run a test, enter the test number: \");\n"
234 " scanf(\"%d\", &testNum);\n"
235 " if (testNum >= NumTests)\n"
237 " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
240 " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
243 " // If partial match is requested\n"
244 " partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
245 " if (partial_match && ac < 3)\n"
247 " printf(\"-R needs an additional parameter.\\n\");\n"
251 " arg = lowercase(av[1 + partial_match]);\n"
252 " for (i =0; i < NumTests; ++i)\n"
254 " test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
255 " if (partial_match && strstr(test_name, arg) != NULL)\n"
257 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
259 " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
261 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
263 " free(test_name);\n"
267 " // If the test was not found but there is only one test, then\n"
268 " // run it with the arguments\n"
269 " if (NumTests == 1)\n"
271 " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
274 " // Nothing was run, display the test names\n"
275 " printf(\"Available tests:\\n\");\n"
276 " for (i =0; i < NumTests; ++i)\n"
278 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
280 " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
287 // Create the source list
289 std::string sourceListValue
;
291 cfile
.SetIsAnAbstractClass(false);
292 cfile
.SetName(cmSystemTools::GetFilenameWithoutExtension(args
[1]).c_str(),
293 m_Makefile
->GetCurrentOutputDirectory(),
294 cmSystemTools::GetFilenameExtension(args
[1]).c_str()+1,
296 m_Makefile
->AddSource(cfile
);
297 sourceListValue
= args
[1];
299 for(i
= testsBegin
; i
!= tests
.end(); ++i
)
302 cfile
.SetIsAnAbstractClass(false);
303 cfile
.SetName(i
->c_str(),
304 m_Makefile
->GetCurrentDirectory(),
305 m_Makefile
->GetSourceExtensions(),
306 m_Makefile
->GetHeaderExtensions());
307 m_Makefile
->AddSource(cfile
);
308 sourceListValue
+= ";";
309 sourceListValue
+= *i
;
312 m_Makefile
->AddDefinition(sourceList
, sourceListValue
.c_str());