main.c 22.1 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Main function
 *
 * Copyright 1993 Robert J. Amstadt
 * Copyright 1995 Martin von Loewis
 * Copyright 1995, 1996, 1997 Alexandre Julliard
 * Copyright 1997 Eric Youngdale
 * Copyright 1999 Ulrich Weigand
9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24
 */

25
#include "config.h"
26
#include "wine/port.h"
27

28 29
#include <assert.h>
#include <stdio.h>
30
#include <signal.h>
31
#include <errno.h>
32
#include <string.h>
33
#include <stdarg.h>
Marcus Meissner's avatar
Marcus Meissner committed
34
#include <ctype.h>
35 36 37
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
38 39 40 41

#include "build.h"

int UsePIC = 0;
42
int nb_errors = 0;
43
int display_warnings = 0;
44
int kill_at = 0;
45
int verbose = 0;
46
int link_ext_symbols = 0;
47
int force_pointer_size = 0;
48
int unwind_tables = 0;
49
int unix_lib = 0;
50

51
#ifdef __i386__
52
enum target_cpu target_cpu = CPU_x86;
53 54
#elif defined(__x86_64__)
enum target_cpu target_cpu = CPU_x86_64;
55 56
#elif defined(__powerpc__)
enum target_cpu target_cpu = CPU_POWERPC;
57 58
#elif defined(__arm__)
enum target_cpu target_cpu = CPU_ARM;
59 60
#elif defined(__aarch64__)
enum target_cpu target_cpu = CPU_ARM64;
61
#else
62 63 64 65 66
#error Unsupported CPU
#endif

#ifdef __APPLE__
enum target_platform target_platform = PLATFORM_APPLE;
67
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
68
enum target_platform target_platform = PLATFORM_FREEBSD;
69 70
#elif defined(__sun)
enum target_platform target_platform = PLATFORM_SOLARIS;
71
#elif defined(_WIN32)
72 73 74
enum target_platform target_platform = PLATFORM_WINDOWS;
#else
enum target_platform target_platform = PLATFORM_UNSPECIFIED;
75 76
#endif

77
char *target_alias = NULL;
78

79
char *input_file_name = NULL;
80
char *spec_file_name = NULL;
81
FILE *output_file = NULL;
82
const char *output_file_name = NULL;
83
static int fake_module;
84

85
struct strarray lib_path = { 0 };
86
struct strarray tools_path = { 0 };
87 88 89 90
struct strarray as_command = { 0 };
struct strarray cc_command = { 0 };
struct strarray ld_command = { 0 };
struct strarray nm_command = { 0 };
91
char *cpu_option = NULL;
92
char *fpu_option = NULL;
93
char *arch_option = NULL;
94 95 96 97 98
#ifdef __SOFTFP__
const char *float_abi_option = "soft";
#else
const char *float_abi_option = "softfp";
#endif
99

100 101 102 103 104 105
#ifdef __thumb__
int thumb_mode = 1;
#else
int thumb_mode = 0;
#endif

106
static struct strarray res_files;
107 108

/* execution mode */
109
enum exec_mode_values
110 111
{
    MODE_NONE,
112
    MODE_DLL,
113
    MODE_EXE,
114
    MODE_DEF,
115
    MODE_IMPLIB,
116
    MODE_RESOURCES
117 118 119
};

static enum exec_mode_values exec_mode = MODE_NONE;
120

121 122 123 124 125 126 127 128
static const struct
{
    const char *name;
    enum target_platform platform;
} platform_names[] =
{
    { "macos",   PLATFORM_APPLE },
    { "darwin",  PLATFORM_APPLE },
129
    { "freebsd", PLATFORM_FREEBSD },
130
    { "solaris", PLATFORM_SOLARIS },
131
    { "mingw32", PLATFORM_WINDOWS },
132 133 134 135
    { "windows", PLATFORM_WINDOWS },
    { "winnt",   PLATFORM_WINDOWS }
};

136
/* set the dll file name from the input file name */
137
static void set_dll_file_name( const char *name, DLLSPEC *spec )
138 139 140
{
    char *p;

141
    if (spec->file_name) return;
142

143
    if ((p = strrchr( name, '\\' ))) name = p + 1;
144
    if ((p = strrchr( name, '/' ))) name = p + 1;
145 146
    spec->file_name = xmalloc( strlen(name) + 5 );
    strcpy( spec->file_name, name );
147 148 149 150
    if ((p = strrchr( spec->file_name, '.' )))
    {
        if (!strcmp( p, ".spec" ) || !strcmp( p, ".def" )) *p = 0;
    }
151 152
}

