winegcc.c 30.1 KB
Newer Older
1 2 3
/*
 * MinGW wrapper: makes gcc behave like MinGW.
 *
4
 * Copyright 2000 Manuel Novoa III
5
 * Copyright 2000 Francois Gouget
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * Copyright 2002 Dimitrie O. Paun
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
21 22 23 24 25 26
 *
 * DESCRIPTION
 *
 * all options for gcc start with '-' and are for the most part
 * single options (no parameters as separate argument). 
 * There are of course exceptions to this rule, so here is an 
27
 * exhaustive list of options that do take parameters (potentially)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 * as a separate argument:
 *
 * Compiler:
 * -x language
 * -o filename
 * -aux-info filename
 *
 * Preprocessor:
 * -D name 
 * -U name
 * -I dir
 * -MF file
 * -MT target
 * -MQ target
 * (all -i.* arg)
 * -include file 
 * -imacros file
 * -idirafter dir
 * -iwithprefix dir
 * -iwithprefixbefore dir
 * -isystem dir
 * -A predicate=answer
 *
 * Linking:
 * -l library
 * -Xlinker option
 * -u symbol
 *
 * Misc:
 * -b machine
 * -V version
 * -G num  (see NOTES below)
 *
 * NOTES
 * There is -G option for compatibility with System V that
63 64
 * takes no parameters. This makes "-G num" parsing ambiguous.
 * This option is synonymous to -shared, and as such we will
65 66 67 68 69 70 71 72 73 74
 * not support it for now.
 *
 * Special interest options 
 *
 *      Assembler Option
 *          -Wa,option
 *
 *      Linker Options
 *          object-file-name  -llibrary -nostartfiles  -nodefaultlibs
 *          -nostdlib -s  -static  -static-libgcc  -shared  -shared-libgcc
75
 *          -symbolic -Wl,option  -Xlinker option -u symbol --image-base
76 77 78 79 80 81 82
 *
 *      Directory Options
 *          -Bprefix  -Idir  -I-  -Ldir  -specs=file
 *
 *      Target Options
 *          -b machine  -V version
 *
83 84
 * Please note that the Target Options are relevant to everything:
 *   compiler, linker, assembler, preprocessor.
85 86
 * 
 */ 
87 88 89 90 91

#include "config.h"
#include "wine/port.h"

#include <stdio.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
92
#include <stdlib.h>
93
#include <signal.h>
94
#include <stdarg.h>
95
#include <string.h>
96
#include <errno.h>
97

98
#include "utils.h"
99

100
static const char* app_loader_template =
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    "#!/bin/sh\n"
    "\n"
    "appname=\"%s\"\n"
    "# determine the application directory\n"
    "appdir=''\n"
    "case \"$0\" in\n"
    "  */*)\n"
    "    # $0 contains a path, use it\n"
    "    appdir=`dirname \"$0\"`\n"
    "    ;;\n"
    "  *)\n"
    "    # no directory in $0, search in PATH\n"
    "    saved_ifs=$IFS\n"
    "    IFS=:\n"
    "    for d in $PATH\n"
    "    do\n"
    "      IFS=$saved_ifs\n"
    "      if [ -x \"$d/$appname\" ]; then appdir=\"$d\"; break; fi\n"
    "    done\n"
    "    ;;\n"
    "esac\n"
    "\n"
    "# figure out the full app path\n"
    "if [ -n \"$appdir\" ]; then\n"
125
    "    apppath=\"$appdir/$appname\"\n"
126 127 128
    "    WINEDLLPATH=\"$appdir:$WINEDLLPATH\"\n"
    "    export WINEDLLPATH\n"
    "else\n"
129
    "    apppath=\"$appname\"\n"
130 131 132 133 134 135
    "fi\n"
    "\n"
    "# determine the WINELOADER\n"
    "if [ ! -x \"$WINELOADER\" ]; then WINELOADER=\"wine\"; fi\n"
    "\n"
    "# and try to start the app\n"
136
    "exec \"$WINELOADER\" \"$apppath\" \"$@\"\n"
137 138
;

139
static int keep_generated = 0;
140
static strarray* tmp_files;
141
#ifdef HAVE_SIGSET_T
142
static sigset_t signal_mask;
143
#endif
144

145 146
enum processor { proc_cc, proc_cxx, proc_cpp, proc_as };

147 148
struct options 
{
149
    enum processor processor;
150
    int shared;
151 152 153
    int use_msvcrt;
    int nostdinc;
    int nostdlib;
154
    int nostartfiles;
155
    int nodefaultlibs;
156
    int noshortwchar;
157
    int gui_app;
158
    int unicode_app;
159
    int compile_only;
160
    const char* wine_objdir;
161
    const char* output_name;
162
    const char* image_base;
163
    strarray* prefix;
164
    strarray* lib_dirs;
165 166
    strarray* linker_args;
    strarray* compiler_args;
167
    strarray* winebuild_args;
168 169 170
    strarray* files;
};

