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
);
86 unsigned int count
; /* strings in use */
87 unsigned int size
; /* total allocated size */
91 static struct strarray include_args
;
92 static struct strarray object_extensions
;
94 static const char *src_dir
;
95 static const char *top_src_dir
;
96 static const char *top_obj_dir
;
97 static const char *parent_dir
;
98 static const char *OutputFileName
= "Makefile";
99 static const char *Separator
= "### Dependencies";
100 static const char *input_file_name
;
101 static int relative_dir_mode
;
102 static int input_line
;
103 static FILE *output_file
;
105 static const char Usage
[] =
106 "Usage: makedep [options] [files]\n"
108 " -Idir Search for include files in directory 'dir'\n"
109 " -Cdir Search for source files in directory 'dir'\n"
110 " -Sdir Set the top source directory\n"
111 " -Tdir Set the top object directory\n"
112 " -Pdir Set the parent source directory\n"
113 " -R from to Compute the relative path between two directories\n"
114 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
115 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
119 #define __attribute__(x)
122 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
123 static void fatal_perror( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
124 static int output( const char *format
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
125 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
127 /*******************************************************************
130 static void fatal_error( const char *msg
, ... )
133 va_start( valist
, msg
);
136 fprintf( stderr
, "%s:", input_file_name
);
137 if (input_line
) fprintf( stderr
, "%d:", input_line
);
138 fprintf( stderr
, " error: " );
140 else fprintf( stderr
, "makedep: error: " );
141 vfprintf( stderr
, msg
, valist
);
147 /*******************************************************************
150 static void fatal_perror( const char *msg
, ... )
153 va_start( valist
, msg
);
156 fprintf( stderr
, "%s:", input_file_name
);
157 if (input_line
) fprintf( stderr
, "%d:", input_line
);
158 fprintf( stderr
, " error: " );
160 else fprintf( stderr
, "makedep: error: " );
161 vfprintf( stderr
, msg
, valist
);
168 /*******************************************************************
171 static void *xmalloc( size_t size
)
174 if (!(res
= malloc (size
? size
: 1)))
175 fatal_error( "Virtual memory exhausted.\n" );
180 /*******************************************************************
183 static void *xrealloc (void *ptr
, size_t size
)
187 if (!(res
= realloc( ptr
, size
)))
188 fatal_error( "Virtual memory exhausted.\n" );
192 /*******************************************************************
195 static char *xstrdup( const char *str
)
197 char *res
= strdup( str
);
198 if (!res
) fatal_error( "Virtual memory exhausted.\n" );
203 /*******************************************************************
206 static char *strmake( const char* fmt
, ... )
214 char *p
= xmalloc (size
);
216 n
= vsnprintf (p
, size
, fmt
, ap
);
218 if (n
== -1) size
*= 2;
219 else if ((size_t)n
>= size
) size
= n
+ 1;
226 /*******************************************************************
229 static int strendswith( const char* str
, const char* end
)
234 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
238 /*******************************************************************
241 static int output( const char *format
, ... )
246 va_start( valist
, format
);
247 ret
= vfprintf( output_file
, format
, valist
);
249 if (ret
< 0) fatal_perror( "output" );
254 /*******************************************************************
257 static void strarray_init( struct strarray
*array
)
265 /*******************************************************************
268 static void strarray_add( struct strarray
*array
, const char *str
)
270 if (array
->count
== array
->size
)
272 if (array
->size
) array
->size
*= 2;
273 else array
->size
= 16;
274 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
276 array
->str
[array
->count
++] = str
;
280 /*******************************************************************
283 static void strarray_insert( struct strarray
*array
, unsigned int pos
, const char *str
)
287 strarray_add( array
, NULL
);
288 for (i
= array
->count
- 1; i
> pos
; i
--) array
->str
[i
] = array
->str
[i
- 1];
289 array
->str
[pos
] = 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 /*******************************************************************
423 static struct incl_file
*find_src_file( const char *name
)
425 struct incl_file
*file
;
427 LIST_FOR_EACH_ENTRY( file
, &sources
, struct incl_file
, entry
)
428 if (!strcmp( name
, file
->name
)) return file
;
432 /*******************************************************************
435 static struct incl_file
*find_include_file( const char *name
)
437 struct incl_file
*file
;
439 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
)
440 if (!strcmp( name
, file
->name
)) return file
;
444 /*******************************************************************
447 * Add an include file if it doesn't already exists.
449 static struct incl_file
*add_include( struct incl_file
*pFile
, const char *name
, int system
)
451 struct incl_file
*include
;
455 for (pos
= 0; pos
< MAX_INCLUDES
; pos
++) if (!pFile
->files
[pos
]) break;
456 if (pos
>= MAX_INCLUDES
)
457 fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
459 /* enforce some rules for the Wine tree */
461 if (!memcmp( name
, "../", 3 ))
462 fatal_error( "#include directive with relative path not allowed\n" );
464 if (!strcmp( name
, "config.h" ))
466 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
467 fatal_error( "config.h must not be included by a header file\n" );
469 fatal_error( "config.h must be included before anything else\n" );
471 else if (!strcmp( name
, "wine/port.h" ))
473 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
474 fatal_error( "wine/port.h must not be included by a header file\n" );
475 if (!pos
) fatal_error( "config.h must be included before wine/port.h\n" );
477 fatal_error( "wine/port.h must be included before everything except config.h\n" );
478 if (strcmp( pFile
->files
[0]->name
, "config.h" ))
479 fatal_error( "config.h must be included before wine/port.h\n" );
482 LIST_FOR_EACH_ENTRY( include
, &includes
, struct incl_file
, entry
)
483 if (!strcmp( name
, include
->name
)) goto found
;
485 include
= xmalloc( sizeof(*include
) );
486 memset( include
, 0, sizeof(*include
) );
487 include
->name
= xstrdup(name
);
488 include
->included_by
= pFile
;
489 include
->included_line
= input_line
;
490 if (system
) include
->flags
|= FLAG_SYSTEM
;
491 list_add_tail( &includes
, &include
->entry
);
493 pFile
->files
[pos
] = include
;
498 /*******************************************************************
501 static FILE *open_src_file( struct incl_file
*pFile
)
505 /* first try name as is */
506 if ((file
= fopen( pFile
->name
, "r" )))
508 pFile
->filename
= xstrdup( pFile
->name
);
511 /* now try in source dir */
514 pFile
->filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
515 file
= fopen( pFile
->filename
, "r" );
517 /* now try parent dir */
518 if (!file
&& parent_dir
)
521 pFile
->filename
= strmake( "%s/%s/%s", src_dir
, parent_dir
, pFile
->name
);
523 pFile
->filename
= strmake( "%s/%s", parent_dir
, pFile
->name
);
524 if ((file
= fopen( pFile
->filename
, "r" ))) return file
;
525 file
= fopen( pFile
->filename
, "r" );
527 if (!file
) fatal_perror( "open %s", pFile
->name
);
532 /*******************************************************************
535 static FILE *open_include_file( struct incl_file
*pFile
)
543 /* check for generated bison header */
545 if (strendswith( pFile
->name
, ".tab.h" ))
547 filename
= replace_extension( pFile
->name
, 6, ".y" );
548 if (src_dir
) filename
= strmake( "%s/%s", src_dir
, filename
);
550 if ((file
= fopen( filename
, "r" )))
552 pFile
->sourcename
= filename
;
553 pFile
->filename
= xstrdup( pFile
->name
);
554 /* don't bother to parse it */
561 /* check for corresponding idl file in source dir */
563 if (strendswith( pFile
->name
, ".h" ))
565 filename
= replace_extension( pFile
->name
, 2, ".idl" );
566 if (src_dir
) filename
= strmake( "%s/%s", src_dir
, filename
);
568 if ((file
= fopen( filename
, "r" )))
570 pFile
->sourcename
= filename
;
571 pFile
->filename
= xstrdup( pFile
->name
);
577 /* now try in source dir */
579 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
581 filename
= xstrdup( pFile
->name
);
582 if ((file
= fopen( filename
, "r" ))) goto found
;
585 /* now try in parent source dir */
589 filename
= strmake( "%s/%s/%s", src_dir
, parent_dir
, pFile
->name
);
591 filename
= strmake( "%s/%s", parent_dir
, pFile
->name
);
592 if ((file
= fopen( filename
, "r" ))) goto found
;
596 /* check for corresponding idl file in global includes */
598 if (strendswith( pFile
->name
, ".h" ))
600 filename
= replace_extension( pFile
->name
, 2, ".idl" );
602 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
603 else if (top_obj_dir
)
604 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
608 if (filename
&& (file
= fopen( filename
, "r" )))
610 pFile
->sourcename
= filename
;
611 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
617 /* check for corresponding .in file in global includes (for config.h.in) */
619 if (strendswith( pFile
->name
, ".h" ))
621 filename
= replace_extension( pFile
->name
, 2, ".h.in" );
623 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
624 else if (top_obj_dir
)
625 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
629 if (filename
&& (file
= fopen( filename
, "r" )))
631 pFile
->sourcename
= filename
;
632 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
638 /* check for corresponding .x file in global includes */
640 if (strendswith( pFile
->name
, "tmpl.h" ))
642 filename
= replace_extension( pFile
->name
, 2, ".x" );
644 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
645 else if (top_obj_dir
)
646 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
650 if (filename
&& (file
= fopen( filename
, "r" )))
652 pFile
->sourcename
= filename
;
653 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
659 /* now search in include paths */
660 for (i
= 0; i
< include_args
.count
; i
++)
662 const char *dir
= include_args
.str
[i
] + 2; /* skip -I */
665 /* ignore absolute paths that don't point into the source dir */
666 if (!top_src_dir
) continue;
667 len
= strlen( top_src_dir
);
668 if (strncmp( dir
, top_src_dir
, len
)) continue;
669 if (dir
[len
] && dir
[len
] != '/') continue;
671 filename
= strmake( "%s/%s", dir
, pFile
->name
);
672 if ((file
= fopen( filename
, "r" ))) goto found
;
675 if (pFile
->flags
& FLAG_SYSTEM
) return NULL
; /* ignore system files we cannot find */
677 /* try in src file directory */
678 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
680 int l
= p
- pFile
->included_by
->filename
+ 1;
681 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
682 memcpy( filename
, pFile
->included_by
->filename
, l
);
683 strcpy( filename
+ l
, pFile
->name
);
684 if ((file
= fopen( filename
, "r" ))) goto found
;
688 fprintf( stderr
, "%s:%d: error: ", pFile
->included_by
->filename
, pFile
->included_line
);
689 perror( pFile
->name
);
690 pFile
= pFile
->included_by
;
691 while (pFile
&& pFile
->included_by
)
693 const char *parent
= pFile
->included_by
->sourcename
;
694 if (!parent
) parent
= pFile
->included_by
->name
;
695 fprintf( stderr
, "%s:%d: note: %s was first included here\n",
696 parent
, pFile
->included_line
, pFile
->name
);
697 pFile
= pFile
->included_by
;
702 pFile
->filename
= filename
;
707 /*******************************************************************
708 * parse_include_directive
710 static void parse_include_directive( struct incl_file
*source
, char *str
)
712 char quote
, *include
, *p
= str
;
714 while (*p
&& isspace(*p
)) p
++;
715 if (*p
!= '\"' && *p
!= '<' ) return;
717 if (quote
== '<') quote
= '>';
719 while (*p
&& (*p
!= quote
)) p
++;
720 if (!*p
) fatal_error( "malformed include directive '%s'\n", str
);
722 add_include( source
, include
, (quote
== '>') );
726 /*******************************************************************
727 * parse_pragma_directive
729 static void parse_pragma_directive( struct incl_file
*source
, char *str
)
731 char *flag
, *p
= str
;
733 if (!isspace( *p
)) return;
734 while (*p
&& isspace(*p
)) p
++;
735 p
= strtok( p
, " \t" );
736 if (strcmp( p
, "makedep" )) return;
738 while ((flag
= strtok( NULL
, " \t" )))
740 if (!strcmp( flag
, "depend" ))
742 while ((p
= strtok( NULL
, " \t" ))) add_include( source
, p
, 0 );
745 if (strendswith( source
->name
, ".idl" ))
747 if (!strcmp( flag
, "header" )) source
->flags
|= FLAG_IDL_HEADER
;
748 else if (!strcmp( flag
, "proxy" )) source
->flags
|= FLAG_IDL_PROXY
;
749 else if (!strcmp( flag
, "client" )) source
->flags
|= FLAG_IDL_CLIENT
;
750 else if (!strcmp( flag
, "server" )) source
->flags
|= FLAG_IDL_SERVER
;
751 else if (!strcmp( flag
, "ident" )) source
->flags
|= FLAG_IDL_IDENT
;
752 else if (!strcmp( flag
, "typelib" )) source
->flags
|= FLAG_IDL_TYPELIB
;
753 else if (!strcmp( flag
, "register" )) source
->flags
|= FLAG_IDL_REGISTER
;
755 else if (strendswith( source
->name
, ".rc" ))
757 if (!strcmp( flag
, "po" )) source
->flags
|= FLAG_RC_PO
;
759 else if (!strcmp( flag
, "implib" )) source
->flags
|= FLAG_C_IMPLIB
;
764 /*******************************************************************
765 * parse_cpp_directive
767 static void parse_cpp_directive( struct incl_file
*source
, char *str
)
769 while (*str
&& isspace(*str
)) str
++;
770 if (*str
++ != '#') return;
771 while (*str
&& isspace(*str
)) str
++;
773 if (!strncmp( str
, "include", 7 ))
774 parse_include_directive( source
, str
+ 7 );
775 else if (!strncmp( str
, "import", 6 ) && strendswith( source
->name
, ".m" ))
776 parse_include_directive( source
, str
+ 6 );
777 else if (!strncmp( str
, "pragma", 6 ))
778 parse_pragma_directive( source
, str
+ 6 );
782 /*******************************************************************
785 * If for_h_file is non-zero, it means we are not interested in the idl file
786 * itself, but only in the contents of the .h file that will be generated from it.
788 static void parse_idl_file( struct incl_file
*pFile
, FILE *file
, int for_h_file
)
790 char *buffer
, *include
;
795 /* generated .h file always includes these */
796 add_include( pFile
, "rpc.h", 1 );
797 add_include( pFile
, "rpcndr.h", 1 );
800 while ((buffer
= get_line( file
)))
804 while (*p
&& isspace(*p
)) p
++;
806 if (!strncmp( p
, "import", 6 ))
809 while (*p
&& isspace(*p
)) p
++;
810 if (*p
!= '"') continue;
812 while (*p
&& (*p
!= '"')) p
++;
813 if (!*p
) fatal_error( "malformed import directive\n" );
815 if (for_h_file
&& strendswith( include
, ".idl" )) strcpy( p
- 4, ".h" );
816 add_include( pFile
, include
, 0 );
820 if (for_h_file
) /* only check for #include inside cpp_quote */
822 if (strncmp( p
, "cpp_quote", 9 )) continue;
824 while (*p
&& isspace(*p
)) p
++;
825 if (*p
++ != '(') continue;
826 while (*p
&& isspace(*p
)) p
++;
827 if (*p
++ != '"') continue;
828 if (*p
++ != '#') continue;
829 while (*p
&& isspace(*p
)) p
++;
830 if (strncmp( p
, "include", 7 )) continue;
832 while (*p
&& isspace(*p
)) p
++;
833 if (*p
== '\\' && p
[1] == '"')
840 if (*p
++ != '<' ) continue;
844 while (*p
&& (*p
!= quote
)) p
++;
845 if (!*p
|| (quote
== '"' && p
[-1] != '\\'))
846 fatal_error( "malformed #include directive inside cpp_quote\n" );
847 if (quote
== '"') p
--; /* remove backslash */
849 add_include( pFile
, include
, (quote
== '>') );
853 parse_cpp_directive( pFile
, p
);
857 /*******************************************************************
860 static void parse_c_file( struct incl_file
*pFile
, FILE *file
)
865 while ((buffer
= get_line( file
)))
867 parse_cpp_directive( pFile
, buffer
);
872 /*******************************************************************
875 static void parse_rc_file( struct incl_file
*pFile
, FILE *file
)
877 char *buffer
, *include
;
880 while ((buffer
= get_line( file
)))
884 while (*p
&& isspace(*p
)) p
++;
886 if (p
[0] == '/' && p
[1] == '*') /* check for magic makedep comment */
889 while (*p
&& isspace(*p
)) p
++;
890 if (strncmp( p
, "@makedep:", 9 )) continue;
892 while (*p
&& isspace(*p
)) p
++;
897 while (*p
&& *p
!= quote
) p
++;
902 while (*p
&& !isspace(*p
) && *p
!= '*') p
++;
905 fatal_error( "malformed makedep comment\n" );
907 add_include( pFile
, include
, (quote
== '>') );
911 parse_cpp_directive( pFile
, buffer
);
916 /*******************************************************************
919 static void parse_man_page( struct incl_file
*source
, FILE *file
)
923 /* make sure it gets rebuilt when the version changes */
924 add_include( source
, "config.h", 1 );
927 while ((buffer
= get_line( file
)))
929 if (strncmp( buffer
, ".TH", 3 )) continue;
930 if (!(p
= strtok( buffer
, " \t" ))) continue; /* .TH */
931 if (!(p
= strtok( NULL
, " \t" ))) continue; /* program name */
932 if (!(p
= strtok( NULL
, " \t" ))) continue; /* man section */
933 source
->sourcename
= xstrdup( p
); /* abuse source name to store section */
939 /*******************************************************************
940 * parse_generated_idl
942 static void parse_generated_idl( struct incl_file
*source
)
944 char *header
= replace_extension( source
->name
, 4, ".h" );
946 source
->filename
= xstrdup( source
->name
);
948 if (strendswith( source
->name
, "_c.c" ))
950 add_include( source
, header
, 0 );
952 else if (strendswith( source
->name
, "_i.c" ))
954 add_include( source
, "rpc.h", 1 );
955 add_include( source
, "rpcndr.h", 1 );
956 add_include( source
, "guiddef.h", 1 );
958 else if (strendswith( source
->name
, "_p.c" ))
960 add_include( source
, "objbase.h", 1 );
961 add_include( source
, "rpcproxy.h", 1 );
962 add_include( source
, "wine/exception.h", 1 );
963 add_include( source
, header
, 0 );
965 else if (strendswith( source
->name
, "_s.c" ))
967 add_include( source
, "wine/exception.h", 1 );
968 add_include( source
, header
, 0 );
974 /*******************************************************************
977 static int is_generated_idl( struct incl_file
*source
)
979 return (strendswith( source
->name
, "_c.c" ) ||
980 strendswith( source
->name
, "_i.c" ) ||
981 strendswith( source
->name
, "_p.c" ) ||
982 strendswith( source
->name
, "_s.c" ));
985 /*******************************************************************
988 static void parse_file( struct incl_file
*source
, int src
)
992 /* don't try to open certain types of files */
993 if (strendswith( source
->name
, ".tlb" ) ||
994 strendswith( source
->name
, ".x" ))
996 source
->filename
= xstrdup( source
->name
);
1000 file
= src
? open_src_file( source
) : open_include_file( source
);
1002 input_file_name
= source
->filename
;
1004 if (source
->sourcename
&& strendswith( source
->sourcename
, ".idl" ))
1005 parse_idl_file( source
, file
, 1 );
1006 else if (strendswith( source
->filename
, ".idl" ))
1007 parse_idl_file( source
, file
, 0 );
1008 else if (strendswith( source
->filename
, ".c" ) ||
1009 strendswith( source
->filename
, ".m" ) ||
1010 strendswith( source
->filename
, ".h" ) ||
1011 strendswith( source
->filename
, ".l" ) ||
1012 strendswith( source
->filename
, ".y" ))
1013 parse_c_file( source
, file
);
1014 else if (strendswith( source
->filename
, ".rc" ))
1015 parse_rc_file( source
, file
);
1016 else if (strendswith( source
->filename
, ".man.in" ))
1017 parse_man_page( source
, file
);
1019 input_file_name
= NULL
;
1023 /*******************************************************************
1026 * Add a source file to the list.
1028 static struct incl_file
*add_src_file( const char *name
)
1030 struct incl_file
*file
;
1032 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1033 file
= xmalloc( sizeof(*file
) );
1034 memset( file
, 0, sizeof(*file
) );
1035 file
->name
= xstrdup(name
);
1036 list_add_tail( &sources
, &file
->entry
);
1038 /* special cases for generated files */
1040 if (is_generated_idl( file
))
1042 parse_generated_idl( file
);
1046 if (!strcmp( file
->name
, "dlldata.o" ))
1048 file
->filename
= xstrdup( "dlldata.c" );
1049 add_include( file
, "objbase.h", 1 );
1050 add_include( file
, "rpcproxy.h", 1 );
1054 if (!strcmp( file
->name
, "testlist.o" ))
1056 file
->filename
= xstrdup( "testlist.c" );
1057 add_include( file
, "wine/test.h", 1 );
1061 if (strendswith( file
->name
, ".o" ))
1063 /* default to .c for unknown extra object files */
1064 file
->filename
= replace_extension( file
->name
, 2, ".c" );
1068 if (strendswith( file
->name
, ".tlb" ) ||
1069 strendswith( file
->name
, ".res" ) ||
1070 strendswith( file
->name
, ".pot" ) ||
1071 strendswith( file
->name
, ".x" ))
1073 file
->filename
= xstrdup( file
->name
);
1077 parse_file( file
, 1 );
1082 /*******************************************************************
1083 * add_generated_sources
1085 static void add_generated_sources(void)
1088 struct incl_file
*source
, *next
;
1090 LIST_FOR_EACH_ENTRY_SAFE( source
, next
, &sources
, struct incl_file
, entry
)
1092 if (!source
->flags
) continue;
1093 for (i
= 0; i
< sizeof(idl_outputs
) / sizeof(idl_outputs
[0]); i
++)
1095 if (!(source
->flags
& idl_outputs
[i
].flag
)) continue;
1096 if (!strendswith( idl_outputs
[i
].ext
, ".c" )) continue;
1097 add_src_file( replace_extension( source
->name
, 4, idl_outputs
[i
].ext
));
1099 if (source
->flags
& FLAG_IDL_PROXY
) add_src_file( "dlldata.o" );
1102 LIST_FOR_EACH_ENTRY_SAFE( source
, next
, &sources
, struct incl_file
, entry
)
1104 if (strendswith( source
->name
, "_c.c" ) ||
1105 strendswith( source
->name
, "_i.c" ) ||
1106 strendswith( source
->name
, "_p.c" ) ||
1107 strendswith( source
->name
, "_s.c" ) ||
1108 strendswith( source
->name
, ".tlb" ))
1110 char *idl
= replace_extension( source
->name
, 4, ".idl" );
1111 struct incl_file
*file
= add_src_file( idl
);
1112 if (strendswith( source
->name
, "_c.c" )) file
->flags
|= FLAG_IDL_CLIENT
;
1113 else if (strendswith( source
->name
, "_i.c" )) file
->flags
|= FLAG_IDL_IDENT
;
1114 else if (strendswith( source
->name
, "_p.c" )) file
->flags
|= FLAG_IDL_PROXY
;
1115 else if (strendswith( source
->name
, "_s.c" )) file
->flags
|= FLAG_IDL_SERVER
;
1116 else if (strendswith( source
->name
, ".tlb" )) file
->flags
|= FLAG_IDL_TYPELIB
;
1119 if (strendswith( source
->name
, "_r.res" ) ||
1120 strendswith( source
->name
, "_t.res" ))
1122 char *idl
= replace_extension( source
->name
, 6, ".idl" );
1123 struct incl_file
*file
= add_src_file( idl
);
1124 if (strendswith( source
->name
, "_r.res" )) file
->flags
|= FLAG_IDL_REGISTER
;
1125 else if (strendswith( source
->name
, "_t.res" )) file
->flags
|= FLAG_IDL_TYPELIB
;
1128 if (strendswith( source
->name
, ".pot" ))
1130 char *rc
= replace_extension( source
->name
, 4, ".rc" );
1131 struct incl_file
*file
= add_src_file( rc
);
1132 file
->flags
|= FLAG_RC_PO
;
1138 /*******************************************************************
1141 static void output_include( struct incl_file
*pFile
, struct incl_file
*owner
, int *column
)
1145 if (pFile
->owner
== owner
) return;
1146 if (!pFile
->filename
) return;
1147 pFile
->owner
= owner
;
1148 output_filename( pFile
->filename
, column
);
1149 for (i
= 0; i
< MAX_INCLUDES
; i
++)
1150 if (pFile
->files
[i
]) output_include( pFile
->files
[i
], owner
, column
);
1154 /*******************************************************************
1157 static void output_sources(void)
1159 struct incl_file
*source
;
1160 struct strarray clean_files
, subdirs
;
1161 int i
, column
, po_srcs
= 0, mc_srcs
= 0;
1162 int is_test
= find_src_file( "testlist.o" ) != NULL
;
1164 strarray_init( &clean_files
);
1165 strarray_init( &subdirs
);
1167 column
= output( "includes = -I." );
1168 if (src_dir
) output_filename( strmake( "-I%s", src_dir
), &column
);
1171 if (src_dir
) output_filename( strmake( "-I%s/%s", src_dir
, parent_dir
), &column
);
1172 else output_filename( strmake( "-I%s", parent_dir
), &column
);
1174 if (top_src_dir
&& top_obj_dir
) output_filename( strmake( "-I%s/include", top_obj_dir
), &column
);
1175 for (i
= 0; i
< include_args
.count
; i
++) output_filename( include_args
.str
[i
], &column
);
1178 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1180 char *obj
= xstrdup( source
->name
);
1181 char *ext
= get_extension( obj
);
1183 if (!ext
) fatal_error( "unsupported file type %s\n", source
->name
);
1187 if (!strcmp( ext
, "y" )) /* yacc file */
1189 /* add source file dependency for parallel makes */
1190 char *header
= strmake( "%s.tab.h", obj
);
1192 if (find_include_file( header
))
1194 output( "%s.tab.h: %s\n", obj
, source
->filename
);
1195 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
1196 obj
, obj
, source
->filename
);
1197 output( "%s.tab.c: %s %s\n", obj
, source
->filename
, header
);
1198 strarray_add( &clean_files
, strmake( "%s.tab.h", obj
));
1200 else output( "%s.tab.c: %s\n", obj
, source
->filename
);
1202 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj
, source
->filename
);
1203 output( "%s.tab.o: %s.tab.c\n", obj
, obj
);
1204 output( "\t$(CC) -c $(includes) $(ALLCFLAGS) -o $@ %s.tab.c\n", obj
);
1205 strarray_add( &clean_files
, strmake( "%s.tab.c", obj
));
1206 strarray_add( &clean_files
, strmake( "%s.tab.o", obj
));
1207 column
+= output( "%s.tab.o:", obj
);
1210 else if (!strcmp( ext
, "l" )) /* lex file */
1212 output( "%s.yy.c: %s\n", obj
, source
->filename
);
1213 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source
->filename
);
1214 output( "%s.yy.o: %s.yy.c\n", obj
, obj
);
1215 output( "\t$(CC) -c $(includes) $(ALLCFLAGS) -o $@ %s.yy.c\n", obj
);
1216 strarray_add( &clean_files
, strmake( "%s.yy.c", obj
));
1217 strarray_add( &clean_files
, strmake( "%s.yy.o", obj
));
1218 column
+= output( "%s.yy.o:", obj
);
1220 else if (!strcmp( ext
, "rc" )) /* resource file */
1222 if (source
->flags
& FLAG_RC_PO
)
1224 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj
, source
->filename
);
1225 output( "\t$(WRC) $(includes) $(RCFLAGS) -o $@ %s\n", source
->filename
);
1226 column
+= output( "%s.res rsrc.pot:", obj
);
1231 output( "%s.res: $(WRC) %s\n", obj
, source
->filename
);
1232 output( "\t$(WRC) $(includes) $(RCFLAGS) -o $@ %s\n", source
->filename
);
1233 column
+= output( "%s.res:", obj
);
1235 strarray_add( &clean_files
, strmake( "%s.res", obj
));
1237 else if (!strcmp( ext
, "mc" )) /* message file */
1239 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj
, source
->filename
);
1240 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source
->filename
);
1241 strarray_add( &clean_files
, strmake( "%s.res", obj
));
1243 column
+= output( "msg.pot %s.res:", obj
);
1245 else if (!strcmp( ext
, "idl" )) /* IDL file */
1248 unsigned int i
, nb_targets
= 0;
1250 if (!source
->flags
|| find_include_file( strmake( "%s.h", obj
)))
1251 source
->flags
|= FLAG_IDL_HEADER
;
1253 for (i
= 0; i
< sizeof(idl_outputs
) / sizeof(idl_outputs
[0]); i
++)
1255 if (!(source
->flags
& idl_outputs
[i
].flag
)) continue;
1256 output( "%s%s: $(WIDL)\n", obj
, idl_outputs
[i
].ext
);
1257 output( "\t$(WIDL) $(includes) %s -o $@ %s\n", idl_outputs
[i
].widl_arg
, source
->filename
);
1258 targets
[nb_targets
++] = strmake( "%s%s", obj
, idl_outputs
[i
].ext
);
1260 for (i
= 0; i
< nb_targets
; i
++)
1262 column
+= output( "%s%c", targets
[i
], i
< nb_targets
- 1 ? ' ' : ':' );
1263 strarray_add( &clean_files
, targets
[i
] );
1265 column
+= output( " %s", source
->filename
);
1267 else if (!strcmp( ext
, "in" )) /* man page */
1269 if (strendswith( obj
, ".man" ) && source
->sourcename
)
1271 char *dir
, *dest
= replace_extension( obj
, 4, "" );
1272 char *lang
= strchr( dest
, '.' );
1276 dir
= strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang
, source
->sourcename
);
1278 else dir
= strmake( "$(DESTDIR)$(mandir)/man%s", source
->sourcename
);
1279 output( "install-man-pages:: %s %s\n", obj
, dir
);
1280 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n",
1281 obj
, dir
, dest
, source
->sourcename
);
1282 output( "uninstall::\n" );
1283 output( "\t$(RM) %s/%s.%s\n",
1284 dir
, dest
, source
->sourcename
);
1286 strarray_add( &subdirs
, dir
);
1288 strarray_add( &clean_files
, xstrdup(obj
) );
1289 output( "%s: %s\n", obj
, source
->filename
);
1290 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source
->filename
);
1291 column
+= output( "%s:", obj
);
1293 else if (!strcmp( ext
, "tlb" ) || !strcmp( ext
, "res" ) || !strcmp( ext
, "pot" ))
1295 continue; /* nothing to do for typelib files */
1299 for (i
= 0; i
< object_extensions
.count
; i
++)
1301 strarray_add( &clean_files
, strmake( "%s.%s", obj
, object_extensions
.str
[i
] ));
1302 output( "%s.%s: %s\n", obj
, object_extensions
.str
[i
], source
->filename
);
1303 if (strstr( object_extensions
.str
[i
], "cross" ))
1304 output( "\t$(CROSSCC) -c $(includes) $(ALLCROSSCFLAGS) -o $@ %s\n", source
->filename
);
1306 output( "\t$(CC) -c $(includes) $(ALLCFLAGS) -o $@ %s\n", source
->filename
);
1308 if (source
->flags
& FLAG_C_IMPLIB
)
1310 strarray_add( &clean_files
, strmake( "%s.cross.o", obj
));
1311 output( "%s.cross.o: %s\n", obj
, source
->filename
);
1312 output( "\t$(CROSSCC) -c $(includes) $(ALLCROSSCFLAGS) -o $@ %s\n", source
->filename
);
1314 if (is_test
&& !strcmp( ext
, "c" ) && !is_generated_idl( source
))
1316 output( "%s.ok:\n", obj
);
1317 output( "\t$(RUNTEST) $(RUNTESTFLAGS) %s && touch $@\n", obj
);
1319 for (i
= 0; i
< object_extensions
.count
; i
++)
1320 column
+= output( "%s.%s ", obj
, object_extensions
.str
[i
] );
1321 if (source
->flags
& FLAG_C_IMPLIB
) column
+= output( "%s.cross.o", obj
);
1322 column
+= output( ":" );
1326 for (i
= 0; i
< MAX_INCLUDES
; i
++)
1327 if (source
->files
[i
]) output_include( source
->files
[i
], source
, &column
);
1331 /* rules for files that depend on multiple sources */
1335 column
= output( "rsrc.pot: $(WRC)" );
1336 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1337 if (source
->flags
& FLAG_RC_PO
) output_filename( source
->filename
, &column
);
1339 column
= output( "\t$(WRC) $(includes) $(RCFLAGS) -O pot -o $@" );
1340 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1341 if (source
->flags
& FLAG_RC_PO
) output_filename( source
->filename
, &column
);
1343 strarray_add( &clean_files
, "rsrc.pot" );
1348 column
= output( "msg.pot: $(WMC)" );
1349 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1350 if (strendswith( source
->name
, ".mc" )) output_filename( source
->filename
, &column
);
1352 column
= output( "\t$(WMC) -O pot -o $@" );
1353 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1354 if (strendswith( source
->name
, ".mc" )) output_filename( source
->filename
, &column
);
1356 strarray_add( &clean_files
, "msg.pot" );
1359 if (find_src_file( "dlldata.o" ))
1361 output( "dlldata.c: $(WIDL) Makefile.in\n" );
1362 column
= output( "\t$(WIDL) --dlldata-only -o $@" );
1363 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1364 if (source
->flags
& FLAG_IDL_PROXY
) output_filename( source
->filename
, &column
);
1366 strarray_add( &clean_files
, "dlldata.c" );
1371 output( "testlist.c: $(MAKECTESTS) Makefile.in\n" );
1372 column
= output( "\t$(MAKECTESTS) -o $@" );
1373 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1374 if (strendswith( source
->name
, ".c" ) && !is_generated_idl( source
))
1375 output_filename( source
->name
, &column
);
1377 column
= output( "check test:" );
1378 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1379 if (strendswith( source
->name
, ".c" ) && !is_generated_idl( source
))
1380 output_filename( replace_extension( source
->name
, 2, ".ok" ), &column
);
1382 output( "clean testclean::\n" );
1383 column
= output( "\t$(RM)" );
1384 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1385 if (strendswith( source
->name
, ".c" ) && !is_generated_idl( source
))
1386 output_filename( replace_extension( source
->name
, 2, ".ok" ), &column
);
1388 strarray_add( &clean_files
, "testlist.c" );
1391 if (clean_files
.count
)
1393 output( "clean::\n" );
1394 column
= output( "\t$(RM)" );
1395 for (i
= 0; i
< clean_files
.count
; i
++) output_filename( clean_files
.str
[i
], &column
);
1401 for (i
= column
= 0; i
< subdirs
.count
; i
++) output_filename( subdirs
.str
[i
], &column
);
1403 output( "\t$(MKDIR_P) -m 755 $@\n" );
1408 /*******************************************************************
1411 static FILE *create_temp_file( char **tmp_name
)
1413 char *name
= xmalloc( strlen(OutputFileName
) + 13 );
1414 unsigned int i
, id
= getpid();
1418 for (i
= 0; i
< 100; i
++)
1420 sprintf( name
, "%s.tmp%08x", OutputFileName
, id
);
1421 if ((fd
= open( name
, O_RDWR
| O_CREAT
| O_EXCL
, 0666 )) != -1)
1423 ret
= fdopen( fd
, "w" );
1426 if (errno
!= EEXIST
) break;
1429 if (!ret
) fatal_error( "failed to create output file for '%s'\n", OutputFileName
);
1435 /*******************************************************************
1436 * output_dependencies
1438 static void output_dependencies(void)
1440 char *tmp_name
= NULL
;
1442 if (Separator
&& ((output_file
= fopen( OutputFileName
, "r" ))))
1445 FILE *tmp_file
= create_temp_file( &tmp_name
);
1448 while (fgets( buffer
, sizeof(buffer
), output_file
) && !found
)
1450 if (fwrite( buffer
, 1, strlen(buffer
), tmp_file
) != strlen(buffer
))
1451 fatal_error( "failed to write to %s\n", tmp_name
);
1452 found
= !strncmp( buffer
, Separator
, strlen(Separator
) );
1454 fclose( output_file
);
1455 output_file
= tmp_file
;
1456 if (!found
&& !list_empty(&sources
)) output( "\n%s\n", Separator
);
1460 if (!(output_file
= fopen( OutputFileName
, Separator
? "a" : "w" )))
1461 fatal_perror( "%s", OutputFileName
);
1464 if (!list_empty( &sources
)) output_sources();
1466 fclose( output_file
);
1471 int ret
= rename( tmp_name
, OutputFileName
);
1472 if (ret
== -1 && errno
== EEXIST
)
1474 /* rename doesn't overwrite on windows */
1475 unlink( OutputFileName
);
1476 ret
= rename( tmp_name
, OutputFileName
);
1481 fatal_error( "failed to rename output file to '%s'\n", OutputFileName
);
1488 /*******************************************************************
1491 static void parse_option( const char *opt
)
1496 if (opt
[2]) strarray_add( &include_args
, opt
);
1502 top_src_dir
= opt
+ 2;
1505 top_obj_dir
= opt
+ 2;
1508 parent_dir
= opt
+ 2;
1511 if (opt
[2]) OutputFileName
= opt
+ 2;
1514 relative_dir_mode
= 1;
1517 if (opt
[2]) Separator
= opt
+ 2;
1518 else Separator
= NULL
;
1521 if (opt
[2]) strarray_add( &object_extensions
, xstrdup( opt
+ 2 ));
1524 fprintf( stderr
, "Unknown option '%s'\n%s", opt
, Usage
);
1530 /*******************************************************************
1533 int main( int argc
, char *argv
[] )
1535 struct incl_file
*pFile
;
1541 if (argv
[i
][0] == '-')
1543 parse_option( argv
[i
] );
1544 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
1550 if (relative_dir_mode
)
1556 fprintf( stderr
, "Option -r needs two directories\n%s", Usage
);
1559 relpath
= get_relative_path( argv
[1], argv
[2] );
1560 printf( "%s\n", relpath
? relpath
: "." );
1564 /* ignore redundant source paths */
1565 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
1566 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
1568 if (top_src_dir
) strarray_insert( &include_args
, 0, strmake( "-I%s/include", top_src_dir
));
1569 else if (top_obj_dir
) strarray_insert( &include_args
, 0, strmake( "-I%s/include", top_obj_dir
));
1571 /* set the default extension list for object files */
1572 if (!object_extensions
.count
) strarray_add( &object_extensions
, "o" );
1574 for (i
= 1; i
< argc
; i
++) add_src_file( argv
[i
] );
1575 add_generated_sources();
1577 LIST_FOR_EACH_ENTRY( pFile
, &includes
, struct incl_file
, entry
) parse_file( pFile
, 0 );
1578 output_dependencies();