wow64: Correct a reversed strcpy in wow64_NtQuerySystemInformation(SystemModuleInform...
[wine.git] / tools / winegcc / winegcc.c
blob34f8a1e4e875966d14d6d208d82b638f66ae3591
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 -static-libstdc++
75 * -shared -shared-libgcc -symbolic -Wl,option
76 * -Xlinker option -u symbol --image-base -fuse-ld
78 * Directory Options
79 * -Bprefix -Idir -I- -Ldir -specs=file
81 * Target Options
82 * -b machine -V version
84 * Please note that the Target Options are relevant to everything:
85 * compiler, linker, assembler, preprocessor.
87 */
89 #include "config.h"
91 #include <assert.h>
92 #include <stdio.h>
93 #include <stdlib.h>
94 #include <stdarg.h>
95 #include <string.h>
96 #include <errno.h>
97 #include <ctype.h>
98 #include <limits.h>
99 #include <sys/types.h>
101 #include "utils.h"
103 static const char* app_loader_template =
104 "#!/bin/sh\n"
105 "\n"
106 "appname=\"%s\"\n"
107 "# determine the application directory\n"
108 "appdir=''\n"
109 "case \"$0\" in\n"
110 " */*)\n"
111 " # $0 contains a path, use it\n"
112 " appdir=`dirname \"$0\"`\n"
113 " ;;\n"
114 " *)\n"
115 " # no directory in $0, search in PATH\n"
116 " saved_ifs=$IFS\n"
117 " IFS=:\n"
118 " for d in $PATH\n"
119 " do\n"
120 " IFS=$saved_ifs\n"
121 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
122 " done\n"
123 " ;;\n"
124 "esac\n"
125 "\n"
126 "# figure out the full app path\n"
127 "if [ -n \"$appdir\" ]; then\n"
128 " apppath=\"$appdir/$appname\"\n"
129 " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
130 " export WINEDLLPATH\n"
131 "else\n"
132 " apppath=\"$appname\"\n"
133 "fi\n"
134 "\n"
135 "# determine the WINELOADER\n"
136 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
137 "\n"
138 "# and try to start the app\n"
139 "exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
142 static const char *output_file_name;
143 static const char *output_debug_file;
144 static const char *output_implib;
145 static int keep_generated = 0;
146 const char *temp_dir = NULL;
147 struct strarray temp_files = { 0 };
149 static const char *bindir;
150 static const char *libdir;
151 static const char *includedir;
153 enum processor { proc_cc, proc_cxx, proc_cpp, proc_as };
155 struct options
157 enum processor processor;
158 struct target target;
159 const char *target_alias;
160 const char *version;
161 int shared;
162 int use_msvcrt;
163 int nostdinc;
164 int nostdlib;
165 int nostartfiles;
166 int nodefaultlibs;
167 int noshortwchar;
168 int data_only;
169 int gui_app;
170 int unicode_app;
171 int win16_app;
172 int compile_only;
173 int force_pointer_size;
174 int large_address_aware;
175 int wine_builtin;
176 int fake_module;
177 int unwind_tables;
178 int strip;
179 int pic;
180 const char* wine_objdir;
181 const char* winebuild;
182 const char* output_name;
183 const char* image_base;
184 const char* section_align;
185 const char* file_align;
186 const char* sysroot;
187 const char* isysroot;
188 const char* lib_suffix;
189 const char* subsystem;
190 const char* entry_point;
191 const char* debug_file;
192 const char* out_implib;
193 struct strarray prefix;
194 struct strarray lib_dirs;
195 struct strarray args;
196 struct strarray linker_args;
197 struct strarray compiler_args;
198 struct strarray winebuild_args;
199 struct strarray files;
200 struct strarray delayimports;
203 static void cleanup_output_files(void)
205 if (output_file_name) unlink( output_file_name );
206 if (output_debug_file) unlink( output_debug_file );
207 if (output_implib) unlink( output_implib );
210 static void clean_temp_files(void)
212 if (!keep_generated) remove_temp_files();
215 /* clean things up when aborting on a signal */
216 static void exit_on_signal( int sig )
218 exit(1); /* this will call the atexit functions */
221 static int is_pe_target( const struct options *opts )
223 switch (opts->target.platform)
225 case PLATFORM_MINGW:
226 case PLATFORM_CYGWIN:
227 case PLATFORM_WINDOWS:
228 return 1;
229 default:
230 return 0;
234 enum tool
236 TOOL_CC,
237 TOOL_CXX,
238 TOOL_CPP,
239 TOOL_LD,
240 TOOL_OBJCOPY,
243 static const struct
245 const char *base;
246 const char *llvm_base;
247 const char *deflt;
248 } tool_names[] =
250 { "gcc", "clang --driver-mode=gcc", CC },
251 { "g++", "clang --driver-mode=g++", CXX },
252 { "cpp", "clang --driver-mode=cpp", CPP },
253 { "ld", "ld.lld", LD },
254 { "objcopy", "llvm-objcopy" },
257 static struct strarray build_tool_name( struct options *opts, enum tool tool )
259 const char *base = tool_names[tool].base;
260 const char *llvm_base = tool_names[tool].llvm_base;
261 const char *deflt = tool_names[tool].deflt;
262 const char *path;
263 struct strarray ret = empty_strarray;
264 char* str;
266 if (opts->target_alias && opts->version)
268 str = strmake("%s-%s-%s", opts->target_alias, base, opts->version);
270 else if (opts->target_alias)
272 str = strmake("%s-%s", opts->target_alias, base);
274 else if (opts->version)
276 str = strmake("%s-%s", base, opts->version);
278 else
279 str = xstrdup((deflt && *deflt) ? deflt : base);
281 if ((path = find_binary( opts->prefix, str ))) return strarray_fromstring( path, " " );
283 if (!opts->version) str = xstrdup( llvm_base );
284 else str = strmake( "%s-%s", llvm_base, opts->version );
285 path = find_binary( opts->prefix, str );
286 if (!path)
288 error( "Could not find %s\n", base );
289 return ret;
292 ret = strarray_fromstring( path, " " );
293 if (!strncmp( llvm_base, "clang", 5 ))
295 if (opts->target_alias)
297 strarray_add( &ret, "-target" );
298 strarray_add( &ret, opts->target_alias );
300 strarray_add( &ret, "-Wno-unused-command-line-argument" );
301 strarray_add( &ret, "-fuse-ld=lld" );
303 return ret;
306 static struct strarray get_translator(struct options *opts)
308 switch(opts->processor)
310 case proc_cpp:
311 return build_tool_name( opts, TOOL_CPP );
312 case proc_cc:
313 case proc_as:
314 return build_tool_name( opts, TOOL_CC );
315 case proc_cxx:
316 return build_tool_name( opts, TOOL_CXX );
318 assert(0);
319 return empty_strarray;
322 static int try_link( struct strarray prefix, struct strarray link_tool, const char *cflags )
324 const char *in = make_temp_file( "try_link", ".c" );
325 const char *out = make_temp_file( "try_link", ".out" );
326 const char *err = make_temp_file( "try_link", ".err" );
327 struct strarray link = empty_strarray;
328 int sout = -1, serr = -1;
329 int ret;
331 create_file( in, 0644, "int main(void){return 1;}\n" );
333 strarray_addall( &link, link_tool );
334 strarray_add( &link, "-o" );
335 strarray_add( &link, out );
336 strarray_addall( &link, strarray_fromstring( cflags, " " ) );
337 strarray_add( &link, in );
339 sout = dup( fileno(stdout) );
340 freopen( err, "w", stdout );
341 serr = dup( fileno(stderr) );
342 freopen( err, "w", stderr );
343 ret = spawn( prefix, link, 1 );
344 if (sout >= 0)
346 dup2( sout, fileno(stdout) );
347 close( sout );
349 if (serr >= 0)
351 dup2( serr, fileno(stderr) );
352 close( serr );
354 return ret;
357 static struct strarray get_link_args( struct options *opts, const char *output_name )
359 struct strarray link_args = get_translator( opts );
360 struct strarray flags = empty_strarray;
362 strarray_addall( &link_args, opts->linker_args );
364 if (verbose > 1) strarray_add( &flags, "-v" );
366 switch (opts->target.platform)
368 case PLATFORM_APPLE:
369 strarray_add( &flags, "-bundle" );
370 strarray_add( &flags, "-multiply_defined" );
371 strarray_add( &flags, "suppress" );
372 if (opts->image_base)
374 strarray_add( &flags, "-image_base" );
375 strarray_add( &flags, opts->image_base );
377 if (opts->strip) strarray_add( &flags, "-Wl,-x" );
378 strarray_addall( &link_args, flags );
379 return link_args;
381 case PLATFORM_SOLARIS:
383 char *mapfile = make_temp_file( output_name, ".map" );
384 const char *align = opts->section_align ? opts->section_align : "0x1000";
386 create_file( mapfile, 0644, "text = A%s;\ndata = A%s;\n", align, align );
387 strarray_add( &flags, strmake("-Wl,-M,%s", mapfile) );
389 break;
391 case PLATFORM_ANDROID:
392 /* the Android loader requires a soname for all libraries */
393 strarray_add( &flags, strmake( "-Wl,-soname,%s.so", output_name ));
394 break;
396 case PLATFORM_MINGW:
397 case PLATFORM_CYGWIN:
398 if (opts->shared || opts->win16_app)
400 strarray_add( &flags, "-shared" );
401 strarray_add( &flags, "-Wl,--kill-at" );
403 else strarray_add( &flags, opts->gui_app ? "-mwindows" : "-mconsole" );
405 if (opts->unicode_app) strarray_add( &flags, "-municode" );
406 if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
407 if (opts->nostartfiles || opts->use_msvcrt) strarray_add( &flags, "-nostartfiles" );
408 if (opts->subsystem) strarray_add( &flags, strmake("-Wl,--subsystem,%s", opts->subsystem ));
410 strarray_add( &flags, "-Wl,--exclude-all-symbols" );
411 strarray_add( &flags, "-Wl,--nxcompat" );
413 if (opts->image_base) strarray_add( &flags, strmake("-Wl,--image-base,%s", opts->image_base ));
415 if (opts->large_address_aware && opts->target.cpu == CPU_i386)
416 strarray_add( &flags, "-Wl,--large-address-aware" );
418 /* make sure we don't need a libgcc_s dll on Windows */
419 if (!opts->nodefaultlibs && !opts->use_msvcrt)
420 strarray_add( &flags, "-static-libgcc" );
422 if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
423 strarray_add(&link_args, strmake("-Wl,--pdb=%s", opts->debug_file));
425 if (opts->out_implib)
426 strarray_add(&link_args, strmake("-Wl,--out-implib,%s", opts->out_implib));
428 if (!try_link( opts->prefix, link_args, "-Wl,--file-alignment,0x1000" ))
429 strarray_add( &link_args, strmake( "-Wl,--file-alignment,%s",
430 opts->file_align ? opts->file_align : "0x1000" ));
431 else if (!try_link( opts->prefix, link_args, "-Wl,-Xlink=-filealign:0x1000" ))
432 /* lld from llvm 10 does not support mingw style --file-alignment,
433 * but it's possible to use msvc syntax */
434 strarray_add( &link_args, strmake( "-Wl,-Xlink=-filealign:%s",
435 opts->file_align ? opts->file_align : "0x1000" ));
437 strarray_addall( &link_args, flags );
438 return link_args;
440 case PLATFORM_WINDOWS:
441 if (opts->shared || opts->win16_app)
443 strarray_add( &flags, "-shared" );
444 strarray_add( &flags, "-Wl,-kill-at" );
446 if (opts->unicode_app) strarray_add( &flags, "-municode" );
447 if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
448 if (opts->nostartfiles) strarray_add( &flags, "-nostartfiles" );
449 if (opts->use_msvcrt) strarray_add( &flags, "-nostdlib" );
450 if (opts->image_base) strarray_add( &flags, strmake("-Wl,-base:%s", opts->image_base ));
451 if (opts->subsystem)
452 strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->subsystem ));
453 else
454 strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->gui_app ? "windows" : "console" ));
456 if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
458 strarray_add(&link_args, "-Wl,-debug");
459 strarray_add(&link_args, strmake("-Wl,-pdb:%s", opts->debug_file));
461 else if (!opts->strip)
462 strarray_add(&link_args, "-Wl,-debug:dwarf");
464 if (opts->out_implib)
465 strarray_add(&link_args, strmake("-Wl,-implib:%s", opts->out_implib));
467 strarray_add( &link_args, strmake( "-Wl,-filealign:%s", opts->file_align ? opts->file_align : "0x1000" ));
469 strarray_addall( &link_args, flags );
470 return link_args;
472 default:
473 if (opts->image_base)
475 if (!try_link( opts->prefix, link_args, strmake("-Wl,-Ttext-segment=%s", opts->image_base)) )
476 strarray_add( &flags, strmake("-Wl,-Ttext-segment=%s", opts->image_base) );
478 if (!try_link( opts->prefix, link_args, "-Wl,-z,max-page-size=0x1000"))
479 strarray_add( &flags, "-Wl,-z,max-page-size=0x1000");
480 break;
483 /* generic Unix shared library flags */
485 strarray_add( &link_args, "-shared" );
486 strarray_add( &link_args, "-Wl,-Bsymbolic" );
487 if (!opts->noshortwchar && opts->target.cpu == CPU_ARM)
488 strarray_add( &flags, "-Wl,--no-wchar-size-warning" );
489 if (!try_link( opts->prefix, link_args, "-Wl,-z,defs" ))
490 strarray_add( &flags, "-Wl,-z,defs" );
492 strarray_addall( &link_args, flags );
493 return link_args;
496 static const char *get_multiarch_dir( struct target target )
498 switch (target.cpu)
500 case CPU_i386: return "/i386-linux-gnu";
501 case CPU_x86_64: return "/x86_64-linux-gnu";
502 case CPU_ARM: return "/arm-linux-gnueabi";
503 case CPU_ARM64: return "/aarch64-linux-gnu";
505 assert(0);
506 return NULL;
509 static char *get_lib_dir( struct options *opts )
511 const char *stdlibpath[] = { libdir, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
512 const char *bit_suffix, *other_bit_suffix, *build_multiarch, *target_multiarch, *winecrt0;
513 const char *root = opts->sysroot ? opts->sysroot : "";
514 unsigned int i;
515 struct stat st;
516 size_t build_len, target_len;
518 bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "64" : "32";
519 other_bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "32" : "64";
520 winecrt0 = strmake( "/wine%s/libwinecrt0.a", get_arch_dir( opts->target ));
521 build_multiarch = get_multiarch_dir( get_default_target() );
522 target_multiarch = get_multiarch_dir( opts->target );
523 build_len = strlen( build_multiarch );
524 target_len = strlen( target_multiarch );
526 for (i = 0; i < ARRAY_SIZE(stdlibpath); i++)
528 const char *root = (i && opts->sysroot) ? opts->sysroot : "";
529 char *p, *buffer;
531 if (!stdlibpath[i]) continue;
532 buffer = xmalloc( strlen(root) + strlen(stdlibpath[i]) +
533 strlen("/arm-linux-gnueabi") + strlen(winecrt0) + 1 );
534 strcpy( buffer, root );
535 strcat( buffer, stdlibpath[i] );
536 p = buffer + strlen(buffer);
537 while (p > buffer && p[-1] == '/') p--;
538 strcpy( p, winecrt0 );
539 if (!stat( buffer, &st )) goto found;
540 if (p > buffer + 2 && (!memcmp( p - 2, "32", 2 ) || !memcmp( p - 2, "64", 2 )))
542 p -= 2;
543 strcpy( p, winecrt0 );
544 if (!stat( buffer, &st )) goto found;
546 strcpy( p, bit_suffix );
547 strcat( p, winecrt0 );
548 if (!stat( buffer, &st )) goto found;
549 strcpy( p, target_multiarch );
550 strcat( p, winecrt0 );
551 if (!stat( buffer, &st )) goto found;
553 strcpy( buffer, root );
554 strcat( buffer, stdlibpath[i] );
555 p = buffer + strlen(buffer);
556 while (p > buffer && p[-1] == '/') p--;
557 strcpy( p, winecrt0 );
559 /* try to fixup each parent dirs named lib, lib32 or lib64 with target bitness suffix */
560 while (p > buffer)
562 p--;
563 while (p > buffer && *p != '/') p--;
564 if (*p != '/') break;
566 /* try s/$build_cpu/$target_cpu/ on multiarch */
567 if (get_default_target().cpu != opts->target.cpu &&
568 !memcmp( p, build_multiarch, build_len ) && p[build_len] == '/')
570 memmove( p + target_len, p + build_len, strlen( p + build_len ) + 1 );
571 memcpy( p, target_multiarch, target_len );
572 if (!stat( buffer, &st )) goto found;
573 memmove( p + build_len, p + target_len, strlen( p + target_len ) + 1 );
574 memcpy( p, build_multiarch, build_len );
577 if (memcmp( p + 1, "lib", 3 )) continue;
578 if (p[4] == '/')
580 memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
581 memcpy( p + 4, bit_suffix, 2 );
582 if (!stat( buffer, &st )) goto found;
583 memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
585 else if (!memcmp( p + 4, other_bit_suffix, 2 ) && p[6] == '/')
587 memcpy( p + 4, bit_suffix, 2 );
588 if (!stat( buffer, &st )) goto found;
589 memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
590 if (!stat( buffer, &st )) goto found;
591 memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
592 memcpy( p + 4, other_bit_suffix, 2 );
596 free( buffer );
597 continue;
599 found:
600 buffer[strlen(buffer) - strlen(winecrt0)] = 0;
601 return buffer;
603 return strmake( "%s%s", root, LIBDIR );
606 static void init_argv0_dir( const char *argv0 )
608 if (!(bindir = get_argv0_dir( argv0 ))) return;
609 includedir = strmake( "%s/%s", bindir, BIN_TO_INCLUDEDIR );
610 libdir = strmake( "%s/%s", bindir, BIN_TO_LIBDIR );
613 static void compile(struct options* opts, const char* lang)
615 struct strarray comp_args = get_translator(opts);
616 unsigned int i, j;
617 int gcc_defs = 0;
618 struct strarray gcc;
619 struct strarray gpp;
621 if (opts->force_pointer_size)
622 strarray_add( &comp_args, strmake("-m%u", 8 * opts->force_pointer_size ) );
623 switch(opts->processor)
625 case proc_cpp: gcc_defs = 1; break;
626 case proc_as: gcc_defs = 0; break;
627 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
628 /* mixing different C and C++ compilers isn't supported in configure anyway */
629 case proc_cc:
630 case proc_cxx:
631 gcc = build_tool_name(opts, TOOL_CC);
632 gpp = build_tool_name(opts, TOOL_CXX);
633 for ( j = 0; !gcc_defs && j < comp_args.count; j++ )
635 const char *cc = comp_args.str[j];
637 for (i = 0; !gcc_defs && i < gcc.count; i++)
638 gcc_defs = gcc.str[i][0] != '-' && strendswith(cc, gcc.str[i]);
639 for (i = 0; !gcc_defs && i < gpp.count; i++)
640 gcc_defs = gpp.str[i][0] != '-' && strendswith(cc, gpp.str[i]);
642 break;
645 if (opts->target.platform == PLATFORM_WINDOWS ||
646 opts->target.platform == PLATFORM_CYGWIN ||
647 opts->target.platform == PLATFORM_MINGW)
648 goto no_compat_defines;
650 if (opts->processor != proc_cpp)
652 if (gcc_defs && !opts->wine_objdir && !opts->noshortwchar)
654 strarray_add(&comp_args, "-fshort-wchar");
655 strarray_add(&comp_args, "-DWINE_UNICODE_NATIVE");
657 strarray_add(&comp_args, "-D_REENTRANT");
658 if (opts->pic)
659 strarray_add(&comp_args, "-fPIC");
660 else
661 strarray_add(&comp_args, "-fno-PIC");
664 if (get_target_ptr_size( opts->target ) == 8)
666 strarray_add(&comp_args, "-DWIN64");
667 strarray_add(&comp_args, "-D_WIN64");
668 strarray_add(&comp_args, "-D__WIN64");
669 strarray_add(&comp_args, "-D__WIN64__");
672 strarray_add(&comp_args, "-DWIN32");
673 strarray_add(&comp_args, "-D_WIN32");
674 strarray_add(&comp_args, "-D__WIN32");
675 strarray_add(&comp_args, "-D__WIN32__");
676 strarray_add(&comp_args, "-D__WINNT");
677 strarray_add(&comp_args, "-D__WINNT__");
679 if (gcc_defs)
681 switch (opts->target.cpu)
683 case CPU_x86_64:
684 case CPU_ARM64:
685 strarray_add(&comp_args, "-D__stdcall=__attribute__((ms_abi))");
686 strarray_add(&comp_args, "-D__cdecl=__stdcall");
687 strarray_add(&comp_args, "-D__fastcall=__stdcall");
688 break;
689 case CPU_i386:
690 strarray_add(&comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
691 strarray_add(&comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
692 strarray_add(&comp_args, "-D__fastcall=__attribute__((__fastcall__))");
693 break;
694 case CPU_ARM:
695 strarray_add(&comp_args, "-D__stdcall=__attribute__((pcs(\"aapcs-vfp\")))");
696 strarray_add(&comp_args, "-D__cdecl=__stdcall");
697 strarray_add(&comp_args, "-D__fastcall=__stdcall");
698 break;
700 strarray_add(&comp_args, "-D_stdcall=__stdcall");
701 strarray_add(&comp_args, "-D_cdecl=__cdecl");
702 strarray_add(&comp_args, "-D_fastcall=__fastcall");
703 strarray_add(&comp_args, "-D__declspec(x)=__declspec_##x");
704 strarray_add(&comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
705 strarray_add(&comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
706 strarray_add(&comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
707 strarray_add(&comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
708 strarray_add(&comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
709 strarray_add(&comp_args, "-D__declspec_naked=__attribute__((naked))");
710 strarray_add(&comp_args, "-D__declspec_noinline=__attribute__((noinline))");
711 strarray_add(&comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
712 strarray_add(&comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
713 strarray_add(&comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
714 strarray_add(&comp_args, "-D__declspec_selectany=__attribute__((weak))");
715 strarray_add(&comp_args, "-D__declspec_thread=__thread");
718 strarray_add(&comp_args, "-D__int8=char");
719 strarray_add(&comp_args, "-D__int16=short");
720 strarray_add(&comp_args, "-D__int32=int");
721 if (get_target_ptr_size( opts->target ) == 8)
722 strarray_add(&comp_args, "-D__int64=long");
723 else
724 strarray_add(&comp_args, "-D__int64=long long");
726 no_compat_defines:
727 strarray_add(&comp_args, "-D__WINE__");
729 /* options we handle explicitly */
730 if (opts->compile_only)
731 strarray_add(&comp_args, "-c");
732 if (opts->output_name)
734 strarray_add(&comp_args, "-o");
735 strarray_add(&comp_args, opts->output_name);
738 /* the rest of the pass-through parameters */
739 strarray_addall(&comp_args, opts->compiler_args);
741 /* the language option, if any */
742 if (lang && strcmp(lang, "-xnone"))
743 strarray_add(&comp_args, lang);
745 /* last, but not least, the files */
746 for ( j = 0; j < opts->files.count; j++ )
748 if (opts->files.str[j][0] != '-' || !opts->files.str[j][1]) /* not an option or bare '-' (i.e. stdin) */
749 strarray_add(&comp_args, opts->files.str[j]);
752 /* standard includes come last in the include search path */
753 if (!opts->wine_objdir && !opts->nostdinc)
755 const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
756 const char *root = opts->isysroot ? opts->isysroot : opts->sysroot ? opts->sysroot : "";
757 const char *isystem = gcc_defs ? "-isystem" : "-I";
758 const char *idirafter = gcc_defs ? "-idirafter" : "-I";
760 if (opts->use_msvcrt)
762 if (includedir) strarray_add( &comp_args, strmake( "%s%s/wine/msvcrt", isystem, includedir ));
763 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
765 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
766 strarray_add(&comp_args, strmake( "%s%s%s/wine/msvcrt", isystem, root, incl_dirs[j] ));
768 strarray_add(&comp_args, "-D__MSVCRT__");
770 if (includedir)
772 strarray_add( &comp_args, strmake( "%s%s/wine/windows", isystem, includedir ));
773 strarray_add( &comp_args, strmake( "%s%s", idirafter, includedir ));
775 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
777 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
778 strarray_add(&comp_args, strmake( "%s%s%s/wine/windows", isystem, root, incl_dirs[j] ));
779 strarray_add(&comp_args, strmake( "%s%s%s", idirafter, root, incl_dirs[j] ));
782 else if (opts->wine_objdir)
783 strarray_add(&comp_args, strmake("-I%s/include", opts->wine_objdir) );
785 spawn(opts->prefix, comp_args, 0);
788 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
790 struct options copts;
792 /* make a copy so we don't change any of the initial stuff */
793 /* a shallow copy is exactly what we want in this case */
794 copts = *opts;
795 copts.output_name = make_temp_file(get_basename_noext(file), ".o");
796 copts.compile_only = 1;
797 copts.files = empty_strarray;
798 strarray_add(&copts.files, file);
799 compile(&copts, lang);
800 return copts.output_name;
803 /* return the initial set of options needed to run winebuild */
804 static struct strarray get_winebuild_args(struct options *opts)
806 const char* winebuild = getenv("WINEBUILD");
807 const char *binary = NULL;
808 struct strarray spec_args = empty_strarray;
809 unsigned int i;
811 if (opts->winebuild)
812 binary = opts->winebuild;
813 else if (opts->wine_objdir)
814 binary = strmake( "%s/tools/winebuild/winebuild%s", opts->wine_objdir, EXEEXT );
815 else if (winebuild)
816 binary = find_binary( opts->prefix, winebuild );
817 else if (bindir)
818 binary = strmake( "%s/winebuild%s", bindir, EXEEXT );
819 else
820 binary = find_binary( opts->prefix, "winebuild" );
821 if (!binary) error( "Could not find winebuild\n" );
822 strarray_add( &spec_args, binary );
823 if (verbose) strarray_add( &spec_args, "-v" );
824 if (keep_generated) strarray_add( &spec_args, "--save-temps" );
825 if (opts->target_alias)
827 strarray_add( &spec_args, "--target" );
828 strarray_add( &spec_args, opts->target_alias );
830 if (opts->force_pointer_size)
831 strarray_add(&spec_args, strmake("-m%u", 8 * opts->force_pointer_size ));
832 for (i = 0; i < opts->prefix.count; i++)
833 strarray_add( &spec_args, strmake( "-B%s", opts->prefix.str[i] ));
834 strarray_addall( &spec_args, opts->winebuild_args );
835 if (opts->unwind_tables) strarray_add( &spec_args, "-fasynchronous-unwind-tables" );
836 else strarray_add( &spec_args, "-fno-asynchronous-unwind-tables" );
837 return spec_args;
840 static void fixup_constructors( struct options *opts, const char *file )
842 struct strarray args = get_winebuild_args( opts );
844 strarray_add( &args, "--fixup-ctors" );
845 strarray_add( &args, file );
846 spawn( opts->prefix, args, 0 );
849 static void make_wine_builtin( struct options *opts, const char *file )
851 struct strarray args = get_winebuild_args( opts );
853 strarray_add( &args, "--builtin" );
854 strarray_add( &args, file );
855 spawn( opts->prefix, args, 0 );
858 /* check if there is a static lib associated to a given dll */
859 static char *find_static_lib( const char *dll )
861 char *lib = strmake("%s.a", dll);
862 if (get_file_type(lib) == file_arh) return lib;
863 free( lib );
864 return NULL;
867 static const char *find_libgcc(struct strarray prefix, struct strarray link_tool)
869 const char *out = make_temp_file( "find_libgcc", ".out" );
870 const char *err = make_temp_file( "find_libgcc", ".err" );
871 struct strarray link = empty_strarray;
872 int sout = -1, serr = -1;
873 char *libgcc, *p;
874 struct stat st;
875 size_t cnt;
876 int ret;
878 strarray_addall( &link, link_tool );
879 strarray_add( &link, "-print-libgcc-file-name" );
881 sout = dup( fileno(stdout) );
882 freopen( out, "w", stdout );
883 serr = dup( fileno(stderr) );
884 freopen( err, "w", stderr );
885 ret = spawn( prefix, link, 1 );
886 if (sout >= 0)
888 dup2( sout, fileno(stdout) );
889 close( sout );
891 if (serr >= 0)
893 dup2( serr, fileno(stderr) );
894 close( serr );
897 if (ret || stat(out, &st) || !st.st_size) return NULL;
899 libgcc = xmalloc(st.st_size + 1);
900 sout = open(out, O_RDONLY);
901 if (sout == -1) return NULL;
902 cnt = read(sout, libgcc, st.st_size);
903 close(sout);
904 libgcc[cnt] = 0;
905 if ((p = strchr(libgcc, '\n'))) *p = 0;
906 return libgcc;
910 /* add specified library to the list of files */
911 static void add_library( struct options *opts, struct strarray lib_dirs,
912 struct strarray *files, const char *library )
914 char *static_lib, *fullname = 0;
916 switch(get_lib_type(opts->target, lib_dirs, library, "lib", opts->lib_suffix, &fullname))
918 case file_arh:
919 strarray_add(files, strmake("-a%s", fullname));
920 break;
921 case file_dll:
922 strarray_add(files, strmake("-d%s", fullname));
923 if ((static_lib = find_static_lib(fullname)))
925 strarray_add(files, strmake("-a%s",static_lib));
926 free(static_lib);
928 break;
929 case file_so:
930 default:
931 /* keep it anyway, the linker may know what to do with it */
932 strarray_add(files, strmake("-l%s", library));
933 break;
935 free(fullname);
938 /* run winebuild to generate the .spec.o file */
939 static const char *build_spec_obj( struct options *opts, const char *spec_file, const char *output_file,
940 struct strarray files, struct strarray lib_dirs, const char *entry_point )
942 unsigned int i;
943 int is_pe = is_pe_target( opts );
944 struct strarray spec_args = get_winebuild_args( opts );
945 struct strarray tool;
946 const char *spec_o_name, *output_name;
948 /* get the filename from the path */
949 output_name = get_basename( output_file );
951 tool = build_tool_name( opts, TOOL_CC );
952 strarray_add( &spec_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
953 if (!is_pe)
955 tool = build_tool_name( opts, TOOL_LD );
956 strarray_add( &spec_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
959 spec_o_name = make_temp_file(output_name, ".spec.o");
960 if (!is_pe)
962 if (opts->pic) strarray_add(&spec_args, "-fPIC");
963 if (opts->use_msvcrt) strarray_add(&spec_args, "-mno-cygwin");
965 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
966 if (opts->fake_module)
968 strarray_add(&spec_args, "--fake-module");
969 strarray_add(&spec_args, "-o");
970 strarray_add(&spec_args, output_file);
972 else
974 strarray_add(&spec_args, "-o");
975 strarray_add(&spec_args, spec_o_name);
977 if (spec_file)
979 strarray_add(&spec_args, "-E");
980 strarray_add(&spec_args, spec_file);
983 if (!opts->shared)
985 strarray_add(&spec_args, "-F");
986 strarray_add(&spec_args, output_name);
987 strarray_add(&spec_args, "--subsystem");
988 strarray_add(&spec_args, opts->gui_app ? "windows" : "console");
989 if (opts->large_address_aware) strarray_add( &spec_args, "--large-address-aware" );
992 if (opts->target.platform == PLATFORM_WINDOWS && opts->target.cpu == CPU_i386)
993 strarray_add(&spec_args, "--safeseh");
995 if (entry_point)
997 strarray_add(&spec_args, "--entry");
998 strarray_add(&spec_args, entry_point);
1001 if (opts->subsystem)
1003 strarray_add(&spec_args, "--subsystem");
1004 strarray_add(&spec_args, opts->subsystem);
1007 for (i = 0; i < lib_dirs.count; i++)
1008 strarray_add(&spec_args, strmake("-L%s", lib_dirs.str[i]));
1010 if (!is_pe)
1012 for (i = 0; i < opts->delayimports.count; i++)
1013 strarray_add(&spec_args, strmake("-d%s", opts->delayimports.str[i]));
1016 /* add resource files */
1017 for (i = 0; i < files.count; i++)
1018 if (files.str[i][1] == 'r') strarray_add(&spec_args, files.str[i]);
1020 /* add other files */
1021 strarray_add(&spec_args, "--");
1022 for (i = 0; i < files.count; i++)
1024 switch(files.str[i][1])
1026 case 'd':
1027 case 'a':
1028 case 'o':
1029 strarray_add(&spec_args, files.str[i] + 2);
1030 break;
1034 spawn(opts->prefix, spec_args, 0);
1035 return spec_o_name;
1038 /* run winebuild to generate a data-only library */
1039 static void build_data_lib( struct options *opts, const char *spec_file, const char *output_file, struct strarray files )
1041 unsigned int i;
1042 struct strarray spec_args = get_winebuild_args( opts );
1044 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
1045 strarray_add(&spec_args, "-o");
1046 strarray_add(&spec_args, output_file);
1047 if (spec_file)
1049 strarray_add(&spec_args, "-E");
1050 strarray_add(&spec_args, spec_file);
1053 /* add resource files */
1054 for (i = 0; i < files.count; i++)
1055 if (files.str[i][1] == 'r') strarray_add(&spec_args, files.str[i]);
1057 spawn(opts->prefix, spec_args, 0);
1060 static void build(struct options* opts)
1062 struct strarray lib_dirs = empty_strarray;
1063 struct strarray files = empty_strarray;
1064 struct strarray link_args;
1065 char *output_file, *output_path;
1066 const char *spec_o_name = NULL, *libgcc = NULL;
1067 const char *output_name, *spec_file, *lang;
1068 int generate_app_loader = 1;
1069 const char *crt_lib = NULL, *entry_point = NULL;
1070 int is_pe = is_pe_target( opts );
1071 unsigned int j;
1073 /* NOTE: for the files array we'll use the following convention:
1074 * -axxx: xxx is an archive (.a)
1075 * -dxxx: xxx is a DLL (.def)
1076 * -lxxx: xxx is an unsorted library
1077 * -oxxx: xxx is an object (.o)
1078 * -rxxx: xxx is a resource (.res)
1079 * -sxxx: xxx is a shared lib (.so)
1080 * -xlll: lll is the language (c, c++, etc.)
1083 output_file = xstrdup( opts->output_name ? opts->output_name : "a.out" );
1085 /* 'winegcc -o app xxx.exe.so' only creates the load script */
1086 if (opts->files.count == 1 && strendswith(opts->files.str[0], ".exe.so"))
1088 create_file(output_file, 0755, app_loader_template, opts->files.str[0]);
1089 return;
1092 /* generate app loader only for .exe */
1093 if (opts->shared || is_pe || strendswith(output_file, ".so"))
1094 generate_app_loader = 0;
1096 if (strendswith(output_file, ".fake")) opts->fake_module = 1;
1098 /* normalize the filename a bit: strip .so, ensure it has proper ext */
1099 if (!strchr(get_basename( output_file ), '.'))
1100 output_file = strmake("%s.%s", output_file, opts->shared ? "dll" : "exe");
1101 else if (strendswith(output_file, ".so"))
1102 output_file[strlen(output_file) - 3] = 0;
1103 output_path = is_pe ? output_file : strmake( "%s.so", output_file );
1105 /* get the filename from the path */
1106 output_name = get_basename( output_file );
1108 /* prepare the linking path */
1109 if (!opts->wine_objdir)
1111 char *lib_dir = get_lib_dir( opts );
1112 strarray_addall( &lib_dirs, opts->lib_dirs );
1113 strarray_add( &lib_dirs, strmake( "%s/wine%s", lib_dir, get_arch_dir( opts->target )));
1114 strarray_add( &lib_dirs, lib_dir );
1116 else
1118 strarray_add(&lib_dirs, strmake("%s/dlls", opts->wine_objdir));
1119 strarray_addall(&lib_dirs, opts->lib_dirs);
1122 /* mark the files with their appropriate type */
1123 spec_file = lang = 0;
1124 for ( j = 0; j < opts->files.count; j++ )
1126 const char* file = opts->files.str[j];
1127 if (file[0] != '-')
1129 switch(get_file_type(file))
1131 case file_def:
1132 case file_spec:
1133 if (spec_file)
1134 error("Only one spec file can be specified\n");
1135 spec_file = file;
1136 break;
1137 case file_rc:
1138 /* FIXME: invoke wrc to build it */
1139 error("Can't compile .rc file at the moment: %s\n", file);
1140 break;
1141 case file_res:
1142 strarray_add(&files, strmake("-r%s", file));
1143 break;
1144 case file_obj:
1145 strarray_add(&files, strmake("-o%s", file));
1146 break;
1147 case file_arh:
1148 if (opts->use_msvcrt)
1150 char *p = get_basename( file );
1151 if (!strncmp(p, "libmsvcr", 8) || !strncmp(p, "libucrt", 7)) crt_lib = file;
1153 strarray_add(&files, strmake("-a%s", file));
1154 break;
1155 case file_so:
1156 strarray_add(&files, strmake("-s%s", file));
1157 break;
1158 case file_na:
1159 error("File does not exist: %s\n", file);
1160 break;
1161 default:
1162 file = compile_to_object(opts, file, lang);
1163 strarray_add(&files, strmake("-o%s", file));
1164 break;
1167 else if (file[1] == 'l')
1168 add_library(opts, lib_dirs, &files, file + 2 );
1169 else if (file[1] == 'x')
1170 lang = file;
1171 else if(file[1] == 'W')
1172 strarray_add(&files, file);
1175 /* add the default libraries, if needed */
1177 if (!opts->wine_objdir && !opts->nodefaultlibs)
1179 if (opts->gui_app)
1181 add_library(opts, lib_dirs, &files, "shell32");
1182 add_library(opts, lib_dirs, &files, "comdlg32");
1183 add_library(opts, lib_dirs, &files, "gdi32");
1185 add_library(opts, lib_dirs, &files, "advapi32");
1186 add_library(opts, lib_dirs, &files, "user32");
1187 add_library(opts, lib_dirs, &files, "winecrt0");
1188 if (opts->use_msvcrt)
1190 if (!crt_lib)
1192 if (strncmp( output_name, "msvcr", 5 ) &&
1193 strncmp( output_name, "ucrt", 4 ) &&
1194 strcmp( output_name, "crtdll.dll" ))
1195 add_library(opts, lib_dirs, &files, "ucrtbase");
1197 else strarray_add(&files, strmake("-a%s", crt_lib));
1199 if (opts->win16_app) add_library(opts, lib_dirs, &files, "kernel");
1200 add_library(opts, lib_dirs, &files, "kernel32");
1201 add_library(opts, lib_dirs, &files, "ntdll");
1204 /* set default entry point, if needed */
1205 if (!opts->entry_point)
1207 if (opts->subsystem && !strcmp( opts->subsystem, "native" ))
1208 entry_point = (is_pe && opts->target.cpu == CPU_i386) ? "DriverEntry@8" : "DriverEntry";
1209 else if (opts->use_msvcrt && !opts->shared && !opts->win16_app)
1210 entry_point = opts->unicode_app ? "wmainCRTStartup" : "mainCRTStartup";
1212 else entry_point = opts->entry_point;
1214 /* run winebuild to generate the .spec.o file */
1215 if (opts->data_only)
1217 build_data_lib( opts, spec_file, output_file, files );
1218 return;
1220 spec_o_name = build_spec_obj( opts, spec_file, output_file, files, lib_dirs, entry_point );
1222 if (opts->fake_module) return; /* nothing else to do */
1224 /* link everything together now */
1225 link_args = get_link_args( opts, output_name );
1227 if (opts->nodefaultlibs || opts->use_msvcrt)
1229 switch (opts->target.platform)
1231 case PLATFORM_MINGW:
1232 case PLATFORM_CYGWIN:
1233 libgcc = find_libgcc( opts->prefix, link_args );
1234 if (!libgcc) libgcc = "-lgcc";
1235 break;
1236 default:
1237 break;
1241 strarray_add(&link_args, "-o");
1242 strarray_add(&link_args, output_path);
1244 for ( j = 0; j < lib_dirs.count; j++ )
1245 strarray_add(&link_args, strmake("-L%s", lib_dirs.str[j]));
1247 if (is_pe && opts->use_msvcrt && !entry_point && (opts->shared || opts->win16_app))
1248 entry_point = opts->target.cpu == CPU_i386 ? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1250 if (is_pe && entry_point)
1252 if (opts->target.platform == PLATFORM_WINDOWS)
1253 strarray_add(&link_args, strmake("-Wl,-entry:%s", entry_point));
1254 else
1255 strarray_add(&link_args, strmake("-Wl,--entry,%s%s",
1256 is_pe && opts->target.cpu == CPU_i386 ? "_" : "",
1257 entry_point));
1260 if (spec_o_name) strarray_add(&link_args, spec_o_name);
1262 if (is_pe)
1264 for (j = 0; j < opts->delayimports.count; j++)
1266 if (opts->target.platform == PLATFORM_WINDOWS)
1267 strarray_add(&link_args, strmake("-Wl,-delayload:%s", opts->delayimports.str[j]));
1268 else
1269 strarray_add(&link_args, strmake("-Wl,-delayload,%s",opts->delayimports.str[j]));
1273 for ( j = 0; j < files.count; j++ )
1275 const char* name = files.str[j] + 2;
1276 switch(files.str[j][1])
1278 case 'l':
1279 strarray_add(&link_args, strmake("-l%s", name));
1280 break;
1281 case 's':
1282 case 'o':
1283 strarray_add(&link_args, name);
1284 break;
1285 case 'a':
1286 if (!opts->use_msvcrt && !opts->lib_suffix && strchr(name, '/'))
1288 /* turn the path back into -Ldir -lfoo options
1289 * this makes sure that we use the specified libs even
1290 * when mingw adds its own import libs to the link */
1291 const char *p = get_basename( name );
1293 if (is_pe)
1295 if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
1297 strarray_add(&link_args, strmake("-L%s", get_dirname(name) ));
1298 strarray_add(&link_args, strmake("-l%s", get_basename_noext( p + 3 )));
1299 break;
1302 else
1304 /* don't link to ntdll or ntoskrnl in non-msvcrt mode
1305 * since they export CRT functions */
1306 if (!strcmp( p, "libntdll.a" )) break;
1307 if (!strcmp( p, "libntoskrnl.a" )) break;
1310 strarray_add(&link_args, name);
1311 break;
1312 case 'W':
1313 strarray_add(&link_args, files.str[j]);
1314 break;
1318 if (!opts->nostdlib && !is_pe)
1320 strarray_add(&link_args, "-ldl");
1321 strarray_add(&link_args, "-lm");
1322 strarray_add(&link_args, "-lc");
1325 if (libgcc) strarray_add(&link_args, libgcc);
1327 output_file_name = output_path;
1328 output_debug_file = opts->debug_file;
1329 output_implib = opts->out_implib;
1330 atexit( cleanup_output_files );
1332 spawn(opts->prefix, link_args, 0);
1334 if (opts->debug_file && !strendswith(opts->debug_file, ".pdb"))
1336 struct strarray tool, objcopy = build_tool_name(opts, TOOL_OBJCOPY);
1338 tool = empty_strarray;
1339 strarray_addall( &tool, objcopy );
1340 strarray_add(&tool, "--only-keep-debug");
1341 strarray_add(&tool, output_path);
1342 strarray_add(&tool, opts->debug_file);
1343 spawn(opts->prefix, tool, 1);
1345 tool = empty_strarray;
1346 strarray_addall( &tool, objcopy );
1347 strarray_add(&tool, "--strip-debug");
1348 strarray_add(&tool, output_path);
1349 spawn(opts->prefix, tool, 1);
1351 tool = empty_strarray;
1352 strarray_addall( &tool, objcopy );
1353 strarray_add(&tool, "--add-gnu-debuglink");
1354 strarray_add(&tool, opts->debug_file);
1355 strarray_add(&tool, output_path);
1356 spawn(opts->prefix, tool, 0);
1359 if (opts->out_implib && !is_pe)
1361 struct strarray tool, implib_args;
1363 if (!spec_file)
1364 error("--out-implib requires a .spec or .def file\n");
1366 implib_args = get_winebuild_args( opts );
1367 tool = build_tool_name( opts, TOOL_CC );
1368 strarray_add( &implib_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
1369 tool = build_tool_name( opts, TOOL_LD );
1370 strarray_add( &implib_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
1372 strarray_add(&implib_args, "--implib");
1373 strarray_add(&implib_args, "-o");
1374 strarray_add(&implib_args, opts->out_implib);
1375 strarray_add(&implib_args, "--export");
1376 strarray_add(&implib_args, spec_file);
1378 spawn(opts->prefix, implib_args, 0);
1381 if (!is_pe) fixup_constructors( opts, output_path );
1382 else if (opts->wine_builtin) make_wine_builtin( opts, output_path );
1384 /* create the loader script */
1385 if (generate_app_loader)
1386 create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
1390 static void forward( struct options *opts )
1392 struct strarray args = get_translator(opts);
1394 strarray_addall(&args, opts->compiler_args);
1395 strarray_addall(&args, opts->linker_args);
1396 spawn(opts->prefix, args, 0);
1399 static int is_linker_arg(const char* arg)
1401 static const char* link_switches[] =
1403 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1404 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1405 "-fprofile-generate", "-fprofile-use"
1407 unsigned int j;
1409 switch (arg[1])
1411 case 'R':
1412 case 'z':
1413 case 'u':
1414 return 1;
1415 case 'W':
1416 if (strncmp("-Wl,", arg, 4) == 0) return 1;
1417 break;
1418 case 'X':
1419 if (strcmp("-Xlinker", arg) == 0) return 1;
1420 break;
1421 case 'a':
1422 if (strcmp("-arch", arg) == 0) return 1;
1423 break;
1424 case 'f':
1425 if (strncmp("-fuse-ld=", arg, 9) == 0) return 1;
1426 break;
1427 case 'r':
1428 if (strncmp("-rtlib=", arg, 7) == 0) return 1;
1429 break;
1432 for (j = 0; j < ARRAY_SIZE(link_switches); j++)
1433 if (strcmp(link_switches[j], arg) == 0) return 1;
1435 return 0;
1438 static void parse_target_option( struct options *opts, const char *target )
1440 opts->target_alias = xstrdup( target );
1441 if (!parse_target( target, &opts->target )) error( "Invalid target specification '%s'\n", target );
1444 static int is_option( struct options *opts, int i, const char *option, const char **option_arg )
1446 if (!strcmp( opts->args.str[i], option ))
1448 if (opts->args.count == i) error( "option %s requires an argument\n", opts->args.str[i] );
1449 *option_arg = opts->args.str[i + 1];
1450 return 1;
1452 if (!strncmp( opts->args.str[i], option, strlen(option) ) && opts->args.str[i][strlen(option)] == '=')
1454 *option_arg = opts->args.str[i] + strlen(option) + 1;
1455 return 1;
1457 return 0;
1460 int main(int argc, char **argv)
1462 int i, c, next_is_arg = 0, linking = 1;
1463 int raw_compiler_arg, raw_linker_arg, raw_winebuild_arg;
1464 const char* option_arg;
1465 struct options opts;
1466 char* lang = 0;
1467 char* str;
1469 init_signals( exit_on_signal );
1470 init_argv0_dir( argv[0] );
1472 /* setup tmp file removal at exit */
1473 atexit(clean_temp_files);
1475 /* initialize options */
1476 memset(&opts, 0, sizeof(opts));
1477 opts.target = init_argv0_target( argv[0] );
1478 opts.pic = 1;
1480 /* determine the processor type */
1481 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
1482 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
1484 for (i = 1; i < argc; i++)
1486 char *input_buffer = NULL, *iter, *opt, *out;
1487 struct stat st;
1488 int fd;
1490 if (argv[i][0] != '@' || (fd = open( argv[i] + 1, O_RDONLY | O_BINARY )) == -1)
1492 strarray_add( &opts.args, argv[i] );
1493 continue;
1495 if ((fstat( fd, &st ) == -1)) error( "Cannot stat %s\n", argv[i] + 1 );
1496 if (st.st_size)
1498 input_buffer = xmalloc( st.st_size + 1 );
1499 if (read( fd, input_buffer, st.st_size ) != st.st_size) error( "Cannot read %s\n", argv[i] + 1 );
1501 close( fd );
1502 for (iter = input_buffer; iter < input_buffer + st.st_size; iter++)
1504 char quote = 0;
1505 while (iter < input_buffer + st.st_size && isspace(*iter)) iter++;
1506 if (iter == input_buffer + st.st_size) break;
1507 opt = out = iter;
1508 while (iter < input_buffer + st.st_size && (quote || !isspace(*iter)))
1510 if (*iter == quote)
1512 iter++;
1513 quote = 0;
1515 else if (*iter == '\'' || *iter == '"') quote = *iter++;
1516 else
1518 if (*iter == '\\' && iter + 1 < input_buffer + st.st_size) iter++;
1519 *out++ = *iter++;
1522 *out = 0;
1523 strarray_add( &opts.args, opt );
1527 /* parse options */
1528 for (i = 0; i < opts.args.count; i++)
1530 if (opts.args.str[i][0] == '-' && opts.args.str[i][1]) /* option, except '-' alone is stdin, which is a file */
1532 /* determine if this switch is followed by a separate argument */
1533 next_is_arg = 0;
1534 option_arg = 0;
1535 switch(opts.args.str[i][1])
1537 case 'x': case 'o': case 'D': case 'U':
1538 case 'I': case 'A': case 'l': case 'u':
1539 case 'b': case 'V': case 'G': case 'L':
1540 case 'B': case 'R': case 'z':
1541 if (opts.args.str[i][2]) option_arg = &opts.args.str[i][2];
1542 else next_is_arg = 1;
1543 break;
1544 case 'i':
1545 next_is_arg = 1;
1546 break;
1547 case 'a':
1548 if (strcmp("-aux-info", opts.args.str[i]) == 0)
1549 next_is_arg = 1;
1550 if (strcmp("-arch", opts.args.str[i]) == 0)
1551 next_is_arg = 1;
1552 break;
1553 case 'X':
1554 if (strcmp("-Xlinker", opts.args.str[i]) == 0)
1555 next_is_arg = 1;
1556 break;
1557 case 'M':
1558 c = opts.args.str[i][2];
1559 if (c == 'F' || c == 'T' || c == 'Q')
1561 if (opts.args.str[i][3]) option_arg = &opts.args.str[i][3];
1562 else next_is_arg = 1;
1564 break;
1565 case 'f':
1566 if (strcmp("-framework", opts.args.str[i]) == 0)
1567 next_is_arg = 1;
1568 break;
1569 case 't':
1570 next_is_arg = strcmp("-target", opts.args.str[i]) == 0;
1571 break;
1572 case '-':
1573 next_is_arg = (strcmp("--param", opts.args.str[i]) == 0 ||
1574 strcmp("--sysroot", opts.args.str[i]) == 0 ||
1575 strcmp("--target", opts.args.str[i]) == 0 ||
1576 strcmp("--wine-objdir", opts.args.str[i]) == 0 ||
1577 strcmp("--winebuild", opts.args.str[i]) == 0 ||
1578 strcmp("--lib-suffix", opts.args.str[i]) == 0);
1579 break;
1581 if (next_is_arg)
1583 if (i + 1 >= opts.args.count) error("option -%c requires an argument\n", opts.args.str[i][1]);
1584 option_arg = opts.args.str[i+1];
1587 /* determine what options go 'as is' to the linker & the compiler */
1588 raw_linker_arg = is_linker_arg(opts.args.str[i]);
1589 raw_compiler_arg = !raw_linker_arg;
1590 raw_winebuild_arg = 0;
1592 /* do a bit of semantic analysis */
1593 switch (opts.args.str[i][1])
1595 case 'B':
1596 str = xstrdup(option_arg);
1597 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
1598 strarray_add(&opts.prefix, str);
1599 raw_linker_arg = 1;
1600 break;
1601 case 'b':
1602 parse_target_option( &opts, option_arg );
1603 raw_compiler_arg = 0;
1604 break;
1605 case 'V':
1606 opts.version = xstrdup( option_arg );
1607 raw_compiler_arg = 0;
1608 break;
1609 case 'c': /* compile or assemble */
1610 raw_compiler_arg = 0;
1611 if (opts.args.str[i][2] == 0) opts.compile_only = 1;
1612 /* fall through */
1613 case 'S': /* generate assembler code */
1614 case 'E': /* preprocess only */
1615 if (opts.args.str[i][2] == 0) linking = 0;
1616 break;
1617 case 'f':
1618 if (strcmp("-fno-short-wchar", opts.args.str[i]) == 0)
1619 opts.noshortwchar = 1;
1620 else if (!strcmp("-fasynchronous-unwind-tables", opts.args.str[i]))
1621 opts.unwind_tables = 1;
1622 else if (!strcmp("-fno-asynchronous-unwind-tables", opts.args.str[i]))
1623 opts.unwind_tables = 0;
1624 else if (!strcmp("-fPIC", opts.args.str[i]) || !strcmp("-fpic", opts.args.str[i]))
1625 opts.pic = 1;
1626 else if (!strcmp("-fno-PIC", opts.args.str[i]) || !strcmp("-fno-pic", opts.args.str[i]))
1627 opts.pic = 0;
1628 break;
1629 case 'i':
1630 if (!strcmp( "-isysroot", opts.args.str[i] )) opts.isysroot = opts.args.str[i + 1];
1631 break;
1632 case 'l':
1633 strarray_add(&opts.files, strmake("-l%s", option_arg));
1634 raw_compiler_arg = 0;
1635 break;
1636 case 'L':
1637 strarray_add(&opts.lib_dirs, option_arg);
1638 raw_compiler_arg = 0;
1639 break;
1640 case 'M': /* map file generation */
1641 linking = 0;
1642 break;
1643 case 'm':
1644 if (strcmp("-mno-cygwin", opts.args.str[i]) == 0)
1646 opts.use_msvcrt = 1;
1647 raw_compiler_arg = 0;
1649 if (strcmp("-mcygwin", opts.args.str[i]) == 0)
1651 opts.use_msvcrt = 0;
1652 raw_compiler_arg = 0;
1654 else if (strcmp("-mwindows", opts.args.str[i]) == 0)
1656 opts.gui_app = 1;
1657 raw_compiler_arg = 0;
1659 else if (strcmp("-mconsole", opts.args.str[i]) == 0)
1661 opts.gui_app = 0;
1662 raw_compiler_arg = 0;
1664 else if (strcmp("-municode", opts.args.str[i]) == 0)
1666 opts.unicode_app = 1;
1667 raw_compiler_arg = 0;
1668 raw_winebuild_arg = 1;
1670 else if (strcmp("-mthreads", opts.args.str[i]) == 0)
1672 raw_compiler_arg = 0;
1674 else if (strcmp("-m16", opts.args.str[i]) == 0)
1676 opts.win16_app = 1;
1677 raw_compiler_arg = 0;
1678 raw_winebuild_arg = 1;
1680 else if (strcmp("-m32", opts.args.str[i]) == 0)
1682 set_target_ptr_size( &opts.target, 4 );
1683 opts.force_pointer_size = 4;
1684 raw_linker_arg = 1;
1686 else if (strcmp("-m64", opts.args.str[i]) == 0)
1688 set_target_ptr_size( &opts.target, 8 );
1689 opts.force_pointer_size = 8;
1690 raw_linker_arg = 1;
1692 else if (!strcmp("-marm", opts.args.str[i] ) || !strcmp("-mthumb", opts.args.str[i] ))
1694 raw_linker_arg = 1;
1695 raw_winebuild_arg = 1;
1697 else if (!strncmp("-mcpu=", opts.args.str[i], 6) ||
1698 !strncmp("-mfpu=", opts.args.str[i], 6) ||
1699 !strncmp("-march=", opts.args.str[i], 7) ||
1700 !strncmp("-mfloat-abi=", opts.args.str[i], 12))
1701 raw_winebuild_arg = 1;
1702 break;
1703 case 'n':
1704 if (strcmp("-nostdinc", opts.args.str[i]) == 0)
1705 opts.nostdinc = 1;
1706 else if (strcmp("-nodefaultlibs", opts.args.str[i]) == 0)
1707 opts.nodefaultlibs = 1;
1708 else if (strcmp("-nostdlib", opts.args.str[i]) == 0)
1709 opts.nostdlib = 1;
1710 else if (strcmp("-nostartfiles", opts.args.str[i]) == 0)
1711 opts.nostartfiles = 1;
1712 break;
1713 case 'o':
1714 opts.output_name = option_arg;
1715 raw_compiler_arg = 0;
1716 break;
1717 case 'p':
1718 if (strcmp("-pthread", opts.args.str[i]) == 0)
1720 raw_compiler_arg = 1;
1721 raw_linker_arg = 1;
1723 break;
1724 case 's':
1725 if (strcmp("-static", opts.args.str[i]) == 0)
1726 linking = -1;
1727 else if(strcmp("-save-temps", opts.args.str[i]) == 0)
1728 keep_generated = 1;
1729 else if (strncmp("-specs=", opts.args.str[i], 7) == 0)
1730 raw_linker_arg = 1;
1731 else if(strcmp("-shared", opts.args.str[i]) == 0)
1733 opts.shared = 1;
1734 raw_compiler_arg = raw_linker_arg = 0;
1736 else if (strcmp("-s", opts.args.str[i]) == 0 && opts.target.platform == PLATFORM_APPLE)
1738 /* On Mac, change -s into -Wl,-x. ld's -s switch
1739 * is deprecated, and it doesn't work on Tiger with
1740 * MH_BUNDLEs anyway
1742 opts.strip = 1;
1743 raw_linker_arg = 0;
1745 break;
1746 case 't':
1747 if (is_option( &opts, i, "-target", &option_arg ))
1749 parse_target_option( &opts, option_arg );
1750 raw_compiler_arg = raw_linker_arg = 0;
1752 break;
1753 case 'v':
1754 if (opts.args.str[i][2] == 0) verbose++;
1755 break;
1756 case 'W':
1757 if (strncmp("-Wl,", opts.args.str[i], 4) == 0)
1759 unsigned int j;
1760 struct strarray Wl = strarray_fromstring(opts.args.str[i] + 4, ",");
1761 for (j = 0; j < Wl.count; j++)
1763 if (!strcmp(Wl.str[j], "--image-base") && j < Wl.count - 1)
1765 opts.image_base = xstrdup( Wl.str[++j] );
1766 continue;
1768 if (!strcmp(Wl.str[j], "--section-alignment") && j < Wl.count - 1)
1770 opts.section_align = xstrdup( Wl.str[++j] );
1771 continue;
1773 if (!strcmp(Wl.str[j], "--file-alignment") && j < Wl.count - 1)
1775 opts.file_align = xstrdup( Wl.str[++j] );
1776 continue;
1778 if (!strcmp(Wl.str[j], "--large-address-aware"))
1780 opts.large_address_aware = 1;
1781 continue;
1783 if (!strcmp(Wl.str[j], "--wine-builtin"))
1785 opts.wine_builtin = 1;
1786 continue;
1788 if (!strcmp(Wl.str[j], "--subsystem") && j < Wl.count - 1)
1790 opts.subsystem = xstrdup( Wl.str[++j] );
1791 continue;
1793 if (!strcmp(Wl.str[j], "--entry") && j < Wl.count - 1)
1795 opts.entry_point = xstrdup( Wl.str[++j] );
1796 continue;
1798 if (!strcmp(Wl.str[j], "-delayload") && j < Wl.count - 1)
1800 strarray_add( &opts.delayimports, Wl.str[++j] );
1801 continue;
1803 if (!strcmp(Wl.str[j], "--debug-file") && j < Wl.count - 1)
1805 opts.debug_file = xstrdup( Wl.str[++j] );
1806 continue;
1808 if (!strcmp(Wl.str[j], "--whole-archive") ||
1809 !strcmp(Wl.str[j], "--no-whole-archive") ||
1810 !strcmp(Wl.str[j], "--start-group") ||
1811 !strcmp(Wl.str[j], "--end-group"))
1813 strarray_add( &opts.files, strmake( "-Wl,%s", Wl.str[j] ));
1814 continue;
1816 if (!strcmp(Wl.str[j], "--out-implib"))
1818 opts.out_implib = xstrdup( Wl.str[++j] );
1819 continue;
1821 if (!strcmp(Wl.str[j], "-static")) linking = -1;
1822 strarray_add(&opts.linker_args, strmake("-Wl,%s",Wl.str[j]));
1824 raw_compiler_arg = raw_linker_arg = 0;
1826 else if (strncmp("-Wb,", opts.args.str[i], 4) == 0)
1828 unsigned int j;
1829 struct strarray Wb = strarray_fromstring(opts.args.str[i] + 4, ",");
1830 for (j = 0; j < Wb.count; j++)
1832 if (!strcmp(Wb.str[j], "--data-only")) opts.data_only = 1;
1833 if (!strcmp(Wb.str[j], "--fake-module")) opts.fake_module = 1;
1834 else strarray_add( &opts.winebuild_args, Wb.str[j] );
1836 raw_compiler_arg = raw_linker_arg = 0;
1838 break;
1839 case 'x':
1840 lang = strmake("-x%s", option_arg);
1841 strarray_add(&opts.files, lang);
1842 /* we'll pass these flags ourselves, explicitly */
1843 raw_compiler_arg = raw_linker_arg = 0;
1844 break;
1845 case '-':
1846 if (strcmp("-static", opts.args.str[i]+1) == 0)
1847 linking = -1;
1848 else if (is_option( &opts, i, "--sysroot", &option_arg ))
1850 opts.sysroot = option_arg;
1851 raw_linker_arg = 1;
1853 else if (is_option( &opts, i, "--target", &option_arg ))
1855 parse_target_option( &opts, option_arg );
1856 raw_compiler_arg = raw_linker_arg = 0;
1858 else if (is_option( &opts, i, "--wine-objdir", &option_arg ))
1860 opts.wine_objdir = option_arg;
1861 raw_compiler_arg = raw_linker_arg = 0;
1863 else if (is_option( &opts, i, "--winebuild", &option_arg ))
1865 opts.winebuild = option_arg;
1866 raw_compiler_arg = raw_linker_arg = 0;
1868 else if (is_option( &opts, i, "--lib-suffix", &option_arg ))
1870 opts.lib_suffix = option_arg;
1871 raw_compiler_arg = raw_linker_arg = 0;
1873 break;
1876 /* put the arg into the appropriate bucket */
1877 if (raw_linker_arg)
1879 strarray_add( &opts.linker_args, opts.args.str[i] );
1880 if (next_is_arg && (i + 1 < opts.args.count))
1881 strarray_add( &opts.linker_args, opts.args.str[i + 1] );
1883 if (raw_compiler_arg)
1885 strarray_add( &opts.compiler_args, opts.args.str[i] );
1886 if (next_is_arg && (i + 1 < opts.args.count))
1887 strarray_add( &opts.compiler_args, opts.args.str[i + 1] );
1889 if (raw_winebuild_arg)
1891 strarray_add( &opts.winebuild_args, opts.args.str[i] );
1892 if (next_is_arg && (i + 1 < opts.args.count))
1893 strarray_add( &opts.winebuild_args, opts.args.str[i + 1] );
1896 /* skip the next token if it's an argument */
1897 if (next_is_arg) i++;
1899 else
1901 strarray_add( &opts.files, opts.args.str[i] );
1905 if (opts.force_pointer_size) set_target_ptr_size( &opts.target, opts.force_pointer_size );
1906 if (opts.processor == proc_cpp) linking = 0;
1907 if (linking == -1) error("Static linking is not supported\n");
1909 if (is_pe_target( &opts )) opts.use_msvcrt = 1;
1911 if (opts.files.count == 0 && !opts.fake_module) forward(&opts);
1912 else if (linking) build(&opts);
1913 else compile(&opts, lang);
1915 output_file_name = NULL;
1916 output_debug_file = NULL;
1917 output_implib = NULL;
1918 return 0;