171
static void clean_temp_files(void)
172
{
173
    unsigned int i;
174 175 176 177 178

    if (keep_generated) return;

    for (i = 0; i < tmp_files->size; i++)
	unlink(tmp_files->base[i]);
179 180
}

181 182 183 184 185 186
/* clean things up when aborting on a signal */
static void exit_on_signal( int sig )
{
    exit(1);  /* this will call the atexit functions */
}

187
static char* get_temp_file(const char* prefix, const char* suffix)
188
{
189
    int fd;
190
    char* tmp = strmake("%s-XXXXXX%s", prefix, suffix);
191

192 193
#ifdef HAVE_SIGPROCMASK
    sigset_t old_set;
194 195
    /* block signals while manipulating the temp files list */
    sigprocmask( SIG_BLOCK, &signal_mask, &old_set );
196
#endif
197
    fd = mkstemps( tmp, strlen(suffix) );
198 199 200 201
    if (fd == -1)
    {
        /* could not create it in current directory, try in /tmp */
        free(tmp);
202
        tmp = strmake("/tmp/%s-XXXXXX%s", prefix, suffix);
203
        fd = mkstemps( tmp, strlen(suffix) );
204
        if (fd == -1) error( "could not create temp file\n" );
205 206
    }
    close( fd );
207
    strarray_add(tmp_files, tmp);
208
#ifdef HAVE_SIGPROCMASK
209
    sigprocmask( SIG_SETMASK, &old_set, NULL );
210
#endif
211 212 213
    return tmp;
}

214
static const strarray* get_translator(enum processor processor)
215
{
216
    static strarray* cpp = 0;
217
    static strarray* as = 0;
218 219 220
    static strarray* cc = 0;
    static strarray* cxx = 0;

221
    switch(processor)
222
    {
223 224 225 226 227 228 229 230 231
        case proc_cpp: 
	    if (!cpp) cpp = strarray_fromstring(CPP, " ");
	    return cpp;
        case proc_cc:  
	    if (!cc) cc = strarray_fromstring(CC, " ");
	    return cc;
        case proc_cxx: 
	    if (!cxx) cxx = strarray_fromstring(CXX, " ");
	    return cxx;
232 233 234
        case proc_as:
	    if (!as) as = strarray_fromstring(AS, " ");
	    return as;
235
    }
236
    error("Unknown processor\n");
237 238
}

239
static void compile(struct options* opts, const char* lang)
240
{
241
    strarray* comp_args = strarray_alloc();
242 243
    unsigned int j;
    int gcc_defs = 0;
244

245 246
    switch(opts->processor)
    {
247
	case proc_cpp:  gcc_defs = 1; break;
248 249 250 251
#ifdef __GNUC__
	/* Note: if the C compiler is gcc we assume the C++ compiler is too */
	/* mixing different C and C++ compilers isn't supported in configure anyway */
	case proc_cc:  gcc_defs = 1; break;
252
	case proc_cxx: gcc_defs = 1; break;
253 254
#else
	case proc_cc:  gcc_defs = 0; break;
255
	case proc_cxx: gcc_defs = 0; break;
256
#endif
257
	case proc_as:  gcc_defs = 0; break;
258
    }
259
    strarray_addall(comp_args, get_translator(opts->processor));
260

261
    if (opts->processor != proc_cpp)
262
    {
263
#ifdef CC_FLAG_SHORT_WCHAR
264
	if (!opts->wine_objdir && !opts->noshortwchar)
265 266 267 268 269 270
	{
            strarray_add(comp_args, CC_FLAG_SHORT_WCHAR);
            strarray_add(comp_args, "-DWINE_UNICODE_NATIVE");
	}
#endif
        strarray_addall(comp_args, strarray_fromstring(DLLFLAGS, " "));
271
    }
272

273 274 275 276 277 278
#ifdef _WIN64
    strarray_add(comp_args, "-DWIN64");
    strarray_add(comp_args, "-D_WIN64");
    strarray_add(comp_args, "-D__WIN64");
    strarray_add(comp_args, "-D__WIN64__");
#else
279 280 281 282
    strarray_add(comp_args, "-DWIN32");
    strarray_add(comp_args, "-D_WIN32");
    strarray_add(comp_args, "-D__WIN32");
    strarray_add(comp_args, "-D__WIN32__");
283
#endif
284 285 286
    strarray_add(comp_args, "-D__WINNT");
    strarray_add(comp_args, "-D__WINNT__");

287 288
    if (gcc_defs)
    {
289
#ifdef __APPLE__ /* Mac OS X uses a 16-byte aligned stack and not a 4-byte one */
290 291 292 293 294
	strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
	strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
	strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__)) __attribute__((__force_align_arg_pointer__))");
	strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__)) __attribute__((__force_align_arg_pointer__))");
