ENH: mark some vars as advanced (and resort the list)
[cmake.git] / Source / cmCreateTestSourceList.cxx
blob62d169bfe717862481f3d7cdc1498f46d35f8bdb
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmCreateTestSourceList.cxx,v $
5 Language: C++
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.");
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 = (char)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 i, NumTests, 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 " // Ask for a test\n"
221 " printf(\"Available tests:\\n\");\n"
222 " for (i =0; i < NumTests; ++i)\n"
223 " {\n"
224 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
225 " }\n"
226 " printf(\"To run a test, enter the test number: \");\n"
227 " testNum = 0;\n"
228 " scanf(\"%d\", &testNum);\n"
229 " if (testNum >= NumTests)\n"
230 " {\n"
231 " printf(\"%3d is an invalid test number.\\n\", testNum);\n"
232 " return -1;\n"
233 " }\n"
234 " return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
235 " }\n"
236 " \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"
240 " {\n"
241 " printf(\"-R needs an additional parameter.\\n\");\n"
242 " return -1;\n"
243 " }\n"
244 " \n"
245 " arg = lowercase(av[1 + partial_match]);\n"
246 " for (i =0; i < NumTests; ++i)\n"
247 " {\n"
248 " test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
249 " if (partial_match && strstr(test_name, arg) != NULL)\n"
250 " {\n"
251 " free(arg);\n"
252 " free(test_name);\n"
253 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
254 " }\n"
255 " else if (!partial_match && strcmp(test_name, arg) == 0)\n"
256 " {\n"
257 " free(arg);\n"
258 " free(test_name);\n"
259 " return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
260 " }\n"
261 " free(test_name);\n"
262 " }\n"
263 " free(arg);\n"
264 " \n"
265 " // Nothing was run, display the test names\n"
266 " printf(\"Available tests:\\n\");\n"
267 " for (i =0; i < NumTests; ++i)\n"
268 " {\n"
269 " printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
270 " }\n"
271 " printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
272 " \n"
273 " return -1;\n"
274 "}\n";
276 fout.close();
278 // Create the source list
279 cmSourceFile cfile;
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,
286 false);
287 m_Makefile->AddSource(cfile);
288 sourceListValue = args[1];
290 for(i = testsBegin; i != tests.end(); ++i)
292 cmSourceFile cfile;
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());
304 return true;