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
;
165 const char* output_name
;
168 strarray
* linker_args
;
169 strarray
* compiler_args
;
170 strarray
* winebuild_args
;
174 static void clean_temp_files()
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
) );
190 /* could not create it in current directory, try in /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" );
197 strarray_add(tmp_files
, 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
)
211 if (!cpp
) cpp
= strarray_fromstring(CPP
, " ");
214 if (!cc
) cc
= strarray_fromstring(CC
, " ");
217 if (!cxx
) cxx
= strarray_fromstring(CXX
, " ");
220 error("Unknown processor");
223 static void compile(struct options
* opts
)
225 strarray
* comp_args
= strarray_alloc();
228 switch(opts
->processor
)
230 case proc_cpp
: gcc_defs
= 1; break;
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;
237 case proc_cc
: gcc_defs
= 0; break;
238 case proc_cxx
: gcc_defs
= 0; break;
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");
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__");
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");
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
;
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
);
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
);
338 strarray_free(copts
.files
);
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
;
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;
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]);
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;
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 */
407 files
= strarray_alloc();
408 for ( j
= 0; j
< opts
->files
->size
; j
++ )
410 const char* file
= opts
->files
->base
[j
];
413 switch(get_file_type(file
))
418 error("Spec file %s not supported in non-shared mode", file
);
420 error("Only one spec file can be specified in shared mode");
424 /* FIXME: invoke wrc to build it */
425 error("Can't compile .rc file at the moment: %s", file
);
428 strarray_add(files
, strmake("-r%s", file
));
431 strarray_add(files
, strmake("-o%s", file
));
434 error("File does not exist: %s", file
);
437 file
= compile_to_object(opts
, file
);
438 strarray_add(files
, strmake("-o%s", file
));
442 else if (file
[1] == 'l')
445 switch(get_lib_type(lib_dirs
, file
+ 2, &fullname
))
448 strarray_add(files
, strmake("-a%s", fullname
));
451 strarray_add(files
, strmake("-d%s", file
+ 2));
454 strarray_add(files
, strmake("-s%s", file
+ 2));
457 /* keep it anyway, the linker may know what to do with it */
458 strarray_add(files
, file
);
464 if (opts
->shared
&& !spec_file
)
465 error("A spec file is currently needed in shared mode");
467 /* add the default libraries, if needed */
470 if (opts
->use_msvcrt
) strarray_add(files
, "-dmsvcrt");
473 if (!opts
->wine_mode
&& !opts
->nodefaultlibs
)
477 strarray_add(files
, "-dshell32");
478 strarray_add(files
, "-dcomdlg32");
479 strarray_add(files
, "-dgdi32");
481 strarray_add(files
, "-dadvapi32");
482 strarray_add(files
, "-duser32");
483 strarray_add(files
, "-dkernel32");
486 /* run winebuild to generate the .spec.c file */
487 spec_args
= strarray_alloc();
488 spec_c_name
= get_temp_file(output_name
, ".spec.c");
489 strarray_add(spec_args
, winebuild
);
490 strarray_add(spec_args
, "-o");
491 strarray_add(spec_args
, spec_c_name
);
494 strarray_add(spec_args
, "--dll");
495 strarray_add(spec_args
, spec_file
);
499 strarray_add(spec_args
, "--exe");
500 strarray_add(spec_args
, output_name
);
501 strarray_add(spec_args
, "--subsystem");
502 strarray_add(spec_args
, opts
->gui_app
? "windows" : "console");
505 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
506 strarray_add(spec_args
, strmake("-L%s", lib_dirs
->base
[j
]));
508 for ( j
= 0 ; j
< opts
->winebuild_args
->size
; j
++ )
509 strarray_add(spec_args
, opts
->winebuild_args
->base
[j
]);
511 for ( j
= 0; j
< files
->size
; j
++ )
513 const char* name
= files
->base
[j
] + 2;
514 switch(files
->base
[j
][1])
517 strarray_add(spec_args
, strmake("-l%s", name
));
520 strarray_add(spec_args
, files
->base
[j
]);
524 strarray_add(spec_args
, name
);
529 spawn(opts
->prefix
, spec_args
);
531 /* compile the .spec.c file into a .spec.o file */
532 comp_args
= strarray_alloc();
533 spec_o_name
= compile_to_object(opts
, spec_c_name
);
535 /* link everything together now */
536 link_args
= strarray_alloc();
537 strarray_addall(link_args
, get_translator(opts
));
538 strarray_addall(link_args
, strarray_fromstring(LDDLLFLAGS
, " "));
540 strarray_add(link_args
, "-o");
541 strarray_add(link_args
, strmake("%s.so", output_file
));
543 for ( j
= 0 ; j
< opts
->linker_args
->size
; j
++ )
544 strarray_add(link_args
, opts
->linker_args
->base
[j
]);
546 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
547 strarray_add(link_args
, strmake("-L%s", lib_dirs
->base
[j
]));
549 strarray_add(link_args
, spec_o_name
);
551 for ( j
= 0; j
< files
->size
; j
++ )
553 const char* name
= files
->base
[j
] + 2;
554 switch(files
->base
[j
][1])
558 strarray_add(link_args
, strmake("-l%s", name
));
562 strarray_add(link_args
, name
);
569 strarray_add(link_args
, "-lwine");
570 strarray_add(link_args
, "-lm");
571 strarray_add(link_args
, "-lc");
574 spawn(opts
->prefix
, link_args
);
576 /* create the loader script */
577 if (generate_app_loader
)
579 if (strendswith(output_file
, ".exe")) output_file
[strlen(output_file
) - 4] = 0;
580 create_file(output_file
, 0755, app_loader_template
, strmake("%s.so", output_name
));
585 static void forward(int argc
, char **argv
, struct options
* opts
)
587 strarray
* args
= strarray_alloc();
590 strarray_addall(args
, get_translator(opts
));
592 for( j
= 1; j
< argc
; j
++ )
593 strarray_add(args
, argv
[j
]);
595 spawn(opts
->prefix
, args
);
600 * object-file-name -llibrary -nostartfiles -nodefaultlibs
601 * -nostdlib -s -static -static-libgcc -shared -shared-libgcc
602 * -symbolic -Wl,option -Xlinker option -u symbol
604 static int is_linker_arg(const char* arg
)
606 static const char* link_switches
[] =
608 "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s",
609 "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic"
619 if (strncmp("-Wl,", arg
, 4) == 0) return 1;
622 if (strcmp("-Xlinker", arg
) == 0) return 1;
626 for (j
= 0; j
< sizeof(link_switches
)/sizeof(link_switches
[0]); j
++)
627 if (strcmp(link_switches
[j
], arg
) == 0) return 1;
634 * -b machine -V version
636 static int is_target_arg(const char* arg
)
638 return arg
[1] == 'b' || arg
[2] == 'V';
644 * -Bprefix -Idir -I- -Ldir -specs=file
646 static int is_directory_arg(const char* arg
)
648 return arg
[1] == 'B' || arg
[1] == 'L' || arg
[1] == 'I' || strncmp("-specs=", arg
, 7) == 0;
653 * -mno-cygwin -mwindows -mconsole -mthreads
655 static int is_mingw_arg(const char* arg
)
657 static const char* mingw_switches
[] =
659 "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads"
663 for (j
= 0; j
< sizeof(mingw_switches
)/sizeof(mingw_switches
[0]); j
++)
664 if (strcmp(mingw_switches
[j
], arg
) == 0) return 1;
669 int main(int argc
, char **argv
)
671 int i
, c
, next_is_arg
= 0, linking
= 1;
672 int raw_compiler_arg
, raw_linker_arg
;
673 const char* option_arg
;
677 /* setup tmp file removal at exit */
678 tmp_files
= strarray_alloc();
679 atexit(clean_temp_files
);
681 /* initialize options */
682 memset(&opts
, 0, sizeof(opts
));
683 opts
.lib_dirs
= strarray_alloc();
684 opts
.files
= strarray_alloc();
685 opts
.linker_args
= strarray_alloc();
686 opts
.compiler_args
= strarray_alloc();
687 opts
.winebuild_args
= strarray_alloc();
689 /* determine the processor type */
690 if (strendswith(argv
[0], "winecpp")) opts
.processor
= proc_cpp
;
691 else if (strendswith(argv
[0], "++")) opts
.processor
= proc_cxx
;
694 for ( i
= 1 ; i
< argc
; i
++ )
696 if (argv
[i
][0] == '-') /* option */
698 /* determine if tihs switch is followed by a separate argument */
703 case 'x': case 'o': case 'D': case 'U':
704 case 'I': case 'A': case 'l': case 'u':
705 case 'b': case 'V': case 'G': case 'L':
707 if (argv
[i
][2]) option_arg
= &argv
[i
][2];
708 else next_is_arg
= 1;
714 if (strcmp("-aux-info", argv
[i
]) == 0)
718 if (strcmp("-Xlinker", argv
[i
]) == 0)
723 if (c
== 'F' || c
== 'T' || c
== 'Q')
725 if (argv
[i
][3]) option_arg
= &argv
[i
][3];
726 else next_is_arg
= 1;
730 if (next_is_arg
) option_arg
= argv
[i
+1];
732 /* determine what options go 'as is' to the linker & the compiler */
733 raw_compiler_arg
= raw_linker_arg
= 0;
734 if (is_linker_arg(argv
[i
]))
740 if (is_directory_arg(argv
[i
]) || is_target_arg(argv
[i
]))
742 raw_compiler_arg
= !is_mingw_arg(argv
[i
]);
745 /* these things we handle explicitly so we don't pass them 'as is' */
746 if (argv
[i
][1] == 'l' || argv
[i
][1] == 'I' || argv
[i
][1] == 'L')
748 if (argv
[i
][1] == 'c' || argv
[i
][1] == 'L')
749 raw_compiler_arg
= 0;
750 if (argv
[i
][1] == 'o')
751 raw_compiler_arg
= raw_linker_arg
= 0;
753 /* do a bit of semantic analysis */
757 str
= strdup(option_arg
);
758 if (strendswith(str
, "/tools/winebuild"))
761 /* don't pass it to the compiler, this generates warnings */
762 raw_compiler_arg
= raw_linker_arg
= 0;
764 if (strendswith(str
, "/")) str
[strlen(str
) - 1] = 0;
765 if (!opts
.prefix
) opts
.prefix
= strarray_alloc();
766 strarray_add(opts
.prefix
, str
);
768 case 'c': /* compile or assemble */
769 if (argv
[i
][2] == 0) opts
.compile_only
= 1;
771 case 'S': /* generate assembler code */
772 case 'E': /* preprocess only */
773 if (argv
[i
][2] == 0) linking
= 0;
776 if (strcmp("-fno-short-wchar", argv
[i
]) == 0)
777 opts
.noshortwchar
= 1;
780 strarray_add(opts
.files
, strmake("-l%s", option_arg
));
783 strarray_add(opts
.lib_dirs
, option_arg
);
785 case 'M': /* map file generation */
789 if (strcmp("-mno-cygwin", argv
[i
]) == 0)
791 else if (strcmp("-mwindows", argv
[i
]) == 0)
793 else if (strcmp("-mconsole", argv
[i
]) == 0)
797 if (strcmp("-nostdinc", argv
[i
]) == 0)
799 else if (strcmp("-nodefaultlibs", argv
[i
]) == 0)
800 opts
.nodefaultlibs
= 1;
801 else if (strcmp("-nostdlib", argv
[i
]) == 0)
805 opts
.output_name
= option_arg
;
808 if (strcmp("-static", argv
[i
]) == 0)
810 else if(strcmp("-save-temps", argv
[i
]) == 0)
812 else if(strcmp("-shared", argv
[i
]) == 0)
815 raw_compiler_arg
= raw_linker_arg
= 0;
819 if (argv
[i
][2] == 0) verbose
++;
822 if (strncmp("-Wl,", argv
[i
], 4) == 0)
824 if (strstr(argv
[i
], "-static"))
827 else if (strncmp("-Wb,", argv
[i
], 4) == 0)
829 strarray
* Wb
= strarray_fromstring(argv
[i
] + 4, ",");
830 strarray_addall(opts
.winebuild_args
, Wb
);
832 /* don't pass it to the compiler, it generates errors */
833 raw_compiler_arg
= raw_linker_arg
= 0;
837 if (strcmp("-static", argv
[i
]+1) == 0)
842 /* put the arg into the appropriate bucket */
845 strarray_add(opts
.linker_args
, argv
[i
]);
846 if (next_is_arg
&& (i
+ 1 < argc
))
847 strarray_add(opts
.linker_args
, argv
[i
+ 1]);
849 if (raw_compiler_arg
)
851 strarray_add(opts
.compiler_args
, argv
[i
]);
852 if (next_is_arg
&& (i
+ 1 < argc
))
853 strarray_add(opts
.compiler_args
, argv
[i
+ 1]);
856 /* skip the next token if it's an argument */
857 if (next_is_arg
) i
++;
861 strarray_add(opts
.files
, argv
[i
]);
865 if (opts
.processor
== proc_cpp
) linking
= 0;
866 if (linking
== -1) error("Static linking is not supported.");
868 if (opts
.files
->size
== 0) forward(argc
, argv
, &opts
);
869 else if (linking
) build(&opts
);