Added --ld-cmd and --nm-cmd options in winebuild, and make winegcc
[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, "--ld-cmd");
495     strarray_add(spec_args, LD);
496     strarray_add(spec_args, "-o");
497     strarray_add(spec_args, spec_c_name);
498     if (opts->shared)
499     {
500         strarray_add(spec_args, "--dll");
501         strarray_add(spec_args, spec_file);
502     }
503     else
504     {
505         strarray_add(spec_args, "--exe");
506         strarray_add(spec_args, output_name);
507         strarray_add(spec_args, "--subsystem");
508         strarray_add(spec_args, opts->gui_app ? "windows" : "console");
509     }
510
511     for ( j = 0; j < lib_dirs->size; j++ )
512         strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
513
514     for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
515         strarray_add(spec_args, opts->winebuild_args->base[j]);
516
517     for ( j = 0; j < files->size; j++ )
518     {
519         const char* name = files->base[j] + 2;
520         switch(files->base[j][1])
521         {
522             case 'd':
523                 strarray_add(spec_args, strmake("-l%s", name));
524                 break;
525             case 'r':
526                 strarray_add(spec_args, files->base[j]);
527                 break;
528             case 'a':
529             case 'o':
530                 strarray_add(spec_args, name);
531                 break;
532         }
533     }
534
535     spawn(opts->prefix, spec_args);
536
537     /* compile the .spec.c file into a .spec.o file */
538     old_processor = opts->processor;
539     /* Always compile spec.c as c, even if linking with g++ */
540     opts->processor = proc_cc;
541     spec_o_name = compile_to_object(opts, spec_c_name, 0);
542     opts->processor = old_processor;
543
544     /* link everything together now */
545     link_args = strarray_alloc();
546     strarray_addall(link_args, get_translator(opts));
547     strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " "));
548
549     strarray_add(link_args, "-o");
550     strarray_add(link_args, strmake("%s.so", output_file));
551
552     for ( j = 0 ; j < opts->linker_args->size ; j++ ) 
553         strarray_add(link_args, opts->linker_args->base[j]);
554
555     for ( j = 0; j < lib_dirs->size; j++ )
556         strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
557
558     strarray_add(link_args, spec_o_name);
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 'l':
566             case 's':
567                 strarray_add(link_args, strmake("-l%s", name));
568                 break;
569             case 'a':
570             case 'o':
571                 strarray_add(link_args, name);
572                 break;
573         }
574     }
575
576     if (!opts->nostdlib) 
577     {
578         strarray_add(link_args, "-lwine");
579         strarray_add(link_args, "-lm");
580         strarray_add(link_args, "-lc");
581     }
582
583     spawn(opts->prefix, link_args);
584
585     /* create the loader script */
586     if (generate_app_loader)
587     {
588         if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0;
589         create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name));
590     }
591 }
592
593
594 static void forward(int argc, char **argv, struct options* opts)
595 {
596     strarray* args = strarray_alloc();
597     int j;
598
599     strarray_addall(args, get_translator(opts));
600
601     for( j = 1; j < argc; j++ ) 
602         strarray_add(args, argv[j]);
603
604     spawn(opts->prefix, args);
605 }
606
607 /*
608  *      Linker Options
609  *          object-file-name  -llibrary -nostartfiles  -nodefaultlibs
610  *          -nostdlib -s  -static  -static-libgcc  -shared  -shared-libgcc
611  *          -symbolic -Wl,option  -Xlinker option -u symbol
612  *          -framework name
613  */
614 static int is_linker_arg(const char* arg)
615 {
616     static const char* link_switches[] = 
617     {
618         "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s", 
619         "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic",
620         "-framework"
621     };
622     int j;
623
624     switch (arg[1]) 
625     {
626         case 'l': 
627         case 'u':
628             return 1;
629         case 'W':
630             if (strncmp("-Wl,", arg, 4) == 0) return 1;
631             break;
632         case 'X':
633             if (strcmp("-Xlinker", arg) == 0) return 1;
634             break;
635     }
636
637     for (j = 0; j < sizeof(link_switches)/sizeof(link_switches[0]); j++)
638         if (strcmp(link_switches[j], arg) == 0) return 1;
639
640     return 0;
641 }
642
643 /*
644  *      Target Options
645  *          -b machine  -V version
646  */
647 static int is_target_arg(const char* arg)
648 {
649     return arg[1] == 'b' || arg[2] == 'V';
650 }
651
652
653 /*
654  *      Directory Options
655  *          -Bprefix  -Idir  -I-  -Ldir  -specs=file
656  */
657 static int is_directory_arg(const char* arg)
658 {
659     return arg[1] == 'B' || arg[1] == 'L' || arg[1] == 'I' || strncmp("-specs=", arg, 7) == 0;
660 }
661
662 /*
663  *      MinGW Options
664  *          -mno-cygwin -mwindows -mconsole -mthreads
665  */ 
666 static int is_mingw_arg(const char* arg)
667 {
668     static const char* mingw_switches[] = 
669     {
670         "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads"
671     };
672     int j;
673
674     for (j = 0; j < sizeof(mingw_switches)/sizeof(mingw_switches[0]); j++)
675         if (strcmp(mingw_switches[j], arg) == 0) return 1;
676
677     return 0;
678 }
679
680 int main(int argc, char **argv)
681 {
682     int i, c, next_is_arg = 0, linking = 1;
683     int raw_compiler_arg, raw_linker_arg;
684     const char* option_arg;
685     struct options opts;
686     char* lang = 0;
687     char* str;
688
689     /* setup tmp file removal at exit */
690     tmp_files = strarray_alloc();
691     atexit(clean_temp_files);
692     
693     /* initialize options */
694     memset(&opts, 0, sizeof(opts));
695     opts.lib_dirs = strarray_alloc();
696     opts.files = strarray_alloc();
697     opts.linker_args = strarray_alloc();
698     opts.compiler_args = strarray_alloc();
699     opts.winebuild_args = strarray_alloc();
700
701     /* determine the processor type */
702     if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
703     else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
704     
705     /* parse options */
706     for ( i = 1 ; i < argc ; i++ ) 
707     {
708         if (argv[i][0] == '-')  /* option */
709         {
710             /* determine if tihs switch is followed by a separate argument */
711             next_is_arg = 0;
712             option_arg = 0;
713             switch(argv[i][1])
714             {
715                 case 'x': case 'o': case 'D': case 'U':
716                 case 'I': case 'A': case 'l': case 'u':
717                 case 'b': case 'V': case 'G': case 'L':
718                 case 'B':
719                     if (argv[i][2]) option_arg = &argv[i][2];
720                     else next_is_arg = 1;
721                     break;
722                 case 'i':
723                     next_is_arg = 1;
724                     break;
725                 case 'a':
726                     if (strcmp("-aux-info", argv[i]) == 0)
727                         next_is_arg = 1;
728                     break;
729                 case 'X':
730                     if (strcmp("-Xlinker", argv[i]) == 0)
731                         next_is_arg = 1;
732                     break;
733                 case 'M':
734                     c = argv[i][2];
735                     if (c == 'F' || c == 'T' || c == 'Q')
736                     {
737                         if (argv[i][3]) option_arg = &argv[i][3];
738                         else next_is_arg = 1;
739                     }
740                     break;
741                 case 'f':
742                     if (strcmp("-framework", argv[i]) == 0)
743                         next_is_arg = 1;
744                     break;
745             }
746             if (next_is_arg) option_arg = argv[i+1];
747
748             /* determine what options go 'as is' to the linker & the compiler */
749             raw_compiler_arg = raw_linker_arg = 0;
750             if (is_linker_arg(argv[i])) 
751             {
752                 raw_linker_arg = 1;
753             }
754             else 
755             {
756                 if (is_directory_arg(argv[i]) || is_target_arg(argv[i]))
757                     raw_linker_arg = 1;
758                 raw_compiler_arg = !is_mingw_arg(argv[i]);
759             }
760
761             /* these things we handle explicitly so we don't pass them 'as is' */
762             if (argv[i][1] == 'l' || argv[i][1] == 'I' || argv[i][1] == 'L')
763                 raw_linker_arg = 0;
764             if (argv[i][1] == 'c' || argv[i][1] == 'L')
765                 raw_compiler_arg = 0;
766             if (argv[i][1] == 'o')
767                 raw_compiler_arg = raw_linker_arg = 0;
768
769             /* do a bit of semantic analysis */
770             switch (argv[i][1]) 
771             {
772                 case 'B':
773                     str = strdup(option_arg);
774                     if (strendswith(str, "/tools/winebuild"))
775                     {
776                         opts.wine_mode = 1;
777                         /* don't pass it to the compiler, this generates warnings */
778                         raw_compiler_arg = raw_linker_arg = 0;
779                     }
780                     if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
781                     if (!opts.prefix) opts.prefix = strarray_alloc();
782                     strarray_add(opts.prefix, str);
783                     break;
784                 case 'c':        /* compile or assemble */
785                     if (argv[i][2] == 0) opts.compile_only = 1;
786                     /* fall through */
787                 case 'S':        /* generate assembler code */
788                 case 'E':        /* preprocess only */
789                     if (argv[i][2] == 0) linking = 0;
790                     break;
791                 case 'f':
792                     if (strcmp("-fno-short-wchar", argv[i]) == 0)
793                         opts.noshortwchar = 1;
794                     break;
795                 case 'l':
796                     strarray_add(opts.files, strmake("-l%s", option_arg));
797                     break;
798                 case 'L':
799                     strarray_add(opts.lib_dirs, option_arg);
800                     break;
801                 case 'M':        /* map file generation */
802                     linking = 0;
803                     break;
804                 case 'm':
805                     if (strcmp("-mno-cygwin", argv[i]) == 0)
806                         opts.use_msvcrt = 1;
807                     else if (strcmp("-mwindows", argv[i]) == 0)
808                         opts.gui_app = 1;
809                     else if (strcmp("-mconsole", argv[i]) == 0)
810                         opts.gui_app = 0;
811                     break;
812                 case 'n':
813                     if (strcmp("-nostdinc", argv[i]) == 0)
814                         opts.nostdinc = 1;
815                     else if (strcmp("-nodefaultlibs", argv[i]) == 0)
816                         opts.nodefaultlibs = 1;
817                     else if (strcmp("-nostdlib", argv[i]) == 0)
818                         opts.nostdlib = 1;
819                     break;
820                 case 'o':
821                     opts.output_name = option_arg;
822                     break;
823                 case 's':
824                     if (strcmp("-static", argv[i]) == 0) 
825                         linking = -1;
826                     else if(strcmp("-save-temps", argv[i]) == 0)
827                         keep_generated = 1;
828                     else if(strcmp("-shared", argv[i]) == 0)
829                     {
830                         opts.shared = 1;
831                         raw_compiler_arg = raw_linker_arg = 0;
832                     }
833                     break;
834                 case 'v':
835                     if (argv[i][2] == 0) verbose++;
836                     break;
837                 case 'W':
838                     if (strncmp("-Wl,", argv[i], 4) == 0)
839                     {
840                         if (strstr(argv[i], "-static"))
841                             linking = -1;
842                     }
843                     else if (strncmp("-Wb,", argv[i], 4) == 0)
844                     {
845                         strarray* Wb = strarray_fromstring(argv[i] + 4, ",");
846                         strarray_addall(opts.winebuild_args, Wb);
847                         strarray_free(Wb);
848                         /* don't pass it to the compiler, it generates errors */
849                         raw_compiler_arg = raw_linker_arg = 0;
850                     }
851                     break;
852                 case 'x':
853                     lang = strmake("-x%s", option_arg);
854                     strarray_add(opts.files, lang);
855                     /* we'll pass these flags ourselves, explicitely */
856                     raw_compiler_arg = raw_linker_arg = 0;
857                     break;
858                 case '-':
859                     if (strcmp("-static", argv[i]+1) == 0)
860                         linking = -1;
861                     break;
862             }
863
864             /* put the arg into the appropriate bucket */
865             if (raw_linker_arg) 
866             {
867                 strarray_add(opts.linker_args, argv[i]);
868                 if (next_is_arg && (i + 1 < argc)) 
869                     strarray_add(opts.linker_args, argv[i + 1]);
870             }
871             if (raw_compiler_arg)
872             {
873                 strarray_add(opts.compiler_args, argv[i]);
874                 if (next_is_arg && (i + 1 < argc))
875                     strarray_add(opts.compiler_args, argv[i + 1]);
876             }
877
878             /* skip the next token if it's an argument */
879             if (next_is_arg) i++;
880         }
881         else
882         {
883             strarray_add(opts.files, argv[i]);
884         } 
885     }
886
887     if (opts.processor == proc_cpp) linking = 0;
888     if (linking == -1) error("Static linking is not supported.");
889
890     if (opts.files->size == 0) forward(argc, argv, &opts);
891     else if (linking) build(&opts);
892     else compile(&opts, lang);
893
894     return 0;
895 }