Handle file mappings on removable media entirely inside ntdll.
[wine.git] / tools / winegcc / winegcc.c
blobb4bdf613db4f90a14b41c8a684c3285fd7162534
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
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 <stdarg.h>
94 #include <string.h>
95 #include <errno.h>
97 #include "utils.h"
99 static const char* app_loader_template =
100 "#!/bin/sh\n"
101 "\n"
102 "appname=\"%s\"\n"
103 "# determine the application directory\n"
104 "appdir=''\n"
105 "case \"$0\" in\n"
106 " */*)\n"
107 " # $0 contains a path, use it\n"
108 " appdir=`dirname \"$0\"`\n"
109 " ;;\n"
110 " *)\n"
111 " # no directory in $0, search in PATH\n"
112 " saved_ifs=$IFS\n"
113 " IFS=:\n"
114 " for d in $PATH\n"
115 " do\n"
116 " IFS=$saved_ifs\n"
117 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
118 " done\n"
119 " ;;\n"
120 "esac\n"
121 "\n"
122 "while true; do\n"
123 " case \"$1\" in\n"
124 " --debugmsg)\n"
125 " debugmsg=\"$1 $2\"\n"
126 " shift; shift;\n"
127 " ;;\n"
128 " *)\n"
129 " break\n"
130 " ;;\n"
131 " esac\n"
132 "done\n"
133 "\n"
134 "# figure out the full app path\n"
135 "if [ -n \"$appdir\" ]; then\n"
136 " apppath=\"$appdir/$appname\"\n"
137 " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
138 " export WINEDLLPATH\n"
139 "else\n"
140 " apppath=\"$appname\"\n"
141 "fi\n"
142 "\n"
143 "# determine the WINELOADER\n"
144 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
145 "\n"
146 "# and try to start the app\n"
147 "exec \"$WINELOADER\" $debugmsg -- \"$apppath\" \"$@\"\n"
150 static int keep_generated = 0;
151 static strarray* tmp_files;
153 struct options
155 enum { proc_cc = 0, proc_cxx = 1, proc_cpp = 2} processor;
156 int shared;
157 int use_msvcrt;
158 int nostdinc;
159 int nostdlib;
160 int nodefaultlibs;
161 int noshortwchar;
162 int gui_app;
163 int compile_only;
164 int wine_mode;
165 const char* output_name;
166 strarray* prefix;
167 strarray* lib_dirs;
168 strarray* linker_args;
169 strarray* compiler_args;
170 strarray* winebuild_args;
171 strarray* files;
174 static void clean_temp_files()
176 int i;
178 if (keep_generated) return;
180 for (i = 0; i < tmp_files->size; i++)
181 unlink(tmp_files->base[i]);
184 char* get_temp_file(const char* prefix, const char* suffix)
186 char* tmp = strmake("%s-XXXXXX%s", prefix, suffix);
187 int fd = mkstemps( tmp, strlen(suffix) );
188 if (fd == -1)
190 /* could not create it in current directory, try in /tmp */
191 free(tmp);
192 tmp = strmake("/tmp/%s-XXXXXX%s", prefix, suffix);
193 fd = mkstemps( tmp, strlen(suffix) );
194 if (fd == -1) error( "could not create temp file" );
196 close( fd );
197 strarray_add(tmp_files, tmp);
199 return tmp;
202 static const strarray* get_translator(struct options* opts)
204 static strarray* cpp = 0;
205 static strarray* cc = 0;
206 static strarray* cxx = 0;
208 switch(opts->processor)
210 case proc_cpp:
211 if (!cpp) cpp = strarray_fromstring(CPP, " ");
212 return cpp;
213 case proc_cc:
214 if (!cc) cc = strarray_fromstring(CC, " ");
215 return cc;
216 case proc_cxx:
217 if (!cxx) cxx = strarray_fromstring(CXX, " ");
218 return cxx;
220 error("Unknown processor");
223 static void compile(struct options* opts)
225 strarray* comp_args = strarray_alloc();
226 int j, gcc_defs = 0;
228 switch(opts->processor)
230 case proc_cpp: gcc_defs = 1; break;
231 #ifdef __GNUC__
232 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
233 /* mixing different C and C++ compilers isn't supported in configure anyway */
234 case proc_cc: gcc_defs = 1; break;
235 case proc_cxx: gcc_defs = 1; break;
236 #else
237 case proc_cc: gcc_defs = 0; break;
238 case proc_cxx: gcc_defs = 0; break;
239 #endif
241 strarray_addall(comp_args, get_translator(opts));
243 if (opts->processor != proc_cpp)
245 #ifdef CC_FLAG_SHORT_WCHAR
246 if (!opts->wine_mode && !opts->noshortwchar)
248 strarray_add(comp_args, CC_FLAG_SHORT_WCHAR);
249 strarray_add(comp_args, "-DWINE_UNICODE_NATIVE");
251 #endif
252 strarray_addall(comp_args, strarray_fromstring(DLLFLAGS, " "));
254 if (!opts->wine_mode && !opts->nostdinc)
256 if (opts->use_msvcrt)
258 strarray_add(comp_args, "-I" INCLUDEDIR "/msvcrt");
259 strarray_add(comp_args, "-D__MSVCRT__");
261 strarray_add(comp_args, "-I" INCLUDEDIR "/windows");
263 strarray_add(comp_args, "-DWIN32");
264 strarray_add(comp_args, "-D_WIN32");
265 strarray_add(comp_args, "-D__WIN32");
266 strarray_add(comp_args, "-D__WIN32__");
267 strarray_add(comp_args, "-D__WINNT");
268 strarray_add(comp_args, "-D__WINNT__");
270 if (gcc_defs)
272 strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__))");
273 strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__))");
274 strarray_add(comp_args, "-D__fastcall=__attribute__((__fastcall__))");
275 strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__))");
276 strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__))");
277 strarray_add(comp_args, "-D_fastcall=__attribute__((__fastcall__))");
278 strarray_add(comp_args, "-D__declspec(x)=__declspec_##x");
279 strarray_add(comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
280 strarray_add(comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
281 strarray_add(comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
282 strarray_add(comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
283 strarray_add(comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
284 strarray_add(comp_args, "-D__declspec_naked=__attribute__((naked))");
285 strarray_add(comp_args, "-D__declspec_noinline=__attribute__((noinline))");
286 strarray_add(comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
287 strarray_add(comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
288 strarray_add(comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
289 strarray_add(comp_args, "-D__declspec_selectany=__attribute__((weak))");
290 strarray_add(comp_args, "-D__declspec_thread=__thread");
293 /* Wine specific defines */
294 strarray_add(comp_args, "-D__WINE__");
295 strarray_add(comp_args, "-D__int8=char");
296 strarray_add(comp_args, "-D__int16=short");
297 /* FIXME: what about 64-bit platforms? */
298 strarray_add(comp_args, "-D__int32=int");
299 #ifdef HAVE_LONG_LONG
300 strarray_add(comp_args, "-D__int64=long long");
301 #endif
303 /* options we handle explicitly */
304 if (opts->compile_only)
305 strarray_add(comp_args, "-c");
306 if (opts->output_name)
308 strarray_add(comp_args, "-o");
309 strarray_add(comp_args, opts->output_name);
312 /* the rest of the pass-through parameters */
313 for ( j = 0 ; j < opts->compiler_args->size ; j++ )
314 strarray_add(comp_args, opts->compiler_args->base[j]);
316 /* last, but not least, the files */
317 for ( j = 0; j < opts->files->size; j++ )
318 strarray_add(comp_args, opts->files->base[j]);
320 spawn(opts->prefix, comp_args);
323 static const char* compile_to_object(struct options* opts, const char* file)
325 struct options copts;
326 char* base_name;
328 /* make a copy we so don't change any of the initial stuff */
329 /* a shallow copy is exactly what we want in this case */
330 base_name = get_basename(file);
331 copts = *opts;
332 copts.processor = proc_cc;
333 copts.output_name = get_temp_file(base_name, ".o");
334 copts.compile_only = 1;
335 copts.files = strarray_alloc();
336 strarray_add(copts.files, file);
337 compile(&copts);
338 strarray_free(copts.files);
339 free(base_name);
341 return copts.output_name;
344 static void build(struct options* opts)
346 static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
347 strarray *lib_dirs, *files;
348 strarray *spec_args, *comp_args, *link_args;
349 char *output_file;
350 const char *spec_c_name, *spec_o_name;
351 const char *output_name, *spec_file;
352 const char* winebuild = getenv("WINEBUILD");
353 int generate_app_loader = 1;
354 int j;
356 /* NOTE: for the files array we'll use the following convention:
357 * -axxx: xxx is an archive (.a)
358 * -dxxx: xxx is a DLL (.def)
359 * -lxxx: xxx is an unsorted library
360 * -oxxx: xxx is an object (.o)
361 * -rxxx: xxx is a resource (.res)
362 * -sxxx: xxx is a shared lib (.so)
365 if (!winebuild) winebuild = "winebuild";
367 output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
369 /* 'winegcc -o app xxx.exe.so' only creates the load script */
370 if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so"))
372 create_file(output_file, 0755, app_loader_template, opts->files->base[0]);
373 return;
376 /* generate app loader only for .exe */
377 if (opts->shared || strendswith(output_file, ".exe.so"))
378 generate_app_loader = 0;
380 /* normalize the filename a bit: strip .so, ensure it has proper ext */
381 if (strendswith(output_file, ".so"))
382 output_file[strlen(output_file) - 3] = 0;
383 if (opts->shared)
385 if ((output_name = strrchr(output_file, '/'))) output_name++;
386 else output_name = output_file;
387 if (!strchr(output_name, '.'))
388 output_file = strmake("%s.dll", output_file);
390 else if (!strendswith(output_file, ".exe"))
391 output_file = strmake("%s.exe", output_file);
393 /* get the filename by the path, if present */
394 if ((output_name = strrchr(output_file, '/'))) output_name++;
395 else output_name = output_file;
397 /* prepare the linking path */
398 lib_dirs = strarray_dup(opts->lib_dirs);
399 if (!opts->wine_mode)
401 for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ )
402 strarray_add(lib_dirs, stdlibpath[j]);
405 /* mark the files with their appropriate type */
406 spec_file = 0;
407 files = strarray_alloc();
408 for ( j = 0; j < opts->files->size; j++ )
410 const char* file = opts->files->base[j];
411 if (file[0] != '-')
413 switch(get_file_type(file))
415 case file_def:
416 case file_spec:
417 if (!opts->shared)
418 error("Spec file %s not supported in non-shared mode", file);
419 if (spec_file)
420 error("Only one spec file can be specified in shared mode");
421 spec_file = file;
422 break;
423 case file_rc:
424 /* FIXME: invoke wrc to build it */
425 error("Can't compile .rc file at the moment: %s", file);
426 break;
427 case file_res:
428 strarray_add(files, strmake("-r%s", file));
429 break;
430 case file_obj:
431 strarray_add(files, strmake("-o%s", file));
432 break;
433 case file_arh:
434 strarray_add(files, strmake("-a%s", file));
435 break;
436 case file_so:
437 strarray_add(files, strmake("-s%s", file));
438 break;
439 case file_na:
440 error("File does not exist: %s", file);
441 break;
442 default:
443 file = compile_to_object(opts, file);
444 strarray_add(files, strmake("-o%s", file));
445 break;
448 else if (file[1] == 'l')
450 char* fullname = 0;
451 switch(get_lib_type(lib_dirs, file + 2, &fullname))
453 case file_arh:
454 strarray_add(files, strmake("-a%s", fullname));
455 break;
456 case file_dll:
457 strarray_add(files, strmake("-d%s", file + 2));
458 break;
459 case file_so:
460 strarray_add(files, strmake("-s%s", file + 2));
461 break;
462 default:
463 /* keep it anyway, the linker may know what to do with it */
464 strarray_add(files, file);
465 break;
467 free(fullname);
470 if (opts->shared && !spec_file)
471 error("A spec file is currently needed in shared mode");
473 /* add the default libraries, if needed */
474 if (!opts->nostdlib)
476 if (opts->use_msvcrt) strarray_add(files, "-dmsvcrt");
479 if (!opts->wine_mode && !opts->nodefaultlibs)
481 if (opts->gui_app)
483 strarray_add(files, "-dshell32");
484 strarray_add(files, "-dcomdlg32");
485 strarray_add(files, "-dgdi32");
487 strarray_add(files, "-dadvapi32");
488 strarray_add(files, "-duser32");
489 strarray_add(files, "-dkernel32");
492 /* run winebuild to generate the .spec.c file */
493 spec_args = strarray_alloc();
494 spec_c_name = get_temp_file(output_name, ".spec.c");
495 strarray_add(spec_args, winebuild);
496 strarray_add(spec_args, "-o");
497 strarray_add(spec_args, spec_c_name);
498 if (opts->shared)
500 strarray_add(spec_args, "--dll");
501 strarray_add(spec_args, spec_file);
503 else
505 strarray_add(spec_args, "--exe");
506 strarray_add(spec_args, output_name);
507 strarray_add(spec_args, "--subsystem");
508 strarray_add(spec_args, opts->gui_app ? "windows" : "console");
511 for ( j = 0; j < lib_dirs->size; j++ )
512 strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
514 for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
515 strarray_add(spec_args, opts->winebuild_args->base[j]);
517 for ( j = 0; j < files->size; j++ )
519 const char* name = files->base[j] + 2;
520 switch(files->base[j][1])
522 case 'd':
523 strarray_add(spec_args, strmake("-l%s", name));
524 break;
525 case 'r':
526 strarray_add(spec_args, files->base[j]);
527 break;
528 case 'a':
529 case 'o':
530 strarray_add(spec_args, name);
531 break;
535 spawn(opts->prefix, spec_args);
537 /* compile the .spec.c file into a .spec.o file */
538 comp_args = strarray_alloc();
539 spec_o_name = compile_to_object(opts, spec_c_name);
541 /* link everything together now */
542 link_args = strarray_alloc();
543 strarray_addall(link_args, get_translator(opts));
544 strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " "));
546 strarray_add(link_args, "-o");
547 strarray_add(link_args, strmake("%s.so", output_file));
549 for ( j = 0 ; j < opts->linker_args->size ; j++ )
550 strarray_add(link_args, opts->linker_args->base[j]);
552 for ( j = 0; j < lib_dirs->size; j++ )
553 strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
555 strarray_add(link_args, spec_o_name);
557 for ( j = 0; j < files->size; j++ )
559 const char* name = files->base[j] + 2;
560 switch(files->base[j][1])
562 case 'l':
563 case 's':
564 strarray_add(link_args, strmake("-l%s", name));
565 break;
566 case 'a':
567 case 'o':
568 strarray_add(link_args, name);
569 break;
573 if (!opts->nostdlib)
575 strarray_add(link_args, "-lwine");
576 strarray_add(link_args, "-lm");
577 strarray_add(link_args, "-lc");
580 spawn(opts->prefix, link_args);
582 /* create the loader script */
583 if (generate_app_loader)
585 if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0;
586 create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
591 static void forward(int argc, char **argv, struct options* opts)
593 strarray* args = strarray_alloc();
594 int j;
596 strarray_addall(args, get_translator(opts));
598 for( j = 1; j < argc; j++ )
599 strarray_add(args, argv[j]);
601 spawn(opts->prefix, args);
605 * Linker Options
606 * object-file-name -llibrary -nostartfiles -nodefaultlibs
607 * -nostdlib -s -static -static-libgcc -shared -shared-libgcc
608 * -symbolic -Wl,option -Xlinker option -u symbol
610 static int is_linker_arg(const char* arg)
612 static const char* link_switches[] =
614 "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s",
615 "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic"
617 int j;
619 switch (arg[1])
621 case 'l':
622 case 'u':
623 return 1;
624 case 'W':
625 if (strncmp("-Wl,", arg, 4) == 0) return 1;
626 break;
627 case 'X':
628 if (strcmp("-Xlinker", arg) == 0) return 1;
629 break;
632 for (j = 0; j < sizeof(link_switches)/sizeof(link_switches[0]); j++)
633 if (strcmp(link_switches[j], arg) == 0) return 1;
635 return 0;
639 * Target Options
640 * -b machine -V version
642 static int is_target_arg(const char* arg)
644 return arg[1] == 'b' || arg[2] == 'V';
649 * Directory Options
650 * -Bprefix -Idir -I- -Ldir -specs=file
652 static int is_directory_arg(const char* arg)
654 return arg[1] == 'B' || arg[1] == 'L' || arg[1] == 'I' || strncmp("-specs=", arg, 7) == 0;
658 * MinGW Options
659 * -mno-cygwin -mwindows -mconsole -mthreads
661 static int is_mingw_arg(const char* arg)
663 static const char* mingw_switches[] =
665 "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads"
667 int j;
669 for (j = 0; j < sizeof(mingw_switches)/sizeof(mingw_switches[0]); j++)
670 if (strcmp(mingw_switches[j], arg) == 0) return 1;
672 return 0;
675 int main(int argc, char **argv)
677 int i, c, next_is_arg = 0, linking = 1;
678 int raw_compiler_arg, raw_linker_arg;
679 const char* option_arg;
680 struct options opts;
681 char* str;
683 /* setup tmp file removal at exit */
684 tmp_files = strarray_alloc();
685 atexit(clean_temp_files);
687 /* initialize options */
688 memset(&opts, 0, sizeof(opts));
689 opts.lib_dirs = strarray_alloc();
690 opts.files = strarray_alloc();
691 opts.linker_args = strarray_alloc();
692 opts.compiler_args = strarray_alloc();
693 opts.winebuild_args = strarray_alloc();
695 /* determine the processor type */
696 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
697 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
699 /* parse options */
700 for ( i = 1 ; i < argc ; i++ )
702 if (argv[i][0] == '-') /* option */
704 /* determine if tihs switch is followed by a separate argument */
705 next_is_arg = 0;
706 option_arg = 0;
707 switch(argv[i][1])
709 case 'x': case 'o': case 'D': case 'U':
710 case 'I': case 'A': case 'l': case 'u':
711 case 'b': case 'V': case 'G': case 'L':
712 case 'B':
713 if (argv[i][2]) option_arg = &argv[i][2];
714 else next_is_arg = 1;
715 break;
716 case 'i':
717 next_is_arg = 1;
718 break;
719 case 'a':
720 if (strcmp("-aux-info", argv[i]) == 0)
721 next_is_arg = 1;
722 break;
723 case 'X':
724 if (strcmp("-Xlinker", argv[i]) == 0)
725 next_is_arg = 1;
726 break;
727 case 'M':
728 c = argv[i][2];
729 if (c == 'F' || c == 'T' || c == 'Q')
731 if (argv[i][3]) option_arg = &argv[i][3];
732 else next_is_arg = 1;
734 break;
736 if (next_is_arg) option_arg = argv[i+1];
738 /* determine what options go 'as is' to the linker & the compiler */
739 raw_compiler_arg = raw_linker_arg = 0;
740 if (is_linker_arg(argv[i]))
742 raw_linker_arg = 1;
744 else
746 if (is_directory_arg(argv[i]) || is_target_arg(argv[i]))
747 raw_linker_arg = 1;
748 raw_compiler_arg = !is_mingw_arg(argv[i]);
751 /* these things we handle explicitly so we don't pass them 'as is' */
752 if (argv[i][1] == 'l' || argv[i][1] == 'I' || argv[i][1] == 'L')
753 raw_linker_arg = 0;
754 if (argv[i][1] == 'c' || argv[i][1] == 'L')
755 raw_compiler_arg = 0;
756 if (argv[i][1] == 'o')
757 raw_compiler_arg = raw_linker_arg = 0;
759 /* do a bit of semantic analysis */
760 switch (argv[i][1])
762 case 'B':
763 str = strdup(option_arg);
764 if (strendswith(str, "/tools/winebuild"))
766 opts.wine_mode = 1;
767 /* don't pass it to the compiler, this generates warnings */
768 raw_compiler_arg = raw_linker_arg = 0;
770 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
771 if (!opts.prefix) opts.prefix = strarray_alloc();
772 strarray_add(opts.prefix, str);
773 break;
774 case 'c': /* compile or assemble */
775 if (argv[i][2] == 0) opts.compile_only = 1;
776 /* fall through */
777 case 'S': /* generate assembler code */
778 case 'E': /* preprocess only */
779 if (argv[i][2] == 0) linking = 0;
780 break;
781 case 'f':
782 if (strcmp("-fno-short-wchar", argv[i]) == 0)
783 opts.noshortwchar = 1;
784 break;
785 case 'l':
786 strarray_add(opts.files, strmake("-l%s", option_arg));
787 break;
788 case 'L':
789 strarray_add(opts.lib_dirs, option_arg);
790 break;
791 case 'M': /* map file generation */
792 linking = 0;
793 break;
794 case 'm':
795 if (strcmp("-mno-cygwin", argv[i]) == 0)
796 opts.use_msvcrt = 1;
797 else if (strcmp("-mwindows", argv[i]) == 0)
798 opts.gui_app = 1;
799 else if (strcmp("-mconsole", argv[i]) == 0)
800 opts.gui_app = 0;
801 break;
802 case 'n':
803 if (strcmp("-nostdinc", argv[i]) == 0)
804 opts.nostdinc = 1;
805 else if (strcmp("-nodefaultlibs", argv[i]) == 0)
806 opts.nodefaultlibs = 1;
807 else if (strcmp("-nostdlib", argv[i]) == 0)
808 opts.nostdlib = 1;
809 break;
810 case 'o':
811 opts.output_name = option_arg;
812 break;
813 case 's':
814 if (strcmp("-static", argv[i]) == 0)
815 linking = -1;
816 else if(strcmp("-save-temps", argv[i]) == 0)
817 keep_generated = 1;
818 else if(strcmp("-shared", argv[i]) == 0)
820 opts.shared = 1;
821 raw_compiler_arg = raw_linker_arg = 0;
823 break;
824 case 'v':
825 if (argv[i][2] == 0) verbose++;
826 break;
827 case 'W':
828 if (strncmp("-Wl,", argv[i], 4) == 0)
830 if (strstr(argv[i], "-static"))
831 linking = -1;
833 else if (strncmp("-Wb,", argv[i], 4) == 0)
835 strarray* Wb = strarray_fromstring(argv[i] + 4, ",");
836 strarray_addall(opts.winebuild_args, Wb);
837 strarray_free(Wb);
838 /* don't pass it to the compiler, it generates errors */
839 raw_compiler_arg = raw_linker_arg = 0;
841 break;
842 case '-':
843 if (strcmp("-static", argv[i]+1) == 0)
844 linking = -1;
845 break;
848 /* put the arg into the appropriate bucket */
849 if (raw_linker_arg)
851 strarray_add(opts.linker_args, argv[i]);
852 if (next_is_arg && (i + 1 < argc))
853 strarray_add(opts.linker_args, argv[i + 1]);
855 if (raw_compiler_arg)
857 strarray_add(opts.compiler_args, argv[i]);
858 if (next_is_arg && (i + 1 < argc))
859 strarray_add(opts.compiler_args, argv[i + 1]);
862 /* skip the next token if it's an argument */
863 if (next_is_arg) i++;
865 else
867 strarray_add(opts.files, argv[i]);
871 if (opts.processor == proc_cpp) linking = 0;
872 if (linking == -1) error("Static linking is not supported.");
874 if (opts.files->size == 0) forward(argc, argv, &opts);
875 else if (linking) build(&opts);
876 else compile(&opts);
878 return 0;