153 154 155
/* set the dll name from the file name */
static void init_dll_name( DLLSPEC *spec )
{
156
    if (!spec->file_name && output_file_name)
157 158 159 160 161
    {
        char *p;
        spec->file_name = xstrdup( output_file_name );
        if ((p = strrchr( spec->file_name, '.' ))) *p = 0;
    }
162
    if (!spec->dll_name && spec->file_name)  /* set default name from file name */
163 164 165 166 167
    {
        char *p;
        spec->dll_name = xstrdup( spec->file_name );
        if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
    }
168
    if (spec->dll_name) spec->c_name = make_c_identifier( spec->dll_name );
169 170
}

171 172 173 174 175 176 177 178 179
/* set the dll subsystem */
static void set_subsystem( const char *subsystem, DLLSPEC *spec )
{
    char *major, *minor, *str = xstrdup( subsystem );

    if ((major = strchr( str, ':' ))) *major++ = 0;
    if (!strcmp( str, "native" )) spec->subsystem = IMAGE_SUBSYSTEM_NATIVE;
    else if (!strcmp( str, "windows" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
    else if (!strcmp( str, "console" )) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
180
    else if (!strcmp( str, "wince" ))   spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_CE_GUI;
181
    else if (!strcmp( str, "win16" )) spec->type = SPEC_WIN16;
182 183 184 185 186 187 188 189 190 191 192 193 194
    else fatal_error( "Invalid subsystem name '%s'\n", subsystem );
    if (major)
    {
        if ((minor = strchr( major, '.' )))
        {
            *minor++ = 0;
            spec->subsystem_minor = atoi( minor );
        }
        spec->subsystem_major = atoi( major );
    }
    free( str );
}

195 196 197 198 199 200 201 202
/* set the target CPU and platform */
static void set_target( const char *target )
{
    unsigned int i;
    char *p, *platform, *spec = xstrdup( target );

    /* target specification is in the form CPU-MANUFACTURER-OS or CPU-MANUFACTURER-KERNEL-OS */

203 204
    target_alias = xstrdup( target );

205 206
    /* get the CPU part */

207 208
    if ((p = strchr( spec, '-' )))
    {
209 210
        int cpu;

211
        *p++ = 0;
212 213 214
        cpu = get_cpu_from_name( spec );
        if (cpu == -1) fatal_error( "Unrecognized CPU '%s'\n", spec );
        target_cpu = cpu;
215 216 217 218 219 220 221 222 223 224
        platform = p;
        if ((p = strrchr( p, '-' ))) platform = p + 1;
    }
    else if (!strcmp( spec, "mingw32" ))
    {
        target_cpu = CPU_x86;
        platform = spec;
    }
    else
        fatal_error( "Invalid target specification '%s'\n", target );
225 226 227 228

    /* get the OS part */

    target_platform = PLATFORM_UNSPECIFIED;  /* default value */
229
    for (i = 0; i < ARRAY_SIZE(platform_names); i++)
230 231 232 233 234 235 236 237 238 239 240
    {
        if (!strncmp( platform_names[i].name, platform, strlen(platform_names[i].name) ))
        {
            target_platform = platform_names[i].platform;
            break;
        }
    }

    free( spec );
}

241 242 243 244 245 246
/* cleanup on program exit */
static void cleanup(void)
{
    if (output_file_name) unlink( output_file_name );
}

247 248 249 250 251
/* clean things up when aborting on a signal */
static void exit_on_signal( int sig )
{
    exit(1);  /* this will call atexit functions */
}
252 253 254 255

/*******************************************************************
 *         command-line option handling
 */
256 257 258
static const char usage_str[] =
"Usage: winebuild [OPTIONS] [FILES]\n\n"
"Options:\n"
259 260
"       --as-cmd=AS           Command to use for assembling (default: as)\n"
"   -b, --target=TARGET       Specify target CPU and platform for cross-compiling\n"
261
"   -B PREFIX                 Look for build tools in the PREFIX directory\n"
262
"       --cc-cmd=CC           C compiler to use for assembling (default: fall back to --as-cmd)\n"
263 264 265 266 267
"   -d, --delay-lib=LIB       Import the specified library in delayed mode\n"
"   -D SYM                    Ignored for C flags compatibility\n"
"   -e, --entry=FUNC          Set the DLL entry point function (default: DllMain)\n"
"   -E, --export=FILE         Export the symbols defined in the .spec or .def file\n"
"       --external-symbols    Allow linking to external symbols\n"
268
"   -f FLAGS                  Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n"
269
"   -F, --filename=DLLFILE    Set the DLL filename (default: from input file name)\n"
270
"       --fake-module         Create a fake binary module\n"
271 272 273 274 275 276 277 278 279
"   -h, --help                Display this help message\n"
"   -H, --heap=SIZE           Set the heap size for a Win16 dll\n"
"   -I DIR                    Ignored for C flags compatibility\n"
"   -k, --kill-at             Kill stdcall decorations in generated .def files\n"
"   -K, FLAGS                 Compiler flags (only -KPIC is supported)\n"
"       --large-address-aware Support an address space larger than 2Gb\n"
"       --ld-cmd=LD           Command to use for linking (default: ld)\n"
"   -l, --library=LIB         Import the specified library\n"
"   -L, --library-path=DIR    Look for imports libraries in DIR\n"
280
"   -m16, -m32, -m64          Force building 16-bit, 32-bit resp. 64-bit code\n"
281 282 283 284 285 286 287
"   -M, --main-module=MODULE  Set the name of the main module for a Win16 dll\n"
"       --nm-cmd=NM           Command to use to get undefined symbols (default: nm)\n"
"       --nxcompat=y|n        Set the NX compatibility flag (default: yes)\n"
"   -N, --dll-name=DLLNAME    Set the DLL name (default: from input file name)\n"
"   -o, --output=NAME         Set the output file name (default: stdout)\n"
"   -r, --res=RSRC.RES        Load resources from RSRC.RES\n"
"       --save-temps          Do not delete the generated intermediate files\n"
288
"       --subsystem=SUBSYS    Set the subsystem (one of native, windows, console, wince)\n"
289 290 291 292
"   -u, --undefined=SYMBOL    Add an undefined reference to SYMBOL when linking\n"
"   -v, --verbose             Display the programs invoked\n"
"       --version             Print the version and exit\n"
"   -w, --warnings            Turn on warnings\n"
293
"\nMode options:\n"
294 295 296
"       --dll                 Build a .c file from a .spec or .def file\n"
"       --def                 Build a .def file from a .spec file\n"
"       --exe                 Build a .c file for an executable\n"
297
"       --implib              Build an import library\n"
298
"       --resources           Build a .o file for the resource files\n\n"
299 300 301 302
"The mode options are mutually exclusive; you must specify one and only one.\n\n";

enum long_options_values
{
303
    LONG_OPT_DLL = 1,
304 305
    LONG_OPT_DEF,
    LONG_OPT_EXE,
306
    LONG_OPT_IMPLIB,
307
    LONG_OPT_ASCMD,
308
    LONG_OPT_CCCMD,
309
    LONG_OPT_EXTERNAL_SYMS,
310
    LONG_OPT_FAKE_MODULE,
311
    LONG_OPT_LARGE_ADDRESS_AWARE,
312 313
    LONG_OPT_LDCMD,
    LONG_OPT_NMCMD,
314
    LONG_OPT_NXCOMPAT,
315
    LONG_OPT_RESOURCES,
316
    LONG_OPT_SAVE_TEMPS,
317
    LONG_OPT_SUBSYSTEM,
318
    LONG_OPT_VERSION
319 320
};

321
static const char short_options[] = "B:C:D:E:F:H:I:K:L:M:N:b:d:e:f:hkl:m:o:r:u:vw";
322 323 324

static const struct option long_options[] =
{
325 326 327
    { "dll",           0, 0, LONG_OPT_DLL },
    { "def",           0, 0, LONG_OPT_DEF },
    { "exe",           0, 0, LONG_OPT_EXE },
328
    { "implib",        0, 0, LONG_OPT_IMPLIB },
329
    { "as-cmd",        1, 0, LONG_OPT_ASCMD },
330
    { "cc-cmd",        1, 0, LONG_OPT_CCCMD },
331
    { "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS },
332
    { "fake-module",   0, 0, LONG_OPT_FAKE_MODULE },
333
    { "large-address-aware", 0, 0, LONG_OPT_LARGE_ADDRESS_AWARE },
334 335
    { "ld-cmd",        1, 0, LONG_OPT_LDCMD },
    { "nm-cmd",        1, 0, LONG_OPT_NMCMD },
336
    { "nxcompat",      1, 0, LONG_OPT_NXCOMPAT },
337
    { "resources",     0, 0, LONG_OPT_RESOURCES },
338 339 340
    { "save-temps",    0, 0, LONG_OPT_SAVE_TEMPS },
    { "subsystem",     1, 0, LONG_OPT_SUBSYSTEM },
    { "version",       0, 0, LONG_OPT_VERSION },
341
    /* aliases for short options */
342
    { "target",        1, 0, 'b' },
343
    { "delay-lib",     1, 0, 'd' },
344
    { "export",        1, 0, 'E' },
345 346 347 348 349 350 351 352 353 354 355
    { "entry",         1, 0, 'e' },
    { "filename",      1, 0, 'F' },
    { "help",          0, 0, 'h' },
    { "heap",          1, 0, 'H' },
    { "kill-at",       0, 0, 'k' },
    { "library",       1, 0, 'l' },
    { "library-path",  1, 0, 'L' },
    { "main-module",   1, 0, 'M' },
    { "dll-name",      1, 0, 'N' },
    { "output",        1, 0, 'o' },
    { "res",           1, 0, 'r' },
356
    { "undefined",     1, 0, 'u' },
357
    { "verbose",       0, 0, 'v' },
358 359
    { "warnings",      0, 0, 'w' },
    { NULL,            0, 0, 0 }
360 361
};

362
static void usage( int exit_code )
363
{
364 365
    fprintf( stderr, "%s", usage_str );
    exit( exit_code );
366 367
}

368
static void set_exec_mode( enum exec_mode_values mode )
369
{
370 371
    if (exec_mode != MODE_NONE) usage(1);
    exec_mode = mode;
372 373
}

374 375 376 377 378 379 380 381 382
/* get the default entry point for a given spec file */
static const char *get_default_entry_point( const DLLSPEC *spec )
{
    if (spec->characteristics & IMAGE_FILE_DLL) return "__wine_spec_dll_entry";
    if (spec->subsystem == IMAGE_SUBSYSTEM_NATIVE) return "__wine_spec_drv_entry";
    if (spec->type == SPEC_WIN16) return "__wine_spec_exe16_entry";
    return "__wine_spec_exe_entry";
}

383
/* parse options from the argv array and remove all the recognized ones */
384
static char **parse_options( int argc, char **argv, DLLSPEC *spec )
385
{
386
    char *p;
387
    int optc;
388
    int save_temps = 0;
389

390
    while ((optc = getopt_long( argc, argv, short_options, long_options, NULL )) != -1)
391
    {
392
        switch(optc)
393
        {
394 395 396
        case 'B':
            strarray_add( &tools_path, xstrdup( optarg ), NULL );
            break;
397 398 399
        case 'D':
            /* ignored */
            break;
400 401 402 403
        case 'E':
            spec_file_name = xstrdup( optarg );
            set_dll_file_name( optarg, spec );
            break;
404
        case 'F':
405
            spec->file_name = xstrdup( optarg );
406 407 408 409
            break;
        case 'H':
            if (!isdigit(optarg[0]))
                fatal_error( "Expected number argument with -H option instead of '%s'\n", optarg );
410 411 412
            spec->heap_size = atoi(optarg);
            if (spec->heap_size > 65535)
                fatal_error( "Invalid heap size %d, maximum is 65535\n", spec->heap_size );
413 414 415 416 417 418 419 420
            break;
        case 'I':
            /* ignored */
            break;
        case 'K':
            /* ignored, because cc generates correct code. */
            break;
        case 'L':
421
            strarray_add( &lib_path, xstrdup( optarg ), NULL );
422
            break;
423
        case 'm':
424 425
            if (!strcmp( optarg, "16" )) spec->type = SPEC_WIN16;
            else if (!strcmp( optarg, "32" )) force_pointer_size = 4;
426
            else if (!strcmp( optarg, "64" )) force_pointer_size = 8;
427 428
            else if (!strcmp( optarg, "arm" )) thumb_mode = 0;
            else if (!strcmp( optarg, "thumb" )) thumb_mode = 1;
429
            else if (!strcmp( optarg, "unix" )) unix_lib = 1;
430
            else if (!strncmp( optarg, "cpu=", 4 )) cpu_option = xstrdup( optarg + 4 );
431
            else if (!strncmp( optarg, "fpu=", 4 )) fpu_option = xstrdup( optarg + 4 );
432
            else if (!strncmp( optarg, "arch=", 5 )) arch_option = xstrdup( optarg + 5 );
433
            else if (!strncmp( optarg, "float-abi=", 10 )) float_abi_option = xstrdup( optarg + 10 );
434
            else fatal_error( "Unknown -m option '%s'\n", optarg );
435
            break;
436
        case 'M':
437
            spec->main_module = xstrdup( optarg );
438 439
            break;
        case 'N':
440
            spec->dll_name = xstrdup( optarg );
441
            break;
442 443 444
        case 'b':
            set_target( optarg );
            break;
445
        case 'd':
446
            add_delayed_import( optarg );
447 448
            break;
        case 'e':
449 450
            spec->init_func = xstrdup( optarg );
            if ((p = strchr( spec->init_func, '@' ))) *p = 0;  /* kill stdcall decoration */
451 452 453
            break;
        case 'f':
            if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1;
454 455
            else if (!strcmp( optarg, "asynchronous-unwind-tables")) unwind_tables = 1;
            else if (!strcmp( optarg, "no-asynchronous-unwind-tables")) unwind_tables = 0;
456 457 458 459 460 461 462 463 464
            /* ignore all other flags */
            break;
        case 'h':
            usage(0);
            break;
        case 'k':
            kill_at = 1;
            break;
        case 'l':
465
            add_import_dll( optarg, NULL );
466 467
            break;
        case 'o':
468 469 470
            if (unlink( optarg ) == -1 && errno != ENOENT)
                fatal_error( "Unable to create output file '%s'\n", optarg );
            output_file_name = xstrdup( optarg );
471 472
            break;
        case 'r':
473
            strarray_add( &res_files, xstrdup( optarg ), NULL );
474
            break;
475 476 477
        case 'u':
            add_extra_ld_symbol( optarg );
            break;
478 479 480
        case 'v':
            verbose++;
            break;
481 482 483
        case 'w':
            display_warnings = 1;
            break;
484 485
        case LONG_OPT_DLL:
            set_exec_mode( MODE_DLL );
486 487 488 489 490 491
            break;
        case LONG_OPT_DEF:
            set_exec_mode( MODE_DEF );
            break;
        case LONG_OPT_EXE:
            set_exec_mode( MODE_EXE );
492
            if (!spec->subsystem) spec->subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI;
493
            break;
494 495 496
        case LONG_OPT_IMPLIB:
            set_exec_mode( MODE_IMPLIB );
            break;
497
        case LONG_OPT_ASCMD:
498
            as_command = strarray_fromstring( optarg, " " );
499
            break;
500 501 502
        case LONG_OPT_CCCMD:
            cc_command = strarray_fromstring( optarg, " " );
            break;
503 504 505
        case LONG_OPT_FAKE_MODULE:
            fake_module = 1;
            break;
506 507 508
        case LONG_OPT_EXTERNAL_SYMS:
            link_ext_symbols = 1;
            break;
509 510 511
        case LONG_OPT_LARGE_ADDRESS_AWARE:
            spec->characteristics |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
            break;
512
        case LONG_OPT_LDCMD:
513
            ld_command = strarray_fromstring( optarg, " " );
514 515
            break;
        case LONG_OPT_NMCMD:
516
            nm_command = strarray_fromstring( optarg, " " );
517
            break;
518 519 520 521
        case LONG_OPT_NXCOMPAT:
            if (optarg[0] == 'n' || optarg[0] == 'N')
                spec->dll_characteristics &= ~IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
            break;
522 523 524
        case LONG_OPT_RESOURCES:
            set_exec_mode( MODE_RESOURCES );
            break;
525 526 527
        case LONG_OPT_SAVE_TEMPS:
            save_temps = 1;
            break;
528 529 530
        case LONG_OPT_SUBSYSTEM:
            set_subsystem( optarg, spec );
            break;
531 532 533 534 535 536
        case LONG_OPT_VERSION:
            printf( "winebuild version " PACKAGE_VERSION "\n" );
            exit(0);
        case '?':
            usage(1);
            break;
537 538
        }
    }
539

540 541
    if (!save_temps) atexit( cleanup_tmp_files );

542 543
    if (spec->file_name && !strchr( spec->file_name, '.' ))
        strcat( spec->file_name, exec_mode == MODE_EXE ? ".exe" : ".dll" );
544
    init_dll_name( spec );
545

546 547 548 549 550 551 552 553 554 555 556 557 558 559
    switch (target_cpu)
    {
    case CPU_x86:
        if (force_pointer_size == 8) target_cpu = CPU_x86_64;
        break;
    case CPU_x86_64:
        if (force_pointer_size == 4) target_cpu = CPU_x86;
        break;
    default:
        if (force_pointer_size == 8)
            fatal_error( "Cannot build 64-bit code for this CPU\n" );
        break;
    }

560
    return &argv[optind];
561 562 563
}


564
/* load all specified resource files */
565
static void load_resources( char *argv[], DLLSPEC *spec )
566 567
{
    int i;
568
    char **ptr, **last;
569

570
    switch (spec->type)
571 572
    {
    case SPEC_WIN16:
573
        for (i = 0; i < res_files.count; i++) load_res16_file( res_files.str[i], spec );
574
        break;
575

576
    case SPEC_WIN32:
577
        for (i = 0; i < res_files.count; i++)
578
        {
579 580
            if (!load_res32_file( res_files.str[i], spec ))
                fatal_error( "%s is not a valid Win32 resource file\n", res_files.str[i] );
581 582 583 584 585
        }

        /* load any resource file found in the remaining arguments */
        for (ptr = last = argv; *ptr; ptr++)
        {
586
            if (!load_res32_file( *ptr, spec ))
587 588 589
                *last++ = *ptr; /* not a resource file, keep it in the list */
        }
        *last = NULL;
590 591 592 593
        break;
    }
}

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
/* add input files that look like import libs to the import list */
static void load_import_libs( char *argv[] )
{
    char **ptr, **last;

    for (ptr = last = argv; *ptr; ptr++)
    {
        if (strendswith( *ptr, ".def" ))
            add_import_dll( NULL, *ptr );
        else
            *last++ = *ptr; /* not an import dll, keep it in the list */
    }
    *last = NULL;
}

609 610 611 612
static int parse_input_file( DLLSPEC *spec )
{
    FILE *input_file = open_input_file( NULL, spec_file_name );
    char *extension = strrchr( spec_file_name, '.' );
613
    int result;
614

615
    spec->src_name = xstrdup( input_file_name );
616
    if (extension && !strcmp( extension, ".def" ))
617
        result = parse_def_file( input_file, spec );
618
    else
619
        result = parse_spec_file( input_file, spec );
620
    close_input_file( input_file );
621
    return result;
622 623 624
}


625 626 627 628 629
/*******************************************************************
 *         main
 */
int main(int argc, char **argv)
{
630
    DLLSPEC *spec = alloc_dll_spec();
631

632
#ifdef SIGHUP
633
    signal( SIGHUP, exit_on_signal );
634
#endif
635 636 637
    signal( SIGTERM, exit_on_signal );
    signal( SIGINT, exit_on_signal );

638
    argv = parse_options( argc, argv, spec );
639
    atexit( cleanup );  /* make sure we remove the output file on exit */
640 641 642

    switch(exec_mode)
    {
643
    case MODE_DLL:
644 645
        if (spec->subsystem != IMAGE_SUBSYSTEM_NATIVE)
            spec->characteristics |= IMAGE_FILE_DLL;
646 647
        /* fall through */
    case MODE_EXE:
648
        load_resources( argv, spec );
649
        if (spec_file_name && !parse_input_file( spec )) break;
650
        if (!spec->init_func) spec->init_func = xstrdup( get_default_entry_point( spec ));
651

652 653 654 655 656 657
        if (fake_module)
        {
            if (spec->type == SPEC_WIN16) output_fake_module16( spec );
            else output_fake_module( spec );
            break;
        }
658
        if (target_platform != PLATFORM_WINDOWS)
659
        {
660 661 662
            load_import_libs( argv );
            read_undef_symbols( spec, argv );
            resolve_imports( spec );
663
        }
664 665
        if (spec->type == SPEC_WIN16) output_spec16_file( spec );
        else output_spec32_file( spec );
666
        break;
667
    case MODE_DEF:
668
        if (argv[0]) fatal_error( "file argument '%s' not allowed in this mode\n", argv[0] );
669
        if (!spec_file_name) fatal_error( "missing .spec file\n" );
670
        if (!parse_input_file( spec )) break;
671
        open_output_file();
672
        output_def_file( spec, 0 );
673
        close_output_file();
674
        break;
675 676 677 678 679
    case MODE_IMPLIB:
        if (!spec_file_name) fatal_error( "missing .spec file\n" );
        if (!parse_input_file( spec )) break;
        output_import_lib( spec, argv );
        break;
680 681 682 683
    case MODE_RESOURCES:
        load_resources( argv, spec );
        output_res_o_file( spec );
        break;
684
    default:
685
        usage(1);
686 687
        break;
    }
688
    if (nb_errors) exit(1);
689
    output_file_name = NULL;
690 691
    return 0;
}