comctl32/treeview: Use message return values in TreeView tests.
[wine/multimedia.git] / tools / makedep.c
blob4194073890b02741ec7df315ed19647f9ec194b6
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 _INCL_PATH
58 struct list entry;
59 const char *name;
60 } INCL_PATH;
62 static struct list paths = LIST_INIT(paths);
64 static const char *src_dir;
65 static const char *top_src_dir;
66 static const char *top_obj_dir;
67 static const char *OutputFileName = "Makefile";
68 static const char *Separator = "### Dependencies";
69 static const char *ProgramName;
70 static int input_line;
72 static const char Usage[] =
73 "Usage: %s [options] [files]\n"
74 "Options:\n"
75 " -Idir Search for include files in directory 'dir'\n"
76 " -Cdir Search for source files in directory 'dir'\n"
77 " -Sdir Set the top source directory\n"
78 " -Tdir Set the top object directory\n"
79 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
80 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
83 /*******************************************************************
84 * fatal_error
86 static void fatal_error( const char *msg, ... )
88 va_list valist;
89 va_start( valist, msg );
90 vfprintf( stderr, msg, valist );
91 va_end( valist );
92 exit(1);
96 /*******************************************************************
97 * xmalloc
99 static void *xmalloc( size_t size )
101 void *res;
102 if (!(res = malloc (size ? size : 1)))
103 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
104 return res;
108 /*******************************************************************
109 * xrealloc
111 static void *xrealloc (void *ptr, size_t size)
113 void *res;
114 assert( size );
115 if (!(res = realloc( ptr, size )))
116 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
117 return res;
120 /*******************************************************************
121 * xstrdup
123 static char *xstrdup( const char *str )
125 char *res = strdup( str );
126 if (!res) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
127 return res;
131 /*******************************************************************
132 * strmake
134 static char *strmake( const char* fmt, ... )
136 int n;
137 size_t size = 100;
138 va_list ap;
140 for (;;)
142 char *p = xmalloc (size);
143 va_start(ap, fmt);
144 n = vsnprintf (p, size, fmt, ap);
145 va_end(ap);
146 if (n == -1) size *= 2;
147 else if ((size_t)n >= size) size = n + 1;
148 else return p;
149 free(p);
154 /*******************************************************************
155 * strendswith
157 static int strendswith( const char* str, const char* end )
159 int l = strlen(str);
160 int m = strlen(end);
162 return l >= m && strcmp(str + l - m, end) == 0;
165 /*******************************************************************
166 * get_extension
168 static char *get_extension( char *filename )
170 char *ext = strrchr( filename, '.' );
171 if (ext && strchr( ext, '/' )) ext = NULL;
172 return ext;
176 /*******************************************************************
177 * get_line
179 static char *get_line( FILE *file )
181 static char *buffer;
182 static unsigned int size;
184 if (!size)
186 size = 1024;
187 buffer = xmalloc( size );
189 if (!fgets( buffer, size, file )) return NULL;
190 input_line++;
192 for (;;)
194 char *p = buffer + strlen(buffer);
195 /* if line is larger than buffer, resize buffer */
196 while (p == buffer + size - 1 && p[-1] != '\n')
198 buffer = xrealloc( buffer, size * 2 );
199 fgets( buffer + size - 1, size + 1, file );
200 p = buffer + strlen(buffer);
201 size *= 2;
203 if (p > buffer && p[-1] == '\n')
205 *(--p) = 0;
206 if (p > buffer && p[-1] == '\r') *(--p) = 0;
207 if (p > buffer && p[-1] == '\\')
209 *(--p) = 0;
210 /* line ends in backslash, read continuation line */
211 fgets( p, size - (p - buffer), file );
212 input_line++;
213 continue;
216 return buffer;
220 /*******************************************************************
221 * add_include_path
223 * Add a directory to the include path.
225 static void add_include_path( const char *name )
227 INCL_PATH *path = xmalloc( sizeof(*path) );
228 list_add_tail( &paths, &path->entry );
229 path->name = name;
232 /*******************************************************************
233 * find_src_file
235 static INCL_FILE *find_src_file( const char *name )
237 INCL_FILE *file;
239 LIST_FOR_EACH_ENTRY( file, &sources, INCL_FILE, entry )
240 if (!strcmp( name, file->name )) return file;
241 return NULL;
244 /*******************************************************************
245 * add_include
247 * Add an include file if it doesn't already exists.
249 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
251 INCL_FILE *include;
252 char *ext;
253 int pos;
255 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
256 if (pos >= MAX_INCLUDES)
257 fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
258 ProgramName, pFile->name );
260 /* enforce some rules for the Wine tree */
262 if (!memcmp( name, "../", 3 ))
263 fatal_error( "%s:%d: #include directive with relative path not allowed\n",
264 pFile->filename, line );
266 if (!strcmp( name, "config.h" ))
268 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
269 fatal_error( "%s:%d: config.h must not be included by a header file\n",
270 pFile->filename, line );
271 if (pos)
272 fatal_error( "%s:%d: config.h must be included before anything else\n",
273 pFile->filename, line );
275 else if (!strcmp( name, "wine/port.h" ))
277 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
278 fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
279 pFile->filename, line );
280 if (!pos) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
281 pFile->filename, line );
282 if (pos > 1)
283 fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
284 pFile->filename, line );
285 if (strcmp( pFile->files[0]->name, "config.h" ))
286 fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
287 pFile->filename, line );
290 LIST_FOR_EACH_ENTRY( include, &includes, INCL_FILE, entry )
291 if (!strcmp( name, include->name )) goto found;
293 include = xmalloc( sizeof(INCL_FILE) );
294 memset( include, 0, sizeof(INCL_FILE) );
295 include->name = xstrdup(name);
296 include->included_by = pFile;
297 include->included_line = line;
298 include->system = system;
299 list_add_tail( &includes, &include->entry );
300 found:
301 pFile->files[pos] = include;
302 return include;
306 /*******************************************************************
307 * open_src_file
309 static FILE *open_src_file( INCL_FILE *pFile )
311 FILE *file;
313 /* first try name as is */
314 if ((file = fopen( pFile->name, "r" )))
316 pFile->filename = xstrdup( pFile->name );
317 return file;
319 /* now try in source dir */
320 if (src_dir)
322 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
323 file = fopen( pFile->filename, "r" );
325 if (!file)
327 perror( pFile->name );
328 exit(1);
330 return file;
334 /*******************************************************************
335 * open_include_file
337 static FILE *open_include_file( INCL_FILE *pFile )
339 FILE *file = NULL;
340 char *filename, *p;
341 INCL_PATH *path;
343 errno = ENOENT;
345 /* check for generated bison header */
347 if (strendswith( pFile->name, ".tab.h" ))
349 if (src_dir)
350 filename = strmake( "%s/%.*s.y", src_dir, strlen(pFile->name) - 6, pFile->name );
351 else
352 filename = strmake( "%.*s.y", strlen(pFile->name) - 6, pFile->name );
354 if ((file = fopen( filename, "r" )))
356 pFile->sourcename = filename;
357 pFile->filename = xstrdup( pFile->name );
358 /* don't bother to parse it */
359 fclose( file );
360 return NULL;
362 free( filename );
365 /* check for generated message resource */
367 if (strendswith( pFile->name, ".mc.rc" ))
369 if (src_dir)
370 filename = strmake( "%s/%s", src_dir, pFile->name );
371 else
372 filename = xstrdup( pFile->name );
374 filename[strlen(filename) - 3] = 0;
376 if ((file = fopen( filename, "r" )))
378 pFile->sourcename = filename;
379 pFile->filename = xstrdup( pFile->name );
380 /* don't bother to parse it */
381 fclose( file );
382 return NULL;
384 free( filename );
387 /* check for corresponding idl file in source dir */
389 if (strendswith( pFile->name, ".h" ))
391 if (src_dir)
392 filename = strmake( "%s/%.*s.idl", src_dir, strlen(pFile->name) - 2, pFile->name );
393 else
394 filename = strmake( "%.*s.idl", strlen(pFile->name) - 2, pFile->name );
396 if ((file = fopen( filename, "r" )))
398 pFile->sourcename = filename;
399 pFile->filename = xstrdup( pFile->name );
400 return file;
402 free( filename );
405 /* first try name as is */
406 if ((file = fopen( pFile->name, "r" )))
408 pFile->filename = xstrdup( pFile->name );
409 return file;
412 /* now try in source dir */
413 if (src_dir)
415 filename = strmake( "%s/%s", src_dir, pFile->name );
416 if ((file = fopen( filename, "r" ))) goto found;
417 free( filename );
420 /* check for corresponding idl file in global includes */
422 if (strendswith( pFile->name, ".h" ))
424 if (top_src_dir)
425 filename = strmake( "%s/include/%.*s.idl",
426 top_src_dir, strlen(pFile->name) - 2, pFile->name );
427 else if (top_obj_dir)
428 filename = strmake( "%s/include/%.*s.idl",
429 top_obj_dir, strlen(pFile->name) - 2, pFile->name );
430 else
431 filename = NULL;
433 if (filename && (file = fopen( filename, "r" )))
435 pFile->sourcename = filename;
436 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
437 return file;
439 free( filename );
442 /* now try in global includes */
443 if (top_obj_dir)
445 filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
446 if ((file = fopen( filename, "r" ))) goto found;
447 free( filename );
449 if (top_src_dir)
451 filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
452 if ((file = fopen( filename, "r" ))) goto found;
453 free( filename );
456 /* now search in include paths */
457 LIST_FOR_EACH_ENTRY( path, &paths, INCL_PATH, entry )
459 filename = strmake( "%s/%s", path->name, pFile->name );
460 if ((file = fopen( filename, "r" ))) goto found;
461 free( filename );
463 if (pFile->system) return NULL; /* ignore system files we cannot find */
465 /* try in src file directory */
466 if ((p = strrchr(pFile->included_by->filename, '/')))
468 int l = p - pFile->included_by->filename + 1;
469 filename = xmalloc(l + strlen(pFile->name) + 1);
470 memcpy( filename, pFile->included_by->filename, l );
471 strcpy( filename + l, pFile->name );
472 if ((file = fopen( filename, "r" ))) goto found;
473 free( filename );
476 perror( pFile->name );
477 while (pFile->included_by)
479 const char *parent = pFile->included_by->sourcename;
480 if (!parent) parent = pFile->included_by->name;
481 fprintf( stderr, " %s was first included from %s:%d\n",
482 pFile->name, parent, pFile->included_line );
483 pFile = pFile->included_by;
485 exit(1);
487 found:
488 pFile->filename = filename;
489 return file;
493 /*******************************************************************
494 * parse_idl_file
496 * If for_h_file is non-zero, it means we are not interested in the idl file
497 * itself, but only in the contents of the .h file that will be generated from it.
499 static void parse_idl_file( INCL_FILE *pFile, FILE *file, int for_h_file )
501 char *buffer, *include;
503 if (for_h_file)
505 /* generated .h file always includes these */
506 add_include( pFile, "rpc.h", 0, 1 );
507 add_include( pFile, "rpcndr.h", 0, 1 );
510 input_line = 0;
511 while ((buffer = get_line( file )))
513 char quote;
514 char *p = buffer;
515 while (*p && isspace(*p)) p++;
517 if (!strncmp( p, "import", 6 ))
519 p += 6;
520 while (*p && isspace(*p)) p++;
521 if (*p != '"') continue;
522 include = ++p;
523 while (*p && (*p != '"')) p++;
524 if (!*p) fatal_error( "%s:%d: Malformed import directive\n", pFile->filename, input_line );
525 *p = 0;
526 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
527 add_include( pFile, include, input_line, 0 );
528 continue;
531 if (for_h_file) /* only check for #include inside cpp_quote */
533 if (strncmp( p, "cpp_quote", 9 )) continue;
534 p += 9;
535 while (*p && isspace(*p)) p++;
536 if (*p++ != '(') continue;
537 while (*p && isspace(*p)) p++;
538 if (*p++ != '"') continue;
539 if (*p++ != '#') continue;
540 while (*p && isspace(*p)) p++;
541 if (strncmp( p, "include", 7 )) continue;
542 p += 7;
543 while (*p && isspace(*p)) p++;
544 if (*p == '\\' && p[1] == '"')
546 p += 2;
547 quote = '"';
549 else
551 if (*p++ != '<' ) continue;
552 quote = '>';
554 include = p;
555 while (*p && (*p != quote)) p++;
556 if (!*p || (quote == '"' && p[-1] != '\\'))
557 fatal_error( "%s:%d: Malformed #include directive inside cpp_quote\n",
558 pFile->filename, input_line );
559 if (quote == '"') p--; /* remove backslash */
560 *p = 0;
561 add_include( pFile, include, input_line, (quote == '>') );
562 continue;
565 /* check for normal #include */
566 if (*p++ != '#') continue;
567 while (*p && isspace(*p)) p++;
568 if (strncmp( p, "include", 7 )) continue;
569 p += 7;
570 while (*p && isspace(*p)) p++;
571 if (*p != '\"' && *p != '<' ) continue;
572 quote = *p++;
573 if (quote == '<') quote = '>';
574 include = p;
575 while (*p && (*p != quote)) p++;
576 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n", pFile->filename, input_line );
577 *p = 0;
578 add_include( pFile, include, input_line, (quote == '>') );
582 /*******************************************************************
583 * parse_c_file
585 static void parse_c_file( INCL_FILE *pFile, FILE *file )
587 char *buffer, *include;
589 input_line = 0;
590 while ((buffer = get_line( file )))
592 char quote;
593 char *p = buffer;
594 while (*p && isspace(*p)) p++;
595 if (*p++ != '#') continue;
596 while (*p && isspace(*p)) p++;
597 if (strncmp( p, "include", 7 )) continue;
598 p += 7;
599 while (*p && isspace(*p)) p++;
600 if (*p != '\"' && *p != '<' ) continue;
601 quote = *p++;
602 if (quote == '<') quote = '>';
603 include = p;
604 while (*p && (*p != quote)) p++;
605 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
606 pFile->filename, input_line );
607 *p = 0;
608 add_include( pFile, include, input_line, (quote == '>') );
613 /*******************************************************************
614 * parse_rc_file
616 static void parse_rc_file( INCL_FILE *pFile, FILE *file )
618 char *buffer, *include;
620 input_line = 0;
621 while ((buffer = get_line( file )))
623 char quote;
624 char *p = buffer;
625 while (*p && isspace(*p)) p++;
627 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
629 p += 2;
630 while (*p && isspace(*p)) p++;
631 if (strncmp( p, "@makedep:", 9 )) continue;
632 p += 9;
633 while (*p && isspace(*p)) p++;
634 quote = '"';
635 if (*p == quote)
637 include = ++p;
638 while (*p && *p != quote) p++;
640 else
642 include = p;
643 while (*p && !isspace(*p) && *p != '*') p++;
645 if (!*p)
646 fatal_error( "%s:%d: Malformed makedep comment\n", pFile->filename, input_line );
647 *p = 0;
649 else /* check for #include */
651 if (*p++ != '#') continue;
652 while (*p && isspace(*p)) p++;
653 if (strncmp( p, "include", 7 )) continue;
654 p += 7;
655 while (*p && isspace(*p)) p++;
656 if (*p != '\"' && *p != '<' ) continue;
657 quote = *p++;
658 if (quote == '<') quote = '>';
659 include = p;
660 while (*p && (*p != quote)) p++;
661 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
662 pFile->filename, input_line );
663 *p = 0;
665 add_include( pFile, include, input_line, (quote == '>') );
670 /*******************************************************************
671 * parse_generated_idl
673 static void parse_generated_idl( INCL_FILE *source )
675 char *header, *basename;
677 basename = xstrdup( source->name );
678 basename[strlen(basename) - 4] = 0;
679 header = strmake( "%s.h", basename );
680 source->filename = xstrdup( source->name );
682 if (strendswith( source->name, "_c.c" ))
684 add_include( source, header, 0, 0 );
686 else if (strendswith( source->name, "_i.c" ))
688 add_include( source, "rpc.h", 0, 1 );
689 add_include( source, "rpcndr.h", 0, 1 );
690 add_include( source, "guiddef.h", 0, 1 );
692 else if (strendswith( source->name, "_p.c" ))
694 add_include( source, "objbase.h", 0, 1 );
695 add_include( source, "rpcproxy.h", 0, 1 );
696 add_include( source, "wine/exception.h", 0, 1 );
697 add_include( source, header, 0, 0 );
699 else if (strendswith( source->name, "_s.c" ))
701 add_include( source, "wine/exception.h", 0, 1 );
702 add_include( source, header, 0, 0 );
704 else if (!strcmp( source->name, "dlldata.c" ))
706 add_include( source, "objbase.h", 0, 1 );
707 add_include( source, "rpcproxy.h", 0, 1 );
710 free( header );
711 free( basename );
714 /*******************************************************************
715 * parse_file
717 static void parse_file( INCL_FILE *pFile, int src )
719 FILE *file;
721 /* special case for source files generated from idl */
722 if (strendswith( pFile->name, "_c.c" ) ||
723 strendswith( pFile->name, "_i.c" ) ||
724 strendswith( pFile->name, "_p.c" ) ||
725 strendswith( pFile->name, "_s.c" ) ||
726 !strcmp( pFile->name, "dlldata.c" ))
728 parse_generated_idl( pFile );
729 return;
732 /* don't try to open .tlb files */
733 if (strendswith( pFile->name, ".tlb" ))
735 pFile->filename = xstrdup( pFile->name );
736 return;
739 file = src ? open_src_file( pFile ) : open_include_file( pFile );
740 if (!file) return;
742 if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" ))
743 parse_idl_file( pFile, file, 1 );
744 else if (strendswith( pFile->filename, ".idl" ))
745 parse_idl_file( pFile, file, 0 );
746 else if (strendswith( pFile->filename, ".c" ) ||
747 strendswith( pFile->filename, ".h" ) ||
748 strendswith( pFile->filename, ".l" ) ||
749 strendswith( pFile->filename, ".y" ))
750 parse_c_file( pFile, file );
751 else if (strendswith( pFile->filename, ".rc" ))
752 parse_rc_file( pFile, file );
753 fclose(file);
757 /*******************************************************************
758 * add_src_file
760 * Add a source file to the list.
762 static INCL_FILE *add_src_file( const char *name )
764 INCL_FILE *file;
766 if (find_src_file( name )) return NULL; /* we already have it */
767 file = xmalloc( sizeof(*file) );
768 memset( file, 0, sizeof(*file) );
769 file->name = xstrdup(name);
770 list_add_tail( &sources, &file->entry );
771 parse_file( file, 1 );
772 return file;
776 /*******************************************************************
777 * output_include
779 static void output_include( FILE *file, INCL_FILE *pFile,
780 INCL_FILE *owner, int *column )
782 int i;
784 if (pFile->owner == owner) return;
785 if (!pFile->filename) return;
786 pFile->owner = owner;
787 if (*column + strlen(pFile->filename) + 1 > 70)
789 fprintf( file, " \\\n" );
790 *column = 0;
792 fprintf( file, " %s", pFile->filename );
793 *column += strlen(pFile->filename) + 1;
794 for (i = 0; i < MAX_INCLUDES; i++)
795 if (pFile->files[i]) output_include( file, pFile->files[i],
796 owner, column );
800 /*******************************************************************
801 * output_src
803 static int output_src( FILE *file, INCL_FILE *pFile, int *column )
805 char *obj = xstrdup( pFile->name );
806 char *ext = get_extension( obj );
807 if (ext)
809 *ext++ = 0;
810 if (!strcmp( ext, "y" )) /* yacc file */
812 *column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj );
814 else if (!strcmp( ext, "l" )) /* lex file */
816 *column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj );
818 else if (!strcmp( ext, "rc" )) /* resource file */
820 *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
822 else if (!strcmp( ext, "mc" )) /* message file */
824 *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
826 else if (!strcmp( ext, "idl" )) /* IDL file */
828 char *name;
829 int got_header = 0;
830 const char *suffix = "cips";
832 name = strmake( "%s.tlb", obj );
833 if (find_src_file( name )) *column += fprintf( file, "%s", name );
834 else
836 got_header = 1;
837 *column += fprintf( file, "%s.h", obj );
839 free( name );
841 while (*suffix)
843 name = strmake( "%s_%c.c", obj, *suffix );
844 if (find_src_file( name ))
846 if (!got_header++) *column += fprintf( file, " %s.h", obj );
847 *column += fprintf( file, " %s", name );
849 free( name );
850 suffix++;
852 *column += fprintf( file, ": %s", pFile->filename );
854 else if (!strcmp( ext, "tlb" ))
856 return 0; /* nothing to do for typelib files */
858 else
860 *column += fprintf( file, "%s.o: %s", obj, pFile->filename );
863 free( obj );
864 return 1;
868 /*******************************************************************
869 * create_temp_file
871 static FILE *create_temp_file( char **tmp_name )
873 char *name = xmalloc( strlen(OutputFileName) + 13 );
874 unsigned int i, id = getpid();
875 int fd;
876 FILE *ret = NULL;
878 for (i = 0; i < 100; i++)
880 sprintf( name, "%s.tmp%08x", OutputFileName, id );
881 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
883 ret = fdopen( fd, "w" );
884 break;
886 if (errno != EEXIST) break;
887 id += 7777;
889 if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
890 *tmp_name = name;
891 return ret;
895 /*******************************************************************
896 * output_dependencies
898 static void output_dependencies(void)
900 INCL_FILE *pFile;
901 int i, column;
902 FILE *file = NULL;
903 char *tmp_name = NULL;
905 if (Separator && ((file = fopen( OutputFileName, "r" ))))
907 char buffer[1024];
908 FILE *tmp_file = create_temp_file( &tmp_name );
910 while (fgets( buffer, sizeof(buffer), file ))
912 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
913 fatal_error( "error writing to %s\n", tmp_name );
914 if (!strncmp( buffer, Separator, strlen(Separator) )) break;
916 fclose( file );
917 file = tmp_file;
919 else
921 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
923 perror( OutputFileName );
924 exit(1);
927 LIST_FOR_EACH_ENTRY( pFile, &sources, INCL_FILE, entry )
929 column = 0;
930 if (!output_src( file, pFile, &column )) continue;
931 for (i = 0; i < MAX_INCLUDES; i++)
932 if (pFile->files[i]) output_include( file, pFile->files[i],
933 pFile, &column );
934 fprintf( file, "\n" );
936 fclose( file );
938 if (tmp_name)
940 if (rename( tmp_name, OutputFileName ) == -1)
942 unlink( tmp_name );
943 fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
945 free( tmp_name );
950 /*******************************************************************
951 * parse_option
953 static void parse_option( const char *opt )
955 switch(opt[1])
957 case 'I':
958 if (opt[2]) add_include_path( opt + 2 );
959 break;
960 case 'C':
961 src_dir = opt + 2;
962 break;
963 case 'S':
964 top_src_dir = opt + 2;
965 break;
966 case 'T':
967 top_obj_dir = opt + 2;
968 break;
969 case 'f':
970 if (opt[2]) OutputFileName = opt + 2;
971 break;
972 case 's':
973 if (opt[2]) Separator = opt + 2;
974 else Separator = NULL;
975 break;
976 default:
977 fprintf( stderr, "Unknown option '%s'\n", opt );
978 fprintf( stderr, Usage, ProgramName );
979 exit(1);
984 /*******************************************************************
985 * main
987 int main( int argc, char *argv[] )
989 INCL_FILE *pFile;
990 INCL_PATH *path, *next;
991 int i, j;
993 ProgramName = argv[0];
995 i = 1;
996 while (i < argc)
998 if (argv[i][0] == '-')
1000 parse_option( argv[i] );
1001 for (j = i; j < argc; j++) argv[j] = argv[j+1];
1002 argc--;
1004 else i++;
1007 /* ignore redundant source paths */
1008 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
1009 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
1011 /* get rid of absolute paths that don't point into the source dir */
1012 LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, INCL_PATH, entry )
1014 if (path->name[0] != '/') continue;
1015 if (top_src_dir)
1017 if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) ) &&
1018 path->name[strlen(top_src_dir)] == '/') continue;
1020 list_remove( &path->entry );
1021 free( path );
1024 for (i = 1; i < argc; i++)
1026 add_src_file( argv[i] );
1027 if (strendswith( argv[i], "_p.c" )) add_src_file( "dlldata.c" );
1029 LIST_FOR_EACH_ENTRY( pFile, &includes, INCL_FILE, entry ) parse_file( pFile, 0 );
1030 output_dependencies();
1031 return 0;