rpcrt4: Accept HTTP idle packets while waiting for the second prepare header.
[wine.git] / tools / makedep.c
blobc70b5a90174422cbd147925fae759662bcb2716c
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: error: 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: error: 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: error: 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 if (!fgets( buffer + size - 1, size + 1, file )) break;
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 if (!fgets( p, size - (p - buffer), file )) return buffer;
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 * find_include_file
267 static INCL_FILE *find_include_file( const char *name )
269 INCL_FILE *file;
271 LIST_FOR_EACH_ENTRY( file, &includes, INCL_FILE, entry )
272 if (!strcmp( name, file->name )) return file;
273 return NULL;
276 /*******************************************************************
277 * add_include
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 )
283 INCL_FILE *include;
284 char *ext;
285 int pos;
287 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
288 if (pos >= MAX_INCLUDES)
289 fatal_error( "%s: %s: error: 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: error: #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: error: config.h must not be included by a header file\n",
302 pFile->filename, line );
303 if (pos)
304 fatal_error( "%s:%d: error: 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: error: wine/port.h must not be included by a header file\n",
311 pFile->filename, line );
312 if (!pos) fatal_error( "%s:%d: error: config.h must be included before wine/port.h\n",
313 pFile->filename, line );
314 if (pos > 1)
315 fatal_error( "%s:%d: error: 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: error: 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 );
332 found:
333 pFile->files[pos] = include;
334 return include;
338 /*******************************************************************
339 * open_src_file
341 static FILE *open_src_file( INCL_FILE *pFile )
343 FILE *file;
345 /* first try name as is */
346 if ((file = fopen( pFile->name, "r" )))
348 pFile->filename = xstrdup( pFile->name );
349 return file;
351 /* now try in source dir */
352 if (src_dir)
354 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
355 file = fopen( pFile->filename, "r" );
357 if (!file)
359 fprintf( stderr, "%s: error: ", ProgramName );
360 perror( pFile->name );
361 exit(1);
363 return file;
367 /*******************************************************************
368 * open_include_file
370 static FILE *open_include_file( INCL_FILE *pFile )
372 FILE *file = NULL;
373 char *filename, *p;
374 INCL_PATH *path;
376 errno = ENOENT;
378 /* check for generated bison header */
380 if (strendswith( pFile->name, ".tab.h" ))
382 if (src_dir)
383 filename = strmake( "%s/%.*s.y", src_dir, strlen(pFile->name) - 6, pFile->name );
384 else
385 filename = strmake( "%.*s.y", strlen(pFile->name) - 6, pFile->name );
387 if ((file = fopen( filename, "r" )))
389 pFile->sourcename = filename;
390 pFile->filename = xstrdup( pFile->name );
391 /* don't bother to parse it */
392 fclose( file );
393 return NULL;
395 free( filename );
398 /* check for generated message resource */
400 if (strendswith( pFile->name, ".mc.rc" ))
402 if (src_dir)
403 filename = strmake( "%s/%s", src_dir, pFile->name );
404 else
405 filename = xstrdup( pFile->name );
407 filename[strlen(filename) - 3] = 0;
409 if ((file = fopen( filename, "r" )))
411 pFile->sourcename = filename;
412 pFile->filename = xstrdup( pFile->name );
413 /* don't bother to parse it */
414 fclose( file );
415 return NULL;
417 free( filename );
420 /* check for corresponding idl file in source dir */
422 if (strendswith( pFile->name, ".h" ))
424 if (src_dir)
425 filename = strmake( "%s/%.*s.idl", src_dir, strlen(pFile->name) - 2, pFile->name );
426 else
427 filename = strmake( "%.*s.idl", strlen(pFile->name) - 2, pFile->name );
429 if ((file = fopen( filename, "r" )))
431 pFile->sourcename = filename;
432 pFile->filename = xstrdup( pFile->name );
433 return file;
435 free( filename );
438 /* first try name as is */
439 if ((file = fopen( pFile->name, "r" )))
441 pFile->filename = xstrdup( pFile->name );
442 return file;
445 /* now try in source dir */
446 if (src_dir)
448 filename = strmake( "%s/%s", src_dir, pFile->name );
449 if ((file = fopen( filename, "r" ))) goto found;
450 free( filename );
453 /* check for corresponding idl file in global includes */
455 if (strendswith( pFile->name, ".h" ))
457 if (top_src_dir)
458 filename = strmake( "%s/include/%.*s.idl",
459 top_src_dir, strlen(pFile->name) - 2, pFile->name );
460 else if (top_obj_dir)
461 filename = strmake( "%s/include/%.*s.idl",
462 top_obj_dir, strlen(pFile->name) - 2, pFile->name );
463 else
464 filename = NULL;
466 if (filename && (file = fopen( filename, "r" )))
468 pFile->sourcename = filename;
469 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
470 return file;
472 free( filename );
475 /* check for corresponding .x file in global includes */
477 if (strendswith( pFile->name, "tmpl.h" ))
479 if (top_src_dir)
480 filename = strmake( "%s/include/%.*s.x",
481 top_src_dir, strlen(pFile->name) - 2, pFile->name );
482 else if (top_obj_dir)
483 filename = strmake( "%s/include/%.*s.x",
484 top_obj_dir, strlen(pFile->name) - 2, pFile->name );
485 else
486 filename = NULL;
488 if (filename && (file = fopen( filename, "r" )))
490 pFile->sourcename = filename;
491 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
492 return file;
494 free( filename );
497 /* now try in global includes */
498 if (top_obj_dir)
500 filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
501 if ((file = fopen( filename, "r" ))) goto found;
502 free( filename );
504 if (top_src_dir)
506 filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
507 if ((file = fopen( filename, "r" ))) goto found;
508 free( filename );
511 /* now search in include paths */
512 LIST_FOR_EACH_ENTRY( path, &paths, INCL_PATH, entry )
514 filename = strmake( "%s/%s", path->name, pFile->name );
515 if ((file = fopen( filename, "r" ))) goto found;
516 free( filename );
518 if (pFile->system) return NULL; /* ignore system files we cannot find */
520 /* try in src file directory */
521 if ((p = strrchr(pFile->included_by->filename, '/')))
523 int l = p - pFile->included_by->filename + 1;
524 filename = xmalloc(l + strlen(pFile->name) + 1);
525 memcpy( filename, pFile->included_by->filename, l );
526 strcpy( filename + l, pFile->name );
527 if ((file = fopen( filename, "r" ))) goto found;
528 free( filename );
531 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
532 perror( pFile->name );
533 pFile = pFile->included_by;
534 while (pFile && pFile->included_by)
536 const char *parent = pFile->included_by->sourcename;
537 if (!parent) parent = pFile->included_by->name;
538 fprintf( stderr, "%s:%d: note: %s was first included here\n",
539 parent, pFile->included_line, pFile->name );
540 pFile = pFile->included_by;
542 exit(1);
544 found:
545 pFile->filename = filename;
546 return file;
550 /*******************************************************************
551 * parse_idl_file
553 * If for_h_file is non-zero, it means we are not interested in the idl file
554 * itself, but only in the contents of the .h file that will be generated from it.
556 static void parse_idl_file( INCL_FILE *pFile, FILE *file, int for_h_file )
558 char *buffer, *include;
560 if (for_h_file)
562 /* generated .h file always includes these */
563 add_include( pFile, "rpc.h", 0, 1 );
564 add_include( pFile, "rpcndr.h", 0, 1 );
567 input_line = 0;
568 while ((buffer = get_line( file )))
570 char quote;
571 char *p = buffer;
572 while (*p && isspace(*p)) p++;
574 if (!strncmp( p, "import", 6 ))
576 p += 6;
577 while (*p && isspace(*p)) p++;
578 if (*p != '"') continue;
579 include = ++p;
580 while (*p && (*p != '"')) p++;
581 if (!*p) fatal_error( "%s:%d: error: Malformed import directive\n",
582 pFile->filename, input_line );
583 *p = 0;
584 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
585 add_include( pFile, include, input_line, 0 );
586 continue;
589 if (for_h_file) /* only check for #include inside cpp_quote */
591 if (strncmp( p, "cpp_quote", 9 )) continue;
592 p += 9;
593 while (*p && isspace(*p)) p++;
594 if (*p++ != '(') continue;
595 while (*p && isspace(*p)) p++;
596 if (*p++ != '"') continue;
597 if (*p++ != '#') continue;
598 while (*p && isspace(*p)) p++;
599 if (strncmp( p, "include", 7 )) continue;
600 p += 7;
601 while (*p && isspace(*p)) p++;
602 if (*p == '\\' && p[1] == '"')
604 p += 2;
605 quote = '"';
607 else
609 if (*p++ != '<' ) continue;
610 quote = '>';
612 include = p;
613 while (*p && (*p != quote)) p++;
614 if (!*p || (quote == '"' && p[-1] != '\\'))
615 fatal_error( "%s:%d: error: Malformed #include directive inside cpp_quote\n",
616 pFile->filename, input_line );
617 if (quote == '"') p--; /* remove backslash */
618 *p = 0;
619 add_include( pFile, include, input_line, (quote == '>') );
620 continue;
623 /* check for normal #include */
624 if (*p++ != '#') continue;
625 while (*p && isspace(*p)) p++;
626 if (strncmp( p, "include", 7 )) continue;
627 p += 7;
628 while (*p && isspace(*p)) p++;
629 if (*p != '\"' && *p != '<' ) continue;
630 quote = *p++;
631 if (quote == '<') quote = '>';
632 include = p;
633 while (*p && (*p != quote)) p++;
634 if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n",
635 pFile->filename, input_line );
636 *p = 0;
637 add_include( pFile, include, input_line, (quote == '>') );
641 /*******************************************************************
642 * parse_c_file
644 static void parse_c_file( INCL_FILE *pFile, FILE *file )
646 char *buffer, *include;
648 input_line = 0;
649 while ((buffer = get_line( file )))
651 char quote;
652 char *p = buffer;
653 while (*p && isspace(*p)) p++;
654 if (*p++ != '#') continue;
655 while (*p && isspace(*p)) p++;
656 if (!strncmp( p, "include", 7 )) p += 7;
657 else if (!strncmp( p, "import", 6 )) p += 6;
658 else continue;
659 while (*p && isspace(*p)) p++;
660 if (*p != '\"' && *p != '<' ) continue;
661 quote = *p++;
662 if (quote == '<') quote = '>';
663 include = p;
664 while (*p && (*p != quote)) p++;
665 if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n",
666 pFile->filename, input_line );
667 *p = 0;
668 add_include( pFile, include, input_line, (quote == '>') );
673 /*******************************************************************
674 * parse_rc_file
676 static void parse_rc_file( INCL_FILE *pFile, FILE *file )
678 char *buffer, *include;
680 input_line = 0;
681 while ((buffer = get_line( file )))
683 char quote;
684 char *p = buffer;
685 while (*p && isspace(*p)) p++;
687 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
689 p += 2;
690 while (*p && isspace(*p)) p++;
691 if (strncmp( p, "@makedep:", 9 )) continue;
692 p += 9;
693 while (*p && isspace(*p)) p++;
694 quote = '"';
695 if (*p == quote)
697 include = ++p;
698 while (*p && *p != quote) p++;
700 else
702 include = p;
703 while (*p && !isspace(*p) && *p != '*') p++;
705 if (!*p)
706 fatal_error( "%s:%d: error: Malformed makedep comment\n", pFile->filename, input_line );
707 *p = 0;
709 else /* check for #include */
711 if (*p++ != '#') continue;
712 while (*p && isspace(*p)) p++;
713 if (strncmp( p, "include", 7 )) continue;
714 p += 7;
715 while (*p && isspace(*p)) p++;
716 if (*p != '\"' && *p != '<' ) continue;
717 quote = *p++;
718 if (quote == '<') quote = '>';
719 include = p;
720 while (*p && (*p != quote)) p++;
721 if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n",
722 pFile->filename, input_line );
723 *p = 0;
725 add_include( pFile, include, input_line, (quote == '>') );
730 /*******************************************************************
731 * parse_generated_idl
733 static void parse_generated_idl( INCL_FILE *source )
735 char *header, *basename;
737 basename = xstrdup( source->name );
738 basename[strlen(basename) - 4] = 0;
739 header = strmake( "%s.h", basename );
740 source->filename = xstrdup( source->name );
742 if (strendswith( source->name, "_c.c" ))
744 add_include( source, header, 0, 0 );
746 else if (strendswith( source->name, "_i.c" ))
748 add_include( source, "rpc.h", 0, 1 );
749 add_include( source, "rpcndr.h", 0, 1 );
750 add_include( source, "guiddef.h", 0, 1 );
752 else if (strendswith( source->name, "_p.c" ))
754 add_include( source, "objbase.h", 0, 1 );
755 add_include( source, "rpcproxy.h", 0, 1 );
756 add_include( source, "wine/exception.h", 0, 1 );
757 add_include( source, header, 0, 0 );
759 else if (strendswith( source->name, "_s.c" ))
761 add_include( source, "wine/exception.h", 0, 1 );
762 add_include( source, header, 0, 0 );
764 else if (!strcmp( source->name, "dlldata.c" ))
766 add_include( source, "objbase.h", 0, 1 );
767 add_include( source, "rpcproxy.h", 0, 1 );
770 free( header );
771 free( basename );
774 /*******************************************************************
775 * parse_file
777 static void parse_file( INCL_FILE *pFile, int src )
779 FILE *file;
781 /* special case for source files generated from idl */
782 if (strendswith( pFile->name, "_c.c" ) ||
783 strendswith( pFile->name, "_i.c" ) ||
784 strendswith( pFile->name, "_p.c" ) ||
785 strendswith( pFile->name, "_s.c" ) ||
786 !strcmp( pFile->name, "dlldata.c" ))
788 parse_generated_idl( pFile );
789 return;
792 /* don't try to open certain types of files */
793 if (strendswith( pFile->name, ".tlb" ) ||
794 strendswith( pFile->name, ".res" ) ||
795 strendswith( pFile->name, ".x" ))
797 pFile->filename = xstrdup( pFile->name );
798 return;
801 file = src ? open_src_file( pFile ) : open_include_file( pFile );
802 if (!file) return;
804 if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" ))
805 parse_idl_file( pFile, file, 1 );
806 else if (strendswith( pFile->filename, ".idl" ))
807 parse_idl_file( pFile, file, 0 );
808 else if (strendswith( pFile->filename, ".c" ) ||
809 strendswith( pFile->filename, ".m" ) ||
810 strendswith( pFile->filename, ".h" ) ||
811 strendswith( pFile->filename, ".l" ) ||
812 strendswith( pFile->filename, ".y" ))
813 parse_c_file( pFile, file );
814 else if (strendswith( pFile->filename, ".rc" ))
815 parse_rc_file( pFile, file );
816 fclose(file);
820 /*******************************************************************
821 * add_src_file
823 * Add a source file to the list.
825 static INCL_FILE *add_src_file( const char *name )
827 INCL_FILE *file;
829 if (find_src_file( name )) return NULL; /* we already have it */
830 file = xmalloc( sizeof(*file) );
831 memset( file, 0, sizeof(*file) );
832 file->name = xstrdup(name);
833 list_add_tail( &sources, &file->entry );
834 parse_file( file, 1 );
835 return file;
839 /*******************************************************************
840 * output_include
842 static void output_include( FILE *file, INCL_FILE *pFile,
843 INCL_FILE *owner, int *column )
845 int i;
847 if (pFile->owner == owner) return;
848 if (!pFile->filename) return;
849 pFile->owner = owner;
850 if (*column + strlen(pFile->filename) + 1 > 70)
852 fprintf( file, " \\\n" );
853 *column = 0;
855 fprintf( file, " %s", pFile->filename );
856 *column += strlen(pFile->filename) + 1;
857 for (i = 0; i < MAX_INCLUDES; i++)
858 if (pFile->files[i]) output_include( file, pFile->files[i],
859 owner, column );
863 /*******************************************************************
864 * output_src
866 static int output_src( FILE *file, INCL_FILE *pFile, int *column )
868 char *obj = xstrdup( pFile->name );
869 char *ext = get_extension( obj );
870 if (ext)
872 *ext++ = 0;
873 if (!strcmp( ext, "y" )) /* yacc file */
875 /* add source file dependency for parallel makes */
876 char *header = strmake( "%s.tab.h", obj );
877 if (find_include_file( header )) fprintf( file, "%s.tab.c: %s\n", obj, header );
878 free( header );
879 *column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj );
881 else if (!strcmp( ext, "l" )) /* lex file */
883 *column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj );
885 else if (!strcmp( ext, "rc" )) /* resource file */
887 *column += fprintf( file, "rsrc.pot %s.res: %s", obj, pFile->filename );
889 else if (!strcmp( ext, "mc" )) /* message file */
891 *column += fprintf( file, "msg.pot %s.res: %s", obj, pFile->filename );
893 else if (!strcmp( ext, "idl" )) /* IDL file */
895 char *name;
896 int got_header = 0;
897 const char *suffix = "cips";
899 name = strmake( "%s.tlb", obj );
900 if (find_src_file( name )) *column += fprintf( file, "%s %s_t.res", name, obj );
901 else
903 got_header = 1;
904 *column += fprintf( file, "%s.h", obj );
906 free( name );
908 while (*suffix)
910 name = strmake( "%s_%c.c", obj, *suffix );
911 if (find_src_file( name ))
913 if (!got_header++) *column += fprintf( file, " %s.h", obj );
914 *column += fprintf( file, " %s", name );
916 free( name );
917 suffix++;
920 name = strmake( "%s_r.res", obj );
921 if (find_src_file( name )) *column += fprintf( file, " %s", name );
922 free( name );
924 *column += fprintf( file, ": %s", pFile->filename );
926 else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" ))
928 return 0; /* nothing to do for typelib files */
930 else
932 OBJECT_EXTENSION *ext;
933 LIST_FOR_EACH_ENTRY( ext, &object_extensions, OBJECT_EXTENSION, entry )
934 *column += fprintf( file, "%s.%s ", obj, ext->extension );
935 *column += fprintf( file, ": %s", pFile->filename );
938 free( obj );
939 return 1;
943 /*******************************************************************
944 * create_temp_file
946 static FILE *create_temp_file( char **tmp_name )
948 char *name = xmalloc( strlen(OutputFileName) + 13 );
949 unsigned int i, id = getpid();
950 int fd;
951 FILE *ret = NULL;
953 for (i = 0; i < 100; i++)
955 sprintf( name, "%s.tmp%08x", OutputFileName, id );
956 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
958 ret = fdopen( fd, "w" );
959 break;
961 if (errno != EEXIST) break;
962 id += 7777;
964 if (!ret) fatal_error( "%s: error: failed to create output file for '%s'\n",
965 ProgramName, OutputFileName );
966 *tmp_name = name;
967 return ret;
971 /*******************************************************************
972 * output_dependencies
974 static void output_dependencies(void)
976 INCL_FILE *pFile;
977 int i, column;
978 FILE *file = NULL;
979 char *tmp_name = NULL;
981 if (Separator && ((file = fopen( OutputFileName, "r" ))))
983 char buffer[1024];
984 FILE *tmp_file = create_temp_file( &tmp_name );
985 int found = 0;
987 while (fgets( buffer, sizeof(buffer), file ) && !found)
989 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
990 fatal_error( "%s: error: failed to write to %s\n", ProgramName, tmp_name );
991 found = !strncmp( buffer, Separator, strlen(Separator) );
993 fclose( file );
994 file = tmp_file;
995 if (!found && list_head(&sources)) fprintf( file, "\n%s\n", Separator );
997 else
999 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
1001 perror( OutputFileName );
1002 exit(1);
1005 LIST_FOR_EACH_ENTRY( pFile, &sources, INCL_FILE, entry )
1007 column = 0;
1008 if (!output_src( file, pFile, &column )) continue;
1009 for (i = 0; i < MAX_INCLUDES; i++)
1010 if (pFile->files[i]) output_include( file, pFile->files[i],
1011 pFile, &column );
1012 fprintf( file, "\n" );
1014 fclose( file );
1016 if (tmp_name)
1018 int ret = rename( tmp_name, OutputFileName );
1019 if (ret == -1 && errno == EEXIST)
1021 /* rename doesn't overwrite on windows */
1022 unlink( OutputFileName );
1023 ret = rename( tmp_name, OutputFileName );
1025 if (ret == -1)
1027 unlink( tmp_name );
1028 fatal_error( "%s: error: failed to rename output file to '%s'\n",
1029 ProgramName, OutputFileName );
1031 free( tmp_name );
1036 /*******************************************************************
1037 * parse_option
1039 static void parse_option( const char *opt )
1041 switch(opt[1])
1043 case 'I':
1044 if (opt[2]) add_include_path( opt + 2 );
1045 break;
1046 case 'C':
1047 src_dir = opt + 2;
1048 break;
1049 case 'S':
1050 top_src_dir = opt + 2;
1051 break;
1052 case 'T':
1053 top_obj_dir = opt + 2;
1054 break;
1055 case 'f':
1056 if (opt[2]) OutputFileName = opt + 2;
1057 break;
1058 case 's':
1059 if (opt[2]) Separator = opt + 2;
1060 else Separator = NULL;
1061 break;
1062 case 'x':
1063 if (opt[2]) add_object_extension( opt + 2 );
1064 break;
1065 default:
1066 fprintf( stderr, "Unknown option '%s'\n", opt );
1067 fprintf( stderr, Usage, ProgramName );
1068 exit(1);
1073 /*******************************************************************
1074 * main
1076 int main( int argc, char *argv[] )
1078 INCL_FILE *pFile;
1079 INCL_PATH *path, *next;
1080 int i, j;
1082 if ((ProgramName = strrchr( argv[0], '/' ))) ProgramName++;
1083 else ProgramName = argv[0];
1085 i = 1;
1086 while (i < argc)
1088 if (argv[i][0] == '-')
1090 parse_option( argv[i] );
1091 for (j = i; j < argc; j++) argv[j] = argv[j+1];
1092 argc--;
1094 else i++;
1097 /* ignore redundant source paths */
1098 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
1099 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
1101 /* set the default extension list for object files */
1102 if (list_empty( &object_extensions ))
1103 add_object_extension( "o" );
1105 /* get rid of absolute paths that don't point into the source dir */
1106 LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, INCL_PATH, entry )
1108 if (path->name[0] != '/') continue;
1109 if (top_src_dir)
1111 if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) ) &&
1112 path->name[strlen(top_src_dir)] == '/') continue;
1114 list_remove( &path->entry );
1115 free( path );
1118 for (i = 1; i < argc; i++)
1120 add_src_file( argv[i] );
1121 if (strendswith( argv[i], "_p.c" )) add_src_file( "dlldata.c" );
1123 LIST_FOR_EACH_ENTRY( pFile, &includes, INCL_FILE, entry ) parse_file( pFile, 0 );
1124 output_dependencies();
1125 return 0;