Fix timer tests to pass on windows 98.
[wine/wine-kai.git] / tools / winebuild / main.c
blob15f3bf8ef62dfa1a19fc1af1bd93586bfb42d737
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 <signal.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <ctype.h>
35 #ifdef HAVE_GETOPT_H
36 # include <getopt.h>
37 #endif
39 #include "windef.h"
40 #include "winbase.h"
41 #include "build.h"
43 int UsePIC = 0;
44 int nb_debug_channels = 0;
45 int nb_lib_paths = 0;
46 int nb_errors = 0;
47 int display_warnings = 0;
48 int kill_at = 0;
50 /* we only support relay debugging on i386 */
51 #ifdef __i386__
52 int debugging = 1;
53 #else
54 int debugging = 0;
55 #endif
57 char **debug_channels = NULL;
58 char **lib_path = NULL;
60 char *input_file_name = NULL;
61 const char *output_file_name = NULL;
63 char *ld_command = "ld";
64 char *nm_command = "nm";
66 static FILE *output_file;
67 static const char *current_src_dir;
68 static int nb_res_files;
69 static char **res_files;
70 static char *spec_file_name;
72 /* execution mode */
73 enum exec_mode_values
75 MODE_NONE,
76 MODE_DLL,
77 MODE_EXE,
78 MODE_DEF,
79 MODE_DEBUG,
80 MODE_RELAY16,
81 MODE_RELAY32
84 static enum exec_mode_values exec_mode = MODE_NONE;
86 /* set the dll file name from the input file name */
87 static void set_dll_file_name( const char *name, DLLSPEC *spec )
89 char *p;
91 if (spec->file_name) return;
93 if ((p = strrchr( name, '\\' ))) name = p + 1;
94 if ((p = strrchr( name, '/' ))) name = p + 1;
95 spec->file_name = xmalloc( strlen(name) + 5 );
96 strcpy( spec->file_name, name );
97 if ((p = strrchr( spec->file_name, '.' )))
99 if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
101 if (!strchr( spec->file_name, '.' )) strcat( spec->file_name, ".dll" );
104 /* set the dll subsystem */
105 static void set_subsystem( const char *subsystem, DLLSPEC *spec )
107 char *major, *minor, *str = xstrdup( subsystem );
109 if ((major = strchr( str, ':' ))) *major++ = 0;
110 if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
111 else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
112 else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
113 else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
114 if (major)
116 if ((minor = strchr( major, '.' )))
118 *minor++ = 0;
119 spec->subsystem_minor = atoi( minor );
121 spec->subsystem_major = atoi( major );
123 free( str );
126 /* cleanup on program exit */
127 static void cleanup(void)
129 if (output_file_name) unlink( output_file_name );
132 /* clean things up when aborting on a signal */
133 static void exit_on_signal( int sig )
135 exit(1); /* this will call atexit functions */
138 /*******************************************************************
139 * command-line option handling
141 static const char usage_str[] =
142 "Usage: winebuild [OPTIONS] [FILES]\n\n"
143 "Options:\n"
144 " -C --source-dir=DIR Look for source files in DIR\n"
145 " -d --delay-lib=LIB Import the specified library in delayed mode\n"
146 " -D SYM Ignored for C flags compatibility\n"
147 " -e --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
148 " -f FLAGS Compiler flags (only -fPIC is supported)\n"
149 " -F --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
150 " -h --help Display this help message\n"
151 " -H --heap=SIZE Set the heap size for a Win16 dll\n"
152 " -i --ignore=SYM[,SYM] Ignore specified symbols when resolving imports\n"
153 " -I DIR Ignored for C flags compatibility\n"
154 " -k --kill-at Kill stdcall decorations in generated .def files\n"
155 " -K FLAGS Compiler flags (only -KPIC is supported)\n"
156 " --ld-cmd=LD Command to use for linking (default: ld)\n"
157 " -l --library=LIB Import the specified library\n"
158 " -L --library-path=DIR Look for imports libraries in DIR\n"
159 " -M --main-module=MODULE Set the name of the main module for a Win16 dll\n"
160 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
161 " -N --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
162 " -o --output=NAME Set the output file name (default: stdout)\n"
163 " -r --res=RSRC.RES Load resources from RSRC.RES\n"
164 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console)\n"
165 " --version Print the version and exit\n"
166 " -w --warnings Turn on warnings\n"
167 "\nMode options:\n"
168 " --dll=FILE Build a .c file from a .spec or .def file\n"
169 " --def=FILE.SPEC Build a .def file from a spec file\n"
170 " --exe=NAME Build a .c file for the named executable\n"
171 " --debug [FILES] Build a .c file with the debug channels declarations\n"
172 " --relay16 Build the 16-bit relay assembly routines\n"
173 " --relay32 Build the 32-bit relay assembly routines\n\n"
174 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
176 enum long_options_values
178 LONG_OPT_DLL = 1,
179 LONG_OPT_DEF,
180 LONG_OPT_EXE,
181 LONG_OPT_DEBUG,
182 LONG_OPT_LDCMD,
183 LONG_OPT_NMCMD,
184 LONG_OPT_RELAY16,
185 LONG_OPT_RELAY32,
186 LONG_OPT_SUBSYSTEM,
187 LONG_OPT_VERSION
190 static const char short_options[] = "C:D:F:H:I:K:L:M:N:d:e:f:hi:kl:m:o:r:w";
192 static const struct option long_options[] =
194 { "dll", 1, 0, LONG_OPT_DLL },
195 { "def", 1, 0, LONG_OPT_DEF },
196 { "exe", 1, 0, LONG_OPT_EXE },
197 { "debug", 0, 0, LONG_OPT_DEBUG },
198 { "ld-cmd", 1, 0, LONG_OPT_LDCMD },
199 { "nm-cmd", 1, 0, LONG_OPT_NMCMD },
200 { "relay16", 0, 0, LONG_OPT_RELAY16 },
201 { "relay32", 0, 0, LONG_OPT_RELAY32 },
202 { "subsystem",1, 0, LONG_OPT_SUBSYSTEM },
203 { "version", 0, 0, LONG_OPT_VERSION },
204 /* aliases for short options */
205 { "source-dir", 1, 0, 'C' },
206 { "delay-lib", 1, 0, 'd' },
207 { "entry", 1, 0, 'e' },
208 { "filename", 1, 0, 'F' },
209 { "help", 0, 0, 'h' },
210 { "heap", 1, 0, 'H' },
211 { "ignore", 1, 0, 'i' },
212 { "kill-at", 0, 0, 'k' },
213 { "library", 1, 0, 'l' },
214 { "library-path", 1, 0, 'L' },
215 { "main-module", 1, 0, 'M' },
216 { "dll-name", 1, 0, 'N' },
217 { "output", 1, 0, 'o' },
218 { "res", 1, 0, 'r' },
219 { "warnings", 0, 0, 'w' },
220 { NULL, 0, 0, 0 }
223 static void usage( int exit_code )
225 fprintf( stderr, "%s", usage_str );
226 exit( exit_code );
229 static void set_exec_mode( enum exec_mode_values mode )
231 if (exec_mode != MODE_NONE) usage(1);
232 exec_mode = mode;
235 /* parse options from the argv array and remove all the recognized ones */
236 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
238 char *p;
239 int optc;
241 while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
243 switch(optc)
245 case 'C':
246 current_src_dir = optarg;
247 break;
248 case 'D':
249 /* ignored */
250 break;
251 case 'F':
252 spec->file_name = xstrdup( optarg );
253 break;
254 case 'H':
255 if (!isdigit(optarg[0]))
256 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
257 spec->heap_size = atoi(optarg);
258 if (spec->heap_size > 65535)
259 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
260 break;
261 case 'I':
262 /* ignored */
263 break;
264 case 'K':
265 /* ignored, because cc generates correct code. */
266 break;
267 case 'L':
268 lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
269 lib_path[nb_lib_paths++] = xstrdup( optarg );
270 break;
271 case 'M':
272 spec->owner_name = xstrdup( optarg );
273 spec->type = SPEC_WIN16;
274 break;
275 case 'N':
276 spec->dll_name = xstrdup( optarg );
277 break;
278 case 'd':
279 add_import_dll( optarg, 1 );
280 break;
281 case 'e':
282 spec->init_func = xstrdup( optarg );
283 if ((p = strchr( spec->init_func, '@' ))) *p = 0; /* kill stdcall decoration */
284 break;
285 case 'f':
286 if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
287 /* ignore all other flags */
288 break;
289 case 'h':
290 usage(0);
291 break;
292 case 'i':
294 char *str = xstrdup( optarg );
295 char *token = strtok( str, "," );
296 while (token)
298 add_ignore_symbol( token );
299 token = strtok( NULL, "," );
301 free( str );
303 break;
304 case 'k':
305 kill_at = 1;
306 break;
307 case 'l':
308 add_import_dll( optarg, 0 );
309 break;
310 case 'o':
311 if (unlink( optarg ) == -1 && errno != ENOENT)
312 fatal_error( "Unable to create output file '%s'\n", optarg );
313 if (!(output_file = fopen( optarg, "w" )))
314 fatal_error( "Unable to create output file '%s'\n", optarg );
315 output_file_name = xstrdup(optarg);
316 atexit( cleanup ); /* make sure we remove the output file on exit */
317 break;
318 case 'r':
319 res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
320 res_files[nb_res_files++] = xstrdup( optarg );
321 break;
322 case 'w':
323 display_warnings = 1;
324 break;
325 case LONG_OPT_DLL:
326 set_exec_mode( MODE_DLL );
327 spec_file_name = xstrdup( optarg );
328 set_dll_file_name( optarg, spec );
329 break;
330 case LONG_OPT_DEF:
331 set_exec_mode( MODE_DEF );
332 spec_file_name = xstrdup( optarg );
333 set_dll_file_name( optarg, spec );
334 break;
335 case LONG_OPT_EXE:
336 set_exec_mode( MODE_EXE );
337 if ((p = strrchr( optarg, '/' ))) p++;
338 else p = optarg;
339 spec->file_name = xmalloc( strlen(p) + 5 );
340 strcpy( spec->file_name, p );
341 if (!strchr( spec->file_name, '.' )) strcat( spec->file_name, ".exe" );
342 if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
343 break;
344 case LONG_OPT_DEBUG:
345 set_exec_mode( MODE_DEBUG );
346 break;
347 case LONG_OPT_LDCMD:
348 ld_command = xstrdup( optarg );
349 break;
350 case LONG_OPT_NMCMD:
351 nm_command = xstrdup( optarg );
352 break;
353 case LONG_OPT_RELAY16:
354 set_exec_mode( MODE_RELAY16 );
355 break;
356 case LONG_OPT_RELAY32:
357 set_exec_mode( MODE_RELAY32 );
358 break;
359 case LONG_OPT_SUBSYSTEM:
360 set_subsystem( optarg, spec );
361 break;
362 case LONG_OPT_VERSION:
363 printf( "winebuild version " PACKAGE_VERSION "\n" );
364 exit(0);
365 case '?':
366 usage(1);
367 break;
370 return &argv[optind];
374 /* load all specified resource files */
375 static void load_resources( char *argv[], DLLSPEC *spec )
377 int i;
378 char **ptr, **last;
380 switch (spec->type)
382 case SPEC_WIN16:
383 for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i], spec );
384 break;
386 case SPEC_WIN32:
387 for (i = 0; i < nb_res_files; i++)
389 if (!load_res32_file( res_files[i], spec ))
390 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
393 /* load any resource file found in the remaining arguments */
394 for (ptr = last = argv; *ptr; ptr++)
396 if (!load_res32_file( *ptr, spec ))
397 *last++ = *ptr; /* not a resource file, keep it in the list */
399 *last = NULL;
400 break;
404 static int parse_input_file( DLLSPEC *spec )
406 FILE *input_file = open_input_file( NULL, spec_file_name );
407 char *extension = strrchr( spec_file_name, '.' );
409 if (extension && !strcmp( extension, ".def" ))
410 return parse_def_file( input_file, spec );
411 else
412 return parse_spec_file( input_file, spec );
413 close_input_file( input_file );
417 /*******************************************************************
418 * main
420 int main(int argc, char **argv)
422 DLLSPEC *spec = alloc_dll_spec();
424 #ifdef SIGHUP
425 signal( SIGHUP, exit_on_signal );
426 #endif
427 signal( SIGTERM, exit_on_signal );
428 signal( SIGINT, exit_on_signal );
430 output_file = stdout;
431 argv = parse_options( argc, argv, spec );
433 switch(exec_mode)
435 case MODE_DLL:
436 spec->characteristics |= IMAGE_FILE_DLL;
437 load_resources( argv, spec );
438 if (!parse_input_file( spec )) break;
439 switch (spec->type)
441 case SPEC_WIN16:
442 if (argv[0])
443 fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
444 BuildSpec16File( output_file, spec );
445 break;
446 case SPEC_WIN32:
447 read_undef_symbols( argv );
448 BuildSpec32File( output_file, spec );
449 break;
450 default: assert(0);
452 break;
453 case MODE_EXE:
454 if (spec->type == SPEC_WIN16) fatal_error( "Cannot build 16-bit exe files\n" );
455 load_resources( argv, spec );
456 read_undef_symbols( argv );
457 BuildSpec32File( output_file, spec );
458 break;
459 case MODE_DEF:
460 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
461 if (spec->type == SPEC_WIN16) fatal_error( "Cannot yet build .def file for 16-bit dlls\n" );
462 if (!parse_input_file( spec )) break;
463 BuildDef32File( output_file, spec );
464 break;
465 case MODE_DEBUG:
466 BuildDebugFile( output_file, current_src_dir, argv );
467 break;
468 case MODE_RELAY16:
469 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
470 BuildRelays16( output_file );
471 break;
472 case MODE_RELAY32:
473 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
474 BuildRelays32( output_file );
475 break;
476 default:
477 usage(1);
478 break;
480 if (nb_errors) exit(1);
481 if (output_file_name)
483 fclose( output_file );
484 output_file_name = NULL;
486 return 0;