For each imported dll, check if there is a static library with the
[wine] / tools / winegcc / winegcc.c
1 /*
2  * MinGW wrapper: makes gcc behave like MinGW.
3  *
4  * Copyright 2000 Manuel Novoa III
5  * Copyright 2000 Francois Gouget
6  * Copyright 2002 Dimitrie O. Paun
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  * DESCRIPTION
23  *
24  * all options for gcc start with '-' and are for the most part
25  * single options (no parameters as separate argument). 
26  * There are of course exceptions to this rule, so here is an 
27  * exhaustive list of options that do take parameters (potentially)
28  * as a separate argument:
29  *
30  * Compiler:
31  * -x language
32  * -o filename
33  * -aux-info filename
34  *
35  * Preprocessor:
36  * -D name 
37  * -U name
38  * -I dir
39  * -MF file
40  * -MT target
41  * -MQ target
42  * (all -i.* arg)
43  * -include file 
44  * -imacros file
45  * -idirafter dir
46  * -iwithprefix dir
47  * -iwithprefixbefore dir
48  * -isystem dir
49  * -A predicate=answer
50  *
51  * Linking:
52  * -l library
53  * -Xlinker option
54  * -u symbol
55  *
56  * Misc:
57  * -b machine
58  * -V version
59  * -G num  (see NOTES below)
60  *
61  * NOTES
62  * There is -G option for compatibility with System V that
63  * takes no parameters. This makes "-G num" parsing ambiguous.
64  * This option is synonymous to -shared, and as such we will
65  * not support it for now.
66  *
67  * Special interest options 
68  *
69  *      Assembler Option
70  *          -Wa,option
71  *
72  *      Linker Options
73  *          object-file-name  -llibrary -nostartfiles  -nodefaultlibs
74  *          -nostdlib -s  -static  -static-libgcc  -shared  -shared-libgcc
75  *          -symbolic -Wl,option  -Xlinker option -u symbol
76  *
77  *      Directory Options
78  *          -Bprefix  -Idir  -I-  -Ldir  -specs=file
79  *
80  *      Target Options
81  *          -b machine  -V version
82  *
83  * Please note that the Target Options are relevant to everything:
84  *   compiler, linker, assembler, preprocessor.
85  * 
86  */ 
87
88 #include "config.h"
89 #include "wine/port.h"
90
91 #include <stdio.h>
92 #include <stdlib.h>
93 #include <signal.h>
94 #include <stdarg.h>
95 #include <string.h>
96 #include <errno.h>
97
98 #include "utils.h"
99
100 static const char* app_loader_template =
101     "#!/bin/sh\n"
102     "\n"
103     "appname=\"%s\"\n"
104     "# determine the application directory\n"
105     "appdir=''\n"
106     "case \"$0\" in\n"
107     "  */*)\n"
108     "    # $0 contains a path, use it\n"
109     "    appdir=`dirname \"$0\"`\n"
110     "    ;;\n"
111     "  *)\n"
112     "    # no directory in $0, search in PATH\n"
113     "    saved_ifs=$IFS\n"
114     "    IFS=:\n"
115     "    for d in $PATH\n"
116     "    do\n"
117     "      IFS=$saved_ifs\n"
118     "      if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
119     "    done\n"
120     "    ;;\n"
121     "esac\n"
122     "\n"
123     "# figure out the full app path\n"
124     "if [ -n \"$appdir\" ]; then\n"
125     "    apppath=\"$appdir/$appname\"\n"
126     "    WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
127     "    export WINEDLLPATH\n"
128     "else\n"
129     "    apppath=\"$appname\"\n"
130     "fi\n"
131     "\n"
132     "# determine the WINELOADER\n"
133     "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
134     "\n"
135     "# and try to start the app\n"
136     "exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
137 ;
138
139 static int keep_generated = 0;
140 static strarray* tmp_files;
141 #ifdef HAVE_SIGSET_T
142 static sigset_t signal_mask;
143 #endif
144
145 struct options 
146 {
147     enum { proc_cc = 0, proc_cxx = 1, proc_cpp = 2} processor;
148     int shared;
149     int use_msvcrt;
150     int nostdinc;
151     int nostdlib;
152     int nodefaultlibs;
153     int noshortwchar;
154     int gui_app;
155     int compile_only;
156     int wine_mode;
157     const char* output_name;
158     strarray* prefix;
159     strarray* lib_dirs;
160     strarray* linker_args;
161     strarray* compiler_args;
162     strarray* winebuild_args;
163     strarray* files;
164 };
165
166 static void clean_temp_files()
167 {
168     int i;
169
170     if (keep_generated) return;
171
172     for (i = 0; i < tmp_files->size; i++)
173         unlink(tmp_files->base[i]);
174 }
175
176 /* clean things up when aborting on a signal */
177 static void exit_on_signal( int sig )
178 {
179     exit(1);  /* this will call the atexit functions */
180 }
181
182 static char* get_temp_file(const char* prefix, const char* suffix)
183 {
184     int fd;
185     char* tmp = strmake("%s-XXXXXX%s", prefix, suffix);
186
187 #ifdef HAVE_SIGPROCMASK
188     sigset_t old_set;
189     /* block signals while manipulating the temp files list */
190     sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
191 #endif
192     fd = mkstemps( tmp, strlen(suffix) );
193     if (fd == -1)
194     {
195         /* could not create it in current directory, try in /tmp */
196         free(tmp);
197         tmp = strmake("/tmp/%s-XXXXXX%s", prefix, suffix);
198         fd = mkstemps( tmp, strlen(suffix) );
199         if (fd == -1) error( "could not create temp file" );
200     }
201     close( fd );
202     strarray_add(tmp_files, tmp);
203 #ifdef HAVE_SIGPROCMASK
204     sigprocmask( SIG_SETMASK, &old_set, NULL );
205 #endif
206     return tmp;
207 }
208
209 static const strarray* get_translator(struct options* opts)
210 {
211     static strarray* cpp = 0;
212     static strarray* cc = 0;
213     static strarray* cxx = 0;
214
215     switch(opts->processor)
216     {
217         case proc_cpp: 
218             if (!cpp) cpp = strarray_fromstring(CPP, " ");
219             return cpp;
220         case proc_cc:  
221             if (!cc) cc = strarray_fromstring(CC, " ");
222             return cc;
223         case proc_cxx: 
224             if (!cxx) cxx = strarray_fromstring(CXX, " ");
225             return cxx;
226     }
227     error("Unknown processor");
228 }
229
230 static void compile(struct options* opts, const char* lang)
231 {
232     strarray* comp_args = strarray_alloc();
233     int j, gcc_defs = 0;
234
235     switch(opts->processor)
236     {
237         case proc_cpp:  gcc_defs = 1; break;
238 #ifdef __GNUC__
239         /* Note: if the C compiler is gcc we assume the C++ compiler is too */
240         /* mixing different C and C++ compilers isn't supported in configure anyway */
241         case proc_cc:  gcc_defs = 1; break;
242         case proc_cxx: gcc_defs = 1; break;
243 #else
244         case proc_cc:  gcc_defs = 0; break;
245         case proc_cxx: gcc_defs = 0; break;
246 #endif
247     }
248     strarray_addall(comp_args, get_translator(opts));
249
250     if (opts->processor != proc_cpp)
251     {
252 #ifdef CC_FLAG_SHORT_WCHAR
253         if (!opts->wine_mode && !opts->noshortwchar)
254         {
255             strarray_add(comp_args, CC_FLAG_SHORT_WCHAR);
256             strarray_add(comp_args, "-DWINE_UNICODE_NATIVE");
257         }
258 #endif
259         strarray_addall(comp_args, strarray_fromstring(DLLFLAGS, " "));
260     }
261
262     strarray_add(comp_args, "-DWIN32");
263     strarray_add(comp_args, "-D_WIN32");
264     strarray_add(comp_args, "-D__WIN32");
265     strarray_add(comp_args, "-D__WIN32__");
266     strarray_add(comp_args, "-D__WINNT");
267     strarray_add(comp_args, "-D__WINNT__");
268
269     if (gcc_defs)
270     {
271         strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__))");
272         strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__))");
273         strarray_add(comp_args, "-D__fastcall=__attribute__((__fastcall__))");
274         strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__))");
275         strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__))");
276         strarray_add(comp_args, "-D_fastcall=__attribute__((__fastcall__))");
277         strarray_add(comp_args, "-D__declspec(x)=__declspec_##x");
278         strarray_add(comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
279         strarray_add(comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
280         strarray_add(comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
281         strarray_add(comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
282         strarray_add(comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
283         strarray_add(comp_args, "-D__declspec_naked=__attribute__((naked))");
284         strarray_add(comp_args, "-D__declspec_noinline=__attribute__((noinline))");
285         strarray_add(comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
286         strarray_add(comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
287         strarray_add(comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
288         strarray_add(comp_args, "-D__declspec_selectany=__attribute__((weak))");
289         strarray_add(comp_args, "-D__declspec_thread=__thread");
290     }
291
292     /* Wine specific defines */
293     strarray_add(comp_args, "-D__WINE__");
294     strarray_add(comp_args, "-D__int8=char");
295     strarray_add(comp_args, "-D__int16=short");
296     /* FIXME: what about 64-bit platforms? */
297     strarray_add(comp_args, "-D__int32=int");
298 #ifdef HAVE_LONG_LONG
299     strarray_add(comp_args, "-D__int64=long long");
300 #endif
301
302     /* options we handle explicitly */
303     if (opts->compile_only)
304         strarray_add(comp_args, "-c");
305     if (opts->output_name)
306     {
307         strarray_add(comp_args, "-o");
308         strarray_add(comp_args, opts->output_name);
309     }
310
311     /* the rest of the pass-through parameters */
312     for ( j = 0 ; j < opts->compiler_args->size ; j++ ) 
313         strarray_add(comp_args, opts->compiler_args->base[j]);
314
315     /* the language option, if any */
316     if (lang && strcmp(lang, "-xnone"))
317         strarray_add(comp_args, lang);
318
319     /* last, but not least, the files */
320     for ( j = 0; j < opts->files->size; j++ )
321     {
322         if (opts->files->base[j][0] != '-')
323             strarray_add(comp_args, opts->files->base[j]);
324     }
325
326     /* standard includes come last in the include search path */
327 #ifdef __GNUC__
328 #define SYS_INCLUDE "-isystem"
329 #else
330 #define SYS_INCLUDE "-I"
331 #endif
332     if (!opts->wine_mode && !opts->nostdinc)
333     {
334         if (opts->use_msvcrt)
335         {
336             strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/msvcrt");
337             strarray_add(comp_args, "-D__MSVCRT__");
338         }
339         strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/windows");
340     }
341 #undef SYS_INCLUDE
342
343     spawn(opts->prefix, comp_args);
344 }
345
346 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
347 {
348     struct options copts;
349     char* base_name;
350
351     /* make a copy so we don't change any of the initial stuff */
352     /* a shallow copy is exactly what we want in this case */
353     base_name = get_basename(file);
354     copts = *opts;
355     copts.output_name = get_temp_file(base_name, ".o");
356     copts.compile_only = 1;
357     copts.files = strarray_alloc();
358     strarray_add(copts.files, file);
359     compile(&copts, lang);
360     strarray_free(copts.files);
361     free(base_name);
362
363     return copts.output_name;
364 }
365
366 /* check if there is a static lib associated to a given dll */
367 static char *find_static_lib( const char *dll )
368 {
369     char *lib = strmake("%s.a", dll);
370     if (get_file_type(lib) == file_arh) return lib;
371     free( lib );
372     return NULL;
373 }
374
375 static void build(struct options* opts)
376 {
377     static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
378     strarray *lib_dirs, *files;
379     strarray *spec_args, *link_args;
380     char *output_file;
381     const char *spec_c_name, *spec_o_name;
382     const char *output_name, *spec_file, *lang;
383     const char* winebuild = getenv("WINEBUILD");
384     int generate_app_loader = 1;
385     int old_processor;
386     int j;
387
388     /* NOTE: for the files array we'll use the following convention:
389      *    -axxx:  xxx is an archive (.a)
390      *    -dxxx:  xxx is a DLL (.def)
391      *    -lxxx:  xxx is an unsorted library
392      *    -oxxx:  xxx is an object (.o)
393      *    -rxxx:  xxx is a resource (.res)
394      *    -sxxx:  xxx is a shared lib (.so)
395      *    -xlll:  lll is the language (c, c++, etc.)
396      */
397
398     if (!winebuild) winebuild = "winebuild";
399
400     output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
401
402     /* 'winegcc -o app xxx.exe.so' only creates the load script */
403     if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so"))
404     {
405         create_file(output_file, 0755, app_loader_template, opts->files->base[0]);
406         return;
407     }
408
409     /* generate app loader only for .exe */
410     if (opts->shared || strendswith(output_file, ".exe.so"))
411         generate_app_loader = 0;
412
413     /* normalize the filename a bit: strip .so, ensure it has proper ext */
414     if (strendswith(output_file, ".so")) 
415         output_file[strlen(output_file) - 3] = 0;
416     if (opts->shared)
417     {
418         if ((output_name = strrchr(output_file, '/'))) output_name++;
419         else output_name = output_file;
420         if (!strchr(output_name, '.'))
421             output_file = strmake("%s.dll", output_file);
422     }
423     else if (!strendswith(output_file, ".exe"))
424         output_file = strmake("%s.exe", output_file);
425
426     /* get the filename from the path */
427     if ((output_name = strrchr(output_file, '/'))) output_name++;
428     else output_name = output_file;
429
430     /* prepare the linking path */
431     lib_dirs = strarray_dup(opts->lib_dirs);
432     if (!opts->wine_mode)
433     {
434         for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ )
435             strarray_add(lib_dirs, stdlibpath[j]);
436     }
437
438     /* mark the files with their appropriate type */
439     spec_file = lang = 0;
440     files = strarray_alloc();
441     for ( j = 0; j < opts->files->size; j++ )
442     {
443         const char* file = opts->files->base[j];
444         if (file[0] != '-')
445         {
446             switch(get_file_type(file))
447             {
448                 case file_def:
449                 case file_spec:
450                     if (spec_file)
451                         error("Only one spec file can be specified.");
452                     spec_file = file;
453                     break;
454                 case file_rc:
455                     /* FIXME: invoke wrc to build it */
456                     error("Can't compile .rc file at the moment: %s", file);
457                     break;
458                 case file_res:
459                     strarray_add(files, strmake("-r%s", file));
460                     break;
461                 case file_obj:
462                     strarray_add(files, strmake("-o%s", file));
463                     break;
464                 case file_arh:
465                     strarray_add(files, strmake("-a%s", file));
466                     break;
467                 case file_so:
468                     strarray_add(files, strmake("-s%s", file));
469                     break;
470                 case file_na:
471                     error("File does not exist: %s", file);
472                     break;
473                 default:
474                     file = compile_to_object(opts, file, lang);
475                     strarray_add(files, strmake("-o%s", file));
476                     break;
477             }
478         }
479         else if (file[1] == 'l')
480         {
481             char *static_lib, *fullname = 0;
482             switch(get_lib_type(lib_dirs, file + 2, &fullname))
483             {
484                 case file_arh:
485                     strarray_add(files, strmake("-a%s", fullname));
486                     break;
487                 case file_dll:
488                     strarray_add(files, strmake("-d%s", file + 2));
489                     if ((static_lib = find_static_lib(fullname)))
490                     {
491                         strarray_add(files, strmake("-a%s",static_lib));
492                         free(static_lib);
493                     }
494                     break;
495                 case file_so:
496                     strarray_add(files, strmake("-s%s", file + 2));
497                     break;
498                 default:
499                     /* keep it anyway, the linker may know what to do with it */
500                     strarray_add(files, file);
501                     break;
502             }
503             free(fullname);
504         }
505         else if (file[1] == 'x')
506             lang = file;
507     }
508     if (opts->shared && !spec_file)
509         error("A spec file is currently needed in shared mode");
510
511     /* add the default libraries, if needed */
512     if (!opts->nostdlib) 
513     {
514         if (opts->use_msvcrt) strarray_add(files, "-dmsvcrt");
515     }
516
517     if (!opts->wine_mode && !opts->nodefaultlibs) 
518     {
519         if (opts->gui_app) 
520         {
521             strarray_add(files, "-dshell32");
522             strarray_add(files, "-dcomdlg32");
523             strarray_add(files, "-dgdi32");
524         }
525         strarray_add(files, "-dadvapi32");
526         strarray_add(files, "-duser32");
527         strarray_add(files, "-dkernel32");
528     }
529
530     /* run winebuild to generate the .spec.c file */
531     spec_args = strarray_alloc();
532     spec_c_name = get_temp_file(output_name, ".spec.c");
533     strarray_add(spec_args, winebuild);
534     strarray_add(spec_args, "--ld-cmd");
535     strarray_add(spec_args, LD);
536     strarray_addall(spec_args, strarray_fromstring(DLLFLAGS, " "));
537     strarray_add(spec_args, opts->shared ? "--dll" : "--exe");
538     strarray_add(spec_args, "-o");
539     strarray_add(spec_args, spec_c_name);
540     if (spec_file)
541     {
542         strarray_add(spec_args, "-E");
543         strarray_add(spec_args, spec_file);
544     }
545
546     if (!opts->shared)
547     {
548         strarray_add(spec_args, "-F");
549         strarray_add(spec_args, output_name);
550         strarray_add(spec_args, "--subsystem");
551         strarray_add(spec_args, opts->gui_app ? "windows" : "console");
552     }
553
554     for ( j = 0; j < lib_dirs->size; j++ )
555         strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
556
557     for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
558         strarray_add(spec_args, opts->winebuild_args->base[j]);
559
560     for ( j = 0; j < files->size; j++ )
561     {
562         const char* name = files->base[j] + 2;
563         switch(files->base[j][1])
564         {
565             case 'd':
566                 strarray_add(spec_args, strmake("-l%s", name));
567                 break;
568             case 'r':
569                 strarray_add(spec_args, files->base[j]);
570                 break;
571             case 'a':
572             case 'o':
573                 strarray_add(spec_args, name);
574                 break;
575         }
576     }
577
578     spawn(opts->prefix, spec_args);
579
580     /* compile the .spec.c file into a .spec.o file */
581     old_processor = opts->processor;
582     /* Always compile spec.c as c, even if linking with g++ */
583     opts->processor = proc_cc;
584     spec_o_name = compile_to_object(opts, spec_c_name, 0);
585     opts->processor = old_processor;
586
587     /* link everything together now */
588     link_args = strarray_alloc();
589     strarray_addall(link_args, get_translator(opts));
590     strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " "));
591
592     strarray_add(link_args, "-o");
593     strarray_add(link_args, strmake("%s.so", output_file));
594
595     for ( j = 0 ; j < opts->linker_args->size ; j++ ) 
596         strarray_add(link_args, opts->linker_args->base[j]);
597
598     for ( j = 0; j < lib_dirs->size; j++ )
599         strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
600
601     strarray_add(link_args, spec_o_name);
602
603     for ( j = 0; j < files->size; j++ )
604     {
605         const char* name = files->base[j] + 2;
606         switch(files->base[j][1])
607         {
608             case 'l':
609             case 's':
610                 strarray_add(link_args, strmake("-l%s", name));
611                 break;
612             case 'a':
613             case 'o':
614                 strarray_add(link_args, name);
615                 break;
616         }
617     }
618
619     if (!opts->nostdlib) 
620     {
621         strarray_add(link_args, "-lwine");
622         strarray_add(link_args, "-lm");
623         strarray_add(link_args, "-lc");
624     }
625
626     spawn(opts->prefix, link_args);
627
628     /* create the loader script */
629     if (generate_app_loader)
630     {
631         if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0;
632         create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name));
633     }
634 }
635
636
637 static void forward(int argc, char **argv, struct options* opts)
638 {
639     strarray* args = strarray_alloc();
640     int j;
641
642     strarray_addall(args, get_translator(opts));
643
644     for( j = 1; j < argc; j++ ) 
645         strarray_add(args, argv[j]);
646
647     spawn(opts->prefix, args);
648 }
649
650 /*
651  *      Linker Options
652  *          object-file-name  -llibrary -nostartfiles  -nodefaultlibs
653  *          -nostdlib -s  -static  -static-libgcc  -shared  -shared-libgcc
654  *          -symbolic -Wl,option  -Xlinker option -u symbol
655  *          -framework name
656  */
657 static int is_linker_arg(const char* arg)
658 {
659     static const char* link_switches[] = 
660     {
661         "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s", 
662         "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic",
663         "-framework"
664     };
665     int j;
666
667     switch (arg[1]) 
668     {
669         case 'l': 
670         case 'u':
671             return 1;
672         case 'W':
673             if (strncmp("-Wl,", arg, 4) == 0) return 1;
674             break;
675         case 'X':
676             if (strcmp("-Xlinker", arg) == 0) return 1;
677             break;
678     }
679
680     for (j = 0; j < sizeof(link_switches)/sizeof(link_switches[0]); j++)
681         if (strcmp(link_switches[j], arg) == 0) return 1;
682
683     return 0;
684 }
685
686 /*
687  *      Target Options
688  *          -b machine  -V version
689  */
690 static int is_target_arg(const char* arg)
691 {
692     return arg[1] == 'b' || arg[2] == 'V';
693 }
694
695
696 /*
697  *      Directory Options
698  *          -Bprefix  -Idir  -I-  -Ldir  -specs=file
699  */
700 static int is_directory_arg(const char* arg)
701 {
702     return arg[1] == 'B' || arg[1] == 'L' || arg[1] == 'I' || strncmp("-specs=", arg, 7) == 0;
703 }
704
705 /*
706  *      MinGW Options
707  *          -mno-cygwin -mwindows -mconsole -mthreads
708  */ 
709 static int is_mingw_arg(const char* arg)
710 {
711     static const char* mingw_switches[] = 
712     {
713         "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads"
714     };
715     int j;
716
717     for (j = 0; j < sizeof(mingw_switches)/sizeof(mingw_switches[0]); j++)
718         if (strcmp(mingw_switches[j], arg) == 0) return 1;
719
720     return 0;
721 }
722
723 int main(int argc, char **argv)
724 {
725     int i, c, next_is_arg = 0, linking = 1;
726     int raw_compiler_arg, raw_linker_arg;
727     const char* option_arg;
728     struct options opts;
729     char* lang = 0;
730     char* str;
731
732 #ifdef SIGHUP
733     signal( SIGHUP, exit_on_signal );
734 #endif
735     signal( SIGTERM, exit_on_signal );
736     signal( SIGINT, exit_on_signal );
737 #ifdef HAVE_SIGADDSET
738     sigemptyset( &signal_mask );
739     sigaddset( &signal_mask, SIGHUP );
740     sigaddset( &signal_mask, SIGTERM );
741     sigaddset( &signal_mask, SIGINT );
742 #endif
743
744     /* setup tmp file removal at exit */
745     tmp_files = strarray_alloc();
746     atexit(clean_temp_files);
747     
748     /* initialize options */
749     memset(&opts, 0, sizeof(opts));
750     opts.lib_dirs = strarray_alloc();
751     opts.files = strarray_alloc();
752     opts.linker_args = strarray_alloc();
753     opts.compiler_args = strarray_alloc();
754     opts.winebuild_args = strarray_alloc();
755
756     /* determine the processor type */
757     if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
758     else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
759     
760     /* parse options */
761     for ( i = 1 ; i < argc ; i++ ) 
762     {
763         if (argv[i][0] == '-')  /* option */
764         {
765             /* determine if tihs switch is followed by a separate argument */
766             next_is_arg = 0;
767             option_arg = 0;
768             switch(argv[i][1])
769             {
770                 case 'x': case 'o': case 'D': case 'U':
771                 case 'I': case 'A': case 'l': case 'u':
772                 case 'b': case 'V': case 'G': case 'L':
773                 case 'B':
774                     if (argv[i][2]) option_arg = &argv[i][2];
775                     else next_is_arg = 1;
776                     break;
777                 case 'i':
778                     next_is_arg = 1;
779                     break;
780                 case 'a':
781                     if (strcmp("-aux-info", argv[i]) == 0)
782                         next_is_arg = 1;
783                     break;
784                 case 'X':
785                     if (strcmp("-Xlinker", argv[i]) == 0)
786                         next_is_arg = 1;
787                     break;
788                 case 'M':
789                     c = argv[i][2];
790                     if (c == 'F' || c == 'T' || c == 'Q')
791                     {
792                         if (argv[i][3]) option_arg = &argv[i][3];
793                         else next_is_arg = 1;
794                     }
795                     break;
796                 case 'f':
797                     if (strcmp("-framework", argv[i]) == 0)
798                         next_is_arg = 1;
799                     break;
800             }
801             if (next_is_arg) option_arg = argv[i+1];
802
803             /* determine what options go 'as is' to the linker & the compiler */
804             raw_compiler_arg = raw_linker_arg = 0;
805             if (is_linker_arg(argv[i])) 
806             {
807                 raw_linker_arg = 1;
808             }
809             else 
810             {
811                 if (is_directory_arg(argv[i]) || is_target_arg(argv[i]))
812                     raw_linker_arg = 1;
813                 raw_compiler_arg = !is_mingw_arg(argv[i]);
814             }
815
816             /* these things we handle explicitly so we don't pass them 'as is' */
817             if (argv[i][1] == 'l' || argv[i][1] == 'I' || argv[i][1] == 'L')
818                 raw_linker_arg = 0;
819             if (argv[i][1] == 'c' || argv[i][1] == 'L')
820                 raw_compiler_arg = 0;
821             if (argv[i][1] == 'o')
822                 raw_compiler_arg = raw_linker_arg = 0;
823
824             /* do a bit of semantic analysis */
825             switch (argv[i][1]) 
826             {
827                 case 'B':
828                     str = strdup(option_arg);
829                     if (strendswith(str, "/tools/winebuild"))
830                     {
831                         opts.wine_mode = 1;
832                         /* don't pass it to the compiler, this generates warnings */
833                         raw_compiler_arg = raw_linker_arg = 0;
834                     }
835                     if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
836                     if (!opts.prefix) opts.prefix = strarray_alloc();
837                     strarray_add(opts.prefix, str);
838                     break;
839                 case 'c':        /* compile or assemble */
840                     if (argv[i][2] == 0) opts.compile_only = 1;
841                     /* fall through */
842                 case 'S':        /* generate assembler code */
843                 case 'E':        /* preprocess only */
844                     if (argv[i][2] == 0) linking = 0;
845                     break;
846                 case 'f':
847                     if (strcmp("-fno-short-wchar", argv[i]) == 0)
848                         opts.noshortwchar = 1;
849                     break;
850                 case 'l':
851                     strarray_add(opts.files, strmake("-l%s", option_arg));
852                     break;
853                 case 'L':
854                     strarray_add(opts.lib_dirs, option_arg);
855                     break;
856                 case 'M':        /* map file generation */
857                     linking = 0;
858                     break;
859                 case 'm':
860                     if (strcmp("-mno-cygwin", argv[i]) == 0)
861                         opts.use_msvcrt = 1;
862                     else if (strcmp("-mwindows", argv[i]) == 0)
863                         opts.gui_app = 1;
864                     else if (strcmp("-mconsole", argv[i]) == 0)
865                         opts.gui_app = 0;
866                     break;
867                 case 'n':
868                     if (strcmp("-nostdinc", argv[i]) == 0)
869                         opts.nostdinc = 1;
870                     else if (strcmp("-nodefaultlibs", argv[i]) == 0)
871                         opts.nodefaultlibs = 1;
872                     else if (strcmp("-nostdlib", argv[i]) == 0)
873                         opts.nostdlib = 1;
874                     break;
875                 case 'o':
876                     opts.output_name = option_arg;
877                     break;
878                 case 's':
879                     if (strcmp("-static", argv[i]) == 0) 
880                         linking = -1;
881                     else if(strcmp("-save-temps", argv[i]) == 0)
882                         keep_generated = 1;
883                     else if(strcmp("-shared", argv[i]) == 0)
884                     {
885                         opts.shared = 1;
886                         raw_compiler_arg = raw_linker_arg = 0;
887                     }
888                     break;
889                 case 'v':
890                     if (argv[i][2] == 0) verbose++;
891                     break;
892                 case 'W':
893                     if (strncmp("-Wl,", argv[i], 4) == 0)
894                     {
895                         if (strstr(argv[i], "-static"))
896                             linking = -1;
897                     }
898                     else if (strncmp("-Wb,", argv[i], 4) == 0)
899                     {
900                         strarray* Wb = strarray_fromstring(argv[i] + 4, ",");
901                         strarray_addall(opts.winebuild_args, Wb);
902                         strarray_free(Wb);
903                         /* don't pass it to the compiler, it generates errors */
904                         raw_compiler_arg = raw_linker_arg = 0;
905                     }
906                     break;
907                 case 'x':
908                     lang = strmake("-x%s", option_arg);
909                     strarray_add(opts.files, lang);
910                     /* we'll pass these flags ourselves, explicitely */
911                     raw_compiler_arg = raw_linker_arg = 0;
912                     break;
913                 case '-':
914                     if (strcmp("-static", argv[i]+1) == 0)
915                         linking = -1;
916                     break;
917             }
918
919             /* put the arg into the appropriate bucket */
920             if (raw_linker_arg) 
921             {
922                 strarray_add(opts.linker_args, argv[i]);
923                 if (next_is_arg && (i + 1 < argc)) 
924                     strarray_add(opts.linker_args, argv[i + 1]);
925             }
926             if (raw_compiler_arg)
927             {
928                 strarray_add(opts.compiler_args, argv[i]);
929                 if (next_is_arg && (i + 1 < argc))
930                     strarray_add(opts.compiler_args, argv[i + 1]);
931             }
932
933             /* skip the next token if it's an argument */
934             if (next_is_arg) i++;
935         }
936         else
937         {
938             strarray_add(opts.files, argv[i]);
939         } 
940     }
941
942     if (opts.processor == proc_cpp) linking = 0;
943     if (linking == -1) error("Static linking is not supported.");
944
945     if (opts.files->size == 0) forward(argc, argv, &opts);
946     else if (linking) build(&opts);
947     else compile(&opts, lang);
948
949     return 0;
950 }