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