makedep.c 16 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Generate include file dependencies
 *
 * Copyright 1996 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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"

Alexandre Julliard's avatar
Alexandre Julliard committed
25 26 27
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
28
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
29
#include <string.h>
30 31 32
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
Alexandre Julliard's avatar
Alexandre Julliard committed
33 34

/* Max first-level includes per file */
35
#define MAX_INCLUDES 200
Alexandre Julliard's avatar
Alexandre Julliard committed
36 37 38 39 40 41

typedef struct _INCL_FILE
{
    struct _INCL_FILE *next;
    char              *name;
    char              *filename;
42 43
    struct _INCL_FILE *included_by;   /* file that included this one */
    int                included_line; /* line where this file was included */
44
    int                system;        /* is it a system include (#include <name>) */
Alexandre Julliard's avatar
Alexandre Julliard committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    struct _INCL_FILE *owner;
    struct _INCL_FILE *files[MAX_INCLUDES];
} INCL_FILE;

static INCL_FILE *firstSrc;
static INCL_FILE *firstInclude;

typedef struct _INCL_PATH
{
    struct _INCL_PATH *next;
    const char        *name;
} INCL_PATH;

static INCL_PATH *firstPath;

static const char *SrcDir = NULL;
static const char *OutputFileName = "Makefile";
static const char *Separator = "### Dependencies";
static const char *ProgramName;

static const char Usage[] =
    "Usage: %s [options] [files]\n"
    "Options:\n"
    "   -Idir   Search for include files in directory 'dir'\n"
    "   -Cdir   Search for source files in directory 'dir'\n"
    "   -fxxx   Store output in file 'xxx' (default: Makefile)\n"
    "   -sxxx   Use 'xxx' as separator (default: \"### Dependencies\")\n";


74 75 76 77 78 79 80 81 82 83 84 85 86
/*******************************************************************
 *         fatal_error
 */
static void fatal_error( const char *msg, ... )
{
    va_list valist;
    va_start( valist, msg );
    vfprintf( stderr, msg, valist );
    va_end( valist );
    exit(1);
}


Alexandre Julliard's avatar
Alexandre Julliard committed
87 88 89 90 91 92 93
/*******************************************************************
 *         xmalloc
 */
static void *xmalloc( int size )
{
    void *res;
    if (!(res = malloc (size ? size : 1)))
94
        fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
Alexandre Julliard's avatar
Alexandre Julliard committed
95 96 97 98 99 100 101 102 103 104
    return res;
}


/*******************************************************************
 *         xstrdup
 */
static char *xstrdup( const char *str )
{
    char *res = strdup( str );
105
    if (!res) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
Alexandre Julliard's avatar
Alexandre Julliard committed
106 107 108 109
    return res;
}


110 111 112 113 114 115 116 117 118 119 120
/*******************************************************************
 *         get_extension
 */
static char *get_extension( char *filename )
{
    char *ext = strrchr( filename, '.' );
    if (ext && strchr( ext, '/' )) ext = NULL;
    return ext;
}


121 122 123 124 125 126 127 128
/*******************************************************************
 *         is_generated
 *
 * Test if a given file type is generated during the make process
 */
static int is_generated( const char *name )
{
    static const char * const extensions[] = { ".tab.h", ".mc.rc" };
129
    size_t i, len = strlen(name);
130 131 132 133 134 135 136 137
    for (i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
    {
        if (len <= strlen(extensions[i])) continue;
        if (!strcmp( name + len - strlen(extensions[i]), extensions[i] )) return 1;
    }
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
/*******************************************************************
 *         add_include_path
 *
 * Add a directory to the include path.
 */
static void add_include_path( const char *name )
{
    INCL_PATH *path = xmalloc( sizeof(*path) );
    INCL_PATH **p = &firstPath;
    while (*p) p = &(*p)->next;
    *p = path;
    path->next = NULL;
    path->name = name;
}


/*******************************************************************
 *         add_src_file
 *
 * Add a source file to the list.
 */
static INCL_FILE *add_src_file( const char *name )
{
    INCL_FILE **p = &firstSrc;
    INCL_FILE *file = xmalloc( sizeof(*file) );
    memset( file, 0, sizeof(*file) );
    file->name = xstrdup(name);
    while (*p) p = &(*p)->next;
    *p = file;
    return file;
}


/*******************************************************************
 *         add_include
 *
 * Add an include file if it doesn't already exists.
 */
176
static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
Alexandre Julliard's avatar
Alexandre Julliard committed
177 178
{
    INCL_FILE **p = &firstInclude;
179
    char *ext;
Alexandre Julliard's avatar
Alexandre Julliard committed
180 181 182 183
    int pos;

    for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
    if (pos >= MAX_INCLUDES)
184 185 186 187 188 189 190 191 192 193
        fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
                     ProgramName, pFile->name );

    /* enforce some rules for the Wine tree */

    if (!memcmp( name, "../", 3 ))
        fatal_error( "%s:%d: #include directive with relative path not allowed\n",
                     pFile->filename, line );

    if (!strcmp( name, "config.h" ))
Alexandre Julliard's avatar
Alexandre Julliard committed
194
    {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
            fatal_error( "%s:%d: config.h must not be included by a header file\n",
                         pFile->filename, line );
        if (pos)
            fatal_error( "%s:%d: config.h must be included before anything else\n",
                         pFile->filename, line );
    }
    else if (!strcmp( name, "wine/port.h" ))
    {
        if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
            fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
                         pFile->filename, line );
        if (!pos) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
                               pFile->filename, line );
        if (pos > 1)
            fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
                         pFile->filename, line );
        if (strcmp( pFile->files[0]->name, "config.h" ))
            fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
                         pFile->filename, line );
