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 * Test if a given file type is generated during the make process
108 static int is_generated( const char *name )
110 static const char * const extensions[] = { ".tab.h", ".mc.rc" };
111 size_t i, len = strlen(name);
112 for (i = 0; i < sizeof(extensions)/sizeof(extensions[0]); i++)
114 if (len <= strlen(extensions[i])) continue;
115 if (!strcmp( name + len - strlen(extensions[i]), extensions[i] )) return 1;
120 /*******************************************************************
123 * Add a directory to the include path.
125 static void add_include_path( const char *name )
127 INCL_PATH *path = xmalloc( sizeof(*path) );
128 INCL_PATH **p = &firstPath;
129 while (*p) p = &(*p)->next;
136 /*******************************************************************
139 * Add a source file to the list.
141 static INCL_FILE *add_src_file( const char *name )
143 INCL_FILE **p = &firstSrc;
144 INCL_FILE *file = xmalloc( sizeof(*file) );
145 memset( file, 0, sizeof(*file) );
146 file->name = xstrdup(name);
147 while (*p) p = &(*p)->next;
153 /*******************************************************************
156 * Add an include file if it doesn't already exists.
158 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
160 INCL_FILE **p = &firstInclude;
163 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
164 if (pos >= MAX_INCLUDES)
166 fprintf( stderr, "%s: %s: too many included files, please fix MAX_INCLUDES\n",
167 ProgramName, pFile->name );
171 while (*p && strcmp( name, (*p)->name )) p = &(*p)->next;
174 *p = xmalloc( sizeof(INCL_FILE) );
175 memset( *p, 0, sizeof(INCL_FILE) );
176 (*p)->name = xstrdup(name);
177 (*p)->included_by = pFile;
178 (*p)->included_line = line;
179 (*p)->system = system || pFile->system;
181 pFile->files[pos] = *p;
186 /*******************************************************************
189 static FILE *open_src_file( INCL_FILE *pFile )
193 /* first try name as is */
194 if ((file = fopen( pFile->name, "r" )))
196 pFile->filename = xstrdup( pFile->name );
199 /* now try in source dir */
202 pFile->filename = xmalloc( strlen(SrcDir) + strlen(pFile->name) + 2 );
203 strcpy( pFile->filename, SrcDir );
204 strcat( pFile->filename, "/" );
205 strcat( pFile->filename, pFile->name );
206 file = fopen( pFile->filename, "r" );
210 perror( pFile->name );
217 /*******************************************************************
220 static FILE *open_include_file( INCL_FILE *pFile )
225 for (path = firstPath; path; path = path->next)
227 char *filename = xmalloc(strlen(path->name) + strlen(pFile->name) + 2);
228 strcpy( filename, path->name );
229 strcat( filename, "/" );
230 strcat( filename, pFile->name );
231 if ((file = fopen( filename, "r" )))
233 pFile->filename = filename;
238 if (!file && pFile->system) return NULL; /* ignore system files we cannot find */
240 /* try in src file directory */
243 char *p = strrchr(pFile->included_by->filename, '/');
246 int l = p - pFile->included_by->filename + 1;
247 char *filename = xmalloc(l + strlen(pFile->name) + 1);
248 memcpy( filename, pFile->included_by->filename, l );
249 strcpy( filename + l, pFile->name );
250 if ((file = fopen( filename, "r" ))) pFile->filename = filename;
251 else free( filename );
257 if (pFile->included_by->system) return NULL; /* ignore if included by a system file */
258 if (firstPath) perror( pFile->name );
259 else fprintf( stderr, "%s: %s: File not found\n",
260 ProgramName, pFile->name );
261 while (pFile->included_by)
263 fprintf( stderr, " %s was first included from %s:%d\n",
264 pFile->name, pFile->included_by->name, pFile->included_line );
265 pFile = pFile->included_by;
273 /*******************************************************************
276 static void parse_file( INCL_FILE *pFile, int src )
283 if (is_generated( pFile->name ))
285 /* file is generated during make, don't try to open it */
286 pFile->filename = xstrdup( pFile->name );
290 file = src ? open_src_file( pFile ) : open_include_file( pFile );
293 while (fgets( buffer, sizeof(buffer)-1, file ))
298 while (*p && isspace(*p)) p++;
299 if (*p++ != '#') continue;
300 while (*p && isspace(*p)) p++;
301 if (strncmp( p, "include", 7 )) continue;
303 while (*p && isspace(*p)) p++;
304 if (*p != '\"' && *p != '<' ) continue;
306 if (quote == '<') quote = '>';
308 while (*p && (*p != quote)) p++;
311 fprintf( stderr, "%s:%d: Malformed #include directive\n",
312 pFile->filename, line );
316 add_include( pFile, include, line, (quote == '>') );
322 /*******************************************************************
325 static void output_include( FILE *file, INCL_FILE *pFile,
326 INCL_FILE *owner, int *column )
330 if (pFile->owner == owner) return;
331 if (!pFile->filename) return;
332 pFile->owner = owner;
333 if (*column + strlen(pFile->filename) + 1 > 70)
335 fprintf( file, " \\\n" );
338 fprintf( file, " %s", pFile->filename );
339 *column += strlen(pFile->filename) + 1;
340 for (i = 0; i < MAX_INCLUDES; i++)
341 if (pFile->files[i]) output_include( file, pFile->files[i],
346 /*******************************************************************
349 static void output_src( FILE *file, INCL_FILE *pFile, int *column )
351 char *obj = xstrdup( pFile->name );
352 char *ext = strrchr( obj, '.' );
353 if (ext && strchr( ext, '/' )) ext = NULL;
357 if (!strcmp( ext, "y" )) /* yacc file */
359 *column += fprintf( file, "y.tab.o: y.tab.c" );
361 else if (!strcmp( ext, "l" )) /* lex file */
363 *column += fprintf( file, "%s.o: %s.c", LEX_OUTPUT_ROOT, LEX_OUTPUT_ROOT );
365 else if (!strcmp( ext, "rc" )) /* resource file */
367 *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
369 else if (!strcmp( ext, "mc" )) /* message file */
371 *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
375 *column += fprintf( file, "%s.o: %s", obj, pFile->filename );
382 /*******************************************************************
383 * output_dependencies
385 static void output_dependencies(void)
392 if (Separator && ((file = fopen( OutputFileName, "r+" ))))
394 while (fgets( buffer, sizeof(buffer), file ))
395 if (!strncmp( buffer, Separator, strlen(Separator) )) break;
396 ftruncate( fileno(file), ftell(file) );
397 fseek( file, 0L, SEEK_END );
401 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
403 perror( OutputFileName );
407 for( pFile = firstSrc; pFile; pFile = pFile->next)
410 output_src( file, pFile, &column );
411 for (i = 0; i < MAX_INCLUDES; i++)
412 if (pFile->files[i]) output_include( file, pFile->files[i],
414 fprintf( file, "\n" );
420 /*******************************************************************
423 static void parse_option( const char *opt )
428 if (opt[2]) add_include_path( opt + 2 );
431 if (opt[2]) SrcDir = opt + 2;
435 if (opt[2]) OutputFileName = opt + 2;
438 if (opt[2]) Separator = opt + 2;
439 else Separator = NULL;
442 fprintf( stderr, "Unknown option '%s'\n", opt );
443 fprintf( stderr, Usage, ProgramName );
449 /*******************************************************************
452 int main( int argc, char *argv[] )
456 ProgramName = argv[0];
459 if (*argv[1] == '-') parse_option( argv[1] );
462 pFile = add_src_file( argv[1] );
463 parse_file( pFile, 1 );
468 for (pFile = firstInclude; pFile; pFile = pFile->next)
469 parse_file( pFile, 0 );
470 if( firstSrc ) output_dependencies();