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__");
920 strarray_add( comp_args
, strmake( "-I%s", includedir
));
921 strarray_add( comp_args
, strmake( "%s%s/wine/windows", isystem
, includedir
));
923 for (j
= 0; j
< ARRAY_SIZE(incl_dirs
); j
++)
925 if (j
&& !strcmp( incl_dirs
[0], incl_dirs
[j
] )) continue;
926 strarray_add(comp_args
, strmake( "-I%s%s", root
, incl_dirs
[j
] ));
927 strarray_add(comp_args
, strmake( "%s%s%s/wine/windows", isystem
, root
, incl_dirs
[j
] ));
930 else if (opts
->wine_objdir
)
931 strarray_add(comp_args
, strmake("-I%s/include", opts
->wine_objdir
) );
933 spawn(opts
->prefix
, comp_args
, 0);
934 strarray_free(comp_args
);
937 static const char* compile_to_object(struct options
* opts
, const char* file
, const char* lang
)
939 struct options copts
;
942 /* make a copy so we don't change any of the initial stuff */
943 /* a shallow copy is exactly what we want in this case */
944 base_name
= get_basename(file
);
946 copts
.output_name
= get_temp_file(base_name
, ".o");
947 copts
.compile_only
= 1;
948 copts
.files
= strarray_alloc();
949 strarray_add(copts
.files
, file
);
950 compile(&copts
, lang
);
951 strarray_free(copts
.files
);
954 return copts
.output_name
;
957 /* return the initial set of options needed to run winebuild */
958 static strarray
*get_winebuild_args(struct options
*opts
)
960 const char* winebuild
= getenv("WINEBUILD");
961 const char *binary
= NULL
;
962 strarray
*spec_args
= strarray_alloc();
966 binary
= opts
->winebuild
;
967 else if (opts
->wine_objdir
)
968 binary
= strmake( "%s/tools/winebuild/winebuild%s", opts
->wine_objdir
, EXEEXT
);
970 binary
= find_binary( opts
->prefix
, winebuild
);
972 binary
= strmake( "%s/winebuild%s", bindir
, EXEEXT
);
974 binary
= find_binary( opts
->prefix
, "winebuild" );
975 if (!binary
) error( "Could not find winebuild\n" );
976 strarray_add( spec_args
, binary
);
977 if (verbose
) strarray_add( spec_args
, "-v" );
978 if (keep_generated
) strarray_add( spec_args
, "--save-temps" );
981 strarray_add( spec_args
, "--target" );
982 strarray_add( spec_args
, opts
->target
);
986 for (i
= 0; i
< opts
->prefix
->size
; i
++)
987 strarray_add( spec_args
, strmake( "-B%s", opts
->prefix
->base
[i
] ));
989 strarray_addall( spec_args
, opts
->winebuild_args
);
990 if (opts
->unwind_tables
) strarray_add( spec_args
, "-fasynchronous-unwind-tables" );
991 else strarray_add( spec_args
, "-fno-asynchronous-unwind-tables" );
995 static void fixup_constructors( struct options
*opts
, const char *file
)
997 strarray
*args
= get_winebuild_args( opts
);
999 strarray_add( args
, "--fixup-ctors" );
1000 strarray_add( args
, file
);
1001 spawn( opts
->prefix
, args
, 0 );
1002 strarray_free( args
);
1005 static void make_wine_builtin( struct options
*opts
, const char *file
)
1007 strarray
*args
= get_winebuild_args( opts
);
1009 strarray_add( args
, "--builtin" );
1010 strarray_add( args
, file
);
1011 spawn( opts
->prefix
, args
, 0 );
1012 strarray_free( args
);
1015 /* check if there is a static lib associated to a given dll */
1016 static char *find_static_lib( const char *dll
)
1018 char *lib
= strmake("%s.a", dll
);
1019 if (get_file_type(lib
) == file_arh
) return lib
;
1024 static const char *find_libgcc(const strarray
*prefix
, const strarray
*link_tool
)
1026 const char *out
= get_temp_file( "find_libgcc", ".out" );
1027 const char *err
= get_temp_file( "find_libgcc", ".err" );
1028 strarray
*link
= strarray_dup( link_tool
);
1029 int sout
= -1, serr
= -1;
1035 strarray_add( link
, "-print-libgcc-file-name" );
1037 sout
= dup( fileno(stdout
) );
1038 freopen( out
, "w", stdout
);
1039 serr
= dup( fileno(stderr
) );
1040 freopen( err
, "w", stderr
);
1041 ret
= spawn( prefix
, link
, 1 );
1044 dup2( sout
, fileno(stdout
) );
1049 dup2( serr
, fileno(stderr
) );
1052 strarray_free( link
);
1054 if (ret
|| stat(out
, &st
) || !st
.st_size
) return NULL
;
1056 libgcc
= xmalloc(st
.st_size
+ 1);
1057 sout
= open(out
, O_RDONLY
);
1058 if (sout
== -1) return NULL
;
1059 cnt
= read(sout
, libgcc
, st
.st_size
);
1062 if ((p
= strchr(libgcc
, '\n'))) *p
= 0;
1067 /* add specified library to the list of files */
1068 static void add_library( struct options
*opts
, strarray
*lib_dirs
, strarray
*files
, const char *library
)
1070 char *static_lib
, *fullname
= 0;
1072 switch(get_lib_type(opts
->target_platform
, lib_dirs
, library
, opts
->lib_suffix
, &fullname
))
1075 strarray_add(files
, strmake("-a%s", fullname
));
1078 strarray_add(files
, strmake("-d%s", fullname
));
1079 if ((static_lib
= find_static_lib(fullname
)))
1081 strarray_add(files
, strmake("-a%s",static_lib
));
1087 /* keep it anyway, the linker may know what to do with it */
1088 strarray_add(files
, strmake("-l%s", library
));
1094 static void build(struct options
* opts
)
1096 strarray
*lib_dirs
, *files
;
1097 strarray
*spec_args
, *link_args
, *implib_args
, *tool
;
1098 char *output_file
, *output_path
;
1099 const char *spec_o_name
, *libgcc
= NULL
;
1100 const char *output_name
, *spec_file
, *lang
;
1101 int generate_app_loader
= 1;
1102 const char *crt_lib
= NULL
, *entry_point
= NULL
;
1103 int fake_module
= 0;
1104 int is_pe
= (opts
->target_platform
== PLATFORM_WINDOWS
|| opts
->target_platform
== PLATFORM_CYGWIN
|| opts
->target_platform
== PLATFORM_MINGW
);
1107 /* NOTE: for the files array we'll use the following convention:
1108 * -axxx: xxx is an archive (.a)
1109 * -dxxx: xxx is a DLL (.def)
1110 * -lxxx: xxx is an unsorted library
1111 * -oxxx: xxx is an object (.o)
1112 * -rxxx: xxx is a resource (.res)
1113 * -sxxx: xxx is a shared lib (.so)
1114 * -xlll: lll is the language (c, c++, etc.)
1117 output_file
= strdup( opts
->output_name
? opts
->output_name
: "a.out" );
1119 /* 'winegcc -o app xxx.exe.so' only creates the load script */
1120 if (opts
->files
->size
== 1 && strendswith(opts
->files
->base
[0], ".exe.so"))
1122 create_file(output_file
, 0755, app_loader_template
, opts
->files
->base
[0]);
1126 /* generate app loader only for .exe */
1127 if (opts
->shared
|| is_pe
|| strendswith(output_file
, ".so"))
1128 generate_app_loader
= 0;
1130 if (strendswith(output_file
, ".fake")) fake_module
= 1;
1132 /* normalize the filename a bit: strip .so, ensure it has proper ext */
1133 if ((output_name
= strrchr(output_file
, '/'))) output_name
++;
1134 else output_name
= output_file
;
1135 if (!strchr(output_name
, '.'))
1136 output_file
= strmake("%s.%s", output_file
, opts
->shared
? "dll" : "exe");
1137 else if (strendswith(output_file
, ".so"))
1138 output_file
[strlen(output_file
) - 3] = 0;
1139 output_path
= is_pe
? output_file
: strmake( "%s.so", output_file
);
1141 /* get the filename from the path */
1142 if ((output_name
= strrchr(output_file
, '/'))) output_name
++;
1143 else output_name
= output_file
;
1145 /* prepare the linking path */
1146 if (!opts
->wine_objdir
)
1148 char *lib_dir
= get_lib_dir( opts
);
1149 lib_dirs
= strarray_dup(opts
->lib_dirs
);
1150 strarray_add( lib_dirs
, strmake( "%s/wine", lib_dir
));
1151 strarray_add( lib_dirs
, lib_dir
);
1155 lib_dirs
= strarray_alloc();
1156 strarray_add(lib_dirs
, strmake("%s/dlls", opts
->wine_objdir
));
1157 strarray_add(lib_dirs
, strmake("%s/libs/wine", opts
->wine_objdir
));
1158 strarray_addall(lib_dirs
, opts
->lib_dirs
);
1161 /* mark the files with their appropriate type */
1162 spec_file
= lang
= 0;
1163 files
= strarray_alloc();
1164 for ( j
= 0; j
< opts
->files
->size
; j
++ )
1166 const char* file
= opts
->files
->base
[j
];
1169 switch(get_file_type(file
))
1174 error("Only one spec file can be specified\n");
1178 /* FIXME: invoke wrc to build it */
1179 error("Can't compile .rc file at the moment: %s\n", file
);
1182 strarray_add(files
, strmake("-r%s", file
));
1185 strarray_add(files
, strmake("-o%s", file
));
1188 if (opts
->use_msvcrt
)
1190 const char *p
= strrchr(file
, '/');
1193 if (!strncmp(p
, "libmsvcr", 8) || !strncmp(p
, "libucrt", 7)) crt_lib
= file
;
1195 strarray_add(files
, strmake("-a%s", file
));
1198 strarray_add(files
, strmake("-s%s", file
));
1201 error("File does not exist: %s\n", file
);
1204 file
= compile_to_object(opts
, file
, lang
);
1205 strarray_add(files
, strmake("-o%s", file
));
1209 else if (file
[1] == 'l')
1210 add_library(opts
, lib_dirs
, files
, file
+ 2 );
1211 else if (file
[1] == 'x')
1213 else if(file
[1] == 'W')
1214 strarray_add(files
, file
);
1217 /* add the default libraries, if needed */
1219 if (!opts
->wine_objdir
&& !opts
->nodefaultlibs
)
1223 add_library(opts
, lib_dirs
, files
, "shell32");
1224 add_library(opts
, lib_dirs
, files
, "comdlg32");
1225 add_library(opts
, lib_dirs
, files
, "gdi32");
1227 add_library(opts
, lib_dirs
, files
, "advapi32");
1228 add_library(opts
, lib_dirs
, files
, "user32");
1231 if (!opts
->nodefaultlibs
&& !opts
->unix_lib
)
1233 add_library(opts
, lib_dirs
, files
, "winecrt0");
1234 if (opts
->use_msvcrt
)
1238 if (strncmp( output_name
, "msvcr", 5 ) &&
1239 strncmp( output_name
, "ucrt", 4 ) &&
1240 strcmp( output_name
, "crtdll.dll" ))
1241 add_library(opts
, lib_dirs
, files
, "ucrtbase");
1243 else strarray_add(files
, strmake("-a%s", crt_lib
));
1245 if (opts
->win16_app
) add_library(opts
, lib_dirs
, files
, "kernel");
1246 add_library(opts
, lib_dirs
, files
, "kernel32");
1247 add_library(opts
, lib_dirs
, files
, "ntdll");
1250 /* set default entry point, if needed */
1251 if (!opts
->entry_point
)
1253 if (opts
->subsystem
&& !strcmp( opts
->subsystem
, "native" ))
1254 entry_point
= (is_pe
&& opts
->target_cpu
== CPU_x86
) ? "DriverEntry@8" : "DriverEntry";
1255 else if (opts
->use_msvcrt
&& !opts
->shared
&& !opts
->win16_app
)
1256 entry_point
= opts
->unicode_app
? "wmainCRTStartup" : "mainCRTStartup";
1258 else entry_point
= opts
->entry_point
;
1260 /* run winebuild to generate the .spec.o file */
1261 spec_args
= get_winebuild_args( opts
);
1262 if ((tool
= build_tool_name( opts
, TOOL_CC
))) strarray_add( spec_args
, strmake( "--cc-cmd=%s", strarray_tostring( tool
, " " )));
1263 if (!is_pe
&& (tool
= build_tool_name( opts
, TOOL_LD
))) strarray_add( spec_args
, strmake( "--ld-cmd=%s", strarray_tostring( tool
, " " )));
1265 spec_o_name
= get_temp_file(output_name
, ".spec.o");
1266 if (opts
->force_pointer_size
)
1267 strarray_add(spec_args
, strmake("-m%u", 8 * opts
->force_pointer_size
));
1268 strarray_add(spec_args
, "-D_REENTRANT");
1269 if (opts
->pic
&& !is_pe
) strarray_add(spec_args
, "-fPIC");
1270 strarray_add(spec_args
, opts
->shared
? "--dll" : "--exe");
1273 strarray_add(spec_args
, "--fake-module");
1274 strarray_add(spec_args
, "-o");
1275 strarray_add(spec_args
, output_file
);
1279 strarray_add(spec_args
, "-o");
1280 strarray_add(spec_args
, spec_o_name
);
1284 strarray_add(spec_args
, "-E");
1285 strarray_add(spec_args
, spec_file
);
1290 strarray_add(spec_args
, "-F");
1291 strarray_add(spec_args
, output_name
);
1292 strarray_add(spec_args
, "--subsystem");
1293 strarray_add(spec_args
, opts
->gui_app
? "windows" : "console");
1294 if (opts
->large_address_aware
) strarray_add( spec_args
, "--large-address-aware" );
1297 if (opts
->target_platform
== PLATFORM_WINDOWS
) strarray_add(spec_args
, "--safeseh");
1301 strarray_add(spec_args
, "--entry");
1302 strarray_add(spec_args
, entry_point
);
1305 if (opts
->subsystem
)
1307 strarray_add(spec_args
, "--subsystem");
1308 strarray_add(spec_args
, opts
->subsystem
);
1311 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
1312 strarray_add(spec_args
, strmake("-L%s", lib_dirs
->base
[j
]));
1316 for (j
= 0; j
< opts
->delayimports
->size
; j
++)
1317 strarray_add(spec_args
, strmake("-d%s", opts
->delayimports
->base
[j
]));
1320 /* add resource files */
1321 for ( j
= 0; j
< files
->size
; j
++ )
1322 if (files
->base
[j
][1] == 'r') strarray_add(spec_args
, files
->base
[j
]);
1324 /* add other files */
1325 strarray_add(spec_args
, "--");
1326 for ( j
= 0; j
< files
->size
; j
++ )
1328 switch(files
->base
[j
][1])
1333 strarray_add(spec_args
, files
->base
[j
] + 2);
1338 spawn(opts
->prefix
, spec_args
, 0);
1339 strarray_free (spec_args
);
1340 if (fake_module
) return; /* nothing else to do */
1342 /* link everything together now */
1343 link_args
= get_link_args( opts
, output_name
);
1345 if ((opts
->nodefaultlibs
|| opts
->use_msvcrt
) && opts
->target_platform
== PLATFORM_MINGW
)
1347 libgcc
= find_libgcc(opts
->prefix
, link_args
);
1348 if (!libgcc
) libgcc
= "-lgcc";
1351 strarray_add(link_args
, "-o");
1352 strarray_add(link_args
, output_path
);
1354 for ( j
= 0; j
< lib_dirs
->size
; j
++ )
1355 strarray_add(link_args
, strmake("-L%s", lib_dirs
->base
[j
]));
1357 if (is_pe
&& opts
->use_msvcrt
&& !entry_point
&& (opts
->shared
|| opts
->win16_app
))
1358 entry_point
= opts
->target_cpu
== CPU_x86
? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1360 if (is_pe
&& entry_point
)
1362 if (opts
->target_platform
== PLATFORM_WINDOWS
)
1363 strarray_add(link_args
, strmake("-Wl,-entry:%s", entry_point
));
1365 strarray_add(link_args
, strmake("-Wl,--entry,%s%s",
1366 is_pe
&& opts
->target_cpu
== CPU_x86
? "_" : "",
1370 strarray_add(link_args
, spec_o_name
);
1374 for (j
= 0; j
< opts
->delayimports
->size
; j
++)
1376 if (opts
->target_platform
== PLATFORM_WINDOWS
)
1377 strarray_add(link_args
, strmake("-Wl,-delayload:%s", opts
->delayimports
->base
[j
]));
1379 strarray_add(link_args
, strmake("-Wl,-delayload,%s",opts
->delayimports
->base
[j
]));
1383 for ( j
= 0; j
< files
->size
; j
++ )
1385 const char* name
= files
->base
[j
] + 2;
1386 switch(files
->base
[j
][1])
1389 strarray_add(link_args
, strmake("-l%s", name
));
1393 strarray_add(link_args
, name
);
1396 if (is_pe
&& !opts
->lib_suffix
&& strchr(name
, '/'))
1398 /* turn the path back into -Ldir -lfoo options
1399 * this makes sure that we use the specified libs even
1400 * when mingw adds its own import libs to the link */
1401 char *lib
= xstrdup( name
);
1402 char *p
= strrchr( lib
, '/' );
1405 if (!strncmp( p
, "lib", 3 ) && strcmp( p
, "libmsvcrt.a" ))
1407 char *ext
= strrchr( p
, '.' );
1411 strarray_add(link_args
, strmake("-L%s", lib
));
1412 strarray_add(link_args
, strmake("-l%s", p
));
1418 strarray_add(link_args
, name
);
1421 strarray_add(link_args
, files
->base
[j
]);
1426 if (!opts
->nostdlib
&& !is_pe
)
1428 strarray_add(link_args
, "-lm");
1429 strarray_add(link_args
, "-lc");
1432 if (libgcc
) strarray_add(link_args
, libgcc
);
1434 output_file_name
= output_path
;
1435 output_debug_file
= opts
->debug_file
;
1436 output_implib
= opts
->out_implib
;
1437 atexit( cleanup_output_files
);
1439 spawn(opts
->prefix
, link_args
, 0);
1440 strarray_free (link_args
);
1442 if (opts
->debug_file
&& !strendswith(opts
->debug_file
, ".pdb"))
1444 strarray
*tool
, *objcopy
= build_tool_name(opts
, TOOL_OBJCOPY
);
1446 tool
= strarray_dup(objcopy
);
1447 strarray_add(tool
, "--only-keep-debug");
1448 strarray_add(tool
, output_path
);
1449 strarray_add(tool
, opts
->debug_file
);
1450 spawn(opts
->prefix
, tool
, 1);
1451 strarray_free(tool
);
1453 tool
= strarray_dup(objcopy
);
1454 strarray_add(tool
, "--strip-debug");
1455 strarray_add(tool
, output_path
);
1456 spawn(opts
->prefix
, tool
, 1);
1457 strarray_free(tool
);
1460 strarray_add(tool
, "--add-gnu-debuglink");
1461 strarray_add(tool
, opts
->debug_file
);
1462 strarray_add(tool
, output_path
);
1463 spawn(opts
->prefix
, tool
, 0);
1464 strarray_free(tool
);
1467 if (opts
->out_implib
&& !is_pe
)
1470 error("--out-implib requires a .spec or .def file\n");
1472 implib_args
= get_winebuild_args( opts
);
1473 if ((tool
= build_tool_name( opts
, TOOL_CC
))) strarray_add( implib_args
, strmake( "--cc-cmd=%s", strarray_tostring( tool
, " " )));
1474 if ((tool
= build_tool_name( opts
, TOOL_LD
))) strarray_add( implib_args
, strmake( "--ld-cmd=%s", strarray_tostring( tool
, " " )));
1476 strarray_add(implib_args
, "--implib");
1477 strarray_add(implib_args
, "-o");
1478 strarray_add(implib_args
, opts
->out_implib
);
1479 strarray_add(implib_args
, "--export");
1480 strarray_add(implib_args
, spec_file
);
1482 spawn(opts
->prefix
, implib_args
, 0);
1483 strarray_free (implib_args
);
1486 /* set the base address with prelink if linker support is not present */
1487 if (opts
->prelink
&& !opts
->target
)
1489 if (opts
->prelink
[0] && strcmp(opts
->prelink
,"false"))
1491 strarray
*prelink_args
= strarray_alloc();
1492 strarray_add(prelink_args
, opts
->prelink
);
1493 strarray_add(prelink_args
, "--reloc-only");
1494 strarray_add(prelink_args
, opts
->image_base
);
1495 strarray_add(prelink_args
, output_path
);
1496 spawn(opts
->prefix
, prelink_args
, 1);
1497 strarray_free(prelink_args
);
1501 if (!is_pe
) fixup_constructors( opts
, output_path
);
1502 else if (opts
->wine_builtin
) make_wine_builtin( opts
, output_path
);
1504 /* create the loader script */
1505 if (generate_app_loader
)
1506 create_file(output_file
, 0755, app_loader_template
, strmake("%s.so", output_name
));
1510 static void forward( struct options
*opts
)
1512 strarray
* args
= strarray_alloc();
1514 strarray_addall(args
, get_translator(opts
));
1515 strarray_addall(args
, opts
->compiler_args
);
1516 strarray_addall(args
, opts
->linker_args
);
1517 spawn(opts
->prefix
, args
, 0);
1518 strarray_free (args
);
1521 static int is_linker_arg(const char* arg
)
1523 static const char* link_switches
[] =
1525 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1526 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1527 "-fprofile-generate", "-fprofile-use"
1538 if (strncmp("-Wl,", arg
, 4) == 0) return 1;
1541 if (strcmp("-Xlinker", arg
) == 0) return 1;
1544 if (strcmp("-arch", arg
) == 0) return 1;
1547 if (strncmp("-fuse-ld=", arg
, 9) == 0) return 1;
1550 if (strncmp("-rtlib=", arg
, 7) == 0) return 1;
1554 for (j
= 0; j
< ARRAY_SIZE(link_switches
); j
++)
1555 if (strcmp(link_switches
[j
], arg
) == 0) return 1;
1560 static void parse_target_option( struct options
*opts
, const char *target
)
1562 char *p
, *spec
= xstrdup( target
);
1565 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
1567 /* get the CPU part */
1569 if ((p
= strchr( spec
, '-' )))
1572 for (i
= 0; i
< ARRAY_SIZE(cpu_names
); i
++)
1574 if (!strcmp( cpu_names
[i
].name
, spec
))
1576 opts
->target_cpu
= cpu_names
[i
].cpu
;
1580 if (i
== ARRAY_SIZE(cpu_names
))
1581 error( "Unrecognized CPU '%s'\n", spec
);
1583 else if (!strcmp( spec
, "mingw32" ))
1585 opts
->target_cpu
= CPU_x86
;
1589 error( "Invalid target specification '%s'\n", target
);
1591 /* get the OS part */
1593 opts
->target_platform
= PLATFORM_UNSPECIFIED
; /* default value */
1596 for (i
= 0; i
< ARRAY_SIZE(platform_names
); i
++)
1598 if (!strncmp( platform_names
[i
].name
, p
, strlen(platform_names
[i
].name
) ))
1600 opts
->target_platform
= platform_names
[i
].platform
;
1604 if (opts
->target_platform
!= PLATFORM_UNSPECIFIED
|| !(p
= strchr( p
, '-' ))) break;
1609 opts
->target
= xstrdup( target
);
1612 static int is_option( struct options
*opts
, int i
, const char *option
, const char **option_arg
)
1614 if (!strcmp( opts
->args
->base
[i
], option
))
1616 if (opts
->args
->size
== i
) error( "option %s requires an argument\n", opts
->args
->base
[i
] );
1617 *option_arg
= opts
->args
->base
[i
+ 1];
1620 if (!strncmp( opts
->args
->base
[i
], option
, strlen(option
) ) && opts
->args
->base
[i
][strlen(option
)] == '=')
1622 *option_arg
= opts
->args
->base
[i
] + strlen(option
) + 1;
1628 int main(int argc
, char **argv
)
1630 int i
, c
, next_is_arg
= 0, linking
= 1;
1631 int raw_compiler_arg
, raw_linker_arg
, raw_winebuild_arg
;
1632 const char* option_arg
;
1633 struct options opts
;
1638 signal( SIGHUP
, exit_on_signal
);
1640 signal( SIGTERM
, exit_on_signal
);
1641 signal( SIGINT
, exit_on_signal
);
1642 #ifdef HAVE_SIGADDSET
1643 sigemptyset( &signal_mask
);
1644 sigaddset( &signal_mask
, SIGHUP
);
1645 sigaddset( &signal_mask
, SIGTERM
);
1646 sigaddset( &signal_mask
, SIGINT
);
1648 init_argv0_dir( argv
[0] );
1650 /* setup tmp file removal at exit */
1651 tmp_files
= strarray_alloc();
1652 atexit(clean_temp_files
);
1654 /* initialize options */
1655 memset(&opts
, 0, sizeof(opts
));
1656 opts
.target_cpu
= build_cpu
;
1657 opts
.target_platform
= build_platform
;
1658 opts
.lib_dirs
= strarray_alloc();
1659 opts
.files
= strarray_alloc();
1660 opts
.linker_args
= strarray_alloc();
1661 opts
.compiler_args
= strarray_alloc();
1662 opts
.winebuild_args
= strarray_alloc();
1663 opts
.delayimports
= strarray_alloc();
1664 opts
.args
= strarray_alloc();
1667 /* determine the processor type */
1668 if (strendswith(argv
[0], "winecpp")) opts
.processor
= proc_cpp
;
1669 else if (strendswith(argv
[0], "++")) opts
.processor
= proc_cxx
;
1671 for (i
= 1; i
< argc
; i
++)
1673 char *input_buffer
= NULL
, *iter
, *opt
, *out
;
1677 if (argv
[i
][0] != '@' || (fd
= open( argv
[i
] + 1, O_RDONLY
| O_BINARY
)) == -1)
1679 strarray_add( opts
.args
, argv
[i
] );
1682 if ((fstat( fd
, &st
) == -1)) error( "Cannot stat %s", argv
[i
] + 1 );
1685 input_buffer
= xmalloc( st
.st_size
+ 1 );
1686 if (read( fd
, input_buffer
, st
.st_size
) != st
.st_size
) error( "Cannot read %s\n", argv
[i
] + 1 );
1689 for (iter
= input_buffer
; iter
< input_buffer
+ st
.st_size
; iter
++)
1692 while (iter
< input_buffer
+ st
.st_size
&& isspace(*iter
)) iter
++;
1693 if (iter
== input_buffer
+ st
.st_size
) break;
1695 while (iter
< input_buffer
+ st
.st_size
&& (quote
|| !isspace(*iter
)))
1702 else if (*iter
== '\'' || *iter
== '"') quote
= *iter
++;
1705 if (*iter
== '\\' && iter
+ 1 < input_buffer
+ st
.st_size
) iter
++;
1710 strarray_add( opts
.args
, opt
);
1715 for (i
= 0; i
< opts
.args
->size
; i
++)
1717 if (opts
.args
->base
[i
][0] == '-' && opts
.args
->base
[i
][1]) /* option, except '-' alone is stdin, which is a file */
1719 /* determine if this switch is followed by a separate argument */
1722 switch(opts
.args
->base
[i
][1])
1724 case 'x': case 'o': case 'D': case 'U':
1725 case 'I': case 'A': case 'l': case 'u':
1726 case 'b': case 'V': case 'G': case 'L':
1727 case 'B': case 'R': case 'z':
1728 if (opts
.args
->base
[i
][2]) option_arg
= &opts
.args
->base
[i
][2];
1729 else next_is_arg
= 1;
1735 if (strcmp("-aux-info", opts
.args
->base
[i
]) == 0)
1737 if (strcmp("-arch", opts
.args
->base
[i
]) == 0)
1741 if (strcmp("-Xlinker", opts
.args
->base
[i
]) == 0)
1745 c
= opts
.args
->base
[i
][2];
1746 if (c
== 'F' || c
== 'T' || c
== 'Q')
1748 if (opts
.args
->base
[i
][3]) option_arg
= &opts
.args
->base
[i
][3];
1749 else next_is_arg
= 1;
1753 if (strcmp("-framework", opts
.args
->base
[i
]) == 0)
1757 next_is_arg
= strcmp("-target", opts
.args
->base
[i
]) == 0;
1760 next_is_arg
= (strcmp("--param", opts
.args
->base
[i
]) == 0 ||
1761 strcmp("--sysroot", opts
.args
->base
[i
]) == 0 ||
1762 strcmp("--target", opts
.args
->base
[i
]) == 0 ||
1763 strcmp("--wine-objdir", opts
.args
->base
[i
]) == 0 ||
1764 strcmp("--winebuild", opts
.args
->base
[i
]) == 0 ||
1765 strcmp("--lib-suffix", opts
.args
->base
[i
]) == 0);
1770 if (i
+ 1 >= opts
.args
->size
) error("option -%c requires an argument\n", opts
.args
->base
[i
][1]);
1771 option_arg
= opts
.args
->base
[i
+1];
1774 /* determine what options go 'as is' to the linker & the compiler */
1775 raw_linker_arg
= is_linker_arg(opts
.args
->base
[i
]);
1776 raw_compiler_arg
= !raw_linker_arg
;
1777 raw_winebuild_arg
= 0;
1779 /* do a bit of semantic analysis */
1780 switch (opts
.args
->base
[i
][1])
1783 str
= strdup(option_arg
);
1784 if (strendswith(str
, "/")) str
[strlen(str
) - 1] = 0;
1785 if (!opts
.prefix
) opts
.prefix
= strarray_alloc();
1786 strarray_add(opts
.prefix
, str
);
1790 parse_target_option( &opts
, option_arg
);
1791 raw_compiler_arg
= 0;
1794 opts
.version
= xstrdup( option_arg
);
1795 raw_compiler_arg
= 0;
1797 case 'c': /* compile or assemble */
1798 raw_compiler_arg
= 0;
1799 if (opts
.args
->base
[i
][2] == 0) opts
.compile_only
= 1;
1801 case 'S': /* generate assembler code */
1802 case 'E': /* preprocess only */
1803 if (opts
.args
->base
[i
][2] == 0) linking
= 0;
1806 if (strcmp("-fno-short-wchar", opts
.args
->base
[i
]) == 0)
1807 opts
.noshortwchar
= 1;
1808 else if (!strcmp("-fasynchronous-unwind-tables", opts
.args
->base
[i
]))
1809 opts
.unwind_tables
= 1;
1810 else if (!strcmp("-fno-asynchronous-unwind-tables", opts
.args
->base
[i
]))
1811 opts
.unwind_tables
= 0;
1812 else if (!strcmp("-fPIC", opts
.args
->base
[i
]) || !strcmp("-fpic", opts
.args
->base
[i
]))
1814 else if (!strcmp("-fno-PIC", opts
.args
->base
[i
]) || !strcmp("-fno-pic", opts
.args
->base
[i
]))
1818 if (!strcmp( "-isysroot", opts
.args
->base
[i
] )) opts
.isysroot
= opts
.args
->base
[i
+ 1];
1821 strarray_add(opts
.files
, strmake("-l%s", option_arg
));
1822 raw_compiler_arg
= 0;
1825 strarray_add(opts
.lib_dirs
, option_arg
);
1826 raw_compiler_arg
= 0;
1828 case 'M': /* map file generation */
1832 if (strcmp("-mno-cygwin", opts
.args
->base
[i
]) == 0)
1834 opts
.use_msvcrt
= 1;
1835 raw_compiler_arg
= 0;
1836 raw_winebuild_arg
= 1;
1838 else if (strcmp("-mwindows", opts
.args
->base
[i
]) == 0)
1841 raw_compiler_arg
= 0;
1843 else if (strcmp("-mconsole", opts
.args
->base
[i
]) == 0)
1846 raw_compiler_arg
= 0;
1848 else if (strcmp("-municode", opts
.args
->base
[i
]) == 0)
1850 opts
.unicode_app
= 1;
1851 raw_compiler_arg
= 0;
1852 raw_winebuild_arg
= 1;
1854 else if (strcmp("-mthreads", opts
.args
->base
[i
]) == 0)
1856 raw_compiler_arg
= 0;
1858 else if (strcmp("-munix", opts
.args
->base
[i
]) == 0)
1861 raw_compiler_arg
= 0;
1862 raw_winebuild_arg
= 1;
1864 else if (strcmp("-m16", opts
.args
->base
[i
]) == 0)
1867 raw_compiler_arg
= 0;
1868 raw_winebuild_arg
= 1;
1870 else if (strcmp("-m32", opts
.args
->base
[i
]) == 0)
1872 if (opts
.target_cpu
== CPU_x86_64
)
1873 opts
.target_cpu
= CPU_x86
;
1874 else if (opts
.target_cpu
== CPU_ARM64
)
1875 opts
.target_cpu
= CPU_ARM
;
1876 opts
.force_pointer_size
= 4;
1879 else if (strcmp("-m64", opts
.args
->base
[i
]) == 0)
1881 if (opts
.target_cpu
== CPU_x86
)
1882 opts
.target_cpu
= CPU_x86_64
;
1883 else if (opts
.target_cpu
== CPU_ARM
)
1884 opts
.target_cpu
= CPU_ARM64
;
1885 opts
.force_pointer_size
= 8;
1888 else if (!strcmp("-marm", opts
.args
->base
[i
] ) || !strcmp("-mthumb", opts
.args
->base
[i
] ))
1891 raw_winebuild_arg
= 1;
1893 else if (!strncmp("-mcpu=", opts
.args
->base
[i
], 6) ||
1894 !strncmp("-mfpu=", opts
.args
->base
[i
], 6) ||
1895 !strncmp("-march=", opts
.args
->base
[i
], 7) ||
1896 !strncmp("-mfloat-abi=", opts
.args
->base
[i
], 12))
1897 raw_winebuild_arg
= 1;
1900 if (strcmp("-nostdinc", opts
.args
->base
[i
]) == 0)
1902 else if (strcmp("-nodefaultlibs", opts
.args
->base
[i
]) == 0)
1903 opts
.nodefaultlibs
= 1;
1904 else if (strcmp("-nostdlib", opts
.args
->base
[i
]) == 0)
1906 else if (strcmp("-nostartfiles", opts
.args
->base
[i
]) == 0)
1907 opts
.nostartfiles
= 1;
1910 opts
.output_name
= option_arg
;
1911 raw_compiler_arg
= 0;
1914 if (strcmp("-static", opts
.args
->base
[i
]) == 0)
1916 else if(strcmp("-save-temps", opts
.args
->base
[i
]) == 0)
1918 else if (strncmp("-specs=", opts
.args
->base
[i
], 7) == 0)
1920 else if(strcmp("-shared", opts
.args
->base
[i
]) == 0)
1923 raw_compiler_arg
= raw_linker_arg
= 0;
1925 else if (strcmp("-s", opts
.args
->base
[i
]) == 0 && opts
.target_platform
== PLATFORM_APPLE
)
1927 /* On Mac, change -s into -Wl,-x. ld's -s switch
1928 * is deprecated, and it doesn't work on Tiger with
1936 if (is_option( &opts
, i
, "-target", &option_arg
))
1938 parse_target_option( &opts
, option_arg
);
1939 raw_compiler_arg
= raw_linker_arg
= 0;
1943 if (opts
.args
->base
[i
][2] == 0) verbose
++;
1946 if (strncmp("-Wl,", opts
.args
->base
[i
], 4) == 0)
1949 strarray
* Wl
= strarray_fromstring(opts
.args
->base
[i
] + 4, ",");
1950 for (j
= 0; j
< Wl
->size
; j
++)
1952 if (!strcmp(Wl
->base
[j
], "--image-base") && j
< Wl
->size
- 1)
1954 opts
.image_base
= strdup( Wl
->base
[++j
] );
1957 if (!strcmp(Wl
->base
[j
], "--section-alignment") && j
< Wl
->size
- 1)
1959 opts
.section_align
= strdup( Wl
->base
[++j
] );
1962 if (!strcmp(Wl
->base
[j
], "--file-alignment") && j
< Wl
->size
- 1)
1964 opts
.file_align
= strdup( Wl
->base
[++j
] );
1967 if (!strcmp(Wl
->base
[j
], "--large-address-aware"))
1969 opts
.large_address_aware
= 1;
1972 if (!strcmp(Wl
->base
[j
], "--wine-builtin"))
1974 opts
.wine_builtin
= 1;
1977 if (!strcmp(Wl
->base
[j
], "--subsystem") && j
< Wl
->size
- 1)
1979 opts
.subsystem
= strdup( Wl
->base
[++j
] );
1982 if (!strcmp(Wl
->base
[j
], "--entry") && j
< Wl
->size
- 1)
1984 opts
.entry_point
= strdup( Wl
->base
[++j
] );
1987 if (!strcmp(Wl
->base
[j
], "-delayload") && j
< Wl
->size
- 1)
1989 strarray_add( opts
.delayimports
, Wl
->base
[++j
] );
1992 if (!strcmp(Wl
->base
[j
], "--debug-file") && j
< Wl
->size
- 1)
1994 opts
.debug_file
= strdup( Wl
->base
[++j
] );
1997 if (!strcmp(Wl
->base
[j
], "--whole-archive") || !strcmp(Wl
->base
[j
], "--no-whole-archive"))
1999 strarray_add( opts
.files
, strmake( "-Wl,%s", Wl
->base
[j
] ));
2002 if (!strcmp(Wl
->base
[j
], "--out-implib"))
2004 opts
.out_implib
= strdup( Wl
->base
[++j
] );
2007 if (!strcmp(Wl
->base
[j
], "-static")) linking
= -1;
2008 strarray_add(opts
.linker_args
, strmake("-Wl,%s",Wl
->base
[j
]));
2011 raw_compiler_arg
= raw_linker_arg
= 0;
2013 else if (strncmp("-Wb,", opts
.args
->base
[i
], 4) == 0)
2015 strarray
* Wb
= strarray_fromstring(opts
.args
->base
[i
] + 4, ",");
2016 strarray_addall(opts
.winebuild_args
, Wb
);
2018 /* don't pass it to the compiler, it generates errors */
2019 raw_compiler_arg
= raw_linker_arg
= 0;
2023 lang
= strmake("-x%s", option_arg
);
2024 strarray_add(opts
.files
, lang
);
2025 /* we'll pass these flags ourselves, explicitly */
2026 raw_compiler_arg
= raw_linker_arg
= 0;
2029 if (strcmp("-static", opts
.args
->base
[i
]+1) == 0)
2031 else if (is_option( &opts
, i
, "--sysroot", &option_arg
))
2033 opts
.sysroot
= option_arg
;
2036 else if (is_option( &opts
, i
, "--target", &option_arg
))
2038 parse_target_option( &opts
, option_arg
);
2039 raw_compiler_arg
= raw_linker_arg
= 0;
2041 else if (is_option( &opts
, i
, "--wine-objdir", &option_arg
))
2043 opts
.wine_objdir
= option_arg
;
2044 raw_compiler_arg
= raw_linker_arg
= 0;
2046 else if (is_option( &opts
, i
, "--winebuild", &option_arg
))
2048 opts
.winebuild
= option_arg
;
2049 raw_compiler_arg
= raw_linker_arg
= 0;
2051 else if (is_option( &opts
, i
, "--lib-suffix", &option_arg
))
2053 opts
.lib_suffix
= option_arg
;
2054 raw_compiler_arg
= raw_linker_arg
= 0;
2059 /* put the arg into the appropriate bucket */
2062 strarray_add( opts
.linker_args
, opts
.args
->base
[i
] );
2063 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2064 strarray_add( opts
.linker_args
, opts
.args
->base
[i
+ 1] );
2066 if (raw_compiler_arg
)
2068 strarray_add( opts
.compiler_args
, opts
.args
->base
[i
] );
2069 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2070 strarray_add( opts
.compiler_args
, opts
.args
->base
[i
+ 1] );
2072 if (raw_winebuild_arg
)
2074 strarray_add( opts
.winebuild_args
, opts
.args
->base
[i
] );
2075 if (next_is_arg
&& (i
+ 1 < opts
.args
->size
))
2076 strarray_add( opts
.winebuild_args
, opts
.args
->base
[i
+ 1] );
2079 /* skip the next token if it's an argument */
2080 if (next_is_arg
) i
++;
2084 strarray_add( opts
.files
, opts
.args
->base
[i
] );
2088 if (opts
.processor
== proc_cpp
) linking
= 0;
2089 if (linking
== -1) error("Static linking is not supported\n");
2091 if (opts
.files
->size
== 0) forward(&opts
);
2092 else if (linking
) build(&opts
);
2093 else compile(&opts
, lang
);
2095 output_file_name
= NULL
;
2096 output_debug_file
= NULL
;
2097 output_implib
= NULL
;