wrc: Fixed typo for the --pedantic argument.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 --image-base
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 enum processor { proc_cc, proc_cxx, proc_cpp, proc_as };
146
147 struct options 
148 {
149     enum processor processor;
150     int shared;
151     int use_msvcrt;
152     int nostdinc;
153     int nostdlib;
154     int nostartfiles;
155     int nodefaultlibs;
156     int noshortwchar;
157     int gui_app;
158     int unicode_app;
159     int compile_only;
160     const char* wine_objdir;
161     const char* output_name;
162     const char* image_base;
163     strarray* prefix;
164     strarray* lib_dirs;
165     strarray* linker_args;
166     strarray* compiler_args;
167     strarray* winebuild_args;
168     strarray* files;
169 };
170
171 static void clean_temp_files(void)
172 {
173     int i;
174
175     if (keep_generated) return;
176
177     for (i = 0; i < tmp_files->size; i++)
178         unlink(tmp_files->base[i]);
179 }
180
181 /* clean things up when aborting on a signal */
182 static void exit_on_signal( int sig )
183 {
184     exit(1);  /* this will call the atexit functions */
185 }
186
187 static char* get_temp_file(const char* prefix, const char* suffix)
188 {
189     int fd;
190     char* tmp = strmake("%s-XXXXXX%s", prefix, suffix);
191
192 #ifdef HAVE_SIGPROCMASK
193     sigset_t old_set;
194     /* block signals while manipulating the temp files list */
195     sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
196 #endif
197     fd = mkstemps( tmp, strlen(suffix) );
198     if (fd == -1)
199     {
200         /* could not create it in current directory, try in /tmp */
201         free(tmp);
202         tmp = strmake("/tmp/%s-XXXXXX%s", prefix, suffix);
203         fd = mkstemps( tmp, strlen(suffix) );
204         if (fd == -1) error( "could not create temp file" );
205     }
206     close( fd );
207     strarray_add(tmp_files, tmp);
208 #ifdef HAVE_SIGPROCMASK
209     sigprocmask( SIG_SETMASK, &old_set, NULL );
210 #endif
211     return tmp;
212 }
213
214 static const strarray* get_translator(enum processor processor)
215 {
216     static strarray* cpp = 0;
217     static strarray* as = 0;
218     static strarray* cc = 0;
219     static strarray* cxx = 0;
220
221     switch(processor)
222     {
223         case proc_cpp: 
224             if (!cpp) cpp = strarray_fromstring(CPP, " ");
225             return cpp;
226         case proc_cc:  
227             if (!cc) cc = strarray_fromstring(CC, " ");
228             return cc;
229         case proc_cxx: 
230             if (!cxx) cxx = strarray_fromstring(CXX, " ");
231             return cxx;
232         case proc_as:
233             if (!as) as = strarray_fromstring(AS, " ");
234             return as;
235     }
236     error("Unknown processor");
237 }
238
239 static void compile(struct options* opts, const char* lang)
240 {
241     strarray* comp_args = strarray_alloc();
242     int j, gcc_defs = 0;
243
244     switch(opts->processor)
245     {
246         case proc_cpp:  gcc_defs = 1; break;
247 #ifdef __GNUC__
248         /* Note: if the C compiler is gcc we assume the C++ compiler is too */
249         /* mixing different C and C++ compilers isn't supported in configure anyway */
250         case proc_cc:  gcc_defs = 1; break;
251         case proc_cxx: gcc_defs = 1; break;
252 #else
253         case proc_cc:  gcc_defs = 0; break;
254         case proc_cxx: gcc_defs = 0; break;
255 #endif
256         case proc_as:  gcc_defs = 0; break;
257     }
258     strarray_addall(comp_args, get_translator(opts->processor));
259
260     if (opts->processor != proc_cpp)
261     {
262 #ifdef CC_FLAG_SHORT_WCHAR
263         if (!opts->wine_objdir && !opts->noshortwchar)
264         {
265             strarray_add(comp_args, CC_FLAG_SHORT_WCHAR);
266             strarray_add(comp_args, "-DWINE_UNICODE_NATIVE");
267         }
268 #endif
269         strarray_addall(comp_args, strarray_fromstring(DLLFLAGS, " "));
270     }
271
272 #ifdef _WIN64
273     strarray_add(comp_args, "-DWIN64");
274     strarray_add(comp_args, "-D_WIN64");
275     strarray_add(comp_args, "-D__WIN64");
276     strarray_add(comp_args, "-D__WIN64__");
277 #else
278     strarray_add(comp_args, "-DWIN32");
279     strarray_add(comp_args, "-D_WIN32");
280     strarray_add(comp_args, "-D__WIN32");
281     strarray_add(comp_args, "-D__WIN32__");
282 #endif
283     strarray_add(comp_args, "-D__WINNT");
284     strarray_add(comp_args, "-D__WINNT__");
285
286     if (gcc_defs)
287     {
288         strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__))");
289         strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__))");
290         strarray_add(comp_args, "-D__fastcall=__attribute__((__fastcall__))");
291         strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__))");
292         strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__))");
293         strarray_add(comp_args, "-D_fastcall=__attribute__((__fastcall__))");
294         strarray_add(comp_args, "-D__declspec(x)=__declspec_##x");
295         strarray_add(comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
296         strarray_add(comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
297         strarray_add(comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
298         strarray_add(comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
299         strarray_add(comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
300         strarray_add(comp_args, "-D__declspec_naked=__attribute__((naked))");
301         strarray_add(comp_args, "-D__declspec_noinline=__attribute__((noinline))");
302         strarray_add(comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
303         strarray_add(comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
304         strarray_add(comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
305         strarray_add(comp_args, "-D__declspec_selectany=__attribute__((weak))");
306         strarray_add(comp_args, "-D__declspec_thread=__thread");
307     }
308
309     /* Wine specific defines */
310     strarray_add(comp_args, "-D__WINE__");
311     strarray_add(comp_args, "-D__int8=char");
312     strarray_add(comp_args, "-D__int16=short");
313     /* FIXME: what about 64-bit platforms? */
314     strarray_add(comp_args, "-D__int32=int");
315 #ifdef HAVE_LONG_LONG
316     strarray_add(comp_args, "-D__int64=long long");
317 #endif
318
319     /* options we handle explicitly */
320     if (opts->compile_only)
321         strarray_add(comp_args, "-c");
322     if (opts->output_name)
323     {
324         strarray_add(comp_args, "-o");
325         strarray_add(comp_args, opts->output_name);
326     }
327
328     /* the rest of the pass-through parameters */
329     for ( j = 0 ; j < opts->compiler_args->size ; j++ ) 
330         strarray_add(comp_args, opts->compiler_args->base[j]);
331
332     /* the language option, if any */
333     if (lang && strcmp(lang, "-xnone"))
334         strarray_add(comp_args, lang);
335
336     /* last, but not least, the files */
337     for ( j = 0; j < opts->files->size; j++ )
338     {
339         if (opts->files->base[j][0] != '-')
340             strarray_add(comp_args, opts->files->base[j]);
341     }
342
343     /* standard includes come last in the include search path */
344 #ifdef __GNUC__
345 #define SYS_INCLUDE "-isystem"
346 #else
347 #define SYS_INCLUDE "-I"
348 #endif
349     if (!opts->wine_objdir && !opts->nostdinc)
350     {
351         if (opts->use_msvcrt)
352         {
353             strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/msvcrt");
354             strarray_add(comp_args, "-D__MSVCRT__");
355         }
356         strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/windows");
357     }
358 #undef SYS_INCLUDE
359
360     spawn(opts->prefix, comp_args, 0);
361 }
362
363 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
364 {
365     struct options copts;
366     char* base_name;
367
368     /* make a copy so we don't change any of the initial stuff */
369     /* a shallow copy is exactly what we want in this case */
370     base_name = get_basename(file);
371     copts = *opts;
372     copts.output_name = get_temp_file(base_name, ".o");
373     copts.compile_only = 1;
374     copts.files = strarray_alloc();
375     strarray_add(copts.files, file);
376     compile(&copts, lang);
377     strarray_free(copts.files);
378     free(base_name);
379
380     return copts.output_name;
381 }
382
383 /* check if there is a static lib associated to a given dll */
384 static char *find_static_lib( const char *dll )
385 {
386     char *lib = strmake("%s.a", dll);
387     if (get_file_type(lib) == file_arh) return lib;
388     free( lib );
389     return NULL;
390 }
391
392 /* add specified library to the list of files */
393 static void add_library( strarray *lib_dirs, strarray *files, const char *library )
394 {
395     char *static_lib, *fullname = 0;
396
397     switch(get_lib_type(lib_dirs, library, &fullname))
398     {
399     case file_arh:
400         strarray_add(files, strmake("-a%s", fullname));
401         break;
402     case file_dll:
403         strarray_add(files, strmake("-d%s", fullname));
404         if ((static_lib = find_static_lib(fullname)))
405         {
406             strarray_add(files, strmake("-a%s",static_lib));
407             free(static_lib);
408         }
409         break;
410     case file_so:
411     default:
412         /* keep it anyway, the linker may know what to do with it */
413         strarray_add(files, strmake("-l%s", library));
414         break;
415     }
416     free(fullname);
417 }
418
419 static void build(struct options* opts)
420 {
421     static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
422     strarray *lib_dirs, *files;
423     strarray *spec_args, *link_args;
424     char *output_file;
425     const char *spec_o_name;
426     const char *output_name, *spec_file, *lang;
427     const char* winebuild = getenv("WINEBUILD");
428     int generate_app_loader = 1;
429     int j;
430
431     /* NOTE: for the files array we'll use the following convention:
432      *    -axxx:  xxx is an archive (.a)
433      *    -dxxx:  xxx is a DLL (.def)
434      *    -lxxx:  xxx is an unsorted library
435      *    -oxxx:  xxx is an object (.o)
436      *    -rxxx:  xxx is a resource (.res)
437      *    -sxxx:  xxx is a shared lib (.so)
438      *    -xlll:  lll is the language (c, c++, etc.)
439      */
440
441     if (!winebuild) winebuild = "winebuild";
442
443     output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
444
445     /* 'winegcc -o app xxx.exe.so' only creates the load script */
446     if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so"))
447     {
448         create_file(output_file, 0755, app_loader_template, opts->files->base[0]);
449         return;
450     }
451
452     /* generate app loader only for .exe */
453     if (opts->shared || strendswith(output_file, ".exe.so"))
454         generate_app_loader = 0;
455
456     /* normalize the filename a bit: strip .so, ensure it has proper ext */
457     if (strendswith(output_file, ".so")) 
458         output_file[strlen(output_file) - 3] = 0;
459     if (opts->shared)
460     {
461         if ((output_name = strrchr(output_file, '/'))) output_name++;
462         else output_name = output_file;
463         if (!strchr(output_name, '.'))
464             output_file = strmake("%s.dll", output_file);
465     }
466     else if (!strendswith(output_file, ".exe"))
467         output_file = strmake("%s.exe", output_file);
468
469     /* get the filename from the path */
470     if ((output_name = strrchr(output_file, '/'))) output_name++;
471     else output_name = output_file;
472
473     /* prepare the linking path */
474     if (!opts->wine_objdir)
475     {
476         lib_dirs = strarray_dup(opts->lib_dirs);
477         for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ )
478             strarray_add(lib_dirs, stdlibpath[j]);
479     }
480     else
481     {
482         lib_dirs = strarray_alloc();
483         strarray_add(lib_dirs, strmake("%s/dlls", opts->wine_objdir));
484         strarray_add(lib_dirs, strmake("%s/libs/wine", opts->wine_objdir));
485         strarray_addall(lib_dirs, opts->lib_dirs);
486     }
487
488     /* mark the files with their appropriate type */
489     spec_file = lang = 0;
490     files = strarray_alloc();
491     for ( j = 0; j < opts->files->size; j++ )
492     {
493         const char* file = opts->files->base[j];
494         if (file[0] != '-')
495         {
496             switch(get_file_type(file))
497             {
498                 case file_def:
499                 case file_spec:
500                     if (spec_file)
501                         error("Only one spec file can be specified.");
502                     spec_file = file;
503                     break;
504                 case file_rc:
505                     /* FIXME: invoke wrc to build it */
506                     error("Can't compile .rc file at the moment: %s", file);
507                     break;
508                 case file_res:
509                     strarray_add(files, strmake("-r%s", file));
510                     break;
511                 case file_obj:
512                     strarray_add(files, strmake("-o%s", file));
513                     break;
514                 case file_arh:
515                     strarray_add(files, strmake("-a%s", file));
516                     break;
517                 case file_so:
518                     strarray_add(files, strmake("-s%s", file));
519                     break;
520                 case file_na:
521                     error("File does not exist: %s", file);
522                     break;
523                 default:
524                     file = compile_to_object(opts, file, lang);
525                     strarray_add(files, strmake("-o%s", file));
526                     break;
527             }
528         }
529         else if (file[1] == 'l')
530             add_library( lib_dirs, files, file + 2 );
531         else if (file[1] == 'x')
532             lang = file;
533     }
534     if (opts->shared && !spec_file)
535         error("A spec file is currently needed in shared mode");
536
537     /* add the default libraries, if needed */
538     if (!opts->nostdlib && opts->use_msvcrt) add_library(lib_dirs, files, "msvcrt");
539
540     if (!opts->wine_objdir && !opts->nodefaultlibs) 
541     {
542         if (opts->gui_app) 
543         {
544             add_library(lib_dirs, files, "shell32");
545             add_library(lib_dirs, files, "comdlg32");
546             add_library(lib_dirs, files, "gdi32");
547         }
548         add_library(lib_dirs, files, "advapi32");
549         add_library(lib_dirs, files, "user32");
550         add_library(lib_dirs, files, "kernel32");
551     }
552
553     if (!opts->nostartfiles) add_library(lib_dirs, files, "winecrt0");
554     if (!opts->nostdlib) add_library(lib_dirs, files, "wine");
555
556     /* run winebuild to generate the .spec.o file */
557     spec_args = strarray_alloc();
558     spec_o_name = get_temp_file(output_name, ".spec.o");
559     strarray_add(spec_args, winebuild);
560     if (verbose) strarray_add(spec_args, "-v");
561     if (keep_generated) strarray_add(spec_args, "--save-temps");
562     strarray_add(spec_args, "--as-cmd");
563     strarray_add(spec_args, AS);
564     strarray_add(spec_args, "--ld-cmd");
565     strarray_add(spec_args, LD);
566     strarray_addall(spec_args, strarray_fromstring(DLLFLAGS, " "));
567     strarray_add(spec_args, opts->shared ? "--dll" : "--exe");
568     strarray_add(spec_args, "-o");
569     strarray_add(spec_args, spec_o_name);
570     if (spec_file)
571     {
572         strarray_add(spec_args, "-E");
573         strarray_add(spec_args, spec_file);
574     }
575
576     if (!opts->shared)
577     {
578         strarray_add(spec_args, "-F");
579         strarray_add(spec_args, output_name);
580         strarray_add(spec_args, "--subsystem");
581         strarray_add(spec_args, opts->gui_app ? "windows" : "console");
582         if (opts->unicode_app)
583         {
584             strarray_add(spec_args, "--entry");
585             strarray_add(spec_args, "__wine_spec_exe_wentry");
586         }
587     }
588
589     for ( j = 0; j < lib_dirs->size; j++ )
590         strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
591
592     for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
593         strarray_add(spec_args, opts->winebuild_args->base[j]);
594
595     for ( j = 0; j < files->size; j++ )
596     {
597         const char* name = files->base[j] + 2;
598         switch(files->base[j][1])
599         {
600             case 'r':
601                 strarray_add(spec_args, files->base[j]);
602                 break;
603             case 'd':
604             case 'a':
605             case 'o':
606                 strarray_add(spec_args, name);
607                 break;
608         }
609     }
610
611     spawn(opts->prefix, spec_args, 0);
612
613     /* link everything together now */
614     link_args = strarray_alloc();
615     strarray_addall(link_args, get_translator(opts->processor));
616     strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " "));
617
618     strarray_add(link_args, "-o");
619     strarray_add(link_args, strmake("%s.so", output_file));
620
621     for ( j = 0 ; j < opts->linker_args->size ; j++ ) 
622         strarray_add(link_args, opts->linker_args->base[j]);
623
624 #ifdef __APPLE__
625     if (opts->image_base)
626     {
627         strarray_add(link_args, "-image_base");
628         strarray_add(link_args, opts->image_base);
629     }
630 #endif
631
632     for ( j = 0; j < lib_dirs->size; j++ )
633         strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
634
635     strarray_add(link_args, spec_o_name);
636
637     for ( j = 0; j < files->size; j++ )
638     {
639         const char* name = files->base[j] + 2;
640         switch(files->base[j][1])
641         {
642             case 'l':
643             case 's':
644                 strarray_add(link_args, strmake("-l%s", name));
645                 break;
646             case 'a':
647             case 'o':
648                 strarray_add(link_args, name);
649                 break;
650         }
651     }
652
653     if (!opts->nostdlib) 
654     {
655         strarray_add(link_args, "-lm");
656         strarray_add(link_args, "-lc");
657     }
658
659     spawn(opts->prefix, link_args, 0);
660
661     /* set the base address */
662     if (opts->image_base)
663     {
664         const char *prelink = PRELINK;
665         if (prelink[0] && strcmp(prelink,"false"))
666         {
667             strarray *prelink_args = strarray_alloc();
668             strarray_add(prelink_args, prelink);
669             strarray_add(prelink_args, "--reloc-only");
670             strarray_add(prelink_args, opts->image_base);
671             strarray_add(prelink_args, strmake("%s.so", output_file));
672             spawn(opts->prefix, prelink_args, 1);
673             strarray_free(prelink_args);
674         }
675     }
676
677     /* create the loader script */
678     if (generate_app_loader)
679     {
680         if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0;
681         create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name));
682     }
683 }
684
685
686 static void forward(int argc, char **argv, struct options* opts)
687 {
688     strarray* args = strarray_alloc();
689     int j;
690
691     strarray_addall(args, get_translator(opts->processor));
692
693     for( j = 1; j < argc; j++ ) 
694         strarray_add(args, argv[j]);
695
696     spawn(opts->prefix, args, 0);
697 }
698
699 /*
700  *      Linker Options
701  *          object-file-name  -llibrary -nostartfiles  -nodefaultlibs
702  *          -nostdlib -s  -static  -static-libgcc  -shared  -shared-libgcc
703  *          -symbolic -Wl,option  -Xlinker option -u symbol
704  *          -framework name
705  */
706 static int is_linker_arg(const char* arg)
707 {
708     static const char* link_switches[] = 
709     {
710         "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s", 
711         "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic",
712         "-framework"
713     };
714     int j;
715
716     switch (arg[1]) 
717     {
718         case 'l': 
719         case 'u':
720             return 1;
721         case 'W':
722             if (strncmp("-Wl,", arg, 4) == 0) return 1;
723             break;
724         case 'X':
725             if (strcmp("-Xlinker", arg) == 0) return 1;
726             break;
727     }
728
729     for (j = 0; j < sizeof(link_switches)/sizeof(link_switches[0]); j++)
730         if (strcmp(link_switches[j], arg) == 0) return 1;
731
732     return 0;
733 }
734
735 /*
736  *      Target Options
737  *          -b machine  -V version
738  */
739 static int is_target_arg(const char* arg)
740 {
741     return arg[1] == 'b' || arg[2] == 'V';
742 }
743
744
745 /*
746  *      Directory Options
747  *          -Bprefix  -Idir  -I-  -Ldir  -specs=file
748  */
749 static int is_directory_arg(const char* arg)
750 {
751     return arg[1] == 'B' || arg[1] == 'L' || arg[1] == 'I' || strncmp("-specs=", arg, 7) == 0;
752 }
753
754 /*
755  *      MinGW Options
756  *          -mno-cygwin -mwindows -mconsole -mthreads -municode
757  */ 
758 static int is_mingw_arg(const char* arg)
759 {
760     static const char* mingw_switches[] = 
761     {
762         "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads", "-municode"
763     };
764     int j;
765
766     for (j = 0; j < sizeof(mingw_switches)/sizeof(mingw_switches[0]); j++)
767         if (strcmp(mingw_switches[j], arg) == 0) return 1;
768
769     return 0;
770 }
771
772 int main(int argc, char **argv)
773 {
774     int i, c, next_is_arg = 0, linking = 1;
775     int raw_compiler_arg, raw_linker_arg;
776     const char* option_arg;
777     struct options opts;
778     char* lang = 0;
779     char* str;
780
781 #ifdef SIGHUP
782     signal( SIGHUP, exit_on_signal );
783 #endif
784     signal( SIGTERM, exit_on_signal );
785     signal( SIGINT, exit_on_signal );
786 #ifdef HAVE_SIGADDSET
787     sigemptyset( &signal_mask );
788     sigaddset( &signal_mask, SIGHUP );
789     sigaddset( &signal_mask, SIGTERM );
790     sigaddset( &signal_mask, SIGINT );
791 #endif
792
793     /* setup tmp file removal at exit */
794     tmp_files = strarray_alloc();
795     atexit(clean_temp_files);
796     
797     /* initialize options */
798     memset(&opts, 0, sizeof(opts));
799     opts.lib_dirs = strarray_alloc();
800     opts.files = strarray_alloc();
801     opts.linker_args = strarray_alloc();
802     opts.compiler_args = strarray_alloc();
803     opts.winebuild_args = strarray_alloc();
804
805     /* determine the processor type */
806     if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
807     else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
808     
809     /* parse options */
810     for ( i = 1 ; i < argc ; i++ ) 
811     {
812         if (argv[i][0] == '-')  /* option */
813         {
814             /* determine if tihs switch is followed by a separate argument */
815             next_is_arg = 0;
816             option_arg = 0;
817             switch(argv[i][1])
818             {
819                 case 'x': case 'o': case 'D': case 'U':
820                 case 'I': case 'A': case 'l': case 'u':
821                 case 'b': case 'V': case 'G': case 'L':
822                 case 'B':
823                     if (argv[i][2]) option_arg = &argv[i][2];
824                     else next_is_arg = 1;
825                     break;
826                 case 'i':
827                     next_is_arg = 1;
828                     break;
829                 case 'a':
830                     if (strcmp("-aux-info", argv[i]) == 0)
831                         next_is_arg = 1;
832                     break;
833                 case 'X':
834                     if (strcmp("-Xlinker", argv[i]) == 0)
835                         next_is_arg = 1;
836                     break;
837                 case 'M':
838                     c = argv[i][2];
839                     if (c == 'F' || c == 'T' || c == 'Q')
840                     {
841                         if (argv[i][3]) option_arg = &argv[i][3];
842                         else next_is_arg = 1;
843                     }
844                     break;
845                 case 'f':
846                     if (strcmp("-framework", argv[i]) == 0)
847                         next_is_arg = 1;
848                     break;
849             }
850             if (next_is_arg) option_arg = argv[i+1];
851
852             /* determine what options go 'as is' to the linker & the compiler */
853             raw_compiler_arg = raw_linker_arg = 0;
854             if (is_linker_arg(argv[i])) 
855             {
856                 raw_linker_arg = 1;
857             }
858             else 
859             {
860                 if (is_directory_arg(argv[i]) || is_target_arg(argv[i]))
861                     raw_linker_arg = 1;
862                 raw_compiler_arg = !is_mingw_arg(argv[i]);
863             }
864
865             /* these things we handle explicitly so we don't pass them 'as is' */
866             if (argv[i][1] == 'l' || argv[i][1] == 'I' || argv[i][1] == 'L')
867                 raw_linker_arg = 0;
868             if (argv[i][1] == 'c' || argv[i][1] == 'L')
869                 raw_compiler_arg = 0;
870             if (argv[i][1] == 'o')
871                 raw_compiler_arg = raw_linker_arg = 0;
872
873             /* do a bit of semantic analysis */
874             switch (argv[i][1]) 
875             {
876                 case 'B':
877                     str = strdup(option_arg);
878                     if (strendswith(str, "/tools/winebuild"))
879                     {
880                         char *objdir = strdup(str);
881                         objdir[strlen(objdir) - sizeof("/tools/winebuild") + 1] = 0;
882                         opts.wine_objdir = objdir;
883                         /* don't pass it to the compiler, this generates warnings */
884                         raw_compiler_arg = raw_linker_arg = 0;
885                     }
886                     if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
887                     if (!opts.prefix) opts.prefix = strarray_alloc();
888                     strarray_add(opts.prefix, str);
889                     break;
890                 case 'c':        /* compile or assemble */
891                     if (argv[i][2] == 0) opts.compile_only = 1;
892                     /* fall through */
893                 case 'S':        /* generate assembler code */
894                 case 'E':        /* preprocess only */
895                     if (argv[i][2] == 0) linking = 0;
896                     break;
897                 case 'f':
898                     if (strcmp("-fno-short-wchar", argv[i]) == 0)
899                         opts.noshortwchar = 1;
900                     break;
901                 case 'l':
902                     strarray_add(opts.files, strmake("-l%s", option_arg));
903                     break;
904                 case 'L':
905                     strarray_add(opts.lib_dirs, option_arg);
906                     break;
907                 case 'M':        /* map file generation */
908                     linking = 0;
909                     break;
910                 case 'm':
911                     if (strcmp("-mno-cygwin", argv[i]) == 0)
912                         opts.use_msvcrt = 1;
913                     else if (strcmp("-mwindows", argv[i]) == 0)
914                         opts.gui_app = 1;
915                     else if (strcmp("-mconsole", argv[i]) == 0)
916                         opts.gui_app = 0;
917                     else if (strcmp("-municode", argv[i]) == 0)
918                         opts.unicode_app = 1;
919                     else if (strcmp("-m32", argv[i]) == 0 || strcmp("-m64", argv[i]) == 0)
920                         raw_linker_arg = 1;
921                     break;
922                 case 'n':
923                     if (strcmp("-nostdinc", argv[i]) == 0)
924                         opts.nostdinc = 1;
925                     else if (strcmp("-nodefaultlibs", argv[i]) == 0)
926                         opts.nodefaultlibs = 1;
927                     else if (strcmp("-nostdlib", argv[i]) == 0)
928                         opts.nostdlib = 1;
929                     else if (strcmp("-nostartfiles", argv[i]) == 0)
930                         opts.nostartfiles = 1;
931                     break;
932                 case 'o':
933                     opts.output_name = option_arg;
934                     break;
935                 case 's':
936                     if (strcmp("-static", argv[i]) == 0) 
937                         linking = -1;
938                     else if(strcmp("-save-temps", argv[i]) == 0)
939                         keep_generated = 1;
940                     else if(strcmp("-shared", argv[i]) == 0)
941                     {
942                         opts.shared = 1;
943                         raw_compiler_arg = raw_linker_arg = 0;
944                     }
945                     break;
946                 case 'v':
947                     if (argv[i][2] == 0) verbose++;
948                     break;
949                 case 'W':
950                     if (strncmp("-Wl,", argv[i], 4) == 0)
951                     {
952                         unsigned int j;
953                         strarray* Wl = strarray_fromstring(argv[i] + 4, ",");
954                         for (j = 0; j < Wl->size; j++)
955                         {
956                             if (!strcmp(Wl->base[j], "--image-base") && j < Wl->size - 1)
957                             {
958                                 opts.image_base = strdup( Wl->base[++j] );
959                                 continue;
960                             }
961                             if (!strcmp(Wl->base[j], "-static")) linking = -1;
962                             strarray_add(opts.linker_args, strmake("-Wl,%s",Wl->base[j]));
963                         }
964                         strarray_free(Wl);
965                         raw_compiler_arg = raw_linker_arg = 0;
966                     }
967                     else if (strncmp("-Wb,", argv[i], 4) == 0)
968                     {
969                         strarray* Wb = strarray_fromstring(argv[i] + 4, ",");
970                         strarray_addall(opts.winebuild_args, Wb);
971                         strarray_free(Wb);
972                         /* don't pass it to the compiler, it generates errors */
973                         raw_compiler_arg = raw_linker_arg = 0;
974                     }
975                     break;
976                 case 'x':
977                     lang = strmake("-x%s", option_arg);
978                     strarray_add(opts.files, lang);
979                     /* we'll pass these flags ourselves, explicitely */
980                     raw_compiler_arg = raw_linker_arg = 0;
981                     break;
982                 case '-':
983                     if (strcmp("-static", argv[i]+1) == 0)
984                         linking = -1;
985                     break;
986             }
987
988             /* put the arg into the appropriate bucket */
989             if (raw_linker_arg) 
990             {
991                 strarray_add(opts.linker_args, argv[i]);
992                 if (next_is_arg && (i + 1 < argc)) 
993                     strarray_add(opts.linker_args, argv[i + 1]);
994             }
995             if (raw_compiler_arg)
996             {
997                 strarray_add(opts.compiler_args, argv[i]);
998                 if (next_is_arg && (i + 1 < argc))
999                     strarray_add(opts.compiler_args, argv[i + 1]);
1000             }
1001
1002             /* skip the next token if it's an argument */
1003             if (next_is_arg) i++;
1004         }
1005         else
1006         {
1007             strarray_add(opts.files, argv[i]);
1008         } 
1009     }
1010
1011     if (opts.processor == proc_cpp) linking = 0;
1012     if (linking == -1) error("Static linking is not supported.");
1013
1014     if (opts.files->size == 0) forward(argc, argv, &opts);
1015     else if (linking) build(&opts);
1016     else compile(&opts, lang);
1017
1018     return 0;
1019 }