winedbg: Support # as comment delimiter in commands.
[wine.git] / tools / winegcc / winegcc.c
blobe8457e46643555365b110ed9f7b430b906e5c86d
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"
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>
98 #include <ctype.h>
99 #include <limits.h>
100 #include <sys/types.h>
101 #ifdef HAVE_SYS_SYSCTL_H
102 # include <sys/sysctl.h>
103 #endif
105 #include "utils.h"
107 static const char* app_loader_template =
108 "#!/bin/sh\n"
109 "\n"
110 "appname=\"%s\"\n"
111 "# determine the application directory\n"
112 "appdir=''\n"
113 "case \"$0\" in\n"
114 " */*)\n"
115 " # $0 contains a path, use it\n"
116 " appdir=`dirname \"$0\"`\n"
117 " ;;\n"
118 " *)\n"
119 " # no directory in $0, search in PATH\n"
120 " saved_ifs=$IFS\n"
121 " IFS=:\n"
122 " for d in $PATH\n"
123 " do\n"
124 " IFS=$saved_ifs\n"
125 " if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
126 " done\n"
127 " ;;\n"
128 "esac\n"
129 "\n"
130 "# figure out the full app path\n"
131 "if [ -n \"$appdir\" ]; then\n"
132 " apppath=\"$appdir/$appname\"\n"
133 " WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
134 " export WINEDLLPATH\n"
135 "else\n"
136 " apppath=\"$appname\"\n"
137 "fi\n"
138 "\n"
139 "# determine the WINELOADER\n"
140 "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
141 "\n"
142 "# and try to start the app\n"
143 "exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
146 static const char *output_file_name;
147 static const char *output_debug_file;
148 static const char *output_implib;
149 static int keep_generated = 0;
150 static struct strarray tmp_files;
151 #ifdef HAVE_SIGSET_T
152 static sigset_t signal_mask;
153 #endif
155 static const char *bindir;
156 static const char *libdir;
157 static const char *includedir;
159 enum processor { proc_cc, proc_cxx, proc_cpp, proc_as };
161 struct options
163 enum processor processor;
164 struct target target;
165 const char *target_alias;
166 const char *version;
167 int shared;
168 int use_msvcrt;
169 int nostdinc;
170 int nostdlib;
171 int nostartfiles;
172 int nodefaultlibs;
173 int noshortwchar;
174 int unix_lib;
175 int gui_app;
176 int unicode_app;
177 int win16_app;
178 int compile_only;
179 int force_pointer_size;
180 int large_address_aware;
181 int wine_builtin;
182 int fake_module;
183 int unwind_tables;
184 int strip;
185 int pic;
186 const char* wine_objdir;
187 const char* winebuild;
188 const char* output_name;
189 const char* image_base;
190 const char* section_align;
191 const char* file_align;
192 const char* sysroot;
193 const char* isysroot;
194 const char* lib_suffix;
195 const char* subsystem;
196 const char* entry_point;
197 const char* prelink;
198 const char* debug_file;
199 const char* out_implib;
200 struct strarray prefix;
201 struct strarray lib_dirs;
202 struct strarray args;
203 struct strarray linker_args;
204 struct strarray compiler_args;
205 struct strarray winebuild_args;
206 struct strarray files;
207 struct strarray delayimports;
210 static void cleanup_output_files(void)
212 if (output_file_name) unlink( output_file_name );
213 if (output_debug_file) unlink( output_debug_file );
214 if (output_implib) unlink( output_implib );
217 static void clean_temp_files(void)
219 unsigned int i;
221 if (keep_generated) return;
223 for (i = 0; i < tmp_files.count; i++)
224 unlink(tmp_files.str[i]);
227 /* clean things up when aborting on a signal */
228 static void exit_on_signal( int sig )
230 exit(1); /* this will call the atexit functions */
233 static char* get_temp_file(const char* prefix, const char* suffix)
235 int fd;
236 char *tmp;
238 #ifdef HAVE_SIGPROCMASK
239 sigset_t old_set;
240 /* block signals while manipulating the temp files list */
241 sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
242 #endif
243 fd = make_temp_file( prefix, suffix, &tmp );
244 close( fd );
245 strarray_add(&tmp_files, tmp);
246 #ifdef HAVE_SIGPROCMASK
247 sigprocmask( SIG_SETMASK, &old_set, NULL );
248 #endif
249 return tmp;
252 static int is_pe_target( const struct options *opts )
254 switch (opts->target.platform)
256 case PLATFORM_MINGW:
257 case PLATFORM_CYGWIN:
258 case PLATFORM_WINDOWS:
259 return 1;
260 default:
261 return 0;
265 enum tool
267 TOOL_CC,
268 TOOL_CXX,
269 TOOL_CPP,
270 TOOL_LD,
271 TOOL_OBJCOPY,
274 static const struct
276 const char *base;
277 const char *llvm_base;
278 const char *deflt;
279 } tool_names[] =
281 { "gcc", "clang --driver-mode=gcc", CC },
282 { "g++", "clang --driver-mode=g++", CXX },
283 { "cpp", "clang --driver-mode=cpp", CPP },
284 { "ld", "ld.lld", LD },
285 { "objcopy", "llvm-objcopy" },
288 static struct strarray build_tool_name( struct options *opts, enum tool tool )
290 const char *base = tool_names[tool].base;
291 const char *llvm_base = tool_names[tool].llvm_base;
292 const char *deflt = tool_names[tool].deflt;
293 const char *path;
294 struct strarray ret = empty_strarray;
295 char* str;
297 if (opts->target_alias && opts->version)
299 str = strmake("%s-%s-%s", opts->target_alias, base, opts->version);
301 else if (opts->target_alias)
303 str = strmake("%s-%s", opts->target_alias, base);
305 else if (opts->version)
307 str = strmake("%s-%s", base, opts->version);
309 else
310 str = xstrdup((deflt && *deflt) ? deflt : base);
312 if ((path = find_binary( opts->prefix, str ))) return strarray_fromstring( path, " " );
314 if (!opts->version) str = xstrdup( llvm_base );
315 else str = strmake( "%s-%s", llvm_base, opts->version );
316 path = find_binary( opts->prefix, str );
317 if (!path)
319 error( "Could not find %s\n", base );
320 return ret;
323 ret = strarray_fromstring( path, " " );
324 if (!strncmp( llvm_base, "clang", 5 ))
326 if (opts->target_alias)
328 strarray_add( &ret, "-target" );
329 strarray_add( &ret, opts->target_alias );
331 strarray_add( &ret, "-Wno-unused-command-line-argument" );
332 strarray_add( &ret, "-fuse-ld=lld" );
334 return ret;
337 static struct strarray get_translator(struct options *opts)
339 enum tool tool;
341 switch(opts->processor)
343 case proc_cpp:
344 tool = TOOL_CPP;
345 break;
346 case proc_cc:
347 case proc_as:
348 tool = TOOL_CC;
349 break;
350 case proc_cxx:
351 tool = TOOL_CXX;
352 break;
353 default:
354 assert(0);
356 return build_tool_name( opts, tool );
359 static int try_link( struct strarray prefix, struct strarray link_tool, const char *cflags )
361 const char *in = get_temp_file( "try_link", ".c" );
362 const char *out = get_temp_file( "try_link", ".out" );
363 const char *err = get_temp_file( "try_link", ".err" );
364 struct strarray link = empty_strarray;
365 int sout = -1, serr = -1;
366 int ret;
368 create_file( in, 0644, "int main(void){return 1;}\n" );
370 strarray_addall( &link, link_tool );
371 strarray_add( &link, "-o" );
372 strarray_add( &link, out );
373 strarray_addall( &link, strarray_fromstring( cflags, " " ) );
374 strarray_add( &link, in );
376 sout = dup( fileno(stdout) );
377 freopen( err, "w", stdout );
378 serr = dup( fileno(stderr) );
379 freopen( err, "w", stderr );
380 ret = spawn( prefix, link, 1 );
381 if (sout >= 0)
383 dup2( sout, fileno(stdout) );
384 close( sout );
386 if (serr >= 0)
388 dup2( serr, fileno(stderr) );
389 close( serr );
391 return ret;
394 static struct strarray get_link_args( struct options *opts, const char *output_name )
396 struct strarray link_args = get_translator( opts );
397 struct strarray flags = empty_strarray;
399 strarray_addall( &link_args, opts->linker_args );
401 if (verbose > 1) strarray_add( &flags, "-v" );
403 switch (opts->target.platform)
405 case PLATFORM_APPLE:
406 strarray_add( &flags, opts->unix_lib ? "-dynamiclib" : "-bundle" );
407 strarray_add( &flags, "-multiply_defined" );
408 strarray_add( &flags, "suppress" );
409 if (opts->image_base)
411 strarray_add( &flags, "-image_base" );
412 strarray_add( &flags, opts->image_base );
414 if (opts->strip) strarray_add( &flags, "-Wl,-x" );
415 if (opts->unix_lib)
417 strarray_add( &flags, "-install_name" );
418 strarray_add( &flags, strmake( "@loader_path/%s.so", output_name ) );
420 strarray_addall( &link_args, flags );
421 return link_args;
423 case PLATFORM_SOLARIS:
425 char *mapfile = get_temp_file( output_name, ".map" );
426 const char *align = opts->section_align ? opts->section_align : "0x1000";
428 create_file( mapfile, 0644, "text = A%s;\ndata = A%s;\n", align, align );
429 strarray_add( &flags, strmake("-Wl,-M,%s", mapfile) );
430 strarray_add( &tmp_files, mapfile );
432 break;
434 case PLATFORM_ANDROID:
435 /* the Android loader requires a soname for all libraries */
436 strarray_add( &flags, strmake( "-Wl,-soname,%s.so", output_name ));
437 break;
439 case PLATFORM_MINGW:
440 case PLATFORM_CYGWIN:
441 if (opts->shared || opts->win16_app)
443 strarray_add( &flags, "-shared" );
444 strarray_add( &flags, "-Wl,--kill-at" );
446 else strarray_add( &flags, opts->gui_app ? "-mwindows" : "-mconsole" );
448 if (opts->unicode_app) strarray_add( &flags, "-municode" );
449 if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
450 if (opts->nostartfiles || opts->use_msvcrt) strarray_add( &flags, "-nostartfiles" );
451 if (opts->subsystem) strarray_add( &flags, strmake("-Wl,--subsystem,%s", opts->subsystem ));
453 strarray_add( &flags, "-Wl,--nxcompat" );
455 if (opts->image_base) strarray_add( &flags, strmake("-Wl,--image-base,%s", opts->image_base ));
457 if (opts->large_address_aware && opts->target.cpu == CPU_i386)
458 strarray_add( &flags, "-Wl,--large-address-aware" );
460 /* make sure we don't need a libgcc_s dll on Windows */
461 if (!opts->nodefaultlibs && !opts->use_msvcrt)
462 strarray_add( &flags, "-static-libgcc" );
464 if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
465 strarray_add(&link_args, strmake("-Wl,-pdb,%s", opts->debug_file));
467 if (opts->out_implib)
468 strarray_add(&link_args, strmake("-Wl,--out-implib,%s", opts->out_implib));
470 if (!try_link( opts->prefix, link_args, "-Wl,--file-alignment,0x1000" ))
471 strarray_add( &link_args, strmake( "-Wl,--file-alignment,%s",
472 opts->file_align ? opts->file_align : "0x1000" ));
473 else if (!try_link( opts->prefix, link_args, "-Wl,-Xlink=-filealign:0x1000" ))
474 /* lld from llvm 10 does not support mingw style --file-alignment,
475 * but it's possible to use msvc syntax */
476 strarray_add( &link_args, strmake( "-Wl,-Xlink=-filealign:%s",
477 opts->file_align ? opts->file_align : "0x1000" ));
479 strarray_addall( &link_args, flags );
480 return link_args;
482 case PLATFORM_WINDOWS:
483 if (opts->shared || opts->win16_app)
485 strarray_add( &flags, "-shared" );
486 strarray_add( &flags, "-Wl,-kill-at" );
488 if (opts->unicode_app) strarray_add( &flags, "-municode" );
489 if (opts->nodefaultlibs || opts->use_msvcrt) strarray_add( &flags, "-nodefaultlibs" );
490 if (opts->nostartfiles || opts->use_msvcrt) strarray_add( &flags, "-nostartfiles" );
491 if (opts->image_base) strarray_add( &flags, strmake("-Wl,-base:%s", opts->image_base ));
492 if (opts->subsystem)
493 strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->subsystem ));
494 else
495 strarray_add( &flags, strmake("-Wl,-subsystem:%s", opts->gui_app ? "windows" : "console" ));
497 if (opts->debug_file && strendswith(opts->debug_file, ".pdb"))
499 strarray_add(&link_args, "-Wl,-debug");
500 strarray_add(&link_args, strmake("-Wl,-pdb:%s", opts->debug_file));
502 else if (!opts->strip)
503 strarray_add(&link_args, "-Wl,-debug:dwarf");
505 if (opts->out_implib)
506 strarray_add(&link_args, strmake("-Wl,-implib:%s", opts->out_implib));
508 strarray_add( &link_args, strmake( "-Wl,-filealign:%s", opts->file_align ? opts->file_align : "0x1000" ));
510 strarray_addall( &link_args, flags );
511 return link_args;
513 default:
514 if (opts->image_base)
516 if (!try_link( opts->prefix, link_args, strmake("-Wl,-Ttext-segment=%s", opts->image_base)) )
517 strarray_add( &flags, strmake("-Wl,-Ttext-segment=%s", opts->image_base) );
518 else
519 opts->prelink = PRELINK;
521 if (!try_link( opts->prefix, link_args, "-Wl,-z,max-page-size=0x1000"))
522 strarray_add( &flags, "-Wl,-z,max-page-size=0x1000");
523 if (opts->unix_lib) strarray_add( &flags, strmake( "-Wl,-soname,%s.so", output_name ));
524 break;
527 /* generic Unix shared library flags */
529 strarray_add( &link_args, "-shared" );
530 strarray_add( &link_args, "-Wl,-Bsymbolic" );
531 if (!opts->noshortwchar && opts->target.cpu == CPU_ARM)
532 strarray_add( &flags, "-Wl,--no-wchar-size-warning" );
533 if (!try_link( opts->prefix, link_args, "-Wl,-z,defs" ))
534 strarray_add( &flags, "-Wl,-z,defs" );
536 strarray_addall( &link_args, flags );
537 return link_args;
540 static const char *get_multiarch_dir( struct target target )
542 switch (target.cpu)
544 case CPU_i386: return "/i386-linux-gnu";
545 case CPU_x86_64: return "/x86_64-linux-gnu";
546 case CPU_ARM: return "/arm-linux-gnueabi";
547 case CPU_ARM64: return "/aarch64-linux-gnu";
549 assert(0);
550 return NULL;
553 static char *get_lib_dir( struct options *opts )
555 const char *stdlibpath[] = { libdir, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
556 const char *bit_suffix, *other_bit_suffix, *build_multiarch, *target_multiarch, *winecrt0;
557 const char *root = opts->sysroot ? opts->sysroot : "";
558 unsigned int i;
559 struct stat st;
560 size_t build_len, target_len;
562 bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "64" : "32";
563 other_bit_suffix = get_target_ptr_size( opts->target ) == 8 ? "32" : "64";
564 winecrt0 = strmake( "/wine%s/libwinecrt0.a", get_arch_dir( opts->target ));
565 build_multiarch = get_multiarch_dir( get_default_target() );
566 target_multiarch = get_multiarch_dir( opts->target );
567 build_len = strlen( build_multiarch );
568 target_len = strlen( target_multiarch );
570 for (i = 0; i < ARRAY_SIZE(stdlibpath); i++)
572 const char *root = (i && opts->sysroot) ? opts->sysroot : "";
573 char *p, *buffer;
575 if (!stdlibpath[i]) continue;
576 buffer = xmalloc( strlen(root) + strlen(stdlibpath[i]) +
577 strlen("/arm-linux-gnueabi") + strlen(winecrt0) + 1 );
578 strcpy( buffer, root );
579 strcat( buffer, stdlibpath[i] );
580 p = buffer + strlen(buffer);
581 while (p > buffer && p[-1] == '/') p--;
582 strcpy( p, winecrt0 );
583 if (!stat( buffer, &st )) goto found;
584 if (p > buffer + 2 && (!memcmp( p - 2, "32", 2 ) || !memcmp( p - 2, "64", 2 )))
586 p -= 2;
587 strcpy( p, winecrt0 );
588 if (!stat( buffer, &st )) goto found;
590 strcpy( p, bit_suffix );
591 strcat( p, winecrt0 );
592 if (!stat( buffer, &st )) goto found;
593 strcpy( p, target_multiarch );
594 strcat( p, winecrt0 );
595 if (!stat( buffer, &st )) goto found;
597 strcpy( buffer, root );
598 strcat( buffer, stdlibpath[i] );
599 p = buffer + strlen(buffer);
600 while (p > buffer && p[-1] == '/') p--;
601 strcpy( p, winecrt0 );
603 /* try to fixup each parent dirs named lib, lib32 or lib64 with target bitness suffix */
604 while (p > buffer)
606 p--;
607 while (p > buffer && *p != '/') p--;
608 if (*p != '/') break;
610 /* try s/$build_cpu/$target_cpu/ on multiarch */
611 if (get_default_target().cpu != opts->target.cpu &&
612 !memcmp( p, build_multiarch, build_len ) && p[build_len] == '/')
614 memmove( p + target_len, p + build_len, strlen( p + build_len ) + 1 );
615 memcpy( p, target_multiarch, target_len );
616 if (!stat( buffer, &st )) goto found;
617 memmove( p + build_len, p + target_len, strlen( p + target_len ) + 1 );
618 memcpy( p, build_multiarch, build_len );
621 if (memcmp( p + 1, "lib", 3 )) continue;
622 if (p[4] == '/')
624 memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
625 memcpy( p + 4, bit_suffix, 2 );
626 if (!stat( buffer, &st )) goto found;
627 memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
629 else if (!memcmp( p + 4, other_bit_suffix, 2 ) && p[6] == '/')
631 memcpy( p + 4, bit_suffix, 2 );
632 if (!stat( buffer, &st )) goto found;
633 memmove( p + 4, p + 6, strlen( p + 6 ) + 1 );
634 if (!stat( buffer, &st )) goto found;
635 memmove( p + 6, p + 4, strlen( p + 4 ) + 1 );
636 memcpy( p + 4, other_bit_suffix, 2 );
640 free( buffer );
641 continue;
643 found:
644 buffer[strlen(buffer) - strlen(winecrt0)] = 0;
645 return buffer;
647 return strmake( "%s%s", root, LIBDIR );
650 static void init_argv0_dir( const char *argv0 )
652 #ifndef _WIN32
653 char *dir;
655 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
656 dir = realpath( "/proc/self/exe", NULL );
657 #elif defined (__FreeBSD__) || defined(__DragonFly__)
658 static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
659 size_t path_size = PATH_MAX;
660 char *path = malloc( path_size );
661 if (path && !sysctl( pathname, sizeof(pathname)/sizeof(pathname[0]), path, &path_size, NULL, 0 ))
662 dir = realpath( path, NULL );
663 free( path );
664 #else
665 dir = realpath( argv0, NULL );
666 #endif
667 if (!dir) return;
668 bindir = get_dirname( dir );
669 includedir = strmake( "%s/%s", bindir, BIN_TO_INCLUDEDIR );
670 libdir = strmake( "%s/%s", bindir, BIN_TO_LIBDIR );
671 #endif
674 static void compile(struct options* opts, const char* lang)
676 struct strarray comp_args = get_translator(opts);
677 unsigned int i, j;
678 int gcc_defs = 0;
679 struct strarray gcc;
680 struct strarray gpp;
682 if (opts->force_pointer_size)
683 strarray_add( &comp_args, strmake("-m%u", 8 * opts->force_pointer_size ) );
684 switch(opts->processor)
686 case proc_cpp: gcc_defs = 1; break;
687 case proc_as: gcc_defs = 0; break;
688 /* Note: if the C compiler is gcc we assume the C++ compiler is too */
689 /* mixing different C and C++ compilers isn't supported in configure anyway */
690 case proc_cc:
691 case proc_cxx:
692 gcc = build_tool_name(opts, TOOL_CC);
693 gpp = build_tool_name(opts, TOOL_CXX);
694 for ( j = 0; !gcc_defs && j < comp_args.count; j++ )
696 const char *cc = comp_args.str[j];
698 for (i = 0; !gcc_defs && i < gcc.count; i++)
699 gcc_defs = gcc.str[i][0] != '-' && strendswith(cc, gcc.str[i]);
700 for (i = 0; !gcc_defs && i < gpp.count; i++)
701 gcc_defs = gpp.str[i][0] != '-' && strendswith(cc, gpp.str[i]);
703 break;
706 if (opts->target.platform == PLATFORM_WINDOWS ||
707 opts->target.platform == PLATFORM_CYGWIN ||
708 opts->target.platform == PLATFORM_MINGW)
709 goto no_compat_defines;
711 if (opts->processor != proc_cpp)
713 if (gcc_defs && !opts->wine_objdir && !opts->noshortwchar)
715 strarray_add(&comp_args, "-fshort-wchar");
716 strarray_add(&comp_args, "-DWINE_UNICODE_NATIVE");
718 strarray_add(&comp_args, "-D_REENTRANT");
719 if (opts->pic)
720 strarray_add(&comp_args, "-fPIC");
721 else
722 strarray_add(&comp_args, "-fno-PIC");
725 if (get_target_ptr_size( opts->target ) == 8)
727 strarray_add(&comp_args, "-DWIN64");
728 strarray_add(&comp_args, "-D_WIN64");
729 strarray_add(&comp_args, "-D__WIN64");
730 strarray_add(&comp_args, "-D__WIN64__");
733 strarray_add(&comp_args, "-DWIN32");
734 strarray_add(&comp_args, "-D_WIN32");
735 strarray_add(&comp_args, "-D__WIN32");
736 strarray_add(&comp_args, "-D__WIN32__");
737 strarray_add(&comp_args, "-D__WINNT");
738 strarray_add(&comp_args, "-D__WINNT__");
740 if (gcc_defs)
742 switch (opts->target.cpu)
744 case CPU_x86_64:
745 case CPU_ARM64:
746 strarray_add(&comp_args, "-D__stdcall=__attribute__((ms_abi))");
747 strarray_add(&comp_args, "-D__cdecl=__stdcall");
748 strarray_add(&comp_args, "-D__fastcall=__stdcall");
749 break;
750 case CPU_i386:
751 strarray_add(&comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
752 strarray_add(&comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
753 strarray_add(&comp_args, "-D__fastcall=__attribute__((__fastcall__))");
754 break;
755 case CPU_ARM:
756 strarray_add(&comp_args, "-D__stdcall=__attribute__((pcs(\"aapcs-vfp\")))");
757 strarray_add(&comp_args, "-D__cdecl=__stdcall");
758 strarray_add(&comp_args, "-D__fastcall=__stdcall");
759 break;
761 strarray_add(&comp_args, "-D_stdcall=__stdcall");
762 strarray_add(&comp_args, "-D_cdecl=__cdecl");
763 strarray_add(&comp_args, "-D_fastcall=__fastcall");
764 strarray_add(&comp_args, "-D__declspec(x)=__declspec_##x");
765 strarray_add(&comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
766 strarray_add(&comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
767 strarray_add(&comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
768 strarray_add(&comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
769 strarray_add(&comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
770 strarray_add(&comp_args, "-D__declspec_naked=__attribute__((naked))");
771 strarray_add(&comp_args, "-D__declspec_noinline=__attribute__((noinline))");
772 strarray_add(&comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
773 strarray_add(&comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
774 strarray_add(&comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
775 strarray_add(&comp_args, "-D__declspec_selectany=__attribute__((weak))");
776 strarray_add(&comp_args, "-D__declspec_thread=__thread");
779 strarray_add(&comp_args, "-D__int8=char");
780 strarray_add(&comp_args, "-D__int16=short");
781 strarray_add(&comp_args, "-D__int32=int");
782 if (get_target_ptr_size( opts->target ) == 8)
783 strarray_add(&comp_args, "-D__int64=long");
784 else
785 strarray_add(&comp_args, "-D__int64=long long");
787 no_compat_defines:
788 strarray_add(&comp_args, "-D__WINE__");
790 /* options we handle explicitly */
791 if (opts->compile_only)
792 strarray_add(&comp_args, "-c");
793 if (opts->output_name)
795 strarray_add(&comp_args, "-o");
796 strarray_add(&comp_args, opts->output_name);
799 /* the rest of the pass-through parameters */
800 strarray_addall(&comp_args, opts->compiler_args);
802 /* the language option, if any */
803 if (lang && strcmp(lang, "-xnone"))
804 strarray_add(&comp_args, lang);
806 /* last, but not least, the files */
807 for ( j = 0; j < opts->files.count; j++ )
809 if (opts->files.str[j][0] != '-' || !opts->files.str[j][1]) /* not an option or bare '-' (i.e. stdin) */
810 strarray_add(&comp_args, opts->files.str[j]);
813 /* standard includes come last in the include search path */
814 if (!opts->wine_objdir && !opts->nostdinc)
816 const char *incl_dirs[] = { INCLUDEDIR, "/usr/include", "/usr/local/include" };
817 const char *root = opts->isysroot ? opts->isysroot : opts->sysroot ? opts->sysroot : "";
818 const char *isystem = gcc_defs ? "-isystem" : "-I";
819 const char *idirafter = gcc_defs ? "-idirafter" : "-I";
821 if (opts->use_msvcrt)
823 if (includedir) strarray_add( &comp_args, strmake( "%s%s/wine/msvcrt", isystem, includedir ));
824 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
826 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
827 strarray_add(&comp_args, strmake( "%s%s%s/wine/msvcrt", isystem, root, incl_dirs[j] ));
829 strarray_add(&comp_args, "-D__MSVCRT__");
831 if (includedir)
833 strarray_add( &comp_args, strmake( "%s%s/wine/windows", isystem, includedir ));
834 strarray_add( &comp_args, strmake( "%s%s", idirafter, includedir ));
836 for (j = 0; j < ARRAY_SIZE(incl_dirs); j++)
838 if (j && !strcmp( incl_dirs[0], incl_dirs[j] )) continue;
839 strarray_add(&comp_args, strmake( "%s%s%s/wine/windows", isystem, root, incl_dirs[j] ));
840 strarray_add(&comp_args, strmake( "%s%s%s", idirafter, root, incl_dirs[j] ));
843 else if (opts->wine_objdir)
844 strarray_add(&comp_args, strmake("-I%s/include", opts->wine_objdir) );
846 spawn(opts->prefix, comp_args, 0);
849 static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
851 struct options copts;
853 /* make a copy so we don't change any of the initial stuff */
854 /* a shallow copy is exactly what we want in this case */
855 copts = *opts;
856 copts.output_name = get_temp_file(get_basename_noext(file), ".o");
857 copts.compile_only = 1;
858 copts.files = empty_strarray;
859 strarray_add(&copts.files, file);
860 compile(&copts, lang);
861 return copts.output_name;
864 /* return the initial set of options needed to run winebuild */
865 static struct strarray get_winebuild_args(struct options *opts)
867 const char* winebuild = getenv("WINEBUILD");
868 const char *binary = NULL;
869 struct strarray spec_args = empty_strarray;
870 unsigned int i;
872 if (opts->winebuild)
873 binary = opts->winebuild;
874 else if (opts->wine_objdir)
875 binary = strmake( "%s/tools/winebuild/winebuild%s", opts->wine_objdir, EXEEXT );
876 else if (winebuild)
877 binary = find_binary( opts->prefix, winebuild );
878 else if (bindir)
879 binary = strmake( "%s/winebuild%s", bindir, EXEEXT );
880 else
881 binary = find_binary( opts->prefix, "winebuild" );
882 if (!binary) error( "Could not find winebuild\n" );
883 strarray_add( &spec_args, binary );
884 if (verbose) strarray_add( &spec_args, "-v" );
885 if (keep_generated) strarray_add( &spec_args, "--save-temps" );
886 if (opts->target_alias)
888 strarray_add( &spec_args, "--target" );
889 strarray_add( &spec_args, opts->target_alias );
891 for (i = 0; i < opts->prefix.count; i++)
892 strarray_add( &spec_args, strmake( "-B%s", opts->prefix.str[i] ));
893 strarray_addall( &spec_args, opts->winebuild_args );
894 if (opts->unwind_tables) strarray_add( &spec_args, "-fasynchronous-unwind-tables" );
895 else strarray_add( &spec_args, "-fno-asynchronous-unwind-tables" );
896 return spec_args;
899 static void fixup_constructors( struct options *opts, const char *file )
901 struct strarray args = get_winebuild_args( opts );
903 strarray_add( &args, "--fixup-ctors" );
904 strarray_add( &args, file );
905 spawn( opts->prefix, args, 0 );
908 static void make_wine_builtin( struct options *opts, const char *file )
910 struct strarray args = get_winebuild_args( opts );
912 strarray_add( &args, "--builtin" );
913 strarray_add( &args, file );
914 spawn( opts->prefix, args, 0 );
917 /* check if there is a static lib associated to a given dll */
918 static char *find_static_lib( const char *dll )
920 char *lib = strmake("%s.a", dll);
921 if (get_file_type(lib) == file_arh) return lib;
922 free( lib );
923 return NULL;
926 static const char *find_libgcc(struct strarray prefix, struct strarray link_tool)
928 const char *out = get_temp_file( "find_libgcc", ".out" );
929 const char *err = get_temp_file( "find_libgcc", ".err" );
930 struct strarray link = empty_strarray;
931 int sout = -1, serr = -1;
932 char *libgcc, *p;
933 struct stat st;
934 size_t cnt;
935 int ret;
937 strarray_addall( &link, link_tool );
938 strarray_add( &link, "-print-libgcc-file-name" );
940 sout = dup( fileno(stdout) );
941 freopen( out, "w", stdout );
942 serr = dup( fileno(stderr) );
943 freopen( err, "w", stderr );
944 ret = spawn( prefix, link, 1 );
945 if (sout >= 0)
947 dup2( sout, fileno(stdout) );
948 close( sout );
950 if (serr >= 0)
952 dup2( serr, fileno(stderr) );
953 close( serr );
956 if (ret || stat(out, &st) || !st.st_size) return NULL;
958 libgcc = xmalloc(st.st_size + 1);
959 sout = open(out, O_RDONLY);
960 if (sout == -1) return NULL;
961 cnt = read(sout, libgcc, st.st_size);
962 close(sout);
963 libgcc[cnt] = 0;
964 if ((p = strchr(libgcc, '\n'))) *p = 0;
965 return libgcc;
969 /* add specified library to the list of files */
970 static void add_library( struct options *opts, struct strarray lib_dirs,
971 struct strarray *files, const char *library )
973 char *static_lib, *fullname = 0;
975 switch(get_lib_type(opts->target, lib_dirs, library, "lib", opts->lib_suffix, &fullname))
977 case file_arh:
978 strarray_add(files, strmake("-a%s", fullname));
979 break;
980 case file_dll:
981 strarray_add(files, strmake("-d%s", fullname));
982 if ((static_lib = find_static_lib(fullname)))
984 strarray_add(files, strmake("-a%s",static_lib));
985 free(static_lib);
987 break;
988 case file_so:
989 default:
990 /* keep it anyway, the linker may know what to do with it */
991 strarray_add(files, strmake("-l%s", library));
992 break;
994 free(fullname);
997 /* run winebuild to generate the .spec.o file */
998 static const char *build_spec_obj( struct options *opts, const char *spec_file, const char *output_file,
999 struct strarray files, struct strarray lib_dirs, const char *entry_point )
1001 unsigned int i;
1002 int is_pe = is_pe_target( opts );
1003 struct strarray spec_args = get_winebuild_args( opts );
1004 struct strarray tool;
1005 const char *spec_o_name, *output_name;
1007 /* get the filename from the path */
1008 output_name = get_basename( output_file );
1010 tool = build_tool_name( opts, TOOL_CC );
1011 strarray_add( &spec_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
1012 if (!is_pe)
1014 tool = build_tool_name( opts, TOOL_LD );
1015 strarray_add( &spec_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
1018 spec_o_name = get_temp_file(output_name, ".spec.o");
1019 if (opts->force_pointer_size)
1020 strarray_add(&spec_args, strmake("-m%u", 8 * opts->force_pointer_size ));
1021 if (opts->pic && !is_pe) strarray_add(&spec_args, "-fPIC");
1022 strarray_add(&spec_args, opts->shared ? "--dll" : "--exe");
1023 if (opts->fake_module)
1025 strarray_add(&spec_args, "--fake-module");
1026 strarray_add(&spec_args, "-o");
1027 strarray_add(&spec_args, output_file);
1029 else
1031 strarray_add(&spec_args, "-o");
1032 strarray_add(&spec_args, spec_o_name);
1034 if (spec_file)
1036 strarray_add(&spec_args, "-E");
1037 strarray_add(&spec_args, spec_file);
1040 if (!opts->shared)
1042 strarray_add(&spec_args, "-F");
1043 strarray_add(&spec_args, output_name);
1044 strarray_add(&spec_args, "--subsystem");
1045 strarray_add(&spec_args, opts->gui_app ? "windows" : "console");
1046 if (opts->large_address_aware) strarray_add( &spec_args, "--large-address-aware" );
1049 if (opts->target.platform == PLATFORM_WINDOWS) strarray_add(&spec_args, "--safeseh");
1051 if (entry_point)
1053 strarray_add(&spec_args, "--entry");
1054 strarray_add(&spec_args, entry_point);
1057 if (opts->subsystem)
1059 strarray_add(&spec_args, "--subsystem");
1060 strarray_add(&spec_args, opts->subsystem);
1063 for (i = 0; i < lib_dirs.count; i++)
1064 strarray_add(&spec_args, strmake("-L%s", lib_dirs.str[i]));
1066 if (!is_pe)
1068 for (i = 0; i < opts->delayimports.count; i++)
1069 strarray_add(&spec_args, strmake("-d%s", opts->delayimports.str[i]));
1072 /* add resource files */
1073 for (i = 0; i < files.count; i++)
1074 if (files.str[i][1] == 'r') strarray_add(&spec_args, files.str[i]);
1076 /* add other files */
1077 strarray_add(&spec_args, "--");
1078 for (i = 0; i < files.count; i++)
1080 switch(files.str[i][1])
1082 case 'd':
1083 case 'a':
1084 case 'o':
1085 strarray_add(&spec_args, files.str[i] + 2);
1086 break;
1090 spawn(opts->prefix, spec_args, 0);
1091 return spec_o_name;
1094 static void build(struct options* opts)
1096 struct strarray lib_dirs = empty_strarray;
1097 struct strarray files = empty_strarray;
1098 struct strarray link_args;
1099 char *output_file, *output_path;
1100 const char *spec_o_name = NULL, *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 is_pe = is_pe_target( opts );
1105 unsigned int j;
1107 /* NOTE: for the files array we'll use the following convention:
1108 * -axxx: xxx is an archive (.a)
1109 * -dxxx: xxx is a DLL (.def)
1110 * -lxxx: xxx is an unsorted library
1111 * -oxxx: xxx is an object (.o)
1112 * -rxxx: xxx is a resource (.res)
1113 * -sxxx: xxx is a shared lib (.so)
1114 * -xlll: lll is the language (c, c++, etc.)
1117 output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
1119 /* 'winegcc -o app xxx.exe.so' only creates the load script */
1120 if (opts->files.count == 1 && strendswith(opts->files.str[0], ".exe.so"))
1122 create_file(output_file, 0755, app_loader_template, opts->files.str[0]);
1123 return;
1126 /* generate app loader only for .exe */
1127 if (opts->shared || is_pe || strendswith(output_file, ".so"))
1128 generate_app_loader = 0;
1130 if (strendswith(output_file, ".fake")) opts->fake_module = 1;
1132 /* normalize the filename a bit: strip .so, ensure it has proper ext */
1133 if (!strchr(get_basename( output_file ), '.'))
1134 output_file = strmake("%s.%s", output_file, opts->shared ? "dll" : "exe");
1135 else if (strendswith(output_file, ".so"))
1136 output_file[strlen(output_file) - 3] = 0;
1137 output_path = is_pe ? output_file : strmake( "%s.so", output_file );
1139 /* get the filename from the path */
1140 output_name = get_basename( output_file );
1142 /* prepare the linking path */
1143 if (!opts->wine_objdir)
1145 char *lib_dir = get_lib_dir( opts );
1146 strarray_addall( &lib_dirs, opts->lib_dirs );
1147 strarray_add( &lib_dirs, strmake( "%s/wine%s", lib_dir, get_arch_dir( opts->target )));
1148 strarray_add( &lib_dirs, lib_dir );
1150 else
1152 strarray_add(&lib_dirs, strmake("%s/dlls", opts->wine_objdir));
1153 strarray_addall(&lib_dirs, opts->lib_dirs);
1156 /* mark the files with their appropriate type */
1157 spec_file = lang = 0;
1158 for ( j = 0; j < opts->files.count; j++ )
1160 const char* file = opts->files.str[j];
1161 if (file[0] != '-')
1163 switch(get_file_type(file))
1165 case file_def:
1166 case file_spec:
1167 if (spec_file)
1168 error("Only one spec file can be specified\n");
1169 spec_file = file;
1170 break;
1171 case file_rc:
1172 /* FIXME: invoke wrc to build it */
1173 error("Can't compile .rc file at the moment: %s\n", file);
1174 break;
1175 case file_res:
1176 strarray_add(&files, strmake("-r%s", file));
1177 break;
1178 case file_obj:
1179 strarray_add(&files, strmake("-o%s", file));
1180 break;
1181 case file_arh:
1182 if (opts->use_msvcrt)
1184 char *p = get_basename( file );
1185 if (!strncmp(p, "libmsvcr", 8) || !strncmp(p, "libucrt", 7)) crt_lib = file;
1187 strarray_add(&files, strmake("-a%s", file));
1188 break;
1189 case file_so:
1190 strarray_add(&files, strmake("-s%s", file));
1191 break;
1192 case file_na:
1193 error("File does not exist: %s\n", file);
1194 break;
1195 default:
1196 file = compile_to_object(opts, file, lang);
1197 strarray_add(&files, strmake("-o%s", file));
1198 break;
1201 else if (file[1] == 'l')
1202 add_library(opts, lib_dirs, &files, file + 2 );
1203 else if (file[1] == 'x')
1204 lang = file;
1205 else if(file[1] == 'W')
1206 strarray_add(&files, file);
1209 /* add the default libraries, if needed */
1211 if (!opts->wine_objdir && !opts->nodefaultlibs)
1213 if (opts->gui_app)
1215 add_library(opts, lib_dirs, &files, "shell32");
1216 add_library(opts, lib_dirs, &files, "comdlg32");
1217 add_library(opts, lib_dirs, &files, "gdi32");
1219 add_library(opts, lib_dirs, &files, "advapi32");
1220 add_library(opts, lib_dirs, &files, "user32");
1221 add_library(opts, lib_dirs, &files, "winecrt0");
1222 if (opts->use_msvcrt)
1224 if (!crt_lib)
1226 if (strncmp( output_name, "msvcr", 5 ) &&
1227 strncmp( output_name, "ucrt", 4 ) &&
1228 strcmp( output_name, "crtdll.dll" ))
1229 add_library(opts, lib_dirs, &files, "ucrtbase");
1231 else strarray_add(&files, strmake("-a%s", crt_lib));
1233 if (opts->win16_app) add_library(opts, lib_dirs, &files, "kernel");
1234 add_library(opts, lib_dirs, &files, "kernel32");
1235 add_library(opts, lib_dirs, &files, "ntdll");
1238 /* set default entry point, if needed */
1239 if (!opts->entry_point)
1241 if (opts->subsystem && !opts->unix_lib && !strcmp( opts->subsystem, "native" ))
1242 entry_point = (is_pe && opts->target.cpu == CPU_i386) ? "DriverEntry@8" : "DriverEntry";
1243 else if (opts->use_msvcrt && !opts->shared && !opts->win16_app)
1244 entry_point = opts->unicode_app ? "wmainCRTStartup" : "mainCRTStartup";
1246 else entry_point = opts->entry_point;
1248 /* run winebuild to generate the .spec.o file */
1249 if (spec_file || !opts->unix_lib)
1250 spec_o_name = build_spec_obj( opts, spec_file, output_file, files, lib_dirs, entry_point );
1252 if (opts->fake_module) return; /* nothing else to do */
1254 /* link everything together now */
1255 link_args = get_link_args( opts, output_name );
1257 if ((opts->nodefaultlibs || opts->use_msvcrt) && opts->target.platform == PLATFORM_MINGW)
1259 libgcc = find_libgcc(opts->prefix, link_args);
1260 if (!libgcc) libgcc = "-lgcc";
1263 strarray_add(&link_args, "-o");
1264 strarray_add(&link_args, output_path);
1266 for ( j = 0; j < lib_dirs.count; j++ )
1267 strarray_add(&link_args, strmake("-L%s", lib_dirs.str[j]));
1269 if (is_pe && opts->use_msvcrt && !entry_point && (opts->shared || opts->win16_app))
1270 entry_point = opts->target.cpu == CPU_i386 ? "DllMainCRTStartup@12" : "DllMainCRTStartup";
1272 if (is_pe && entry_point)
1274 if (opts->target.platform == PLATFORM_WINDOWS)
1275 strarray_add(&link_args, strmake("-Wl,-entry:%s", entry_point));
1276 else
1277 strarray_add(&link_args, strmake("-Wl,--entry,%s%s",
1278 is_pe && opts->target.cpu == CPU_i386 ? "_" : "",
1279 entry_point));
1282 if (spec_o_name) strarray_add(&link_args, spec_o_name);
1284 if (is_pe)
1286 for (j = 0; j < opts->delayimports.count; j++)
1288 if (opts->target.platform == PLATFORM_WINDOWS)
1289 strarray_add(&link_args, strmake("-Wl,-delayload:%s", opts->delayimports.str[j]));
1290 else
1291 strarray_add(&link_args, strmake("-Wl,-delayload,%s",opts->delayimports.str[j]));
1295 for ( j = 0; j < files.count; j++ )
1297 const char* name = files.str[j] + 2;
1298 switch(files.str[j][1])
1300 case 'l':
1301 strarray_add(&link_args, strmake("-l%s", name));
1302 break;
1303 case 's':
1304 case 'o':
1305 strarray_add(&link_args, name);
1306 break;
1307 case 'a':
1308 if (!opts->use_msvcrt && !opts->lib_suffix && strchr(name, '/'))
1310 /* turn the path back into -Ldir -lfoo options
1311 * this makes sure that we use the specified libs even
1312 * when mingw adds its own import libs to the link */
1313 const char *p = get_basename( name );
1315 if (is_pe)
1317 if (!strncmp( p, "lib", 3 ) && strcmp( p, "libmsvcrt.a" ))
1319 strarray_add(&link_args, strmake("-L%s", get_dirname(name) ));
1320 strarray_add(&link_args, strmake("-l%s", get_basename_noext( p + 3 )));
1321 break;
1324 else
1326 /* don't link to ntdll or ntoskrnl in non-msvcrt mode
1327 * since they export CRT functions */
1328 if (!strcmp( p, "libntdll.a" )) break;
1329 if (!strcmp( p, "libntoskrnl.a" )) break;
1332 strarray_add(&link_args, name);
1333 break;
1334 case 'W':
1335 strarray_add(&link_args, files.str[j]);
1336 break;
1340 if (!opts->nostdlib && !is_pe)
1342 strarray_add(&link_args, "-lm");
1343 strarray_add(&link_args, "-lc");
1346 if (libgcc) strarray_add(&link_args, libgcc);
1348 output_file_name = output_path;
1349 output_debug_file = opts->debug_file;
1350 output_implib = opts->out_implib;
1351 atexit( cleanup_output_files );
1353 spawn(opts->prefix, link_args, 0);
1355 if (opts->debug_file && !strendswith(opts->debug_file, ".pdb"))
1357 struct strarray tool, objcopy = build_tool_name(opts, TOOL_OBJCOPY);
1359 tool = empty_strarray;
1360 strarray_addall( &tool, objcopy );
1361 strarray_add(&tool, "--only-keep-debug");
1362 strarray_add(&tool, output_path);
1363 strarray_add(&tool, opts->debug_file);
1364 spawn(opts->prefix, tool, 1);
1366 tool = empty_strarray;
1367 strarray_addall( &tool, objcopy );
1368 strarray_add(&tool, "--strip-debug");
1369 strarray_add(&tool, output_path);
1370 spawn(opts->prefix, tool, 1);
1372 tool = empty_strarray;
1373 strarray_addall( &tool, objcopy );
1374 strarray_add(&tool, "--add-gnu-debuglink");
1375 strarray_add(&tool, opts->debug_file);
1376 strarray_add(&tool, output_path);
1377 spawn(opts->prefix, tool, 0);
1380 if (opts->unix_lib) return;
1382 if (opts->out_implib && !is_pe)
1384 struct strarray tool, implib_args;
1386 if (!spec_file)
1387 error("--out-implib requires a .spec or .def file\n");
1389 implib_args = get_winebuild_args( opts );
1390 tool = build_tool_name( opts, TOOL_CC );
1391 strarray_add( &implib_args, strmake( "--cc-cmd=%s", strarray_tostring( tool, " " )));
1392 tool = build_tool_name( opts, TOOL_LD );
1393 strarray_add( &implib_args, strmake( "--ld-cmd=%s", strarray_tostring( tool, " " )));
1395 strarray_add(&implib_args, "--implib");
1396 strarray_add(&implib_args, "-o");
1397 strarray_add(&implib_args, opts->out_implib);
1398 strarray_add(&implib_args, "--export");
1399 strarray_add(&implib_args, spec_file);
1401 spawn(opts->prefix, implib_args, 0);
1404 /* set the base address with prelink if linker support is not present */
1405 if (opts->prelink && !opts->target_alias)
1407 if (opts->prelink[0] && strcmp(opts->prelink,"false"))
1409 struct strarray prelink_args = empty_strarray;
1410 strarray_add(&prelink_args, opts->prelink);
1411 strarray_add(&prelink_args, "--reloc-only");
1412 strarray_add(&prelink_args, opts->image_base);
1413 strarray_add(&prelink_args, output_path);
1414 spawn(opts->prefix, prelink_args, 1);
1418 if (!is_pe) fixup_constructors( opts, output_path );
1419 else if (opts->wine_builtin) make_wine_builtin( opts, output_path );
1421 /* create the loader script */
1422 if (generate_app_loader)
1423 create_file(output_file, 0755, app_loader_template, strmake("%s.so", output_name));
1427 static void forward( struct options *opts )
1429 struct strarray args = get_translator(opts);
1431 strarray_addall(&args, opts->compiler_args);
1432 strarray_addall(&args, opts->linker_args);
1433 spawn(opts->prefix, args, 0);
1436 static int is_linker_arg(const char* arg)
1438 static const char* link_switches[] =
1440 "-nostdlib", "-s", "-static", "-static-libgcc", "-static-libstdc++",
1441 "-shared", "-shared-libgcc", "-symbolic", "-framework", "--coverage",
1442 "-fprofile-generate", "-fprofile-use"
1444 unsigned int j;
1446 switch (arg[1])
1448 case 'R':
1449 case 'z':
1450 case 'u':
1451 return 1;
1452 case 'W':
1453 if (strncmp("-Wl,", arg, 4) == 0) return 1;
1454 break;
1455 case 'X':
1456 if (strcmp("-Xlinker", arg) == 0) return 1;
1457 break;
1458 case 'a':
1459 if (strcmp("-arch", arg) == 0) return 1;
1460 break;
1461 case 'f':
1462 if (strncmp("-fuse-ld=", arg, 9) == 0) return 1;
1463 break;
1464 case 'r':
1465 if (strncmp("-rtlib=", arg, 7) == 0) return 1;
1466 break;
1469 for (j = 0; j < ARRAY_SIZE(link_switches); j++)
1470 if (strcmp(link_switches[j], arg) == 0) return 1;
1472 return 0;
1475 static void parse_target_option( struct options *opts, const char *target )
1477 opts->target_alias = xstrdup( target );
1478 if (!parse_target( target, &opts->target )) error( "Invalid target specification '%s'\n", target );
1481 static int is_option( struct options *opts, int i, const char *option, const char **option_arg )
1483 if (!strcmp( opts->args.str[i], option ))
1485 if (opts->args.count == i) error( "option %s requires an argument\n", opts->args.str[i] );
1486 *option_arg = opts->args.str[i + 1];
1487 return 1;
1489 if (!strncmp( opts->args.str[i], option, strlen(option) ) && opts->args.str[i][strlen(option)] == '=')
1491 *option_arg = opts->args.str[i] + strlen(option) + 1;
1492 return 1;
1494 return 0;
1497 int main(int argc, char **argv)
1499 int i, c, next_is_arg = 0, linking = 1;
1500 int raw_compiler_arg, raw_linker_arg, raw_winebuild_arg;
1501 const char* option_arg;
1502 struct options opts;
1503 char* lang = 0;
1504 char* str;
1506 #ifdef SIGHUP
1507 signal( SIGHUP, exit_on_signal );
1508 #endif
1509 signal( SIGTERM, exit_on_signal );
1510 signal( SIGINT, exit_on_signal );
1511 #ifdef HAVE_SIGADDSET
1512 sigemptyset( &signal_mask );
1513 sigaddset( &signal_mask, SIGHUP );
1514 sigaddset( &signal_mask, SIGTERM );
1515 sigaddset( &signal_mask, SIGINT );
1516 #endif
1517 init_argv0_dir( argv[0] );
1519 /* setup tmp file removal at exit */
1520 atexit(clean_temp_files);
1522 /* initialize options */
1523 memset(&opts, 0, sizeof(opts));
1524 opts.target = init_argv0_target( argv[0] );
1525 opts.pic = 1;
1527 /* determine the processor type */
1528 if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
1529 else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
1531 for (i = 1; i < argc; i++)
1533 char *input_buffer = NULL, *iter, *opt, *out;
1534 struct stat st;
1535 int fd;
1537 if (argv[i][0] != '@' || (fd = open( argv[i] + 1, O_RDONLY | O_BINARY )) == -1)
1539 strarray_add( &opts.args, argv[i] );
1540 continue;
1542 if ((fstat( fd, &st ) == -1)) error( "Cannot stat %s\n", argv[i] + 1 );
1543 if (st.st_size)
1545 input_buffer = xmalloc( st.st_size + 1 );
1546 if (read( fd, input_buffer, st.st_size ) != st.st_size) error( "Cannot read %s\n", argv[i] + 1 );
1548 close( fd );
1549 for (iter = input_buffer; iter < input_buffer + st.st_size; iter++)
1551 char quote = 0;
1552 while (iter < input_buffer + st.st_size && isspace(*iter)) iter++;
1553 if (iter == input_buffer + st.st_size) break;
1554 opt = out = iter;
1555 while (iter < input_buffer + st.st_size && (quote || !isspace(*iter)))
1557 if (*iter == quote)
1559 iter++;
1560 quote = 0;
1562 else if (*iter == '\'' || *iter == '"') quote = *iter++;
1563 else
1565 if (*iter == '\\' && iter + 1 < input_buffer + st.st_size) iter++;
1566 *out++ = *iter++;
1569 *out = 0;
1570 strarray_add( &opts.args, opt );
1574 /* parse options */
1575 for (i = 0; i < opts.args.count; i++)
1577 if (opts.args.str[i][0] == '-' && opts.args.str[i][1]) /* option, except '-' alone is stdin, which is a file */
1579 /* determine if this switch is followed by a separate argument */
1580 next_is_arg = 0;
1581 option_arg = 0;
1582 switch(opts.args.str[i][1])
1584 case 'x': case 'o': case 'D': case 'U':
1585 case 'I': case 'A': case 'l': case 'u':
1586 case 'b': case 'V': case 'G': case 'L':
1587 case 'B': case 'R': case 'z':
1588 if (opts.args.str[i][2]) option_arg = &opts.args.str[i][2];
1589 else next_is_arg = 1;
1590 break;
1591 case 'i':
1592 next_is_arg = 1;
1593 break;
1594 case 'a':
1595 if (strcmp("-aux-info", opts.args.str[i]) == 0)
1596 next_is_arg = 1;
1597 if (strcmp("-arch", opts.args.str[i]) == 0)
1598 next_is_arg = 1;
1599 break;
1600 case 'X':
1601 if (strcmp("-Xlinker", opts.args.str[i]) == 0)
1602 next_is_arg = 1;
1603 break;
1604 case 'M':
1605 c = opts.args.str[i][2];
1606 if (c == 'F' || c == 'T' || c == 'Q')
1608 if (opts.args.str[i][3]) option_arg = &opts.args.str[i][3];
1609 else next_is_arg = 1;
1611 break;
1612 case 'f':
1613 if (strcmp("-framework", opts.args.str[i]) == 0)
1614 next_is_arg = 1;
1615 break;
1616 case 't':
1617 next_is_arg = strcmp("-target", opts.args.str[i]) == 0;
1618 break;
1619 case '-':
1620 next_is_arg = (strcmp("--param", opts.args.str[i]) == 0 ||
1621 strcmp("--sysroot", opts.args.str[i]) == 0 ||
1622 strcmp("--target", opts.args.str[i]) == 0 ||
1623 strcmp("--wine-objdir", opts.args.str[i]) == 0 ||
1624 strcmp("--winebuild", opts.args.str[i]) == 0 ||
1625 strcmp("--lib-suffix", opts.args.str[i]) == 0);
1626 break;
1628 if (next_is_arg)
1630 if (i + 1 >= opts.args.count) error("option -%c requires an argument\n", opts.args.str[i][1]);
1631 option_arg = opts.args.str[i+1];
1634 /* determine what options go 'as is' to the linker & the compiler */
1635 raw_linker_arg = is_linker_arg(opts.args.str[i]);
1636 raw_compiler_arg = !raw_linker_arg;
1637 raw_winebuild_arg = 0;
1639 /* do a bit of semantic analysis */
1640 switch (opts.args.str[i][1])
1642 case 'B':
1643 str = strdup(option_arg);
1644 if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
1645 strarray_add(&opts.prefix, str);
1646 raw_linker_arg = 1;
1647 break;
1648 case 'b':
1649 parse_target_option( &opts, option_arg );
1650 raw_compiler_arg = 0;
1651 break;
1652 case 'V':
1653 opts.version = xstrdup( option_arg );
1654 raw_compiler_arg = 0;
1655 break;
1656 case 'c': /* compile or assemble */
1657 raw_compiler_arg = 0;
1658 if (opts.args.str[i][2] == 0) opts.compile_only = 1;
1659 /* fall through */
1660 case 'S': /* generate assembler code */
1661 case 'E': /* preprocess only */
1662 if (opts.args.str[i][2] == 0) linking = 0;
1663 break;
1664 case 'f':
1665 if (strcmp("-fno-short-wchar", opts.args.str[i]) == 0)
1666 opts.noshortwchar = 1;
1667 else if (!strcmp("-fasynchronous-unwind-tables", opts.args.str[i]))
1668 opts.unwind_tables = 1;
1669 else if (!strcmp("-fno-asynchronous-unwind-tables", opts.args.str[i]))
1670 opts.unwind_tables = 0;
1671 else if (!strcmp("-fPIC", opts.args.str[i]) || !strcmp("-fpic", opts.args.str[i]))
1672 opts.pic = 1;
1673 else if (!strcmp("-fno-PIC", opts.args.str[i]) || !strcmp("-fno-pic", opts.args.str[i]))
1674 opts.pic = 0;
1675 break;
1676 case 'i':
1677 if (!strcmp( "-isysroot", opts.args.str[i] )) opts.isysroot = opts.args.str[i + 1];
1678 break;
1679 case 'l':
1680 strarray_add(&opts.files, strmake("-l%s", option_arg));
1681 raw_compiler_arg = 0;
1682 break;
1683 case 'L':
1684 strarray_add(&opts.lib_dirs, option_arg);
1685 raw_compiler_arg = 0;
1686 break;
1687 case 'M': /* map file generation */
1688 linking = 0;
1689 break;
1690 case 'm':
1691 if (strcmp("-mno-cygwin", opts.args.str[i]) == 0)
1693 opts.use_msvcrt = 1;
1694 raw_compiler_arg = 0;
1695 raw_winebuild_arg = 1;
1697 if (strcmp("-mcygwin", opts.args.str[i]) == 0)
1699 opts.use_msvcrt = 0;
1700 raw_compiler_arg = 0;
1702 else if (strcmp("-mwindows", opts.args.str[i]) == 0)
1704 opts.gui_app = 1;
1705 raw_compiler_arg = 0;
1707 else if (strcmp("-mconsole", opts.args.str[i]) == 0)
1709 opts.gui_app = 0;
1710 raw_compiler_arg = 0;
1712 else if (strcmp("-municode", opts.args.str[i]) == 0)
1714 opts.unicode_app = 1;
1715 raw_compiler_arg = 0;
1716 raw_winebuild_arg = 1;
1718 else if (strcmp("-mthreads", opts.args.str[i]) == 0)
1720 raw_compiler_arg = 0;
1722 else if (strcmp("-munix", opts.args.str[i]) == 0)
1724 opts.unix_lib = 1;
1725 raw_compiler_arg = 0;
1726 raw_winebuild_arg = 1;
1728 else if (strcmp("-m16", opts.args.str[i]) == 0)
1730 opts.win16_app = 1;
1731 raw_compiler_arg = 0;
1732 raw_winebuild_arg = 1;
1734 else if (strcmp("-m32", opts.args.str[i]) == 0)
1736 set_target_ptr_size( &opts.target, 4 );
1737 opts.force_pointer_size = 4;
1738 raw_linker_arg = 1;
1740 else if (strcmp("-m64", opts.args.str[i]) == 0)
1742 set_target_ptr_size( &opts.target, 8 );
1743 opts.force_pointer_size = 8;
1744 raw_linker_arg = 1;
1746 else if (!strcmp("-marm", opts.args.str[i] ) || !strcmp("-mthumb", opts.args.str[i] ))
1748 raw_linker_arg = 1;
1749 raw_winebuild_arg = 1;
1751 else if (!strncmp("-mcpu=", opts.args.str[i], 6) ||
1752 !strncmp("-mfpu=", opts.args.str[i], 6) ||
1753 !strncmp("-march=", opts.args.str[i], 7) ||
1754 !strncmp("-mfloat-abi=", opts.args.str[i], 12))
1755 raw_winebuild_arg = 1;
1756 break;
1757 case 'n':
1758 if (strcmp("-nostdinc", opts.args.str[i]) == 0)
1759 opts.nostdinc = 1;
1760 else if (strcmp("-nodefaultlibs", opts.args.str[i]) == 0)
1761 opts.nodefaultlibs = 1;
1762 else if (strcmp("-nostdlib", opts.args.str[i]) == 0)
1763 opts.nostdlib = 1;
1764 else if (strcmp("-nostartfiles", opts.args.str[i]) == 0)
1765 opts.nostartfiles = 1;
1766 break;
1767 case 'o':
1768 opts.output_name = option_arg;
1769 raw_compiler_arg = 0;
1770 break;
1771 case 'p':
1772 if (strcmp("-pthread", opts.args.str[i]) == 0)
1774 raw_compiler_arg = 1;
1775 raw_linker_arg = 1;
1777 break;
1778 case 's':
1779 if (strcmp("-static", opts.args.str[i]) == 0)
1780 linking = -1;
1781 else if(strcmp("-save-temps", opts.args.str[i]) == 0)
1782 keep_generated = 1;
1783 else if (strncmp("-specs=", opts.args.str[i], 7) == 0)
1784 raw_linker_arg = 1;
1785 else if(strcmp("-shared", opts.args.str[i]) == 0)
1787 opts.shared = 1;
1788 raw_compiler_arg = raw_linker_arg = 0;
1790 else if (strcmp("-s", opts.args.str[i]) == 0 && opts.target.platform == PLATFORM_APPLE)
1792 /* On Mac, change -s into -Wl,-x. ld's -s switch
1793 * is deprecated, and it doesn't work on Tiger with
1794 * MH_BUNDLEs anyway
1796 opts.strip = 1;
1797 raw_linker_arg = 0;
1799 break;
1800 case 't':
1801 if (is_option( &opts, i, "-target", &option_arg ))
1803 parse_target_option( &opts, option_arg );
1804 raw_compiler_arg = raw_linker_arg = 0;
1806 break;
1807 case 'v':
1808 if (opts.args.str[i][2] == 0) verbose++;
1809 break;
1810 case 'W':
1811 if (strncmp("-Wl,", opts.args.str[i], 4) == 0)
1813 unsigned int j;
1814 struct strarray Wl = strarray_fromstring(opts.args.str[i] + 4, ",");
1815 for (j = 0; j < Wl.count; j++)
1817 if (!strcmp(Wl.str[j], "--image-base") && j < Wl.count - 1)
1819 opts.image_base = strdup( Wl.str[++j] );
1820 continue;
1822 if (!strcmp(Wl.str[j], "--section-alignment") && j < Wl.count - 1)
1824 opts.section_align = strdup( Wl.str[++j] );
1825 continue;
1827 if (!strcmp(Wl.str[j], "--file-alignment") && j < Wl.count - 1)
1829 opts.file_align = strdup( Wl.str[++j] );
1830 continue;
1832 if (!strcmp(Wl.str[j], "--large-address-aware"))
1834 opts.large_address_aware = 1;
1835 continue;
1837 if (!strcmp(Wl.str[j], "--wine-builtin"))
1839 opts.wine_builtin = 1;
1840 continue;
1842 if (!strcmp(Wl.str[j], "--subsystem") && j < Wl.count - 1)
1844 opts.subsystem = strdup( Wl.str[++j] );
1845 continue;
1847 if (!strcmp(Wl.str[j], "--entry") && j < Wl.count - 1)
1849 opts.entry_point = strdup( Wl.str[++j] );
1850 continue;
1852 if (!strcmp(Wl.str[j], "-delayload") && j < Wl.count - 1)
1854 strarray_add( &opts.delayimports, Wl.str[++j] );
1855 continue;
1857 if (!strcmp(Wl.str[j], "--debug-file") && j < Wl.count - 1)
1859 opts.debug_file = strdup( Wl.str[++j] );
1860 continue;
1862 if (!strcmp(Wl.str[j], "--whole-archive") ||
1863 !strcmp(Wl.str[j], "--no-whole-archive") ||
1864 !strcmp(Wl.str[j], "--start-group") ||
1865 !strcmp(Wl.str[j], "--end-group"))
1867 strarray_add( &opts.files, strmake( "-Wl,%s", Wl.str[j] ));
1868 continue;
1870 if (!strcmp(Wl.str[j], "--out-implib"))
1872 opts.out_implib = strdup( Wl.str[++j] );
1873 continue;
1875 if (!strcmp(Wl.str[j], "-static")) linking = -1;
1876 strarray_add(&opts.linker_args, strmake("-Wl,%s",Wl.str[j]));
1878 raw_compiler_arg = raw_linker_arg = 0;
1880 else if (strncmp("-Wb,", opts.args.str[i], 4) == 0)
1882 unsigned int j;
1883 struct strarray Wb = strarray_fromstring(opts.args.str[i] + 4, ",");
1884 for (j = 0; j < Wb.count; j++)
1886 if (!strcmp(Wb.str[j], "--fake-module")) opts.fake_module = 1;
1887 else strarray_add( &opts.winebuild_args, Wb.str[j] );
1889 raw_compiler_arg = raw_linker_arg = 0;
1891 break;
1892 case 'x':
1893 lang = strmake("-x%s", option_arg);
1894 strarray_add(&opts.files, lang);
1895 /* we'll pass these flags ourselves, explicitly */
1896 raw_compiler_arg = raw_linker_arg = 0;
1897 break;
1898 case '-':
1899 if (strcmp("-static", opts.args.str[i]+1) == 0)
1900 linking = -1;
1901 else if (is_option( &opts, i, "--sysroot", &option_arg ))
1903 opts.sysroot = option_arg;
1904 raw_linker_arg = 1;
1906 else if (is_option( &opts, i, "--target", &option_arg ))
1908 parse_target_option( &opts, option_arg );
1909 raw_compiler_arg = raw_linker_arg = 0;
1911 else if (is_option( &opts, i, "--wine-objdir", &option_arg ))
1913 opts.wine_objdir = option_arg;
1914 raw_compiler_arg = raw_linker_arg = 0;
1916 else if (is_option( &opts, i, "--winebuild", &option_arg ))
1918 opts.winebuild = option_arg;
1919 raw_compiler_arg = raw_linker_arg = 0;
1921 else if (is_option( &opts, i, "--lib-suffix", &option_arg ))
1923 opts.lib_suffix = option_arg;
1924 raw_compiler_arg = raw_linker_arg = 0;
1926 break;
1929 /* put the arg into the appropriate bucket */
1930 if (raw_linker_arg)
1932 strarray_add( &opts.linker_args, opts.args.str[i] );
1933 if (next_is_arg && (i + 1 < opts.args.count))
1934 strarray_add( &opts.linker_args, opts.args.str[i + 1] );
1936 if (raw_compiler_arg)
1938 strarray_add( &opts.compiler_args, opts.args.str[i] );
1939 if (next_is_arg && (i + 1 < opts.args.count))
1940 strarray_add( &opts.compiler_args, opts.args.str[i + 1] );
1942 if (raw_winebuild_arg)
1944 strarray_add( &opts.winebuild_args, opts.args.str[i] );
1945 if (next_is_arg && (i + 1 < opts.args.count))
1946 strarray_add( &opts.winebuild_args, opts.args.str[i + 1] );
1949 /* skip the next token if it's an argument */
1950 if (next_is_arg) i++;
1952 else
1954 strarray_add( &opts.files, opts.args.str[i] );
1958 if (opts.force_pointer_size) set_target_ptr_size( &opts.target, opts.force_pointer_size );
1959 if (opts.processor == proc_cpp) linking = 0;
1960 if (linking == -1) error("Static linking is not supported\n");
1962 if (!opts.wine_objdir && is_pe_target( &opts )) opts.use_msvcrt = 1;
1964 if (opts.files.count == 0 && !opts.fake_module) forward(&opts);
1965 else if (linking) build(&opts);
1966 else compile(&opts, lang);
1968 output_file_name = NULL;
1969 output_debug_file = NULL;
1970 output_implib = NULL;
1971 return 0;