2 * Generate include file dependencies
4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
33 /* Max first-level includes per file */
34 #define MAX_INCLUDES 200
36 typedef struct _INCL_FILE
38 struct _INCL_FILE *next;
41 struct _INCL_FILE *included_by; /* file that included this one */
42 int included_line; /* line where this file was included */
43 int system; /* is it a system include (#include <name>) */
44 struct _INCL_FILE *owner;
45 struct _INCL_FILE *files[MAX_INCLUDES];
48 static INCL_FILE *firstSrc;
49 static INCL_FILE *firstInclude;
51 typedef struct _INCL_PATH
53 struct _INCL_PATH *next;
57 static INCL_PATH *firstPath;
59 static const char *SrcDir = NULL;
60 static const char *OutputFileName = "Makefile";
61 static const char *Separator = "### Dependencies";
62 static const char *ProgramName;
64 static const char Usage[] =
65 "Usage: %s [options] [files]\n"
67 " -Idir Search for include files in directory 'dir'\n"
68 " -Cdir Search for source files in directory 'dir'\n"
69 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
70 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
73 /*******************************************************************
76 static void *xmalloc( int size )
79 if (!(res = malloc (size ? size : 1)))
81 fprintf( stderr, "%s: Virtual memory exhausted.\n", ProgramName );
88 /*******************************************************************
91 static char *xstrdup( const char *str )
93 char *res = strdup( str );
96 fprintf( stderr, "%s: Virtual memory exhausted.\n", ProgramName );
103 /*******************************************************************
106 static char *get_extension( char *filename )
108 char *ext = strrchr( filename, '.' );
109 if (ext && strchr( ext, '/' )) ext = NULL;
114 /*******************************************************************
117 * Test if a given file type is generated during the make process
119 static int is_generated( const char *name )
121 static const char * const extensions[] = { ".tab.h", ".mc.rc" };
122 size_t i, len = strlen(name);
123 for (i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
125 if (len <= strlen(extensions[i])) continue;
126 if (!strcmp( name + len - strlen(extensions[i]), extensions[i] )) return 1;
131 /*******************************************************************
134 * Add a directory to the include path.
136 static void add_include_path( const char *name )
138 INCL_PATH *path = xmalloc( sizeof(*path) );
139 INCL_PATH **p = &firstPath;
140 while (*p) p = &(*p)->next;
147 /*******************************************************************
150 * Add a source file to the list.
152 static INCL_FILE *add_src_file( const char *name )
154 INCL_FILE **p = &firstSrc;
155 INCL_FILE *file = xmalloc( sizeof(*file) );
156 memset( file, 0, sizeof(*file) );
157 file->name = xstrdup(name);
158 while (*p) p = &(*p)->next;
164 /*******************************************************************
167 * Add an include file if it doesn't already exists.
169 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
171 INCL_FILE **p = &firstInclude;
174 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
175 if (pos >= MAX_INCLUDES)
177 fprintf( stderr, "%s: %s: too many included files, please fix MAX_INCLUDES\n",
178 ProgramName, pFile->name );
182 while (*p && strcmp( name, (*p)->name )) p = &(*p)->next;
185 *p = xmalloc( sizeof(INCL_FILE) );
186 memset( *p, 0, sizeof(INCL_FILE) );
187 (*p)->name = xstrdup(name);
188 (*p)->included_by = pFile;
189 (*p)->included_line = line;
190 (*p)->system = system || pFile->system;
192 pFile->files[pos] = *p;
197 /*******************************************************************
200 static FILE *open_src_file( INCL_FILE *pFile )
204 /* first try name as is */
205 if ((file = fopen( pFile->name, "r" )))
207 pFile->filename = xstrdup( pFile->name );
210 /* now try in source dir */
213 pFile->filename = xmalloc( strlen(SrcDir) + strlen(pFile->name) + 2 );
214 strcpy( pFile->filename, SrcDir );
215 strcat( pFile->filename, "/" );
216 strcat( pFile->filename, pFile->name );
217 file = fopen( pFile->filename, "r" );
221 perror( pFile->name );
228 /*******************************************************************
231 static FILE *open_include_file( INCL_FILE *pFile )
236 for (path = firstPath; path; path = path->next)
238 char *filename = xmalloc(strlen(path->name) + strlen(pFile->name) + 2);
239 strcpy( filename, path->name );
240 strcat( filename, "/" );
241 strcat( filename, pFile->name );
242 if ((file = fopen( filename, "r" )))
244 pFile->filename = filename;
249 if (!file && pFile->system) return NULL; /* ignore system files we cannot find */
251 /* try in src file directory */
254 char *p = strrchr(pFile->included_by->filename, '/');
257 int l = p - pFile->included_by->filename + 1;
258 char *filename = xmalloc(l + strlen(pFile->name) + 1);
259 memcpy( filename, pFile->included_by->filename, l );
260 strcpy( filename + l, pFile->name );
261 if ((file = fopen( filename, "r" ))) pFile->filename = filename;
262 else free( filename );
268 if (pFile->included_by->system) return NULL; /* ignore if included by a system file */
269 if (firstPath) perror( pFile->name );
270 else fprintf( stderr, "%s: %s: File not found\n",
271 ProgramName, pFile->name );
272 while (pFile->included_by)
274 fprintf( stderr, " %s was first included from %s:%d\n",
275 pFile->name, pFile->included_by->name, pFile->included_line );
276 pFile = pFile->included_by;
284 /*******************************************************************
287 static void parse_idl_file( INCL_FILE *pFile, FILE *file )
293 while (fgets( buffer, sizeof(buffer)-1, file ))
298 while (*p && isspace(*p)) p++;
300 if (!strncmp( p, "import", 6 ))
303 while (*p && isspace(*p)) p++;
304 if (*p != '\"') continue;
308 if (*p++ != '#') continue;
309 while (*p && isspace(*p)) p++;
310 if (strncmp( p, "include", 7 )) continue;
312 while (*p && isspace(*p)) p++;
313 if (*p != '\"' && *p != '<' ) continue;
317 if (quote == '<') quote = '>';
319 while (*p && (*p != quote)) p++;
322 fprintf( stderr, "%s:%d: Malformed #include or import directive\n",
323 pFile->filename, line );
327 add_include( pFile, include, line, (quote == '>') );
331 /*******************************************************************
334 static void parse_c_file( INCL_FILE *pFile, FILE *file )
340 while (fgets( buffer, sizeof(buffer)-1, file ))
345 while (*p && isspace(*p)) p++;
346 if (*p++ != '#') continue;
347 while (*p && isspace(*p)) p++;
348 if (strncmp( p, "include", 7 )) continue;
350 while (*p && isspace(*p)) p++;
351 if (*p != '\"' && *p != '<' ) continue;
353 if (quote == '<') quote = '>';
355 while (*p && (*p != quote)) p++;
358 fprintf( stderr, "%s:%d: Malformed #include directive\n",
359 pFile->filename, line );
363 add_include( pFile, include, line, (quote == '>') );
368 /*******************************************************************
371 static void parse_file( INCL_FILE *pFile, int src )
376 if (is_generated( pFile->name ))
378 /* file is generated during make, don't try to open it */
379 pFile->filename = xstrdup( pFile->name );
383 file = src ? open_src_file( pFile ) : open_include_file( pFile );
385 ext = get_extension( pFile->name );
386 if (ext && !strcmp( ext, ".idl" )) parse_idl_file( pFile, file );
387 else parse_c_file( pFile, file );
392 /*******************************************************************
395 static void output_include( FILE *file, INCL_FILE *pFile,
396 INCL_FILE *owner, int *column )
400 if (pFile->owner == owner) return;
401 if (!pFile->filename) return;
402 pFile->owner = owner;
403 if (*column + strlen(pFile->filename) + 1 > 70)
405 fprintf( file, " \\\n" );
408 fprintf( file, " %s", pFile->filename );
409 *column += strlen(pFile->filename) + 1;
410 for (i = 0; i < MAX_INCLUDES; i++)
411 if (pFile->files[i]) output_include( file, pFile->files[i],
416 /*******************************************************************
419 static void output_src( FILE *file, INCL_FILE *pFile, int *column )
421 char *obj = xstrdup( pFile->name );
422 char *ext = get_extension( obj );
426 if (!strcmp( ext, "y" )) /* yacc file */
428 *column += fprintf( file, "y.tab.o: y.tab.c" );
430 else if (!strcmp( ext, "l" )) /* lex file */
432 *column += fprintf( file, "%s.o: %s.c", LEX_OUTPUT_ROOT, LEX_OUTPUT_ROOT );
434 else if (!strcmp( ext, "rc" )) /* resource file */
436 *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
438 else if (!strcmp( ext, "mc" )) /* message file */
440 *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
442 else if (!strcmp( ext, "idl" )) /* IDL file */
444 *column += fprintf( file, "%s.h: %s", obj, pFile->filename );
448 *column += fprintf( file, "%s.o: %s", obj, pFile->filename );
455 /*******************************************************************
456 * output_dependencies
458 static void output_dependencies(void)
465 if (Separator && ((file = fopen( OutputFileName, "r+" ))))
467 while (fgets( buffer, sizeof(buffer), file ))
468 if (!strncmp( buffer, Separator, strlen(Separator) )) break;
469 ftruncate( fileno(file), ftell(file) );
470 fseek( file, 0L, SEEK_END );
474 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
476 perror( OutputFileName );
480 for( pFile = firstSrc; pFile; pFile = pFile->next)
483 output_src( file, pFile, &column );
484 for (i = 0; i < MAX_INCLUDES; i++)
485 if (pFile->files[i]) output_include( file, pFile->files[i],
487 fprintf( file, "\n" );
493 /*******************************************************************
496 static void parse_option( const char *opt )
501 if (opt[2]) add_include_path( opt + 2 );
504 if (opt[2]) SrcDir = opt + 2;
508 if (opt[2]) OutputFileName = opt + 2;
511 if (opt[2]) Separator = opt + 2;
512 else Separator = NULL;
515 fprintf( stderr, "Unknown option '%s'\n", opt );
516 fprintf( stderr, Usage, ProgramName );
522 /*******************************************************************
525 int main( int argc, char *argv[] )
529 ProgramName = argv[0];
532 if (*argv[1] == '-') parse_option( argv[1] );
535 pFile = add_src_file( argv[1] );
536 parse_file( pFile, 1 );
541 for (pFile = firstInclude; pFile; pFile = pFile->next)
542 parse_file( pFile, 0 );
543 if( firstSrc ) output_dependencies();