iphlpapi: Fix HRESULT/win32 error code confusion.
[wine] / tools / winebuild / main.c
1 /*
2  * Main function
3  *
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  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
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
38
39 #include "build.h"
40
41 int UsePIC = 0;
42 int nb_lib_paths = 0;
43 int nb_errors = 0;
44 int display_warnings = 0;
45 int kill_at = 0;
46 int verbose = 0;
47 int save_temps = 0;
48 int link_ext_symbols = 0;
49 int force_pointer_size = 0;
50 int unwind_tables = 0;
51
52 #ifdef __i386__
53 enum target_cpu target_cpu = CPU_x86;
54 #elif defined(__x86_64__)
55 enum target_cpu target_cpu = CPU_x86_64;
56 #elif defined(__sparc__)
57 enum target_cpu target_cpu = CPU_SPARC;
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
67
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_WINDOWS;
76 #else
77 enum target_platform target_platform = PLATFORM_UNSPECIFIED;
78 #endif
79
80 char *target_alias = NULL;
81 char **lib_path = NULL;
82
83 char *input_file_name = NULL;
84 char *spec_file_name = NULL;
85 FILE *output_file = NULL;
86 const char *output_file_name = NULL;
87 static const char *output_file_source_name;
88 static int fake_module;
89
90 char *as_command = NULL;
91 char *ld_command = NULL;
92 char *nm_command = NULL;
93 char *cpu_option = NULL;
94
95 static int nb_res_files;
96 static char **res_files;
97
98 /* execution mode */
99 enum exec_mode_values
100 {
101     MODE_NONE,
102     MODE_DLL,
103     MODE_EXE,
104     MODE_DEF,
105     MODE_IMPLIB,
106     MODE_RESOURCES
107 };
108
109 static enum exec_mode_values exec_mode = MODE_NONE;
110
111 static const struct
112 {
113     const char *name;
114     enum target_platform platform;
115 } platform_names[] =
116 {
117     { "macos",   PLATFORM_APPLE },
118     { "darwin",  PLATFORM_APPLE },
119     { "freebsd", PLATFORM_FREEBSD },
120     { "solaris", PLATFORM_SOLARIS },
121     { "mingw32", PLATFORM_WINDOWS },
122     { "windows", PLATFORM_WINDOWS },
123     { "winnt",   PLATFORM_WINDOWS }
124 };
125
126 /* set the dll file name from the input file name */
127 static void set_dll_file_name( const char *name, DLLSPEC *spec )
128 {
129     char *p;
130
131     if (spec->file_name) return;
132
133     if ((p = strrchr( name, '\\' ))) name = p + 1;
134     if ((p = strrchr( name, '/' ))) name = p + 1;
135     spec->file_name = xmalloc( strlen(name) + 5 );
136     strcpy( spec->file_name, name );
137     if ((p = strrchr( spec->file_name, '.' )))
138     {
139         if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
140     }
141 }
142
143 /* set the dll name from the file name */
144 static void init_dll_name( DLLSPEC *spec )
145 {
146     if (!spec->file_name && output_file_name)
147     {
148         char *p;
149         spec->file_name = xstrdup( output_file_name );
150         if ((p = strrchr( spec->file_name, '.' ))) *p = 0;
151     }
152     if (!spec->dll_name && spec->file_name)  /* set default name from file name */
153     {
154         char *p;
155         spec->dll_name = xstrdup( spec->file_name );
156         if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
157     }
158 }
159
160 /* set the dll subsystem */
161 static void set_subsystem( const char *subsystem, DLLSPEC *spec )
162 {
163     char *major, *minor, *str = xstrdup( subsystem );
164
165     if ((major = strchr( str, ':' ))) *major++ = 0;
166     if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
167     else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
168     else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
169     else if (!strcmp( str, "wince" ))   spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CE_GUI;
170     else if (!strcmp( str, "win16" )) spec->type = SPEC_WIN16;
171     else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
172     if (major)
173     {
174         if ((minor = strchr( major, '.' )))
175         {
176             *minor++ = 0;
177             spec->subsystem_minor = atoi( minor );
178         }
179         spec->subsystem_major = atoi( major );
180     }
181     free( str );
182 }
183
184 /* set the target CPU and platform */
185 static void set_target( const char *target )
186 {
187     unsigned int i;
188     char *p, *platform, *spec = xstrdup( target );
189
190     /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
191
192     target_alias = xstrdup( target );
193
194     /* get the CPU part */
195
196     if ((p = strchr( spec, '-' )))
197     {
198         *p++ = 0;
199         if ((target_cpu = get_cpu_from_name( spec )) == -1)
200             fatal_error( "Unrecognized CPU '%s'\n", spec );
201         platform = p;
202         if ((p = strrchr( p, '-' ))) platform = p + 1;
203     }
204     else if (!strcmp( spec, "mingw32" ))
205     {
206         target_cpu = CPU_x86;
207         platform = spec;
208     }
209     else
210         fatal_error( "Invalid target specification '%s'\n", target );
211
212     /* get the OS part */
213
214     target_platform = PLATFORM_UNSPECIFIED;  /* default value */
215     for (i = 0; i < sizeof(platform_names)/sizeof(platform_names[0]); i++)
216     {
217         if (!strncmp( platform_names[i].name, platform, strlen(platform_names[i].name) ))
218         {
219             target_platform = platform_names[i].platform;
220             break;
221         }
222     }
223
224     free( spec );
225 }
226
227 /* cleanup on program exit */
228 static void cleanup(void)
229 {
230     if (output_file_name) unlink( output_file_name );
231 }
232
233 /* clean things up when aborting on a signal */
234 static void exit_on_signal( int sig )
235 {
236     exit(1);  /* this will call atexit functions */
237 }
238
239 /*******************************************************************
240  *         command-line option handling
241  */
242 static const char usage_str[] =
243 "Usage: winebuild [OPTIONS] [FILES]\n\n"
244 "Options:\n"
245 "       --as-cmd=AS           Command to use for assembling (default: as)\n"
246 "   -b, --target=TARGET       Specify target CPU and platform for cross-compiling\n"
247 "   -d, --delay-lib=LIB       Import the specified library in delayed mode\n"
248 "   -D SYM                    Ignored for C flags compatibility\n"
249 "   -e, --entry=FUNC          Set the DLL entry point function (default: DllMain)\n"
250 "   -E, --export=FILE         Export the symbols defined in the .spec or .def file\n"
251 "       --external-symbols    Allow linking to external symbols\n"
252 "   -f FLAGS                  Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
253 "   -F, --filename=DLLFILE    Set the DLL filename (default: from input file name)\n"
254 "       --fake-module         Create a fake binary module\n"
255 "   -h, --help                Display this help message\n"
256 "   -H, --heap=SIZE           Set the heap size for a Win16 dll\n"
257 "   -I DIR                    Ignored for C flags compatibility\n"
258 "   -k, --kill-at             Kill stdcall decorations in generated .def files\n"
259 "   -K, FLAGS                 Compiler flags (only -KPIC is supported)\n"
260 "       --large-address-aware Support an address space larger than 2Gb\n"
261 "       --ld-cmd=LD           Command to use for linking (default: ld)\n"
262 "   -l, --library=LIB         Import the specified library\n"
263 "   -L, --library-path=DIR    Look for imports libraries in DIR\n"
264 "   -m16, -m32, -m64          Force building 16-bit, 32-bit resp. 64-bit code\n"
265 "   -M, --main-module=MODULE  Set the name of the main module for a Win16 dll\n"
266 "       --nm-cmd=NM           Command to use to get undefined symbols (default: nm)\n"
267 "       --nxcompat=y|n        Set the NX compatibility flag (default: yes)\n"
268 "   -N, --dll-name=DLLNAME    Set the DLL name (default: from input file name)\n"
269 "   -o, --output=NAME         Set the output file name (default: stdout)\n"
270 "   -r, --res=RSRC.RES        Load resources from RSRC.RES\n"
271 "       --save-temps          Do not delete the generated intermediate files\n"
272 "       --subsystem=SUBSYS    Set the subsystem (one of native, windows, console, wince)\n"
273 "   -u, --undefined=SYMBOL    Add an undefined reference to SYMBOL when linking\n"
274 "   -v, --verbose             Display the programs invoked\n"
275 "       --version             Print the version and exit\n"
276 "   -w, --warnings            Turn on warnings\n"
277 "\nMode options:\n"
278 "       --dll                 Build a .c file from a .spec or .def file\n"
279 "       --def                 Build a .def file from a .spec file\n"
280 "       --exe                 Build a .c file for an executable\n"
281 "       --implib              Build an import library\n"
282 "       --resources           Build a .o file for the resource files\n\n"
283 "The mode options are mutually exclusive; you must specify one and only one.\n\n";
284
285 enum long_options_values
286 {
287     LONG_OPT_DLL = 1,
288     LONG_OPT_DEF,
289     LONG_OPT_EXE,
290     LONG_OPT_IMPLIB,
291     LONG_OPT_ASCMD,
292     LONG_OPT_EXTERNAL_SYMS,
293     LONG_OPT_FAKE_MODULE,
294     LONG_OPT_LARGE_ADDRESS_AWARE,
295     LONG_OPT_LDCMD,
296     LONG_OPT_NMCMD,
297     LONG_OPT_NXCOMPAT,
298     LONG_OPT_RESOURCES,
299     LONG_OPT_SAVE_TEMPS,
300     LONG_OPT_SUBSYSTEM,
301     LONG_OPT_VERSION
302 };
303
304 static const char short_options[] = "C:D:E:F:H:I:K:L:M:N:b:d:e:f:hkl:m:o:r:u:vw";
305
306 static const struct option long_options[] =
307 {
308     { "dll",           0, 0, LONG_OPT_DLL },
309     { "def",           0, 0, LONG_OPT_DEF },
310     { "exe",           0, 0, LONG_OPT_EXE },
311     { "implib",        0, 0, LONG_OPT_IMPLIB },
312     { "as-cmd",        1, 0, LONG_OPT_ASCMD },
313     { "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS },
314     { "fake-module",   0, 0, LONG_OPT_FAKE_MODULE },
315     { "large-address-aware", 0, 0, LONG_OPT_LARGE_ADDRESS_AWARE },
316     { "ld-cmd",        1, 0, LONG_OPT_LDCMD },
317     { "nm-cmd",        1, 0, LONG_OPT_NMCMD },
318     { "nxcompat",      1, 0, LONG_OPT_NXCOMPAT },
319     { "resources",     0, 0, LONG_OPT_RESOURCES },
320     { "save-temps",    0, 0, LONG_OPT_SAVE_TEMPS },
321     { "subsystem",     1, 0, LONG_OPT_SUBSYSTEM },
322     { "version",       0, 0, LONG_OPT_VERSION },
323     /* aliases for short options */
324     { "target",        1, 0, 'b' },
325     { "delay-lib",     1, 0, 'd' },
326     { "export",        1, 0, 'E' },
327     { "entry",         1, 0, 'e' },
328     { "filename",      1, 0, 'F' },
329     { "help",          0, 0, 'h' },
330     { "heap",          1, 0, 'H' },
331     { "kill-at",       0, 0, 'k' },
332     { "library",       1, 0, 'l' },
333     { "library-path",  1, 0, 'L' },
334     { "main-module",   1, 0, 'M' },
335     { "dll-name",      1, 0, 'N' },
336     { "output",        1, 0, 'o' },
337     { "res",           1, 0, 'r' },
338     { "undefined",     1, 0, 'u' },
339     { "verbose",       0, 0, 'v' },
340     { "warnings",      0, 0, 'w' },
341     { NULL,            0, 0, 0 }
342 };
343
344 static void usage( int exit_code )
345 {
346     fprintf( stderr, "%s", usage_str );
347     exit( exit_code );
348 }
349
350 static void set_exec_mode( enum exec_mode_values mode )
351 {
352     if (exec_mode != MODE_NONE) usage(1);
353     exec_mode = mode;
354 }
355
356 /* parse options from the argv array and remove all the recognized ones */
357 static char **parse_options( int argc, char **argv, DLLSPEC *spec )
358 {
359     char *p;
360     int optc;
361
362     while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
363     {
364         switch(optc)
365         {
366         case 'D':
367             /* ignored */
368             break;
369         case 'E':
370             spec_file_name = xstrdup( optarg );
371             set_dll_file_name( optarg, spec );
372             break;
373         case 'F':
374             spec->file_name = xstrdup( optarg );
375             break;
376         case 'H':
377             if (!isdigit(optarg[0]))
378                 fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
379             spec->heap_size = atoi(optarg);
380             if (spec->heap_size > 65535)
381                 fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
382             break;
383         case 'I':
384             /* ignored */
385             break;
386         case 'K':
387             /* ignored, because cc generates correct code. */
388             break;
389         case 'L':
390             lib_path = xrealloc( lib_path, (nb_lib_paths+1) * sizeof(*lib_path) );
391             lib_path[nb_lib_paths++] = xstrdup( optarg );
392             break;
393         case 'm':
394             if (!strcmp( optarg, "16" )) spec->type = SPEC_WIN16;
395             else if (!strcmp( optarg, "32" )) force_pointer_size = 4;
396             else if (!strcmp( optarg, "64" )) force_pointer_size = 8;
397             else if (!strncmp( optarg, "cpu=", 4 )) cpu_option = xstrdup( optarg + 4 );
398             else fatal_error( "Invalid -m option '%s', expected -m16, -m32, -m64 or -mcpu\n", optarg );
399             break;
400         case 'M':
401             spec->main_module = xstrdup( optarg );
402             break;
403         case 'N':
404             spec->dll_name = xstrdup( optarg );
405             break;
406         case 'b':
407             set_target( optarg );
408             break;
409         case 'd':
410             add_delayed_import( optarg );
411             break;
412         case 'e':
413             spec->init_func = xstrdup( optarg );
414             if ((p = strchr( spec->init_func, '@' ))) *p = 0;  /* kill stdcall decoration */
415             break;
416         case 'f':
417             if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
418             else if (!strcmp( optarg, "asynchronous-unwind-tables")) unwind_tables = 1;
419             else if (!strcmp( optarg, "no-asynchronous-unwind-tables")) unwind_tables = 0;
420             /* ignore all other flags */
421             break;
422         case 'h':
423             usage(0);
424             break;
425         case 'k':
426             kill_at = 1;
427             break;
428         case 'l':
429             add_import_dll( optarg, NULL );
430             break;
431         case 'o':
432             {
433                 char *ext = strrchr( optarg, '.' );
434
435                 if (unlink( optarg ) == -1 && errno != ENOENT)
436                     fatal_error( "Unable to create output file '%s'\n", optarg );
437                 if (ext && !strcmp( ext, ".o" ))
438                 {
439                     output_file_source_name = get_temp_file_name( optarg, ".s" );
440                     if (!(output_file = fopen( output_file_source_name, "w" )))
441                         fatal_error( "Unable to create output file '%s'\n", optarg );
442                 }
443                 else
444                 {
445                     if (!(output_file = fopen( optarg, "w" )))
446                         fatal_error( "Unable to create output file '%s'\n", optarg );
447                 }
448                 output_file_name = xstrdup(optarg);
449                 atexit( cleanup );  /* make sure we remove the output file on exit */
450             }
451             break;
452         case 'r':
453             res_files = xrealloc( res_files, (nb_res_files+1) * sizeof(*res_files) );
454             res_files[nb_res_files++] = xstrdup( optarg );
455             break;
456         case 'u':
457             add_extra_ld_symbol( optarg );
458             break;
459         case 'v':
460             verbose++;
461             break;
462         case 'w':
463             display_warnings = 1;
464             break;
465         case LONG_OPT_DLL:
466             set_exec_mode( MODE_DLL );
467             break;
468         case LONG_OPT_DEF:
469             set_exec_mode( MODE_DEF );
470             break;
471         case LONG_OPT_EXE:
472             set_exec_mode( MODE_EXE );
473             if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
474             break;
475         case LONG_OPT_IMPLIB:
476             set_exec_mode( MODE_IMPLIB );
477             break;
478         case LONG_OPT_ASCMD:
479             as_command = xstrdup( optarg );
480             break;
481         case LONG_OPT_FAKE_MODULE:
482             fake_module = 1;
483             break;
484         case LONG_OPT_EXTERNAL_SYMS:
485             link_ext_symbols = 1;
486             break;
487         case LONG_OPT_LARGE_ADDRESS_AWARE:
488             spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
489             break;
490         case LONG_OPT_LDCMD:
491             ld_command = xstrdup( optarg );
492             break;
493         case LONG_OPT_NMCMD:
494             nm_command = xstrdup( optarg );
495             break;
496         case LONG_OPT_NXCOMPAT:
497             if (optarg[0] == 'n' || optarg[0] == 'N')
498                 spec->dll_characteristics &= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
499             break;
500         case LONG_OPT_RESOURCES:
501             set_exec_mode( MODE_RESOURCES );
502             break;
503         case LONG_OPT_SAVE_TEMPS:
504             save_temps = 1;
505             break;
506         case LONG_OPT_SUBSYSTEM:
507             set_subsystem( optarg, spec );
508             break;
509         case LONG_OPT_VERSION:
510             printf( "winebuild version " PACKAGE_VERSION "\n" );
511             exit(0);
512         case '?':
513             usage(1);
514             break;
515         }
516     }
517
518     if (spec->file_name && !strchr( spec->file_name, '.' ))
519         strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
520     init_dll_name( spec );
521
522     switch (target_cpu)
523     {
524     case CPU_x86:
525         if (force_pointer_size == 8) target_cpu = CPU_x86_64;
526         break;
527     case CPU_x86_64:
528         if (force_pointer_size == 4) target_cpu = CPU_x86;
529         break;
530     default:
531         if (force_pointer_size == 8)
532             fatal_error( "Cannot build 64-bit code for this CPU\n" );
533         break;
534     }
535
536     return &argv[optind];
537 }
538
539
540 /* load all specified resource files */
541 static void load_resources( char *argv[], DLLSPEC *spec )
542 {
543     int i;
544     char **ptr, **last;
545
546     switch (spec->type)
547     {
548     case SPEC_WIN16:
549         for (i = 0; i < nb_res_files; i++) load_res16_file( res_files[i], spec );
550         break;
551
552     case SPEC_WIN32:
553         for (i = 0; i < nb_res_files; i++)
554         {
555             if (!load_res32_file( res_files[i], spec ))
556                 fatal_error( "%s is not a valid Win32 resource file\n", res_files[i] );
557         }
558
559         /* load any resource file found in the remaining arguments */
560         for (ptr = last = argv; *ptr; ptr++)
561         {
562             if (!load_res32_file( *ptr, spec ))
563                 *last++ = *ptr; /* not a resource file, keep it in the list */
564         }
565         *last = NULL;
566         break;
567     }
568 }
569
570 /* add input files that look like import libs to the import list */
571 static void load_import_libs( char *argv[] )
572 {
573     char **ptr, **last;
574
575     for (ptr = last = argv; *ptr; ptr++)
576     {
577         if (strendswith( *ptr, ".def" ))
578             add_import_dll( NULL, *ptr );
579         else
580             *last++ = *ptr; /* not an import dll, keep it in the list */
581     }
582     *last = NULL;
583 }
584
585 static int parse_input_file( DLLSPEC *spec )
586 {
587     FILE *input_file = open_input_file( NULL, spec_file_name );
588     char *extension = strrchr( spec_file_name, '.' );
589     int result;
590
591     spec->src_name = xstrdup( input_file_name );
592     if (extension && !strcmp( extension, ".def" ))
593         result = parse_def_file( input_file, spec );
594     else
595         result = parse_spec_file( input_file, spec );
596     close_input_file( input_file );
597     return result;
598 }
599
600
601 /*******************************************************************
602  *         main
603  */
604 int main(int argc, char **argv)
605 {
606     DLLSPEC *spec = alloc_dll_spec();
607
608 #ifdef SIGHUP
609     signal( SIGHUP, exit_on_signal );
610 #endif
611     signal( SIGTERM, exit_on_signal );
612     signal( SIGINT, exit_on_signal );
613
614     output_file = stdout;
615     argv = parse_options( argc, argv, spec );
616
617     switch(exec_mode)
618     {
619     case MODE_DLL:
620         if (spec->subsystem != IMAGE_SUBSYSTEM_NATIVE)
621             spec->characteristics |= IMAGE_FILE_DLL;
622         /* fall through */
623     case MODE_EXE:
624         load_resources( argv, spec );
625         load_import_libs( argv );
626         if (spec_file_name && !parse_input_file( spec )) break;
627         if (fake_module)
628         {
629             if (spec->type == SPEC_WIN16) output_fake_module16( spec );
630             else output_fake_module( spec );
631             break;
632         }
633         read_undef_symbols( spec, argv );
634         switch (spec->type)
635         {
636             case SPEC_WIN16:
637                 output_spec16_file( spec );
638                 break;
639             case SPEC_WIN32:
640                 BuildSpec32File( spec );
641                 break;
642             default: assert(0);
643         }
644         break;
645     case MODE_DEF:
646         if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
647         if (!spec_file_name) fatal_error( "missing .spec file\n" );
648         if (!parse_input_file( spec )) break;
649         output_def_file( spec, 1 );
650         break;
651     case MODE_IMPLIB:
652         if (!spec_file_name) fatal_error( "missing .spec file\n" );
653         if (!parse_input_file( spec )) break;
654         output_import_lib( spec, argv );
655         break;
656     case MODE_RESOURCES:
657         load_resources( argv, spec );
658         output_res_o_file( spec );
659         break;
660     default:
661         usage(1);
662         break;
663     }
664     if (nb_errors) exit(1);
665     if (output_file_name)
666     {
667         if (fclose( output_file ) < 0) fatal_perror( "fclose" );
668         if (output_file_source_name) assemble_file( output_file_source_name, output_file_name );
669         output_file_name = NULL;
670     }
671     return 0;
672 }