makedep: Remove support for no longer used .mc.rc files.
[wine.git] / tools / makedep.c
blobfb6b9297b22bb7cec3d2bc6242347367678a5b3f
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 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];
53 static struct list sources = LIST_INIT(sources);
54 static struct list includes = LIST_INIT(includes);
56 struct object_extension
58 struct list entry;
59 const char *extension;
62 static struct list object_extensions = LIST_INIT(object_extensions);
64 struct incl_path
66 struct list entry;
67 const char *name;
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 *input_file_name;
78 static int input_line;
79 static FILE *output_file;
81 static const char Usage[] =
82 "Usage: makedep [options] [files]\n"
83 "Options:\n"
84 " -Idir Search for include files in directory 'dir'\n"
85 " -Cdir Search for source files in directory 'dir'\n"
86 " -Sdir Set the top source directory\n"
87 " -Tdir Set the top object directory\n"
88 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
89 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
92 #ifndef __GNUC__
93 #define __attribute__(x)
94 #endif
96 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
97 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
98 static int output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
99 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
101 /*******************************************************************
102 * fatal_error
104 static void fatal_error( const char *msg, ... )
106 va_list valist;
107 va_start( valist, msg );
108 if (input_file_name)
110 fprintf( stderr, "%s:", input_file_name );
111 if (input_line) fprintf( stderr, "%d:", input_line );
112 fprintf( stderr, " error: " );
114 else fprintf( stderr, "makedep: error: " );
115 vfprintf( stderr, msg, valist );
116 va_end( valist );
117 exit(1);
121 /*******************************************************************
122 * fatal_perror
124 static void fatal_perror( const char *msg, ... )
126 va_list valist;
127 va_start( valist, msg );
128 if (input_file_name)
130 fprintf( stderr, "%s:", input_file_name );
131 if (input_line) fprintf( stderr, "%d:", input_line );
132 fprintf( stderr, " error:" );
134 else fprintf( stderr, "makedep: error:" );
135 perror( " " );
136 va_end( valist );
137 exit(1);
141 /*******************************************************************
142 * xmalloc
144 static void *xmalloc( size_t size )
146 void *res;
147 if (!(res = malloc (size ? size : 1)))
148 fatal_error( "Virtual memory exhausted.\n" );
149 return res;
153 /*******************************************************************
154 * xrealloc
156 static void *xrealloc (void *ptr, size_t size)
158 void *res;
159 assert( size );
160 if (!(res = realloc( ptr, size )))
161 fatal_error( "Virtual memory exhausted.\n" );
162 return res;
165 /*******************************************************************
166 * xstrdup
168 static char *xstrdup( const char *str )
170 char *res = strdup( str );
171 if (!res) fatal_error( "Virtual memory exhausted.\n" );
172 return res;
176 /*******************************************************************
177 * strmake
179 static char *strmake( const char* fmt, ... )
181 int n;
182 size_t size = 100;
183 va_list ap;
185 for (;;)
187 char *p = xmalloc (size);
188 va_start(ap, fmt);
189 n = vsnprintf (p, size, fmt, ap);
190 va_end(ap);
191 if (n == -1) size *= 2;
192 else if ((size_t)n >= size) size = n + 1;
193 else return p;
194 free(p);
199 /*******************************************************************
200 * strendswith
202 static int strendswith( const char* str, const char* end )
204 int l = strlen(str);
205 int m = strlen(end);
207 return l >= m && strcmp(str + l - m, end) == 0;
211 /*******************************************************************
212 * output
214 static int output( const char *format, ... )
216 int ret;
217 va_list valist;
219 va_start( valist, format );
220 ret = vfprintf( output_file, format, valist );
221 va_end( valist );
222 if (ret < 0) fatal_perror( "output" );
223 return ret;
227 /*******************************************************************
228 * output_filename
230 static void output_filename( const char *name, int *column )
232 if (*column + strlen(name) + 1 > 100)
234 output( " \\\n" );
235 *column = 0;
237 *column += output( " %s", name );
241 /*******************************************************************
242 * get_extension
244 static char *get_extension( char *filename )
246 char *ext = strrchr( filename, '.' );
247 if (ext && strchr( ext, '/' )) ext = NULL;
248 return ext;
252 /*******************************************************************
253 * get_line
255 static char *get_line( FILE *file )
257 static char *buffer;
258 static unsigned int size;
260 if (!size)
262 size = 1024;
263 buffer = xmalloc( size );
265 if (!fgets( buffer, size, file )) return NULL;
266 input_line++;
268 for (;;)
270 char *p = buffer + strlen(buffer);
271 /* if line is larger than buffer, resize buffer */
272 while (p == buffer + size - 1 && p[-1] != '\n')
274 buffer = xrealloc( buffer, size * 2 );
275 if (!fgets( buffer + size - 1, size + 1, file )) break;
276 p = buffer + strlen(buffer);
277 size *= 2;
279 if (p > buffer && p[-1] == '\n')
281 *(--p) = 0;
282 if (p > buffer && p[-1] == '\r') *(--p) = 0;
283 if (p > buffer && p[-1] == '\\')
285 *(--p) = 0;
286 /* line ends in backslash, read continuation line */
287 if (!fgets( p, size - (p - buffer), file )) return buffer;
288 input_line++;
289 continue;
292 return buffer;
296 /*******************************************************************
297 * add_object_extension
299 * Add an extension for object files.
301 static void add_object_extension( const char *ext )
303 struct object_extension *object_extension = xmalloc( sizeof(*object_extension) );
304 list_add_tail( &object_extensions, &object_extension->entry );
305 object_extension->extension = ext;
308 /*******************************************************************
309 * add_include_path
311 * Add a directory to the include path.
313 static void add_include_path( const char *name )
315 struct incl_path *path = xmalloc( sizeof(*path) );
316 list_add_tail( &paths, &path->entry );
317 path->name = name;
320 /*******************************************************************
321 * find_src_file
323 static struct incl_file *find_src_file( const char *name )
325 struct incl_file *file;
327 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
328 if (!strcmp( name, file->name )) return file;
329 return NULL;
332 /*******************************************************************
333 * find_include_file
335 static struct incl_file *find_include_file( const char *name )
337 struct incl_file *file;
339 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
340 if (!strcmp( name, file->name )) return file;
341 return NULL;
344 /*******************************************************************
345 * find_target_src_file
347 * Check if we have a source file as a target for the specified source with a different extension.
349 static struct incl_file *find_target_src_file( const char *name, const char *ext )
351 struct incl_file *ret;
352 char *p, *match = xmalloc( strlen( name ) + strlen( ext ) + 1 );
354 strcpy( match, name );
355 if ((p = get_extension( match ))) strcpy( p, ext );
356 else strcat( match, ext );
357 ret = find_src_file( match );
358 free( match );
359 return ret;
362 /*******************************************************************
363 * add_include
365 * Add an include file if it doesn't already exists.
367 static struct incl_file *add_include( struct incl_file *pFile, const char *name, int system )
369 struct incl_file *include;
370 char *ext;
371 int pos;
373 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
374 if (pos >= MAX_INCLUDES)
375 fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
377 /* enforce some rules for the Wine tree */
379 if (!memcmp( name, "../", 3 ))
380 fatal_error( "#include directive with relative path not allowed\n" );
382 if (!strcmp( name, "config.h" ))
384 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
385 fatal_error( "config.h must not be included by a header file\n" );
386 if (pos)
387 fatal_error( "config.h must be included before anything else\n" );
389 else if (!strcmp( name, "wine/port.h" ))
391 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
392 fatal_error( "wine/port.h must not be included by a header file\n" );
393 if (!pos) fatal_error( "config.h must be included before wine/port.h\n" );
394 if (pos > 1)
395 fatal_error( "wine/port.h must be included before everything except config.h\n" );
396 if (strcmp( pFile->files[0]->name, "config.h" ))
397 fatal_error( "config.h must be included before wine/port.h\n" );
400 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
401 if (!strcmp( name, include->name )) goto found;
403 include = xmalloc( sizeof(*include) );
404 memset( include, 0, sizeof(*include) );
405 include->name = xstrdup(name);
406 include->included_by = pFile;
407 include->included_line = input_line;
408 include->system = system;
409 list_add_tail( &includes, &include->entry );
410 found:
411 pFile->files[pos] = include;
412 return include;
416 /*******************************************************************
417 * open_src_file
419 static FILE *open_src_file( struct incl_file *pFile )
421 FILE *file;
423 /* first try name as is */
424 if ((file = fopen( pFile->name, "r" )))
426 pFile->filename = xstrdup( pFile->name );
427 return file;
429 /* now try in source dir */
430 if (src_dir)
432 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
433 file = fopen( pFile->filename, "r" );
435 if (!file) fatal_perror( "open %s", pFile->name );
436 return file;
440 /*******************************************************************
441 * open_include_file
443 static FILE *open_include_file( struct incl_file *pFile )
445 FILE *file = NULL;
446 char *filename, *p;
447 struct incl_path *path;
449 errno = ENOENT;
451 /* check for generated bison header */
453 if (strendswith( pFile->name, ".tab.h" ))
455 if (src_dir)
456 filename = strmake( "%s/%.*s.y", src_dir, strlen(pFile->name) - 6, pFile->name );
457 else
458 filename = strmake( "%.*s.y", strlen(pFile->name) - 6, pFile->name );
460 if ((file = fopen( filename, "r" )))
462 pFile->sourcename = filename;
463 pFile->filename = xstrdup( pFile->name );
464 /* don't bother to parse it */
465 fclose( file );
466 return NULL;
468 free( filename );
471 /* check for corresponding idl file in source dir */
473 if (strendswith( pFile->name, ".h" ))
475 if (src_dir)
476 filename = strmake( "%s/%.*s.idl", src_dir, strlen(pFile->name) - 2, pFile->name );
477 else
478 filename = strmake( "%.*s.idl", strlen(pFile->name) - 2, pFile->name );
480 if ((file = fopen( filename, "r" )))
482 pFile->sourcename = filename;
483 pFile->filename = xstrdup( pFile->name );
484 return file;
486 free( filename );
489 /* first try name as is */
490 if ((file = fopen( pFile->name, "r" )))
492 pFile->filename = xstrdup( pFile->name );
493 return file;
496 /* now try in source dir */
497 if (src_dir)
499 filename = strmake( "%s/%s", src_dir, pFile->name );
500 if ((file = fopen( filename, "r" ))) goto found;
501 free( filename );
504 /* check for corresponding idl file in global includes */
506 if (strendswith( pFile->name, ".h" ))
508 if (top_src_dir)
509 filename = strmake( "%s/include/%.*s.idl",
510 top_src_dir, strlen(pFile->name) - 2, pFile->name );
511 else if (top_obj_dir)
512 filename = strmake( "%s/include/%.*s.idl",
513 top_obj_dir, strlen(pFile->name) - 2, pFile->name );
514 else
515 filename = NULL;
517 if (filename && (file = fopen( filename, "r" )))
519 pFile->sourcename = filename;
520 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
521 return file;
523 free( filename );
526 /* check for corresponding .x file in global includes */
528 if (strendswith( pFile->name, "tmpl.h" ))
530 if (top_src_dir)
531 filename = strmake( "%s/include/%.*s.x",
532 top_src_dir, strlen(pFile->name) - 2, pFile->name );
533 else if (top_obj_dir)
534 filename = strmake( "%s/include/%.*s.x",
535 top_obj_dir, strlen(pFile->name) - 2, pFile->name );
536 else
537 filename = NULL;
539 if (filename && (file = fopen( filename, "r" )))
541 pFile->sourcename = filename;
542 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
543 return file;
545 free( filename );
548 /* now try in global includes */
549 if (top_obj_dir)
551 filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
552 if ((file = fopen( filename, "r" ))) goto found;
553 free( filename );
555 if (top_src_dir)
557 filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
558 if ((file = fopen( filename, "r" ))) goto found;
559 free( filename );
562 /* now search in include paths */
563 LIST_FOR_EACH_ENTRY( path, &paths, struct incl_path, entry )
565 filename = strmake( "%s/%s", path->name, pFile->name );
566 if ((file = fopen( filename, "r" ))) goto found;
567 free( filename );
569 if (pFile->system) return NULL; /* ignore system files we cannot find */
571 /* try in src file directory */
572 if ((p = strrchr(pFile->included_by->filename, '/')))
574 int l = p - pFile->included_by->filename + 1;
575 filename = xmalloc(l + strlen(pFile->name) + 1);
576 memcpy( filename, pFile->included_by->filename, l );
577 strcpy( filename + l, pFile->name );
578 if ((file = fopen( filename, "r" ))) goto found;
579 free( filename );
582 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
583 perror( pFile->name );
584 pFile = pFile->included_by;
585 while (pFile && pFile->included_by)
587 const char *parent = pFile->included_by->sourcename;
588 if (!parent) parent = pFile->included_by->name;
589 fprintf( stderr, "%s:%d: note: %s was first included here\n",
590 parent, pFile->included_line, pFile->name );
591 pFile = pFile->included_by;
593 exit(1);
595 found:
596 pFile->filename = filename;
597 return file;
601 /*******************************************************************
602 * parse_idl_file
604 * If for_h_file is non-zero, it means we are not interested in the idl file
605 * itself, but only in the contents of the .h file that will be generated from it.
607 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
609 char *buffer, *include;
611 input_line = 0;
612 if (for_h_file)
614 /* generated .h file always includes these */
615 add_include( pFile, "rpc.h", 1 );
616 add_include( pFile, "rpcndr.h", 1 );
619 while ((buffer = get_line( file )))
621 char quote;
622 char *p = buffer;
623 while (*p && isspace(*p)) p++;
625 if (!strncmp( p, "import", 6 ))
627 p += 6;
628 while (*p && isspace(*p)) p++;
629 if (*p != '"') continue;
630 include = ++p;
631 while (*p && (*p != '"')) p++;
632 if (!*p) fatal_error( "malformed import directive\n" );
633 *p = 0;
634 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
635 add_include( pFile, include, 0 );
636 continue;
639 if (for_h_file) /* only check for #include inside cpp_quote */
641 if (strncmp( p, "cpp_quote", 9 )) continue;
642 p += 9;
643 while (*p && isspace(*p)) p++;
644 if (*p++ != '(') continue;
645 while (*p && isspace(*p)) p++;
646 if (*p++ != '"') continue;
647 if (*p++ != '#') continue;
648 while (*p && isspace(*p)) p++;
649 if (strncmp( p, "include", 7 )) continue;
650 p += 7;
651 while (*p && isspace(*p)) p++;
652 if (*p == '\\' && p[1] == '"')
654 p += 2;
655 quote = '"';
657 else
659 if (*p++ != '<' ) continue;
660 quote = '>';
662 include = p;
663 while (*p && (*p != quote)) p++;
664 if (!*p || (quote == '"' && p[-1] != '\\'))
665 fatal_error( "malformed #include directive inside cpp_quote\n" );
666 if (quote == '"') p--; /* remove backslash */
667 *p = 0;
668 add_include( pFile, include, (quote == '>') );
669 continue;
672 /* check for normal #include */
673 if (*p++ != '#') continue;
674 while (*p && isspace(*p)) p++;
675 if (strncmp( p, "include", 7 )) continue;
676 p += 7;
677 while (*p && isspace(*p)) p++;
678 if (*p != '\"' && *p != '<' ) continue;
679 quote = *p++;
680 if (quote == '<') quote = '>';
681 include = p;
682 while (*p && (*p != quote)) p++;
683 if (!*p) fatal_error( "malformed #include directive\n" );
684 *p = 0;
685 add_include( pFile, include, (quote == '>') );
689 /*******************************************************************
690 * parse_c_file
692 static void parse_c_file( struct incl_file *pFile, FILE *file )
694 char *buffer, *include;
696 input_line = 0;
697 while ((buffer = get_line( file )))
699 char quote;
700 char *p = buffer;
701 while (*p && isspace(*p)) p++;
702 if (*p++ != '#') continue;
703 while (*p && isspace(*p)) p++;
704 if (!strncmp( p, "include", 7 )) p += 7;
705 else if (!strncmp( p, "import", 6 )) p += 6;
706 else continue;
707 while (*p && isspace(*p)) p++;
708 if (*p != '\"' && *p != '<' ) continue;
709 quote = *p++;
710 if (quote == '<') quote = '>';
711 include = p;
712 while (*p && (*p != quote)) p++;
713 if (!*p) fatal_error( "malformed #include directive\n" );
714 *p = 0;
715 add_include( pFile, include, (quote == '>') );
720 /*******************************************************************
721 * parse_rc_file
723 static void parse_rc_file( struct incl_file *pFile, FILE *file )
725 char *buffer, *include;
727 input_line = 0;
728 while ((buffer = get_line( file )))
730 char quote;
731 char *p = buffer;
732 while (*p && isspace(*p)) p++;
734 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
736 p += 2;
737 while (*p && isspace(*p)) p++;
738 if (strncmp( p, "@makedep:", 9 )) continue;
739 p += 9;
740 while (*p && isspace(*p)) p++;
741 quote = '"';
742 if (*p == quote)
744 include = ++p;
745 while (*p && *p != quote) p++;
747 else
749 include = p;
750 while (*p && !isspace(*p) && *p != '*') p++;
752 if (!*p)
753 fatal_error( "malformed makedep comment\n" );
754 *p = 0;
756 else /* check for #include */
758 if (*p++ != '#') continue;
759 while (*p && isspace(*p)) p++;
760 if (strncmp( p, "include", 7 )) continue;
761 p += 7;
762 while (*p && isspace(*p)) p++;
763 if (*p != '\"' && *p != '<' ) continue;
764 quote = *p++;
765 if (quote == '<') quote = '>';
766 include = p;
767 while (*p && (*p != quote)) p++;
768 if (!*p) fatal_error( "malformed #include directive\n" );
769 *p = 0;
771 add_include( pFile, include, (quote == '>') );
776 /*******************************************************************
777 * parse_generated_idl
779 static void parse_generated_idl( struct incl_file *source )
781 char *header, *basename;
783 basename = xstrdup( source->name );
784 basename[strlen(basename) - 4] = 0;
785 header = strmake( "%s.h", basename );
786 source->filename = xstrdup( source->name );
788 if (strendswith( source->name, "_c.c" ))
790 add_include( source, header, 0 );
792 else if (strendswith( source->name, "_i.c" ))
794 add_include( source, "rpc.h", 1 );
795 add_include( source, "rpcndr.h", 1 );
796 add_include( source, "guiddef.h", 1 );
798 else if (strendswith( source->name, "_p.c" ))
800 add_include( source, "objbase.h", 1 );
801 add_include( source, "rpcproxy.h", 1 );
802 add_include( source, "wine/exception.h", 1 );
803 add_include( source, header, 0 );
805 else if (strendswith( source->name, "_s.c" ))
807 add_include( source, "wine/exception.h", 1 );
808 add_include( source, header, 0 );
811 free( header );
812 free( basename );
815 /*******************************************************************
816 * is_generated_idl
818 static int is_generated_idl( struct incl_file *source )
820 return (strendswith( source->name, "_c.c" ) ||
821 strendswith( source->name, "_i.c" ) ||
822 strendswith( source->name, "_p.c" ) ||
823 strendswith( source->name, "_s.c" ));
826 /*******************************************************************
827 * parse_file
829 static void parse_file( struct incl_file *source, int src )
831 FILE *file;
833 /* special case for source files generated from idl */
834 if (is_generated_idl( source ))
836 parse_generated_idl( source );
837 return;
840 if (!strcmp( source->name, "dlldata.o" ))
842 source->filename = xstrdup( "dlldata.c" );
843 add_include( source, "objbase.h", 1 );
844 add_include( source, "rpcproxy.h", 1 );
845 return;
848 if (!strcmp( source->name, "testlist.o" ))
850 source->filename = xstrdup( "testlist.c" );
851 add_include( source, "wine/test.h", 1 );
852 return;
855 if (strendswith( source->name, ".o" ))
857 /* default to .c for unknown extra object files */
858 source->filename = xstrdup( source->name );
859 source->filename[strlen(source->filename) - 1] = 'c';
860 return;
863 /* don't try to open certain types of files */
864 if (strendswith( source->name, ".tlb" ) ||
865 strendswith( source->name, ".res" ) ||
866 strendswith( source->name, ".pot" ) ||
867 strendswith( source->name, ".x" ))
869 source->filename = xstrdup( source->name );
870 return;
873 file = src ? open_src_file( source ) : open_include_file( source );
874 if (!file) return;
875 input_file_name = source->filename;
877 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
878 parse_idl_file( source, file, 1 );
879 else if (strendswith( source->filename, ".idl" ))
880 parse_idl_file( source, file, 0 );
881 else if (strendswith( source->filename, ".c" ) ||
882 strendswith( source->filename, ".m" ) ||
883 strendswith( source->filename, ".h" ) ||
884 strendswith( source->filename, ".l" ) ||
885 strendswith( source->filename, ".y" ))
886 parse_c_file( source, file );
887 else if (strendswith( source->filename, ".rc" ))
888 parse_rc_file( source, file );
889 fclose(file);
890 input_file_name = NULL;
894 /*******************************************************************
895 * add_src_file
897 * Add a source file to the list.
899 static struct incl_file *add_src_file( const char *name )
901 struct incl_file *file;
903 if (find_src_file( name )) return NULL; /* we already have it */
904 file = xmalloc( sizeof(*file) );
905 memset( file, 0, sizeof(*file) );
906 file->name = xstrdup(name);
907 list_add_tail( &sources, &file->entry );
908 parse_file( file, 1 );
909 return file;
913 /*******************************************************************
914 * output_include
916 static void output_include( struct incl_file *pFile, struct incl_file *owner, int *column )
918 int i;
920 if (pFile->owner == owner) return;
921 if (!pFile->filename) return;
922 pFile->owner = owner;
923 output_filename( pFile->filename, column );
924 for (i = 0; i < MAX_INCLUDES; i++)
925 if (pFile->files[i]) output_include( pFile->files[i], owner, column );
929 /*******************************************************************
930 * output_sources
932 static void output_sources(void)
934 struct incl_file *source;
935 int i, column, po_srcs = 0, mc_srcs = 0;
937 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
939 char *obj = xstrdup( source->name );
940 char *ext = get_extension( obj );
942 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
943 *ext++ = 0;
944 column = 0;
946 if (!strcmp( ext, "y" )) /* yacc file */
948 /* add source file dependency for parallel makes */
949 char *header = strmake( "%s.tab.h", obj );
950 if (find_include_file( header ))
952 output( "%s.tab.h: %s\n", obj, source->filename );
953 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
954 obj, obj, source->filename );
955 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
957 else output( "%s.tab.c: %s\n", obj, source->filename );
959 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj, source->filename );
960 column += output( "%s.tab.o: %s.tab.c", obj, obj );
961 free( header );
963 else if (!strcmp( ext, "l" )) /* lex file */
965 output( "%s.yy.c: %s\n", obj, source->filename );
966 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source->filename );
967 column += output( "%s.yy.o: %s.yy.c", obj, obj );
969 else if (!strcmp( ext, "rc" )) /* resource file */
971 if (find_target_src_file( source->name, ".pot" ))
973 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj, source->filename );
974 output( "\t$(WRC) $(RCFLAGS) -o $@ %s\n", source->filename );
975 column += output( "%s.res rsrc.pot:", obj );
976 po_srcs++;
978 else
980 output( "%s.res: $(WRC) %s\n", obj, source->filename );
981 output( "\t$(WRC) $(RCFLAGS) -o $@ %s\n", source->filename );
982 column += output( "%s.res:", obj );
985 else if (!strcmp( ext, "mc" )) /* message file */
987 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj, source->filename );
988 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source->filename );
989 mc_srcs++;
990 column += output( "msg.pot %s.res:", obj );
992 else if (!strcmp( ext, "idl" )) /* IDL file */
994 char *targets[8];
995 int nb_targets = 0;
996 char ending[] = "_?.c";
997 const char *suffix;
998 char *header = strmake( "%s.h", obj );
1000 if (find_target_src_file( source->name, ".tlb" ))
1002 output( "%s.tlb %s_t.res: $(WIDL)\n", obj, obj );
1003 output( "\t$(WIDL) $(TARGETFLAGS) $(IDLFLAGS) -t -o $@ %s\n", source->filename );
1004 targets[nb_targets++] = strmake( "%s.tlb", obj );
1005 targets[nb_targets++] = strmake( "%s_t.res", obj );
1008 for (suffix = "cips"; *suffix; suffix++)
1010 ending[1] = *suffix;
1011 if (!find_target_src_file( source->name, ending )) continue;
1012 output( "%s%s: $(WIDL)\n", obj, ending );
1013 output( "\t$(WIDL) $(IDLFLAGS) -%c -o $@ %s\n",
1014 *suffix == 'i' ? 'u' : *suffix, source->filename );
1015 targets[nb_targets++] = strmake( "%s%s", obj, ending );
1018 if (find_target_src_file( source->name, "_r.res" ))
1020 output( "%s_r.res: $(WIDL)\n", obj );
1021 output( "\t$(WIDL) $(IDLFLAGS) -r -o $@ %s\n", source->filename );
1022 targets[nb_targets++] = strmake( "%s_r.res", obj );
1025 if (!nb_targets || find_include_file( header ))
1027 output( "%s.h: $(WIDL)\n", obj );
1028 output( "\t$(WIDL) $(IDLFLAGS) -h -o $@ %s\n", source->filename );
1029 targets[nb_targets++] = header;
1031 else free( header );
1033 for (i = 0; i < nb_targets; i++)
1035 column += output( "%s%c", targets[i], i < nb_targets - 1 ? ' ' : ':' );
1036 free( targets[i] );
1038 column += output( " %s", source->filename );
1040 else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" ) || !strcmp( ext, "pot" ))
1042 continue; /* nothing to do for typelib files */
1044 else
1046 struct object_extension *ext;
1047 LIST_FOR_EACH_ENTRY( ext, &object_extensions, struct object_extension, entry )
1048 column += output( "%s.%s ", obj, ext->extension );
1049 column += output( ": %s", source->filename );
1051 free( obj );
1053 for (i = 0; i < MAX_INCLUDES; i++)
1054 if (source->files[i]) output_include( source->files[i], source, &column );
1055 output( "\n" );
1058 /* rules for files that depend on multiple sources */
1060 if (po_srcs)
1062 column = output( "rsrc.pot: $(WRC)" );
1063 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1064 if (strendswith( source->name, ".rc" ) && find_target_src_file( source->name, ".pot" ))
1065 output_filename( source->filename, &column );
1066 output( "\n" );
1067 column = output( "\t$(WRC) $(RCFLAGS) -O pot -o $@" );
1068 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1069 if (strendswith( source->name, ".rc" ) && find_target_src_file( source->name, ".pot" ))
1070 output_filename( source->filename, &column );
1071 output( "\n" );
1074 if (mc_srcs)
1076 column = output( "msg.pot: $(WMC)" );
1077 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1078 if (strendswith( source->name, ".mc" )) output_filename( source->filename, &column );
1079 output( "\n" );
1080 column = output( "\t$(WMC) -O pot -o $@" );
1081 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1082 if (strendswith( source->name, ".mc" )) output_filename( source->filename, &column );
1083 output( "\n" );
1086 if (find_src_file( "dlldata.o" ))
1088 output( "dlldata.c: $(WIDL) Makefile.in\n" );
1089 column = output( "\t$(WIDL) $(IDLFLAGS) --dlldata-only -o $@" );
1090 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1091 if (strendswith( source->name, ".idl" ) && find_target_src_file( source->name, "_p.c" ))
1092 output_filename( source->filename, &column );
1093 output( "\n" );
1096 if (find_src_file( "testlist.o" ))
1098 output( "testlist.c: $(MAKECTESTS) Makefile.in\n" );
1099 column = output( "\t$(MAKECTESTS) -o $@" );
1100 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1101 if (strendswith( source->name, ".c" ) && !is_generated_idl( source ))
1102 output_filename( source->filename, &column );
1103 output( "\n" );
1108 /*******************************************************************
1109 * create_temp_file
1111 static FILE *create_temp_file( char **tmp_name )
1113 char *name = xmalloc( strlen(OutputFileName) + 13 );
1114 unsigned int i, id = getpid();
1115 int fd;
1116 FILE *ret = NULL;
1118 for (i = 0; i < 100; i++)
1120 sprintf( name, "%s.tmp%08x", OutputFileName, id );
1121 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
1123 ret = fdopen( fd, "w" );
1124 break;
1126 if (errno != EEXIST) break;
1127 id += 7777;
1129 if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
1130 *tmp_name = name;
1131 return ret;
1135 /*******************************************************************
1136 * output_dependencies
1138 static void output_dependencies(void)
1140 char *tmp_name = NULL;
1142 if (Separator && ((output_file = fopen( OutputFileName, "r" ))))
1144 char buffer[1024];
1145 FILE *tmp_file = create_temp_file( &tmp_name );
1146 int found = 0;
1148 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
1150 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
1151 fatal_error( "failed to write to %s\n", tmp_name );
1152 found = !strncmp( buffer, Separator, strlen(Separator) );
1154 fclose( output_file );
1155 output_file = tmp_file;
1156 if (!found && list_head(&sources)) output( "\n%s\n", Separator );
1158 else
1160 if (!(output_file = fopen( OutputFileName, Separator ? "a" : "w" )))
1161 fatal_perror( "%s", OutputFileName );
1164 output_sources();
1166 fclose( output_file );
1167 output_file = NULL;
1169 if (tmp_name)
1171 int ret = rename( tmp_name, OutputFileName );
1172 if (ret == -1 && errno == EEXIST)
1174 /* rename doesn't overwrite on windows */
1175 unlink( OutputFileName );
1176 ret = rename( tmp_name, OutputFileName );
1178 if (ret == -1)
1180 unlink( tmp_name );
1181 fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
1183 free( tmp_name );
1188 /*******************************************************************
1189 * parse_option
1191 static void parse_option( const char *opt )
1193 switch(opt[1])
1195 case 'I':
1196 if (opt[2]) add_include_path( opt + 2 );
1197 break;
1198 case 'C':
1199 src_dir = opt + 2;
1200 break;
1201 case 'S':
1202 top_src_dir = opt + 2;
1203 break;
1204 case 'T':
1205 top_obj_dir = opt + 2;
1206 break;
1207 case 'f':
1208 if (opt[2]) OutputFileName = opt + 2;
1209 break;
1210 case 's':
1211 if (opt[2]) Separator = opt + 2;
1212 else Separator = NULL;
1213 break;
1214 case 'x':
1215 if (opt[2]) add_object_extension( opt + 2 );
1216 break;
1217 default:
1218 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
1219 exit(1);
1224 /*******************************************************************
1225 * main
1227 int main( int argc, char *argv[] )
1229 struct incl_file *pFile;
1230 struct incl_path *path, *next;
1231 int i, j;
1233 i = 1;
1234 while (i < argc)
1236 if (argv[i][0] == '-')
1238 parse_option( argv[i] );
1239 for (j = i; j < argc; j++) argv[j] = argv[j+1];
1240 argc--;
1242 else i++;
1245 /* ignore redundant source paths */
1246 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
1247 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
1249 /* set the default extension list for object files */
1250 if (list_empty( &object_extensions ))
1251 add_object_extension( "o" );
1253 /* get rid of absolute paths that don't point into the source dir */
1254 LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, struct incl_path, entry )
1256 if (path->name[0] != '/') continue;
1257 if (top_src_dir)
1259 if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) ) &&
1260 path->name[strlen(top_src_dir)] == '/') continue;
1262 list_remove( &path->entry );
1263 free( path );
1266 for (i = 1; i < argc; i++) add_src_file( argv[i] );
1268 LIST_FOR_EACH_ENTRY( pFile, &includes, struct incl_file, entry ) parse_file( pFile, 0 );
1269 output_dependencies();
1270 return 0;