1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmCreateTestSourceList.cxx,v $
6 Date: $Date: 2002-09-27 20:23:55 $
7 Version: $Revision: 1.25 $
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"
18 #include "cmSourceFile.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 = (char)tolower(*p);\n"
200 " return new_string;\n"
203 "int main(int ac, char *av[])\n"
205 " int i, NumTests, 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 " // Ask for a test\n"
221 " printf(\"Available tests:\\n\");\n"
222 " for (i =0; i < NumTests; ++i)\n"
224 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
226 " printf(\"To run a test, enter the test number: \");\n"
228 " scanf(\"%d\", &testNum);\n"
229 " if (testNum >= NumTests)\n"
231 " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
234 " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
237 " // If partial match is requested\n"
238 " partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
239 " if (partial_match && ac < 3)\n"
241 " printf(\"-R needs an additional parameter.\\n\");\n"
245 " arg = lowercase(av[1 + partial_match]);\n"
246 " for (i =0; i < NumTests; ++i)\n"
248 " test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
249 " if (partial_match && strstr(test_name, arg) != NULL)\n"
252 " free(test_name);\n"
253 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
255 " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
258 " free(test_name);\n"
259 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
261 " free(test_name);\n"
265 " // Nothing was run, display the test names\n"
266 " printf(\"Available tests:\\n\");\n"
267 " for (i =0; i < NumTests; ++i)\n"
269 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
271 " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
278 // Create the source list
280 std::string sourceListValue
;
282 cfile
.SetProperty("ABSTRACT","0");
283 cfile
.SetName(cmSystemTools::GetFilenameWithoutExtension(args
[1]).c_str(),
284 m_Makefile
->GetCurrentOutputDirectory(),
285 cmSystemTools::GetFilenameExtension(args
[1]).c_str()+1,
287 m_Makefile
->AddSource(cfile
);
288 sourceListValue
= args
[1];
290 for(i
= testsBegin
; i
!= tests
.end(); ++i
)
293 cfile
.SetProperty("ABSTRACT","0");
294 cfile
.SetName(i
->c_str(),
295 m_Makefile
->GetCurrentDirectory(),
296 m_Makefile
->GetSourceExtensions(),
297 m_Makefile
->GetHeaderExtensions());
298 m_Makefile
->AddSource(cfile
);
299 sourceListValue
+= ";";
300 sourceListValue
+= *i
;
303 m_Makefile
->AddDefinition(sourceList
, sourceListValue
.c_str());