Make wineinstall work in the new-autoconf-version world.
[wine/testsucceed.git] / tools / winebuild / main.c
blob37f39bdc2a5e99243e79ec3f7fe57bb5356b827b
1 /*
2 * Main function
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
9 */
11 #include "config.h"
13 #include <assert.h>
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <string.h>
19 #include "winnt.h"
20 #include "build.h"
22 ORDDEF *EntryPoints[MAX_ORDINALS];
23 ORDDEF *Ordinals[MAX_ORDINALS];
24 ORDDEF *Names[MAX_ORDINALS];
26 SPEC_MODE SpecMode = SPEC_MODE_DLL;
27 int Base = MAX_ORDINALS;
28 int Limit = 0;
29 int DLLHeapSize = 0;
30 int UsePIC = 0;
31 int stack_size = 0;
32 int nb_entry_points = 0;
33 int nb_names = 0;
34 int nb_debug_channels = 0;
35 int nb_lib_paths = 0;
37 /* we only support relay debugging on i386 */
38 #if defined(__i386__) && !defined(NO_TRACE_MSGS)
39 int debugging = 1;
40 #else
41 int debugging = 0;
42 #endif
44 char DLLName[80];
45 char DLLFileName[80];
46 char owner_name[80];
47 char *init_func = NULL;
48 char **debug_channels = NULL;
49 char **lib_path = NULL;
51 const char *input_file_name;
52 const char *output_file_name;
54 static FILE *input_file;
55 static FILE *output_file;
57 /* execution mode */
58 static enum
60 MODE_NONE,
61 MODE_SPEC,
62 MODE_GLUE,
63 MODE_DEF,
64 MODE_RELAY16,
65 MODE_RELAY32
66 } exec_mode = MODE_NONE;
68 /* open the input file */
69 static void open_input( const char *name )
71 input_file_name = name;
72 if (!(input_file = fopen( name, "r" )))
74 fprintf( stderr, "Cannot open input file '%s'\n", name );
75 exit(1);
79 /* cleanup on program exit */
80 static void cleanup(void)
82 if (output_file_name) unlink( output_file_name );
86 /*******************************************************************
87 * command-line option handling
90 struct option_descr
92 const char *name;
93 int has_arg;
94 void (*func)();
95 const char *usage;
98 static void do_pic(void);
99 static void do_output( const char *arg );
100 static void do_usage(void);
101 static void do_spec( const char *arg );
102 static void do_def( const char *arg );
103 static void do_glue( const char *arg );
104 static void do_relay16(void);
105 static void do_relay32(void);
106 static void do_sym( const char *arg );
107 static void do_lib( const char *arg );
109 static const struct option_descr option_table[] =
111 { "-fPIC", 0, do_pic, "-fPIC Generate PIC code" },
112 { "-h", 0, do_usage, "-h Display this help message" },
113 { "-L", 1, do_lib, "-L directory Look for imports libraries in 'directory'" },
114 { "-o", 1, do_output, "-o name Set the output file name (default: stdout)" },
115 { "-sym", 1, do_sym, "-sym file.o Read the list of undefined symbols from 'file.o'" },
116 { "-spec", 1, do_spec, "-spec file.spec Build a .c file from a spec file" },
117 { "-def", 1, do_def, "-def file.spec Build a .def file from a spec file" },
118 { "-glue", 1, do_glue, "-glue file.c Build the 16-bit glue for a .c file" },
119 { "-relay16", 0, do_relay16, "-relay16 Build the 16-bit relay assembly routines" },
120 { "-relay32", 0, do_relay32, "-relay32 Build the 32-bit relay assembly routines" },
121 { NULL, 0, NULL, NULL }
124 static void do_pic(void)
126 UsePIC = 1;
129 static void do_output( const char *arg )
131 if ( ( unlink ( arg ) ) == -1 && ( errno != ENOENT ) )
133 fprintf ( stderr, "Unable to create output file '%s'\n", arg );
134 exit (1);
136 if (!(output_file = fopen( arg, "w" )))
138 fprintf( stderr, "Unable to create output file '%s'\n", arg );
139 exit(1);
141 output_file_name = arg;
142 atexit( cleanup ); /* make sure we remove the output file on exit */
145 static void do_usage(void)
147 const struct option_descr *opt;
148 fprintf( stderr, "Usage: winebuild [options]\n\n" );
149 fprintf( stderr, "Options:\n" );
150 for (opt = option_table; opt->name; opt++) fprintf( stderr, " %s\n", opt->usage );
151 fprintf( stderr, "\nExactly one of -spec, -glue or -relay must be specified.\n\n" );
152 exit(1);
155 static void do_spec( const char *arg )
157 if (exec_mode != MODE_NONE || !arg[0]) do_usage();
158 exec_mode = MODE_SPEC;
159 open_input( arg );
162 static void do_def( const char *arg )
164 if (exec_mode != MODE_NONE || !arg[0]) do_usage();
165 exec_mode = MODE_DEF;
166 open_input( arg );
169 static void do_glue( const char *arg )
171 if (exec_mode != MODE_NONE || !arg[0]) do_usage();
172 exec_mode = MODE_GLUE;
173 open_input( arg );
176 static void do_relay16(void)
178 if (exec_mode != MODE_NONE) do_usage();
179 exec_mode = MODE_RELAY16;
182 static void do_relay32(void)
184 if (exec_mode != MODE_NONE) do_usage();
185 exec_mode = MODE_RELAY32;
188 static void do_sym( const char *arg )
190 extern void read_undef_symbols( const char *name );
191 read_undef_symbols( arg );
194 static void do_lib( const char *arg )
196 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
197 lib_path[nb_lib_paths++] = xstrdup( arg );
200 /* parse options from the argv array and remove all the recognized ones */
201 static void parse_options( char *argv[] )
203 const struct option_descr *opt;
204 char * const * ptr;
205 const char* arg=NULL;
207 ptr=argv+1;
208 while (*ptr != NULL)
210 for (opt = option_table; opt->name; opt++)
212 if (opt->has_arg && !strncmp( *ptr, opt->name, strlen(opt->name) ))
214 arg=*ptr+strlen(opt->name);
215 if (*arg=='\0')
217 ptr++;
218 arg=*ptr;
220 break;
222 if (!strcmp( *ptr, opt->name ))
224 arg=NULL;
225 break;
229 if (!opt->name)
231 fprintf( stderr, "Unrecognized option '%s'\n", *ptr );
232 do_usage();
235 if (opt->has_arg && arg!=NULL) opt->func( arg );
236 else opt->func( "" );
237 ptr++;
242 /*******************************************************************
243 * main
245 int main(int argc, char **argv)
247 output_file = stdout;
248 parse_options( argv );
250 switch(exec_mode)
252 case MODE_SPEC:
253 switch (ParseTopLevel( input_file ))
255 case SPEC_WIN16:
256 BuildSpec16File( output_file );
257 break;
258 case SPEC_WIN32:
259 BuildSpec32File( output_file );
260 break;
261 default: assert(0);
263 break;
264 case MODE_DEF:
265 switch (ParseTopLevel( input_file ))
267 case SPEC_WIN16:
268 fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
269 break;
270 case SPEC_WIN32:
271 BuildDef32File( output_file );
272 break;
273 default: assert(0);
275 break;
276 case MODE_GLUE:
277 BuildGlue( output_file, input_file );
278 break;
279 case MODE_RELAY16:
280 BuildRelays16( output_file );
281 break;
282 case MODE_RELAY32:
283 BuildRelays32( output_file );
284 break;
285 default:
286 do_usage();
287 break;
289 if (output_file_name)
291 fclose( output_file );
292 output_file_name = NULL;
294 return 0;