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 _OBJECT_EXTENSION
59 const char *extension
;
62 static struct list object_extensions
= LIST_INIT(object_extensions
);
64 typedef struct _INCL_PATH
70 static struct list paths
= LIST_INIT(paths
);
72 static const char *src_dir
;
73 static const char *top_src_dir
;
74 static const char *top_obj_dir
;
75 static const char *OutputFileName
= "Makefile";
76 static const char *Separator
= "### Dependencies";
77 static const char *ProgramName
;
78 static int input_line
;
80 static const char Usage
[] =
81 "Usage: %s [options] [files]\n"
83 " -Idir Search for include files in directory 'dir'\n"
84 " -Cdir Search for source files in directory 'dir'\n"
85 " -Sdir Set the top source directory\n"
86 " -Tdir Set the top object directory\n"
87 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
88 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
91 /*******************************************************************
94 static void fatal_error( const char *msg
, ... )
97 va_start( valist
, msg
);
98 vfprintf( stderr
, msg
, valist
);
104 /*******************************************************************
107 static void *xmalloc( size_t size
)
110 if (!(res
= malloc (size
? size
: 1)))
111 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
116 /*******************************************************************
119 static void *xrealloc (void *ptr
, size_t size
)
123 if (!(res
= realloc( ptr
, size
)))
124 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
128 /*******************************************************************
131 static char *xstrdup( const char *str
)
133 char *res
= strdup( str
);
134 if (!res
) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName
);
139 /*******************************************************************
142 static char *strmake( const char* fmt
, ... )
150 char *p
= xmalloc (size
);
152 n
= vsnprintf (p
, size
, fmt
, ap
);
154 if (n
== -1) size
*= 2;
155 else if ((size_t)n
>= size
) size
= n
+ 1;
162 /*******************************************************************
165 static int strendswith( const char* str
, const char* end
)
170 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
173 /*******************************************************************
176 static char *get_extension( char *filename
)
178 char *ext
= strrchr( filename
, '.' );
179 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
184 /*******************************************************************
187 static char *get_line( FILE *file
)
190 static unsigned int size
;
195 buffer
= xmalloc( size
);
197 if (!fgets( buffer
, size
, file
)) return NULL
;
202 char *p
= buffer
+ strlen(buffer
);
203 /* if line is larger than buffer, resize buffer */
204 while (p
== buffer
+ size
- 1 && p
[-1] != '\n')
206 buffer
= xrealloc( buffer
, size
* 2 );
207 if (!fgets( buffer
+ size
- 1, size
+ 1, file
)) break;
208 p
= buffer
+ strlen(buffer
);
211 if (p
> buffer
&& p
[-1] == '\n')
214 if (p
> buffer
&& p
[-1] == '\r') *(--p
) = 0;
215 if (p
> buffer
&& p
[-1] == '\\')
218 /* line ends in backslash, read continuation line */
219 if (!fgets( p
, size
- (p
- buffer
), file
)) return buffer
;
228 /*******************************************************************
229 * add_object_extension
231 * Add an extension for object files.
233 static void add_object_extension( const char *ext
)
235 OBJECT_EXTENSION
*object_extension
= xmalloc( sizeof(*object_extension
) );
236 list_add_tail( &object_extensions
, &object_extension
->entry
);
237 object_extension
->extension
= ext
;
240 /*******************************************************************
243 * Add a directory to the include path.
245 static void add_include_path( const char *name
)
247 INCL_PATH
*path
= xmalloc( sizeof(*path
) );
248 list_add_tail( &paths
, &path
->entry
);
252 /*******************************************************************
255 static INCL_FILE
*find_src_file( const char *name
)
259 LIST_FOR_EACH_ENTRY( file
, &sources
, INCL_FILE
, entry
)
260 if (!strcmp( name
, file
->name
)) return file
;
264 /*******************************************************************
267 static INCL_FILE
*find_include_file( const char *name
)
271 LIST_FOR_EACH_ENTRY( file
, &includes
, INCL_FILE
, entry
)
272 if (!strcmp( name
, file
->name
)) return file
;
276 /*******************************************************************
279 * Add an include file if it doesn't already exists.
281 static INCL_FILE
*add_include( INCL_FILE
*pFile
, const char *name
, int line
, int system
)
287 for (pos
= 0; pos
< MAX_INCLUDES
; pos
++) if (!pFile
->files
[pos
]) break;
288 if (pos
>= MAX_INCLUDES
)
289 fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
290 ProgramName
, pFile
->name
);
292 /* enforce some rules for the Wine tree */
294 if (!memcmp( name
, "../", 3 ))
295 fatal_error( "%s:%d: #include directive with relative path not allowed\n",
296 pFile
->filename
, line
);
298 if (!strcmp( name
, "config.h" ))
300 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
301 fatal_error( "%s:%d: config.h must not be included by a header file\n",
302 pFile
->filename
, line
);
304 fatal_error( "%s:%d: config.h must be included before anything else\n",
305 pFile
->filename
, line
);
307 else if (!strcmp( name
, "wine/port.h" ))
309 if ((ext
= strrchr( pFile
->filename
, '.' )) && !strcmp( ext
, ".h" ))
310 fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
311 pFile
->filename
, line
);
312 if (!pos
) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
313 pFile
->filename
, line
);
315 fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
316 pFile
->filename
, line
);
317 if (strcmp( pFile
->files
[0]->name
, "config.h" ))
318 fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
319 pFile
->filename
, line
);
322 LIST_FOR_EACH_ENTRY( include
, &includes
, INCL_FILE
, entry
)
323 if (!strcmp( name
, include
->name
)) goto found
;
325 include
= xmalloc( sizeof(INCL_FILE
) );
326 memset( include
, 0, sizeof(INCL_FILE
) );
327 include
->name
= xstrdup(name
);
328 include
->included_by
= pFile
;
329 include
->included_line
= line
;
330 include
->system
= system
;
331 list_add_tail( &includes
, &include
->entry
);
333 pFile
->files
[pos
] = include
;
338 /*******************************************************************
341 static FILE *open_src_file( INCL_FILE
*pFile
)
345 /* first try name as is */
346 if ((file
= fopen( pFile
->name
, "r" )))
348 pFile
->filename
= xstrdup( pFile
->name
);
351 /* now try in source dir */
354 pFile
->filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
355 file
= fopen( pFile
->filename
, "r" );
359 perror( pFile
->name
);
366 /*******************************************************************
369 static FILE *open_include_file( INCL_FILE
*pFile
)
377 /* check for generated bison header */
379 if (strendswith( pFile
->name
, ".tab.h" ))
382 filename
= strmake( "%s/%.*s.y", src_dir
, strlen(pFile
->name
) - 6, pFile
->name
);
384 filename
= strmake( "%.*s.y", strlen(pFile
->name
) - 6, pFile
->name
);
386 if ((file
= fopen( filename
, "r" )))
388 pFile
->sourcename
= filename
;
389 pFile
->filename
= xstrdup( pFile
->name
);
390 /* don't bother to parse it */
397 /* check for generated message resource */
399 if (strendswith( pFile
->name
, ".mc.rc" ))
402 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
404 filename
= xstrdup( pFile
->name
);
406 filename
[strlen(filename
) - 3] = 0;
408 if ((file
= fopen( filename
, "r" )))
410 pFile
->sourcename
= filename
;
411 pFile
->filename
= xstrdup( pFile
->name
);
412 /* don't bother to parse it */
419 /* check for corresponding idl file in source dir */
421 if (strendswith( pFile
->name
, ".h" ))
424 filename
= strmake( "%s/%.*s.idl", src_dir
, strlen(pFile
->name
) - 2, pFile
->name
);
426 filename
= strmake( "%.*s.idl", strlen(pFile
->name
) - 2, pFile
->name
);
428 if ((file
= fopen( filename
, "r" )))
430 pFile
->sourcename
= filename
;
431 pFile
->filename
= xstrdup( pFile
->name
);
437 /* first try name as is */
438 if ((file
= fopen( pFile
->name
, "r" )))
440 pFile
->filename
= xstrdup( pFile
->name
);
444 /* now try in source dir */
447 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
448 if ((file
= fopen( filename
, "r" ))) goto found
;
452 /* check for corresponding idl file in global includes */
454 if (strendswith( pFile
->name
, ".h" ))
457 filename
= strmake( "%s/include/%.*s.idl",
458 top_src_dir
, strlen(pFile
->name
) - 2, pFile
->name
);
459 else if (top_obj_dir
)
460 filename
= strmake( "%s/include/%.*s.idl",
461 top_obj_dir
, strlen(pFile
->name
) - 2, pFile
->name
);
465 if (filename
&& (file
= fopen( filename
, "r" )))
467 pFile
->sourcename
= filename
;
468 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
474 /* check for corresponding .x file in global includes */
476 if (strendswith( pFile
->name
, "tmpl.h" ))
479 filename
= strmake( "%s/include/%.*s.x",
480 top_src_dir
, strlen(pFile
->name
) - 2, pFile
->name
);
481 else if (top_obj_dir
)
482 filename
= strmake( "%s/include/%.*s.x",
483 top_obj_dir
, strlen(pFile
->name
) - 2, pFile
->name
);
487 if (filename
&& (file
= fopen( filename
, "r" )))
489 pFile
->sourcename
= filename
;
490 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
496 /* now try in global includes */
499 filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
500 if ((file
= fopen( filename
, "r" ))) goto found
;
505 filename
= strmake( "%s/include/%s", top_src_dir
, pFile
->name
);
506 if ((file
= fopen( filename
, "r" ))) goto found
;
510 /* now search in include paths */
511 LIST_FOR_EACH_ENTRY( path
, &paths
, INCL_PATH
, entry
)
513 filename
= strmake( "%s/%s", path
->name
, pFile
->name
);
514 if ((file
= fopen( filename
, "r" ))) goto found
;
517 if (pFile
->system
) return NULL
; /* ignore system files we cannot find */
519 /* try in src file directory */
520 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
522 int l
= p
- pFile
->included_by
->filename
+ 1;
523 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
524 memcpy( filename
, pFile
->included_by
->filename
, l
);
525 strcpy( filename
+ l
, pFile
->name
);
526 if ((file
= fopen( filename
, "r" ))) goto found
;
530 perror( pFile
->name
);
531 while (pFile
->included_by
)
533 const char *parent
= pFile
->included_by
->sourcename
;
534 if (!parent
) parent
= pFile
->included_by
->name
;
535 fprintf( stderr
, " %s was first included from %s:%d\n",
536 pFile
->name
, parent
, pFile
->included_line
);
537 pFile
= pFile
->included_by
;
542 pFile
->filename
= filename
;
547 /*******************************************************************
550 * If for_h_file is non-zero, it means we are not interested in the idl file
551 * itself, but only in the contents of the .h file that will be generated from it.
553 static void parse_idl_file( INCL_FILE
*pFile
, FILE *file
, int for_h_file
)
555 char *buffer
, *include
;
559 /* generated .h file always includes these */
560 add_include( pFile
, "rpc.h", 0, 1 );
561 add_include( pFile
, "rpcndr.h", 0, 1 );
565 while ((buffer
= get_line( file
)))
569 while (*p
&& isspace(*p
)) p
++;
571 if (!strncmp( p
, "import", 6 ))
574 while (*p
&& isspace(*p
)) p
++;
575 if (*p
!= '"') continue;
577 while (*p
&& (*p
!= '"')) p
++;
578 if (!*p
) fatal_error( "%s:%d: Malformed import directive\n", pFile
->filename
, input_line
);
580 if (for_h_file
&& strendswith( include
, ".idl" )) strcpy( p
- 4, ".h" );
581 add_include( pFile
, include
, input_line
, 0 );
585 if (for_h_file
) /* only check for #include inside cpp_quote */
587 if (strncmp( p
, "cpp_quote", 9 )) continue;
589 while (*p
&& isspace(*p
)) p
++;
590 if (*p
++ != '(') continue;
591 while (*p
&& isspace(*p
)) p
++;
592 if (*p
++ != '"') continue;
593 if (*p
++ != '#') continue;
594 while (*p
&& isspace(*p
)) p
++;
595 if (strncmp( p
, "include", 7 )) continue;
597 while (*p
&& isspace(*p
)) p
++;
598 if (*p
== '\\' && p
[1] == '"')
605 if (*p
++ != '<' ) continue;
609 while (*p
&& (*p
!= quote
)) p
++;
610 if (!*p
|| (quote
== '"' && p
[-1] != '\\'))
611 fatal_error( "%s:%d: Malformed #include directive inside cpp_quote\n",
612 pFile
->filename
, input_line
);
613 if (quote
== '"') p
--; /* remove backslash */
615 add_include( pFile
, include
, input_line
, (quote
== '>') );
619 /* check for normal #include */
620 if (*p
++ != '#') continue;
621 while (*p
&& isspace(*p
)) p
++;
622 if (strncmp( p
, "include", 7 )) continue;
624 while (*p
&& isspace(*p
)) p
++;
625 if (*p
!= '\"' && *p
!= '<' ) continue;
627 if (quote
== '<') quote
= '>';
629 while (*p
&& (*p
!= quote
)) p
++;
630 if (!*p
) fatal_error( "%s:%d: Malformed #include directive\n", pFile
->filename
, input_line
);
632 add_include( pFile
, include
, input_line
, (quote
== '>') );
636 /*******************************************************************
639 static void parse_c_file( INCL_FILE
*pFile
, FILE *file
)
641 char *buffer
, *include
;
644 while ((buffer
= get_line( file
)))
648 while (*p
&& isspace(*p
)) p
++;
649 if (*p
++ != '#') continue;
650 while (*p
&& isspace(*p
)) p
++;
651 if (strncmp( p
, "include", 7 )) continue;
653 while (*p
&& isspace(*p
)) p
++;
654 if (*p
!= '\"' && *p
!= '<' ) continue;
656 if (quote
== '<') quote
= '>';
658 while (*p
&& (*p
!= quote
)) p
++;
659 if (!*p
) fatal_error( "%s:%d: Malformed #include directive\n",
660 pFile
->filename
, input_line
);
662 add_include( pFile
, include
, input_line
, (quote
== '>') );
667 /*******************************************************************
670 static void parse_rc_file( INCL_FILE
*pFile
, FILE *file
)
672 char *buffer
, *include
;
675 while ((buffer
= get_line( file
)))
679 while (*p
&& isspace(*p
)) p
++;
681 if (p
[0] == '/' && p
[1] == '*') /* check for magic makedep comment */
684 while (*p
&& isspace(*p
)) p
++;
685 if (strncmp( p
, "@makedep:", 9 )) continue;
687 while (*p
&& isspace(*p
)) p
++;
692 while (*p
&& *p
!= quote
) p
++;
697 while (*p
&& !isspace(*p
) && *p
!= '*') p
++;
700 fatal_error( "%s:%d: Malformed makedep comment\n", pFile
->filename
, input_line
);
703 else /* check for #include */
705 if (*p
++ != '#') continue;
706 while (*p
&& isspace(*p
)) p
++;
707 if (strncmp( p
, "include", 7 )) continue;
709 while (*p
&& isspace(*p
)) p
++;
710 if (*p
!= '\"' && *p
!= '<' ) continue;
712 if (quote
== '<') quote
= '>';
714 while (*p
&& (*p
!= quote
)) p
++;
715 if (!*p
) fatal_error( "%s:%d: Malformed #include directive\n",
716 pFile
->filename
, input_line
);
719 add_include( pFile
, include
, input_line
, (quote
== '>') );
724 /*******************************************************************
725 * parse_generated_idl
727 static void parse_generated_idl( INCL_FILE
*source
)
729 char *header
, *basename
;
731 basename
= xstrdup( source
->name
);
732 basename
[strlen(basename
) - 4] = 0;
733 header
= strmake( "%s.h", basename
);
734 source
->filename
= xstrdup( source
->name
);
736 if (strendswith( source
->name
, "_c.c" ))
738 add_include( source
, header
, 0, 0 );
740 else if (strendswith( source
->name
, "_i.c" ))
742 add_include( source
, "rpc.h", 0, 1 );
743 add_include( source
, "rpcndr.h", 0, 1 );
744 add_include( source
, "guiddef.h", 0, 1 );
746 else if (strendswith( source
->name
, "_p.c" ))
748 add_include( source
, "objbase.h", 0, 1 );
749 add_include( source
, "rpcproxy.h", 0, 1 );
750 add_include( source
, "wine/exception.h", 0, 1 );
751 add_include( source
, header
, 0, 0 );
753 else if (strendswith( source
->name
, "_s.c" ))
755 add_include( source
, "wine/exception.h", 0, 1 );
756 add_include( source
, header
, 0, 0 );
758 else if (!strcmp( source
->name
, "dlldata.c" ))
760 add_include( source
, "objbase.h", 0, 1 );
761 add_include( source
, "rpcproxy.h", 0, 1 );
768 /*******************************************************************
771 static void parse_file( INCL_FILE
*pFile
, int src
)
775 /* special case for source files generated from idl */
776 if (strendswith( pFile
->name
, "_c.c" ) ||
777 strendswith( pFile
->name
, "_i.c" ) ||
778 strendswith( pFile
->name
, "_p.c" ) ||
779 strendswith( pFile
->name
, "_s.c" ) ||
780 !strcmp( pFile
->name
, "dlldata.c" ))
782 parse_generated_idl( pFile
);
786 /* don't try to open certain types of files */
787 if (strendswith( pFile
->name
, ".tlb" ) ||
788 strendswith( pFile
->name
, ".res" ) ||
789 strendswith( pFile
->name
, ".x" ))
791 pFile
->filename
= xstrdup( pFile
->name
);
795 file
= src
? open_src_file( pFile
) : open_include_file( pFile
);
798 if (pFile
->sourcename
&& strendswith( pFile
->sourcename
, ".idl" ))
799 parse_idl_file( pFile
, file
, 1 );
800 else if (strendswith( pFile
->filename
, ".idl" ))
801 parse_idl_file( pFile
, file
, 0 );
802 else if (strendswith( pFile
->filename
, ".c" ) ||
803 strendswith( pFile
->filename
, ".h" ) ||
804 strendswith( pFile
->filename
, ".l" ) ||
805 strendswith( pFile
->filename
, ".y" ))
806 parse_c_file( pFile
, file
);
807 else if (strendswith( pFile
->filename
, ".rc" ))
808 parse_rc_file( pFile
, file
);
813 /*******************************************************************
816 * Add a source file to the list.
818 static INCL_FILE
*add_src_file( const char *name
)
822 if (find_src_file( name
)) return NULL
; /* we already have it */
823 file
= xmalloc( sizeof(*file
) );
824 memset( file
, 0, sizeof(*file
) );
825 file
->name
= xstrdup(name
);
826 list_add_tail( &sources
, &file
->entry
);
827 parse_file( file
, 1 );
832 /*******************************************************************
835 static void output_include( FILE *file
, INCL_FILE
*pFile
,
836 INCL_FILE
*owner
, int *column
)
840 if (pFile
->owner
== owner
) return;
841 if (!pFile
->filename
) return;
842 pFile
->owner
= owner
;
843 if (*column
+ strlen(pFile
->filename
) + 1 > 70)
845 fprintf( file
, " \\\n" );
848 fprintf( file
, " %s", pFile
->filename
);
849 *column
+= strlen(pFile
->filename
) + 1;
850 for (i
= 0; i
< MAX_INCLUDES
; i
++)
851 if (pFile
->files
[i
]) output_include( file
, pFile
->files
[i
],
856 /*******************************************************************
859 static int output_src( FILE *file
, INCL_FILE
*pFile
, int *column
)
861 char *obj
= xstrdup( pFile
->name
);
862 char *ext
= get_extension( obj
);
866 if (!strcmp( ext
, "y" )) /* yacc file */
868 /* add source file dependency for parallel makes */
869 char *header
= strmake( "%s.tab.h", obj
);
870 if (find_include_file( header
)) fprintf( file
, "%s.tab.c: %s\n", obj
, header
);
872 *column
+= fprintf( file
, "%s.tab.o: %s.tab.c", obj
, obj
);
874 else if (!strcmp( ext
, "l" )) /* lex file */
876 *column
+= fprintf( file
, "%s.yy.o: %s.yy.c", obj
, obj
);
878 else if (!strcmp( ext
, "rc" )) /* resource file */
880 *column
+= fprintf( file
, "rsrc.pot %s.res: %s", obj
, pFile
->filename
);
882 else if (!strcmp( ext
, "mc" )) /* message file */
884 *column
+= fprintf( file
, "msg.pot %s.res: %s", obj
, pFile
->filename
);
886 else if (!strcmp( ext
, "idl" )) /* IDL file */
890 const char *suffix
= "cips";
892 name
= strmake( "%s.tlb", obj
);
893 if (find_src_file( name
)) *column
+= fprintf( file
, "%s %s_t.res", name
, obj
);
897 *column
+= fprintf( file
, "%s.h", obj
);
903 name
= strmake( "%s_%c.c", obj
, *suffix
);
904 if (find_src_file( name
))
906 if (!got_header
++) *column
+= fprintf( file
, " %s.h", obj
);
907 *column
+= fprintf( file
, " %s", name
);
913 name
= strmake( "%s_r.res", obj
);
914 if (find_src_file( name
)) *column
+= fprintf( file
, " %s", name
);
917 *column
+= fprintf( file
, ": %s", pFile
->filename
);
919 else if (!strcmp( ext
, "tlb" ) || !strcmp( ext
, "res" ))
921 return 0; /* nothing to do for typelib files */
925 OBJECT_EXTENSION
*ext
;
926 LIST_FOR_EACH_ENTRY( ext
, &object_extensions
, OBJECT_EXTENSION
, entry
)
927 *column
+= fprintf( file
, "%s.%s ", obj
, ext
->extension
);
928 *column
+= fprintf( file
, ": %s", pFile
->filename
);
936 /*******************************************************************
939 static FILE *create_temp_file( char **tmp_name
)
941 char *name
= xmalloc( strlen(OutputFileName
) + 13 );
942 unsigned int i
, id
= getpid();
946 for (i
= 0; i
< 100; i
++)
948 sprintf( name
, "%s.tmp%08x", OutputFileName
, id
);
949 if ((fd
= open( name
, O_RDWR
| O_CREAT
| O_EXCL
, 0666 )) != -1)
951 ret
= fdopen( fd
, "w" );
954 if (errno
!= EEXIST
) break;
957 if (!ret
) fatal_error( "failed to create output file for '%s'\n", OutputFileName
);
963 /*******************************************************************
964 * output_dependencies
966 static void output_dependencies(void)
971 char *tmp_name
= NULL
;
973 if (Separator
&& ((file
= fopen( OutputFileName
, "r" ))))
976 FILE *tmp_file
= create_temp_file( &tmp_name
);
979 while (fgets( buffer
, sizeof(buffer
), file
) && !found
)
981 if (fwrite( buffer
, 1, strlen(buffer
), tmp_file
) != strlen(buffer
))
982 fatal_error( "error writing to %s\n", tmp_name
);
983 found
= !strncmp( buffer
, Separator
, strlen(Separator
) );
987 if (!found
&& list_head(&sources
)) fprintf( file
, "\n%s\n", Separator
);
991 if (!(file
= fopen( OutputFileName
, Separator
? "a" : "w" )))
993 perror( OutputFileName
);
997 LIST_FOR_EACH_ENTRY( pFile
, &sources
, INCL_FILE
, entry
)
1000 if (!output_src( file
, pFile
, &column
)) continue;
1001 for (i
= 0; i
< MAX_INCLUDES
; i
++)
1002 if (pFile
->files
[i
]) output_include( file
, pFile
->files
[i
],
1004 fprintf( file
, "\n" );
1010 int ret
= rename( tmp_name
, OutputFileName
);
1011 if (ret
== -1 && errno
== EEXIST
)
1013 /* rename doesn't overwrite on windows */
1014 unlink( OutputFileName
);
1015 ret
= rename( tmp_name
, OutputFileName
);
1020 fatal_error( "failed to rename output file to '%s'\n", OutputFileName
);
1027 /*******************************************************************
1030 static void parse_option( const char *opt
)
1035 if (opt
[2]) add_include_path( opt
+ 2 );
1041 top_src_dir
= opt
+ 2;
1044 top_obj_dir
= opt
+ 2;
1047 if (opt
[2]) OutputFileName
= opt
+ 2;
1050 if (opt
[2]) Separator
= opt
+ 2;
1051 else Separator
= NULL
;
1054 if (opt
[2]) add_object_extension( opt
+ 2 );
1057 fprintf( stderr
, "Unknown option '%s'\n", opt
);
1058 fprintf( stderr
, Usage
, ProgramName
);
1064 /*******************************************************************
1067 int main( int argc
, char *argv
[] )
1070 INCL_PATH
*path
, *next
;
1073 ProgramName
= argv
[0];
1078 if (argv
[i
][0] == '-')
1080 parse_option( argv
[i
] );
1081 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
1087 /* ignore redundant source paths */
1088 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
1089 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
1091 /* set the default extension list for object files */
1092 if (list_empty( &object_extensions
))
1093 add_object_extension( "o" );
1095 /* get rid of absolute paths that don't point into the source dir */
1096 LIST_FOR_EACH_ENTRY_SAFE( path
, next
, &paths
, INCL_PATH
, entry
)
1098 if (path
->name
[0] != '/') continue;
1101 if (!strncmp( path
->name
, top_src_dir
, strlen(top_src_dir
) ) &&
1102 path
->name
[strlen(top_src_dir
)] == '/') continue;
1104 list_remove( &path
->entry
);
1108 for (i
= 1; i
< argc
; i
++)
1110 add_src_file( argv
[i
] );
1111 if (strendswith( argv
[i
], "_p.c" )) add_src_file( "dlldata.c" );
1113 LIST_FOR_EACH_ENTRY( pFile
, &includes
, INCL_FILE
, entry
) parse_file( pFile
, 0 );
1114 output_dependencies();