makedep.c 151 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3
/*
 * Generate include file dependencies
 *
4
 * Copyright 1996, 2013 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21
#include "config.h"
22
#define NO_LIBWINE_PORT
23 24
#include "wine/port.h"

25
#include <assert.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <ctype.h>
27
#include <errno.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
28 29
#include <stdio.h>
#include <stdlib.h>
30
#include <stdarg.h>
31
#include <signal.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
32
#include <string.h>
33 34 35
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
36
#include "wine/list.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
37

38 39 40 41 42 43 44
struct strarray
{
    unsigned int count;  /* strings in use */
    unsigned int size;   /* total allocated size */
    const char **str;
};

45 46 47 48 49
enum incl_type
{
    INCL_NORMAL,           /* #include "foo.h" */
    INCL_SYSTEM,           /* #include <foo.h> */
    INCL_IMPORT,           /* idl import "foo.idl" */
50
    INCL_IMPORTLIB,        /* idl importlib "foo.tlb" */
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    INCL_CPP_QUOTE,        /* idl cpp_quote("#include \"foo.h\"") */
    INCL_CPP_QUOTE_SYSTEM  /* idl cpp_quote("#include <foo.h>") */
};

struct dependency
{
    int                line;          /* source line where this header is included */
    enum incl_type     type;          /* type of include */
    char              *name;          /* header name */
};

struct file
{
    struct list        entry;
    char              *name;          /* full file name relative to cwd */
    void              *args;          /* custom arguments for makefile rule */
    unsigned int       flags;         /* flags (see below) */
    unsigned int       deps_count;    /* files in use */
    unsigned int       deps_size;     /* total allocated size */
    struct dependency *deps;          /* all header dependencies */
};

73
struct incl_file
Alexandre Julliard's avatar
Alexandre Julliard committed
74
{
75
    struct list        entry;
76
    struct file       *file;
Alexandre Julliard's avatar
Alexandre Julliard committed
77 78
    char              *name;
    char              *filename;
79
    char              *sourcename;    /* source file name for generated headers */
80
    struct incl_file  *included_by;   /* file that included this one */
81
    int                included_line; /* line where this file was included */
82
    enum incl_type     type;          /* type of include */
83
    int                use_msvcrt;    /* put msvcrt headers in the search path? */
84
    struct incl_file  *owner;
85 86 87
    unsigned int       files_count;   /* files in use */
    unsigned int       files_size;    /* total allocated size */
    struct incl_file **files;
88
    struct strarray    dependencies;  /* file dependencies */
89
};
Alexandre Julliard's avatar
Alexandre Julliard committed
90

91 92
#define FLAG_GENERATED      0x000001  /* generated file */
#define FLAG_INSTALL        0x000002  /* file to install */
93
#define FLAG_PARENTDIR      0x000004  /* file comes from parent dir */
94 95 96 97 98 99 100 101 102
#define FLAG_IDL_PROXY      0x000100  /* generates a proxy (_p.c) file */
#define FLAG_IDL_CLIENT     0x000200  /* generates a client (_c.c) file */
#define FLAG_IDL_SERVER     0x000400  /* generates a server (_s.c) file */
#define FLAG_IDL_IDENT      0x000800  /* generates an ident (_i.c) file */
#define FLAG_IDL_REGISTER   0x001000  /* generates a registration (_r.res) file */
#define FLAG_IDL_TYPELIB    0x002000  /* generates a typelib (.tlb) file */
#define FLAG_IDL_REGTYPELIB 0x004000  /* generates a registered typelib (_t.res) file */
#define FLAG_IDL_HEADER     0x008000  /* generates a header (.h) file */
#define FLAG_RC_PO          0x010000  /* rc file contains translations */
103 104 105
#define FLAG_C_IMPLIB       0x020000  /* file is part of an import library */
#define FLAG_C_UNIX         0x040000  /* file is part of a Unix library */
#define FLAG_SFD_FONTS      0x080000  /* sfd file generated bitmap fonts */
106 107 108 109 110 111 112

static const struct
{
    unsigned int flag;
    const char *ext;
} idl_outputs[] =
{
113 114 115 116 117 118 119 120
    { FLAG_IDL_TYPELIB,    ".tlb" },
    { FLAG_IDL_REGTYPELIB, "_t.res" },
    { FLAG_IDL_CLIENT,     "_c.c" },
    { FLAG_IDL_IDENT,      "_i.c" },
    { FLAG_IDL_PROXY,      "_p.c" },
    { FLAG_IDL_SERVER,     "_s.c" },
    { FLAG_IDL_REGISTER,   "_r.res" },
    { FLAG_IDL_HEADER,     ".h" }
121 122
};

123
#define HASH_SIZE 997
124 125

static struct list files[HASH_SIZE];
Alexandre Julliard's avatar
Alexandre Julliard committed
126

127 128
static const struct strarray empty_strarray;

129 130
enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };

131 132 133 134 135 136
/* variables common to all makefiles */
static struct strarray linguas;
static struct strarray dll_flags;
static struct strarray target_flags;
static struct strarray msvcrt_flags;
static struct strarray extra_cflags;
137
static struct strarray extra_cross_cflags;
138
static struct strarray cpp_flags;
139
static struct strarray lddll_flags;
140
static struct strarray libs;
141
static struct strarray enable_tests;
142
static struct strarray cmdline_vars;
143
static struct strarray disabled_dirs;
144 145
static struct strarray cross_import_libs;
static struct strarray delay_import_libs;
146 147
static struct strarray top_install_lib;
static struct strarray top_install_dev;
148 149 150 151 152 153 154 155 156
static const char *root_src_dir;
static const char *tools_dir;
static const char *tools_ext;
static const char *exe_ext;
static const char *dll_ext;
static const char *man_ext;
static const char *crosstarget;
static const char *fontforge;
static const char *convert;
157 158 159 160
static const char *flex;
static const char *bison;
static const char *ar;
static const char *ranlib;
161 162
static const char *rsvg;
static const char *icotool;
163
static const char *dlltool;
164
static const char *msgfmt;
165
static const char *ln_s;
166
static const char *sed_cmd;
167

168 169
struct makefile
{
170
    /* values determined from input makefile */
171
    struct strarray vars;
172
    struct strarray include_paths;
173
    struct strarray include_args;
174
    struct strarray define_args;
175
    struct strarray programs;
176
    struct strarray scripts;
177
    struct strarray imports;
178
    struct strarray subdirs;
179 180
    struct strarray delayimports;
    struct strarray extradllflags;
181 182
    struct strarray install_lib;
    struct strarray install_dev;
183
    struct strarray extra_targets;
184
    struct list     sources;
185
    struct list     includes;
186 187
    const char     *base_dir;
    const char     *src_dir;
188
    const char     *obj_dir;
189 190 191
    const char     *top_src_dir;
    const char     *top_obj_dir;
    const char     *parent_dir;
192 193
    const char     *module;
    const char     *testdll;
194
    const char     *sharedlib;
195
    const char     *staticlib;
196
    const char     *staticimplib;
197
    const char     *importlib;
198
    int             disabled;
199
    int             use_msvcrt;
200
    int             is_cross;
201
    int             is_win16;
202
    int             is_exe;
203
    struct makefile **submakes;
204 205 206 207 208

    /* values generated at output time */
    struct strarray in_files;
    struct strarray ok_files;
    struct strarray clean_files;
209 210
    struct strarray distclean_files;
    struct strarray uninstall_files;
211 212
    struct strarray object_files;
    struct strarray crossobj_files;
213
    struct strarray unixobj_files;
214
    struct strarray res_files;
215 216 217 218 219
    struct strarray c2man_files;
    struct strarray dlldata_files;
    struct strarray implib_objs;
    struct strarray all_targets;
    struct strarray phony_targets;
220
    struct strarray dependencies;
221
    struct strarray install_rules[NB_INSTALL_RULES];
222 223
};

224
static struct makefile *top_makefile;
225

226
static const char separator[] = "### Dependencies";
227
static const char *output_makefile_name = "Makefile";
228
static const char *input_file_name;
229 230
static const char *output_file_name;
static const char *temp_file_name;
231
static int relative_dir_mode;
232
static int input_line;
233
static int output_column;
234
static FILE *output_file;
Alexandre Julliard's avatar
Alexandre Julliard committed
235 236

static const char Usage[] =
237
    "Usage: makedep [options] [directories]\n"
Alexandre Julliard's avatar
Alexandre Julliard committed
238
    "Options:\n"
239
    "   -R from to  Compute the relative path between two directories\n"
240
    "   -fxxx       Store output in file 'xxx' (default: Makefile)\n";
Alexandre Julliard's avatar
Alexandre Julliard committed
241 242


243 244 245 246 247 248
#ifndef __GNUC__
#define __attribute__(x)
#endif

static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
249
static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
250
static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
251

252 253 254 255 256 257 258
/*******************************************************************
 *         fatal_error
 */
static void fatal_error( const char *msg, ... )
{
    va_list valist;
    va_start( valist, msg );
259 260 261 262 263 264 265
    if (input_file_name)
    {
        fprintf( stderr, "%s:", input_file_name );
        if (input_line) fprintf( stderr, "%d:", input_line );
        fprintf( stderr, " error: " );
    }
    else fprintf( stderr, "makedep: error: " );
266 267 268 269 270 271
    vfprintf( stderr, msg, valist );
    va_end( valist );
    exit(1);
}


272 273 274 275 276 277 278 279 280 281 282
/*******************************************************************
 *         fatal_perror
 */
static void fatal_perror( const char *msg, ... )
{
    va_list valist;
    va_start( valist, msg );
    if (input_file_name)
    {
        fprintf( stderr, "%s:", input_file_name );
        if (input_line) fprintf( stderr, "%d:", input_line );
283
        fprintf( stderr, " error: " );
284
    }
285 286
    else fprintf( stderr, "makedep: error: " );
    vfprintf( stderr, msg, valist );
287 288 289 290 291 292
    perror( " " );
    va_end( valist );
    exit(1);
}


293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
/*******************************************************************
 *         cleanup_files
 */
static void cleanup_files(void)
{
    if (temp_file_name) unlink( temp_file_name );
    if (output_file_name) unlink( output_file_name );
}


/*******************************************************************
 *         exit_on_signal
 */
static void exit_on_signal( int sig )
{
    exit( 1 );  /* this will call the atexit functions */
}


Alexandre Julliard's avatar
Alexandre Julliard committed
312 313 314
/*******************************************************************
 *         xmalloc
 */
