Reverse the order for deleting the items in resetcontent to correctly
[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     if (!opts->wine_mode && !opts->nostdinc)
262     {
263         if (opts->use_msvcrt)
264         {
265             strarray_add(comp_args, "-I" INCLUDEDIR "/msvcrt");
266             strarray_add(comp_args, "-D__MSVCRT__");
267         }
268         strarray_add(comp_args, "-I" INCLUDEDIR "/windows");
269     }
270     strarray_add(comp_args, "-DWIN32");
271     strarray_add(comp_args, "-D_WIN32");
272     strarray_add(comp_args, "-D__WIN32");
273     strarray_add(comp_args, "-D__WIN32__");
274     strarray_add(comp_args, "-D__WINNT");
275     strarray_add(comp_args, "-D__WINNT__");
276
277     if (gcc_defs)
278     {
279         strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__))");
280         strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__))");
281         strarray_add(comp_args, "-D__fastcall=__attribute__((__fastcall__))");
282         strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__))");
283         strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__))");
284         strarray_add(comp_args, "-D_fastcall=__attribute__((__fastcall__))");
285         strarray_add(comp_args, "-D__declspec(x)=__declspec_##x");
286         strarray_add(comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
287         strarray_add(comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
288         strarray_add(comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
289         strarray_add(comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
290         strarray_add(comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
291         strarray_add(comp_args, "-D__declspec_naked=__attribute__((naked))");
292         strarray_add(comp_args, "-D__declspec_noinline=__attribute__((noinline))");
293         strarray_add(comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
294         strarray_add(comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
295         strarray_add(comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
296         strarray_add(comp_args, "-D__declspec_selectany=__attribute__((weak))");
297         strarray_add(comp_args, "-D__declspec_thread=__thread");
298     }
299
300     /* Wine specific defines */
301     strarray_add(comp_args, "-D__WINE__");
302     strarray_add(comp_args, "-D__int8=char");
303     strarray_add(comp_args, "-D__int16=short");
304     /* FIXME: what about 64-bit platforms? */
305     strarray_add(comp_args, "-D__int32=int");
306 #ifdef HAVE_LONG_LONG
307     strarray_add(comp_args, "-D__int64=long long");
308 #endif
309
310     /* options we handle explicitly */
311     if (opts->compile_only)
312         strarray_add(comp_args, "-c");
313     if (opts->output_name)
314     {
315         strarray_add(comp_args, "-o");
316         strarray_add(comp_args, opts->output_name);
317     }
318
319     /* the rest of the pass-through parameters */
320     for ( j = 0 ; j < opts->compiler_args->size ; j++ ) 
321         strarray_add(comp_args, opts->compiler_args->base[j]);
322
323     /* the language option, if any */
324     if (lang && strcmp(lang, "-xnone"))
325         strarray_add(comp_args, lang);
326
327     /* last, but not least, the files */
328     for ( j = 0; j < opts->files->size; j++ )
329     {
330         if (opts->files->base[j][0] != '-')
331             strarray_add(comp_args, opts->files->base[j]);
332     }
333
334     spawn(opts->prefix, comp_args);
335 }
336
337 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
338 {
339     struct options copts;
340     char* base_name;
341
342     /* make a copy we so don't change any of the initial stuff */
343     /* a shallow copy is exactly what we want in this case */
344     base_name = get_basename(file);
345     copts = *opts;
346     copts.output_name = get_temp_file(base_name, ".o");
347     copts.compile_only = 1;
348     copts.files = strarray_alloc();
349     strarray_add(copts.files, file);
350     compile(&copts, lang);
351     strarray_free(copts.files);
352     free(base_name);
353
354     return copts.output_name;
355 }
356
357 static void build(struct options* opts)
358 {
359     static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
360     strarray *lib_dirs, *files;
361     strarray *spec_args, *link_args;
362     char *output_file;
363     const char *spec_c_name, *spec_o_name;
364     const char *output_name, *spec_file, *lang;
365     const char* winebuild = getenv("WINEBUILD");
366     int generate_app_loader = 1;
367     int old_processor;
368     int j;
369
370     /* NOTE: for the files array we'll use the following convention:
371      *    -axxx:  xxx is an archive (.a)
372      *    -dxxx:  xxx is a DLL (.def)
373      *    -lxxx:  xxx is an unsorted library
374      *    -oxxx:  xxx is an object (.o)
375      *    -rxxx:  xxx is a resource (.res)
376      *    -sxxx:  xxx is a shared lib (.so)
377      *    -xlll:  lll is the language (c, c++, etc.)
378      */
379
380     if (!winebuild) winebuild = "winebuild";
381
382     output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
383
384     /* 'winegcc -o app xxx.exe.so' only creates the load script */
385     if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so"))
386     {
387         create_file(output_file, 0755, app_loader_template, opts->files->base[0]);
388         return;
389     }
390
391     /* generate app loader only for .exe */
392     if (opts->shared || strendswith(output_file, ".exe.so"))
393         generate_app_loader = 0;
394
395     /* normalize the filename a bit: strip .so, ensure it has proper ext */
396     if (strendswith(output_file, ".so")) 
397         output_file[strlen(output_file) - 3] = 0;
398     if (opts->shared)
399     {
400         if ((output_name = strrchr(output_file, '/'))) output_name++;
401         else output_name = output_file;
402         if (!strchr(output_name, '.'))
403             output_file = strmake("%s.dll", output_file);
404     }
405     else if (!strendswith(output_file, ".exe"))
406         output_file = strmake("%s.exe", output_file);
407
408     /* get the filename from the path */
409     if ((output_name = strrchr(output_file, '/'))) output_name++;
410     else output_name = output_file;
411
412     /* prepare the linking path */
413     lib_dirs = strarray_dup(opts->lib_dirs);
414     if (!opts->wine_mode)
415     {
416         for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ )
417             strarray_add(lib_dirs, stdlibpath[j]);
418     }
419
420     /* mark the files with their appropriate type */
421     spec_file = lang = 0;
422     files = strarray_alloc();
423     for ( j = 0; j < opts->files->size; j++ )
424     {
425         const char* file = opts->files->base[j];
426         if (file[0] != '-')
427         {
428             switch(get_file_type(file))
429             {
430                 case file_def:
431                 case file_spec:
432                     if (!opts->shared) 
433                         error("Spec file %s not supported in non-shared mode", file);
434                     if (spec_file)
435                         error("Only one spec file can be specified in shared mode");
436                     spec_file = file;
437                     break;
438                 case file_rc:
439                     /* FIXME: invoke wrc to build it */
440                     error("Can't compile .rc file at the moment: %s", file);
441                     break;
442                 case file_res:
443                     strarray_add(files, strmake("-r%s", file));
444                     break;
445                 case file_obj:
446                     strarray_add(files, strmake("-o%s", file));
447                     break;
448                 case file_arh:
449                     strarray_add(files, strmake("-a%s", file));
450                     break;
451                 case file_so:
452                     strarray_add(files, strmake("-s%s", file));
453                     break;
454                 case file_na:
455                     error("File does not exist: %s", file);
456                     break;
457                 default:
458                     file = compile_to_object(opts, file, lang);
459                     strarray_add(files, strmake("-o%s", file));
460                     break;
461             }
462         }
463         else if (file[1] == 'l')
464         {
465             char* fullname = 0;
466             switch(get_lib_type(lib_dirs, file + 2, &fullname))
467             {
468                 case file_arh:
469                     strarray_add(files, strmake("-a%s", fullname));
470                     break;
471                 case file_dll:
472                     strarray_add(files, strmake("-d%s", file + 2));
473                     break;
474                 case file_so:
475                     strarray_add(files, strmake("-s%s", file + 2));
476                     break;
477                 default:
478                     /* keep it anyway, the linker may know what to do with it */
479                     strarray_add(files, file);
480                     break;
481             }
482             free(fullname);
483         }
484         else if (file[1] == 'x')
485             lang = file;
486     }
487     if (opts->shared && !spec_file)
488         error("A spec file is currently needed in shared mode");
489
490     /* add the default libraries, if needed */
491     if (!opts->nostdlib) 
492     {
493         if (opts->use_msvcrt) strarray_add(files, "-dmsvcrt");
494     }
495
496     if (!opts->wine_mode && !opts->nodefaultlibs) 
497     {
498         if (opts->gui_app) 
499         {
500             strarray_add(files, "-dshell32");
501             strarray_add(files, "-dcomdlg32");
502             strarray_add(files, "-dgdi32");
503         }
504         strarray_add(files, "-dadvapi32");
505         strarray_add(files, "-duser32");
506         strarray_add(files, "-dkernel32");
507     }
508
509     /* run winebuild to generate the .spec.c file */
510     spec_args = strarray_alloc();
511     spec_c_name = get_temp_file(output_name, ".spec.c");
512     strarray_add(spec_args, winebuild);
513     strarray_add(spec_args, "--ld-cmd");
514     strarray_add(spec_args, LD);
515     strarray_addall(spec_args, strarray_fromstring(DLLFLAGS, " "));
516     strarray_add(spec_args, "-o");
517     strarray_add(spec_args, spec_c_name);
518     if (opts->shared)
519     {
520         strarray_add(spec_args, "--dll");
521         strarray_add(spec_args, spec_file);
522     }
523     else
524     {
525         strarray_add(spec_args, "--exe");
526         strarray_add(spec_args, output_name);
527         strarray_add(spec_args, "--subsystem");
528         strarray_add(spec_args, opts->gui_app ? "windows" : "console");
529     }
530
531     for ( j = 0; j < lib_dirs->size; j++ )
532         strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
533
534     for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
535         strarray_add(spec_args, opts->winebuild_args->base[j]);
536
537     for ( j = 0; j < files->size; j++ )
538     {
539         const char* name = files->base[j] + 2;
540         switch(files->base[j][1])
541         {
542             case 'd':
543                 strarray_add(spec_args, strmake("-l%s", name));
544                 break;
545             case 'r':
546                 strarray_add(spec_args, files->base[j]);
547                 break;
548             case 'a':
549             case 'o':
550                 strarray_add(spec_args, name);
551                 break;
552         }
553     }
554
555     spawn(opts->prefix, spec_args);
556
557     /* compile the .spec.c file into a .spec.o file */
558     old_processor = opts->processor;
559     /* Always compile spec.c as c, even if linking with g++ */
560     opts->processor = proc_cc;
561     spec_o_name = compile_to_object(opts, spec_c_name, 0);
562     opts->processor = old_processor;
563
564     /* link everything together now */
565     link_args = strarray_alloc();
566     strarray_addall(link_args, get_translator(opts));
567     strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " "));
568
569     strarray_add(link_args, "-o");
570     strarray_add(link_args, strmake("%s.so", output_file));
571
572     for ( j = 0 ; j < opts->linker_args->size ; j++ ) 
573         strarray_add(link_args, opts->linker_args->base[j]);
574
575     for ( j = 0; j < lib_dirs->size; j++ )
576         strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
577
578     strarray_add(link_args, spec_o_name);
579
580     for ( j = 0; j < files->size; j++ )
581     {
582         const char* name = files->base[j] + 2;
583         switch(files->base[j][1])
584         {
585             case 'l':
586             case 's':
587                 strarray_add(link_args, strmake("-l%s", name));
588                 break;
589             case 'a':
590             case 'o':
591                 strarray_add(link_args, name);
592                 break;
593         }
594     }
595
596     if (!opts->nostdlib) 
597     {
598         strarray_add(link_args, "-lwine");
599         strarray_add(link_args, "-lm");
600         strarray_add(link_args, "-lc");
601     }
602
603     spawn(opts->prefix, link_args);
604
605     /* create the loader script */
606     if (generate_app_loader)
607     {
608         if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0;
609         create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name));
610     }
611 }
612
613
614 static void forward(int argc, char **argv, struct options* opts)
615 {
616     strarray* args = strarray_alloc();
617     int j;
618
619     strarray_addall(args, get_translator(opts));
620
621     for( j = 1; j < argc; j++ ) 
622         strarray_add(args, argv[j]);
623
624     spawn(opts->prefix, args);
625 }
626
627 /*
628  *      Linker Options
629  *          object-file-name  -llibrary -nostartfiles  -nodefaultlibs
630  *          -nostdlib -s  -static  -static-libgcc  -shared  -shared-libgcc
631  *          -symbolic -Wl,option  -Xlinker option -u symbol
632  *          -framework name
633  */
634 static int is_linker_arg(const char* arg)
635 {
636     static const char* link_switches[] = 
637     {
638         "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s", 
639         "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic",
640         "-framework"
641     };
642     int j;
643
644     switch (arg[1]) 
645     {
646         case 'l': 
647         case 'u':
648             return 1;
649         case 'W':
650             if (strncmp("-Wl,", arg, 4) == 0) return 1;
651             break;
652         case 'X':
653             if (strcmp("-Xlinker", arg) == 0) return 1;
654             break;
655     }
656
657     for (j = 0; j < sizeof(link_switches)/sizeof(link_switches[0]); j++)
658         if (strcmp(link_switches[j], arg) == 0) return 1;
659
660     return 0;
661 }
662
663 /*
664  *      Target Options
665  *          -b machine  -V version
666  */
667 static int is_target_arg(const char* arg)
668 {
669     return arg[1] == 'b' || arg[2] == 'V';
670 }
671
672
673 /*
674  *      Directory Options
675  *          -Bprefix  -Idir  -I-  -Ldir  -specs=file
676  */
677 static int is_directory_arg(const char* arg)
678 {
679     return arg[1] == 'B' || arg[1] == 'L' || arg[1] == 'I' || strncmp("-specs=", arg, 7) == 0;
680 }
681
682 /*
683  *      MinGW Options
684  *          -mno-cygwin -mwindows -mconsole -mthreads
685  */ 
686 static int is_mingw_arg(const char* arg)
687 {
688     static const char* mingw_switches[] = 
689     {
690         "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads"
691     };
692     int j;
693
694     for (j = 0; j < sizeof(mingw_switches)/sizeof(mingw_switches[0]); j++)
695         if (strcmp(mingw_switches[j], arg) == 0) return 1;
696
697     return 0;
698 }
699
700 int main(int argc, char **argv)
701 {
702     int i, c, next_is_arg = 0, linking = 1;
703     int raw_compiler_arg, raw_linker_arg;
704     const char* option_arg;
705     struct options opts;
706     char* lang = 0;
707     char* str;
708
709 #ifdef SIGHUP
710     signal( SIGHUP, exit_on_signal );
711 #endif
712     signal( SIGTERM, exit_on_signal );
713     signal( SIGINT, exit_on_signal );
714 #ifdef HAVE_SIGADDSET
715     sigemptyset( &signal_mask );
716     sigaddset( &signal_mask, SIGHUP );
717     sigaddset( &signal_mask, SIGTERM );
718     sigaddset( &signal_mask, SIGINT );
719 #endif
720
721     /* setup tmp file removal at exit */
722     tmp_files = strarray_alloc();
723     atexit(clean_temp_files);
724     
725     /* initialize options */
726     memset(&opts, 0, sizeof(opts));
727     opts.lib_dirs = strarray_alloc();
728     opts.files = strarray_alloc();
729     opts.linker_args = strarray_alloc();
730     opts.compiler_args = strarray_alloc();
731     opts.winebuild_args = strarray_alloc();
732
733     /* determine the processor type */
734     if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
735     else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
736     
737     /* parse options */
738     for ( i = 1 ; i < argc ; i++ ) 
739     {
740         if (argv[i][0] == '-')  /* option */
741         {
742             /* determine if tihs switch is followed by a separate argument */
743             next_is_arg = 0;
744             option_arg = 0;
745             switch(argv[i][1])
746             {
747                 case 'x': case 'o': case 'D': case 'U':
748                 case 'I': case 'A': case 'l': case 'u':
749                 case 'b': case 'V': case 'G': case 'L':
750                 case 'B':
751                     if (argv[i][2]) option_arg = &argv[i][2];
752                     else next_is_arg = 1;
753                     break;
754                 case 'i':
755                     next_is_arg = 1;
756                     break;
757                 case 'a':
758                     if (strcmp("-aux-info", argv[i]) == 0)
759                         next_is_arg = 1;
760                     break;
761                 case 'X':
762                     if (strcmp("-Xlinker", argv[i]) == 0)
763                         next_is_arg = 1;
764                     break;
765                 case 'M':
766                     c = argv[i][2];
767                     if (c == 'F' || c == 'T' || c == 'Q')
768                     {
769                         if (argv[i][3]) option_arg = &argv[i][3];
770                         else next_is_arg = 1;
771                     }
772                     break;
773                 case 'f':
774                     if (strcmp("-framework", argv[i]) == 0)
775                         next_is_arg = 1;
776                     break;
777             }
778             if (next_is_arg) option_arg = argv[i+1];
779
780             /* determine what options go 'as is' to the linker & the compiler */
781             raw_compiler_arg = raw_linker_arg = 0;
782             if (is_linker_arg(argv[i])) 
783             {
784                 raw_linker_arg = 1;
785             }
786             else 
787             {
788                 if (is_directory_arg(argv[i]) || is_target_arg(argv[i]))
789                     raw_linker_arg = 1;
790                 raw_compiler_arg = !is_mingw_arg(argv[i]);
791             }
792
793             /* these things we handle explicitly so we don't pass them 'as is' */
794             if (argv[i][1] == 'l' || argv[i][1] == 'I' || argv[i][1] == 'L')
795                 raw_linker_arg = 0;
796             if (argv[i][1] == 'c' || argv[i][1] == 'L')
797                 raw_compiler_arg = 0;
798             if (argv[i][1] == 'o')
799                 raw_compiler_arg = raw_linker_arg = 0;
800
801             /* do a bit of semantic analysis */
802             switch (argv[i][1]) 
803             {
804                 case 'B':
805                     str = strdup(option_arg);
806                     if (strendswith(str, "/tools/winebuild"))
807                     {
808                         opts.wine_mode = 1;
809                         /* don't pass it to the compiler, this generates warnings */
810                         raw_compiler_arg = raw_linker_arg = 0;
811                     }
812                     if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
813                     if (!opts.prefix) opts.prefix = strarray_alloc();
814                     strarray_add(opts.prefix, str);
815                     break;
816                 case 'c':        /* compile or assemble */
817                     if (argv[i][2] == 0) opts.compile_only = 1;
818                     /* fall through */
819                 case 'S':        /* generate assembler code */
820                 case 'E':        /* preprocess only */
821                     if (argv[i][2] == 0) linking = 0;
822                     break;
823                 case 'f':
824                     if (strcmp("-fno-short-wchar", argv[i]) == 0)
825                         opts.noshortwchar = 1;
826                     break;
827                 case 'l':
828                     strarray_add(opts.files, strmake("-l%s", option_arg));
829                     break;
830                 case 'L':
831                     strarray_add(opts.lib_dirs, option_arg);
832                     break;
833                 case 'M':        /* map file generation */
834                     linking = 0;
835                     break;
836                 case 'm':
837                     if (strcmp("-mno-cygwin", argv[i]) == 0)
838                         opts.use_msvcrt = 1;
839                     else if (strcmp("-mwindows", argv[i]) == 0)
840                         opts.gui_app = 1;
841                     else if (strcmp("-mconsole", argv[i]) == 0)
842                         opts.gui_app = 0;
843                     break;
844                 case 'n':
845                     if (strcmp("-nostdinc", argv[i]) == 0)
846                         opts.nostdinc = 1;
847                     else if (strcmp("-nodefaultlibs", argv[i]) == 0)
848                         opts.nodefaultlibs = 1;
849                     else if (strcmp("-nostdlib", argv[i]) == 0)
850                         opts.nostdlib = 1;
851                     break;
852                 case 'o':
853                     opts.output_name = option_arg;
854                     break;
855                 case 's':
856                     if (strcmp("-static", argv[i]) == 0) 
857                         linking = -1;
858                     else if(strcmp("-save-temps", argv[i]) == 0)
859                         keep_generated = 1;
860                     else if(strcmp("-shared", argv[i]) == 0)
861                     {
862                         opts.shared = 1;
863                         raw_compiler_arg = raw_linker_arg = 0;
864                     }
865                     break;
866                 case 'v':
867                     if (argv[i][2] == 0) verbose++;
868                     break;
869                 case 'W':
870                     if (strncmp("-Wl,", argv[i], 4) == 0)
871                     {
872                         if (strstr(argv[i], "-static"))
873                             linking = -1;
874                     }
875                     else if (strncmp("-Wb,", argv[i], 4) == 0)
876                     {
877                         strarray* Wb = strarray_fromstring(argv[i] + 4, ",");
878                         strarray_addall(opts.winebuild_args, Wb);
879                         strarray_free(Wb);
880                         /* don't pass it to the compiler, it generates errors */
881                         raw_compiler_arg = raw_linker_arg = 0;
882                     }
883                     break;
884                 case 'x':
885                     lang = strmake("-x%s", option_arg);
886                     strarray_add(opts.files, lang);
887                     /* we'll pass these flags ourselves, explicitely */
888                     raw_compiler_arg = raw_linker_arg = 0;
889                     break;
890                 case '-':
891                     if (strcmp("-static", argv[i]+1) == 0)
892                         linking = -1;
893                     break;
894             }
895
896             /* put the arg into the appropriate bucket */
897             if (raw_linker_arg) 
898             {
899                 strarray_add(opts.linker_args, argv[i]);
900                 if (next_is_arg && (i + 1 < argc)) 
901                     strarray_add(opts.linker_args, argv[i + 1]);
902             }
903             if (raw_compiler_arg)
904             {
905                 strarray_add(opts.compiler_args, argv[i]);
906                 if (next_is_arg && (i + 1 < argc))
907                     strarray_add(opts.compiler_args, argv[i + 1]);
908             }
909
910             /* skip the next token if it's an argument */
911             if (next_is_arg) i++;
912         }
913         else
914         {
915             strarray_add(opts.files, argv[i]);
916         } 
917     }
918
919     if (opts.processor == proc_cpp) linking = 0;
920     if (linking == -1) error("Static linking is not supported.");
921
922     if (opts.files->size == 0) forward(argc, argv, &opts);
923     else if (linking) build(&opts);
924     else compile(&opts, lang);
925
926     return 0;
927 }