#else
295 296 297 298
	strarray_add(comp_args, "-D__stdcall=__attribute__((__stdcall__))");
	strarray_add(comp_args, "-D__cdecl=__attribute__((__cdecl__))");
	strarray_add(comp_args, "-D_stdcall=__attribute__((__stdcall__))");
	strarray_add(comp_args, "-D_cdecl=__attribute__((__cdecl__))");
299 300 301
#endif

	strarray_add(comp_args, "-D__fastcall=__attribute__((__fastcall__))");
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	strarray_add(comp_args, "-D_fastcall=__attribute__((__fastcall__))");
	strarray_add(comp_args, "-D__declspec(x)=__declspec_##x");
	strarray_add(comp_args, "-D__declspec_align(x)=__attribute__((aligned(x)))");
	strarray_add(comp_args, "-D__declspec_allocate(x)=__attribute__((section(x)))");
	strarray_add(comp_args, "-D__declspec_deprecated=__attribute__((deprecated))");
	strarray_add(comp_args, "-D__declspec_dllimport=__attribute__((dllimport))");
	strarray_add(comp_args, "-D__declspec_dllexport=__attribute__((dllexport))");
	strarray_add(comp_args, "-D__declspec_naked=__attribute__((naked))");
	strarray_add(comp_args, "-D__declspec_noinline=__attribute__((noinline))");
	strarray_add(comp_args, "-D__declspec_noreturn=__attribute__((noreturn))");
	strarray_add(comp_args, "-D__declspec_nothrow=__attribute__((nothrow))");
	strarray_add(comp_args, "-D__declspec_novtable=__attribute__(())"); /* ignore it */
	strarray_add(comp_args, "-D__declspec_selectany=__attribute__((weak))");
	strarray_add(comp_args, "-D__declspec_thread=__thread");
    }
317 318

    /* Wine specific defines */
319 320 321
    strarray_add(comp_args, "-D__WINE__");
    strarray_add(comp_args, "-D__int8=char");
    strarray_add(comp_args, "-D__int16=short");
322
    /* FIXME: what about 64-bit platforms? */
323
    strarray_add(comp_args, "-D__int32=int");
324
#ifdef HAVE_LONG_LONG
325
    strarray_add(comp_args, "-D__int64=long long");
326
#endif
327 328 329

    /* options we handle explicitly */
    if (opts->compile_only)
330
	strarray_add(comp_args, "-c");
331
    if (opts->output_name)
332 333 334 335
    {
	strarray_add(comp_args, "-o");
	strarray_add(comp_args, opts->output_name);
    }
336 337 338

    /* the rest of the pass-through parameters */
    for ( j = 0 ; j < opts->compiler_args->size ; j++ ) 
339
        strarray_add(comp_args, opts->compiler_args->base[j]);
340

341 342 343 344
    /* the language option, if any */
    if (lang && strcmp(lang, "-xnone"))
	strarray_add(comp_args, lang);

345 346
    /* last, but not least, the files */
    for ( j = 0; j < opts->files->size; j++ )
347 348 349 350
    {
	if (opts->files->base[j][0] != '-')
	    strarray_add(comp_args, opts->files->base[j]);
    }
351

352 353 354 355 356 357
    /* standard includes come last in the include search path */
#ifdef __GNUC__
#define SYS_INCLUDE "-isystem"
#else
#define SYS_INCLUDE "-I"
#endif
358
    if (!opts->wine_objdir && !opts->nostdinc)
359 360 361 362 363 364 365 366 367 368
    {
        if (opts->use_msvcrt)
        {
            strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/msvcrt");
            strarray_add(comp_args, "-D__MSVCRT__");
        }
        strarray_add(comp_args, SYS_INCLUDE INCLUDEDIR "/windows");
    }
#undef SYS_INCLUDE

369
    spawn(opts->prefix, comp_args, 0);
370 371
}

372
static const char* compile_to_object(struct options* opts, const char* file, const char* lang)
373 374 375
{
    struct options copts;
    char* base_name;
376

377
    /* make a copy so we don't change any of the initial stuff */
378 379 380 381 382 383 384
    /* a shallow copy is exactly what we want in this case */
    base_name = get_basename(file);
    copts = *opts;
    copts.output_name = get_temp_file(base_name, ".o");
    copts.compile_only = 1;
    copts.files = strarray_alloc();
    strarray_add(copts.files, file);
385
    compile(&copts, lang);
386 387 388 389 390 391
    strarray_free(copts.files);
    free(base_name);

    return copts.output_name;
}

392 393 394 395 396 397 398 399 400
/* check if there is a static lib associated to a given dll */
static char *find_static_lib( const char *dll )
{
    char *lib = strmake("%s.a", dll);
    if (get_file_type(lib) == file_arh) return lib;
    free( lib );
    return NULL;
}

