FIX: stupid pb fixed (close to being medieval'ed by The Ken)
[cmake.git] / Source / cmCreateTestSourceList.cxx
blob642abbe8d2799f218bc1755a85df01924545ede6
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmCreateTestSourceList.cxx,v $
5 Language: C++
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.");
26 return false;
29 std::vector<std::string> args;
30 cmSystemTools::ExpandListArguments(argsIn, args);
32 std::vector<std::string>::iterator i = args.begin();
33 std::string extraInclude;
34 std::string function;
35 std::vector<std::string> tests;
36 // extract extra include and function ot
37 for(; i != args.end(); i++)
39 if(*i == "EXTRA_INCLUDE")
41 ++i;
42 if(i == args.end())
44 this->SetError("incorrect arguments to EXTRA_INCLUDE");
45 return false;
47 extraInclude = *i;
49 else if(*i == "FUNCTION")
51 ++i;
52 if(i == args.end())
54 this->SetError("incorrect arguments to FUNCTION");
55 return false;
57 function = *i;
59 else
61 tests.push_back(*i);
64 i = tests.begin();
66 // Name of the source list
68 const char* sourceList = i->c_str();
69 ++i;
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.");
76 return false;
78 std::string driver = m_Makefile->GetCurrentOutputDirectory();
79 driver += "/";
80 driver += *i;
81 ++i;
83 std::ofstream fout(driver.c_str());
84 if (!fout)
86 std::string err = "Could not create file ";
87 err += driver;
88 err += " for cmCreateTestSourceList command.";
89 this->SetError(err.c_str());
90 return false;
93 // Create the test driver file
95 fout <<
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";
105 fout <<
106 "\n"
107 "// Forward declare test functions\n"
108 "\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
116 // list.
117 // For the moment:
118 // - replace spaces ' ', ':' and '/' with underscores '_'
120 for(i = testsBegin; i != tests.end(); ++i)
122 if(*i == "EXTRA_INCLUDE")
124 break;
126 std::string func_name;
127 if (cmSystemTools::GetFilenamePath(*i).size() > 0)
129 func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
130 cmSystemTools::GetFilenameWithoutLastExtension(*i);
132 else
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";
144 fout <<
145 "\n"
146 "// Create map\n"
147 "\n"
148 "typedef int (*MainFuncPointer)(int , char*[]);\n"
149 "typedef struct\n"
150 "{\n"
151 " const char* name;\n"
152 " MainFuncPointer func;\n"
153 "} functionMapEntry;\n"
154 "\n"
155 "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
157 int numTests = 0;
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);
167 else
169 func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
171 fout <<
172 " {\n"
173 " \"" << func_name << "\",\n"
174 " " << *j << "\n"
175 " },\n";
176 numTests++;
179 fout <<
180 "};\n"
181 "\n"
182 "// Allocate and create a lowercased copy of string\n"
183 "// (note that it has to be free'd manually)\n"
184 "\n"
185 "char* lowercase(const char *string)\n"
186 "{\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"
190 " {\n"
191 " return NULL;\n"
192 " }\n"
193 " strcpy(new_string, string);\n"
194 " p = new_string;\n"
195 " while (*p != 0)\n"
196 " {\n"
197 " *p = tolower(*p);\n"
198 " ++p;\n"
199 " }\n"
200 " return new_string;\n"
201 "}\n"
202 "\n"
203 "int main(int ac, char *av[])\n"
204 "{\n"
205 " int NumTests, i, testNum, partial_match;\n"
206 " char *arg, *test_name;\n"
207 " \n"
208 " NumTests = " << numTests << ";\n"
209 " \n"
210 " // If no test name was given\n";
211 if(function.size())
213 fout << " // process command line with user function\n"
214 << " " << function << "(&ac, &av);\n";
217 fout <<
218 " if (ac < 2)\n"
219 " {\n"
220 " // If there is only one test, then run it with the arguments\n"
221 " if (NumTests == 1)\n"
222 " {\n"
223 " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
224 " }\n"
225 " \n"
226 " // Ask for a test\n"
227 " printf(\"Available tests:\\n\");\n"
228 " for (i =0; i < NumTests; ++i)\n"
229 " {\n"
230 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
231 " }\n"
232 " printf(\"To run a test, enter the test number: \");\n"
233 " testNum = 0;\n"
234 " scanf(\"%d\", &testNum);\n"
235 " if (testNum >= NumTests)\n"
236 " {\n"
237 " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
238 " return -1;\n"
239 " }\n"
240 " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
241 " }\n"
242 " \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"
246 " {\n"
247 " printf(\"-R needs an additional parameter.\\n\");\n"
248 " return -1;\n"
249 " }\n"
250 " \n"
251 " arg = lowercase(av[1 + partial_match]);\n"
252 " for (i =0; i < NumTests; ++i)\n"
253 " {\n"
254 " test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
255 " if (partial_match && strstr(test_name, arg) != NULL)\n"
256 " {\n"
257 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
258 " }\n"
259 " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
260 " {\n"
261 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
262 " }\n"
263 " free(test_name);\n"
264 " }\n"
265 " free(arg);\n"
266 " \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"
270 " {\n"
271 " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
272 " }\n"
273 " \n"
274 " // Nothing was run, display the test names\n"
275 " printf(\"Available tests:\\n\");\n"
276 " for (i =0; i < NumTests; ++i)\n"
277 " {\n"
278 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
279 " }\n"
280 " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
281 " \n"
282 " return -1;\n"
283 "}\n";
285 fout.close();
287 // Create the source list
288 cmSourceFile cfile;
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,
295 false);
296 m_Makefile->AddSource(cfile);
297 sourceListValue = args[1];
299 for(i = testsBegin; i != tests.end(); ++i)
301 cmSourceFile cfile;
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());
313 return true;