wined3d: Add shader version/end masks to the WINED3D namespace.
[wine/dibdrv.git] / tools / makedep.c
blob300ed8d4cabb0bb2a97e6361621cc9d088d06644
1 /*
2 * Generate include file dependencies
4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include "wine/list.h"
37 /* Max first-level includes per file */
38 #define MAX_INCLUDES 200
40 typedef struct _INCL_FILE
42 struct list entry;
43 char *name;
44 char *filename;
45 char *sourcename; /* source file name for generated headers */
46 struct _INCL_FILE *included_by; /* file that included this one */
47 int included_line; /* line where this file was included */
48 int system; /* is it a system include (#include <name>) */
49 struct _INCL_FILE *owner;
50 struct _INCL_FILE *files[MAX_INCLUDES];
51 } INCL_FILE;
53 static struct list sources = LIST_INIT(sources);
54 static struct list includes = LIST_INIT(includes);
56 typedef struct _INCL_PATH
58 struct list entry;
59 const char *name;
60 } INCL_PATH;
62 static struct list paths = LIST_INIT(paths);
64 static const char *src_dir;
65 static const char *top_src_dir;
66 static const char *top_obj_dir;
67 static const char *OutputFileName = "Makefile";
68 static const char *Separator = "### Dependencies";
69 static const char *ProgramName;
70 static int input_line;
72 static const char Usage[] =
73 "Usage: %s [options] [files]\n"
74 "Options:\n"
75 " -Idir Search for include files in directory 'dir'\n"
76 " -Cdir Search for source files in directory 'dir'\n"
77 " -Sdir Set the top source directory\n"
78 " -Sdir Set the top object directory\n"
79 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
80 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
83 /*******************************************************************
84 * fatal_error
86 static void fatal_error( const char *msg, ... )
88 va_list valist;
89 va_start( valist, msg );
90 vfprintf( stderr, msg, valist );
91 va_end( valist );
92 exit(1);
96 /*******************************************************************
97 * xmalloc
99 static void *xmalloc( size_t size )
101 void *res;
102 if (!(res = malloc (size ? size : 1)))
103 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
104 return res;
108 /*******************************************************************
109 * xrealloc
111 static void *xrealloc (void *ptr, size_t size)
113 void *res;
114 assert( size );
115 if (!(res = realloc( ptr, size )))
116 fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
117 return res;
120 /*******************************************************************
121 * xstrdup
123 static char *xstrdup( const char *str )
125 char *res = strdup( str );
126 if (!res) fatal_error( "%s: Virtual memory exhausted.\n", ProgramName );
127 return res;
131 /*******************************************************************
132 * strmake
134 static char *strmake( const char* fmt, ... )
136 int n;
137 size_t size = 100;
138 va_list ap;
140 for (;;)
142 char *p = xmalloc (size);
143 va_start(ap, fmt);
144 n = vsnprintf (p, size, fmt, ap);
145 va_end(ap);
146 if (n == -1) size *= 2;
147 else if ((size_t)n >= size) size = n + 1;
148 else return p;
149 free(p);
154 /*******************************************************************
155 * strendswith
157 static int strendswith( const char* str, const char* end )
159 int l = strlen(str);
160 int m = strlen(end);
162 return l >= m && strcmp(str + l - m, end) == 0;
165 /*******************************************************************
166 * get_extension
168 static char *get_extension( char *filename )
170 char *ext = strrchr( filename, '.' );
171 if (ext && strchr( ext, '/' )) ext = NULL;
172 return ext;
176 /*******************************************************************
177 * get_line
179 static char *get_line( FILE *file )
181 static char *buffer;
182 static unsigned int size;
184 if (!size)
186 size = 1024;
187 buffer = xmalloc( size );
189 if (!fgets( buffer, size, file )) return NULL;
190 input_line++;
192 for (;;)
194 char *p = buffer + strlen(buffer);
195 /* if line is larger than buffer, resize buffer */
196 while (p == buffer + size - 1 && p[-1] != '\n')
198 buffer = xrealloc( buffer, size * 2 );
199 fgets( buffer + size - 1, size + 1, file );
200 p = buffer + strlen(buffer);
201 size *= 2;
203 if (p > buffer && p[-1] == '\n')
205 *(--p) = 0;
206 if (p > buffer && p[-1] == '\r') *(--p) = 0;
207 if (p > buffer && p[-1] == '\\')
209 *(--p) = 0;
210 /* line ends in backslash, read continuation line */
211 fgets( p, size - (p - buffer), file );
212 input_line++;
213 continue;
216 return buffer;
220 /*******************************************************************
221 * add_include_path
223 * Add a directory to the include path.
225 static void add_include_path( const char *name )
227 INCL_PATH *path = xmalloc( sizeof(*path) );
228 list_add_tail( &paths, &path->entry );
229 path->name = name;
233 /*******************************************************************
234 * add_src_file
236 * Add a source file to the list.
238 static INCL_FILE *add_src_file( const char *name )
240 INCL_FILE *file = xmalloc( sizeof(*file) );
241 memset( file, 0, sizeof(*file) );
242 file->name = xstrdup(name);
243 list_add_tail( &sources, &file->entry );
244 return file;
248 /*******************************************************************
249 * add_include
251 * Add an include file if it doesn't already exists.
253 static INCL_FILE *add_include( INCL_FILE *pFile, const char *name, int line, int system )
255 INCL_FILE *include;
256 char *ext;
257 int pos;
259 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
260 if (pos >= MAX_INCLUDES)
261 fatal_error( "%s: %s: too many included files, please fix MAX_INCLUDES\n",
262 ProgramName, pFile->name );
264 /* enforce some rules for the Wine tree */
266 if (!memcmp( name, "../", 3 ))
267 fatal_error( "%s:%d: #include directive with relative path not allowed\n",
268 pFile->filename, line );
270 if (!strcmp( name, "config.h" ))
272 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
273 fatal_error( "%s:%d: config.h must not be included by a header file\n",
274 pFile->filename, line );
275 if (pos)
276 fatal_error( "%s:%d: config.h must be included before anything else\n",
277 pFile->filename, line );
279 else if (!strcmp( name, "wine/port.h" ))
281 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
282 fatal_error( "%s:%d: wine/port.h must not be included by a header file\n",
283 pFile->filename, line );
284 if (!pos) fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
285 pFile->filename, line );
286 if (pos > 1)
287 fatal_error( "%s:%d: wine/port.h must be included before everything except config.h\n",
288 pFile->filename, line );
289 if (strcmp( pFile->files[0]->name, "config.h" ))
290 fatal_error( "%s:%d: config.h must be included before wine/port.h\n",
291 pFile->filename, line );
294 LIST_FOR_EACH_ENTRY( include, &includes, INCL_FILE, entry )
295 if (!strcmp( name, include->name )) goto found;
297 include = xmalloc( sizeof(INCL_FILE) );
298 memset( include, 0, sizeof(INCL_FILE) );
299 include->name = xstrdup(name);
300 include->included_by = pFile;
301 include->included_line = line;
302 include->system = system;
303 list_add_tail( &includes, &include->entry );
304 found:
305 pFile->files[pos] = include;
306 return include;
310 /*******************************************************************
311 * open_src_file
313 static FILE *open_src_file( INCL_FILE *pFile )
315 FILE *file;
317 /* first try name as is */
318 if ((file = fopen( pFile->name, "r" )))
320 pFile->filename = xstrdup( pFile->name );
321 return file;
323 /* now try in source dir */
324 if (src_dir)
326 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
327 file = fopen( pFile->filename, "r" );
329 if (!file)
331 perror( pFile->name );
332 exit(1);
334 return file;
338 /*******************************************************************
339 * open_include_file
341 static FILE *open_include_file( INCL_FILE *pFile )
343 FILE *file = NULL;
344 char *filename, *p;
345 INCL_PATH *path;
347 errno = ENOENT;
349 /* check for generated bison header */
351 if (strendswith( pFile->name, ".tab.h" ))
353 if (src_dir)
354 filename = strmake( "%s/%.*s.y", src_dir, strlen(pFile->name) - 6, pFile->name );
355 else
356 filename = strmake( "%.*s.y", strlen(pFile->name) - 6, pFile->name );
358 if ((file = fopen( filename, "r" )))
360 pFile->sourcename = filename;
361 pFile->filename = xstrdup( pFile->name );
362 /* don't bother to parse it */
363 fclose( file );
364 return NULL;
366 free( filename );
369 /* check for generated message resource */
371 if (strendswith( pFile->name, ".mc.rc" ))
373 if (src_dir)
374 filename = strmake( "%s/%s", src_dir, pFile->name );
375 else
376 filename = xstrdup( pFile->name );
378 filename[strlen(filename) - 3] = 0;
380 if ((file = fopen( filename, "r" )))
382 pFile->sourcename = filename;
383 pFile->filename = xstrdup( pFile->name );
384 /* don't bother to parse it */
385 fclose( file );
386 return NULL;
388 free( filename );
391 /* check for corresponding idl file in source dir */
393 if (strendswith( pFile->name, ".h" ))
395 if (src_dir)
396 filename = strmake( "%s/%.*s.idl", src_dir, strlen(pFile->name) - 2, pFile->name );
397 else
398 filename = strmake( "%.*s.idl", strlen(pFile->name) - 2, pFile->name );
400 if ((file = fopen( filename, "r" )))
402 pFile->sourcename = filename;
403 pFile->filename = xstrdup( pFile->name );
404 return file;
406 free( filename );
409 /* first try name as is */
410 if ((file = fopen( pFile->name, "r" )))
412 pFile->filename = xstrdup( pFile->name );
413 return file;
416 /* now try in source dir */
417 if (src_dir)
419 filename = strmake( "%s/%s", src_dir, pFile->name );
420 if ((file = fopen( filename, "r" ))) goto found;
421 free( filename );
424 /* check for corresponding idl file in global includes */
426 if (strendswith( pFile->name, ".h" ))
428 if (top_src_dir)
429 filename = strmake( "%s/include/%.*s.idl",
430 top_src_dir, strlen(pFile->name) - 2, pFile->name );
431 else if (top_obj_dir)
432 filename = strmake( "%s/include/%.*s.idl",
433 top_obj_dir, strlen(pFile->name) - 2, pFile->name );
434 else
435 filename = NULL;
437 if (filename && (file = fopen( filename, "r" )))
439 pFile->sourcename = filename;
440 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
441 return file;
443 free( filename );
446 /* now try in global includes */
447 if (top_obj_dir)
449 filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
450 if ((file = fopen( filename, "r" ))) goto found;
451 free( filename );
453 if (top_src_dir)
455 filename = strmake( "%s/include/%s", top_src_dir, pFile->name );
456 if ((file = fopen( filename, "r" ))) goto found;
457 free( filename );
460 /* now search in include paths */
461 LIST_FOR_EACH_ENTRY( path, &paths, INCL_PATH, entry )
463 filename = strmake( "%s/%s", path->name, pFile->name );
464 if ((file = fopen( filename, "r" ))) goto found;
465 free( filename );
467 if (pFile->system) return NULL; /* ignore system files we cannot find */
469 /* try in src file directory */
470 if ((p = strrchr(pFile->included_by->filename, '/')))
472 int l = p - pFile->included_by->filename + 1;
473 filename = xmalloc(l + strlen(pFile->name) + 1);
474 memcpy( filename, pFile->included_by->filename, l );
475 strcpy( filename + l, pFile->name );
476 if ((file = fopen( filename, "r" ))) goto found;
477 free( filename );
480 perror( pFile->name );
481 while (pFile->included_by)
483 const char *parent = pFile->included_by->sourcename;
484 if (!parent) parent = pFile->included_by->name;
485 fprintf( stderr, " %s was first included from %s:%d\n",
486 pFile->name, parent, pFile->included_line );
487 pFile = pFile->included_by;
489 exit(1);
491 found:
492 pFile->filename = filename;
493 return file;
497 /*******************************************************************
498 * parse_idl_file
500 * If for_h_file is non-zero, it means we are not interested in the idl file
501 * itself, but only in the contents of the .h file that will be generated from it.
503 static void parse_idl_file( INCL_FILE *pFile, FILE *file, int for_h_file )
505 char *buffer, *include;
507 if (for_h_file)
509 /* generated .h file always includes these */
510 add_include( pFile, "rpc.h", 0, 1 );
511 add_include( pFile, "rpcndr.h", 0, 1 );
514 input_line = 0;
515 while ((buffer = get_line( file )))
517 char quote;
518 char *p = buffer;
519 while (*p && isspace(*p)) p++;
521 if (!strncmp( p, "import", 6 ))
523 p += 6;
524 while (*p && isspace(*p)) p++;
525 if (*p != '"') continue;
526 include = ++p;
527 while (*p && (*p != '"')) p++;
528 if (!*p) fatal_error( "%s:%d: Malformed import directive\n", pFile->filename, input_line );
529 *p = 0;
530 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
531 add_include( pFile, include, input_line, 0 );
532 continue;
535 if (for_h_file) /* only check for #include inside cpp_quote */
537 if (strncmp( p, "cpp_quote", 9 )) continue;
538 p += 9;
539 while (*p && isspace(*p)) p++;
540 if (*p++ != '(') continue;
541 while (*p && isspace(*p)) p++;
542 if (*p++ != '"') continue;
543 if (*p++ != '#') continue;
544 while (*p && isspace(*p)) p++;
545 if (strncmp( p, "include", 7 )) continue;
546 p += 7;
547 while (*p && isspace(*p)) p++;
548 if (*p == '\\' && p[1] == '"')
550 p += 2;
551 quote = '"';
553 else
555 if (*p++ != '<' ) continue;
556 quote = '>';
558 include = p;
559 while (*p && (*p != quote)) p++;
560 if (!*p || (quote == '"' && p[-1] != '\\'))
561 fatal_error( "%s:%d: Malformed #include directive inside cpp_quote\n",
562 pFile->filename, input_line );
563 if (quote == '"') p--; /* remove backslash */
564 *p = 0;
565 add_include( pFile, include, input_line, (quote == '>') );
566 continue;
569 /* check for normal #include */
570 if (*p++ != '#') continue;
571 while (*p && isspace(*p)) p++;
572 if (strncmp( p, "include", 7 )) continue;
573 p += 7;
574 while (*p && isspace(*p)) p++;
575 if (*p != '\"' && *p != '<' ) continue;
576 quote = *p++;
577 if (quote == '<') quote = '>';
578 include = p;
579 while (*p && (*p != quote)) p++;
580 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n", pFile->filename, input_line );
581 *p = 0;
582 add_include( pFile, include, input_line, (quote == '>') );
586 /*******************************************************************
587 * parse_c_file
589 static void parse_c_file( INCL_FILE *pFile, FILE *file )
591 char *buffer, *include;
593 input_line = 0;
594 while ((buffer = get_line( file )))
596 char quote;
597 char *p = buffer;
598 while (*p && isspace(*p)) p++;
599 if (*p++ != '#') continue;
600 while (*p && isspace(*p)) p++;
601 if (strncmp( p, "include", 7 )) continue;
602 p += 7;
603 while (*p && isspace(*p)) p++;
604 if (*p != '\"' && *p != '<' ) continue;
605 quote = *p++;
606 if (quote == '<') quote = '>';
607 include = p;
608 while (*p && (*p != quote)) p++;
609 if (!*p) fatal_error( "%s:%d: Malformed #include directive\n",
610 pFile->filename, input_line );
611 *p = 0;
612 add_include( pFile, include, input_line, (quote == '>') );
617 /*******************************************************************
618 * parse_file
620 static void parse_file( INCL_FILE *pFile, int src )
622 FILE *file;
624 file = src ? open_src_file( pFile ) : open_include_file( pFile );
625 if (!file) return;
627 if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" ))
628 parse_idl_file( pFile, file, 1 );
629 else if (strendswith( pFile->filename, ".idl" ))
630 parse_idl_file( pFile, file, 0 );
631 else
632 parse_c_file( pFile, file );
633 fclose(file);
637 /*******************************************************************
638 * output_include
640 static void output_include( FILE *file, INCL_FILE *pFile,
641 INCL_FILE *owner, int *column )
643 int i;
645 if (pFile->owner == owner) return;
646 if (!pFile->filename) return;
647 pFile->owner = owner;
648 if (*column + strlen(pFile->filename) + 1 > 70)
650 fprintf( file, " \\\n" );
651 *column = 0;
653 fprintf( file, " %s", pFile->filename );
654 *column += strlen(pFile->filename) + 1;
655 for (i = 0; i < MAX_INCLUDES; i++)
656 if (pFile->files[i]) output_include( file, pFile->files[i],
657 owner, column );
661 /*******************************************************************
662 * output_src
664 static void output_src( FILE *file, INCL_FILE *pFile, int *column )
666 char *obj = xstrdup( pFile->name );
667 char *ext = get_extension( obj );
668 if (ext)
670 *ext++ = 0;
671 if (!strcmp( ext, "y" )) /* yacc file */
673 *column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj );
675 else if (!strcmp( ext, "l" )) /* lex file */
677 *column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj );
679 else if (!strcmp( ext, "rc" )) /* resource file */
681 *column += fprintf( file, "%s.res: %s", obj, pFile->filename );
683 else if (!strcmp( ext, "mc" )) /* message file */
685 *column += fprintf( file, "%s.mc.rc: %s", obj, pFile->filename );
687 else if (!strcmp( ext, "idl" )) /* IDL file */
689 *column += fprintf( file, "%s.h: %s", obj, pFile->filename );
691 else
693 *column += fprintf( file, "%s.o: %s", obj, pFile->filename );
696 free( obj );
700 /*******************************************************************
701 * output_dependencies
703 static void output_dependencies(void)
705 INCL_FILE *pFile;
706 int i, column;
707 FILE *file = NULL;
708 char *buffer;
710 if (Separator && ((file = fopen( OutputFileName, "r+" ))))
712 while ((buffer = get_line( file )))
714 if (strncmp( buffer, Separator, strlen(Separator) )) continue;
715 ftruncate( fileno(file), ftell(file) );
716 fseek( file, 0L, SEEK_END );
717 break;
720 if (!file)
722 if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
724 perror( OutputFileName );
725 exit(1);
728 LIST_FOR_EACH_ENTRY( pFile, &sources, INCL_FILE, entry )
730 column = 0;
731 output_src( file, pFile, &column );
732 for (i = 0; i < MAX_INCLUDES; i++)
733 if (pFile->files[i]) output_include( file, pFile->files[i],
734 pFile, &column );
735 fprintf( file, "\n" );
737 fclose(file);
741 /*******************************************************************
742 * parse_option
744 static void parse_option( const char *opt )
746 switch(opt[1])
748 case 'I':
749 if (opt[2]) add_include_path( opt + 2 );
750 break;
751 case 'C':
752 src_dir = opt + 2;
753 break;
754 case 'S':
755 top_src_dir = opt + 2;
756 break;
757 case 'T':
758 top_obj_dir = opt + 2;
759 break;
760 case 'f':
761 if (opt[2]) OutputFileName = opt + 2;
762 break;
763 case 's':
764 if (opt[2]) Separator = opt + 2;
765 else Separator = NULL;
766 break;
767 default:
768 fprintf( stderr, "Unknown option '%s'\n", opt );
769 fprintf( stderr, Usage, ProgramName );
770 exit(1);
775 /*******************************************************************
776 * main
778 int main( int argc, char *argv[] )
780 INCL_FILE *pFile;
781 INCL_PATH *path, *next;
782 int i, j;
784 ProgramName = argv[0];
786 i = 1;
787 while (i < argc)
789 if (argv[i][0] == '-')
791 parse_option( argv[i] );
792 for (j = i; j < argc; j++) argv[j] = argv[j+1];
793 argc--;
795 else i++;
798 /* ignore redundant source paths */
799 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
800 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
802 /* get rid of absolute paths that don't point into the source dir */
803 LIST_FOR_EACH_ENTRY_SAFE( path, next, &paths, INCL_PATH, entry )
805 if (path->name[0] != '/') continue;
806 if (top_src_dir)
808 if (!strncmp( path->name, top_src_dir, strlen(top_src_dir) )) continue;
809 if (path->name[strlen(top_src_dir)] == '/') continue;
811 list_remove( &path->entry );
812 free( path );
815 for (i = 1; i < argc; i++)
817 pFile = add_src_file( argv[i] );
818 parse_file( pFile, 1 );
820 LIST_FOR_EACH_ENTRY( pFile, &includes, INCL_FILE, entry ) parse_file( pFile, 0 );
821 output_dependencies();
822 return 0;