winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / tools / winegcc / winegcc.c
blob694e2544a240bdb8f3062fe22b28988be1854797
1 /*
2 * MinGW wrapper: makes gcc behave like MinGW.
4 * Copyright 2000 Manuel Novoa III
5 * Copyright 2000 Francois Gouget
6 * Copyright 2002 Dimitrie O. Paun
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * DESCRIPTION
24 * all options for gcc start with '-' and are for the most part
25 * single options (no parameters as separate argument).
26 * There are of course exceptions to this rule, so here is an
27 * exhaustive list of options that do take parameters (potentially)
28 * as a separate argument:
30 * Compiler:
31 * -x language
32 * -o filename
33 * -aux-info filename
35 * Preprocessor:
36 * -D name
37 * -U name
38 * -I dir
39 * -MF file
40 * -MT target
41 * -MQ target
42 * (all -i.* arg)
43 * -include file
44 * -imacros file
45 * -idirafter dir
46 * -iwithprefix dir
47 * -iwithprefixbefore dir
48 * -isystem dir
49 * -A predicate=answer
51 * Linking:
52 * -l library
53 * -Xlinker option
54 * -u symbol
56 * Misc:
57 * -b machine
58 * -V version
59 * -G num (see NOTES below)
61 * NOTES
62 * There is -G option for compatibility with System V that
63 * takes no parameters. This makes "-G num" parsing ambiguous.
64 * This option is synonymous to -shared, and as such we will
65 * not support it for now.
67 * Special interest options
69 * Assembler Option
70 * -Wa,option
72 * Linker Options
73 * object-file-name -llibrary -nostartfiles -nodefaultlibs
74 * -nostdlib -s -static -static-libgcc -static-libstdc++
75 * -shared -shared-libgcc -symbolic -Wl,option
76 * -Xlinker option -u symbol --image-base -fuse-ld
78 * Directory Options
79 * -Bprefix -Idir -I- -Ldir -specs=file
81 * Target Options
82 * -b machine -V version
84 * Please note that the Target Options are relevant to everything:
85 * compiler, linker, assembler, preprocessor.
87 */
89 #include "config.h"
90 #include "wine/port.h"
92 #include <assert.h>
93 #include <stdio.h>
94 #include <stdlib.h>
95 #include <signal.h>
96 #include <stdarg.h>
97 #include <string.h>
98 #include <errno.h>
99 #include <ctype.h>
101 #include "utils.h"
103 static const char* app_loader_template =
104 "#!/bin/sh\n"
105 "\n"
106 "appname=\"%s\"\n"
107 "# determine the application directory\n"
108 "appdir=''\n"
109 "case \"$0\" in\n"
110 " */*)\n"
111 " # $0 contains a path, use it\n"
112 " appdir=`dirname \"$0\"`\n"
113 " ;;\n"
114 " *)\n"
115 " # no directory in $0, search in PATH\n"
116 " saved_ifs=$IFS\n"
117 " IFS=:\n"
118 " for d in $PATH\n"
119 " do\n"
120 " IFS=$saved_ifs\n"
121 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
122 " done\n"
123 " ;;\n"
124 "esac\n"
125 "\n"
126 "# figure out the full app path\n"
127 "if [ -n \"$appdir\" ]; then\n"
128 " apppath=\"$appdir/$appname\"\n"
129 " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
130 " export WINEDLLPATH\n"
131 "else\n"
132 " apppath=\"$appname\"\n"
133 "fi\n"
134 "\n"
135 "# determine the WINELOADER\n"
136 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
137 "\n"
138 "# and try to start the app\n"
139 "exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
142 static const char *output_file_name;
143 static const char *output_debug_file;
144 static const char *output_implib;
145 static int keep_generated = 0;
146 static strarray* tmp_files;
147 #ifdef HAVE_SIGSET_T
148 static sigset_t signal_mask;
149 #endif
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 };
157 static const struct
159 const char *name;
160 enum target_cpu cpu;
161 } cpu_names[] =
163 { "i386", CPU_x86 },
164 { "i486", CPU_x86 },
165 { "i586", CPU_x86 },
166 { "i686", CPU_x86 },
167 { "i786", CPU_x86 },
168 { "amd64", CPU_x86_64 },
169 { "x86_64", CPU_x86_64 },
170 { "powerpc", CPU_POWERPC },
171 { "arm", CPU_ARM },
172 { "armv5", CPU_ARM },
173 { "armv6", CPU_ARM },
174 { "armv7", CPU_ARM },
175 { "armv7a", CPU_ARM },
176 { "arm64", CPU_ARM64 },
177 { "aarch64", CPU_ARM64 },
180 static const struct
182 const char *name;
183 enum target_platform platform;
184 } platform_names[] =
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 }
197 struct options
199 enum processor processor;
200 enum target_cpu target_cpu;
201 enum target_platform target_platform;
202 const char *target;
203 const char *version;
204 int shared;
205 int use_msvcrt;
206 int nostdinc;
207 int nostdlib;
208 int nostartfiles;
209 int nodefaultlibs;
210 int noshortwchar;
211 int unix_lib;
212 int gui_app;
213 int unicode_app;
214 int win16_app;
215 int compile_only;
216 int force_pointer_size;
217 int large_address_aware;
218 int wine_builtin;
219 int unwind_tables;
220 int strip;
221 int pic;
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;
228 const char* sysroot;
229 const char* isysroot;
230 const char* lib_suffix;
231 const char* subsystem;
232 const char* entry_point;
233 const char* prelink;
234 const char* debug_file;
235 const char* out_implib;
236 strarray* prefix;
237 strarray* lib_dirs;
238 strarray* args;
239 strarray* linker_args;
240 strarray* compiler_args;
241 strarray* winebuild_args;
242 strarray* files;
243 strarray* delayimports;
246 #ifdef __i386__
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;
256 #else
257 #error Unsupported CPU
258 #endif
260 #ifdef __APPLE__
261 static enum target_platform build_platform = PLATFORM_APPLE;
262 #elif defined(__ANDROID__)
263 static enum target_platform build_platform = PLATFORM_ANDROID;
264 #elif defined(__sun)
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;
270 #else
271 static enum target_platform build_platform = PLATFORM_UNSPECIFIED;
272 #endif
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)
283 unsigned int i;
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)
299 int fd;
300 char* tmp = strmake("%s-XXXXXX%s", prefix, suffix);
302 #ifdef HAVE_SIGPROCMASK
303 sigset_t old_set;
304 /* block signals while manipulating the temp files list */
305 sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
306 #endif
307 fd = mkstemps( tmp, strlen(suffix) );
308 if (fd == -1)
310 /* could not create it in current directory, try in TMPDIR */
311 const char* tmpdir;
313 free(tmp);
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" );
319 close( fd );
320 strarray_add(tmp_files, tmp);
321 #ifdef HAVE_SIGPROCMASK
322 sigprocmask( SIG_SETMASK, &old_set, NULL );
323 #endif
324 return tmp;
327 enum tool
329 TOOL_CC,
330 TOOL_CXX,
331 TOOL_CPP,
332 TOOL_LD,
333 TOOL_OBJCOPY,
336 static const struct
338 const char *base;
339 const char *llvm_base;
340 const char *deflt;
341 } tool_names[] =
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;
355 const char *path;
356 strarray *ret;
357 char* str;
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);
371 else
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 );
379 if (!path)
381 error( "Could not find %s\n", base );
382 return NULL;
385 ret = strarray_fromstring( path, " " );
386 if (!strncmp( llvm_base, "clang", 5 ))
388 if (opts->target)
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" );
396 return ret;
399 static strarray* get_translator(struct options *opts)
401 enum tool tool;
403 switch(opts->processor)
405 case proc_cpp:
406 tool = TOOL_CPP;
407 break;
408 case proc_cc:
409 case proc_as:
410 tool = TOOL_CC;
411 break;
412 case proc_cxx:
413 tool = TOOL_CXX;
414 break;
415 default:
416 assert(0);
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;
428 int ret;
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 );
442 if (sout >= 0)
444 dup2( sout, fileno(stdout) );
445 close( sout );
447 if (serr >= 0)
449 dup2( serr, fileno(stderr) );
450 close( serr );
452 strarray_free( link );
453 return ret;
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)
467 case PLATFORM_APPLE:
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 );
483 return link_args;
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 );
494 break;
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 ));
499 break;
501 case PLATFORM_MINGW:
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 );
541 return link_args;
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 ));
553 if (opts->subsystem)
554 strarray_add( flags, strmake("-Wl,-subsystem:%s", opts->subsystem ));
555 else
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 );
572 return link_args;
574 default:
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) );
579 else
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");
584 break;
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 );
597 return link_args;
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 );
604 if (fd != -1)
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);
614 else
615 ret = (opts->target_cpu != CPU_x86_64 && opts->target_cpu != CPU_ARM64);
618 close( fd );
620 return ret;
623 static const char *get_multiarch_dir( enum target_cpu cpu )
625 switch(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";
632 default:
633 assert(0);
634 return NULL;
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 : "";
644 unsigned int i;
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 : "";
657 char *p, *buffer;
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--;
666 strcpy( p, ntdll );
667 if (check_platform( opts, buffer )) goto found;
668 if (p > buffer + 2 && (!memcmp( p - 2, "32", 2 ) || !memcmp( p - 2, "64", 2 )))
670 p -= 2;
671 strcpy( p, ntdll );
672 if (check_platform( opts, buffer )) goto found;
674 strcpy( p, bit_suffix );
675 strcat( p, ntdll );
676 if (check_platform( opts, buffer )) goto found;
677 strcpy( p, target_multiarch );
678 strcat( p, ntdll );
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--;
685 strcpy( p, ntdll );
687 /* try to fixup each parent dirs named lib, lib32 or lib64 with target bitness suffix */
688 while (p > buffer)
690 p--;
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;
705 if (p[4] == '/')
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 );
723 free( buffer );
724 continue;
726 found:
727 buffer[strlen(buffer) - strlen(ntdll)] = 0;
728 return buffer;
730 return strmake( "%s%s", root, LIBDIR );
733 static void init_argv0_dir( const char *argv0 )
735 #ifndef _WIN32
736 char *p, *dir;
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 );
742 #else
743 dir = realpath( argv0, NULL );
744 #endif
745 if (!dir) return;
746 if (!(p = strrchr( dir, '/' ))) return;
747 if (p == dir) p++;
748 *p = 0;
749 bindir = dir;
750 includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR );
751 libdir = strmake( "%s/%s", dir, BIN_TO_LIBDIR );
752 #endif
755 static void compile(struct options* opts, const char* lang)
757 strarray* comp_args = strarray_alloc();
758 unsigned int i, j;
759 int gcc_defs = 0;
760 strarray* gcc;
761 strarray* gpp;
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 */
772 case proc_cc:
773 case proc_cxx:
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]);
785 strarray_free(gcc);
786 strarray_free(gpp);
787 break;
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");
801 if (opts->pic)
802 strarray_add(comp_args, "-fPIC");
803 else
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__");
822 if (gcc_defs)
824 switch (opts->target_cpu)
826 case CPU_x86_64:
827 case CPU_ARM64:
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");
831 break;
832 case CPU_x86:
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__))");
836 break;
837 case CPU_ARM:
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");
841 break;
842 case CPU_POWERPC:
843 strarray_add(comp_args, "-D__stdcall=");
844 strarray_add(comp_args, "-D__cdecl=");
845 strarray_add(comp_args, "-D__fastcall=");
846 break;
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");
871 else
872 strarray_add(comp_args, "-D__int64=long long");
874 no_compat_defines:
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;
941 char* base_name;
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);
946 copts = *opts;
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);
953 free(base_name);
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();
964 unsigned int i;
966 if (opts->winebuild)
967 binary = opts->winebuild;
968 else if (opts->wine_objdir)
969 binary = strmake( "%s/tools/winebuild/winebuild%s", opts->wine_objdir, EXEEXT );
970 else if (winebuild)
971 binary = find_binary( opts->prefix, winebuild );
972 else if (bindir)
973 binary = strmake( "%s/winebuild%s", bindir, EXEEXT );
974 else
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" );
980 if (opts->target)
982 strarray_add( spec_args, "--target" );
983 strarray_add( spec_args, opts->target );
985 if (opts->prefix)
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" );
993 return spec_args;
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;
1021 free( lib );
1022 return NULL;
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;
1031 char *libgcc, *p;
1032 struct stat st;
1033 size_t cnt;
1034 int ret;
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 );
1043 if (sout >= 0)
1045 dup2( sout, fileno(stdout) );
1046 close( sout );
1048 if (serr >= 0)
1050 dup2( serr, fileno(stderr) );
1051 close( serr );
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);
1061 close(sout);
1062 libgcc[cnt] = 0;
1063 if ((p = strchr(libgcc, '\n'))) *p = 0;
1064 return libgcc;
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))
1075 case file_arh:
1076 strarray_add(files, strmake("-a%s", fullname));
1077 break;
1078 case file_dll:
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));
1083 free(static_lib);
1085 break;
1086 case file_so:
1087 default:
1088 /* keep it anyway, the linker may know what to do with it */
1089 strarray_add(files, strmake("-l%s", library));
1090 break;
1092 free(fullname);
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);
1106 unsigned int j;
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]);
1124 return;
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 );
1154 else
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];
1168 if (file[0] != '-')
1170 switch(get_file_type(file))
1172 case file_def:
1173 case file_spec:
1174 if (spec_file)
1175 error("Only one spec file can be specified\n");
1176 spec_file = file;
1177 break;
1178 case file_rc:
1179 /* FIXME: invoke wrc to build it */
1180 error("Can't compile .rc file at the moment: %s\n", file);
1181 break;
1182 case file_res:
1183 strarray_add(files, strmake("-r%s", file));
1184 break;
1185 case file_obj:
1186 strarray_add(files, strmake("-o%s", file));
1187 break;
1188 case file_arh:
1189 if (opts->use_msvcrt)
1191 const char *p = strrchr(file, '/');
1192 if (p) p++;
1193 else p = file;
1194 if (!strncmp(p, "libmsvcr", 8) || !strncmp(p, "libucrt", 7)) crt_lib = file;
1196 strarray_add(files, strmake("-a%s", file));
1197 break;
1198 case file_so:
1199 strarray_add(files, strmake("-s%s", file));
1200 break;
1201 case file_na:
1202 error("File does not exist: %s\n", file);
1203 break;
1204 default:
1205 file = compile_to_object(opts, file, lang);
1206 strarray_add(files, strmake("-o%s", file));
1207 break;
1210 else if (file[1] == 'l')
1211 add_library(opts, lib_dirs, files, file + 2 );
1212 else if (file[1] == 'x')
1213 lang = file;
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)
1222 if (opts->gui_app)
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)
1237 if (!crt_lib)
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");
1272 if (fake_module)
1274 strarray_add(spec_args, "--fake-module");
1275 strarray_add(spec_args, "-o");
1276 strarray_add(spec_args, output_file);
1278 else
1280 strarray_add(spec_args, "-o");
1281 strarray_add(spec_args, spec_o_name);
1283 if (spec_file)
1285 strarray_add(spec_args, "-E");
1286 strarray_add(spec_args, spec_file);
1289 if (!opts->shared)
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");
1300 if (entry_point)
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]));
1315 if (!is_pe)
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])
1331 case 'd':
1332 case 'a':
1333 case 'o':
1334 strarray_add(spec_args, files->base[j] + 2);
1335 break;
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));
1365 else
1366 strarray_add(link_args, strmake("-Wl,--entry,%s%s",
1367 is_pe && opts->target_cpu == CPU_x86 ? "_" : "",
1368 entry_point));
1371 strarray_add(link_args, spec_o_name);
1373 if (is_pe)
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]));
1379 else
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])
1389 case 'l':
1390 strarray_add(link_args, strmake("-l%s", name));
1391 break;
1392 case 's':
1393 case 'o':
1394 strarray_add(link_args, name);
1395 break;
1396 case 'a':
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, '/' );
1405 *p++ = 0;
1406 if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
1408 char *ext = strrchr( p, '.' );
1410 if (ext) *ext = 0;
1411 p += 3;
1412 strarray_add(link_args, strmake("-L%s", lib ));
1413 strarray_add(link_args, strmake("-l%s", p ));
1414 free( lib );
1415 break;
1417 free( lib );
1419 strarray_add(link_args, name);
1420 break;
1421 case 'W':
1422 strarray_add(link_args, files->base[j]);
1423 break;
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);
1460 tool = objcopy;
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)
1470 if (!spec_file)
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"
1530 unsigned int j;
1532 switch (arg[1])
1534 case 'R':
1535 case 'z':
1536 case 'u':
1537 return 1;
1538 case 'W':
1539 if (strncmp("-Wl,", arg, 4) == 0) return 1;
1540 break;
1541 case 'X':
1542 if (strcmp("-Xlinker", arg) == 0) return 1;
1543 break;
1544 case 'a':
1545 if (strcmp("-arch", arg) == 0) return 1;
1546 break;
1547 case 'f':
1548 if (strncmp("-fuse-ld=", arg, 9) == 0) return 1;
1549 break;
1550 case 'r':
1551 if (strncmp("-rtlib=", arg, 7) == 0) return 1;
1552 break;
1555 for (j = 0; j < ARRAY_SIZE(link_switches); j++)
1556 if (strcmp(link_switches[j], arg) == 0) return 1;
1558 return 0;
1561 static void parse_target_option( struct options *opts, const char *target )
1563 char *p, *spec = xstrdup( target );
1564 unsigned int i;
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, '-' )))
1572 *p++ = 0;
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;
1578 break;
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;
1587 p = spec;
1589 else
1590 error( "Invalid target specification '%s'\n", target );
1592 /* get the OS part */
1594 opts->target_platform = PLATFORM_UNSPECIFIED; /* default value */
1595 for (;;)
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;
1602 break;
1605 if (opts->target_platform != PLATFORM_UNSPECIFIED || !(p = strchr( p, '-' ))) break;
1606 p++;
1609 free( spec );
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];
1619 return 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;
1624 return 1;
1626 return 0;
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;
1635 char* lang = 0;
1636 char* str;
1638 #ifdef SIGHUP
1639 signal( SIGHUP, exit_on_signal );
1640 #endif
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 );
1648 #endif
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();
1666 opts.pic = 1;
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;
1675 struct stat st;
1676 int fd;
1678 if (argv[i][0] != '@' || (fd = open( argv[i] + 1, O_RDONLY | O_BINARY )) == -1)
1680 strarray_add( opts.args, argv[i] );
1681 continue;
1683 if ((fstat( fd, &st ) == -1)) error( "Cannot stat %s\n", argv[i] + 1 );
1684 if (st.st_size)
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 );
1689 close( fd );
1690 for (iter = input_buffer; iter < input_buffer + st.st_size; iter++)
1692 char quote = 0;
1693 while (iter < input_buffer + st.st_size && isspace(*iter)) iter++;
1694 if (iter == input_buffer + st.st_size) break;
1695 opt = out = iter;
1696 while (iter < input_buffer + st.st_size && (quote || !isspace(*iter)))
1698 if (*iter == quote)
1700 iter++;
1701 quote = 0;
1703 else if (*iter == '\'' || *iter == '"') quote = *iter++;
1704 else
1706 if (*iter == '\\' && iter + 1 < input_buffer + st.st_size) iter++;
1707 *out++ = *iter++;
1710 *out = 0;
1711 strarray_add( opts.args, opt );
1715 /* parse options */
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 */
1721 next_is_arg = 0;
1722 option_arg = 0;
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;
1731 break;
1732 case 'i':
1733 next_is_arg = 1;
1734 break;
1735 case 'a':
1736 if (strcmp("-aux-info", opts.args->base[i]) == 0)
1737 next_is_arg = 1;
1738 if (strcmp("-arch", opts.args->base[i]) == 0)
1739 next_is_arg = 1;
1740 break;
1741 case 'X':
1742 if (strcmp("-Xlinker", opts.args->base[i]) == 0)
1743 next_is_arg = 1;
1744 break;
1745 case 'M':
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;
1752 break;
1753 case 'f':
1754 if (strcmp("-framework", opts.args->base[i]) == 0)
1755 next_is_arg = 1;
1756 break;
1757 case 't':
1758 next_is_arg = strcmp("-target", opts.args->base[i]) == 0;
1759 break;
1760 case '-':
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);
1767 break;
1769 if (next_is_arg)
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])
1783 case 'B':
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);
1788 raw_linker_arg = 1;
1789 break;
1790 case 'b':
1791 parse_target_option( &opts, option_arg );
1792 raw_compiler_arg = 0;
1793 break;
1794 case 'V':
1795 opts.version = xstrdup( option_arg );
1796 raw_compiler_arg = 0;
1797 break;
1798 case 'c': /* compile or assemble */
1799 raw_compiler_arg = 0;
1800 if (opts.args->base[i][2] == 0) opts.compile_only = 1;
1801 /* fall through */
1802 case 'S': /* generate assembler code */
1803 case 'E': /* preprocess only */
1804 if (opts.args->base[i][2] == 0) linking = 0;
1805 break;
1806 case 'f':
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]))
1814 opts.pic = 1;
1815 else if (!strcmp("-fno-PIC", opts.args->base[i]) || !strcmp("-fno-pic", opts.args->base[i]))
1816 opts.pic = 0;
1817 break;
1818 case 'i':
1819 if (!strcmp( "-isysroot", opts.args->base[i] )) opts.isysroot = opts.args->base[i + 1];
1820 break;
1821 case 'l':
1822 strarray_add(opts.files, strmake("-l%s", option_arg));
1823 raw_compiler_arg = 0;
1824 break;
1825 case 'L':
1826 strarray_add(opts.lib_dirs, option_arg);
1827 raw_compiler_arg = 0;
1828 break;
1829 case 'M': /* map file generation */
1830 linking = 0;
1831 break;
1832 case 'm':
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)
1841 opts.gui_app = 1;
1842 raw_compiler_arg = 0;
1844 else if (strcmp("-mconsole", opts.args->base[i]) == 0)
1846 opts.gui_app = 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)
1861 opts.unix_lib = 1;
1862 raw_compiler_arg = 0;
1863 raw_winebuild_arg = 1;
1865 else if (strcmp("-m16", opts.args->base[i]) == 0)
1867 opts.win16_app = 1;
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;
1878 raw_linker_arg = 1;
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;
1887 raw_linker_arg = 1;
1889 else if (!strcmp("-marm", opts.args->base[i] ) || !strcmp("-mthumb", opts.args->base[i] ))
1891 raw_linker_arg = 1;
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;
1899 break;
1900 case 'n':
1901 if (strcmp("-nostdinc", opts.args->base[i]) == 0)
1902 opts.nostdinc = 1;
1903 else if (strcmp("-nodefaultlibs", opts.args->base[i]) == 0)
1904 opts.nodefaultlibs = 1;
1905 else if (strcmp("-nostdlib", opts.args->base[i]) == 0)
1906 opts.nostdlib = 1;
1907 else if (strcmp("-nostartfiles", opts.args->base[i]) == 0)
1908 opts.nostartfiles = 1;
1909 break;
1910 case 'o':
1911 opts.output_name = option_arg;
1912 raw_compiler_arg = 0;
1913 break;
1914 case 's':
1915 if (strcmp("-static", opts.args->base[i]) == 0)
1916 linking = -1;
1917 else if(strcmp("-save-temps", opts.args->base[i]) == 0)
1918 keep_generated = 1;
1919 else if (strncmp("-specs=", opts.args->base[i], 7) == 0)
1920 raw_linker_arg = 1;
1921 else if(strcmp("-shared", opts.args->base[i]) == 0)
1923 opts.shared = 1;
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
1930 * MH_BUNDLEs anyway
1932 opts.strip = 1;
1933 raw_linker_arg = 0;
1935 break;
1936 case 't':
1937 if (is_option( &opts, i, "-target", &option_arg ))
1939 parse_target_option( &opts, option_arg );
1940 raw_compiler_arg = raw_linker_arg = 0;
1942 break;
1943 case 'v':
1944 if (opts.args->base[i][2] == 0) verbose++;
1945 break;
1946 case 'W':
1947 if (strncmp("-Wl,", opts.args->base[i], 4) == 0)
1949 unsigned int j;
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] );
1956 continue;
1958 if (!strcmp(Wl->base[j], "--section-alignment") && j < Wl->size - 1)
1960 opts.section_align = strdup( Wl->base[++j] );
1961 continue;
1963 if (!strcmp(Wl->base[j], "--file-alignment") && j < Wl->size - 1)
1965 opts.file_align = strdup( Wl->base[++j] );
1966 continue;
1968 if (!strcmp(Wl->base[j], "--large-address-aware"))
1970 opts.large_address_aware = 1;
1971 continue;
1973 if (!strcmp(Wl->base[j], "--wine-builtin"))
1975 opts.wine_builtin = 1;
1976 continue;
1978 if (!strcmp(Wl->base[j], "--subsystem") && j < Wl->size - 1)
1980 opts.subsystem = strdup( Wl->base[++j] );
1981 continue;
1983 if (!strcmp(Wl->base[j], "--entry") && j < Wl->size - 1)
1985 opts.entry_point = strdup( Wl->base[++j] );
1986 continue;
1988 if (!strcmp(Wl->base[j], "-delayload") && j < Wl->size - 1)
1990 strarray_add( opts.delayimports, Wl->base[++j] );
1991 continue;
1993 if (!strcmp(Wl->base[j], "--debug-file") && j < Wl->size - 1)
1995 opts.debug_file = strdup( Wl->base[++j] );
1996 continue;
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] ));
2001 continue;
2003 if (!strcmp(Wl->base[j], "--out-implib"))
2005 opts.out_implib = strdup( Wl->base[++j] );
2006 continue;
2008 if (!strcmp(Wl->base[j], "-static")) linking = -1;
2009 strarray_add(opts.linker_args, strmake("-Wl,%s",Wl->base[j]));
2011 strarray_free(Wl);
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);
2018 strarray_free(Wb);
2019 /* don't pass it to the compiler, it generates errors */
2020 raw_compiler_arg = raw_linker_arg = 0;
2022 break;
2023 case 'x':
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;
2028 break;
2029 case '-':
2030 if (strcmp("-static", opts.args->base[i]+1) == 0)
2031 linking = -1;
2032 else if (is_option( &opts, i, "--sysroot", &option_arg ))
2034 opts.sysroot = option_arg;
2035 raw_linker_arg = 1;
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;
2057 break;
2060 /* put the arg into the appropriate bucket */
2061 if (raw_linker_arg)
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++;
2083 else
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;
2099 return 0;