winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / tools / winebuild / main.c
blob72e13b7992bba1c95674dcfae58c4a4284d4cf77
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "build.h"
41 int UsePIC = 0;
42 int nb_errors = 0;
43 int display_warnings = 0;
44 int kill_at = 0;
45 int verbose = 0;
46 int link_ext_symbols = 0;
47 int force_pointer_size = 0;
48 int unwind_tables = 0;
49 int use_msvcrt = 0;
50 int unix_lib = 0;
51 int safe_seh = 0;
52 int prefer_native = 0;
54 #ifdef __i386__
55 enum target_cpu target_cpu = CPU_x86;
56 #elif defined(__x86_64__)
57 enum target_cpu target_cpu = CPU_x86_64;
58 #elif defined(__powerpc__)
59 enum target_cpu target_cpu = CPU_POWERPC;
60 #elif defined(__arm__)
61 enum target_cpu target_cpu = CPU_ARM;
62 #elif defined(__aarch64__)
63 enum target_cpu target_cpu = CPU_ARM64;
64 #else
65 #error Unsupported CPU
66 #endif
68 #ifdef __APPLE__
69 enum target_platform target_platform = PLATFORM_APPLE;
70 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
71 enum target_platform target_platform = PLATFORM_FREEBSD;
72 #elif defined(__sun)
73 enum target_platform target_platform = PLATFORM_SOLARIS;
74 #elif defined(_WIN32)
75 enum target_platform target_platform = PLATFORM_MINGW;
76 #else
77 enum target_platform target_platform = PLATFORM_UNSPECIFIED;
78 #endif
80 char *target_alias = NULL;
82 char *input_file_name = NULL;
83 char *spec_file_name = NULL;
84 FILE *output_file = NULL;
85 const char *output_file_name = NULL;
86 static int fake_module;
88 struct strarray lib_path = { 0 };
89 struct strarray tools_path = { 0 };
90 struct strarray as_command = { 0 };
91 struct strarray cc_command = { 0 };
92 struct strarray ld_command = { 0 };
93 struct strarray nm_command = { 0 };
94 char *cpu_option = NULL;
95 char *fpu_option = NULL;
96 char *arch_option = NULL;
97 #ifdef __SOFTFP__
98 const char *float_abi_option = "soft";
99 #else
100 const char *float_abi_option = "softfp";
101 #endif
103 #ifdef __thumb__
104 int thumb_mode = 1;
105 #else
106 int thumb_mode = 0;
107 #endif
109 static struct strarray res_files;
111 /* execution mode */
112 enum exec_mode_values
114 MODE_NONE,
115 MODE_DLL,
116 MODE_EXE,
117 MODE_DEF,
118 MODE_IMPLIB,
119 MODE_STATICLIB,
120 MODE_BUILTIN,
121 MODE_FIXUP_CTORS,
122 MODE_RESOURCES
125 static enum exec_mode_values exec_mode = MODE_NONE;
127 static const struct
129 const char *name;
130 enum target_platform platform;
131 } platform_names[] =
133 { "macos", PLATFORM_APPLE },
134 { "darwin", PLATFORM_APPLE },
135 { "freebsd", PLATFORM_FREEBSD },
136 { "solaris", PLATFORM_SOLARIS },
137 { "mingw32", PLATFORM_MINGW },
138 { "windows-gnu", PLATFORM_MINGW },
139 { "windows", PLATFORM_WINDOWS },
140 { "winnt", PLATFORM_MINGW }
143 /* set the dll file name from the input file name */
144 static void set_dll_file_name( const char *name, DLLSPEC *spec )
146 char *p;
148 if (spec->file_name) return;
150 if ((p = strrchr( name, '\\' ))) name = p + 1;
151 if ((p = strrchr( name, '/' ))) name = p + 1;
152 spec->file_name = xmalloc( strlen(name) + 5 );
153 strcpy( spec->file_name, name );
154 if ((p = strrchr( spec->file_name, '.' )))
156 if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
160 /* set the dll name from the file name */
161 static void init_dll_name( DLLSPEC *spec )
163 if (!spec->file_name && output_file_name)
165 char *p;
166 spec->file_name = xstrdup( output_file_name );
167 if ((p = strrchr( spec->file_name, '.' ))) *p = 0;
169 if (!spec->dll_name && spec->file_name) /* set default name from file name */
171 char *p;
172 spec->dll_name = xstrdup( spec->file_name );
173 if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
175 if (spec->dll_name) spec->c_name = make_c_identifier( spec->dll_name );
178 /* set the dll subsystem */
179 static void set_subsystem( const char *subsystem, DLLSPEC *spec )
181 char *major, *minor, *str = xstrdup( subsystem );
183 if ((major = strchr( str, ':' ))) *major++ = 0;
184 if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
185 else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
186 else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
187 else if (!strcmp( str, "wince" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CE_GUI;
188 else if (!strcmp( str, "win16" )) spec->type = SPEC_WIN16;
189 else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
190 if (major)
192 if ((minor = strchr( major, '.' )))
194 *minor++ = 0;
195 spec->subsystem_minor = atoi( minor );
197 spec->subsystem_major = atoi( major );
199 free( str );
202 /* set the target CPU and platform */
203 static void set_target( const char *target )
205 unsigned int i;
206 char *p, *spec = xstrdup( target );
208 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
210 target_alias = xstrdup( target );
212 /* get the CPU part */
214 if ((p = strchr( spec, '-' )))
216 int cpu;
218 *p++ = 0;
219 cpu = get_cpu_from_name( spec );
220 if (cpu == -1) fatal_error( "Unrecognized CPU '%s'\n", spec );
221 target_cpu = cpu;
223 else if (!strcmp( spec, "mingw32" ))
225 target_cpu = CPU_x86;
226 p = spec;
228 else
229 fatal_error( "Invalid target specification '%s'\n", target );
231 /* get the OS part */
233 target_platform = PLATFORM_UNSPECIFIED; /* default value */
234 for (;;)
236 for (i = 0; i < ARRAY_SIZE(platform_names); i++)
238 if (!strncmp( platform_names[i].name, p, strlen(platform_names[i].name) ))
240 target_platform = platform_names[i].platform;
241 break;
244 if (target_platform != PLATFORM_UNSPECIFIED || !(p = strchr( p, '-' ))) break;
245 p++;
248 free( spec );
250 if (target_cpu == CPU_ARM && is_pe()) thumb_mode = 1;
253 /* cleanup on program exit */
254 static void cleanup(void)
256 if (output_file_name) unlink( output_file_name );
259 /* clean things up when aborting on a signal */
260 static void exit_on_signal( int sig )
262 exit(1); /* this will call atexit functions */
265 /*******************************************************************
266 * command-line option handling
268 static const char usage_str[] =
269 "Usage: winebuild [OPTIONS] [FILES]\n\n"
270 "Options:\n"
271 " --as-cmd=AS Command to use for assembling (default: as)\n"
272 " -b, --target=TARGET Specify target CPU and platform for cross-compiling\n"
273 " -B PREFIX Look for build tools in the PREFIX directory\n"
274 " --cc-cmd=CC C compiler to use for assembling (default: fall back to --as-cmd)\n"
275 " -d, --delay-lib=LIB Import the specified library in delayed mode\n"
276 " -D SYM Ignored for C flags compatibility\n"
277 " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n"
278 " -E, --export=FILE Export the symbols defined in the .spec or .def file\n"
279 " --external-symbols Allow linking to external symbols\n"
280 " -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
281 " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
282 " --fake-module Create a fake binary module\n"
283 " -h, --help Display this help message\n"
284 " -H, --heap=SIZE Set the heap size for a Win16 dll\n"
285 " -I DIR Ignored for C flags compatibility\n"
286 " -k, --kill-at Kill stdcall decorations in generated .def files\n"
287 " -K, FLAGS Compiler flags (only -KPIC is supported)\n"
288 " --large-address-aware Support an address space larger than 2Gb\n"
289 " --ld-cmd=LD Command to use for linking (default: ld)\n"
290 " -l, --library=LIB Import the specified library\n"
291 " -L, --library-path=DIR Look for imports libraries in DIR\n"
292 " -m16, -m32, -m64 Force building 16-bit, 32-bit resp. 64-bit code\n"
293 " -M, --main-module=MODULE Set the name of the main module for a Win16 dll\n"
294 " --nm-cmd=NM Command to use to get undefined symbols (default: nm)\n"
295 " --nxcompat=y|n Set the NX compatibility flag (default: yes)\n"
296 " -N, --dll-name=DLLNAME Set the DLL name (default: from input file name)\n"
297 " -o, --output=NAME Set the output file name (default: stdout)\n"
298 " --prefer-native Set the flag to prefer loading native at run time\n"
299 " -r, --res=RSRC.RES Load resources from RSRC.RES\n"
300 " --safeseh Mark object files as SEH compatible\n"
301 " --save-temps Do not delete the generated intermediate files\n"
302 " --subsystem=SUBSYS Set the subsystem (one of native, windows, console, wince)\n"
303 " -u, --undefined=SYMBOL Add an undefined reference to SYMBOL when linking\n"
304 " -v, --verbose Display the programs invoked\n"
305 " --version Print the version and exit\n"
306 " -w, --warnings Turn on warnings\n"
307 "\nMode options:\n"
308 " --dll Build a library from a .spec file and object files\n"
309 " --def Build a .def file from a .spec file\n"
310 " --exe Build an executable from object files\n"
311 " --implib Build an import library\n"
312 " --staticlib Build a static library\n"
313 " --resources Build a .o or .res file for the resource files\n\n"
314 " --builtin Mark a library as a Wine builtin\n"
315 " --fixup-ctors Fixup the constructors data after the module has been built\n"
316 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
318 enum long_options_values
320 LONG_OPT_DLL = 1,
321 LONG_OPT_DEF,
322 LONG_OPT_EXE,
323 LONG_OPT_IMPLIB,
324 LONG_OPT_BUILTIN,
325 LONG_OPT_ASCMD,
326 LONG_OPT_CCCMD,
327 LONG_OPT_EXTERNAL_SYMS,
328 LONG_OPT_FAKE_MODULE,
329 LONG_OPT_FIXUP_CTORS,
330 LONG_OPT_LARGE_ADDRESS_AWARE,
331 LONG_OPT_LDCMD,
332 LONG_OPT_NMCMD,
333 LONG_OPT_NXCOMPAT,
334 LONG_OPT_PREFER_NATIVE,
335 LONG_OPT_RESOURCES,
336 LONG_OPT_SAFE_SEH,
337 LONG_OPT_SAVE_TEMPS,
338 LONG_OPT_STATICLIB,
339 LONG_OPT_SUBSYSTEM,
340 LONG_OPT_VERSION
343 static const char short_options[] = "B:C:D:E:F:H:I:K:L:M:N:b:d:e:f:hkl:m:o:r:u:vw";
345 static const struct option long_options[] =
347 { "dll", 0, 0, LONG_OPT_DLL },
348 { "def", 0, 0, LONG_OPT_DEF },
349 { "exe", 0, 0, LONG_OPT_EXE },
350 { "implib", 0, 0, LONG_OPT_IMPLIB },
351 { "staticlib", 0, 0, LONG_OPT_STATICLIB },
352 { "builtin", 0, 0, LONG_OPT_BUILTIN },
353 { "as-cmd", 1, 0, LONG_OPT_ASCMD },
354 { "cc-cmd", 1, 0, LONG_OPT_CCCMD },
355 { "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS },
356 { "fake-module", 0, 0, LONG_OPT_FAKE_MODULE },
357 { "fixup-ctors", 0, 0, LONG_OPT_FIXUP_CTORS },
358 { "large-address-aware", 0, 0, LONG_OPT_LARGE_ADDRESS_AWARE },
359 { "ld-cmd", 1, 0, LONG_OPT_LDCMD },
360 { "nm-cmd", 1, 0, LONG_OPT_NMCMD },
361 { "nxcompat", 1, 0, LONG_OPT_NXCOMPAT },
362 { "prefer-native", 0, 0, LONG_OPT_PREFER_NATIVE },
363 { "resources", 0, 0, LONG_OPT_RESOURCES },
364 { "safeseh", 0, 0, LONG_OPT_SAFE_SEH },
365 { "save-temps", 0, 0, LONG_OPT_SAVE_TEMPS },
366 { "subsystem", 1, 0, LONG_OPT_SUBSYSTEM },
367 { "version", 0, 0, LONG_OPT_VERSION },
368 /* aliases for short options */
369 { "target", 1, 0, 'b' },
370 { "delay-lib", 1, 0, 'd' },
371 { "export", 1, 0, 'E' },
372 { "entry", 1, 0, 'e' },
373 { "filename", 1, 0, 'F' },
374 { "help", 0, 0, 'h' },
375 { "heap", 1, 0, 'H' },
376 { "kill-at", 0, 0, 'k' },
377 { "library", 1, 0, 'l' },
378 { "library-path", 1, 0, 'L' },
379 { "main-module", 1, 0, 'M' },
380 { "dll-name", 1, 0, 'N' },
381 { "output", 1, 0, 'o' },
382 { "res", 1, 0, 'r' },
383 { "undefined", 1, 0, 'u' },
384 { "verbose", 0, 0, 'v' },
385 { "warnings", 0, 0, 'w' },
386 { NULL, 0, 0, 0 }
389 static void usage( int exit_code )
391 fprintf( stderr, "%s", usage_str );
392 exit( exit_code );
395 static void set_exec_mode( enum exec_mode_values mode )
397 if (exec_mode != MODE_NONE) usage(1);
398 exec_mode = mode;
401 /* get the default entry point for a given spec file */
402 static const char *get_default_entry_point( const DLLSPEC *spec )
404 if (spec->characteristics & IMAGE_FILE_DLL) return "DllMain";
405 if (spec->subsystem == IMAGE_SUBSYSTEM_NATIVE) return "DriverEntry";
406 if (spec->type == SPEC_WIN16)
408 add_spec_extra_ld_symbol("WinMain16");
409 return "__wine_spec_exe16_entry";
411 else if (spec->unicode_app)
413 /* __wine_spec_exe_wentry always calls wmain */
414 add_spec_extra_ld_symbol("wmain");
415 if (spec->subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
416 add_spec_extra_ld_symbol("wWinMain");
417 return "__wine_spec_exe_wentry";
419 else
421 /* __wine_spec_exe_entry always calls main */
422 add_spec_extra_ld_symbol("main");
423 if (spec->subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
424 add_spec_extra_ld_symbol("WinMain");
425 return "__wine_spec_exe_entry";
429 /* parse options from the argv array and remove all the recognized ones */
430 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
432 char *p;
433 int optc;
434 int save_temps = 0;
436 while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
438 switch(optc)
440 case 'B':
441 strarray_add( &tools_path, xstrdup( optarg ), NULL );
442 break;
443 case 'D':
444 /* ignored */
445 break;
446 case 'E':
447 spec_file_name = xstrdup( optarg );
448 set_dll_file_name( optarg, spec );
449 break;
450 case 'F':
451 spec->file_name = xstrdup( optarg );
452 break;
453 case 'H':
454 if (!isdigit(optarg[0]))
455 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
456 spec->heap_size = atoi(optarg);
457 if (spec->heap_size > 65535)
458 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
459 break;
460 case 'I':
461 /* ignored */
462 break;
463 case 'K':
464 /* ignored, because cc generates correct code. */
465 break;
466 case 'L':
467 strarray_add( &lib_path, xstrdup( optarg ), NULL );
468 break;
469 case 'm':
470 if (!strcmp( optarg, "16" )) spec->type = SPEC_WIN16;
471 else if (!strcmp( optarg, "32" )) force_pointer_size = 4;
472 else if (!strcmp( optarg, "64" )) force_pointer_size = 8;
473 else if (!strcmp( optarg, "arm" )) thumb_mode = 0;
474 else if (!strcmp( optarg, "thumb" )) thumb_mode = 1;
475 else if (!strcmp( optarg, "no-cygwin" )) use_msvcrt = 1;
476 else if (!strcmp( optarg, "unix" )) unix_lib = 1;
477 else if (!strcmp( optarg, "unicode" )) spec->unicode_app = 1;
478 else if (!strncmp( optarg, "cpu=", 4 )) cpu_option = xstrdup( optarg + 4 );
479 else if (!strncmp( optarg, "fpu=", 4 )) fpu_option = xstrdup( optarg + 4 );
480 else if (!strncmp( optarg, "arch=", 5 )) arch_option = xstrdup( optarg + 5 );
481 else if (!strncmp( optarg, "float-abi=", 10 )) float_abi_option = xstrdup( optarg + 10 );
482 else fatal_error( "Unknown -m option '%s'\n", optarg );
483 break;
484 case 'M':
485 spec->main_module = xstrdup( optarg );
486 break;
487 case 'N':
488 spec->dll_name = xstrdup( optarg );
489 break;
490 case 'b':
491 set_target( optarg );
492 break;
493 case 'd':
494 add_delayed_import( optarg );
495 break;
496 case 'e':
497 spec->init_func = xstrdup( optarg );
498 if ((p = strchr( spec->init_func, '@' ))) *p = 0; /* kill stdcall decoration */
499 break;
500 case 'f':
501 if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
502 else if (!strcmp( optarg, "asynchronous-unwind-tables")) unwind_tables = 1;
503 else if (!strcmp( optarg, "no-asynchronous-unwind-tables")) unwind_tables = 0;
504 /* ignore all other flags */
505 break;
506 case 'h':
507 usage(0);
508 break;
509 case 'k':
510 kill_at = 1;
511 break;
512 case 'l':
513 add_import_dll( optarg, NULL );
514 break;
515 case 'o':
516 if (unlink( optarg ) == -1 && errno != ENOENT)
517 fatal_error( "Unable to create output file '%s'\n", optarg );
518 output_file_name = xstrdup( optarg );
519 break;
520 case 'r':
521 strarray_add( &res_files, xstrdup( optarg ), NULL );
522 break;
523 case 'u':
524 add_extra_ld_symbol( optarg );
525 break;
526 case 'v':
527 verbose++;
528 break;
529 case 'w':
530 display_warnings = 1;
531 break;
532 case LONG_OPT_DLL:
533 set_exec_mode( MODE_DLL );
534 break;
535 case LONG_OPT_DEF:
536 set_exec_mode( MODE_DEF );
537 break;
538 case LONG_OPT_EXE:
539 set_exec_mode( MODE_EXE );
540 if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
541 break;
542 case LONG_OPT_IMPLIB:
543 set_exec_mode( MODE_IMPLIB );
544 break;
545 case LONG_OPT_STATICLIB:
546 set_exec_mode( MODE_STATICLIB );
547 break;
548 case LONG_OPT_BUILTIN:
549 set_exec_mode( MODE_BUILTIN );
550 break;
551 case LONG_OPT_FIXUP_CTORS:
552 set_exec_mode( MODE_FIXUP_CTORS );
553 break;
554 case LONG_OPT_ASCMD:
555 as_command = strarray_fromstring( optarg, " " );
556 break;
557 case LONG_OPT_CCCMD:
558 cc_command = strarray_fromstring( optarg, " " );
559 break;
560 case LONG_OPT_FAKE_MODULE:
561 fake_module = 1;
562 break;
563 case LONG_OPT_EXTERNAL_SYMS:
564 link_ext_symbols = 1;
565 break;
566 case LONG_OPT_LARGE_ADDRESS_AWARE:
567 spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
568 break;
569 case LONG_OPT_LDCMD:
570 ld_command = strarray_fromstring( optarg, " " );
571 break;
572 case LONG_OPT_NMCMD:
573 nm_command = strarray_fromstring( optarg, " " );
574 break;
575 case LONG_OPT_NXCOMPAT:
576 if (optarg[0] == 'n' || optarg[0] == 'N')
577 spec->dll_characteristics &= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
578 break;
579 case LONG_OPT_SAFE_SEH:
580 safe_seh = 1;
581 break;
582 case LONG_OPT_PREFER_NATIVE:
583 prefer_native = 1;
584 spec->dll_characteristics |= IMAGE_DLLCHARACTERISTICS_PREFER_NATIVE;
585 break;
586 case LONG_OPT_RESOURCES:
587 set_exec_mode( MODE_RESOURCES );
588 break;
589 case LONG_OPT_SAVE_TEMPS:
590 save_temps = 1;
591 break;
592 case LONG_OPT_SUBSYSTEM:
593 set_subsystem( optarg, spec );
594 break;
595 case LONG_OPT_VERSION:
596 printf( "winebuild version " PACKAGE_VERSION "\n" );
597 exit(0);
598 case '?':
599 usage(1);
600 break;
604 if (!save_temps) atexit( cleanup_tmp_files );
606 if (spec->file_name && !strchr( spec->file_name, '.' ))
607 strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
608 init_dll_name( spec );
610 switch (target_cpu)
612 case CPU_x86:
613 if (force_pointer_size == 8) target_cpu = CPU_x86_64;
614 break;
615 case CPU_x86_64:
616 if (force_pointer_size == 4) target_cpu = CPU_x86;
617 break;
618 default:
619 if (force_pointer_size == 8)
620 fatal_error( "Cannot build 64-bit code for this CPU\n" );
621 break;
624 return &argv[optind];
628 /* load all specified resource files */
629 static void load_resources( char *argv[], DLLSPEC *spec )
631 int i;
632 char **ptr, **last;
634 switch (spec->type)
636 case SPEC_WIN16:
637 for (i = 0; i < res_files.count; i++) load_res16_file( res_files.str[i], spec );
638 break;
640 case SPEC_WIN32:
641 for (i = 0; i < res_files.count; i++)
643 if (!load_res32_file( res_files.str[i], spec ))
644 fatal_error( "%s is not a valid Win32 resource file\n", res_files.str[i] );
647 /* load any resource file found in the remaining arguments */
648 for (ptr = last = argv; *ptr; ptr++)
650 if (!load_res32_file( *ptr, spec ))
651 *last++ = *ptr; /* not a resource file, keep it in the list */
653 *last = NULL;
654 break;
658 /* add input files that look like import libs to the import list */
659 static void load_import_libs( char *argv[] )
661 char **ptr, **last;
663 for (ptr = last = argv; *ptr; ptr++)
665 if (strendswith( *ptr, ".def" ))
666 add_import_dll( NULL, *ptr );
667 else
668 *last++ = *ptr; /* not an import dll, keep it in the list */
670 *last = NULL;
673 static int parse_input_file( DLLSPEC *spec )
675 FILE *input_file = open_input_file( NULL, spec_file_name );
676 char *extension = strrchr( spec_file_name, '.' );
677 int result;
679 spec->src_name = xstrdup( input_file_name );
680 if (extension && !strcmp( extension, ".def" ))
681 result = parse_def_file( input_file, spec );
682 else
683 result = parse_spec_file( input_file, spec );
684 close_input_file( input_file );
685 return result;
689 /*******************************************************************
690 * main
692 int main(int argc, char **argv)
694 DLLSPEC *spec = alloc_dll_spec();
696 #ifdef SIGHUP
697 signal( SIGHUP, exit_on_signal );
698 #endif
699 signal( SIGTERM, exit_on_signal );
700 signal( SIGINT, exit_on_signal );
702 argv = parse_options( argc, argv, spec );
703 atexit( cleanup ); /* make sure we remove the output file on exit */
705 switch(exec_mode)
707 case MODE_DLL:
708 if (spec->subsystem != IMAGE_SUBSYSTEM_NATIVE)
709 spec->characteristics |= IMAGE_FILE_DLL;
710 /* fall through */
711 case MODE_EXE:
712 load_resources( argv, spec );
713 if (spec_file_name && !parse_input_file( spec )) break;
714 if (!spec->init_func && !unix_lib) spec->init_func = xstrdup( get_default_entry_point( spec ));
716 if (fake_module)
718 if (spec->type == SPEC_WIN16) output_fake_module16( spec );
719 else output_fake_module( spec );
720 break;
722 if (!is_pe())
724 load_import_libs( argv );
725 read_undef_symbols( spec, argv );
726 resolve_imports( spec );
728 if (spec->type == SPEC_WIN16) output_spec16_file( spec );
729 else output_spec32_file( spec );
730 break;
731 case MODE_DEF:
732 if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
733 if (!spec_file_name) fatal_error( "missing .spec file\n" );
734 if (!parse_input_file( spec )) break;
735 open_output_file();
736 output_def_file( spec, 0 );
737 close_output_file();
738 break;
739 case MODE_IMPLIB:
740 if (!spec_file_name) fatal_error( "missing .spec file\n" );
741 if (!parse_input_file( spec )) break;
742 output_static_lib( spec, argv );
743 break;
744 case MODE_STATICLIB:
745 output_static_lib( NULL, argv );
746 break;
747 case MODE_BUILTIN:
748 if (!argv[0]) fatal_error( "missing file argument for --builtin option\n" );
749 make_builtin_files( argv );
750 break;
751 case MODE_FIXUP_CTORS:
752 if (!argv[0]) fatal_error( "missing file argument for --fixup-ctors option\n" );
753 fixup_constructors( argv );
754 break;
755 case MODE_RESOURCES:
756 load_resources( argv, spec );
757 output_res_o_file( spec );
758 break;
759 default:
760 usage(1);
761 break;
763 if (nb_errors) exit(1);
764 output_file_name = NULL;
765 return 0;