wmc: Add support for generating message translations based on po files.
[wine] / tools / makedep.c
1 /*
2  * Generate include file dependencies
3  *
4  * Copyright 1996 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
24
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"
36
37 /* Max first-level includes per file */
38 #define MAX_INCLUDES 200
39
40 typedef struct _INCL_FILE
41 {
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;
52
53 static struct list sources = LIST_INIT(sources);
54 static struct list includes = LIST_INIT(includes);
55
56 typedef struct _OBJECT_EXTENSION
57 {
58     struct list entry;
59     const char *extension;
60 } OBJECT_EXTENSION;
61
62 static struct list object_extensions = LIST_INIT(object_extensions);
63
64 typedef struct _INCL_PATH
65 {
66     struct list entry;
67     const char *name;
68 } INCL_PATH;
69
70 static struct list paths = LIST_INIT(paths);
71
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;
79
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";
89
90
91 /*******************************************************************
92  *         fatal_error
93  */
94 static void fatal_error( const char *msg, ... )
95 {
96     va_list valist;
97     va_start( valist, msg );
98     vfprintf( stderr, msg, valist );
99     va_end( valist );
100     exit(1);
101 }
102
103
104 /*******************************************************************
105  *         xmalloc
106  */
107 static void *xmalloc( size_t size )
108 {
109     void *res;
110     if (!(res = malloc (size ? size : 1)))
111         fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
112     return res;
113 }
114
115
116 /*******************************************************************
117  *         xrealloc
118  */
119 static void *xrealloc (void *ptr, size_t size)
120 {
121     void *res;
122     assert( size );
123     if (!(res = realloc( ptr, size )))
124         fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
125     return res;
126 }
127
128 /*******************************************************************
129  *         xstrdup
130  */
131 static char *xstrdup( const char *str )
132 {
133     char *res = strdup( str );
134     if (!res) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
135     return res;
136 }
137
138
139 /*******************************************************************
140  *         strmake
141  */
142 static char *strmake( const char* fmt, ... )
143 {
144     int n;
145     size_t size = 100;
146     va_list ap;
147
148     for (;;)
149     {
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);
158     }
159 }
160
161
162 /*******************************************************************
163  *         strendswith
164  */
165 static int strendswith( const char* str, const char* end )
166 {
167     int l = strlen(str);
168     int m = strlen(end);
169
170     return l >= m && strcmp(str + l - m, end) == 0;
171 }
172
173 /*******************************************************************
174  *         get_extension
175  */
176 static char *get_extension( char *filename )
177 {
178     char *ext = strrchr( filename, '.' );
179     if (ext && strchr( ext, '/' )) ext = NULL;
180     return ext;
181 }
182
183
184 /*******************************************************************
185  *         get_line
186  */
187 static char *get_line( FILE *file )
188 {
189     static char *buffer;
190     static unsigned int size;
191
192     if (!size)
193     {
194         size = 1024;
195         buffer = xmalloc( size );
196     }
197     if (!fgets( buffer, size, file )) return NULL;
198     input_line++;
199
200     for (;;)
201     {
202         char *p = buffer + strlen(buffer);
203         /* if line is larger than buffer, resize buffer */
204         while (p == buffer + size - 1 && p[-1] != '\n')
205         {
206             buffer = xrealloc( buffer, size * 2 );
207             if (!fgets( buffer + size - 1, size + 1, file )) break;
208             p = buffer + strlen(buffer);
209             size *= 2;
210         }
211         if (p > buffer && p[-1] == '\n')
212         {
213             *(--p) = 0;
214             if (p > buffer && p[-1] == '\r') *(--p) = 0;
215             if (p > buffer && p[-1] == '\\')
216             {
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;
222             }
223         }
224         return buffer;
225     }
226 }
227
228 /*******************************************************************
229  *         add_object_extension
230  *
231  * Add an extension for object files.
232  */
233 static void add_object_extension( const char *ext )
234 {
235     OBJECT_EXTENSION *object_extension = xmalloc( sizeof(*object_extension) );
236     list_add_tail( &object_extensions, &object_extension->entry );
237     object_extension->extension = ext;
238 }
239
240 /*******************************************************************
241  *         add_include_path
242  *
243  * Add a directory to the include path.
244  */
245 static void add_include_path( const char *name )
246 {
247     INCL_PATH *path = xmalloc( sizeof(*path) );
248     list_add_tail( &paths, &path->entry );
249     path->name = name;
250 }
251
252 /*******************************************************************
253  *         find_src_file
254  */
255 static INCL_FILE *find_src_file( const char *name )
256 {
257     INCL_FILE *file;
258
259     LIST_FOR_EACH_ENTRY( file, &sources, INCL_FILE, entry )
260         if (!strcmp( name, file->name )) return file;
261     return NULL;
262 }
263
264 /*******************************************************************
265  *         find_include_file
266  */
267 static INCL_FILE *find_include_file( const char *name )
268 {
269     INCL_FILE *file;
270
271     LIST_FOR_EACH_ENTRY( file, &includes, INCL_FILE, entry )
272         if (!strcmp( name, file->name )) return file;
273     return NULL;
274 }
275
276 /*******************************************************************
277  *         add_include
278  *
279  * Add an include file if it doesn't already exists.
280  */
281 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
282 {
283     INCL_FILE *include;
284     char *ext;
285     int pos;
286
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 );
291
292     /* enforce some rules for the Wine tree */
293
294     if (!memcmp( name, "../", 3 ))
295         fatal_error( "%s:%d: #include directive with relative path not allowed\n",
296                      pFile->filename, line );
297
298     if (!strcmp( name, "config.h" ))
299     {
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 );
303         if (pos)
304             fatal_error( "%s:%d: config.h must be included before anything else\n",
305                          pFile->filename, line );
306     }
307     else if (!strcmp( name, "wine/port.h" ))
308     {
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 );
314         if (pos > 1)
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 );
320     }
321
322     LIST_FOR_EACH_ENTRY( include, &includes, INCL_FILE, entry )
323         if (!strcmp( name, include->name )) goto found;
324
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;
335 }
336
337
338 /*******************************************************************
339  *         open_src_file
340  */
341 static FILE *open_src_file( INCL_FILE *pFile )
342 {
343     FILE *file;
344
345     /* first try name as is */
346     if ((file = fopen( pFile->name, "r" )))
347     {
348         pFile->filename = xstrdup( pFile->name );
349         return file;
350     }
351     /* now try in source dir */
352     if (src_dir)
353     {
354         pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
355         file = fopen( pFile->filename, "r" );
356     }
357     if (!file)
358     {
359         perror( pFile->name );
360         exit(1);
361     }
362     return file;
363 }
364
365
366 /*******************************************************************
367  *         open_include_file
368  */
369 static FILE *open_include_file( INCL_FILE *pFile )
370 {
371     FILE *file = NULL;
372     char *filename, *p;
373     INCL_PATH *path;
374
375     errno = ENOENT;
376
377     /* check for generated bison header */
378
379     if (strendswith( pFile->name, ".tab.h" ))
380     {
381         if (src_dir)
382             filename = strmake( "%s/%.*s.y", src_dir, strlen(pFile->name) - 6, pFile->name );
383         else
384             filename = strmake( "%.*s.y", strlen(pFile->name) - 6, pFile->name );
385
386         if ((file = fopen( filename, "r" )))
387         {
388             pFile->sourcename = filename;
389             pFile->filename = xstrdup( pFile->name );
390             /* don't bother to parse it */
391             fclose( file );
392             return NULL;
393         }
394         free( filename );
395     }
396
397     /* check for generated message resource */
398
399     if (strendswith( pFile->name, ".mc.rc" ))
400     {
401         if (src_dir)
402             filename = strmake( "%s/%s", src_dir, pFile->name );
403         else
404             filename = xstrdup( pFile->name );
405
406         filename[strlen(filename) - 3] = 0;
407
408         if ((file = fopen( filename, "r" )))
409         {
410             pFile->sourcename = filename;
411             pFile->filename = xstrdup( pFile->name );
412             /* don't bother to parse it */
413             fclose( file );
414             return NULL;
415         }
416         free( filename );
417     }
418
419     /* check for corresponding idl file in source dir */
420
421     if (strendswith( pFile->name, ".h" ))
422     {
423         if (src_dir)
424             filename = strmake( "%s/%.*s.idl", src_dir, strlen(pFile->name) - 2, pFile->name );
425         else
426             filename = strmake( "%.*s.idl", strlen(pFile->name) - 2, pFile->name );
427
428         if ((file = fopen( filename, "r" )))
429         {
430             pFile->sourcename = filename;
431             pFile->filename = xstrdup( pFile->name );
432             return file;
433         }
434         free( filename );
435     }
436
437     /* first try name as is */
438     if ((file = fopen( pFile->name, "r" )))
439     {
440         pFile->filename = xstrdup( pFile->name );
441         return file;
442     }
443
444     /* now try in source dir */
445     if (src_dir)
446     {
447         filename = strmake( "%s/%s", src_dir, pFile->name );
448         if ((file = fopen( filename, "r" ))) goto found;
449         free( filename );
450     }
451
452     /* check for corresponding idl file in global includes */
453
454     if (strendswith( pFile->name, ".h" ))
455     {
456         if (top_src_dir)
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 );
462         else
463             filename = NULL;
464
465         if (filename && (file = fopen( filename, "r" )))
466         {
467             pFile->sourcename = filename;
468             pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
469             return file;
470         }
471         free( filename );
472     }
473
474     /* now try in global includes */
475     if (top_obj_dir)
476     {
477         filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
478         if ((file = fopen( filename, "r" ))) goto found;
479         free( filename );
480     }
481     if (top_src_dir)
482     {
483         filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
484         if ((file = fopen( filename, "r" ))) goto found;
485         free( filename );
486     }
487
488     /* now search in include paths */
489     LIST_FOR_EACH_ENTRY( path, &paths, INCL_PATH, entry )
490     {
491         filename = strmake( "%s/%s", path->name, pFile->name );
492         if ((file = fopen( filename, "r" ))) goto found;
493         free( filename );
494     }
495     if (pFile->system) return NULL;  /* ignore system files we cannot find */
496
497     /* try in src file directory */
498     if ((p = strrchr(pFile->included_by->filename, '/')))
499     {
500         int l = p - pFile->included_by->filename + 1;
501         filename = xmalloc(l + strlen(pFile->name) + 1);
502         memcpy( filename, pFile->included_by->filename, l );
503         strcpy( filename + l, pFile->name );
504         if ((file = fopen( filename, "r" ))) goto found;
505         free( filename );
506     }
507
508     perror( pFile->name );
509     while (pFile->included_by)
510     {
511         const char *parent = pFile->included_by->sourcename;
512         if (!parent) parent = pFile->included_by->name;
513         fprintf( stderr, "  %s was first included from %s:%d\n",
514                  pFile->name, parent, pFile->included_line );
515         pFile = pFile->included_by;
516     }
517     exit(1);
518
519 found:
520     pFile->filename = filename;
521     return file;
522 }
523
524
525 /*******************************************************************
526  *         parse_idl_file
527  *
528  * If for_h_file is non-zero, it means we are not interested in the idl file
529  * itself, but only in the contents of the .h file that will be generated from it.
530  */
531 static void parse_idl_file( INCL_FILE *pFile, FILE *file, int for_h_file )
532 {
533     char *buffer, *include;
534
535     if (for_h_file)
536     {
537         /* generated .h file always includes these */
538         add_include( pFile, "rpc.h", 0, 1 );
539         add_include( pFile, "rpcndr.h", 0, 1 );
540     }
541
542     input_line = 0;
543     while ((buffer = get_line( file )))
544     {
545         char quote;
546         char *p = buffer;
547         while (*p && isspace(*p)) p++;
548
549         if (!strncmp( p, "import", 6 ))
550         {
551             p += 6;
552             while (*p && isspace(*p)) p++;
553             if (*p != '"') continue;
554             include = ++p;
555             while (*p && (*p != '"')) p++;
556             if (!*p) fatal_error( "%s:%d: Malformed import directive\n", pFile->filename, input_line );
557             *p = 0;
558             if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
559             add_include( pFile, include, input_line, 0 );
560             continue;
561         }
562
563         if (for_h_file)  /* only check for #include inside cpp_quote */
564         {
565             if (strncmp( p, "cpp_quote", 9 )) continue;
566             p += 9;
567             while (*p && isspace(*p)) p++;
568             if (*p++ != '(') continue;
569             while (*p && isspace(*p)) p++;
570             if (*p++ != '"') continue;
571             if (*p++ != '#') continue;
572             while (*p && isspace(*p)) p++;
573             if (strncmp( p, "include", 7 )) continue;
574             p += 7;
575             while (*p && isspace(*p)) p++;
576             if (*p == '\\' && p[1] == '"')
577             {
578                 p += 2;
579                 quote = '"';
580             }
581             else
582             {
583                 if (*p++ != '<' ) continue;
584                 quote = '>';
585             }
586             include = p;
587             while (*p && (*p != quote)) p++;
588             if (!*p || (quote == '"' && p[-1] != '\\'))
589                 fatal_error( "%s:%d: Malformed #include directive inside cpp_quote\n",
590                              pFile->filename, input_line );
591             if (quote == '"') p--;  /* remove backslash */
592             *p = 0;
593             add_include( pFile, include, input_line, (quote == '>') );
594             continue;
595         }
596
597         /* check for normal #include */
598         if (*p++ != '#') continue;
599         while (*p && isspace(*p)) p++;
600         if (strncmp( p, "include", 7 )) continue;
601         p += 7;
602         while (*p && isspace(*p)) p++;
603         if (*p != '\"' && *p != '<' ) continue;
604         quote = *p++;
605         if (quote == '<') quote = '>';
606         include = p;
607         while (*p && (*p != quote)) p++;
608         if (!*p) fatal_error( "%s:%d: Malformed #include directive\n", pFile->filename, input_line );
609         *p = 0;
610         add_include( pFile, include, input_line, (quote == '>') );
611     }
612 }
613
614 /*******************************************************************
615  *         parse_c_file
616  */
617 static void parse_c_file( INCL_FILE *pFile, FILE *file )
618 {
619     char *buffer, *include;
620
621     input_line = 0;
622     while ((buffer = get_line( file )))
623     {
624         char quote;
625         char *p = buffer;
626         while (*p && isspace(*p)) p++;
627         if (*p++ != '#') continue;
628         while (*p && isspace(*p)) p++;
629         if (strncmp( p, "include", 7 )) continue;
630         p += 7;
631         while (*p && isspace(*p)) p++;
632         if (*p != '\"' && *p != '<' ) continue;
633         quote = *p++;
634         if (quote == '<') quote = '>';
635         include = p;
636         while (*p && (*p != quote)) p++;
637         if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
638                               pFile->filename, input_line );
639         *p = 0;
640         add_include( pFile, include, input_line, (quote == '>') );
641     }
642 }
643
644
645 /*******************************************************************
646  *         parse_rc_file
647  */
648 static void parse_rc_file( INCL_FILE *pFile, FILE *file )
649 {
650     char *buffer, *include;
651
652     input_line = 0;
653     while ((buffer = get_line( file )))
654     {
655         char quote;
656         char *p = buffer;
657         while (*p && isspace(*p)) p++;
658
659         if (p[0] == '/' && p[1] == '*')  /* check for magic makedep comment */
660         {
661             p += 2;
662             while (*p && isspace(*p)) p++;
663             if (strncmp( p, "@makedep:", 9 )) continue;
664             p += 9;
665             while (*p && isspace(*p)) p++;
666             quote = '"';
667             if (*p == quote)
668             {
669                 include = ++p;
670                 while (*p && *p != quote) p++;
671             }
672             else
673             {
674                 include = p;
675                 while (*p && !isspace(*p) && *p != '*') p++;
676             }
677             if (!*p)
678                 fatal_error( "%s:%d: Malformed makedep comment\n", pFile->filename, input_line );
679             *p = 0;
680         }
681         else  /* check for #include */
682         {
683             if (*p++ != '#') continue;
684             while (*p && isspace(*p)) p++;
685             if (strncmp( p, "include", 7 )) continue;
686             p += 7;
687             while (*p && isspace(*p)) p++;
688             if (*p != '\"' && *p != '<' ) continue;
689             quote = *p++;
690             if (quote == '<') quote = '>';
691             include = p;
692             while (*p && (*p != quote)) p++;
693             if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
694                                   pFile->filename, input_line );
695             *p = 0;
696         }
697         add_include( pFile, include, input_line, (quote == '>') );
698     }
699 }
700
701
702 /*******************************************************************
703  *         parse_generated_idl
704  */
705 static void parse_generated_idl( INCL_FILE *source )
706 {
707     char *header, *basename;
708
709     basename = xstrdup( source->name );
710     basename[strlen(basename) - 4] = 0;
711     header = strmake( "%s.h", basename );
712     source->filename = xstrdup( source->name );
713
714     if (strendswith( source->name, "_c.c" ))
715     {
716         add_include( source, header, 0, 0 );
717     }
718     else if (strendswith( source->name, "_i.c" ))
719     {
720         add_include( source, "rpc.h", 0, 1 );
721         add_include( source, "rpcndr.h", 0, 1 );
722         add_include( source, "guiddef.h", 0, 1 );
723     }
724     else if (strendswith( source->name, "_p.c" ))
725     {
726         add_include( source, "objbase.h", 0, 1 );
727         add_include( source, "rpcproxy.h", 0, 1 );
728         add_include( source, "wine/exception.h", 0, 1 );
729         add_include( source, header, 0, 0 );
730     }
731     else if (strendswith( source->name, "_s.c" ))
732     {
733         add_include( source, "wine/exception.h", 0, 1 );
734         add_include( source, header, 0, 0 );
735     }
736     else if (!strcmp( source->name, "dlldata.c" ))
737     {
738         add_include( source, "objbase.h", 0, 1 );
739         add_include( source, "rpcproxy.h", 0, 1 );
740     }
741
742     free( header );
743     free( basename );
744 }
745
746 /*******************************************************************
747  *         parse_file
748  */
749 static void parse_file( INCL_FILE *pFile, int src )
750 {
751     FILE *file;
752
753     /* special case for source files generated from idl */
754     if (strendswith( pFile->name, "_c.c" ) ||
755         strendswith( pFile->name, "_i.c" ) ||
756         strendswith( pFile->name, "_p.c" ) ||
757         strendswith( pFile->name, "_s.c" ) ||
758         !strcmp( pFile->name, "dlldata.c" ))
759     {
760         parse_generated_idl( pFile );
761         return;
762     }
763
764     /* don't try to open .tlb or .res files */
765     if (strendswith( pFile->name, ".tlb" ) ||
766         strendswith( pFile->name, ".res" ))
767     {
768         pFile->filename = xstrdup( pFile->name );
769         return;
770     }
771
772     file = src ? open_src_file( pFile ) : open_include_file( pFile );
773     if (!file) return;
774
775     if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" ))
776         parse_idl_file( pFile, file, 1 );
777     else if (strendswith( pFile->filename, ".idl" ))
778         parse_idl_file( pFile, file, 0 );
779     else if (strendswith( pFile->filename, ".c" ) ||
780              strendswith( pFile->filename, ".h" ) ||
781              strendswith( pFile->filename, ".l" ) ||
782              strendswith( pFile->filename, ".y" ))
783         parse_c_file( pFile, file );
784     else if (strendswith( pFile->filename, ".rc" ))
785         parse_rc_file( pFile, file );
786     fclose(file);
787 }
788
789
790 /*******************************************************************
791  *         add_src_file
792  *
793  * Add a source file to the list.
794  */
795 static INCL_FILE *add_src_file( const char *name )
796 {
797     INCL_FILE *file;
798
799     if (find_src_file( name )) return NULL;  /* we already have it */
800     file = xmalloc( sizeof(*file) );
801     memset( file, 0, sizeof(*file) );
802     file->name = xstrdup(name);
803     list_add_tail( &sources, &file->entry );
804     parse_file( file, 1 );
805     return file;
806 }
807
808
809 /*******************************************************************
810  *         output_include
811  */
812 static void output_include( FILE *file, INCL_FILE *pFile,
813                             INCL_FILE *owner, int *column )
814 {
815     int i;
816
817     if (pFile->owner == owner) return;
818     if (!pFile->filename) return;
819     pFile->owner = owner;
820     if (*column + strlen(pFile->filename) + 1 > 70)
821     {
822         fprintf( file, " \\\n" );
823         *column = 0;
824     }
825     fprintf( file, " %s", pFile->filename );
826     *column += strlen(pFile->filename) + 1;
827     for (i = 0; i < MAX_INCLUDES; i++)
828         if (pFile->files[i]) output_include( file, pFile->files[i],
829                                              owner, column );
830 }
831
832
833 /*******************************************************************
834  *         output_src
835  */
836 static int output_src( FILE *file, INCL_FILE *pFile, int *column )
837 {
838     char *obj = xstrdup( pFile->name );
839     char *ext = get_extension( obj );
840     if (ext)
841     {
842         *ext++ = 0;
843         if (!strcmp( ext, "y" ))  /* yacc file */
844         {
845             /* add source file dependency for parallel makes */
846             char *header = strmake( "%s.tab.h", obj );
847             if (find_include_file( header )) fprintf( file, "%s.tab.c: %s\n", obj, header );
848             free( header );
849             *column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj );
850         }
851         else if (!strcmp( ext, "l" ))  /* lex file */
852         {
853             *column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj );
854         }
855         else if (!strcmp( ext, "rc" ))  /* resource file */
856         {
857             *column += fprintf( file, "rsrc.pot %s.res: %s", obj, pFile->filename );
858         }
859         else if (!strcmp( ext, "mc" ))  /* message file */
860         {
861             *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
862         }
863         else if (!strcmp( ext, "idl" ))  /* IDL file */
864         {
865             char *name;
866             int got_header = 0;
867             const char *suffix = "cips";
868
869             name = strmake( "%s.tlb", obj );
870             if (find_src_file( name )) *column += fprintf( file, "%s", name );
871             else
872             {
873                 got_header = 1;
874                 *column += fprintf( file, "%s.h", obj );
875             }
876             free( name );
877
878             while (*suffix)
879             {
880                 name = strmake( "%s_%c.c", obj, *suffix );
881                 if (find_src_file( name ))
882                 {
883                     if (!got_header++) *column += fprintf( file, " %s.h", obj );
884                     *column += fprintf( file, " %s", name );
885                 }
886                 free( name );
887                 suffix++;
888             }
889
890             name = strmake( "%s_r.res", obj );
891             if (find_src_file( name )) *column += fprintf( file, " %s", name );
892             free( name );
893
894             *column += fprintf( file, ": %s", pFile->filename );
895         }
896         else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" ))
897         {
898             return 0;  /* nothing to do for typelib files */
899         }
900         else
901         {
902             OBJECT_EXTENSION *ext;
903             LIST_FOR_EACH_ENTRY( ext, &object_extensions, OBJECT_EXTENSION, entry )
904                 *column += fprintf( file, "%s.%s ", obj, ext->extension );
905             *column += fprintf( file, ": %s", pFile->filename );
906         }
907     }
908     free( obj );
909     return 1;
910 }
911
912
913 /*******************************************************************
914  *         create_temp_file
915  */
916 static FILE *create_temp_file( char **tmp_name )
917 {
918     char *name = xmalloc( strlen(OutputFileName) + 13 );
919     unsigned int i, id = getpid();
920     int fd;
921     FILE *ret = NULL;
922
923     for (i = 0; i < 100; i++)
924     {
925         sprintf( name, "%s.tmp%08x", OutputFileName, id );
926         if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
927         {
928             ret = fdopen( fd, "w" );
929             break;
930         }
931         if (errno != EEXIST) break;
932         id += 7777;
933     }
934     if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
935     *tmp_name = name;
936     return ret;
937 }
938
939
940 /*******************************************************************
941  *         output_dependencies
942  */
943 static void output_dependencies(void)
944 {
945     INCL_FILE *pFile;
946     int i, column;
947     FILE *file = NULL;
948     char *tmp_name = NULL;
949
950     if (Separator && ((file = fopen( OutputFileName, "r" ))))
951     {
952         char buffer[1024];
953         FILE *tmp_file = create_temp_file( &tmp_name );
954         int found = 0;
955
956         while (fgets( buffer, sizeof(buffer), file ) && !found)
957         {
958             if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
959                 fatal_error( "error writing to %s\n", tmp_name );
960             found = !strncmp( buffer, Separator, strlen(Separator) );
961         }
962         fclose( file );
963         file = tmp_file;
964         if (!found && list_head(&sources)) fprintf( file, "\n%s\n", Separator );
965     }
966     else
967     {
968         if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
969         {
970             perror( OutputFileName );
971             exit(1);
972         }
973     }
974     LIST_FOR_EACH_ENTRY( pFile, &sources, INCL_FILE, entry )
975     {
976         column = 0;
977         if (!output_src( file, pFile, &column )) continue;
978         for (i = 0; i < MAX_INCLUDES; i++)
979             if (pFile->files[i]) output_include( file, pFile->files[i],
980                                                  pFile, &column );
981         fprintf( file, "\n" );
982     }
983     fclose( file );
984
985     if (tmp_name)
986     {
987         int ret = rename( tmp_name, OutputFileName );
988         if (ret == -1 && errno == EEXIST)
989         {
990             /* rename doesn't overwrite on windows */
991             unlink( OutputFileName );
992             ret = rename( tmp_name, OutputFileName );
993         }
994         if (ret == -1)
995         {
996             unlink( tmp_name );
997             fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
998         }
999         free( tmp_name );
1000     }
1001 }
1002
1003
1004 /*******************************************************************
1005  *         parse_option
1006  */
1007 static void parse_option( const char *opt )
1008 {
1009     switch(opt[1])
1010     {
1011     case 'I':
1012         if (opt[2]) add_include_path( opt + 2 );
1013         break;
1014     case 'C':
1015         src_dir = opt + 2;
1016         break;
1017     case 'S':
1018         top_src_dir = opt + 2;
1019         break;
1020     case 'T':
1021         top_obj_dir = opt + 2;
1022         break;
1023     case 'f':
1024         if (opt[2]) OutputFileName = opt + 2;
1025         break;
1026     case 's':
1027         if (opt[2]) Separator = opt + 2;
1028         else Separator = NULL;
1029         break;
1030     case 'x':
1031         if (opt[2]) add_object_extension( opt + 2 );
1032         break;
1033     default:
1034         fprintf( stderr, "Unknown option '%s'\n", opt );
1035         fprintf( stderr, Usage, ProgramName );
1036         exit(1);
1037     }
1038 }
1039
1040
1041 /*******************************************************************
1042  *         main
1043  */
1044 int main( int argc, char *argv[] )
1045 {
1046     INCL_FILE *pFile;
1047     INCL_PATH *path, *next;
1048     int i, j;
1049
1050     ProgramName = argv[0];
1051
1052     i = 1;
1053     while (i < argc)
1054     {
1055         if (argv[i][0] == '-')
1056         {
1057             parse_option( argv[i] );
1058             for (j = i; j < argc; j++) argv[j] = argv[j+1];
1059             argc--;
1060         }
1061         else i++;
1062     }
1063
1064     /* ignore redundant source paths */
1065     if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
1066     if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
1067
1068     /* set the default extension list for object files */
1069     if (list_empty( &object_extensions ))
1070         add_object_extension( "o" );
1071
1072     /* get rid of absolute paths that don't point into the source dir */
1073     LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, INCL_PATH, entry )
1074     {
1075         if (path->name[0] != '/') continue;
1076         if (top_src_dir)
1077         {
1078             if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) ) &&
1079                 path->name[strlen(top_src_dir)] == '/') continue;
1080         }
1081         list_remove( &path->entry );
1082         free( path );
1083     }
1084
1085     for (i = 1; i < argc; i++)
1086     {
1087         add_src_file( argv[i] );
1088         if (strendswith( argv[i], "_p.c" )) add_src_file( "dlldata.c" );
1089     }
1090     LIST_FOR_EACH_ENTRY( pFile, &includes, INCL_FILE, entry ) parse_file( pFile, 0 );
1091     output_dependencies();
1092     return 0;
1093 }