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_cpp
= 1, proc_pp
= 2} processor
;
163 const char* output_name
;
166 strarray
*linker_args
;
167 strarray
*compiler_args
;
168 strarray
* winebuild_args
;
172 static void clean_temp_files()
176 if (keep_generated
) return;
178 for (i
= 0; i
< tmp_files
->size
; i
++)
179 unlink(tmp_files
->base
[i
]);
182 char* get_temp_file(const char* prefix
, const char* suffix
)
184 char *tmp
= strmake("%s-XXXXXX%s", prefix
, suffix
);
185 int fd
= mkstemps( tmp
, strlen(suffix
) );
188 /* could not create it in current directory, try in /tmp */
190 tmp
= strmake("/tmp/%s-XXXXXX%s", prefix
, suffix
);
191 fd
= mkstemps( tmp
, strlen(suffix
) );
192 if (fd
== -1) error( "could not create temp file" );
195 strarray_add(tmp_files
, tmp
);
200 static const char* get_translator(struct options
* opts
)
202 switch(opts
->processor
)
204 case proc_pp
: return CPP
;
205 case proc_cc
: return CC
;
206 case proc_cpp
: return CXX
;
208 error("Unknown processor");
211 static void compile(struct options
* opts
)
213 strarray
*comp_args
= strarray_alloc();
216 switch(opts
->processor
)
218 case proc_pp
: gcc_defs
= 1; break;
220 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
221 /* mixing different C and C++ compilers isn't supported in configure anyway */
222 case proc_cc
: gcc_defs
= 1; break;
223 case proc_cpp
: gcc_defs
= 1; break;
225 case proc_cc
: gcc_defs
= 0; break;
226 case proc_cpp
: gcc_defs
= 0; break;
229 strarray_add(comp_args
, get_translator(opts
));
231 if (opts
->processor
!= proc_pp
)
233 #ifdef CC_FLAG_SHORT_WCHAR
234 if (!opts
->noshortwchar
)
236 strarray_add(comp_args
, CC_FLAG_SHORT_WCHAR
);
237 strarray_add(comp_args
, "-DWINE_UNICODE_NATIVE");
240 strarray_addall(comp_args
, strarray_fromstring(DLLFLAGS
, " "));
244 if (opts
->use_msvcrt
)
246 strarray_add(comp_args
, "-I" INCLUDEDIR
"/msvcrt");
247 strarray_add(comp_args
, "-D__MSVCRT__");
249 strarray_add(comp_args
, "-I" INCLUDEDIR
"/windows");
251 strarray_add(comp_args
, "-DWIN32");
252 strarray_add(comp_args
, "-D_WIN32");
253 strarray_add(comp_args
, "-D__WIN32");
254 strarray_add(comp_args
, "-D__WIN32__");
255 strarray_add(comp_args
, "-D__WINNT");
256 strarray_add(comp_args
, "-D__WINNT__");
260 strarray_add(comp_args
, "-D__stdcall=__attribute__((__stdcall__))");
261 strarray_add(comp_args
, "-D__cdecl=__attribute__((__cdecl__))");
262 strarray_add(comp_args
, "-D__fastcall=__attribute__((__fastcall__))");
263 strarray_add(comp_args
, "-D_stdcall=__attribute__((__stdcall__))");
264 strarray_add(comp_args
, "-D_cdecl=__attribute__((__cdecl__))");
265 strarray_add(comp_args
, "-D_fastcall=__attribute__((__fastcall__))");
266 strarray_add(comp_args
, "-D__declspec(x)=__declspec_##x");
267 strarray_add(comp_args
, "-D__declspec_align(x)=__attribute__((aligned(x)))");
268 strarray_add(comp_args
, "-D__declspec_allocate(x)=__attribute__((section(x)))");
269 strarray_add(comp_args
, "-D__declspec_deprecated=__attribute__((deprecated))");
270 strarray_add(comp_args
, "-D__declspec_dllimport=__attribute__((dllimport))");
271 strarray_add(comp_args
, "-D__declspec_dllexport=__attribute__((dllexport))");
272 strarray_add(comp_args
, "-D__declspec_naked=__attribute__((naked))");
273 strarray_add(comp_args
, "-D__declspec_noinline=__attribute__((noinline))");
274 strarray_add(comp_args
, "-D__declspec_noreturn=__attribute__((noreturn))");
275 strarray_add(comp_args
, "-D__declspec_nothrow=__attribute__((nothrow))");
276 strarray_add(comp_args
, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
277 strarray_add(comp_args
, "-D__declspec_selectany=__attribute__((weak))");
278 strarray_add(comp_args
, "-D__declspec_thread=__thread");
281 /* Wine specific defines */
282 strarray_add(comp_args
, "-D__WINE__");
283 strarray_add(comp_args
, "-D__int8=char");
284 strarray_add(comp_args
, "-D__int16=short");
285 /* FIXME: what about 64-bit platforms? */
286 strarray_add(comp_args
, "-D__int32=int");
287 #ifdef HAVE_LONG_LONG
288 strarray_add(comp_args
, "-D__int64=long long");
291 /* options we handle explicitly */
292 if (opts
->compile_only
)
293 strarray_add(comp_args
, "-c");
294 if (opts
->output_name
)
296 strarray_add(comp_args
, "-o");
297 strarray_add(comp_args
, opts
->output_name
);
300 /* the rest of the pass-through parameters */
301 for ( j
= 0 ; j
< opts
->compiler_args
->size
; j
++ )
302 strarray_add(comp_args
, opts
->compiler_args
->base
[j
]);
304 /* last, but not least, the files */
305 for ( j
= 0; j
< opts
->files
->size
; j
++ )
306 strarray_add(comp_args
, opts
->files
->base
[j
]);
311 static const char* compile_to_object(struct options
* opts
, const char* file
)
313 struct options copts
;
316 /* make a copy we so don't change any of the initial stuff */
317 /* a shallow copy is exactly what we want in this case */
318 base_name
= get_basename(file
);
320 copts
.processor
= proc_cc
;
321 copts
.output_name
= get_temp_file(base_name
, ".o");
322 copts
.compile_only
= 1;
323 copts
.files
= strarray_alloc();
324 strarray_add(copts
.files
, file
);
326 strarray_free(copts
.files
);
329 return copts
.output_name
;
332 static void build(struct options
* opts
)
334 static const char *stdlibpath
[] = { DLLDIR
, LIBDIR
, "/usr/lib", "/usr/local/lib" };
335 strarray
*so_libs
, *arh_libs
, *dll_libs
, *lib_paths
, *lib_dirs
;
336 strarray
*res_files
, *obj_files
, *spec_args
, *comp_args
, *link_args
;
337 char *spec_c_name
, *spec_o_name
, *base_file
, *base_name
;
338 const char* output_name
;
339 const char *winebuild
= getenv("WINEBUILD");
340 int generate_app_loader
= 1;
343 if (!winebuild
) winebuild
= "winebuild";
345 so_libs
= strarray_alloc();
346 arh_libs
= strarray_alloc();
347 dll_libs
= strarray_alloc();
348 obj_files
= strarray_alloc();
349 res_files
= strarray_alloc();
350 lib_paths
= strarray_alloc();
352 output_name
= opts
->output_name
? opts
->output_name
: "a.out";
354 /* get base filename by removing the .exe extension, if present */
355 base_file
= strdup(output_name
);
356 if (strendswith(base_file
, ".exe.so"))
358 base_file
[strlen(base_file
) - 7] = 0;
359 generate_app_loader
= 0;
361 else if (strendswith(base_file
, ".exe")) base_file
[strlen(base_file
) - 4] = 0;
362 if ((base_name
= strrchr(base_file
, '/'))) base_name
++;
363 else base_name
= base_file
;
365 if (opts
->files
->size
== 1 && strendswith(opts
->files
->base
[0], ".exe.so"))
367 create_file(base_file
, 0755, app_loader_template
, opts
->files
->base
[0]);
371 /* prepare the linking path */
372 lib_dirs
= strarray_dup(opts
->lib_dirs
);
373 for ( j
= 0; j
< sizeof(stdlibpath
)/sizeof(stdlibpath
[0]);j
++ )
374 strarray_add(lib_dirs
, stdlibpath
[j
]);
376 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
377 strarray_add(lib_paths
, strmake("-L%s", lib_dirs
->base
[j
]));
379 /* prepare the libraries */
380 for ( j
= 0; j
< opts
->lib_names
->size
; j
++ )
382 const char* name
= opts
->lib_names
->base
[j
];
384 switch(get_lib_type(lib_dirs
, name
, &fullname
))
387 strarray_add(arh_libs
, strdup(fullname
));
390 strarray_add(dll_libs
, strmake("-l%s", name
));
393 strarray_add(so_libs
, strmake("-l%s", name
));
396 fprintf(stderr
, "Can't find library '%s', ignoring\n", name
);
403 if (opts
->use_msvcrt
) strarray_add(dll_libs
, "-lmsvcrt");
406 if (!opts
->nodefaultlibs
)
410 strarray_add(dll_libs
, "-lshell32");
411 strarray_add(dll_libs
, "-lcomdlg32");
412 strarray_add(dll_libs
, "-lgdi32");
414 strarray_add(dll_libs
, "-ladvapi32");
415 strarray_add(dll_libs
, "-luser32");
416 strarray_add(dll_libs
, "-lkernel32");
419 /* sort object file */
420 for ( j
= 0; j
< opts
->files
->size
; j
++ )
422 const char* file
= opts
->files
->base
[j
];
423 switch(get_file_type(file
))
426 /* FIXME: invoke wrc to build it */
429 strarray_add(res_files
, file
);
432 strarray_add(obj_files
, file
);
435 error("File does not exist: %s", file
);
438 file
= compile_to_object(opts
, file
);
439 strarray_add(obj_files
, file
);
444 /* run winebuild to generate the .spec.c file */
445 spec_args
= strarray_alloc();
446 spec_c_name
= get_temp_file(base_name
, ".spec.c");
447 strarray_add(spec_args
, winebuild
);
448 strarray_add(spec_args
, "-o");
449 strarray_add(spec_args
, spec_c_name
);
450 strarray_add(spec_args
, "--exe");
451 strarray_add(spec_args
, strmake("%s.exe", base_name
));
452 strarray_add(spec_args
, opts
->gui_app
? "-mgui" : "-mcui");
454 for ( j
= 0; j
< lib_paths
->size
; j
++ )
455 strarray_add(spec_args
, lib_paths
->base
[j
]);
457 for ( j
= 0 ; j
< opts
->winebuild_args
->size
; j
++ )
458 strarray_add(spec_args
, opts
->winebuild_args
->base
[j
]);
460 for ( j
= 0; j
< dll_libs
->size
; j
++ )
461 strarray_add(spec_args
, dll_libs
->base
[j
]);
463 for ( j
= 0; j
< res_files
->size
; j
++ )
464 strarray_add(spec_args
, res_files
->base
[j
]);
466 for ( j
= 0; j
< obj_files
->size
; j
++ )
467 strarray_add(spec_args
, obj_files
->base
[j
]);
469 for ( j
= 0; j
< arh_libs
->size
; j
++)
470 strarray_add(spec_args
, arh_libs
->base
[j
]);
474 /* compile the .spec.c file into a .spec.o file */
475 comp_args
= strarray_alloc();
476 spec_o_name
= get_temp_file(base_name
, ".spec.o");
477 strarray_add(comp_args
, CC
);
478 strarray_addall(comp_args
, strarray_fromstring(DLLFLAGS
, " "));
479 strarray_add(comp_args
, "-o");
480 strarray_add(comp_args
, spec_o_name
);
481 strarray_add(comp_args
, "-c");
482 strarray_add(comp_args
, spec_c_name
);
486 /* link everything together now */
487 link_args
= strarray_alloc();
488 strarray_add(link_args
, get_translator(opts
));
489 strarray_addall(link_args
, strarray_fromstring(LDDLLFLAGS
, " "));
491 strarray_add(link_args
, "-o");
492 strarray_add(link_args
, strmake("%s.exe.so", base_file
));
494 for ( j
= 0 ; j
< opts
->linker_args
->size
; j
++ )
495 strarray_add(link_args
, opts
->linker_args
->base
[j
]);
497 for ( j
= 0; j
< lib_paths
->size
; j
++ )
498 strarray_add(link_args
, lib_paths
->base
[j
]);
500 strarray_add(link_args
, "-lwine");
501 strarray_add(link_args
, "-lm");
503 for ( j
= 0; j
< so_libs
->size
; j
++ )
504 strarray_add(link_args
, so_libs
->base
[j
]);
506 for ( j
= 0; j
< obj_files
->size
; j
++ )
507 strarray_add(link_args
, obj_files
->base
[j
]);
509 strarray_add(link_args
, spec_o_name
);
511 for ( j
= 0; j
< arh_libs
->size
; j
++ )
512 strarray_add(link_args
, arh_libs
->base
[j
]);
516 /* create the loader script */
517 if (generate_app_loader
)
518 create_file(base_file
, 0755, app_loader_template
, strmake("%s.exe.so", base_name
));
522 static void forward(int argc
, char **argv
, struct options
* opts
)
524 strarray
*args
= strarray_alloc();
527 strarray_add(args
, get_translator(opts
));
529 for( j
= 1; j
< argc
; j
++ )
530 strarray_add(args
, argv
[j
]);
537 * object-file-name -llibrary -nostartfiles -nodefaultlibs
538 * -nostdlib -s -static -static-libgcc -shared -shared-libgcc
539 * -symbolic -Wl,option -Xlinker option -u symbol
541 static int is_linker_arg(const char* arg
)
543 static const char* link_switches
[] =
545 "-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s",
546 "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic"
556 if (strncmp("-Wl,", arg
, 4) == 0) return 1;
559 if (strcmp("-Xlinker", arg
) == 0) return 1;
563 for (j
= 0; j
< sizeof(link_switches
)/sizeof(link_switches
[0]); j
++)
564 if (strcmp(link_switches
[j
], arg
) == 0) return 1;
571 * -b machine -V version
573 static int is_target_arg(const char* arg
)
575 return arg
[1] == 'b' || arg
[2] == 'V';
581 * -Bprefix -Idir -I- -Ldir -specs=file
583 static int is_directory_arg(const char* arg
)
585 return arg
[1] == 'B' || arg
[1] == 'L' || arg
[1] == 'I' || strncmp("-specs=", arg
, 7) == 0;
590 * -mno-cygwin -mwindows -mconsole -mthreads
592 static int is_mingw_arg(const char* arg
)
594 static const char* mingw_switches
[] =
596 "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads"
600 for (j
= 0; j
< sizeof(mingw_switches
)/sizeof(mingw_switches
[0]); j
++)
601 if (strcmp(mingw_switches
[j
], arg
) == 0) return 1;
606 int main(int argc
, char **argv
)
608 int i
, c
, next_is_arg
= 0, linking
= 1;
609 int raw_compiler_arg
, raw_linker_arg
;
610 const char* option_arg
;
613 /* setup tmp file removal at exit */
614 tmp_files
= strarray_alloc();
615 atexit(clean_temp_files
);
617 /* initialize options */
618 memset(&opts
, 0, sizeof(opts
));
619 opts
.lib_names
= strarray_alloc();
620 opts
.lib_dirs
= strarray_alloc();
621 opts
.files
= strarray_alloc();
622 opts
.linker_args
= strarray_alloc();
623 opts
.compiler_args
= strarray_alloc();
624 opts
.winebuild_args
= strarray_alloc();
626 /* determine the processor type */
627 if (strendswith(argv
[0], "winecpp")) opts
.processor
= proc_pp
;
628 else if (strendswith(argv
[0], "++")) opts
.processor
= proc_cpp
;
631 for ( i
= 1 ; i
< argc
; i
++ )
633 if (argv
[i
][0] == '-') /* option */
635 /* determine if tihs switch is followed by a separate argument */
640 case 'x': case 'o': case 'D': case 'U':
641 case 'I': case 'A': case 'l': case 'u':
642 case 'b': case 'V': case 'G': case 'L':
643 if (argv
[i
][2]) option_arg
= &argv
[i
][2];
644 else next_is_arg
= 1;
650 if (strcmp("-aux-info", argv
[i
]) == 0)
654 if (strcmp("-Xlinker", argv
[i
]) == 0)
659 if (c
== 'F' || c
== 'T' || c
== 'Q')
661 if (argv
[i
][3]) option_arg
= &argv
[i
][3];
662 else next_is_arg
= 1;
666 if (next_is_arg
) option_arg
= argv
[i
+1];
668 /* determine what options go 'as is' to the linker & the compiler */
669 raw_compiler_arg
= raw_linker_arg
= 0;
670 if (is_linker_arg(argv
[i
]))
676 if (is_directory_arg(argv
[i
]) || is_target_arg(argv
[i
]))
678 raw_compiler_arg
= !is_mingw_arg(argv
[i
]);
681 /* these things we handle explicitly so we don't pass them 'as is' */
682 if (argv
[i
][1] == 'l' || argv
[i
][1] == 'I' || argv
[i
][1] == 'L')
684 if (argv
[i
][1] == 'c' || argv
[i
][1] == 'L')
685 raw_compiler_arg
= 0;
686 if (argv
[i
][1] == 'o')
687 raw_compiler_arg
= raw_linker_arg
= 0;
689 /* put the arg into the appropriate bucket */
692 strarray_add(opts
.linker_args
, argv
[i
]);
693 if (next_is_arg
&& (i
+ 1 < argc
))
694 strarray_add(opts
.linker_args
, argv
[i
+ 1]);
696 if (raw_compiler_arg
)
698 strarray_add(opts
.compiler_args
, argv
[i
]);
699 if (next_is_arg
&& (i
+ 1 < argc
))
700 strarray_add(opts
.compiler_args
, argv
[i
+ 1]);
703 /* do a bit of semantic analysis */
706 case 'c': /* compile or assemble */
707 if (argv
[i
][2] == 0) opts
.compile_only
= 1;
709 case 'S': /* generate assembler code */
710 case 'E': /* preprocess only */
711 if (argv
[i
][2] == 0) linking
= 0;
714 if (strcmp("-fno-short-wchar", argv
[i
]) == 0)
715 opts
.noshortwchar
= 1;
718 strarray_add(opts
.lib_names
, option_arg
);
721 strarray_add(opts
.lib_dirs
, option_arg
);
723 case 'M': /* map file generation */
727 if (strcmp("-mno-cygwin", argv
[i
]) == 0)
729 else if (strcmp("-mwindows", argv
[i
]) == 0)
731 else if (strcmp("-mconsole", argv
[i
]) == 0)
735 if (strcmp("-nostdinc", argv
[i
]) == 0)
737 else if (strcmp("-nodefaultlibs", argv
[i
]) == 0)
738 opts
.nodefaultlibs
= 1;
739 else if (strcmp("-nostdlib", argv
[i
]) == 0)
743 opts
.output_name
= option_arg
;
746 if (strcmp("-static", argv
[i
]) == 0)
748 else if(strcmp("-save-temps", argv
[i
]) == 0)
752 if (argv
[i
][2] == 0) verbose
++;
755 if (strncmp("-Wl,", argv
[i
], 4) == 0)
757 if (strstr(argv
[i
], "-static"))
760 else if (strncmp("-Wb,", argv
[i
], 4) == 0)
762 strarray
* Wb
= strarray_fromstring(argv
[i
] + 4, ",");
763 strarray_addall(opts
.winebuild_args
, Wb
);
768 if (strcmp("-static", argv
[i
]+1) == 0)
773 /* skip the next token if it's an argument */
774 if (next_is_arg
) i
++;
778 strarray_add(opts
.files
, argv
[i
]);
782 if (opts
.processor
== proc_pp
) linking
= 0;
783 if (linking
== -1) error("Static linking is not supported.");
785 if (opts
.files
->size
== 0) forward(argc
, argv
, &opts
);
786 else if (linking
) build(&opts
);