Alexandre Julliard's avatar
Alexandre Julliard committed
215 216 217 218 219 220 221 222
    }

    while (*p && strcmp( name, (*p)->name )) p = &(*p)->next;
    if (!*p)
    {
        *p = xmalloc( sizeof(INCL_FILE) );
        memset( *p, 0, sizeof(INCL_FILE) );
        (*p)->name = xstrdup(name);
223 224
        (*p)->included_by = pFile;
        (*p)->included_line = line;
225
        (*p)->system = system || pFile->system;
Alexandre Julliard's avatar
Alexandre Julliard committed
226 227 228 229 230 231 232 233 234 235 236 237 238
    }
    pFile->files[pos] = *p;
    return *p;
}


/*******************************************************************
 *         open_src_file
 */
static FILE *open_src_file( INCL_FILE *pFile )
{
    FILE *file;

239 240 241 242 243 244 245
    /* first try name as is */
    if ((file = fopen( pFile->name, "r" )))
    {
        pFile->filename = xstrdup( pFile->name );
        return file;
    }
    /* now try in source dir */
Alexandre Julliard's avatar
Alexandre Julliard committed
246 247 248 249 250 251
    if (SrcDir)
    {
        pFile->filename = xmalloc( strlen(SrcDir) + strlen(pFile->name) + 2 );
        strcpy( pFile->filename, SrcDir );
        strcat( pFile->filename, "/" );
        strcat( pFile->filename, pFile->name );
252
        file = fopen( pFile->filename, "r" );
Alexandre Julliard's avatar
Alexandre Julliard committed
253
    }
254
    if (!file)
Alexandre Julliard's avatar
Alexandre Julliard committed
255
    {
256
        perror( pFile->name );
Alexandre Julliard's avatar
Alexandre Julliard committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        exit(1);
    }
    return file;
}


/*******************************************************************
 *         open_include_file
 */