315
static void *xmalloc( size_t size )
Alexandre Julliard's avatar
Alexandre Julliard committed
316 317 318
{
    void *res;
    if (!(res = malloc (size ? size : 1)))
319
        fatal_error( "Virtual memory exhausted.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
320 321 322 323
    return res;
}


324 325 326
/*******************************************************************
 *         xrealloc
 */
327
static void *xrealloc (void *ptr, size_t size)
328 329 330 331
{
    void *res;
    assert( size );
    if (!(res = realloc( ptr, size )))
332
        fatal_error( "Virtual memory exhausted.\n" );
333 334 335
    return res;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
336 337 338 339 340 341
/*******************************************************************
 *         xstrdup
 */
static char *xstrdup( const char *str )
{
    char *res = strdup( str );
342
    if (!res) fatal_error( "Virtual memory exhausted.\n" );
Alexandre Julliard's avatar
Alexandre Julliard committed
343 344 345 346
    return res;
}


347 348 349
/*******************************************************************
 *         strmake
 */
350
static char *strmake( const char* fmt, ... )
351 352 353 354 355 356 357 358 359 360 361 362 363
{
    int n;
    size_t size = 100;
    va_list ap;

    for (;;)
    {
        char *p = xmalloc (size);
        va_start(ap, fmt);
        n = vsnprintf (p, size, fmt, ap);
        va_end(ap);
        if (n == -1) size *= 2;
        else if ((size_t)n >= size) size = n + 1;
364
        else return xrealloc( p, n + 1 );
365 366 367 368 369
        free(p);
    }
}


370 371 372 373 374
/*******************************************************************
 *         strendswith
 */
static int strendswith( const char* str, const char* end )
{
375 376
    size_t l = strlen( str );
    size_t m = strlen( end );
377 378 379 380

    return l >= m && strcmp(str + l - m, end) == 0;
}

381 382 383 384

/*******************************************************************
 *         output
 */
385
static void output( const char *format, ... )
386 387 388 389 390 391 392 393
{
    int ret;
    va_list valist;

    va_start( valist, format );
    ret = vfprintf( output_file, format, valist );
    va_end( valist );
    if (ret < 0) fatal_perror( "output" );
394 395
    if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
    else output_column += ret;
396 397 398
}


399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/*******************************************************************
 *         strarray_add
 */
static void strarray_add( struct strarray *array, const char *str )
{
    if (array->count == array->size)
    {
	if (array->size) array->size *= 2;
        else array->size = 16;
	array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
    }
    array->str[array->count++] = str;
}


414 415 416
/*******************************************************************
 *         strarray_addall
 */
417
static void strarray_addall( struct strarray *array, struct strarray added )
418 419 420
{
    unsigned int i;

421
    for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
422 423 424
}


425
/*******************************************************************
426
 *         strarray_exists
427
 */
428
static int strarray_exists( const struct strarray *array, const char *str )
429 430 431
{
    unsigned int i;

432 433 434 435 436 437 438 439 440 441 442
    for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
    return 0;
}


/*******************************************************************
 *         strarray_add_uniq
 */
static void strarray_add_uniq( struct strarray *array, const char *str )
{
    if (!strarray_exists( array, str )) strarray_add( array, str );
443 444 445
}


446 447 448 449 450 451 452 453 454 455 456
/*******************************************************************
 *         strarray_addall_uniq
 */
static void strarray_addall_uniq( struct strarray *array, struct strarray added )
{
    unsigned int i;

    for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
}


457 458 459 460 461
/*******************************************************************
 *         strarray_get_value
 *
 * Find a value in a name/value pair string array.
 */
462
static const char *strarray_get_value( const struct strarray *array, const char *name )
463
{
464
    int pos, res, min = 0, max = array->count / 2 - 1;
465

466 467 468 469 470 471 472
    while (min <= max)
    {
        pos = (min + max) / 2;
        if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
        if (res < 0) min = pos + 1;
        else max = pos - 1;
    }
473 474 475 476 477 478 479 480 481 482 483
    return NULL;
}


/*******************************************************************
 *         strarray_set_value
 *
 * Define a value in a name/value pair string array.
 */
static void strarray_set_value( struct strarray *array, const char *name, const char *value )
{
484
    int i, pos, res, min = 0, max = array->count / 2 - 1;
485

486
    while (min <= max)
487
    {
488 489 490 491 492 493 494 495 496
        pos = (min + max) / 2;
        if (!(res = strcmp( array->str[pos * 2], name )))
        {
            /* redefining a variable replaces the previous value */
            array->str[pos * 2 + 1] = value;
            return;
        }
        if (res < 0) min = pos + 1;
        else max = pos - 1;
497
    }
498 499 500 501 502
    strarray_add( array, NULL );
    strarray_add( array, NULL );
    for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
    array->str[min * 2] = name;
    array->str[min * 2 + 1] = value;
503 504 505
}


506 507 508 509 510 511 512 513 514
/*******************************************************************
 *         strarray_set_qsort
 */
static void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
{
    if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
}


515 516 517
/*******************************************************************
 *         output_filename
 */
518
static void output_filename( const char *name )
519
{
520
    if (output_column + strlen(name) + 1 > 100)
521
    {
522 523
        output( " \\\n" );
        output( "  " );
524
    }
525 526
    else if (output_column) output( " " );
    output( "%s", name );
527 528 529 530 531 532
}


/*******************************************************************
 *         output_filenames
 */
533
static void output_filenames( struct strarray array )
534 535 536
{
    unsigned int i;

537
    for (i = 0; i < array.count; i++) output_filename( array.str[i] );
538 539 540
}


541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/*******************************************************************
 *         output_rm_filenames
 */
static void output_rm_filenames( struct strarray array )
{
    static const unsigned int max_cmdline = 30000;  /* to be on the safe side */
    unsigned int i, len;

    if (!array.count) return;
    output( "\trm -f" );
    for (i = len = 0; i < array.count; i++)
    {
        if (len > max_cmdline)
        {
            output( "\n" );
            output( "\trm -f" );
            len = 0;
        }
        output_filename( array.str[i] );
        len += strlen( array.str[i] ) + 1;
    }
    output( "\n" );
}


566 567 568 569 570 571 572 573 574 575 576
/*******************************************************************
 *         get_extension
 */
static char *get_extension( char *filename )
{
    char *ext = strrchr( filename, '.' );
    if (ext && strchr( ext, '/' )) ext = NULL;
    return ext;
}


577 578 579
/*******************************************************************
 *         replace_extension
 */
580
static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
581
{
582
    char *ret;
583 584
    size_t name_len = strlen( name );
    size_t ext_len = strlen( old_ext );
585 586 587 588 589

    if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
    ret = xmalloc( name_len + strlen( new_ext ) + 1 );
    memcpy( ret, name, name_len );
    strcpy( ret + name_len, new_ext );
590 591 592 593
    return ret;
}


594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
/*******************************************************************
 *         replace_filename
 */
static char *replace_filename( const char *path, const char *name )
{
    const char *p;
    char *ret;
    size_t len;

    if (!path) return xstrdup( name );
    if (!(p = strrchr( path, '/' ))) return xstrdup( name );
    len = p - path + 1;
    ret = xmalloc( len + strlen( name ) + 1 );
    memcpy( ret, path, len );
    strcpy( ret + len, name );
    return ret;
}


613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
/*******************************************************************
 *         strarray_replace_extension
 */
static struct strarray strarray_replace_extension( const struct strarray *array,
                                                   const char *old_ext, const char *new_ext )
{
    unsigned int i;
    struct strarray ret;

    ret.count = ret.size = array->count;
    ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
    for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
    return ret;
}


629 630 631
/*******************************************************************
 *         replace_substr
 */
632
static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
633
{
634
    size_t pos = start - str;
635 636 637 638 639 640 641 642
    char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
    memcpy( ret, str, pos );
    strcpy( ret + pos, replace );
    strcat( ret + pos, start + len );
    return ret;
}


643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
/*******************************************************************
 *         get_relative_path
 *
 * Determine where the destination path is located relative to the 'from' path.
 */
static char *get_relative_path( const char *from, const char *dest )
{
    const char *start;
    char *ret, *p;
    unsigned int dotdots = 0;

    /* a path of "." is equivalent to an empty path */
    if (!strcmp( from, "." )) from = "";

    for (;;)
    {
        while (*from == '/') from++;
        while (*dest == '/') dest++;
        start = dest;  /* save start of next path element */
        if (!*from) break;

        while (*from && *from != '/' && *from == *dest) { from++; dest++; }
        if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;

        /* count remaining elements in 'from' */
        do
        {
            dotdots++;
            while (*from && *from != '/') from++;
            while (*from == '/') from++;
        }
        while (*from);
        break;
    }

    if (!start[0] && !dotdots) return NULL;  /* empty path */

    ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
    for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );

    if (start[0]) strcpy( p, start );
    else p[-1] = 0;  /* remove trailing slash */
    return ret;
}


689 690 691 692 693
/*******************************************************************
 *         concat_paths
 */
static char *concat_paths( const char *base, const char *path )
{
694
    if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
695
    if (!path || !path[0]) return xstrdup( base );
696 697 698 699 700
    if (path[0] == '/') return xstrdup( path );
    return strmake( "%s/%s", base, path );
}


701 702 703
/*******************************************************************
 *         base_dir_path
 */
704
static char *base_dir_path( const struct makefile *make, const char *path )
705
{
706
    return concat_paths( make->base_dir, path );
707 708 709
}


710 711 712
/*******************************************************************
 *         obj_dir_path
 */
713
static char *obj_dir_path( const struct makefile *make, const char *path )
714 715 716 717 718
{
    return concat_paths( make->obj_dir, path );
}


719 720 721
/*******************************************************************
 *         src_dir_path
 */
722
static char *src_dir_path( const struct makefile *make, const char *path )
723
{
724
    if (make->src_dir) return concat_paths( make->src_dir, path );
725
    return obj_dir_path( make, path );
726 727 728 729 730 731
}


/*******************************************************************
 *         top_obj_dir_path
 */
732
static char *top_obj_dir_path( const struct makefile *make, const char *path )
733
{
734
    return concat_paths( make->top_obj_dir, path );
735 736 737 738
}


/*******************************************************************
739
 *         top_src_dir_path
740
 */
741
static char *top_src_dir_path( const struct makefile *make, const char *path )
742
{
743
    if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
744
    return top_obj_dir_path( make, path );
745 746 747
}


748 749 750 751 752 753 754 755 756
/*******************************************************************
 *         root_dir_path
 */
static char *root_dir_path( const char *path )
{
    return concat_paths( root_src_dir, path );
}


757 758 759
/*******************************************************************
 *         tools_dir_path
 */
760
static char *tools_dir_path( const struct makefile *make, const char *path )
761
{
762 763
    if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
    return top_obj_dir_path( make, strmake( "tools/%s", path ));
764 765 766
}


767 768 769
/*******************************************************************
 *         tools_path
 */
770
static char *tools_path( const struct makefile *make, const char *name )
771
{
772
    return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
773 774 775
}


776 777 778 779 780 781
/*******************************************************************
 *         get_line
 */
static char *get_line( FILE *file )
{
    static char *buffer;
782
    static size_t size;
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798

    if (!size)
    {
        size = 1024;
        buffer = xmalloc( size );
    }
    if (!fgets( buffer, size, file )) return NULL;
    input_line++;

    for (;;)
    {
        char *p = buffer + strlen(buffer);
        /* if line is larger than buffer, resize buffer */
        while (p == buffer + size - 1 && p[-1] != '\n')
        {
            buffer = xrealloc( buffer, size * 2 );
799
            if (!fgets( buffer + size - 1, size + 1, file )) break;
800 801 802 803 804 805 806 807 808 809 810
            p = buffer + strlen(buffer);
            size *= 2;
        }
        if (p > buffer && p[-1] == '\n')
        {
            *(--p) = 0;
            if (p > buffer && p[-1] == '\r') *(--p) = 0;
            if (p > buffer && p[-1] == '\\')
            {
                *(--p) = 0;
                /* line ends in backslash, read continuation line */
811
                if (!fgets( p, size - (p - buffer), file )) return buffer;
812 813 814 815 816 817 818 819
                input_line++;
                continue;
            }
        }
        return buffer;
    }
}

820 821 822 823 824 825

/*******************************************************************
 *         hash_filename
 */
static unsigned int hash_filename( const char *name )
{
826
    /* FNV-1 hash */
827
    unsigned int ret = 2166136261u;
828
    while (*name) ret = (ret * 16777619) ^ *name++;
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 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 885
    return ret % HASH_SIZE;
}


/*******************************************************************
 *         add_file
 */
static struct file *add_file( const char *name )
{
    struct file *file = xmalloc( sizeof(*file) );
    memset( file, 0, sizeof(*file) );
    file->name = xstrdup( name );
    return file;
}


/*******************************************************************
 *         add_dependency
 */
static void add_dependency( struct file *file, const char *name, enum incl_type type )
{
    /* enforce some rules for the Wine tree */

    if (!memcmp( name, "../", 3 ))
        fatal_error( "#include directive with relative path not allowed\n" );

    if (!strcmp( name, "config.h" ))
    {
        if (strendswith( file->name, ".h" ))
            fatal_error( "config.h must not be included by a header file\n" );
        if (file->deps_count)
            fatal_error( "config.h must be included before anything else\n" );
    }
    else if (!strcmp( name, "wine/port.h" ))
    {
        if (strendswith( file->name, ".h" ))
            fatal_error( "wine/port.h must not be included by a header file\n" );
        if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
        if (file->deps_count > 1)
            fatal_error( "wine/port.h must be included before everything except config.h\n" );
        if (strcmp( file->deps[0].name, "config.h" ))
            fatal_error( "config.h must be included before wine/port.h\n" );
    }

    if (file->deps_count >= file->deps_size)
    {
        file->deps_size *= 2;
        if (file->deps_size < 16) file->deps_size = 16;
        file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
    }
    file->deps[file->deps_count].line = input_line;
    file->deps[file->deps_count].type = type;
    file->deps[file->deps_count].name = xstrdup( name );
    file->deps_count++;
}


886 887 888
/*******************************************************************
 *         find_src_file
 */
889
static struct incl_file *find_src_file( const struct makefile *make, const char *name )
890
{
891
    struct incl_file *file;
892

893
    LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
894 895 896
        if (!strcmp( name, file->name )) return file;
    return NULL;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
897

898 899 900
/*******************************************************************
 *         find_include_file
 */
901
static struct incl_file *find_include_file( const struct makefile *make, const char *name )
902
{
903
    struct incl_file *file;
904

905
    LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
906 907 908 909
        if (!strcmp( name, file->name )) return file;
    return NULL;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
910 911 912 913 914
/*******************************************************************
 *         add_include
 *
 * Add an include file if it doesn't already exists.
 */
915
static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
916
                                      const char *name, int line, enum incl_type type )
Alexandre Julliard's avatar
Alexandre Julliard committed
917
{
918
    struct incl_file *include;
Alexandre Julliard's avatar
Alexandre Julliard committed
919

920 921 922 923 924 925
    if (parent->files_count >= parent->files_size)
    {
        parent->files_size *= 2;
        if (parent->files_size < 16) parent->files_size = 16;
        parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
    }
926

927
    LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
928 929
        if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
            goto found;
930

931 932
    include = xmalloc( sizeof(*include) );
    memset( include, 0, sizeof(*include) );
933
    include->name = xstrdup(name);
934
    include->included_by = parent;
935
    include->included_line = line;
936
    include->type = type;
937
    include->use_msvcrt = parent->use_msvcrt;
938
    list_add_tail( &make->includes, &include->entry );
939
found:
940
    parent->files[parent->files_count++] = include;
941
    return include;
Alexandre Julliard's avatar
Alexandre Julliard committed
942 943 944
}


945 946 947 948 949
/*******************************************************************
 *         add_generated_source
 *
 * Add a generated source file to the list.
 */
950 951
static struct incl_file *add_generated_source( struct makefile *make,
                                               const char *name, const char *filename )
952 953 954
{
    struct incl_file *file;

955
    if ((file = find_src_file( make, name ))) return file;  /* we already have it */
956 957
    file = xmalloc( sizeof(*file) );
    memset( file, 0, sizeof(*file) );
958
    file->file = add_file( name );
959
    file->name = xstrdup( name );
960
    file->filename = obj_dir_path( make, filename ? filename : name );
961
    file->file->flags = FLAG_GENERATED;
962
    file->use_msvcrt = make->use_msvcrt;
963
    list_add_tail( &make->sources, &file->entry );
964 965 966 967
    return file;
}


968 969 970
/*******************************************************************
 *         parse_include_directive
 */
971
static void parse_include_directive( struct file *source, char *str )
972 973 974 975 976 977 978 979 980 981 982
{
    char quote, *include, *p = str;

    while (*p && isspace(*p)) p++;
    if (*p != '\"' && *p != '<' ) return;
    quote = *p++;
    if (quote == '<') quote = '>';
    include = p;
    while (*p && (*p != quote)) p++;
    if (!*p) fatal_error( "malformed include directive '%s'\n", str );
    *p = 0;
983
    add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
984 985 986 987 988 989
}


/*******************************************************************
 *         parse_pragma_directive
 */
990
static void parse_pragma_directive( struct file *source, char *str )
991 992 993 994 995 996 997 998 999 1000 1001 1002
{
    char *flag, *p = str;

    if (!isspace( *p )) return;
    while (*p && isspace(*p)) p++;
    p = strtok( p, " \t" );
    if (strcmp( p, "makedep" )) return;

    while ((flag = strtok( NULL, " \t" )))
    {
        if (!strcmp( flag, "depend" ))
        {
1003
            while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
1004 1005
            return;
        }
1006 1007
        else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;

1008 1009 1010 1011 1012 1013 1014 1015 1016
        if (strendswith( source->name, ".idl" ))
        {
            if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
            else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
            else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
            else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
            else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
            else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
            else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
1017
            else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
1018 1019 1020 1021 1022
        }
        else if (strendswith( source->name, ".rc" ))
        {
            if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
        }
1023 1024 1025 1026
        else if (strendswith( source->name, ".sfd" ))
        {
            if (!strcmp( flag, "font" ))
            {
1027 1028 1029 1030 1031 1032 1033 1034 1035
                struct strarray *array = source->args;

                if (!array)
                {
                    source->args = array = xmalloc( sizeof(*array) );
                    *array = empty_strarray;
                    source->flags |= FLAG_SFD_FONTS;
                }
                strarray_add( array, xstrdup( strtok( NULL, "" )));
1036 1037 1038
                return;
            }
        }
1039 1040 1041 1042 1043
        else
        {
            if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
            if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
        }
1044 1045 1046 1047 1048 1049 1050
    }
}


/*******************************************************************
 *         parse_cpp_directive
 */
1051
static void parse_cpp_directive( struct file *source, char *str )
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
{
    while (*str && isspace(*str)) str++;
    if (*str++ != '#') return;
    while (*str && isspace(*str)) str++;

    if (!strncmp( str, "include", 7 ))
        parse_include_directive( source, str + 7 );
    else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
        parse_include_directive( source, str + 6 );
    else if (!strncmp( str, "pragma", 6 ))
        parse_pragma_directive( source, str + 6 );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1066
/*******************************************************************
1067
 *         parse_idl_file
Alexandre Julliard's avatar
Alexandre Julliard committed
1068
 */
1069
static void parse_idl_file( struct file *source, FILE *file )
Alexandre Julliard's avatar
Alexandre Julliard committed
1070
{
1071
    char *buffer, *include;
1072

1073
    input_line = 0;
1074

1075
    while ((buffer = get_line( file )))
1076
    {
1077
        char quote;
1078 1079
        char *p = buffer;
        while (*p && isspace(*p)) p++;
1080

1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
        if (!strncmp( p, "importlib", 9 ))
        {
            p += 9;
            while (*p && isspace(*p)) p++;
            if (*p++ != '(') continue;
            while (*p && isspace(*p)) p++;
            if (*p++ != '"') continue;
            include = p;
            while (*p && (*p != '"')) p++;
            if (!*p) fatal_error( "malformed importlib directive\n" );
            *p = 0;
            add_dependency( source, include, INCL_IMPORTLIB );
            continue;
        }

1096 1097 1098 1099
        if (!strncmp( p, "import", 6 ))
        {
            p += 6;
            while (*p && isspace(*p)) p++;
1100 1101 1102
            if (*p != '"') continue;
            include = ++p;
            while (*p && (*p != '"')) p++;
1103
            if (!*p) fatal_error( "malformed import directive\n" );
1104
            *p = 0;
1105
            add_dependency( source, include, INCL_IMPORT );
1106
            continue;
1107
        }
1108

1109 1110
        /* check for #include inside cpp_quote */
        if (!strncmp( p, "cpp_quote", 9 ))
1111
        {
1112 1113 1114 1115 1116
            p += 9;
            while (*p && isspace(*p)) p++;
            if (*p++ != '(') continue;
            while (*p && isspace(*p)) p++;
            if (*p++ != '"') continue;
1117 1118 1119 1120 1121
            if (*p++ != '#') continue;
            while (*p && isspace(*p)) p++;
            if (strncmp( p, "include", 7 )) continue;
            p += 7;
            while (*p && isspace(*p)) p++;
1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
            if (*p == '\\' && p[1] == '"')
            {
                p += 2;
                quote = '"';
            }
            else
            {
                if (*p++ != '<' ) continue;
                quote = '>';
            }
            include = p;
            while (*p && (*p != quote)) p++;
            if (!*p || (quote == '"' && p[-1] != '\\'))
1135
                fatal_error( "malformed #include directive inside cpp_quote\n" );
1136 1137
            if (quote == '"') p--;  /* remove backslash */
            *p = 0;
1138
            add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1139
            continue;
1140 1141
        }

1142
        parse_cpp_directive( source, p );
1143
    }
1144
}
1145

1146 1147 1148
/*******************************************************************
 *         parse_c_file
 */
1149
static void parse_c_file( struct file *source, FILE *file )
1150
{
1151
    char *buffer;
Alexandre Julliard's avatar
Alexandre Julliard committed
1152

1153 1154
    input_line = 0;
    while ((buffer = get_line( file )))
Alexandre Julliard's avatar
Alexandre Julliard committed
1155
    {
1156
        parse_cpp_directive( source, buffer );
Alexandre Julliard's avatar
Alexandre Julliard committed
1157
    }
1158 1159 1160
}


1161 1162 1163
/*******************************************************************
 *         parse_rc_file
 */
1164
static void parse_rc_file( struct file *source, FILE *file )
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
{
    char *buffer, *include;

    input_line = 0;
    while ((buffer = get_line( file )))
    {
        char quote;
        char *p = buffer;
        while (*p && isspace(*p)) p++;

        if (p[0] == '/' && p[1] == '*')  /* check for magic makedep comment */
        {
            p += 2;
            while (*p && isspace(*p)) p++;
            if (strncmp( p, "@makedep:", 9 )) continue;
            p += 9;
            while (*p && isspace(*p)) p++;
            quote = '"';
            if (*p == quote)
            {
                include = ++p;
                while (*p && *p != quote) p++;
            }
            else
            {
                include = p;
                while (*p && !isspace(*p) && *p != '*') p++;
            }
            if (!*p)
1194
                fatal_error( "malformed makedep comment\n" );
1195
            *p = 0;
1196
            add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1197
            continue;
1198
        }
1199

1200
        parse_cpp_directive( source, buffer );
1201 1202 1203 1204
    }
}


1205
/*******************************************************************
1206
 *         parse_in_file
1207
 */
1208
static void parse_in_file( struct file *source, FILE *file )
1209 1210 1211 1212
{
    char *p, *buffer;

    /* make sure it gets rebuilt when the version changes */
1213
    add_dependency( source, "config.h", INCL_SYSTEM );
1214

1215
    if (!strendswith( source->name, ".man.in" )) return;  /* not a man page */
1216

1217 1218 1219 1220 1221 1222 1223
    input_line = 0;
    while ((buffer = get_line( file )))
    {
        if (strncmp( buffer, ".TH", 3 )) continue;
        if (!(p = strtok( buffer, " \t" ))) continue;  /* .TH */
        if (!(p = strtok( NULL, " \t" ))) continue;  /* program name */
        if (!(p = strtok( NULL, " \t" ))) continue;  /* man section */
1224 1225 1226 1227 1228 1229 1230 1231 1232
        source->args = xstrdup( p );
        return;
    }
}


/*******************************************************************
 *         parse_sfd_file
 */
1233
static void parse_sfd_file( struct file *source, FILE *file )
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
{
    char *p, *eol, *buffer;

    input_line = 0;
    while ((buffer = get_line( file )))
    {
        if (strncmp( buffer, "UComments:", 10 )) continue;
        p = buffer + 10;
        while (*p == ' ') p++;
        if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
        {
            p++;
            buffer[strlen(buffer) - 1] = 0;
        }
        while ((eol = strstr( p, "+AAoA" )))
        {
            *eol = 0;
            while (*p && isspace(*p)) p++;
            if (*p++ == '#')
            {
                while (*p && isspace(*p)) p++;
                if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
            }
            p = eol + 5;
        }
        while (*p && isspace(*p)) p++;
        if (*p++ != '#') return;
        while (*p && isspace(*p)) p++;
        if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1263 1264 1265 1266 1267
        return;
    }
}


1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
static const struct
{
    const char *ext;
    void (*parse)( struct file *file, FILE *f );
} parse_functions[] =
{
    { ".c",   parse_c_file },
    { ".h",   parse_c_file },
    { ".inl", parse_c_file },
    { ".l",   parse_c_file },
    { ".m",   parse_c_file },
    { ".rh",  parse_c_file },
    { ".x",   parse_c_file },
    { ".y",   parse_c_file },
    { ".idl", parse_idl_file },
    { ".rc",  parse_rc_file },
    { ".in",  parse_in_file },
    { ".sfd", parse_sfd_file }
};

1288 1289 1290 1291 1292 1293 1294
/*******************************************************************
 *         load_file
 */
static struct file *load_file( const char *name )
{
    struct file *file;
    FILE *f;
1295
    unsigned int i, hash = hash_filename( name );
1296 1297 1298 1299 1300 1301 1302

    LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
        if (!strcmp( name, file->name )) return file;

    if (!(f = fopen( name, "r" ))) return NULL;

    file = add_file( name );
1303
    list_add_tail( &files[hash], &file->entry );
1304 1305 1306
    input_file_name = file->name;
    input_line = 0;

1307 1308 1309 1310 1311 1312
    for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
    {
        if (!strendswith( name, parse_functions[i].ext )) continue;
        parse_functions[i].parse( file, f );
        break;
    }
1313 1314 1315 1316 1317 1318 1319 1320 1321

    fclose( f );
    input_file_name = NULL;

    return file;
}


/*******************************************************************
1322 1323 1324
 *         open_include_path_file
 *
 * Open a file from a directory on the include path.
1325
 */
1326 1327
static struct file *open_include_path_file( const struct makefile *make, const char *dir,
                                            const char *name, char **filename )
1328
{
1329 1330
    char *src_path = base_dir_path( make, concat_paths( dir, name ));
    struct file *ret = load_file( src_path );
1331

1332
    if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
1333 1334 1335 1336
    return ret;
}


1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
/*******************************************************************
 *         open_file_same_dir
 *
 * Open a file in the same directory as the parent.
 */
static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
{
    char *src_path = replace_filename( parent->file->name, name );
    struct file *ret = load_file( src_path );

    if (ret) *filename = replace_filename( parent->filename, name );
    free( src_path );
    return ret;
}


1353 1354 1355 1356 1357
/*******************************************************************
 *         open_local_file
 *
 * Open a file in the source directory of the makefile.
 */
1358
static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
{
    char *src_path = root_dir_path( base_dir_path( make, path ));
    struct file *ret = load_file( src_path );

    /* if not found, try parent dir */
    if (!ret && make->parent_dir)
    {
        free( src_path );
        path = strmake( "%s/%s", make->parent_dir, path );
        src_path = root_dir_path( base_dir_path( make, path ));
        ret = load_file( src_path );
1370
        if (ret) ret->flags |= FLAG_PARENTDIR;
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
    }

    if (ret) *filename = src_dir_path( make, path );
    free( src_path );
    return ret;
}


/*******************************************************************
 *         open_global_file
 *
 * Open a file in the top-level source directory.
 */
1384
static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1385 1386 1387 1388
{
    char *src_path = root_dir_path( path );
    struct file *ret = load_file( src_path );

1389
    if (ret) *filename = top_src_dir_path( make, path );
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399
    free( src_path );
    return ret;
}


/*******************************************************************
 *         open_global_header
 *
 * Open a file in the global include source directory.
 */
1400
static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1401 1402 1403 1404 1405 1406 1407 1408
{
    return open_global_file( make, strmake( "include/%s", path ), filename );
}


/*******************************************************************
 *         open_src_file
 */
1409
static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420
{
    struct file *file = open_local_file( make, pFile->name, &pFile->filename );

    if (!file) fatal_perror( "open %s", pFile->name );
    return file;
}


/*******************************************************************
 *         open_include_file
 */
1421
static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1422 1423
{
    struct file *file = NULL;
1424
    char *filename;
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
    unsigned int i, len;

    errno = ENOENT;

    /* check for generated bison header */

    if (strendswith( pFile->name, ".tab.h" ) &&
        (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
    {
        pFile->sourcename = filename;
        pFile->filename = obj_dir_path( make, pFile->name );
        return file;
    }

    /* check for corresponding idl file in source dir */

    if (strendswith( pFile->name, ".h" ) &&
        (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
    {
        pFile->sourcename = filename;
        pFile->filename = obj_dir_path( make, pFile->name );
        return file;
    }

1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
    /* check for corresponding tlb file in source dir */

    if (strendswith( pFile->name, ".tlb" ) &&
        (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
    {
        pFile->sourcename = filename;
        pFile->filename = obj_dir_path( make, pFile->name );
        return file;
    }

1459 1460 1461 1462 1463 1464 1465 1466
    /* check for extra targets */
    if (strarray_exists( &make->extra_targets, pFile->name ))
    {
        pFile->sourcename = filename;
        pFile->filename = obj_dir_path( make, pFile->name );
        return NULL;
    }

1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
    /* now try in source dir */
    if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;

    /* check for corresponding idl file in global includes */

    if (strendswith( pFile->name, ".h" ) &&
        (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
    {
        pFile->sourcename = filename;
        pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
        return file;
    }

    /* check for corresponding .in file in global includes (for config.h.in) */

    if (strendswith( pFile->name, ".h" ) &&
        (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
    {
        pFile->sourcename = filename;
        pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
        return file;
    }

    /* check for corresponding .x file in global includes */

    if (strendswith( pFile->name, "tmpl.h" ) &&
        (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
    {
        pFile->sourcename = filename;
        pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
        return file;
    }

1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
    /* check for corresponding .tlb file in global includes */

    if (strendswith( pFile->name, ".tlb" ) &&
        (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
    {
        pFile->sourcename = filename;
        pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
        return file;
    }

1510 1511 1512 1513 1514
    /* check in global includes source dir */

    if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;

    /* check in global msvcrt includes */
1515
    if (pFile->use_msvcrt &&
1516 1517 1518 1519
        (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
        return file;

    /* now search in include paths */
1520
    for (i = 0; i < make->include_paths.count; i++)
1521
    {
1522
        const char *dir = make->include_paths.str[i];
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
        const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;

        if (prefix)
        {
            len = strlen( prefix );
            if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
            {
                while (dir[len] == '/') len++;
                file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
                if (file) return file;
            }
            if (make->top_src_dir) continue;  /* ignore paths that don't point to the top source dir */
        }
        if (*dir != '/')
        {
1538
            if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1539 1540 1541
                return file;
        }
    }
1542

1543
    if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt)
1544 1545 1546 1547 1548 1549 1550
    {
        if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
        fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
                 pFile->included_by->file->name, pFile->included_line, pFile->name );
        exit(1);
    }

1551
    if (pFile->type == INCL_SYSTEM) return NULL;  /* ignore system files we cannot find */
1552 1553

    /* try in src file directory */
1554
    if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573

    fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
    perror( pFile->name );
    pFile = pFile->included_by;
    while (pFile && pFile->included_by)
    {
        const char *parent = pFile->included_by->sourcename;
        if (!parent) parent = pFile->included_by->file->name;
        fprintf( stderr, "%s:%d: note: %s was first included here\n",
                 parent, pFile->included_line, pFile->name );
        pFile = pFile->included_by;
    }
    exit(1);
}


/*******************************************************************
 *         add_all_includes
 */
1574
static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
{
    unsigned int i;

    parent->files_count = 0;
    parent->files_size = file->deps_count;
    parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
    for (i = 0; i < file->deps_count; i++)
    {
        switch (file->deps[i].type)
        {
        case INCL_NORMAL:
        case INCL_IMPORT:
1587 1588 1589 1590
            add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
            break;
        case INCL_IMPORTLIB:
            add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1591 1592
            break;
        case INCL_SYSTEM:
1593
            add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1594 1595 1596 1597 1598 1599 1600 1601 1602
            break;
        case INCL_CPP_QUOTE:
        case INCL_CPP_QUOTE_SYSTEM:
            break;
        }
    }
}


1603 1604 1605
/*******************************************************************
 *         parse_file
 */
1606
static void parse_file( struct makefile *make, struct incl_file *source, int src )
1607
{
1608
    struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1609

1610
    if (!file) return;
1611 1612 1613 1614 1615

    source->file = file;
    source->files_count = 0;
    source->files_size = file->deps_count;
    source->files = xmalloc( source->files_size * sizeof(*source->files) );
1616
    if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1617 1618 1619 1620 1621 1622 1623

    if (source->sourcename)
    {
        if (strendswith( source->sourcename, ".idl" ))
        {
            unsigned int i;

1624 1625
            if (strendswith( source->name, ".tlb" )) return;  /* typelibs don't include anything */

1626
            /* generated .h file always includes these */
1627 1628
            add_include( make, source, "rpc.h", 0, INCL_NORMAL );
            add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1629 1630 1631 1632 1633 1634
            for (i = 0; i < file->deps_count; i++)
            {
                switch (file->deps[i].type)
                {
                case INCL_IMPORT:
                    if (strendswith( file->deps[i].name, ".idl" ))
1635
                        add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1636
                                     file->deps[i].line, INCL_NORMAL );
1637
                    else
1638
                        add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1639 1640
                    break;
                case INCL_CPP_QUOTE:
1641
                    add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1642 1643
                    break;
                case INCL_CPP_QUOTE_SYSTEM:
1644
                    add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1645 1646 1647
                    break;
                case INCL_NORMAL:
                case INCL_SYSTEM:
1648
                case INCL_IMPORTLIB:
1649 1650 1651 1652 1653 1654 1655 1656 1657
                    break;
                }
            }
            return;
        }
        if (strendswith( source->sourcename, ".y" ))
            return;  /* generated .tab.h doesn't include anything */
    }

1658
    add_all_includes( make, source, file );
Alexandre Julliard's avatar
Alexandre Julliard committed
1659 1660 1661
}


1662 1663 1664 1665 1666
/*******************************************************************
 *         add_src_file
 *
 * Add a source file to the list.
 */
1667
static struct incl_file *add_src_file( struct makefile *make, const char *name )
1668
{
1669
    struct incl_file *file;
1670

1671
    if ((file = find_src_file( make, name ))) return file;  /* we already have it */
1672 1673 1674
    file = xmalloc( sizeof(*file) );
    memset( file, 0, sizeof(*file) );
    file->name = xstrdup(name);
1675
    file->use_msvcrt = make->use_msvcrt;
1676
    list_add_tail( &make->sources, &file->entry );
1677
    parse_file( make, file, 1 );
1678
    return file;
1679
}
1680

1681

1682 1683 1684
/*******************************************************************
 *         open_input_makefile
 */
1685
static FILE *open_input_makefile( const struct makefile *make )
1686 1687 1688 1689
{
    FILE *ret;

    if (make->base_dir)
1690 1691 1692
        input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
    else
        input_file_name = output_makefile_name;  /* always use output name for main Makefile */
1693 1694

    input_line = 0;
1695
    if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1696 1697 1698 1699
    return ret;
}


1700 1701 1702
/*******************************************************************
 *         get_make_variable
 */
1703
static const char *get_make_variable( const struct makefile *make, const char *name )
1704
{
1705
    const char *ret;
1706

1707
    if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1708
    if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1709
    if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1710 1711 1712 1713 1714 1715 1716
    return NULL;
}


/*******************************************************************
 *         get_expanded_make_variable
 */
1717
static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1718
{
1719 1720
    const char *var;
    char *p, *end, *expand, *tmp;
1721

1722 1723
    var = get_make_variable( make, name );
    if (!var) return NULL;
1724

1725
    p = expand = xstrdup( var );
1726 1727 1728 1729 1730 1731 1732
    while ((p = strchr( p, '$' )))
    {
        if (p[1] == '(')
        {
            if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
            *end++ = 0;
            if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1733
            var = get_make_variable( make, p + 2 );
1734
            tmp = replace_substr( expand, p, end - p, var ? var : "" );
1735 1736 1737 1738
            /* switch to the new string */
            p = tmp + (p - expand);
            free( expand );
            expand = tmp;
1739
        }
1740 1741 1742 1743 1744
        else if (p[1] == '{')  /* don't expand ${} variables */
        {
            if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
            p = end + 1;
        }
1745 1746
        else if (p[1] == '$')
        {
1747
            p += 2;
1748 1749 1750
        }
        else fatal_error( "syntax error in '%s'\n", expand );
    }
1751 1752 1753 1754 1755 1756 1757

    /* consider empty variables undefined */
    p = expand;
    while (*p && isspace(*p)) p++;
    if (*p) return expand;
    free( expand );
    return NULL;
1758 1759 1760 1761 1762 1763
}


/*******************************************************************
 *         get_expanded_make_var_array
 */
1764
static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1765
{
1766
    struct strarray ret = empty_strarray;
1767 1768
    char *value, *token;

1769
    if ((value = get_expanded_make_variable( make, name )))
1770 1771 1772 1773 1774 1775
        for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
            strarray_add( &ret, token );
    return ret;
}


1776
/*******************************************************************
1777
 *         get_expanded_file_local_var
1778
 */
1779 1780
static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
                                                    const char *name )
1781
{
1782
    char *p, *var = strmake( "%s_%s", file, name );
1783 1784

    for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1785
    return get_expanded_make_var_array( make, var );
1786 1787 1788
}


1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807
/*******************************************************************
 *         set_make_variable
 */
static int set_make_variable( struct strarray *array, const char *assignment )
{
    char *p, *name;

    p = name = xstrdup( assignment );
    while (isalnum(*p) || *p == '_') p++;
    if (name == p) return 0;  /* not a variable */
    if (isspace(*p))
    {
        *p++ = 0;
        while (isspace(*p)) p++;
    }
    if (*p != '=') return 0;  /* not an assignment */
    *p++ = 0;
    while (isspace(*p)) p++;

1808
    strarray_set_value( array, name, p );
1809 1810 1811 1812
    return 1;
}


1813 1814 1815
/*******************************************************************
 *         parse_makefile
 */
1816
static struct makefile *parse_makefile( const char *path )
1817 1818 1819
{
    char *buffer;
    FILE *file;
1820
    struct makefile *make = xmalloc( sizeof(*make) );
1821

1822 1823 1824 1825 1826 1827 1828 1829
    memset( make, 0, sizeof(*make) );
    if (path)
    {
        make->top_obj_dir = get_relative_path( path, "" );
        make->base_dir = path;
        if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
    }

1830
    file = open_input_makefile( make );
1831 1832
    while ((buffer = get_line( file )))
    {
1833
        if (!strncmp( buffer, separator, strlen(separator) )) break;
1834 1835 1836
        if (*buffer == '\t') continue;  /* command */
        while (isspace( *buffer )) buffer++;
        if (*buffer == '#') continue;  /* comment */
1837
        set_make_variable( &make->vars, buffer );
1838 1839 1840
    }
    fclose( file );
    input_file_name = NULL;
1841
    return make;
1842 1843 1844
}


1845 1846 1847
/*******************************************************************
 *         add_generated_sources
 */
1848
static void add_generated_sources( struct makefile *make )
1849
{
1850
    unsigned int i;
1851
    struct incl_file *source, *next, *file;
1852
    struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1853

1854
    LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1855
    {
1856
        if (source->file->flags & FLAG_IDL_CLIENT)
1857
        {
1858
            file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1859
            add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1860
            add_all_includes( make, file, file->file );
1861
        }
1862
        if (source->file->flags & FLAG_IDL_SERVER)
1863
        {
1864
            file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1865 1866
            add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
            add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1867
            add_all_includes( make, file, file->file );
1868
        }
1869
        if (source->file->flags & FLAG_IDL_IDENT)
1870
        {
1871
            file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1872 1873 1874
            add_dependency( file->file, "rpc.h", INCL_NORMAL );
            add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
            add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1875
            add_all_includes( make, file, file->file );
1876
        }
1877
        if (source->file->flags & FLAG_IDL_PROXY)
1878
        {
1879
            file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1880 1881
            add_dependency( file->file, "objbase.h", INCL_NORMAL );
            add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1882
            add_all_includes( make, file, file->file );
1883
            file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1884 1885 1886 1887
            add_dependency( file->file, "objbase.h", INCL_NORMAL );
            add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
            add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
            add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1888
            add_all_includes( make, file, file->file );
1889
        }
1890 1891 1892 1893
        if (source->file->flags & FLAG_IDL_TYPELIB)
        {
            add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
        }
1894
        if (source->file->flags & FLAG_IDL_REGTYPELIB)
1895
        {
1896
            add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1897
        }
1898
        if (source->file->flags & FLAG_IDL_REGISTER)
1899
        {
1900
            add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1901
        }
1902 1903 1904 1905
        if (source->file->flags & FLAG_IDL_HEADER)
        {
            add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
        }
1906 1907 1908 1909
        if (!source->file->flags && strendswith( source->name, ".idl" ))
        {
            add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
        }
1910 1911 1912 1913
        if (strendswith( source->name, ".x" ))
        {
            add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
        }
1914 1915
        if (strendswith( source->name, ".y" ))
        {
1916
            file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1917 1918 1919 1920 1921 1922
            /* steal the includes list from the source file */
            file->files_count = source->files_count;
            file->files_size = source->files_size;
            file->files = source->files;
            source->files_count = source->files_size = 0;
            source->files = NULL;
1923 1924 1925
        }
        if (strendswith( source->name, ".l" ))
        {
1926
            file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1927 1928 1929 1930 1931 1932
            /* steal the includes list from the source file */
            file->files_count = source->files_count;
            file->files_size = source->files_size;
            file->files = source->files;
            source->files_count = source->files_size = 0;
            source->files = NULL;
1933
        }
1934 1935 1936
        if (source->file->flags & FLAG_C_IMPLIB)
        {
            if (!make->staticimplib && make->importlib && *dll_ext)
1937
                make->staticimplib = strmake( "lib%s.a", make->importlib );
1938
        }
1939 1940 1941 1942 1943
        if (strendswith( source->name, ".po" ))
        {
            if (!make->disabled)
                strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
        }
1944
    }
1945
    if (make->testdll)
1946
    {
1947
        file = add_generated_source( make, "testlist.o", "testlist.c" );
1948
        add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1949
        add_all_includes( make, file, file->file );
1950
    }
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
    for (i = 0; i < objs.count; i++)
    {
        /* default to .c for unknown extra object files */
        if (strendswith( objs.str[i], ".o" ))
            add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
        else if (strendswith( objs.str[i], ".res" ))
            add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
        else
            add_generated_source( make, objs.str[i], NULL );
    }
1961 1962 1963
}


1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
/*******************************************************************
 *         create_dir
 */
static void create_dir( const char *dir )
{
    char *p, *path;

    p = path = xstrdup( dir );
    while ((p = strchr( p, '/' )))
    {
        *p = 0;
        if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
        *p++ = '/';
        while (*p == '/') p++;
    }
    if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
    free( path );
}


1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
/*******************************************************************
 *         create_file_directories
 *
 * Create the base directories of all the files.
 */
static void create_file_directories( const struct makefile *make, struct strarray files )
{
    struct strarray subdirs = empty_strarray;
    unsigned int i;
    char *dir;

    for (i = 0; i < files.count; i++)
    {
        if (!strchr( files.str[i], '/' )) continue;
        dir = base_dir_path( make, files.str[i] );
        *strrchr( dir, '/' ) = 0;
        strarray_add_uniq( &subdirs, dir );
    }

    for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
}


2007 2008 2009
/*******************************************************************
 *         output_filenames_obj_dir
 */
2010
static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
2011 2012 2013
{
    unsigned int i;

2014
    for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
2015 2016 2017
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2018
/*******************************************************************
2019
 *         get_dependencies
Alexandre Julliard's avatar
Alexandre Julliard committed
2020
 */
2021
static void get_dependencies( struct incl_file *file, struct incl_file *source )
Alexandre Julliard's avatar
Alexandre Julliard committed
2022
{
2023 2024 2025
    unsigned int i;

    if (!file->filename) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
2026

2027 2028 2029 2030 2031 2032 2033
    if (file != source)
    {
        if (file->owner == source) return;  /* already processed */
        if (file->type == INCL_IMPORTLIB &&
            !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
            return;  /* library is imported only when building a typelib */
        file->owner = source;
2034
        strarray_add( &source->dependencies, file->filename );
2035
    }
2036
    for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
Alexandre Julliard's avatar
Alexandre Julliard committed
2037 2038 2039
}


2040 2041 2042 2043 2044 2045 2046 2047 2048
/*******************************************************************
 *         get_local_dependencies
 *
 * Get the local dependencies of a given target.
 */
static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
                                               struct strarray targets )
{
    unsigned int i;
2049
    struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061

    for (i = 0; i < deps.count; i++)
    {
        if (strarray_exists( &targets, deps.str[i] ))
            deps.str[i] = obj_dir_path( make, deps.str[i] );
        else
            deps.str[i] = src_dir_path( make, deps.str[i] );
    }
    return deps;
}


2062
/*******************************************************************
2063
 *         get_static_lib
2064
 *
2065
 * Check if makefile builds the named static library and return the full lib path.
2066
 */
2067
static const char *get_static_lib( const struct makefile *make, const char *name )
2068
{
2069 2070 2071 2072 2073
    if (!make->staticlib) return NULL;
    if (strncmp( make->staticlib, "lib", 3 )) return NULL;
    if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
    if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
    return base_dir_path( make, make->staticlib );
2074 2075 2076
}


2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
/*******************************************************************
 *         get_parent_makefile
 */
static struct makefile *get_parent_makefile( struct makefile *make )
{
    char *dir, *p;
    int i;

    if (!make->base_dir) return NULL;
    dir = xstrdup( make->base_dir );
    if (!(p = strrchr( dir, '/' ))) return NULL;
    *p = 0;
    for (i = 0; i < top_makefile->subdirs.count; i++)
        if (!strcmp( top_makefile->submakes[i]->base_dir, dir )) return top_makefile->submakes[i];
    return NULL;
}


/*******************************************************************
2096
 *         needs_cross_lib
2097
 */
2098
static int needs_cross_lib( const struct makefile *make )
2099
{
2100
    if (!crosstarget) return 0;
2101 2102 2103 2104 2105 2106 2107 2108
    if (make->importlib) return strarray_exists( &cross_import_libs, make->importlib );
    if (make->staticlib)
    {
        const char *name = replace_extension( make->staticlib, ".a", "" );
        if (!strncmp( name, "lib", 3 )) name += 3;
        return strarray_exists( &cross_import_libs, name );
    }
    return 0;
2109 2110 2111
}


2112 2113 2114 2115 2116 2117 2118
/*******************************************************************
 *         needs_delay_lib
 */
static int needs_delay_lib( const struct makefile *make )
{
    if (*dll_ext && !crosstarget) return 0;
    if (!make->importlib) return 0;
2119
    return strarray_exists( &delay_import_libs, make->importlib );
2120 2121 2122
}


2123 2124 2125 2126 2127 2128 2129 2130 2131
/*******************************************************************
 *         add_default_libraries
 */
static struct strarray add_default_libraries( const struct makefile *make, struct strarray *deps )
{
    struct strarray ret = empty_strarray;
    struct strarray all_libs = empty_strarray;
    unsigned int i, j;

2132
    if (!make->use_msvcrt) strarray_add( &all_libs, "-lwine_port" );
2133 2134 2135 2136 2137
    strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
    strarray_addall( &all_libs, libs );

    for (i = 0; i < all_libs.count; i++)
    {
2138 2139
        const char *lib = NULL;

2140 2141 2142 2143 2144 2145 2146 2147
        if (!strncmp( all_libs.str[i], "-l", 2 ))
        {
            const char *name = all_libs.str[i] + 2;

            for (j = 0; j < top_makefile->subdirs.count; j++)
            {
                const struct makefile *submake = top_makefile->submakes[j];

2148
                if ((lib = get_static_lib( submake, name ))) break;
2149 2150
            }
        }
2151 2152 2153 2154 2155 2156 2157 2158

        if (lib)
        {
            lib = top_obj_dir_path( make, lib );
            strarray_add( deps, lib );
            strarray_add( &ret, lib );
        }
        else strarray_add( &ret, all_libs.str[i] );
2159 2160 2161 2162 2163
    }
    return ret;
}


2164 2165 2166 2167
/*******************************************************************
 *         add_import_libs
 */
static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2168
                                        struct strarray imports, int delay )
2169 2170 2171 2172 2173 2174 2175
{
    struct strarray ret = empty_strarray;
    unsigned int i, j;

    for (i = 0; i < imports.count; i++)
    {
        const char *name = imports.str[i];
2176
        const char *lib = NULL;
2177 2178 2179 2180 2181 2182 2183

        for (j = 0; j < top_makefile->subdirs.count; j++)
        {
            const struct makefile *submake = top_makefile->submakes[j];

            if (submake->importlib && !strcmp( submake->importlib, name ))
            {
2184
                if (make->is_cross || !*dll_ext || submake->staticimplib)
2185
                    lib = base_dir_path( submake, strmake( "lib%s.a", name ));
2186
                else
2187 2188
                    strarray_add( deps, top_obj_dir_path( make,
                                            strmake( "%s/lib%s.def", submake->base_dir, name )));
2189 2190 2191
                break;
            }

2192 2193
            if ((lib = get_static_lib( submake, name ))) break;
        }
2194

2195 2196
        if (lib)
        {
2197
            if (delay) lib = replace_extension( lib, ".a", ".delay.a" );
2198
            else if (make->is_cross) lib = replace_extension( lib, ".a", ".cross.a" );
2199 2200 2201
            lib = top_obj_dir_path( make, lib );
            strarray_add( deps, lib );
            strarray_add( &ret, lib );
2202
        }
2203
        else strarray_add( &ret, strmake( "-l%s", name ));
2204 2205 2206 2207 2208
    }
    return ret;
}


2209 2210 2211 2212 2213 2214 2215 2216
/*******************************************************************
 *         get_default_imports
 */
static struct strarray get_default_imports( const struct makefile *make )
{
    struct strarray ret = empty_strarray;

    if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
2217
    if (make->use_msvcrt) strarray_add( &ret, "msvcrt" );
2218
    strarray_add( &ret, "winecrt0" );
2219 2220 2221 2222 2223 2224 2225
    if (make->is_win16) strarray_add( &ret, "kernel" );
    strarray_add( &ret, "kernel32" );
    strarray_add( &ret, "ntdll" );
    return ret;
}


2226 2227 2228
/*******************************************************************
 *         add_install_rule
 */
2229 2230
static void add_install_rule( struct makefile *make, const char *target,
                              const char *file, const char *dest )
2231
{
2232 2233 2234
    if (strarray_exists( &make->install_lib, target ) ||
        strarray_exists( &top_install_lib, make->base_dir ) ||
        strarray_exists( &top_install_lib, base_dir_path( make, target )))
2235
    {
2236 2237
        strarray_add( &make->install_rules[INSTALL_LIB], file );
        strarray_add( &make->install_rules[INSTALL_LIB], dest );
2238
    }
2239 2240 2241
    else if (strarray_exists( &make->install_dev, target ) ||
             strarray_exists( &top_install_dev, make->base_dir ) ||
             strarray_exists( &top_install_dev, base_dir_path( make, target )))
2242
    {
2243 2244
        strarray_add( &make->install_rules[INSTALL_DEV], file );
        strarray_add( &make->install_rules[INSTALL_DEV], dest );
2245 2246 2247 2248
    }
}


2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261
/*******************************************************************
 *         get_include_install_path
 *
 * Determine the installation path for a given include file.
 */
static const char *get_include_install_path( const char *name )
{
    if (!strncmp( name, "wine/", 5 )) return name + 5;
    if (!strncmp( name, "msvcrt/", 7 )) return name;
    return strmake( "windows/%s", name );
}


2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
/*******************************************************************
 *         get_shared_library_name
 *
 * Determine possible names for a shared library with a version number.
 */
static struct strarray get_shared_lib_names( const char *libname )
{
    struct strarray ret = empty_strarray;
    const char *ext, *p;
    char *name, *first, *second;
    size_t len = 0;

    strarray_add( &ret, libname );

    for (p = libname; (p = strchr( p, '.' )); p++)
        if ((len = strspn( p + 1, "0123456789." ))) break;

    if (!len) return ret;
    ext = p + 1 + len;
    if (*ext && ext[-1] == '.') ext--;

    /* keep only the first group of digits */
    name = xstrdup( libname );
    first = name + (p - libname);
    if ((second = strchr( first + 1, '.' )))
    {
        strcpy( second, ext );
        strarray_add( &ret, xstrdup( name ));
    }
    /* now remove all digits */
    strcpy( first, ext );
    strarray_add( &ret, name );
    return ret;
}


2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317
/*******************************************************************
 *         get_source_defines
 */
static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
                                           const char *obj )
{
    unsigned int i;
    struct strarray ret = empty_strarray;

    strarray_addall( &ret, make->include_args );
    if (source->use_msvcrt)
        strarray_add( &ret, strmake( "-I%s", top_src_dir_path( make, "include/msvcrt" )));
    for (i = 0; i < make->include_paths.count; i++)
        strarray_add( &ret, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
    strarray_addall( &ret, make->define_args );
    strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
    return ret;
}


2318 2319 2320
/*******************************************************************
 *         output_winegcc_command
 */
2321
static void output_winegcc_command( struct makefile *make )
2322 2323 2324 2325
{
    output( "\t%s -o $@", tools_path( make, "winegcc" ));
    output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
    if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2326
    if (make->is_cross)
2327 2328 2329 2330 2331 2332 2333 2334
    {
        output_filename( "-b" );
        output_filename( crosstarget );
        output_filename( "--lib-suffix=.cross.a" );
    }
    else
    {
        output_filenames( target_flags );
2335
        output_filenames( lddll_flags );
2336 2337 2338 2339
    }
}


2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
/*******************************************************************
 *         output_symlink_rule
 *
 * Output a rule to create a symlink.
 */
static void output_symlink_rule( const char *src_name, const char *link_name )
{
    const char *name;

    output( "\trm -f %s && ", link_name );

    /* dest path with a directory needs special handling if ln -s isn't supported */
    if (strcmp( ln_s, "ln -s" ) && ((name = strrchr( link_name, '/' ))))
    {
        char *dir = xstrdup( link_name );
        dir[name - link_name] = 0;
        output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
        free( dir );
    }
    else
    {
        output( "%s %s %s\n", ln_s, src_name, link_name );
    }
}


2366
/*******************************************************************
2367
 *         output_install_commands
2368
 */
2369 2370
static void output_install_commands( struct makefile *make, const struct makefile *submake,
                                     struct strarray files )
2371 2372
{
    unsigned int i;
2373
    char *install_sh = top_src_dir_path( make, "tools/install-sh" );
2374 2375 2376 2377

    for (i = 0; i < files.count; i += 2)
    {
        const char *file = files.str[i];
2378
        const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2379

2380
        if (submake) file = base_dir_path( submake, file );
2381
        switch (*files.str[i + 1])
2382
        {
2383 2384 2385 2386
        case 'c':  /* cross-compiled program */
            output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
                    crosstarget, install_sh, obj_dir_path( make, file ), dest );
            break;
2387
        case 'd':  /* data file */
2388 2389
            output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
                    install_sh, obj_dir_path( make, file ), dest );
2390 2391
            break;
        case 'D':  /* data file in source dir */
2392 2393
            output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
                    install_sh, src_dir_path( make, file ), dest );
2394 2395
            break;
        case 'p':  /* program file */
2396 2397
            output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
                    install_sh, obj_dir_path( make, file ), dest );
2398 2399
            break;
        case 's':  /* script */
2400 2401
            output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
                    install_sh, obj_dir_path( make, file ), dest );
2402 2403
            break;
        case 'S':  /* script in source dir */
2404 2405 2406 2407 2408
            output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
                    install_sh, src_dir_path( make, file ), dest );
            break;
        case 't':  /* script in tools dir */
            output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2409
                    install_sh, tools_dir_path( make, files.str[i] ), dest );
2410
            break;
2411
        case 'y':  /* symlink */
2412
            output_symlink_rule( files.str[i], dest );
2413
            break;
2414 2415 2416
        default:
            assert(0);
        }
2417
        strarray_add( &make->uninstall_files, dest );
2418
    }
2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440
}


/*******************************************************************
 *         output_install_rules
 *
 * Rules are stored as a (file,dest) pair of values.
 * The first char of dest indicates the type of install.
 */
static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
{
    unsigned int i;
    struct strarray files = make->install_rules[rules];
    struct strarray targets = empty_strarray;

    if (!files.count) return;

    for (i = 0; i < files.count; i += 2)
    {
        const char *file = files.str[i];
        switch (*files.str[i + 1])
        {
2441
        case 'c':  /* cross-compiled program */
2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
        case 'd':  /* data file */
        case 'p':  /* program file */
        case 's':  /* script */
            strarray_add_uniq( &targets, obj_dir_path( make, file ));
            break;
        case 't':  /* script in tools dir */
            strarray_add_uniq( &targets, tools_dir_path( make, file ));
            break;
        }
    }

    output( "install %s::", target );
    output_filenames( targets );
    output( "\n" );
    output_install_commands( make, NULL, files );
2457

2458 2459
    strarray_add_uniq( &make->phony_targets, "install" );
    strarray_add_uniq( &make->phony_targets, target );
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515
}


static int cmp_string_length( const char **a, const char **b )
{
    int paths_a = 0, paths_b = 0;
    const char *p;

    for (p = *a; *p; p++) if (*p == '/') paths_a++;
    for (p = *b; *p; p++) if (*p == '/') paths_b++;
    if (paths_b != paths_a) return paths_b - paths_a;
    return strcmp( *a, *b );
}

/*******************************************************************
 *         output_uninstall_rules
 */
static void output_uninstall_rules( struct makefile *make )
{
    static const char *dirs_order[] =
        { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };

    struct strarray subdirs = empty_strarray;
    unsigned int i, j;

    if (!make->uninstall_files.count) return;
    output( "uninstall::\n" );
    output_rm_filenames( make->uninstall_files );
    strarray_add_uniq( &make->phony_targets, "uninstall" );

    if (!make->subdirs.count) return;
    for (i = 0; i < make->uninstall_files.count; i++)
    {
        char *dir = xstrdup( make->uninstall_files.str[i] );
        while (strchr( dir, '/' ))
        {
            *strrchr( dir, '/' ) = 0;
            strarray_add_uniq( &subdirs, xstrdup(dir) );
        }
    }
    strarray_qsort( &subdirs, cmp_string_length );
    output( "\t-rmdir" );
    for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
    {
        for (j = 0; j < subdirs.count; j++)
        {
            if (!subdirs.str[j]) continue;
            if (strncmp( subdirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
                continue;
            output_filename( subdirs.str[j] );
            subdirs.str[j] = NULL;
        }
    }
    for (j = 0; j < subdirs.count; j++)
        if (subdirs.str[j]) output_filename( subdirs.str[j] );
    output( "\n" );
2516 2517 2518
}


2519 2520 2521 2522 2523 2524 2525
/*******************************************************************
 *         output_importlib_symlinks
 */
static struct strarray output_importlib_symlinks( const struct makefile *parent,
                                                  const struct makefile *make )
{
    struct strarray ret = empty_strarray;
2526 2527
    const char *lib, *dst, *ext[4];
    int i, count = 0;
2528 2529 2530

    if (!make->module) return ret;
    if (!make->importlib) return ret;
2531
    if (make->is_win16 && make->disabled) return ret;
2532 2533 2534 2535 2536 2537 2538
    if (strncmp( make->base_dir, "dlls/", 5 )) return ret;
    if (!strcmp( make->module, make->importlib )) return ret;
    if (!strchr( make->importlib, '.' ) &&
        !strncmp( make->module, make->importlib, strlen( make->importlib )) &&
        !strcmp( make->module + strlen( make->importlib ), ".dll" ))
        return ret;

2539 2540
    ext[count++] = *dll_ext ? "def" : "a";
    if (needs_delay_lib( make )) ext[count++] = "delay.a";
2541
    if (needs_cross_lib( make )) ext[count++] = "cross.a";
2542

2543
    for (i = 0; i < count; i++)
2544
    {
2545
        lib = strmake( "lib%s.%s", make->importlib, ext[i] );
2546 2547 2548 2549
        dst = concat_paths( obj_dir_path( parent, "dlls" ), lib );
        output( "%s: %s\n", dst, base_dir_path( make, lib ));
        output_symlink_rule( concat_paths( make->base_dir + strlen("dlls/"), lib ), dst );
        strarray_add( &ret, dst );
2550 2551 2552 2553 2554
    }
    return ret;
}


2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570
/*******************************************************************
 *         output_po_files
 */
static void output_po_files( const struct makefile *make )
{
    const char *po_dir = src_dir_path( make, "po" );
    struct strarray pot_files = empty_strarray;
    struct incl_file *source;
    unsigned int i;

    for (i = 0; i < make->subdirs.count; i++)
    {
        struct makefile *submake = make->submakes[i];

        LIST_FOR_EACH_ENTRY( source, &submake->sources, struct incl_file, entry )
        {
2571
            if (source->file->flags & FLAG_PARENTDIR) continue;
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
            if (strendswith( source->name, ".rc" ) && (source->file->flags & FLAG_RC_PO))
            {
                char *pot_file = replace_extension( source->name, ".rc", ".pot" );
                char *pot_path = base_dir_path( submake, pot_file );
                output( "%s: tools/wrc include dummy\n", pot_path );
                output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
                strarray_add( &pot_files, pot_path );
            }
            else if (strendswith( source->name, ".mc" ))
            {
                char *pot_file = replace_extension( source->name, ".mc", ".pot" );
                char *pot_path = base_dir_path( submake, pot_file );
                output( "%s: tools/wmc include dummy\n", pot_path );
                output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
                strarray_add( &pot_files, pot_path );
            }
        }
    }
    if (linguas.count)
    {
        for (i = 0; i < linguas.count; i++)
            output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
        output( ": %s/wine.pot\n", po_dir );
        output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
                po_dir );
2597 2598 2599 2600
        output( "po:" );
        for (i = 0; i < linguas.count; i++)
            output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
        output( "\n" );
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
    }
    output( "%s/wine.pot:", po_dir );
    output_filenames( pot_files );
    output( "\n" );
    output( "\tmsgcat -o $@" );
    output_filenames( pot_files );
    output( "\n" );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2611
/*******************************************************************
2612
 *         output_source_y
Alexandre Julliard's avatar
Alexandre Julliard committed
2613
 */
2614
static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
Alexandre Julliard's avatar
Alexandre Julliard committed
2615
{
2616 2617
    /* add source file dependency for parallel makes */
    char *header = strmake( "%s.tab.h", obj );
2618

2619 2620 2621
    if (find_include_file( make, header ))
    {
        output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2622 2623
        output( "\t%s -p %s_ -o %s.tab.c -d %s\n",
                bison, obj, obj_dir_path( make, obj ), source->filename );
2624 2625 2626 2627 2628
        output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
                source->filename, obj_dir_path( make, header ));
        strarray_add( &make->clean_files, header );
    }
    else output( "%s.tab.c: %s\n", obj, source->filename );
2629

2630
    output( "\t%s -p %s_ -o $@ %s\n", bison, obj, source->filename );
2631
}
2632

2633

2634 2635 2636 2637 2638 2639
/*******************************************************************
 *         output_source_l
 */
static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
{
    output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2640
    output( "\t%s -o$@ %s\n", flex, source->filename );
2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651
}


/*******************************************************************
 *         output_source_h
 */
static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
{
    if (source->file->flags & FLAG_GENERATED)
        strarray_add( &make->all_targets, source->name );
    else
2652 2653
        add_install_rule( make, source->name, source->name,
                          strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2654
}
2655 2656


2657 2658 2659 2660 2661
/*******************************************************************
 *         output_source_rc
 */
static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
{
2662
    struct strarray defines = get_source_defines( make, source, obj );
2663
    unsigned int i;
2664

2665
    if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2666
    strarray_add( &make->res_files, strmake( "%s.res", obj ));
2667 2668 2669 2670 2671
    output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
    output( "\t%s -o $@", tools_path( make, "wrc" ) );
    if (make->is_win16) output_filename( "-m16" );
    else output_filenames( target_flags );
    output_filename( "--nostdinc" );
2672
    output_filenames( defines );
2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
    if (linguas.count && (source->file->flags & FLAG_RC_PO))
    {
        char *po_dir = top_obj_dir_path( make, "po" );
        output_filename( strmake( "--po-dir=%s", po_dir ));
        output_filename( source->filename );
        output( "\n" );
        output( "%s.res:", obj_dir_path( make, obj ));
        for (i = 0; i < linguas.count; i++)
            output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
        output( "\n" );
    }
    else
    {
        output_filename( source->filename );
        output( "\n" );
    }
2689
    if (source->file->flags & FLAG_RC_PO && !(source->file->flags & FLAG_PARENTDIR))
2690 2691 2692 2693 2694 2695 2696
    {
        strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
        output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
        output( "\t%s -O pot -o $@", tools_path( make, "wrc" ) );
        if (make->is_win16) output_filename( "-m16" );
        else output_filenames( target_flags );
        output_filename( "--nostdinc" );
2697
        output_filenames( defines );
2698 2699 2700 2701 2702 2703 2704 2705 2706
        output_filename( source->filename );
        output( "\n" );
        output( "%s.pot ", obj_dir_path( make, obj ));
    }
    output( "%s.res:", obj_dir_path( make, obj ));
    output_filename( tools_path( make, "wrc" ));
    output_filenames( source->dependencies );
    output( "\n" );
}
2707

2708

2709 2710 2711 2712 2713 2714
/*******************************************************************
 *         output_source_mc
 */
static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
{
    unsigned int i;
2715

2716
    strarray_add( &make->res_files, strmake( "%s.res", obj ));
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
    strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
    output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
    output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
    if (linguas.count)
    {
        char *po_dir = top_obj_dir_path( make, "po" );
        output_filename( strmake( "--po-dir=%s", po_dir ));
        output( "\n" );
        output( "%s.res:", obj_dir_path( make, obj ));
        for (i = 0; i < linguas.count; i++)
            output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
    }
    output( "\n" );
    output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
    output( "\t%s -O pot -o $@ %s", tools_path( make, "wmc" ), source->filename );
    output( "\n" );
    output( "%s.pot %s.res:", obj_dir_path( make, obj ), obj_dir_path( make, obj ));
    output_filename( tools_path( make, "wmc" ));
    output_filenames( source->dependencies );
    output( "\n" );
}
2738

2739

2740 2741 2742 2743 2744
/*******************************************************************
 *         output_source_res
 */
static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
{
2745
    strarray_add( &make->res_files, source->name );
2746
}
2747 2748


2749 2750 2751 2752 2753
/*******************************************************************
 *         output_source_idl
 */
static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
{
2754
    struct strarray defines = get_source_defines( make, source, obj );
2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
    struct strarray targets = empty_strarray;
    char *dest;
    unsigned int i;

    if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
    if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;

    for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
    {
        if (!(source->file->flags & idl_outputs[i].flag)) continue;
        dest = strmake( "%s%s", obj, idl_outputs[i].ext );
        if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
        strarray_add( &targets, dest );
    }
    if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
    if (source->file->flags & FLAG_INSTALL)
    {
2772 2773
        add_install_rule( make, source->name, xstrdup( source->name ),
                          strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2774
        if (source->file->flags & FLAG_IDL_HEADER)
2775 2776
            add_install_rule( make, source->name, strmake( "%s.h", obj ),
                              strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2777 2778 2779 2780 2781 2782 2783
    }
    if (!targets.count) return;

    output_filenames_obj_dir( make, targets );
    output( ": %s\n", tools_path( make, "widl" ));
    output( "\t%s -o $@", tools_path( make, "widl" ) );
    output_filenames( target_flags );
2784
    output_filenames( defines );
2785
    output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2786
    output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815
    output_filename( source->filename );
    output( "\n" );
    output_filenames_obj_dir( make, targets );
    output( ": %s", source->filename );
    output_filenames( source->dependencies );
    output( "\n" );
}


/*******************************************************************
 *         output_source_tlb
 */
static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
{
    strarray_add( &make->all_targets, source->name );
}


/*******************************************************************
 *         output_source_x
 */
static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
{
    output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
            tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
    output( "\t%s%s -H -o $@ %s\n",
            tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
    if (source->file->flags & FLAG_INSTALL)
    {
2816 2817 2818 2819
        add_install_rule( make, source->name, source->name,
                          strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
        add_install_rule( make, source->name, strmake( "%s.h", obj ),
                          strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829
    }
}


/*******************************************************************
 *         output_source_sfd
 */
static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
{
    unsigned int i;
2830 2831
    char *ttf_obj = strmake( "%s.ttf", obj );
    char *ttf_file = src_dir_path( make, ttf_obj );
2832 2833 2834 2835 2836 2837 2838 2839 2840

    if (fontforge && !make->src_dir)
    {
        output( "%s: %s\n", ttf_file, source->filename );
        output( "\t%s -script %s %s $@\n",
                fontforge, top_src_dir_path( make, "fonts/genttf.ff" ), source->filename );
        if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
    }
    if (source->file->flags & FLAG_INSTALL)
2841 2842
        add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));

2843 2844 2845 2846 2847
    if (source->file->flags & FLAG_SFD_FONTS)
    {
        struct strarray *array = source->file->args;

        for (i = 0; i < array->count; i++)
2848
        {
2849 2850 2851 2852 2853 2854
            char *font = strtok( xstrdup(array->str[i]), " \t" );
            char *args = strtok( NULL, "" );

            strarray_add( &make->all_targets, xstrdup( font ));
            output( "%s: %s %s\n", obj_dir_path( make, font ),
                    tools_path( make, "sfnt2fon" ), ttf_file );
2855
            output( "\t%s -q -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2856
            add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2857
        }
2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
    }
}


/*******************************************************************
 *         output_source_svg
 */
static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
{
    static const char * const images[] = { "bmp", "cur", "ico", NULL };
    unsigned int i;

    if (convert && rsvg && icotool && !make->src_dir)
    {
        for (i = 0; images[i]; i++)
            if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;

        if (images[i])
2876
        {
2877 2878 2879
            output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
            output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
                    top_src_dir_path( make, "tools/buildimage" ), source->filename );
2880
        }
2881 2882 2883 2884
    }
}


2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904
/*******************************************************************
 *         output_source_nls
 */
static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
{
    add_install_rule( make, source->name, source->name,
                      strmake( "D$(datadir)/wine/%s", source->name ));
}


/*******************************************************************
 *         output_source_desktop
 */
static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
{
    add_install_rule( make, source->name, source->name,
                      strmake( "D$(datadir)/applications/%s", source->name ));
}


2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929
/*******************************************************************
 *         output_source_po
 */
static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
{
    output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
    output( "\t%s -o $@ %s\n", msgfmt, source->filename );
    strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
}


/*******************************************************************
 *         output_source_in
 */
static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
{
    unsigned int i;

    if (strendswith( obj, ".man" ) && source->file->args)
    {
        struct strarray symlinks;
        char *dir, *dest = replace_extension( obj, ".man", "" );
        char *lang = strchr( dest, '.' );
        char *section = source->file->args;
        if (lang)
2930
        {
2931 2932
            *lang++ = 0;
            dir = strmake( "$(mandir)/%s/man%s", lang, section );
2933
        }
2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945
        else dir = strmake( "$(mandir)/man%s", section );
        add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
        symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
        for (i = 0; i < symlinks.count; i++)
            add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
                              strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
        free( dest );
        free( dir );
    }
    strarray_add( &make->in_files, xstrdup(obj) );
    strarray_add( &make->all_targets, xstrdup(obj) );
    output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2946
    output( "\t%s %s >$@ || (rm -f $@ && false)\n", sed_cmd, source->filename );
2947 2948 2949 2950 2951 2952 2953
    output( "%s:", obj_dir_path( make, obj ));
    output_filenames( source->dependencies );
    output( "\n" );
    add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
}


2954 2955 2956 2957 2958 2959
/*******************************************************************
 *         output_source_spec
 */
static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
{
    struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2960
    struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
2961
    struct strarray all_libs, dep_libs = empty_strarray;
2962
    char *dll_name, *obj_name;
2963 2964

    if (!imports.count) imports = make->imports;
2965
    if (!dll_flags.count) dll_flags = make->extradllflags;
2966 2967
    all_libs = add_import_libs( make, &dep_libs, imports, 0 );
    add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
2968
    strarray_addall( &all_libs, libs );
2969 2970
    dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
    obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
2971

2972
    strarray_add( &make->clean_files, dll_name );
2973
    strarray_add( &make->res_files, strmake( "%s.res", obj ));
2974 2975 2976
    output( "%s.res: %s\n", obj_dir_path( make, obj ), obj_dir_path( make, dll_name ));
    output( "\techo \"%s.dll TESTDLL \\\"%s\\\"\" | %s -o $@\n", obj,
            obj_dir_path( make, dll_name ), tools_path( make, "wrc" ));
2977

2978
    output( "%s:", obj_dir_path( make, dll_name ));
2979
    output_filename( source->filename );
2980
    output_filename( obj_name );
2981 2982 2983 2984
    output_filenames( dep_libs );
    output_filename( tools_path( make, "winebuild" ));
    output_filename( tools_path( make, "winegcc" ));
    output( "\n" );
2985
    output_winegcc_command( make );
2986
    output_filename( "-s" );
2987
    output_filenames( dll_flags );
2988 2989
    output_filename( "-shared" );
    output_filename( source->filename );
2990
    output_filename( obj_name );
2991
    output_filenames( all_libs );
2992
    output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
2993 2994 2995 2996
    output( "\n" );
}


2997 2998 2999 3000 3001
/*******************************************************************
 *         output_source_default
 */
static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
{
3002
    struct strarray defines = get_source_defines( make, source, obj );
3003 3004 3005
    int is_dll_src = (make->testdll &&
                      strendswith( source->name, ".c" ) &&
                      find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3006
    int need_cross = (crosstarget &&
3007
                      !(source->file->flags & FLAG_C_UNIX) &&
3008 3009 3010
                      (make->is_cross ||
                       ((source->file->flags & FLAG_C_IMPLIB) &&
                        (needs_cross_lib( make ) || needs_delay_lib( make ))) ||
3011
                       (make->staticlib && needs_cross_lib( make ))));
3012 3013 3014 3015
    int need_obj = ((*dll_ext || !make->staticlib || !(source->file->flags & FLAG_C_UNIX)) &&
                    (!need_cross ||
                     (source->file->flags & FLAG_C_IMPLIB) ||
                     (make->module && make->staticlib)));
3016 3017 3018 3019 3020

    if ((source->file->flags & FLAG_GENERATED) &&
        (!make->testdll || !strendswith( source->filename, "testlist.c" )))
        strarray_add( &make->clean_files, source->filename );
    if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
3021 3022

    if (need_obj)
3023
    {
3024 3025 3026 3027 3028 3029
        if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
            strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
        else if (!is_dll_src)
            strarray_add( &make->object_files, strmake( "%s.o", obj ));
        else
            strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3030 3031
        output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
        output( "\t$(CC) -c -o $@ %s", source->filename );
3032
        output_filenames( defines );
3033 3034 3035
        if (make->module || make->staticlib || make->sharedlib || make->testdll)
        {
            output_filenames( dll_flags );
3036
            if (source->use_msvcrt) output_filenames( msvcrt_flags );
3037 3038 3039 3040 3041
        }
        output_filenames( extra_cflags );
        output_filenames( cpp_flags );
        output_filename( "$(CFLAGS)" );
        output( "\n" );
3042
    }
3043
    if (need_cross)
3044
    {
3045
        strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
3046 3047
        output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
        output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
3048
        output_filenames( defines );
3049
        output_filenames( extra_cross_cflags );
3050 3051 3052 3053 3054 3055 3056
        output_filenames( cpp_flags );
        output_filename( "$(CROSSCFLAGS)" );
        output( "\n" );
    }
    if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
    {
        strarray_add( &make->c2man_files, source->filename );
3057
        if (make->testdll && !is_dll_src)
Alexandre Julliard's avatar
Alexandre Julliard committed
3058
        {
3059 3060 3061 3062 3063
            strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
            output( "%s.ok:\n", obj_dir_path( make, obj ));
            output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
                    top_src_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ),
                    make->testdll, replace_extension( make->testdll, ".dll", "_test.exe" ),
3064
                    make->is_cross ? "" : dll_ext, obj );
Alexandre Julliard's avatar
Alexandre Julliard committed
3065 3066
        }
    }
3067 3068
    if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
    if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
    output( ":" );
    output_filenames( source->dependencies );
    output( "\n" );
}


/* dispatch table to output rules for a single source file */
static const struct
{
    const char *ext;
    void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
} output_source_funcs[] =
{
    { "y", output_source_y },
    { "l", output_source_l },
    { "h", output_source_h },
    { "rh", output_source_h },
    { "inl", output_source_h },
    { "rc", output_source_rc },
    { "mc", output_source_mc },
    { "res", output_source_res },
    { "idl", output_source_idl },
    { "tlb", output_source_tlb },
    { "sfd", output_source_sfd },
    { "svg", output_source_svg },
3094 3095
    { "nls", output_source_nls },
    { "desktop", output_source_desktop },
3096 3097 3098
    { "po", output_source_po },
    { "in", output_source_in },
    { "x", output_source_x },
3099
    { "spec", output_source_spec },
3100 3101
    { NULL, output_source_default }
};
3102 3103


3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127
/*******************************************************************
 *         has_object_file
 */
static int has_object_file( struct makefile *make )
{
    struct incl_file *source;
    int i;

    LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
    {
        char *ext = get_extension( source->name );

        if (!ext) fatal_error( "unsupported file type %s\n", source->name );
        ext++;

        for (i = 0; output_source_funcs[i].ext; i++)
            if (!strcmp( ext, output_source_funcs[i].ext )) break;

        if (!output_source_funcs[i].ext) return 1;  /* default extension builds to an object file */
    }
    return 0;
}


3128 3129 3130 3131 3132 3133
/*******************************************************************
 *         output_man_pages
 */
static void output_man_pages( struct makefile *make )
{
    if (make->c2man_files.count)
3134
    {
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
        char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));

        output( "manpages::\n" );
        output( "\t%s -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
        output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
        output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
        output_filename( strmake( "-o %s/man%s",
                                  top_obj_dir_path( make, "documentation" ), man_ext ));
        output_filenames( make->c2man_files );
        output( "\n" );
        output( "htmlpages::\n" );
        output( "\t%s -Th -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
        output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
        output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
        output_filename( strmake( "-o %s",
                                  top_obj_dir_path( make, "documentation/html" )));
        output_filenames( make->c2man_files );
3152
        output( "\n" );
3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172
        output( "sgmlpages::\n" );
        output( "\t%s -Ts -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
        output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
        output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
        output_filename( strmake( "-o %s",
                                  top_obj_dir_path( make, "documentation/api-guide" )));
        output_filenames( make->c2man_files );
        output( "\n" );
        output( "xmlpages::\n" );
        output( "\t%s -Tx -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
        output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
        output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
        output_filename( strmake( "-o %s",
                                  top_obj_dir_path( make, "documentation/api-guide-xml" )));
        output_filenames( make->c2man_files );
        output( "\n" );
        strarray_add( &make->phony_targets, "manpages" );
        strarray_add( &make->phony_targets, "htmlpages" );
        strarray_add( &make->phony_targets, "sgmlpages" );
        strarray_add( &make->phony_targets, "xmlpages" );
3173
    }
3174 3175
    else output( "manpages htmlpages sgmlpages xmlpages::\n" );
}
3176

3177

3178 3179 3180 3181 3182 3183 3184 3185 3186 3187
/*******************************************************************
 *         output_module
 */
static void output_module( struct makefile *make )
{
    struct strarray all_libs = empty_strarray;
    struct strarray dep_libs = empty_strarray;
    char *module_path = obj_dir_path( make, make->module );
    char *spec_file = NULL;
    unsigned int i;
3188

3189
    if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3190 3191 3192
    strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1 ));
    strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0 ));
    add_import_libs( make, &dep_libs, get_default_imports( make ), 0 );  /* dependencies only */
3193

3194
    if (make->is_cross)
3195
    {
3196 3197 3198 3199
        strarray_add( &make->all_targets, strmake( "%s", make->module ));
        add_install_rule( make, make->module, strmake( "%s", make->module ),
                          strmake( "c$(dlldir)/%s", make->module ));
        output( "%s:", module_path );
3200 3201 3202
    }
    else
    {
3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
        strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));

        if (*dll_ext)
        {
            for (i = 0; i < make->delayimports.count; i++)
                strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
            strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
            strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
            add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
                              strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
            add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
                              strmake( "d$(dlldir)/fakedlls/%s", make->module ));
            output( "%s%s %s.fake:", module_path, dll_ext, module_path );
        }
        else
        {
            strarray_add( &make->all_targets, make->module );
            add_install_rule( make, make->module, make->module,
                              strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
            output( "%s:", module_path );
        }
3224
    }
3225

3226
    if (spec_file) output_filename( spec_file );
3227
    output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3228
    output_filenames_obj_dir( make, make->res_files );
3229 3230 3231 3232
    output_filenames( dep_libs );
    output_filename( tools_path( make, "winebuild" ));
    output_filename( tools_path( make, "winegcc" ));
    output( "\n" );
3233 3234
    output_winegcc_command( make );
    if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3235 3236
    if (spec_file)
    {
3237 3238
        output_filename( "-shared" );
        output_filename( spec_file );
3239
    }
3240
    output_filenames( make->extradllflags );
3241
    output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3242
    output_filenames_obj_dir( make, make->res_files );
3243
    output_filenames( all_libs );
3244
    output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3245 3246 3247 3248 3249 3250
    output( "\n" );

    if (spec_file && make->importlib)
    {
        char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
        if (*dll_ext && !make->implib_objs.count)
3251
        {
3252 3253
            strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
            output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
3254
            output( "\t%s -w --def -o $@", tools_path( make, "winebuild" ) );
3255 3256
            output_filenames( target_flags );
            if (make->is_win16) output_filename( "-m16" );
3257 3258
            output_filename( "--export" );
            output_filename( spec_file );
3259 3260 3261 3262
            output( "\n" );
            add_install_rule( make, make->importlib,
                              strmake( "lib%s.def", make->importlib ),
                              strmake( "d$(dlldir)/lib%s.def", make->importlib ));
3263 3264 3265
        }
        else
        {
3266
            strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3267 3268 3269 3270 3271
            if (!*dll_ext && needs_delay_lib( make ))
            {
                strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
                output( "%s.delay.a ", importlib_path );
            }
3272 3273 3274
            output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
            output_filenames_obj_dir( make, make->implib_objs );
            output( "\n" );
3275
            output( "\t%s -w --implib -o $@", tools_path( make, "winebuild" ) );
3276
            output_filenames( target_flags );
3277 3278 3279
            if (make->is_win16) output_filename( "-m16" );
            output_filename( "--export" );
            output_filename( spec_file );
3280 3281 3282 3283 3284
            output_filenames_obj_dir( make, make->implib_objs );
            output( "\n" );
            add_install_rule( make, make->importlib,
                              strmake( "lib%s.a", make->importlib ),
                              strmake( "d$(dlldir)/lib%s.a", make->importlib ));
3285
        }
3286
        if (crosstarget && (needs_cross_lib( make ) || needs_delay_lib( make )))
3287
        {
3288
            struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
3289 3290 3291 3292 3293
            if (needs_cross_lib( make ))
            {
                strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
                output_filename( strmake( "%s.cross.a", importlib_path ));
            }
3294 3295 3296
            if (needs_delay_lib( make ))
            {
                strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3297
                output_filename( strmake( "%s.delay.a", importlib_path ));
3298
            }
3299
            output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3300 3301
            output_filenames_obj_dir( make, cross_files );
            output( "\n" );
3302 3303 3304 3305
            output( "\t%s -b %s -w --implib -o $@", tools_path( make, "winebuild" ), crosstarget );
            if (make->is_win16) output_filename( "-m16" );
            output_filename( "--export" );
            output_filename( spec_file );
3306 3307
            output_filenames_obj_dir( make, cross_files );
            output( "\n" );
3308
        }
3309
    }
3310

3311 3312
    if (spec_file)
        output_man_pages( make );
3313
    else if (*dll_ext && !make->is_win16)
3314 3315 3316
    {
        char *binary = replace_extension( make->module, ".exe", "" );
        add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3317
    }
3318 3319
}

3320

3321 3322 3323 3324 3325 3326 3327 3328
/*******************************************************************
 *         output_static_lib
 */
static void output_static_lib( struct makefile *make )
{
    strarray_add( &make->all_targets, make->staticlib );
    output( "%s:", obj_dir_path( make, make->staticlib ));
    output_filenames_obj_dir( make, make->object_files );
3329
    output_filenames_obj_dir( make, make->unixobj_files );
3330
    output( "\n\trm -f $@\n" );
3331
    output( "\t%s rc $@", ar );
3332
    output_filenames_obj_dir( make, make->object_files );
3333
    output_filenames_obj_dir( make, make->unixobj_files );
3334
    output( "\n\t%s $@\n", ranlib );
3335 3336
    add_install_rule( make, make->staticlib, make->staticlib,
                      strmake( "d$(dlldir)/%s", make->staticlib ));
3337
    if (needs_cross_lib( make ))
3338
    {
3339 3340 3341 3342 3343
        char *name = replace_extension( make->staticlib, ".a", ".cross.a" );

        strarray_add( &make->all_targets, name );
        output( "%s:", obj_dir_path( make, name ));
        output_filenames_obj_dir( make, make->crossobj_files );
3344
        output( "\n\trm -f $@\n" );
3345
        output( "\t%s-ar rc $@", crosstarget );
3346 3347
        output_filenames_obj_dir( make, make->crossobj_files );
        output( "\n\t%s-ranlib $@\n", crosstarget );
3348
    }
3349
}
3350

3351

3352 3353 3354 3355 3356 3357 3358 3359 3360 3361
/*******************************************************************
 *         output_shared_lib
 */
static void output_shared_lib( struct makefile *make )
{
    unsigned int i;
    char *basename, *p;
    struct strarray names = get_shared_lib_names( make->sharedlib );
    struct strarray all_libs = empty_strarray;
    struct strarray dep_libs = empty_strarray;
3362

3363 3364
    basename = xstrdup( make->sharedlib );
    if ((p = strchr( basename, '.' ))) *p = 0;
3365

3366 3367 3368
    strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
    strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
    strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
3369

3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381
    output( "%s:", obj_dir_path( make, make->sharedlib ));
    output_filenames_obj_dir( make, make->object_files );
    output_filenames( dep_libs );
    output( "\n" );
    output( "\t$(CC) -o $@" );
    output_filenames_obj_dir( make, make->object_files );
    output_filenames( all_libs );
    output_filename( "$(LDFLAGS)" );
    output( "\n" );
    add_install_rule( make, make->sharedlib, make->sharedlib,
                      strmake( "p$(libdir)/%s", make->sharedlib ));
    for (i = 1; i < names.count; i++)
3382
    {
3383 3384 3385 3386
        output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
        output_symlink_rule( obj_dir_path( make, names.str[i-1] ), obj_dir_path( make, names.str[i] ));
        add_install_rule( make, names.str[i], names.str[i-1],
                          strmake( "y$(libdir)/%s", names.str[i] ));
3387
    }
3388 3389
    strarray_addall( &make->all_targets, names );
}
3390

3391 3392 3393 3394 3395 3396 3397 3398 3399 3400

/*******************************************************************
 *         output_test_module
 */
static void output_test_module( struct makefile *make )
{
    char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
    char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
    char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
    struct strarray dep_libs = empty_strarray;
3401
    struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0 );
3402
    struct makefile *parent = get_parent_makefile( make );
3403 3404
    const char *ext = make->is_cross ? "" : dll_ext;
    const char *parent_ext = parent && parent->is_cross ? "" : dll_ext;
3405

3406
    add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
3407
    strarray_addall( &all_libs, libs );
3408 3409 3410
    strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
    strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
    output( "%s%s:\n", obj_dir_path( make, testmodule ), ext );
3411
    output_winegcc_command( make );
3412
    output_filenames( make->extradllflags );
3413
    output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3414
    output_filenames_obj_dir( make, make->res_files );
3415
    output_filenames( all_libs );
3416
    output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3417
    output( "\n" );
3418
    output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3419
    output_winegcc_command( make );
3420
    output_filename( "-s" );
3421
    output_filename( strmake( "-Wb,-F,%s", testmodule ));
3422
    output_filenames( make->extradllflags );
3423
    output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3424
    output_filenames_obj_dir( make, make->res_files );
3425
    output_filenames( all_libs );
3426
    output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3427
    output( "\n" );
3428
    output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3429
    output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3430
    output_filenames_obj_dir( make, make->res_files );
3431 3432 3433 3434 3435
    output_filenames( dep_libs );
    output_filename( tools_path( make, "winebuild" ));
    output_filename( tools_path( make, "winegcc" ));
    output( "\n" );

3436
    if (!make->disabled && !strarray_exists( &disabled_dirs, "programs/winetest" ))
3437 3438
        output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
    output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
3439
            obj_dir_path( make, stripped ), ext );
3440
    output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
3441
            testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3442

3443
    output_filenames_obj_dir( make, make->ok_files );
3444
    output( ": %s%s ../%s%s\n", testmodule, ext, make->testdll, parent_ext );
3445
    output( "check test:" );
3446
    if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3447 3448 3449
    output( "\n" );
    strarray_add( &make->phony_targets, "check" );
    strarray_add( &make->phony_targets, "test" );
3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
    output( "testclean::\n" );
    output( "\trm -f" );
    output_filenames_obj_dir( make, make->ok_files );
    output( "\n" );
    strarray_addall( &make->clean_files, make->ok_files );
    strarray_add( &make->phony_targets, "testclean" );
}


/*******************************************************************
 *         output_programs
 */
static void output_programs( struct makefile *make )
{
    unsigned int i, j;
    char *ldrpath_local   = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
    char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
3467

3468 3469
    for (i = 0; i < make->programs.count; i++)
    {
3470
        char *program_installed = NULL;
3471
        char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3472
        struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3473 3474 3475
        struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
        struct strarray objs     = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
        struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3476

3477
        if (!objs.count) objs = make->object_files;
3478 3479
        strarray_addall( &all_libs, add_default_libraries( make, &deps ));

3480 3481
        output( "%s:", obj_dir_path( make, program ) );
        output_filenames_obj_dir( make, objs );
3482
        output_filenames( deps );
3483 3484 3485
        output( "\n" );
        output( "\t$(CC) -o $@" );
        output_filenames_obj_dir( make, objs );
3486 3487 3488 3489 3490 3491

        if (strarray_exists( &all_libs, "-lwine" ))
        {
            strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
            if (ldrpath_local && ldrpath_install)
            {
3492
                program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
3493 3494 3495 3496 3497 3498
                output_filename( ldrpath_local );
                output_filenames( all_libs );
                output_filename( "$(LDFLAGS)" );
                output( "\n" );
                output( "%s:", obj_dir_path( make, program_installed ) );
                output_filenames_obj_dir( make, objs );
3499
                output_filenames( deps );
3500 3501 3502 3503
                output( "\n" );
                output( "\t$(CC) -o $@" );
                output_filenames_obj_dir( make, objs );
                output_filename( ldrpath_install );
3504
                strarray_add( &make->all_targets, program_installed );
3505 3506 3507
            }
        }

3508 3509 3510
        output_filenames( all_libs );
        output_filename( "$(LDFLAGS)" );
        output( "\n" );
3511
        strarray_add( &make->all_targets, program );
3512

3513
        for (j = 0; j < symlinks.count; j++)
3514
        {
3515 3516
            output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
            output_symlink_rule( obj_dir_path( make, program ), obj_dir_path( make, symlinks.str[j] ));
3517
        }
3518
        strarray_addall( &make->all_targets, symlinks );
3519

3520
        add_install_rule( make, program, program_installed ? program_installed : program,
3521
                          strmake( "p$(bindir)/%s", program ));
3522
        for (j = 0; j < symlinks.count; j++)
3523
            add_install_rule( make, symlinks.str[j], program,
3524
                              strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3525
    }
3526 3527 3528 3529 3530 3531 3532 3533
}


/*******************************************************************
 *         output_subdirs
 */
static void output_subdirs( struct makefile *make )
{
3534
    struct strarray symlinks = empty_strarray;
3535
    struct strarray all_deps = empty_strarray;
3536
    struct strarray build_deps = empty_strarray;
3537
    struct strarray builddeps_deps = empty_strarray;
3538
    struct strarray makefile_deps = empty_strarray;
3539
    struct strarray clean_files = empty_strarray;
3540
    struct strarray testclean_files = empty_strarray;
3541
    struct strarray distclean_files = empty_strarray;
3542
    struct strarray tools_deps = empty_strarray;
3543
    struct strarray tooldeps_deps = empty_strarray;
3544
    struct strarray winetest_deps = empty_strarray;
3545
    unsigned int i, j;
3546

3547
    strarray_addall( &distclean_files, make->distclean_files );
3548 3549 3550
    for (i = 0; i < make->subdirs.count; i++)
    {
        const struct makefile *submake = make->submakes[i];
3551
        char *subdir = base_dir_path( submake, "" );
3552 3553 3554

        strarray_add( &makefile_deps, top_src_dir_path( make, base_dir_path( submake,
                                                        strmake ( "%s.in", output_makefile_name ))));
3555 3556
        for (j = 0; j < submake->clean_files.count; j++)
            strarray_add( &clean_files, base_dir_path( submake, submake->clean_files.str[j] ));
3557 3558 3559 3560
        for (j = 0; j < submake->distclean_files.count; j++)
            strarray_add( &distclean_files, base_dir_path( submake, submake->distclean_files.str[j] ));
        for (j = 0; j < submake->ok_files.count; j++)
            strarray_add( &testclean_files, base_dir_path( submake, submake->ok_files.str[j] ));
3561 3562 3563 3564 3565 3566 3567

        /* import libs are still created for disabled dirs, except for win16 ones */
        if (submake->module && submake->importlib && !(submake->disabled && submake->is_win16))
        {
            char *importlib_path = base_dir_path( submake, strmake( "lib%s", submake->importlib ));
            if (submake->implib_objs.count)
            {
3568
                output( "%s.a: dummy\n", importlib_path );
3569
                output( "\t@cd %s && $(MAKE) lib%s.a\n", subdir, submake->importlib );
3570
                strarray_add( &tools_deps, strmake( "%s.a", importlib_path ));
3571
                if (needs_cross_lib( submake ))
3572
                {
3573
                    output( "%s.cross.a: dummy\n", importlib_path );
3574
                    output( "\t@cd %s && $(MAKE) lib%s.cross.a\n", subdir, submake->importlib );
3575
                    strarray_add( &tools_deps, strmake( "%s.cross.a", importlib_path ));
3576
                }
3577 3578 3579 3580 3581 3582
                if (needs_delay_lib( submake ))
                {
                    output( "%s.delay.a: dummy\n", importlib_path );
                    output( "\t@cd %s && $(MAKE) lib%s.delay.a\n", subdir, submake->importlib );
                    strarray_add( &tools_deps, strmake( "%s.delay.a", importlib_path ));
                }
3583 3584 3585 3586 3587
            }
            else
            {
                char *spec_file = top_src_dir_path( make, base_dir_path( submake,
                                                   replace_extension( submake->module, ".dll", ".spec" )));
3588 3589 3590 3591 3592 3593 3594
                if (*dll_ext)
                {
                    output( "%s.def: %s", importlib_path, spec_file );
                    strarray_add( &build_deps, strmake( "%s.def", importlib_path ));
                }
                else
                {
3595 3596 3597 3598 3599
                    if (needs_delay_lib( submake ))
                    {
                        output( "%s.delay.a ", importlib_path );
                        strarray_add( &build_deps, strmake( "%s.delay.a", importlib_path ));
                    }
3600
                    output( "%s.a: %s", importlib_path, spec_file );
3601
                    strarray_add( &build_deps, strmake( "%s.a", importlib_path ));
3602
                }
3603 3604 3605 3606 3607 3608 3609 3610 3611
                output_filename( tools_path( make, "winebuild" ));
                output( "\n" );
                output( "\t%s -w -o $@", tools_path( make, "winebuild" ));
                output_filename( *dll_ext ? "--def" : "--implib" );
                output_filenames( target_flags );
                if (submake->is_win16) output_filename( "-m16" );
                output_filename( "--export" );
                output_filename( spec_file );
                output( "\n" );
3612
                if (crosstarget && (needs_cross_lib( submake ) || needs_delay_lib( submake )))
3613
                {
3614 3615 3616 3617 3618 3619 3620 3621 3622 3623
                    if (needs_cross_lib( submake ))
                    {
                        output_filename( strmake( "%s.cross.a", importlib_path ));
                        strarray_add( &build_deps, strmake( "%s.cross.a", importlib_path ));
                    }
                    if (needs_delay_lib( submake ))
                    {
                        output_filename( strmake( "%s.delay.a", importlib_path ));
                        strarray_add( &build_deps, strmake( "%s.delay.a", importlib_path ));
                    }
3624
                    output( ": %s", spec_file );
3625 3626 3627
                    output_filename( tools_path( make, "winebuild" ));
                    output( "\n" );
                    output( "\t%s -b %s -w -o $@", tools_path( make, "winebuild" ), crosstarget );
3628
                    if (submake->is_win16) output_filename( "-m16" );
3629 3630 3631 3632 3633 3634 3635 3636 3637
                    output_filename( "--implib" );
                    output_filename( "--export" );
                    output_filename( spec_file );
                    output( "\n" );
                }
            }
            strarray_addall( &symlinks, output_importlib_symlinks( make, submake ));
        }

3638
        if (submake->disabled) continue;
3639 3640 3641 3642 3643 3644 3645
        strarray_add( &all_deps, subdir );

        if (submake->module)
        {
            if (!submake->staticlib)
            {
                strarray_add( &builddeps_deps, subdir );
3646
                if (!submake->is_exe)
3647 3648 3649 3650 3651 3652 3653 3654
                {
                    output( "manpages htmlpages sgmlpages xmlpages::\n" );
                    output( "\t@cd %s && $(MAKE) $@\n", subdir );
                }
            }
            else strarray_add( &tools_deps, subdir );
        }
        else if (submake->testdll)
3655
        {
3656
            output( "%s/test: dummy\n", subdir );
3657 3658
            output( "\t@cd %s && $(MAKE) test\n", subdir );
            strarray_add( &winetest_deps, subdir );
3659
            strarray_add( &builddeps_deps, subdir );
3660
        }
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684
        else
        {
            if (!strcmp( submake->base_dir, "tools" ) || !strncmp( submake->base_dir, "tools/", 6 ))
            {
                strarray_add( &tooldeps_deps, submake->base_dir );
                for (j = 0; j < submake->programs.count; j++)
                    output( "%s/%s%s: %s\n", submake->base_dir,
                            submake->programs.str[j], tools_ext, submake->base_dir );
            }
            if (submake->programs.count || submake->sharedlib)
            {
                struct strarray libs = get_expanded_make_var_array( submake, "EXTRALIBS" );
                for (j = 0; j < submake->programs.count; j++)
                    strarray_addall( &libs, get_expanded_file_local_var( submake,
                                                                    submake->programs.str[j], "LDFLAGS" ));
                output( "%s: libs/port", submake->base_dir );
                for (j = 0; j < libs.count; j++)
                {
                    if (!strcmp( libs.str[j], "-lwpp" )) output_filename( "libs/wpp" );
                    if (!strcmp( libs.str[j], "-lwine" )) output_filename( "libs/wine" );
                }
                output( "\n" );
            }
        }
3685

3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
        if (submake->install_rules[INSTALL_LIB].count)
        {
            output( "install install-lib:: %s\n", submake->base_dir );
            output_install_commands( make, submake, submake->install_rules[INSTALL_LIB] );
        }
        if (submake->install_rules[INSTALL_DEV].count)
        {
            output( "install install-dev:: %s\n", submake->base_dir );
            output_install_commands( make, submake, submake->install_rules[INSTALL_DEV] );
        }
3696
    }
3697 3698 3699 3700 3701 3702
    output( "all:" );
    output_filenames( all_deps );
    output( "\n" );
    output_filenames( all_deps );
    output( ": dummy\n" );
    output( "\t@cd $@ && $(MAKE)\n" );
3703 3704 3705 3706 3707
    output( "Makefile:" );
    output_filenames( makefile_deps );
    output( "\n" );
    output_filenames( makefile_deps );
    output( ":\n" );
3708 3709 3710 3711 3712 3713 3714
    if (tooldeps_deps.count)
    {
        output( "__tooldeps__:" );
        output_filenames( tooldeps_deps );
        output( "\n" );
        strarray_add( &make->phony_targets, "__tooldeps__" );
    }
3715 3716
    if (winetest_deps.count)
    {
3717
        output( "buildtests programs/winetest:" );
3718 3719
        output_filenames( winetest_deps );
        output( "\n" );
3720 3721 3722 3723 3724 3725 3726 3727
        output( "check test:" );
        for (i = 0; i < winetest_deps.count; i++)
        {
            char *target = strmake( "%s/test", winetest_deps.str[i] );
            output_filename( target );
            strarray_add( &make->phony_targets, target );
        }
        output( "\n" );
3728
        strarray_add( &make->phony_targets, "buildtests" );
3729 3730 3731
        strarray_add( &make->phony_targets, "check" );
        strarray_add( &make->phony_targets, "test" );
    }
3732

3733 3734
    output( "clean::\n");
    output_rm_filenames( clean_files );
3735 3736
    output( "testclean::\n");
    output_rm_filenames( testclean_files );
3737
    output( "distclean::\n");
3738
    output_rm_filenames( distclean_files );
3739 3740 3741 3742 3743 3744 3745 3746 3747
    output_filenames( tools_deps );
    output( ":" );
    output_filename( tools_dir_path( make, "widl" ));
    output_filename( tools_dir_path( make, "winebuild" ));
    output_filename( tools_dir_path( make, "winegcc" ));
    output_filename( obj_dir_path( make, "include" ));
    output( "\n" );
    output_filenames( builddeps_deps );
    output( ": __builddeps__\n" );
3748
    strarray_add( &make->phony_targets, "distclean" );
3749
    strarray_add( &make->phony_targets, "testclean" );
3750
    strarray_addall( &make->phony_targets, all_deps );
3751

3752
    strarray_addall( &make->clean_files, symlinks );
3753
    strarray_addall( &build_deps, tools_deps );
3754
    strarray_addall( &build_deps, symlinks );
3755 3756 3757 3758 3759
    if (build_deps.count)
    {
        output( "__builddeps__:" );
        output_filenames( build_deps );
        output( "\n" );
3760
        strarray_add( &make->phony_targets, "__builddeps__" );
3761 3762 3763 3764 3765 3766 3767 3768
    }
    if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
}


/*******************************************************************
 *         output_sources
 */
3769
static void output_sources( struct makefile *make )
3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787
{
    struct incl_file *source;
    unsigned int i, j;

    strarray_add( &make->phony_targets, "all" );

    LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
    {
        char *obj = xstrdup( source->name );
        char *ext = get_extension( obj );

        if (!ext) fatal_error( "unsupported file type %s\n", source->name );
        *ext++ = 0;

        for (j = 0; output_source_funcs[j].ext; j++)
            if (!strcmp( ext, output_source_funcs[j].ext )) break;

        output_source_funcs[j].fn( make, source, obj );
3788
        strarray_addall_uniq( &make->dependencies, source->dependencies );
3789 3790
    }

3791 3792 3793 3794 3795 3796 3797 3798 3799
    /* special case for winetest: add resource files from other test dirs */
    if (make->base_dir && !strcmp( make->base_dir, "programs/winetest" ))
    {
        struct strarray tests = enable_tests;
        if (!tests.count)
            for (i = 0; i < top_makefile->subdirs.count; i++)
                if (top_makefile->submakes[i]->testdll && !top_makefile->submakes[i]->disabled)
                    strarray_add( &tests, top_makefile->submakes[i]->testdll );
        for (i = 0; i < tests.count; i++)
3800
            strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3801 3802
    }

3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814
    if (make->dlldata_files.count)
    {
        output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
                tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
        output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
        output_filenames( make->dlldata_files );
        output( "\n" );
    }

    if (make->staticlib) output_static_lib( make );
    else if (make->module) output_module( make );
    else if (make->testdll) output_test_module( make );
3815 3816
    else if (make->sharedlib) output_shared_lib( make );
    else if (make->programs.count) output_programs( make );
3817

3818
    for (i = 0; i < make->scripts.count; i++)
3819
        add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3820 3821
                          strmake( "S$(bindir)/%s", make->scripts.str[i] ));

3822 3823 3824 3825 3826 3827 3828 3829 3830
    if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
    strarray_add( &make->distclean_files, "Makefile" );
    if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );

    if (!make->base_dir)
        strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
    else if (!strcmp( make->base_dir, "po" ))
        strarray_add( &make->distclean_files, "LINGUAS" );

3831 3832
    if (make->subdirs.count) output_subdirs( make );

3833
    if (!make->disabled)
3834
    {
3835
        if (make->all_targets.count)
3836 3837
        {
            output( "all:" );
3838
            output_filenames_obj_dir( make, make->all_targets );
3839 3840
            output( "\n" );
        }
3841 3842 3843
        output_install_rules( make, INSTALL_LIB, "install-lib" );
        output_install_rules( make, INSTALL_DEV, "install-dev" );
        output_uninstall_rules( make );
3844
    }
3845

3846 3847 3848 3849 3850 3851
    if (make->dependencies.count)
    {
        output_filenames( make->dependencies );
        output( ":\n" );
    }

3852
    strarray_addall( &make->clean_files, make->object_files );
3853
    strarray_addall( &make->clean_files, make->crossobj_files );
3854
    strarray_addall( &make->clean_files, make->unixobj_files );
3855
    strarray_addall( &make->clean_files, make->res_files );
3856
    strarray_addall( &make->clean_files, make->all_targets );
3857
    strarray_addall( &make->clean_files, make->extra_targets );
3858

3859
    if (make->clean_files.count)
3860 3861 3862
    {
        output( "%s::\n", obj_dir_path( make, "clean" ));
        output( "\trm -f" );
3863
        output_filenames_obj_dir( make, make->clean_files );
3864 3865
        output( "\n" );
        if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
3866
        strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3867 3868
    }

3869
    if (make->phony_targets.count)
3870
    {
3871
        output( ".PHONY:" );
3872
        output_filenames( make->phony_targets );
3873 3874
        output( "\n" );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
3875 3876 3877
}


3878 3879 3880
/*******************************************************************
 *         create_temp_file
 */
3881
static FILE *create_temp_file( const char *orig )
3882
{
3883
    char *name = xmalloc( strlen(orig) + 13 );
3884 3885 3886 3887 3888 3889
    unsigned int i, id = getpid();
    int fd;
    FILE *ret = NULL;

    for (i = 0; i < 100; i++)
    {
3890
        sprintf( name, "%s.tmp%08x", orig, id );
3891
        if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3892 3893 3894 3895 3896 3897 3898
        {
            ret = fdopen( fd, "w" );
            break;
        }
        if (errno != EEXIST) break;
        id += 7777;
    }
3899
    if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3900
    temp_file_name = name;
3901 3902 3903 3904
    return ret;
}


3905 3906 3907
/*******************************************************************
 *         rename_temp_file
 */
3908
static void rename_temp_file( const char *dest )
3909
{
3910
    int ret = rename( temp_file_name, dest );
3911 3912 3913 3914
    if (ret == -1 && errno == EEXIST)
    {
        /* rename doesn't overwrite on windows */
        unlink( dest );
3915
        ret = rename( temp_file_name, dest );
3916
    }
3917 3918
    if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
    temp_file_name = NULL;
3919 3920 3921
}


3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964
/*******************************************************************
 *         are_files_identical
 */
static int are_files_identical( FILE *file1, FILE *file2 )
{
    for (;;)
    {
        char buffer1[8192], buffer2[8192];
        int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
        int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
        if (size1 != size2) return 0;
        if (!size1) return feof( file1 ) && feof( file2 );
        if (memcmp( buffer1, buffer2, size1 )) return 0;
    }
}


/*******************************************************************
 *         rename_temp_file_if_changed
 */
static void rename_temp_file_if_changed( const char *dest )
{
    FILE *file1, *file2;
    int do_rename = 1;

    if ((file1 = fopen( dest, "r" )))
    {
        if ((file2 = fopen( temp_file_name, "r" )))
        {
            do_rename = !are_files_identical( file1, file2 );
            fclose( file2 );
        }
        fclose( file1 );
    }
    if (!do_rename)
    {
        unlink( temp_file_name );
        temp_file_name = NULL;
    }
    else rename_temp_file( dest );
}


3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985
/*******************************************************************
 *         output_linguas
 */
static void output_linguas( const struct makefile *make )
{
    const char *dest = base_dir_path( make, "LINGUAS" );
    struct incl_file *source;

    output_file = create_temp_file( dest );

    output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
    LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
        if (strendswith( source->name, ".po" ))
            output( "%s\n", replace_extension( source->name, ".po", "" ));

    if (fclose( output_file )) fatal_perror( "write" );
    output_file = NULL;
    rename_temp_file_if_changed( dest );
}


3986 3987 3988
/*******************************************************************
 *         output_testlist
 */
3989
static void output_testlist( const struct makefile *make )
3990
{
3991 3992 3993 3994
    const char *dest = base_dir_path( make, "testlist.c" );
    struct strarray files = empty_strarray;
    unsigned int i;

3995 3996
    for (i = 0; i < make->ok_files.count; i++)
        strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019

    output_file = create_temp_file( dest );

    output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
    output( "#define WIN32_LEAN_AND_MEAN\n" );
    output( "#include <windows.h>\n\n" );
    output( "#define STANDALONE\n" );
    output( "#include \"wine/test.h\"\n\n" );

    for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
    output( "\n" );
    output( "const struct test winetest_testlist[] =\n" );
    output( "{\n" );
    for (i = 0; i < files.count; i++) output( "    { \"%s\", func_%s },\n", files.str[i], files.str[i] );
    output( "    { 0, 0 }\n" );
    output( "};\n" );

    if (fclose( output_file )) fatal_perror( "write" );
    output_file = NULL;
    rename_temp_file_if_changed( dest );
}


4020 4021 4022
/*******************************************************************
 *         output_gitignore
 */
4023
static void output_gitignore( const char *dest, struct strarray files )
4024 4025 4026
{
    int i;

4027
    output_file = create_temp_file( dest );
4028 4029

    output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4030
    for (i = 0; i < files.count; i++)
4031
    {
4032 4033
        if (!strchr( files.str[i], '/' )) output( "/" );
        output( "%s\n", files.str[i] );
4034 4035
    }

4036
    if (fclose( output_file )) fatal_perror( "write" );
4037
    output_file = NULL;
4038
    rename_temp_file( dest );
4039 4040 4041
}


4042 4043 4044
/*******************************************************************
 *         output_top_variables
 */
4045
static void output_top_variables( const struct makefile *make )
4046 4047 4048 4049 4050 4051 4052 4053 4054
{
    unsigned int i;
    struct strarray *vars = &top_makefile->vars;

    if (!make->base_dir) return;  /* don't output variables in the top makefile */

    output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
    output( "all:\n\n" );
    for (i = 0; i < vars->count; i += 2)
4055 4056
    {
        if (!strcmp( vars->str[i], "SUBDIRS" )) continue;  /* not inherited */
4057
        output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
4058
    }
4059 4060 4061 4062
    output( "\n" );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4063 4064 4065
/*******************************************************************
 *         output_dependencies
 */
4066
static void output_dependencies( struct makefile *make )
Alexandre Julliard's avatar
Alexandre Julliard committed
4067
{
4068
    struct strarray ignore_files = empty_strarray;
4069
    char buffer[1024];
4070
    FILE *src_file;
4071
    int found = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
4072

4073 4074
    if (make->base_dir) create_dir( make->base_dir );

4075
    output_file_name = base_dir_path( make, output_makefile_name );
4076 4077
    output_file = create_temp_file( output_file_name );
    output_top_variables( make );
4078

4079
    /* copy the contents of the source makefile */
4080
    src_file = open_input_makefile( make );
4081 4082
    while (fgets( buffer, sizeof(buffer), src_file ) && !found)
    {
4083
        if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4084 4085
        found = !strncmp( buffer, separator, strlen(separator) );
    }
4086
    if (fclose( src_file )) fatal_perror( "close" );
4087
    input_file_name = NULL;
4088

4089
    if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4090
    output_sources( make );
4091 4092 4093

    fclose( output_file );
    output_file = NULL;
4094
    rename_temp_file( output_file_name );
4095

4096 4097 4098 4099
    strarray_addall( &ignore_files, make->distclean_files );
    strarray_addall( &ignore_files, make->clean_files );
    if (make->testdll) output_testlist( make );
    if (make->base_dir && !strcmp( make->base_dir, "po" )) output_linguas( make );
4100
    if (!make->src_dir) output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
4101

4102
    create_file_directories( make, ignore_files );
4103

4104
    output_file_name = NULL;
4105 4106 4107 4108
}


/*******************************************************************
4109
 *         load_sources
4110
 */
4111
static void load_sources( struct makefile *make )
4112 4113 4114
{
    static const char *source_vars[] =
    {
4115
        "SOURCES",
4116 4117 4118 4119
        "C_SRCS",
        "OBJC_SRCS",
        "RC_SRCS",
        "MC_SRCS",
4120
        "IDL_SRCS",
4121 4122
        "BISON_SRCS",
        "LEX_SRCS",
4123
        "HEADER_SRCS",
4124
        "XTEMPLATE_SRCS",
4125
        "SVG_SRCS",
4126
        "FONT_SRCS",
4127
        "IN_SRCS",
4128
        "PO_SRCS",
4129 4130 4131 4132 4133 4134 4135 4136
        "MANPAGES",
        NULL
    };
    const char **var;
    unsigned int i;
    struct strarray value;
    struct incl_file *file;

4137 4138 4139 4140 4141
    if (root_src_dir)
    {
        make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
        make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
    }
4142
    strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
4143
    strarray_set_value( &make->vars, "top_srcdir", top_src_dir_path( make, "" ));
4144
    strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4145

4146 4147 4148
    make->parent_dir    = get_expanded_make_variable( make, "PARENTSRC" );
    make->module        = get_expanded_make_variable( make, "MODULE" );
    make->testdll       = get_expanded_make_variable( make, "TESTDLL" );
4149
    make->sharedlib     = get_expanded_make_variable( make, "SHAREDLIB" );
4150 4151
    make->staticlib     = get_expanded_make_variable( make, "STATICLIB" );
    make->importlib     = get_expanded_make_variable( make, "IMPORTLIB" );
4152

4153
    make->programs      = get_expanded_make_var_array( make, "PROGRAMS" );
4154
    make->scripts       = get_expanded_make_var_array( make, "SCRIPTS" );
4155 4156 4157
    make->imports       = get_expanded_make_var_array( make, "IMPORTS" );
    make->delayimports  = get_expanded_make_var_array( make, "DELAYIMPORTS" );
    make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4158 4159
    make->install_lib   = get_expanded_make_var_array( make, "INSTALL_LIB" );
    make->install_dev   = get_expanded_make_var_array( make, "INSTALL_DEV" );
4160
    make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4161 4162

    if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
4163
    if (make->testdll) strarray_add( &make->extradllflags, "-mno-cygwin" );
4164

4165
    strarray_addall( &make->extradllflags, get_expanded_make_var_array( make, "APPMODE" ));
4166
    make->disabled   = make->base_dir && strarray_exists( &disabled_dirs, make->base_dir );
4167 4168 4169 4170
    make->is_win16   = strarray_exists( &make->extradllflags, "-m16" );
    make->use_msvcrt = strarray_exists( &make->extradllflags, "-mno-cygwin" );
    make->is_exe     = strarray_exists( &make->extradllflags, "-mconsole" ) ||
                       strarray_exists( &make->extradllflags, "-mwindows" );
4171

4172 4173 4174 4175 4176 4177
    if (make->module && !make->install_lib.count && !make->install_dev.count)
    {
        if (make->importlib) strarray_add( &make->install_dev, make->importlib );
        if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
        else strarray_add( &make->install_lib, make->module );
    }
4178

4179
    make->include_paths = empty_strarray;
4180
    make->include_args = empty_strarray;
4181 4182
    make->define_args = empty_strarray;
    strarray_add( &make->define_args, "-D__WINESRC__" );
4183

4184
    value = get_expanded_make_var_array( make, "EXTRAINCL" );
4185 4186
    for (i = 0; i < value.count; i++)
        if (!strncmp( value.str[i], "-I", 2 ))
4187
            strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4188
        else
4189
            strarray_add_uniq( &make->define_args, value.str[i] );
4190
    strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4191

4192 4193 4194 4195 4196 4197 4198 4199 4200
    strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
    if (make->src_dir)
        strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
    if (make->parent_dir)
        strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
    strarray_add( &make->include_args, strmake( "-I%s", top_obj_dir_path( make, "include" )));
    if (make->top_src_dir)
        strarray_add( &make->include_args, strmake( "-I%s", top_src_dir_path( make, "include" )));

4201
    list_init( &make->sources );
4202
    list_init( &make->includes );
4203 4204 4205

    for (var = source_vars; *var; var++)
    {
4206 4207
        value = get_expanded_make_var_array( make, *var );
        for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4208 4209
    }

4210
    add_generated_sources( make );
4211

4212
    LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4213
    LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4214

4215
    if (crosstarget) make->is_cross = (make->testdll || make->use_msvcrt || !has_object_file( make ));
4216 4217 4218 4219 4220

    if (make->is_cross)
    {
        for (i = 0; i < make->imports.count; i++)
            strarray_add_uniq( &cross_import_libs, make->imports.str[i] );
4221
        if (make->use_msvcrt) strarray_add_uniq( &cross_import_libs, "msvcrt" );
4222
        if (make->is_win16) strarray_add_uniq( &cross_import_libs, "kernel" );
4223 4224 4225 4226 4227 4228
        strarray_add_uniq( &cross_import_libs, "winecrt0" );
        strarray_add_uniq( &cross_import_libs, "kernel32" );
        strarray_add_uniq( &cross_import_libs, "ntdll" );
    }

    if (!*dll_ext || make->is_cross)
4229
        for (i = 0; i < make->delayimports.count; i++)
4230
            strarray_add_uniq( &delay_import_libs, make->delayimports.str[i] );
Alexandre Julliard's avatar
Alexandre Julliard committed
4231 4232 4233
}


4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256
/*******************************************************************
 *         parse_makeflags
 */
static void parse_makeflags( const char *flags )
{
    const char *p = flags;
    char *var, *buffer = xmalloc( strlen(flags) + 1 );

    while (*p)
    {
        while (isspace(*p)) p++;
        var = buffer;
        while (*p && !isspace(*p))
        {
            if (*p == '\\' && p[1]) p++;
            *var++ = *p++;
        }
        *var = 0;
        if (var > buffer) set_make_variable( &cmdline_vars, buffer );
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
4257 4258 4259
/*******************************************************************
 *         parse_option
 */
4260
static int parse_option( const char *opt )
Alexandre Julliard's avatar
Alexandre Julliard committed
4261
{
4262 4263 4264 4265 4266
    if (opt[0] != '-')
    {
        if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
        return 0;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4267 4268 4269
    switch(opt[1])
    {
    case 'f':
4270 4271
        if (opt[2]) output_makefile_name = opt + 2;
        break;
4272 4273 4274
    case 'R':
        relative_dir_mode = 1;
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
4275
    default:
4276
        fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
Alexandre Julliard's avatar
Alexandre Julliard committed
4277 4278
        exit(1);
    }
4279
    return 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
4280 4281 4282 4283 4284 4285 4286 4287
}


/*******************************************************************
 *         main
 */
int main( int argc, char *argv[] )
{
4288
    const char *makeflags = getenv( "MAKEFLAGS" );
4289
    int i, j;
Alexandre Julliard's avatar
Alexandre Julliard committed
4290

4291 4292
    if (makeflags) parse_makeflags( makeflags );

4293 4294
    i = 1;
    while (i < argc)
Alexandre Julliard's avatar
Alexandre Julliard committed
4295
    {
4296
        if (parse_option( argv[i] ))
4297 4298 4299 4300 4301 4302 4303
        {
            for (j = i; j < argc; j++) argv[j] = argv[j+1];
            argc--;
        }
        else i++;
    }

4304 4305 4306 4307 4308 4309
    if (relative_dir_mode)
    {
        char *relpath;

        if (argc != 3)
        {
4310
            fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4311 4312 4313 4314 4315 4316 4317
            exit( 1 );
        }
        relpath = get_relative_path( argv[1], argv[2] );
        printf( "%s\n", relpath ? relpath : "." );
        exit( 0 );
    }

4318 4319 4320 4321 4322 4323 4324
    atexit( cleanup_files );
    signal( SIGTERM, exit_on_signal );
    signal( SIGINT, exit_on_signal );
#ifdef SIGHUP
    signal( SIGHUP, exit_on_signal );
#endif

4325 4326
    for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );

4327
    top_makefile = parse_makefile( NULL );
4328 4329 4330 4331 4332

    target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
    msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
    dll_flags    = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
    extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4333
    extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4334
    cpp_flags    = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4335
    lddll_flags  = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4336
    libs         = get_expanded_make_var_array( top_makefile, "LIBS" );
4337
    enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4338 4339
    top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
    top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4340 4341 4342 4343 4344 4345

    root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
    tools_dir    = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
    tools_ext    = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
    exe_ext      = get_expanded_make_variable( top_makefile, "EXEEXT" );
    man_ext      = get_expanded_make_variable( top_makefile, "api_manext" );
4346
    dll_ext      = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4347 4348 4349
    crosstarget  = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
    fontforge    = get_expanded_make_variable( top_makefile, "FONTFORGE" );
    convert      = get_expanded_make_variable( top_makefile, "CONVERT" );
4350 4351 4352 4353
    flex         = get_expanded_make_variable( top_makefile, "FLEX" );
    bison        = get_expanded_make_variable( top_makefile, "BISON" );
    ar           = get_expanded_make_variable( top_makefile, "AR" );
    ranlib       = get_expanded_make_variable( top_makefile, "RANLIB" );
4354 4355
    rsvg         = get_expanded_make_variable( top_makefile, "RSVG" );
    icotool      = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4356
    dlltool      = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4357
    msgfmt       = get_expanded_make_variable( top_makefile, "MSGFMT" );
4358
    sed_cmd      = get_expanded_make_variable( top_makefile, "SED_CMD" );
4359
    ln_s         = get_expanded_make_variable( top_makefile, "LN_S" );
4360

4361
    if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4362
    if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4363
    if (!exe_ext) exe_ext = "";
4364 4365 4366
    if (!tools_ext) tools_ext = "";
    if (!man_ext) man_ext = "3w";

4367 4368
    if (argc == 1)
    {
4369
        disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4370 4371 4372 4373
        top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
        top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );

        for (i = 0; i < top_makefile->subdirs.count; i++)
4374
            top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i] );
4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388

        load_sources( top_makefile );
        for (i = 0; i < top_makefile->subdirs.count; i++)
            load_sources( top_makefile->submakes[i] );

        for (i = 0; i < top_makefile->subdirs.count; i++)
            output_dependencies( top_makefile->submakes[i] );

        output_dependencies( top_makefile );
        return 0;
    }

    for (i = 1; i < argc; i++)
    {
4389
        struct makefile *make = parse_makefile( argv[i] );
4390 4391 4392
        load_sources( make );
        output_dependencies( make );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
4393 4394
    return 0;
}