1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/*=========================================================================
Program: CMake - Cross-Platform Makefile Generator
Module: $RCSfile$
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "cmCreateTestSourceList.h"
#include "cmSourceFile.h"
// cmCreateTestSourceList
bool cmCreateTestSourceList::InitialPass(std::vector<std::string> const& args)
{
if (args.size() < 3)
{
this->SetError("called with wrong number of arguments.");
return false;
}
std::vector<std::string>::const_iterator i = args.begin();
std::string extraInclude;
std::string function;
std::vector<std::string> tests;
// extract extra include and function ot
for(; i != args.end(); i++)
{
if(*i == "EXTRA_INCLUDE")
{
++i;
if(i == args.end())
{
this->SetError("incorrect arguments to EXTRA_INCLUDE");
return false;
}
extraInclude = *i;
}
else if(*i == "FUNCTION")
{
++i;
if(i == args.end())
{
this->SetError("incorrect arguments to FUNCTION");
return false;
}
function = *i;
}
else
{
tests.push_back(*i);
}
}
i = tests.begin();
// Name of the source list
const char* sourceList = i->c_str();
++i;
// Name of the test driver
// make sure they specified an extension
if (cmSystemTools::GetFilenameExtension(*i).size() < 2)
{
this->SetError("You must specify a file extenion for the test driver file.");
return false;
}
std::string driver = m_Makefile->GetCurrentOutputDirectory();
driver += "/";
driver += *i;
++i;
std::ofstream fout(driver.c_str());
if (!fout)
{
std::string err = "Could not create file ";
err += driver;
err += " for cmCreateTestSourceList command.";
this->SetError(err.c_str());
return false;
}
// Create the test driver file
fout <<
"#include <ctype.h>\n"
"#include <stdio.h>\n"
"#include <string.h>\n"
"#include <stdlib.h>\n"
"\n";
fout <<
"#if defined(_MSC_VER) && defined(_DEBUG)\n"
"/* MSVC debug hook to prevent dialogs when running from DART. */\n"
"# include <crtdbg.h>\n"
"static int TestDriverDebugReport(int type, char* message, int* retVal)\n"
"{\n"
" (void)type; (void)retVal;\n"
" fprintf(stderr, message);\n"
" exit(1);\n"
" return 0;\n"
"}\n"
"#endif\n";
if(extraInclude.size())
{
fout << "#include \"" << extraInclude << "\"\n";
}
fout <<
"\n"
"/* Forward declare test functions. */\n"
"\n";
std::vector<std::string>::const_iterator testsBegin = i;
std::vector<std::string> tests_func_name;
// The rest of the arguments consist of a list of test source files.
// Sadly, they can be in directories. Let's find a unique function
// name for the corresponding test, and push it to the tests_func_name
// list.
// For the moment:
// - replace spaces ' ', ':' and '/' with underscores '_'
for(i = testsBegin; i != tests.end(); ++i)
{
if(*i == "EXTRA_INCLUDE")
{
break;
}
std::string func_name;
if (cmSystemTools::GetFilenamePath(*i).size() > 0)
{
func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
cmSystemTools::GetFilenameWithoutLastExtension(*i);
}
else
{
func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
}
cmSystemTools::ConvertToUnixSlashes(func_name);
cmSystemTools::ReplaceString(func_name, " ", "_");
cmSystemTools::ReplaceString(func_name, "/", "_");
cmSystemTools::ReplaceString(func_name, ":", "_");
tests_func_name.push_back(func_name);
fout << "int " << func_name << "(int, char*[]);\n";
}
fout <<
"\n"
"/* Create map. */\n"
"\n"
"typedef int (*MainFuncPointer)(int , char*[]);\n"
"typedef struct\n"
"{\n"
" const char* name;\n"
" MainFuncPointer func;\n"
"} functionMapEntry;\n"
"\n"
"functionMapEntry cmakeGeneratedFunctionMapEntries[] = {\n";
int numTests = 0;
std::vector<std::string>::iterator j;
for(i = testsBegin, j = tests_func_name.begin(); i != tests.end(); ++i, ++j)
{
std::string func_name;
if (cmSystemTools::GetFilenamePath(*i).size() > 0)
{
func_name = cmSystemTools::GetFilenamePath(*i) + "/" +
cmSystemTools::GetFilenameWithoutLastExtension(*i);
}
else
{
func_name = cmSystemTools::GetFilenameWithoutLastExtension(*i);
}
fout <<
" {\n"
" \"" << func_name << "\",\n"
" " << *j << "\n"
" },\n";
numTests++;
}
fout <<
"};\n"
"\n"
"/* Allocate and create a lowercased copy of string\n"
" (note that it has to be free'd manually) */\n"
"\n"
"char* lowercase(const char *string)\n"
"{\n"
" char *new_string, *p;\n"
" new_string = (char *)malloc(sizeof(char) * (size_t)(strlen(string) + 1));\n"
" if (!new_string)\n"
" {\n"
" return 0;\n"
" }\n"
" strcpy(new_string, string);\n"
" p = new_string;\n"
" while (*p != 0)\n"
" {\n"
" *p = (char)tolower(*p);\n"
" ++p;\n"
" }\n"
" return new_string;\n"
"}\n"
"\n"
"int main(int ac, char *av[])\n"
"{\n"
" int i, NumTests, testNum, partial_match;\n"
" char *arg, *test_name;\n"
" \n"
"#if defined(_MSC_VER) && defined(_DEBUG)\n"
" /* If running from DART, put in debug hook. */\n"
" if(getenv(\"DART_TEST_FROM_DART\"))\n"
" {\n"
" _CrtSetReportHook(TestDriverDebugReport);\n"
" }\n"
"#endif\n"
" \n"
" NumTests = " << numTests << ";\n"
" \n"
" /* If no test name was given */\n";
if(function.size())
{
fout << " /* process command line with user function. */\n"
<< " " << function << "(&ac, &av);\n";
}
fout <<
" if (ac < 2)\n"
" {\n"
" /* Ask for a test. */\n"
" printf(\"Available tests:\\n\");\n"
" for (i =0; i < NumTests; ++i)\n"
" {\n"
" printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
" }\n"
" printf(\"To run a test, enter the test number: \");\n"
" testNum = 0;\n"
" scanf(\"%d\", &testNum);\n"
" if (testNum >= NumTests)\n"
" {\n"
" printf(\"%3d is an invalid test number.\\n\", testNum);\n"
" return -1;\n"
" }\n"
" return (*cmakeGeneratedFunctionMapEntries[testNum].func)(ac-1, av+1);\n"
" }\n"
" \n"
" /* If partial match is requested. */\n"
" partial_match = (strcmp(av[1], \"-R\") == 0) ? 1 : 0;\n"
" if (partial_match && ac < 3)\n"
" {\n"
" printf(\"-R needs an additional parameter.\\n\");\n"
" return -1;\n"
" }\n"
" \n"
" arg = lowercase(av[1 + partial_match]);\n"
" for (i =0; i < NumTests; ++i)\n"
" {\n"
" test_name = lowercase(cmakeGeneratedFunctionMapEntries[i].name);\n"
" if (partial_match && strstr(test_name, arg) != NULL)\n"
" {\n"
" free(arg);\n"
" free(test_name);\n"
" return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 2, av + 2);\n"
" }\n"
" else if (!partial_match && strcmp(test_name, arg) == 0)\n"
" {\n"
" free(arg);\n"
" free(test_name);\n"
" return (*cmakeGeneratedFunctionMapEntries[i].func)(ac - 1, av + 1);\n"
" }\n"
" free(test_name);\n"
" }\n"
" free(arg);\n"
" \n"
" /* Nothing was run, display the test names. */\n"
" printf(\"Available tests:\\n\");\n"
" for (i =0; i < NumTests; ++i)\n"
" {\n"
" printf(\"%3d. %s\\n\", i, cmakeGeneratedFunctionMapEntries[i].name);\n"
" }\n"
" printf(\"Failed: %s is an invalid test name.\\n\", av[1]);\n"
" \n"
" return -1;\n"
"}\n";
fout.close();
// Create the source list
cmSourceFile cfile;
std::string sourceListValue;
cfile.SetProperty("ABSTRACT","0");
cfile.SetName(cmSystemTools::GetFilenameWithoutExtension(args[1]).c_str(),
m_Makefile->GetCurrentOutputDirectory(),
cmSystemTools::GetFilenameExtension(args[1]).c_str()+1,
false);
m_Makefile->AddSource(cfile);
sourceListValue = args[1];
for(i = testsBegin; i != tests.end(); ++i)
{
cmSourceFile icfile;
icfile.SetProperty("ABSTRACT","0");
icfile.SetName(i->c_str(),
m_Makefile->GetCurrentDirectory(),
m_Makefile->GetSourceExtensions(),
m_Makefile->GetHeaderExtensions());
m_Makefile->AddSource(icfile);
sourceListValue += ";";
sourceListValue += *i;
}
m_Makefile->AddDefinition(sourceList, sourceListValue.c_str());
return true;
}
|