dinput: Fix definitions to match PSDK.
[wine/multimedia.git] / tools / makedep.c
blob60278a94c5115839a57fba343265ebf0504a23eb
1 /*
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
21 #include "config.h"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include "wine/list.h"
37 /* Max first-level includes per file */
38 #define MAX_INCLUDES 200
40 typedef struct _INCL_FILE
42 struct list entry;
43 char *name;
44 char *filename;
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];
51 } INCL_FILE;
53 static struct list sources = LIST_INIT(sources);
54 static struct list includes = LIST_INIT(includes);
56 typedef struct _OBJECT_EXTENSION
58 struct list entry;
59 const char *extension;
60 } OBJECT_EXTENSION;
62 static struct list object_extensions = LIST_INIT(object_extensions);
64 typedef struct _INCL_PATH
66 struct list entry;
67 const char *name;
68 } 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"
82 "Options:\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 /*******************************************************************
92 * fatal_error
94 static void fatal_error( const char *msg, ... )
96 va_list valist;
97 va_start( valist, msg );
98 vfprintf( stderr, msg, valist );
99 va_end( valist );
100 exit(1);
104 /*******************************************************************
105 * xmalloc
107 static void *xmalloc( size_t size )
109 void *res;
110 if (!(res = malloc (size ? size : 1)))
111 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
112 return res;
116 /*******************************************************************
117 * xrealloc
119 static void *xrealloc (void *ptr, size_t size)
121 void *res;
122 assert( size );
123 if (!(res = realloc( ptr, size )))
124 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
125 return res;
128 /*******************************************************************
129 * xstrdup
131 static char *xstrdup( const char *str )
133 char *res = strdup( str );
134 if (!res) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
135 return res;
139 /*******************************************************************
140 * strmake
142 static char *strmake( const char* fmt, ... )
144 int n;
145 size_t size = 100;
146 va_list ap;
148 for (;;)
150 char *p = xmalloc (size);
151 va_start(ap, fmt);
152 n = vsnprintf (p, size, fmt, ap);
153 va_end(ap);
154 if (n == -1) size *= 2;
155 else if ((size_t)n >= size) size = n + 1;
156 else return p;
157 free(p);
162 /*******************************************************************
163 * strendswith
165 static int strendswith( const char* str, const char* end )
167 int l = strlen(str);
168 int m = strlen(end);
170 return l >= m && strcmp(str + l - m, end) == 0;
173 /*******************************************************************
174 * get_extension
176 static char *get_extension( char *filename )
178 char *ext = strrchr( filename, '.' );
179 if (ext && strchr( ext, '/' )) ext = NULL;
180 return ext;
184 /*******************************************************************
185 * get_line
187 static char *get_line( FILE *file )
189 static char *buffer;
190 static unsigned int size;
192 if (!size)
194 size = 1024;
195 buffer = xmalloc( size );
197 if (!fgets( buffer, size, file )) return NULL;
198 input_line++;
200 for (;;)
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 fgets( buffer + size - 1, size + 1, file );
208 p = buffer + strlen(buffer);
209 size *= 2;
211 if (p > buffer && p[-1] == '\n')
213 *(--p) = 0;
214 if (p > buffer && p[-1] == '\r') *(--p) = 0;
215 if (p > buffer && p[-1] == '\\')
217 *(--p) = 0;
218 /* line ends in backslash, read continuation line */
219 fgets( p, size - (p - buffer), file );
220 input_line++;
221 continue;
224 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 /*******************************************************************
241 * add_include_path
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 );
249 path->name = name;
252 /*******************************************************************
253 * find_src_file
255 static INCL_FILE *find_src_file( const char *name )
257 INCL_FILE *file;
259 LIST_FOR_EACH_ENTRY( file, &sources, INCL_FILE, entry )
260 if (!strcmp( name, file->name )) return file;
261 return NULL;
264 /*******************************************************************
265 * add_include
267 * Add an include file if it doesn't already exists.
269 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
271 INCL_FILE *include;
272 char *ext;
273 int pos;
275 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
276 if (pos >= MAX_INCLUDES)
277 fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
278 ProgramName, pFile->name );
280 /* enforce some rules for the Wine tree */
282 if (!memcmp( name, "../", 3 ))
283 fatal_error( "%s:%d: #include directive with relative path not allowed\n",
284 pFile->filename, line );
286 if (!strcmp( name, "config.h" ))
288 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
289 fatal_error( "%s:%d: config.h must not be included by a header file\n",
290 pFile->filename, line );
291 if (pos)
292 fatal_error( "%s:%d: config.h must be included before anything else\n",
293 pFile->filename, line );
295 else if (!strcmp( name, "wine/port.h" ))
297 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
298 fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
299 pFile->filename, line );
300 if (!pos) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
301 pFile->filename, line );
302 if (pos > 1)
303 fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
304 pFile->filename, line );
305 if (strcmp( pFile->files[0]->name, "config.h" ))
306 fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
307 pFile->filename, line );
310 LIST_FOR_EACH_ENTRY( include, &includes, INCL_FILE, entry )
311 if (!strcmp( name, include->name )) goto found;
313 include = xmalloc( sizeof(INCL_FILE) );
314 memset( include, 0, sizeof(INCL_FILE) );
315 include->name = xstrdup(name);
316 include->included_by = pFile;
317 include->included_line = line;
318 include->system = system;
319 list_add_tail( &includes, &include->entry );
320 found:
321 pFile->files[pos] = include;
322 return include;
326 /*******************************************************************
327 * open_src_file
329 static FILE *open_src_file( INCL_FILE *pFile )
331 FILE *file;
333 /* first try name as is */
334 if ((file = fopen( pFile->name, "r" )))
336 pFile->filename = xstrdup( pFile->name );
337 return file;
339 /* now try in source dir */
340 if (src_dir)
342 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
343 file = fopen( pFile->filename, "r" );
345 if (!file)
347 perror( pFile->name );
348 exit(1);
350 return file;
354 /*******************************************************************
355 * open_include_file
357 static FILE *open_include_file( INCL_FILE *pFile )
359 FILE *file = NULL;
360 char *filename, *p;
361 INCL_PATH *path;
363 errno = ENOENT;
365 /* check for generated bison header */
367 if (strendswith( pFile->name, ".tab.h" ))
369 if (src_dir)
370 filename = strmake( "%s/%.*s.y", src_dir, strlen(pFile->name) - 6, pFile->name );
371 else
372 filename = strmake( "%.*s.y", strlen(pFile->name) - 6, pFile->name );
374 if ((file = fopen( filename, "r" )))
376 pFile->sourcename = filename;
377 pFile->filename = xstrdup( pFile->name );
378 /* don't bother to parse it */
379 fclose( file );
380 return NULL;
382 free( filename );
385 /* check for generated message resource */
387 if (strendswith( pFile->name, ".mc.rc" ))
389 if (src_dir)
390 filename = strmake( "%s/%s", src_dir, pFile->name );
391 else
392 filename = xstrdup( pFile->name );
394 filename[strlen(filename) - 3] = 0;
396 if ((file = fopen( filename, "r" )))
398 pFile->sourcename = filename;
399 pFile->filename = xstrdup( pFile->name );
400 /* don't bother to parse it */
401 fclose( file );
402 return NULL;
404 free( filename );
407 /* check for corresponding idl file in source dir */
409 if (strendswith( pFile->name, ".h" ))
411 if (src_dir)
412 filename = strmake( "%s/%.*s.idl", src_dir, strlen(pFile->name) - 2, pFile->name );
413 else
414 filename = strmake( "%.*s.idl", strlen(pFile->name) - 2, pFile->name );
416 if ((file = fopen( filename, "r" )))
418 pFile->sourcename = filename;
419 pFile->filename = xstrdup( pFile->name );
420 return file;
422 free( filename );
425 /* first try name as is */
426 if ((file = fopen( pFile->name, "r" )))
428 pFile->filename = xstrdup( pFile->name );
429 return file;
432 /* now try in source dir */
433 if (src_dir)
435 filename = strmake( "%s/%s", src_dir, pFile->name );
436 if ((file = fopen( filename, "r" ))) goto found;
437 free( filename );
440 /* check for corresponding idl file in global includes */
442 if (strendswith( pFile->name, ".h" ))
444 if (top_src_dir)
445 filename = strmake( "%s/include/%.*s.idl",
446 top_src_dir, strlen(pFile->name) - 2, pFile->name );
447 else if (top_obj_dir)
448 filename = strmake( "%s/include/%.*s.idl",
449 top_obj_dir, strlen(pFile->name) - 2, pFile->name );
450 else
451 filename = NULL;
453 if (filename && (file = fopen( filename, "r" )))
455 pFile->sourcename = filename;
456 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
457 return file;
459 free( filename );
462 /* now try in global includes */
463 if (top_obj_dir)
465 filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
466 if ((file = fopen( filename, "r" ))) goto found;
467 free( filename );
469 if (top_src_dir)
471 filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
472 if ((file = fopen( filename, "r" ))) goto found;
473 free( filename );
476 /* now search in include paths */
477 LIST_FOR_EACH_ENTRY( path, &paths, INCL_PATH, entry )
479 filename = strmake( "%s/%s", path->name, pFile->name );
480 if ((file = fopen( filename, "r" ))) goto found;
481 free( filename );
483 if (pFile->system) return NULL; /* ignore system files we cannot find */
485 /* try in src file directory */
486 if ((p = strrchr(pFile->included_by->filename, '/')))
488 int l = p - pFile->included_by->filename + 1;
489 filename = xmalloc(l + strlen(pFile->name) + 1);
490 memcpy( filename, pFile->included_by->filename, l );
491 strcpy( filename + l, pFile->name );
492 if ((file = fopen( filename, "r" ))) goto found;
493 free( filename );
496 perror( pFile->name );
497 while (pFile->included_by)
499 const char *parent = pFile->included_by->sourcename;
500 if (!parent) parent = pFile->included_by->name;
501 fprintf( stderr, " %s was first included from %s:%d\n",
502 pFile->name, parent, pFile->included_line );
503 pFile = pFile->included_by;
505 exit(1);
507 found:
508 pFile->filename = filename;
509 return file;
513 /*******************************************************************
514 * parse_idl_file
516 * If for_h_file is non-zero, it means we are not interested in the idl file
517 * itself, but only in the contents of the .h file that will be generated from it.
519 static void parse_idl_file( INCL_FILE *pFile, FILE *file, int for_h_file )
521 char *buffer, *include;
523 if (for_h_file)
525 /* generated .h file always includes these */
526 add_include( pFile, "rpc.h", 0, 1 );
527 add_include( pFile, "rpcndr.h", 0, 1 );
530 input_line = 0;
531 while ((buffer = get_line( file )))
533 char quote;
534 char *p = buffer;
535 while (*p && isspace(*p)) p++;
537 if (!strncmp( p, "import", 6 ))
539 p += 6;
540 while (*p && isspace(*p)) p++;
541 if (*p != '"') continue;
542 include = ++p;
543 while (*p && (*p != '"')) p++;
544 if (!*p) fatal_error( "%s:%d: Malformed import directive\n", pFile->filename, input_line );
545 *p = 0;
546 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
547 add_include( pFile, include, input_line, 0 );
548 continue;
551 if (for_h_file) /* only check for #include inside cpp_quote */
553 if (strncmp( p, "cpp_quote", 9 )) continue;
554 p += 9;
555 while (*p && isspace(*p)) p++;
556 if (*p++ != '(') continue;
557 while (*p && isspace(*p)) p++;
558 if (*p++ != '"') continue;
559 if (*p++ != '#') continue;
560 while (*p && isspace(*p)) p++;
561 if (strncmp( p, "include", 7 )) continue;
562 p += 7;
563 while (*p && isspace(*p)) p++;
564 if (*p == '\\' && p[1] == '"')
566 p += 2;
567 quote = '"';
569 else
571 if (*p++ != '<' ) continue;
572 quote = '>';
574 include = p;
575 while (*p && (*p != quote)) p++;
576 if (!*p || (quote == '"' && p[-1] != '\\'))
577 fatal_error( "%s:%d: Malformed #include directive inside cpp_quote\n",
578 pFile->filename, input_line );
579 if (quote == '"') p--; /* remove backslash */
580 *p = 0;
581 add_include( pFile, include, input_line, (quote == '>') );
582 continue;
585 /* check for normal #include */
586 if (*p++ != '#') continue;
587 while (*p && isspace(*p)) p++;
588 if (strncmp( p, "include", 7 )) continue;
589 p += 7;
590 while (*p && isspace(*p)) p++;
591 if (*p != '\"' && *p != '<' ) continue;
592 quote = *p++;
593 if (quote == '<') quote = '>';
594 include = p;
595 while (*p && (*p != quote)) p++;
596 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n", pFile->filename, input_line );
597 *p = 0;
598 add_include( pFile, include, input_line, (quote == '>') );
602 /*******************************************************************
603 * parse_c_file
605 static void parse_c_file( INCL_FILE *pFile, FILE *file )
607 char *buffer, *include;
609 input_line = 0;
610 while ((buffer = get_line( file )))
612 char quote;
613 char *p = buffer;
614 while (*p && isspace(*p)) p++;
615 if (*p++ != '#') continue;
616 while (*p && isspace(*p)) p++;
617 if (strncmp( p, "include", 7 )) continue;
618 p += 7;
619 while (*p && isspace(*p)) p++;
620 if (*p != '\"' && *p != '<' ) continue;
621 quote = *p++;
622 if (quote == '<') quote = '>';
623 include = p;
624 while (*p && (*p != quote)) p++;
625 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
626 pFile->filename, input_line );
627 *p = 0;
628 add_include( pFile, include, input_line, (quote == '>') );
633 /*******************************************************************
634 * parse_rc_file
636 static void parse_rc_file( INCL_FILE *pFile, FILE *file )
638 char *buffer, *include;
640 input_line = 0;
641 while ((buffer = get_line( file )))
643 char quote;
644 char *p = buffer;
645 while (*p && isspace(*p)) p++;
647 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
649 p += 2;
650 while (*p && isspace(*p)) p++;
651 if (strncmp( p, "@makedep:", 9 )) continue;
652 p += 9;
653 while (*p && isspace(*p)) p++;
654 quote = '"';
655 if (*p == quote)
657 include = ++p;
658 while (*p && *p != quote) p++;
660 else
662 include = p;
663 while (*p && !isspace(*p) && *p != '*') p++;
665 if (!*p)
666 fatal_error( "%s:%d: Malformed makedep comment\n", pFile->filename, input_line );
667 *p = 0;
669 else /* check for #include */
671 if (*p++ != '#') continue;
672 while (*p && isspace(*p)) p++;
673 if (strncmp( p, "include", 7 )) continue;
674 p += 7;
675 while (*p && isspace(*p)) p++;
676 if (*p != '\"' && *p != '<' ) continue;
677 quote = *p++;
678 if (quote == '<') quote = '>';
679 include = p;
680 while (*p && (*p != quote)) p++;
681 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
682 pFile->filename, input_line );
683 *p = 0;
685 add_include( pFile, include, input_line, (quote == '>') );
690 /*******************************************************************
691 * parse_generated_idl
693 static void parse_generated_idl( INCL_FILE *source )
695 char *header, *basename;
697 basename = xstrdup( source->name );
698 basename[strlen(basename) - 4] = 0;
699 header = strmake( "%s.h", basename );
700 source->filename = xstrdup( source->name );
702 if (strendswith( source->name, "_c.c" ))
704 add_include( source, header, 0, 0 );
706 else if (strendswith( source->name, "_i.c" ))
708 add_include( source, "rpc.h", 0, 1 );
709 add_include( source, "rpcndr.h", 0, 1 );
710 add_include( source, "guiddef.h", 0, 1 );
712 else if (strendswith( source->name, "_p.c" ))
714 add_include( source, "objbase.h", 0, 1 );
715 add_include( source, "rpcproxy.h", 0, 1 );
716 add_include( source, "wine/exception.h", 0, 1 );
717 add_include( source, header, 0, 0 );
719 else if (strendswith( source->name, "_s.c" ))
721 add_include( source, "wine/exception.h", 0, 1 );
722 add_include( source, header, 0, 0 );
724 else if (!strcmp( source->name, "dlldata.c" ))
726 add_include( source, "objbase.h", 0, 1 );
727 add_include( source, "rpcproxy.h", 0, 1 );
730 free( header );
731 free( basename );
734 /*******************************************************************
735 * parse_file
737 static void parse_file( INCL_FILE *pFile, int src )
739 FILE *file;
741 /* special case for source files generated from idl */
742 if (strendswith( pFile->name, "_c.c" ) ||
743 strendswith( pFile->name, "_i.c" ) ||
744 strendswith( pFile->name, "_p.c" ) ||
745 strendswith( pFile->name, "_s.c" ) ||
746 !strcmp( pFile->name, "dlldata.c" ))
748 parse_generated_idl( pFile );
749 return;
752 /* don't try to open .tlb files */
753 if (strendswith( pFile->name, ".tlb" ))
755 pFile->filename = xstrdup( pFile->name );
756 return;
759 file = src ? open_src_file( pFile ) : open_include_file( pFile );
760 if (!file) return;
762 if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" ))
763 parse_idl_file( pFile, file, 1 );
764 else if (strendswith( pFile->filename, ".idl" ))
765 parse_idl_file( pFile, file, 0 );
766 else if (strendswith( pFile->filename, ".c" ) ||
767 strendswith( pFile->filename, ".h" ) ||
768 strendswith( pFile->filename, ".l" ) ||
769 strendswith( pFile->filename, ".y" ))
770 parse_c_file( pFile, file );
771 else if (strendswith( pFile->filename, ".rc" ))
772 parse_rc_file( pFile, file );
773 fclose(file);
777 /*******************************************************************
778 * add_src_file
780 * Add a source file to the list.
782 static INCL_FILE *add_src_file( const char *name )
784 INCL_FILE *file;
786 if (find_src_file( name )) return NULL; /* we already have it */
787 file = xmalloc( sizeof(*file) );
788 memset( file, 0, sizeof(*file) );
789 file->name = xstrdup(name);
790 list_add_tail( &sources, &file->entry );
791 parse_file( file, 1 );
792 return file;
796 /*******************************************************************
797 * output_include
799 static void output_include( FILE *file, INCL_FILE *pFile,
800 INCL_FILE *owner, int *column )
802 int i;
804 if (pFile->owner == owner) return;
805 if (!pFile->filename) return;
806 pFile->owner = owner;
807 if (*column + strlen(pFile->filename) + 1 > 70)
809 fprintf( file, " \\\n" );
810 *column = 0;
812 fprintf( file, " %s", pFile->filename );
813 *column += strlen(pFile->filename) + 1;
814 for (i = 0; i < MAX_INCLUDES; i++)
815 if (pFile->files[i]) output_include( file, pFile->files[i],
816 owner, column );
820 /*******************************************************************
821 * output_src
823 static int output_src( FILE *file, INCL_FILE *pFile, int *column )
825 char *obj = xstrdup( pFile->name );
826 char *ext = get_extension( obj );
827 if (ext)
829 *ext++ = 0;
830 if (!strcmp( ext, "y" )) /* yacc file */
832 *column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj );
834 else if (!strcmp( ext, "l" )) /* lex file */
836 *column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj );
838 else if (!strcmp( ext, "rc" )) /* resource file */
840 *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
842 else if (!strcmp( ext, "mc" )) /* message file */
844 *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
846 else if (!strcmp( ext, "idl" )) /* IDL file */
848 char *name;
849 int got_header = 0;
850 const char *suffix = "cips";
852 name = strmake( "%s.tlb", obj );
853 if (find_src_file( name )) *column += fprintf( file, "%s", name );
854 else
856 got_header = 1;
857 *column += fprintf( file, "%s.h", obj );
859 free( name );
861 while (*suffix)
863 name = strmake( "%s_%c.c", obj, *suffix );
864 if (find_src_file( name ))
866 if (!got_header++) *column += fprintf( file, " %s.h", obj );
867 *column += fprintf( file, " %s", name );
869 free( name );
870 suffix++;
872 *column += fprintf( file, ": %s", pFile->filename );
874 else if (!strcmp( ext, "tlb" ))
876 return 0; /* nothing to do for typelib files */
878 else
880 OBJECT_EXTENSION *ext;
881 LIST_FOR_EACH_ENTRY( ext, &object_extensions, OBJECT_EXTENSION, entry )
882 *column += fprintf( file, "%s.%s ", obj, ext->extension );
883 *column += fprintf( file, ": %s", pFile->filename );
886 free( obj );
887 return 1;
891 /*******************************************************************
892 * create_temp_file
894 static FILE *create_temp_file( char **tmp_name )
896 char *name = xmalloc( strlen(OutputFileName) + 13 );
897 unsigned int i, id = getpid();
898 int fd;
899 FILE *ret = NULL;
901 for (i = 0; i < 100; i++)
903 sprintf( name, "%s.tmp%08x", OutputFileName, id );
904 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
906 ret = fdopen( fd, "w" );
907 break;
909 if (errno != EEXIST) break;
910 id += 7777;
912 if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
913 *tmp_name = name;
914 return ret;
918 /*******************************************************************
919 * output_dependencies
921 static void output_dependencies(void)
923 INCL_FILE *pFile;
924 int i, column;
925 FILE *file = NULL;
926 char *tmp_name = NULL;
928 if (Separator && ((file = fopen( OutputFileName, "r" ))))
930 char buffer[1024];
931 FILE *tmp_file = create_temp_file( &tmp_name );
933 while (fgets( buffer, sizeof(buffer), file ))
935 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
936 fatal_error( "error writing to %s\n", tmp_name );
937 if (!strncmp( buffer, Separator, strlen(Separator) )) break;
939 fclose( file );
940 file = tmp_file;
942 else
944 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
946 perror( OutputFileName );
947 exit(1);
950 LIST_FOR_EACH_ENTRY( pFile, &sources, INCL_FILE, entry )
952 column = 0;
953 if (!output_src( file, pFile, &column )) continue;
954 for (i = 0; i < MAX_INCLUDES; i++)
955 if (pFile->files[i]) output_include( file, pFile->files[i],
956 pFile, &column );
957 fprintf( file, "\n" );
959 fclose( file );
961 if (tmp_name)
963 int ret = rename( tmp_name, OutputFileName );
964 if (ret == -1 && errno == EEXIST)
966 /* rename doesn't overwrite on windows */
967 unlink( OutputFileName );
968 ret = rename( tmp_name, OutputFileName );
970 if (ret == -1)
972 unlink( tmp_name );
973 fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
975 free( tmp_name );
980 /*******************************************************************
981 * parse_option
983 static void parse_option( const char *opt )
985 switch(opt[1])
987 case 'I':
988 if (opt[2]) add_include_path( opt + 2 );
989 break;
990 case 'C':
991 src_dir = opt + 2;
992 break;
993 case 'S':
994 top_src_dir = opt + 2;
995 break;
996 case 'T':
997 top_obj_dir = opt + 2;
998 break;
999 case 'f':
1000 if (opt[2]) OutputFileName = opt + 2;
1001 break;
1002 case 's':
1003 if (opt[2]) Separator = opt + 2;
1004 else Separator = NULL;
1005 break;
1006 case 'x':
1007 if (opt[2]) add_object_extension( opt + 2 );
1008 break;
1009 default:
1010 fprintf( stderr, "Unknown option '%s'\n", opt );
1011 fprintf( stderr, Usage, ProgramName );
1012 exit(1);
1017 /*******************************************************************
1018 * main
1020 int main( int argc, char *argv[] )
1022 INCL_FILE *pFile;
1023 INCL_PATH *path, *next;
1024 int i, j;
1026 ProgramName = argv[0];
1028 i = 1;
1029 while (i < argc)
1031 if (argv[i][0] == '-')
1033 parse_option( argv[i] );
1034 for (j = i; j < argc; j++) argv[j] = argv[j+1];
1035 argc--;
1037 else i++;
1040 /* ignore redundant source paths */
1041 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
1042 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
1044 /* set the default extension list for object files */
1045 if (list_empty( &object_extensions ))
1046 add_object_extension( "o" );
1048 /* get rid of absolute paths that don't point into the source dir */
1049 LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, INCL_PATH, entry )
1051 if (path->name[0] != '/') continue;
1052 if (top_src_dir)
1054 if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) ) &&
1055 path->name[strlen(top_src_dir)] == '/') continue;
1057 list_remove( &path->entry );
1058 free( path );
1061 for (i = 1; i < argc; i++)
1063 add_src_file( argv[i] );
1064 if (strendswith( argv[i], "_p.c" )) add_src_file( "dlldata.c" );
1066 LIST_FOR_EACH_ENTRY( pFile, &includes, INCL_FILE, entry ) parse_file( pFile, 0 );
1067 output_dependencies();
1068 return 0;