msvcp90: Return last index in string::find_last_not_of_cstr_substr if input is empty.
[wine.git] / tools / makedep.c
blob3b441be01bce4d17b48ae55e4a9034ad97e86fd9
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 unsigned int flags; /* flags (see below) */
49 struct incl_file *owner;
50 struct incl_file *files[MAX_INCLUDES];
53 #define FLAG_SYSTEM 0x0001 /* is it a system include (#include <name>) */
54 #define FLAG_IDL_PROXY 0x0002 /* generates a proxy (_p.c) file */
55 #define FLAG_IDL_CLIENT 0x0004 /* generates a client (_c.c) file */
56 #define FLAG_IDL_SERVER 0x0008 /* generates a server (_s.c) file */
57 #define FLAG_IDL_IDENT 0x0010 /* generates an ident (_i.c) file */
58 #define FLAG_IDL_REGISTER 0x0020 /* generates a registration (_r.res) file */
59 #define FLAG_IDL_TYPELIB 0x0040 /* generates a typelib (.tlb) file */
60 #define FLAG_IDL_HEADER 0x0080 /* generates a header (.h) file */
61 #define FLAG_RC_PO 0x0100 /* rc file contains translations */
62 #define FLAG_C_IMPLIB 0x0200 /* file is part of an import library */
64 static const struct
66 unsigned int flag;
67 const char *ext;
68 const char *widl_arg;
69 } idl_outputs[] =
71 { FLAG_IDL_TYPELIB, ".tlb", "$(TARGETFLAGS) $(IDLFLAGS) -t" },
72 { FLAG_IDL_TYPELIB, "_t.res", "$(TARGETFLAGS) $(IDLFLAGS) -t" },
73 { FLAG_IDL_CLIENT, "_c.c", "$(IDLFLAGS) -c" },
74 { FLAG_IDL_IDENT, "_i.c", "$(IDLFLAGS) -u" },
75 { FLAG_IDL_PROXY, "_p.c", "$(IDLFLAGS) -p" },
76 { FLAG_IDL_SERVER, "_s.c", "$(IDLFLAGS) -s" },
77 { FLAG_IDL_REGISTER, "_r.res", "$(IDLFLAGS) -r" },
78 { FLAG_IDL_HEADER, ".h", "$(IDLFLAGS) -h" },
81 static struct list sources = LIST_INIT(sources);
82 static struct list includes = LIST_INIT(includes);
84 struct object_extension
86 struct list entry;
87 const char *extension;
90 static struct list object_extensions = LIST_INIT(object_extensions);
92 struct incl_path
94 struct list entry;
95 const char *name;
98 static struct list paths = LIST_INIT(paths);
100 struct strarray
102 unsigned int count; /* strings in use */
103 unsigned int size; /* total allocated size */
104 const char **str;
107 static const char *src_dir;
108 static const char *top_src_dir;
109 static const char *top_obj_dir;
110 static const char *parent_dir;
111 static const char *OutputFileName = "Makefile";
112 static const char *Separator = "### Dependencies";
113 static const char *input_file_name;
114 static int relative_dir_mode;
115 static int input_line;
116 static FILE *output_file;
118 static const char Usage[] =
119 "Usage: makedep [options] [files]\n"
120 "Options:\n"
121 " -Idir Search for include files in directory 'dir'\n"
122 " -Cdir Search for source files in directory 'dir'\n"
123 " -Sdir Set the top source directory\n"
124 " -Tdir Set the top object directory\n"
125 " -Pdir Set the parent source directory\n"
126 " -R from to Compute the relative path between two directories\n"
127 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
128 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
131 #ifndef __GNUC__
132 #define __attribute__(x)
133 #endif
135 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
136 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
137 static int output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
138 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
140 /*******************************************************************
141 * fatal_error
143 static void fatal_error( const char *msg, ... )
145 va_list valist;
146 va_start( valist, msg );
147 if (input_file_name)
149 fprintf( stderr, "%s:", input_file_name );
150 if (input_line) fprintf( stderr, "%d:", input_line );
151 fprintf( stderr, " error: " );
153 else fprintf( stderr, "makedep: error: " );
154 vfprintf( stderr, msg, valist );
155 va_end( valist );
156 exit(1);
160 /*******************************************************************
161 * fatal_perror
163 static void fatal_perror( const char *msg, ... )
165 va_list valist;
166 va_start( valist, msg );
167 if (input_file_name)
169 fprintf( stderr, "%s:", input_file_name );
170 if (input_line) fprintf( stderr, "%d:", input_line );
171 fprintf( stderr, " error: " );
173 else fprintf( stderr, "makedep: error: " );
174 vfprintf( stderr, msg, valist );
175 perror( " " );
176 va_end( valist );
177 exit(1);
181 /*******************************************************************
182 * xmalloc
184 static void *xmalloc( size_t size )
186 void *res;
187 if (!(res = malloc (size ? size : 1)))
188 fatal_error( "Virtual memory exhausted.\n" );
189 return res;
193 /*******************************************************************
194 * xrealloc
196 static void *xrealloc (void *ptr, size_t size)
198 void *res;
199 assert( size );
200 if (!(res = realloc( ptr, size )))
201 fatal_error( "Virtual memory exhausted.\n" );
202 return res;
205 /*******************************************************************
206 * xstrdup
208 static char *xstrdup( const char *str )
210 char *res = strdup( str );
211 if (!res) fatal_error( "Virtual memory exhausted.\n" );
212 return res;
216 /*******************************************************************
217 * strmake
219 static char *strmake( const char* fmt, ... )
221 int n;
222 size_t size = 100;
223 va_list ap;
225 for (;;)
227 char *p = xmalloc (size);
228 va_start(ap, fmt);
229 n = vsnprintf (p, size, fmt, ap);
230 va_end(ap);
231 if (n == -1) size *= 2;
232 else if ((size_t)n >= size) size = n + 1;
233 else return p;
234 free(p);
239 /*******************************************************************
240 * strendswith
242 static int strendswith( const char* str, const char* end )
244 int l = strlen(str);
245 int m = strlen(end);
247 return l >= m && strcmp(str + l - m, end) == 0;
251 /*******************************************************************
252 * output
254 static int output( const char *format, ... )
256 int ret;
257 va_list valist;
259 va_start( valist, format );
260 ret = vfprintf( output_file, format, valist );
261 va_end( valist );
262 if (ret < 0) fatal_perror( "output" );
263 return ret;
267 /*******************************************************************
268 * strarray_init
270 static void strarray_init( struct strarray *array )
272 array->count = 0;
273 array->size = 0;
274 array->str = NULL;
278 /*******************************************************************
279 * strarray_add
281 static void strarray_add( struct strarray *array, const char *str )
283 if (array->count == array->size)
285 if (array->size) array->size *= 2;
286 else array->size = 16;
287 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
289 array->str[array->count++] = str;
293 /*******************************************************************
294 * output_filename
296 static void output_filename( const char *name, int *column )
298 if (*column + strlen(name) + 1 > 100)
300 output( " \\\n" );
301 *column = 0;
303 *column += output( " %s", name );
307 /*******************************************************************
308 * get_extension
310 static char *get_extension( char *filename )
312 char *ext = strrchr( filename, '.' );
313 if (ext && strchr( ext, '/' )) ext = NULL;
314 return ext;
318 /*******************************************************************
319 * replace_extension
321 static char *replace_extension( const char *name, unsigned int old_len, const char *new_ext )
323 char *ret = xmalloc( strlen( name ) + strlen( new_ext ) + 1 );
324 strcpy( ret, name );
325 strcpy( ret + strlen( ret ) - old_len, new_ext );
326 return ret;
330 /*******************************************************************
331 * get_relative_path
333 * Determine where the destination path is located relative to the 'from' path.
335 static char *get_relative_path( const char *from, const char *dest )
337 const char *start;
338 char *ret, *p;
339 unsigned int dotdots = 0;
341 /* a path of "." is equivalent to an empty path */
342 if (!strcmp( from, "." )) from = "";
344 for (;;)
346 while (*from == '/') from++;
347 while (*dest == '/') dest++;
348 start = dest; /* save start of next path element */
349 if (!*from) break;
351 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
352 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
354 /* count remaining elements in 'from' */
357 dotdots++;
358 while (*from && *from != '/') from++;
359 while (*from == '/') from++;
361 while (*from);
362 break;
365 if (!start[0] && !dotdots) return NULL; /* empty path */
367 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
368 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
370 if (start[0]) strcpy( p, start );
371 else p[-1] = 0; /* remove trailing slash */
372 return ret;
376 /*******************************************************************
377 * get_line
379 static char *get_line( FILE *file )
381 static char *buffer;
382 static unsigned int size;
384 if (!size)
386 size = 1024;
387 buffer = xmalloc( size );
389 if (!fgets( buffer, size, file )) return NULL;
390 input_line++;
392 for (;;)
394 char *p = buffer + strlen(buffer);
395 /* if line is larger than buffer, resize buffer */
396 while (p == buffer + size - 1 && p[-1] != '\n')
398 buffer = xrealloc( buffer, size * 2 );
399 if (!fgets( buffer + size - 1, size + 1, file )) break;
400 p = buffer + strlen(buffer);
401 size *= 2;
403 if (p > buffer && p[-1] == '\n')
405 *(--p) = 0;
406 if (p > buffer && p[-1] == '\r') *(--p) = 0;
407 if (p > buffer && p[-1] == '\\')
409 *(--p) = 0;
410 /* line ends in backslash, read continuation line */
411 if (!fgets( p, size - (p - buffer), file )) return buffer;
412 input_line++;
413 continue;
416 return buffer;
420 /*******************************************************************
421 * add_object_extension
423 * Add an extension for object files.
425 static void add_object_extension( const char *ext )
427 struct object_extension *object_extension = xmalloc( sizeof(*object_extension) );
428 list_add_tail( &object_extensions, &object_extension->entry );
429 object_extension->extension = ext;
432 /*******************************************************************
433 * add_include_path
435 * Add a directory to the include path.
437 static void add_include_path( const char *name )
439 struct incl_path *path = xmalloc( sizeof(*path) );
440 list_add_tail( &paths, &path->entry );
441 path->name = name;
444 /*******************************************************************
445 * find_src_file
447 static struct incl_file *find_src_file( const char *name )
449 struct incl_file *file;
451 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
452 if (!strcmp( name, file->name )) return file;
453 return NULL;
456 /*******************************************************************
457 * find_include_file
459 static struct incl_file *find_include_file( const char *name )
461 struct incl_file *file;
463 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
464 if (!strcmp( name, file->name )) return file;
465 return NULL;
468 /*******************************************************************
469 * add_include
471 * Add an include file if it doesn't already exists.
473 static struct incl_file *add_include( struct incl_file *pFile, const char *name, int system )
475 struct incl_file *include;
476 char *ext;
477 int pos;
479 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
480 if (pos >= MAX_INCLUDES)
481 fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
483 /* enforce some rules for the Wine tree */
485 if (!memcmp( name, "../", 3 ))
486 fatal_error( "#include directive with relative path not allowed\n" );
488 if (!strcmp( name, "config.h" ))
490 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
491 fatal_error( "config.h must not be included by a header file\n" );
492 if (pos)
493 fatal_error( "config.h must be included before anything else\n" );
495 else if (!strcmp( name, "wine/port.h" ))
497 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
498 fatal_error( "wine/port.h must not be included by a header file\n" );
499 if (!pos) fatal_error( "config.h must be included before wine/port.h\n" );
500 if (pos > 1)
501 fatal_error( "wine/port.h must be included before everything except config.h\n" );
502 if (strcmp( pFile->files[0]->name, "config.h" ))
503 fatal_error( "config.h must be included before wine/port.h\n" );
506 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
507 if (!strcmp( name, include->name )) goto found;
509 include = xmalloc( sizeof(*include) );
510 memset( include, 0, sizeof(*include) );
511 include->name = xstrdup(name);
512 include->included_by = pFile;
513 include->included_line = input_line;
514 if (system) include->flags |= FLAG_SYSTEM;
515 list_add_tail( &includes, &include->entry );
516 found:
517 pFile->files[pos] = include;
518 return include;
522 /*******************************************************************
523 * open_src_file
525 static FILE *open_src_file( struct incl_file *pFile )
527 FILE *file;
529 /* first try name as is */
530 if ((file = fopen( pFile->name, "r" )))
532 pFile->filename = xstrdup( pFile->name );
533 return file;
535 /* now try in source dir */
536 if (src_dir)
538 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
539 file = fopen( pFile->filename, "r" );
541 /* now try parent dir */
542 if (!file && parent_dir)
544 if (src_dir)
545 pFile->filename = strmake( "%s/%s/%s", src_dir, parent_dir, pFile->name );
546 else
547 pFile->filename = strmake( "%s/%s", parent_dir, pFile->name );
548 if ((file = fopen( pFile->filename, "r" ))) return file;
549 file = fopen( pFile->filename, "r" );
551 if (!file) fatal_perror( "open %s", pFile->name );
552 return file;
556 /*******************************************************************
557 * open_include_file
559 static FILE *open_include_file( struct incl_file *pFile )
561 FILE *file = NULL;
562 char *filename, *p;
563 struct incl_path *path;
565 errno = ENOENT;
567 /* check for generated bison header */
569 if (strendswith( pFile->name, ".tab.h" ))
571 filename = replace_extension( pFile->name, 6, ".y" );
572 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
574 if ((file = fopen( filename, "r" )))
576 pFile->sourcename = filename;
577 pFile->filename = xstrdup( pFile->name );
578 /* don't bother to parse it */
579 fclose( file );
580 return NULL;
582 free( filename );
585 /* check for corresponding idl file in source dir */
587 if (strendswith( pFile->name, ".h" ))
589 filename = replace_extension( pFile->name, 2, ".idl" );
590 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
592 if ((file = fopen( filename, "r" )))
594 pFile->sourcename = filename;
595 pFile->filename = xstrdup( pFile->name );
596 return file;
598 free( filename );
601 /* first try name as is */
602 if ((file = fopen( pFile->name, "r" )))
604 pFile->filename = xstrdup( pFile->name );
605 return file;
608 /* now try in source dir */
609 if (src_dir)
611 filename = strmake( "%s/%s", src_dir, pFile->name );
612 if ((file = fopen( filename, "r" ))) goto found;
613 free( filename );
616 /* now try in parent source dir */
617 if (parent_dir)
619 if (src_dir)
620 filename = strmake( "%s/%s/%s", src_dir, parent_dir, pFile->name );
621 else
622 filename = strmake( "%s/%s", parent_dir, pFile->name );
623 if ((file = fopen( filename, "r" ))) goto found;
624 free( filename );
627 /* check for corresponding idl file in global includes */
629 if (strendswith( pFile->name, ".h" ))
631 filename = replace_extension( pFile->name, 2, ".idl" );
632 if (top_src_dir)
633 filename = strmake( "%s/include/%s", top_src_dir, filename );
634 else if (top_obj_dir)
635 filename = strmake( "%s/include/%s", top_obj_dir, filename );
636 else
637 filename = NULL;
639 if (filename && (file = fopen( filename, "r" )))
641 pFile->sourcename = filename;
642 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
643 return file;
645 free( filename );
648 /* check for corresponding .x file in global includes */
650 if (strendswith( pFile->name, "tmpl.h" ))
652 filename = replace_extension( pFile->name, 2, ".x" );
653 if (top_src_dir)
654 filename = strmake( "%s/include/%s", top_src_dir, filename );
655 else if (top_obj_dir)
656 filename = strmake( "%s/include/%s", top_obj_dir, filename );
657 else
658 filename = NULL;
660 if (filename && (file = fopen( filename, "r" )))
662 pFile->sourcename = filename;
663 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
664 return file;
666 free( filename );
669 /* now try in global includes */
670 if (top_obj_dir)
672 filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
673 if ((file = fopen( filename, "r" ))) goto found;
674 free( filename );
676 if (top_src_dir)
678 filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
679 if ((file = fopen( filename, "r" ))) goto found;
680 free( filename );
683 /* now search in include paths */
684 LIST_FOR_EACH_ENTRY( path, &paths, struct incl_path, entry )
686 filename = strmake( "%s/%s", path->name, pFile->name );
687 if ((file = fopen( filename, "r" ))) goto found;
688 free( filename );
690 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
692 /* try in src file directory */
693 if ((p = strrchr(pFile->included_by->filename, '/')))
695 int l = p - pFile->included_by->filename + 1;
696 filename = xmalloc(l + strlen(pFile->name) + 1);
697 memcpy( filename, pFile->included_by->filename, l );
698 strcpy( filename + l, pFile->name );
699 if ((file = fopen( filename, "r" ))) goto found;
700 free( filename );
703 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
704 perror( pFile->name );
705 pFile = pFile->included_by;
706 while (pFile && pFile->included_by)
708 const char *parent = pFile->included_by->sourcename;
709 if (!parent) parent = pFile->included_by->name;
710 fprintf( stderr, "%s:%d: note: %s was first included here\n",
711 parent, pFile->included_line, pFile->name );
712 pFile = pFile->included_by;
714 exit(1);
716 found:
717 pFile->filename = filename;
718 return file;
722 /*******************************************************************
723 * parse_include_directive
725 static void parse_include_directive( struct incl_file *source, char *str )
727 char quote, *include, *p = str;
729 while (*p && isspace(*p)) p++;
730 if (*p != '\"' && *p != '<' ) return;
731 quote = *p++;
732 if (quote == '<') quote = '>';
733 include = p;
734 while (*p && (*p != quote)) p++;
735 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
736 *p = 0;
737 add_include( source, include, (quote == '>') );
741 /*******************************************************************
742 * parse_pragma_directive
744 static void parse_pragma_directive( struct incl_file *source, char *str )
746 char *flag, *p = str;
748 if (!isspace( *p )) return;
749 while (*p && isspace(*p)) p++;
750 p = strtok( p, " \t" );
751 if (strcmp( p, "makedep" )) return;
753 while ((flag = strtok( NULL, " \t" )))
755 if (!strcmp( flag, "depend" ))
757 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
758 return;
760 if (strendswith( source->name, ".idl" ))
762 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
763 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
764 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
765 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
766 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
767 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
768 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
770 else if (strendswith( source->name, ".rc" ))
772 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
774 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
779 /*******************************************************************
780 * parse_cpp_directive
782 static void parse_cpp_directive( struct incl_file *source, char *str )
784 while (*str && isspace(*str)) str++;
785 if (*str++ != '#') return;
786 while (*str && isspace(*str)) str++;
788 if (!strncmp( str, "include", 7 ))
789 parse_include_directive( source, str + 7 );
790 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
791 parse_include_directive( source, str + 6 );
792 else if (!strncmp( str, "pragma", 6 ))
793 parse_pragma_directive( source, str + 6 );
797 /*******************************************************************
798 * parse_idl_file
800 * If for_h_file is non-zero, it means we are not interested in the idl file
801 * itself, but only in the contents of the .h file that will be generated from it.
803 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
805 char *buffer, *include;
807 input_line = 0;
808 if (for_h_file)
810 /* generated .h file always includes these */
811 add_include( pFile, "rpc.h", 1 );
812 add_include( pFile, "rpcndr.h", 1 );
815 while ((buffer = get_line( file )))
817 char quote;
818 char *p = buffer;
819 while (*p && isspace(*p)) p++;
821 if (!strncmp( p, "import", 6 ))
823 p += 6;
824 while (*p && isspace(*p)) p++;
825 if (*p != '"') continue;
826 include = ++p;
827 while (*p && (*p != '"')) p++;
828 if (!*p) fatal_error( "malformed import directive\n" );
829 *p = 0;
830 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
831 add_include( pFile, include, 0 );
832 continue;
835 if (for_h_file) /* only check for #include inside cpp_quote */
837 if (strncmp( p, "cpp_quote", 9 )) continue;
838 p += 9;
839 while (*p && isspace(*p)) p++;
840 if (*p++ != '(') continue;
841 while (*p && isspace(*p)) p++;
842 if (*p++ != '"') continue;
843 if (*p++ != '#') continue;
844 while (*p && isspace(*p)) p++;
845 if (strncmp( p, "include", 7 )) continue;
846 p += 7;
847 while (*p && isspace(*p)) p++;
848 if (*p == '\\' && p[1] == '"')
850 p += 2;
851 quote = '"';
853 else
855 if (*p++ != '<' ) continue;
856 quote = '>';
858 include = p;
859 while (*p && (*p != quote)) p++;
860 if (!*p || (quote == '"' && p[-1] != '\\'))
861 fatal_error( "malformed #include directive inside cpp_quote\n" );
862 if (quote == '"') p--; /* remove backslash */
863 *p = 0;
864 add_include( pFile, include, (quote == '>') );
865 continue;
868 parse_cpp_directive( pFile, p );
872 /*******************************************************************
873 * parse_c_file
875 static void parse_c_file( struct incl_file *pFile, FILE *file )
877 char *buffer;
879 input_line = 0;
880 while ((buffer = get_line( file )))
882 parse_cpp_directive( pFile, buffer );
887 /*******************************************************************
888 * parse_rc_file
890 static void parse_rc_file( struct incl_file *pFile, FILE *file )
892 char *buffer, *include;
894 input_line = 0;
895 while ((buffer = get_line( file )))
897 char quote;
898 char *p = buffer;
899 while (*p && isspace(*p)) p++;
901 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
903 p += 2;
904 while (*p && isspace(*p)) p++;
905 if (strncmp( p, "@makedep:", 9 )) continue;
906 p += 9;
907 while (*p && isspace(*p)) p++;
908 quote = '"';
909 if (*p == quote)
911 include = ++p;
912 while (*p && *p != quote) p++;
914 else
916 include = p;
917 while (*p && !isspace(*p) && *p != '*') p++;
919 if (!*p)
920 fatal_error( "malformed makedep comment\n" );
921 *p = 0;
922 add_include( pFile, include, (quote == '>') );
923 continue;
926 parse_cpp_directive( pFile, buffer );
931 /*******************************************************************
932 * parse_generated_idl
934 static void parse_generated_idl( struct incl_file *source )
936 char *header = replace_extension( source->name, 4, ".h" );
938 source->filename = xstrdup( source->name );
940 if (strendswith( source->name, "_c.c" ))
942 add_include( source, header, 0 );
944 else if (strendswith( source->name, "_i.c" ))
946 add_include( source, "rpc.h", 1 );
947 add_include( source, "rpcndr.h", 1 );
948 add_include( source, "guiddef.h", 1 );
950 else if (strendswith( source->name, "_p.c" ))
952 add_include( source, "objbase.h", 1 );
953 add_include( source, "rpcproxy.h", 1 );
954 add_include( source, "wine/exception.h", 1 );
955 add_include( source, header, 0 );
957 else if (strendswith( source->name, "_s.c" ))
959 add_include( source, "wine/exception.h", 1 );
960 add_include( source, header, 0 );
963 free( header );
966 /*******************************************************************
967 * is_generated_idl
969 static int is_generated_idl( struct incl_file *source )
971 return (strendswith( source->name, "_c.c" ) ||
972 strendswith( source->name, "_i.c" ) ||
973 strendswith( source->name, "_p.c" ) ||
974 strendswith( source->name, "_s.c" ));
977 /*******************************************************************
978 * parse_file
980 static void parse_file( struct incl_file *source, int src )
982 FILE *file;
984 /* don't try to open certain types of files */
985 if (strendswith( source->name, ".tlb" ) ||
986 strendswith( source->name, ".x" ))
988 source->filename = xstrdup( source->name );
989 return;
992 file = src ? open_src_file( source ) : open_include_file( source );
993 if (!file) return;
994 input_file_name = source->filename;
996 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
997 parse_idl_file( source, file, 1 );
998 else if (strendswith( source->filename, ".idl" ))
999 parse_idl_file( source, file, 0 );
1000 else if (strendswith( source->filename, ".c" ) ||
1001 strendswith( source->filename, ".m" ) ||
1002 strendswith( source->filename, ".h" ) ||
1003 strendswith( source->filename, ".l" ) ||
1004 strendswith( source->filename, ".y" ))
1005 parse_c_file( source, file );
1006 else if (strendswith( source->filename, ".rc" ))
1007 parse_rc_file( source, file );
1008 fclose(file);
1009 input_file_name = NULL;
1013 /*******************************************************************
1014 * add_src_file
1016 * Add a source file to the list.
1018 static struct incl_file *add_src_file( const char *name )
1020 struct incl_file *file;
1022 if ((file = find_src_file( name ))) return file; /* we already have it */
1023 file = xmalloc( sizeof(*file) );
1024 memset( file, 0, sizeof(*file) );
1025 file->name = xstrdup(name);
1026 list_add_tail( &sources, &file->entry );
1028 /* special cases for generated files */
1030 if (is_generated_idl( file ))
1032 parse_generated_idl( file );
1033 return file;
1036 if (!strcmp( file->name, "dlldata.o" ))
1038 file->filename = xstrdup( "dlldata.c" );
1039 add_include( file, "objbase.h", 1 );
1040 add_include( file, "rpcproxy.h", 1 );
1041 return file;
1044 if (!strcmp( file->name, "testlist.o" ))
1046 file->filename = xstrdup( "testlist.c" );
1047 add_include( file, "wine/test.h", 1 );
1048 return file;
1051 if (strendswith( file->name, ".o" ))
1053 /* default to .c for unknown extra object files */
1054 file->filename = replace_extension( file->name, 2, ".c" );
1055 return file;
1058 if (strendswith( file->name, ".tlb" ) ||
1059 strendswith( file->name, ".res" ) ||
1060 strendswith( file->name, ".pot" ) ||
1061 strendswith( file->name, ".x" ))
1063 file->filename = xstrdup( file->name );
1064 return file;
1067 parse_file( file, 1 );
1068 return file;
1072 /*******************************************************************
1073 * add_generated_sources
1075 static void add_generated_sources(void)
1077 unsigned int i;
1078 struct incl_file *source, *next;
1080 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1082 if (!source->flags) continue;
1083 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1085 if (!(source->flags & idl_outputs[i].flag)) continue;
1086 if (!strendswith( idl_outputs[i].ext, ".c" )) continue;
1087 add_src_file( replace_extension( source->name, 4, idl_outputs[i].ext ));
1089 if (source->flags & FLAG_IDL_PROXY) add_src_file( "dlldata.o" );
1092 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1094 if (strendswith( source->name, "_c.c" ) ||
1095 strendswith( source->name, "_i.c" ) ||
1096 strendswith( source->name, "_p.c" ) ||
1097 strendswith( source->name, "_s.c" ) ||
1098 strendswith( source->name, ".tlb" ))
1100 char *idl = replace_extension( source->name, 4, ".idl" );
1101 struct incl_file *file = add_src_file( idl );
1102 if (strendswith( source->name, "_c.c" )) file->flags |= FLAG_IDL_CLIENT;
1103 else if (strendswith( source->name, "_i.c" )) file->flags |= FLAG_IDL_IDENT;
1104 else if (strendswith( source->name, "_p.c" )) file->flags |= FLAG_IDL_PROXY;
1105 else if (strendswith( source->name, "_s.c" )) file->flags |= FLAG_IDL_SERVER;
1106 else if (strendswith( source->name, ".tlb" )) file->flags |= FLAG_IDL_TYPELIB;
1107 continue;
1109 if (strendswith( source->name, "_r.res" ) ||
1110 strendswith( source->name, "_t.res" ))
1112 char *idl = replace_extension( source->name, 6, ".idl" );
1113 struct incl_file *file = add_src_file( idl );
1114 if (strendswith( source->name, "_r.res" )) file->flags |= FLAG_IDL_REGISTER;
1115 else if (strendswith( source->name, "_t.res" )) file->flags |= FLAG_IDL_TYPELIB;
1116 continue;
1118 if (strendswith( source->name, ".pot" ))
1120 char *rc = replace_extension( source->name, 4, ".rc" );
1121 struct incl_file *file = add_src_file( rc );
1122 file->flags |= FLAG_RC_PO;
1128 /*******************************************************************
1129 * output_include
1131 static void output_include( struct incl_file *pFile, struct incl_file *owner, int *column )
1133 int i;
1135 if (pFile->owner == owner) return;
1136 if (!pFile->filename) return;
1137 pFile->owner = owner;
1138 output_filename( pFile->filename, column );
1139 for (i = 0; i < MAX_INCLUDES; i++)
1140 if (pFile->files[i]) output_include( pFile->files[i], owner, column );
1144 /*******************************************************************
1145 * output_sources
1147 static void output_sources(void)
1149 struct incl_file *source;
1150 struct strarray clean_files;
1151 int i, column, po_srcs = 0, mc_srcs = 0;
1153 strarray_init( &clean_files );
1155 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1157 char *obj = xstrdup( source->name );
1158 char *ext = get_extension( obj );
1160 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1161 *ext++ = 0;
1162 column = 0;
1164 if (!strcmp( ext, "y" )) /* yacc file */
1166 /* add source file dependency for parallel makes */
1167 char *header = strmake( "%s.tab.h", obj );
1169 if (find_include_file( header ))
1171 output( "%s.tab.h: %s\n", obj, source->filename );
1172 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
1173 obj, obj, source->filename );
1174 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1175 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1177 else output( "%s.tab.c: %s\n", obj, source->filename );
1179 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj, source->filename );
1180 output( "%s.tab.o: %s.tab.c\n", obj, obj );
1181 output( "\t$(CC) -c $(ALLCFLAGS) -o $@ %s.tab.c\n", obj );
1182 strarray_add( &clean_files, strmake( "%s.tab.c", obj ));
1183 strarray_add( &clean_files, strmake( "%s.tab.o", obj ));
1184 column += output( "%s.tab.o:", obj );
1185 free( header );
1187 else if (!strcmp( ext, "l" )) /* lex file */
1189 output( "%s.yy.c: %s\n", obj, source->filename );
1190 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source->filename );
1191 output( "%s.yy.o: %s.yy.c\n", obj, obj );
1192 output( "\t$(CC) -c $(ALLCFLAGS) -o $@ %s.yy.c\n", obj );
1193 strarray_add( &clean_files, strmake( "%s.yy.c", obj ));
1194 strarray_add( &clean_files, strmake( "%s.yy.o", obj ));
1195 column += output( "%s.yy.o:", obj );
1197 else if (!strcmp( ext, "rc" )) /* resource file */
1199 if (source->flags & FLAG_RC_PO)
1201 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj, source->filename );
1202 output( "\t$(WRC) $(RCFLAGS) -o $@ %s\n", source->filename );
1203 column += output( "%s.res rsrc.pot:", obj );
1204 po_srcs++;
1206 else
1208 output( "%s.res: $(WRC) %s\n", obj, source->filename );
1209 output( "\t$(WRC) $(RCFLAGS) -o $@ %s\n", source->filename );
1210 column += output( "%s.res:", obj );
1212 strarray_add( &clean_files, strmake( "%s.res", obj ));
1214 else if (!strcmp( ext, "mc" )) /* message file */
1216 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj, source->filename );
1217 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source->filename );
1218 strarray_add( &clean_files, strmake( "%s.res", obj ));
1219 mc_srcs++;
1220 column += output( "msg.pot %s.res:", obj );
1222 else if (!strcmp( ext, "idl" )) /* IDL file */
1224 char *targets[8];
1225 unsigned int i, nb_targets = 0;
1227 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1228 source->flags |= FLAG_IDL_HEADER;
1230 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1232 if (!(source->flags & idl_outputs[i].flag)) continue;
1233 output( "%s%s: $(WIDL)\n", obj, idl_outputs[i].ext );
1234 output( "\t$(WIDL) %s -o $@ %s\n", idl_outputs[i].widl_arg, source->filename );
1235 targets[nb_targets++] = strmake( "%s%s", obj, idl_outputs[i].ext );
1237 for (i = 0; i < nb_targets; i++)
1239 column += output( "%s%c", targets[i], i < nb_targets - 1 ? ' ' : ':' );
1240 strarray_add( &clean_files, targets[i] );
1242 column += output( " %s", source->filename );
1244 else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" ) || !strcmp( ext, "pot" ))
1246 continue; /* nothing to do for typelib files */
1248 else
1250 struct object_extension *ext;
1251 LIST_FOR_EACH_ENTRY( ext, &object_extensions, struct object_extension, entry )
1253 strarray_add( &clean_files, strmake( "%s.%s", obj, ext->extension ));
1254 output( "%s.%s: %s\n", obj, ext->extension, source->filename );
1255 if (strstr( ext->extension, "cross" ))
1256 output( "\t$(CROSSCC) -c $(ALLCROSSCFLAGS) -o $@ %s\n", source->filename );
1257 else
1258 output( "\t$(CC) -c $(ALLCFLAGS) -o $@ %s\n", source->filename );
1260 if (source->flags & FLAG_C_IMPLIB)
1262 strarray_add( &clean_files, strmake( "%s.cross.o", obj ));
1263 output( "%s.cross.o: %s\n", obj, source->filename );
1264 output( "\t$(CROSSCC) -c $(ALLCROSSCFLAGS) -o $@ %s\n", source->filename );
1266 LIST_FOR_EACH_ENTRY( ext, &object_extensions, struct object_extension, entry )
1267 column += output( "%s.%s ", obj, ext->extension );
1268 if (source->flags & FLAG_C_IMPLIB) column += output( "%s.cross.o", obj );
1269 column += output( ":" );
1271 free( obj );
1273 for (i = 0; i < MAX_INCLUDES; i++)
1274 if (source->files[i]) output_include( source->files[i], source, &column );
1275 output( "\n" );
1278 /* rules for files that depend on multiple sources */
1280 if (po_srcs)
1282 column = output( "rsrc.pot: $(WRC)" );
1283 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1284 if (source->flags & FLAG_RC_PO) output_filename( source->filename, &column );
1285 output( "\n" );
1286 column = output( "\t$(WRC) $(RCFLAGS) -O pot -o $@" );
1287 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1288 if (source->flags & FLAG_RC_PO) output_filename( source->filename, &column );
1289 output( "\n" );
1290 strarray_add( &clean_files, "rsrc.pot" );
1293 if (mc_srcs)
1295 column = output( "msg.pot: $(WMC)" );
1296 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1297 if (strendswith( source->name, ".mc" )) output_filename( source->filename, &column );
1298 output( "\n" );
1299 column = output( "\t$(WMC) -O pot -o $@" );
1300 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1301 if (strendswith( source->name, ".mc" )) output_filename( source->filename, &column );
1302 output( "\n" );
1303 strarray_add( &clean_files, "msg.pot" );
1306 if (find_src_file( "dlldata.o" ))
1308 output( "dlldata.c: $(WIDL) Makefile.in\n" );
1309 column = output( "\t$(WIDL) $(IDLFLAGS) --dlldata-only -o $@" );
1310 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1311 if (source->flags & FLAG_IDL_PROXY) output_filename( source->filename, &column );
1312 output( "\n" );
1313 strarray_add( &clean_files, "dlldata.c" );
1316 if (find_src_file( "testlist.o" ))
1318 output( "testlist.c: $(MAKECTESTS) Makefile.in\n" );
1319 column = output( "\t$(MAKECTESTS) -o $@" );
1320 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1321 if (strendswith( source->name, ".c" ) && !is_generated_idl( source ))
1322 output_filename( source->filename, &column );
1323 output( "\n" );
1324 strarray_add( &clean_files, "testlist.c" );
1327 if (clean_files.count)
1329 output( "clean::\n" );
1330 column = output( "\t$(RM)" );
1331 for (i = 0; i < clean_files.count; i++) output_filename( clean_files.str[i], &column );
1332 output( "\n" );
1337 /*******************************************************************
1338 * create_temp_file
1340 static FILE *create_temp_file( char **tmp_name )
1342 char *name = xmalloc( strlen(OutputFileName) + 13 );
1343 unsigned int i, id = getpid();
1344 int fd;
1345 FILE *ret = NULL;
1347 for (i = 0; i < 100; i++)
1349 sprintf( name, "%s.tmp%08x", OutputFileName, id );
1350 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
1352 ret = fdopen( fd, "w" );
1353 break;
1355 if (errno != EEXIST) break;
1356 id += 7777;
1358 if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
1359 *tmp_name = name;
1360 return ret;
1364 /*******************************************************************
1365 * output_dependencies
1367 static void output_dependencies(void)
1369 char *tmp_name = NULL;
1371 if (Separator && ((output_file = fopen( OutputFileName, "r" ))))
1373 char buffer[1024];
1374 FILE *tmp_file = create_temp_file( &tmp_name );
1375 int found = 0;
1377 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
1379 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
1380 fatal_error( "failed to write to %s\n", tmp_name );
1381 found = !strncmp( buffer, Separator, strlen(Separator) );
1383 fclose( output_file );
1384 output_file = tmp_file;
1385 if (!found && list_head(&sources)) output( "\n%s\n", Separator );
1387 else
1389 if (!(output_file = fopen( OutputFileName, Separator ? "a" : "w" )))
1390 fatal_perror( "%s", OutputFileName );
1393 output_sources();
1395 fclose( output_file );
1396 output_file = NULL;
1398 if (tmp_name)
1400 int ret = rename( tmp_name, OutputFileName );
1401 if (ret == -1 && errno == EEXIST)
1403 /* rename doesn't overwrite on windows */
1404 unlink( OutputFileName );
1405 ret = rename( tmp_name, OutputFileName );
1407 if (ret == -1)
1409 unlink( tmp_name );
1410 fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
1412 free( tmp_name );
1417 /*******************************************************************
1418 * parse_option
1420 static void parse_option( const char *opt )
1422 switch(opt[1])
1424 case 'I':
1425 if (opt[2]) add_include_path( opt + 2 );
1426 break;
1427 case 'C':
1428 src_dir = opt + 2;
1429 break;
1430 case 'S':
1431 top_src_dir = opt + 2;
1432 break;
1433 case 'T':
1434 top_obj_dir = opt + 2;
1435 break;
1436 case 'P':
1437 parent_dir = opt + 2;
1438 break;
1439 case 'f':
1440 if (opt[2]) OutputFileName = opt + 2;
1441 break;
1442 case 'R':
1443 relative_dir_mode = 1;
1444 break;
1445 case 's':
1446 if (opt[2]) Separator = opt + 2;
1447 else Separator = NULL;
1448 break;
1449 case 'x':
1450 if (opt[2]) add_object_extension( opt + 2 );
1451 break;
1452 default:
1453 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
1454 exit(1);
1459 /*******************************************************************
1460 * main
1462 int main( int argc, char *argv[] )
1464 struct incl_file *pFile;
1465 struct incl_path *path, *next;
1466 int i, j;
1468 i = 1;
1469 while (i < argc)
1471 if (argv[i][0] == '-')
1473 parse_option( argv[i] );
1474 for (j = i; j < argc; j++) argv[j] = argv[j+1];
1475 argc--;
1477 else i++;
1480 if (relative_dir_mode)
1482 char *relpath;
1484 if (argc != 3)
1486 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
1487 exit( 1 );
1489 relpath = get_relative_path( argv[1], argv[2] );
1490 printf( "%s\n", relpath ? relpath : "." );
1491 exit( 0 );
1494 /* ignore redundant source paths */
1495 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
1496 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
1498 /* set the default extension list for object files */
1499 if (list_empty( &object_extensions ))
1500 add_object_extension( "o" );
1502 /* get rid of absolute paths that don't point into the source dir */
1503 LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, struct incl_path, entry )
1505 if (path->name[0] != '/') continue;
1506 if (top_src_dir)
1508 if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) ) &&
1509 path->name[strlen(top_src_dir)] == '/') continue;
1511 list_remove( &path->entry );
1512 free( path );
1515 for (i = 1; i < argc; i++) add_src_file( argv[i] );
1516 add_generated_sources();
1518 LIST_FOR_EACH_ENTRY( pFile, &includes, struct incl_file, entry ) parse_file( pFile, 0 );
1519 output_dependencies();
1520 return 0;