401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
/* add specified library to the list of files */
static void add_library( strarray *lib_dirs, strarray *files, const char *library )
{
    char *static_lib, *fullname = 0;

    switch(get_lib_type(lib_dirs, library, &fullname))
    {
    case file_arh:
        strarray_add(files, strmake("-a%s", fullname));
        break;
    case file_dll:
        strarray_add(files, strmake("-d%s", fullname));
        if ((static_lib = find_static_lib(fullname)))
        {
            strarray_add(files, strmake("-a%s",static_lib));
            free(static_lib);
        }
        break;
    case file_so:
    default:
        /* keep it anyway, the linker may know what to do with it */
        strarray_add(files, strmake("-l%s", library));
        break;
    }
    free(fullname);
}

428
static void build(struct options* opts)
429
{
430
    static const char *stdlibpath[] = { DLLDIR, LIBDIR, "/usr/lib", "/usr/local/lib", "/lib" };
431
    strarray *lib_dirs, *files;
432
    strarray *spec_args, *link_args;
433
    char *output_file;
434
    const char *spec_o_name;
435
    const char *output_name, *spec_file, *lang;
436
    const char* winebuild = getenv("WINEBUILD");
437
    int generate_app_loader = 1;
438
    unsigned int j;
439

440 441 442 443 444 445 446
    /* NOTE: for the files array we'll use the following convention:
     *    -axxx:  xxx is an archive (.a)
     *    -dxxx:  xxx is a DLL (.def)
     *    -lxxx:  xxx is an unsorted library
     *    -oxxx:  xxx is an object (.o)
     *    -rxxx:  xxx is a resource (.res)
     *    -sxxx:  xxx is a shared lib (.so)
447
     *    -xlll:  lll is the language (c, c++, etc.)
448
     */
449

450
    if (!winebuild) winebuild = "winebuild";
451

452
    output_file = strdup( opts->output_name ? opts->output_name : "a.out" );
453

454
    /* 'winegcc -o app xxx.exe.so' only creates the load script */
455
    if (opts->files->size == 1 && strendswith(opts->files->base[0], ".exe.so"))
456
    {
457
	create_file(output_file, 0755, app_loader_template, opts->files->base[0]);
458 459
	return;
    }
460

461 462 463 464 465 466 467
    /* generate app loader only for .exe */
    if (opts->shared || strendswith(output_file, ".exe.so"))
	generate_app_loader = 0;

    /* normalize the filename a bit: strip .so, ensure it has proper ext */
    if (strendswith(output_file, ".so")) 
	output_file[strlen(output_file) - 3] = 0;
468 469 470 471 472 473 474 475 476
    if (opts->shared)
    {
	if ((output_name = strrchr(output_file, '/'))) output_name++;
	else output_name = output_file;
	if (!strchr(output_name, '.'))
	    output_file = strmake("%s.dll", output_file);
    }
    else if (!strendswith(output_file, ".exe"))
	output_file = strmake("%s.exe", output_file);
477

478
    /* get the filename from the path */
479 480 481
    if ((output_name = strrchr(output_file, '/'))) output_name++;
    else output_name = output_file;

482
    /* prepare the linking path */
483
    if (!opts->wine_objdir)
484
    {
485
        lib_dirs = strarray_dup(opts->lib_dirs);
486
	for ( j = 0; j < sizeof(stdlibpath)/sizeof(stdlibpath[0]); j++ )
487 488
	    strarray_add(lib_dirs, stdlibpath[j]);
    }
489 490
    else
    {
491
        lib_dirs = strarray_alloc();
492 493
        strarray_add(lib_dirs, strmake("%s/dlls", opts->wine_objdir));
        strarray_add(lib_dirs, strmake("%s/libs/wine", opts->wine_objdir));
494
        strarray_addall(lib_dirs, opts->lib_dirs);
495
    }
496

497
    /* mark the files with their appropriate type */
498
    spec_file = lang = 0;
499 500
    files = strarray_alloc();
    for ( j = 0; j < opts->files->size; j++ )
501
    {
502 503
	const char* file = opts->files->base[j];
	if (file[0] != '-')
504
	{
505 506
	    switch(get_file_type(file))
	    {
507 508 509
		case file_def:
		case file_spec:
		    if (spec_file)
510
			error("Only one spec file can be specified\n");
511 512
		    spec_file = file;
		    break;
513 514
		case file_rc:
		    /* FIXME: invoke wrc to build it */
515
		    error("Can't compile .rc file at the moment: %s\n", file);
516 517 518 519 520 521 522
	            break;
	    	case file_res:
		    strarray_add(files, strmake("-r%s", file));
		    break;
		case file_obj:
		    strarray_add(files, strmake("-o%s", file));
		    break;
523 524 525 526 527 528
		case file_arh:
		    strarray_add(files, strmake("-a%s", file));
		    break;
		case file_so:
		    strarray_add(files, strmake("-s%s", file));
		    break;
529
	    	case file_na:
530
		    error("File does not exist: %s\n", file);
531 532
		    break;
	        default:
533
		    file = compile_to_object(opts, file, lang);
534 535 536 537 538
		    strarray_add(files, strmake("-o%s", file));
		    break;
	    }
	}
	else if (file[1] == 'l')
539
            add_library( lib_dirs, files, file + 2 );
540 541
	else if (file[1] == 'x')
	    lang = file;
542
    }
543
    if (opts->shared && !spec_file)
544
	error("A spec file is currently needed in shared mode\n");
545

546
    /* add the default libraries, if needed */
547
    if (!opts->nostdlib && opts->use_msvcrt) add_library(lib_dirs, files, "msvcrt");
548

549
    if (!opts->wine_objdir && !opts->nodefaultlibs) 
550
    {
551 552
        if (opts->gui_app) 
	{
553 554 555
	    add_library(lib_dirs, files, "shell32");
	    add_library(lib_dirs, files, "comdlg32");
	    add_library(lib_dirs, files, "gdi32");
556
	}
557 558 559
        add_library(lib_dirs, files, "advapi32");
        add_library(lib_dirs, files, "user32");
        add_library(lib_dirs, files, "kernel32");
560 561
    }

562 563
    if (!opts->nostartfiles) add_library(lib_dirs, files, "winecrt0");
    if (!opts->nostdlib) add_library(lib_dirs, files, "wine");
564

565
    /* run winebuild to generate the .spec.o file */
566
    spec_args = strarray_alloc();
567
    spec_o_name = get_temp_file(output_name, ".spec.o");
568
    strarray_add(spec_args, winebuild);
569 570
    if (verbose) strarray_add(spec_args, "-v");
    if (keep_generated) strarray_add(spec_args, "--save-temps");
571 572
    strarray_add(spec_args, "--as-cmd");
    strarray_add(spec_args, AS);
573 574
    strarray_add(spec_args, "--ld-cmd");
    strarray_add(spec_args, LD);
575
    strarray_addall(spec_args, strarray_fromstring(DLLFLAGS, " "));
576
    strarray_add(spec_args, opts->shared ? "--dll" : "--exe");
577
    strarray_add(spec_args, "-o");
578
    strarray_add(spec_args, spec_o_name);
579
    if (spec_file)
580
    {
581
        strarray_add(spec_args, "-E");
582 583
        strarray_add(spec_args, spec_file);
    }
584 585

    if (!opts->shared)
586
    {
587
        strarray_add(spec_args, "-F");
588
        strarray_add(spec_args, output_name);
589 590
        strarray_add(spec_args, "--subsystem");
        strarray_add(spec_args, opts->gui_app ? "windows" : "console");
591 592 593
        if (opts->unicode_app)
        {
            strarray_add(spec_args, "--entry");
594
            strarray_add(spec_args, "__wine_spec_exe_wentry");
595
        }
596
    }
597

598 599
    for ( j = 0; j < lib_dirs->size; j++ )
	strarray_add(spec_args, strmake("-L%s", lib_dirs->base[j]));
600

601 602 603
    for ( j = 0 ; j < opts->winebuild_args->size ; j++ )
        strarray_add(spec_args, opts->winebuild_args->base[j]);

604 605 606 607 608 609 610 611
    for ( j = 0; j < files->size; j++ )
    {
	const char* name = files->base[j] + 2;
	switch(files->base[j][1])
	{
	    case 'r':
		strarray_add(spec_args, files->base[j]);
		break;
612
	    case 'd':
613 614 615 616 617 618
	    case 'a':
	    case 'o':
		strarray_add(spec_args, name);
		break;
	}
    }
619

620
    spawn(opts->prefix, spec_args, 0);
621 622 623

    /* link everything together now */
    link_args = strarray_alloc();
624
    strarray_addall(link_args, get_translator(opts->processor));
625
    strarray_addall(link_args, strarray_fromstring(LDDLLFLAGS, " "));
626

627
    strarray_add(link_args, "-o");
628
    strarray_add(link_args, strmake("%s.so", output_file));
629 630 631 632

    for ( j = 0 ; j < opts->linker_args->size ; j++ ) 
        strarray_add(link_args, opts->linker_args->base[j]);

633 634 635 636 637 638 639 640
#ifdef __APPLE__
    if (opts->image_base)
    {
        strarray_add(link_args, "-image_base");
        strarray_add(link_args, opts->image_base);
    }
#endif

641 642
    for ( j = 0; j < lib_dirs->size; j++ )
	strarray_add(link_args, strmake("-L%s", lib_dirs->base[j]));
643

644
    strarray_add(link_args, spec_o_name);
645

646 647 648 649 650
    for ( j = 0; j < files->size; j++ )
    {
	const char* name = files->base[j] + 2;
	switch(files->base[j][1])
	{
651
	    case 'l':
652
	    case 's':
653 654 655 656 657 658 659 660
		strarray_add(link_args, strmake("-l%s", name));
		break;
	    case 'a':
	    case 'o':
		strarray_add(link_args, name);
		break;
	}
    }
661

662 663 664 665 666
    if (!opts->nostdlib) 
    {
	strarray_add(link_args, "-lm");
	strarray_add(link_args, "-lc");
    }
667

668
    spawn(opts->prefix, link_args, 0);
669

670 671 672 673 674 675 676 677 678 679 680
    /* set the base address */
    if (opts->image_base)
    {
        const char *prelink = PRELINK;
        if (prelink[0] && strcmp(prelink,"false"))
        {
            strarray *prelink_args = strarray_alloc();
            strarray_add(prelink_args, prelink);
            strarray_add(prelink_args, "--reloc-only");
            strarray_add(prelink_args, opts->image_base);
            strarray_add(prelink_args, strmake("%s.so", output_file));
681
            spawn(opts->prefix, prelink_args, 1);
682 683 684 685
            strarray_free(prelink_args);
        }
    }

686
    /* create the loader script */
687
    if (generate_app_loader)
688 689
    {
        if (strendswith(output_file, ".exe")) output_file[strlen(output_file) - 4] = 0;
690
        create_file(output_file, 0755, app_loader_template, strmake("%s.exe.so", output_name));
691
    }
692 693 694
}


