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
24 * all options for gcc start with '-' and are for the most part
25 * single options (no parameters as separate argument).
26 * There are of course exceptions to this rule, so here is an
27 * exhaustive list of options that do take parameters (potentially)
28 * as a separate argument:
47 * -iwithprefixbefore dir
59 * -G num (see NOTES below)
62 * There is -G option for compatibility with System V that
63 * takes no parameters. This makes "-G num" parsing ambiguous.
64 * This option is synonymous to -shared, and as such we will
65 * not support it for now.
67 * Special interest options
73 * object-file-name -llibrary -nostartfiles -nodefaultlibs
74 * -nostdlib -s -static -static-libgcc -static-libstdc++
75 * -shared -shared-libgcc -symbolic -Wl,option
76 * -Xlinker option -u symbol --image-base -fuse-ld
79 * -Bprefix -Idir -I- -Ldir -specs=file
82 * -b machine -V version
84 * Please note that the Target Options are relevant to everything:
85 * compiler, linker, assembler, preprocessor.
90 #include "wine/port.h"
103 static const char* app_loader_template
=
107 "# determine the application directory\n"
111 " # $0 contains a path, use it\n"
112 " appdir=`dirname \"$0\"`\n"
115 " # no directory in $0, search in PATH\n"
121 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\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"
132 " apppath=\"$appname\"\n"
135 "# determine the WINELOADER\n"
136 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\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 static strarray
* tmp_files
;
148 static sigset_t signal_mask
;
151 static const char *bindir
;
152 static const char *libdir
;
153 static const char *includedir
;
155 enum processor
{ proc_cc
, proc_cxx
, proc_cpp
, proc_as
};
168 { "amd64", CPU_x86_64
},
169 { "x86_64", CPU_x86_64
},
170 { "powerpc", CPU_POWERPC
},
172 { "armv5", CPU_ARM
},
173 { "armv6", CPU_ARM
},
174 { "armv7", CPU_ARM
},
175 { "armv7a", CPU_ARM
},
176 { "arm64", CPU_ARM64
},
177 { "aarch64", CPU_ARM64
},
183 enum target_platform platform
;
186 { "macos", PLATFORM_APPLE
},
187 { "darwin", PLATFORM_APPLE
},
188 { "android", PLATFORM_ANDROID
},
189 { "solaris", PLATFORM_SOLARIS
},
190 { "cygwin", PLATFORM_CYGWIN
},
191 { "mingw32", PLATFORM_MINGW
},
192 { "windows-gnu", PLATFORM_MINGW
},
193 { "windows", PLATFORM_WINDOWS
},
194 { "winnt", PLATFORM_MINGW
}
199 enum processor processor
;
200 enum target_cpu target_cpu
;
201 enum target_platform target_platform
;
216 int force_pointer_size
;
217 int large_address_aware
;
222 const char* wine_objdir
;
223 const char* winebuild
;
224 const char* output_name
;
225 const char* image_base
;
226 const char* section_align
;
227 const char* file_align
;
229 const char* isysroot
;
230 const char* lib_suffix
;
231 const char* subsystem
;
232 const char* entry_point
;
234 const char* debug_file
;
235 const char* out_implib
;
239 strarray
* linker_args
;
240 strarray
* compiler_args
;
241 strarray
* winebuild_args
;
243 strarray
* delayimports
;
247 static const enum target_cpu build_cpu
= CPU_x86
;
248 #elif defined(__x86_64__)
249 static const enum target_cpu build_cpu
= CPU_x86_64
;
250 #elif defined(__powerpc__)
251 static const enum target_cpu build_cpu
= CPU_POWERPC
;
252 #elif defined(__arm__)
253 static const enum target_cpu build_cpu
= CPU_ARM
;
254 #elif defined(__aarch64__)
255 static const enum target_cpu build_cpu
= CPU_ARM64
;
257 #error Unsupported CPU
261 static enum target_platform build_platform
= PLATFORM_APPLE
;
262 #elif defined(__ANDROID__)
263 static enum target_platform build_platform
= PLATFORM_ANDROID
;
265 static enum target_platform build_platform
= PLATFORM_SOLARIS
;
266 #elif defined(__CYGWIN__)
267 static enum target_platform build_platform
= PLATFORM_CYGWIN
;
268 #elif defined(_WIN32)
269 static enum target_platform build_platform
= PLATFORM_MINGW
;
271 static enum target_platform build_platform
= PLATFORM_UNSPECIFIED
;
274 static void cleanup_output_files(void)
276 if (output_file_name
) unlink( output_file_name
);
277 if (output_debug_file
) unlink( output_debug_file
);
278 if (output_implib
) unlink( output_implib
);
281 static void clean_temp_files(void)
285 if (keep_generated
) return;
287 for (i
= 0; i
< tmp_files
->size
; i
++)
288 unlink(tmp_files
->base
[i
]);
291 /* clean things up when aborting on a signal */
292 static void exit_on_signal( int sig
)
294 exit(1); /* this will call the atexit functions */
297 static char* get_temp_file(const char* prefix
, const char* suffix
)
300 char* tmp
= strmake("%s-XXXXXX%s", prefix
, suffix
);
302 #ifdef HAVE_SIGPROCMASK
304 /* block signals while manipulating the temp files list */
305 sigprocmask( SIG_BLOCK
, &signal_mask
, &old_set
);
307 fd
= mkstemps( tmp
, strlen(suffix
) );
310 /* could not create it in current directory, try in TMPDIR */
314 if (!(tmpdir
= getenv("TMPDIR"))) tmpdir
= "/tmp";
315 tmp
= strmake("%s/%s-XXXXXX%s", tmpdir
, prefix
, suffix
);
316 fd
= mkstemps( tmp
, strlen(suffix
) );
317 if (fd
== -1) error( "could not create temp file\n" );
320 strarray_add(tmp_files
, tmp
);
321 #ifdef HAVE_SIGPROCMASK
322 sigprocmask( SIG_SETMASK
, &old_set
, NULL
);
327 static int is_pe_target( const struct options
*opts
)
329 switch(opts
->target_platform
)
332 case PLATFORM_CYGWIN
:
333 case PLATFORM_WINDOWS
:
352 const char *llvm_base
;
356 { "gcc", "clang --driver-mode=gcc", CC
},
357 { "g++", "clang --driver-mode=g++", CXX
},
358 { "cpp", "clang --driver-mode=cpp", CPP
},
359 { "ld", "ld.lld", LD
},
360 { "objcopy", "llvm-objcopy" },
363 static strarray
* build_tool_name( struct options
*opts
, enum tool tool
)
365 const char *base
= tool_names
[tool
].base
;
366 const char *llvm_base
= tool_names
[tool
].llvm_base
;
367 const char *deflt
= tool_names
[tool
].deflt
;
372 if (opts
->target
&& opts
->version
)
374 str
= strmake("%s-%s-%s", opts
->target
, base
, opts
->version
);
376 else if (opts
->target
)
378 str
= strmake("%s-%s", opts
->target
, base
);
380 else if (opts
->version
)
382 str
= strmake("%s-%s", base
, opts
->version
);
385 str
= xstrdup((deflt
&& *deflt
) ? deflt
: base
);
387 if ((path
= find_binary( opts
->prefix
, str
))) return strarray_fromstring( path
, " " );
389 if (!opts
->version
) str
= xstrdup( llvm_base
);
390 else str
= strmake( "%s-%s", llvm_base
, opts
->version
);
391 path
= find_binary( opts
->prefix
, str
);
394 error( "Could not find %s\n", base
);
398 ret
= strarray_fromstring( path
, " " );
399 if (!strncmp( llvm_base
, "clang", 5 ))
403 strarray_add( ret
, "-target" );
404 strarray_add( ret
, opts
->target
);
406 strarray_add( ret
, "-Wno-unused-command-line-argument" );
407 strarray_add( ret
, "-fuse-ld=lld" );
412 static strarray
* get_translator(struct options
*opts
)
416 switch(opts
->processor
)
431 return build_tool_name( opts
, tool
);
434 static int try_link( const strarray
*prefix
, const strarray
*link_tool
, const char *cflags
)
436 const char *in
= get_temp_file( "try_link", ".c" );
437 const char *out
= get_temp_file( "try_link", ".out" );
438 const char *err
= get_temp_file( "try_link", ".err" );
439 strarray
*link
= strarray_dup( link_tool
);
440 int sout
= -1, serr
= -1;
443 create_file( in
, 0644, "int main(void){return 1;}\n" );
445 strarray_add( link
, "-o" );
446 strarray_add( link
, out
);
447 strarray_addall( link
, strarray_fromstring( cflags
, " " ) );
448 strarray_add( link
, in
);
450 sout
= dup( fileno(stdout
) );
451 freopen( err
, "w", stdout
);
452 serr
= dup( fileno(stderr
) );
453 freopen( err
, "w", stderr
);
454 ret
= spawn( prefix
, link
, 1 );
457 dup2( sout
, fileno(stdout
) );
462 dup2( serr
, fileno(stderr
) );
465 strarray_free( link
);
469 static strarray
*get_link_args( struct options
*opts
, const char *output_name
)
471 strarray
*link_args
= get_translator( opts
);
472 strarray
*flags
= strarray_alloc();
474 strarray_addall( link_args
, opts
->linker_args
);
476 if (verbose
> 1) strarray_add( flags
, "-v" );
478 switch (opts
->target_platform
)
481 strarray_add( flags
, "-bundle" );
482 strarray_add( flags
, "-multiply_defined" );
483 strarray_add( flags
, "suppress" );
484 if (opts
->target_cpu
== CPU_POWERPC
)
486 strarray_add( flags
, "-read_only_relocs" );
487 strarray_add( flags
, "warning" );
489 if (opts
->image_base
)
491 strarray_add( flags
, "-image_base" );
492 strarray_add( flags
, opts
->image_base
);
494 if (opts
->strip
) strarray_add( flags
, "-Wl,-x" );
495 strarray_addall( link_args
, flags
);
498 case PLATFORM_SOLARIS
:
500 char *mapfile
= get_temp_file( output_name
, ".map" );
501 const char *align
= opts
->section_align
? opts
->section_align
: "0x1000";
503 create_file( mapfile
, 0644, "text = A%s;\ndata = A%s;\n", align
, align
);
504 strarray_add( flags
, strmake("-Wl,-M,%s", mapfile
) );
505 strarray_add( tmp_files
, mapfile
);
509 case PLATFORM_ANDROID
:
510 /* the Android loader requires a soname for all libraries */
511 strarray_add( flags
, strmake( "-Wl,-soname,%s.so", output_name
));
515 case PLATFORM_CYGWIN
:
516 if (opts
->shared
|| opts
->win16_app
)
518 strarray_add( flags
, "-shared" );
519 strarray_add( flags
, "-Wl,--kill-at" );
521 else strarray_add( flags
, opts
->gui_app
? "-mwindows" : "-mconsole" );
523 if (opts
->unicode_app
) strarray_add( flags
, "-municode" );
524 if (opts
->nodefaultlibs
|| opts
->use_msvcrt
) strarray_add( flags
, "-nodefaultlibs" );
525 if (opts
->nostartfiles
|| opts
->use_msvcrt
) strarray_add( flags
, "-nostartfiles" );
526 if (opts
->subsystem
) strarray_add( flags
, strmake("-Wl,--subsystem,%s", opts
->subsystem
));
528 strarray_add( flags
, "-Wl,--nxcompat" );
530 if (opts
->image_base
) strarray_add( flags
, strmake("-Wl,--image-base,%s", opts
->image_base
));
532 if (opts
->large_address_aware
&& opts
->target_cpu
== CPU_x86
)
533 strarray_add( flags
, "-Wl,--large-address-aware" );
535 /* make sure we don't need a libgcc_s dll on Windows */
536 strarray_add( flags
, "-static-libgcc" );
538 if (opts
->debug_file
&& strendswith(opts
->debug_file
, ".pdb"))
539 strarray_add(link_args
, strmake("-Wl,-pdb,%s", opts
->debug_file
));
541 if (opts
->out_implib
)
542 strarray_add(link_args
, strmake("-Wl,--out-implib,%s", opts
->out_implib
));
544 if (!try_link( opts
->prefix
, link_args
, "-Wl,--file-alignment,0x1000" ))
545 strarray_add( link_args
, strmake( "-Wl,--file-alignment,%s",
546 opts
->file_align
? opts
->file_align
: "0x1000" ));
547 else if (!try_link( opts
->prefix
, link_args
, "-Wl,-Xlink=-filealign:0x1000" ))
548 /* lld from llvm 10 does not support mingw style --file-alignment,
549 * but it's possible to use msvc syntax */
550 strarray_add( link_args
, strmake( "-Wl,-Xlink=-filealign:%s",
551 opts
->file_align
? opts
->file_align
: "0x1000" ));
553 strarray_addall( link_args
, flags
);
556 case PLATFORM_WINDOWS
:
557 if (opts
->shared
|| opts
->win16_app
)
559 strarray_add( flags
, "-shared" );
560 strarray_add( flags
, "-Wl,-kill-at" );
562 if (opts
->unicode_app
) strarray_add( flags
, "-municode" );
563 if (opts
->nodefaultlibs
|| opts
->use_msvcrt
) strarray_add( flags
, "-nodefaultlibs" );
564 if (opts
->nostartfiles
|| opts
->use_msvcrt
) strarray_add( flags
, "-nostartfiles" );
565 if (opts
->image_base
) strarray_add( flags
, strmake("-Wl,-base:%s", opts
->image_base
));
567 strarray_add( flags
, strmake("-Wl,-subsystem:%s", opts
->subsystem
));
569 strarray_add( flags
, strmake("-Wl,-subsystem:%s", opts
->gui_app
? "windows" : "console" ));
571 if (opts
->debug_file
&& strendswith(opts
->debug_file
, ".pdb"))
573 strarray_add(link_args
, "-Wl,-debug");
574 strarray_add(link_args
, strmake("-Wl,-pdb:%s", opts
->debug_file
));
576 else if (!opts
->strip
)
577 strarray_add(link_args
, "-Wl,-debug:dwarf");
579 if (opts
->out_implib
)
580 strarray_add(link_args
, strmake("-Wl,-implib:%s", opts
->out_implib
));
582 strarray_add( link_args
, strmake( "-Wl,-filealign:%s", opts
->file_align
? opts
->file_align
: "0x1000" ));
584 strarray_addall( link_args
, flags
);
588 if (opts
->image_base
)
590 if (!try_link( opts
->prefix
, link_args
, strmake("-Wl,-Ttext-segment=%s", opts
->image_base
)) )
591 strarray_add( flags
, strmake("-Wl,-Ttext-segment=%s", opts
->image_base
) );
593 opts
->prelink
= PRELINK
;
595 if (!try_link( opts
->prefix
, link_args
, "-Wl,-z,max-page-size=0x1000"))
596 strarray_add( flags
, "-Wl,-z,max-page-size=0x1000");
597 if (opts
->unix_lib
) strarray_add( flags
, strmake( "-Wl,-soname,%s.so", output_name
));
601 /* generic Unix shared library flags */
603 strarray_add( link_args
, "-shared" );
604 strarray_add( link_args
, "-Wl,-Bsymbolic" );
605 if (!opts
->noshortwchar
&& opts
->target_cpu
== CPU_ARM
)
606 strarray_add( flags
, "-Wl,--no-wchar-size-warning" );
607 if (!try_link( opts
->prefix
, link_args
, "-Wl,-z,defs" ))
608 strarray_add( flags
, "-Wl,-z,defs" );
610 strarray_addall( link_args
, flags
);
614 static const char *get_multiarch_dir( enum target_cpu cpu
)
618 case CPU_x86
: return "/i386-linux-gnu";
619 case CPU_x86_64
: return "/x86_64-linux-gnu";
620 case CPU_ARM
: return "/arm-linux-gnueabi";
621 case CPU_ARM64
: return "/aarch64-linux-gnu";
622 case CPU_POWERPC
: return "/powerpc-linux-gnu";
629 static const char *get_wine_arch_dir( enum target_cpu target_cpu
, enum target_platform target_platform
)
635 case CPU_x86
: cpu
= "i386"; break;
636 case CPU_x86_64
: cpu
= "x86_64"; break;
637 case CPU_ARM
: cpu
= "arm"; break;
638 case CPU_ARM64
: cpu
= "aarch64"; break;
639 default: return "/wine";
641 switch (target_platform
)
643 case PLATFORM_WINDOWS
:
644 case PLATFORM_CYGWIN
:
646 return strmake( "/wine/%s-windows", cpu
);
648 return strmake( "/wine/%s-unix", cpu
);
652 static char *get_lib_dir( struct options
*opts
)
654 const char *stdlibpath
[] = { libdir
, LIBDIR
, "/usr/lib", "/usr/local/lib", "/lib" };
655 const char *bit_suffix
, *other_bit_suffix
, *build_multiarch
, *target_multiarch
, *winecrt0
;
656 const char *root
= opts
->sysroot
? opts
->sysroot
: "";
659 size_t build_len
, target_len
;
661 bit_suffix
= opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
? "64" : "32";
662 other_bit_suffix
= opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
? "32" : "64";
663 winecrt0
= strmake( "%s/libwinecrt0.a", get_wine_arch_dir( opts
->target_cpu
, opts
->target_platform
));
664 build_multiarch
= get_multiarch_dir( build_cpu
);
665 target_multiarch
= get_multiarch_dir( opts
->target_cpu
);
666 build_len
= strlen( build_multiarch
);
667 target_len
= strlen( target_multiarch
);
669 for (i
= 0; i
< ARRAY_SIZE(stdlibpath
); i
++)
671 const char *root
= (i
&& opts
->sysroot
) ? opts
->sysroot
: "";
674 if (!stdlibpath
[i
]) continue;
675 buffer
= xmalloc( strlen(root
) + strlen(stdlibpath
[i
]) +
676 strlen("/arm-linux-gnueabi") + strlen(winecrt0
) + 1 );
677 strcpy( buffer
, root
);
678 strcat( buffer
, stdlibpath
[i
] );
679 p
= buffer
+ strlen(buffer
);
680 while (p
> buffer
&& p
[-1] == '/') p
--;
681 strcpy( p
, winecrt0
);
682 if (!stat( buffer
, &st
)) goto found
;
683 if (p
> buffer
+ 2 && (!memcmp( p
- 2, "32", 2 ) || !memcmp( p
- 2, "64", 2 )))
686 strcpy( p
, winecrt0
);
687 if (!stat( buffer
, &st
)) goto found
;
689 strcpy( p
, bit_suffix
);
690 strcat( p
, winecrt0
);
691 if (!stat( buffer
, &st
)) goto found
;
692 strcpy( p
, target_multiarch
);
693 strcat( p
, winecrt0
);
694 if (!stat( buffer
, &st
)) goto found
;
696 strcpy( buffer
, root
);
697 strcat( buffer
, stdlibpath
[i
] );
698 p
= buffer
+ strlen(buffer
);
699 while (p
> buffer
&& p
[-1] == '/') p
--;
700 strcpy( p
, winecrt0
);
702 /* try to fixup each parent dirs named lib, lib32 or lib64 with target bitness suffix */
706 while (p
> buffer
&& *p
!= '/') p
--;
707 if (*p
!= '/') break;
709 /* try s/$build_cpu/$target_cpu/ on multiarch */
710 if (build_cpu
!= opts
->target_cpu
&& !memcmp( p
, build_multiarch
, build_len
) && p
[build_len
] == '/')
712 memmove( p
+ target_len
, p
+ build_len
, strlen( p
+ build_len
) + 1 );
713 memcpy( p
, target_multiarch
, target_len
);
714 if (!stat( buffer
, &st
)) goto found
;
715 memmove( p
+ build_len
, p
+ target_len
, strlen( p
+ target_len
) + 1 );
716 memcpy( p
, build_multiarch
, build_len
);
719 if (memcmp( p
+ 1, "lib", 3 )) continue;
722 memmove( p
+ 6, p
+ 4, strlen( p
+ 4 ) + 1 );
723 memcpy( p
+ 4, bit_suffix
, 2 );
724 if (!stat( buffer
, &st
)) goto found
;
725 memmove( p
+ 4, p
+ 6, strlen( p
+ 6 ) + 1 );
727 else if (!memcmp( p
+ 4, other_bit_suffix
, 2 ) && p
[6] == '/')
729 memcpy( p
+ 4, bit_suffix
, 2 );
730 if (!stat( buffer
, &st
)) goto found
;
731 memmove( p
+ 4, p
+ 6, strlen( p
+ 6 ) + 1 );
732 if (!stat( buffer
, &st
)) goto found
;
733 memmove( p
+ 6, p
+ 4, strlen( p
+ 4 ) + 1 );
734 memcpy( p
+ 4, other_bit_suffix
, 2 );
742 buffer
[strlen(buffer
) - strlen(winecrt0
)] = 0;
745 return strmake( "%s%s", root
, LIBDIR
);
748 static void init_argv0_dir( const char *argv0
)
753 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
754 dir
= realpath( "/proc/self/exe", NULL
);
755 #elif defined (__FreeBSD__) || defined(__DragonFly__)
756 dir
= realpath( "/proc/curproc/file", NULL
);
758 dir
= realpath( argv0
, NULL
);
761 if (!(p
= strrchr( dir
, '/' ))) return;
765 includedir
= strmake( "%s/%s", dir
, BIN_TO_INCLUDEDIR
);
766 libdir
= strmake( "%s/%s", dir
, BIN_TO_LIBDIR
);
770 static void compile(struct options
* opts
, const char* lang
)
772 strarray
* comp_args
= strarray_alloc();
778 strarray_addall(comp_args
, get_translator(opts
));
779 if (opts
->force_pointer_size
)
780 strarray_add( comp_args
, strmake("-m%u", 8 * opts
->force_pointer_size
) );
781 switch(opts
->processor
)
783 case proc_cpp
: gcc_defs
= 1; break;
784 case proc_as
: gcc_defs
= 0; break;
785 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
786 /* mixing different C and C++ compilers isn't supported in configure anyway */
789 gcc
= build_tool_name(opts
, TOOL_CC
);
790 gpp
= build_tool_name(opts
, TOOL_CXX
);
791 for ( j
= 0; !gcc_defs
&& j
< comp_args
->size
; j
++ )
793 const char *cc
= comp_args
->base
[j
];
795 for (i
= 0; !gcc_defs
&& i
< gcc
->size
; i
++)
796 gcc_defs
= gcc
->base
[i
][0] != '-' && strendswith(cc
, gcc
->base
[i
]);
797 for (i
= 0; !gcc_defs
&& i
< gpp
->size
; i
++)
798 gcc_defs
= gpp
->base
[i
][0] != '-' && strendswith(cc
, gpp
->base
[i
]);
805 if (opts
->target_platform
== PLATFORM_WINDOWS
|| opts
->target_platform
== PLATFORM_CYGWIN
|| opts
->target_platform
== PLATFORM_MINGW
)
806 goto no_compat_defines
;
808 if (opts
->processor
!= proc_cpp
)
810 if (gcc_defs
&& !opts
->wine_objdir
&& !opts
->noshortwchar
)
812 strarray_add(comp_args
, "-fshort-wchar");
813 strarray_add(comp_args
, "-DWINE_UNICODE_NATIVE");
815 strarray_add(comp_args
, "-D_REENTRANT");
817 strarray_add(comp_args
, "-fPIC");
819 strarray_add(comp_args
, "-fno-PIC");
822 if (opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
)
824 strarray_add(comp_args
, "-DWIN64");
825 strarray_add(comp_args
, "-D_WIN64");
826 strarray_add(comp_args
, "-D__WIN64");
827 strarray_add(comp_args
, "-D__WIN64__");
830 strarray_add(comp_args
, "-DWIN32");
831 strarray_add(comp_args
, "-D_WIN32");
832 strarray_add(comp_args
, "-D__WIN32");
833 strarray_add(comp_args
, "-D__WIN32__");
834 strarray_add(comp_args
, "-D__WINNT");
835 strarray_add(comp_args
, "-D__WINNT__");
839 switch (opts
->target_cpu
)
843 strarray_add(comp_args
, "-D__stdcall=__attribute__((ms_abi))");
844 strarray_add(comp_args
, "-D__cdecl=__stdcall");
845 strarray_add(comp_args
, "-D__fastcall=__stdcall");
848 strarray_add(comp_args
, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
849 strarray_add(comp_args
, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
850 strarray_add(comp_args
, "-D__fastcall=__attribute__((__fastcall__))");
853 strarray_add(comp_args
, "-D__stdcall=__attribute__((pcs(\"aapcs-vfp\")))");
854 strarray_add(comp_args
, "-D__cdecl=__stdcall");
855 strarray_add(comp_args
, "-D__fastcall=__stdcall");
858 strarray_add(comp_args
, "-D__stdcall=");
859 strarray_add(comp_args
, "-D__cdecl=");
860 strarray_add(comp_args
, "-D__fastcall=");
863 strarray_add(comp_args
, "-D_stdcall=__stdcall");
864 strarray_add(comp_args
, "-D_cdecl=__cdecl");
865 strarray_add(comp_args
, "-D_fastcall=__fastcall");
866 strarray_add(comp_args
, "-D__declspec(x)=__declspec_##x");
867 strarray_add(comp_args
, "-D__declspec_align(x)=__attribute__((aligned(x)))");
868 strarray_add(comp_args
, "-D__declspec_allocate(x)=__attribute__((section(x)))");
869 strarray_add(comp_args
, "-D__declspec_deprecated=__attribute__((deprecated))");
870 strarray_add(comp_args
, "-D__declspec_dllimport=__attribute__((dllimport))");
871 strarray_add(comp_args
, "-D__declspec_dllexport=__attribute__((dllexport))");
872 strarray_add(comp_args
, "-D__declspec_naked=__attribute__((naked))");
873 strarray_add(comp_args
, "-D__declspec_noinline=__attribute__((noinline))");
874 strarray_add(comp_args
, "-D__declspec_noreturn=__attribute__((noreturn))");
875 strarray_add(comp_args
, "-D__declspec_nothrow=__attribute__((nothrow))");
876 strarray_add(comp_args
, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
877 strarray_add(comp_args
, "-D__declspec_selectany=__attribute__((weak))");
878 strarray_add(comp_args
, "-D__declspec_thread=__thread");
881 strarray_add(comp_args
, "-D__int8=char");
882 strarray_add(comp_args
, "-D__int16=short");
883 strarray_add(comp_args
, "-D__int32=int");
884 if (opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
)
885 strarray_add(comp_args
, "-D__int64=long");
887 strarray_add(comp_args
, "-D__int64=long long");
890 strarray_add(comp_args
, "-D__WINE__");
892 /* options we handle explicitly */
893 if (opts
->compile_only
)
894 strarray_add(comp_args
, "-c");
895 if (opts
->output_name
)
897 strarray_add(comp_args
, "-o");
898 strarray_add(comp_args
, opts
->output_name
);
901 /* the rest of the pass-through parameters */
902 for ( j
= 0 ; j
< opts
->compiler_args
->size
; j
++ )
903 strarray_add(comp_args
, opts
->compiler_args
->base
[j
]);
905 /* the language option, if any */
906 if (lang
&& strcmp(lang
, "-xnone"))
907 strarray_add(comp_args
, lang
);
909 /* last, but not least, the files */
910 for ( j
= 0; j
< opts
->files
->size
; j
++ )
912 if (opts
->files
->base
[j
][0] != '-' || !opts
->files
->base
[j
][1]) /* not an option or bare '-' (i.e. stdin) */
913 strarray_add(comp_args
, opts
->files
->base
[j
]);
916 /* standard includes come last in the include search path */
917 if (!opts
->wine_objdir
&& !opts
->nostdinc
)
919 const char *incl_dirs
[] = { INCLUDEDIR
, "/usr/include", "/usr/local/include" };
920 const char *root
= opts
->isysroot
? opts
->isysroot
: opts
->sysroot
? opts
->sysroot
: "";
921 const char *isystem
= gcc_defs
? "-isystem" : "-I";
922 const char *idirafter
= gcc_defs
? "-idirafter" : "-I";
924 if (opts
->use_msvcrt
)
926 if (includedir
) strarray_add( comp_args
, strmake( "%s%s/wine/msvcrt", isystem
, includedir
));
927 for (j
= 0; j
< ARRAY_SIZE(incl_dirs
); j
++)
929 if (j
&& !strcmp( incl_dirs
[0], incl_dirs
[j
] )) continue;
930 strarray_add(comp_args
, strmake( "%s%s%s/wine/msvcrt", isystem
, root
, incl_dirs
[j
] ));
932 strarray_add(comp_args
, "-D__MSVCRT__");
936 strarray_add( comp_args
, strmake( "%s%s/wine/windows", isystem
, includedir
));
937 strarray_add( comp_args
, strmake( "%s%s", idirafter
, includedir
));
939 for (j
= 0; j
< ARRAY_SIZE(incl_dirs
); j
++)
941 if (j
&& !strcmp( incl_dirs
[0], incl_dirs
[j
] )) continue;
942 strarray_add(comp_args
, strmake( "%s%s%s/wine/windows", isystem
, root
, incl_dirs
[j
] ));
943 strarray_add(comp_args
, strmake( "%s%s%s", idirafter
, root
, incl_dirs
[j
] ));
946 else if (opts
->wine_objdir
)
947 strarray_add(comp_args
, strmake("-I%s/include", opts
->wine_objdir
) );
949 spawn(opts
->prefix
, comp_args
, 0);
950 strarray_free(comp_args
);
953 static const char* compile_to_object(struct options
* opts
, const char* file
, const char* lang
)
955 struct options copts
;
958 /* make a copy so we don't change any of the initial stuff */
959 /* a shallow copy is exactly what we want in this case */
960 base_name
= get_basename(file
);
962 copts
.output_name
= get_temp_file(base_name
, ".o");
963 copts
.compile_only
= 1;
964 copts
.files
= strarray_alloc();
965 strarray_add(copts
.files
, file
);
966 compile(&copts
, lang
);
967 strarray_free(copts
.files
);
970 return copts
.output_name
;
973 /* return the initial set of options needed to run winebuild */
974 static strarray
*get_winebuild_args(struct options
*opts
)
976 const char* winebuild
= getenv("WINEBUILD");
977 const char *binary
= NULL
;
978 strarray
*spec_args
= strarray_alloc();
982 binary
= opts
->winebuild
;
983 else if (opts
->wine_objdir
)
984 binary
= strmake( "%s/tools/winebuild/winebuild%s", opts
->wine_objdir
, EXEEXT
);
986 binary
= find_binary( opts
->prefix
, winebuild
);
988 binary
= strmake( "%s/winebuild%s", bindir
, EXEEXT
);
990 binary
= find_binary( opts
->prefix
, "winebuild" );
991 if (!binary
) error( "Could not find winebuild\n" );
992 strarray_add( spec_args
, binary
);
993 if (verbose
) strarray_add( spec_args
, "-v" );
994 if (keep_generated
) strarray_add( spec_args
, "--save-temps" );
997 strarray_add( spec_args
, "--target" );
998 strarray_add( spec_args
, opts
->target
);
1002 for (i
= 0; i
< opts
->prefix
->size
; i
++)
1003 strarray_add( spec_args
, strmake( "-B%s", opts
->prefix
->base
[i
] ));
1005 strarray_addall( spec_args
, opts
->winebuild_args
);
1006 if (opts
->unwind_tables
) strarray_add( spec_args
, "-fasynchronous-unwind-tables" );
1007 else strarray_add( spec_args
, "-fno-asynchronous-unwind-tables" );
1011 static void fixup_constructors( struct options
*opts
, const char *file
)
1013 strarray
*args
= get_winebuild_args( opts
);
1015 strarray_add( args
, "--fixup-ctors" );
1016 strarray_add( args
, file
);
1017 spawn( opts
->prefix
, args
, 0 );
1018 strarray_free( args
);
1021 static void make_wine_builtin( struct options
*opts
, const char *file
)
1023 strarray
*args
= get_winebuild_args( opts
);
1025 strarray_add( args
, "--builtin" );
1026 strarray_add( args
, file
);
1027 spawn( opts
->prefix
, args
, 0 );
1028 strarray_free( args
);
1031 /* check if there is a static lib associated to a given dll */
1032 static char *find_static_lib( const char *dll
)
1034 char *lib
= strmake("%s.a", dll
);
1035 if (get_file_type(lib
) == file_arh
) return lib
;
1040 static const char *find_libgcc(const strarray
*prefix
, const strarray
*link_tool
)
1042 const char *out
= get_temp_file( "find_libgcc", ".out" );
1043 const char *err
= get_temp_file( "find_libgcc", ".err" );
1044 strarray
*link
= strarray_dup( link_tool
);
1045 int sout
= -1, serr
= -1;
1051 strarray_add( link
, "-print-libgcc-file-name" );
1053 sout
= dup( fileno(stdout
) );
1054 freopen( out
, "w", stdout
);
1055 serr
= dup( fileno(stderr
) );
1056 freopen( err
, "w", stderr
);
1057 ret
= spawn( prefix
, link
, 1 );
1060 dup2( sout
, fileno(stdout
) );
1065 dup2( serr
, fileno(stderr
) );
1068 strarray_free( link
);
1070 if (ret
|| stat(out
, &st
) || !st
.st_size
) return NULL
;
1072 libgcc
= xmalloc(st
.st_size
+ 1);
1073 sout
= open(out
, O_RDONLY
);
1074 if (sout
== -1) return NULL
;
1075 cnt
= read(sout
, libgcc
, st
.st_size
);
1078 if ((p
= strchr(libgcc
, '\n'))) *p
= 0;
1083 /* add specified library to the list of files */
1084 static void add_library( struct options
*opts
, strarray
*lib_dirs
, strarray
*files
, const char *library
)
1086 char *static_lib
, *fullname
= 0, *unixlib
;
1088 switch(get_lib_type(opts
->target_platform
, lib_dirs
, library
, "lib", opts
->lib_suffix
, &fullname
))
1091 strarray_add(files
, strmake("-a%s", fullname
));
1094 if (opts
->unix_lib
&& opts
->subsystem
&& !strcmp(opts
->subsystem
, "native"))
1096 if (get_lib_type(opts
->target_platform
, lib_dirs
, library
, "", ".so", &unixlib
) == file_so
)
1098 strarray_add(files
, strmake("-s%s", unixlib
));
1103 strarray_add(files
, strmake("-l%s", library
));
1107 strarray_add(files
, strmake("-d%s", fullname
));
1108 if ((static_lib
= find_static_lib(fullname
)))
1110 strarray_add(files
, strmake("-a%s",static_lib
));
1116 /* keep it anyway, the linker may know what to do with it */
1117 strarray_add(files
, strmake("-l%s", library
));
1123 /* run winebuild to generate the .spec.o file */
1124 static const char *build_spec_obj( struct options
*opts
, const char *spec_file
, const char *output_file
,
1125 strarray
*files
, strarray
*lib_dirs
, const char *entry_point
)
1128 int is_pe
= is_pe_target( opts
);
1129 strarray
*spec_args
= get_winebuild_args( opts
);
1131 const char *spec_o_name
, *output_name
;
1132 int fake_module
= strendswith(output_file
, ".fake");
1134 /* get the filename from the path */
1135 if ((output_name
= strrchr(output_file
, '/'))) output_name
++;
1136 else output_name
= output_file
;
1138 if ((tool
= build_tool_name( opts
, TOOL_CC
))) strarray_add( spec_args
, strmake( "--cc-cmd=%s", strarray_tostring( tool
, " " )));
1139 if (!is_pe
&& (tool
= build_tool_name( opts
, TOOL_LD
))) strarray_add( spec_args
, strmake( "--ld-cmd=%s", strarray_tostring( tool
, " " )));
1141 spec_o_name
= get_temp_file(output_name
, ".spec.o");
1142 if (opts
->force_pointer_size
)
1143 strarray_add(spec_args
, strmake("-m%u", 8 * opts
->force_pointer_size
));
1144 if (opts
->pic
&& !is_pe
) strarray_add(spec_args
, "-fPIC");
1145 strarray_add(spec_args
, opts
->shared
? "--dll" : "--exe");
1148 strarray_add(spec_args
, "--fake-module");
1149 strarray_add(spec_args
, "-o");
1150 strarray_add(spec_args
, output_file
);
1154 strarray_add(spec_args
, "-o");
1155 strarray_add(spec_args
, spec_o_name
);
1159 strarray_add(spec_args
, "-E");
1160 strarray_add(spec_args
, spec_file
);
1165 strarray_add(spec_args
, "-F");
1166 strarray_add(spec_args
, output_name
);
1167 strarray_add(spec_args
, "--subsystem");
1168 strarray_add(spec_args
, opts
->gui_app
? "windows" : "console");
1169 if (opts
->large_address_aware
) strarray_add( spec_args
, "--large-address-aware" );
1172 if (opts
->target_platform
== PLATFORM_WINDOWS
) strarray_add(spec_args
, "--safeseh");
1176 strarray_add(spec_args
, "--entry");
1177 strarray_add(spec_args
, entry_point
);
1180 if (opts
->subsystem
)
1182 strarray_add(spec_args
, "--subsystem");
1183 strarray_add(spec_args
, opts
->subsystem
);
1186 for (i
= 0; i
< lib_dirs
->size
; i
++)
1187 strarray_add(spec_args
, strmake("-L%s", lib_dirs
->base
[i
]));
1191 for (i
= 0; i
< opts
->delayimports
->size
; i
++)
1192 strarray_add(spec_args
, strmake("-d%s", opts
->delayimports
->base
[i
]));
1195 /* add resource files */
1196 for (i
= 0; i
< files
->size
; i
++)
1197 if (files
->base
[i
][1] == 'r') strarray_add(spec_args
, files
->base
[i
]);
1199 /* add other files */
1200 strarray_add(spec_args
, "--");
1201 for (i
= 0; i
< files
->size
; i
++)
1203 switch(files
->base
[i
][1])
1208 strarray_add(spec_args
, files
->base
[i
] + 2);
1213 spawn(opts
->prefix
, spec_args
, 0);
1214 strarray_free (spec_args
);
1218 static void build(struct options
* opts
)
1220 strarray
*lib_dirs
, *files
;
1221 strarray
*link_args
, *implib_args
, *tool
;
1222 char *output_file
, *output_path
;
1223 const char *spec_o_name
= NULL
, *libgcc
= NULL
;
1224 const char *output_name
, *spec_file
, *lang
;
1225 int generate_app_loader
= 1;
1226 const char *crt_lib
= NULL
, *entry_point
= NULL
;
1227 int fake_module
= 0;
1228 int is_pe
= is_pe_target( opts
);
1231 /* NOTE: for the files array we'll use the following convention:
1232 * -axxx: xxx is an archive (.a)
1233 * -dxxx: xxx is a DLL (.def)
1234 * -lxxx: xxx is an unsorted library
1235 * -oxxx: xxx is an object (.o)
1236 * -rxxx: xxx is a resource (.res)
1237 * -sxxx: xxx is a shared lib (.so)
1238 * -xlll: lll is the language (c, c++, etc.)
1241 output_file
= strdup( opts
->output_name
? opts
->output_name
: "a.out" );
1243 /* 'winegcc -o app xxx.exe.so' only creates the load script */
1244 if (opts
->files
->size
== 1 && strendswith(opts
->files
->base
[0], ".exe.so"))
1246 create_file(output_file
, 0755, app_loader_template
, opts
->files
->base
[0]);
1250 /* generate app loader only for .exe */
1251 if (opts
->shared
|| is_pe
|| strendswith(output_file
, ".so"))
1252 generate_app_loader
= 0;
1254 if (strendswith(output_file
, ".fake")) fake_module
= 1;
1256 /* normalize the filename a bit: strip .so, ensure it has proper ext */
1257 if ((output_name
= strrchr(output_file
, '/'))) output_name
++;
1258 else output_name
= output_file
;
1259 if (!strchr(output_name
, '.'))
1260 output_file
= strmake("%s.%s", output_file
, opts
->shared
? "dll" : "exe");
1261 else if (strendswith(output_file
, ".so"))
1262 output_file
[strlen(output_file
) - 3] = 0;
1263 output_path
= is_pe
? output_file
: strmake( "%s.so", output_file
);
1265 /* get the filename from the path */
1266 if ((output_name
= strrchr(output_file
, '/'))) output_name
++;
1267 else output_name
= output_file
;
1269 /* prepare the linking path */
1270 if (!opts
->wine_objdir
)
1272 char *lib_dir
= get_lib_dir( opts
);
1273 lib_dirs
= strarray_dup(opts
->lib_dirs
);
1274 strarray_add( lib_dirs
, strmake( "%s%s", lib_dir
,
1275 get_wine_arch_dir( opts
->target_cpu
, opts
->target_platform
)));
1276 strarray_add( lib_dirs
, lib_dir
);
1280 lib_dirs
= strarray_alloc();
1281 strarray_add(lib_dirs
, strmake("%s/dlls", opts
->wine_objdir
));
1282 strarray_addall(lib_dirs
, opts
->lib_dirs
);
1285 /* mark the files with their appropriate type */
1286 spec_file
= lang
= 0;
1287 files
= strarray_alloc();
1288 for ( j
= 0; j
< opts
->files
->size
; j
++ )
1290 const char* file
= opts
->files
->base
[j
];
1293 switch(get_file_type(file
))
1298 error("Only one spec file can be specified\n");
1302 /* FIXME: invoke wrc to build it */
1303 error("Can't compile .rc file at the moment: %s\n", file
);
1306 strarray_add(files
, strmake("-r%s", file
));
1309 strarray_add(files
, strmake("-o%s", file
));
1312 if (opts
->use_msvcrt
)
1314 const char *p
= strrchr(file
, '/');
1317 if (!strncmp(p
, "libmsvcr", 8) || !strncmp(p
, "libucrt", 7)) crt_lib
= file
;
1319 strarray_add(files
, strmake("-a%s", file
));
1322 strarray_add(files
, strmake("-s%s", file
));
1325 error("File does not exist: %s\n", file
);
1328 file
= compile_to_object(opts
, file
, lang
);
1329 strarray_add(files
, strmake("-o%s", file
));
1333 else if (file
[1] == 'l')
1334 add_library(opts
, lib_dirs
, files
, file
+ 2 );
1335 else if (file
[1] == 'x')
1337 else if(file
[1] == 'W')
1338 strarray_add(files
, file
);
1341 /* add the default libraries, if needed */
1343 if (!opts
->wine_objdir
&& !opts
->nodefaultlibs
)
1347 add_library(opts
, lib_dirs
, files
, "shell32");
1348 add_library(opts
, lib_dirs
, files
, "comdlg32");
1349 add_library(opts
, lib_dirs
, files
, "gdi32");
1351 add_library(opts
, lib_dirs
, files
, "advapi32");
1352 add_library(opts
, lib_dirs
, files
, "user32");
1355 if (!opts
->nodefaultlibs
&& !opts
->unix_lib
)
1357 add_library(opts
, lib_dirs
, files
, "winecrt0");
1358 if (opts
->use_msvcrt
)
1362 if (strncmp( output_name
, "msvcr", 5 ) &&
1363 strncmp( output_name
, "ucrt", 4 ) &&
1364 strcmp( output_name
, "crtdll.dll" ))
1365 add_library(opts
, lib_dirs
, files
, "ucrtbase");
1367 else strarray_add(files
, strmake("-a%s", crt_lib
));
1369 if (opts
->win16_app
) add_library(opts
, lib_dirs
, files
, "kernel");
1370 add_library(opts
, lib_dirs
, files
, "kernel32");
1371 add_library(opts
, lib_dirs
, files
, "ntdll");
1374 /* set default entry point, if needed */
1375 if (!opts
->entry_point
)
1377 if (opts
->subsystem
&& !opts
->unix_lib
&& !strcmp( opts
->subsystem
, "native" ))
1378 entry_point
= (is_pe
&& opts
->target_cpu
== CPU_x86
) ? "DriverEntry@8" : "DriverEntry";
1379 else if (opts
->use_msvcrt
&& !opts
->shared
&& !opts
->win16_app
)
1380 entry_point
= opts
->unicode_app
? "wmainCRTStartup" : "mainCRTStartup";
1382 else entry_point
= opts
->entry_point
;
1384 /* run winebuild to generate the .spec.o file */
1385 if (!(opts
->unix_lib
&& opts
->subsystem
&& !strcmp(opts
->subsystem
, "native")))
1386 spec_o_name
= build_spec_obj( opts
, spec_file
, output_file
, files
, lib_dirs
, entry_point
);
1388 if (fake_module
) return; /* nothing else to do */
1390 /* link everything together now */
1391 link_args
= get_link_args( opts
, output_name
);
1393 if ((opts
->nodefaultlibs
|| opts
->use_msvcrt
) && opts
->target_platform
== PLATFORM_MINGW
)
1395 libgcc
= find_libgcc(opts
->prefix
, link_args
);
1396 if (!libgcc
) libgcc
= "-lgcc";
1399 strarray_add(link_args
, "-o");
1400 strarray_add(link_args
, output_path
);
1402 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
1403 strarray_add(link_args
, strmake("-L%s", lib_dirs
->base
[j
]));
1405 if (is_pe
&& opts
->use_msvcrt
&& !entry_point
&& (opts
->shared
|| opts
->win16_app
))
1406 entry_point
= opts
->target_cpu
== CPU_x86
? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1408 if (is_pe
&& entry_point
)
1410 if (opts
->target_platform
== PLATFORM_WINDOWS
)
1411 strarray_add(link_args
, strmake("-Wl,-entry:%s", entry_point
));
1413 strarray_add(link_args
, strmake("-Wl,--entry,%s%s",
1414 is_pe
&& opts
->target_cpu
== CPU_x86
? "_" : "",
1418 if (spec_o_name
) strarray_add(link_args
, spec_o_name
);
1422 for (j
= 0; j
< opts
->delayimports
->size
; j
++)
1424 if (opts
->target_platform
== PLATFORM_WINDOWS
)
1425 strarray_add(link_args
, strmake("-Wl,-delayload:%s", opts
->delayimports
->base
[j
]));
1427 strarray_add(link_args
, strmake("-Wl,-delayload,%s",opts
->delayimports
->base
[j
]));
1431 for ( j
= 0; j
< files
->size
; j
++ )
1433 const char* name
= files
->base
[j
] + 2;
1434 switch(files
->base
[j
][1])
1437 strarray_add(link_args
, strmake("-l%s", name
));
1441 strarray_add(link_args
, name
);
1444 if (is_pe
&& !opts
->use_msvcrt
&& !opts
->lib_suffix
&& strchr(name
, '/'))
1446 /* turn the path back into -Ldir -lfoo options
1447 * this makes sure that we use the specified libs even
1448 * when mingw adds its own import libs to the link */
1449 char *lib
= xstrdup( name
);
1450 char *p
= strrchr( lib
, '/' );
1453 if (!strncmp( p
, "lib", 3 ) && strcmp( p
, "libmsvcrt.a" ))
1455 char *ext
= strrchr( p
, '.' );
1459 strarray_add(link_args
, strmake("-L%s", lib
));
1460 strarray_add(link_args
, strmake("-l%s", p
));
1466 strarray_add(link_args
, name
);
1469 strarray_add(link_args
, files
->base
[j
]);
1474 if (!opts
->nostdlib
&& !is_pe
)
1476 strarray_add(link_args
, "-lm");
1477 strarray_add(link_args
, "-lc");
1480 if (libgcc
) strarray_add(link_args
, libgcc
);
1482 output_file_name
= output_path
;
1483 output_debug_file
= opts
->debug_file
;
1484 output_implib
= opts
->out_implib
;
1485 atexit( cleanup_output_files
);
1487 spawn(opts
->prefix
, link_args
, 0);
1488 strarray_free (link_args
);
1490 if (opts
->debug_file
&& !strendswith(opts
->debug_file
, ".pdb"))
1492 strarray
*tool
, *objcopy
= build_tool_name(opts
, TOOL_OBJCOPY
);
1494 tool
= strarray_dup(objcopy
);
1495 strarray_add(tool
, "--only-keep-debug");
1496 strarray_add(tool
, output_path
);
1497 strarray_add(tool
, opts
->debug_file
);
1498 spawn(opts
->prefix
, tool
, 1);
1499 strarray_free(tool
);
1501 tool
= strarray_dup(objcopy
);
1502 strarray_add(tool
, "--strip-debug");
1503 strarray_add(tool
, output_path
);
1504 spawn(opts
->prefix
, tool
, 1);
1505 strarray_free(tool
);
1508 strarray_add(tool
, "--add-gnu-debuglink");
1509 strarray_add(tool
, opts
->debug_file
);
1510 strarray_add(tool
, output_path
);
1511 spawn(opts
->prefix
, tool
, 0);
1512 strarray_free(tool
);
1515 if (opts
->out_implib
&& !is_pe
)
1518 error("--out-implib requires a .spec or .def file\n");
1520 implib_args
= get_winebuild_args( opts
);
1521 if ((tool
= build_tool_name( opts
, TOOL_CC
))) strarray_add( implib_args
, strmake( "--cc-cmd=%s", strarray_tostring( tool
, " " )));
1522 if ((tool
= build_tool_name( opts
, TOOL_LD
))) strarray_add( implib_args
, strmake( "--ld-cmd=%s", strarray_tostring( tool
, " " )));
1524 strarray_add(implib_args
, "--implib");
1525 strarray_add(implib_args
, "-o");
1526 strarray_add(implib_args
, opts
->out_implib
);
1527 strarray_add(implib_args
, "--export");
1528 strarray_add(implib_args
, spec_file
);
1530 spawn(opts
->prefix
, implib_args
, 0);
1531 strarray_free (implib_args
);
1534 /* set the base address with prelink if linker support is not present */
1535 if (opts
->prelink
&& !opts
->target
)
1537 if (opts
->prelink
[0] && strcmp(opts
->prelink
,"false"))
1539 strarray
*prelink_args
= strarray_alloc();
1540 strarray_add(prelink_args
, opts
->prelink
);
1541 strarray_add(prelink_args
, "--reloc-only");
1542 strarray_add(prelink_args
, opts
->image_base
);
1543 strarray_add(prelink_args
, output_path
);
1544 spawn(opts
->prefix
, prelink_args
, 1);
1545 strarray_free(prelink_args
);
1549 if (!is_pe
) fixup_constructors( opts
, output_path
);
1550 else if (opts
->wine_builtin
) make_wine_builtin( opts
, output_path
);
1552 /* create the loader script */
1553 if (generate_app_loader
)
1554 create_file(output_file
, 0755, app_loader_template
, strmake("%s.so", output_name
));
1558 static void forward( struct options
*opts
)
1560 strarray
* args
= strarray_alloc();
1562 strarray_addall(args
, get_translator(opts
));
1563 strarray_addall(args
, opts
->compiler_args
);
1564 strarray_addall(args
, opts
->linker_args
);
1565 spawn(opts
->prefix
, args
, 0);
1566 strarray_free (args
);
1569 static int is_linker_arg(const char* arg
)
1571 static const char* link_switches
[] =
1573 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1574 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1575 "-fprofile-generate", "-fprofile-use"
1586 if (strncmp("-Wl,", arg
, 4) == 0) return 1;
1589 if (strcmp("-Xlinker", arg
) == 0) return 1;
1592 if (strcmp("-arch", arg
) == 0) return 1;
1595 if (strncmp("-fuse-ld=", arg
, 9) == 0) return 1;
1598 if (strncmp("-rtlib=", arg
, 7) == 0) return 1;
1602 for (j
= 0; j
< ARRAY_SIZE(link_switches
); j
++)
1603 if (strcmp(link_switches
[j
], arg
) == 0) return 1;
1608 static void parse_target_option( struct options
*opts
, const char *target
)
1610 char *p
, *spec
= xstrdup( target
);
1613 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
1615 /* get the CPU part */
1617 if ((p
= strchr( spec
, '-' )))
1620 for (i
= 0; i
< ARRAY_SIZE(cpu_names
); i
++)
1622 if (!strcmp( cpu_names
[i
].name
, spec
))
1624 opts
->target_cpu
= cpu_names
[i
].cpu
;
1628 if (i
== ARRAY_SIZE(cpu_names
))
1629 error( "Unrecognized CPU '%s'\n", spec
);
1631 else if (!strcmp( spec
, "mingw32" ))
1633 opts
->target_cpu
= CPU_x86
;
1637 error( "Invalid target specification '%s'\n", target
);
1639 /* get the OS part */
1641 opts
->target_platform
= PLATFORM_UNSPECIFIED
; /* default value */
1644 for (i
= 0; i
< ARRAY_SIZE(platform_names
); i
++)
1646 if (!strncmp( platform_names
[i
].name
, p
, strlen(platform_names
[i
].name
) ))
1648 opts
->target_platform
= platform_names
[i
].platform
;
1652 if (opts
->target_platform
!= PLATFORM_UNSPECIFIED
|| !(p
= strchr( p
, '-' ))) break;
1657 opts
->target
= xstrdup( target
);
1660 static int is_option( struct options
*opts
, int i
, const char *option
, const char **option_arg
)
1662 if (!strcmp( opts
->args
->base
[i
], option
))
1664 if (opts
->args
->size
== i
) error( "option %s requires an argument\n", opts
->args
->base
[i
] );
1665 *option_arg
= opts
->args
->base
[i
+ 1];
1668 if (!strncmp( opts
->args
->base
[i
], option
, strlen(option
) ) && opts
->args
->base
[i
][strlen(option
)] == '=')
1670 *option_arg
= opts
->args
->base
[i
] + strlen(option
) + 1;
1676 int main(int argc
, char **argv
)
1678 int i
, c
, next_is_arg
= 0, linking
= 1;
1679 int raw_compiler_arg
, raw_linker_arg
, raw_winebuild_arg
;
1680 const char* option_arg
;
1681 struct options opts
;
1686 signal( SIGHUP
, exit_on_signal
);
1688 signal( SIGTERM
, exit_on_signal
);
1689 signal( SIGINT
, exit_on_signal
);
1690 #ifdef HAVE_SIGADDSET
1691 sigemptyset( &signal_mask
);
1692 sigaddset( &signal_mask
, SIGHUP
);
1693 sigaddset( &signal_mask
, SIGTERM
);
1694 sigaddset( &signal_mask
, SIGINT
);
1696 init_argv0_dir( argv
[0] );
1698 /* setup tmp file removal at exit */
1699 tmp_files
= strarray_alloc();
1700 atexit(clean_temp_files
);
1702 /* initialize options */
1703 memset(&opts
, 0, sizeof(opts
));
1704 opts
.target_cpu
= build_cpu
;
1705 opts
.target_platform
= build_platform
;
1706 opts
.lib_dirs
= strarray_alloc();
1707 opts
.files
= strarray_alloc();
1708 opts
.linker_args
= strarray_alloc();
1709 opts
.compiler_args
= strarray_alloc();
1710 opts
.winebuild_args
= strarray_alloc();
1711 opts
.delayimports
= strarray_alloc();
1712 opts
.args
= strarray_alloc();
1715 /* determine the processor type */
1716 if (strendswith(argv
[0], "winecpp")) opts
.processor
= proc_cpp
;
1717 else if (strendswith(argv
[0], "++")) opts
.processor
= proc_cxx
;
1719 for (i
= 1; i
< argc
; i
++)
1721 char *input_buffer
= NULL
, *iter
, *opt
, *out
;
1725 if (argv
[i
][0] != '@' || (fd
= open( argv
[i
] + 1, O_RDONLY
| O_BINARY
)) == -1)
1727 strarray_add( opts
.args
, argv
[i
] );
1730 if ((fstat( fd
, &st
) == -1)) error( "Cannot stat %s\n", argv
[i
] + 1 );
1733 input_buffer
= xmalloc( st
.st_size
+ 1 );
1734 if (read( fd
, input_buffer
, st
.st_size
) != st
.st_size
) error( "Cannot read %s\n", argv
[i
] + 1 );
1737 for (iter
= input_buffer
; iter
< input_buffer
+ st
.st_size
; iter
++)
1740 while (iter
< input_buffer
+ st
.st_size
&& isspace(*iter
)) iter
++;
1741 if (iter
== input_buffer
+ st
.st_size
) break;
1743 while (iter
< input_buffer
+ st
.st_size
&& (quote
|| !isspace(*iter
)))
1750 else if (*iter
== '\'' || *iter
== '"') quote
= *iter
++;
1753 if (*iter
== '\\' && iter
+ 1 < input_buffer
+ st
.st_size
) iter
++;
1758 strarray_add( opts
.args
, opt
);
1763 for (i
= 0; i
< opts
.args
->size
; i
++)
1765 if (opts
.args
->base
[i
][0] == '-' && opts
.args
->base
[i
][1]) /* option, except '-' alone is stdin, which is a file */
1767 /* determine if this switch is followed by a separate argument */
1770 switch(opts
.args
->base
[i
][1])
1772 case 'x': case 'o': case 'D': case 'U':
1773 case 'I': case 'A': case 'l': case 'u':
1774 case 'b': case 'V': case 'G': case 'L':
1775 case 'B': case 'R': case 'z':
1776 if (opts
.args
->base
[i
][2]) option_arg
= &opts
.args
->base
[i
][2];
1777 else next_is_arg
= 1;
1783 if (strcmp("-aux-info", opts
.args
->base
[i
]) == 0)
1785 if (strcmp("-arch", opts
.args
->base
[i
]) == 0)
1789 if (strcmp("-Xlinker", opts
.args
->base
[i
]) == 0)
1793 c
= opts
.args
->base
[i
][2];
1794 if (c
== 'F' || c
== 'T' || c
== 'Q')
1796 if (opts
.args
->base
[i
][3]) option_arg
= &opts
.args
->base
[i
][3];
1797 else next_is_arg
= 1;
1801 if (strcmp("-framework", opts
.args
->base
[i
]) == 0)
1805 next_is_arg
= strcmp("-target", opts
.args
->base
[i
]) == 0;
1808 next_is_arg
= (strcmp("--param", opts
.args
->base
[i
]) == 0 ||
1809 strcmp("--sysroot", opts
.args
->base
[i
]) == 0 ||
1810 strcmp("--target", opts
.args
->base
[i
]) == 0 ||
1811 strcmp("--wine-objdir", opts
.args
->base
[i
]) == 0 ||
1812 strcmp("--winebuild", opts
.args
->base
[i
]) == 0 ||
1813 strcmp("--lib-suffix", opts
.args
->base
[i
]) == 0);
1818 if (i
+ 1 >= opts
.args
->size
) error("option -%c requires an argument\n", opts
.args
->base
[i
][1]);
1819 option_arg
= opts
.args
->base
[i
+1];
1822 /* determine what options go 'as is' to the linker & the compiler */
1823 raw_linker_arg
= is_linker_arg(opts
.args
->base
[i
]);
1824 raw_compiler_arg
= !raw_linker_arg
;
1825 raw_winebuild_arg
= 0;
1827 /* do a bit of semantic analysis */
1828 switch (opts
.args
->base
[i
][1])
1831 str
= strdup(option_arg
);
1832 if (strendswith(str
, "/")) str
[strlen(str
) - 1] = 0;
1833 if (!opts
.prefix
) opts
.prefix
= strarray_alloc();
1834 strarray_add(opts
.prefix
, str
);
1838 parse_target_option( &opts
, option_arg
);
1839 raw_compiler_arg
= 0;
1842 opts
.version
= xstrdup( option_arg
);
1843 raw_compiler_arg
= 0;
1845 case 'c': /* compile or assemble */
1846 raw_compiler_arg
= 0;
1847 if (opts
.args
->base
[i
][2] == 0) opts
.compile_only
= 1;
1849 case 'S': /* generate assembler code */
1850 case 'E': /* preprocess only */
1851 if (opts
.args
->base
[i
][2] == 0) linking
= 0;
1854 if (strcmp("-fno-short-wchar", opts
.args
->base
[i
]) == 0)
1855 opts
.noshortwchar
= 1;
1856 else if (!strcmp("-fasynchronous-unwind-tables", opts
.args
->base
[i
]))
1857 opts
.unwind_tables
= 1;
1858 else if (!strcmp("-fno-asynchronous-unwind-tables", opts
.args
->base
[i
]))
1859 opts
.unwind_tables
= 0;
1860 else if (!strcmp("-fPIC", opts
.args
->base
[i
]) || !strcmp("-fpic", opts
.args
->base
[i
]))
1862 else if (!strcmp("-fno-PIC", opts
.args
->base
[i
]) || !strcmp("-fno-pic", opts
.args
->base
[i
]))
1866 if (!strcmp( "-isysroot", opts
.args
->base
[i
] )) opts
.isysroot
= opts
.args
->base
[i
+ 1];
1869 strarray_add(opts
.files
, strmake("-l%s", option_arg
));
1870 raw_compiler_arg
= 0;
1873 strarray_add(opts
.lib_dirs
, option_arg
);
1874 raw_compiler_arg
= 0;
1876 case 'M': /* map file generation */
1880 if (strcmp("-mno-cygwin", opts
.args
->base
[i
]) == 0)
1882 opts
.use_msvcrt
= 1;
1883 raw_compiler_arg
= 0;
1884 raw_winebuild_arg
= 1;
1886 else if (strcmp("-mwindows", opts
.args
->base
[i
]) == 0)
1889 raw_compiler_arg
= 0;
1891 else if (strcmp("-mconsole", opts
.args
->base
[i
]) == 0)
1894 raw_compiler_arg
= 0;
1896 else if (strcmp("-municode", opts
.args
->base
[i
]) == 0)
1898 opts
.unicode_app
= 1;
1899 raw_compiler_arg
= 0;
1900 raw_winebuild_arg
= 1;
1902 else if (strcmp("-mthreads", opts
.args
->base
[i
]) == 0)
1904 raw_compiler_arg
= 0;
1906 else if (strcmp("-munix", opts
.args
->base
[i
]) == 0)
1909 raw_compiler_arg
= 0;
1910 raw_winebuild_arg
= 1;
1912 else if (strcmp("-m16", opts
.args
->base
[i
]) == 0)
1915 raw_compiler_arg
= 0;
1916 raw_winebuild_arg
= 1;
1918 else if (strcmp("-m32", opts
.args
->base
[i
]) == 0)
1920 if (opts
.target_cpu
== CPU_x86_64
)
1921 opts
.target_cpu
= CPU_x86
;
1922 else if (opts
.target_cpu
== CPU_ARM64
)
1923 opts
.target_cpu
= CPU_ARM
;
1924 opts
.force_pointer_size
= 4;
1927 else if (strcmp("-m64", opts
.args
->base
[i
]) == 0)
1929 if (opts
.target_cpu
== CPU_x86
)
1930 opts
.target_cpu
= CPU_x86_64
;
1931 else if (opts
.target_cpu
== CPU_ARM
)
1932 opts
.target_cpu
= CPU_ARM64
;
1933 opts
.force_pointer_size
= 8;
1936 else if (!strcmp("-marm", opts
.args
->base
[i
] ) || !strcmp("-mthumb", opts
.args
->base
[i
] ))
1939 raw_winebuild_arg
= 1;
1941 else if (!strncmp("-mcpu=", opts
.args
->base
[i
], 6) ||
1942 !strncmp("-mfpu=", opts
.args
->base
[i
], 6) ||
1943 !strncmp("-march=", opts
.args
->base
[i
], 7) ||
1944 !strncmp("-mfloat-abi=", opts
.args
->base
[i
], 12))
1945 raw_winebuild_arg
= 1;
1948 if (strcmp("-nostdinc", opts
.args
->base
[i
]) == 0)
1950 else if (strcmp("-nodefaultlibs", opts
.args
->base
[i
]) == 0)
1951 opts
.nodefaultlibs
= 1;
1952 else if (strcmp("-nostdlib", opts
.args
->base
[i
]) == 0)
1954 else if (strcmp("-nostartfiles", opts
.args
->base
[i
]) == 0)
1955 opts
.nostartfiles
= 1;
1958 opts
.output_name
= option_arg
;
1959 raw_compiler_arg
= 0;
1962 if (strcmp("-pthread", opts
.args
->base
[i
]) == 0)
1964 raw_compiler_arg
= 1;
1969 if (strcmp("-static", opts
.args
->base
[i
]) == 0)
1971 else if(strcmp("-save-temps", opts
.args
->base
[i
]) == 0)
1973 else if (strncmp("-specs=", opts
.args
->base
[i
], 7) == 0)
1975 else if(strcmp("-shared", opts
.args
->base
[i
]) == 0)
1978 raw_compiler_arg
= raw_linker_arg
= 0;
1980 else if (strcmp("-s", opts
.args
->base
[i
]) == 0 && opts
.target_platform
== PLATFORM_APPLE
)
1982 /* On Mac, change -s into -Wl,-x. ld's -s switch
1983 * is deprecated, and it doesn't work on Tiger with
1991 if (is_option( &opts
, i
, "-target", &option_arg
))
1993 parse_target_option( &opts
, option_arg
);
1994 raw_compiler_arg
= raw_linker_arg
= 0;
1998 if (opts
.args
->base
[i
][2] == 0) verbose
++;
2001 if (strncmp("-Wl,", opts
.args
->base
[i
], 4) == 0)
2004 strarray
* Wl
= strarray_fromstring(opts
.args
->base
[i
] + 4, ",");
2005 for (j
= 0; j
< Wl
->size
; j
++)
2007 if (!strcmp(Wl
->base
[j
], "--image-base") && j
< Wl
->size
- 1)
2009 opts
.image_base
= strdup( Wl
->base
[++j
] );
2012 if (!strcmp(Wl
->base
[j
], "--section-alignment") && j
< Wl
->size
- 1)
2014 opts
.section_align
= strdup( Wl
->base
[++j
] );
2017 if (!strcmp(Wl
->base
[j
], "--file-alignment") && j
< Wl
->size
- 1)
2019 opts
.file_align
= strdup( Wl
->base
[++j
] );
2022 if (!strcmp(Wl
->base
[j
], "--large-address-aware"))
2024 opts
.large_address_aware
= 1;
2027 if (!strcmp(Wl
->base
[j
], "--wine-builtin"))
2029 opts
.wine_builtin
= 1;
2032 if (!strcmp(Wl
->base
[j
], "--subsystem") && j
< Wl
->size
- 1)
2034 opts
.subsystem
= strdup( Wl
->base
[++j
] );
2037 if (!strcmp(Wl
->base
[j
], "--entry") && j
< Wl
->size
- 1)
2039 opts
.entry_point
= strdup( Wl
->base
[++j
] );
2042 if (!strcmp(Wl
->base
[j
], "-delayload") && j
< Wl
->size
- 1)
2044 strarray_add( opts
.delayimports
, Wl
->base
[++j
] );
2047 if (!strcmp(Wl
->base
[j
], "--debug-file") && j
< Wl
->size
- 1)
2049 opts
.debug_file
= strdup( Wl
->base
[++j
] );
2052 if (!strcmp(Wl
->base
[j
], "--whole-archive") ||
2053 !strcmp(Wl
->base
[j
], "--no-whole-archive") ||
2054 !strcmp(Wl
->base
[j
], "--start-group") ||
2055 !strcmp(Wl
->base
[j
], "--end-group"))
2057 strarray_add( opts
.files
, strmake( "-Wl,%s", Wl
->base
[j
] ));
2060 if (!strcmp(Wl
->base
[j
], "--out-implib"))
2062 opts
.out_implib
= strdup( Wl
->base
[++j
] );
2065 if (!strcmp(Wl
->base
[j
], "-static")) linking
= -1;
2066 strarray_add(opts
.linker_args
, strmake("-Wl,%s",Wl
->base
[j
]));
2069 raw_compiler_arg
= raw_linker_arg
= 0;
2071 else if (strncmp("-Wb,", opts
.args
->base
[i
], 4) == 0)
2073 strarray
* Wb
= strarray_fromstring(opts
.args
->base
[i
] + 4, ",");
2074 strarray_addall(opts
.winebuild_args
, Wb
);
2076 /* don't pass it to the compiler, it generates errors */
2077 raw_compiler_arg
= raw_linker_arg
= 0;
2081 lang
= strmake("-x%s", option_arg
);
2082 strarray_add(opts
.files
, lang
);
2083 /* we'll pass these flags ourselves, explicitly */
2084 raw_compiler_arg
= raw_linker_arg
= 0;
2087 if (strcmp("-static", opts
.args
->base
[i
]+1) == 0)
2089 else if (is_option( &opts
, i
, "--sysroot", &option_arg
))
2091 opts
.sysroot
= option_arg
;
2094 else if (is_option( &opts
, i
, "--target", &option_arg
))
2096 parse_target_option( &opts
, option_arg
);
2097 raw_compiler_arg
= raw_linker_arg
= 0;
2099 else if (is_option( &opts
, i
, "--wine-objdir", &option_arg
))
2101 opts
.wine_objdir
= option_arg
;
2102 raw_compiler_arg
= raw_linker_arg
= 0;
2104 else if (is_option( &opts
, i
, "--winebuild", &option_arg
))
2106 opts
.winebuild
= option_arg
;
2107 raw_compiler_arg
= raw_linker_arg
= 0;
2109 else if (is_option( &opts
, i
, "--lib-suffix", &option_arg
))
2111 opts
.lib_suffix
= option_arg
;
2112 raw_compiler_arg
= raw_linker_arg
= 0;
2117 /* put the arg into the appropriate bucket */
2120 strarray_add( opts
.linker_args
, opts
.args
->base
[i
] );
2121 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2122 strarray_add( opts
.linker_args
, opts
.args
->base
[i
+ 1] );
2124 if (raw_compiler_arg
)
2126 strarray_add( opts
.compiler_args
, opts
.args
->base
[i
] );
2127 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2128 strarray_add( opts
.compiler_args
, opts
.args
->base
[i
+ 1] );
2130 if (raw_winebuild_arg
)
2132 strarray_add( opts
.winebuild_args
, opts
.args
->base
[i
] );
2133 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2134 strarray_add( opts
.winebuild_args
, opts
.args
->base
[i
+ 1] );
2137 /* skip the next token if it's an argument */
2138 if (next_is_arg
) i
++;
2142 strarray_add( opts
.files
, opts
.args
->base
[i
] );
2146 if (opts
.processor
== proc_cpp
) linking
= 0;
2147 if (linking
== -1) error("Static linking is not supported\n");
2149 if (!opts
.wine_objdir
&& is_pe_target( &opts
)) opts
.use_msvcrt
= 1;
2151 if (opts
.files
->size
== 0) forward(&opts
);
2152 else if (linking
) build(&opts
);
2153 else compile(&opts
, lang
);
2155 output_file_name
= NULL
;
2156 output_debug_file
= NULL
;
2157 output_implib
= NULL
;