wineps: Return the size directly from get_bbox to avoid compiler warnings.
[wine.git] / tools / makedep.c
blob982d827b5dbdd2dffdea8a526165b3d89dbfcea5
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 * replace_extension
255 static char *replace_extension( const char *name, unsigned int old_len, const char *new_ext )
257 char *ret = xmalloc( strlen( name ) + strlen( new_ext ) + 1 );
258 strcpy( ret, name );
259 strcpy( ret + strlen( ret ) - old_len, new_ext );
260 return ret;
264 /*******************************************************************
265 * get_line
267 static char *get_line( FILE *file )
269 static char *buffer;
270 static unsigned int size;
272 if (!size)
274 size = 1024;
275 buffer = xmalloc( size );
277 if (!fgets( buffer, size, file )) return NULL;
278 input_line++;
280 for (;;)
282 char *p = buffer + strlen(buffer);
283 /* if line is larger than buffer, resize buffer */
284 while (p == buffer + size - 1 && p[-1] != '\n')
286 buffer = xrealloc( buffer, size * 2 );
287 if (!fgets( buffer + size - 1, size + 1, file )) break;
288 p = buffer + strlen(buffer);
289 size *= 2;
291 if (p > buffer && p[-1] == '\n')
293 *(--p) = 0;
294 if (p > buffer && p[-1] == '\r') *(--p) = 0;
295 if (p > buffer && p[-1] == '\\')
297 *(--p) = 0;
298 /* line ends in backslash, read continuation line */
299 if (!fgets( p, size - (p - buffer), file )) return buffer;
300 input_line++;
301 continue;
304 return buffer;
308 /*******************************************************************
309 * add_object_extension
311 * Add an extension for object files.
313 static void add_object_extension( const char *ext )
315 struct object_extension *object_extension = xmalloc( sizeof(*object_extension) );
316 list_add_tail( &object_extensions, &object_extension->entry );
317 object_extension->extension = ext;
320 /*******************************************************************
321 * add_include_path
323 * Add a directory to the include path.
325 static void add_include_path( const char *name )
327 struct incl_path *path = xmalloc( sizeof(*path) );
328 list_add_tail( &paths, &path->entry );
329 path->name = name;
332 /*******************************************************************
333 * find_src_file
335 static struct incl_file *find_src_file( const char *name )
337 struct incl_file *file;
339 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
340 if (!strcmp( name, file->name )) return file;
341 return NULL;
344 /*******************************************************************
345 * find_include_file
347 static struct incl_file *find_include_file( const char *name )
349 struct incl_file *file;
351 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
352 if (!strcmp( name, file->name )) return file;
353 return NULL;
356 /*******************************************************************
357 * find_target_src_file
359 * Check if we have a source file as a target for the specified source with a different extension.
361 static struct incl_file *find_target_src_file( const char *name, const char *ext )
363 struct incl_file *ret;
364 char *p, *match = xmalloc( strlen( name ) + strlen( ext ) + 1 );
366 strcpy( match, name );
367 if ((p = get_extension( match ))) strcpy( p, ext );
368 else strcat( match, ext );
369 ret = find_src_file( match );
370 free( match );
371 return ret;
374 /*******************************************************************
375 * add_include
377 * Add an include file if it doesn't already exists.
379 static struct incl_file *add_include( struct incl_file *pFile, const char *name, int system )
381 struct incl_file *include;
382 char *ext;
383 int pos;
385 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
386 if (pos >= MAX_INCLUDES)
387 fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
389 /* enforce some rules for the Wine tree */
391 if (!memcmp( name, "../", 3 ))
392 fatal_error( "#include directive with relative path not allowed\n" );
394 if (!strcmp( name, "config.h" ))
396 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
397 fatal_error( "config.h must not be included by a header file\n" );
398 if (pos)
399 fatal_error( "config.h must be included before anything else\n" );
401 else if (!strcmp( name, "wine/port.h" ))
403 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
404 fatal_error( "wine/port.h must not be included by a header file\n" );
405 if (!pos) fatal_error( "config.h must be included before wine/port.h\n" );
406 if (pos > 1)
407 fatal_error( "wine/port.h must be included before everything except config.h\n" );
408 if (strcmp( pFile->files[0]->name, "config.h" ))
409 fatal_error( "config.h must be included before wine/port.h\n" );
412 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
413 if (!strcmp( name, include->name )) goto found;
415 include = xmalloc( sizeof(*include) );
416 memset( include, 0, sizeof(*include) );
417 include->name = xstrdup(name);
418 include->included_by = pFile;
419 include->included_line = input_line;
420 include->system = system;
421 list_add_tail( &includes, &include->entry );
422 found:
423 pFile->files[pos] = include;
424 return include;
428 /*******************************************************************
429 * open_src_file
431 static FILE *open_src_file( struct incl_file *pFile )
433 FILE *file;
435 /* first try name as is */
436 if ((file = fopen( pFile->name, "r" )))
438 pFile->filename = xstrdup( pFile->name );
439 return file;
441 /* now try in source dir */
442 if (src_dir)
444 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
445 file = fopen( pFile->filename, "r" );
447 if (!file) fatal_perror( "open %s", pFile->name );
448 return file;
452 /*******************************************************************
453 * open_include_file
455 static FILE *open_include_file( struct incl_file *pFile )
457 FILE *file = NULL;
458 char *filename, *p;
459 struct incl_path *path;
461 errno = ENOENT;
463 /* check for generated bison header */
465 if (strendswith( pFile->name, ".tab.h" ))
467 filename = replace_extension( pFile->name, 6, ".y" );
468 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
470 if ((file = fopen( filename, "r" )))
472 pFile->sourcename = filename;
473 pFile->filename = xstrdup( pFile->name );
474 /* don't bother to parse it */
475 fclose( file );
476 return NULL;
478 free( filename );
481 /* check for corresponding idl file in source dir */
483 if (strendswith( pFile->name, ".h" ))
485 filename = replace_extension( pFile->name, 2, ".idl" );
486 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
488 if ((file = fopen( filename, "r" )))
490 pFile->sourcename = filename;
491 pFile->filename = xstrdup( pFile->name );
492 return file;
494 free( filename );
497 /* first try name as is */
498 if ((file = fopen( pFile->name, "r" )))
500 pFile->filename = xstrdup( pFile->name );
501 return file;
504 /* now try in source dir */
505 if (src_dir)
507 filename = strmake( "%s/%s", src_dir, pFile->name );
508 if ((file = fopen( filename, "r" ))) goto found;
509 free( filename );
512 /* check for corresponding idl file in global includes */
514 if (strendswith( pFile->name, ".h" ))
516 filename = replace_extension( pFile->name, 2, ".idl" );
517 if (top_src_dir)
518 filename = strmake( "%s/include/%s", top_src_dir, filename );
519 else if (top_obj_dir)
520 filename = strmake( "%s/include/%s", top_obj_dir, filename );
521 else
522 filename = NULL;
524 if (filename && (file = fopen( filename, "r" )))
526 pFile->sourcename = filename;
527 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
528 return file;
530 free( filename );
533 /* check for corresponding .x file in global includes */
535 if (strendswith( pFile->name, "tmpl.h" ))
537 filename = replace_extension( pFile->name, 2, ".x" );
538 if (top_src_dir)
539 filename = strmake( "%s/include/%s", top_src_dir, filename );
540 else if (top_obj_dir)
541 filename = strmake( "%s/include/%s", top_obj_dir, filename );
542 else
543 filename = NULL;
545 if (filename && (file = fopen( filename, "r" )))
547 pFile->sourcename = filename;
548 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
549 return file;
551 free( filename );
554 /* now try in global includes */
555 if (top_obj_dir)
557 filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
558 if ((file = fopen( filename, "r" ))) goto found;
559 free( filename );
561 if (top_src_dir)
563 filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
564 if ((file = fopen( filename, "r" ))) goto found;
565 free( filename );
568 /* now search in include paths */
569 LIST_FOR_EACH_ENTRY( path, &paths, struct incl_path, entry )
571 filename = strmake( "%s/%s", path->name, pFile->name );
572 if ((file = fopen( filename, "r" ))) goto found;
573 free( filename );
575 if (pFile->system) return NULL; /* ignore system files we cannot find */
577 /* try in src file directory */
578 if ((p = strrchr(pFile->included_by->filename, '/')))
580 int l = p - pFile->included_by->filename + 1;
581 filename = xmalloc(l + strlen(pFile->name) + 1);
582 memcpy( filename, pFile->included_by->filename, l );
583 strcpy( filename + l, pFile->name );
584 if ((file = fopen( filename, "r" ))) goto found;
585 free( filename );
588 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
589 perror( pFile->name );
590 pFile = pFile->included_by;
591 while (pFile && pFile->included_by)
593 const char *parent = pFile->included_by->sourcename;
594 if (!parent) parent = pFile->included_by->name;
595 fprintf( stderr, "%s:%d: note: %s was first included here\n",
596 parent, pFile->included_line, pFile->name );
597 pFile = pFile->included_by;
599 exit(1);
601 found:
602 pFile->filename = filename;
603 return file;
607 /*******************************************************************
608 * parse_idl_file
610 * If for_h_file is non-zero, it means we are not interested in the idl file
611 * itself, but only in the contents of the .h file that will be generated from it.
613 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
615 char *buffer, *include;
617 input_line = 0;
618 if (for_h_file)
620 /* generated .h file always includes these */
621 add_include( pFile, "rpc.h", 1 );
622 add_include( pFile, "rpcndr.h", 1 );
625 while ((buffer = get_line( file )))
627 char quote;
628 char *p = buffer;
629 while (*p && isspace(*p)) p++;
631 if (!strncmp( p, "import", 6 ))
633 p += 6;
634 while (*p && isspace(*p)) p++;
635 if (*p != '"') continue;
636 include = ++p;
637 while (*p && (*p != '"')) p++;
638 if (!*p) fatal_error( "malformed import directive\n" );
639 *p = 0;
640 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
641 add_include( pFile, include, 0 );
642 continue;
645 if (for_h_file) /* only check for #include inside cpp_quote */
647 if (strncmp( p, "cpp_quote", 9 )) continue;
648 p += 9;
649 while (*p && isspace(*p)) p++;
650 if (*p++ != '(') continue;
651 while (*p && isspace(*p)) p++;
652 if (*p++ != '"') continue;
653 if (*p++ != '#') continue;
654 while (*p && isspace(*p)) p++;
655 if (strncmp( p, "include", 7 )) continue;
656 p += 7;
657 while (*p && isspace(*p)) p++;
658 if (*p == '\\' && p[1] == '"')
660 p += 2;
661 quote = '"';
663 else
665 if (*p++ != '<' ) continue;
666 quote = '>';
668 include = p;
669 while (*p && (*p != quote)) p++;
670 if (!*p || (quote == '"' && p[-1] != '\\'))
671 fatal_error( "malformed #include directive inside cpp_quote\n" );
672 if (quote == '"') p--; /* remove backslash */
673 *p = 0;
674 add_include( pFile, include, (quote == '>') );
675 continue;
678 /* check for normal #include */
679 if (*p++ != '#') continue;
680 while (*p && isspace(*p)) p++;
681 if (strncmp( p, "include", 7 )) continue;
682 p += 7;
683 while (*p && isspace(*p)) p++;
684 if (*p != '\"' && *p != '<' ) continue;
685 quote = *p++;
686 if (quote == '<') quote = '>';
687 include = p;
688 while (*p && (*p != quote)) p++;
689 if (!*p) fatal_error( "malformed #include directive\n" );
690 *p = 0;
691 add_include( pFile, include, (quote == '>') );
695 /*******************************************************************
696 * parse_c_file
698 static void parse_c_file( struct incl_file *pFile, FILE *file )
700 char *buffer, *include;
702 input_line = 0;
703 while ((buffer = get_line( file )))
705 char quote;
706 char *p = buffer;
707 while (*p && isspace(*p)) p++;
708 if (*p++ != '#') continue;
709 while (*p && isspace(*p)) p++;
710 if (!strncmp( p, "include", 7 )) p += 7;
711 else if (!strncmp( p, "import", 6 )) p += 6;
712 else continue;
713 while (*p && isspace(*p)) p++;
714 if (*p != '\"' && *p != '<' ) continue;
715 quote = *p++;
716 if (quote == '<') quote = '>';
717 include = p;
718 while (*p && (*p != quote)) p++;
719 if (!*p) fatal_error( "malformed #include directive\n" );
720 *p = 0;
721 add_include( pFile, include, (quote == '>') );
726 /*******************************************************************
727 * parse_rc_file
729 static void parse_rc_file( struct incl_file *pFile, FILE *file )
731 char *buffer, *include;
733 input_line = 0;
734 while ((buffer = get_line( file )))
736 char quote;
737 char *p = buffer;
738 while (*p && isspace(*p)) p++;
740 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
742 p += 2;
743 while (*p && isspace(*p)) p++;
744 if (strncmp( p, "@makedep:", 9 )) continue;
745 p += 9;
746 while (*p && isspace(*p)) p++;
747 quote = '"';
748 if (*p == quote)
750 include = ++p;
751 while (*p && *p != quote) p++;
753 else
755 include = p;
756 while (*p && !isspace(*p) && *p != '*') p++;
758 if (!*p)
759 fatal_error( "malformed makedep comment\n" );
760 *p = 0;
762 else /* check for #include */
764 if (*p++ != '#') continue;
765 while (*p && isspace(*p)) p++;
766 if (strncmp( p, "include", 7 )) continue;
767 p += 7;
768 while (*p && isspace(*p)) p++;
769 if (*p != '\"' && *p != '<' ) continue;
770 quote = *p++;
771 if (quote == '<') quote = '>';
772 include = p;
773 while (*p && (*p != quote)) p++;
774 if (!*p) fatal_error( "malformed #include directive\n" );
775 *p = 0;
777 add_include( pFile, include, (quote == '>') );
782 /*******************************************************************
783 * parse_generated_idl
785 static void parse_generated_idl( struct incl_file *source )
787 char *header = replace_extension( source->name, 4, ".h" );
789 source->filename = xstrdup( source->name );
791 if (strendswith( source->name, "_c.c" ))
793 add_include( source, header, 0 );
795 else if (strendswith( source->name, "_i.c" ))
797 add_include( source, "rpc.h", 1 );
798 add_include( source, "rpcndr.h", 1 );
799 add_include( source, "guiddef.h", 1 );
801 else if (strendswith( source->name, "_p.c" ))
803 add_include( source, "objbase.h", 1 );
804 add_include( source, "rpcproxy.h", 1 );
805 add_include( source, "wine/exception.h", 1 );
806 add_include( source, header, 0 );
808 else if (strendswith( source->name, "_s.c" ))
810 add_include( source, "wine/exception.h", 1 );
811 add_include( source, header, 0 );
814 free( header );
817 /*******************************************************************
818 * is_generated_idl
820 static int is_generated_idl( struct incl_file *source )
822 return (strendswith( source->name, "_c.c" ) ||
823 strendswith( source->name, "_i.c" ) ||
824 strendswith( source->name, "_p.c" ) ||
825 strendswith( source->name, "_s.c" ));
828 /*******************************************************************
829 * parse_file
831 static void parse_file( struct incl_file *source, int src )
833 FILE *file;
835 /* don't try to open certain types of files */
836 if (strendswith( source->name, ".tlb" ) ||
837 strendswith( source->name, ".x" ))
839 source->filename = xstrdup( source->name );
840 return;
843 file = src ? open_src_file( source ) : open_include_file( source );
844 if (!file) return;
845 input_file_name = source->filename;
847 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
848 parse_idl_file( source, file, 1 );
849 else if (strendswith( source->filename, ".idl" ))
850 parse_idl_file( source, file, 0 );
851 else if (strendswith( source->filename, ".c" ) ||
852 strendswith( source->filename, ".m" ) ||
853 strendswith( source->filename, ".h" ) ||
854 strendswith( source->filename, ".l" ) ||
855 strendswith( source->filename, ".y" ))
856 parse_c_file( source, file );
857 else if (strendswith( source->filename, ".rc" ))
858 parse_rc_file( source, file );
859 fclose(file);
860 input_file_name = NULL;
864 /*******************************************************************
865 * add_src_file
867 * Add a source file to the list.
869 static struct incl_file *add_src_file( const char *name )
871 struct incl_file *file;
872 char *idl;
874 if (find_src_file( name )) return NULL; /* we already have it */
875 file = xmalloc( sizeof(*file) );
876 memset( file, 0, sizeof(*file) );
877 file->name = xstrdup(name);
878 list_add_tail( &sources, &file->entry );
880 /* special cases for generated files */
882 if (is_generated_idl( file ))
884 parse_generated_idl( file );
885 goto add_idl_source;
888 if (!strcmp( file->name, "dlldata.o" ))
890 file->filename = xstrdup( "dlldata.c" );
891 add_include( file, "objbase.h", 1 );
892 add_include( file, "rpcproxy.h", 1 );
893 return file;
896 if (!strcmp( file->name, "testlist.o" ))
898 file->filename = xstrdup( "testlist.c" );
899 add_include( file, "wine/test.h", 1 );
900 return file;
903 if (strendswith( file->name, ".o" ))
905 /* default to .c for unknown extra object files */
906 file->filename = replace_extension( file->name, 2, ".c" );
907 return file;
910 if (strendswith( file->name, ".tlb" ) ||
911 strendswith( file->name, "_r.res" ) ||
912 strendswith( file->name, "_t.res" ))
914 file->filename = xstrdup( file->name );
915 goto add_idl_source;
918 if (strendswith( file->name, ".res" ) ||
919 strendswith( file->name, ".pot" ) ||
920 strendswith( file->name, ".x" ))
922 file->filename = xstrdup( file->name );
923 return file;
926 parse_file( file, 1 );
927 return file;
929 add_idl_source:
930 idl = replace_extension( name, strendswith( name, ".res" ) ? 6 : 4, ".idl" );
931 add_src_file( idl );
932 free( idl );
933 return file;
937 /*******************************************************************
938 * output_include
940 static void output_include( struct incl_file *pFile, struct incl_file *owner, int *column )
942 int i;
944 if (pFile->owner == owner) return;
945 if (!pFile->filename) return;
946 pFile->owner = owner;
947 output_filename( pFile->filename, column );
948 for (i = 0; i < MAX_INCLUDES; i++)
949 if (pFile->files[i]) output_include( pFile->files[i], owner, column );
953 /*******************************************************************
954 * output_sources
956 static void output_sources(void)
958 struct incl_file *source;
959 int i, column, po_srcs = 0, mc_srcs = 0;
961 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
963 char *obj = xstrdup( source->name );
964 char *ext = get_extension( obj );
966 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
967 *ext++ = 0;
968 column = 0;
970 if (!strcmp( ext, "y" )) /* yacc file */
972 /* add source file dependency for parallel makes */
973 char *header = strmake( "%s.tab.h", obj );
974 if (find_include_file( header ))
976 output( "%s.tab.h: %s\n", obj, source->filename );
977 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
978 obj, obj, source->filename );
979 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
981 else output( "%s.tab.c: %s\n", obj, source->filename );
983 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj, source->filename );
984 column += output( "%s.tab.o: %s.tab.c", obj, obj );
985 free( header );
987 else if (!strcmp( ext, "l" )) /* lex file */
989 output( "%s.yy.c: %s\n", obj, source->filename );
990 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source->filename );
991 column += output( "%s.yy.o: %s.yy.c", obj, obj );
993 else if (!strcmp( ext, "rc" )) /* resource file */
995 if (find_target_src_file( source->name, ".pot" ))
997 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj, source->filename );
998 output( "\t$(WRC) $(RCFLAGS) -o $@ %s\n", source->filename );
999 column += output( "%s.res rsrc.pot:", obj );
1000 po_srcs++;
1002 else
1004 output( "%s.res: $(WRC) %s\n", obj, source->filename );
1005 output( "\t$(WRC) $(RCFLAGS) -o $@ %s\n", source->filename );
1006 column += output( "%s.res:", obj );
1009 else if (!strcmp( ext, "mc" )) /* message file */
1011 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj, source->filename );
1012 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source->filename );
1013 mc_srcs++;
1014 column += output( "msg.pot %s.res:", obj );
1016 else if (!strcmp( ext, "idl" )) /* IDL file */
1018 char *targets[8];
1019 int nb_targets = 0;
1020 char ending[] = "_?.c";
1021 const char *suffix;
1022 char *header = strmake( "%s.h", obj );
1024 if (find_target_src_file( source->name, ".tlb" ))
1026 output( "%s.tlb %s_t.res: $(WIDL)\n", obj, obj );
1027 output( "\t$(WIDL) $(TARGETFLAGS) $(IDLFLAGS) -t -o $@ %s\n", source->filename );
1028 targets[nb_targets++] = strmake( "%s.tlb", obj );
1029 targets[nb_targets++] = strmake( "%s_t.res", obj );
1032 for (suffix = "cips"; *suffix; suffix++)
1034 ending[1] = *suffix;
1035 if (!find_target_src_file( source->name, ending )) continue;
1036 output( "%s%s: $(WIDL)\n", obj, ending );
1037 output( "\t$(WIDL) $(IDLFLAGS) -%c -o $@ %s\n",
1038 *suffix == 'i' ? 'u' : *suffix, source->filename );
1039 targets[nb_targets++] = strmake( "%s%s", obj, ending );
1042 if (find_target_src_file( source->name, "_r.res" ))
1044 output( "%s_r.res: $(WIDL)\n", obj );
1045 output( "\t$(WIDL) $(IDLFLAGS) -r -o $@ %s\n", source->filename );
1046 targets[nb_targets++] = strmake( "%s_r.res", obj );
1049 if (!nb_targets || find_include_file( header ))
1051 output( "%s.h: $(WIDL)\n", obj );
1052 output( "\t$(WIDL) $(IDLFLAGS) -h -o $@ %s\n", source->filename );
1053 targets[nb_targets++] = header;
1055 else free( header );
1057 for (i = 0; i < nb_targets; i++)
1059 column += output( "%s%c", targets[i], i < nb_targets - 1 ? ' ' : ':' );
1060 free( targets[i] );
1062 column += output( " %s", source->filename );
1064 else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" ) || !strcmp( ext, "pot" ))
1066 continue; /* nothing to do for typelib files */
1068 else
1070 struct object_extension *ext;
1071 LIST_FOR_EACH_ENTRY( ext, &object_extensions, struct object_extension, entry )
1072 column += output( "%s.%s ", obj, ext->extension );
1073 column += output( ": %s", source->filename );
1075 free( obj );
1077 for (i = 0; i < MAX_INCLUDES; i++)
1078 if (source->files[i]) output_include( source->files[i], source, &column );
1079 output( "\n" );
1082 /* rules for files that depend on multiple sources */
1084 if (po_srcs)
1086 column = output( "rsrc.pot: $(WRC)" );
1087 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1088 if (strendswith( source->name, ".rc" ) && find_target_src_file( source->name, ".pot" ))
1089 output_filename( source->filename, &column );
1090 output( "\n" );
1091 column = output( "\t$(WRC) $(RCFLAGS) -O pot -o $@" );
1092 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1093 if (strendswith( source->name, ".rc" ) && find_target_src_file( source->name, ".pot" ))
1094 output_filename( source->filename, &column );
1095 output( "\n" );
1098 if (mc_srcs)
1100 column = output( "msg.pot: $(WMC)" );
1101 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1102 if (strendswith( source->name, ".mc" )) output_filename( source->filename, &column );
1103 output( "\n" );
1104 column = output( "\t$(WMC) -O pot -o $@" );
1105 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1106 if (strendswith( source->name, ".mc" )) output_filename( source->filename, &column );
1107 output( "\n" );
1110 if (find_src_file( "dlldata.o" ))
1112 output( "dlldata.c: $(WIDL) Makefile.in\n" );
1113 column = output( "\t$(WIDL) $(IDLFLAGS) --dlldata-only -o $@" );
1114 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1115 if (strendswith( source->name, ".idl" ) && find_target_src_file( source->name, "_p.c" ))
1116 output_filename( source->filename, &column );
1117 output( "\n" );
1120 if (find_src_file( "testlist.o" ))
1122 output( "testlist.c: $(MAKECTESTS) Makefile.in\n" );
1123 column = output( "\t$(MAKECTESTS) -o $@" );
1124 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1125 if (strendswith( source->name, ".c" ) && !is_generated_idl( source ))
1126 output_filename( source->filename, &column );
1127 output( "\n" );
1132 /*******************************************************************
1133 * create_temp_file
1135 static FILE *create_temp_file( char **tmp_name )
1137 char *name = xmalloc( strlen(OutputFileName) + 13 );
1138 unsigned int i, id = getpid();
1139 int fd;
1140 FILE *ret = NULL;
1142 for (i = 0; i < 100; i++)
1144 sprintf( name, "%s.tmp%08x", OutputFileName, id );
1145 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
1147 ret = fdopen( fd, "w" );
1148 break;
1150 if (errno != EEXIST) break;
1151 id += 7777;
1153 if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
1154 *tmp_name = name;
1155 return ret;
1159 /*******************************************************************
1160 * output_dependencies
1162 static void output_dependencies(void)
1164 char *tmp_name = NULL;
1166 if (Separator && ((output_file = fopen( OutputFileName, "r" ))))
1168 char buffer[1024];
1169 FILE *tmp_file = create_temp_file( &tmp_name );
1170 int found = 0;
1172 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
1174 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
1175 fatal_error( "failed to write to %s\n", tmp_name );
1176 found = !strncmp( buffer, Separator, strlen(Separator) );
1178 fclose( output_file );
1179 output_file = tmp_file;
1180 if (!found && list_head(&sources)) output( "\n%s\n", Separator );
1182 else
1184 if (!(output_file = fopen( OutputFileName, Separator ? "a" : "w" )))
1185 fatal_perror( "%s", OutputFileName );
1188 output_sources();
1190 fclose( output_file );
1191 output_file = NULL;
1193 if (tmp_name)
1195 int ret = rename( tmp_name, OutputFileName );
1196 if (ret == -1 && errno == EEXIST)
1198 /* rename doesn't overwrite on windows */
1199 unlink( OutputFileName );
1200 ret = rename( tmp_name, OutputFileName );
1202 if (ret == -1)
1204 unlink( tmp_name );
1205 fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
1207 free( tmp_name );
1212 /*******************************************************************
1213 * parse_option
1215 static void parse_option( const char *opt )
1217 switch(opt[1])
1219 case 'I':
1220 if (opt[2]) add_include_path( opt + 2 );
1221 break;
1222 case 'C':
1223 src_dir = opt + 2;
1224 break;
1225 case 'S':
1226 top_src_dir = opt + 2;
1227 break;
1228 case 'T':
1229 top_obj_dir = opt + 2;
1230 break;
1231 case 'f':
1232 if (opt[2]) OutputFileName = opt + 2;
1233 break;
1234 case 's':
1235 if (opt[2]) Separator = opt + 2;
1236 else Separator = NULL;
1237 break;
1238 case 'x':
1239 if (opt[2]) add_object_extension( opt + 2 );
1240 break;
1241 default:
1242 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
1243 exit(1);
1248 /*******************************************************************
1249 * main
1251 int main( int argc, char *argv[] )
1253 struct incl_file *pFile;
1254 struct incl_path *path, *next;
1255 int i, j;
1257 i = 1;
1258 while (i < argc)
1260 if (argv[i][0] == '-')
1262 parse_option( argv[i] );
1263 for (j = i; j < argc; j++) argv[j] = argv[j+1];
1264 argc--;
1266 else i++;
1269 /* ignore redundant source paths */
1270 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
1271 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
1273 /* set the default extension list for object files */
1274 if (list_empty( &object_extensions ))
1275 add_object_extension( "o" );
1277 /* get rid of absolute paths that don't point into the source dir */
1278 LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, struct incl_path, entry )
1280 if (path->name[0] != '/') continue;
1281 if (top_src_dir)
1283 if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) ) &&
1284 path->name[strlen(top_src_dir)] == '/') continue;
1286 list_remove( &path->entry );
1287 free( path );
1290 for (i = 1; i < argc; i++) add_src_file( argv[i] );
1292 LIST_FOR_EACH_ENTRY( pFile, &includes, struct incl_file, entry ) parse_file( pFile, 0 );
1293 output_dependencies();
1294 return 0;