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
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 unsigned int flags
; /* flags (see below) */
49 struct incl_file
*owner
;
50 struct incl_file
*files
[MAX_INCLUDES
];
53 #define FLAG_SYSTEM 0x0001 /* is it a system include (#include <name>) */
54 #define FLAG_IDL_PROXY 0x0002 /* generates a proxy (_p.c) file */
55 #define FLAG_IDL_CLIENT 0x0004 /* generates a client (_c.c) file */
56 #define FLAG_IDL_SERVER 0x0008 /* generates a server (_s.c) file */
57 #define FLAG_IDL_IDENT 0x0010 /* generates an ident (_i.c) file */
58 #define FLAG_IDL_REGISTER 0x0020 /* generates a registration (_r.res) file */
59 #define FLAG_IDL_TYPELIB 0x0040 /* generates a typelib (.tlb) file */
60 #define FLAG_IDL_HEADER 0x0080 /* generates a header (.h) file */
61 #define FLAG_RC_PO 0x0100 /* rc file contains translations */
62 #define FLAG_C_IMPLIB 0x0200 /* file is part of an import library */
71 { FLAG_IDL_TYPELIB
, ".tlb", "$(TARGETFLAGS) $(IDLFLAGS) -t" },
72 { FLAG_IDL_TYPELIB
, "_t.res", "$(TARGETFLAGS) $(IDLFLAGS) -t" },
73 { FLAG_IDL_CLIENT
, "_c.c", "$(IDLFLAGS) -c" },
74 { FLAG_IDL_IDENT
, "_i.c", "$(IDLFLAGS) -u" },
75 { FLAG_IDL_PROXY
, "_p.c", "$(IDLFLAGS) -p" },
76 { FLAG_IDL_SERVER
, "_s.c", "$(IDLFLAGS) -s" },
77 { FLAG_IDL_REGISTER
, "_r.res", "$(IDLFLAGS) -r" },
78 { FLAG_IDL_HEADER
, ".h", "$(IDLFLAGS) -h" },
81 static struct list sources
= LIST_INIT(sources
);
82 static struct list includes
= LIST_INIT(includes
);
84 struct object_extension
87 const char *extension
;
90 static struct list object_extensions
= LIST_INIT(object_extensions
);
98 static struct list paths
= LIST_INIT(paths
);
102 unsigned int count
; /* strings in use */
103 unsigned int size
; /* total allocated size */
107 static const char *src_dir
;
108 static const char *top_src_dir
;
109 static const char *top_obj_dir
;
110 static const char *parent_dir
;
111 static const char *OutputFileName
= "Makefile";
112 static const char *Separator
= "### Dependencies";
113 static const char *input_file_name
;
114 static int relative_dir_mode
;
115 static int input_line
;
116 static FILE *output_file
;
118 static const char Usage
[] =
119 "Usage: makedep [options] [files]\n"
121 " -Idir Search for include files in directory 'dir'\n"
122 " -Cdir Search for source files in directory 'dir'\n"
123 " -Sdir Set the top source directory\n"
124 " -Tdir Set the top object directory\n"
125 " -Pdir Set the parent source directory\n"
126 " -R from to Compute the relative path between two directories\n"
127 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
128 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
132 #define __attribute__(x)
135 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
136 static void fatal_perror( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
137 static int output( const char *format
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
138 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
140 /*******************************************************************
143 static void fatal_error( const char *msg
, ... )
146 va_start( valist
, msg
);
149 fprintf( stderr
, "%s:", input_file_name
);
150 if (input_line
) fprintf( stderr
, "%d:", input_line
);
151 fprintf( stderr
, " error: " );
153 else fprintf( stderr
, "makedep: error: " );
154 vfprintf( stderr
, msg
, valist
);
160 /*******************************************************************
163 static void fatal_perror( const char *msg
, ... )
166 va_start( valist
, msg
);
169 fprintf( stderr
, "%s:", input_file_name
);
170 if (input_line
) fprintf( stderr
, "%d:", input_line
);
171 fprintf( stderr
, " error: " );
173 else fprintf( stderr
, "makedep: error: " );
174 vfprintf( stderr
, msg
, valist
);
181 /*******************************************************************
184 static void *xmalloc( size_t size
)
187 if (!(res
= malloc (size
? size
: 1)))
188 fatal_error( "Virtual memory exhausted.\n" );
193 /*******************************************************************
196 static void *xrealloc (void *ptr
, size_t size
)
200 if (!(res
= realloc( ptr
, size
)))
201 fatal_error( "Virtual memory exhausted.\n" );
205 /*******************************************************************
208 static char *xstrdup( const char *str
)
210 char *res
= strdup( str
);
211 if (!res
) fatal_error( "Virtual memory exhausted.\n" );
216 /*******************************************************************
219 static char *strmake( const char* fmt
, ... )
227 char *p
= xmalloc (size
);
229 n
= vsnprintf (p
, size
, fmt
, ap
);
231 if (n
== -1) size
*= 2;
232 else if ((size_t)n
>= size
) size
= n
+ 1;
239 /*******************************************************************
242 static int strendswith( const char* str
, const char* end
)
247 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
251 /*******************************************************************
254 static int output( const char *format
, ... )
259 va_start( valist
, format
);
260 ret
= vfprintf( output_file
, format
, valist
);
262 if (ret
< 0) fatal_perror( "output" );
267 /*******************************************************************
270 static void strarray_init( struct strarray
*array
)
278 /*******************************************************************
281 static void strarray_add( struct strarray
*array
, const char *str
)
283 if (array
->count
== array
->size
)
285 if (array
->size
) array
->size
*= 2;
286 else array
->size
= 16;
287 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
289 array
->str
[array
->count
++] = str
;
293 /*******************************************************************
296 static void output_filename( const char *name
, int *column
)
298 if (*column
+ strlen(name
) + 1 > 100)
303 *column
+= output( " %s", name
);
307 /*******************************************************************
310 static char *get_extension( char *filename
)
312 char *ext
= strrchr( filename
, '.' );
313 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
318 /*******************************************************************
321 static char *replace_extension( const char *name
, unsigned int old_len
, const char *new_ext
)
323 char *ret
= xmalloc( strlen( name
) + strlen( new_ext
) + 1 );
325 strcpy( ret
+ strlen( ret
) - old_len
, new_ext
);
330 /*******************************************************************
333 * Determine where the destination path is located relative to the 'from' path.
335 static char *get_relative_path( const char *from
, const char *dest
)
339 unsigned int dotdots
= 0;
341 /* a path of "." is equivalent to an empty path */
342 if (!strcmp( from
, "." )) from
= "";
346 while (*from
== '/') from
++;
347 while (*dest
== '/') dest
++;
348 start
= dest
; /* save start of next path element */
351 while (*from
&& *from
!= '/' && *from
== *dest
) { from
++; dest
++; }
352 if ((!*from
|| *from
== '/') && (!*dest
|| *dest
== '/')) continue;
354 /* count remaining elements in 'from' */
358 while (*from
&& *from
!= '/') from
++;
359 while (*from
== '/') from
++;
365 if (!start
[0] && !dotdots
) return NULL
; /* empty path */
367 ret
= xmalloc( 3 * dotdots
+ strlen( start
) + 1 );
368 for (p
= ret
; dotdots
; dotdots
--, p
+= 3) memcpy( p
, "../", 3 );
370 if (start
[0]) strcpy( p
, start
);
371 else p
[-1] = 0; /* remove trailing slash */
376 /*******************************************************************
379 static char *get_line( FILE *file
)
382 static unsigned int size
;
387 buffer
= xmalloc( size
);
389 if (!fgets( buffer
, size
, file
)) return NULL
;
394 char *p
= buffer
+ strlen(buffer
);
395 /* if line is larger than buffer, resize buffer */
396 while (p
== buffer
+ size
- 1 && p
[-1] != '\n')
398 buffer
= xrealloc( buffer
, size
* 2 );
399 if (!fgets( buffer
+ size
- 1, size
+ 1, file
)) break;
400 p
= buffer
+ strlen(buffer
);
403 if (p
> buffer
&& p
[-1] == '\n')
406 if (p
> buffer
&& p
[-1] == '\r') *(--p
) = 0;
407 if (p
> buffer
&& p
[-1] == '\\')
410 /* line ends in backslash, read continuation line */
411 if (!fgets( p
, size
- (p
- buffer
), file
)) return buffer
;
420 /*******************************************************************
421 * add_object_extension
423 * Add an extension for object files.
425 static void add_object_extension( const char *ext
)
427 struct object_extension
*object_extension
= xmalloc( sizeof(*object_extension
) );
428 list_add_tail( &object_extensions
, &object_extension
->entry
);
429 object_extension
->extension
= ext
;
432 /*******************************************************************
435 * Add a directory to the include path.
437 static void add_include_path( const char *name
)
439 struct incl_path
*path
= xmalloc( sizeof(*path
) );
440 list_add_tail( &paths
, &path
->entry
);
444 /*******************************************************************
447 static struct incl_file
*find_src_file( const char *name
)
449 struct incl_file
*file
;
451 LIST_FOR_EACH_ENTRY( file
, &sources
, struct incl_file
, entry
)
452 if (!strcmp( name
, file
->name
)) return file
;
456 /*******************************************************************
459 static struct incl_file
*find_include_file( const char *name
)
461 struct incl_file
*file
;
463 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
)
464 if (!strcmp( name
, file
->name
)) return file
;
468 /*******************************************************************
471 * Add an include file if it doesn't already exists.
473 static struct incl_file
*add_include( struct incl_file
*pFile
, const char *name
, int system
)
475 struct incl_file
*include
;
479 for (pos
= 0; pos
< MAX_INCLUDES
; pos
++) if (!pFile
->files
[pos
]) break;
480 if (pos
>= MAX_INCLUDES
)
481 fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
483 /* enforce some rules for the Wine tree */
485 if (!memcmp( name
, "../", 3 ))
486 fatal_error( "#include directive with relative path not allowed\n" );
488 if (!strcmp( name
, "config.h" ))
490 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
491 fatal_error( "config.h must not be included by a header file\n" );
493 fatal_error( "config.h must be included before anything else\n" );
495 else if (!strcmp( name
, "wine/port.h" ))
497 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
498 fatal_error( "wine/port.h must not be included by a header file\n" );
499 if (!pos
) fatal_error( "config.h must be included before wine/port.h\n" );
501 fatal_error( "wine/port.h must be included before everything except config.h\n" );
502 if (strcmp( pFile
->files
[0]->name
, "config.h" ))
503 fatal_error( "config.h must be included before wine/port.h\n" );
506 LIST_FOR_EACH_ENTRY( include
, &includes
, struct incl_file
, entry
)
507 if (!strcmp( name
, include
->name
)) goto found
;
509 include
= xmalloc( sizeof(*include
) );
510 memset( include
, 0, sizeof(*include
) );
511 include
->name
= xstrdup(name
);
512 include
->included_by
= pFile
;
513 include
->included_line
= input_line
;
514 if (system
) include
->flags
|= FLAG_SYSTEM
;
515 list_add_tail( &includes
, &include
->entry
);
517 pFile
->files
[pos
] = include
;
522 /*******************************************************************
525 static FILE *open_src_file( struct incl_file
*pFile
)
529 /* first try name as is */
530 if ((file
= fopen( pFile
->name
, "r" )))
532 pFile
->filename
= xstrdup( pFile
->name
);
535 /* now try in source dir */
538 pFile
->filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
539 file
= fopen( pFile
->filename
, "r" );
541 /* now try parent dir */
542 if (!file
&& parent_dir
)
545 pFile
->filename
= strmake( "%s/%s/%s", src_dir
, parent_dir
, pFile
->name
);
547 pFile
->filename
= strmake( "%s/%s", parent_dir
, pFile
->name
);
548 if ((file
= fopen( pFile
->filename
, "r" ))) return file
;
549 file
= fopen( pFile
->filename
, "r" );
551 if (!file
) fatal_perror( "open %s", pFile
->name
);
556 /*******************************************************************
559 static FILE *open_include_file( struct incl_file
*pFile
)
563 struct incl_path
*path
;
567 /* check for generated bison header */
569 if (strendswith( pFile
->name
, ".tab.h" ))
571 filename
= replace_extension( pFile
->name
, 6, ".y" );
572 if (src_dir
) filename
= strmake( "%s/%s", src_dir
, filename
);
574 if ((file
= fopen( filename
, "r" )))
576 pFile
->sourcename
= filename
;
577 pFile
->filename
= xstrdup( pFile
->name
);
578 /* don't bother to parse it */
585 /* check for corresponding idl file in source dir */
587 if (strendswith( pFile
->name
, ".h" ))
589 filename
= replace_extension( pFile
->name
, 2, ".idl" );
590 if (src_dir
) filename
= strmake( "%s/%s", src_dir
, filename
);
592 if ((file
= fopen( filename
, "r" )))
594 pFile
->sourcename
= filename
;
595 pFile
->filename
= xstrdup( pFile
->name
);
601 /* first try name as is */
602 if ((file
= fopen( pFile
->name
, "r" )))
604 pFile
->filename
= xstrdup( pFile
->name
);
608 /* now try in source dir */
611 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
612 if ((file
= fopen( filename
, "r" ))) goto found
;
616 /* now try in parent source dir */
620 filename
= strmake( "%s/%s/%s", src_dir
, parent_dir
, pFile
->name
);
622 filename
= strmake( "%s/%s", parent_dir
, pFile
->name
);
623 if ((file
= fopen( filename
, "r" ))) goto found
;
627 /* check for corresponding idl file in global includes */
629 if (strendswith( pFile
->name
, ".h" ))
631 filename
= replace_extension( pFile
->name
, 2, ".idl" );
633 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
634 else if (top_obj_dir
)
635 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
639 if (filename
&& (file
= fopen( filename
, "r" )))
641 pFile
->sourcename
= filename
;
642 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
648 /* check for corresponding .x file in global includes */
650 if (strendswith( pFile
->name
, "tmpl.h" ))
652 filename
= replace_extension( pFile
->name
, 2, ".x" );
654 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
655 else if (top_obj_dir
)
656 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
660 if (filename
&& (file
= fopen( filename
, "r" )))
662 pFile
->sourcename
= filename
;
663 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
669 /* now try in global includes */
672 filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
673 if ((file
= fopen( filename
, "r" ))) goto found
;
678 filename
= strmake( "%s/include/%s", top_src_dir
, pFile
->name
);
679 if ((file
= fopen( filename
, "r" ))) goto found
;
683 /* now search in include paths */
684 LIST_FOR_EACH_ENTRY( path
, &paths
, struct incl_path
, entry
)
686 filename
= strmake( "%s/%s", path
->name
, pFile
->name
);
687 if ((file
= fopen( filename
, "r" ))) goto found
;
690 if (pFile
->flags
& FLAG_SYSTEM
) return NULL
; /* ignore system files we cannot find */
692 /* try in src file directory */
693 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
695 int l
= p
- pFile
->included_by
->filename
+ 1;
696 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
697 memcpy( filename
, pFile
->included_by
->filename
, l
);
698 strcpy( filename
+ l
, pFile
->name
);
699 if ((file
= fopen( filename
, "r" ))) goto found
;
703 fprintf( stderr
, "%s:%d: error: ", pFile
->included_by
->filename
, pFile
->included_line
);
704 perror( pFile
->name
);
705 pFile
= pFile
->included_by
;
706 while (pFile
&& pFile
->included_by
)
708 const char *parent
= pFile
->included_by
->sourcename
;
709 if (!parent
) parent
= pFile
->included_by
->name
;
710 fprintf( stderr
, "%s:%d: note: %s was first included here\n",
711 parent
, pFile
->included_line
, pFile
->name
);
712 pFile
= pFile
->included_by
;
717 pFile
->filename
= filename
;
722 /*******************************************************************
723 * parse_include_directive
725 static void parse_include_directive( struct incl_file
*source
, char *str
)
727 char quote
, *include
, *p
= str
;
729 while (*p
&& isspace(*p
)) p
++;
730 if (*p
!= '\"' && *p
!= '<' ) return;
732 if (quote
== '<') quote
= '>';
734 while (*p
&& (*p
!= quote
)) p
++;
735 if (!*p
) fatal_error( "malformed include directive '%s'\n", str
);
737 add_include( source
, include
, (quote
== '>') );
741 /*******************************************************************
742 * parse_pragma_directive
744 static void parse_pragma_directive( struct incl_file
*source
, char *str
)
746 char *flag
, *p
= str
;
748 if (!isspace( *p
)) return;
749 while (*p
&& isspace(*p
)) p
++;
750 p
= strtok( p
, " \t" );
751 if (strcmp( p
, "makedep" )) return;
753 while ((flag
= strtok( NULL
, " \t" )))
755 if (!strcmp( flag
, "depend" ))
757 while ((p
= strtok( NULL
, " \t" ))) add_include( source
, p
, 0 );
760 if (strendswith( source
->name
, ".idl" ))
762 if (!strcmp( flag
, "header" )) source
->flags
|= FLAG_IDL_HEADER
;
763 else if (!strcmp( flag
, "proxy" )) source
->flags
|= FLAG_IDL_PROXY
;
764 else if (!strcmp( flag
, "client" )) source
->flags
|= FLAG_IDL_CLIENT
;
765 else if (!strcmp( flag
, "server" )) source
->flags
|= FLAG_IDL_SERVER
;
766 else if (!strcmp( flag
, "ident" )) source
->flags
|= FLAG_IDL_IDENT
;
767 else if (!strcmp( flag
, "typelib" )) source
->flags
|= FLAG_IDL_TYPELIB
;
768 else if (!strcmp( flag
, "register" )) source
->flags
|= FLAG_IDL_REGISTER
;
770 else if (strendswith( source
->name
, ".rc" ))
772 if (!strcmp( flag
, "po" )) source
->flags
|= FLAG_RC_PO
;
774 else if (!strcmp( flag
, "implib" )) source
->flags
|= FLAG_C_IMPLIB
;
779 /*******************************************************************
780 * parse_cpp_directive
782 static void parse_cpp_directive( struct incl_file
*source
, char *str
)
784 while (*str
&& isspace(*str
)) str
++;
785 if (*str
++ != '#') return;
786 while (*str
&& isspace(*str
)) str
++;
788 if (!strncmp( str
, "include", 7 ))
789 parse_include_directive( source
, str
+ 7 );
790 else if (!strncmp( str
, "import", 6 ) && strendswith( source
->name
, ".m" ))
791 parse_include_directive( source
, str
+ 6 );
792 else if (!strncmp( str
, "pragma", 6 ))
793 parse_pragma_directive( source
, str
+ 6 );
797 /*******************************************************************
800 * If for_h_file is non-zero, it means we are not interested in the idl file
801 * itself, but only in the contents of the .h file that will be generated from it.
803 static void parse_idl_file( struct incl_file
*pFile
, FILE *file
, int for_h_file
)
805 char *buffer
, *include
;
810 /* generated .h file always includes these */
811 add_include( pFile
, "rpc.h", 1 );
812 add_include( pFile
, "rpcndr.h", 1 );
815 while ((buffer
= get_line( file
)))
819 while (*p
&& isspace(*p
)) p
++;
821 if (!strncmp( p
, "import", 6 ))
824 while (*p
&& isspace(*p
)) p
++;
825 if (*p
!= '"') continue;
827 while (*p
&& (*p
!= '"')) p
++;
828 if (!*p
) fatal_error( "malformed import directive\n" );
830 if (for_h_file
&& strendswith( include
, ".idl" )) strcpy( p
- 4, ".h" );
831 add_include( pFile
, include
, 0 );
835 if (for_h_file
) /* only check for #include inside cpp_quote */
837 if (strncmp( p
, "cpp_quote", 9 )) continue;
839 while (*p
&& isspace(*p
)) p
++;
840 if (*p
++ != '(') continue;
841 while (*p
&& isspace(*p
)) p
++;
842 if (*p
++ != '"') continue;
843 if (*p
++ != '#') continue;
844 while (*p
&& isspace(*p
)) p
++;
845 if (strncmp( p
, "include", 7 )) continue;
847 while (*p
&& isspace(*p
)) p
++;
848 if (*p
== '\\' && p
[1] == '"')
855 if (*p
++ != '<' ) continue;
859 while (*p
&& (*p
!= quote
)) p
++;
860 if (!*p
|| (quote
== '"' && p
[-1] != '\\'))
861 fatal_error( "malformed #include directive inside cpp_quote\n" );
862 if (quote
== '"') p
--; /* remove backslash */
864 add_include( pFile
, include
, (quote
== '>') );
868 parse_cpp_directive( pFile
, p
);
872 /*******************************************************************
875 static void parse_c_file( struct incl_file
*pFile
, FILE *file
)
880 while ((buffer
= get_line( file
)))
882 parse_cpp_directive( pFile
, buffer
);
887 /*******************************************************************
890 static void parse_rc_file( struct incl_file
*pFile
, FILE *file
)
892 char *buffer
, *include
;
895 while ((buffer
= get_line( file
)))
899 while (*p
&& isspace(*p
)) p
++;
901 if (p
[0] == '/' && p
[1] == '*') /* check for magic makedep comment */
904 while (*p
&& isspace(*p
)) p
++;
905 if (strncmp( p
, "@makedep:", 9 )) continue;
907 while (*p
&& isspace(*p
)) p
++;
912 while (*p
&& *p
!= quote
) p
++;
917 while (*p
&& !isspace(*p
) && *p
!= '*') p
++;
920 fatal_error( "malformed makedep comment\n" );
922 add_include( pFile
, include
, (quote
== '>') );
926 parse_cpp_directive( pFile
, buffer
);
931 /*******************************************************************
932 * parse_generated_idl
934 static void parse_generated_idl( struct incl_file
*source
)
936 char *header
= replace_extension( source
->name
, 4, ".h" );
938 source
->filename
= xstrdup( source
->name
);
940 if (strendswith( source
->name
, "_c.c" ))
942 add_include( source
, header
, 0 );
944 else if (strendswith( source
->name
, "_i.c" ))
946 add_include( source
, "rpc.h", 1 );
947 add_include( source
, "rpcndr.h", 1 );
948 add_include( source
, "guiddef.h", 1 );
950 else if (strendswith( source
->name
, "_p.c" ))
952 add_include( source
, "objbase.h", 1 );
953 add_include( source
, "rpcproxy.h", 1 );
954 add_include( source
, "wine/exception.h", 1 );
955 add_include( source
, header
, 0 );
957 else if (strendswith( source
->name
, "_s.c" ))
959 add_include( source
, "wine/exception.h", 1 );
960 add_include( source
, header
, 0 );
966 /*******************************************************************
969 static int is_generated_idl( struct incl_file
*source
)
971 return (strendswith( source
->name
, "_c.c" ) ||
972 strendswith( source
->name
, "_i.c" ) ||
973 strendswith( source
->name
, "_p.c" ) ||
974 strendswith( source
->name
, "_s.c" ));
977 /*******************************************************************
980 static void parse_file( struct incl_file
*source
, int src
)
984 /* don't try to open certain types of files */
985 if (strendswith( source
->name
, ".tlb" ) ||
986 strendswith( source
->name
, ".x" ))
988 source
->filename
= xstrdup( source
->name
);
992 file
= src
? open_src_file( source
) : open_include_file( source
);
994 input_file_name
= source
->filename
;
996 if (source
->sourcename
&& strendswith( source
->sourcename
, ".idl" ))
997 parse_idl_file( source
, file
, 1 );
998 else if (strendswith( source
->filename
, ".idl" ))
999 parse_idl_file( source
, file
, 0 );
1000 else if (strendswith( source
->filename
, ".c" ) ||
1001 strendswith( source
->filename
, ".m" ) ||
1002 strendswith( source
->filename
, ".h" ) ||
1003 strendswith( source
->filename
, ".l" ) ||
1004 strendswith( source
->filename
, ".y" ))
1005 parse_c_file( source
, file
);
1006 else if (strendswith( source
->filename
, ".rc" ))
1007 parse_rc_file( source
, file
);
1009 input_file_name
= NULL
;
1013 /*******************************************************************
1016 * Add a source file to the list.
1018 static struct incl_file
*add_src_file( const char *name
)
1020 struct incl_file
*file
;
1022 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1023 file
= xmalloc( sizeof(*file
) );
1024 memset( file
, 0, sizeof(*file
) );
1025 file
->name
= xstrdup(name
);
1026 list_add_tail( &sources
, &file
->entry
);
1028 /* special cases for generated files */
1030 if (is_generated_idl( file
))
1032 parse_generated_idl( file
);
1036 if (!strcmp( file
->name
, "dlldata.o" ))
1038 file
->filename
= xstrdup( "dlldata.c" );
1039 add_include( file
, "objbase.h", 1 );
1040 add_include( file
, "rpcproxy.h", 1 );
1044 if (!strcmp( file
->name
, "testlist.o" ))
1046 file
->filename
= xstrdup( "testlist.c" );
1047 add_include( file
, "wine/test.h", 1 );
1051 if (strendswith( file
->name
, ".o" ))
1053 /* default to .c for unknown extra object files */
1054 file
->filename
= replace_extension( file
->name
, 2, ".c" );
1058 if (strendswith( file
->name
, ".tlb" ) ||
1059 strendswith( file
->name
, ".res" ) ||
1060 strendswith( file
->name
, ".pot" ) ||
1061 strendswith( file
->name
, ".x" ))
1063 file
->filename
= xstrdup( file
->name
);
1067 parse_file( file
, 1 );
1072 /*******************************************************************
1073 * add_generated_sources
1075 static void add_generated_sources(void)
1078 struct incl_file
*source
, *next
;
1080 LIST_FOR_EACH_ENTRY_SAFE( source
, next
, &sources
, struct incl_file
, entry
)
1082 if (!source
->flags
) continue;
1083 for (i
= 0; i
< sizeof(idl_outputs
) / sizeof(idl_outputs
[0]); i
++)
1085 if (!(source
->flags
& idl_outputs
[i
].flag
)) continue;
1086 if (!strendswith( idl_outputs
[i
].ext
, ".c" )) continue;
1087 add_src_file( replace_extension( source
->name
, 4, idl_outputs
[i
].ext
));
1089 if (source
->flags
& FLAG_IDL_PROXY
) add_src_file( "dlldata.o" );
1092 LIST_FOR_EACH_ENTRY_SAFE( source
, next
, &sources
, struct incl_file
, entry
)
1094 if (strendswith( source
->name
, "_c.c" ) ||
1095 strendswith( source
->name
, "_i.c" ) ||
1096 strendswith( source
->name
, "_p.c" ) ||
1097 strendswith( source
->name
, "_s.c" ) ||
1098 strendswith( source
->name
, ".tlb" ))
1100 char *idl
= replace_extension( source
->name
, 4, ".idl" );
1101 struct incl_file
*file
= add_src_file( idl
);
1102 if (strendswith( source
->name
, "_c.c" )) file
->flags
|= FLAG_IDL_CLIENT
;
1103 else if (strendswith( source
->name
, "_i.c" )) file
->flags
|= FLAG_IDL_IDENT
;
1104 else if (strendswith( source
->name
, "_p.c" )) file
->flags
|= FLAG_IDL_PROXY
;
1105 else if (strendswith( source
->name
, "_s.c" )) file
->flags
|= FLAG_IDL_SERVER
;
1106 else if (strendswith( source
->name
, ".tlb" )) file
->flags
|= FLAG_IDL_TYPELIB
;
1109 if (strendswith( source
->name
, "_r.res" ) ||
1110 strendswith( source
->name
, "_t.res" ))
1112 char *idl
= replace_extension( source
->name
, 6, ".idl" );
1113 struct incl_file
*file
= add_src_file( idl
);
1114 if (strendswith( source
->name
, "_r.res" )) file
->flags
|= FLAG_IDL_REGISTER
;
1115 else if (strendswith( source
->name
, "_t.res" )) file
->flags
|= FLAG_IDL_TYPELIB
;
1118 if (strendswith( source
->name
, ".pot" ))
1120 char *rc
= replace_extension( source
->name
, 4, ".rc" );
1121 struct incl_file
*file
= add_src_file( rc
);
1122 file
->flags
|= FLAG_RC_PO
;
1128 /*******************************************************************
1131 static void output_include( struct incl_file
*pFile
, struct incl_file
*owner
, int *column
)
1135 if (pFile
->owner
== owner
) return;
1136 if (!pFile
->filename
) return;
1137 pFile
->owner
= owner
;
1138 output_filename( pFile
->filename
, column
);
1139 for (i
= 0; i
< MAX_INCLUDES
; i
++)
1140 if (pFile
->files
[i
]) output_include( pFile
->files
[i
], owner
, column
);
1144 /*******************************************************************
1147 static void output_sources(void)
1149 struct incl_file
*source
;
1150 struct strarray clean_files
;
1151 int i
, column
, po_srcs
= 0, mc_srcs
= 0;
1153 strarray_init( &clean_files
);
1155 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1157 char *obj
= xstrdup( source
->name
);
1158 char *ext
= get_extension( obj
);
1160 if (!ext
) fatal_error( "unsupported file type %s\n", source
->name
);
1164 if (!strcmp( ext
, "y" )) /* yacc file */
1166 /* add source file dependency for parallel makes */
1167 char *header
= strmake( "%s.tab.h", obj
);
1169 if (find_include_file( header
))
1171 output( "%s.tab.h: %s\n", obj
, source
->filename
);
1172 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
1173 obj
, obj
, source
->filename
);
1174 output( "%s.tab.c: %s %s\n", obj
, source
->filename
, header
);
1175 strarray_add( &clean_files
, strmake( "%s.tab.h", obj
));
1177 else output( "%s.tab.c: %s\n", obj
, source
->filename
);
1179 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj
, source
->filename
);
1180 output( "%s.tab.o: %s.tab.c\n", obj
, obj
);
1181 output( "\t$(CC) -c $(ALLCFLAGS) -o $@ %s.tab.c\n", obj
);
1182 strarray_add( &clean_files
, strmake( "%s.tab.c", obj
));
1183 strarray_add( &clean_files
, strmake( "%s.tab.o", obj
));
1184 column
+= output( "%s.tab.o:", obj
);
1187 else if (!strcmp( ext
, "l" )) /* lex file */
1189 output( "%s.yy.c: %s\n", obj
, source
->filename
);
1190 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source
->filename
);
1191 output( "%s.yy.o: %s.yy.c\n", obj
, obj
);
1192 output( "\t$(CC) -c $(ALLCFLAGS) -o $@ %s.yy.c\n", obj
);
1193 strarray_add( &clean_files
, strmake( "%s.yy.c", obj
));
1194 strarray_add( &clean_files
, strmake( "%s.yy.o", obj
));
1195 column
+= output( "%s.yy.o:", obj
);
1197 else if (!strcmp( ext
, "rc" )) /* resource file */
1199 if (source
->flags
& FLAG_RC_PO
)
1201 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj
, source
->filename
);
1202 output( "\t$(WRC) $(RCFLAGS) -o $@ %s\n", source
->filename
);
1203 column
+= output( "%s.res rsrc.pot:", obj
);
1208 output( "%s.res: $(WRC) %s\n", obj
, source
->filename
);
1209 output( "\t$(WRC) $(RCFLAGS) -o $@ %s\n", source
->filename
);
1210 column
+= output( "%s.res:", obj
);
1212 strarray_add( &clean_files
, strmake( "%s.res", obj
));
1214 else if (!strcmp( ext
, "mc" )) /* message file */
1216 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj
, source
->filename
);
1217 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source
->filename
);
1218 strarray_add( &clean_files
, strmake( "%s.res", obj
));
1220 column
+= output( "msg.pot %s.res:", obj
);
1222 else if (!strcmp( ext
, "idl" )) /* IDL file */
1225 unsigned int i
, nb_targets
= 0;
1227 if (!source
->flags
|| find_include_file( strmake( "%s.h", obj
)))
1228 source
->flags
|= FLAG_IDL_HEADER
;
1230 for (i
= 0; i
< sizeof(idl_outputs
) / sizeof(idl_outputs
[0]); i
++)
1232 if (!(source
->flags
& idl_outputs
[i
].flag
)) continue;
1233 output( "%s%s: $(WIDL)\n", obj
, idl_outputs
[i
].ext
);
1234 output( "\t$(WIDL) %s -o $@ %s\n", idl_outputs
[i
].widl_arg
, source
->filename
);
1235 targets
[nb_targets
++] = strmake( "%s%s", obj
, idl_outputs
[i
].ext
);
1237 for (i
= 0; i
< nb_targets
; i
++)
1239 column
+= output( "%s%c", targets
[i
], i
< nb_targets
- 1 ? ' ' : ':' );
1240 strarray_add( &clean_files
, targets
[i
] );
1242 column
+= output( " %s", source
->filename
);
1244 else if (!strcmp( ext
, "tlb" ) || !strcmp( ext
, "res" ) || !strcmp( ext
, "pot" ))
1246 continue; /* nothing to do for typelib files */
1250 struct object_extension
*ext
;
1251 LIST_FOR_EACH_ENTRY( ext
, &object_extensions
, struct object_extension
, entry
)
1253 strarray_add( &clean_files
, strmake( "%s.%s", obj
, ext
->extension
));
1254 output( "%s.%s: %s\n", obj
, ext
->extension
, source
->filename
);
1255 if (strstr( ext
->extension
, "cross" ))
1256 output( "\t$(CROSSCC) -c $(ALLCROSSCFLAGS) -o $@ %s\n", source
->filename
);
1258 output( "\t$(CC) -c $(ALLCFLAGS) -o $@ %s\n", source
->filename
);
1260 if (source
->flags
& FLAG_C_IMPLIB
)
1262 strarray_add( &clean_files
, strmake( "%s.cross.o", obj
));
1263 output( "%s.cross.o: %s\n", obj
, source
->filename
);
1264 output( "\t$(CROSSCC) -c $(ALLCROSSCFLAGS) -o $@ %s\n", source
->filename
);
1266 LIST_FOR_EACH_ENTRY( ext
, &object_extensions
, struct object_extension
, entry
)
1267 column
+= output( "%s.%s ", obj
, ext
->extension
);
1268 if (source
->flags
& FLAG_C_IMPLIB
) column
+= output( "%s.cross.o", obj
);
1269 column
+= output( ":" );
1273 for (i
= 0; i
< MAX_INCLUDES
; i
++)
1274 if (source
->files
[i
]) output_include( source
->files
[i
], source
, &column
);
1278 /* rules for files that depend on multiple sources */
1282 column
= output( "rsrc.pot: $(WRC)" );
1283 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1284 if (source
->flags
& FLAG_RC_PO
) output_filename( source
->filename
, &column
);
1286 column
= output( "\t$(WRC) $(RCFLAGS) -O pot -o $@" );
1287 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1288 if (source
->flags
& FLAG_RC_PO
) output_filename( source
->filename
, &column
);
1290 strarray_add( &clean_files
, "rsrc.pot" );
1295 column
= output( "msg.pot: $(WMC)" );
1296 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1297 if (strendswith( source
->name
, ".mc" )) output_filename( source
->filename
, &column
);
1299 column
= output( "\t$(WMC) -O pot -o $@" );
1300 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1301 if (strendswith( source
->name
, ".mc" )) output_filename( source
->filename
, &column
);
1303 strarray_add( &clean_files
, "msg.pot" );
1306 if (find_src_file( "dlldata.o" ))
1308 output( "dlldata.c: $(WIDL) Makefile.in\n" );
1309 column
= output( "\t$(WIDL) $(IDLFLAGS) --dlldata-only -o $@" );
1310 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1311 if (source
->flags
& FLAG_IDL_PROXY
) output_filename( source
->filename
, &column
);
1313 strarray_add( &clean_files
, "dlldata.c" );
1316 if (find_src_file( "testlist.o" ))
1318 output( "testlist.c: $(MAKECTESTS) Makefile.in\n" );
1319 column
= output( "\t$(MAKECTESTS) -o $@" );
1320 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1321 if (strendswith( source
->name
, ".c" ) && !is_generated_idl( source
))
1322 output_filename( source
->filename
, &column
);
1324 strarray_add( &clean_files
, "testlist.c" );
1327 if (clean_files
.count
)
1329 output( "clean::\n" );
1330 column
= output( "\t$(RM)" );
1331 for (i
= 0; i
< clean_files
.count
; i
++) output_filename( clean_files
.str
[i
], &column
);
1337 /*******************************************************************
1340 static FILE *create_temp_file( char **tmp_name
)
1342 char *name
= xmalloc( strlen(OutputFileName
) + 13 );
1343 unsigned int i
, id
= getpid();
1347 for (i
= 0; i
< 100; i
++)
1349 sprintf( name
, "%s.tmp%08x", OutputFileName
, id
);
1350 if ((fd
= open( name
, O_RDWR
| O_CREAT
| O_EXCL
, 0666 )) != -1)
1352 ret
= fdopen( fd
, "w" );
1355 if (errno
!= EEXIST
) break;
1358 if (!ret
) fatal_error( "failed to create output file for '%s'\n", OutputFileName
);
1364 /*******************************************************************
1365 * output_dependencies
1367 static void output_dependencies(void)
1369 char *tmp_name
= NULL
;
1371 if (Separator
&& ((output_file
= fopen( OutputFileName
, "r" ))))
1374 FILE *tmp_file
= create_temp_file( &tmp_name
);
1377 while (fgets( buffer
, sizeof(buffer
), output_file
) && !found
)
1379 if (fwrite( buffer
, 1, strlen(buffer
), tmp_file
) != strlen(buffer
))
1380 fatal_error( "failed to write to %s\n", tmp_name
);
1381 found
= !strncmp( buffer
, Separator
, strlen(Separator
) );
1383 fclose( output_file
);
1384 output_file
= tmp_file
;
1385 if (!found
&& list_head(&sources
)) output( "\n%s\n", Separator
);
1389 if (!(output_file
= fopen( OutputFileName
, Separator
? "a" : "w" )))
1390 fatal_perror( "%s", OutputFileName
);
1395 fclose( output_file
);
1400 int ret
= rename( tmp_name
, OutputFileName
);
1401 if (ret
== -1 && errno
== EEXIST
)
1403 /* rename doesn't overwrite on windows */
1404 unlink( OutputFileName
);
1405 ret
= rename( tmp_name
, OutputFileName
);
1410 fatal_error( "failed to rename output file to '%s'\n", OutputFileName
);
1417 /*******************************************************************
1420 static void parse_option( const char *opt
)
1425 if (opt
[2]) add_include_path( opt
+ 2 );
1431 top_src_dir
= opt
+ 2;
1434 top_obj_dir
= opt
+ 2;
1437 parent_dir
= opt
+ 2;
1440 if (opt
[2]) OutputFileName
= opt
+ 2;
1443 relative_dir_mode
= 1;
1446 if (opt
[2]) Separator
= opt
+ 2;
1447 else Separator
= NULL
;
1450 if (opt
[2]) add_object_extension( opt
+ 2 );
1453 fprintf( stderr
, "Unknown option '%s'\n%s", opt
, Usage
);
1459 /*******************************************************************
1462 int main( int argc
, char *argv
[] )
1464 struct incl_file
*pFile
;
1465 struct incl_path
*path
, *next
;
1471 if (argv
[i
][0] == '-')
1473 parse_option( argv
[i
] );
1474 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
1480 if (relative_dir_mode
)
1486 fprintf( stderr
, "Option -r needs two directories\n%s", Usage
);
1489 relpath
= get_relative_path( argv
[1], argv
[2] );
1490 printf( "%s\n", relpath
? relpath
: "." );
1494 /* ignore redundant source paths */
1495 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
1496 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
1498 /* set the default extension list for object files */
1499 if (list_empty( &object_extensions
))
1500 add_object_extension( "o" );
1502 /* get rid of absolute paths that don't point into the source dir */
1503 LIST_FOR_EACH_ENTRY_SAFE( path
, next
, &paths
, struct incl_path
, entry
)
1505 if (path
->name
[0] != '/') continue;
1508 if (!strncmp( path
->name
, top_src_dir
, strlen(top_src_dir
) ) &&
1509 path
->name
[strlen(top_src_dir
)] == '/') continue;
1511 list_remove( &path
->entry
);
1515 for (i
= 1; i
< argc
; i
++) add_src_file( argv
[i
] );
1516 add_generated_sources();
1518 LIST_FOR_EACH_ENTRY( pFile
, &includes
, struct incl_file
, entry
) parse_file( pFile
, 0 );
1519 output_dependencies();