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