Sync msvcmaker with make_ctests to generate valid code.
[wine.git] / tools / make_ctests.c
blobedd9f9fbbb5b82d3976d96af74b7fff732a62e32
1 /*
2 * Generate a C file containing a list of tests
4 * Copyright 2002, 2005 Alexandre Julliard
5 * Copyright 2002 Dimitrie O. Paun
6 * Copyright 2005 Royce Mitchell III for the ReactOS Project
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 ****** Keep in sync with tools/winapi/msvcmaker:_generate_testlist_c *****
25 #include "config.h"
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
35 static const char *output_file;
37 static void fatal_error( const char *msg, ... )
39 va_list valist;
40 va_start( valist, msg );
41 fprintf( stderr, "make_ctests: " );
42 vfprintf( stderr, msg, valist );
43 va_end( valist );
44 if (output_file) unlink( output_file );
45 exit(1);
48 static void fatal_perror( const char *msg, ... )
50 va_list valist;
51 va_start( valist, msg );
52 fprintf( stderr, "make_ctests: " );
53 vfprintf( stderr, msg, valist );
54 perror( " " );
55 va_end( valist );
56 exit(1);
59 static void *xmalloc( size_t size )
61 void *res = malloc (size ? size : 1);
62 if (!res) fatal_error( "virtual memory exhausted.\n" );
63 return res;
66 static char* basename( const char* filename )
68 const char *p, *p2;
69 char *out;
70 size_t out_len;
72 p = strrchr ( filename, '/' );
73 if ( !p )
74 p = filename;
75 else
76 ++p;
78 /* look for backslashes, too... */
79 p2 = strrchr ( p, '\\' );
80 if ( p2 ) p = p2 + 1;
82 /* find extension... */
83 p2 = strrchr ( p, '.' );
84 if ( !p2 )
85 p2 = p + strlen(p);
87 /* malloc a copy */
88 out_len = p2-p;
89 out = xmalloc ( out_len+1 );
90 memcpy ( out, p, out_len );
91 out[out_len] = '\0';
92 return out;
95 int main( int argc, const char** argv )
97 int i, count = 0;
98 FILE *out = stdout;
99 char **tests = xmalloc( argc * sizeof(*tests) );
101 for (i = 1; i < argc; i++)
103 if (!strcmp( argv[i], "-o" ) && i < argc-1)
105 output_file = argv[++i];
106 continue;
108 tests[count++] = basename( argv[i] );
111 if (output_file)
113 if (!(out = fopen( output_file, "w" )))
114 fatal_perror( "cannot create %s", output_file );
117 fprintf( out,
118 "/* Automatically generated file; DO NOT EDIT!! */\n"
119 "\n"
120 "/* stdarg.h is needed for Winelib */\n"
121 "#include <stdarg.h>\n"
122 "#include <stdio.h>\n"
123 "#include <stdlib.h>\n"
124 "#include \"windef.h\"\n"
125 "#include \"winbase.h\"\n\n"
126 "#define STANDALONE\n"
127 "#include \"wine/test.h\"\n\n" );
129 for (i = 0; i < count; i++) fprintf( out, "extern void func_%s(void);\n", tests[i] );
131 fprintf( out,
132 "\n"
133 "const struct test winetest_testlist[] =\n"
134 "{\n" );
136 for (i = 0; i < count; i++) fprintf( out, " { \"%s\", func_%s },\n", tests[i], tests[i] );
138 fprintf( out,
139 " { 0, 0 }\n"
140 "};\n" );
142 if (output_file && fclose( out ))
143 fatal_perror( "error writing to %s", output_file );
145 return 0;