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
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:
47 * -iwithprefixbefore dir
59 * -G num (see NOTES below)
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
73 * object-file-name -llibrary -nostartfiles -nodefaultlibs
74 * -nostdlib -s -static -static-libgcc -shared -shared-libgcc
75 * -symbolic -Wl,option -Xlinker option -u symbol
78 * -Bprefix -Idir -I- -Ldir -specs=file
81 * -b machine -V version
83 * Please note that the Target Options are relevant to everything:
84 * compiler, linker, assembler, preprocessor.
89 #include "wine/port.h"
99 static const char* app_loader_template
=
103 "# determine the application directory\n"
107 " # $0 contains a path, use it\n"
108 " appdir=`dirname \"$0\"`\n"
111 " # no directory in $0, search in PATH\n"
117 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
125 " debugmsg=\"$1 $2\"\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"
140 " apppath=\"$appname\"\n"
143 "# determine the WINELOADER\n"
144 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\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
;
155 enum { proc_cc
= 0, proc_cxx
= 1, proc_cpp
= 2} processor
;
164 const char* output_name
;
167 strarray
* linker_args
;
168 strarray
* compiler_args
;
169 strarray
* winebuild_args
;
173 static void clean_temp_files()
177 if (keep_generated
) return;
179 for (i
= 0; i
< tmp_files
->size
; i
++)
180 unlink(tmp_files
->base
[i
]);
183 char* get_temp_file(const char* prefix
, const char* suffix
)
185 char* tmp
= strmake("%s-XXXXXX%s", prefix
, suffix
);
186 int fd
= mkstemps( tmp
, strlen(suffix
) );
189 /* could not create it in current directory, try in /tmp */
191 tmp
= strmake("/tmp/%s-XXXXXX%s", prefix
, suffix
);
192 fd
= mkstemps( tmp
, strlen(suffix
) );
193 if (fd
== -1) error( "could not create temp file" );
196 strarray_add(tmp_files
, tmp
);
201 static const strarray
* get_translator(struct options
* opts
)
203 static strarray
* cpp
= 0;
204 static strarray
* cc
= 0;
205 static strarray
* cxx
= 0;
207 switch(opts
->processor
)
210 if (!cpp
) cpp
= strarray_fromstring(CPP
, " ");
213 if (!cc
) cc
= strarray_fromstring(CC
, " ");
216 if (!cxx
) cxx
= strarray_fromstring(CXX
, " ");
219 error("Unknown processor");
222 static void compile(struct options
* opts
)
224 strarray
* comp_args
= strarray_alloc();
227 switch(opts
->processor
)
229 case proc_cpp
: gcc_defs
= 1; break;
231 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
232 /* mixing different C and C++ compilers isn't supported in configure anyway */
233 case proc_cc
: gcc_defs
= 1; break;
234 case proc_cxx
: gcc_defs
= 1; break;
236 case proc_cc
: gcc_defs
= 0; break;
237 case proc_cxx
: gcc_defs
= 0; break;
240 strarray_addall(comp_args
, get_translator(opts
));
242 if (opts
->processor
!= proc_cpp
)
244 #ifdef CC_FLAG_SHORT_WCHAR
245 if (!opts
->wine_mode
&& !opts
->noshortwchar
)
247 strarray_add(comp_args
, CC_FLAG_SHORT_WCHAR
);
248 strarray_add(comp_args
, "-DWINE_UNICODE_NATIVE");
251 strarray_addall(comp_args
, strarray_fromstring(DLLFLAGS
, " "));
253 if (!opts
->wine_mode
&& !opts
->nostdinc
)
255 if (opts
->use_msvcrt
)
257 strarray_add(comp_args
, "-I" INCLUDEDIR
"/msvcrt");
258 strarray_add(comp_args
, "-D__MSVCRT__");
260 strarray_add(comp_args
, "-I" INCLUDEDIR
"/windows");
262 strarray_add(comp_args
, "-DWIN32");
263 strarray_add(comp_args
, "-D_WIN32");
264 strarray_add(comp_args
, "-D__WIN32");
265 strarray_add(comp_args
, "-D__WIN32__");
266 strarray_add(comp_args
, "-D__WINNT");
267 strarray_add(comp_args
, "-D__WINNT__");
271 strarray_add(comp_args
, "-D__stdcall=__attribute__((__stdcall__))");
272 strarray_add(comp_args
, "-D__cdecl=__attribute__((__cdecl__))");
273 strarray_add(comp_args
, "-D__fastcall=__attribute__((__fastcall__))");
274 strarray_add(comp_args
, "-D_stdcall=__attribute__((__stdcall__))");
275 strarray_add(comp_args
, "-D_cdecl=__attribute__((__cdecl__))");
276 strarray_add(comp_args
, "-D_fastcall=__attribute__((__fastcall__))");
277 strarray_add(comp_args
, "-D__declspec(x)=__declspec_##x");
278 strarray_add(comp_args
, "-D__declspec_align(x)=__attribute__((aligned(x)))");
279 strarray_add(comp_args
, "-D__declspec_allocate(x)=__attribute__((section(x)))");
280 strarray_add(comp_args
, "-D__declspec_deprecated=__attribute__((deprecated))");
281 strarray_add(comp_args
, "-D__declspec_dllimport=__attribute__((dllimport))");
282 strarray_add(comp_args
, "-D__declspec_dllexport=__attribute__((dllexport))");
283 strarray_add(comp_args
, "-D__declspec_naked=__attribute__((naked))");
284 strarray_add(comp_args
, "-D__declspec_noinline=__attribute__((noinline))");
285 strarray_add(comp_args
, "-D__declspec_noreturn=__attribute__((noreturn))");
286 strarray_add(comp_args
, "-D__declspec_nothrow=__attribute__((nothrow))");
287 strarray_add(comp_args
, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
288 strarray_add(comp_args
, "-D__declspec_selectany=__attribute__((weak))");
289 strarray_add(comp_args
, "-D__declspec_thread=__thread");
292 /* Wine specific defines */
293 strarray_add(comp_args
, "-D__WINE__");
294 strarray_add(comp_args
, "-D__int8=char");
295 strarray_add(comp_args
, "-D__int16=short");
296 /* FIXME: what about 64-bit platforms? */
297 strarray_add(comp_args
, "-D__int32=int");
298 #ifdef HAVE_LONG_LONG
299 strarray_add(comp_args
, "-D__int64=long long");
302 /* options we handle explicitly */
303 if (opts
->compile_only
)
304 strarray_add(comp_args
, "-c");
305 if (opts
->output_name
)
307 strarray_add(comp_args
, "-o");
308 strarray_add(comp_args
, opts
->output_name
);
311 /* the rest of the pass-through parameters */
312 for ( j
= 0 ; j
< opts
->compiler_args
->size
; j
++ )
313 strarray_add(comp_args
, opts
->compiler_args
->base
[j
]);
315 /* last, but not least, the files */
316 for ( j
= 0; j
< opts
->files
->size
; j
++ )
317 strarray_add(comp_args
, opts
->files
->base
[j
]);
319 spawn(opts
->prefix
, comp_args
);
322 static const char* compile_to_object(struct options
* opts
, const char* file
)
324 struct options copts
;
327 /* make a copy we so don't change any of the initial stuff */
328 /* a shallow copy is exactly what we want in this case */
329 base_name
= get_basename(file
);
331 copts
.processor
= proc_cc
;
332 copts
.output_name
= get_temp_file(base_name
, ".o");
333 copts
.compile_only
= 1;
334 copts
.files
= strarray_alloc();
335 strarray_add(copts
.files
, file
);
337 strarray_free(copts
.files
);
340 return copts
.output_name
;
343 static void build(struct options
* opts
)
345 static const char *stdlibpath
[] = { DLLDIR
, LIBDIR
, "/usr/lib", "/usr/local/lib", "/lib" };
346 strarray
*lib_dirs
, *files
;
347 strarray
*spec_args
, *comp_args
, *link_args
;
348 char *spec_c_name
, *spec_o_name
, *base_file
, *base_name
;
349 const char* output_name
;
350 const char* winebuild
= getenv("WINEBUILD");
351 int generate_app_loader
= 1;
354 /* NOTE: for the files array we'll use the following convention:
355 * -axxx: xxx is an archive (.a)
356 * -dxxx: xxx is a DLL (.def)
357 * -lxxx: xxx is an unsorted library
358 * -oxxx: xxx is an object (.o)
359 * -rxxx: xxx is a resource (.res)
360 * -sxxx: xxx is a shared lib (.so)
363 if (!winebuild
) winebuild
= "winebuild";
365 output_name
= opts
->output_name
? opts
->output_name
: "a.out";
367 /* get base filename by removing the .exe extension, if present */
368 base_file
= strdup(output_name
);
369 if (strendswith(base_file
, ".exe.so"))
371 base_file
[strlen(base_file
) - 7] = 0;
372 generate_app_loader
= 0;
374 else if (strendswith(base_file
, ".exe")) base_file
[strlen(base_file
) - 4] = 0;
375 if ((base_name
= strrchr(base_file
, '/'))) base_name
++;
376 else base_name
= base_file
;
378 /* 'winegcc -o app xxx.exe.so' only creates the load script */
379 if (opts
->files
->size
== 1 && strendswith(opts
->files
->base
[0], ".exe.so"))
381 create_file(base_file
, 0755, app_loader_template
, opts
->files
->base
[0]);
385 /* prepare the linking path */
386 lib_dirs
= strarray_dup(opts
->lib_dirs
);
387 if (!opts
->wine_mode
)
389 for ( j
= 0; j
< sizeof(stdlibpath
)/sizeof(stdlibpath
[0]);j
++ )
390 strarray_add(lib_dirs
, stdlibpath
[j
]);
393 /* mark the files with their appropriate type */
394 files
= strarray_alloc();
395 for ( j
= 0; j
< opts
->files
->size
; j
++ )
397 const char* file
= opts
->files
->base
[j
];
400 switch(get_file_type(file
))
403 /* FIXME: invoke wrc to build it */
404 error("Can't compile .rc file at the moment: %s", file
);
407 strarray_add(files
, strmake("-r%s", file
));
410 strarray_add(files
, strmake("-o%s", file
));
413 error("File does not exist: %s", file
);
416 file
= compile_to_object(opts
, file
);
417 strarray_add(files
, strmake("-o%s", file
));
421 else if (file
[1] == 'l')
424 switch(get_lib_type(lib_dirs
, file
+ 2, &fullname
))
427 strarray_add(files
, strmake("-a%s", fullname
));
430 strarray_add(files
, strmake("-d%s", file
+ 2));
433 strarray_add(files
, strmake("-s%s", file
+ 2));
436 /* keep it anyway, the linker may know what to do with it */
437 strarray_add(files
, file
);
444 /* add the default libraries, if needed */
447 if (opts
->use_msvcrt
) strarray_add(files
, "-dmsvcrt");
450 if (!opts
->wine_mode
&& !opts
->nodefaultlibs
)
454 strarray_add(files
, "-dshell32");
455 strarray_add(files
, "-dcomdlg32");
456 strarray_add(files
, "-dgdi32");
458 strarray_add(files
, "-dadvapi32");
459 strarray_add(files
, "-duser32");
460 strarray_add(files
, "-dkernel32");
463 /* run winebuild to generate the .spec.c file */
464 spec_args
= strarray_alloc();
465 spec_c_name
= get_temp_file(base_name
, ".spec.c");
466 strarray_add(spec_args
, winebuild
);
467 strarray_add(spec_args
, "-o");
468 strarray_add(spec_args
, spec_c_name
);
469 strarray_add(spec_args
, "--exe");
470 strarray_add(spec_args
, strmake("%s.exe", base_name
));
471 strarray_add(spec_args
, opts
->gui_app
? "-mgui" : "-mcui");
473 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
474 strarray_add(spec_args
, strmake("-L%s", lib_dirs
->base
[j
]));
476 for ( j
= 0 ; j
< opts
->winebuild_args
->size
; j
++ )
477 strarray_add(spec_args
, opts
->winebuild_args
->base
[j
]);
479 for ( j
= 0; j
< files
->size
; j
++ )
481 const char* name
= files
->base
[j
] + 2;
482 switch(files
->base
[j
][1])
485 strarray_add(spec_args
, strmake("-l%s", name
));
488 strarray_add(spec_args
, files
->base
[j
]);
492 strarray_add(spec_args
, name
);
497 spawn(opts
->prefix
, spec_args
);
499 /* compile the .spec.c file into a .spec.o file */
500 comp_args
= strarray_alloc();
501 spec_o_name
= get_temp_file(base_name
, ".spec.o");
502 strarray_add(comp_args
, CC
);
503 strarray_addall(comp_args
, strarray_fromstring(DLLFLAGS
, " "));
504 strarray_add(comp_args
, "-o");
505 strarray_add(comp_args
, spec_o_name
);
506 strarray_add(comp_args
, "-c");
507 strarray_add(comp_args
, spec_c_name
);
509 spawn(opts
->prefix
, comp_args
);
511 /* link everything together now */
512 link_args
= strarray_alloc();
513 strarray_addall(link_args
, get_translator(opts
));
514 strarray_addall(link_args
, strarray_fromstring(LDDLLFLAGS
, " "));
516 strarray_add(link_args
, "-o");
517 strarray_add(link_args
, strmake("%s.exe.so", base_file
));
519 for ( j
= 0 ; j
< opts
->linker_args
->size
; j
++ )
520 strarray_add(link_args
, opts
->linker_args
->base
[j
]);
522 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
523 strarray_add(link_args
, strmake("-L%s", lib_dirs
->base
[j
]));
525 strarray_add(link_args
, spec_o_name
);
527 for ( j
= 0; j
< files
->size
; j
++ )
529 const char* name
= files
->base
[j
] + 2;
530 switch(files
->base
[j
][1])
534 strarray_add(link_args
, strmake("-l%s", name
));
538 strarray_add(link_args
, name
);
545 strarray_add(link_args
, "-lwine");
546 strarray_add(link_args
, "-lm");
547 strarray_add(link_args
, "-lc");
550 spawn(opts
->prefix
, link_args
);
552 /* create the loader script */
553 if (generate_app_loader
)
554 create_file(base_file
, 0755, app_loader_template
, strmake("%s.exe.so", base_name
));
558 static void forward(int argc
, char **argv
, struct options
* opts
)
560 strarray
* args
= strarray_alloc();
563 strarray_addall(args
, get_translator(opts
));
565 for( j
= 1; j
< argc
; j
++ )
566 strarray_add(args
, argv
[j
]);
568 spawn(opts
->prefix
, args
);
573 * object-file-name -llibrary -nostartfiles -nodefaultlibs
574 * -nostdlib -s -static -static-libgcc -shared -shared-libgcc
575 * -symbolic -Wl,option -Xlinker option -u symbol
577 static int is_linker_arg(const char* arg
)
579 static const char* link_switches
[] =
581 "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s",
582 "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic"
592 if (strncmp("-Wl,", arg
, 4) == 0) return 1;
595 if (strcmp("-Xlinker", arg
) == 0) return 1;
599 for (j
= 0; j
< sizeof(link_switches
)/sizeof(link_switches
[0]); j
++)
600 if (strcmp(link_switches
[j
], arg
) == 0) return 1;
607 * -b machine -V version
609 static int is_target_arg(const char* arg
)
611 return arg
[1] == 'b' || arg
[2] == 'V';
617 * -Bprefix -Idir -I- -Ldir -specs=file
619 static int is_directory_arg(const char* arg
)
621 return arg
[1] == 'B' || arg
[1] == 'L' || arg
[1] == 'I' || strncmp("-specs=", arg
, 7) == 0;
626 * -mno-cygwin -mwindows -mconsole -mthreads
628 static int is_mingw_arg(const char* arg
)
630 static const char* mingw_switches
[] =
632 "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads"
636 for (j
= 0; j
< sizeof(mingw_switches
)/sizeof(mingw_switches
[0]); j
++)
637 if (strcmp(mingw_switches
[j
], arg
) == 0) return 1;
642 int main(int argc
, char **argv
)
644 int i
, c
, next_is_arg
= 0, linking
= 1;
645 int raw_compiler_arg
, raw_linker_arg
;
646 const char* option_arg
;
650 /* setup tmp file removal at exit */
651 tmp_files
= strarray_alloc();
652 atexit(clean_temp_files
);
654 /* initialize options */
655 memset(&opts
, 0, sizeof(opts
));
656 opts
.lib_dirs
= strarray_alloc();
657 opts
.files
= strarray_alloc();
658 opts
.linker_args
= strarray_alloc();
659 opts
.compiler_args
= strarray_alloc();
660 opts
.winebuild_args
= strarray_alloc();
662 /* determine the processor type */
663 if (strendswith(argv
[0], "winecpp")) opts
.processor
= proc_cpp
;
664 else if (strendswith(argv
[0], "++")) opts
.processor
= proc_cxx
;
667 for ( i
= 1 ; i
< argc
; i
++ )
669 if (argv
[i
][0] == '-') /* option */
671 /* determine if tihs switch is followed by a separate argument */
676 case 'x': case 'o': case 'D': case 'U':
677 case 'I': case 'A': case 'l': case 'u':
678 case 'b': case 'V': case 'G': case 'L':
680 if (argv
[i
][2]) option_arg
= &argv
[i
][2];
681 else next_is_arg
= 1;
687 if (strcmp("-aux-info", argv
[i
]) == 0)
691 if (strcmp("-Xlinker", argv
[i
]) == 0)
696 if (c
== 'F' || c
== 'T' || c
== 'Q')
698 if (argv
[i
][3]) option_arg
= &argv
[i
][3];
699 else next_is_arg
= 1;
703 if (next_is_arg
) option_arg
= argv
[i
+1];
705 /* determine what options go 'as is' to the linker & the compiler */
706 raw_compiler_arg
= raw_linker_arg
= 0;
707 if (is_linker_arg(argv
[i
]))
713 if (is_directory_arg(argv
[i
]) || is_target_arg(argv
[i
]))
715 raw_compiler_arg
= !is_mingw_arg(argv
[i
]);
718 /* these things we handle explicitly so we don't pass them 'as is' */
719 if (argv
[i
][1] == 'l' || argv
[i
][1] == 'I' || argv
[i
][1] == 'L')
721 if (argv
[i
][1] == 'c' || argv
[i
][1] == 'L')
722 raw_compiler_arg
= 0;
723 if (argv
[i
][1] == 'o')
724 raw_compiler_arg
= raw_linker_arg
= 0;
726 /* do a bit of semantic analysis */
730 str
= strdup(option_arg
);
731 if (strendswith(str
, "/tools/winebuild"))
734 /* don't pass it to the compiler, this generates warnings */
735 raw_compiler_arg
= raw_linker_arg
= 0;
737 if (strendswith(str
, "/")) str
[strlen(str
) - 1] = 0;
738 if (!opts
.prefix
) opts
.prefix
= strarray_alloc();
739 strarray_add(opts
.prefix
, str
);
741 case 'c': /* compile or assemble */
742 if (argv
[i
][2] == 0) opts
.compile_only
= 1;
744 case 'S': /* generate assembler code */
745 case 'E': /* preprocess only */
746 if (argv
[i
][2] == 0) linking
= 0;
749 if (strcmp("-fno-short-wchar", argv
[i
]) == 0)
750 opts
.noshortwchar
= 1;
753 strarray_add(opts
.files
, strmake("-l%s", option_arg
));
756 strarray_add(opts
.lib_dirs
, option_arg
);
758 case 'M': /* map file generation */
762 if (strcmp("-mno-cygwin", argv
[i
]) == 0)
764 else if (strcmp("-mwindows", argv
[i
]) == 0)
766 else if (strcmp("-mconsole", argv
[i
]) == 0)
770 if (strcmp("-nostdinc", argv
[i
]) == 0)
772 else if (strcmp("-nodefaultlibs", argv
[i
]) == 0)
773 opts
.nodefaultlibs
= 1;
774 else if (strcmp("-nostdlib", argv
[i
]) == 0)
778 opts
.output_name
= option_arg
;
781 if (strcmp("-static", argv
[i
]) == 0)
783 else if(strcmp("-save-temps", argv
[i
]) == 0)
787 if (argv
[i
][2] == 0) verbose
++;
790 if (strncmp("-Wl,", argv
[i
], 4) == 0)
792 if (strstr(argv
[i
], "-static"))
795 else if (strncmp("-Wb,", argv
[i
], 4) == 0)
797 strarray
* Wb
= strarray_fromstring(argv
[i
] + 4, ",");
798 strarray_addall(opts
.winebuild_args
, Wb
);
803 if (strcmp("-static", argv
[i
]+1) == 0)
808 /* put the arg into the appropriate bucket */
811 strarray_add(opts
.linker_args
, argv
[i
]);
812 if (next_is_arg
&& (i
+ 1 < argc
))
813 strarray_add(opts
.linker_args
, argv
[i
+ 1]);
815 if (raw_compiler_arg
)
817 strarray_add(opts
.compiler_args
, argv
[i
]);
818 if (next_is_arg
&& (i
+ 1 < argc
))
819 strarray_add(opts
.compiler_args
, argv
[i
+ 1]);
822 /* skip the next token if it's an argument */
823 if (next_is_arg
) i
++;
827 strarray_add(opts
.files
, argv
[i
]);
831 if (opts
.processor
== proc_cpp
) linking
= 0;
832 if (linking
== -1) error("Static linking is not supported.");
834 if (opts
.files
->size
== 0) forward(argc
, argv
, &opts
);
835 else if (linking
) build(&opts
);