static FILE *open_include_file( INCL_FILE *pFile )
{
    FILE *file = NULL;
    INCL_PATH *path;

    for (path = firstPath; path; path = path->next)
    {
        char *filename = xmalloc(strlen(path->name) + strlen(pFile->name) + 2);
        strcpy( filename, path->name );
        strcat( filename, "/" );
        strcat( filename, pFile->name );
        if ((file = fopen( filename, "r" )))
        {
            pFile->filename = filename;
            break;
        }
        free( filename );
    }
284 285
    if (!file && pFile->system) return NULL;  /* ignore system files we cannot find */

286 287 288
    /* try in src file directory */
    if (!file)
    {
289
        char *p = strrchr(pFile->included_by->filename, '/');
290 291
        if (p)
        {
292
            int l = p - pFile->included_by->filename + 1;
293
            char *filename = xmalloc(l + strlen(pFile->name) + 1);
294
            memcpy( filename, pFile->included_by->filename, l );
295 296 297 298 299 300
            strcpy( filename + l, pFile->name );
            if ((file = fopen( filename, "r" ))) pFile->filename = filename;
            else free( filename );
        }
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
301 302
    if (!file)
    {
303
        if (pFile->included_by->system) return NULL;  /* ignore if included by a system file */
Alexandre Julliard's avatar
Alexandre Julliard committed
304 305 306
        if (firstPath) perror( pFile->name );
        else fprintf( stderr, "%s: %s: File not found\n",
                      ProgramName, pFile->name );
307 308 309 310 311 312
        while (pFile->included_by)
        {
            fprintf( stderr, "  %s was first included from %s:%d\n",
                     pFile->name, pFile->included_by->name, pFile->included_line );
            pFile = pFile->included_by;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
313 314 315 316 317 318 319
        exit(1);
    }
    return file;
}


/*******************************************************************
320
 *         parse_idl_file
Alexandre Julliard's avatar
Alexandre Julliard committed
321
 */
322
static void parse_idl_file( INCL_FILE *pFile, FILE *file )
Alexandre Julliard's avatar
Alexandre Julliard committed
323 324 325 326
{
    char buffer[1024];
    char *include;
    int line = 0;
327

328
    while (fgets( buffer, sizeof(buffer)-1, file ))
329
    {
330
        char quote;
331 332 333
        char *p = buffer;
        line++;
        while (*p && isspace(*p)) p++;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

        if (!strncmp( p, "import", 6 ))
        {
            p += 6;
            while (*p && isspace(*p)) p++;
            if (*p != '\"') continue;
        }
        else
        {
            if (*p++ != '#') continue;
            while (*p && isspace(*p)) p++;
            if (strncmp( p, "include", 7 )) continue;
            p += 7;
            while (*p && isspace(*p)) p++;
            if (*p != '\"' && *p != '<' ) continue;
        }

        quote = *p++;
        if (quote == '<') quote = '>';
353
        include = p;
354
        while (*p && (*p != quote)) p++;
355 356
        if (!*p) fatal_error( "%s:%d: Malformed #include or import directive\n",
                              pFile->filename, line );
357
        *p = 0;
358
        add_include( pFile, include, line, (quote == '>') );
359
    }
360
}
361

362 363 364 365 366 367 368 369
/*******************************************************************
 *         parse_c_file
 */
static void parse_c_file( INCL_FILE *pFile, FILE *file )
{
    char buffer[1024];
    char *include;
    int line = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
370 371 372

    while (fgets( buffer, sizeof(buffer)-1, file ))
    {
373
        char quote;
Alexandre Julliard's avatar
Alexandre Julliard committed
374 375 376 377 378 379 380 381
        char *p = buffer;
        line++;
        while (*p && isspace(*p)) p++;
        if (*p++ != '#') continue;
        while (*p && isspace(*p)) p++;
        if (strncmp( p, "include", 7 )) continue;
        p += 7;
        while (*p && isspace(*p)) p++;
382 383 384
        if (*p != '\"' && *p != '<' ) continue;
        quote = *p++;
        if (quote == '<') quote = '>';
Alexandre Julliard's avatar
Alexandre Julliard committed
385
        include = p;
386
        while (*p && (*p != quote)) p++;
387 388
        if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
                              pFile->filename, line );
Alexandre Julliard's avatar
Alexandre Julliard committed
389
        *p = 0;
390
        add_include( pFile, include, line, (quote == '>') );
Alexandre Julliard's avatar
Alexandre Julliard committed
391
    }
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
}


/*******************************************************************
 *         parse_file
 */
static void parse_file( INCL_FILE *pFile, int src )
{
    char *ext;
    FILE *file;

    if (is_generated( pFile->name ))
    {
        /* file is generated during make, don't try to open it */
        pFile->filename = xstrdup( pFile->name );
        return;
    }

    file = src ? open_src_file( pFile ) : open_include_file( pFile );
    if (!file) return;
    ext = get_extension( pFile->name );
    if (ext && !strcmp( ext, ".idl" )) parse_idl_file( pFile, file );
    else parse_c_file( pFile, file );
Alexandre Julliard's avatar
Alexandre Julliard committed
415
    fclose(file);
Alexandre Julliard's avatar
Alexandre Julliard committed
416 417 418 419 420 421 422 423 424 425 426 427
}


/*******************************************************************
 *         output_include
 */
static void output_include( FILE *file, INCL_FILE *pFile,
                            INCL_FILE *owner, int *column )
{
    int i;

    if (pFile->owner == owner) return;
428
    if (!pFile->filename) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442
    pFile->owner = owner;
    if (*column + strlen(pFile->filename) + 1 > 70)
    {
        fprintf( file, " \\\n" );
        *column = 0;
    }
    fprintf( file, " %s", pFile->filename );
    *column += strlen(pFile->filename) + 1;
    for (i = 0; i < MAX_INCLUDES; i++)
        if (pFile->files[i]) output_include( file, pFile->files[i],
                                             owner, column );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
443 444 445 446 447
/*******************************************************************
 *         output_src
 */
static void output_src( FILE *file, INCL_FILE *pFile, int *column )
{
448
    char *obj = xstrdup( pFile->name );
449
    char *ext = get_extension( obj );
Alexandre Julliard's avatar
Alexandre Julliard committed
450 451
    if (ext)
    {
452 453
        *ext++ = 0;
        if (!strcmp( ext, "y" ))  /* yacc file */
Alexandre Julliard's avatar
Alexandre Julliard committed
454
        {
455
            *column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj );
Alexandre Julliard's avatar
Alexandre Julliard committed
456
        }
457
        else if (!strcmp( ext, "l" ))  /* lex file */
Alexandre Julliard's avatar
Alexandre Julliard committed
458
        {
459
            *column += fprintf( file, "%s.o: %s.c", LEX_OUTPUT_ROOT, LEX_OUTPUT_ROOT );
Alexandre Julliard's avatar
Alexandre Julliard committed
460
        }
461
        else if (!strcmp( ext, "rc" ))  /* resource file */
Alexandre Julliard's avatar
Alexandre Julliard committed
462
        {
463 464
            *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
        }
465
        else if (!strcmp( ext, "mc" ))  /* message file */
466
        {
467
            *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
Alexandre Julliard's avatar
Alexandre Julliard committed
468
        }
469 470 471 472
        else if (!strcmp( ext, "idl" ))  /* IDL file */
        {
            *column += fprintf( file, "%s.h: %s", obj, pFile->filename );
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
473 474
        else
        {
475
            *column += fprintf( file, "%s.o: %s", obj, pFile->filename );
Alexandre Julliard's avatar
Alexandre Julliard committed
476 477 478 479 480 481
        }
    }
    free( obj );
}


Alexandre Julliard's avatar
Alexandre Julliard committed
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
/*******************************************************************
 *         output_dependencies
 */
static void output_dependencies(void)
{
    INCL_FILE *pFile;
    int i, column;
    FILE *file = NULL;
    char buffer[1024];

    if (Separator && ((file = fopen( OutputFileName, "r+" ))))
    {
        while (fgets( buffer, sizeof(buffer), file ))
            if (!strncmp( buffer, Separator, strlen(Separator) )) break;
        ftruncate( fileno(file), ftell(file) );
Alexandre Julliard's avatar
Alexandre Julliard committed
497
        fseek( file, 0L, SEEK_END );
Alexandre Julliard's avatar
Alexandre Julliard committed
498 499 500
    }
    if (!file)
    {
501
        if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
Alexandre Julliard's avatar
Alexandre Julliard committed
502 503 504 505 506 507 508
        {
            perror( OutputFileName );
            exit(1);
        }
    }
    for( pFile = firstSrc; pFile; pFile = pFile->next)
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
509 510
        column = 0;
        output_src( file, pFile, &column );
Alexandre Julliard's avatar
Alexandre Julliard committed
511 512 513 514 515
        for (i = 0; i < MAX_INCLUDES; i++)
            if (pFile->files[i]) output_include( file, pFile->files[i],
                                                 pFile, &column );
        fprintf( file, "\n" );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
516
    fclose(file);
Alexandre Julliard's avatar
Alexandre Julliard committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 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 566 567 568 569
}


/*******************************************************************
 *         parse_option
 */
static void parse_option( const char *opt )
{
    switch(opt[1])
    {
    case 'I':
        if (opt[2]) add_include_path( opt + 2 );
        break;
    case 'C':
        if (opt[2]) SrcDir = opt + 2;
        else SrcDir = NULL;
        break;
    case 'f':
        if (opt[2]) OutputFileName = opt + 2;
        break;
    case 's':
        if (opt[2]) Separator = opt + 2;
        else Separator = NULL;
        break;
    default:
        fprintf( stderr, "Unknown option '%s'\n", opt );
        fprintf( stderr, Usage, ProgramName );
        exit(1);
    }
}


/*******************************************************************
 *         main
 */
int main( int argc, char *argv[] )
{
    INCL_FILE *pFile;

    ProgramName = argv[0];
    while (argc > 1)
    {
        if (*argv[1] == '-') parse_option( argv[1] );
        else
        {
            pFile = add_src_file( argv[1] );
            parse_file( pFile, 1 );
        }
        argc--;
        argv++;
    }
    for (pFile = firstInclude; pFile; pFile = pFile->next)
        parse_file( pFile, 0 );
570
    if( firstSrc ) output_dependencies();
Alexandre Julliard's avatar
Alexandre Julliard committed
571 572
    return 0;
}