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"
34 /* Max first-level includes per file */
35 #define MAX_INCLUDES 200
37 typedef struct _INCL_FILE
39 struct _INCL_FILE
*next
;
42 struct _INCL_FILE
*included_by
; /* file that included this one */
43 int included_line
; /* line where this file was included */
44 int system
; /* is it a system include (#include <name>) */
45 struct _INCL_FILE
*owner
;
46 struct _INCL_FILE
*files
[MAX_INCLUDES
];
49 static INCL_FILE
*firstSrc
;
50 static INCL_FILE
*firstInclude
;
52 typedef struct _INCL_PATH
54 struct _INCL_PATH
*next
;
58 static INCL_PATH
*firstPath
;
60 static const char *SrcDir
= NULL
;
61 static const char *OutputFileName
= "Makefile";
62 static const char *Separator
= "### Dependencies";
63 static const char *ProgramName
;
65 static const char Usage
[] =
66 "Usage: %s [options] [files]\n"
68 " -Idir Search for include files in directory 'dir'\n"
69 " -Cdir Search for source files in directory 'dir'\n"
70 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
71 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
74 /*******************************************************************
77 static void fatal_error( const char *msg
, ... )
80 va_start( valist
, msg
);
81 vfprintf( stderr
, msg
, valist
);
87 /*******************************************************************
90 static void *xmalloc( int size
)
93 if (!(res
= malloc (size
? size
: 1)))
94 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
99 /*******************************************************************
102 static char *xstrdup( const char *str
)
104 char *res
= strdup( str
);
105 if (!res
) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
110 /*******************************************************************
113 static char *get_extension( char *filename
)
115 char *ext
= strrchr( filename
, '.' );
116 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
121 /*******************************************************************
124 * Test if a given file type is generated during the make process
126 static int is_generated( const char *name
)
128 static const char * const extensions
[] = { ".tab.h", ".mc.rc" };
129 size_t i
, len
= strlen(name
);
130 for (i
= 0; i
< sizeof(extensions
)/sizeof(extensions
[0]); i
++)
132 if (len
<= strlen(extensions
[i
])) continue;
133 if (!strcmp( name
+ len
- strlen(extensions
[i
]), extensions
[i
] )) return 1;
138 /*******************************************************************
141 * Add a directory to the include path.
143 static void add_include_path( const char *name
)
145 INCL_PATH
*path
= xmalloc( sizeof(*path
) );
146 INCL_PATH
**p
= &firstPath
;
147 while (*p
) p
= &(*p
)->next
;
154 /*******************************************************************
157 * Add a source file to the list.
159 static INCL_FILE
*add_src_file( const char *name
)
161 INCL_FILE
**p
= &firstSrc
;
162 INCL_FILE
*file
= xmalloc( sizeof(*file
) );
163 memset( file
, 0, sizeof(*file
) );
164 file
->name
= xstrdup(name
);
165 while (*p
) p
= &(*p
)->next
;
171 /*******************************************************************
174 * 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
)
178 INCL_FILE
**p
= &firstInclude
;
182 for (pos
= 0; pos
< MAX_INCLUDES
; pos
++) if (!pFile
->files
[pos
]) break;
183 if (pos
>= MAX_INCLUDES
)
184 fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
185 ProgramName
, pFile
->name
);
187 /* enforce some rules for the Wine tree */
189 if (!memcmp( name
, "../", 3 ))
190 fatal_error( "%s:%d: #include directive with relative path not allowed\n",
191 pFile
->filename
, line
);
193 if (!strcmp( name
, "config.h" ))
195 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
196 fatal_error( "%s:%d: config.h must not be included by a header file\n",
197 pFile
->filename
, line
);
199 fatal_error( "%s:%d: config.h must be included before anything else\n",
200 pFile
->filename
, line
);
202 else if (!strcmp( name
, "wine/port.h" ))
204 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
205 fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
206 pFile
->filename
, line
);
207 if (!pos
) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
208 pFile
->filename
, line
);
210 fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
211 pFile
->filename
, line
);
212 if (strcmp( pFile
->files
[0]->name
, "config.h" ))
213 fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
214 pFile
->filename
, line
);
217 while (*p
&& strcmp( name
, (*p
)->name
)) p
= &(*p
)->next
;
220 *p
= xmalloc( sizeof(INCL_FILE
) );
221 memset( *p
, 0, sizeof(INCL_FILE
) );
222 (*p
)->name
= xstrdup(name
);
223 (*p
)->included_by
= pFile
;
224 (*p
)->included_line
= line
;
225 (*p
)->system
= system
|| pFile
->system
;
227 pFile
->files
[pos
] = *p
;
232 /*******************************************************************
235 static FILE *open_src_file( INCL_FILE
*pFile
)
239 /* first try name as is */
240 if ((file
= fopen( pFile
->name
, "r" )))
242 pFile
->filename
= xstrdup( pFile
->name
);
245 /* now try in source dir */
248 pFile
->filename
= xmalloc( strlen(SrcDir
) + strlen(pFile
->name
) + 2 );
249 strcpy( pFile
->filename
, SrcDir
);
250 strcat( pFile
->filename
, "/" );
251 strcat( pFile
->filename
, pFile
->name
);
252 file
= fopen( pFile
->filename
, "r" );
256 perror( pFile
->name
);
263 /*******************************************************************
266 static FILE *open_include_file( INCL_FILE
*pFile
)
271 for (path
= firstPath
; path
; path
= path
->next
)
273 char *filename
= xmalloc(strlen(path
->name
) + strlen(pFile
->name
) + 2);
274 strcpy( filename
, path
->name
);
275 strcat( filename
, "/" );
276 strcat( filename
, pFile
->name
);
277 if ((file
= fopen( filename
, "r" )))
279 pFile
->filename
= filename
;
284 if (!file
&& pFile
->system
) return NULL
; /* ignore system files we cannot find */
286 /* try in src file directory */
289 char *p
= strrchr(pFile
->included_by
->filename
, '/');
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 strcpy( filename
+ l
, pFile
->name
);
296 if ((file
= fopen( filename
, "r" ))) pFile
->filename
= filename
;
297 else free( filename
);
303 if (pFile
->included_by
->system
) return NULL
; /* ignore if included by a system file */
304 if (firstPath
) perror( pFile
->name
);
305 else fprintf( stderr
, "%s: %s: File not found\n",
306 ProgramName
, pFile
->name
);
307 while (pFile
->included_by
)
309 fprintf( stderr
, " %s was first included from %s:%d\n",
310 pFile
->name
, pFile
->included_by
->name
, pFile
->included_line
);
311 pFile
= pFile
->included_by
;
319 /*******************************************************************
322 static void parse_idl_file( INCL_FILE
*pFile
, FILE *file
)
328 while (fgets( buffer
, sizeof(buffer
)-1, file
))
333 while (*p
&& isspace(*p
)) p
++;
335 if (!strncmp( p
, "import", 6 ))
338 while (*p
&& isspace(*p
)) p
++;
339 if (*p
!= '\"') continue;
343 if (*p
++ != '#') continue;
344 while (*p
&& isspace(*p
)) p
++;
345 if (strncmp( p
, "include", 7 )) continue;
347 while (*p
&& isspace(*p
)) p
++;
348 if (*p
!= '\"' && *p
!= '<' ) continue;
352 if (quote
== '<') quote
= '>';
354 while (*p
&& (*p
!= quote
)) p
++;
355 if (!*p
) fatal_error( "%s:%d: Malformed #include or import directive\n",
356 pFile
->filename
, line
);
358 add_include( pFile
, include
, line
, (quote
== '>') );
362 /*******************************************************************
365 static void parse_c_file( INCL_FILE
*pFile
, FILE *file
)
371 while (fgets( buffer
, sizeof(buffer
)-1, file
))
376 while (*p
&& isspace(*p
)) p
++;
377 if (*p
++ != '#') continue;
378 while (*p
&& isspace(*p
)) p
++;
379 if (strncmp( p
, "include", 7 )) continue;
381 while (*p
&& isspace(*p
)) p
++;
382 if (*p
!= '\"' && *p
!= '<' ) continue;
384 if (quote
== '<') quote
= '>';
386 while (*p
&& (*p
!= quote
)) p
++;
387 if (!*p
) fatal_error( "%s:%d: Malformed #include directive\n",
388 pFile
->filename
, line
);
390 add_include( pFile
, include
, line
, (quote
== '>') );
395 /*******************************************************************
398 static void parse_file( INCL_FILE
*pFile
, int src
)
403 if (is_generated( pFile
->name
))
405 /* file is generated during make, don't try to open it */
406 pFile
->filename
= xstrdup( pFile
->name
);
410 file
= src
? open_src_file( pFile
) : open_include_file( pFile
);
412 ext
= get_extension( pFile
->name
);
413 if (ext
&& !strcmp( ext
, ".idl" )) parse_idl_file( pFile
, file
);
414 else parse_c_file( pFile
, file
);
419 /*******************************************************************
422 static void output_include( FILE *file
, INCL_FILE
*pFile
,
423 INCL_FILE
*owner
, int *column
)
427 if (pFile
->owner
== owner
) return;
428 if (!pFile
->filename
) return;
429 pFile
->owner
= owner
;
430 if (*column
+ strlen(pFile
->filename
) + 1 > 70)
432 fprintf( file
, " \\\n" );
435 fprintf( file
, " %s", pFile
->filename
);
436 *column
+= strlen(pFile
->filename
) + 1;
437 for (i
= 0; i
< MAX_INCLUDES
; i
++)
438 if (pFile
->files
[i
]) output_include( file
, pFile
->files
[i
],
443 /*******************************************************************
446 static void output_src( FILE *file
, INCL_FILE
*pFile
, int *column
)
448 char *obj
= xstrdup( pFile
->name
);
449 char *ext
= get_extension( obj
);
453 if (!strcmp( ext
, "y" )) /* yacc file */
455 *column
+= fprintf( file
, "%s.tab.o: %s.tab.c", obj
, obj
);
457 else if (!strcmp( ext
, "l" )) /* lex file */
459 *column
+= fprintf( file
, "%s.o: %s.c", LEX_OUTPUT_ROOT
, LEX_OUTPUT_ROOT
);
461 else if (!strcmp( ext
, "rc" )) /* resource file */
463 *column
+= fprintf( file
, "%s.res: %s", obj
, pFile
->filename
);
465 else if (!strcmp( ext
, "mc" )) /* message file */
467 *column
+= fprintf( file
, "%s.mc.rc: %s", obj
, pFile
->filename
);
469 else if (!strcmp( ext
, "idl" )) /* IDL file */
471 *column
+= fprintf( file
, "%s.h: %s", obj
, pFile
->filename
);
475 *column
+= fprintf( file
, "%s.o: %s", obj
, pFile
->filename
);
482 /*******************************************************************
483 * output_dependencies
485 static void output_dependencies(void)
492 if (Separator
&& ((file
= fopen( OutputFileName
, "r+" ))))
494 while (fgets( buffer
, sizeof(buffer
), file
))
495 if (!strncmp( buffer
, Separator
, strlen(Separator
) )) break;
496 ftruncate( fileno(file
), ftell(file
) );
497 fseek( file
, 0L, SEEK_END
);
501 if (!(file
= fopen( OutputFileName
, Separator
? "a" : "w" )))
503 perror( OutputFileName
);
507 for( pFile
= firstSrc
; pFile
; pFile
= pFile
->next
)
510 output_src( file
, pFile
, &column
);
511 for (i
= 0; i
< MAX_INCLUDES
; i
++)
512 if (pFile
->files
[i
]) output_include( file
, pFile
->files
[i
],
514 fprintf( file
, "\n" );
520 /*******************************************************************
523 static void parse_option( const char *opt
)
528 if (opt
[2]) add_include_path( opt
+ 2 );
531 if (opt
[2]) SrcDir
= opt
+ 2;
535 if (opt
[2]) OutputFileName
= opt
+ 2;
538 if (opt
[2]) Separator
= opt
+ 2;
539 else Separator
= NULL
;
542 fprintf( stderr
, "Unknown option '%s'\n", opt
);
543 fprintf( stderr
, Usage
, ProgramName
);
549 /*******************************************************************
552 int main( int argc
, char *argv
[] )
556 ProgramName
= argv
[0];
559 if (*argv
[1] == '-') parse_option( argv
[1] );
562 pFile
= add_src_file( argv
[1] );
563 parse_file( pFile
, 1 );
568 for (pFile
= firstInclude
; pFile
; pFile
= pFile
->next
)
569 parse_file( pFile
, 0 );
570 if( firstSrc
) output_dependencies();