include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / tools / winegcc / winegcc.c
bloba6b91a4edc65e5a10553026ed18c92af762e53bf
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 compile(struct options* opts, const char* lang)
616 struct strarray comp_args = get_translator(opts);
617 unsigned int i, j;
618 int gcc_defs = 0;
619 struct strarray gcc;
620 struct strarray gpp;
622 if (opts->force_pointer_size)
623 strarray_add( &comp_args, strmake("-m%u", 8 * opts->force_pointer_size ) );
624 switch(opts->processor)
626 case proc_cpp: gcc_defs = 1; break;
627 case proc_as: gcc_defs = 0; break;
628 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
629 /* mixing different C and C++ compilers isn't supported in configure anyway */
630 case proc_cc:
631 case proc_cxx:
632 gcc = build_tool_name( opts, opts->target_alias, TOOL_CC );
633 gpp = build_tool_name( opts, opts->target_alias, TOOL_CXX );
634 for ( j = 0; !gcc_defs && j < comp_args.count; j++ )
636 const char *cc = comp_args.str[j];
638 for (i = 0; !gcc_defs && i < gcc.count; i++)
639 gcc_defs = gcc.str[i][0] != '-' && strendswith(cc, gcc.str[i]);
640 for (i = 0; !gcc_defs && i < gpp.count; i++)
641 gcc_defs = gpp.str[i][0] != '-' && strendswith(cc, gpp.str[i]);
643 break;
646 if (opts->target.platform == PLATFORM_WINDOWS ||
647 opts->target.platform == PLATFORM_CYGWIN ||
648 opts->target.platform == PLATFORM_MINGW)
649 goto no_compat_defines;
651 if (opts->processor != proc_cpp)
653 if (gcc_defs && !opts->wine_objdir && !opts->noshortwchar)
655 strarray_add(&comp_args, "-fshort-wchar");
656 strarray_add(&comp_args, "-DWINE_UNICODE_NATIVE");
658 strarray_add(&comp_args, "-D_REENTRANT");
659 if (opts->pic)
660 strarray_add(&comp_args, "-fPIC");
661 else
662 strarray_add(&comp_args, "-fno-PIC");
665 if (get_target_ptr_size( opts->target ) == 8)
667 strarray_add(&comp_args, "-DWIN64");
668 strarray_add(&comp_args, "-D_WIN64");
669 strarray_add(&comp_args, "-D__WIN64");
670 strarray_add(&comp_args, "-D__WIN64__");
673 strarray_add(&comp_args, "-DWIN32");
674 strarray_add(&comp_args, "-D_WIN32");
675 strarray_add(&comp_args, "-D__WIN32");
676 strarray_add(&comp_args, "-D__WIN32__");
677 strarray_add(&comp_args, "-D__WINNT");
678 strarray_add(&comp_args, "-D__WINNT__");
680 if (gcc_defs)
682 switch (opts->target.cpu)
684 case CPU_x86_64:
685 case CPU_ARM64:
686 strarray_add(&comp_args, "-D__stdcall=__attribute__((ms_abi))");
687 strarray_add(&comp_args, "-D__cdecl=__stdcall");
688 strarray_add(&comp_args, "-D__fastcall=__stdcall");
689 break;
690 case CPU_i386:
691 strarray_add(&comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
692 strarray_add(&comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
693 strarray_add(&comp_args, "-D__fastcall=__attribute__((__fastcall__))");
694 break;
695 case CPU_ARM:
696 strarray_add(&comp_args, "-D__stdcall=__attribute__((pcs(\"aapcs-vfp\")))");
697 strarray_add(&comp_args, "-D__cdecl=__stdcall");
698 strarray_add(&comp_args, "-D__fastcall=__stdcall");
699 break;
700 case CPU_ARM64EC:
701 break;
703 strarray_add(&comp_args, "-D_stdcall=__stdcall");
704 strarray_add(&comp_args, "-D_cdecl=__cdecl");
705 strarray_add(&comp_args, "-D_fastcall=__fastcall");
706 strarray_add(&comp_args, "-D__declspec(x)=__declspec_##x");
707 strarray_add(&comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
708 strarray_add(&comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
709 strarray_add(&comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
710 strarray_add(&comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
711 strarray_add(&comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
712 strarray_add(&comp_args, "-D__declspec_naked=__attribute__((naked))");
713 strarray_add(&comp_args, "-D__declspec_noinline=__attribute__((noinline))");
714 strarray_add(&comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
715 strarray_add(&comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
716 strarray_add(&comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
717 strarray_add(&comp_args, "-D__declspec_selectany=__attribute__((weak))");
718 strarray_add(&comp_args, "-D__declspec_thread=__thread");
721 strarray_add(&comp_args, "-D__int8=char");
722 strarray_add(&comp_args, "-D__int16=short");
723 strarray_add(&comp_args, "-D__int32=int");
724 if (get_target_ptr_size( opts->target ) == 8)
725 strarray_add(&comp_args, "-D__int64=long");
726 else
727 strarray_add(&comp_args, "-D__int64=long long");
729 no_compat_defines:
730 strarray_add(&comp_args, "-D__WINE__");
732 /* options we handle explicitly */
733 if (opts->compile_only)
734 strarray_add(&comp_args, "-c");
735 if (opts->output_name)
737 strarray_add(&comp_args, "-o");
738 strarray_add(&comp_args, opts->output_name);
741 /* the rest of the pass-through parameters */
742 strarray_addall(&comp_args, opts->compiler_args);
744 /* the language option, if any */
745 if (lang && strcmp(lang, "-xnone"))
746 strarray_add(&comp_args, lang);
748 /* last, but not least, the files */
749 for ( j = 0; j < opts->files.count; j++ )
751 if (opts->files.str[j][0] != '-' || !opts->files.str[j][1]) /* not an option or bare '-' (i.e. stdin) */
752 strarray_add(&comp_args, opts->files.str[j]);
755 /* standard includes come last in the include search path */
756 if (!opts->wine_objdir && !opts->nostdinc)
758 const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
759 const char *root = opts->isysroot ? opts->isysroot : opts->sysroot ? opts->sysroot : "";
760 const char *isystem = gcc_defs ? "-isystem" : "-I";
761 const char *idirafter = gcc_defs ? "-idirafter" : "-I";
763 if (opts->use_msvcrt)
765 if (includedir) strarray_add( &comp_args, strmake( "%s%s/wine/msvcrt", isystem, includedir ));
766 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
768 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
769 strarray_add(&comp_args, strmake( "%s%s%s/wine/msvcrt", isystem, root, incl_dirs[j] ));
771 strarray_add(&comp_args, "-D__MSVCRT__");
773 if (includedir)
775 strarray_add( &comp_args, strmake( "%s%s/wine/windows", isystem, includedir ));
776 strarray_add( &comp_args, strmake( "%s%s", idirafter, includedir ));
778 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
780 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
781 strarray_add(&comp_args, strmake( "%s%s%s/wine/windows", isystem, root, incl_dirs[j] ));
782 strarray_add(&comp_args, strmake( "%s%s%s", idirafter, root, incl_dirs[j] ));
785 else if (opts->wine_objdir)
786 strarray_add(&comp_args, strmake("-I%s/include", opts->wine_objdir) );
788 spawn(opts->prefix, comp_args, 0);
791 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
793 struct options copts;
795 /* make a copy so we don't change any of the initial stuff */
796 /* a shallow copy is exactly what we want in this case */
797 copts = *opts;
798 copts.output_name = make_temp_file(get_basename_noext(file), ".o");
799 copts.compile_only = 1;
800 copts.files = empty_strarray;
801 strarray_add(&copts.files, file);
802 compile(&copts, lang);
803 return copts.output_name;
806 /* return the initial set of options needed to run winebuild */
807 static struct strarray get_winebuild_args( struct options *opts, const char *target )
809 const char* winebuild = getenv("WINEBUILD");
810 const char *binary = NULL;
811 struct strarray spec_args = empty_strarray;
812 unsigned int i;
814 if (opts->winebuild)
815 binary = opts->winebuild;
816 else if (opts->wine_objdir)
817 binary = strmake( "%s/tools/winebuild/winebuild%s", opts->wine_objdir, EXEEXT );
818 else if (winebuild)
819 binary = find_binary( opts->prefix, winebuild );
820 else if (bindir)
821 binary = strmake( "%s/winebuild%s", bindir, EXEEXT );
822 else
823 binary = find_binary( opts->prefix, "winebuild" );
824 if (!binary) error( "Could not find winebuild\n" );
825 strarray_add( &spec_args, binary );
826 if (verbose) strarray_add( &spec_args, "-v" );
827 if (keep_generated) strarray_add( &spec_args, "--save-temps" );
828 if (target)
830 strarray_add( &spec_args, "--target" );
831 strarray_add( &spec_args, target );
833 if (opts->force_pointer_size)
834 strarray_add(&spec_args, strmake("-m%u", 8 * opts->force_pointer_size ));
835 for (i = 0; i < opts->prefix.count; i++)
836 strarray_add( &spec_args, strmake( "-B%s", opts->prefix.str[i] ));
837 strarray_addall( &spec_args, opts->winebuild_args );
838 return spec_args;
841 static void fixup_constructors( struct options *opts, const char *file )
843 struct strarray args = get_winebuild_args( opts, opts->target_alias );
845 strarray_add( &args, "--fixup-ctors" );
846 strarray_add( &args, file );
847 spawn( opts->prefix, args, 0 );
850 static void make_wine_builtin( struct options *opts, const char *file )
852 struct strarray args = get_winebuild_args( opts, opts->target_alias );
854 strarray_add( &args, "--builtin" );
855 strarray_add( &args, file );
856 spawn( opts->prefix, args, 0 );
859 /* check if there is a static lib associated to a given dll */
860 static char *find_static_lib( const char *dll )
862 char *lib = strmake("%s.a", dll);
863 if (get_file_type(lib) == file_arh) return lib;
864 free( lib );
865 return NULL;
868 static const char *find_libgcc(struct strarray prefix, struct strarray link_tool)
870 const char *out = make_temp_file( "find_libgcc", ".out" );
871 const char *err = make_temp_file( "find_libgcc", ".err" );
872 struct strarray link = empty_strarray;
873 int sout = -1, serr = -1;
874 char *libgcc, *p;
875 struct stat st;
876 size_t cnt;
877 int ret;
879 strarray_addall( &link, link_tool );
880 strarray_add( &link, "-print-libgcc-file-name" );
882 sout = dup( fileno(stdout) );
883 freopen( out, "w", stdout );
884 serr = dup( fileno(stderr) );
885 freopen( err, "w", stderr );
886 ret = spawn( prefix, link, 1 );
887 if (sout >= 0)
889 dup2( sout, fileno(stdout) );
890 close( sout );
892 if (serr >= 0)
894 dup2( serr, fileno(stderr) );
895 close( serr );
898 if (ret || stat(out, &st) || !st.st_size) return NULL;
900 libgcc = xmalloc(st.st_size + 1);
901 sout = open(out, O_RDONLY);
902 if (sout == -1) return NULL;
903 cnt = read(sout, libgcc, st.st_size);
904 close(sout);
905 libgcc[cnt] = 0;
906 if ((p = strchr(libgcc, '\n'))) *p = 0;
907 return libgcc;
911 /* add specified library to the list of files */
912 static void add_library( struct options *opts, struct strarray lib_dirs,
913 struct strarray *files, const char *library )
915 char *static_lib, *fullname = 0;
917 switch(get_lib_type(opts->target, lib_dirs, library, "lib", opts->lib_suffix, &fullname))
919 case file_arh:
920 strarray_add(files, strmake("-a%s", fullname));
921 break;
922 case file_dll:
923 strarray_add(files, strmake("-d%s", fullname));
924 if ((static_lib = find_static_lib(fullname)))
926 strarray_add(files, strmake("-a%s",static_lib));
927 free(static_lib);
929 break;
930 case file_so:
931 default:
932 /* keep it anyway, the linker may know what to do with it */
933 strarray_add(files, strmake("-l%s", library));
934 break;
936 free(fullname);
939 /* run winebuild to generate the .spec.o file */
940 static void build_spec_obj( struct options *opts, const char *spec_file, const char *output_file,
941 const char *target, struct strarray files, struct strarray resources,
942 struct strarray lib_dirs, const char *entry_point, struct strarray *spec_objs )
944 unsigned int i;
945 int is_pe = is_pe_target( opts );
946 struct strarray spec_args = get_winebuild_args( opts, target );
947 struct strarray tool;
948 const char *spec_o_name, *output_name;
950 /* get the filename from the path */
951 output_name = get_basename( output_file );
953 tool = build_tool_name( opts, target, TOOL_CC );
954 strarray_add( &spec_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
955 if (!is_pe)
957 tool = build_tool_name( opts, target, TOOL_LD );
958 strarray_add( &spec_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
961 spec_o_name = make_temp_file(output_name, ".spec.o");
962 if (!is_pe)
964 if (opts->pic) strarray_add(&spec_args, "-fPIC");
965 if (opts->use_msvcrt) strarray_add(&spec_args, "-mno-cygwin");
966 if (opts->unwind_tables) strarray_add( &spec_args, "-fasynchronous-unwind-tables" );
968 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
969 if (opts->fake_module)
971 strarray_add(&spec_args, "--fake-module");
972 strarray_add(&spec_args, "-o");
973 strarray_add(&spec_args, output_file);
975 else
977 strarray_add(&spec_args, "-o");
978 strarray_add(&spec_args, spec_o_name);
980 if (spec_file)
982 strarray_add(&spec_args, "-E");
983 strarray_add(&spec_args, spec_file);
986 if (!opts->shared)
988 strarray_add(&spec_args, "-F");
989 strarray_add(&spec_args, output_name);
990 strarray_add(&spec_args, "--subsystem");
991 strarray_add(&spec_args, opts->gui_app ? "windows" : "console");
992 if (opts->large_address_aware) strarray_add( &spec_args, "--large-address-aware" );
995 if (opts->target.platform == PLATFORM_WINDOWS && opts->target.cpu == CPU_i386)
996 strarray_add(&spec_args, "--safeseh");
998 if (entry_point)
1000 strarray_add(&spec_args, "--entry");
1001 strarray_add(&spec_args, entry_point);
1004 if (opts->subsystem)
1006 strarray_add(&spec_args, "--subsystem");
1007 strarray_add(&spec_args, opts->subsystem);
1010 for (i = 0; i < lib_dirs.count; i++)
1011 strarray_add(&spec_args, strmake("-L%s", lib_dirs.str[i]));
1013 if (!is_pe)
1015 for (i = 0; i < opts->delayimports.count; i++)
1016 strarray_add(&spec_args, strmake("-d%s", opts->delayimports.str[i]));
1019 strarray_addall( &spec_args, resources );
1021 /* add other files */
1022 strarray_add(&spec_args, "--");
1023 for (i = 0; i < files.count; i++)
1025 switch(files.str[i][1])
1027 case 'd':
1028 case 'a':
1029 case 'o':
1030 strarray_add(&spec_args, files.str[i] + 2);
1031 break;
1035 spawn(opts->prefix, spec_args, 0);
1036 strarray_add( spec_objs, spec_o_name );
1039 /* run winebuild to generate a data-only library */
1040 static void build_data_lib( struct options *opts, const char *spec_file, const char *output_file, struct strarray files )
1042 unsigned int i;
1043 struct strarray spec_args = get_winebuild_args( opts, opts->target_alias );
1045 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
1046 strarray_add(&spec_args, "-o");
1047 strarray_add(&spec_args, output_file);
1048 if (spec_file)
1050 strarray_add(&spec_args, "-E");
1051 strarray_add(&spec_args, spec_file);
1054 /* add resource files */
1055 for (i = 0; i < files.count; i++)
1056 if (files.str[i][1] == 'r') strarray_add(&spec_args, files.str[i]);
1058 spawn(opts->prefix, spec_args, 0);
1061 static void build(struct options* opts)
1063 struct strarray resources = empty_strarray;
1064 struct strarray spec_objs = empty_strarray;
1065 struct strarray lib_dirs = empty_strarray;
1066 struct strarray files = empty_strarray;
1067 struct strarray link_args;
1068 char *output_file, *output_path;
1069 const char *output_name, *spec_file, *lang;
1070 const char *libgcc = NULL;
1071 int generate_app_loader = 1;
1072 const char *crt_lib = NULL, *entry_point = NULL;
1073 int is_pe = is_pe_target( opts );
1074 unsigned int i, j;
1076 /* NOTE: for the files array we'll use the following convention:
1077 * -axxx: xxx is an archive (.a)
1078 * -dxxx: xxx is a DLL (.def)
1079 * -lxxx: xxx is an unsorted library
1080 * -oxxx: xxx is an object (.o)
1081 * -rxxx: xxx is a resource (.res)
1082 * -sxxx: xxx is a shared lib (.so)
1083 * -xlll: lll is the language (c, c++, etc.)
1086 output_file = xstrdup( opts->output_name ? opts->output_name : "a.out" );
1088 /* 'winegcc -o app xxx.exe.so' only creates the load script */
1089 if (opts->files.count == 1 && strendswith(opts->files.str[0], ".exe.so"))
1091 create_file(output_file, 0755, app_loader_template, opts->files.str[0]);
1092 return;
1095 /* generate app loader only for .exe */
1096 if (opts->shared || is_pe || strendswith(output_file, ".so"))
1097 generate_app_loader = 0;
1099 if (strendswith(output_file, ".fake")) opts->fake_module = 1;
1101 /* normalize the filename a bit: strip .so, ensure it has proper ext */
1102 if (!strchr(get_basename( output_file ), '.'))
1103 output_file = strmake("%s.%s", output_file, opts->shared ? "dll" : "exe");
1104 else if (strendswith(output_file, ".so"))
1105 output_file[strlen(output_file) - 3] = 0;
1106 output_path = is_pe ? output_file : strmake( "%s.so", output_file );
1108 /* get the filename from the path */
1109 output_name = get_basename( output_file );
1111 /* prepare the linking path */
1112 if (!opts->wine_objdir)
1114 char *lib_dir = get_lib_dir( opts );
1115 strarray_addall( &lib_dirs, opts->lib_dirs );
1116 strarray_add( &lib_dirs, strmake( "%s/wine%s", lib_dir, get_arch_dir( opts->target )));
1117 strarray_add( &lib_dirs, lib_dir );
1119 else
1121 strarray_add(&lib_dirs, strmake("%s/dlls", opts->wine_objdir));
1122 strarray_addall(&lib_dirs, opts->lib_dirs);
1125 /* mark the files with their appropriate type */
1126 spec_file = lang = 0;
1127 for ( j = 0; j < opts->files.count; j++ )
1129 const char* file = opts->files.str[j];
1130 if (file[0] != '-')
1132 switch(get_file_type(file))
1134 case file_def:
1135 case file_spec:
1136 if (spec_file)
1137 error("Only one spec file can be specified\n");
1138 spec_file = file;
1139 break;
1140 case file_rc:
1141 /* FIXME: invoke wrc to build it */
1142 error("Can't compile .rc file at the moment: %s\n", file);
1143 break;
1144 case file_res:
1145 strarray_add(&files, strmake("-r%s", file));
1146 break;
1147 case file_obj:
1148 strarray_add(&files, strmake("-o%s", file));
1149 break;
1150 case file_arh:
1151 if (opts->use_msvcrt)
1153 char *p = get_basename( file );
1154 if (!strncmp(p, "libmsvcr", 8) || !strncmp(p, "libucrt", 7)) crt_lib = file;
1156 strarray_add(&files, strmake("-a%s", file));
1157 break;
1158 case file_so:
1159 strarray_add(&files, strmake("-s%s", file));
1160 break;
1161 case file_na:
1162 error("File does not exist: %s\n", file);
1163 break;
1164 default:
1165 file = compile_to_object(opts, file, lang);
1166 strarray_add(&files, strmake("-o%s", file));
1167 break;
1170 else if (file[1] == 'l')
1171 add_library(opts, lib_dirs, &files, file + 2 );
1172 else if (file[1] == 'x')
1173 lang = file;
1174 else if(file[1] == 'W')
1175 strarray_add(&files, file);
1178 /* add the default libraries, if needed */
1180 if (!opts->wine_objdir && !opts->nodefaultlibs)
1182 if (opts->gui_app)
1184 add_library(opts, lib_dirs, &files, "shell32");
1185 add_library(opts, lib_dirs, &files, "comdlg32");
1186 add_library(opts, lib_dirs, &files, "gdi32");
1188 add_library(opts, lib_dirs, &files, "advapi32");
1189 add_library(opts, lib_dirs, &files, "user32");
1190 add_library(opts, lib_dirs, &files, "winecrt0");
1191 if (opts->use_msvcrt)
1193 if (!crt_lib)
1195 if (strncmp( output_name, "msvcr", 5 ) &&
1196 strncmp( output_name, "ucrt", 4 ) &&
1197 strcmp( output_name, "crtdll.dll" ))
1198 add_library(opts, lib_dirs, &files, "ucrtbase");
1200 else strarray_add(&files, strmake("-a%s", crt_lib));
1202 if (opts->win16_app) add_library(opts, lib_dirs, &files, "kernel");
1203 add_library(opts, lib_dirs, &files, "kernel32");
1204 add_library(opts, lib_dirs, &files, "ntdll");
1207 /* set default entry point, if needed */
1208 if (!opts->entry_point)
1210 if (opts->subsystem && !strcmp( opts->subsystem, "native" ))
1211 entry_point = (is_pe && opts->target.cpu == CPU_i386) ? "DriverEntry@8" : "DriverEntry";
1212 else if (opts->use_msvcrt && !opts->shared && !opts->win16_app)
1213 entry_point = opts->unicode_app ? "wmainCRTStartup" : "mainCRTStartup";
1215 else entry_point = opts->entry_point;
1217 /* run winebuild to generate the .spec.o file */
1218 if (opts->data_only)
1220 build_data_lib( opts, spec_file, output_file, files );
1221 return;
1224 for (i = 0; i < files.count; i++)
1225 if (files.str[i][1] == 'r') strarray_add( &resources, files.str[i] );
1227 build_spec_obj( opts, spec_file, output_file, opts->target_alias, files, resources, lib_dirs,
1228 entry_point, &spec_objs );
1229 if (opts->native_arch)
1231 const char *suffix = strchr( opts->target_alias, '-' );
1232 if (!suffix) suffix = "";
1233 build_spec_obj( opts, spec_file, output_file, strmake( "%s%s", opts->native_arch, suffix ),
1234 files, empty_strarray, lib_dirs, entry_point, &spec_objs );
1237 if (opts->fake_module) return; /* nothing else to do */
1239 /* link everything together now */
1240 link_args = get_link_args( opts, output_name );
1242 if (opts->nodefaultlibs || opts->use_msvcrt)
1244 switch (opts->target.platform)
1246 case PLATFORM_MINGW:
1247 case PLATFORM_CYGWIN:
1248 libgcc = find_libgcc( opts->prefix, link_args );
1249 if (!libgcc) libgcc = "-lgcc";
1250 break;
1251 default:
1252 break;
1256 strarray_add(&link_args, "-o");
1257 strarray_add(&link_args, output_path);
1259 for ( j = 0; j < lib_dirs.count; j++ )
1260 strarray_add(&link_args, strmake("-L%s", lib_dirs.str[j]));
1262 if (is_pe && opts->use_msvcrt && !entry_point && (opts->shared || opts->win16_app))
1263 entry_point = opts->target.cpu == CPU_i386 ? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1265 if (is_pe && entry_point)
1267 if (opts->target.platform == PLATFORM_WINDOWS)
1268 strarray_add(&link_args, strmake("-Wl,-entry:%s", entry_point));
1269 else
1270 strarray_add(&link_args, strmake("-Wl,--entry,%s%s",
1271 is_pe && opts->target.cpu == CPU_i386 ? "_" : "",
1272 entry_point));
1275 strarray_addall( &link_args, spec_objs );
1277 if (is_pe)
1279 for (j = 0; j < opts->delayimports.count; j++)
1281 if (opts->target.platform == PLATFORM_WINDOWS)
1282 strarray_add(&link_args, strmake("-Wl,-delayload:%s", opts->delayimports.str[j]));
1283 else
1284 strarray_add(&link_args, strmake("-Wl,-delayload,%s",opts->delayimports.str[j]));
1288 for ( j = 0; j < files.count; j++ )
1290 const char* name = files.str[j] + 2;
1291 switch(files.str[j][1])
1293 case 'l':
1294 strarray_add(&link_args, strmake("-l%s", name));
1295 break;
1296 case 's':
1297 case 'o':
1298 strarray_add(&link_args, name);
1299 break;
1300 case 'a':
1301 if (!opts->use_msvcrt && !opts->lib_suffix && strchr(name, '/'))
1303 /* turn the path back into -Ldir -lfoo options
1304 * this makes sure that we use the specified libs even
1305 * when mingw adds its own import libs to the link */
1306 const char *p = get_basename( name );
1308 if (is_pe)
1310 if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
1312 strarray_add(&link_args, strmake("-L%s", get_dirname(name) ));
1313 strarray_add(&link_args, strmake("-l%s", get_basename_noext( p + 3 )));
1314 break;
1317 else
1319 /* don't link to ntdll or ntoskrnl in non-msvcrt mode
1320 * since they export CRT functions */
1321 if (!strcmp( p, "libntdll.a" )) break;
1322 if (!strcmp( p, "libntoskrnl.a" )) break;
1325 strarray_add(&link_args, name);
1326 break;
1327 case 'W':
1328 strarray_add(&link_args, files.str[j]);
1329 break;
1333 if (!opts->nostdlib && !is_pe)
1335 strarray_add(&link_args, "-ldl");
1336 strarray_add(&link_args, "-lm");
1337 strarray_add(&link_args, "-lc");
1340 if (libgcc) strarray_add(&link_args, libgcc);
1342 output_file_name = output_path;
1343 output_debug_file = opts->debug_file;
1344 output_implib = opts->out_implib;
1345 atexit( cleanup_output_files );
1347 spawn(opts->prefix, link_args, 0);
1349 if (opts->debug_file && !strendswith(opts->debug_file, ".pdb"))
1351 struct strarray tool, objcopy = build_tool_name(opts, opts->target_alias, TOOL_OBJCOPY);
1353 tool = empty_strarray;
1354 strarray_addall( &tool, objcopy );
1355 strarray_add(&tool, "--only-keep-debug");
1356 strarray_add(&tool, output_path);
1357 strarray_add(&tool, opts->debug_file);
1358 spawn(opts->prefix, tool, 1);
1360 tool = empty_strarray;
1361 strarray_addall( &tool, objcopy );
1362 strarray_add(&tool, "--strip-debug");
1363 strarray_add(&tool, output_path);
1364 spawn(opts->prefix, tool, 1);
1366 tool = empty_strarray;
1367 strarray_addall( &tool, objcopy );
1368 strarray_add(&tool, "--add-gnu-debuglink");
1369 strarray_add(&tool, opts->debug_file);
1370 strarray_add(&tool, output_path);
1371 spawn(opts->prefix, tool, 0);
1374 if (opts->out_implib && !is_pe)
1376 struct strarray tool, implib_args;
1378 if (!spec_file)
1379 error("--out-implib requires a .spec or .def file\n");
1381 implib_args = get_winebuild_args( opts, opts->target_alias );
1382 tool = build_tool_name( opts, opts->target_alias, TOOL_CC );
1383 strarray_add( &implib_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
1384 tool = build_tool_name( opts, opts->target_alias, TOOL_LD );
1385 strarray_add( &implib_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
1387 strarray_add(&implib_args, "--implib");
1388 strarray_add(&implib_args, "-o");
1389 strarray_add(&implib_args, opts->out_implib);
1390 strarray_add(&implib_args, "--export");
1391 strarray_add(&implib_args, spec_file);
1393 spawn(opts->prefix, implib_args, 0);
1396 if (!is_pe) fixup_constructors( opts, output_path );
1397 else if (opts->wine_builtin) make_wine_builtin( opts, output_path );
1399 /* create the loader script */
1400 if (generate_app_loader)
1401 create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
1405 static void forward( struct options *opts )
1407 struct strarray args = get_translator(opts);
1409 strarray_addall(&args, opts->compiler_args);
1410 strarray_addall(&args, opts->linker_args);
1411 spawn(opts->prefix, args, 0);
1414 static int is_linker_arg(const char* arg)
1416 static const char* link_switches[] =
1418 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1419 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1420 "-fprofile-generate", "-fprofile-use"
1422 unsigned int j;
1424 switch (arg[1])
1426 case 'R':
1427 case 'z':
1428 case 'u':
1429 return 1;
1430 case 'W':
1431 if (strncmp("-Wl,", arg, 4) == 0) return 1;
1432 break;
1433 case 'X':
1434 if (strcmp("-Xlinker", arg) == 0) return 1;
1435 break;
1436 case 'a':
1437 if (strcmp("-arch", arg) == 0) return 1;
1438 break;
1439 case 'f':
1440 if (strncmp("-fuse-ld=", arg, 9) == 0) return 1;
1441 break;
1442 case 'r':
1443 if (strncmp("-rtlib=", arg, 7) == 0) return 1;
1444 break;
1447 for (j = 0; j < ARRAY_SIZE(link_switches); j++)
1448 if (strcmp(link_switches[j], arg) == 0) return 1;
1450 return 0;
1453 static void parse_target_option( struct options *opts, const char *target )
1455 opts->target_alias = xstrdup( target );
1456 if (!parse_target( target, &opts->target )) error( "Invalid target specification '%s'\n", target );
1459 static int is_option( struct options *opts, int i, const char *option, const char **option_arg )
1461 if (!strcmp( opts->args.str[i], option ))
1463 if (opts->args.count == i) error( "option %s requires an argument\n", opts->args.str[i] );
1464 *option_arg = opts->args.str[i + 1];
1465 return 1;
1467 if (!strncmp( opts->args.str[i], option, strlen(option) ) && opts->args.str[i][strlen(option)] == '=')
1469 *option_arg = opts->args.str[i] + strlen(option) + 1;
1470 return 1;
1472 return 0;
1475 int main(int argc, char **argv)
1477 int i, c, next_is_arg = 0, linking = 1;
1478 int raw_compiler_arg, raw_linker_arg, raw_winebuild_arg;
1479 const char* option_arg;
1480 struct options opts;
1481 char* lang = 0;
1482 char* str;
1484 init_signals( exit_on_signal );
1485 bindir = get_bindir( argv[0] );
1486 libdir = get_libdir( bindir );
1487 includedir = get_includedir( bindir );
1489 /* setup tmp file removal at exit */
1490 atexit(clean_temp_files);
1492 /* initialize options */
1493 memset(&opts, 0, sizeof(opts));
1494 opts.target = init_argv0_target( argv[0] );
1495 opts.pic = 1;
1497 /* determine the processor type */
1498 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
1499 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
1501 for (i = 1; i < argc; i++)
1503 char *input_buffer = NULL, *iter, *opt, *out;
1504 struct stat st;
1505 int fd;
1507 if (argv[i][0] != '@' || (fd = open( argv[i] + 1, O_RDONLY | O_BINARY )) == -1)
1509 strarray_add( &opts.args, argv[i] );
1510 continue;
1512 if ((fstat( fd, &st ) == -1)) error( "Cannot stat %s\n", argv[i] + 1 );
1513 if (st.st_size)
1515 input_buffer = xmalloc( st.st_size + 1 );
1516 if (read( fd, input_buffer, st.st_size ) != st.st_size) error( "Cannot read %s\n", argv[i] + 1 );
1518 close( fd );
1519 for (iter = input_buffer; iter < input_buffer + st.st_size; iter++)
1521 char quote = 0;
1522 while (iter < input_buffer + st.st_size && isspace(*iter)) iter++;
1523 if (iter == input_buffer + st.st_size) break;
1524 opt = out = iter;
1525 while (iter < input_buffer + st.st_size && (quote || !isspace(*iter)))
1527 if (*iter == quote)
1529 iter++;
1530 quote = 0;
1532 else if (*iter == '\'' || *iter == '"') quote = *iter++;
1533 else
1535 if (*iter == '\\' && iter + 1 < input_buffer + st.st_size) iter++;
1536 *out++ = *iter++;
1539 *out = 0;
1540 strarray_add( &opts.args, opt );
1544 /* parse options */
1545 for (i = 0; i < opts.args.count; i++)
1547 if (opts.args.str[i][0] == '-' && opts.args.str[i][1]) /* option, except '-' alone is stdin, which is a file */
1549 /* determine if this switch is followed by a separate argument */
1550 next_is_arg = 0;
1551 option_arg = 0;
1552 switch(opts.args.str[i][1])
1554 case 'x': case 'o': case 'D': case 'U':
1555 case 'I': case 'A': case 'l': case 'u':
1556 case 'b': case 'V': case 'G': case 'L':
1557 case 'B': case 'R': case 'z':
1558 if (opts.args.str[i][2]) option_arg = &opts.args.str[i][2];
1559 else next_is_arg = 1;
1560 break;
1561 case 'i':
1562 next_is_arg = 1;
1563 break;
1564 case 'a':
1565 if (strcmp("-aux-info", opts.args.str[i]) == 0)
1566 next_is_arg = 1;
1567 if (strcmp("-arch", opts.args.str[i]) == 0)
1568 next_is_arg = 1;
1569 break;
1570 case 'X':
1571 if (strcmp("-Xlinker", opts.args.str[i]) == 0)
1572 next_is_arg = 1;
1573 break;
1574 case 'M':
1575 c = opts.args.str[i][2];
1576 if (c == 'F' || c == 'T' || c == 'Q')
1578 if (opts.args.str[i][3]) option_arg = &opts.args.str[i][3];
1579 else next_is_arg = 1;
1581 break;
1582 case 'f':
1583 if (strcmp("-framework", opts.args.str[i]) == 0)
1584 next_is_arg = 1;
1585 break;
1586 case 't':
1587 next_is_arg = strcmp("-target", opts.args.str[i]) == 0;
1588 break;
1589 case '-':
1590 next_is_arg = (strcmp("--param", opts.args.str[i]) == 0 ||
1591 strcmp("--sysroot", opts.args.str[i]) == 0 ||
1592 strcmp("--target", opts.args.str[i]) == 0 ||
1593 strcmp("--wine-objdir", opts.args.str[i]) == 0 ||
1594 strcmp("--winebuild", opts.args.str[i]) == 0 ||
1595 strcmp("--lib-suffix", opts.args.str[i]) == 0);
1596 break;
1598 if (next_is_arg)
1600 if (i + 1 >= opts.args.count) error("option -%c requires an argument\n", opts.args.str[i][1]);
1601 option_arg = opts.args.str[i+1];
1604 /* determine what options go 'as is' to the linker & the compiler */
1605 raw_linker_arg = is_linker_arg(opts.args.str[i]);
1606 raw_compiler_arg = !raw_linker_arg;
1607 raw_winebuild_arg = 0;
1609 /* do a bit of semantic analysis */
1610 switch (opts.args.str[i][1])
1612 case 'B':
1613 str = xstrdup(option_arg);
1614 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
1615 strarray_add(&opts.prefix, str);
1616 raw_linker_arg = 1;
1617 break;
1618 case 'b':
1619 parse_target_option( &opts, option_arg );
1620 raw_compiler_arg = 0;
1621 break;
1622 case 'V':
1623 opts.version = xstrdup( option_arg );
1624 raw_compiler_arg = 0;
1625 break;
1626 case 'c': /* compile or assemble */
1627 raw_compiler_arg = 0;
1628 if (opts.args.str[i][2] == 0) opts.compile_only = 1;
1629 /* fall through */
1630 case 'S': /* generate assembler code */
1631 case 'E': /* preprocess only */
1632 if (opts.args.str[i][2] == 0) linking = 0;
1633 break;
1634 case 'f':
1635 if (strcmp("-fno-short-wchar", opts.args.str[i]) == 0)
1636 opts.noshortwchar = 1;
1637 else if (!strcmp("-fasynchronous-unwind-tables", opts.args.str[i]))
1638 opts.unwind_tables = 1;
1639 else if (!strcmp("-fno-asynchronous-unwind-tables", opts.args.str[i]))
1640 opts.unwind_tables = 0;
1641 else if (!strcmp("-fPIC", opts.args.str[i]) || !strcmp("-fpic", opts.args.str[i]))
1642 opts.pic = 1;
1643 else if (!strcmp("-fno-PIC", opts.args.str[i]) || !strcmp("-fno-pic", opts.args.str[i]))
1644 opts.pic = 0;
1645 break;
1646 case 'i':
1647 if (!strcmp( "-isysroot", opts.args.str[i] )) opts.isysroot = opts.args.str[i + 1];
1648 break;
1649 case 'l':
1650 strarray_add(&opts.files, strmake("-l%s", option_arg));
1651 raw_compiler_arg = 0;
1652 break;
1653 case 'L':
1654 strarray_add(&opts.lib_dirs, option_arg);
1655 raw_compiler_arg = 0;
1656 break;
1657 case 'M': /* map file generation */
1658 linking = 0;
1659 break;
1660 case 'm':
1661 if (strcmp("-mno-cygwin", opts.args.str[i]) == 0)
1663 opts.use_msvcrt = 1;
1664 raw_compiler_arg = 0;
1666 if (strcmp("-mcygwin", opts.args.str[i]) == 0)
1668 opts.use_msvcrt = 0;
1669 raw_compiler_arg = 0;
1671 else if (strcmp("-mwindows", opts.args.str[i]) == 0)
1673 opts.gui_app = 1;
1674 raw_compiler_arg = 0;
1676 else if (strcmp("-mconsole", opts.args.str[i]) == 0)
1678 opts.gui_app = 0;
1679 raw_compiler_arg = 0;
1681 else if (strcmp("-municode", opts.args.str[i]) == 0)
1683 opts.unicode_app = 1;
1684 raw_compiler_arg = 0;
1685 raw_winebuild_arg = 1;
1687 else if (strcmp("-mthreads", opts.args.str[i]) == 0)
1689 raw_compiler_arg = 0;
1691 else if (strcmp("-m16", opts.args.str[i]) == 0)
1693 opts.win16_app = 1;
1694 raw_compiler_arg = 0;
1695 raw_winebuild_arg = 1;
1697 else if (strcmp("-m32", opts.args.str[i]) == 0)
1699 set_target_ptr_size( &opts.target, 4 );
1700 opts.force_pointer_size = 4;
1701 raw_linker_arg = 1;
1703 else if (strcmp("-m64", opts.args.str[i]) == 0)
1705 set_target_ptr_size( &opts.target, 8 );
1706 opts.force_pointer_size = 8;
1707 raw_linker_arg = 1;
1709 else if (!strcmp("-marm", opts.args.str[i] ) || !strcmp("-mthumb", opts.args.str[i] ))
1711 raw_linker_arg = 1;
1713 else if (!strcmp("-marm64x", opts.args.str[i] ))
1715 raw_linker_arg = 1;
1716 opts.native_arch = "aarch64";
1718 else if (!strncmp("-mcpu=", opts.args.str[i], 6) ||
1719 !strncmp("-mfpu=", opts.args.str[i], 6) ||
1720 !strncmp("-march=", opts.args.str[i], 7))
1721 raw_winebuild_arg = 1;
1722 break;
1723 case 'n':
1724 if (strcmp("-nostdinc", opts.args.str[i]) == 0)
1725 opts.nostdinc = 1;
1726 else if (strcmp("-nodefaultlibs", opts.args.str[i]) == 0)
1727 opts.nodefaultlibs = 1;
1728 else if (strcmp("-nostdlib", opts.args.str[i]) == 0)
1729 opts.nostdlib = 1;
1730 else if (strcmp("-nostartfiles", opts.args.str[i]) == 0)
1731 opts.nostartfiles = 1;
1732 break;
1733 case 'o':
1734 opts.output_name = option_arg;
1735 raw_compiler_arg = 0;
1736 break;
1737 case 'p':
1738 if (strcmp("-pthread", opts.args.str[i]) == 0)
1740 raw_compiler_arg = 1;
1741 raw_linker_arg = 1;
1743 break;
1744 case 's':
1745 if (strcmp("-static", opts.args.str[i]) == 0)
1746 linking = -1;
1747 else if(strcmp("-save-temps", opts.args.str[i]) == 0)
1748 keep_generated = 1;
1749 else if (strncmp("-specs=", opts.args.str[i], 7) == 0)
1750 raw_linker_arg = 1;
1751 else if(strcmp("-shared", opts.args.str[i]) == 0)
1753 opts.shared = 1;
1754 raw_compiler_arg = raw_linker_arg = 0;
1756 else if (strcmp("-s", opts.args.str[i]) == 0 && opts.target.platform == PLATFORM_APPLE)
1758 /* On Mac, change -s into -Wl,-x. ld's -s switch
1759 * is deprecated, and it doesn't work on Tiger with
1760 * MH_BUNDLEs anyway
1762 opts.strip = 1;
1763 raw_linker_arg = 0;
1765 break;
1766 case 't':
1767 if (is_option( &opts, i, "-target", &option_arg ))
1769 parse_target_option( &opts, option_arg );
1770 raw_compiler_arg = raw_linker_arg = 0;
1772 break;
1773 case 'v':
1774 if (opts.args.str[i][2] == 0) verbose++;
1775 break;
1776 case 'W':
1777 if (strncmp("-Wl,", opts.args.str[i], 4) == 0)
1779 unsigned int j;
1780 struct strarray Wl = strarray_fromstring(opts.args.str[i] + 4, ",");
1781 for (j = 0; j < Wl.count; j++)
1783 if (!strcmp(Wl.str[j], "--image-base") && j < Wl.count - 1)
1785 opts.image_base = xstrdup( Wl.str[++j] );
1786 continue;
1788 if (!strcmp(Wl.str[j], "--section-alignment") && j < Wl.count - 1)
1790 opts.section_align = xstrdup( Wl.str[++j] );
1791 continue;
1793 if (!strcmp(Wl.str[j], "--file-alignment") && j < Wl.count - 1)
1795 opts.file_align = xstrdup( Wl.str[++j] );
1796 continue;
1798 if (!strcmp(Wl.str[j], "--large-address-aware"))
1800 opts.large_address_aware = 1;
1801 continue;
1803 if (!strcmp(Wl.str[j], "--wine-builtin"))
1805 opts.wine_builtin = 1;
1806 continue;
1808 if (!strcmp(Wl.str[j], "--subsystem") && j < Wl.count - 1)
1810 opts.subsystem = xstrdup( Wl.str[++j] );
1811 continue;
1813 if (!strcmp(Wl.str[j], "--entry") && j < Wl.count - 1)
1815 opts.entry_point = xstrdup( Wl.str[++j] );
1816 continue;
1818 if (!strcmp(Wl.str[j], "-delayload") && j < Wl.count - 1)
1820 strarray_add( &opts.delayimports, Wl.str[++j] );
1821 continue;
1823 if (!strcmp(Wl.str[j], "--debug-file") && j < Wl.count - 1)
1825 opts.debug_file = xstrdup( Wl.str[++j] );
1826 continue;
1828 if (!strcmp(Wl.str[j], "--whole-archive") ||
1829 !strcmp(Wl.str[j], "--no-whole-archive") ||
1830 !strcmp(Wl.str[j], "--start-group") ||
1831 !strcmp(Wl.str[j], "--end-group"))
1833 strarray_add( &opts.files, strmake( "-Wl,%s", Wl.str[j] ));
1834 continue;
1836 if (!strcmp(Wl.str[j], "--out-implib"))
1838 opts.out_implib = xstrdup( Wl.str[++j] );
1839 continue;
1841 if (!strcmp(Wl.str[j], "-static")) linking = -1;
1842 strarray_add(&opts.linker_args, strmake("-Wl,%s",Wl.str[j]));
1844 raw_compiler_arg = raw_linker_arg = 0;
1846 else if (strncmp("-Wb,", opts.args.str[i], 4) == 0)
1848 unsigned int j;
1849 struct strarray Wb = strarray_fromstring(opts.args.str[i] + 4, ",");
1850 for (j = 0; j < Wb.count; j++)
1852 if (!strcmp(Wb.str[j], "--data-only")) opts.data_only = 1;
1853 if (!strcmp(Wb.str[j], "--fake-module")) opts.fake_module = 1;
1854 else strarray_add( &opts.winebuild_args, Wb.str[j] );
1856 raw_compiler_arg = raw_linker_arg = 0;
1858 break;
1859 case 'x':
1860 lang = strmake("-x%s", option_arg);
1861 strarray_add(&opts.files, lang);
1862 /* we'll pass these flags ourselves, explicitly */
1863 raw_compiler_arg = raw_linker_arg = 0;
1864 break;
1865 case '-':
1866 if (strcmp("-static", opts.args.str[i]+1) == 0)
1867 linking = -1;
1868 else if (!strcmp( "-no-default-config", opts.args.str[i] + 1 ))
1870 opts.no_default_config = 1;
1871 raw_compiler_arg = raw_linker_arg = 1;
1873 else if (is_option( &opts, i, "--sysroot", &option_arg ))
1875 opts.sysroot = option_arg;
1876 raw_linker_arg = 1;
1878 else if (is_option( &opts, i, "--target", &option_arg ))
1880 parse_target_option( &opts, option_arg );
1881 raw_compiler_arg = raw_linker_arg = 0;
1883 else if (is_option( &opts, i, "--wine-objdir", &option_arg ))
1885 opts.wine_objdir = option_arg;
1886 raw_compiler_arg = raw_linker_arg = 0;
1888 else if (is_option( &opts, i, "--winebuild", &option_arg ))
1890 opts.winebuild = option_arg;
1891 raw_compiler_arg = raw_linker_arg = 0;
1893 else if (is_option( &opts, i, "--lib-suffix", &option_arg ))
1895 opts.lib_suffix = option_arg;
1896 raw_compiler_arg = raw_linker_arg = 0;
1898 break;
1901 /* put the arg into the appropriate bucket */
1902 if (raw_linker_arg)
1904 strarray_add( &opts.linker_args, opts.args.str[i] );
1905 if (next_is_arg && (i + 1 < opts.args.count))
1906 strarray_add( &opts.linker_args, opts.args.str[i + 1] );
1908 if (raw_compiler_arg)
1910 strarray_add( &opts.compiler_args, opts.args.str[i] );
1911 if (next_is_arg && (i + 1 < opts.args.count))
1912 strarray_add( &opts.compiler_args, opts.args.str[i + 1] );
1914 if (raw_winebuild_arg)
1916 strarray_add( &opts.winebuild_args, opts.args.str[i] );
1917 if (next_is_arg && (i + 1 < opts.args.count))
1918 strarray_add( &opts.winebuild_args, opts.args.str[i + 1] );
1921 /* skip the next token if it's an argument */
1922 if (next_is_arg) i++;
1924 else
1926 strarray_add( &opts.files, opts.args.str[i] );
1930 if (opts.force_pointer_size) set_target_ptr_size( &opts.target, opts.force_pointer_size );
1931 if (opts.processor == proc_cpp) linking = 0;
1932 if (linking == -1) error("Static linking is not supported\n");
1934 if (is_pe_target( &opts )) opts.use_msvcrt = 1;
1936 if (opts.files.count == 0 && !opts.fake_module) forward(&opts);
1937 else if (linking) build(&opts);
1938 else compile(&opts, lang);
1940 output_file_name = NULL;
1941 output_debug_file = NULL;
1942 output_implib = NULL;
1943 return 0;