* Bring back rbuild build to be used until bug 6372 is fixed.
[reactos.git] / tools / rbuild / testsupportcode.cpp
1 /*
2 * Copyright (C) 2005 Casper S. Hornstrup
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 #include "pch.h"
19 #include <assert.h>
20
21 #include "rbuild.h"
22
23 using std::string;
24 using std::vector;
25
26 static std::string
27 GetFilename ( const std::string& filename )
28 {
29 size_t index = filename.find_last_of ( cSep );
30 if ( index == string::npos )
31 return filename;
32 else
33 return filename.substr ( index + 1, filename.length () - index );
34 }
35
36 TestSupportCode::TestSupportCode ( const Project& project )
37 : project ( project )
38 {
39 }
40
41 TestSupportCode::~TestSupportCode ()
42 {
43 }
44
45 bool
46 TestSupportCode::IsTestModule ( const Module& module )
47 {
48 return module.type == Test;
49 }
50
51 void
52 TestSupportCode::GenerateTestSupportCode ( bool verbose )
53 {
54 for( std::map<std::string, Module*>::const_iterator p = project.modules.begin(); p != project.modules.end(); ++ p )
55 {
56 if ( IsTestModule ( *p->second ) )
57 {
58 GenerateTestSupportCodeForModule ( *p->second,
59 verbose );
60 }
61 }
62 }
63
64 void
65 TestSupportCode::GenerateTestSupportCodeForModule ( Module& module,
66 bool verbose )
67 {
68 if ( verbose )
69 {
70 printf ( "\nGenerating test support code for %s",
71 module.name.c_str () );
72 }
73
74 WriteHooksFile ( module );
75 WriteStubsFile ( module );
76 WriteStartupFile ( module );
77 }
78
79 string
80 TestSupportCode::GetHooksFilename ( Module& module )
81 {
82 return NormalizeFilename ( Environment::GetIntermediatePath () + sSep + module.output->relative_path + sSep + "_hooks.c" );
83 }
84
85 char*
86 TestSupportCode::WriteStubbedSymbolToHooksFile ( char* buffer,
87 const StubbedComponent& component,
88 const StubbedSymbol& symbol )
89 {
90 buffer = buffer + sprintf ( buffer,
91 " {\"%s\", \"%s\", NULL, NULL, NULL},\n",
92 component.name.c_str (),
93 symbol.newname.c_str () );
94 return buffer;
95 }
96
97 char*
98 TestSupportCode::WriteStubbedComponentToHooksFile ( char* buffer,
99 const StubbedComponent& component )
100 {
101 for ( size_t i = 0; i < component.symbols.size () ; i++ )
102 buffer = WriteStubbedSymbolToHooksFile ( buffer,
103 component,
104 *component.symbols[i] );
105 return buffer;
106 }
107
108 void
109 TestSupportCode::WriteHooksFile ( Module& module )
110 {
111 char* buf;
112 char* s;
113
114 buf = (char*) malloc ( 50*1024 );
115 if ( buf == NULL )
116 throw OutOfMemoryException ();
117
118 s = buf;
119 s = s + sprintf ( s, "/* This file is automatically generated. */\n" );
120 s = s + sprintf ( s, "#include <windows.h>\n" );
121 s = s + sprintf ( s, "#include \"regtests.h\"\n" );
122 s = s + sprintf ( s, "\n" );
123 s = s + sprintf ( s, "_API_DESCRIPTION ExternalDependencies[] =\n" );
124 s = s + sprintf ( s, "{\n" );
125
126 int symbolCount = 0;
127 for ( size_t i = 0; i < module.stubbedComponents.size () ; i++ )
128 {
129 s = WriteStubbedComponentToHooksFile ( s,
130 *module.stubbedComponents[i] );
131 symbolCount += module.stubbedComponents[i]->symbols.size ();
132 }
133
134 s = s + sprintf ( s, "};\n" );
135 s = s + sprintf ( s, "\n" );
136 s = s + sprintf ( s, "#define ExternalDependencyCount %d\n", symbolCount );
137 s = s + sprintf ( s, "ULONG MaxExternalDependency = ExternalDependencyCount - 1;\n" );
138 s = s + sprintf ( s, "\n" );
139
140 FileSupportCode::WriteIfChanged ( buf, GetHooksFilename ( module ) );
141
142 free ( buf );
143 }
144
145 string
146 TestSupportCode::GetStubsFilename ( Module& module )
147 {
148 return NormalizeFilename ( Environment::GetIntermediatePath () + sSep + module.output->relative_path + sSep + "_stubs.S" );
149 }
150
151 string
152 GetLinkerSymbol ( const StubbedSymbol& symbol )
153 {
154 if (symbol.symbol[0] == '@')
155 return symbol.symbol;
156 else
157 return "_" + symbol.symbol;
158 }
159
160 string
161 GetLinkerImportSymbol ( const StubbedSymbol& symbol )
162 {
163 if (symbol.symbol[0] == '@')
164 return "__imp_" + symbol.symbol;
165 else
166 return "__imp__" + symbol.symbol;
167 }
168
169 string
170 GetIndirectCallTargetSymbol ( const StubbedSymbol& symbol )
171 {
172 return GetLinkerSymbol ( symbol ) + "_";
173 }
174
175 char*
176 TestSupportCode::WriteStubbedSymbolToStubsFile ( char* buffer,
177 const StubbedComponent& component,
178 const StubbedSymbol& symbol,
179 int stubIndex )
180 {
181 string linkerSymbol = GetLinkerSymbol ( symbol );
182 string linkerImportSymbol = GetLinkerImportSymbol ( symbol );
183 string indirectCallTargetSymbol = GetIndirectCallTargetSymbol ( symbol );
184 buffer = buffer + sprintf ( buffer,
185 ".globl %s\n",
186 linkerSymbol.c_str () );
187 buffer = buffer + sprintf ( buffer,
188 ".globl %s\n",
189 linkerImportSymbol.c_str () );
190 buffer = buffer + sprintf ( buffer,
191 "%s:\n",
192 linkerSymbol.c_str () );
193 buffer = buffer + sprintf ( buffer,
194 "%s:\n",
195 linkerImportSymbol.c_str () );
196 buffer = buffer + sprintf ( buffer,
197 " .long %s\n",
198 indirectCallTargetSymbol.c_str () );
199 buffer = buffer + sprintf ( buffer,
200 "%s:\n",
201 indirectCallTargetSymbol.c_str () );
202 buffer = buffer + sprintf ( buffer,
203 " pushl $%d\n",
204 stubIndex );
205 buffer = buffer + sprintf ( buffer,
206 " jmp passthrough\n" );
207 buffer = buffer + sprintf ( buffer, "\n" );
208 return buffer;
209 }
210
211 char*
212 TestSupportCode::WriteStubbedComponentToStubsFile ( char* buffer,
213 const StubbedComponent& component,
214 int* stubIndex )
215 {
216 for ( size_t i = 0; i < component.symbols.size () ; i++ )
217 buffer = WriteStubbedSymbolToStubsFile ( buffer,
218 component,
219 *component.symbols[i],
220 (*stubIndex)++ );
221 return buffer;
222 }
223
224 void
225 TestSupportCode::WriteStubsFile ( Module& module )
226 {
227 char* buf;
228 char* s;
229
230 buf = (char*) malloc ( 512*1024 );
231 if ( buf == NULL )
232 throw OutOfMemoryException ();
233
234 s = buf;
235 s = s + sprintf ( s, "/* This file is automatically generated. */\n" );
236 s = s + sprintf ( s, "passthrough:\n" );
237 s = s + sprintf ( s, " call _FrameworkGetHook@4\n" );
238 s = s + sprintf ( s, " test %%eax, %%eax\n" );
239 s = s + sprintf ( s, " je .return\n" );
240 s = s + sprintf ( s, " jmp *%%eax\n" );
241 s = s + sprintf ( s, ".return:\n" );
242 s = s + sprintf ( s, " /* This will most likely corrupt the stack */\n" );
243 s = s + sprintf ( s, " ret\n" );
244 s = s + sprintf ( s, "\n" );
245
246 int stubIndex = 0;
247 for ( size_t i = 0; i < module.stubbedComponents.size () ; i++ )
248 {
249 s = WriteStubbedComponentToStubsFile ( s,
250 *module.stubbedComponents[i],
251 &stubIndex );
252 }
253
254 FileSupportCode::WriteIfChanged ( buf, GetStubsFilename ( module ) );
255
256 free ( buf );
257 }
258
259 string
260 TestSupportCode::GetStartupFilename ( Module& module )
261 {
262 return NormalizeFilename ( Environment::GetIntermediatePath () + sSep + module.output->relative_path + sSep + "_startup.c" );
263 }
264
265 bool
266 TestSupportCode::IsUnknownCharacter ( char ch )
267 {
268 if ( ch >= 'a' && ch <= 'z' )
269 return false;
270 if ( ch >= 'A' && ch <= 'Z' )
271 return false;
272 if ( ch >= '0' && ch <= '9' )
273 return false;
274 return true;
275 }
276
277 string
278 TestSupportCode::GetTestDispatcherName ( string filename )
279 {
280 string filenamePart = ReplaceExtension ( GetFilename ( filename ), "" );
281 if ( filenamePart.length () > 0 )
282 filenamePart[0] = toupper ( filenamePart[0] );
283 for ( size_t i = 1; i < filenamePart.length (); i++ )
284 {
285 if ( IsUnknownCharacter ( filenamePart[i] ) )
286 filenamePart[i] = '_';
287 else
288 filenamePart[i] = tolower ( filenamePart[i] );
289 }
290 return filenamePart + "Test";
291 }
292
293 bool
294 TestSupportCode::IsTestFile ( string& filename ) const
295 {
296 if ( stricmp ( GetFilename ( filename ).c_str (), "setup.c" ) == 0 )
297 return false;
298 return true;
299 }
300
301 void
302 TestSupportCode::GetSourceFilenames ( string_list& list,
303 Module& module ) const
304 {
305 size_t i;
306
307 const vector<CompilationUnit*>& compilationUnits = module.non_if_data.compilationUnits;
308 for ( i = 0; i < compilationUnits.size (); i++ )
309 {
310 const FileLocation& sourceFileLocation = compilationUnits[i]->GetFilename ();
311 string filename = sourceFileLocation.relative_path + sSep + sourceFileLocation.name;
312 if ( !compilationUnits[i]->IsGeneratedFile () && IsTestFile ( filename ) )
313 list.push_back ( filename );
314 }
315 }
316
317 char*
318 TestSupportCode::WriteTestDispatcherPrototypesToStartupFile ( char* buffer,
319 Module& module )
320 {
321 string_list files;
322 GetSourceFilenames ( files,
323 module );
324 for ( size_t i = 0; i < files.size (); i++ )
325 {
326 buffer = buffer + sprintf ( buffer,
327 "extern void %s(int Command, char *Buffer);\n",
328 GetTestDispatcherName ( files[i] ).c_str () );
329 }
330 buffer = buffer + sprintf ( buffer, "\n" );
331 return buffer;
332 }
333
334 char*
335 TestSupportCode::WriteRegisterTestsFunctionToStartupFile ( char* buffer,
336 Module& module )
337 {
338 buffer = buffer + sprintf ( buffer,
339 "extern void AddTest(TestRoutine Routine);\n" );
340 buffer = buffer + sprintf ( buffer,
341 "\n" );
342
343 buffer = buffer + sprintf ( buffer,
344 "void\n" );
345 buffer = buffer + sprintf ( buffer,
346 "RegisterTests()\n" );
347 buffer = buffer + sprintf ( buffer,
348 "{\n" );
349
350 string_list files;
351 GetSourceFilenames ( files,
352 module );
353 for ( size_t i = 0; i < files.size (); i++ )
354 {
355 buffer = buffer + sprintf ( buffer,
356 "AddTest((TestRoutine)%s);\n",
357 GetTestDispatcherName ( files[i]).c_str () );
358 }
359 buffer = buffer + sprintf ( buffer,
360 "}\n" );
361 buffer = buffer + sprintf ( buffer, "\n" );
362 return buffer;
363 }
364
365 void
366 TestSupportCode::WriteStartupFile ( Module& module )
367 {
368 char* buf;
369 char* s;
370
371 buf = (char*) malloc ( 50*1024 );
372 if ( buf == NULL )
373 throw OutOfMemoryException ();
374
375 s = buf;
376 s = s + sprintf ( s, "/* This file is automatically generated. */\n" );
377 s = s + sprintf ( s, "\n" );
378 s = s + sprintf ( s, "#include <windows.h>\n" );
379 s = s + sprintf ( s, "#include \"regtests.h\"\n" );
380 s = s + sprintf ( s, "\n" );
381 s = WriteTestDispatcherPrototypesToStartupFile ( s,
382 module );
383 s = WriteRegisterTestsFunctionToStartupFile ( s,
384 module );
385 s = s + sprintf ( s, "\n" );
386 s = s + sprintf ( s, "void\n" );
387 s = s + sprintf ( s, "ConsoleWrite(char *Buffer)\n" );
388 s = s + sprintf ( s, "{\n" );
389 s = s + sprintf ( s, " printf(Buffer);\n" );
390 s = s + sprintf ( s, "}\n" );
391 s = s + sprintf ( s, "\n" );
392 s = s + sprintf ( s, "int\n" );
393 s = s + sprintf ( s, "WINAPI\n" );
394 s = s + sprintf ( s, "WinMain(HINSTANCE hInstance,\n" );
395 s = s + sprintf ( s, " HINSTANCE hPrevInstance,\n" );
396 s = s + sprintf ( s, " LPSTR lpszCmdParam,\n" );
397 s = s + sprintf ( s, " int nCmdShow)\n" );
398 s = s + sprintf ( s, "{\n" );
399 s = s + sprintf ( s, " _SetPriorityClass(_GetCurrentProcess(), HIGH_PRIORITY_CLASS);\n" );
400 s = s + sprintf ( s, " _SetThreadPriority(_GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);\n" );
401 s = s + sprintf ( s, " InitializeTests();\n" );
402 s = s + sprintf ( s, " RegisterTests();\n" );
403 s = s + sprintf ( s, " SetupOnce();\n" );
404 s = s + sprintf ( s, " PerformTests(ConsoleWrite, NULL);\n" );
405 s = s + sprintf ( s, " _ExitProcess(0);\n" );
406 s = s + sprintf ( s, " return 0;\n" );
407 s = s + sprintf ( s, "}\n" );
408 s = s + sprintf ( s, "\n" );
409
410 FileSupportCode::WriteIfChanged ( buf, GetStartupFilename ( module ) );
411
412 free ( buf );
413 }