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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
35 #include "wine/list.h"
37 /* Max first-level includes per file */
38 #define MAX_INCLUDES 200
40 typedef struct _INCL_FILE
45 struct _INCL_FILE
*included_by
; /* file that included this one */
46 int included_line
; /* line where this file was included */
47 int system
; /* is it a system include (#include <name>) */
48 struct _INCL_FILE
*owner
;
49 struct _INCL_FILE
*files
[MAX_INCLUDES
];
52 static struct list sources
= LIST_INIT(sources
);
53 static struct list includes
= LIST_INIT(includes
);
55 typedef struct _INCL_PATH
61 static struct list paths
= LIST_INIT(paths
);
63 static const char *src_dir
;
64 static const char *top_src_dir
;
65 static const char *top_obj_dir
;
66 static const char *OutputFileName
= "Makefile";
67 static const char *Separator
= "### Dependencies";
68 static const char *ProgramName
;
69 static int input_line
;
71 static const char Usage
[] =
72 "Usage: %s [options] [files]\n"
74 " -Idir Search for include files in directory 'dir'\n"
75 " -Cdir Search for source files in directory 'dir'\n"
76 " -Sdir Set the top source directory\n"
77 " -Sdir Set the top object directory\n"
78 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
79 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
82 /*******************************************************************
85 static void fatal_error( const char *msg
, ... )
88 va_start( valist
, msg
);
89 vfprintf( stderr
, msg
, valist
);
95 /*******************************************************************
98 static void *xmalloc( size_t size
)
101 if (!(res
= malloc (size
? size
: 1)))
102 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
107 /*******************************************************************
110 void *xrealloc (void *ptr
, size_t size
)
114 if (!(res
= realloc( ptr
, size
)))
115 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
119 /*******************************************************************
122 static char *xstrdup( const char *str
)
124 char *res
= strdup( str
);
125 if (!res
) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
130 /*******************************************************************
133 char *strmake( const char* fmt
, ... )
141 char *p
= xmalloc (size
);
143 n
= vsnprintf (p
, size
, fmt
, ap
);
145 if (n
== -1) size
*= 2;
146 else if ((size_t)n
>= size
) size
= n
+ 1;
153 /*******************************************************************
156 static char *get_extension( char *filename
)
158 char *ext
= strrchr( filename
, '.' );
159 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
164 /*******************************************************************
167 static char *get_line( FILE *file
)
170 static unsigned int size
;
175 buffer
= xmalloc( size
);
177 if (!fgets( buffer
, size
, file
)) return NULL
;
182 char *p
= buffer
+ strlen(buffer
);
183 /* if line is larger than buffer, resize buffer */
184 while (p
== buffer
+ size
- 1 && p
[-1] != '\n')
186 buffer
= xrealloc( buffer
, size
* 2 );
187 fgets( buffer
+ size
- 1, size
+ 1, file
);
188 p
= buffer
+ strlen(buffer
);
191 if (p
> buffer
&& p
[-1] == '\n')
194 if (p
> buffer
&& p
[-1] == '\r') *(--p
) = 0;
195 if (p
> buffer
&& p
[-1] == '\\')
198 /* line ends in backslash, read continuation line */
199 fgets( p
, size
- (p
- buffer
), file
);
208 /*******************************************************************
211 * Test if a given file type is generated during the make process
213 static int is_generated( const char *name
)
215 static const char * const extensions
[] = { ".tab.h", ".mc.rc" };
216 size_t i
, len
= strlen(name
);
217 for (i
= 0; i
< sizeof(extensions
)/sizeof(extensions
[0]); i
++)
219 if (len
<= strlen(extensions
[i
])) continue;
220 if (!strcmp( name
+ len
- strlen(extensions
[i
]), extensions
[i
] )) return 1;
225 /*******************************************************************
228 * Add a directory to the include path.
230 static void add_include_path( const char *name
)
232 INCL_PATH
*path
= xmalloc( sizeof(*path
) );
233 list_add_tail( &paths
, &path
->entry
);
238 /*******************************************************************
241 * Add a source file to the list.
243 static INCL_FILE
*add_src_file( const char *name
)
245 INCL_FILE
*file
= xmalloc( sizeof(*file
) );
246 memset( file
, 0, sizeof(*file
) );
247 file
->name
= xstrdup(name
);
248 list_add_tail( &sources
, &file
->entry
);
253 /*******************************************************************
256 * Add an include file if it doesn't already exists.
258 static INCL_FILE
*add_include( INCL_FILE
*pFile
, const char *name
, int line
, int system
)
264 for (pos
= 0; pos
< MAX_INCLUDES
; pos
++) if (!pFile
->files
[pos
]) break;
265 if (pos
>= MAX_INCLUDES
)
266 fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
267 ProgramName
, pFile
->name
);
269 /* enforce some rules for the Wine tree */
271 if (!memcmp( name
, "../", 3 ))
272 fatal_error( "%s:%d: #include directive with relative path not allowed\n",
273 pFile
->filename
, line
);
275 if (!strcmp( name
, "config.h" ))
277 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
278 fatal_error( "%s:%d: config.h must not be included by a header file\n",
279 pFile
->filename
, line
);
281 fatal_error( "%s:%d: config.h must be included before anything else\n",
282 pFile
->filename
, line
);
284 else if (!strcmp( name
, "wine/port.h" ))
286 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
287 fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
288 pFile
->filename
, line
);
289 if (!pos
) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
290 pFile
->filename
, line
);
292 fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
293 pFile
->filename
, line
);
294 if (strcmp( pFile
->files
[0]->name
, "config.h" ))
295 fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
296 pFile
->filename
, line
);
299 LIST_FOR_EACH_ENTRY( include
, &includes
, INCL_FILE
, entry
)
300 if (!strcmp( name
, include
->name
)) goto found
;
302 include
= xmalloc( sizeof(INCL_FILE
) );
303 memset( include
, 0, sizeof(INCL_FILE
) );
304 include
->name
= xstrdup(name
);
305 include
->included_by
= pFile
;
306 include
->included_line
= line
;
307 include
->system
= system
|| pFile
->system
;
308 list_add_tail( &includes
, &include
->entry
);
310 pFile
->files
[pos
] = include
;
315 /*******************************************************************
318 static FILE *open_src_file( INCL_FILE
*pFile
)
322 /* first try name as is */
323 if ((file
= fopen( pFile
->name
, "r" )))
325 pFile
->filename
= xstrdup( pFile
->name
);
328 /* now try in source dir */
331 pFile
->filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
332 file
= fopen( pFile
->filename
, "r" );
336 perror( pFile
->name
);
343 /*******************************************************************
346 static FILE *open_include_file( INCL_FILE
*pFile
)
354 /* first try name as is */
355 if ((file
= fopen( pFile
->name
, "r" )))
357 pFile
->filename
= xstrdup( pFile
->name
);
361 /* now try in source dir */
364 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
365 if ((file
= fopen( filename
, "r" ))) goto found
;
369 /* now try in global includes */
372 filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
373 if ((file
= fopen( filename
, "r" ))) goto found
;
378 filename
= strmake( "%s/include/%s", top_src_dir
, pFile
->name
);
379 if ((file
= fopen( filename
, "r" ))) goto found
;
383 /* now search in include paths */
384 LIST_FOR_EACH_ENTRY( path
, &paths
, INCL_PATH
, entry
)
386 filename
= strmake( "%s/%s", path
->name
, pFile
->name
);
387 if ((file
= fopen( filename
, "r" ))) goto found
;
390 if (pFile
->system
) return NULL
; /* ignore system files we cannot find */
392 /* try in src file directory */
393 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
395 int l
= p
- pFile
->included_by
->filename
+ 1;
396 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
397 memcpy( filename
, pFile
->included_by
->filename
, l
);
398 strcpy( filename
+ l
, pFile
->name
);
399 if ((file
= fopen( filename
, "r" ))) goto found
;
403 if (pFile
->included_by
->system
) return NULL
; /* ignore if included by a system file */
405 perror( pFile
->name
);
406 while (pFile
->included_by
)
408 fprintf( stderr
, " %s was first included from %s:%d\n",
409 pFile
->name
, pFile
->included_by
->name
, pFile
->included_line
);
410 pFile
= pFile
->included_by
;
415 pFile
->filename
= filename
;
420 /*******************************************************************
423 static void parse_idl_file( INCL_FILE
*pFile
, FILE *file
)
425 char *buffer
, *include
;
428 while ((buffer
= get_line( file
)))
432 while (*p
&& isspace(*p
)) p
++;
434 if (!strncmp( p
, "import", 6 ))
437 while (*p
&& isspace(*p
)) p
++;
438 if (*p
!= '\"') continue;
442 if (*p
++ != '#') continue;
443 while (*p
&& isspace(*p
)) p
++;
444 if (strncmp( p
, "include", 7 )) continue;
446 while (*p
&& isspace(*p
)) p
++;
447 if (*p
!= '\"' && *p
!= '<' ) continue;
451 if (quote
== '<') quote
= '>';
453 while (*p
&& (*p
!= quote
)) p
++;
454 if (!*p
) fatal_error( "%s:%d: Malformed #include or import directive\n",
455 pFile
->filename
, input_line
);
457 add_include( pFile
, include
, input_line
, (quote
== '>') );
461 /*******************************************************************
464 static void parse_c_file( INCL_FILE
*pFile
, FILE *file
)
466 char *buffer
, *include
;
469 while ((buffer
= get_line( file
)))
473 while (*p
&& isspace(*p
)) p
++;
474 if (*p
++ != '#') continue;
475 while (*p
&& isspace(*p
)) p
++;
476 if (strncmp( p
, "include", 7 )) continue;
478 while (*p
&& isspace(*p
)) p
++;
479 if (*p
!= '\"' && *p
!= '<' ) continue;
481 if (quote
== '<') quote
= '>';
483 while (*p
&& (*p
!= quote
)) p
++;
484 if (!*p
) fatal_error( "%s:%d: Malformed #include directive\n",
485 pFile
->filename
, input_line
);
487 add_include( pFile
, include
, input_line
, (quote
== '>') );
492 /*******************************************************************
495 static void parse_file( INCL_FILE
*pFile
, int src
)
500 if (is_generated( pFile
->name
))
502 /* file is generated during make, don't try to open it */
503 pFile
->filename
= xstrdup( pFile
->name
);
507 file
= src
? open_src_file( pFile
) : open_include_file( pFile
);
509 ext
= get_extension( pFile
->name
);
510 if (ext
&& !strcmp( ext
, ".idl" )) parse_idl_file( pFile
, file
);
511 else parse_c_file( pFile
, file
);
516 /*******************************************************************
519 static void output_include( FILE *file
, INCL_FILE
*pFile
,
520 INCL_FILE
*owner
, int *column
)
524 if (pFile
->owner
== owner
) return;
525 if (!pFile
->filename
) return;
526 pFile
->owner
= owner
;
527 if (*column
+ strlen(pFile
->filename
) + 1 > 70)
529 fprintf( file
, " \\\n" );
532 fprintf( file
, " %s", pFile
->filename
);
533 *column
+= strlen(pFile
->filename
) + 1;
534 for (i
= 0; i
< MAX_INCLUDES
; i
++)
535 if (pFile
->files
[i
]) output_include( file
, pFile
->files
[i
],
540 /*******************************************************************
543 static void output_src( FILE *file
, INCL_FILE
*pFile
, int *column
)
545 char *obj
= xstrdup( pFile
->name
);
546 char *ext
= get_extension( obj
);
550 if (!strcmp( ext
, "y" )) /* yacc file */
552 *column
+= fprintf( file
, "%s.tab.o: %s.tab.c", obj
, obj
);
554 else if (!strcmp( ext
, "l" )) /* lex file */
556 *column
+= fprintf( file
, "%s.o: %s.c", LEX_OUTPUT_ROOT
, LEX_OUTPUT_ROOT
);
558 else if (!strcmp( ext
, "rc" )) /* resource file */
560 *column
+= fprintf( file
, "%s.res: %s", obj
, pFile
->filename
);
562 else if (!strcmp( ext
, "mc" )) /* message file */
564 *column
+= fprintf( file
, "%s.mc.rc: %s", obj
, pFile
->filename
);
566 else if (!strcmp( ext
, "idl" )) /* IDL file */
568 *column
+= fprintf( file
, "%s.h: %s", obj
, pFile
->filename
);
572 *column
+= fprintf( file
, "%s.o: %s", obj
, pFile
->filename
);
579 /*******************************************************************
580 * output_dependencies
582 static void output_dependencies(void)
589 if (Separator
&& ((file
= fopen( OutputFileName
, "r+" ))))
591 while ((buffer
= get_line( file
)))
592 if (!strncmp( buffer
, Separator
, strlen(Separator
) )) break;
593 ftruncate( fileno(file
), ftell(file
) );
594 fseek( file
, 0L, SEEK_END
);
598 if (!(file
= fopen( OutputFileName
, Separator
? "a" : "w" )))
600 perror( OutputFileName
);
604 LIST_FOR_EACH_ENTRY( pFile
, &sources
, INCL_FILE
, entry
)
607 output_src( file
, pFile
, &column
);
608 for (i
= 0; i
< MAX_INCLUDES
; i
++)
609 if (pFile
->files
[i
]) output_include( file
, pFile
->files
[i
],
611 fprintf( file
, "\n" );
617 /*******************************************************************
620 static void parse_option( const char *opt
)
625 if (opt
[2]) add_include_path( opt
+ 2 );
631 top_src_dir
= opt
+ 2;
634 top_obj_dir
= opt
+ 2;
637 if (opt
[2]) OutputFileName
= opt
+ 2;
640 if (opt
[2]) Separator
= opt
+ 2;
641 else Separator
= NULL
;
644 fprintf( stderr
, "Unknown option '%s'\n", opt
);
645 fprintf( stderr
, Usage
, ProgramName
);
651 /*******************************************************************
654 int main( int argc
, char *argv
[] )
657 INCL_PATH
*path
, *next
;
660 ProgramName
= argv
[0];
665 if (argv
[i
][0] == '-')
667 parse_option( argv
[i
] );
668 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
674 /* ignore redundant source paths */
675 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
676 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
678 /* get rid of absolute paths that don't point into the source dir */
679 LIST_FOR_EACH_ENTRY_SAFE( path
, next
, &paths
, INCL_PATH
, entry
)
681 if (path
->name
[0] != '/') continue;
684 if (!strncmp( path
->name
, top_src_dir
, strlen(top_src_dir
) )) continue;
685 if (path
->name
[strlen(top_src_dir
)] == '/') continue;
687 list_remove( &path
->entry
);
691 for (i
= 1; i
< argc
; i
++)
693 pFile
= add_src_file( argv
[i
] );
694 parse_file( pFile
, 1 );
696 LIST_FOR_EACH_ENTRY( pFile
, &includes
, INCL_FILE
, entry
) parse_file( pFile
, 0 );
697 if (!list_empty( &sources
)) output_dependencies();