wow64: In wow64_NtSetInformationToken forward TokenIntegrityLevel.
[wine.git] / tools / winegcc / winegcc.c
blobfb759063da9cb267d7e9ad57b618978c14c31dc5
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 int no_default_config;
181 const char* wine_objdir;
182 const char* winebuild;
183 const char* output_name;
184 const char* image_base;
185 const char* section_align;
186 const char* file_align;
187 const char* sysroot;
188 const char* isysroot;
189 const char* lib_suffix;
190 const char* subsystem;
191 const char* entry_point;
192 const char* debug_file;
193 const char* out_implib;
194 const char* native_arch;
195 struct strarray prefix;
196 struct strarray lib_dirs;
197 struct strarray args;
198 struct strarray linker_args;
199 struct strarray compiler_args;
200 struct strarray winebuild_args;
201 struct strarray files;
202 struct strarray delayimports;
205 static void cleanup_output_files(void)
207 if (output_file_name) unlink( output_file_name );
208 if (output_debug_file) unlink( output_debug_file );
209 if (output_implib) unlink( output_implib );
212 static void clean_temp_files(void)
214 if (!keep_generated) remove_temp_files();
217 /* clean things up when aborting on a signal */
218 static void exit_on_signal( int sig )
220 exit(1); /* this will call the atexit functions */
223 static int is_pe_target( const struct options *opts )
225 switch (opts->target.platform)
227 case PLATFORM_MINGW:
228 case PLATFORM_CYGWIN:
229 case PLATFORM_WINDOWS:
230 return 1;
231 default:
232 return 0;
236 enum tool
238 TOOL_CC,
239 TOOL_CXX,
240 TOOL_CPP,
241 TOOL_LD,
242 TOOL_OBJCOPY,
245 static const struct
247 const char *base;
248 const char *llvm_base;
249 const char *deflt;
250 } tool_names[] =
252 { "gcc", "clang --driver-mode=gcc", CC },
253 { "g++", "clang --driver-mode=g++", CXX },
254 { "cpp", "clang --driver-mode=cpp", CPP },
255 { "ld", "ld.lld", LD },
256 { "objcopy", "llvm-objcopy" },
259 static struct strarray build_tool_name( struct options *opts, const char *target, enum tool tool )
261 const char *base = tool_names[tool].base;
262 const char *llvm_base = tool_names[tool].llvm_base;
263 const char *deflt = tool_names[tool].deflt;
264 const char *path;
265 struct strarray ret = empty_strarray;
266 char* str;
268 if (target && opts->version)
270 str = strmake( "%s-%s-%s", target, base, opts->version );
272 else if (target)
274 str = strmake( "%s-%s", target, base );
276 else if (opts->version)
278 str = strmake("%s-%s", base, opts->version);
280 else
281 str = xstrdup((deflt && *deflt) ? deflt : base);
283 if ((path = find_binary( opts->prefix, str ))) return strarray_fromstring( path, " " );
285 if (!opts->version) str = xstrdup( llvm_base );
286 else str = strmake( "%s-%s", llvm_base, opts->version );
287 path = find_binary( opts->prefix, str );
288 if (!path)
290 error( "Could not find %s\n", base );
291 return ret;
294 ret = strarray_fromstring( path, " " );
295 if (!strncmp( llvm_base, "clang", 5 ))
297 if (target)
299 strarray_add( &ret, "-target" );
300 strarray_add( &ret, target );
302 strarray_add( &ret, "-Wno-unused-command-line-argument" );
303 strarray_add( &ret, "-fuse-ld=lld" );
304 if (opts->no_default_config) strarray_add( &ret, "--no-default-config" );
306 return ret;
309 static struct strarray get_translator(struct options *opts)
311 switch(opts->processor)
313 case proc_cpp:
314 return build_tool_name( opts, opts->target_alias, TOOL_CPP );
315 case proc_cc:
316 case proc_as:
317 return build_tool_name( opts, opts->target_alias, TOOL_CC );
318 case proc_cxx:
319 return build_tool_name( opts, opts->target_alias, TOOL_CXX );
321 assert(0);
322 return empty_strarray;
325 static int try_link( struct strarray prefix, struct strarray link_tool, const char *cflags )
327 const char *in = make_temp_file( "try_link", ".c" );
328 const char *out = make_temp_file( "try_link", ".out" );
329 const char *err = make_temp_file( "try_link", ".err" );
330 struct strarray link = empty_strarray;
331 int sout = -1, serr = -1;
332 int ret;
334 create_file( in, 0644, "int main(void){return 1;}\n" );
336 strarray_addall( &link, link_tool );
337 strarray_add( &link, "-o" );
338 strarray_add( &link, out );
339 strarray_addall( &link, strarray_fromstring( cflags, " " ) );
340 strarray_add( &link, in );
342 sout = dup( fileno(stdout) );
343 freopen( err, "w", stdout );
344 serr = dup( fileno(stderr) );
345 freopen( err, "w", stderr );
346 ret = spawn( prefix, link, 1 );
347 if (sout >= 0)
349 dup2( sout, fileno(stdout) );
350 close( sout );
352 if (serr >= 0)
354 dup2( serr, fileno(stderr) );
355 close( serr );
357 return ret;
360 static struct strarray get_link_args( struct options *opts, const char *output_name )
362 struct strarray link_args = get_translator( opts );
363 struct strarray flags = empty_strarray;
365 strarray_addall( &link_args, opts->linker_args );
367 if (verbose > 1) strarray_add( &flags, "-v" );
369 switch (opts->target.platform)
371 case PLATFORM_APPLE:
372 strarray_add( &flags, "-bundle" );
373 strarray_add( &flags, "-multiply_defined" );
374 strarray_add( &flags, "suppress" );
375 if (opts->image_base)
377 strarray_add( &flags, "-image_base" );
378 strarray_add( &flags, opts->image_base );
380 if (opts->strip) strarray_add( &flags, "-Wl,-x" );
381 strarray_addall( &link_args, flags );
382 return link_args;
384 case PLATFORM_SOLARIS:
386 char *mapfile = make_temp_file( output_name, ".map" );
387 const char *align = opts->section_align ? opts->section_align : "0x1000";
389 create_file( mapfile, 0644, "text = A%s;\ndata = A%s;\n", align, align );
390 strarray_add( &flags, strmake("-Wl,-M,%s", mapfile) );
392 break;
394 case PLATFORM_ANDROID:
395 /* the Android loader requires a soname for all libraries */
396 strarray_add( &flags, strmake( "-Wl,-soname,%s.so", output_name ));
397 break;
399 case PLATFORM_MINGW:
400 case PLATFORM_CYGWIN:
401 if (opts->shared || opts->win16_app)
403 strarray_add( &flags, "-shared" );
404 strarray_add( &flags, "-Wl,--kill-at" );
406 else strarray_add( &flags, opts->gui_app ? "-mwindows" : "-mconsole" );
408 if (opts->unicode_app) strarray_add( &flags, "-municode" );
409 if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
410 if (opts->nostartfiles || opts->use_msvcrt) strarray_add( &flags, "-nostartfiles" );
411 if (opts->subsystem) strarray_add( &flags, strmake("-Wl,--subsystem,%s", opts->subsystem ));
413 strarray_add( &flags, "-Wl,--exclude-all-symbols" );
414 strarray_add( &flags, "-Wl,--nxcompat" );
415 strarray_add( &flags, "-Wl,--dynamicbase" );
416 strarray_add( &flags, "-Wl,--disable-auto-image-base" );
418 if (opts->image_base) strarray_add( &flags, strmake("-Wl,--image-base,%s", opts->image_base ));
420 if (opts->large_address_aware && opts->target.cpu == CPU_i386)
421 strarray_add( &flags, "-Wl,--large-address-aware" );
423 /* make sure we don't need a libgcc_s dll on Windows */
424 if (!opts->nodefaultlibs && !opts->use_msvcrt)
425 strarray_add( &flags, "-static-libgcc" );
427 if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
428 strarray_add(&link_args, strmake("-Wl,--pdb=%s", opts->debug_file));
430 if (opts->out_implib)
431 strarray_add(&link_args, strmake("-Wl,--out-implib,%s", opts->out_implib));
433 if (!try_link( opts->prefix, link_args, "-Wl,--file-alignment,0x1000" ))
434 strarray_add( &link_args, strmake( "-Wl,--file-alignment,%s",
435 opts->file_align ? opts->file_align : "0x1000" ));
436 else if (!try_link( opts->prefix, link_args, "-Wl,-Xlink=-filealign:0x1000" ))
437 /* lld from llvm 10 does not support mingw style --file-alignment,
438 * but it's possible to use msvc syntax */
439 strarray_add( &link_args, strmake( "-Wl,-Xlink=-filealign:%s",
440 opts->file_align ? opts->file_align : "0x1000" ));
442 strarray_addall( &link_args, flags );
443 return link_args;
445 case PLATFORM_WINDOWS:
446 if (opts->shared || opts->win16_app)
448 strarray_add( &flags, "-shared" );
449 strarray_add( &flags, "-Wl,-kill-at" );
451 if (opts->unicode_app) strarray_add( &flags, "-municode" );
452 if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
453 if (opts->nostartfiles) strarray_add( &flags, "-nostartfiles" );
454 if (opts->use_msvcrt) strarray_add( &flags, "-nostdlib" );
455 if (opts->image_base) strarray_add( &flags, strmake("-Wl,-base:%s", opts->image_base ));
456 if (opts->subsystem)
457 strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->subsystem ));
458 else
459 strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->gui_app ? "windows" : "console" ));
461 if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
463 strarray_add(&link_args, "-Wl,-debug");
464 strarray_add(&link_args, strmake("-Wl,-pdb:%s", opts->debug_file));
466 else if (!opts->strip)
467 strarray_add(&link_args, "-Wl,-debug:dwarf");
469 if (opts->out_implib)
470 strarray_add(&link_args, strmake("-Wl,-implib:%s", opts->out_implib));
471 else
472 strarray_add(&link_args, strmake("-Wl,-implib:%s", make_temp_file( output_name, ".lib" )));
474 strarray_add( &link_args, strmake( "-Wl,-filealign:%s", opts->file_align ? opts->file_align : "0x1000" ));
476 strarray_addall( &link_args, flags );
477 return link_args;
479 default:
480 if (opts->image_base)
482 if (!try_link( opts->prefix, link_args, strmake("-Wl,-Ttext-segment=%s", opts->image_base)) )
483 strarray_add( &flags, strmake("-Wl,-Ttext-segment=%s", opts->image_base) );
485 if (!try_link( opts->prefix, link_args, "-Wl,-z,max-page-size=0x1000"))
486 strarray_add( &flags, "-Wl,-z,max-page-size=0x1000");
487 break;
490 /* generic Unix shared library flags */
492 strarray_add( &link_args, "-shared" );
493 strarray_add( &link_args, "-Wl,-Bsymbolic" );
494 if (!opts->noshortwchar && opts->target.cpu == CPU_ARM)
495 strarray_add( &flags, "-Wl,--no-wchar-size-warning" );
496 if (!try_link( opts->prefix, link_args, "-Wl,-z,defs" ))
497 strarray_add( &flags, "-Wl,-z,defs" );
499 strarray_addall( &link_args, flags );
500 return link_args;
503 static const char *get_multiarch_dir( struct target target )
505 switch (target.cpu)
507 case CPU_i386: return "/i386-linux-gnu";
508 case CPU_x86_64: return "/x86_64-linux-gnu";
509 case CPU_ARM: return "/arm-linux-gnueabi";
510 case CPU_ARM64: return "/aarch64-linux-gnu";
511 default:
512 assert(0);
514 return NULL;
517 static char *get_lib_dir( struct options *opts )
519 const char *stdlibpath[] = { libdir, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
520 const char *bit_suffix, *other_bit_suffix, *build_multiarch, *target_multiarch, *winecrt0;
521 const char *root = opts->sysroot ? opts->sysroot : "";
522 unsigned int i;
523 struct stat st;
524 size_t build_len, target_len;
526 bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "64" : "32";
527 other_bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "32" : "64";
528 winecrt0 = strmake( "/wine%s/libwinecrt0.a", get_arch_dir( opts->target ));
529 build_multiarch = get_multiarch_dir( get_default_target() );
530 target_multiarch = get_multiarch_dir( opts->target );
531 build_len = strlen( build_multiarch );
532 target_len = strlen( target_multiarch );
534 for (i = 0; i < ARRAY_SIZE(stdlibpath); i++)
536 const char *root = (i && opts->sysroot) ? opts->sysroot : "";
537 char *p, *buffer;
539 if (!stdlibpath[i]) continue;
540 buffer = xmalloc( strlen(root) + strlen(stdlibpath[i]) +
541 strlen("/arm-linux-gnueabi") + strlen(winecrt0) + 1 );
542 strcpy( buffer, root );
543 strcat( buffer, stdlibpath[i] );
544 p = buffer + strlen(buffer);
545 while (p > buffer && p[-1] == '/') p--;
546 strcpy( p, winecrt0 );
547 if (!stat( buffer, &st )) goto found;
548 if (p > buffer + 2 && (!memcmp( p - 2, "32", 2 ) || !memcmp( p - 2, "64", 2 )))
550 p -= 2;
551 strcpy( p, winecrt0 );
552 if (!stat( buffer, &st )) goto found;
554 strcpy( p, bit_suffix );
555 strcat( p, winecrt0 );
556 if (!stat( buffer, &st )) goto found;
557 strcpy( p, target_multiarch );
558 strcat( p, winecrt0 );
559 if (!stat( buffer, &st )) goto found;
561 strcpy( buffer, root );
562 strcat( buffer, stdlibpath[i] );
563 p = buffer + strlen(buffer);
564 while (p > buffer && p[-1] == '/') p--;
565 strcpy( p, winecrt0 );
567 /* try to fixup each parent dirs named lib, lib32 or lib64 with target bitness suffix */
568 while (p > buffer)
570 p--;
571 while (p > buffer && *p != '/') p--;
572 if (*p != '/') break;
574 /* try s/$build_cpu/$target_cpu/ on multiarch */
575 if (get_default_target().cpu != opts->target.cpu &&
576 !memcmp( p, build_multiarch, build_len ) && p[build_len] == '/')
578 memmove( p + target_len, p + build_len, strlen( p + build_len ) + 1 );
579 memcpy( p, target_multiarch, target_len );
580 if (!stat( buffer, &st )) goto found;
581 memmove( p + build_len, p + target_len, strlen( p + target_len ) + 1 );
582 memcpy( p, build_multiarch, build_len );
585 if (memcmp( p + 1, "lib", 3 )) continue;
586 if (p[4] == '/')
588 memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
589 memcpy( p + 4, bit_suffix, 2 );
590 if (!stat( buffer, &st )) goto found;
591 memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
593 else if (!memcmp( p + 4, other_bit_suffix, 2 ) && p[6] == '/')
595 memcpy( p + 4, bit_suffix, 2 );
596 if (!stat( buffer, &st )) goto found;
597 memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
598 if (!stat( buffer, &st )) goto found;
599 memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
600 memcpy( p + 4, other_bit_suffix, 2 );
604 free( buffer );
605 continue;
607 found:
608 buffer[strlen(buffer) - strlen(winecrt0)] = 0;
609 return buffer;
611 return strmake( "%s%s", root, LIBDIR );
614 static void init_argv0_dir( const char *argv0 )
616 if (!(bindir = get_argv0_dir( argv0 ))) return;
617 includedir = strmake( "%s/%s", bindir, BIN_TO_INCLUDEDIR );
618 libdir = strmake( "%s/%s", bindir, BIN_TO_LIBDIR );
621 static void compile(struct options* opts, const char* lang)
623 struct strarray comp_args = get_translator(opts);
624 unsigned int i, j;
625 int gcc_defs = 0;
626 struct strarray gcc;
627 struct strarray gpp;
629 if (opts->force_pointer_size)
630 strarray_add( &comp_args, strmake("-m%u", 8 * opts->force_pointer_size ) );
631 switch(opts->processor)
633 case proc_cpp: gcc_defs = 1; break;
634 case proc_as: gcc_defs = 0; break;
635 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
636 /* mixing different C and C++ compilers isn't supported in configure anyway */
637 case proc_cc:
638 case proc_cxx:
639 gcc = build_tool_name( opts, opts->target_alias, TOOL_CC );
640 gpp = build_tool_name( opts, opts->target_alias, TOOL_CXX );
641 for ( j = 0; !gcc_defs && j < comp_args.count; j++ )
643 const char *cc = comp_args.str[j];
645 for (i = 0; !gcc_defs && i < gcc.count; i++)
646 gcc_defs = gcc.str[i][0] != '-' && strendswith(cc, gcc.str[i]);
647 for (i = 0; !gcc_defs && i < gpp.count; i++)
648 gcc_defs = gpp.str[i][0] != '-' && strendswith(cc, gpp.str[i]);
650 break;
653 if (opts->target.platform == PLATFORM_WINDOWS ||
654 opts->target.platform == PLATFORM_CYGWIN ||
655 opts->target.platform == PLATFORM_MINGW)
656 goto no_compat_defines;
658 if (opts->processor != proc_cpp)
660 if (gcc_defs && !opts->wine_objdir && !opts->noshortwchar)
662 strarray_add(&comp_args, "-fshort-wchar");
663 strarray_add(&comp_args, "-DWINE_UNICODE_NATIVE");
665 strarray_add(&comp_args, "-D_REENTRANT");
666 if (opts->pic)
667 strarray_add(&comp_args, "-fPIC");
668 else
669 strarray_add(&comp_args, "-fno-PIC");
672 if (get_target_ptr_size( opts->target ) == 8)
674 strarray_add(&comp_args, "-DWIN64");
675 strarray_add(&comp_args, "-D_WIN64");
676 strarray_add(&comp_args, "-D__WIN64");
677 strarray_add(&comp_args, "-D__WIN64__");
680 strarray_add(&comp_args, "-DWIN32");
681 strarray_add(&comp_args, "-D_WIN32");
682 strarray_add(&comp_args, "-D__WIN32");
683 strarray_add(&comp_args, "-D__WIN32__");
684 strarray_add(&comp_args, "-D__WINNT");
685 strarray_add(&comp_args, "-D__WINNT__");
687 if (gcc_defs)
689 switch (opts->target.cpu)
691 case CPU_x86_64:
692 case CPU_ARM64:
693 strarray_add(&comp_args, "-D__stdcall=__attribute__((ms_abi))");
694 strarray_add(&comp_args, "-D__cdecl=__stdcall");
695 strarray_add(&comp_args, "-D__fastcall=__stdcall");
696 break;
697 case CPU_i386:
698 strarray_add(&comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
699 strarray_add(&comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
700 strarray_add(&comp_args, "-D__fastcall=__attribute__((__fastcall__))");
701 break;
702 case CPU_ARM:
703 strarray_add(&comp_args, "-D__stdcall=__attribute__((pcs(\"aapcs-vfp\")))");
704 strarray_add(&comp_args, "-D__cdecl=__stdcall");
705 strarray_add(&comp_args, "-D__fastcall=__stdcall");
706 break;
707 case CPU_ARM64EC:
708 break;
710 strarray_add(&comp_args, "-D_stdcall=__stdcall");
711 strarray_add(&comp_args, "-D_cdecl=__cdecl");
712 strarray_add(&comp_args, "-D_fastcall=__fastcall");
713 strarray_add(&comp_args, "-D__declspec(x)=__declspec_##x");
714 strarray_add(&comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
715 strarray_add(&comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
716 strarray_add(&comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
717 strarray_add(&comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
718 strarray_add(&comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
719 strarray_add(&comp_args, "-D__declspec_naked=__attribute__((naked))");
720 strarray_add(&comp_args, "-D__declspec_noinline=__attribute__((noinline))");
721 strarray_add(&comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
722 strarray_add(&comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
723 strarray_add(&comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
724 strarray_add(&comp_args, "-D__declspec_selectany=__attribute__((weak))");
725 strarray_add(&comp_args, "-D__declspec_thread=__thread");
728 strarray_add(&comp_args, "-D__int8=char");
729 strarray_add(&comp_args, "-D__int16=short");
730 strarray_add(&comp_args, "-D__int32=int");
731 if (get_target_ptr_size( opts->target ) == 8)
732 strarray_add(&comp_args, "-D__int64=long");
733 else
734 strarray_add(&comp_args, "-D__int64=long long");
736 no_compat_defines:
737 strarray_add(&comp_args, "-D__WINE__");
739 /* options we handle explicitly */
740 if (opts->compile_only)
741 strarray_add(&comp_args, "-c");
742 if (opts->output_name)
744 strarray_add(&comp_args, "-o");
745 strarray_add(&comp_args, opts->output_name);
748 /* the rest of the pass-through parameters */
749 strarray_addall(&comp_args, opts->compiler_args);
751 /* the language option, if any */
752 if (lang && strcmp(lang, "-xnone"))
753 strarray_add(&comp_args, lang);
755 /* last, but not least, the files */
756 for ( j = 0; j < opts->files.count; j++ )
758 if (opts->files.str[j][0] != '-' || !opts->files.str[j][1]) /* not an option or bare '-' (i.e. stdin) */
759 strarray_add(&comp_args, opts->files.str[j]);
762 /* standard includes come last in the include search path */
763 if (!opts->wine_objdir && !opts->nostdinc)
765 const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
766 const char *root = opts->isysroot ? opts->isysroot : opts->sysroot ? opts->sysroot : "";
767 const char *isystem = gcc_defs ? "-isystem" : "-I";
768 const char *idirafter = gcc_defs ? "-idirafter" : "-I";
770 if (opts->use_msvcrt)
772 if (includedir) strarray_add( &comp_args, strmake( "%s%s/wine/msvcrt", isystem, includedir ));
773 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
775 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
776 strarray_add(&comp_args, strmake( "%s%s%s/wine/msvcrt", isystem, root, incl_dirs[j] ));
778 strarray_add(&comp_args, "-D__MSVCRT__");
780 if (includedir)
782 strarray_add( &comp_args, strmake( "%s%s/wine/windows", isystem, includedir ));
783 strarray_add( &comp_args, strmake( "%s%s", idirafter, includedir ));
785 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
787 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
788 strarray_add(&comp_args, strmake( "%s%s%s/wine/windows", isystem, root, incl_dirs[j] ));
789 strarray_add(&comp_args, strmake( "%s%s%s", idirafter, root, incl_dirs[j] ));
792 else if (opts->wine_objdir)
793 strarray_add(&comp_args, strmake("-I%s/include", opts->wine_objdir) );
795 spawn(opts->prefix, comp_args, 0);
798 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
800 struct options copts;
802 /* make a copy so we don't change any of the initial stuff */
803 /* a shallow copy is exactly what we want in this case */
804 copts = *opts;
805 copts.output_name = make_temp_file(get_basename_noext(file), ".o");
806 copts.compile_only = 1;
807 copts.files = empty_strarray;
808 strarray_add(&copts.files, file);
809 compile(&copts, lang);
810 return copts.output_name;
813 /* return the initial set of options needed to run winebuild */
814 static struct strarray get_winebuild_args( struct options *opts, const char *target )
816 const char* winebuild = getenv("WINEBUILD");
817 const char *binary = NULL;
818 struct strarray spec_args = empty_strarray;
819 unsigned int i;
821 if (opts->winebuild)
822 binary = opts->winebuild;
823 else if (opts->wine_objdir)
824 binary = strmake( "%s/tools/winebuild/winebuild%s", opts->wine_objdir, EXEEXT );
825 else if (winebuild)
826 binary = find_binary( opts->prefix, winebuild );
827 else if (bindir)
828 binary = strmake( "%s/winebuild%s", bindir, EXEEXT );
829 else
830 binary = find_binary( opts->prefix, "winebuild" );
831 if (!binary) error( "Could not find winebuild\n" );
832 strarray_add( &spec_args, binary );
833 if (verbose) strarray_add( &spec_args, "-v" );
834 if (keep_generated) strarray_add( &spec_args, "--save-temps" );
835 if (target)
837 strarray_add( &spec_args, "--target" );
838 strarray_add( &spec_args, target );
840 if (opts->force_pointer_size)
841 strarray_add(&spec_args, strmake("-m%u", 8 * opts->force_pointer_size ));
842 for (i = 0; i < opts->prefix.count; i++)
843 strarray_add( &spec_args, strmake( "-B%s", opts->prefix.str[i] ));
844 strarray_addall( &spec_args, opts->winebuild_args );
845 return spec_args;
848 static void fixup_constructors( struct options *opts, const char *file )
850 struct strarray args = get_winebuild_args( opts, opts->target_alias );
852 strarray_add( &args, "--fixup-ctors" );
853 strarray_add( &args, file );
854 spawn( opts->prefix, args, 0 );
857 static void make_wine_builtin( struct options *opts, const char *file )
859 struct strarray args = get_winebuild_args( opts, opts->target_alias );
861 strarray_add( &args, "--builtin" );
862 strarray_add( &args, file );
863 spawn( opts->prefix, args, 0 );
866 /* check if there is a static lib associated to a given dll */
867 static char *find_static_lib( const char *dll )
869 char *lib = strmake("%s.a", dll);
870 if (get_file_type(lib) == file_arh) return lib;
871 free( lib );
872 return NULL;
875 static const char *find_libgcc(struct strarray prefix, struct strarray link_tool)
877 const char *out = make_temp_file( "find_libgcc", ".out" );
878 const char *err = make_temp_file( "find_libgcc", ".err" );
879 struct strarray link = empty_strarray;
880 int sout = -1, serr = -1;
881 char *libgcc, *p;
882 struct stat st;
883 size_t cnt;
884 int ret;
886 strarray_addall( &link, link_tool );
887 strarray_add( &link, "-print-libgcc-file-name" );
889 sout = dup( fileno(stdout) );
890 freopen( out, "w", stdout );
891 serr = dup( fileno(stderr) );
892 freopen( err, "w", stderr );
893 ret = spawn( prefix, link, 1 );
894 if (sout >= 0)
896 dup2( sout, fileno(stdout) );
897 close( sout );
899 if (serr >= 0)
901 dup2( serr, fileno(stderr) );
902 close( serr );
905 if (ret || stat(out, &st) || !st.st_size) return NULL;
907 libgcc = xmalloc(st.st_size + 1);
908 sout = open(out, O_RDONLY);
909 if (sout == -1) return NULL;
910 cnt = read(sout, libgcc, st.st_size);
911 close(sout);
912 libgcc[cnt] = 0;
913 if ((p = strchr(libgcc, '\n'))) *p = 0;
914 return libgcc;
918 /* add specified library to the list of files */
919 static void add_library( struct options *opts, struct strarray lib_dirs,
920 struct strarray *files, const char *library )
922 char *static_lib, *fullname = 0;
924 switch(get_lib_type(opts->target, lib_dirs, library, "lib", opts->lib_suffix, &fullname))
926 case file_arh:
927 strarray_add(files, strmake("-a%s", fullname));
928 break;
929 case file_dll:
930 strarray_add(files, strmake("-d%s", fullname));
931 if ((static_lib = find_static_lib(fullname)))
933 strarray_add(files, strmake("-a%s",static_lib));
934 free(static_lib);
936 break;
937 case file_so:
938 default:
939 /* keep it anyway, the linker may know what to do with it */
940 strarray_add(files, strmake("-l%s", library));
941 break;
943 free(fullname);
946 /* run winebuild to generate the .spec.o file */
947 static void build_spec_obj( struct options *opts, const char *spec_file, const char *output_file,
948 const char *target, struct strarray files, struct strarray resources,
949 struct strarray lib_dirs, const char *entry_point, struct strarray *spec_objs )
951 unsigned int i;
952 int is_pe = is_pe_target( opts );
953 struct strarray spec_args = get_winebuild_args( opts, target );
954 struct strarray tool;
955 const char *spec_o_name, *output_name;
957 /* get the filename from the path */
958 output_name = get_basename( output_file );
960 tool = build_tool_name( opts, target, TOOL_CC );
961 strarray_add( &spec_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
962 if (!is_pe)
964 tool = build_tool_name( opts, target, TOOL_LD );
965 strarray_add( &spec_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
968 spec_o_name = make_temp_file(output_name, ".spec.o");
969 if (!is_pe)
971 if (opts->pic) strarray_add(&spec_args, "-fPIC");
972 if (opts->use_msvcrt) strarray_add(&spec_args, "-mno-cygwin");
973 if (opts->unwind_tables) strarray_add( &spec_args, "-fasynchronous-unwind-tables" );
975 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
976 if (opts->fake_module)
978 strarray_add(&spec_args, "--fake-module");
979 strarray_add(&spec_args, "-o");
980 strarray_add(&spec_args, output_file);
982 else
984 strarray_add(&spec_args, "-o");
985 strarray_add(&spec_args, spec_o_name);
987 if (spec_file)
989 strarray_add(&spec_args, "-E");
990 strarray_add(&spec_args, spec_file);
993 if (!opts->shared)
995 strarray_add(&spec_args, "-F");
996 strarray_add(&spec_args, output_name);
997 strarray_add(&spec_args, "--subsystem");
998 strarray_add(&spec_args, opts->gui_app ? "windows" : "console");
999 if (opts->large_address_aware) strarray_add( &spec_args, "--large-address-aware" );
1002 if (opts->target.platform == PLATFORM_WINDOWS && opts->target.cpu == CPU_i386)
1003 strarray_add(&spec_args, "--safeseh");
1005 if (entry_point)
1007 strarray_add(&spec_args, "--entry");
1008 strarray_add(&spec_args, entry_point);
1011 if (opts->subsystem)
1013 strarray_add(&spec_args, "--subsystem");
1014 strarray_add(&spec_args, opts->subsystem);
1017 for (i = 0; i < lib_dirs.count; i++)
1018 strarray_add(&spec_args, strmake("-L%s", lib_dirs.str[i]));
1020 if (!is_pe)
1022 for (i = 0; i < opts->delayimports.count; i++)
1023 strarray_add(&spec_args, strmake("-d%s", opts->delayimports.str[i]));
1026 strarray_addall( &spec_args, resources );
1028 /* add other files */
1029 strarray_add(&spec_args, "--");
1030 for (i = 0; i < files.count; i++)
1032 switch(files.str[i][1])
1034 case 'd':
1035 case 'a':
1036 case 'o':
1037 strarray_add(&spec_args, files.str[i] + 2);
1038 break;
1042 spawn(opts->prefix, spec_args, 0);
1043 strarray_add( spec_objs, spec_o_name );
1046 /* run winebuild to generate a data-only library */
1047 static void build_data_lib( struct options *opts, const char *spec_file, const char *output_file, struct strarray files )
1049 unsigned int i;
1050 struct strarray spec_args = get_winebuild_args( opts, opts->target_alias );
1052 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
1053 strarray_add(&spec_args, "-o");
1054 strarray_add(&spec_args, output_file);
1055 if (spec_file)
1057 strarray_add(&spec_args, "-E");
1058 strarray_add(&spec_args, spec_file);
1061 /* add resource files */
1062 for (i = 0; i < files.count; i++)
1063 if (files.str[i][1] == 'r') strarray_add(&spec_args, files.str[i]);
1065 spawn(opts->prefix, spec_args, 0);
1068 static void build(struct options* opts)
1070 struct strarray resources = empty_strarray;
1071 struct strarray spec_objs = empty_strarray;
1072 struct strarray lib_dirs = empty_strarray;
1073 struct strarray files = empty_strarray;
1074 struct strarray link_args;
1075 char *output_file, *output_path;
1076 const char *output_name, *spec_file, *lang;
1077 const char *libgcc = NULL;
1078 int generate_app_loader = 1;
1079 const char *crt_lib = NULL, *entry_point = NULL;
1080 int is_pe = is_pe_target( opts );
1081 unsigned int i, j;
1083 /* NOTE: for the files array we'll use the following convention:
1084 * -axxx: xxx is an archive (.a)
1085 * -dxxx: xxx is a DLL (.def)
1086 * -lxxx: xxx is an unsorted library
1087 * -oxxx: xxx is an object (.o)
1088 * -rxxx: xxx is a resource (.res)
1089 * -sxxx: xxx is a shared lib (.so)
1090 * -xlll: lll is the language (c, c++, etc.)
1093 output_file = xstrdup( opts->output_name ? opts->output_name : "a.out" );
1095 /* 'winegcc -o app xxx.exe.so' only creates the load script */
1096 if (opts->files.count == 1 && strendswith(opts->files.str[0], ".exe.so"))
1098 create_file(output_file, 0755, app_loader_template, opts->files.str[0]);
1099 return;
1102 /* generate app loader only for .exe */
1103 if (opts->shared || is_pe || strendswith(output_file, ".so"))
1104 generate_app_loader = 0;
1106 if (strendswith(output_file, ".fake")) opts->fake_module = 1;
1108 /* normalize the filename a bit: strip .so, ensure it has proper ext */
1109 if (!strchr(get_basename( output_file ), '.'))
1110 output_file = strmake("%s.%s", output_file, opts->shared ? "dll" : "exe");
1111 else if (strendswith(output_file, ".so"))
1112 output_file[strlen(output_file) - 3] = 0;
1113 output_path = is_pe ? output_file : strmake( "%s.so", output_file );
1115 /* get the filename from the path */
1116 output_name = get_basename( output_file );
1118 /* prepare the linking path */
1119 if (!opts->wine_objdir)
1121 char *lib_dir = get_lib_dir( opts );
1122 strarray_addall( &lib_dirs, opts->lib_dirs );
1123 strarray_add( &lib_dirs, strmake( "%s/wine%s", lib_dir, get_arch_dir( opts->target )));
1124 strarray_add( &lib_dirs, lib_dir );
1126 else
1128 strarray_add(&lib_dirs, strmake("%s/dlls", opts->wine_objdir));
1129 strarray_addall(&lib_dirs, opts->lib_dirs);
1132 /* mark the files with their appropriate type */
1133 spec_file = lang = 0;
1134 for ( j = 0; j < opts->files.count; j++ )
1136 const char* file = opts->files.str[j];
1137 if (file[0] != '-')
1139 switch(get_file_type(file))
1141 case file_def:
1142 case file_spec:
1143 if (spec_file)
1144 error("Only one spec file can be specified\n");
1145 spec_file = file;
1146 break;
1147 case file_rc:
1148 /* FIXME: invoke wrc to build it */
1149 error("Can't compile .rc file at the moment: %s\n", file);
1150 break;
1151 case file_res:
1152 strarray_add(&files, strmake("-r%s", file));
1153 break;
1154 case file_obj:
1155 strarray_add(&files, strmake("-o%s", file));
1156 break;
1157 case file_arh:
1158 if (opts->use_msvcrt)
1160 char *p = get_basename( file );
1161 if (!strncmp(p, "libmsvcr", 8) || !strncmp(p, "libucrt", 7)) crt_lib = file;
1163 strarray_add(&files, strmake("-a%s", file));
1164 break;
1165 case file_so:
1166 strarray_add(&files, strmake("-s%s", file));
1167 break;
1168 case file_na:
1169 error("File does not exist: %s\n", file);
1170 break;
1171 default:
1172 file = compile_to_object(opts, file, lang);
1173 strarray_add(&files, strmake("-o%s", file));
1174 break;
1177 else if (file[1] == 'l')
1178 add_library(opts, lib_dirs, &files, file + 2 );
1179 else if (file[1] == 'x')
1180 lang = file;
1181 else if(file[1] == 'W')
1182 strarray_add(&files, file);
1185 /* add the default libraries, if needed */
1187 if (!opts->wine_objdir && !opts->nodefaultlibs)
1189 if (opts->gui_app)
1191 add_library(opts, lib_dirs, &files, "shell32");
1192 add_library(opts, lib_dirs, &files, "comdlg32");
1193 add_library(opts, lib_dirs, &files, "gdi32");
1195 add_library(opts, lib_dirs, &files, "advapi32");
1196 add_library(opts, lib_dirs, &files, "user32");
1197 add_library(opts, lib_dirs, &files, "winecrt0");
1198 if (opts->use_msvcrt)
1200 if (!crt_lib)
1202 if (strncmp( output_name, "msvcr", 5 ) &&
1203 strncmp( output_name, "ucrt", 4 ) &&
1204 strcmp( output_name, "crtdll.dll" ))
1205 add_library(opts, lib_dirs, &files, "ucrtbase");
1207 else strarray_add(&files, strmake("-a%s", crt_lib));
1209 if (opts->win16_app) add_library(opts, lib_dirs, &files, "kernel");
1210 add_library(opts, lib_dirs, &files, "kernel32");
1211 add_library(opts, lib_dirs, &files, "ntdll");
1214 /* set default entry point, if needed */
1215 if (!opts->entry_point)
1217 if (opts->subsystem && !strcmp( opts->subsystem, "native" ))
1218 entry_point = (is_pe && opts->target.cpu == CPU_i386) ? "DriverEntry@8" : "DriverEntry";
1219 else if (opts->use_msvcrt && !opts->shared && !opts->win16_app)
1220 entry_point = opts->unicode_app ? "wmainCRTStartup" : "mainCRTStartup";
1222 else entry_point = opts->entry_point;
1224 /* run winebuild to generate the .spec.o file */
1225 if (opts->data_only)
1227 build_data_lib( opts, spec_file, output_file, files );
1228 return;
1231 for (i = 0; i < files.count; i++)
1232 if (files.str[i][1] == 'r') strarray_add( &resources, files.str[i] );
1234 build_spec_obj( opts, spec_file, output_file, opts->target_alias, files, resources, lib_dirs,
1235 entry_point, &spec_objs );
1236 if (opts->native_arch)
1238 const char *suffix = strchr( opts->target_alias, '-' );
1239 if (!suffix) suffix = "";
1240 build_spec_obj( opts, spec_file, output_file, strmake( "%s%s", opts->native_arch, suffix ),
1241 files, empty_strarray, lib_dirs, entry_point, &spec_objs );
1244 if (opts->fake_module) return; /* nothing else to do */
1246 /* link everything together now */
1247 link_args = get_link_args( opts, output_name );
1249 if (opts->nodefaultlibs || opts->use_msvcrt)
1251 switch (opts->target.platform)
1253 case PLATFORM_MINGW:
1254 case PLATFORM_CYGWIN:
1255 libgcc = find_libgcc( opts->prefix, link_args );
1256 if (!libgcc) libgcc = "-lgcc";
1257 break;
1258 default:
1259 break;
1263 strarray_add(&link_args, "-o");
1264 strarray_add(&link_args, output_path);
1266 for ( j = 0; j < lib_dirs.count; j++ )
1267 strarray_add(&link_args, strmake("-L%s", lib_dirs.str[j]));
1269 if (is_pe && opts->use_msvcrt && !entry_point && (opts->shared || opts->win16_app))
1270 entry_point = opts->target.cpu == CPU_i386 ? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1272 if (is_pe && entry_point)
1274 if (opts->target.platform == PLATFORM_WINDOWS)
1275 strarray_add(&link_args, strmake("-Wl,-entry:%s", entry_point));
1276 else
1277 strarray_add(&link_args, strmake("-Wl,--entry,%s%s",
1278 is_pe && opts->target.cpu == CPU_i386 ? "_" : "",
1279 entry_point));
1282 strarray_addall( &link_args, spec_objs );
1284 if (is_pe)
1286 for (j = 0; j < opts->delayimports.count; j++)
1288 if (opts->target.platform == PLATFORM_WINDOWS)
1289 strarray_add(&link_args, strmake("-Wl,-delayload:%s", opts->delayimports.str[j]));
1290 else
1291 strarray_add(&link_args, strmake("-Wl,-delayload,%s",opts->delayimports.str[j]));
1295 for ( j = 0; j < files.count; j++ )
1297 const char* name = files.str[j] + 2;
1298 switch(files.str[j][1])
1300 case 'l':
1301 strarray_add(&link_args, strmake("-l%s", name));
1302 break;
1303 case 's':
1304 case 'o':
1305 strarray_add(&link_args, name);
1306 break;
1307 case 'a':
1308 if (!opts->use_msvcrt && !opts->lib_suffix && strchr(name, '/'))
1310 /* turn the path back into -Ldir -lfoo options
1311 * this makes sure that we use the specified libs even
1312 * when mingw adds its own import libs to the link */
1313 const char *p = get_basename( name );
1315 if (is_pe)
1317 if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
1319 strarray_add(&link_args, strmake("-L%s", get_dirname(name) ));
1320 strarray_add(&link_args, strmake("-l%s", get_basename_noext( p + 3 )));
1321 break;
1324 else
1326 /* don't link to ntdll or ntoskrnl in non-msvcrt mode
1327 * since they export CRT functions */
1328 if (!strcmp( p, "libntdll.a" )) break;
1329 if (!strcmp( p, "libntoskrnl.a" )) break;
1332 strarray_add(&link_args, name);
1333 break;
1334 case 'W':
1335 strarray_add(&link_args, files.str[j]);
1336 break;
1340 if (!opts->nostdlib && !is_pe)
1342 strarray_add(&link_args, "-ldl");
1343 strarray_add(&link_args, "-lm");
1344 strarray_add(&link_args, "-lc");
1347 if (libgcc) strarray_add(&link_args, libgcc);
1349 output_file_name = output_path;
1350 output_debug_file = opts->debug_file;
1351 output_implib = opts->out_implib;
1352 atexit( cleanup_output_files );
1354 spawn(opts->prefix, link_args, 0);
1356 if (opts->debug_file && !strendswith(opts->debug_file, ".pdb"))
1358 struct strarray tool, objcopy = build_tool_name(opts, opts->target_alias, TOOL_OBJCOPY);
1360 tool = empty_strarray;
1361 strarray_addall( &tool, objcopy );
1362 strarray_add(&tool, "--only-keep-debug");
1363 strarray_add(&tool, output_path);
1364 strarray_add(&tool, opts->debug_file);
1365 spawn(opts->prefix, tool, 1);
1367 tool = empty_strarray;
1368 strarray_addall( &tool, objcopy );
1369 strarray_add(&tool, "--strip-debug");
1370 strarray_add(&tool, output_path);
1371 spawn(opts->prefix, tool, 1);
1373 tool = empty_strarray;
1374 strarray_addall( &tool, objcopy );
1375 strarray_add(&tool, "--add-gnu-debuglink");
1376 strarray_add(&tool, opts->debug_file);
1377 strarray_add(&tool, output_path);
1378 spawn(opts->prefix, tool, 0);
1381 if (opts->out_implib && !is_pe)
1383 struct strarray tool, implib_args;
1385 if (!spec_file)
1386 error("--out-implib requires a .spec or .def file\n");
1388 implib_args = get_winebuild_args( opts, opts->target_alias );
1389 tool = build_tool_name( opts, opts->target_alias, TOOL_CC );
1390 strarray_add( &implib_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
1391 tool = build_tool_name( opts, opts->target_alias, TOOL_LD );
1392 strarray_add( &implib_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
1394 strarray_add(&implib_args, "--implib");
1395 strarray_add(&implib_args, "-o");
1396 strarray_add(&implib_args, opts->out_implib);
1397 strarray_add(&implib_args, "--export");
1398 strarray_add(&implib_args, spec_file);
1400 spawn(opts->prefix, implib_args, 0);
1403 if (!is_pe) fixup_constructors( opts, output_path );
1404 else if (opts->wine_builtin) make_wine_builtin( opts, output_path );
1406 /* create the loader script */
1407 if (generate_app_loader)
1408 create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
1412 static void forward( struct options *opts )
1414 struct strarray args = get_translator(opts);
1416 strarray_addall(&args, opts->compiler_args);
1417 strarray_addall(&args, opts->linker_args);
1418 spawn(opts->prefix, args, 0);
1421 static int is_linker_arg(const char* arg)
1423 static const char* link_switches[] =
1425 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1426 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1427 "-fprofile-generate", "-fprofile-use"
1429 unsigned int j;
1431 switch (arg[1])
1433 case 'R':
1434 case 'z':
1435 case 'u':
1436 return 1;
1437 case 'W':
1438 if (strncmp("-Wl,", arg, 4) == 0) return 1;
1439 break;
1440 case 'X':
1441 if (strcmp("-Xlinker", arg) == 0) return 1;
1442 break;
1443 case 'a':
1444 if (strcmp("-arch", arg) == 0) return 1;
1445 break;
1446 case 'f':
1447 if (strncmp("-fuse-ld=", arg, 9) == 0) return 1;
1448 break;
1449 case 'r':
1450 if (strncmp("-rtlib=", arg, 7) == 0) return 1;
1451 break;
1454 for (j = 0; j < ARRAY_SIZE(link_switches); j++)
1455 if (strcmp(link_switches[j], arg) == 0) return 1;
1457 return 0;
1460 static void parse_target_option( struct options *opts, const char *target )
1462 opts->target_alias = xstrdup( target );
1463 if (!parse_target( target, &opts->target )) error( "Invalid target specification '%s'\n", target );
1466 static int is_option( struct options *opts, int i, const char *option, const char **option_arg )
1468 if (!strcmp( opts->args.str[i], option ))
1470 if (opts->args.count == i) error( "option %s requires an argument\n", opts->args.str[i] );
1471 *option_arg = opts->args.str[i + 1];
1472 return 1;
1474 if (!strncmp( opts->args.str[i], option, strlen(option) ) && opts->args.str[i][strlen(option)] == '=')
1476 *option_arg = opts->args.str[i] + strlen(option) + 1;
1477 return 1;
1479 return 0;
1482 int main(int argc, char **argv)
1484 int i, c, next_is_arg = 0, linking = 1;
1485 int raw_compiler_arg, raw_linker_arg, raw_winebuild_arg;
1486 const char* option_arg;
1487 struct options opts;
1488 char* lang = 0;
1489 char* str;
1491 init_signals( exit_on_signal );
1492 init_argv0_dir( argv[0] );
1494 /* setup tmp file removal at exit */
1495 atexit(clean_temp_files);
1497 /* initialize options */
1498 memset(&opts, 0, sizeof(opts));
1499 opts.target = init_argv0_target( argv[0] );
1500 opts.pic = 1;
1502 /* determine the processor type */
1503 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
1504 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
1506 for (i = 1; i < argc; i++)
1508 char *input_buffer = NULL, *iter, *opt, *out;
1509 struct stat st;
1510 int fd;
1512 if (argv[i][0] != '@' || (fd = open( argv[i] + 1, O_RDONLY | O_BINARY )) == -1)
1514 strarray_add( &opts.args, argv[i] );
1515 continue;
1517 if ((fstat( fd, &st ) == -1)) error( "Cannot stat %s\n", argv[i] + 1 );
1518 if (st.st_size)
1520 input_buffer = xmalloc( st.st_size + 1 );
1521 if (read( fd, input_buffer, st.st_size ) != st.st_size) error( "Cannot read %s\n", argv[i] + 1 );
1523 close( fd );
1524 for (iter = input_buffer; iter < input_buffer + st.st_size; iter++)
1526 char quote = 0;
1527 while (iter < input_buffer + st.st_size && isspace(*iter)) iter++;
1528 if (iter == input_buffer + st.st_size) break;
1529 opt = out = iter;
1530 while (iter < input_buffer + st.st_size && (quote || !isspace(*iter)))
1532 if (*iter == quote)
1534 iter++;
1535 quote = 0;
1537 else if (*iter == '\'' || *iter == '"') quote = *iter++;
1538 else
1540 if (*iter == '\\' && iter + 1 < input_buffer + st.st_size) iter++;
1541 *out++ = *iter++;
1544 *out = 0;
1545 strarray_add( &opts.args, opt );
1549 /* parse options */
1550 for (i = 0; i < opts.args.count; i++)
1552 if (opts.args.str[i][0] == '-' && opts.args.str[i][1]) /* option, except '-' alone is stdin, which is a file */
1554 /* determine if this switch is followed by a separate argument */
1555 next_is_arg = 0;
1556 option_arg = 0;
1557 switch(opts.args.str[i][1])
1559 case 'x': case 'o': case 'D': case 'U':
1560 case 'I': case 'A': case 'l': case 'u':
1561 case 'b': case 'V': case 'G': case 'L':
1562 case 'B': case 'R': case 'z':
1563 if (opts.args.str[i][2]) option_arg = &opts.args.str[i][2];
1564 else next_is_arg = 1;
1565 break;
1566 case 'i':
1567 next_is_arg = 1;
1568 break;
1569 case 'a':
1570 if (strcmp("-aux-info", opts.args.str[i]) == 0)
1571 next_is_arg = 1;
1572 if (strcmp("-arch", opts.args.str[i]) == 0)
1573 next_is_arg = 1;
1574 break;
1575 case 'X':
1576 if (strcmp("-Xlinker", opts.args.str[i]) == 0)
1577 next_is_arg = 1;
1578 break;
1579 case 'M':
1580 c = opts.args.str[i][2];
1581 if (c == 'F' || c == 'T' || c == 'Q')
1583 if (opts.args.str[i][3]) option_arg = &opts.args.str[i][3];
1584 else next_is_arg = 1;
1586 break;
1587 case 'f':
1588 if (strcmp("-framework", opts.args.str[i]) == 0)
1589 next_is_arg = 1;
1590 break;
1591 case 't':
1592 next_is_arg = strcmp("-target", opts.args.str[i]) == 0;
1593 break;
1594 case '-':
1595 next_is_arg = (strcmp("--param", opts.args.str[i]) == 0 ||
1596 strcmp("--sysroot", opts.args.str[i]) == 0 ||
1597 strcmp("--target", opts.args.str[i]) == 0 ||
1598 strcmp("--wine-objdir", opts.args.str[i]) == 0 ||
1599 strcmp("--winebuild", opts.args.str[i]) == 0 ||
1600 strcmp("--lib-suffix", opts.args.str[i]) == 0);
1601 break;
1603 if (next_is_arg)
1605 if (i + 1 >= opts.args.count) error("option -%c requires an argument\n", opts.args.str[i][1]);
1606 option_arg = opts.args.str[i+1];
1609 /* determine what options go 'as is' to the linker & the compiler */
1610 raw_linker_arg = is_linker_arg(opts.args.str[i]);
1611 raw_compiler_arg = !raw_linker_arg;
1612 raw_winebuild_arg = 0;
1614 /* do a bit of semantic analysis */
1615 switch (opts.args.str[i][1])
1617 case 'B':
1618 str = xstrdup(option_arg);
1619 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
1620 strarray_add(&opts.prefix, str);
1621 raw_linker_arg = 1;
1622 break;
1623 case 'b':
1624 parse_target_option( &opts, option_arg );
1625 raw_compiler_arg = 0;
1626 break;
1627 case 'V':
1628 opts.version = xstrdup( option_arg );
1629 raw_compiler_arg = 0;
1630 break;
1631 case 'c': /* compile or assemble */
1632 raw_compiler_arg = 0;
1633 if (opts.args.str[i][2] == 0) opts.compile_only = 1;
1634 /* fall through */
1635 case 'S': /* generate assembler code */
1636 case 'E': /* preprocess only */
1637 if (opts.args.str[i][2] == 0) linking = 0;
1638 break;
1639 case 'f':
1640 if (strcmp("-fno-short-wchar", opts.args.str[i]) == 0)
1641 opts.noshortwchar = 1;
1642 else if (!strcmp("-fasynchronous-unwind-tables", opts.args.str[i]))
1643 opts.unwind_tables = 1;
1644 else if (!strcmp("-fno-asynchronous-unwind-tables", opts.args.str[i]))
1645 opts.unwind_tables = 0;
1646 else if (!strcmp("-fPIC", opts.args.str[i]) || !strcmp("-fpic", opts.args.str[i]))
1647 opts.pic = 1;
1648 else if (!strcmp("-fno-PIC", opts.args.str[i]) || !strcmp("-fno-pic", opts.args.str[i]))
1649 opts.pic = 0;
1650 break;
1651 case 'i':
1652 if (!strcmp( "-isysroot", opts.args.str[i] )) opts.isysroot = opts.args.str[i + 1];
1653 break;
1654 case 'l':
1655 strarray_add(&opts.files, strmake("-l%s", option_arg));
1656 raw_compiler_arg = 0;
1657 break;
1658 case 'L':
1659 strarray_add(&opts.lib_dirs, option_arg);
1660 raw_compiler_arg = 0;
1661 break;
1662 case 'M': /* map file generation */
1663 linking = 0;
1664 break;
1665 case 'm':
1666 if (strcmp("-mno-cygwin", opts.args.str[i]) == 0)
1668 opts.use_msvcrt = 1;
1669 raw_compiler_arg = 0;
1671 if (strcmp("-mcygwin", opts.args.str[i]) == 0)
1673 opts.use_msvcrt = 0;
1674 raw_compiler_arg = 0;
1676 else if (strcmp("-mwindows", opts.args.str[i]) == 0)
1678 opts.gui_app = 1;
1679 raw_compiler_arg = 0;
1681 else if (strcmp("-mconsole", opts.args.str[i]) == 0)
1683 opts.gui_app = 0;
1684 raw_compiler_arg = 0;
1686 else if (strcmp("-municode", opts.args.str[i]) == 0)
1688 opts.unicode_app = 1;
1689 raw_compiler_arg = 0;
1690 raw_winebuild_arg = 1;
1692 else if (strcmp("-mthreads", opts.args.str[i]) == 0)
1694 raw_compiler_arg = 0;
1696 else if (strcmp("-m16", opts.args.str[i]) == 0)
1698 opts.win16_app = 1;
1699 raw_compiler_arg = 0;
1700 raw_winebuild_arg = 1;
1702 else if (strcmp("-m32", opts.args.str[i]) == 0)
1704 set_target_ptr_size( &opts.target, 4 );
1705 opts.force_pointer_size = 4;
1706 raw_linker_arg = 1;
1708 else if (strcmp("-m64", opts.args.str[i]) == 0)
1710 set_target_ptr_size( &opts.target, 8 );
1711 opts.force_pointer_size = 8;
1712 raw_linker_arg = 1;
1714 else if (!strcmp("-marm", opts.args.str[i] ) || !strcmp("-mthumb", opts.args.str[i] ))
1716 raw_linker_arg = 1;
1718 else if (!strcmp("-marm64x", opts.args.str[i] ))
1720 raw_linker_arg = 1;
1721 opts.native_arch = "aarch64";
1723 else if (!strncmp("-mcpu=", opts.args.str[i], 6) ||
1724 !strncmp("-mfpu=", opts.args.str[i], 6) ||
1725 !strncmp("-march=", opts.args.str[i], 7))
1726 raw_winebuild_arg = 1;
1727 break;
1728 case 'n':
1729 if (strcmp("-nostdinc", opts.args.str[i]) == 0)
1730 opts.nostdinc = 1;
1731 else if (strcmp("-nodefaultlibs", opts.args.str[i]) == 0)
1732 opts.nodefaultlibs = 1;
1733 else if (strcmp("-nostdlib", opts.args.str[i]) == 0)
1734 opts.nostdlib = 1;
1735 else if (strcmp("-nostartfiles", opts.args.str[i]) == 0)
1736 opts.nostartfiles = 1;
1737 break;
1738 case 'o':
1739 opts.output_name = option_arg;
1740 raw_compiler_arg = 0;
1741 break;
1742 case 'p':
1743 if (strcmp("-pthread", opts.args.str[i]) == 0)
1745 raw_compiler_arg = 1;
1746 raw_linker_arg = 1;
1748 break;
1749 case 's':
1750 if (strcmp("-static", opts.args.str[i]) == 0)
1751 linking = -1;
1752 else if(strcmp("-save-temps", opts.args.str[i]) == 0)
1753 keep_generated = 1;
1754 else if (strncmp("-specs=", opts.args.str[i], 7) == 0)
1755 raw_linker_arg = 1;
1756 else if(strcmp("-shared", opts.args.str[i]) == 0)
1758 opts.shared = 1;
1759 raw_compiler_arg = raw_linker_arg = 0;
1761 else if (strcmp("-s", opts.args.str[i]) == 0 && opts.target.platform == PLATFORM_APPLE)
1763 /* On Mac, change -s into -Wl,-x. ld's -s switch
1764 * is deprecated, and it doesn't work on Tiger with
1765 * MH_BUNDLEs anyway
1767 opts.strip = 1;
1768 raw_linker_arg = 0;
1770 break;
1771 case 't':
1772 if (is_option( &opts, i, "-target", &option_arg ))
1774 parse_target_option( &opts, option_arg );
1775 raw_compiler_arg = raw_linker_arg = 0;
1777 break;
1778 case 'v':
1779 if (opts.args.str[i][2] == 0) verbose++;
1780 break;
1781 case 'W':
1782 if (strncmp("-Wl,", opts.args.str[i], 4) == 0)
1784 unsigned int j;
1785 struct strarray Wl = strarray_fromstring(opts.args.str[i] + 4, ",");
1786 for (j = 0; j < Wl.count; j++)
1788 if (!strcmp(Wl.str[j], "--image-base") && j < Wl.count - 1)
1790 opts.image_base = xstrdup( Wl.str[++j] );
1791 continue;
1793 if (!strcmp(Wl.str[j], "--section-alignment") && j < Wl.count - 1)
1795 opts.section_align = xstrdup( Wl.str[++j] );
1796 continue;
1798 if (!strcmp(Wl.str[j], "--file-alignment") && j < Wl.count - 1)
1800 opts.file_align = xstrdup( Wl.str[++j] );
1801 continue;
1803 if (!strcmp(Wl.str[j], "--large-address-aware"))
1805 opts.large_address_aware = 1;
1806 continue;
1808 if (!strcmp(Wl.str[j], "--wine-builtin"))
1810 opts.wine_builtin = 1;
1811 continue;
1813 if (!strcmp(Wl.str[j], "--subsystem") && j < Wl.count - 1)
1815 opts.subsystem = xstrdup( Wl.str[++j] );
1816 continue;
1818 if (!strcmp(Wl.str[j], "--entry") && j < Wl.count - 1)
1820 opts.entry_point = xstrdup( Wl.str[++j] );
1821 continue;
1823 if (!strcmp(Wl.str[j], "-delayload") && j < Wl.count - 1)
1825 strarray_add( &opts.delayimports, Wl.str[++j] );
1826 continue;
1828 if (!strcmp(Wl.str[j], "--debug-file") && j < Wl.count - 1)
1830 opts.debug_file = xstrdup( Wl.str[++j] );
1831 continue;
1833 if (!strcmp(Wl.str[j], "--whole-archive") ||
1834 !strcmp(Wl.str[j], "--no-whole-archive") ||
1835 !strcmp(Wl.str[j], "--start-group") ||
1836 !strcmp(Wl.str[j], "--end-group"))
1838 strarray_add( &opts.files, strmake( "-Wl,%s", Wl.str[j] ));
1839 continue;
1841 if (!strcmp(Wl.str[j], "--out-implib"))
1843 opts.out_implib = xstrdup( Wl.str[++j] );
1844 continue;
1846 if (!strcmp(Wl.str[j], "-static")) linking = -1;
1847 strarray_add(&opts.linker_args, strmake("-Wl,%s",Wl.str[j]));
1849 raw_compiler_arg = raw_linker_arg = 0;
1851 else if (strncmp("-Wb,", opts.args.str[i], 4) == 0)
1853 unsigned int j;
1854 struct strarray Wb = strarray_fromstring(opts.args.str[i] + 4, ",");
1855 for (j = 0; j < Wb.count; j++)
1857 if (!strcmp(Wb.str[j], "--data-only")) opts.data_only = 1;
1858 if (!strcmp(Wb.str[j], "--fake-module")) opts.fake_module = 1;
1859 else strarray_add( &opts.winebuild_args, Wb.str[j] );
1861 raw_compiler_arg = raw_linker_arg = 0;
1863 break;
1864 case 'x':
1865 lang = strmake("-x%s", option_arg);
1866 strarray_add(&opts.files, lang);
1867 /* we'll pass these flags ourselves, explicitly */
1868 raw_compiler_arg = raw_linker_arg = 0;
1869 break;
1870 case '-':
1871 if (strcmp("-static", opts.args.str[i]+1) == 0)
1872 linking = -1;
1873 else if (!strcmp( "-no-default-config", opts.args.str[i] + 1 ))
1875 opts.no_default_config = 1;
1876 raw_compiler_arg = raw_linker_arg = 1;
1878 else if (is_option( &opts, i, "--sysroot", &option_arg ))
1880 opts.sysroot = option_arg;
1881 raw_linker_arg = 1;
1883 else if (is_option( &opts, i, "--target", &option_arg ))
1885 parse_target_option( &opts, option_arg );
1886 raw_compiler_arg = raw_linker_arg = 0;
1888 else if (is_option( &opts, i, "--wine-objdir", &option_arg ))
1890 opts.wine_objdir = option_arg;
1891 raw_compiler_arg = raw_linker_arg = 0;
1893 else if (is_option( &opts, i, "--winebuild", &option_arg ))
1895 opts.winebuild = option_arg;
1896 raw_compiler_arg = raw_linker_arg = 0;
1898 else if (is_option( &opts, i, "--lib-suffix", &option_arg ))
1900 opts.lib_suffix = option_arg;
1901 raw_compiler_arg = raw_linker_arg = 0;
1903 break;
1906 /* put the arg into the appropriate bucket */
1907 if (raw_linker_arg)
1909 strarray_add( &opts.linker_args, opts.args.str[i] );
1910 if (next_is_arg && (i + 1 < opts.args.count))
1911 strarray_add( &opts.linker_args, opts.args.str[i + 1] );
1913 if (raw_compiler_arg)
1915 strarray_add( &opts.compiler_args, opts.args.str[i] );
1916 if (next_is_arg && (i + 1 < opts.args.count))
1917 strarray_add( &opts.compiler_args, opts.args.str[i + 1] );
1919 if (raw_winebuild_arg)
1921 strarray_add( &opts.winebuild_args, opts.args.str[i] );
1922 if (next_is_arg && (i + 1 < opts.args.count))
1923 strarray_add( &opts.winebuild_args, opts.args.str[i + 1] );
1926 /* skip the next token if it's an argument */
1927 if (next_is_arg) i++;
1929 else
1931 strarray_add( &opts.files, opts.args.str[i] );
1935 if (opts.force_pointer_size) set_target_ptr_size( &opts.target, opts.force_pointer_size );
1936 if (opts.processor == proc_cpp) linking = 0;
1937 if (linking == -1) error("Static linking is not supported\n");
1939 if (is_pe_target( &opts )) opts.use_msvcrt = 1;
1941 if (opts.files.count == 0 && !opts.fake_module) forward(&opts);
1942 else if (linking) build(&opts);
1943 else compile(&opts, lang);
1945 output_file_name = NULL;
1946 output_debug_file = NULL;
1947 output_implib = NULL;
1948 return 0;