695
static void forward(int argc, char **argv, struct options* opts)
696
{
697
    strarray* args = strarray_alloc();
698 699
    int j;

700
    strarray_addall(args, get_translator(opts->processor));
701 702

    for( j = 1; j < argc; j++ ) 
703
	strarray_add(args, argv[j]);
704

705
    spawn(opts->prefix, args, 0);
706 707 708 709 710 711 712
}

/*
 *      Linker Options
 *          object-file-name  -llibrary -nostartfiles  -nodefaultlibs
 *          -nostdlib -s  -static  -static-libgcc  -shared  -shared-libgcc
 *          -symbolic -Wl,option  -Xlinker option -u symbol
713
 *	    -framework name
714 715 716 717 718 719
 */
static int is_linker_arg(const char* arg)
{
    static const char* link_switches[] = 
    {
	"-nostartfiles", "-nodefaultlibs", "-nostdlib", "-s", 
720 721
	"-static", "-static-libgcc", "-shared", "-shared-libgcc", "-symbolic",
	"-framework"
722
    };
723
    unsigned int j;
724 725 726

    switch (arg[1]) 
    {
727 728 729
	case 'R':
	case 'z':
	case 'l':
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	case 'u':
	    return 1;
        case 'W':
            if (strncmp("-Wl,", arg, 4) == 0) return 1;
	    break;
	case 'X':
	    if (strcmp("-Xlinker", arg) == 0) return 1;
	    break;
    }

    for (j = 0; j < sizeof(link_switches)/sizeof(link_switches[0]); j++)
	if (strcmp(link_switches[j], arg) == 0) return 1;

    return 0;
}

