ntoskrnl.exe: Return STATUS_INVALID_DEVICE_REQUEST when calling unhandled driver...
[wine.git] / tools / winegcc / winegcc.c
blob514a57469c335e48ed8415c01179e1721017855c
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 -shared -shared-libgcc
75 * -symbolic -Wl,option -Xlinker option -u symbol --image-base
77 * Directory Options
78 * -Bprefix -Idir -I- -Ldir -specs=file
80 * Target Options
81 * -b machine -V version
83 * Please note that the Target Options are relevant to everything:
84 * compiler, linker, assembler, preprocessor.
86 */
88 #include "config.h"
89 #include "wine/port.h"
91 #include <assert.h>
92 #include <stdio.h>
93 #include <stdlib.h>
94 #include <signal.h>
95 #include <stdarg.h>
96 #include <string.h>
97 #include <errno.h>
99 #include "utils.h"
101 static const char* app_loader_template =
102 "#!/bin/sh\n"
103 "\n"
104 "appname=\"%s\"\n"
105 "# determine the application directory\n"
106 "appdir=''\n"
107 "case \"$0\" in\n"
108 " */*)\n"
109 " # $0 contains a path, use it\n"
110 " appdir=`dirname \"$0\"`\n"
111 " ;;\n"
112 " *)\n"
113 " # no directory in $0, search in PATH\n"
114 " saved_ifs=$IFS\n"
115 " IFS=:\n"
116 " for d in $PATH\n"
117 " do\n"
118 " IFS=$saved_ifs\n"
119 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
120 " done\n"
121 " ;;\n"
122 "esac\n"
123 "\n"
124 "# figure out the full app path\n"
125 "if [ -n \"$appdir\" ]; then\n"
126 " apppath=\"$appdir/$appname\"\n"
127 " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
128 " export WINEDLLPATH\n"
129 "else\n"
130 " apppath=\"$appname\"\n"
131 "fi\n"
132 "\n"
133 "# determine the WINELOADER\n"
134 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
135 "\n"
136 "# and try to start the app\n"
137 "exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
140 static int keep_generated = 0;
141 static strarray* tmp_files;
142 #ifdef HAVE_SIGSET_T
143 static sigset_t signal_mask;
144 #endif
146 enum processor { proc_cc, proc_cxx, proc_cpp, proc_as };
148 static const struct
150 const char *name;
151 enum target_cpu cpu;
152 } cpu_names[] =
154 { "i386", CPU_x86 },
155 { "i486", CPU_x86 },
156 { "i586", CPU_x86 },
157 { "i686", CPU_x86 },
158 { "i786", CPU_x86 },
159 { "amd64", CPU_x86_64 },
160 { "x86_64", CPU_x86_64 },
161 { "powerpc", CPU_POWERPC },
162 { "arm", CPU_ARM },
163 { "armv5", CPU_ARM },
164 { "armv6", CPU_ARM },
165 { "armv7", CPU_ARM },
166 { "arm64", CPU_ARM64 },
167 { "aarch64", CPU_ARM64 },
170 static const struct
172 const char *name;
173 enum target_platform platform;
174 } platform_names[] =
176 { "macos", PLATFORM_APPLE },
177 { "darwin", PLATFORM_APPLE },
178 { "android", PLATFORM_ANDROID },
179 { "solaris", PLATFORM_SOLARIS },
180 { "cygwin", PLATFORM_CYGWIN },
181 { "mingw32", PLATFORM_WINDOWS },
182 { "windows", PLATFORM_WINDOWS },
183 { "winnt", PLATFORM_WINDOWS }
186 struct options
188 enum processor processor;
189 enum target_cpu target_cpu;
190 enum target_platform target_platform;
191 const char *target;
192 const char *version;
193 int shared;
194 int use_msvcrt;
195 int nostdinc;
196 int nostdlib;
197 int nostartfiles;
198 int nodefaultlibs;
199 int noshortwchar;
200 int gui_app;
201 int unicode_app;
202 int win16_app;
203 int compile_only;
204 int force_pointer_size;
205 int large_address_aware;
206 int unwind_tables;
207 int strip;
208 const char* wine_objdir;
209 const char* output_name;
210 const char* image_base;
211 const char* section_align;
212 const char* lib_suffix;
213 strarray* prefix;
214 strarray* lib_dirs;
215 strarray* linker_args;
216 strarray* compiler_args;
217 strarray* winebuild_args;
218 strarray* files;
221 #ifdef __i386__
222 static const enum target_cpu build_cpu = CPU_x86;
223 #elif defined(__x86_64__)
224 static const enum target_cpu build_cpu = CPU_x86_64;
225 #elif defined(__powerpc__)
226 static const enum target_cpu build_cpu = CPU_POWERPC;
227 #elif defined(__arm__)
228 static const enum target_cpu build_cpu = CPU_ARM;
229 #elif defined(__aarch64__)
230 static const enum target_cpu build_cpu = CPU_ARM64;
231 #else
232 #error Unsupported CPU
233 #endif
235 #ifdef __APPLE__
236 static enum target_platform build_platform = PLATFORM_APPLE;
237 #elif defined(__ANDROID__)
238 static enum target_platform build_platform = PLATFORM_ANDROID;
239 #elif defined(__sun)
240 static enum target_platform build_platform = PLATFORM_SOLARIS;
241 #elif defined(__CYGWIN__)
242 static enum target_platform build_platform = PLATFORM_CYGWIN;
243 #elif defined(_WIN32)
244 static enum target_platform build_platform = PLATFORM_WINDOWS;
245 #else
246 static enum target_platform build_platform = PLATFORM_UNSPECIFIED;
247 #endif
249 static void clean_temp_files(void)
251 unsigned int i;
253 if (keep_generated) return;
255 for (i = 0; i < tmp_files->size; i++)
256 unlink(tmp_files->base[i]);
259 /* clean things up when aborting on a signal */
260 static void exit_on_signal( int sig )
262 exit(1); /* this will call the atexit functions */
265 static char* get_temp_file(const char* prefix, const char* suffix)
267 int fd;
268 char* tmp = strmake("%s-XXXXXX%s", prefix, suffix);
270 #ifdef HAVE_SIGPROCMASK
271 sigset_t old_set;
272 /* block signals while manipulating the temp files list */
273 sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
274 #endif
275 fd = mkstemps( tmp, strlen(suffix) );
276 if (fd == -1)
278 /* could not create it in current directory, try in TMPDIR */
279 const char* tmpdir;
281 free(tmp);
282 if (!(tmpdir = getenv("TMPDIR"))) tmpdir = "/tmp";
283 tmp = strmake("%s/%s-XXXXXX%s", tmpdir, prefix, suffix);
284 fd = mkstemps( tmp, strlen(suffix) );
285 if (fd == -1) error( "could not create temp file\n" );
287 close( fd );
288 strarray_add(tmp_files, tmp);
289 #ifdef HAVE_SIGPROCMASK
290 sigprocmask( SIG_SETMASK, &old_set, NULL );
291 #endif
292 return tmp;
295 static char* build_tool_name(struct options *opts, const char* base, const char* deflt)
297 char* str;
299 if (opts->target && opts->version)
301 str = strmake("%s-%s-%s", opts->target, base, opts->version);
303 else if (opts->target)
305 str = strmake("%s-%s", opts->target, base);
307 else if (opts->version)
309 str = strmake("%s-%s", base, opts->version);
311 else
312 str = xstrdup(deflt);
313 return str;
316 static const strarray* get_translator(struct options *opts)
318 char *str = NULL;
319 strarray *ret;
321 switch(opts->processor)
323 case proc_cpp:
324 str = build_tool_name(opts, "cpp", CPP);
325 break;
326 case proc_cc:
327 case proc_as:
328 str = build_tool_name(opts, "gcc", CC);
329 break;
330 case proc_cxx:
331 str = build_tool_name(opts, "g++", CXX);
332 break;
333 default:
334 assert(0);
336 ret = strarray_fromstring( str, " " );
337 free(str);
338 if (opts->force_pointer_size)
339 strarray_add( ret, strmake("-m%u", 8 * opts->force_pointer_size ));
340 return ret;
343 static int try_link( const strarray *prefix, const strarray *link_tool, const char *cflags )
345 const char *in = get_temp_file( "try_link", ".c" );
346 const char *out = get_temp_file( "try_link", ".out" );
347 const char *err = get_temp_file( "try_link", ".err" );
348 strarray *link = strarray_dup( link_tool );
349 int sout = -1, serr = -1;
350 int ret;
352 create_file( in, 0644, "int main(void){return 1;}\n" );
354 strarray_add( link, "-o" );
355 strarray_add( link, out );
356 strarray_addall( link, strarray_fromstring( cflags, " " ) );
357 strarray_add( link, in );
359 sout = dup( fileno(stdout) );
360 freopen( err, "w", stdout );
361 serr = dup( fileno(stderr) );
362 freopen( err, "w", stderr );
363 ret = spawn( prefix, link, 1 );
364 if (sout >= 0)
366 dup2( sout, fileno(stdout) );
367 close( sout );
369 if (serr >= 0)
371 dup2( serr, fileno(stderr) );
372 close( serr );
374 strarray_free( link );
375 return ret;
378 static const strarray* get_lddllflags( const struct options *opts, const strarray *link_tool )
380 strarray *flags = strarray_alloc();
381 switch (opts->target_platform)
383 case PLATFORM_APPLE:
384 strarray_add( flags, "-bundle" );
385 strarray_add( flags, "-multiply_defined" );
386 strarray_add( flags, "suppress" );
387 if (opts->target_cpu == CPU_POWERPC)
389 strarray_add( flags, "-read_only_relocs" );
390 strarray_add( flags, "warning" );
392 break;
394 case PLATFORM_ANDROID:
395 case PLATFORM_SOLARIS:
396 case PLATFORM_UNSPECIFIED:
397 strarray_add( flags, "-shared" );
398 strarray_add( flags, "-Wl,-Bsymbolic" );
400 /* Try all options first - this is likely to succeed on modern compilers */
401 if (!try_link( opts->prefix, link_tool, "-fPIC -shared -Wl,-Bsymbolic "
402 "-Wl,-z,defs -Wl,-init,__wine_spec_init,-fini,_wine_spec_fini" ))
404 strarray_add( flags, "-Wl,-z,defs" );
405 strarray_add( flags, "-Wl,-init,__wine_spec_init,-fini,__wine_spec_fini" );
407 else /* otherwise figure out which ones are allowed */
409 if (!try_link( opts->prefix, link_tool, "-fPIC -shared -Wl,-Bsymbolic -Wl,-z,defs" ))
410 strarray_add( flags, "-Wl,-z,defs" );
411 if (!try_link( opts->prefix, link_tool, "-fPIC -shared -Wl,-Bsymbolic "
412 "-Wl,-init,__wine_spec_init,-fini,_wine_spec_fini" ))
413 strarray_add( flags, "-Wl,-init,__wine_spec_init,-fini,__wine_spec_fini" );
415 break;
417 default:
418 assert(0);
420 return flags;
423 /* check that file is a library for the correct platform */
424 static int check_platform( struct options *opts, const char *file )
426 int ret = 0, fd = open( file, O_RDONLY );
427 if (fd != -1)
429 unsigned char header[16];
430 if (read( fd, header, sizeof(header) ) == sizeof(header))
432 /* FIXME: only ELF is supported, platform is not checked beyond 32/64 */
433 if (!memcmp( header, "\177ELF", 4 ))
435 if (header[4] == 2) /* 64-bit */
436 ret = (opts->target_cpu == CPU_x86_64 || opts->target_cpu == CPU_ARM64);
437 else
438 ret = (opts->target_cpu != CPU_x86_64 && opts->target_cpu != CPU_ARM64);
441 close( fd );
443 return ret;
446 static char *get_lib_dir( struct options *opts )
448 static const char *stdlibpath[] = { LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
449 static const char libwine[] = "/libwine.so";
450 unsigned int i;
452 for (i = 0; i < sizeof(stdlibpath)/sizeof(stdlibpath[0]); i++)
454 char *p, *buffer = xmalloc( strlen(stdlibpath[i]) + strlen("/arm-linux-gnueabi") + strlen(libwine) + 1 );
455 strcpy( buffer, stdlibpath[i] );
456 p = buffer + strlen(buffer);
457 while (p > buffer && p[-1] == '/') p--;
458 strcpy( p, libwine );
459 if (check_platform( opts, buffer )) goto found;
460 if (p > buffer + 2 && (!memcmp( p - 2, "32", 2 ) || !memcmp( p - 2, "64", 2 ))) p -= 2;
461 if (opts->target_cpu != CPU_x86_64 && opts->target_cpu != CPU_ARM64)
463 strcpy( p, "32" );
464 strcat( p, libwine );
465 if (check_platform( opts, buffer )) goto found;
467 if (opts->target_cpu == CPU_x86_64 || opts->target_cpu == CPU_ARM64)
469 strcpy( p, "64" );
470 strcat( p, libwine );
471 if (check_platform( opts, buffer )) goto found;
473 switch(opts->target_cpu)
475 case CPU_x86: strcpy( p, "/i386-linux-gnu" ); break;
476 case CPU_x86_64: strcpy( p, "/x86_64-linux-gnu" ); break;
477 case CPU_ARM: strcpy( p, "/arm-linux-gnueabi" ); break;
478 case CPU_ARM64: strcpy( p, "/aarch64-linux-gnu" ); break;
479 case CPU_POWERPC: strcpy( p, "/powerpc-linux-gnu" ); break;
480 default:
481 assert(0);
483 strcat( p, libwine );
484 if (check_platform( opts, buffer )) goto found;
485 free( buffer );
486 continue;
488 found:
489 buffer[strlen(buffer) - strlen(libwine)] = 0;
490 return buffer;
492 return xstrdup( LIBDIR );
495 static void compile(struct options* opts, const char* lang)
497 strarray* comp_args = strarray_alloc();
498 unsigned int i, j;
499 int gcc_defs = 0;
500 strarray* gcc;
501 strarray* gpp;
503 strarray_addall(comp_args, get_translator(opts));
504 switch(opts->processor)
506 case proc_cpp: gcc_defs = 1; break;
507 case proc_as: gcc_defs = 0; break;
508 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
509 /* mixing different C and C++ compilers isn't supported in configure anyway */
510 case proc_cc:
511 case proc_cxx:
512 gcc = strarray_fromstring(build_tool_name(opts, "gcc", CC), " ");
513 gpp = strarray_fromstring(build_tool_name(opts, "g++", CXX), " ");
514 for ( j = 0; !gcc_defs && j < comp_args->size; j++ )
516 const char *cc = comp_args->base[j];
518 for (i = 0; !gcc_defs && i < gcc->size; i++)
519 gcc_defs = gcc->base[i][0] != '-' && strendswith(cc, gcc->base[i]);
520 for (i = 0; !gcc_defs && i < gpp->size; i++)
521 gcc_defs = gpp->base[i][0] != '-' && strendswith(cc, gpp->base[i]);
523 strarray_free(gcc);
524 strarray_free(gpp);
525 break;
528 if (opts->target_platform == PLATFORM_WINDOWS || opts->target_platform == PLATFORM_CYGWIN)
529 goto no_compat_defines;
531 if (opts->processor != proc_cpp)
533 if (gcc_defs && !opts->wine_objdir && !opts->noshortwchar)
535 strarray_add(comp_args, "-fshort-wchar");
536 strarray_add(comp_args, "-DWINE_UNICODE_NATIVE");
538 strarray_add(comp_args, "-D_REENTRANT");
539 strarray_add(comp_args, "-fPIC");
542 if (opts->target_cpu == CPU_x86_64 || opts->target_cpu == CPU_ARM64)
544 strarray_add(comp_args, "-DWIN64");
545 strarray_add(comp_args, "-D_WIN64");
546 strarray_add(comp_args, "-D__WIN64");
547 strarray_add(comp_args, "-D__WIN64__");
550 strarray_add(comp_args, "-DWIN32");
551 strarray_add(comp_args, "-D_WIN32");
552 strarray_add(comp_args, "-D__WIN32");
553 strarray_add(comp_args, "-D__WIN32__");
554 strarray_add(comp_args, "-D__WINNT");
555 strarray_add(comp_args, "-D__WINNT__");
557 if (gcc_defs)
559 switch (opts->target_cpu)
561 case CPU_x86_64:
562 strarray_add(comp_args, "-D__stdcall=__attribute__((ms_abi))");
563 strarray_add(comp_args, "-D__cdecl=__attribute__((ms_abi))");
564 strarray_add(comp_args, "-D_stdcall=__attribute__((ms_abi))");
565 strarray_add(comp_args, "-D_cdecl=__attribute__((ms_abi))");
566 strarray_add(comp_args, "-D__fastcall=__attribute__((ms_abi))");
567 strarray_add(comp_args, "-D_fastcall=__attribute__((ms_abi))");
568 break;
569 case CPU_x86:
570 strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
571 strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
572 strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
573 strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
574 strarray_add(comp_args, "-D__fastcall=__attribute__((__fastcall__))");
575 strarray_add(comp_args, "-D_fastcall=__attribute__((__fastcall__))");
576 break;
577 case CPU_ARM:
578 case CPU_ARM64:
579 case CPU_POWERPC:
580 strarray_add(comp_args, "-D__stdcall=");
581 strarray_add(comp_args, "-D__cdecl=");
582 strarray_add(comp_args, "-D_stdcall=");
583 strarray_add(comp_args, "-D_cdecl=");
584 strarray_add(comp_args, "-D__fastcall=");
585 strarray_add(comp_args, "-D_fastcall=");
586 break;
588 strarray_add(comp_args, "-D__declspec(x)=__declspec_##x");
589 strarray_add(comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
590 strarray_add(comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
591 strarray_add(comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
592 strarray_add(comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
593 strarray_add(comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
594 strarray_add(comp_args, "-D__declspec_naked=__attribute__((naked))");
595 strarray_add(comp_args, "-D__declspec_noinline=__attribute__((noinline))");
596 strarray_add(comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
597 strarray_add(comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
598 strarray_add(comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
599 strarray_add(comp_args, "-D__declspec_selectany=__attribute__((weak))");
600 strarray_add(comp_args, "-D__declspec_thread=__thread");
603 strarray_add(comp_args, "-D__int8=char");
604 strarray_add(comp_args, "-D__int16=short");
605 strarray_add(comp_args, "-D__int32=int");
606 if (opts->target_cpu == CPU_x86_64 || opts->target_cpu == CPU_ARM64)
607 strarray_add(comp_args, "-D__int64=long");
608 else
609 strarray_add(comp_args, "-D__int64=long long");
611 no_compat_defines:
612 strarray_add(comp_args, "-D__WINE__");
614 /* options we handle explicitly */
615 if (opts->compile_only)
616 strarray_add(comp_args, "-c");
617 if (opts->output_name)
619 strarray_add(comp_args, "-o");
620 strarray_add(comp_args, opts->output_name);
623 /* the rest of the pass-through parameters */
624 for ( j = 0 ; j < opts->compiler_args->size ; j++ )
625 strarray_add(comp_args, opts->compiler_args->base[j]);
627 /* the language option, if any */
628 if (lang && strcmp(lang, "-xnone"))
629 strarray_add(comp_args, lang);
631 /* last, but not least, the files */
632 for ( j = 0; j < opts->files->size; j++ )
634 if (opts->files->base[j][0] != '-')
635 strarray_add(comp_args, opts->files->base[j]);
638 /* standard includes come last in the include search path */
639 if (!opts->wine_objdir && !opts->nostdinc)
641 if (opts->use_msvcrt)
643 strarray_add(comp_args, gcc_defs ? "-isystem" INCLUDEDIR "/msvcrt" : "-I" INCLUDEDIR "/msvcrt" );
644 strarray_add(comp_args, "-D__MSVCRT__");
646 strarray_add(comp_args, gcc_defs ? "-isystem" INCLUDEDIR "/windows" : "-I" INCLUDEDIR "/windows" );
648 else if (opts->wine_objdir)
649 strarray_add(comp_args, strmake("-I%s/include", opts->wine_objdir) );
651 spawn(opts->prefix, comp_args, 0);
652 strarray_free(comp_args);
655 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
657 struct options copts;
658 char* base_name;
660 /* make a copy so we don't change any of the initial stuff */
661 /* a shallow copy is exactly what we want in this case */
662 base_name = get_basename(file);
663 copts = *opts;
664 copts.output_name = get_temp_file(base_name, ".o");
665 copts.compile_only = 1;
666 copts.files = strarray_alloc();
667 strarray_add(copts.files, file);
668 compile(&copts, lang);
669 strarray_free(copts.files);
670 free(base_name);
672 return copts.output_name;
675 /* return the initial set of options needed to run winebuild */
676 static strarray *get_winebuild_args(struct options *opts)
678 const char* winebuild = getenv("WINEBUILD");
679 strarray *spec_args = strarray_alloc();
681 if (!winebuild) winebuild = "winebuild";
682 strarray_add( spec_args, winebuild );
683 if (verbose) strarray_add( spec_args, "-v" );
684 if (keep_generated) strarray_add( spec_args, "--save-temps" );
685 if (opts->target)
687 strarray_add( spec_args, "--target" );
688 strarray_add( spec_args, opts->target );
690 if (opts->unwind_tables) strarray_add( spec_args, "-fasynchronous-unwind-tables" );
691 else strarray_add( spec_args, "-fno-asynchronous-unwind-tables" );
692 return spec_args;
695 static const char* compile_resources_to_object(struct options* opts, const strarray *resources,
696 const char *res_o_name)
698 strarray *winebuild_args = get_winebuild_args( opts );
700 strarray_add( winebuild_args, "--resources" );
701 strarray_add( winebuild_args, "-o" );
702 strarray_add( winebuild_args, res_o_name );
703 strarray_addall( winebuild_args, resources );
705 spawn( opts->prefix, winebuild_args, 0 );
706 strarray_free( winebuild_args );
707 return res_o_name;
710 /* check if there is a static lib associated to a given dll */
711 static char *find_static_lib( const char *dll )
713 char *lib = strmake("%s.a", dll);
714 if (get_file_type(lib) == file_arh) return lib;
715 free( lib );
716 return NULL;
719 /* add specified library to the list of files */
720 static void add_library( struct options *opts, strarray *lib_dirs, strarray *files, const char *library )
722 char *static_lib, *fullname = 0;
724 switch(get_lib_type(opts->target_platform, lib_dirs, library, opts->lib_suffix, &fullname))
726 case file_arh:
727 strarray_add(files, strmake("-a%s", fullname));
728 break;
729 case file_dll:
730 strarray_add(files, strmake("-d%s", fullname));
731 if ((static_lib = find_static_lib(fullname)))
733 strarray_add(files, strmake("-a%s",static_lib));
734 free(static_lib);
736 break;
737 case file_so:
738 default:
739 /* keep it anyway, the linker may know what to do with it */
740 strarray_add(files, strmake("-l%s", library));
741 break;
743 free(fullname);
746 /* hack a main or WinMain function to work around Mingw's lack of Unicode support */
747 static const char *mingw_unicode_hack( struct options *opts )
749 char *main_stub = get_temp_file( opts->output_name, ".c" );
751 create_file( main_stub, 0644,
752 "typedef unsigned short wchar_t;\n"
753 "extern void * __stdcall LoadLibraryA(const char *);\n"
754 "extern void * __stdcall GetProcAddress(void *,const char *);\n"
755 "extern int wmain( int argc, wchar_t *argv[] );\n\n"
756 "int main( int argc, char *argv[] )\n{\n"
757 " int wargc;\n"
758 " wchar_t **wargv, **wenv;\n"
759 " void *msvcrt = LoadLibraryA( \"msvcrt.dll\" );\n"
760 " void (*__wgetmainargs)(int *argc, wchar_t** *wargv, wchar_t** *wenvp, int expand_wildcards,\n"
761 " int *new_mode) = GetProcAddress( msvcrt, \"__wgetmainargs\" );\n"
762 " __wgetmainargs( &wargc, &wargv, &wenv, 0, 0 );\n"
763 " return wmain( wargc, wargv );\n}\n" );
764 return compile_to_object( opts, main_stub, NULL );
767 static void build(struct options* opts)
769 strarray *lib_dirs, *files;
770 strarray *spec_args, *link_args;
771 char *output_file;
772 const char *spec_o_name;
773 const char *output_name, *spec_file, *lang;
774 const char *prelink = NULL;
775 int generate_app_loader = 1;
776 int fake_module = 0;
777 unsigned int j;
779 /* NOTE: for the files array we'll use the following convention:
780 * -axxx: xxx is an archive (.a)
781 * -dxxx: xxx is a DLL (.def)
782 * -lxxx: xxx is an unsorted library
783 * -oxxx: xxx is an object (.o)
784 * -rxxx: xxx is a resource (.res)
785 * -sxxx: xxx is a shared lib (.so)
786 * -xlll: lll is the language (c, c++, etc.)
789 output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
791 /* 'winegcc -o app xxx.exe.so' only creates the load script */
792 if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so"))
794 create_file(output_file, 0755, app_loader_template, opts->files->base[0]);
795 return;
798 /* generate app loader only for .exe */
799 if (opts->shared || strendswith(output_file, ".so"))
800 generate_app_loader = 0;
802 if (strendswith(output_file, ".fake")) fake_module = 1;
804 /* normalize the filename a bit: strip .so, ensure it has proper ext */
805 if (strendswith(output_file, ".so"))
806 output_file[strlen(output_file) - 3] = 0;
807 if ((output_name = strrchr(output_file, '/'))) output_name++;
808 else output_name = output_file;
809 if (!strchr(output_name, '.'))
810 output_file = strmake("%s.%s", output_file, opts->shared ? "dll" : "exe");
812 /* get the filename from the path */
813 if ((output_name = strrchr(output_file, '/'))) output_name++;
814 else output_name = output_file;
816 /* prepare the linking path */
817 if (!opts->wine_objdir)
819 char *lib_dir = get_lib_dir( opts );
820 lib_dirs = strarray_dup(opts->lib_dirs);
821 strarray_add( lib_dirs, strmake( "%s/wine", lib_dir ));
822 strarray_add( lib_dirs, lib_dir );
824 else
826 lib_dirs = strarray_alloc();
827 strarray_add(lib_dirs, strmake("%s/dlls", opts->wine_objdir));
828 strarray_add(lib_dirs, strmake("%s/libs/wine", opts->wine_objdir));
829 strarray_addall(lib_dirs, opts->lib_dirs);
832 /* mark the files with their appropriate type */
833 spec_file = lang = 0;
834 files = strarray_alloc();
835 link_args = strarray_alloc();
836 for ( j = 0; j < opts->files->size; j++ )
838 const char* file = opts->files->base[j];
839 if (file[0] != '-')
841 switch(get_file_type(file))
843 case file_def:
844 case file_spec:
845 if (spec_file)
846 error("Only one spec file can be specified\n");
847 spec_file = file;
848 break;
849 case file_rc:
850 /* FIXME: invoke wrc to build it */
851 error("Can't compile .rc file at the moment: %s\n", file);
852 break;
853 case file_res:
854 strarray_add(files, strmake("-r%s", file));
855 break;
856 case file_obj:
857 strarray_add(files, strmake("-o%s", file));
858 break;
859 case file_arh:
860 strarray_add(files, strmake("-a%s", file));
861 break;
862 case file_so:
863 strarray_add(files, strmake("-s%s", file));
864 break;
865 case file_na:
866 error("File does not exist: %s\n", file);
867 break;
868 default:
869 file = compile_to_object(opts, file, lang);
870 strarray_add(files, strmake("-o%s", file));
871 break;
874 else if (file[1] == 'l')
875 add_library(opts, lib_dirs, files, file + 2 );
876 else if (file[1] == 'x')
877 lang = file;
880 /* building for Windows is completely different */
882 if (opts->target_platform == PLATFORM_WINDOWS || opts->target_platform == PLATFORM_CYGWIN)
884 strarray *resources = strarray_alloc();
885 char *res_o_name = NULL;
887 if (opts->win16_app)
888 error( "Building 16-bit code is not supported for Windows\n" );
890 strarray_addall(link_args, get_translator(opts));
892 if (opts->shared)
894 /* run winebuild to generate the .def file */
895 char *spec_def_name = get_temp_file(output_name, ".spec.def");
896 spec_args = get_winebuild_args( opts );
897 strarray_add(spec_args, "--def");
898 strarray_add(spec_args, "-o");
899 strarray_add(spec_args, spec_def_name);
900 if (spec_file)
902 strarray_add(spec_args, "--export");
903 strarray_add(spec_args, spec_file);
905 spawn(opts->prefix, spec_args, 0);
906 strarray_free(spec_args);
908 strarray_add(link_args, "-shared");
909 if (verbose) strarray_add(link_args, "-v");
910 strarray_add(link_args, "-Wl,--kill-at");
911 strarray_add(link_args, spec_def_name);
913 else
915 strarray_add(link_args, opts->gui_app ? "-mwindows" : "-mconsole");
916 if (opts->nodefaultlibs) strarray_add(link_args, "-nodefaultlibs");
919 for ( j = 0 ; j < opts->linker_args->size ; j++ )
920 strarray_add(link_args, opts->linker_args->base[j]);
922 strarray_add(link_args, "-o");
923 strarray_add(link_args, output_file);
925 if (opts->image_base)
926 strarray_add(link_args, strmake("-Wl,--image-base,%s", opts->image_base));
928 if (opts->large_address_aware && opts->target_cpu == CPU_x86)
929 strarray_add( link_args, "-Wl,--large-address-aware" );
931 if (opts->unicode_app && !opts->shared)
932 strarray_add(link_args, mingw_unicode_hack(opts));
934 for ( j = 0; j < lib_dirs->size; j++ )
935 strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
937 if (!opts->nodefaultlibs)
939 add_library(opts, lib_dirs, files, "winecrt0");
940 add_library(opts, lib_dirs, files, "kernel32");
941 add_library(opts, lib_dirs, files, "ntdll");
943 if (!opts->shared && opts->use_msvcrt && opts->target_platform == PLATFORM_CYGWIN)
944 add_library(opts, lib_dirs, files, "msvcrt");
946 for ( j = 0; j < files->size; j++ )
948 const char* name = files->base[j] + 2;
950 switch(files->base[j][1])
952 case 'l':
953 case 'd':
954 strarray_add(link_args, strmake("-l%s", name));
955 break;
956 case 's':
957 case 'o':
958 strarray_add(link_args, name);
959 break;
960 case 'a':
961 if (!opts->lib_suffix && strchr(name, '/'))
963 /* turn the path back into -Ldir -lfoo options
964 * this makes sure that we use the specified libs even
965 * when mingw adds its own import libs to the link */
966 char *lib = xstrdup( name );
967 char *p = strrchr( lib, '/' );
969 *p++ = 0;
970 if (!strncmp( p, "lib", 3 ))
972 char *ext = strrchr( p, '.' );
974 if (ext) *ext = 0;
975 p += 3;
976 strarray_add(link_args, strmake("-L%s", lib ));
977 strarray_add(link_args, strmake("-l%s", p ));
978 free( lib );
979 break;
981 free( lib );
983 strarray_add(link_args, name);
984 break;
985 case 'r':
986 if (!res_o_name)
988 res_o_name = get_temp_file( output_name, ".res.o" );
989 strarray_add( link_args, res_o_name );
991 strarray_add( resources, name );
992 break;
996 if (res_o_name) compile_resources_to_object( opts, resources, res_o_name );
998 spawn(opts->prefix, link_args, 0);
999 strarray_free (resources);
1000 strarray_free (link_args);
1001 strarray_free (lib_dirs);
1002 strarray_free (files);
1003 return;
1006 /* add the default libraries, if needed */
1007 if (!opts->nostdlib && opts->use_msvcrt) add_library(opts, lib_dirs, files, "msvcrt");
1009 if (!opts->wine_objdir && !opts->nodefaultlibs)
1011 if (opts->gui_app)
1013 add_library(opts, lib_dirs, files, "shell32");
1014 add_library(opts, lib_dirs, files, "comdlg32");
1015 add_library(opts, lib_dirs, files, "gdi32");
1017 add_library(opts, lib_dirs, files, "advapi32");
1018 add_library(opts, lib_dirs, files, "user32");
1021 if (!opts->nodefaultlibs)
1023 add_library(opts, lib_dirs, files, "winecrt0");
1024 if (opts->win16_app) add_library(opts, lib_dirs, files, "kernel");
1025 add_library(opts, lib_dirs, files, "kernel32");
1026 add_library(opts, lib_dirs, files, "ntdll");
1028 if (!opts->nostdlib) add_library(opts, lib_dirs, files, "wine");
1030 /* run winebuild to generate the .spec.o file */
1031 spec_args = get_winebuild_args( opts );
1032 strarray_add( spec_args, strmake( "--cc-cmd=%s", build_tool_name( opts, "gcc", CC )));
1033 strarray_add( spec_args, strmake( "--ld-cmd=%s", build_tool_name( opts, "ld", LD )));
1035 spec_o_name = get_temp_file(output_name, ".spec.o");
1036 if (opts->force_pointer_size)
1037 strarray_add(spec_args, strmake("-m%u", 8 * opts->force_pointer_size ));
1038 strarray_add(spec_args, "-D_REENTRANT");
1039 strarray_add(spec_args, "-fPIC");
1040 strarray_add(spec_args, opts->shared ? "--dll" : "--exe");
1041 if (fake_module)
1043 strarray_add(spec_args, "--fake-module");
1044 strarray_add(spec_args, "-o");
1045 strarray_add(spec_args, output_file);
1047 else
1049 strarray_add(spec_args, "-o");
1050 strarray_add(spec_args, spec_o_name);
1052 if (spec_file)
1054 strarray_add(spec_args, "-E");
1055 strarray_add(spec_args, spec_file);
1057 if (opts->win16_app) strarray_add(spec_args, "-m16");
1059 if (!opts->shared)
1061 strarray_add(spec_args, "-F");
1062 strarray_add(spec_args, output_name);
1063 strarray_add(spec_args, "--subsystem");
1064 strarray_add(spec_args, opts->gui_app ? "windows" : "console");
1065 if (opts->unicode_app)
1067 strarray_add(spec_args, "--entry");
1068 strarray_add(spec_args, "__wine_spec_exe_wentry");
1070 if (opts->large_address_aware) strarray_add( spec_args, "--large-address-aware" );
1073 for ( j = 0; j < lib_dirs->size; j++ )
1074 strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
1076 for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
1077 strarray_add(spec_args, opts->winebuild_args->base[j]);
1079 /* add resource files */
1080 for ( j = 0; j < files->size; j++ )
1081 if (files->base[j][1] == 'r') strarray_add(spec_args, files->base[j]);
1083 /* add other files */
1084 strarray_add(spec_args, "--");
1085 for ( j = 0; j < files->size; j++ )
1087 switch(files->base[j][1])
1089 case 'd':
1090 case 'a':
1091 case 'o':
1092 strarray_add(spec_args, files->base[j] + 2);
1093 break;
1097 spawn(opts->prefix, spec_args, 0);
1098 strarray_free (spec_args);
1099 if (fake_module) return; /* nothing else to do */
1101 /* link everything together now */
1102 strarray_addall(link_args, get_translator(opts));
1103 strarray_addall(link_args, get_lddllflags(opts, link_args));
1105 strarray_add(link_args, "-o");
1106 strarray_add(link_args, strmake("%s.so", output_file));
1108 for ( j = 0 ; j < opts->linker_args->size ; j++ )
1109 strarray_add(link_args, opts->linker_args->base[j]);
1111 switch (opts->target_platform)
1113 case PLATFORM_APPLE:
1114 if (opts->image_base)
1116 strarray_add(link_args, "-image_base");
1117 strarray_add(link_args, opts->image_base);
1119 if (opts->strip)
1120 strarray_add(link_args, "-Wl,-x");
1121 break;
1122 case PLATFORM_SOLARIS:
1124 char *mapfile = get_temp_file( output_name, ".map" );
1125 const char *align = opts->section_align ? opts->section_align : "0x1000";
1127 create_file( mapfile, 0644, "text = A%s;\ndata = A%s;\n", align, align );
1128 strarray_add(link_args, strmake("-Wl,-M,%s", mapfile));
1129 strarray_add(tmp_files, mapfile);
1131 break;
1132 case PLATFORM_ANDROID:
1133 /* the Android loader requires a soname for all libraries */
1134 strarray_add( link_args, strmake( "-Wl,-soname,%s.so", output_name ));
1135 break;
1136 default:
1137 if (opts->image_base)
1139 if (!try_link(opts->prefix, link_args, "-Wl,-z,max-page-size=0x1000"))
1140 strarray_add(link_args, "-Wl,-z,max-page-size=0x1000");
1141 if (!try_link(opts->prefix, link_args, strmake("-Wl,-Ttext-segment=%s", opts->image_base)))
1142 strarray_add(link_args, strmake("-Wl,-Ttext-segment=%s", opts->image_base));
1143 else
1144 prelink = PRELINK;
1146 break;
1149 for ( j = 0; j < lib_dirs->size; j++ )
1150 strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
1152 strarray_add(link_args, spec_o_name);
1154 for ( j = 0; j < files->size; j++ )
1156 const char* name = files->base[j] + 2;
1157 switch(files->base[j][1])
1159 case 'l':
1160 strarray_add(link_args, strmake("-l%s", name));
1161 break;
1162 case 's':
1163 case 'a':
1164 case 'o':
1165 strarray_add(link_args, name);
1166 break;
1170 if (!opts->nostdlib)
1172 strarray_add(link_args, "-lm");
1173 strarray_add(link_args, "-lc");
1176 spawn(opts->prefix, link_args, 0);
1177 strarray_free (link_args);
1179 /* set the base address with prelink if linker support is not present */
1180 if (prelink && !opts->target)
1182 if (prelink[0] && strcmp(prelink,"false"))
1184 strarray *prelink_args = strarray_alloc();
1185 strarray_add(prelink_args, prelink);
1186 strarray_add(prelink_args, "--reloc-only");
1187 strarray_add(prelink_args, opts->image_base);
1188 strarray_add(prelink_args, strmake("%s.so", output_file));
1189 spawn(opts->prefix, prelink_args, 1);
1190 strarray_free(prelink_args);
1194 /* create the loader script */
1195 if (generate_app_loader)
1196 create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
1200 static void forward(int argc, char **argv, struct options* opts)
1202 strarray* args = strarray_alloc();
1203 int j;
1205 strarray_addall(args, get_translator(opts));
1207 for( j = 1; j < argc; j++ )
1208 strarray_add(args, argv[j]);
1210 spawn(opts->prefix, args, 0);
1211 strarray_free (args);
1215 * Linker Options
1216 * object-file-name -llibrary -nostartfiles -nodefaultlibs
1217 * -nostdlib -s -static -static-libgcc -shared -shared-libgcc
1218 * -symbolic -Wl,option -Xlinker option -u symbol
1219 * -framework name
1221 static int is_linker_arg(const char* arg)
1223 static const char* link_switches[] =
1225 "-nostartfiles", "-nostdlib", "-s",
1226 "-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic",
1227 "-framework", "--coverage", "-fprofile-generate", "-fprofile-use"
1229 unsigned int j;
1231 switch (arg[1])
1233 case 'R':
1234 case 'z':
1235 case 'l':
1236 case 'u':
1237 return 1;
1238 case 'W':
1239 if (strncmp("-Wl,", arg, 4) == 0) return 1;
1240 break;
1241 case 'X':
1242 if (strcmp("-Xlinker", arg) == 0) return 1;
1243 break;
1244 case 'a':
1245 if (strcmp("-arch", arg) == 0) return 1;
1246 break;
1249 for (j = 0; j < sizeof(link_switches)/sizeof(link_switches[0]); j++)
1250 if (strcmp(link_switches[j], arg) == 0) return 1;
1252 return 0;
1256 * Target Options
1257 * -b machine -V version
1259 static int is_target_arg(const char* arg)
1261 return arg[1] == 'b' || arg[1] == 'V';
1266 * Directory Options
1267 * -Bprefix -Idir -I- -Ldir -specs=file
1269 static int is_directory_arg(const char* arg)
1271 return arg[1] == 'B' || arg[1] == 'L' || arg[1] == 'I' || strncmp("-specs=", arg, 7) == 0;
1275 * MinGW Options
1276 * -mno-cygwin -mwindows -mconsole -mthreads -municode
1278 static int is_mingw_arg(const char* arg)
1280 static const char* mingw_switches[] =
1282 "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads", "-municode"
1284 unsigned int j;
1286 for (j = 0; j < sizeof(mingw_switches)/sizeof(mingw_switches[0]); j++)
1287 if (strcmp(mingw_switches[j], arg) == 0) return 1;
1289 return 0;
1292 static void parse_target_option( struct options *opts, const char *target )
1294 char *p, *platform, *spec = xstrdup( target );
1295 unsigned int i;
1297 /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */
1299 /* get the CPU part */
1301 if ((p = strchr( spec, '-' )))
1303 *p++ = 0;
1304 for (i = 0; i < sizeof(cpu_names)/sizeof(cpu_names[0]); i++)
1306 if (!strcmp( cpu_names[i].name, spec ))
1308 opts->target_cpu = cpu_names[i].cpu;
1309 break;
1312 if (i == sizeof(cpu_names)/sizeof(cpu_names[0]))
1313 error( "Unrecognized CPU '%s'\n", spec );
1314 platform = p;
1315 if ((p = strrchr( p, '-' ))) platform = p + 1;
1317 else if (!strcmp( spec, "mingw32" ))
1319 opts->target_cpu = CPU_x86;
1320 platform = spec;
1322 else
1323 error( "Invalid target specification '%s'\n", target );
1325 /* get the OS part */
1327 opts->target_platform = PLATFORM_UNSPECIFIED; /* default value */
1328 for (i = 0; i < sizeof(platform_names)/sizeof(platform_names[0]); i++)
1330 if (!strncmp( platform_names[i].name, platform, strlen(platform_names[i].name) ))
1332 opts->target_platform = platform_names[i].platform;
1333 break;
1337 free( spec );
1338 opts->target = xstrdup( target );
1341 int main(int argc, char **argv)
1343 int i, c, next_is_arg = 0, linking = 1;
1344 int raw_compiler_arg, raw_linker_arg;
1345 const char* option_arg;
1346 struct options opts;
1347 char* lang = 0;
1348 char* str;
1350 #ifdef SIGHUP
1351 signal( SIGHUP, exit_on_signal );
1352 #endif
1353 signal( SIGTERM, exit_on_signal );
1354 signal( SIGINT, exit_on_signal );
1355 #ifdef HAVE_SIGADDSET
1356 sigemptyset( &signal_mask );
1357 sigaddset( &signal_mask, SIGHUP );
1358 sigaddset( &signal_mask, SIGTERM );
1359 sigaddset( &signal_mask, SIGINT );
1360 #endif
1362 /* setup tmp file removal at exit */
1363 tmp_files = strarray_alloc();
1364 atexit(clean_temp_files);
1366 /* initialize options */
1367 memset(&opts, 0, sizeof(opts));
1368 opts.target_cpu = build_cpu;
1369 opts.target_platform = build_platform;
1370 opts.lib_dirs = strarray_alloc();
1371 opts.files = strarray_alloc();
1372 opts.linker_args = strarray_alloc();
1373 opts.compiler_args = strarray_alloc();
1374 opts.winebuild_args = strarray_alloc();
1376 /* determine the processor type */
1377 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
1378 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
1380 /* parse options */
1381 for ( i = 1 ; i < argc ; i++ )
1383 if (argv[i][0] == '-') /* option */
1385 /* determine if this switch is followed by a separate argument */
1386 next_is_arg = 0;
1387 option_arg = 0;
1388 switch(argv[i][1])
1390 case 'x': case 'o': case 'D': case 'U':
1391 case 'I': case 'A': case 'l': case 'u':
1392 case 'b': case 'V': case 'G': case 'L':
1393 case 'B': case 'R': case 'z':
1394 if (argv[i][2]) option_arg = &argv[i][2];
1395 else next_is_arg = 1;
1396 break;
1397 case 'i':
1398 next_is_arg = 1;
1399 break;
1400 case 'a':
1401 if (strcmp("-aux-info", argv[i]) == 0)
1402 next_is_arg = 1;
1403 if (strcmp("-arch", argv[i]) == 0)
1404 next_is_arg = 1;
1405 break;
1406 case 'X':
1407 if (strcmp("-Xlinker", argv[i]) == 0)
1408 next_is_arg = 1;
1409 break;
1410 case 'M':
1411 c = argv[i][2];
1412 if (c == 'F' || c == 'T' || c == 'Q')
1414 if (argv[i][3]) option_arg = &argv[i][3];
1415 else next_is_arg = 1;
1417 break;
1418 case 'f':
1419 if (strcmp("-framework", argv[i]) == 0)
1420 next_is_arg = 1;
1421 break;
1422 case '-':
1423 if (strcmp("--param", argv[i]) == 0)
1424 next_is_arg = 1;
1425 break;
1427 if (next_is_arg)
1429 if (i + 1 >= argc) error("option -%c requires an argument\n", argv[i][1]);
1430 option_arg = argv[i+1];
1433 /* determine what options go 'as is' to the linker & the compiler */
1434 raw_compiler_arg = raw_linker_arg = 0;
1435 if (is_linker_arg(argv[i]))
1437 raw_linker_arg = 1;
1439 else
1441 if (is_directory_arg(argv[i]) || is_target_arg(argv[i]))
1442 raw_linker_arg = 1;
1443 raw_compiler_arg = !is_mingw_arg(argv[i]);
1446 /* these things we handle explicitly so we don't pass them 'as is' */
1447 if (argv[i][1] == 'l' || argv[i][1] == 'I' || argv[i][1] == 'L')
1448 raw_linker_arg = 0;
1449 if (argv[i][1] == 'c' || argv[i][1] == 'L')
1450 raw_compiler_arg = 0;
1451 if (argv[i][1] == 'o' || argv[i][1] == 'b' || argv[i][1] == 'V')
1452 raw_compiler_arg = raw_linker_arg = 0;
1454 /* do a bit of semantic analysis */
1455 switch (argv[i][1])
1457 case 'B':
1458 str = strdup(option_arg);
1459 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
1460 if (strendswith(str, "/tools/winebuild"))
1462 char *objdir = strdup(str);
1463 objdir[strlen(objdir) - sizeof("/tools/winebuild") + 1] = 0;
1464 opts.wine_objdir = objdir;
1465 /* don't pass it to the compiler, this generates warnings */
1466 raw_compiler_arg = raw_linker_arg = 0;
1468 else if (!strcmp(str, "tools/winebuild"))
1470 opts.wine_objdir = ".";
1471 raw_compiler_arg = raw_linker_arg = 0;
1473 if (!opts.prefix) opts.prefix = strarray_alloc();
1474 strarray_add(opts.prefix, str);
1475 break;
1476 case 'b':
1477 parse_target_option( &opts, option_arg );
1478 break;
1479 case 'V':
1480 opts.version = xstrdup( option_arg );
1481 break;
1482 case 'c': /* compile or assemble */
1483 if (argv[i][2] == 0) opts.compile_only = 1;
1484 /* fall through */
1485 case 'S': /* generate assembler code */
1486 case 'E': /* preprocess only */
1487 if (argv[i][2] == 0) linking = 0;
1488 break;
1489 case 'f':
1490 if (strcmp("-fno-short-wchar", argv[i]) == 0)
1491 opts.noshortwchar = 1;
1492 else if (!strcmp("-fasynchronous-unwind-tables", argv[i]))
1493 opts.unwind_tables = 1;
1494 else if (!strcmp("-fno-asynchronous-unwind-tables", argv[i]))
1495 opts.unwind_tables = 0;
1496 break;
1497 case 'l':
1498 strarray_add(opts.files, strmake("-l%s", option_arg));
1499 break;
1500 case 'L':
1501 strarray_add(opts.lib_dirs, option_arg);
1502 break;
1503 case 'M': /* map file generation */
1504 linking = 0;
1505 break;
1506 case 'm':
1507 if (strcmp("-mno-cygwin", argv[i]) == 0)
1508 opts.use_msvcrt = 1;
1509 else if (strcmp("-mwindows", argv[i]) == 0)
1510 opts.gui_app = 1;
1511 else if (strcmp("-mconsole", argv[i]) == 0)
1512 opts.gui_app = 0;
1513 else if (strcmp("-municode", argv[i]) == 0)
1514 opts.unicode_app = 1;
1515 else if (strcmp("-m16", argv[i]) == 0)
1516 opts.win16_app = 1;
1517 else if (strcmp("-m32", argv[i]) == 0)
1519 if (opts.target_cpu == CPU_x86_64)
1520 opts.target_cpu = CPU_x86;
1521 else if (opts.target_cpu == CPU_ARM64)
1522 opts.target_cpu = CPU_ARM;
1523 opts.force_pointer_size = 4;
1524 raw_linker_arg = 1;
1526 else if (strcmp("-m64", argv[i]) == 0)
1528 if (opts.target_cpu == CPU_x86)
1529 opts.target_cpu = CPU_x86_64;
1530 else if (opts.target_cpu == CPU_ARM)
1531 opts.target_cpu = CPU_ARM64;
1532 opts.force_pointer_size = 8;
1533 raw_linker_arg = 1;
1535 else if (!strcmp("-marm", argv[i] ) || !strcmp("-mthumb", argv[i] ))
1537 strarray_add(opts.winebuild_args, argv[i]);
1538 raw_linker_arg = 1;
1540 else if (strncmp("-mcpu=", argv[i], 6) == 0 || strncmp("-march=", argv[i], 7) == 0)
1541 strarray_add(opts.winebuild_args, argv[i]);
1542 break;
1543 case 'n':
1544 if (strcmp("-nostdinc", argv[i]) == 0)
1545 opts.nostdinc = 1;
1546 else if (strcmp("-nodefaultlibs", argv[i]) == 0)
1547 opts.nodefaultlibs = 1;
1548 else if (strcmp("-nostdlib", argv[i]) == 0)
1549 opts.nostdlib = 1;
1550 else if (strcmp("-nostartfiles", argv[i]) == 0)
1551 opts.nostartfiles = 1;
1552 break;
1553 case 'o':
1554 opts.output_name = option_arg;
1555 break;
1556 case 's':
1557 if (strcmp("-static", argv[i]) == 0)
1558 linking = -1;
1559 else if(strcmp("-save-temps", argv[i]) == 0)
1560 keep_generated = 1;
1561 else if(strcmp("-shared", argv[i]) == 0)
1563 opts.shared = 1;
1564 raw_compiler_arg = raw_linker_arg = 0;
1566 else if (strcmp("-s", argv[i]) == 0 && opts.target_platform == PLATFORM_APPLE)
1568 /* On Mac, change -s into -Wl,-x. ld's -s switch
1569 * is deprecated, and it doesn't work on Tiger with
1570 * MH_BUNDLEs anyway
1572 opts.strip = 1;
1573 raw_linker_arg = 0;
1575 break;
1576 case 'v':
1577 if (argv[i][2] == 0) verbose++;
1578 break;
1579 case 'W':
1580 if (strncmp("-Wl,", argv[i], 4) == 0)
1582 unsigned int j;
1583 strarray* Wl = strarray_fromstring(argv[i] + 4, ",");
1584 for (j = 0; j < Wl->size; j++)
1586 if (!strcmp(Wl->base[j], "--image-base") && j < Wl->size - 1)
1588 opts.image_base = strdup( Wl->base[++j] );
1589 continue;
1591 if (!strcmp(Wl->base[j], "--section-alignment") && j < Wl->size - 1)
1593 opts.section_align = strdup( Wl->base[++j] );
1594 continue;
1596 if (!strcmp(Wl->base[j], "--large-address-aware"))
1598 opts.large_address_aware = 1;
1599 continue;
1601 if (!strcmp(Wl->base[j], "-static")) linking = -1;
1602 strarray_add(opts.linker_args, strmake("-Wl,%s",Wl->base[j]));
1604 strarray_free(Wl);
1605 raw_compiler_arg = raw_linker_arg = 0;
1607 else if (strncmp("-Wb,", argv[i], 4) == 0)
1609 strarray* Wb = strarray_fromstring(argv[i] + 4, ",");
1610 strarray_addall(opts.winebuild_args, Wb);
1611 strarray_free(Wb);
1612 /* don't pass it to the compiler, it generates errors */
1613 raw_compiler_arg = raw_linker_arg = 0;
1615 break;
1616 case 'x':
1617 lang = strmake("-x%s", option_arg);
1618 strarray_add(opts.files, lang);
1619 /* we'll pass these flags ourselves, explicitly */
1620 raw_compiler_arg = raw_linker_arg = 0;
1621 break;
1622 case '-':
1623 if (strcmp("-static", argv[i]+1) == 0)
1624 linking = -1;
1625 else if (!strncmp("--sysroot", argv[i], 9) && opts.wine_objdir)
1627 if (argv[i][9] == '=') opts.wine_objdir = argv[i] + 10;
1628 else opts.wine_objdir = argv[++i];
1629 raw_compiler_arg = raw_linker_arg = 0;
1631 else if (!strncmp("--lib-suffix", argv[i], 12) && opts.wine_objdir)
1633 if (argv[i][12] == '=') opts.lib_suffix = argv[i] + 13;
1634 else opts.lib_suffix = argv[++i];
1635 raw_compiler_arg = raw_linker_arg = 0;
1637 break;
1640 /* put the arg into the appropriate bucket */
1641 if (raw_linker_arg)
1643 strarray_add(opts.linker_args, argv[i]);
1644 if (next_is_arg && (i + 1 < argc))
1645 strarray_add(opts.linker_args, argv[i + 1]);
1647 if (raw_compiler_arg)
1649 strarray_add(opts.compiler_args, argv[i]);
1650 if (next_is_arg && (i + 1 < argc))
1651 strarray_add(opts.compiler_args, argv[i + 1]);
1654 /* skip the next token if it's an argument */
1655 if (next_is_arg) i++;
1657 else
1659 strarray_add(opts.files, argv[i]);
1663 if (opts.processor == proc_cpp) linking = 0;
1664 if (linking == -1) error("Static linking is not supported\n");
1666 if (opts.files->size == 0) forward(argc, argv, &opts);
1667 else if (linking) build(&opts);
1668 else compile(&opts, lang);
1670 return 0;