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
);
339 const char *llvm_base
;
343 { "gcc", "clang --driver-mode=gcc", CC
},
344 { "g++", "clang --driver-mode=g++", CXX
},
345 { "cpp", "clang --driver-mode=cpp", CPP
},
346 { "ld", "ld.lld", LD
},
347 { "objcopy", "llvm-objcopy" },
350 static strarray
* build_tool_name( struct options
*opts
, enum tool tool
)
352 const char *base
= tool_names
[tool
].base
;
353 const char *llvm_base
= tool_names
[tool
].llvm_base
;
354 const char *deflt
= tool_names
[tool
].deflt
;
359 if (opts
->target
&& opts
->version
)
361 str
= strmake("%s-%s-%s", opts
->target
, base
, opts
->version
);
363 else if (opts
->target
)
365 str
= strmake("%s-%s", opts
->target
, base
);
367 else if (opts
->version
)
369 str
= strmake("%s-%s", base
, opts
->version
);
372 str
= xstrdup((deflt
&& *deflt
) ? deflt
: base
);
374 if ((path
= find_binary( opts
->prefix
, str
))) return strarray_fromstring( path
, " " );
376 if (!opts
->version
) str
= xstrdup( llvm_base
);
377 else str
= strmake( "%s-%s", llvm_base
, opts
->version
);
378 path
= find_binary( opts
->prefix
, str
);
381 error( "Could not find %s\n", base
);
385 ret
= strarray_fromstring( path
, " " );
386 if (!strncmp( llvm_base
, "clang", 5 ))
390 strarray_add( ret
, "-target" );
391 strarray_add( ret
, opts
->target
);
393 strarray_add( ret
, "-Wno-unused-command-line-argument" );
394 strarray_add( ret
, "-fuse-ld=lld" );
399 static strarray
* get_translator(struct options
*opts
)
403 switch(opts
->processor
)
418 return build_tool_name( opts
, tool
);
421 static int try_link( const strarray
*prefix
, const strarray
*link_tool
, const char *cflags
)
423 const char *in
= get_temp_file( "try_link", ".c" );
424 const char *out
= get_temp_file( "try_link", ".out" );
425 const char *err
= get_temp_file( "try_link", ".err" );
426 strarray
*link
= strarray_dup( link_tool
);
427 int sout
= -1, serr
= -1;
430 create_file( in
, 0644, "int main(void){return 1;}\n" );
432 strarray_add( link
, "-o" );
433 strarray_add( link
, out
);
434 strarray_addall( link
, strarray_fromstring( cflags
, " " ) );
435 strarray_add( link
, in
);
437 sout
= dup( fileno(stdout
) );
438 freopen( err
, "w", stdout
);
439 serr
= dup( fileno(stderr
) );
440 freopen( err
, "w", stderr
);
441 ret
= spawn( prefix
, link
, 1 );
444 dup2( sout
, fileno(stdout
) );
449 dup2( serr
, fileno(stderr
) );
452 strarray_free( link
);
456 static strarray
*get_link_args( struct options
*opts
, const char *output_name
)
458 strarray
*link_args
= get_translator( opts
);
459 strarray
*flags
= strarray_alloc();
461 strarray_addall( link_args
, opts
->linker_args
);
463 if (verbose
> 1) strarray_add( flags
, "-v" );
465 switch (opts
->target_platform
)
468 strarray_add( flags
, "-bundle" );
469 strarray_add( flags
, "-multiply_defined" );
470 strarray_add( flags
, "suppress" );
471 if (opts
->target_cpu
== CPU_POWERPC
)
473 strarray_add( flags
, "-read_only_relocs" );
474 strarray_add( flags
, "warning" );
476 if (opts
->image_base
)
478 strarray_add( flags
, "-image_base" );
479 strarray_add( flags
, opts
->image_base
);
481 if (opts
->strip
) strarray_add( flags
, "-Wl,-x" );
482 strarray_addall( link_args
, flags
);
485 case PLATFORM_SOLARIS
:
487 char *mapfile
= get_temp_file( output_name
, ".map" );
488 const char *align
= opts
->section_align
? opts
->section_align
: "0x1000";
490 create_file( mapfile
, 0644, "text = A%s;\ndata = A%s;\n", align
, align
);
491 strarray_add( flags
, strmake("-Wl,-M,%s", mapfile
) );
492 strarray_add( tmp_files
, mapfile
);
496 case PLATFORM_ANDROID
:
497 /* the Android loader requires a soname for all libraries */
498 strarray_add( flags
, strmake( "-Wl,-soname,%s.so", output_name
));
502 case PLATFORM_CYGWIN
:
503 if (opts
->shared
|| opts
->win16_app
)
505 strarray_add( flags
, "-shared" );
506 strarray_add( flags
, "-Wl,--kill-at" );
508 else strarray_add( flags
, opts
->gui_app
? "-mwindows" : "-mconsole" );
510 if (opts
->unicode_app
) strarray_add( flags
, "-municode" );
511 if (opts
->nodefaultlibs
|| opts
->use_msvcrt
) strarray_add( flags
, "-nodefaultlibs" );
512 if (opts
->nostartfiles
|| opts
->use_msvcrt
) strarray_add( flags
, "-nostartfiles" );
513 if (opts
->subsystem
) strarray_add( flags
, strmake("-Wl,--subsystem,%s", opts
->subsystem
));
515 strarray_add( flags
, "-Wl,--nxcompat" );
517 if (opts
->image_base
) strarray_add( flags
, strmake("-Wl,--image-base,%s", opts
->image_base
));
519 if (opts
->large_address_aware
&& opts
->target_cpu
== CPU_x86
)
520 strarray_add( flags
, "-Wl,--large-address-aware" );
522 /* make sure we don't need a libgcc_s dll on Windows */
523 strarray_add( flags
, "-static-libgcc" );
525 if (opts
->debug_file
&& strendswith(opts
->debug_file
, ".pdb"))
526 strarray_add(link_args
, strmake("-Wl,-pdb,%s", opts
->debug_file
));
528 if (opts
->out_implib
)
529 strarray_add(link_args
, strmake("-Wl,--out-implib,%s", opts
->out_implib
));
531 if (!try_link( opts
->prefix
, link_args
, "-Wl,--file-alignment,0x1000" ))
532 strarray_add( link_args
, strmake( "-Wl,--file-alignment,%s",
533 opts
->file_align
? opts
->file_align
: "0x1000" ));
534 else if (!try_link( opts
->prefix
, link_args
, "-Wl,-Xlink=-filealign:0x1000" ))
535 /* lld from llvm 10 does not support mingw style --file-alignment,
536 * but it's possible to use msvc syntax */
537 strarray_add( link_args
, strmake( "-Wl,-Xlink=-filealign:%s",
538 opts
->file_align
? opts
->file_align
: "0x1000" ));
540 strarray_addall( link_args
, flags
);
543 case PLATFORM_WINDOWS
:
544 if (opts
->shared
|| opts
->win16_app
)
546 strarray_add( flags
, "-shared" );
547 strarray_add( flags
, "-Wl,-kill-at" );
549 if (opts
->unicode_app
) strarray_add( flags
, "-municode" );
550 if (opts
->nodefaultlibs
|| opts
->use_msvcrt
) strarray_add( flags
, "-nodefaultlibs" );
551 if (opts
->nostartfiles
|| opts
->use_msvcrt
) strarray_add( flags
, "-nostartfiles" );
552 if (opts
->image_base
) strarray_add( flags
, strmake("-Wl,-base:%s", opts
->image_base
));
554 strarray_add( flags
, strmake("-Wl,-subsystem:%s", opts
->subsystem
));
556 strarray_add( flags
, strmake("-Wl,-subsystem:%s", opts
->gui_app
? "windows" : "console" ));
558 if (opts
->debug_file
&& strendswith(opts
->debug_file
, ".pdb"))
560 strarray_add(link_args
, "-Wl,-debug");
561 strarray_add(link_args
, strmake("-Wl,-pdb:%s", opts
->debug_file
));
563 else if (!opts
->strip
)
564 strarray_add(link_args
, "-Wl,-debug:dwarf");
566 if (opts
->out_implib
)
567 strarray_add(link_args
, strmake("-Wl,-implib:%s", opts
->out_implib
));
569 strarray_add( link_args
, strmake( "-Wl,-filealign:%s", opts
->file_align
? opts
->file_align
: "0x1000" ));
571 strarray_addall( link_args
, flags
);
575 if (opts
->image_base
)
577 if (!try_link( opts
->prefix
, link_args
, strmake("-Wl,-Ttext-segment=%s", opts
->image_base
)) )
578 strarray_add( flags
, strmake("-Wl,-Ttext-segment=%s", opts
->image_base
) );
580 opts
->prelink
= PRELINK
;
582 if (!try_link( opts
->prefix
, link_args
, "-Wl,-z,max-page-size=0x1000"))
583 strarray_add( flags
, "-Wl,-z,max-page-size=0x1000");
587 /* generic Unix shared library flags */
589 strarray_add( link_args
, "-shared" );
590 strarray_add( link_args
, "-Wl,-Bsymbolic" );
591 if (!opts
->noshortwchar
&& opts
->target_cpu
== CPU_ARM
)
592 strarray_add( flags
, "-Wl,--no-wchar-size-warning" );
593 if (!try_link( opts
->prefix
, link_args
, "-Wl,-z,defs" ))
594 strarray_add( flags
, "-Wl,-z,defs" );
596 strarray_addall( link_args
, flags
);
600 /* check that file is a library for the correct platform */
601 static int check_platform( struct options
*opts
, const char *file
)
603 int ret
= 0, fd
= open( file
, O_RDONLY
);
606 unsigned char header
[16];
607 if (read( fd
, header
, sizeof(header
) ) == sizeof(header
))
609 /* FIXME: only ELF is supported, platform is not checked beyond 32/64 */
610 if (!memcmp( header
, "\177ELF", 4 ))
612 if (header
[4] == 2) /* 64-bit */
613 ret
= (opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
);
615 ret
= (opts
->target_cpu
!= CPU_x86_64
&& opts
->target_cpu
!= CPU_ARM64
);
623 static const char *get_multiarch_dir( enum target_cpu cpu
)
627 case CPU_x86
: return "/i386-linux-gnu";
628 case CPU_x86_64
: return "/x86_64-linux-gnu";
629 case CPU_ARM
: return "/arm-linux-gnueabi";
630 case CPU_ARM64
: return "/aarch64-linux-gnu";
631 case CPU_POWERPC
: return "/powerpc-linux-gnu";
638 static char *get_lib_dir( struct options
*opts
)
640 const char *stdlibpath
[] = { libdir
, LIBDIR
, "/usr/lib", "/usr/local/lib", "/lib" };
641 static const char ntdll
[] = "/wine/ntdll.so";
642 const char *bit_suffix
, *other_bit_suffix
, *build_multiarch
, *target_multiarch
;
643 const char *root
= opts
->sysroot
? opts
->sysroot
: "";
645 size_t build_len
, target_len
;
647 bit_suffix
= opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
? "64" : "32";
648 other_bit_suffix
= opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
? "32" : "64";
649 build_multiarch
= get_multiarch_dir( build_cpu
);
650 target_multiarch
= get_multiarch_dir( opts
->target_cpu
);
651 build_len
= strlen( build_multiarch
);
652 target_len
= strlen( target_multiarch
);
654 for (i
= 0; i
< ARRAY_SIZE(stdlibpath
); i
++)
656 const char *root
= (i
&& opts
->sysroot
) ? opts
->sysroot
: "";
659 if (!stdlibpath
[i
]) continue;
660 buffer
= xmalloc( strlen(root
) + strlen(stdlibpath
[i
]) +
661 strlen("/arm-linux-gnueabi") + strlen(ntdll
) + 1 );
662 strcpy( buffer
, root
);
663 strcat( buffer
, stdlibpath
[i
] );
664 p
= buffer
+ strlen(buffer
);
665 while (p
> buffer
&& p
[-1] == '/') p
--;
667 if (check_platform( opts
, buffer
)) goto found
;
668 if (p
> buffer
+ 2 && (!memcmp( p
- 2, "32", 2 ) || !memcmp( p
- 2, "64", 2 )))
672 if (check_platform( opts
, buffer
)) goto found
;
674 strcpy( p
, bit_suffix
);
676 if (check_platform( opts
, buffer
)) goto found
;
677 strcpy( p
, target_multiarch
);
679 if (check_platform( opts
, buffer
)) goto found
;
681 strcpy( buffer
, root
);
682 strcat( buffer
, stdlibpath
[i
] );
683 p
= buffer
+ strlen(buffer
);
684 while (p
> buffer
&& p
[-1] == '/') p
--;
687 /* try to fixup each parent dirs named lib, lib32 or lib64 with target bitness suffix */
691 while (p
> buffer
&& *p
!= '/') p
--;
692 if (*p
!= '/') break;
694 /* try s/$build_cpu/$target_cpu/ on multiarch */
695 if (build_cpu
!= opts
->target_cpu
&& !memcmp( p
, build_multiarch
, build_len
) && p
[build_len
] == '/')
697 memmove( p
+ target_len
, p
+ build_len
, strlen( p
+ build_len
) + 1 );
698 memcpy( p
, target_multiarch
, target_len
);
699 if (check_platform( opts
, buffer
)) goto found
;
700 memmove( p
+ build_len
, p
+ target_len
, strlen( p
+ target_len
) + 1 );
701 memcpy( p
, build_multiarch
, build_len
);
704 if (memcmp( p
+ 1, "lib", 3 )) continue;
707 memmove( p
+ 6, p
+ 4, strlen( p
+ 4 ) + 1 );
708 memcpy( p
+ 4, bit_suffix
, 2 );
709 if (check_platform( opts
, buffer
)) goto found
;
710 memmove( p
+ 4, p
+ 6, strlen( p
+ 6 ) + 1 );
712 else if (!memcmp( p
+ 4, other_bit_suffix
, 2 ) && p
[6] == '/')
714 memcpy( p
+ 4, bit_suffix
, 2 );
715 if (check_platform( opts
, buffer
)) goto found
;
716 memmove( p
+ 4, p
+ 6, strlen( p
+ 6 ) + 1 );
717 if (check_platform( opts
, buffer
)) goto found
;
718 memmove( p
+ 6, p
+ 4, strlen( p
+ 4 ) + 1 );
719 memcpy( p
+ 4, other_bit_suffix
, 2 );
727 buffer
[strlen(buffer
) - strlen(ntdll
)] = 0;
730 return strmake( "%s%s", root
, LIBDIR
);
733 static void init_argv0_dir( const char *argv0
)
738 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
739 dir
= realpath( "/proc/self/exe", NULL
);
740 #elif defined (__FreeBSD__) || defined(__DragonFly__)
741 dir
= realpath( "/proc/curproc/file", NULL
);
743 dir
= realpath( argv0
, NULL
);
746 if (!(p
= strrchr( dir
, '/' ))) return;
750 includedir
= strmake( "%s/%s", dir
, BIN_TO_INCLUDEDIR
);
751 libdir
= strmake( "%s/%s", dir
, BIN_TO_LIBDIR
);
755 static void compile(struct options
* opts
, const char* lang
)
757 strarray
* comp_args
= strarray_alloc();
763 strarray_addall(comp_args
, get_translator(opts
));
764 if (opts
->force_pointer_size
)
765 strarray_add( comp_args
, strmake("-m%u", 8 * opts
->force_pointer_size
) );
766 switch(opts
->processor
)
768 case proc_cpp
: gcc_defs
= 1; break;
769 case proc_as
: gcc_defs
= 0; break;
770 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
771 /* mixing different C and C++ compilers isn't supported in configure anyway */
774 gcc
= build_tool_name(opts
, TOOL_CC
);
775 gpp
= build_tool_name(opts
, TOOL_CXX
);
776 for ( j
= 0; !gcc_defs
&& j
< comp_args
->size
; j
++ )
778 const char *cc
= comp_args
->base
[j
];
780 for (i
= 0; !gcc_defs
&& i
< gcc
->size
; i
++)
781 gcc_defs
= gcc
->base
[i
][0] != '-' && strendswith(cc
, gcc
->base
[i
]);
782 for (i
= 0; !gcc_defs
&& i
< gpp
->size
; i
++)
783 gcc_defs
= gpp
->base
[i
][0] != '-' && strendswith(cc
, gpp
->base
[i
]);
790 if (opts
->target_platform
== PLATFORM_WINDOWS
|| opts
->target_platform
== PLATFORM_CYGWIN
|| opts
->target_platform
== PLATFORM_MINGW
)
791 goto no_compat_defines
;
793 if (opts
->processor
!= proc_cpp
)
795 if (gcc_defs
&& !opts
->wine_objdir
&& !opts
->noshortwchar
)
797 strarray_add(comp_args
, "-fshort-wchar");
798 strarray_add(comp_args
, "-DWINE_UNICODE_NATIVE");
800 strarray_add(comp_args
, "-D_REENTRANT");
802 strarray_add(comp_args
, "-fPIC");
804 strarray_add(comp_args
, "-fno-PIC");
807 if (opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
)
809 strarray_add(comp_args
, "-DWIN64");
810 strarray_add(comp_args
, "-D_WIN64");
811 strarray_add(comp_args
, "-D__WIN64");
812 strarray_add(comp_args
, "-D__WIN64__");
815 strarray_add(comp_args
, "-DWIN32");
816 strarray_add(comp_args
, "-D_WIN32");
817 strarray_add(comp_args
, "-D__WIN32");
818 strarray_add(comp_args
, "-D__WIN32__");
819 strarray_add(comp_args
, "-D__WINNT");
820 strarray_add(comp_args
, "-D__WINNT__");
824 switch (opts
->target_cpu
)
828 strarray_add(comp_args
, "-D__stdcall=__attribute__((ms_abi))");
829 strarray_add(comp_args
, "-D__cdecl=__stdcall");
830 strarray_add(comp_args
, "-D__fastcall=__stdcall");
833 strarray_add(comp_args
, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
834 strarray_add(comp_args
, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
835 strarray_add(comp_args
, "-D__fastcall=__attribute__((__fastcall__))");
838 strarray_add(comp_args
, "-D__stdcall=__attribute__((pcs(\"aapcs-vfp\")))");
839 strarray_add(comp_args
, "-D__cdecl=__stdcall");
840 strarray_add(comp_args
, "-D__fastcall=__stdcall");
843 strarray_add(comp_args
, "-D__stdcall=");
844 strarray_add(comp_args
, "-D__cdecl=");
845 strarray_add(comp_args
, "-D__fastcall=");
848 strarray_add(comp_args
, "-D_stdcall=__stdcall");
849 strarray_add(comp_args
, "-D_cdecl=__cdecl");
850 strarray_add(comp_args
, "-D_fastcall=__fastcall");
851 strarray_add(comp_args
, "-D__declspec(x)=__declspec_##x");
852 strarray_add(comp_args
, "-D__declspec_align(x)=__attribute__((aligned(x)))");
853 strarray_add(comp_args
, "-D__declspec_allocate(x)=__attribute__((section(x)))");
854 strarray_add(comp_args
, "-D__declspec_deprecated=__attribute__((deprecated))");
855 strarray_add(comp_args
, "-D__declspec_dllimport=__attribute__((dllimport))");
856 strarray_add(comp_args
, "-D__declspec_dllexport=__attribute__((dllexport))");
857 strarray_add(comp_args
, "-D__declspec_naked=__attribute__((naked))");
858 strarray_add(comp_args
, "-D__declspec_noinline=__attribute__((noinline))");
859 strarray_add(comp_args
, "-D__declspec_noreturn=__attribute__((noreturn))");
860 strarray_add(comp_args
, "-D__declspec_nothrow=__attribute__((nothrow))");
861 strarray_add(comp_args
, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
862 strarray_add(comp_args
, "-D__declspec_selectany=__attribute__((weak))");
863 strarray_add(comp_args
, "-D__declspec_thread=__thread");
866 strarray_add(comp_args
, "-D__int8=char");
867 strarray_add(comp_args
, "-D__int16=short");
868 strarray_add(comp_args
, "-D__int32=int");
869 if (opts
->target_cpu
== CPU_x86_64
|| opts
->target_cpu
== CPU_ARM64
)
870 strarray_add(comp_args
, "-D__int64=long");
872 strarray_add(comp_args
, "-D__int64=long long");
875 strarray_add(comp_args
, "-D__WINE__");
877 /* options we handle explicitly */
878 if (opts
->compile_only
)
879 strarray_add(comp_args
, "-c");
880 if (opts
->output_name
)
882 strarray_add(comp_args
, "-o");
883 strarray_add(comp_args
, opts
->output_name
);
886 /* the rest of the pass-through parameters */
887 for ( j
= 0 ; j
< opts
->compiler_args
->size
; j
++ )
888 strarray_add(comp_args
, opts
->compiler_args
->base
[j
]);
890 /* the language option, if any */
891 if (lang
&& strcmp(lang
, "-xnone"))
892 strarray_add(comp_args
, lang
);
894 /* last, but not least, the files */
895 for ( j
= 0; j
< opts
->files
->size
; j
++ )
897 if (opts
->files
->base
[j
][0] != '-' || !opts
->files
->base
[j
][1]) /* not an option or bare '-' (i.e. stdin) */
898 strarray_add(comp_args
, opts
->files
->base
[j
]);
901 /* standard includes come last in the include search path */
902 if (!opts
->wine_objdir
&& !opts
->nostdinc
)
904 const char *incl_dirs
[] = { INCLUDEDIR
, "/usr/include", "/usr/local/include" };
905 const char *root
= opts
->isysroot
? opts
->isysroot
: opts
->sysroot
? opts
->sysroot
: "";
906 const char *isystem
= gcc_defs
? "-isystem" : "-I";
908 if (opts
->use_msvcrt
)
910 if (includedir
) strarray_add( comp_args
, strmake( "%s%s/wine/msvcrt", isystem
, includedir
));
911 for (j
= 0; j
< ARRAY_SIZE(incl_dirs
); j
++)
913 if (j
&& !strcmp( incl_dirs
[0], incl_dirs
[j
] )) continue;
914 strarray_add(comp_args
, strmake( "%s%s%s/wine/msvcrt", isystem
, root
, incl_dirs
[j
] ));
916 strarray_add(comp_args
, "-D__MSVCRT__");
918 if (includedir
) strarray_add( comp_args
, strmake( "%s%s/wine/windows", isystem
, includedir
));
919 for (j
= 0; j
< ARRAY_SIZE(incl_dirs
); j
++)
921 if (j
&& !strcmp( incl_dirs
[0], incl_dirs
[j
] )) continue;
922 strarray_add(comp_args
, strmake( "%s%s%s/wine/windows", isystem
, root
, incl_dirs
[j
] ));
924 if (includedir
) strarray_add( comp_args
, strmake( "%s%s", isystem
, includedir
));
925 for (j
= 0; j
< ARRAY_SIZE(incl_dirs
); j
++)
927 if (j
&& !strcmp( incl_dirs
[0], incl_dirs
[j
] )) continue;
928 strarray_add(comp_args
, strmake( "%s%s%s", isystem
, root
, incl_dirs
[j
] ));
931 else if (opts
->wine_objdir
)
932 strarray_add(comp_args
, strmake("-I%s/include", opts
->wine_objdir
) );
934 spawn(opts
->prefix
, comp_args
, 0);
935 strarray_free(comp_args
);
938 static const char* compile_to_object(struct options
* opts
, const char* file
, const char* lang
)
940 struct options copts
;
943 /* make a copy so we don't change any of the initial stuff */
944 /* a shallow copy is exactly what we want in this case */
945 base_name
= get_basename(file
);
947 copts
.output_name
= get_temp_file(base_name
, ".o");
948 copts
.compile_only
= 1;
949 copts
.files
= strarray_alloc();
950 strarray_add(copts
.files
, file
);
951 compile(&copts
, lang
);
952 strarray_free(copts
.files
);
955 return copts
.output_name
;
958 /* return the initial set of options needed to run winebuild */
959 static strarray
*get_winebuild_args(struct options
*opts
)
961 const char* winebuild
= getenv("WINEBUILD");
962 const char *binary
= NULL
;
963 strarray
*spec_args
= strarray_alloc();
967 binary
= opts
->winebuild
;
968 else if (opts
->wine_objdir
)
969 binary
= strmake( "%s/tools/winebuild/winebuild%s", opts
->wine_objdir
, EXEEXT
);
971 binary
= find_binary( opts
->prefix
, winebuild
);
973 binary
= strmake( "%s/winebuild%s", bindir
, EXEEXT
);
975 binary
= find_binary( opts
->prefix
, "winebuild" );
976 if (!binary
) error( "Could not find winebuild\n" );
977 strarray_add( spec_args
, binary
);
978 if (verbose
) strarray_add( spec_args
, "-v" );
979 if (keep_generated
) strarray_add( spec_args
, "--save-temps" );
982 strarray_add( spec_args
, "--target" );
983 strarray_add( spec_args
, opts
->target
);
987 for (i
= 0; i
< opts
->prefix
->size
; i
++)
988 strarray_add( spec_args
, strmake( "-B%s", opts
->prefix
->base
[i
] ));
990 strarray_addall( spec_args
, opts
->winebuild_args
);
991 if (opts
->unwind_tables
) strarray_add( spec_args
, "-fasynchronous-unwind-tables" );
992 else strarray_add( spec_args
, "-fno-asynchronous-unwind-tables" );
996 static void fixup_constructors( struct options
*opts
, const char *file
)
998 strarray
*args
= get_winebuild_args( opts
);
1000 strarray_add( args
, "--fixup-ctors" );
1001 strarray_add( args
, file
);
1002 spawn( opts
->prefix
, args
, 0 );
1003 strarray_free( args
);
1006 static void make_wine_builtin( struct options
*opts
, const char *file
)
1008 strarray
*args
= get_winebuild_args( opts
);
1010 strarray_add( args
, "--builtin" );
1011 strarray_add( args
, file
);
1012 spawn( opts
->prefix
, args
, 0 );
1013 strarray_free( args
);
1016 /* check if there is a static lib associated to a given dll */
1017 static char *find_static_lib( const char *dll
)
1019 char *lib
= strmake("%s.a", dll
);
1020 if (get_file_type(lib
) == file_arh
) return lib
;
1025 static const char *find_libgcc(const strarray
*prefix
, const strarray
*link_tool
)
1027 const char *out
= get_temp_file( "find_libgcc", ".out" );
1028 const char *err
= get_temp_file( "find_libgcc", ".err" );
1029 strarray
*link
= strarray_dup( link_tool
);
1030 int sout
= -1, serr
= -1;
1036 strarray_add( link
, "-print-libgcc-file-name" );
1038 sout
= dup( fileno(stdout
) );
1039 freopen( out
, "w", stdout
);
1040 serr
= dup( fileno(stderr
) );
1041 freopen( err
, "w", stderr
);
1042 ret
= spawn( prefix
, link
, 1 );
1045 dup2( sout
, fileno(stdout
) );
1050 dup2( serr
, fileno(stderr
) );
1053 strarray_free( link
);
1055 if (ret
|| stat(out
, &st
) || !st
.st_size
) return NULL
;
1057 libgcc
= xmalloc(st
.st_size
+ 1);
1058 sout
= open(out
, O_RDONLY
);
1059 if (sout
== -1) return NULL
;
1060 cnt
= read(sout
, libgcc
, st
.st_size
);
1063 if ((p
= strchr(libgcc
, '\n'))) *p
= 0;
1068 /* add specified library to the list of files */
1069 static void add_library( struct options
*opts
, strarray
*lib_dirs
, strarray
*files
, const char *library
)
1071 char *static_lib
, *fullname
= 0;
1073 switch(get_lib_type(opts
->target_platform
, lib_dirs
, library
, opts
->lib_suffix
, &fullname
))
1076 strarray_add(files
, strmake("-a%s", fullname
));
1079 strarray_add(files
, strmake("-d%s", fullname
));
1080 if ((static_lib
= find_static_lib(fullname
)))
1082 strarray_add(files
, strmake("-a%s",static_lib
));
1088 /* keep it anyway, the linker may know what to do with it */
1089 strarray_add(files
, strmake("-l%s", library
));
1095 static void build(struct options
* opts
)
1097 strarray
*lib_dirs
, *files
;
1098 strarray
*spec_args
, *link_args
, *implib_args
, *tool
;
1099 char *output_file
, *output_path
;
1100 const char *spec_o_name
, *libgcc
= NULL
;
1101 const char *output_name
, *spec_file
, *lang
;
1102 int generate_app_loader
= 1;
1103 const char *crt_lib
= NULL
, *entry_point
= NULL
;
1104 int fake_module
= 0;
1105 int is_pe
= (opts
->target_platform
== PLATFORM_WINDOWS
|| opts
->target_platform
== PLATFORM_CYGWIN
|| opts
->target_platform
== PLATFORM_MINGW
);
1108 /* NOTE: for the files array we'll use the following convention:
1109 * -axxx: xxx is an archive (.a)
1110 * -dxxx: xxx is a DLL (.def)
1111 * -lxxx: xxx is an unsorted library
1112 * -oxxx: xxx is an object (.o)
1113 * -rxxx: xxx is a resource (.res)
1114 * -sxxx: xxx is a shared lib (.so)
1115 * -xlll: lll is the language (c, c++, etc.)
1118 output_file
= strdup( opts
->output_name
? opts
->output_name
: "a.out" );
1120 /* 'winegcc -o app xxx.exe.so' only creates the load script */
1121 if (opts
->files
->size
== 1 && strendswith(opts
->files
->base
[0], ".exe.so"))
1123 create_file(output_file
, 0755, app_loader_template
, opts
->files
->base
[0]);
1127 /* generate app loader only for .exe */
1128 if (opts
->shared
|| is_pe
|| strendswith(output_file
, ".so"))
1129 generate_app_loader
= 0;
1131 if (strendswith(output_file
, ".fake")) fake_module
= 1;
1133 /* normalize the filename a bit: strip .so, ensure it has proper ext */
1134 if ((output_name
= strrchr(output_file
, '/'))) output_name
++;
1135 else output_name
= output_file
;
1136 if (!strchr(output_name
, '.'))
1137 output_file
= strmake("%s.%s", output_file
, opts
->shared
? "dll" : "exe");
1138 else if (strendswith(output_file
, ".so"))
1139 output_file
[strlen(output_file
) - 3] = 0;
1140 output_path
= is_pe
? output_file
: strmake( "%s.so", output_file
);
1142 /* get the filename from the path */
1143 if ((output_name
= strrchr(output_file
, '/'))) output_name
++;
1144 else output_name
= output_file
;
1146 /* prepare the linking path */
1147 if (!opts
->wine_objdir
)
1149 char *lib_dir
= get_lib_dir( opts
);
1150 lib_dirs
= strarray_dup(opts
->lib_dirs
);
1151 strarray_add( lib_dirs
, strmake( "%s/wine", lib_dir
));
1152 strarray_add( lib_dirs
, lib_dir
);
1156 lib_dirs
= strarray_alloc();
1157 strarray_add(lib_dirs
, strmake("%s/dlls", opts
->wine_objdir
));
1158 strarray_add(lib_dirs
, strmake("%s/libs/wine", opts
->wine_objdir
));
1159 strarray_addall(lib_dirs
, opts
->lib_dirs
);
1162 /* mark the files with their appropriate type */
1163 spec_file
= lang
= 0;
1164 files
= strarray_alloc();
1165 for ( j
= 0; j
< opts
->files
->size
; j
++ )
1167 const char* file
= opts
->files
->base
[j
];
1170 switch(get_file_type(file
))
1175 error("Only one spec file can be specified\n");
1179 /* FIXME: invoke wrc to build it */
1180 error("Can't compile .rc file at the moment: %s\n", file
);
1183 strarray_add(files
, strmake("-r%s", file
));
1186 strarray_add(files
, strmake("-o%s", file
));
1189 if (opts
->use_msvcrt
)
1191 const char *p
= strrchr(file
, '/');
1194 if (!strncmp(p
, "libmsvcr", 8) || !strncmp(p
, "libucrt", 7)) crt_lib
= file
;
1196 strarray_add(files
, strmake("-a%s", file
));
1199 strarray_add(files
, strmake("-s%s", file
));
1202 error("File does not exist: %s\n", file
);
1205 file
= compile_to_object(opts
, file
, lang
);
1206 strarray_add(files
, strmake("-o%s", file
));
1210 else if (file
[1] == 'l')
1211 add_library(opts
, lib_dirs
, files
, file
+ 2 );
1212 else if (file
[1] == 'x')
1214 else if(file
[1] == 'W')
1215 strarray_add(files
, file
);
1218 /* add the default libraries, if needed */
1220 if (!opts
->wine_objdir
&& !opts
->nodefaultlibs
)
1224 add_library(opts
, lib_dirs
, files
, "shell32");
1225 add_library(opts
, lib_dirs
, files
, "comdlg32");
1226 add_library(opts
, lib_dirs
, files
, "gdi32");
1228 add_library(opts
, lib_dirs
, files
, "advapi32");
1229 add_library(opts
, lib_dirs
, files
, "user32");
1232 if (!opts
->nodefaultlibs
&& !opts
->unix_lib
)
1234 add_library(opts
, lib_dirs
, files
, "winecrt0");
1235 if (opts
->use_msvcrt
)
1239 if (strncmp( output_name
, "msvcr", 5 ) &&
1240 strncmp( output_name
, "ucrt", 4 ) &&
1241 strcmp( output_name
, "crtdll.dll" ))
1242 add_library(opts
, lib_dirs
, files
, "ucrtbase");
1244 else strarray_add(files
, strmake("-a%s", crt_lib
));
1246 if (opts
->win16_app
) add_library(opts
, lib_dirs
, files
, "kernel");
1247 add_library(opts
, lib_dirs
, files
, "kernel32");
1248 add_library(opts
, lib_dirs
, files
, "ntdll");
1251 /* set default entry point, if needed */
1252 if (!opts
->entry_point
)
1254 if (opts
->subsystem
&& !strcmp( opts
->subsystem
, "native" ))
1255 entry_point
= (is_pe
&& opts
->target_cpu
== CPU_x86
) ? "DriverEntry@8" : "DriverEntry";
1256 else if (opts
->use_msvcrt
&& !opts
->shared
&& !opts
->win16_app
)
1257 entry_point
= opts
->unicode_app
? "wmainCRTStartup" : "mainCRTStartup";
1259 else entry_point
= opts
->entry_point
;
1261 /* run winebuild to generate the .spec.o file */
1262 spec_args
= get_winebuild_args( opts
);
1263 if ((tool
= build_tool_name( opts
, TOOL_CC
))) strarray_add( spec_args
, strmake( "--cc-cmd=%s", strarray_tostring( tool
, " " )));
1264 if (!is_pe
&& (tool
= build_tool_name( opts
, TOOL_LD
))) strarray_add( spec_args
, strmake( "--ld-cmd=%s", strarray_tostring( tool
, " " )));
1266 spec_o_name
= get_temp_file(output_name
, ".spec.o");
1267 if (opts
->force_pointer_size
)
1268 strarray_add(spec_args
, strmake("-m%u", 8 * opts
->force_pointer_size
));
1269 strarray_add(spec_args
, "-D_REENTRANT");
1270 if (opts
->pic
&& !is_pe
) strarray_add(spec_args
, "-fPIC");
1271 strarray_add(spec_args
, opts
->shared
? "--dll" : "--exe");
1274 strarray_add(spec_args
, "--fake-module");
1275 strarray_add(spec_args
, "-o");
1276 strarray_add(spec_args
, output_file
);
1280 strarray_add(spec_args
, "-o");
1281 strarray_add(spec_args
, spec_o_name
);
1285 strarray_add(spec_args
, "-E");
1286 strarray_add(spec_args
, spec_file
);
1291 strarray_add(spec_args
, "-F");
1292 strarray_add(spec_args
, output_name
);
1293 strarray_add(spec_args
, "--subsystem");
1294 strarray_add(spec_args
, opts
->gui_app
? "windows" : "console");
1295 if (opts
->large_address_aware
) strarray_add( spec_args
, "--large-address-aware" );
1298 if (opts
->target_platform
== PLATFORM_WINDOWS
) strarray_add(spec_args
, "--safeseh");
1302 strarray_add(spec_args
, "--entry");
1303 strarray_add(spec_args
, entry_point
);
1306 if (opts
->subsystem
)
1308 strarray_add(spec_args
, "--subsystem");
1309 strarray_add(spec_args
, opts
->subsystem
);
1312 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
1313 strarray_add(spec_args
, strmake("-L%s", lib_dirs
->base
[j
]));
1317 for (j
= 0; j
< opts
->delayimports
->size
; j
++)
1318 strarray_add(spec_args
, strmake("-d%s", opts
->delayimports
->base
[j
]));
1321 /* add resource files */
1322 for ( j
= 0; j
< files
->size
; j
++ )
1323 if (files
->base
[j
][1] == 'r') strarray_add(spec_args
, files
->base
[j
]);
1325 /* add other files */
1326 strarray_add(spec_args
, "--");
1327 for ( j
= 0; j
< files
->size
; j
++ )
1329 switch(files
->base
[j
][1])
1334 strarray_add(spec_args
, files
->base
[j
] + 2);
1339 spawn(opts
->prefix
, spec_args
, 0);
1340 strarray_free (spec_args
);
1341 if (fake_module
) return; /* nothing else to do */
1343 /* link everything together now */
1344 link_args
= get_link_args( opts
, output_name
);
1346 if ((opts
->nodefaultlibs
|| opts
->use_msvcrt
) && opts
->target_platform
== PLATFORM_MINGW
)
1348 libgcc
= find_libgcc(opts
->prefix
, link_args
);
1349 if (!libgcc
) libgcc
= "-lgcc";
1352 strarray_add(link_args
, "-o");
1353 strarray_add(link_args
, output_path
);
1355 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
1356 strarray_add(link_args
, strmake("-L%s", lib_dirs
->base
[j
]));
1358 if (is_pe
&& opts
->use_msvcrt
&& !entry_point
&& (opts
->shared
|| opts
->win16_app
))
1359 entry_point
= opts
->target_cpu
== CPU_x86
? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1361 if (is_pe
&& entry_point
)
1363 if (opts
->target_platform
== PLATFORM_WINDOWS
)
1364 strarray_add(link_args
, strmake("-Wl,-entry:%s", entry_point
));
1366 strarray_add(link_args
, strmake("-Wl,--entry,%s%s",
1367 is_pe
&& opts
->target_cpu
== CPU_x86
? "_" : "",
1371 strarray_add(link_args
, spec_o_name
);
1375 for (j
= 0; j
< opts
->delayimports
->size
; j
++)
1377 if (opts
->target_platform
== PLATFORM_WINDOWS
)
1378 strarray_add(link_args
, strmake("-Wl,-delayload:%s", opts
->delayimports
->base
[j
]));
1380 strarray_add(link_args
, strmake("-Wl,-delayload,%s",opts
->delayimports
->base
[j
]));
1384 for ( j
= 0; j
< files
->size
; j
++ )
1386 const char* name
= files
->base
[j
] + 2;
1387 switch(files
->base
[j
][1])
1390 strarray_add(link_args
, strmake("-l%s", name
));
1394 strarray_add(link_args
, name
);
1397 if (is_pe
&& !opts
->use_msvcrt
&& !opts
->lib_suffix
&& strchr(name
, '/'))
1399 /* turn the path back into -Ldir -lfoo options
1400 * this makes sure that we use the specified libs even
1401 * when mingw adds its own import libs to the link */
1402 char *lib
= xstrdup( name
);
1403 char *p
= strrchr( lib
, '/' );
1406 if (!strncmp( p
, "lib", 3 ) && strcmp( p
, "libmsvcrt.a" ))
1408 char *ext
= strrchr( p
, '.' );
1412 strarray_add(link_args
, strmake("-L%s", lib
));
1413 strarray_add(link_args
, strmake("-l%s", p
));
1419 strarray_add(link_args
, name
);
1422 strarray_add(link_args
, files
->base
[j
]);
1427 if (!opts
->nostdlib
&& !is_pe
)
1429 strarray_add(link_args
, "-lm");
1430 strarray_add(link_args
, "-lc");
1433 if (libgcc
) strarray_add(link_args
, libgcc
);
1435 output_file_name
= output_path
;
1436 output_debug_file
= opts
->debug_file
;
1437 output_implib
= opts
->out_implib
;
1438 atexit( cleanup_output_files
);
1440 spawn(opts
->prefix
, link_args
, 0);
1441 strarray_free (link_args
);
1443 if (opts
->debug_file
&& !strendswith(opts
->debug_file
, ".pdb"))
1445 strarray
*tool
, *objcopy
= build_tool_name(opts
, TOOL_OBJCOPY
);
1447 tool
= strarray_dup(objcopy
);
1448 strarray_add(tool
, "--only-keep-debug");
1449 strarray_add(tool
, output_path
);
1450 strarray_add(tool
, opts
->debug_file
);
1451 spawn(opts
->prefix
, tool
, 1);
1452 strarray_free(tool
);
1454 tool
= strarray_dup(objcopy
);
1455 strarray_add(tool
, "--strip-debug");
1456 strarray_add(tool
, output_path
);
1457 spawn(opts
->prefix
, tool
, 1);
1458 strarray_free(tool
);
1461 strarray_add(tool
, "--add-gnu-debuglink");
1462 strarray_add(tool
, opts
->debug_file
);
1463 strarray_add(tool
, output_path
);
1464 spawn(opts
->prefix
, tool
, 0);
1465 strarray_free(tool
);
1468 if (opts
->out_implib
&& !is_pe
)
1471 error("--out-implib requires a .spec or .def file\n");
1473 implib_args
= get_winebuild_args( opts
);
1474 if ((tool
= build_tool_name( opts
, TOOL_CC
))) strarray_add( implib_args
, strmake( "--cc-cmd=%s", strarray_tostring( tool
, " " )));
1475 if ((tool
= build_tool_name( opts
, TOOL_LD
))) strarray_add( implib_args
, strmake( "--ld-cmd=%s", strarray_tostring( tool
, " " )));
1477 strarray_add(implib_args
, "--implib");
1478 strarray_add(implib_args
, "-o");
1479 strarray_add(implib_args
, opts
->out_implib
);
1480 strarray_add(implib_args
, "--export");
1481 strarray_add(implib_args
, spec_file
);
1483 spawn(opts
->prefix
, implib_args
, 0);
1484 strarray_free (implib_args
);
1487 /* set the base address with prelink if linker support is not present */
1488 if (opts
->prelink
&& !opts
->target
)
1490 if (opts
->prelink
[0] && strcmp(opts
->prelink
,"false"))
1492 strarray
*prelink_args
= strarray_alloc();
1493 strarray_add(prelink_args
, opts
->prelink
);
1494 strarray_add(prelink_args
, "--reloc-only");
1495 strarray_add(prelink_args
, opts
->image_base
);
1496 strarray_add(prelink_args
, output_path
);
1497 spawn(opts
->prefix
, prelink_args
, 1);
1498 strarray_free(prelink_args
);
1502 if (!is_pe
) fixup_constructors( opts
, output_path
);
1503 else if (opts
->wine_builtin
) make_wine_builtin( opts
, output_path
);
1505 /* create the loader script */
1506 if (generate_app_loader
)
1507 create_file(output_file
, 0755, app_loader_template
, strmake("%s.so", output_name
));
1511 static void forward( struct options
*opts
)
1513 strarray
* args
= strarray_alloc();
1515 strarray_addall(args
, get_translator(opts
));
1516 strarray_addall(args
, opts
->compiler_args
);
1517 strarray_addall(args
, opts
->linker_args
);
1518 spawn(opts
->prefix
, args
, 0);
1519 strarray_free (args
);
1522 static int is_linker_arg(const char* arg
)
1524 static const char* link_switches
[] =
1526 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1527 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1528 "-fprofile-generate", "-fprofile-use"
1539 if (strncmp("-Wl,", arg
, 4) == 0) return 1;
1542 if (strcmp("-Xlinker", arg
) == 0) return 1;
1545 if (strcmp("-arch", arg
) == 0) return 1;
1548 if (strncmp("-fuse-ld=", arg
, 9) == 0) return 1;
1551 if (strncmp("-rtlib=", arg
, 7) == 0) return 1;
1555 for (j
= 0; j
< ARRAY_SIZE(link_switches
); j
++)
1556 if (strcmp(link_switches
[j
], arg
) == 0) return 1;
1561 static void parse_target_option( struct options
*opts
, const char *target
)
1563 char *p
, *spec
= xstrdup( target
);
1566 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
1568 /* get the CPU part */
1570 if ((p
= strchr( spec
, '-' )))
1573 for (i
= 0; i
< ARRAY_SIZE(cpu_names
); i
++)
1575 if (!strcmp( cpu_names
[i
].name
, spec
))
1577 opts
->target_cpu
= cpu_names
[i
].cpu
;
1581 if (i
== ARRAY_SIZE(cpu_names
))
1582 error( "Unrecognized CPU '%s'\n", spec
);
1584 else if (!strcmp( spec
, "mingw32" ))
1586 opts
->target_cpu
= CPU_x86
;
1590 error( "Invalid target specification '%s'\n", target
);
1592 /* get the OS part */
1594 opts
->target_platform
= PLATFORM_UNSPECIFIED
; /* default value */
1597 for (i
= 0; i
< ARRAY_SIZE(platform_names
); i
++)
1599 if (!strncmp( platform_names
[i
].name
, p
, strlen(platform_names
[i
].name
) ))
1601 opts
->target_platform
= platform_names
[i
].platform
;
1605 if (opts
->target_platform
!= PLATFORM_UNSPECIFIED
|| !(p
= strchr( p
, '-' ))) break;
1610 opts
->target
= xstrdup( target
);
1613 static int is_option( struct options
*opts
, int i
, const char *option
, const char **option_arg
)
1615 if (!strcmp( opts
->args
->base
[i
], option
))
1617 if (opts
->args
->size
== i
) error( "option %s requires an argument\n", opts
->args
->base
[i
] );
1618 *option_arg
= opts
->args
->base
[i
+ 1];
1621 if (!strncmp( opts
->args
->base
[i
], option
, strlen(option
) ) && opts
->args
->base
[i
][strlen(option
)] == '=')
1623 *option_arg
= opts
->args
->base
[i
] + strlen(option
) + 1;
1629 int main(int argc
, char **argv
)
1631 int i
, c
, next_is_arg
= 0, linking
= 1;
1632 int raw_compiler_arg
, raw_linker_arg
, raw_winebuild_arg
;
1633 const char* option_arg
;
1634 struct options opts
;
1639 signal( SIGHUP
, exit_on_signal
);
1641 signal( SIGTERM
, exit_on_signal
);
1642 signal( SIGINT
, exit_on_signal
);
1643 #ifdef HAVE_SIGADDSET
1644 sigemptyset( &signal_mask
);
1645 sigaddset( &signal_mask
, SIGHUP
);
1646 sigaddset( &signal_mask
, SIGTERM
);
1647 sigaddset( &signal_mask
, SIGINT
);
1649 init_argv0_dir( argv
[0] );
1651 /* setup tmp file removal at exit */
1652 tmp_files
= strarray_alloc();
1653 atexit(clean_temp_files
);
1655 /* initialize options */
1656 memset(&opts
, 0, sizeof(opts
));
1657 opts
.target_cpu
= build_cpu
;
1658 opts
.target_platform
= build_platform
;
1659 opts
.lib_dirs
= strarray_alloc();
1660 opts
.files
= strarray_alloc();
1661 opts
.linker_args
= strarray_alloc();
1662 opts
.compiler_args
= strarray_alloc();
1663 opts
.winebuild_args
= strarray_alloc();
1664 opts
.delayimports
= strarray_alloc();
1665 opts
.args
= strarray_alloc();
1668 /* determine the processor type */
1669 if (strendswith(argv
[0], "winecpp")) opts
.processor
= proc_cpp
;
1670 else if (strendswith(argv
[0], "++")) opts
.processor
= proc_cxx
;
1672 for (i
= 1; i
< argc
; i
++)
1674 char *input_buffer
= NULL
, *iter
, *opt
, *out
;
1678 if (argv
[i
][0] != '@' || (fd
= open( argv
[i
] + 1, O_RDONLY
| O_BINARY
)) == -1)
1680 strarray_add( opts
.args
, argv
[i
] );
1683 if ((fstat( fd
, &st
) == -1)) error( "Cannot stat %s\n", argv
[i
] + 1 );
1686 input_buffer
= xmalloc( st
.st_size
+ 1 );
1687 if (read( fd
, input_buffer
, st
.st_size
) != st
.st_size
) error( "Cannot read %s\n", argv
[i
] + 1 );
1690 for (iter
= input_buffer
; iter
< input_buffer
+ st
.st_size
; iter
++)
1693 while (iter
< input_buffer
+ st
.st_size
&& isspace(*iter
)) iter
++;
1694 if (iter
== input_buffer
+ st
.st_size
) break;
1696 while (iter
< input_buffer
+ st
.st_size
&& (quote
|| !isspace(*iter
)))
1703 else if (*iter
== '\'' || *iter
== '"') quote
= *iter
++;
1706 if (*iter
== '\\' && iter
+ 1 < input_buffer
+ st
.st_size
) iter
++;
1711 strarray_add( opts
.args
, opt
);
1716 for (i
= 0; i
< opts
.args
->size
; i
++)
1718 if (opts
.args
->base
[i
][0] == '-' && opts
.args
->base
[i
][1]) /* option, except '-' alone is stdin, which is a file */
1720 /* determine if this switch is followed by a separate argument */
1723 switch(opts
.args
->base
[i
][1])
1725 case 'x': case 'o': case 'D': case 'U':
1726 case 'I': case 'A': case 'l': case 'u':
1727 case 'b': case 'V': case 'G': case 'L':
1728 case 'B': case 'R': case 'z':
1729 if (opts
.args
->base
[i
][2]) option_arg
= &opts
.args
->base
[i
][2];
1730 else next_is_arg
= 1;
1736 if (strcmp("-aux-info", opts
.args
->base
[i
]) == 0)
1738 if (strcmp("-arch", opts
.args
->base
[i
]) == 0)
1742 if (strcmp("-Xlinker", opts
.args
->base
[i
]) == 0)
1746 c
= opts
.args
->base
[i
][2];
1747 if (c
== 'F' || c
== 'T' || c
== 'Q')
1749 if (opts
.args
->base
[i
][3]) option_arg
= &opts
.args
->base
[i
][3];
1750 else next_is_arg
= 1;
1754 if (strcmp("-framework", opts
.args
->base
[i
]) == 0)
1758 next_is_arg
= strcmp("-target", opts
.args
->base
[i
]) == 0;
1761 next_is_arg
= (strcmp("--param", opts
.args
->base
[i
]) == 0 ||
1762 strcmp("--sysroot", opts
.args
->base
[i
]) == 0 ||
1763 strcmp("--target", opts
.args
->base
[i
]) == 0 ||
1764 strcmp("--wine-objdir", opts
.args
->base
[i
]) == 0 ||
1765 strcmp("--winebuild", opts
.args
->base
[i
]) == 0 ||
1766 strcmp("--lib-suffix", opts
.args
->base
[i
]) == 0);
1771 if (i
+ 1 >= opts
.args
->size
) error("option -%c requires an argument\n", opts
.args
->base
[i
][1]);
1772 option_arg
= opts
.args
->base
[i
+1];
1775 /* determine what options go 'as is' to the linker & the compiler */
1776 raw_linker_arg
= is_linker_arg(opts
.args
->base
[i
]);
1777 raw_compiler_arg
= !raw_linker_arg
;
1778 raw_winebuild_arg
= 0;
1780 /* do a bit of semantic analysis */
1781 switch (opts
.args
->base
[i
][1])
1784 str
= strdup(option_arg
);
1785 if (strendswith(str
, "/")) str
[strlen(str
) - 1] = 0;
1786 if (!opts
.prefix
) opts
.prefix
= strarray_alloc();
1787 strarray_add(opts
.prefix
, str
);
1791 parse_target_option( &opts
, option_arg
);
1792 raw_compiler_arg
= 0;
1795 opts
.version
= xstrdup( option_arg
);
1796 raw_compiler_arg
= 0;
1798 case 'c': /* compile or assemble */
1799 raw_compiler_arg
= 0;
1800 if (opts
.args
->base
[i
][2] == 0) opts
.compile_only
= 1;
1802 case 'S': /* generate assembler code */
1803 case 'E': /* preprocess only */
1804 if (opts
.args
->base
[i
][2] == 0) linking
= 0;
1807 if (strcmp("-fno-short-wchar", opts
.args
->base
[i
]) == 0)
1808 opts
.noshortwchar
= 1;
1809 else if (!strcmp("-fasynchronous-unwind-tables", opts
.args
->base
[i
]))
1810 opts
.unwind_tables
= 1;
1811 else if (!strcmp("-fno-asynchronous-unwind-tables", opts
.args
->base
[i
]))
1812 opts
.unwind_tables
= 0;
1813 else if (!strcmp("-fPIC", opts
.args
->base
[i
]) || !strcmp("-fpic", opts
.args
->base
[i
]))
1815 else if (!strcmp("-fno-PIC", opts
.args
->base
[i
]) || !strcmp("-fno-pic", opts
.args
->base
[i
]))
1819 if (!strcmp( "-isysroot", opts
.args
->base
[i
] )) opts
.isysroot
= opts
.args
->base
[i
+ 1];
1822 strarray_add(opts
.files
, strmake("-l%s", option_arg
));
1823 raw_compiler_arg
= 0;
1826 strarray_add(opts
.lib_dirs
, option_arg
);
1827 raw_compiler_arg
= 0;
1829 case 'M': /* map file generation */
1833 if (strcmp("-mno-cygwin", opts
.args
->base
[i
]) == 0)
1835 opts
.use_msvcrt
= 1;
1836 raw_compiler_arg
= 0;
1837 raw_winebuild_arg
= 1;
1839 else if (strcmp("-mwindows", opts
.args
->base
[i
]) == 0)
1842 raw_compiler_arg
= 0;
1844 else if (strcmp("-mconsole", opts
.args
->base
[i
]) == 0)
1847 raw_compiler_arg
= 0;
1849 else if (strcmp("-municode", opts
.args
->base
[i
]) == 0)
1851 opts
.unicode_app
= 1;
1852 raw_compiler_arg
= 0;
1853 raw_winebuild_arg
= 1;
1855 else if (strcmp("-mthreads", opts
.args
->base
[i
]) == 0)
1857 raw_compiler_arg
= 0;
1859 else if (strcmp("-munix", opts
.args
->base
[i
]) == 0)
1862 raw_compiler_arg
= 0;
1863 raw_winebuild_arg
= 1;
1865 else if (strcmp("-m16", opts
.args
->base
[i
]) == 0)
1868 raw_compiler_arg
= 0;
1869 raw_winebuild_arg
= 1;
1871 else if (strcmp("-m32", opts
.args
->base
[i
]) == 0)
1873 if (opts
.target_cpu
== CPU_x86_64
)
1874 opts
.target_cpu
= CPU_x86
;
1875 else if (opts
.target_cpu
== CPU_ARM64
)
1876 opts
.target_cpu
= CPU_ARM
;
1877 opts
.force_pointer_size
= 4;
1880 else if (strcmp("-m64", opts
.args
->base
[i
]) == 0)
1882 if (opts
.target_cpu
== CPU_x86
)
1883 opts
.target_cpu
= CPU_x86_64
;
1884 else if (opts
.target_cpu
== CPU_ARM
)
1885 opts
.target_cpu
= CPU_ARM64
;
1886 opts
.force_pointer_size
= 8;
1889 else if (!strcmp("-marm", opts
.args
->base
[i
] ) || !strcmp("-mthumb", opts
.args
->base
[i
] ))
1892 raw_winebuild_arg
= 1;
1894 else if (!strncmp("-mcpu=", opts
.args
->base
[i
], 6) ||
1895 !strncmp("-mfpu=", opts
.args
->base
[i
], 6) ||
1896 !strncmp("-march=", opts
.args
->base
[i
], 7) ||
1897 !strncmp("-mfloat-abi=", opts
.args
->base
[i
], 12))
1898 raw_winebuild_arg
= 1;
1901 if (strcmp("-nostdinc", opts
.args
->base
[i
]) == 0)
1903 else if (strcmp("-nodefaultlibs", opts
.args
->base
[i
]) == 0)
1904 opts
.nodefaultlibs
= 1;
1905 else if (strcmp("-nostdlib", opts
.args
->base
[i
]) == 0)
1907 else if (strcmp("-nostartfiles", opts
.args
->base
[i
]) == 0)
1908 opts
.nostartfiles
= 1;
1911 opts
.output_name
= option_arg
;
1912 raw_compiler_arg
= 0;
1915 if (strcmp("-static", opts
.args
->base
[i
]) == 0)
1917 else if(strcmp("-save-temps", opts
.args
->base
[i
]) == 0)
1919 else if (strncmp("-specs=", opts
.args
->base
[i
], 7) == 0)
1921 else if(strcmp("-shared", opts
.args
->base
[i
]) == 0)
1924 raw_compiler_arg
= raw_linker_arg
= 0;
1926 else if (strcmp("-s", opts
.args
->base
[i
]) == 0 && opts
.target_platform
== PLATFORM_APPLE
)
1928 /* On Mac, change -s into -Wl,-x. ld's -s switch
1929 * is deprecated, and it doesn't work on Tiger with
1937 if (is_option( &opts
, i
, "-target", &option_arg
))
1939 parse_target_option( &opts
, option_arg
);
1940 raw_compiler_arg
= raw_linker_arg
= 0;
1944 if (opts
.args
->base
[i
][2] == 0) verbose
++;
1947 if (strncmp("-Wl,", opts
.args
->base
[i
], 4) == 0)
1950 strarray
* Wl
= strarray_fromstring(opts
.args
->base
[i
] + 4, ",");
1951 for (j
= 0; j
< Wl
->size
; j
++)
1953 if (!strcmp(Wl
->base
[j
], "--image-base") && j
< Wl
->size
- 1)
1955 opts
.image_base
= strdup( Wl
->base
[++j
] );
1958 if (!strcmp(Wl
->base
[j
], "--section-alignment") && j
< Wl
->size
- 1)
1960 opts
.section_align
= strdup( Wl
->base
[++j
] );
1963 if (!strcmp(Wl
->base
[j
], "--file-alignment") && j
< Wl
->size
- 1)
1965 opts
.file_align
= strdup( Wl
->base
[++j
] );
1968 if (!strcmp(Wl
->base
[j
], "--large-address-aware"))
1970 opts
.large_address_aware
= 1;
1973 if (!strcmp(Wl
->base
[j
], "--wine-builtin"))
1975 opts
.wine_builtin
= 1;
1978 if (!strcmp(Wl
->base
[j
], "--subsystem") && j
< Wl
->size
- 1)
1980 opts
.subsystem
= strdup( Wl
->base
[++j
] );
1983 if (!strcmp(Wl
->base
[j
], "--entry") && j
< Wl
->size
- 1)
1985 opts
.entry_point
= strdup( Wl
->base
[++j
] );
1988 if (!strcmp(Wl
->base
[j
], "-delayload") && j
< Wl
->size
- 1)
1990 strarray_add( opts
.delayimports
, Wl
->base
[++j
] );
1993 if (!strcmp(Wl
->base
[j
], "--debug-file") && j
< Wl
->size
- 1)
1995 opts
.debug_file
= strdup( Wl
->base
[++j
] );
1998 if (!strcmp(Wl
->base
[j
], "--whole-archive") || !strcmp(Wl
->base
[j
], "--no-whole-archive"))
2000 strarray_add( opts
.files
, strmake( "-Wl,%s", Wl
->base
[j
] ));
2003 if (!strcmp(Wl
->base
[j
], "--out-implib"))
2005 opts
.out_implib
= strdup( Wl
->base
[++j
] );
2008 if (!strcmp(Wl
->base
[j
], "-static")) linking
= -1;
2009 strarray_add(opts
.linker_args
, strmake("-Wl,%s",Wl
->base
[j
]));
2012 raw_compiler_arg
= raw_linker_arg
= 0;
2014 else if (strncmp("-Wb,", opts
.args
->base
[i
], 4) == 0)
2016 strarray
* Wb
= strarray_fromstring(opts
.args
->base
[i
] + 4, ",");
2017 strarray_addall(opts
.winebuild_args
, Wb
);
2019 /* don't pass it to the compiler, it generates errors */
2020 raw_compiler_arg
= raw_linker_arg
= 0;
2024 lang
= strmake("-x%s", option_arg
);
2025 strarray_add(opts
.files
, lang
);
2026 /* we'll pass these flags ourselves, explicitly */
2027 raw_compiler_arg
= raw_linker_arg
= 0;
2030 if (strcmp("-static", opts
.args
->base
[i
]+1) == 0)
2032 else if (is_option( &opts
, i
, "--sysroot", &option_arg
))
2034 opts
.sysroot
= option_arg
;
2037 else if (is_option( &opts
, i
, "--target", &option_arg
))
2039 parse_target_option( &opts
, option_arg
);
2040 raw_compiler_arg
= raw_linker_arg
= 0;
2042 else if (is_option( &opts
, i
, "--wine-objdir", &option_arg
))
2044 opts
.wine_objdir
= option_arg
;
2045 raw_compiler_arg
= raw_linker_arg
= 0;
2047 else if (is_option( &opts
, i
, "--winebuild", &option_arg
))
2049 opts
.winebuild
= option_arg
;
2050 raw_compiler_arg
= raw_linker_arg
= 0;
2052 else if (is_option( &opts
, i
, "--lib-suffix", &option_arg
))
2054 opts
.lib_suffix
= option_arg
;
2055 raw_compiler_arg
= raw_linker_arg
= 0;
2060 /* put the arg into the appropriate bucket */
2063 strarray_add( opts
.linker_args
, opts
.args
->base
[i
] );
2064 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2065 strarray_add( opts
.linker_args
, opts
.args
->base
[i
+ 1] );
2067 if (raw_compiler_arg
)
2069 strarray_add( opts
.compiler_args
, opts
.args
->base
[i
] );
2070 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2071 strarray_add( opts
.compiler_args
, opts
.args
->base
[i
+ 1] );
2073 if (raw_winebuild_arg
)
2075 strarray_add( opts
.winebuild_args
, opts
.args
->base
[i
] );
2076 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2077 strarray_add( opts
.winebuild_args
, opts
.args
->base
[i
+ 1] );
2080 /* skip the next token if it's an argument */
2081 if (next_is_arg
) i
++;
2085 strarray_add( opts
.files
, opts
.args
->base
[i
] );
2089 if (opts
.processor
== proc_cpp
) linking
= 0;
2090 if (linking
== -1) error("Static linking is not supported\n");
2092 if (opts
.files
->size
== 0) forward(&opts
);
2093 else if (linking
) build(&opts
);
2094 else compile(&opts
, lang
);
2096 output_file_name
= NULL
;
2097 output_debug_file
= NULL
;
2098 output_implib
= NULL
;