/*
 *      Target Options
 *          -b machine  -V version
 */
static int is_target_arg(const char* arg)
{
    return arg[1] == 'b' || arg[2] == 'V';
}


/*
 *      Directory Options
 *          -Bprefix  -Idir  -I-  -Ldir  -specs=file
 */
static int is_directory_arg(const char* arg)
{
    return arg[1] == 'B' || arg[1] == 'L' || arg[1] == 'I' || strncmp("-specs=", arg, 7) == 0;
}

/*
 *      MinGW Options
767
 *	    -mno-cygwin -mwindows -mconsole -mthreads -municode
768 769 770 771 772
 */ 
static int is_mingw_arg(const char* arg)
{
    static const char* mingw_switches[] = 
    {
773
        "-mno-cygwin", "-mwindows", "-mconsole", "-mthreads", "-municode"
774
    };
775
    unsigned int j;
776 777 778 779 780 781

    for (j = 0; j < sizeof(mingw_switches)/sizeof(mingw_switches[0]); j++)
	if (strcmp(mingw_switches[j], arg) == 0) return 1;

    return 0;
}
782

783 784
int main(int argc, char **argv)
{
785 786 787 788
    int i, c, next_is_arg = 0, linking = 1;
    int raw_compiler_arg, raw_linker_arg;
    const char* option_arg;
    struct options opts;
789
    char* lang = 0;
790
    char* str;
791

792
#ifdef SIGHUP
793
    signal( SIGHUP, exit_on_signal );
794
#endif
795 796
    signal( SIGTERM, exit_on_signal );
    signal( SIGINT, exit_on_signal );
797
#ifdef HAVE_SIGADDSET
798 799 800 801
    sigemptyset( &signal_mask );
    sigaddset( &signal_mask, SIGHUP );
    sigaddset( &signal_mask, SIGTERM );
    sigaddset( &signal_mask, SIGINT );
802
#endif
803

804
    /* setup tmp file removal at exit */
805
    tmp_files = strarray_alloc();
806 807
    atexit(clean_temp_files);
    
808 809 810 811 812 813
    /* initialize options */
    memset(&opts, 0, sizeof(opts));
    opts.lib_dirs = strarray_alloc();
    opts.files = strarray_alloc();
    opts.linker_args = strarray_alloc();
    opts.compiler_args = strarray_alloc();
814
    opts.winebuild_args = strarray_alloc();
815 816

    /* determine the processor type */
817 818
    if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp;
    else if (strendswith(argv[0], "++")) opts.processor = proc_cxx;
819
    
820
    /* parse options */
821 822 823 824
    for ( i = 1 ; i < argc ; i++ ) 
    {
        if (argv[i][0] == '-')  /* option */
	{
825
	    /* determine if tihs switch is followed by a separate argument */
826
	    next_is_arg = 0;
827 828 829 830 831
	    option_arg = 0;
	    switch(argv[i][1])
	    {
		case 'x': case 'o': case 'D': case 'U':
		case 'I': case 'A': case 'l': case 'u':
832
		case 'b': case 'V': case 'G': case 'L':
833
		case 'B': case 'R': case 'z':
834
		    if (argv[i][2]) option_arg = &argv[i][2];
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
		    else next_is_arg = 1;
		    break;
		case 'i':
		    next_is_arg = 1;
		    break;
		case 'a':
		    if (strcmp("-aux-info", argv[i]) == 0)
			next_is_arg = 1;
		    break;
		case 'X':
		    if (strcmp("-Xlinker", argv[i]) == 0)
			next_is_arg = 1;
		    break;
		case 'M':
		    c = argv[i][2];
		    if (c == 'F' || c == 'T' || c == 'Q')
		    {
852
			if (argv[i][3]) option_arg = &argv[i][3];
853 854 855
			else next_is_arg = 1;
		    }
		    break;
856 857 858 859
		case 'f':
		    if (strcmp("-framework", argv[i]) == 0)
			next_is_arg = 1;
		    break;
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
	    }
	    if (next_is_arg) option_arg = argv[i+1];

	    /* determine what options go 'as is' to the linker & the compiler */
	    raw_compiler_arg = raw_linker_arg = 0;
	    if (is_linker_arg(argv[i])) 
	    {
		raw_linker_arg = 1;
	    }
	    else 
	    {
		if (is_directory_arg(argv[i]) || is_target_arg(argv[i]))
		    raw_linker_arg = 1;
		raw_compiler_arg = !is_mingw_arg(argv[i]);
	    }

	    /* these things we handle explicitly so we don't pass them 'as is' */
	    if (argv[i][1] == 'l' || argv[i][1] == 'I' || argv[i][1] == 'L')
		raw_linker_arg = 0;
	    if (argv[i][1] == 'c' || argv[i][1] == 'L')
		raw_compiler_arg = 0;
	    if (argv[i][1] == 'o')
		raw_compiler_arg = raw_linker_arg = 0;

	    /* do a bit of semantic analysis */
885 886
            switch (argv[i][1]) 
	    {
887 888
		case 'B':
		    str = strdup(option_arg);
889 890
		    if (strendswith(str, "/tools/winebuild"))
                    {
891 892 893
                        char *objdir = strdup(str);
                        objdir[strlen(objdir) - sizeof("/tools/winebuild") + 1] = 0;
                        opts.wine_objdir = objdir;
894 895 896
                        /* don't pass it to the compiler, this generates warnings */
                        raw_compiler_arg = raw_linker_arg = 0;
                    }
897
		    if (strendswith(str, "/")) str[strlen(str) - 1] = 0;
898 899
                    if (!opts.prefix) opts.prefix = strarray_alloc();
                    strarray_add(opts.prefix, str);
900
		    break;
901
                case 'c':        /* compile or assemble */
902 903
		    if (argv[i][2] == 0) opts.compile_only = 1;
		    /* fall through */
904 905 906 907
                case 'S':        /* generate assembler code */
                case 'E':        /* preprocess only */
                    if (argv[i][2] == 0) linking = 0;
                    break;
908 909 910 911
		case 'f':
		    if (strcmp("-fno-short-wchar", argv[i]) == 0)
                        opts.noshortwchar = 1;
		    break;
912
		case 'l':
913
		    strarray_add(opts.files, strmake("-l%s", option_arg));
914 915 916 917
		    break;
		case 'L':
		    strarray_add(opts.lib_dirs, option_arg);
		    break;
918 919 920
                case 'M':        /* map file generation */
                    linking = 0;
                    break;
921 922
		case 'm':
		    if (strcmp("-mno-cygwin", argv[i]) == 0)
923
			opts.use_msvcrt = 1;
924
		    else if (strcmp("-mwindows", argv[i]) == 0)
925
			opts.gui_app = 1;
Richard Cohen's avatar
Richard Cohen committed
926
		    else if (strcmp("-mconsole", argv[i]) == 0)
927
			opts.gui_app = 0;
928 929
		    else if (strcmp("-municode", argv[i]) == 0)
			opts.unicode_app = 1;
930 931
		    else if (strcmp("-m32", argv[i]) == 0 || strcmp("-m64", argv[i]) == 0)
			raw_linker_arg = 1;
932 933 934
		    break;
                case 'n':
                    if (strcmp("-nostdinc", argv[i]) == 0)
935
                        opts.nostdinc = 1;
936
                    else if (strcmp("-nodefaultlibs", argv[i]) == 0)
937
                        opts.nodefaultlibs = 1;
938
                    else if (strcmp("-nostdlib", argv[i]) == 0)
939
                        opts.nostdlib = 1;
940 941
                    else if (strcmp("-nostartfiles", argv[i]) == 0)
                        opts.nostartfiles = 1;
942
                    break;
943 944 945
		case 'o':
		    opts.output_name = option_arg;
		    break;
946
                case 's':
947 948
                    if (strcmp("-static", argv[i]) == 0) 
			linking = -1;
949 950
		    else if(strcmp("-save-temps", argv[i]) == 0)
			keep_generated = 1;
951 952 953 954 955
		    else if(strcmp("-shared", argv[i]) == 0)
		    {
			opts.shared = 1;
                        raw_compiler_arg = raw_linker_arg = 0;
		    }
956
                    break;
957
                case 'v':
958
                    if (argv[i][2] == 0) verbose++;
959
                    break;
960 961 962
                case 'W':
                    if (strncmp("-Wl,", argv[i], 4) == 0)
		    {
963 964 965 966 967 968 969 970 971 972 973 974 975 976
                        unsigned int j;
                        strarray* Wl = strarray_fromstring(argv[i] + 4, ",");
                        for (j = 0; j < Wl->size; j++)
                        {
                            if (!strcmp(Wl->base[j], "--image-base") && j < Wl->size - 1)
                            {
                                opts.image_base = strdup( Wl->base[++j] );
                                continue;
                            }
                            if (!strcmp(Wl->base[j], "-static")) linking = -1;
                            strarray_add(opts.linker_args, strmake("-Wl,%s",Wl->base[j]));
                        }
                        strarray_free(Wl);
                        raw_compiler_arg = raw_linker_arg = 0;
977
                    }
978 979 980 981 982
		    else if (strncmp("-Wb,", argv[i], 4) == 0)
		    {
			strarray* Wb = strarray_fromstring(argv[i] + 4, ",");
			strarray_addall(opts.winebuild_args, Wb);
			strarray_free(Wb);
983 984
                        /* don't pass it to the compiler, it generates errors */
                        raw_compiler_arg = raw_linker_arg = 0;
985
		    }
986
                    break;
987 988 989
		case 'x':
		    lang = strmake("-x%s", option_arg);
		    strarray_add(opts.files, lang);
Austin English's avatar
Austin English committed
990
		    /* we'll pass these flags ourselves, explicitly */
991 992
                    raw_compiler_arg = raw_linker_arg = 0;
		    break;
993 994
                case '-':
                    if (strcmp("-static", argv[i]+1) == 0)
995
                        linking = -1;
996 997
                    break;
            }
998

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	    /* put the arg into the appropriate bucket */
	    if (raw_linker_arg) 
	    {
		strarray_add(opts.linker_args, argv[i]);
		if (next_is_arg && (i + 1 < argc)) 
		    strarray_add(opts.linker_args, argv[i + 1]);
	    }
	    if (raw_compiler_arg)
	    {
		strarray_add(opts.compiler_args, argv[i]);
		if (next_is_arg && (i + 1 < argc))
		    strarray_add(opts.compiler_args, argv[i + 1]);
	    }

1013 1014 1015
	    /* skip the next token if it's an argument */
	    if (next_is_arg) i++;
        }
1016 1017
	else
	{
1018 1019
	    strarray_add(opts.files, argv[i]);
	} 
1020 1021
    }

1022
    if (opts.processor == proc_cpp) linking = 0;
1023
    if (linking == -1) error("Static linking is not supported\n");
1024

1025 1026
    if (opts.files->size == 0) forward(argc, argv, &opts);
    else if (linking) build(&opts);
1027
    else compile(&opts, lang);
1028

1029
    return 0;
1030
}