Add support for naming the DLL from the command line.
[wine/gsoc_dplay.git] / tools / winebuild / main.c
blob21c5969249b0803f842e8ebafed31d5293509894
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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
33 #include "build.h"
35 ORDDEF *EntryPoints[MAX_ORDINALS];
36 ORDDEF *Ordinals[MAX_ORDINALS];
37 ORDDEF *Names[MAX_ORDINALS];
39 SPEC_MODE SpecMode = SPEC_MODE_DLL;
40 int Base = MAX_ORDINALS;
41 int Limit = 0;
42 int DLLHeapSize = 0;
43 int UsePIC = 0;
44 int stack_size = 0;
45 int nb_entry_points = 0;
46 int nb_names = 0;
47 int nb_debug_channels = 0;
48 int nb_lib_paths = 0;
49 int display_warnings = 0;
51 /* we only support relay debugging on i386 */
52 #if defined(__i386__) && !defined(NO_TRACE_MSGS)
53 int debugging = 1;
54 #else
55 int debugging = 0;
56 #endif
58 char DLLName[80];
59 char DLLFileName[80];
60 char owner_name[80];
61 char *init_func = NULL;
62 char **debug_channels = NULL;
63 char **lib_path = NULL;
65 char *input_file_name = NULL;
66 const char *output_file_name = NULL;
68 static FILE *input_file;
69 static FILE *output_file;
70 static const char *current_src_dir;
72 /* execution mode */
73 static enum
75 MODE_NONE,
76 MODE_SPEC,
77 MODE_EXE,
78 MODE_GLUE,
79 MODE_DEF,
80 MODE_DEBUG,
81 MODE_RELAY16,
82 MODE_RELAY32
83 } exec_mode = MODE_NONE;
85 /* set the dll file name from the input file name */
86 static void set_dll_file_name( const char *name )
88 char *p;
90 if ((p = strrchr( name, '\\' ))) name = p + 1;
91 if ((p = strrchr( name, '/' ))) name = p + 1;
92 strcpy( DLLFileName, name );
93 if ((p = strrchr( DLLFileName, '.' )) && !strcmp( p, ".spec" )) *p = 0;
94 if (!strchr( DLLFileName, '.' )) strcat( DLLFileName, ".dll" );
97 /* cleanup on program exit */
98 static void cleanup(void)
100 if (output_file_name) unlink( output_file_name );
104 /*******************************************************************
105 * command-line option handling
108 struct option_descr
110 const char *name;
111 int has_arg;
112 void (*func)();
113 const char *usage;
116 static void do_output( const char *arg );
117 static void do_usage(void);
118 static void do_warnings(void);
119 static void do_f_flags( const char *arg );
120 static void do_define( const char *arg );
121 static void do_include( const char *arg );
122 static void do_k_flags( const char *arg );
123 static void do_exe_mode( const char *arg );
124 static void do_module( const char *arg );
125 static void do_name( const char *arg );
126 static void do_spec( const char *arg );
127 static void do_def( const char *arg );
128 static void do_exe( const char *arg );
129 static void do_glue(void);
130 static void do_relay16(void);
131 static void do_relay32(void);
132 static void do_debug(void);
133 static void do_sym(void);
134 static void do_chdir( const char *arg );
135 static void do_lib( const char *arg );
136 static void do_import( const char *arg );
137 static void do_dimport( const char *arg );
138 static void do_rsrc( const char *arg );
140 static const struct option_descr option_table[] =
142 { "-h", 0, do_usage, "-h Display this help message" },
143 { "-w", 0, do_warnings,"-w Turn on warnings" },
144 { "-C", 1, do_chdir, "-C dir Change directory to <dir> before opening source files" },
145 { "-f", 1, do_f_flags, "-f flags Compiler flags (only -fPIC is supported)" },
146 { "-D", 1, do_define, "-D sym Ignored for C flags compatibility" },
147 { "-I", 1, do_include, "-I dir Ignored for C flags compatibility" },
148 { "-K", 1, do_k_flags, "-K flags Compiler flags (only -KPIC is supported)" },
149 { "-m", 1, do_exe_mode,"-m mode Set the executable mode (cui|gui|cuiw|guiw)" },
150 { "-M", 1, do_module, "-M module Set the name of the main (Win32) module for a Win16 dll" },
151 { "-L", 1, do_lib, "-L directory Look for imports libraries in 'directory'" },
152 { "-l", 1, do_import, "-l lib.dll Import the specified library" },
153 { "-dl", 1, do_dimport, "-dl lib.dll Delay-import the specified library" },
154 { "-N", 1, do_name, "-N dllname Set the DLL name (default: set from input file name)" },
155 { "-res", 1, do_rsrc, "-res rsrc.res Load resources from rsrc.res" },
156 { "-o", 1, do_output, "-o name Set the output file name (default: stdout)\n" },
157 { "-sym", 0, do_sym, NULL }, /* ignored for backwards compatibility */
158 { "-spec", 1, do_spec, "-spec file.spec Build a .c file from a spec file" },
159 { "-def", 1, do_def, "-def file.spec Build a .def file from a spec file" },
160 { "-exe", 1, do_exe, "-exe name Build a .c file from the named executable" },
161 { "-debug", 0, do_debug, "-debug [files] Build a .c file containing debug channels declarations" },
162 { "-glue", 0, do_glue, "-glue [files] Build the 16-bit glue for the source files" },
163 { "-relay16", 0, do_relay16, "-relay16 Build the 16-bit relay assembly routines" },
164 { "-relay32", 0, do_relay32, "-relay32 Build the 32-bit relay assembly routines" },
165 { NULL, 0, NULL, NULL }
168 static void do_output( const char *arg )
170 if ( ( unlink ( arg ) ) == -1 && ( errno != ENOENT ) )
172 fprintf ( stderr, "Unable to create output file '%s'\n", arg );
173 exit (1);
175 if (!(output_file = fopen( arg, "w" )))
177 fprintf( stderr, "Unable to create output file '%s'\n", arg );
178 exit(1);
180 output_file_name = arg;
181 atexit( cleanup ); /* make sure we remove the output file on exit */
184 static void do_usage(void)
186 const struct option_descr *opt;
187 fprintf( stderr, "Usage: winebuild [options]\n\n" );
188 fprintf( stderr, "Options:\n" );
189 for (opt = option_table; opt->name; opt++)
190 if (opt->usage) fprintf( stderr, " %s\n", opt->usage );
192 fprintf( stderr, "\nExactly one of -spec, -def, -exe, -debug, -glue, -relay16 or -relay32 must be specified.\n\n" );
193 exit(1);
196 static void do_warnings(void)
198 display_warnings = 1;
201 static void do_f_flags( const char *arg )
203 if (!strcmp( arg, "PIC" )) UsePIC = 1;
204 /* ignore all other flags */
207 static void do_define( const char *arg )
209 /* nothing */
212 static void do_include( const char *arg )
214 /* nothing */
217 static void do_k_flags( const char *arg )
219 /* Ignored, because cc generates correct code. */
220 /* if (!strcmp( arg, "PIC" )) UsePIC = 1; */
221 /* ignore all other flags */
224 static void do_name( const char *arg )
226 strncpy( DLLName, arg, sizeof(DLLName) );
227 DLLName[sizeof(DLLName) - 1] = 0;
230 static void do_spec( const char *arg )
232 if (exec_mode != MODE_NONE || !arg[0]) do_usage();
233 exec_mode = MODE_SPEC;
234 input_file = open_input_file( NULL, arg );
235 set_dll_file_name( arg );
238 static void do_def( const char *arg )
240 if (exec_mode != MODE_NONE || !arg[0]) do_usage();
241 exec_mode = MODE_DEF;
242 input_file = open_input_file( NULL, arg );
243 set_dll_file_name( arg );
246 static void do_exe( const char *arg )
248 const char *p;
250 if (exec_mode != MODE_NONE || !arg[0]) do_usage();
251 exec_mode = MODE_EXE;
252 if ((p = strrchr( arg, '/' ))) p++;
253 else p = arg;
254 strcpy( DLLFileName, p );
255 if (!strchr( DLLFileName, '.' )) strcat( DLLFileName, ".exe" );
256 if (SpecMode == SPEC_MODE_DLL) SpecMode = SPEC_MODE_GUIEXE;
259 static void do_exe_mode( const char *arg )
261 if (!strcmp( arg, "gui" )) SpecMode = SPEC_MODE_GUIEXE;
262 else if (!strcmp( arg, "cui" )) SpecMode = SPEC_MODE_CUIEXE;
263 else if (!strcmp( arg, "guiw" )) SpecMode = SPEC_MODE_GUIEXE_UNICODE;
264 else if (!strcmp( arg, "cuiw" )) SpecMode = SPEC_MODE_CUIEXE_UNICODE;
265 else do_usage();
268 static void do_module( const char *arg )
270 strcpy( owner_name, arg );
273 static void do_glue(void)
275 if (exec_mode != MODE_NONE) do_usage();
276 exec_mode = MODE_GLUE;
279 static void do_debug(void)
281 if (exec_mode != MODE_NONE) do_usage();
282 exec_mode = MODE_DEBUG;
285 static void do_chdir( const char *arg )
287 current_src_dir = arg;
290 static void do_relay16(void)
292 if (exec_mode != MODE_NONE) do_usage();
293 exec_mode = MODE_RELAY16;
296 static void do_relay32(void)
298 if (exec_mode != MODE_NONE) do_usage();
299 exec_mode = MODE_RELAY32;
302 static void do_sym(void)
304 /* nothing */
307 static void do_lib( const char *arg )
309 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
310 lib_path[nb_lib_paths++] = xstrdup( arg );
313 static void do_import( const char *arg )
315 add_import_dll( arg, 0 );
318 static void do_dimport( const char *arg )
320 add_import_dll( arg, 1 );
323 static void do_rsrc( const char *arg )
325 load_res32_file( arg );
328 /* parse options from the argv array and remove all the recognized ones */
329 static void parse_options( char *argv[] )
331 const struct option_descr *opt;
332 char **ptr, **last;
333 const char* arg=NULL;
335 for (ptr = last = argv + 1; *ptr; ptr++)
337 for (opt = option_table; opt->name; opt++)
339 if (opt->has_arg && !strncmp( *ptr, opt->name, strlen(opt->name) ))
341 arg=*ptr+strlen(opt->name);
342 if (*arg=='\0')
344 ptr++;
345 arg=*ptr;
347 break;
349 if (!strcmp( *ptr, opt->name ))
351 arg=NULL;
352 break;
356 if (opt->name)
358 if (opt->has_arg && arg != NULL) opt->func( arg );
359 else opt->func( "" );
361 else /* keep this argument */
363 if (last != ptr) *last = *ptr;
364 last++;
367 *last = NULL;
371 /*******************************************************************
372 * main
374 int main(int argc, char **argv)
376 output_file = stdout;
377 parse_options( argv );
379 switch(exec_mode)
381 case MODE_SPEC:
382 switch (ParseTopLevel( input_file, 0 ))
384 case SPEC_WIN16:
385 if (argv[1])
386 fatal_error( "file argument '%s' not allowed in this mode\n", argv[1] );
387 BuildSpec16File( output_file );
388 break;
389 case SPEC_WIN32:
390 read_undef_symbols( argv + 1 );
391 BuildSpec32File( output_file );
392 break;
393 default: assert(0);
395 break;
396 case MODE_EXE:
397 read_undef_symbols( argv + 1 );
398 BuildSpec32File( output_file );
399 break;
400 case MODE_DEF:
401 if (argv[1]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[1] );
402 switch (ParseTopLevel( input_file, 1 ))
404 case SPEC_WIN16:
405 fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
406 break;
407 case SPEC_WIN32:
408 BuildDef32File( output_file );
409 break;
410 default: assert(0);
412 break;
413 case MODE_DEBUG:
414 BuildDebugFile( output_file, current_src_dir, argv + 1 );
415 break;
416 case MODE_GLUE:
417 BuildGlue( output_file, current_src_dir, argv + 1 );
418 break;
419 case MODE_RELAY16:
420 if (argv[1]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[1] );
421 BuildRelays16( output_file );
422 break;
423 case MODE_RELAY32:
424 if (argv[1]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[1] );
425 BuildRelays32( output_file );
426 break;
427 default:
428 do_usage();
429 break;
431 if (output_file_name)
433 fclose( output_file );
434 output_file_name = NULL;
436 return 0;