ENH: change the search path order (if several Tcl/Tk are installed, the "current...
[cmake.git] / Source / cmCreateTestSourceList.cxx
blob0e427812fb12d53fe631e19e8b0ab864d3e5d7ab
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmCreateTestSourceList.cxx,v $
5 Language: C++
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.");
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
73 std::string driver = m_Makefile->GetCurrentOutputDirectory();
74 driver += "/";
75 driver += *i;
76 driver += ".cxx";
77 ++i;
79 std::ofstream fout(driver.c_str());
80 if (!fout)
82 std::string err = "Could not create file ";
83 err += driver;
84 err += " for cmCreateTestSourceList command.";
85 this->SetError(err.c_str());
86 return false;
89 // Create the test driver file
91 fout <<
92 "#include <ctype.h>\n"
93 "#include <stdio.h>\n"
94 "#include <string.h>\n";
95 if(extraInclude.size())
97 fout << "#include \"" << extraInclude << "\"\n";
100 fout <<
101 "\n"
102 "// Forward declare test functions\n"
103 "\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
111 // list.
112 // For the moment:
113 // - replace spaces ' ', ':' and '/' with underscores '_'
115 for(i = testsBegin; i != tests.end(); ++i)
117 if(*i == "EXTRA_INCLUDE")
119 break;
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";
130 fout <<
131 "\n"
132 "// Create map\n"
133 "\n"
134 "typedef int (*MainFuncPointer)(int , char**);\n"
135 "struct functionMapEntry\n"
136 "{\n"
137 " const char* name;\n"
138 " MainFuncPointer func;\n"
139 "};\n"
140 "\n"
141 "functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
143 int numTests = 0;
144 std::vector<std::string>::iterator j;
145 for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
147 fout <<
148 " {\n"
149 " \"" << *i << "\",\n"
150 " " << *j << "\n"
151 " },\n";
152 numTests++;
155 fout <<
156 "};\n"
157 "\n"
158 "// Allocate and create a lowercased copy of string\n"
159 "\n"
160 "char* lowercase(const char *string)\n"
161 "{\n"
162 " char *new_string = new char[strlen(string) + 1];\n"
163 " if (!new_string)\n"
164 " {\n"
165 " return NULL;\n"
166 " }\n"
167 " strcpy(new_string, string);\n"
168 " char *p = new_string;\n"
169 " while (*p != 0)\n"
170 " {\n"
171 " *p = tolower(*p);\n"
172 " ++p;\n"
173 " }\n"
174 " return new_string;\n"
175 "}\n"
176 "\n"
177 "int main(int ac, char** av)\n"
178 "{\n"
179 " int NumTests = " << numTests << ";\n"
180 " int i;\n"
181 " \n"
182 " // If no test name was given\n";
183 if(function.size())
185 fout << " // process command line with user function\n"
186 << " " << function << "(&ac, &av);\n";
189 fout <<
190 " if (ac < 2)\n"
191 " {\n"
192 " // If there is only one test, then run it with the arguments\n"
193 " if (NumTests == 1)\n"
194 " {\n"
195 " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
196 " }\n"
197 " \n"
198 " // Ask for a test\n"
199 " printf(\"Available tests:\\n\");\n"
200 " for (i =0; i < NumTests; ++i)\n"
201 " {\n"
202 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
203 " }\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"
208 " {\n"
209 " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
210 " return -1;\n"
211 " }\n"
212 " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
213 " }\n"
214 " \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"
218 " {\n"
219 " printf(\"-R needs an additional parameter.\\n\");\n"
220 " return -1;\n"
221 " }\n"
222 " \n"
223 " char *arg = lowercase(av[1 + partial_match]);\n"
224 " for (i =0; i < NumTests; ++i)\n"
225 " {\n"
226 " char *test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
227 " if (partial_match && strstr(test_name, arg) != NULL)\n"
228 " {\n"
229 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
230 " }\n"
231 " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
232 " {\n"
233 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
234 " }\n"
235 " delete [] test_name;\n"
236 " }\n"
237 " delete [] arg;\n"
238 " \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"
242 " {\n"
243 " return (*cmakeGeneratedFunctionMapEntries[0].func)(ac, av);\n"
244 " }\n"
245 " \n"
246 " // Nothing was run, display the test names\n"
247 " printf(\"Available tests:\\n\");\n"
248 " for (i =0; i < NumTests; ++i)\n"
249 " {\n"
250 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
251 " }\n"
252 " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
253 " \n"
254 " return -1;\n"
255 "}\n";
257 fout.close();
259 // Create the source list
260 cmSourceFile cfile;
261 std::string sourceListValue;
263 cfile.SetIsAnAbstractClass(false);
264 cfile.SetName(args[1].c_str(),
265 m_Makefile->GetCurrentOutputDirectory(),
266 "cxx",
267 false);
268 m_Makefile->AddSource(cfile);
269 sourceListValue = args[1] + ".cxx";
271 for(i = testsBegin; i != tests.end(); ++i)
273 cmSourceFile cfile;
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());
285 return true;