winebuild: Select all __wine_spec_exe* entry inside winebuild.
[wine.git] / tools / winegcc / winegcc.c
blob6decb02ee04f306abb2eadc6e40f41a21688a3fd
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));
564 if (opts->out_implib)
565 strarray_add(link_args, strmake("-Wl,-implib:%s", opts->out_implib));
567 else if (!opts->strip)
568 strarray_add(link_args, "-Wl,-debug:dwarf");
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 libwine[] = "/libwine.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(libwine) + 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, libwine );
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, libwine );
672 if (check_platform( opts, buffer )) goto found;
674 strcpy( p, bit_suffix );
675 strcat( p, libwine );
676 if (check_platform( opts, buffer )) goto found;
677 strcpy( p, target_multiarch );
678 strcat( p, libwine );
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, libwine );
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(libwine)] = 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)
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;
940 char* base_name;
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);
945 copts = *opts;
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);
952 free(base_name);
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();
963 unsigned int i;
965 if (opts->winebuild)
966 binary = opts->winebuild;
967 else if (opts->wine_objdir)
968 binary = strmake( "%s/tools/winebuild/winebuild%s", opts->wine_objdir, EXEEXT );
969 else if (winebuild)
970 binary = find_binary( opts->prefix, winebuild );
971 else if (bindir)
972 binary = strmake( "%s/winebuild%s", bindir, EXEEXT );
973 else
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" );
979 if (opts->target)
981 strarray_add( spec_args, "--target" );
982 strarray_add( spec_args, opts->target );
984 if (opts->prefix)
986 for (i = 0; i < opts->prefix->size; i++)
987 strarray_add( spec_args, strmake( "-B%s", opts->prefix->base[i] ));
989 if (opts->use_msvcrt) strarray_add( spec_args, "-mno-cygwin" );
990 if (opts->unix_lib) strarray_add( spec_args, "-munix" );
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 if(opts->unicode_app)
1270 strarray_add(spec_args, "-municode");
1271 strarray_add(spec_args, "-D_REENTRANT");
1272 if (opts->pic && !is_pe) strarray_add(spec_args, "-fPIC");
1273 strarray_add(spec_args, opts->shared ? "--dll" : "--exe");
1274 if (fake_module)
1276 strarray_add(spec_args, "--fake-module");
1277 strarray_add(spec_args, "-o");
1278 strarray_add(spec_args, output_file);
1280 else
1282 strarray_add(spec_args, "-o");
1283 strarray_add(spec_args, spec_o_name);
1285 if (spec_file)
1287 strarray_add(spec_args, "-E");
1288 strarray_add(spec_args, spec_file);
1290 if (opts->win16_app) strarray_add(spec_args, "-m16");
1292 if (!opts->shared)
1294 strarray_add(spec_args, "-F");
1295 strarray_add(spec_args, output_name);
1296 strarray_add(spec_args, "--subsystem");
1297 strarray_add(spec_args, opts->gui_app ? "windows" : "console");
1298 if (opts->large_address_aware) strarray_add( spec_args, "--large-address-aware" );
1301 if (opts->target_platform == PLATFORM_WINDOWS) strarray_add(spec_args, "--safeseh");
1303 if (entry_point)
1305 strarray_add(spec_args, "--entry");
1306 strarray_add(spec_args, entry_point);
1309 if (opts->subsystem)
1311 strarray_add(spec_args, "--subsystem");
1312 strarray_add(spec_args, opts->subsystem);
1315 for ( j = 0; j < lib_dirs->size; j++ )
1316 strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
1318 for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
1319 strarray_add(spec_args, opts->winebuild_args->base[j]);
1321 if (!is_pe)
1323 for (j = 0; j < opts->delayimports->size; j++)
1324 strarray_add(spec_args, strmake("-d%s", opts->delayimports->base[j]));
1327 /* add resource files */
1328 for ( j = 0; j < files->size; j++ )
1329 if (files->base[j][1] == 'r') strarray_add(spec_args, files->base[j]);
1331 /* add other files */
1332 strarray_add(spec_args, "--");
1333 for ( j = 0; j < files->size; j++ )
1335 switch(files->base[j][1])
1337 case 'd':
1338 case 'a':
1339 case 'o':
1340 strarray_add(spec_args, files->base[j] + 2);
1341 break;
1345 spawn(opts->prefix, spec_args, 0);
1346 strarray_free (spec_args);
1347 if (fake_module) return; /* nothing else to do */
1349 /* link everything together now */
1350 link_args = get_link_args( opts, output_name );
1352 if ((opts->nodefaultlibs || opts->use_msvcrt) && opts->target_platform == PLATFORM_MINGW)
1354 libgcc = find_libgcc(opts->prefix, link_args);
1355 if (!libgcc) libgcc = "-lgcc";
1358 strarray_add(link_args, "-o");
1359 strarray_add(link_args, output_path);
1361 for ( j = 0; j < lib_dirs->size; j++ )
1362 strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
1364 if (is_pe && opts->use_msvcrt && !entry_point && (opts->shared || opts->win16_app))
1365 entry_point = opts->target_cpu == CPU_x86 ? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1367 if (is_pe && entry_point)
1369 if (opts->target_platform == PLATFORM_WINDOWS)
1370 strarray_add(link_args, strmake("-Wl,-entry:%s", entry_point));
1371 else
1372 strarray_add(link_args, strmake("-Wl,--entry,%s%s",
1373 is_pe && opts->target_cpu == CPU_x86 ? "_" : "",
1374 entry_point));
1377 strarray_add(link_args, spec_o_name);
1379 if (is_pe)
1381 for (j = 0; j < opts->delayimports->size; j++)
1383 if (opts->target_platform == PLATFORM_WINDOWS)
1384 strarray_add(link_args, strmake("-Wl,-delayload:%s", opts->delayimports->base[j]));
1385 else
1386 strarray_add(link_args, strmake("-Wl,-delayload,%s",opts->delayimports->base[j]));
1390 for ( j = 0; j < files->size; j++ )
1392 const char* name = files->base[j] + 2;
1393 switch(files->base[j][1])
1395 case 'l':
1396 strarray_add(link_args, strmake("-l%s", name));
1397 break;
1398 case 's':
1399 case 'o':
1400 strarray_add(link_args, name);
1401 break;
1402 case 'a':
1403 if (is_pe && !opts->lib_suffix && strchr(name, '/'))
1405 /* turn the path back into -Ldir -lfoo options
1406 * this makes sure that we use the specified libs even
1407 * when mingw adds its own import libs to the link */
1408 char *lib = xstrdup( name );
1409 char *p = strrchr( lib, '/' );
1411 *p++ = 0;
1412 if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
1414 char *ext = strrchr( p, '.' );
1416 if (ext) *ext = 0;
1417 p += 3;
1418 strarray_add(link_args, strmake("-L%s", lib ));
1419 strarray_add(link_args, strmake("-l%s", p ));
1420 free( lib );
1421 break;
1423 free( lib );
1425 strarray_add(link_args, name);
1426 break;
1427 case 'W':
1428 strarray_add(link_args, files->base[j]);
1429 break;
1433 if (!opts->nostdlib && !is_pe)
1435 strarray_add(link_args, "-lm");
1436 strarray_add(link_args, "-lc");
1439 if (libgcc) strarray_add(link_args, libgcc);
1441 output_file_name = output_path;
1442 output_debug_file = opts->debug_file;
1443 output_implib = opts->out_implib;
1444 atexit( cleanup_output_files );
1446 spawn(opts->prefix, link_args, 0);
1447 strarray_free (link_args);
1449 if (opts->debug_file && !strendswith(opts->debug_file, ".pdb"))
1451 strarray *tool, *objcopy = build_tool_name(opts, TOOL_OBJCOPY);
1453 tool = strarray_dup(objcopy);
1454 strarray_add(tool, "--only-keep-debug");
1455 strarray_add(tool, output_path);
1456 strarray_add(tool, opts->debug_file);
1457 spawn(opts->prefix, tool, 1);
1458 strarray_free(tool);
1460 tool = strarray_dup(objcopy);
1461 strarray_add(tool, "--strip-debug");
1462 strarray_add(tool, output_path);
1463 spawn(opts->prefix, tool, 1);
1464 strarray_free(tool);
1466 tool = objcopy;
1467 strarray_add(tool, "--add-gnu-debuglink");
1468 strarray_add(tool, opts->debug_file);
1469 strarray_add(tool, output_path);
1470 spawn(opts->prefix, tool, 0);
1471 strarray_free(tool);
1474 if (opts->out_implib && !is_pe)
1476 if (!spec_file)
1477 error("--out-implib requires a .spec or .def file\n");
1479 implib_args = get_winebuild_args( opts );
1480 if ((tool = build_tool_name( opts, TOOL_CC ))) strarray_add( implib_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
1481 if ((tool = build_tool_name( opts, TOOL_LD ))) strarray_add( implib_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
1483 strarray_add(implib_args, "--implib");
1484 strarray_add(implib_args, "-o");
1485 strarray_add(implib_args, opts->out_implib);
1486 strarray_add(implib_args, "--export");
1487 strarray_add(implib_args, spec_file);
1488 strarray_addall(implib_args, opts->winebuild_args);
1490 spawn(opts->prefix, implib_args, 0);
1491 strarray_free (implib_args);
1494 /* set the base address with prelink if linker support is not present */
1495 if (opts->prelink && !opts->target)
1497 if (opts->prelink[0] && strcmp(opts->prelink,"false"))
1499 strarray *prelink_args = strarray_alloc();
1500 strarray_add(prelink_args, opts->prelink);
1501 strarray_add(prelink_args, "--reloc-only");
1502 strarray_add(prelink_args, opts->image_base);
1503 strarray_add(prelink_args, output_path);
1504 spawn(opts->prefix, prelink_args, 1);
1505 strarray_free(prelink_args);
1509 if (!is_pe) fixup_constructors( opts, output_path );
1510 else if (opts->wine_builtin) make_wine_builtin( opts, output_path );
1512 /* create the loader script */
1513 if (generate_app_loader)
1514 create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
1518 static void forward( struct options *opts )
1520 strarray* args = strarray_alloc();
1522 strarray_addall(args, get_translator(opts));
1523 strarray_addall(args, opts->compiler_args);
1524 strarray_addall(args, opts->linker_args);
1525 spawn(opts->prefix, args, 0);
1526 strarray_free (args);
1529 static int is_linker_arg(const char* arg)
1531 static const char* link_switches[] =
1533 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1534 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1535 "-fprofile-generate", "-fprofile-use"
1537 unsigned int j;
1539 switch (arg[1])
1541 case 'R':
1542 case 'z':
1543 case 'u':
1544 return 1;
1545 case 'W':
1546 if (strncmp("-Wl,", arg, 4) == 0) return 1;
1547 break;
1548 case 'X':
1549 if (strcmp("-Xlinker", arg) == 0) return 1;
1550 break;
1551 case 'a':
1552 if (strcmp("-arch", arg) == 0) return 1;
1553 break;
1554 case 'f':
1555 if (strncmp("-fuse-ld=", arg, 9) == 0) return 1;
1556 break;
1557 case 'r':
1558 if (strncmp("-rtlib=", arg, 7) == 0) return 1;
1559 break;
1562 for (j = 0; j < ARRAY_SIZE(link_switches); j++)
1563 if (strcmp(link_switches[j], arg) == 0) return 1;
1565 return 0;
1568 static void parse_target_option( struct options *opts, const char *target )
1570 char *p, *spec = xstrdup( target );
1571 unsigned int i;
1573 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
1575 /* get the CPU part */
1577 if ((p = strchr( spec, '-' )))
1579 *p++ = 0;
1580 for (i = 0; i < ARRAY_SIZE(cpu_names); i++)
1582 if (!strcmp( cpu_names[i].name, spec ))
1584 opts->target_cpu = cpu_names[i].cpu;
1585 break;
1588 if (i == ARRAY_SIZE(cpu_names))
1589 error( "Unrecognized CPU '%s'\n", spec );
1591 else if (!strcmp( spec, "mingw32" ))
1593 opts->target_cpu = CPU_x86;
1594 p = spec;
1596 else
1597 error( "Invalid target specification '%s'\n", target );
1599 /* get the OS part */
1601 opts->target_platform = PLATFORM_UNSPECIFIED; /* default value */
1602 for (;;)
1604 for (i = 0; i < ARRAY_SIZE(platform_names); i++)
1606 if (!strncmp( platform_names[i].name, p, strlen(platform_names[i].name) ))
1608 opts->target_platform = platform_names[i].platform;
1609 break;
1612 if (opts->target_platform != PLATFORM_UNSPECIFIED || !(p = strchr( p, '-' ))) break;
1613 p++;
1616 free( spec );
1617 opts->target = xstrdup( target );
1620 static int is_option( struct options *opts, int i, const char *option, const char **option_arg )
1622 if (!strcmp( opts->args->base[i], option ))
1624 if (opts->args->size == i) error( "option %s requires an argument\n", opts->args->base[i] );
1625 *option_arg = opts->args->base[i + 1];
1626 return 1;
1628 if (!strncmp( opts->args->base[i], option, strlen(option) ) && opts->args->base[i][strlen(option)] == '=')
1630 *option_arg = opts->args->base[i] + strlen(option) + 1;
1631 return 1;
1633 return 0;
1636 int main(int argc, char **argv)
1638 int i, c, next_is_arg = 0, linking = 1;
1639 int raw_compiler_arg, raw_linker_arg;
1640 const char* option_arg;
1641 struct options opts;
1642 char* lang = 0;
1643 char* str;
1645 #ifdef SIGHUP
1646 signal( SIGHUP, exit_on_signal );
1647 #endif
1648 signal( SIGTERM, exit_on_signal );
1649 signal( SIGINT, exit_on_signal );
1650 #ifdef HAVE_SIGADDSET
1651 sigemptyset( &signal_mask );
1652 sigaddset( &signal_mask, SIGHUP );
1653 sigaddset( &signal_mask, SIGTERM );
1654 sigaddset( &signal_mask, SIGINT );
1655 #endif
1656 init_argv0_dir( argv[0] );
1658 /* setup tmp file removal at exit */
1659 tmp_files = strarray_alloc();
1660 atexit(clean_temp_files);
1662 /* initialize options */
1663 memset(&opts, 0, sizeof(opts));
1664 opts.target_cpu = build_cpu;
1665 opts.target_platform = build_platform;
1666 opts.lib_dirs = strarray_alloc();
1667 opts.files = strarray_alloc();
1668 opts.linker_args = strarray_alloc();
1669 opts.compiler_args = strarray_alloc();
1670 opts.winebuild_args = strarray_alloc();
1671 opts.delayimports = strarray_alloc();
1672 opts.args = strarray_alloc();
1673 opts.pic = 1;
1675 /* determine the processor type */
1676 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
1677 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
1679 for (i = 1; i < argc; i++)
1681 char *input_buffer = NULL, *iter, *opt, *out;
1682 struct stat st;
1683 int fd;
1685 if (argv[i][0] != '@' || (fd = open( argv[i] + 1, O_RDONLY | O_BINARY )) == -1)
1687 strarray_add( opts.args, argv[i] );
1688 continue;
1690 if ((fstat( fd, &st ) == -1)) error( "Cannot stat %s", argv[i] + 1 );
1691 if (st.st_size)
1693 input_buffer = xmalloc( st.st_size + 1 );
1694 if (read( fd, input_buffer, st.st_size ) != st.st_size) error( "Cannot read %s\n", argv[i] + 1 );
1696 close( fd );
1697 for (iter = input_buffer; iter < input_buffer + st.st_size; iter++)
1699 char quote = 0;
1700 while (iter < input_buffer + st.st_size && isspace(*iter)) iter++;
1701 if (iter == input_buffer + st.st_size) break;
1702 opt = out = iter;
1703 while (iter < input_buffer + st.st_size && (quote || !isspace(*iter)))
1705 if (*iter == quote)
1707 iter++;
1708 quote = 0;
1710 else if (*iter == '\'' || *iter == '"') quote = *iter++;
1711 else
1713 if (*iter == '\\' && iter + 1 < input_buffer + st.st_size) iter++;
1714 *out++ = *iter++;
1717 *out = 0;
1718 strarray_add( opts.args, opt );
1722 /* parse options */
1723 for (i = 0; i < opts.args->size; i++)
1725 if (opts.args->base[i][0] == '-' && opts.args->base[i][1]) /* option, except '-' alone is stdin, which is a file */
1727 /* determine if this switch is followed by a separate argument */
1728 next_is_arg = 0;
1729 option_arg = 0;
1730 switch(opts.args->base[i][1])
1732 case 'x': case 'o': case 'D': case 'U':
1733 case 'I': case 'A': case 'l': case 'u':
1734 case 'b': case 'V': case 'G': case 'L':
1735 case 'B': case 'R': case 'z':
1736 if (opts.args->base[i][2]) option_arg = &opts.args->base[i][2];
1737 else next_is_arg = 1;
1738 break;
1739 case 'i':
1740 next_is_arg = 1;
1741 break;
1742 case 'a':
1743 if (strcmp("-aux-info", opts.args->base[i]) == 0)
1744 next_is_arg = 1;
1745 if (strcmp("-arch", opts.args->base[i]) == 0)
1746 next_is_arg = 1;
1747 break;
1748 case 'X':
1749 if (strcmp("-Xlinker", opts.args->base[i]) == 0)
1750 next_is_arg = 1;
1751 break;
1752 case 'M':
1753 c = opts.args->base[i][2];
1754 if (c == 'F' || c == 'T' || c == 'Q')
1756 if (opts.args->base[i][3]) option_arg = &opts.args->base[i][3];
1757 else next_is_arg = 1;
1759 break;
1760 case 'f':
1761 if (strcmp("-framework", opts.args->base[i]) == 0)
1762 next_is_arg = 1;
1763 break;
1764 case 't':
1765 next_is_arg = strcmp("-target", opts.args->base[i]) == 0;
1766 break;
1767 case '-':
1768 next_is_arg = (strcmp("--param", opts.args->base[i]) == 0 ||
1769 strcmp("--sysroot", opts.args->base[i]) == 0 ||
1770 strcmp("--target", opts.args->base[i]) == 0 ||
1771 strcmp("--wine-objdir", opts.args->base[i]) == 0 ||
1772 strcmp("--winebuild", opts.args->base[i]) == 0 ||
1773 strcmp("--lib-suffix", opts.args->base[i]) == 0);
1774 break;
1776 if (next_is_arg)
1778 if (i + 1 >= opts.args->size) error("option -%c requires an argument\n", opts.args->base[i][1]);
1779 option_arg = opts.args->base[i+1];
1782 /* determine what options go 'as is' to the linker & the compiler */
1783 raw_linker_arg = is_linker_arg(opts.args->base[i]);
1784 raw_compiler_arg = !raw_linker_arg;
1786 /* do a bit of semantic analysis */
1787 switch (opts.args->base[i][1])
1789 case 'B':
1790 str = strdup(option_arg);
1791 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
1792 if (!opts.prefix) opts.prefix = strarray_alloc();
1793 strarray_add(opts.prefix, str);
1794 raw_linker_arg = 1;
1795 break;
1796 case 'b':
1797 parse_target_option( &opts, option_arg );
1798 raw_compiler_arg = 0;
1799 break;
1800 case 'V':
1801 opts.version = xstrdup( option_arg );
1802 raw_compiler_arg = 0;
1803 break;
1804 case 'c': /* compile or assemble */
1805 raw_compiler_arg = 0;
1806 if (opts.args->base[i][2] == 0) opts.compile_only = 1;
1807 /* fall through */
1808 case 'S': /* generate assembler code */
1809 case 'E': /* preprocess only */
1810 if (opts.args->base[i][2] == 0) linking = 0;
1811 break;
1812 case 'f':
1813 if (strcmp("-fno-short-wchar", opts.args->base[i]) == 0)
1814 opts.noshortwchar = 1;
1815 else if (!strcmp("-fasynchronous-unwind-tables", opts.args->base[i]))
1816 opts.unwind_tables = 1;
1817 else if (!strcmp("-fno-asynchronous-unwind-tables", opts.args->base[i]))
1818 opts.unwind_tables = 0;
1819 else if (!strcmp("-fPIC", opts.args->base[i]) || !strcmp("-fpic", opts.args->base[i]))
1820 opts.pic = 1;
1821 else if (!strcmp("-fno-PIC", opts.args->base[i]) || !strcmp("-fno-pic", opts.args->base[i]))
1822 opts.pic = 0;
1823 break;
1824 case 'i':
1825 if (!strcmp( "-isysroot", opts.args->base[i] )) opts.isysroot = opts.args->base[i + 1];
1826 break;
1827 case 'l':
1828 strarray_add(opts.files, strmake("-l%s", option_arg));
1829 raw_compiler_arg = 0;
1830 break;
1831 case 'L':
1832 strarray_add(opts.lib_dirs, option_arg);
1833 raw_compiler_arg = 0;
1834 break;
1835 case 'M': /* map file generation */
1836 linking = 0;
1837 break;
1838 case 'm':
1839 if (strcmp("-mno-cygwin", opts.args->base[i]) == 0)
1841 opts.use_msvcrt = 1;
1842 raw_compiler_arg = 0;
1843 strarray_add( opts.winebuild_args, opts.args->base[i] );
1845 else if (strcmp("-mwindows", opts.args->base[i]) == 0)
1847 opts.gui_app = 1;
1848 raw_compiler_arg = 0;
1850 else if (strcmp("-mconsole", opts.args->base[i]) == 0)
1852 opts.gui_app = 0;
1853 raw_compiler_arg = 0;
1855 else if (strcmp("-municode", opts.args->base[i]) == 0)
1857 opts.unicode_app = 1;
1858 raw_compiler_arg = 0;
1860 else if (strcmp("-mthreads", opts.args->base[i]) == 0)
1862 raw_compiler_arg = 0;
1864 else if (strcmp("-munix", opts.args->base[i]) == 0)
1866 opts.unix_lib = 1;
1867 raw_compiler_arg = 0;
1869 else if (strcmp("-m16", opts.args->base[i]) == 0)
1871 opts.win16_app = 1;
1872 raw_compiler_arg = 0;
1874 else if (strcmp("-m32", opts.args->base[i]) == 0)
1876 if (opts.target_cpu == CPU_x86_64)
1877 opts.target_cpu = CPU_x86;
1878 else if (opts.target_cpu == CPU_ARM64)
1879 opts.target_cpu = CPU_ARM;
1880 opts.force_pointer_size = 4;
1881 raw_linker_arg = 1;
1883 else if (strcmp("-m64", opts.args->base[i]) == 0)
1885 if (opts.target_cpu == CPU_x86)
1886 opts.target_cpu = CPU_x86_64;
1887 else if (opts.target_cpu == CPU_ARM)
1888 opts.target_cpu = CPU_ARM64;
1889 opts.force_pointer_size = 8;
1890 raw_linker_arg = 1;
1892 else if (!strcmp("-marm", opts.args->base[i] ) || !strcmp("-mthumb", opts.args->base[i] ))
1894 strarray_add(opts.winebuild_args, opts.args->base[i]);
1895 raw_linker_arg = 1;
1897 else if (!strncmp("-mcpu=", opts.args->base[i], 6) ||
1898 !strncmp("-mfpu=", opts.args->base[i], 6) ||
1899 !strncmp("-march=", opts.args->base[i], 7) ||
1900 !strncmp("-mfloat-abi=", opts.args->base[i], 12))
1901 strarray_add(opts.winebuild_args, opts.args->base[i]);
1902 break;
1903 case 'n':
1904 if (strcmp("-nostdinc", opts.args->base[i]) == 0)
1905 opts.nostdinc = 1;
1906 else if (strcmp("-nodefaultlibs", opts.args->base[i]) == 0)
1907 opts.nodefaultlibs = 1;
1908 else if (strcmp("-nostdlib", opts.args->base[i]) == 0)
1909 opts.nostdlib = 1;
1910 else if (strcmp("-nostartfiles", opts.args->base[i]) == 0)
1911 opts.nostartfiles = 1;
1912 break;
1913 case 'o':
1914 opts.output_name = option_arg;
1915 raw_compiler_arg = 0;
1916 break;
1917 case 's':
1918 if (strcmp("-static", opts.args->base[i]) == 0)
1919 linking = -1;
1920 else if(strcmp("-save-temps", opts.args->base[i]) == 0)
1921 keep_generated = 1;
1922 else if (strncmp("-specs=", opts.args->base[i], 7) == 0)
1923 raw_linker_arg = 1;
1924 else if(strcmp("-shared", opts.args->base[i]) == 0)
1926 opts.shared = 1;
1927 raw_compiler_arg = raw_linker_arg = 0;
1929 else if (strcmp("-s", opts.args->base[i]) == 0 && opts.target_platform == PLATFORM_APPLE)
1931 /* On Mac, change -s into -Wl,-x. ld's -s switch
1932 * is deprecated, and it doesn't work on Tiger with
1933 * MH_BUNDLEs anyway
1935 opts.strip = 1;
1936 raw_linker_arg = 0;
1938 break;
1939 case 't':
1940 if (is_option( &opts, i, "-target", &option_arg ))
1942 parse_target_option( &opts, option_arg );
1943 raw_compiler_arg = raw_linker_arg = 0;
1945 break;
1946 case 'v':
1947 if (opts.args->base[i][2] == 0) verbose++;
1948 break;
1949 case 'W':
1950 if (strncmp("-Wl,", opts.args->base[i], 4) == 0)
1952 unsigned int j;
1953 strarray* Wl = strarray_fromstring(opts.args->base[i] + 4, ",");
1954 for (j = 0; j < Wl->size; j++)
1956 if (!strcmp(Wl->base[j], "--image-base") && j < Wl->size - 1)
1958 opts.image_base = strdup( Wl->base[++j] );
1959 continue;
1961 if (!strcmp(Wl->base[j], "--section-alignment") && j < Wl->size - 1)
1963 opts.section_align = strdup( Wl->base[++j] );
1964 continue;
1966 if (!strcmp(Wl->base[j], "--file-alignment") && j < Wl->size - 1)
1968 opts.file_align = strdup( Wl->base[++j] );
1969 continue;
1971 if (!strcmp(Wl->base[j], "--large-address-aware"))
1973 opts.large_address_aware = 1;
1974 continue;
1976 if (!strcmp(Wl->base[j], "--wine-builtin"))
1978 opts.wine_builtin = 1;
1979 continue;
1981 if (!strcmp(Wl->base[j], "--subsystem") && j < Wl->size - 1)
1983 opts.subsystem = strdup( Wl->base[++j] );
1984 continue;
1986 if (!strcmp(Wl->base[j], "--entry") && j < Wl->size - 1)
1988 opts.entry_point = strdup( Wl->base[++j] );
1989 continue;
1991 if (!strcmp(Wl->base[j], "-delayload") && j < Wl->size - 1)
1993 strarray_add( opts.delayimports, Wl->base[++j] );
1994 continue;
1996 if (!strcmp(Wl->base[j], "--debug-file") && j < Wl->size - 1)
1998 opts.debug_file = strdup( Wl->base[++j] );
1999 continue;
2001 if (!strcmp(Wl->base[j], "--whole-archive") || !strcmp(Wl->base[j], "--no-whole-archive"))
2003 strarray_add( opts.files, strmake( "-Wl,%s", Wl->base[j] ));
2004 continue;
2006 if (!strcmp(Wl->base[j], "--out-implib"))
2008 opts.out_implib = strdup( Wl->base[++j] );
2009 continue;
2011 if (!strcmp(Wl->base[j], "-static")) linking = -1;
2012 strarray_add(opts.linker_args, strmake("-Wl,%s",Wl->base[j]));
2014 strarray_free(Wl);
2015 raw_compiler_arg = raw_linker_arg = 0;
2017 else if (strncmp("-Wb,", opts.args->base[i], 4) == 0)
2019 strarray* Wb = strarray_fromstring(opts.args->base[i] + 4, ",");
2020 strarray_addall(opts.winebuild_args, Wb);
2021 strarray_free(Wb);
2022 /* don't pass it to the compiler, it generates errors */
2023 raw_compiler_arg = raw_linker_arg = 0;
2025 break;
2026 case 'x':
2027 lang = strmake("-x%s", option_arg);
2028 strarray_add(opts.files, lang);
2029 /* we'll pass these flags ourselves, explicitly */
2030 raw_compiler_arg = raw_linker_arg = 0;
2031 break;
2032 case '-':
2033 if (strcmp("-static", opts.args->base[i]+1) == 0)
2034 linking = -1;
2035 else if (is_option( &opts, i, "--sysroot", &option_arg ))
2037 opts.sysroot = option_arg;
2038 raw_linker_arg = 1;
2040 else if (is_option( &opts, i, "--target", &option_arg ))
2042 parse_target_option( &opts, option_arg );
2043 raw_compiler_arg = raw_linker_arg = 0;
2045 else if (is_option( &opts, i, "--wine-objdir", &option_arg ))
2047 opts.wine_objdir = option_arg;
2048 raw_compiler_arg = raw_linker_arg = 0;
2050 else if (is_option( &opts, i, "--winebuild", &option_arg ))
2052 opts.winebuild = option_arg;
2053 raw_compiler_arg = raw_linker_arg = 0;
2055 else if (is_option( &opts, i, "--lib-suffix", &option_arg ))
2057 opts.lib_suffix = option_arg;
2058 raw_compiler_arg = raw_linker_arg = 0;
2060 break;
2063 /* put the arg into the appropriate bucket */
2064 if (raw_linker_arg)
2066 strarray_add( opts.linker_args, opts.args->base[i] );
2067 if (next_is_arg && (i + 1 < opts.args->size))
2068 strarray_add( opts.linker_args, opts.args->base[i + 1] );
2070 if (raw_compiler_arg)
2072 strarray_add( opts.compiler_args, opts.args->base[i] );
2073 if (next_is_arg && (i + 1 < opts.args->size))
2074 strarray_add( opts.compiler_args, opts.args->base[i + 1] );
2077 /* skip the next token if it's an argument */
2078 if (next_is_arg) i++;
2080 else
2082 strarray_add( opts.files, opts.args->base[i] );
2086 if (opts.processor == proc_cpp) linking = 0;
2087 if (linking == -1) error("Static linking is not supported\n");
2089 if (opts.files->size == 0) forward(&opts);
2090 else if (linking) build(&opts);
2091 else compile(&opts, lang);
2093 output_file_name = NULL;
2094 output_debug_file = NULL;
2095 output_implib = NULL;
2096 return 0;