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 char *sourcename
; /* source file name for generated headers */
46 struct _INCL_FILE
*included_by
; /* file that included this one */
47 int included_line
; /* line where this file was included */
48 int system
; /* is it a system include (#include <name>) */
49 struct _INCL_FILE
*owner
;
50 struct _INCL_FILE
*files
[MAX_INCLUDES
];
53 static struct list sources
= LIST_INIT(sources
);
54 static struct list includes
= LIST_INIT(includes
);
56 typedef struct _INCL_PATH
62 static struct list paths
= LIST_INIT(paths
);
64 static const char *src_dir
;
65 static const char *top_src_dir
;
66 static const char *top_obj_dir
;
67 static const char *OutputFileName
= "Makefile";
68 static const char *Separator
= "### Dependencies";
69 static const char *ProgramName
;
70 static int input_line
;
72 static const char Usage
[] =
73 "Usage: %s [options] [files]\n"
75 " -Idir Search for include files in directory 'dir'\n"
76 " -Cdir Search for source files in directory 'dir'\n"
77 " -Sdir Set the top source directory\n"
78 " -Sdir Set the top object directory\n"
79 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
80 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
83 /*******************************************************************
86 static void fatal_error( const char *msg
, ... )
89 va_start( valist
, msg
);
90 vfprintf( stderr
, msg
, valist
);
96 /*******************************************************************
99 static void *xmalloc( size_t size
)
102 if (!(res
= malloc (size
? size
: 1)))
103 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
108 /*******************************************************************
111 static void *xrealloc (void *ptr
, size_t size
)
115 if (!(res
= realloc( ptr
, size
)))
116 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
120 /*******************************************************************
123 static char *xstrdup( const char *str
)
125 char *res
= strdup( str
);
126 if (!res
) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
131 /*******************************************************************
134 static char *strmake( const char* fmt
, ... )
142 char *p
= xmalloc (size
);
144 n
= vsnprintf (p
, size
, fmt
, ap
);
146 if (n
== -1) size
*= 2;
147 else if ((size_t)n
>= size
) size
= n
+ 1;
154 /*******************************************************************
157 static int strendswith( const char* str
, const char* end
)
162 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
165 /*******************************************************************
168 static char *get_extension( char *filename
)
170 char *ext
= strrchr( filename
, '.' );
171 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
176 /*******************************************************************
179 static char *get_line( FILE *file
)
182 static unsigned int size
;
187 buffer
= xmalloc( size
);
189 if (!fgets( buffer
, size
, file
)) return NULL
;
194 char *p
= buffer
+ strlen(buffer
);
195 /* if line is larger than buffer, resize buffer */
196 while (p
== buffer
+ size
- 1 && p
[-1] != '\n')
198 buffer
= xrealloc( buffer
, size
* 2 );
199 fgets( buffer
+ size
- 1, size
+ 1, file
);
200 p
= buffer
+ strlen(buffer
);
203 if (p
> buffer
&& p
[-1] == '\n')
206 if (p
> buffer
&& p
[-1] == '\r') *(--p
) = 0;
207 if (p
> buffer
&& p
[-1] == '\\')
210 /* line ends in backslash, read continuation line */
211 fgets( p
, size
- (p
- buffer
), file
);
220 /*******************************************************************
223 * Add a directory to the include path.
225 static void add_include_path( const char *name
)
227 INCL_PATH
*path
= xmalloc( sizeof(*path
) );
228 list_add_tail( &paths
, &path
->entry
);
233 /*******************************************************************
236 * Add a source file to the list.
238 static INCL_FILE
*add_src_file( const char *name
)
240 INCL_FILE
*file
= xmalloc( sizeof(*file
) );
241 memset( file
, 0, sizeof(*file
) );
242 file
->name
= xstrdup(name
);
243 list_add_tail( &sources
, &file
->entry
);
248 /*******************************************************************
251 * Add an include file if it doesn't already exists.
253 static INCL_FILE
*add_include( INCL_FILE
*pFile
, const char *name
, int line
, int system
)
259 for (pos
= 0; pos
< MAX_INCLUDES
; pos
++) if (!pFile
->files
[pos
]) break;
260 if (pos
>= MAX_INCLUDES
)
261 fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
262 ProgramName
, pFile
->name
);
264 /* enforce some rules for the Wine tree */
266 if (!memcmp( name
, "../", 3 ))
267 fatal_error( "%s:%d: #include directive with relative path not allowed\n",
268 pFile
->filename
, line
);
270 if (!strcmp( name
, "config.h" ))
272 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
273 fatal_error( "%s:%d: config.h must not be included by a header file\n",
274 pFile
->filename
, line
);
276 fatal_error( "%s:%d: config.h must be included before anything else\n",
277 pFile
->filename
, line
);
279 else if (!strcmp( name
, "wine/port.h" ))
281 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
282 fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
283 pFile
->filename
, line
);
284 if (!pos
) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
285 pFile
->filename
, line
);
287 fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
288 pFile
->filename
, line
);
289 if (strcmp( pFile
->files
[0]->name
, "config.h" ))
290 fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
291 pFile
->filename
, line
);
294 LIST_FOR_EACH_ENTRY( include
, &includes
, INCL_FILE
, entry
)
295 if (!strcmp( name
, include
->name
)) goto found
;
297 include
= xmalloc( sizeof(INCL_FILE
) );
298 memset( include
, 0, sizeof(INCL_FILE
) );
299 include
->name
= xstrdup(name
);
300 include
->included_by
= pFile
;
301 include
->included_line
= line
;
302 include
->system
= system
;
303 list_add_tail( &includes
, &include
->entry
);
305 pFile
->files
[pos
] = include
;
310 /*******************************************************************
313 static FILE *open_src_file( INCL_FILE
*pFile
)
317 /* first try name as is */
318 if ((file
= fopen( pFile
->name
, "r" )))
320 pFile
->filename
= xstrdup( pFile
->name
);
323 /* now try in source dir */
326 pFile
->filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
327 file
= fopen( pFile
->filename
, "r" );
331 perror( pFile
->name
);
338 /*******************************************************************
341 static FILE *open_include_file( INCL_FILE
*pFile
)
349 /* check for generated bison header */
351 if (strendswith( pFile
->name
, ".tab.h" ))
354 filename
= strmake( "%s/%.*s.y", src_dir
, strlen(pFile
->name
) - 6, pFile
->name
);
356 filename
= strmake( "%.*s.y", strlen(pFile
->name
) - 6, pFile
->name
);
358 if ((file
= fopen( filename
, "r" )))
360 pFile
->sourcename
= filename
;
361 pFile
->filename
= xstrdup( pFile
->name
);
362 /* don't bother to parse it */
369 /* check for generated message resource */
371 if (strendswith( pFile
->name
, ".mc.rc" ))
374 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
376 filename
= xstrdup( pFile
->name
);
378 filename
[strlen(filename
) - 3] = 0;
380 if ((file
= fopen( filename
, "r" )))
382 pFile
->sourcename
= filename
;
383 pFile
->filename
= xstrdup( pFile
->name
);
384 /* don't bother to parse it */
391 /* check for corresponding idl file in source dir */
393 if (strendswith( pFile
->name
, ".h" ))
396 filename
= strmake( "%s/%.*s.idl", src_dir
, strlen(pFile
->name
) - 2, pFile
->name
);
398 filename
= strmake( "%.*s.idl", strlen(pFile
->name
) - 2, pFile
->name
);
400 if ((file
= fopen( filename
, "r" )))
402 pFile
->sourcename
= filename
;
403 pFile
->filename
= xstrdup( pFile
->name
);
409 /* first try name as is */
410 if ((file
= fopen( pFile
->name
, "r" )))
412 pFile
->filename
= xstrdup( pFile
->name
);
416 /* now try in source dir */
419 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
420 if ((file
= fopen( filename
, "r" ))) goto found
;
424 /* check for corresponding idl file in global includes */
426 if (strendswith( pFile
->name
, ".h" ))
429 filename
= strmake( "%s/include/%.*s.idl",
430 top_src_dir
, strlen(pFile
->name
) - 2, pFile
->name
);
431 else if (top_obj_dir
)
432 filename
= strmake( "%s/include/%.*s.idl",
433 top_obj_dir
, strlen(pFile
->name
) - 2, pFile
->name
);
437 if (filename
&& (file
= fopen( filename
, "r" )))
439 pFile
->sourcename
= filename
;
440 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
446 /* now try in global includes */
449 filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
450 if ((file
= fopen( filename
, "r" ))) goto found
;
455 filename
= strmake( "%s/include/%s", top_src_dir
, pFile
->name
);
456 if ((file
= fopen( filename
, "r" ))) goto found
;
460 /* now search in include paths */
461 LIST_FOR_EACH_ENTRY( path
, &paths
, INCL_PATH
, entry
)
463 filename
= strmake( "%s/%s", path
->name
, pFile
->name
);
464 if ((file
= fopen( filename
, "r" ))) goto found
;
467 if (pFile
->system
) return NULL
; /* ignore system files we cannot find */
469 /* try in src file directory */
470 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
472 int l
= p
- pFile
->included_by
->filename
+ 1;
473 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
474 memcpy( filename
, pFile
->included_by
->filename
, l
);
475 strcpy( filename
+ l
, pFile
->name
);
476 if ((file
= fopen( filename
, "r" ))) goto found
;
480 perror( pFile
->name
);
481 while (pFile
->included_by
)
483 const char *parent
= pFile
->included_by
->sourcename
;
484 if (!parent
) parent
= pFile
->included_by
->name
;
485 fprintf( stderr
, " %s was first included from %s:%d\n",
486 pFile
->name
, parent
, pFile
->included_line
);
487 pFile
= pFile
->included_by
;
492 pFile
->filename
= filename
;
497 /*******************************************************************
500 * If for_h_file is non-zero, it means we are not interested in the idl file
501 * itself, but only in the contents of the .h file that will be generated from it.
503 static void parse_idl_file( INCL_FILE
*pFile
, FILE *file
, int for_h_file
)
505 char *buffer
, *include
;
509 /* generated .h file always includes these */
510 add_include( pFile
, "rpc.h", 0, 1 );
511 add_include( pFile
, "rpcndr.h", 0, 1 );
515 while ((buffer
= get_line( file
)))
519 while (*p
&& isspace(*p
)) p
++;
521 if (!strncmp( p
, "import", 6 ))
524 while (*p
&& isspace(*p
)) p
++;
525 if (*p
!= '"') continue;
527 while (*p
&& (*p
!= '"')) p
++;
528 if (!*p
) fatal_error( "%s:%d: Malformed import directive\n", pFile
->filename
, input_line
);
530 if (for_h_file
&& strendswith( include
, ".idl" )) strcpy( p
- 4, ".h" );
531 add_include( pFile
, include
, input_line
, 0 );
535 if (for_h_file
) /* only check for #include inside cpp_quote */
537 if (strncmp( p
, "cpp_quote", 9 )) continue;
539 while (*p
&& isspace(*p
)) p
++;
540 if (*p
++ != '(') continue;
541 while (*p
&& isspace(*p
)) p
++;
542 if (*p
++ != '"') continue;
543 if (*p
++ != '#') continue;
544 while (*p
&& isspace(*p
)) p
++;
545 if (strncmp( p
, "include", 7 )) continue;
547 while (*p
&& isspace(*p
)) p
++;
548 if (*p
== '\\' && p
[1] == '"')
555 if (*p
++ != '<' ) continue;
559 while (*p
&& (*p
!= quote
)) p
++;
560 if (!*p
|| (quote
== '"' && p
[-1] != '\\'))
561 fatal_error( "%s:%d: Malformed #include directive inside cpp_quote\n",
562 pFile
->filename
, input_line
);
563 if (quote
== '"') p
--; /* remove backslash */
565 add_include( pFile
, include
, input_line
, (quote
== '>') );
569 /* check for normal #include */
570 if (*p
++ != '#') continue;
571 while (*p
&& isspace(*p
)) p
++;
572 if (strncmp( p
, "include", 7 )) continue;
574 while (*p
&& isspace(*p
)) p
++;
575 if (*p
!= '\"' && *p
!= '<' ) continue;
577 if (quote
== '<') quote
= '>';
579 while (*p
&& (*p
!= quote
)) p
++;
580 if (!*p
) fatal_error( "%s:%d: Malformed #include directive\n", pFile
->filename
, input_line
);
582 add_include( pFile
, include
, input_line
, (quote
== '>') );
586 /*******************************************************************
589 static void parse_c_file( INCL_FILE
*pFile
, FILE *file
)
591 char *buffer
, *include
;
594 while ((buffer
= get_line( file
)))
598 while (*p
&& isspace(*p
)) p
++;
599 if (*p
++ != '#') continue;
600 while (*p
&& isspace(*p
)) p
++;
601 if (strncmp( p
, "include", 7 )) continue;
603 while (*p
&& isspace(*p
)) p
++;
604 if (*p
!= '\"' && *p
!= '<' ) continue;
606 if (quote
== '<') quote
= '>';
608 while (*p
&& (*p
!= quote
)) p
++;
609 if (!*p
) fatal_error( "%s:%d: Malformed #include directive\n",
610 pFile
->filename
, input_line
);
612 add_include( pFile
, include
, input_line
, (quote
== '>') );
617 /*******************************************************************
620 static void parse_file( INCL_FILE
*pFile
, int src
)
624 file
= src
? open_src_file( pFile
) : open_include_file( pFile
);
627 if (pFile
->sourcename
&& strendswith( pFile
->sourcename
, ".idl" ))
628 parse_idl_file( pFile
, file
, 1 );
629 else if (strendswith( pFile
->filename
, ".idl" ))
630 parse_idl_file( pFile
, file
, 0 );
632 parse_c_file( pFile
, file
);
637 /*******************************************************************
640 static void output_include( FILE *file
, INCL_FILE
*pFile
,
641 INCL_FILE
*owner
, int *column
)
645 if (pFile
->owner
== owner
) return;
646 if (!pFile
->filename
) return;
647 pFile
->owner
= owner
;
648 if (*column
+ strlen(pFile
->filename
) + 1 > 70)
650 fprintf( file
, " \\\n" );
653 fprintf( file
, " %s", pFile
->filename
);
654 *column
+= strlen(pFile
->filename
) + 1;
655 for (i
= 0; i
< MAX_INCLUDES
; i
++)
656 if (pFile
->files
[i
]) output_include( file
, pFile
->files
[i
],
661 /*******************************************************************
664 static void output_src( FILE *file
, INCL_FILE
*pFile
, int *column
)
666 char *obj
= xstrdup( pFile
->name
);
667 char *ext
= get_extension( obj
);
671 if (!strcmp( ext
, "y" )) /* yacc file */
673 *column
+= fprintf( file
, "%s.tab.o: %s.tab.c", obj
, obj
);
675 else if (!strcmp( ext
, "l" )) /* lex file */
677 *column
+= fprintf( file
, "%s.yy.o: %s.yy.c", obj
, obj
);
679 else if (!strcmp( ext
, "rc" )) /* resource file */
681 *column
+= fprintf( file
, "%s.res: %s", obj
, pFile
->filename
);
683 else if (!strcmp( ext
, "mc" )) /* message file */
685 *column
+= fprintf( file
, "%s.mc.rc: %s", obj
, pFile
->filename
);
687 else if (!strcmp( ext
, "idl" )) /* IDL file */
689 *column
+= fprintf( file
, "%s.h: %s", obj
, pFile
->filename
);
693 *column
+= fprintf( file
, "%s.o: %s", obj
, pFile
->filename
);
700 /*******************************************************************
701 * output_dependencies
703 static void output_dependencies(void)
710 if (Separator
&& ((file
= fopen( OutputFileName
, "r+" ))))
712 while ((buffer
= get_line( file
)))
714 if (strncmp( buffer
, Separator
, strlen(Separator
) )) continue;
715 ftruncate( fileno(file
), ftell(file
) );
716 fseek( file
, 0L, SEEK_END
);
722 if (!(file
= fopen( OutputFileName
, Separator
? "a" : "w" )))
724 perror( OutputFileName
);
728 LIST_FOR_EACH_ENTRY( pFile
, &sources
, INCL_FILE
, entry
)
731 output_src( file
, pFile
, &column
);
732 for (i
= 0; i
< MAX_INCLUDES
; i
++)
733 if (pFile
->files
[i
]) output_include( file
, pFile
->files
[i
],
735 fprintf( file
, "\n" );
741 /*******************************************************************
744 static void parse_option( const char *opt
)
749 if (opt
[2]) add_include_path( opt
+ 2 );
755 top_src_dir
= opt
+ 2;
758 top_obj_dir
= opt
+ 2;
761 if (opt
[2]) OutputFileName
= opt
+ 2;
764 if (opt
[2]) Separator
= opt
+ 2;
765 else Separator
= NULL
;
768 fprintf( stderr
, "Unknown option '%s'\n", opt
);
769 fprintf( stderr
, Usage
, ProgramName
);
775 /*******************************************************************
778 int main( int argc
, char *argv
[] )
781 INCL_PATH
*path
, *next
;
784 ProgramName
= argv
[0];
789 if (argv
[i
][0] == '-')
791 parse_option( argv
[i
] );
792 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
798 /* ignore redundant source paths */
799 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
800 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
802 /* get rid of absolute paths that don't point into the source dir */
803 LIST_FOR_EACH_ENTRY_SAFE( path
, next
, &paths
, INCL_PATH
, entry
)
805 if (path
->name
[0] != '/') continue;
808 if (!strncmp( path
->name
, top_src_dir
, strlen(top_src_dir
) )) continue;
809 if (path
->name
[strlen(top_src_dir
)] == '/') continue;
811 list_remove( &path
->entry
);
815 for (i
= 1; i
< argc
; i
++)
817 pFile
= add_src_file( argv
[i
] );
818 parse_file( pFile
, 1 );
820 LIST_FOR_EACH_ENTRY( pFile
, &includes
, INCL_FILE
, entry
) parse_file( pFile
, 0 );
821 output_dependencies();