po: Update Dutch translation.
[wine.git] / tools / winegcc / winegcc.c
blobab26adb07e83bfc004ff400ecce3f155c9b49dbd
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 <signal.h>
95 #include <stdarg.h>
96 #include <string.h>
97 #include <errno.h>
98 #include <ctype.h>
99 #include <limits.h>
100 #include <sys/types.h>
101 #ifdef HAVE_SYS_SYSCTL_H
102 # include <sys/sysctl.h>
103 #endif
105 #include "utils.h"
107 static const char* app_loader_template =
108 "#!/bin/sh\n"
109 "\n"
110 "appname=\"%s\"\n"
111 "# determine the application directory\n"
112 "appdir=''\n"
113 "case \"$0\" in\n"
114 " */*)\n"
115 " # $0 contains a path, use it\n"
116 " appdir=`dirname \"$0\"`\n"
117 " ;;\n"
118 " *)\n"
119 " # no directory in $0, search in PATH\n"
120 " saved_ifs=$IFS\n"
121 " IFS=:\n"
122 " for d in $PATH\n"
123 " do\n"
124 " IFS=$saved_ifs\n"
125 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
126 " done\n"
127 " ;;\n"
128 "esac\n"
129 "\n"
130 "# figure out the full app path\n"
131 "if [ -n \"$appdir\" ]; then\n"
132 " apppath=\"$appdir/$appname\"\n"
133 " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
134 " export WINEDLLPATH\n"
135 "else\n"
136 " apppath=\"$appname\"\n"
137 "fi\n"
138 "\n"
139 "# determine the WINELOADER\n"
140 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
141 "\n"
142 "# and try to start the app\n"
143 "exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
146 static const char *output_file_name;
147 static const char *output_debug_file;
148 static const char *output_implib;
149 static int keep_generated = 0;
150 static struct strarray tmp_files;
151 #ifdef HAVE_SIGSET_T
152 static sigset_t signal_mask;
153 #endif
155 static const char *bindir;
156 static const char *libdir;
157 static const char *includedir;
159 enum processor { proc_cc, proc_cxx, proc_cpp, proc_as };
161 struct options
163 enum processor processor;
164 struct target target;
165 const char *target_alias;
166 const char *version;
167 int shared;
168 int use_msvcrt;
169 int nostdinc;
170 int nostdlib;
171 int nostartfiles;
172 int nodefaultlibs;
173 int noshortwchar;
174 int data_only;
175 int unix_lib;
176 int gui_app;
177 int unicode_app;
178 int win16_app;
179 int compile_only;
180 int force_pointer_size;
181 int large_address_aware;
182 int wine_builtin;
183 int fake_module;
184 int unwind_tables;
185 int strip;
186 int pic;
187 const char* wine_objdir;
188 const char* winebuild;
189 const char* output_name;
190 const char* image_base;
191 const char* section_align;
192 const char* file_align;
193 const char* sysroot;
194 const char* isysroot;
195 const char* lib_suffix;
196 const char* subsystem;
197 const char* entry_point;
198 const char* prelink;
199 const char* debug_file;
200 const char* out_implib;
201 struct strarray prefix;
202 struct strarray lib_dirs;
203 struct strarray args;
204 struct strarray linker_args;
205 struct strarray compiler_args;
206 struct strarray winebuild_args;
207 struct strarray files;
208 struct strarray delayimports;
211 static void cleanup_output_files(void)
213 if (output_file_name) unlink( output_file_name );
214 if (output_debug_file) unlink( output_debug_file );
215 if (output_implib) unlink( output_implib );
218 static void clean_temp_files(void)
220 unsigned int i;
222 if (keep_generated) return;
224 for (i = 0; i < tmp_files.count; i++)
225 unlink(tmp_files.str[i]);
228 /* clean things up when aborting on a signal */
229 static void exit_on_signal( int sig )
231 exit(1); /* this will call the atexit functions */
234 static char* get_temp_file(const char* prefix, const char* suffix)
236 int fd;
237 char *tmp;
239 #ifdef HAVE_SIGPROCMASK
240 sigset_t old_set;
241 /* block signals while manipulating the temp files list */
242 sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
243 #endif
244 fd = make_temp_file( prefix, suffix, &tmp );
245 close( fd );
246 strarray_add(&tmp_files, tmp);
247 #ifdef HAVE_SIGPROCMASK
248 sigprocmask( SIG_SETMASK, &old_set, NULL );
249 #endif
250 return tmp;
253 static int is_pe_target( const struct options *opts )
255 switch (opts->target.platform)
257 case PLATFORM_MINGW:
258 case PLATFORM_CYGWIN:
259 case PLATFORM_WINDOWS:
260 return 1;
261 default:
262 return 0;
266 enum tool
268 TOOL_CC,
269 TOOL_CXX,
270 TOOL_CPP,
271 TOOL_LD,
272 TOOL_OBJCOPY,
275 static const struct
277 const char *base;
278 const char *llvm_base;
279 const char *deflt;
280 } tool_names[] =
282 { "gcc", "clang --driver-mode=gcc", CC },
283 { "g++", "clang --driver-mode=g++", CXX },
284 { "cpp", "clang --driver-mode=cpp", CPP },
285 { "ld", "ld.lld", LD },
286 { "objcopy", "llvm-objcopy" },
289 static struct strarray build_tool_name( struct options *opts, enum tool tool )
291 const char *base = tool_names[tool].base;
292 const char *llvm_base = tool_names[tool].llvm_base;
293 const char *deflt = tool_names[tool].deflt;
294 const char *path;
295 struct strarray ret = empty_strarray;
296 char* str;
298 if (opts->target_alias && opts->version)
300 str = strmake("%s-%s-%s", opts->target_alias, base, opts->version);
302 else if (opts->target_alias)
304 str = strmake("%s-%s", opts->target_alias, base);
306 else if (opts->version)
308 str = strmake("%s-%s", base, opts->version);
310 else
311 str = xstrdup((deflt && *deflt) ? deflt : base);
313 if ((path = find_binary( opts->prefix, str ))) return strarray_fromstring( path, " " );
315 if (!opts->version) str = xstrdup( llvm_base );
316 else str = strmake( "%s-%s", llvm_base, opts->version );
317 path = find_binary( opts->prefix, str );
318 if (!path)
320 error( "Could not find %s\n", base );
321 return ret;
324 ret = strarray_fromstring( path, " " );
325 if (!strncmp( llvm_base, "clang", 5 ))
327 if (opts->target_alias)
329 strarray_add( &ret, "-target" );
330 strarray_add( &ret, opts->target_alias );
332 strarray_add( &ret, "-Wno-unused-command-line-argument" );
333 strarray_add( &ret, "-fuse-ld=lld" );
335 return ret;
338 static struct strarray get_translator(struct options *opts)
340 enum tool tool;
342 switch(opts->processor)
344 case proc_cpp:
345 tool = TOOL_CPP;
346 break;
347 case proc_cc:
348 case proc_as:
349 tool = TOOL_CC;
350 break;
351 case proc_cxx:
352 tool = TOOL_CXX;
353 break;
354 default:
355 assert(0);
357 return build_tool_name( opts, tool );
360 static int try_link( struct strarray prefix, struct strarray link_tool, const char *cflags )
362 const char *in = get_temp_file( "try_link", ".c" );
363 const char *out = get_temp_file( "try_link", ".out" );
364 const char *err = get_temp_file( "try_link", ".err" );
365 struct strarray link = empty_strarray;
366 int sout = -1, serr = -1;
367 int ret;
369 create_file( in, 0644, "int main(void){return 1;}\n" );
371 strarray_addall( &link, link_tool );
372 strarray_add( &link, "-o" );
373 strarray_add( &link, out );
374 strarray_addall( &link, strarray_fromstring( cflags, " " ) );
375 strarray_add( &link, in );
377 sout = dup( fileno(stdout) );
378 freopen( err, "w", stdout );
379 serr = dup( fileno(stderr) );
380 freopen( err, "w", stderr );
381 ret = spawn( prefix, link, 1 );
382 if (sout >= 0)
384 dup2( sout, fileno(stdout) );
385 close( sout );
387 if (serr >= 0)
389 dup2( serr, fileno(stderr) );
390 close( serr );
392 return ret;
395 static struct strarray get_link_args( struct options *opts, const char *output_name )
397 struct strarray link_args = get_translator( opts );
398 struct strarray flags = empty_strarray;
400 strarray_addall( &link_args, opts->linker_args );
402 if (verbose > 1) strarray_add( &flags, "-v" );
404 switch (opts->target.platform)
406 case PLATFORM_APPLE:
407 strarray_add( &flags, opts->unix_lib ? "-dynamiclib" : "-bundle" );
408 strarray_add( &flags, "-multiply_defined" );
409 strarray_add( &flags, "suppress" );
410 if (opts->image_base)
412 strarray_add( &flags, "-image_base" );
413 strarray_add( &flags, opts->image_base );
415 if (opts->strip) strarray_add( &flags, "-Wl,-x" );
416 if (opts->unix_lib)
418 strarray_add( &flags, "-install_name" );
419 strarray_add( &flags, strmake( "@rpath/%s.so", output_name ) );
420 strarray_add( &flags, "-Wl,-rpath,@loader_path/" );
422 strarray_addall( &link_args, flags );
423 return link_args;
425 case PLATFORM_SOLARIS:
427 char *mapfile = get_temp_file( output_name, ".map" );
428 const char *align = opts->section_align ? opts->section_align : "0x1000";
430 create_file( mapfile, 0644, "text = A%s;\ndata = A%s;\n", align, align );
431 strarray_add( &flags, strmake("-Wl,-M,%s", mapfile) );
432 strarray_add( &tmp_files, mapfile );
434 break;
436 case PLATFORM_ANDROID:
437 /* the Android loader requires a soname for all libraries */
438 strarray_add( &flags, strmake( "-Wl,-soname,%s.so", output_name ));
439 break;
441 case PLATFORM_MINGW:
442 case PLATFORM_CYGWIN:
443 if (opts->shared || opts->win16_app)
445 strarray_add( &flags, "-shared" );
446 strarray_add( &flags, "-Wl,--kill-at" );
448 else strarray_add( &flags, opts->gui_app ? "-mwindows" : "-mconsole" );
450 if (opts->unicode_app) strarray_add( &flags, "-municode" );
451 if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
452 if (opts->nostartfiles || opts->use_msvcrt) strarray_add( &flags, "-nostartfiles" );
453 if (opts->subsystem) strarray_add( &flags, strmake("-Wl,--subsystem,%s", opts->subsystem ));
455 strarray_add( &flags, "-Wl,--nxcompat" );
457 if (opts->image_base) strarray_add( &flags, strmake("-Wl,--image-base,%s", opts->image_base ));
459 if (opts->large_address_aware && opts->target.cpu == CPU_i386)
460 strarray_add( &flags, "-Wl,--large-address-aware" );
462 /* make sure we don't need a libgcc_s dll on Windows */
463 if (!opts->nodefaultlibs && !opts->use_msvcrt)
464 strarray_add( &flags, "-static-libgcc" );
466 if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
467 strarray_add(&link_args, strmake("-Wl,-pdb,%s", opts->debug_file));
469 if (opts->out_implib)
470 strarray_add(&link_args, strmake("-Wl,--out-implib,%s", opts->out_implib));
472 if (!try_link( opts->prefix, link_args, "-Wl,--file-alignment,0x1000" ))
473 strarray_add( &link_args, strmake( "-Wl,--file-alignment,%s",
474 opts->file_align ? opts->file_align : "0x1000" ));
475 else if (!try_link( opts->prefix, link_args, "-Wl,-Xlink=-filealign:0x1000" ))
476 /* lld from llvm 10 does not support mingw style --file-alignment,
477 * but it's possible to use msvc syntax */
478 strarray_add( &link_args, strmake( "-Wl,-Xlink=-filealign:%s",
479 opts->file_align ? opts->file_align : "0x1000" ));
481 strarray_addall( &link_args, flags );
482 return link_args;
484 case PLATFORM_WINDOWS:
485 if (opts->shared || opts->win16_app)
487 strarray_add( &flags, "-shared" );
488 strarray_add( &flags, "-Wl,-kill-at" );
490 if (opts->unicode_app) strarray_add( &flags, "-municode" );
491 if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
492 if (opts->nostartfiles || opts->use_msvcrt) strarray_add( &flags, "-nostartfiles" );
493 if (opts->image_base) strarray_add( &flags, strmake("-Wl,-base:%s", opts->image_base ));
494 if (opts->subsystem)
495 strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->subsystem ));
496 else
497 strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->gui_app ? "windows" : "console" ));
499 if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
501 strarray_add(&link_args, "-Wl,-debug");
502 strarray_add(&link_args, strmake("-Wl,-pdb:%s", opts->debug_file));
504 else if (!opts->strip)
505 strarray_add(&link_args, "-Wl,-debug:dwarf");
507 if (opts->out_implib)
508 strarray_add(&link_args, strmake("-Wl,-implib:%s", opts->out_implib));
510 strarray_add( &link_args, strmake( "-Wl,-filealign:%s", opts->file_align ? opts->file_align : "0x1000" ));
512 strarray_addall( &link_args, flags );
513 return link_args;
515 default:
516 if (opts->image_base)
518 if (!try_link( opts->prefix, link_args, strmake("-Wl,-Ttext-segment=%s", opts->image_base)) )
519 strarray_add( &flags, strmake("-Wl,-Ttext-segment=%s", opts->image_base) );
520 else
521 opts->prelink = PRELINK;
523 if (!try_link( opts->prefix, link_args, "-Wl,-z,max-page-size=0x1000"))
524 strarray_add( &flags, "-Wl,-z,max-page-size=0x1000");
525 if (opts->unix_lib) strarray_add( &flags, strmake( "-Wl,-soname,%s.so", output_name ));
526 break;
529 /* generic Unix shared library flags */
531 strarray_add( &link_args, "-shared" );
532 strarray_add( &link_args, "-Wl,-Bsymbolic" );
533 if (!opts->noshortwchar && opts->target.cpu == CPU_ARM)
534 strarray_add( &flags, "-Wl,--no-wchar-size-warning" );
535 if (!try_link( opts->prefix, link_args, "-Wl,-z,defs" ))
536 strarray_add( &flags, "-Wl,-z,defs" );
538 strarray_addall( &link_args, flags );
539 return link_args;
542 static const char *get_multiarch_dir( struct target target )
544 switch (target.cpu)
546 case CPU_i386: return "/i386-linux-gnu";
547 case CPU_x86_64: return "/x86_64-linux-gnu";
548 case CPU_ARM: return "/arm-linux-gnueabi";
549 case CPU_ARM64: return "/aarch64-linux-gnu";
551 assert(0);
552 return NULL;
555 static char *get_lib_dir( struct options *opts )
557 const char *stdlibpath[] = { libdir, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
558 const char *bit_suffix, *other_bit_suffix, *build_multiarch, *target_multiarch, *winecrt0;
559 const char *root = opts->sysroot ? opts->sysroot : "";
560 unsigned int i;
561 struct stat st;
562 size_t build_len, target_len;
564 bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "64" : "32";
565 other_bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "32" : "64";
566 winecrt0 = strmake( "/wine%s/libwinecrt0.a", get_arch_dir( opts->target ));
567 build_multiarch = get_multiarch_dir( get_default_target() );
568 target_multiarch = get_multiarch_dir( opts->target );
569 build_len = strlen( build_multiarch );
570 target_len = strlen( target_multiarch );
572 for (i = 0; i < ARRAY_SIZE(stdlibpath); i++)
574 const char *root = (i && opts->sysroot) ? opts->sysroot : "";
575 char *p, *buffer;
577 if (!stdlibpath[i]) continue;
578 buffer = xmalloc( strlen(root) + strlen(stdlibpath[i]) +
579 strlen("/arm-linux-gnueabi") + strlen(winecrt0) + 1 );
580 strcpy( buffer, root );
581 strcat( buffer, stdlibpath[i] );
582 p = buffer + strlen(buffer);
583 while (p > buffer && p[-1] == '/') p--;
584 strcpy( p, winecrt0 );
585 if (!stat( buffer, &st )) goto found;
586 if (p > buffer + 2 && (!memcmp( p - 2, "32", 2 ) || !memcmp( p - 2, "64", 2 )))
588 p -= 2;
589 strcpy( p, winecrt0 );
590 if (!stat( buffer, &st )) goto found;
592 strcpy( p, bit_suffix );
593 strcat( p, winecrt0 );
594 if (!stat( buffer, &st )) goto found;
595 strcpy( p, target_multiarch );
596 strcat( p, winecrt0 );
597 if (!stat( buffer, &st )) goto found;
599 strcpy( buffer, root );
600 strcat( buffer, stdlibpath[i] );
601 p = buffer + strlen(buffer);
602 while (p > buffer && p[-1] == '/') p--;
603 strcpy( p, winecrt0 );
605 /* try to fixup each parent dirs named lib, lib32 or lib64 with target bitness suffix */
606 while (p > buffer)
608 p--;
609 while (p > buffer && *p != '/') p--;
610 if (*p != '/') break;
612 /* try s/$build_cpu/$target_cpu/ on multiarch */
613 if (get_default_target().cpu != opts->target.cpu &&
614 !memcmp( p, build_multiarch, build_len ) && p[build_len] == '/')
616 memmove( p + target_len, p + build_len, strlen( p + build_len ) + 1 );
617 memcpy( p, target_multiarch, target_len );
618 if (!stat( buffer, &st )) goto found;
619 memmove( p + build_len, p + target_len, strlen( p + target_len ) + 1 );
620 memcpy( p, build_multiarch, build_len );
623 if (memcmp( p + 1, "lib", 3 )) continue;
624 if (p[4] == '/')
626 memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
627 memcpy( p + 4, bit_suffix, 2 );
628 if (!stat( buffer, &st )) goto found;
629 memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
631 else if (!memcmp( p + 4, other_bit_suffix, 2 ) && p[6] == '/')
633 memcpy( p + 4, bit_suffix, 2 );
634 if (!stat( buffer, &st )) goto found;
635 memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
636 if (!stat( buffer, &st )) goto found;
637 memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
638 memcpy( p + 4, other_bit_suffix, 2 );
642 free( buffer );
643 continue;
645 found:
646 buffer[strlen(buffer) - strlen(winecrt0)] = 0;
647 return buffer;
649 return strmake( "%s%s", root, LIBDIR );
652 static void init_argv0_dir( const char *argv0 )
654 #ifndef _WIN32
655 char *dir;
657 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
658 dir = realpath( "/proc/self/exe", NULL );
659 #elif defined (__FreeBSD__) || defined(__DragonFly__)
660 static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
661 size_t path_size = PATH_MAX;
662 char *path = xmalloc( path_size );
663 if (!sysctl( pathname, sizeof(pathname)/sizeof(pathname[0]), path, &path_size, NULL, 0 ))
664 dir = realpath( path, NULL );
665 free( path );
666 #else
667 dir = realpath( argv0, NULL );
668 #endif
669 if (!dir) return;
670 bindir = get_dirname( dir );
671 includedir = strmake( "%s/%s", bindir, BIN_TO_INCLUDEDIR );
672 libdir = strmake( "%s/%s", bindir, BIN_TO_LIBDIR );
673 #endif
676 static void compile(struct options* opts, const char* lang)
678 struct strarray comp_args = get_translator(opts);
679 unsigned int i, j;
680 int gcc_defs = 0;
681 struct strarray gcc;
682 struct strarray gpp;
684 if (opts->force_pointer_size)
685 strarray_add( &comp_args, strmake("-m%u", 8 * opts->force_pointer_size ) );
686 switch(opts->processor)
688 case proc_cpp: gcc_defs = 1; break;
689 case proc_as: gcc_defs = 0; break;
690 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
691 /* mixing different C and C++ compilers isn't supported in configure anyway */
692 case proc_cc:
693 case proc_cxx:
694 gcc = build_tool_name(opts, TOOL_CC);
695 gpp = build_tool_name(opts, TOOL_CXX);
696 for ( j = 0; !gcc_defs && j < comp_args.count; j++ )
698 const char *cc = comp_args.str[j];
700 for (i = 0; !gcc_defs && i < gcc.count; i++)
701 gcc_defs = gcc.str[i][0] != '-' && strendswith(cc, gcc.str[i]);
702 for (i = 0; !gcc_defs && i < gpp.count; i++)
703 gcc_defs = gpp.str[i][0] != '-' && strendswith(cc, gpp.str[i]);
705 break;
708 if (opts->target.platform == PLATFORM_WINDOWS ||
709 opts->target.platform == PLATFORM_CYGWIN ||
710 opts->target.platform == PLATFORM_MINGW)
711 goto no_compat_defines;
713 if (opts->processor != proc_cpp)
715 if (gcc_defs && !opts->wine_objdir && !opts->noshortwchar)
717 strarray_add(&comp_args, "-fshort-wchar");
718 strarray_add(&comp_args, "-DWINE_UNICODE_NATIVE");
720 strarray_add(&comp_args, "-D_REENTRANT");
721 if (opts->pic)
722 strarray_add(&comp_args, "-fPIC");
723 else
724 strarray_add(&comp_args, "-fno-PIC");
727 if (get_target_ptr_size( opts->target ) == 8)
729 strarray_add(&comp_args, "-DWIN64");
730 strarray_add(&comp_args, "-D_WIN64");
731 strarray_add(&comp_args, "-D__WIN64");
732 strarray_add(&comp_args, "-D__WIN64__");
735 strarray_add(&comp_args, "-DWIN32");
736 strarray_add(&comp_args, "-D_WIN32");
737 strarray_add(&comp_args, "-D__WIN32");
738 strarray_add(&comp_args, "-D__WIN32__");
739 strarray_add(&comp_args, "-D__WINNT");
740 strarray_add(&comp_args, "-D__WINNT__");
742 if (gcc_defs)
744 switch (opts->target.cpu)
746 case CPU_x86_64:
747 case CPU_ARM64:
748 strarray_add(&comp_args, "-D__stdcall=__attribute__((ms_abi))");
749 strarray_add(&comp_args, "-D__cdecl=__stdcall");
750 strarray_add(&comp_args, "-D__fastcall=__stdcall");
751 break;
752 case CPU_i386:
753 strarray_add(&comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
754 strarray_add(&comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
755 strarray_add(&comp_args, "-D__fastcall=__attribute__((__fastcall__))");
756 break;
757 case CPU_ARM:
758 strarray_add(&comp_args, "-D__stdcall=__attribute__((pcs(\"aapcs-vfp\")))");
759 strarray_add(&comp_args, "-D__cdecl=__stdcall");
760 strarray_add(&comp_args, "-D__fastcall=__stdcall");
761 break;
763 strarray_add(&comp_args, "-D_stdcall=__stdcall");
764 strarray_add(&comp_args, "-D_cdecl=__cdecl");
765 strarray_add(&comp_args, "-D_fastcall=__fastcall");
766 strarray_add(&comp_args, "-D__declspec(x)=__declspec_##x");
767 strarray_add(&comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
768 strarray_add(&comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
769 strarray_add(&comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
770 strarray_add(&comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
771 strarray_add(&comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
772 strarray_add(&comp_args, "-D__declspec_naked=__attribute__((naked))");
773 strarray_add(&comp_args, "-D__declspec_noinline=__attribute__((noinline))");
774 strarray_add(&comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
775 strarray_add(&comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
776 strarray_add(&comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
777 strarray_add(&comp_args, "-D__declspec_selectany=__attribute__((weak))");
778 strarray_add(&comp_args, "-D__declspec_thread=__thread");
781 strarray_add(&comp_args, "-D__int8=char");
782 strarray_add(&comp_args, "-D__int16=short");
783 strarray_add(&comp_args, "-D__int32=int");
784 if (get_target_ptr_size( opts->target ) == 8)
785 strarray_add(&comp_args, "-D__int64=long");
786 else
787 strarray_add(&comp_args, "-D__int64=long long");
789 no_compat_defines:
790 strarray_add(&comp_args, "-D__WINE__");
792 /* options we handle explicitly */
793 if (opts->compile_only)
794 strarray_add(&comp_args, "-c");
795 if (opts->output_name)
797 strarray_add(&comp_args, "-o");
798 strarray_add(&comp_args, opts->output_name);
801 /* the rest of the pass-through parameters */
802 strarray_addall(&comp_args, opts->compiler_args);
804 /* the language option, if any */
805 if (lang && strcmp(lang, "-xnone"))
806 strarray_add(&comp_args, lang);
808 /* last, but not least, the files */
809 for ( j = 0; j < opts->files.count; j++ )
811 if (opts->files.str[j][0] != '-' || !opts->files.str[j][1]) /* not an option or bare '-' (i.e. stdin) */
812 strarray_add(&comp_args, opts->files.str[j]);
815 /* standard includes come last in the include search path */
816 if (!opts->wine_objdir && !opts->nostdinc)
818 const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
819 const char *root = opts->isysroot ? opts->isysroot : opts->sysroot ? opts->sysroot : "";
820 const char *isystem = gcc_defs ? "-isystem" : "-I";
821 const char *idirafter = gcc_defs ? "-idirafter" : "-I";
823 if (opts->use_msvcrt)
825 if (includedir) strarray_add( &comp_args, strmake( "%s%s/wine/msvcrt", isystem, includedir ));
826 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
828 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
829 strarray_add(&comp_args, strmake( "%s%s%s/wine/msvcrt", isystem, root, incl_dirs[j] ));
831 strarray_add(&comp_args, "-D__MSVCRT__");
833 if (includedir)
835 strarray_add( &comp_args, strmake( "%s%s/wine/windows", isystem, includedir ));
836 strarray_add( &comp_args, strmake( "%s%s", idirafter, includedir ));
838 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
840 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
841 strarray_add(&comp_args, strmake( "%s%s%s/wine/windows", isystem, root, incl_dirs[j] ));
842 strarray_add(&comp_args, strmake( "%s%s%s", idirafter, root, incl_dirs[j] ));
845 else if (opts->wine_objdir)
846 strarray_add(&comp_args, strmake("-I%s/include", opts->wine_objdir) );
848 spawn(opts->prefix, comp_args, 0);
851 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
853 struct options copts;
855 /* make a copy so we don't change any of the initial stuff */
856 /* a shallow copy is exactly what we want in this case */
857 copts = *opts;
858 copts.output_name = get_temp_file(get_basename_noext(file), ".o");
859 copts.compile_only = 1;
860 copts.files = empty_strarray;
861 strarray_add(&copts.files, file);
862 compile(&copts, lang);
863 return copts.output_name;
866 /* return the initial set of options needed to run winebuild */
867 static struct strarray get_winebuild_args(struct options *opts)
869 const char* winebuild = getenv("WINEBUILD");
870 const char *binary = NULL;
871 struct strarray spec_args = empty_strarray;
872 unsigned int i;
874 if (opts->winebuild)
875 binary = opts->winebuild;
876 else if (opts->wine_objdir)
877 binary = strmake( "%s/tools/winebuild/winebuild%s", opts->wine_objdir, EXEEXT );
878 else if (winebuild)
879 binary = find_binary( opts->prefix, winebuild );
880 else if (bindir)
881 binary = strmake( "%s/winebuild%s", bindir, EXEEXT );
882 else
883 binary = find_binary( opts->prefix, "winebuild" );
884 if (!binary) error( "Could not find winebuild\n" );
885 strarray_add( &spec_args, binary );
886 if (verbose) strarray_add( &spec_args, "-v" );
887 if (keep_generated) strarray_add( &spec_args, "--save-temps" );
888 if (opts->target_alias)
890 strarray_add( &spec_args, "--target" );
891 strarray_add( &spec_args, opts->target_alias );
893 if (opts->force_pointer_size)
894 strarray_add(&spec_args, strmake("-m%u", 8 * opts->force_pointer_size ));
895 for (i = 0; i < opts->prefix.count; i++)
896 strarray_add( &spec_args, strmake( "-B%s", opts->prefix.str[i] ));
897 strarray_addall( &spec_args, opts->winebuild_args );
898 if (opts->unwind_tables) strarray_add( &spec_args, "-fasynchronous-unwind-tables" );
899 else strarray_add( &spec_args, "-fno-asynchronous-unwind-tables" );
900 return spec_args;
903 static void fixup_constructors( struct options *opts, const char *file )
905 struct strarray args = get_winebuild_args( opts );
907 strarray_add( &args, "--fixup-ctors" );
908 strarray_add( &args, file );
909 spawn( opts->prefix, args, 0 );
912 static void make_wine_builtin( struct options *opts, const char *file )
914 struct strarray args = get_winebuild_args( opts );
916 strarray_add( &args, "--builtin" );
917 strarray_add( &args, file );
918 spawn( opts->prefix, args, 0 );
921 /* check if there is a static lib associated to a given dll */
922 static char *find_static_lib( const char *dll )
924 char *lib = strmake("%s.a", dll);
925 if (get_file_type(lib) == file_arh) return lib;
926 free( lib );
927 return NULL;
930 static const char *find_libgcc(struct strarray prefix, struct strarray link_tool)
932 const char *out = get_temp_file( "find_libgcc", ".out" );
933 const char *err = get_temp_file( "find_libgcc", ".err" );
934 struct strarray link = empty_strarray;
935 int sout = -1, serr = -1;
936 char *libgcc, *p;
937 struct stat st;
938 size_t cnt;
939 int ret;
941 strarray_addall( &link, link_tool );
942 strarray_add( &link, "-print-libgcc-file-name" );
944 sout = dup( fileno(stdout) );
945 freopen( out, "w", stdout );
946 serr = dup( fileno(stderr) );
947 freopen( err, "w", stderr );
948 ret = spawn( prefix, link, 1 );
949 if (sout >= 0)
951 dup2( sout, fileno(stdout) );
952 close( sout );
954 if (serr >= 0)
956 dup2( serr, fileno(stderr) );
957 close( serr );
960 if (ret || stat(out, &st) || !st.st_size) return NULL;
962 libgcc = xmalloc(st.st_size + 1);
963 sout = open(out, O_RDONLY);
964 if (sout == -1) return NULL;
965 cnt = read(sout, libgcc, st.st_size);
966 close(sout);
967 libgcc[cnt] = 0;
968 if ((p = strchr(libgcc, '\n'))) *p = 0;
969 return libgcc;
973 /* add specified library to the list of files */
974 static void add_library( struct options *opts, struct strarray lib_dirs,
975 struct strarray *files, const char *library )
977 char *static_lib, *fullname = 0;
979 switch(get_lib_type(opts->target, lib_dirs, library, "lib", opts->lib_suffix, &fullname))
981 case file_arh:
982 strarray_add(files, strmake("-a%s", fullname));
983 break;
984 case file_dll:
985 strarray_add(files, strmake("-d%s", fullname));
986 if ((static_lib = find_static_lib(fullname)))
988 strarray_add(files, strmake("-a%s",static_lib));
989 free(static_lib);
991 break;
992 case file_so:
993 default:
994 /* keep it anyway, the linker may know what to do with it */
995 strarray_add(files, strmake("-l%s", library));
996 break;
998 free(fullname);
1001 /* run winebuild to generate the .spec.o file */
1002 static const char *build_spec_obj( struct options *opts, const char *spec_file, const char *output_file,
1003 struct strarray files, struct strarray lib_dirs, const char *entry_point )
1005 unsigned int i;
1006 int is_pe = is_pe_target( opts );
1007 struct strarray spec_args = get_winebuild_args( opts );
1008 struct strarray tool;
1009 const char *spec_o_name, *output_name;
1011 /* get the filename from the path */
1012 output_name = get_basename( output_file );
1014 tool = build_tool_name( opts, TOOL_CC );
1015 strarray_add( &spec_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
1016 if (!is_pe)
1018 tool = build_tool_name( opts, TOOL_LD );
1019 strarray_add( &spec_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
1022 spec_o_name = get_temp_file(output_name, ".spec.o");
1023 if (opts->pic && !is_pe) strarray_add(&spec_args, "-fPIC");
1024 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
1025 if (opts->fake_module)
1027 strarray_add(&spec_args, "--fake-module");
1028 strarray_add(&spec_args, "-o");
1029 strarray_add(&spec_args, output_file);
1031 else
1033 strarray_add(&spec_args, "-o");
1034 strarray_add(&spec_args, spec_o_name);
1036 if (spec_file)
1038 strarray_add(&spec_args, "-E");
1039 strarray_add(&spec_args, spec_file);
1042 if (!opts->shared)
1044 strarray_add(&spec_args, "-F");
1045 strarray_add(&spec_args, output_name);
1046 strarray_add(&spec_args, "--subsystem");
1047 strarray_add(&spec_args, opts->gui_app ? "windows" : "console");
1048 if (opts->large_address_aware) strarray_add( &spec_args, "--large-address-aware" );
1051 if (opts->target.platform == PLATFORM_WINDOWS) strarray_add(&spec_args, "--safeseh");
1053 if (entry_point)
1055 strarray_add(&spec_args, "--entry");
1056 strarray_add(&spec_args, entry_point);
1059 if (opts->subsystem)
1061 strarray_add(&spec_args, "--subsystem");
1062 strarray_add(&spec_args, opts->subsystem);
1065 for (i = 0; i < lib_dirs.count; i++)
1066 strarray_add(&spec_args, strmake("-L%s", lib_dirs.str[i]));
1068 if (!is_pe)
1070 for (i = 0; i < opts->delayimports.count; i++)
1071 strarray_add(&spec_args, strmake("-d%s", opts->delayimports.str[i]));
1074 /* add resource files */
1075 for (i = 0; i < files.count; i++)
1076 if (files.str[i][1] == 'r') strarray_add(&spec_args, files.str[i]);
1078 /* add other files */
1079 strarray_add(&spec_args, "--");
1080 for (i = 0; i < files.count; i++)
1082 switch(files.str[i][1])
1084 case 'd':
1085 case 'a':
1086 case 'o':
1087 strarray_add(&spec_args, files.str[i] + 2);
1088 break;
1092 spawn(opts->prefix, spec_args, 0);
1093 return spec_o_name;
1096 /* run winebuild to generate a data-only library */
1097 static void build_data_lib( struct options *opts, const char *spec_file, const char *output_file, struct strarray files )
1099 unsigned int i;
1100 struct strarray spec_args = get_winebuild_args( opts );
1102 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
1103 strarray_add(&spec_args, "-o");
1104 strarray_add(&spec_args, output_file);
1105 if (spec_file)
1107 strarray_add(&spec_args, "-E");
1108 strarray_add(&spec_args, spec_file);
1111 /* add resource files */
1112 for (i = 0; i < files.count; i++)
1113 if (files.str[i][1] == 'r') strarray_add(&spec_args, files.str[i]);
1115 spawn(opts->prefix, spec_args, 0);
1118 static void build(struct options* opts)
1120 struct strarray lib_dirs = empty_strarray;
1121 struct strarray files = empty_strarray;
1122 struct strarray link_args;
1123 char *output_file, *output_path;
1124 const char *spec_o_name = NULL, *libgcc = NULL;
1125 const char *output_name, *spec_file, *lang;
1126 int generate_app_loader = 1;
1127 const char *crt_lib = NULL, *entry_point = NULL;
1128 int is_pe = is_pe_target( opts );
1129 unsigned int j;
1131 /* NOTE: for the files array we'll use the following convention:
1132 * -axxx: xxx is an archive (.a)
1133 * -dxxx: xxx is a DLL (.def)
1134 * -lxxx: xxx is an unsorted library
1135 * -oxxx: xxx is an object (.o)
1136 * -rxxx: xxx is a resource (.res)
1137 * -sxxx: xxx is a shared lib (.so)
1138 * -xlll: lll is the language (c, c++, etc.)
1141 output_file = xstrdup( opts->output_name ? opts->output_name : "a.out" );
1143 /* 'winegcc -o app xxx.exe.so' only creates the load script */
1144 if (opts->files.count == 1 && strendswith(opts->files.str[0], ".exe.so"))
1146 create_file(output_file, 0755, app_loader_template, opts->files.str[0]);
1147 return;
1150 /* generate app loader only for .exe */
1151 if (opts->shared || is_pe || strendswith(output_file, ".so"))
1152 generate_app_loader = 0;
1154 if (strendswith(output_file, ".fake")) opts->fake_module = 1;
1156 /* normalize the filename a bit: strip .so, ensure it has proper ext */
1157 if (!strchr(get_basename( output_file ), '.'))
1158 output_file = strmake("%s.%s", output_file, opts->shared ? "dll" : "exe");
1159 else if (strendswith(output_file, ".so"))
1160 output_file[strlen(output_file) - 3] = 0;
1161 output_path = is_pe ? output_file : strmake( "%s.so", output_file );
1163 /* get the filename from the path */
1164 output_name = get_basename( output_file );
1166 /* prepare the linking path */
1167 if (!opts->wine_objdir)
1169 char *lib_dir = get_lib_dir( opts );
1170 strarray_addall( &lib_dirs, opts->lib_dirs );
1171 strarray_add( &lib_dirs, strmake( "%s/wine%s", lib_dir, get_arch_dir( opts->target )));
1172 strarray_add( &lib_dirs, lib_dir );
1174 else
1176 strarray_add(&lib_dirs, strmake("%s/dlls", opts->wine_objdir));
1177 strarray_addall(&lib_dirs, opts->lib_dirs);
1180 /* mark the files with their appropriate type */
1181 spec_file = lang = 0;
1182 for ( j = 0; j < opts->files.count; j++ )
1184 const char* file = opts->files.str[j];
1185 if (file[0] != '-')
1187 switch(get_file_type(file))
1189 case file_def:
1190 case file_spec:
1191 if (spec_file)
1192 error("Only one spec file can be specified\n");
1193 spec_file = file;
1194 break;
1195 case file_rc:
1196 /* FIXME: invoke wrc to build it */
1197 error("Can't compile .rc file at the moment: %s\n", file);
1198 break;
1199 case file_res:
1200 strarray_add(&files, strmake("-r%s", file));
1201 break;
1202 case file_obj:
1203 strarray_add(&files, strmake("-o%s", file));
1204 break;
1205 case file_arh:
1206 if (opts->use_msvcrt)
1208 char *p = get_basename( file );
1209 if (!strncmp(p, "libmsvcr", 8) || !strncmp(p, "libucrt", 7)) crt_lib = file;
1211 strarray_add(&files, strmake("-a%s", file));
1212 break;
1213 case file_so:
1214 strarray_add(&files, strmake("-s%s", file));
1215 break;
1216 case file_na:
1217 error("File does not exist: %s\n", file);
1218 break;
1219 default:
1220 file = compile_to_object(opts, file, lang);
1221 strarray_add(&files, strmake("-o%s", file));
1222 break;
1225 else if (file[1] == 'l')
1226 add_library(opts, lib_dirs, &files, file + 2 );
1227 else if (file[1] == 'x')
1228 lang = file;
1229 else if(file[1] == 'W')
1230 strarray_add(&files, file);
1233 /* add the default libraries, if needed */
1235 if (!opts->wine_objdir && !opts->nodefaultlibs)
1237 if (opts->gui_app)
1239 add_library(opts, lib_dirs, &files, "shell32");
1240 add_library(opts, lib_dirs, &files, "comdlg32");
1241 add_library(opts, lib_dirs, &files, "gdi32");
1243 add_library(opts, lib_dirs, &files, "advapi32");
1244 add_library(opts, lib_dirs, &files, "user32");
1245 add_library(opts, lib_dirs, &files, "winecrt0");
1246 if (opts->use_msvcrt)
1248 if (!crt_lib)
1250 if (strncmp( output_name, "msvcr", 5 ) &&
1251 strncmp( output_name, "ucrt", 4 ) &&
1252 strcmp( output_name, "crtdll.dll" ))
1253 add_library(opts, lib_dirs, &files, "ucrtbase");
1255 else strarray_add(&files, strmake("-a%s", crt_lib));
1257 if (opts->win16_app) add_library(opts, lib_dirs, &files, "kernel");
1258 add_library(opts, lib_dirs, &files, "kernel32");
1259 add_library(opts, lib_dirs, &files, "ntdll");
1262 /* set default entry point, if needed */
1263 if (!opts->entry_point)
1265 if (opts->subsystem && !opts->unix_lib && !strcmp( opts->subsystem, "native" ))
1266 entry_point = (is_pe && opts->target.cpu == CPU_i386) ? "DriverEntry@8" : "DriverEntry";
1267 else if (opts->use_msvcrt && !opts->shared && !opts->win16_app)
1268 entry_point = opts->unicode_app ? "wmainCRTStartup" : "mainCRTStartup";
1270 else entry_point = opts->entry_point;
1272 /* run winebuild to generate the .spec.o file */
1273 if (opts->data_only)
1275 build_data_lib( opts, spec_file, output_file, files );
1276 return;
1278 if (spec_file || !opts->unix_lib)
1279 spec_o_name = build_spec_obj( opts, spec_file, output_file, files, lib_dirs, entry_point );
1281 if (opts->fake_module) return; /* nothing else to do */
1283 /* link everything together now */
1284 link_args = get_link_args( opts, output_name );
1286 if (opts->nodefaultlibs || opts->use_msvcrt)
1288 switch (opts->target.platform)
1290 case PLATFORM_MINGW:
1291 case PLATFORM_CYGWIN:
1292 libgcc = find_libgcc( opts->prefix, link_args );
1293 if (!libgcc) libgcc = "-lgcc";
1294 break;
1295 default:
1296 break;
1300 strarray_add(&link_args, "-o");
1301 strarray_add(&link_args, output_path);
1303 for ( j = 0; j < lib_dirs.count; j++ )
1304 strarray_add(&link_args, strmake("-L%s", lib_dirs.str[j]));
1306 if (is_pe && opts->use_msvcrt && !entry_point && (opts->shared || opts->win16_app))
1307 entry_point = opts->target.cpu == CPU_i386 ? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1309 if (is_pe && entry_point)
1311 if (opts->target.platform == PLATFORM_WINDOWS)
1312 strarray_add(&link_args, strmake("-Wl,-entry:%s", entry_point));
1313 else
1314 strarray_add(&link_args, strmake("-Wl,--entry,%s%s",
1315 is_pe && opts->target.cpu == CPU_i386 ? "_" : "",
1316 entry_point));
1319 if (spec_o_name) strarray_add(&link_args, spec_o_name);
1321 if (is_pe)
1323 for (j = 0; j < opts->delayimports.count; j++)
1325 if (opts->target.platform == PLATFORM_WINDOWS)
1326 strarray_add(&link_args, strmake("-Wl,-delayload:%s", opts->delayimports.str[j]));
1327 else
1328 strarray_add(&link_args, strmake("-Wl,-delayload,%s",opts->delayimports.str[j]));
1332 for ( j = 0; j < files.count; j++ )
1334 const char* name = files.str[j] + 2;
1335 switch(files.str[j][1])
1337 case 'l':
1338 strarray_add(&link_args, strmake("-l%s", name));
1339 break;
1340 case 's':
1341 case 'o':
1342 strarray_add(&link_args, name);
1343 break;
1344 case 'a':
1345 if (!opts->use_msvcrt && !opts->lib_suffix && strchr(name, '/'))
1347 /* turn the path back into -Ldir -lfoo options
1348 * this makes sure that we use the specified libs even
1349 * when mingw adds its own import libs to the link */
1350 const char *p = get_basename( name );
1352 if (is_pe)
1354 if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
1356 strarray_add(&link_args, strmake("-L%s", get_dirname(name) ));
1357 strarray_add(&link_args, strmake("-l%s", get_basename_noext( p + 3 )));
1358 break;
1361 else
1363 /* don't link to ntdll or ntoskrnl in non-msvcrt mode
1364 * since they export CRT functions */
1365 if (!strcmp( p, "libntdll.a" )) break;
1366 if (!strcmp( p, "libntoskrnl.a" )) break;
1369 strarray_add(&link_args, name);
1370 break;
1371 case 'W':
1372 strarray_add(&link_args, files.str[j]);
1373 break;
1377 if (!opts->nostdlib && !is_pe)
1379 strarray_add(&link_args, "-lm");
1380 strarray_add(&link_args, "-lc");
1383 if (libgcc) strarray_add(&link_args, libgcc);
1385 output_file_name = output_path;
1386 output_debug_file = opts->debug_file;
1387 output_implib = opts->out_implib;
1388 atexit( cleanup_output_files );
1390 spawn(opts->prefix, link_args, 0);
1392 if (opts->debug_file && !strendswith(opts->debug_file, ".pdb"))
1394 struct strarray tool, objcopy = build_tool_name(opts, TOOL_OBJCOPY);
1396 tool = empty_strarray;
1397 strarray_addall( &tool, objcopy );
1398 strarray_add(&tool, "--only-keep-debug");
1399 strarray_add(&tool, output_path);
1400 strarray_add(&tool, opts->debug_file);
1401 spawn(opts->prefix, tool, 1);
1403 tool = empty_strarray;
1404 strarray_addall( &tool, objcopy );
1405 strarray_add(&tool, "--strip-debug");
1406 strarray_add(&tool, output_path);
1407 spawn(opts->prefix, tool, 1);
1409 tool = empty_strarray;
1410 strarray_addall( &tool, objcopy );
1411 strarray_add(&tool, "--add-gnu-debuglink");
1412 strarray_add(&tool, opts->debug_file);
1413 strarray_add(&tool, output_path);
1414 spawn(opts->prefix, tool, 0);
1417 if (opts->unix_lib) return;
1419 if (opts->out_implib && !is_pe)
1421 struct strarray tool, implib_args;
1423 if (!spec_file)
1424 error("--out-implib requires a .spec or .def file\n");
1426 implib_args = get_winebuild_args( opts );
1427 tool = build_tool_name( opts, TOOL_CC );
1428 strarray_add( &implib_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
1429 tool = build_tool_name( opts, TOOL_LD );
1430 strarray_add( &implib_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
1432 strarray_add(&implib_args, "--implib");
1433 strarray_add(&implib_args, "-o");
1434 strarray_add(&implib_args, opts->out_implib);
1435 strarray_add(&implib_args, "--export");
1436 strarray_add(&implib_args, spec_file);
1438 spawn(opts->prefix, implib_args, 0);
1441 /* set the base address with prelink if linker support is not present */
1442 if (opts->prelink && !opts->target_alias)
1444 if (opts->prelink[0] && strcmp(opts->prelink,"false"))
1446 struct strarray prelink_args = empty_strarray;
1447 strarray_add(&prelink_args, opts->prelink);
1448 strarray_add(&prelink_args, "--reloc-only");
1449 strarray_add(&prelink_args, opts->image_base);
1450 strarray_add(&prelink_args, output_path);
1451 spawn(opts->prefix, prelink_args, 1);
1455 if (!is_pe) fixup_constructors( opts, output_path );
1456 else if (opts->wine_builtin) make_wine_builtin( opts, output_path );
1458 /* create the loader script */
1459 if (generate_app_loader)
1460 create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
1464 static void forward( struct options *opts )
1466 struct strarray args = get_translator(opts);
1468 strarray_addall(&args, opts->compiler_args);
1469 strarray_addall(&args, opts->linker_args);
1470 spawn(opts->prefix, args, 0);
1473 static int is_linker_arg(const char* arg)
1475 static const char* link_switches[] =
1477 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1478 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1479 "-fprofile-generate", "-fprofile-use"
1481 unsigned int j;
1483 switch (arg[1])
1485 case 'R':
1486 case 'z':
1487 case 'u':
1488 return 1;
1489 case 'W':
1490 if (strncmp("-Wl,", arg, 4) == 0) return 1;
1491 break;
1492 case 'X':
1493 if (strcmp("-Xlinker", arg) == 0) return 1;
1494 break;
1495 case 'a':
1496 if (strcmp("-arch", arg) == 0) return 1;
1497 break;
1498 case 'f':
1499 if (strncmp("-fuse-ld=", arg, 9) == 0) return 1;
1500 break;
1501 case 'r':
1502 if (strncmp("-rtlib=", arg, 7) == 0) return 1;
1503 break;
1506 for (j = 0; j < ARRAY_SIZE(link_switches); j++)
1507 if (strcmp(link_switches[j], arg) == 0) return 1;
1509 return 0;
1512 static void parse_target_option( struct options *opts, const char *target )
1514 opts->target_alias = xstrdup( target );
1515 if (!parse_target( target, &opts->target )) error( "Invalid target specification '%s'\n", target );
1518 static int is_option( struct options *opts, int i, const char *option, const char **option_arg )
1520 if (!strcmp( opts->args.str[i], option ))
1522 if (opts->args.count == i) error( "option %s requires an argument\n", opts->args.str[i] );
1523 *option_arg = opts->args.str[i + 1];
1524 return 1;
1526 if (!strncmp( opts->args.str[i], option, strlen(option) ) && opts->args.str[i][strlen(option)] == '=')
1528 *option_arg = opts->args.str[i] + strlen(option) + 1;
1529 return 1;
1531 return 0;
1534 int main(int argc, char **argv)
1536 int i, c, next_is_arg = 0, linking = 1;
1537 int raw_compiler_arg, raw_linker_arg, raw_winebuild_arg;
1538 const char* option_arg;
1539 struct options opts;
1540 char* lang = 0;
1541 char* str;
1543 #ifdef SIGHUP
1544 signal( SIGHUP, exit_on_signal );
1545 #endif
1546 signal( SIGTERM, exit_on_signal );
1547 signal( SIGINT, exit_on_signal );
1548 #ifdef HAVE_SIGADDSET
1549 sigemptyset( &signal_mask );
1550 sigaddset( &signal_mask, SIGHUP );
1551 sigaddset( &signal_mask, SIGTERM );
1552 sigaddset( &signal_mask, SIGINT );
1553 #endif
1554 init_argv0_dir( argv[0] );
1556 /* setup tmp file removal at exit */
1557 atexit(clean_temp_files);
1559 /* initialize options */
1560 memset(&opts, 0, sizeof(opts));
1561 opts.target = init_argv0_target( argv[0] );
1562 opts.pic = 1;
1564 /* determine the processor type */
1565 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
1566 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
1568 for (i = 1; i < argc; i++)
1570 char *input_buffer = NULL, *iter, *opt, *out;
1571 struct stat st;
1572 int fd;
1574 if (argv[i][0] != '@' || (fd = open( argv[i] + 1, O_RDONLY | O_BINARY )) == -1)
1576 strarray_add( &opts.args, argv[i] );
1577 continue;
1579 if ((fstat( fd, &st ) == -1)) error( "Cannot stat %s\n", argv[i] + 1 );
1580 if (st.st_size)
1582 input_buffer = xmalloc( st.st_size + 1 );
1583 if (read( fd, input_buffer, st.st_size ) != st.st_size) error( "Cannot read %s\n", argv[i] + 1 );
1585 close( fd );
1586 for (iter = input_buffer; iter < input_buffer + st.st_size; iter++)
1588 char quote = 0;
1589 while (iter < input_buffer + st.st_size && isspace(*iter)) iter++;
1590 if (iter == input_buffer + st.st_size) break;
1591 opt = out = iter;
1592 while (iter < input_buffer + st.st_size && (quote || !isspace(*iter)))
1594 if (*iter == quote)
1596 iter++;
1597 quote = 0;
1599 else if (*iter == '\'' || *iter == '"') quote = *iter++;
1600 else
1602 if (*iter == '\\' && iter + 1 < input_buffer + st.st_size) iter++;
1603 *out++ = *iter++;
1606 *out = 0;
1607 strarray_add( &opts.args, opt );
1611 /* parse options */
1612 for (i = 0; i < opts.args.count; i++)
1614 if (opts.args.str[i][0] == '-' && opts.args.str[i][1]) /* option, except '-' alone is stdin, which is a file */
1616 /* determine if this switch is followed by a separate argument */
1617 next_is_arg = 0;
1618 option_arg = 0;
1619 switch(opts.args.str[i][1])
1621 case 'x': case 'o': case 'D': case 'U':
1622 case 'I': case 'A': case 'l': case 'u':
1623 case 'b': case 'V': case 'G': case 'L':
1624 case 'B': case 'R': case 'z':
1625 if (opts.args.str[i][2]) option_arg = &opts.args.str[i][2];
1626 else next_is_arg = 1;
1627 break;
1628 case 'i':
1629 next_is_arg = 1;
1630 break;
1631 case 'a':
1632 if (strcmp("-aux-info", opts.args.str[i]) == 0)
1633 next_is_arg = 1;
1634 if (strcmp("-arch", opts.args.str[i]) == 0)
1635 next_is_arg = 1;
1636 break;
1637 case 'X':
1638 if (strcmp("-Xlinker", opts.args.str[i]) == 0)
1639 next_is_arg = 1;
1640 break;
1641 case 'M':
1642 c = opts.args.str[i][2];
1643 if (c == 'F' || c == 'T' || c == 'Q')
1645 if (opts.args.str[i][3]) option_arg = &opts.args.str[i][3];
1646 else next_is_arg = 1;
1648 break;
1649 case 'f':
1650 if (strcmp("-framework", opts.args.str[i]) == 0)
1651 next_is_arg = 1;
1652 break;
1653 case 't':
1654 next_is_arg = strcmp("-target", opts.args.str[i]) == 0;
1655 break;
1656 case '-':
1657 next_is_arg = (strcmp("--param", opts.args.str[i]) == 0 ||
1658 strcmp("--sysroot", opts.args.str[i]) == 0 ||
1659 strcmp("--target", opts.args.str[i]) == 0 ||
1660 strcmp("--wine-objdir", opts.args.str[i]) == 0 ||
1661 strcmp("--winebuild", opts.args.str[i]) == 0 ||
1662 strcmp("--lib-suffix", opts.args.str[i]) == 0);
1663 break;
1665 if (next_is_arg)
1667 if (i + 1 >= opts.args.count) error("option -%c requires an argument\n", opts.args.str[i][1]);
1668 option_arg = opts.args.str[i+1];
1671 /* determine what options go 'as is' to the linker & the compiler */
1672 raw_linker_arg = is_linker_arg(opts.args.str[i]);
1673 raw_compiler_arg = !raw_linker_arg;
1674 raw_winebuild_arg = 0;
1676 /* do a bit of semantic analysis */
1677 switch (opts.args.str[i][1])
1679 case 'B':
1680 str = xstrdup(option_arg);
1681 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
1682 strarray_add(&opts.prefix, str);
1683 raw_linker_arg = 1;
1684 break;
1685 case 'b':
1686 parse_target_option( &opts, option_arg );
1687 raw_compiler_arg = 0;
1688 break;
1689 case 'V':
1690 opts.version = xstrdup( option_arg );
1691 raw_compiler_arg = 0;
1692 break;
1693 case 'c': /* compile or assemble */
1694 raw_compiler_arg = 0;
1695 if (opts.args.str[i][2] == 0) opts.compile_only = 1;
1696 /* fall through */
1697 case 'S': /* generate assembler code */
1698 case 'E': /* preprocess only */
1699 if (opts.args.str[i][2] == 0) linking = 0;
1700 break;
1701 case 'f':
1702 if (strcmp("-fno-short-wchar", opts.args.str[i]) == 0)
1703 opts.noshortwchar = 1;
1704 else if (!strcmp("-fasynchronous-unwind-tables", opts.args.str[i]))
1705 opts.unwind_tables = 1;
1706 else if (!strcmp("-fno-asynchronous-unwind-tables", opts.args.str[i]))
1707 opts.unwind_tables = 0;
1708 else if (!strcmp("-fPIC", opts.args.str[i]) || !strcmp("-fpic", opts.args.str[i]))
1709 opts.pic = 1;
1710 else if (!strcmp("-fno-PIC", opts.args.str[i]) || !strcmp("-fno-pic", opts.args.str[i]))
1711 opts.pic = 0;
1712 break;
1713 case 'i':
1714 if (!strcmp( "-isysroot", opts.args.str[i] )) opts.isysroot = opts.args.str[i + 1];
1715 break;
1716 case 'l':
1717 strarray_add(&opts.files, strmake("-l%s", option_arg));
1718 raw_compiler_arg = 0;
1719 break;
1720 case 'L':
1721 strarray_add(&opts.lib_dirs, option_arg);
1722 raw_compiler_arg = 0;
1723 break;
1724 case 'M': /* map file generation */
1725 linking = 0;
1726 break;
1727 case 'm':
1728 if (strcmp("-mno-cygwin", opts.args.str[i]) == 0)
1730 opts.use_msvcrt = 1;
1731 raw_compiler_arg = 0;
1732 raw_winebuild_arg = 1;
1734 if (strcmp("-mcygwin", opts.args.str[i]) == 0)
1736 opts.use_msvcrt = 0;
1737 raw_compiler_arg = 0;
1739 else if (strcmp("-mwindows", opts.args.str[i]) == 0)
1741 opts.gui_app = 1;
1742 raw_compiler_arg = 0;
1744 else if (strcmp("-mconsole", opts.args.str[i]) == 0)
1746 opts.gui_app = 0;
1747 raw_compiler_arg = 0;
1749 else if (strcmp("-municode", opts.args.str[i]) == 0)
1751 opts.unicode_app = 1;
1752 raw_compiler_arg = 0;
1753 raw_winebuild_arg = 1;
1755 else if (strcmp("-mthreads", opts.args.str[i]) == 0)
1757 raw_compiler_arg = 0;
1759 else if (strcmp("-munix", opts.args.str[i]) == 0)
1761 opts.unix_lib = 1;
1762 raw_compiler_arg = 0;
1763 raw_winebuild_arg = 1;
1765 else if (strcmp("-m16", opts.args.str[i]) == 0)
1767 opts.win16_app = 1;
1768 raw_compiler_arg = 0;
1769 raw_winebuild_arg = 1;
1771 else if (strcmp("-m32", opts.args.str[i]) == 0)
1773 set_target_ptr_size( &opts.target, 4 );
1774 opts.force_pointer_size = 4;
1775 raw_linker_arg = 1;
1777 else if (strcmp("-m64", opts.args.str[i]) == 0)
1779 set_target_ptr_size( &opts.target, 8 );
1780 opts.force_pointer_size = 8;
1781 raw_linker_arg = 1;
1783 else if (!strcmp("-marm", opts.args.str[i] ) || !strcmp("-mthumb", opts.args.str[i] ))
1785 raw_linker_arg = 1;
1786 raw_winebuild_arg = 1;
1788 else if (!strncmp("-mcpu=", opts.args.str[i], 6) ||
1789 !strncmp("-mfpu=", opts.args.str[i], 6) ||
1790 !strncmp("-march=", opts.args.str[i], 7) ||
1791 !strncmp("-mfloat-abi=", opts.args.str[i], 12))
1792 raw_winebuild_arg = 1;
1793 break;
1794 case 'n':
1795 if (strcmp("-nostdinc", opts.args.str[i]) == 0)
1796 opts.nostdinc = 1;
1797 else if (strcmp("-nodefaultlibs", opts.args.str[i]) == 0)
1798 opts.nodefaultlibs = 1;
1799 else if (strcmp("-nostdlib", opts.args.str[i]) == 0)
1800 opts.nostdlib = 1;
1801 else if (strcmp("-nostartfiles", opts.args.str[i]) == 0)
1802 opts.nostartfiles = 1;
1803 break;
1804 case 'o':
1805 opts.output_name = option_arg;
1806 raw_compiler_arg = 0;
1807 break;
1808 case 'p':
1809 if (strcmp("-pthread", opts.args.str[i]) == 0)
1811 raw_compiler_arg = 1;
1812 raw_linker_arg = 1;
1814 break;
1815 case 's':
1816 if (strcmp("-static", opts.args.str[i]) == 0)
1817 linking = -1;
1818 else if(strcmp("-save-temps", opts.args.str[i]) == 0)
1819 keep_generated = 1;
1820 else if (strncmp("-specs=", opts.args.str[i], 7) == 0)
1821 raw_linker_arg = 1;
1822 else if(strcmp("-shared", opts.args.str[i]) == 0)
1824 opts.shared = 1;
1825 raw_compiler_arg = raw_linker_arg = 0;
1827 else if (strcmp("-s", opts.args.str[i]) == 0 && opts.target.platform == PLATFORM_APPLE)
1829 /* On Mac, change -s into -Wl,-x. ld's -s switch
1830 * is deprecated, and it doesn't work on Tiger with
1831 * MH_BUNDLEs anyway
1833 opts.strip = 1;
1834 raw_linker_arg = 0;
1836 break;
1837 case 't':
1838 if (is_option( &opts, i, "-target", &option_arg ))
1840 parse_target_option( &opts, option_arg );
1841 raw_compiler_arg = raw_linker_arg = 0;
1843 break;
1844 case 'v':
1845 if (opts.args.str[i][2] == 0) verbose++;
1846 break;
1847 case 'W':
1848 if (strncmp("-Wl,", opts.args.str[i], 4) == 0)
1850 unsigned int j;
1851 struct strarray Wl = strarray_fromstring(opts.args.str[i] + 4, ",");
1852 for (j = 0; j < Wl.count; j++)
1854 if (!strcmp(Wl.str[j], "--image-base") && j < Wl.count - 1)
1856 opts.image_base = xstrdup( Wl.str[++j] );
1857 continue;
1859 if (!strcmp(Wl.str[j], "--section-alignment") && j < Wl.count - 1)
1861 opts.section_align = xstrdup( Wl.str[++j] );
1862 continue;
1864 if (!strcmp(Wl.str[j], "--file-alignment") && j < Wl.count - 1)
1866 opts.file_align = xstrdup( Wl.str[++j] );
1867 continue;
1869 if (!strcmp(Wl.str[j], "--large-address-aware"))
1871 opts.large_address_aware = 1;
1872 continue;
1874 if (!strcmp(Wl.str[j], "--wine-builtin"))
1876 opts.wine_builtin = 1;
1877 continue;
1879 if (!strcmp(Wl.str[j], "--subsystem") && j < Wl.count - 1)
1881 opts.subsystem = xstrdup( Wl.str[++j] );
1882 continue;
1884 if (!strcmp(Wl.str[j], "--entry") && j < Wl.count - 1)
1886 opts.entry_point = xstrdup( Wl.str[++j] );
1887 continue;
1889 if (!strcmp(Wl.str[j], "-delayload") && j < Wl.count - 1)
1891 strarray_add( &opts.delayimports, Wl.str[++j] );
1892 continue;
1894 if (!strcmp(Wl.str[j], "--debug-file") && j < Wl.count - 1)
1896 opts.debug_file = xstrdup( Wl.str[++j] );
1897 continue;
1899 if (!strcmp(Wl.str[j], "--whole-archive") ||
1900 !strcmp(Wl.str[j], "--no-whole-archive") ||
1901 !strcmp(Wl.str[j], "--start-group") ||
1902 !strcmp(Wl.str[j], "--end-group"))
1904 strarray_add( &opts.files, strmake( "-Wl,%s", Wl.str[j] ));
1905 continue;
1907 if (!strcmp(Wl.str[j], "--out-implib"))
1909 opts.out_implib = xstrdup( Wl.str[++j] );
1910 continue;
1912 if (!strcmp(Wl.str[j], "-static")) linking = -1;
1913 strarray_add(&opts.linker_args, strmake("-Wl,%s",Wl.str[j]));
1915 raw_compiler_arg = raw_linker_arg = 0;
1917 else if (strncmp("-Wb,", opts.args.str[i], 4) == 0)
1919 unsigned int j;
1920 struct strarray Wb = strarray_fromstring(opts.args.str[i] + 4, ",");
1921 for (j = 0; j < Wb.count; j++)
1923 if (!strcmp(Wb.str[j], "--data-only")) opts.data_only = 1;
1924 if (!strcmp(Wb.str[j], "--fake-module")) opts.fake_module = 1;
1925 else strarray_add( &opts.winebuild_args, Wb.str[j] );
1927 raw_compiler_arg = raw_linker_arg = 0;
1929 break;
1930 case 'x':
1931 lang = strmake("-x%s", option_arg);
1932 strarray_add(&opts.files, lang);
1933 /* we'll pass these flags ourselves, explicitly */
1934 raw_compiler_arg = raw_linker_arg = 0;
1935 break;
1936 case '-':
1937 if (strcmp("-static", opts.args.str[i]+1) == 0)
1938 linking = -1;
1939 else if (is_option( &opts, i, "--sysroot", &option_arg ))
1941 opts.sysroot = option_arg;
1942 raw_linker_arg = 1;
1944 else if (is_option( &opts, i, "--target", &option_arg ))
1946 parse_target_option( &opts, option_arg );
1947 raw_compiler_arg = raw_linker_arg = 0;
1949 else if (is_option( &opts, i, "--wine-objdir", &option_arg ))
1951 opts.wine_objdir = option_arg;
1952 raw_compiler_arg = raw_linker_arg = 0;
1954 else if (is_option( &opts, i, "--winebuild", &option_arg ))
1956 opts.winebuild = option_arg;
1957 raw_compiler_arg = raw_linker_arg = 0;
1959 else if (is_option( &opts, i, "--lib-suffix", &option_arg ))
1961 opts.lib_suffix = option_arg;
1962 raw_compiler_arg = raw_linker_arg = 0;
1964 break;
1967 /* put the arg into the appropriate bucket */
1968 if (raw_linker_arg)
1970 strarray_add( &opts.linker_args, opts.args.str[i] );
1971 if (next_is_arg && (i + 1 < opts.args.count))
1972 strarray_add( &opts.linker_args, opts.args.str[i + 1] );
1974 if (raw_compiler_arg)
1976 strarray_add( &opts.compiler_args, opts.args.str[i] );
1977 if (next_is_arg && (i + 1 < opts.args.count))
1978 strarray_add( &opts.compiler_args, opts.args.str[i + 1] );
1980 if (raw_winebuild_arg)
1982 strarray_add( &opts.winebuild_args, opts.args.str[i] );
1983 if (next_is_arg && (i + 1 < opts.args.count))
1984 strarray_add( &opts.winebuild_args, opts.args.str[i + 1] );
1987 /* skip the next token if it's an argument */
1988 if (next_is_arg) i++;
1990 else
1992 strarray_add( &opts.files, opts.args.str[i] );
1996 if (opts.force_pointer_size) set_target_ptr_size( &opts.target, opts.force_pointer_size );
1997 if (opts.processor == proc_cpp) linking = 0;
1998 if (linking == -1) error("Static linking is not supported\n");
2000 if (!opts.wine_objdir && is_pe_target( &opts )) opts.use_msvcrt = 1;
2002 if (opts.files.count == 0 && !opts.fake_module) forward(&opts);
2003 else if (linking) build(&opts);
2004 else compile(&opts, lang);
2006 output_file_name = NULL;
2007 output_debug_file = NULL;
2008 output_implib = NULL;
2009 return 0;