push 7f8c39dca3a5819e8ef115eebf7abed537de3a22
[wine/hacks.git] / tools / winegcc / winegcc.c
blobb4a314df3aa56b119959f76565a9d07a0933eaa2
1 /*
2 * MinGW wrapper: makes gcc behave like MinGW.
4 * Copyright 2000 Manuel Novoa III
5 * Copyright 2000 Francois Gouget
6 * Copyright 2002 Dimitrie O. Paun
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.
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.
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
22 * DESCRIPTION
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:
30 * Compiler:
31 * -x language
32 * -o filename
33 * -aux-info filename
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
51 * Linking:
52 * -l library
53 * -Xlinker option
54 * -u symbol
56 * Misc:
57 * -b machine
58 * -V version
59 * -G num (see NOTES below)
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.
67 * Special interest options
69 * Assembler Option
70 * -Wa,option
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
77 * Directory Options
78 * -Bprefix -Idir -I- -Ldir -specs=file
80 * Target Options
81 * -b machine -V version
83 * Please note that the Target Options are relevant to everything:
84 * compiler, linker, assembler, preprocessor.
86 */
88 #include "config.h"
89 #include "wine/port.h"
91 #include <stdio.h>
92 #include <stdlib.h>
93 #include <signal.h>
94 #include <stdarg.h>
95 #include <string.h>
96 #include <errno.h>
98 #include "utils.h"
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"
139 static int keep_generated = 0;
140 static strarray* tmp_files;
141 #ifdef HAVE_SIGSET_T
142 static sigset_t signal_mask;
143 #endif
145 enum processor { proc_cc, proc_cxx, proc_cpp, proc_as };
147 struct options
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;
171 static void clean_temp_files(void)
173 unsigned int i;
175 if (keep_generated) return;
177 for (i = 0; i < tmp_files->size; i++)
178 unlink(tmp_files->base[i]);
181 /* clean things up when aborting on a signal */
182 static void exit_on_signal( int sig )
184 exit(1); /* this will call the atexit functions */
187 static char* get_temp_file(const char* prefix, const char* suffix)
189 int fd;
190 char* tmp = strmake("%s-XXXXXX%s", prefix, suffix);
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)
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\n" );
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;
214 static const strarray* get_translator(enum processor processor)
216 static strarray* cpp = 0;
217 static strarray* as = 0;
218 static strarray* cc = 0;
219 static strarray* cxx = 0;
221 switch(processor)
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;
236 error("Unknown processor\n");
239 static void compile(struct options* opts, const char* lang)
241 strarray* comp_args = strarray_alloc();
242 unsigned int j;
243 int gcc_defs = 0;
245 switch(opts->processor)
247 case proc_cpp: gcc_defs = 1; break;
248 #ifdef __GNUC__
249 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
250 /* mixing different C and C++ compilers isn't supported in configure anyway */
251 case proc_cc: gcc_defs = 1; break;
252 case proc_cxx: gcc_defs = 1; break;
253 #else
254 case proc_cc: gcc_defs = 0; break;
255 case proc_cxx: gcc_defs = 0; break;
256 #endif
257 case proc_as: gcc_defs = 0; break;
259 strarray_addall(comp_args, get_translator(opts->processor));
261 if (opts->processor != proc_cpp)
263 #ifdef CC_FLAG_SHORT_WCHAR
264 if (!opts->wine_objdir && !opts->noshortwchar)
266 strarray_add(comp_args, CC_FLAG_SHORT_WCHAR);
267 strarray_add(comp_args, "-DWINE_UNICODE_NATIVE");
269 #endif
270 strarray_addall(comp_args, strarray_fromstring(DLLFLAGS, " "));
273 #ifdef _WIN64
274 strarray_add(comp_args, "-DWIN64");
275 strarray_add(comp_args, "-D_WIN64");
276 strarray_add(comp_args, "-D__WIN64");
277 strarray_add(comp_args, "-D__WIN64__");
278 #else
279 strarray_add(comp_args, "-DWIN32");
280 strarray_add(comp_args, "-D_WIN32");
281 strarray_add(comp_args, "-D__WIN32");
282 strarray_add(comp_args, "-D__WIN32__");
283 #endif
284 strarray_add(comp_args, "-D__WINNT");
285 strarray_add(comp_args, "-D__WINNT__");
287 if (gcc_defs)
289 #ifdef __APPLE__ /* Mac OS X uses a 16-byte aligned stack and not a 4-byte one */
290 strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
291 strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
292 strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
293 strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
294 #else
295 strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__))");
296 strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__))");
297 strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__))");
298 strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__))");
299 #endif
301 strarray_add(comp_args, "-D__fastcall=__attribute__((__fastcall__))");
302 strarray_add(comp_args, "-D_fastcall=__attribute__((__fastcall__))");
303 strarray_add(comp_args, "-D__declspec(x)=__declspec_##x");
304 strarray_add(comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
305 strarray_add(comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
306 strarray_add(comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
307 strarray_add(comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
308 strarray_add(comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
309 strarray_add(comp_args, "-D__declspec_naked=__attribute__((naked))");
310 strarray_add(comp_args, "-D__declspec_noinline=__attribute__((noinline))");
311 strarray_add(comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
312 strarray_add(comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
313 strarray_add(comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
314 strarray_add(comp_args, "-D__declspec_selectany=__attribute__((weak))");
315 strarray_add(comp_args, "-D__declspec_thread=__thread");
318 /* Wine specific defines */
319 strarray_add(comp_args, "-D__WINE__");
320 strarray_add(comp_args, "-D__int8=char");
321 strarray_add(comp_args, "-D__int16=short");
322 /* FIXME: what about 64-bit platforms? */
323 strarray_add(comp_args, "-D__int32=int");
324 #ifdef HAVE_LONG_LONG
325 strarray_add(comp_args, "-D__int64=long long");
326 #endif
328 /* options we handle explicitly */
329 if (opts->compile_only)
330 strarray_add(comp_args, "-c");
331 if (opts->output_name)
333 strarray_add(comp_args, "-o");
334 strarray_add(comp_args, opts->output_name);
337 /* the rest of the pass-through parameters */
338 for ( j = 0 ; j < opts->compiler_args->size ; j++ )
339 strarray_add(comp_args, opts->compiler_args->base[j]);
341 /* the language option, if any */
342 if (lang && strcmp(lang, "-xnone"))
343 strarray_add(comp_args, lang);
345 /* last, but not least, the files */
346 for ( j = 0; j < opts->files->size; j++ )
348 if (opts->files->base[j][0] != '-')
349 strarray_add(comp_args, opts->files->base[j]);
352 /* standard includes come last in the include search path */
353 #ifdef __GNUC__
354 #define SYS_INCLUDE "-isystem"
355 #else
356 #define SYS_INCLUDE "-I"
357 #endif
358 if (!opts->wine_objdir && !opts->nostdinc)
360 if (opts->use_msvcrt)
362 strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/msvcrt");
363 strarray_add(comp_args, "-D__MSVCRT__");
365 strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/windows");
367 #undef SYS_INCLUDE
369 spawn(opts->prefix, comp_args, 0);
372 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
374 struct options copts;
375 char* base_name;
377 /* make a copy so we don't change any of the initial stuff */
378 /* a shallow copy is exactly what we want in this case */
379 base_name = get_basename(file);
380 copts = *opts;
381 copts.output_name = get_temp_file(base_name, ".o");
382 copts.compile_only = 1;
383 copts.files = strarray_alloc();
384 strarray_add(copts.files, file);
385 compile(&copts, lang);
386 strarray_free(copts.files);
387 free(base_name);
389 return copts.output_name;
392 /* check if there is a static lib associated to a given dll */
393 static char *find_static_lib( const char *dll )
395 char *lib = strmake("%s.a", dll);
396 if (get_file_type(lib) == file_arh) return lib;
397 free( lib );
398 return NULL;
401 /* add specified library to the list of files */
402 static void add_library( strarray *lib_dirs, strarray *files, const char *library )
404 char *static_lib, *fullname = 0;
406 switch(get_lib_type(lib_dirs, library, &fullname))
408 case file_arh:
409 strarray_add(files, strmake("-a%s", fullname));
410 break;
411 case file_dll:
412 strarray_add(files, strmake("-d%s", fullname));
413 if ((static_lib = find_static_lib(fullname)))
415 strarray_add(files, strmake("-a%s",static_lib));
416 free(static_lib);
418 break;
419 case file_so:
420 default:
421 /* keep it anyway, the linker may know what to do with it */
422 strarray_add(files, strmake("-l%s", library));
423 break;
425 free(fullname);
428 static void build(struct options* opts)
430 static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
431 strarray *lib_dirs, *files;
432 strarray *spec_args, *link_args;
433 char *output_file;
434 const char *spec_o_name;
435 const char *output_name, *spec_file, *lang;
436 const char* winebuild = getenv("WINEBUILD");
437 int generate_app_loader = 1;
438 unsigned int j;
440 /* NOTE: for the files array we'll use the following convention:
441 * -axxx: xxx is an archive (.a)
442 * -dxxx: xxx is a DLL (.def)
443 * -lxxx: xxx is an unsorted library
444 * -oxxx: xxx is an object (.o)
445 * -rxxx: xxx is a resource (.res)
446 * -sxxx: xxx is a shared lib (.so)
447 * -xlll: lll is the language (c, c++, etc.)
450 if (!winebuild) winebuild = "winebuild";
452 output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
454 /* 'winegcc -o app xxx.exe.so' only creates the load script */
455 if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so"))
457 create_file(output_file, 0755, app_loader_template, opts->files->base[0]);
458 return;
461 /* generate app loader only for .exe */
462 if (opts->shared || strendswith(output_file, ".exe.so"))
463 generate_app_loader = 0;
465 /* normalize the filename a bit: strip .so, ensure it has proper ext */
466 if (strendswith(output_file, ".so"))
467 output_file[strlen(output_file) - 3] = 0;
468 if (opts->shared)
470 if ((output_name = strrchr(output_file, '/'))) output_name++;
471 else output_name = output_file;
472 if (!strchr(output_name, '.'))
473 output_file = strmake("%s.dll", output_file);
475 else if (!strendswith(output_file, ".exe"))
476 output_file = strmake("%s.exe", output_file);
478 /* get the filename from the path */
479 if ((output_name = strrchr(output_file, '/'))) output_name++;
480 else output_name = output_file;
482 /* prepare the linking path */
483 if (!opts->wine_objdir)
485 lib_dirs = strarray_dup(opts->lib_dirs);
486 for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ )
487 strarray_add(lib_dirs, stdlibpath[j]);
489 else
491 lib_dirs = strarray_alloc();
492 strarray_add(lib_dirs, strmake("%s/dlls", opts->wine_objdir));
493 strarray_add(lib_dirs, strmake("%s/libs/wine", opts->wine_objdir));
494 strarray_addall(lib_dirs, opts->lib_dirs);
497 /* mark the files with their appropriate type */
498 spec_file = lang = 0;
499 files = strarray_alloc();
500 for ( j = 0; j < opts->files->size; j++ )
502 const char* file = opts->files->base[j];
503 if (file[0] != '-')
505 switch(get_file_type(file))
507 case file_def:
508 case file_spec:
509 if (spec_file)
510 error("Only one spec file can be specified\n");
511 spec_file = file;
512 break;
513 case file_rc:
514 /* FIXME: invoke wrc to build it */
515 error("Can't compile .rc file at the moment: %s\n", file);
516 break;
517 case file_res:
518 strarray_add(files, strmake("-r%s", file));
519 break;
520 case file_obj:
521 strarray_add(files, strmake("-o%s", file));
522 break;
523 case file_arh:
524 strarray_add(files, strmake("-a%s", file));
525 break;
526 case file_so:
527 strarray_add(files, strmake("-s%s", file));
528 break;
529 case file_na:
530 error("File does not exist: %s\n", file);
531 break;
532 default:
533 file = compile_to_object(opts, file, lang);
534 strarray_add(files, strmake("-o%s", file));
535 break;
538 else if (file[1] == 'l')
539 add_library( lib_dirs, files, file + 2 );
540 else if (file[1] == 'x')
541 lang = file;
543 if (opts->shared && !spec_file)
544 error("A spec file is currently needed in shared mode\n");
546 /* add the default libraries, if needed */
547 if (!opts->nostdlib && opts->use_msvcrt) add_library(lib_dirs, files, "msvcrt");
549 if (!opts->wine_objdir && !opts->nodefaultlibs)
551 if (opts->gui_app)
553 add_library(lib_dirs, files, "shell32");
554 add_library(lib_dirs, files, "comdlg32");
555 add_library(lib_dirs, files, "gdi32");
557 add_library(lib_dirs, files, "advapi32");
558 add_library(lib_dirs, files, "user32");
559 add_library(lib_dirs, files, "kernel32");
562 if (!opts->nostartfiles) add_library(lib_dirs, files, "winecrt0");
563 if (!opts->nostdlib) add_library(lib_dirs, files, "wine");
565 /* run winebuild to generate the .spec.o file */
566 spec_args = strarray_alloc();
567 spec_o_name = get_temp_file(output_name, ".spec.o");
568 strarray_add(spec_args, winebuild);
569 if (verbose) strarray_add(spec_args, "-v");
570 if (keep_generated) strarray_add(spec_args, "--save-temps");
571 strarray_add(spec_args, "--as-cmd");
572 strarray_add(spec_args, AS);
573 strarray_add(spec_args, "--ld-cmd");
574 strarray_add(spec_args, LD);
575 strarray_add(spec_args, "--nm-cmd");
576 strarray_add(spec_args, NM);
577 strarray_addall(spec_args, strarray_fromstring(DLLFLAGS, " "));
578 strarray_add(spec_args, opts->shared ? "--dll" : "--exe");
579 strarray_add(spec_args, "-o");
580 strarray_add(spec_args, spec_o_name);
581 if (spec_file)
583 strarray_add(spec_args, "-E");
584 strarray_add(spec_args, spec_file);
587 if (!opts->shared)
589 strarray_add(spec_args, "-F");
590 strarray_add(spec_args, output_name);
591 strarray_add(spec_args, "--subsystem");
592 strarray_add(spec_args, opts->gui_app ? "windows" : "console");
593 if (opts->unicode_app)
595 strarray_add(spec_args, "--entry");
596 strarray_add(spec_args, "__wine_spec_exe_wentry");
600 for ( j = 0; j < lib_dirs->size; j++ )
601 strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
603 for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
604 strarray_add(spec_args, opts->winebuild_args->base[j]);
606 for ( j = 0; j < files->size; j++ )
608 const char* name = files->base[j] + 2;
609 switch(files->base[j][1])
611 case 'r':
612 strarray_add(spec_args, files->base[j]);
613 break;
614 case 'd':
615 case 'a':
616 case 'o':
617 strarray_add(spec_args, name);
618 break;
622 spawn(opts->prefix, spec_args, 0);
624 /* link everything together now */
625 link_args = strarray_alloc();
626 strarray_addall(link_args, get_translator(opts->processor));
627 strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " "));
629 strarray_add(link_args, "-o");
630 strarray_add(link_args, strmake("%s.so", output_file));
632 for ( j = 0 ; j < opts->linker_args->size ; j++ )
633 strarray_add(link_args, opts->linker_args->base[j]);
635 #ifdef __APPLE__
636 if (opts->image_base)
638 strarray_add(link_args, "-image_base");
639 strarray_add(link_args, opts->image_base);
641 #endif
643 for ( j = 0; j < lib_dirs->size; j++ )
644 strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
646 strarray_add(link_args, spec_o_name);
648 for ( j = 0; j < files->size; j++ )
650 const char* name = files->base[j] + 2;
651 switch(files->base[j][1])
653 case 'l':
654 case 's':
655 strarray_add(link_args, strmake("-l%s", name));
656 break;
657 case 'a':
658 case 'o':
659 strarray_add(link_args, name);
660 break;
664 if (!opts->nostdlib)
666 strarray_add(link_args, "-lm");
667 strarray_add(link_args, "-lc");
670 spawn(opts->prefix, link_args, 0);
672 /* set the base address */
673 if (opts->image_base)
675 const char *prelink = PRELINK;
676 if (prelink[0] && strcmp(prelink,"false"))
678 strarray *prelink_args = strarray_alloc();
679 strarray_add(prelink_args, prelink);
680 strarray_add(prelink_args, "--reloc-only");
681 strarray_add(prelink_args, opts->image_base);
682 strarray_add(prelink_args, strmake("%s.so", output_file));
683 spawn(opts->prefix, prelink_args, 1);
684 strarray_free(prelink_args);
688 /* create the loader script */
689 if (generate_app_loader)
691 if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0;
692 create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name));
697 static void forward(int argc, char **argv, struct options* opts)
699 strarray* args = strarray_alloc();
700 int j;
702 strarray_addall(args, get_translator(opts->processor));
704 for( j = 1; j < argc; j++ )
705 strarray_add(args, argv[j]);
707 spawn(opts->prefix, args, 0);
711 * Linker Options
712 * object-file-name -llibrary -nostartfiles -nodefaultlibs
713 * -nostdlib -s -static -static-libgcc -shared -shared-libgcc
714 * -symbolic -Wl,option -Xlinker option -u symbol
715 * -framework name
717 static int is_linker_arg(const char* arg)
719 static const char* link_switches[] =
721 "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s",
722 "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic",
723 "-framework"
725 unsigned int j;
727 switch (arg[1])
729 case 'R':
730 case 'z':
731 case 'l':
732 case 'u':
733 return 1;
734 case 'W':
735 if (strncmp("-Wl,", arg, 4) == 0) return 1;
736 break;
737 case 'X':
738 if (strcmp("-Xlinker", arg) == 0) return 1;
739 break;
742 for (j = 0; j < sizeof(link_switches)/sizeof(link_switches[0]); j++)
743 if (strcmp(link_switches[j], arg) == 0) return 1;
745 return 0;
749 * Target Options
750 * -b machine -V version
752 static int is_target_arg(const char* arg)
754 return arg[1] == 'b' || arg[2] == 'V';
759 * Directory Options
760 * -Bprefix -Idir -I- -Ldir -specs=file
762 static int is_directory_arg(const char* arg)
764 return arg[1] == 'B' || arg[1] == 'L' || arg[1] == 'I' || strncmp("-specs=", arg, 7) == 0;
768 * MinGW Options
769 * -mno-cygwin -mwindows -mconsole -mthreads -municode
771 static int is_mingw_arg(const char* arg)
773 static const char* mingw_switches[] =
775 "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads", "-municode"
777 unsigned int j;
779 for (j = 0; j < sizeof(mingw_switches)/sizeof(mingw_switches[0]); j++)
780 if (strcmp(mingw_switches[j], arg) == 0) return 1;
782 return 0;
785 int main(int argc, char **argv)
787 int i, c, next_is_arg = 0, linking = 1;
788 int raw_compiler_arg, raw_linker_arg;
789 const char* option_arg;
790 struct options opts;
791 char* lang = 0;
792 char* str;
794 #ifdef SIGHUP
795 signal( SIGHUP, exit_on_signal );
796 #endif
797 signal( SIGTERM, exit_on_signal );
798 signal( SIGINT, exit_on_signal );
799 #ifdef HAVE_SIGADDSET
800 sigemptyset( &signal_mask );
801 sigaddset( &signal_mask, SIGHUP );
802 sigaddset( &signal_mask, SIGTERM );
803 sigaddset( &signal_mask, SIGINT );
804 #endif
806 /* setup tmp file removal at exit */
807 tmp_files = strarray_alloc();
808 atexit(clean_temp_files);
810 /* initialize options */
811 memset(&opts, 0, sizeof(opts));
812 opts.lib_dirs = strarray_alloc();
813 opts.files = strarray_alloc();
814 opts.linker_args = strarray_alloc();
815 opts.compiler_args = strarray_alloc();
816 opts.winebuild_args = strarray_alloc();
818 /* determine the processor type */
819 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
820 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
822 /* parse options */
823 for ( i = 1 ; i < argc ; i++ )
825 if (argv[i][0] == '-') /* option */
827 /* determine if tihs switch is followed by a separate argument */
828 next_is_arg = 0;
829 option_arg = 0;
830 switch(argv[i][1])
832 case 'x': case 'o': case 'D': case 'U':
833 case 'I': case 'A': case 'l': case 'u':
834 case 'b': case 'V': case 'G': case 'L':
835 case 'B': case 'R': case 'z':
836 if (argv[i][2]) option_arg = &argv[i][2];
837 else next_is_arg = 1;
838 break;
839 case 'i':
840 next_is_arg = 1;
841 break;
842 case 'a':
843 if (strcmp("-aux-info", argv[i]) == 0)
844 next_is_arg = 1;
845 break;
846 case 'X':
847 if (strcmp("-Xlinker", argv[i]) == 0)
848 next_is_arg = 1;
849 break;
850 case 'M':
851 c = argv[i][2];
852 if (c == 'F' || c == 'T' || c == 'Q')
854 if (argv[i][3]) option_arg = &argv[i][3];
855 else next_is_arg = 1;
857 break;
858 case 'f':
859 if (strcmp("-framework", argv[i]) == 0)
860 next_is_arg = 1;
861 break;
863 if (next_is_arg) option_arg = argv[i+1];
865 /* determine what options go 'as is' to the linker & the compiler */
866 raw_compiler_arg = raw_linker_arg = 0;
867 if (is_linker_arg(argv[i]))
869 raw_linker_arg = 1;
871 else
873 if (is_directory_arg(argv[i]) || is_target_arg(argv[i]))
874 raw_linker_arg = 1;
875 raw_compiler_arg = !is_mingw_arg(argv[i]);
878 /* these things we handle explicitly so we don't pass them 'as is' */
879 if (argv[i][1] == 'l' || argv[i][1] == 'I' || argv[i][1] == 'L')
880 raw_linker_arg = 0;
881 if (argv[i][1] == 'c' || argv[i][1] == 'L')
882 raw_compiler_arg = 0;
883 if (argv[i][1] == 'o')
884 raw_compiler_arg = raw_linker_arg = 0;
886 /* do a bit of semantic analysis */
887 switch (argv[i][1])
889 case 'B':
890 str = strdup(option_arg);
891 if (strendswith(str, "/tools/winebuild"))
893 char *objdir = strdup(str);
894 objdir[strlen(objdir) - sizeof("/tools/winebuild") + 1] = 0;
895 opts.wine_objdir = objdir;
896 /* don't pass it to the compiler, this generates warnings */
897 raw_compiler_arg = raw_linker_arg = 0;
899 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
900 if (!opts.prefix) opts.prefix = strarray_alloc();
901 strarray_add(opts.prefix, str);
902 break;
903 case 'c': /* compile or assemble */
904 if (argv[i][2] == 0) opts.compile_only = 1;
905 /* fall through */
906 case 'S': /* generate assembler code */
907 case 'E': /* preprocess only */
908 if (argv[i][2] == 0) linking = 0;
909 break;
910 case 'f':
911 if (strcmp("-fno-short-wchar", argv[i]) == 0)
912 opts.noshortwchar = 1;
913 break;
914 case 'l':
915 strarray_add(opts.files, strmake("-l%s", option_arg));
916 break;
917 case 'L':
918 strarray_add(opts.lib_dirs, option_arg);
919 break;
920 case 'M': /* map file generation */
921 linking = 0;
922 break;
923 case 'm':
924 if (strcmp("-mno-cygwin", argv[i]) == 0)
925 opts.use_msvcrt = 1;
926 else if (strcmp("-mwindows", argv[i]) == 0)
927 opts.gui_app = 1;
928 else if (strcmp("-mconsole", argv[i]) == 0)
929 opts.gui_app = 0;
930 else if (strcmp("-municode", argv[i]) == 0)
931 opts.unicode_app = 1;
932 else if (strcmp("-m32", argv[i]) == 0 || strcmp("-m64", argv[i]) == 0)
933 raw_linker_arg = 1;
934 break;
935 case 'n':
936 if (strcmp("-nostdinc", argv[i]) == 0)
937 opts.nostdinc = 1;
938 else if (strcmp("-nodefaultlibs", argv[i]) == 0)
939 opts.nodefaultlibs = 1;
940 else if (strcmp("-nostdlib", argv[i]) == 0)
941 opts.nostdlib = 1;
942 else if (strcmp("-nostartfiles", argv[i]) == 0)
943 opts.nostartfiles = 1;
944 break;
945 case 'o':
946 opts.output_name = option_arg;
947 break;
948 case 's':
949 if (strcmp("-static", argv[i]) == 0)
950 linking = -1;
951 else if(strcmp("-save-temps", argv[i]) == 0)
952 keep_generated = 1;
953 else if(strcmp("-shared", argv[i]) == 0)
955 opts.shared = 1;
956 raw_compiler_arg = raw_linker_arg = 0;
958 break;
959 case 'v':
960 if (argv[i][2] == 0) verbose++;
961 break;
962 case 'W':
963 if (strncmp("-Wl,", argv[i], 4) == 0)
965 unsigned int j;
966 strarray* Wl = strarray_fromstring(argv[i] + 4, ",");
967 for (j = 0; j < Wl->size; j++)
969 if (!strcmp(Wl->base[j], "--image-base") && j < Wl->size - 1)
971 opts.image_base = strdup( Wl->base[++j] );
972 continue;
974 if (!strcmp(Wl->base[j], "-static")) linking = -1;
975 strarray_add(opts.linker_args, strmake("-Wl,%s",Wl->base[j]));
977 strarray_free(Wl);
978 raw_compiler_arg = raw_linker_arg = 0;
980 else if (strncmp("-Wb,", argv[i], 4) == 0)
982 strarray* Wb = strarray_fromstring(argv[i] + 4, ",");
983 strarray_addall(opts.winebuild_args, Wb);
984 strarray_free(Wb);
985 /* don't pass it to the compiler, it generates errors */
986 raw_compiler_arg = raw_linker_arg = 0;
988 break;
989 case 'x':
990 lang = strmake("-x%s", option_arg);
991 strarray_add(opts.files, lang);
992 /* we'll pass these flags ourselves, explicitly */
993 raw_compiler_arg = raw_linker_arg = 0;
994 break;
995 case '-':
996 if (strcmp("-static", argv[i]+1) == 0)
997 linking = -1;
998 break;
1001 /* put the arg into the appropriate bucket */
1002 if (raw_linker_arg)
1004 strarray_add(opts.linker_args, argv[i]);
1005 if (next_is_arg && (i + 1 < argc))
1006 strarray_add(opts.linker_args, argv[i + 1]);
1008 if (raw_compiler_arg)
1010 strarray_add(opts.compiler_args, argv[i]);
1011 if (next_is_arg && (i + 1 < argc))
1012 strarray_add(opts.compiler_args, argv[i + 1]);
1015 /* skip the next token if it's an argument */
1016 if (next_is_arg) i++;
1018 else
1020 strarray_add(opts.files, argv[i]);
1024 if (opts.processor == proc_cpp) linking = 0;
1025 if (linking == -1) error("Static linking is not supported\n");
1027 if (opts.files->size == 0) forward(argc, argv, &opts);
1028 else if (linking) build(&opts);
1029 else compile(&opts, lang);
1031 return 0;