wineps: Use vertical orientation table to determine rotation.
[wine.git] / tools / makedep.c
blob1d997623143534a8e8941c944622c26de8a8a5bf
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 strarray
86 unsigned int count; /* strings in use */
87 unsigned int size; /* total allocated size */
88 const char **str;
91 static struct strarray include_args;
92 static struct strarray object_extensions;
94 static const char *src_dir;
95 static const char *top_src_dir;
96 static const char *top_obj_dir;
97 static const char *parent_dir;
98 static const char *OutputFileName = "Makefile";
99 static const char *Separator = "### Dependencies";
100 static const char *input_file_name;
101 static int relative_dir_mode;
102 static int input_line;
103 static FILE *output_file;
105 static const char Usage[] =
106 "Usage: makedep [options] [files]\n"
107 "Options:\n"
108 " -Idir Search for include files in directory 'dir'\n"
109 " -Cdir Search for source files in directory 'dir'\n"
110 " -Sdir Set the top source directory\n"
111 " -Tdir Set the top object directory\n"
112 " -Pdir Set the parent source directory\n"
113 " -R from to Compute the relative path between two directories\n"
114 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
115 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
118 #ifndef __GNUC__
119 #define __attribute__(x)
120 #endif
122 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
123 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
124 static int output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
125 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
127 /*******************************************************************
128 * fatal_error
130 static void fatal_error( const char *msg, ... )
132 va_list valist;
133 va_start( valist, msg );
134 if (input_file_name)
136 fprintf( stderr, "%s:", input_file_name );
137 if (input_line) fprintf( stderr, "%d:", input_line );
138 fprintf( stderr, " error: " );
140 else fprintf( stderr, "makedep: error: " );
141 vfprintf( stderr, msg, valist );
142 va_end( valist );
143 exit(1);
147 /*******************************************************************
148 * fatal_perror
150 static void fatal_perror( const char *msg, ... )
152 va_list valist;
153 va_start( valist, msg );
154 if (input_file_name)
156 fprintf( stderr, "%s:", input_file_name );
157 if (input_line) fprintf( stderr, "%d:", input_line );
158 fprintf( stderr, " error: " );
160 else fprintf( stderr, "makedep: error: " );
161 vfprintf( stderr, msg, valist );
162 perror( " " );
163 va_end( valist );
164 exit(1);
168 /*******************************************************************
169 * xmalloc
171 static void *xmalloc( size_t size )
173 void *res;
174 if (!(res = malloc (size ? size : 1)))
175 fatal_error( "Virtual memory exhausted.\n" );
176 return res;
180 /*******************************************************************
181 * xrealloc
183 static void *xrealloc (void *ptr, size_t size)
185 void *res;
186 assert( size );
187 if (!(res = realloc( ptr, size )))
188 fatal_error( "Virtual memory exhausted.\n" );
189 return res;
192 /*******************************************************************
193 * xstrdup
195 static char *xstrdup( const char *str )
197 char *res = strdup( str );
198 if (!res) fatal_error( "Virtual memory exhausted.\n" );
199 return res;
203 /*******************************************************************
204 * strmake
206 static char *strmake( const char* fmt, ... )
208 int n;
209 size_t size = 100;
210 va_list ap;
212 for (;;)
214 char *p = xmalloc (size);
215 va_start(ap, fmt);
216 n = vsnprintf (p, size, fmt, ap);
217 va_end(ap);
218 if (n == -1) size *= 2;
219 else if ((size_t)n >= size) size = n + 1;
220 else return p;
221 free(p);
226 /*******************************************************************
227 * strendswith
229 static int strendswith( const char* str, const char* end )
231 int l = strlen(str);
232 int m = strlen(end);
234 return l >= m && strcmp(str + l - m, end) == 0;
238 /*******************************************************************
239 * output
241 static int output( const char *format, ... )
243 int ret;
244 va_list valist;
246 va_start( valist, format );
247 ret = vfprintf( output_file, format, valist );
248 va_end( valist );
249 if (ret < 0) fatal_perror( "output" );
250 return ret;
254 /*******************************************************************
255 * strarray_init
257 static void strarray_init( struct strarray *array )
259 array->count = 0;
260 array->size = 0;
261 array->str = NULL;
265 /*******************************************************************
266 * strarray_add
268 static void strarray_add( struct strarray *array, const char *str )
270 if (array->count == array->size)
272 if (array->size) array->size *= 2;
273 else array->size = 16;
274 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
276 array->str[array->count++] = str;
280 /*******************************************************************
281 * strarray_insert
283 static void strarray_insert( struct strarray *array, unsigned int pos, const char *str )
285 unsigned int i;
287 strarray_add( array, NULL );
288 for (i = array->count - 1; i > pos; i--) array->str[i] = array->str[i - 1];
289 array->str[pos] = 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 * find_src_file
423 static struct incl_file *find_src_file( const char *name )
425 struct incl_file *file;
427 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
428 if (!strcmp( name, file->name )) return file;
429 return NULL;
432 /*******************************************************************
433 * find_include_file
435 static struct incl_file *find_include_file( const char *name )
437 struct incl_file *file;
439 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
440 if (!strcmp( name, file->name )) return file;
441 return NULL;
444 /*******************************************************************
445 * add_include
447 * Add an include file if it doesn't already exists.
449 static struct incl_file *add_include( struct incl_file *pFile, const char *name, int system )
451 struct incl_file *include;
452 char *ext;
453 int pos;
455 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
456 if (pos >= MAX_INCLUDES)
457 fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
459 /* enforce some rules for the Wine tree */
461 if (!memcmp( name, "../", 3 ))
462 fatal_error( "#include directive with relative path not allowed\n" );
464 if (!strcmp( name, "config.h" ))
466 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
467 fatal_error( "config.h must not be included by a header file\n" );
468 if (pos)
469 fatal_error( "config.h must be included before anything else\n" );
471 else if (!strcmp( name, "wine/port.h" ))
473 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
474 fatal_error( "wine/port.h must not be included by a header file\n" );
475 if (!pos) fatal_error( "config.h must be included before wine/port.h\n" );
476 if (pos > 1)
477 fatal_error( "wine/port.h must be included before everything except config.h\n" );
478 if (strcmp( pFile->files[0]->name, "config.h" ))
479 fatal_error( "config.h must be included before wine/port.h\n" );
482 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
483 if (!strcmp( name, include->name )) goto found;
485 include = xmalloc( sizeof(*include) );
486 memset( include, 0, sizeof(*include) );
487 include->name = xstrdup(name);
488 include->included_by = pFile;
489 include->included_line = input_line;
490 if (system) include->flags |= FLAG_SYSTEM;
491 list_add_tail( &includes, &include->entry );
492 found:
493 pFile->files[pos] = include;
494 return include;
498 /*******************************************************************
499 * open_src_file
501 static FILE *open_src_file( struct incl_file *pFile )
503 FILE *file;
505 /* first try name as is */
506 if ((file = fopen( pFile->name, "r" )))
508 pFile->filename = xstrdup( pFile->name );
509 return file;
511 /* now try in source dir */
512 if (src_dir)
514 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
515 file = fopen( pFile->filename, "r" );
517 /* now try parent dir */
518 if (!file && parent_dir)
520 if (src_dir)
521 pFile->filename = strmake( "%s/%s/%s", src_dir, parent_dir, pFile->name );
522 else
523 pFile->filename = strmake( "%s/%s", parent_dir, pFile->name );
524 if ((file = fopen( pFile->filename, "r" ))) return file;
525 file = fopen( pFile->filename, "r" );
527 if (!file) fatal_perror( "open %s", pFile->name );
528 return file;
532 /*******************************************************************
533 * open_include_file
535 static FILE *open_include_file( struct incl_file *pFile )
537 FILE *file = NULL;
538 char *filename, *p;
539 unsigned int i, len;
541 errno = ENOENT;
543 /* check for generated bison header */
545 if (strendswith( pFile->name, ".tab.h" ))
547 filename = replace_extension( pFile->name, 6, ".y" );
548 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
550 if ((file = fopen( filename, "r" )))
552 pFile->sourcename = filename;
553 pFile->filename = xstrdup( pFile->name );
554 /* don't bother to parse it */
555 fclose( file );
556 return NULL;
558 free( filename );
561 /* check for corresponding idl file in source dir */
563 if (strendswith( pFile->name, ".h" ))
565 filename = replace_extension( pFile->name, 2, ".idl" );
566 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
568 if ((file = fopen( filename, "r" )))
570 pFile->sourcename = filename;
571 pFile->filename = xstrdup( pFile->name );
572 return file;
574 free( filename );
577 /* now try in source dir */
578 if (src_dir)
579 filename = strmake( "%s/%s", src_dir, pFile->name );
580 else
581 filename = xstrdup( pFile->name );
582 if ((file = fopen( filename, "r" ))) goto found;
583 free( filename );
585 /* now try in parent source dir */
586 if (parent_dir)
588 if (src_dir)
589 filename = strmake( "%s/%s/%s", src_dir, parent_dir, pFile->name );
590 else
591 filename = strmake( "%s/%s", parent_dir, pFile->name );
592 if ((file = fopen( filename, "r" ))) goto found;
593 free( filename );
596 /* check for corresponding idl file in global includes */
598 if (strendswith( pFile->name, ".h" ))
600 filename = replace_extension( pFile->name, 2, ".idl" );
601 if (top_src_dir)
602 filename = strmake( "%s/include/%s", top_src_dir, filename );
603 else if (top_obj_dir)
604 filename = strmake( "%s/include/%s", top_obj_dir, filename );
605 else
606 filename = NULL;
608 if (filename && (file = fopen( filename, "r" )))
610 pFile->sourcename = filename;
611 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
612 return file;
614 free( filename );
617 /* check for corresponding .in file in global includes (for config.h.in) */
619 if (strendswith( pFile->name, ".h" ))
621 filename = replace_extension( pFile->name, 2, ".h.in" );
622 if (top_src_dir)
623 filename = strmake( "%s/include/%s", top_src_dir, filename );
624 else if (top_obj_dir)
625 filename = strmake( "%s/include/%s", top_obj_dir, filename );
626 else
627 filename = NULL;
629 if (filename && (file = fopen( filename, "r" )))
631 pFile->sourcename = filename;
632 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
633 return file;
635 free( filename );
638 /* check for corresponding .x file in global includes */
640 if (strendswith( pFile->name, "tmpl.h" ))
642 filename = replace_extension( pFile->name, 2, ".x" );
643 if (top_src_dir)
644 filename = strmake( "%s/include/%s", top_src_dir, filename );
645 else if (top_obj_dir)
646 filename = strmake( "%s/include/%s", top_obj_dir, filename );
647 else
648 filename = NULL;
650 if (filename && (file = fopen( filename, "r" )))
652 pFile->sourcename = filename;
653 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
654 return file;
656 free( filename );
659 /* now search in include paths */
660 for (i = 0; i < include_args.count; i++)
662 const char *dir = include_args.str[i] + 2; /* skip -I */
663 if (*dir == '/')
665 /* ignore absolute paths that don't point into the source dir */
666 if (!top_src_dir) continue;
667 len = strlen( top_src_dir );
668 if (strncmp( dir, top_src_dir, len )) continue;
669 if (dir[len] && dir[len] != '/') continue;
671 filename = strmake( "%s/%s", dir, pFile->name );
672 if ((file = fopen( filename, "r" ))) goto found;
673 free( filename );
675 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
677 /* try in src file directory */
678 if ((p = strrchr(pFile->included_by->filename, '/')))
680 int l = p - pFile->included_by->filename + 1;
681 filename = xmalloc(l + strlen(pFile->name) + 1);
682 memcpy( filename, pFile->included_by->filename, l );
683 strcpy( filename + l, pFile->name );
684 if ((file = fopen( filename, "r" ))) goto found;
685 free( filename );
688 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
689 perror( pFile->name );
690 pFile = pFile->included_by;
691 while (pFile && pFile->included_by)
693 const char *parent = pFile->included_by->sourcename;
694 if (!parent) parent = pFile->included_by->name;
695 fprintf( stderr, "%s:%d: note: %s was first included here\n",
696 parent, pFile->included_line, pFile->name );
697 pFile = pFile->included_by;
699 exit(1);
701 found:
702 pFile->filename = filename;
703 return file;
707 /*******************************************************************
708 * parse_include_directive
710 static void parse_include_directive( struct incl_file *source, char *str )
712 char quote, *include, *p = str;
714 while (*p && isspace(*p)) p++;
715 if (*p != '\"' && *p != '<' ) return;
716 quote = *p++;
717 if (quote == '<') quote = '>';
718 include = p;
719 while (*p && (*p != quote)) p++;
720 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
721 *p = 0;
722 add_include( source, include, (quote == '>') );
726 /*******************************************************************
727 * parse_pragma_directive
729 static void parse_pragma_directive( struct incl_file *source, char *str )
731 char *flag, *p = str;
733 if (!isspace( *p )) return;
734 while (*p && isspace(*p)) p++;
735 p = strtok( p, " \t" );
736 if (strcmp( p, "makedep" )) return;
738 while ((flag = strtok( NULL, " \t" )))
740 if (!strcmp( flag, "depend" ))
742 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
743 return;
745 if (strendswith( source->name, ".idl" ))
747 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
748 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
749 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
750 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
751 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
752 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
753 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
755 else if (strendswith( source->name, ".rc" ))
757 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
759 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
764 /*******************************************************************
765 * parse_cpp_directive
767 static void parse_cpp_directive( struct incl_file *source, char *str )
769 while (*str && isspace(*str)) str++;
770 if (*str++ != '#') return;
771 while (*str && isspace(*str)) str++;
773 if (!strncmp( str, "include", 7 ))
774 parse_include_directive( source, str + 7 );
775 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
776 parse_include_directive( source, str + 6 );
777 else if (!strncmp( str, "pragma", 6 ))
778 parse_pragma_directive( source, str + 6 );
782 /*******************************************************************
783 * parse_idl_file
785 * If for_h_file is non-zero, it means we are not interested in the idl file
786 * itself, but only in the contents of the .h file that will be generated from it.
788 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
790 char *buffer, *include;
792 input_line = 0;
793 if (for_h_file)
795 /* generated .h file always includes these */
796 add_include( pFile, "rpc.h", 1 );
797 add_include( pFile, "rpcndr.h", 1 );
800 while ((buffer = get_line( file )))
802 char quote;
803 char *p = buffer;
804 while (*p && isspace(*p)) p++;
806 if (!strncmp( p, "import", 6 ))
808 p += 6;
809 while (*p && isspace(*p)) p++;
810 if (*p != '"') continue;
811 include = ++p;
812 while (*p && (*p != '"')) p++;
813 if (!*p) fatal_error( "malformed import directive\n" );
814 *p = 0;
815 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
816 add_include( pFile, include, 0 );
817 continue;
820 if (for_h_file) /* only check for #include inside cpp_quote */
822 if (strncmp( p, "cpp_quote", 9 )) continue;
823 p += 9;
824 while (*p && isspace(*p)) p++;
825 if (*p++ != '(') continue;
826 while (*p && isspace(*p)) p++;
827 if (*p++ != '"') continue;
828 if (*p++ != '#') continue;
829 while (*p && isspace(*p)) p++;
830 if (strncmp( p, "include", 7 )) continue;
831 p += 7;
832 while (*p && isspace(*p)) p++;
833 if (*p == '\\' && p[1] == '"')
835 p += 2;
836 quote = '"';
838 else
840 if (*p++ != '<' ) continue;
841 quote = '>';
843 include = p;
844 while (*p && (*p != quote)) p++;
845 if (!*p || (quote == '"' && p[-1] != '\\'))
846 fatal_error( "malformed #include directive inside cpp_quote\n" );
847 if (quote == '"') p--; /* remove backslash */
848 *p = 0;
849 add_include( pFile, include, (quote == '>') );
850 continue;
853 parse_cpp_directive( pFile, p );
857 /*******************************************************************
858 * parse_c_file
860 static void parse_c_file( struct incl_file *pFile, FILE *file )
862 char *buffer;
864 input_line = 0;
865 while ((buffer = get_line( file )))
867 parse_cpp_directive( pFile, buffer );
872 /*******************************************************************
873 * parse_rc_file
875 static void parse_rc_file( struct incl_file *pFile, FILE *file )
877 char *buffer, *include;
879 input_line = 0;
880 while ((buffer = get_line( file )))
882 char quote;
883 char *p = buffer;
884 while (*p && isspace(*p)) p++;
886 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
888 p += 2;
889 while (*p && isspace(*p)) p++;
890 if (strncmp( p, "@makedep:", 9 )) continue;
891 p += 9;
892 while (*p && isspace(*p)) p++;
893 quote = '"';
894 if (*p == quote)
896 include = ++p;
897 while (*p && *p != quote) p++;
899 else
901 include = p;
902 while (*p && !isspace(*p) && *p != '*') p++;
904 if (!*p)
905 fatal_error( "malformed makedep comment\n" );
906 *p = 0;
907 add_include( pFile, include, (quote == '>') );
908 continue;
911 parse_cpp_directive( pFile, buffer );
916 /*******************************************************************
917 * parse_man_page
919 static void parse_man_page( struct incl_file *source, FILE *file )
921 char *p, *buffer;
923 /* make sure it gets rebuilt when the version changes */
924 add_include( source, "config.h", 1 );
926 input_line = 0;
927 while ((buffer = get_line( file )))
929 if (strncmp( buffer, ".TH", 3 )) continue;
930 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
931 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
932 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
933 source->sourcename = xstrdup( p ); /* abuse source name to store section */
934 return;
939 /*******************************************************************
940 * parse_generated_idl
942 static void parse_generated_idl( struct incl_file *source )
944 char *header = replace_extension( source->name, 4, ".h" );
946 source->filename = xstrdup( source->name );
948 if (strendswith( source->name, "_c.c" ))
950 add_include( source, header, 0 );
952 else if (strendswith( source->name, "_i.c" ))
954 add_include( source, "rpc.h", 1 );
955 add_include( source, "rpcndr.h", 1 );
956 add_include( source, "guiddef.h", 1 );
958 else if (strendswith( source->name, "_p.c" ))
960 add_include( source, "objbase.h", 1 );
961 add_include( source, "rpcproxy.h", 1 );
962 add_include( source, "wine/exception.h", 1 );
963 add_include( source, header, 0 );
965 else if (strendswith( source->name, "_s.c" ))
967 add_include( source, "wine/exception.h", 1 );
968 add_include( source, header, 0 );
971 free( header );
974 /*******************************************************************
975 * is_generated_idl
977 static int is_generated_idl( struct incl_file *source )
979 return (strendswith( source->name, "_c.c" ) ||
980 strendswith( source->name, "_i.c" ) ||
981 strendswith( source->name, "_p.c" ) ||
982 strendswith( source->name, "_s.c" ));
985 /*******************************************************************
986 * parse_file
988 static void parse_file( struct incl_file *source, int src )
990 FILE *file;
992 /* don't try to open certain types of files */
993 if (strendswith( source->name, ".tlb" ) ||
994 strendswith( source->name, ".x" ))
996 source->filename = xstrdup( source->name );
997 return;
1000 file = src ? open_src_file( source ) : open_include_file( source );
1001 if (!file) return;
1002 input_file_name = source->filename;
1004 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1005 parse_idl_file( source, file, 1 );
1006 else if (strendswith( source->filename, ".idl" ))
1007 parse_idl_file( source, file, 0 );
1008 else if (strendswith( source->filename, ".c" ) ||
1009 strendswith( source->filename, ".m" ) ||
1010 strendswith( source->filename, ".h" ) ||
1011 strendswith( source->filename, ".l" ) ||
1012 strendswith( source->filename, ".y" ))
1013 parse_c_file( source, file );
1014 else if (strendswith( source->filename, ".rc" ))
1015 parse_rc_file( source, file );
1016 else if (strendswith( source->filename, ".man.in" ))
1017 parse_man_page( source, file );
1018 fclose(file);
1019 input_file_name = NULL;
1023 /*******************************************************************
1024 * add_src_file
1026 * Add a source file to the list.
1028 static struct incl_file *add_src_file( const char *name )
1030 struct incl_file *file;
1032 if ((file = find_src_file( name ))) return file; /* we already have it */
1033 file = xmalloc( sizeof(*file) );
1034 memset( file, 0, sizeof(*file) );
1035 file->name = xstrdup(name);
1036 list_add_tail( &sources, &file->entry );
1038 /* special cases for generated files */
1040 if (is_generated_idl( file ))
1042 parse_generated_idl( file );
1043 return file;
1046 if (!strcmp( file->name, "dlldata.o" ))
1048 file->filename = xstrdup( "dlldata.c" );
1049 add_include( file, "objbase.h", 1 );
1050 add_include( file, "rpcproxy.h", 1 );
1051 return file;
1054 if (!strcmp( file->name, "testlist.o" ))
1056 file->filename = xstrdup( "testlist.c" );
1057 add_include( file, "wine/test.h", 1 );
1058 return file;
1061 if (strendswith( file->name, ".o" ))
1063 /* default to .c for unknown extra object files */
1064 file->filename = replace_extension( file->name, 2, ".c" );
1065 return file;
1068 if (strendswith( file->name, ".tlb" ) ||
1069 strendswith( file->name, ".res" ) ||
1070 strendswith( file->name, ".pot" ) ||
1071 strendswith( file->name, ".x" ))
1073 file->filename = xstrdup( file->name );
1074 return file;
1077 parse_file( file, 1 );
1078 return file;
1082 /*******************************************************************
1083 * add_generated_sources
1085 static void add_generated_sources(void)
1087 unsigned int i;
1088 struct incl_file *source, *next;
1090 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1092 if (!source->flags) continue;
1093 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1095 if (!(source->flags & idl_outputs[i].flag)) continue;
1096 if (!strendswith( idl_outputs[i].ext, ".c" )) continue;
1097 add_src_file( replace_extension( source->name, 4, idl_outputs[i].ext ));
1099 if (source->flags & FLAG_IDL_PROXY) add_src_file( "dlldata.o" );
1102 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1104 if (strendswith( source->name, "_c.c" ) ||
1105 strendswith( source->name, "_i.c" ) ||
1106 strendswith( source->name, "_p.c" ) ||
1107 strendswith( source->name, "_s.c" ) ||
1108 strendswith( source->name, ".tlb" ))
1110 char *idl = replace_extension( source->name, 4, ".idl" );
1111 struct incl_file *file = add_src_file( idl );
1112 if (strendswith( source->name, "_c.c" )) file->flags |= FLAG_IDL_CLIENT;
1113 else if (strendswith( source->name, "_i.c" )) file->flags |= FLAG_IDL_IDENT;
1114 else if (strendswith( source->name, "_p.c" )) file->flags |= FLAG_IDL_PROXY;
1115 else if (strendswith( source->name, "_s.c" )) file->flags |= FLAG_IDL_SERVER;
1116 else if (strendswith( source->name, ".tlb" )) file->flags |= FLAG_IDL_TYPELIB;
1117 continue;
1119 if (strendswith( source->name, "_r.res" ) ||
1120 strendswith( source->name, "_t.res" ))
1122 char *idl = replace_extension( source->name, 6, ".idl" );
1123 struct incl_file *file = add_src_file( idl );
1124 if (strendswith( source->name, "_r.res" )) file->flags |= FLAG_IDL_REGISTER;
1125 else if (strendswith( source->name, "_t.res" )) file->flags |= FLAG_IDL_TYPELIB;
1126 continue;
1128 if (strendswith( source->name, ".pot" ))
1130 char *rc = replace_extension( source->name, 4, ".rc" );
1131 struct incl_file *file = add_src_file( rc );
1132 file->flags |= FLAG_RC_PO;
1138 /*******************************************************************
1139 * output_include
1141 static void output_include( struct incl_file *pFile, struct incl_file *owner, int *column )
1143 int i;
1145 if (pFile->owner == owner) return;
1146 if (!pFile->filename) return;
1147 pFile->owner = owner;
1148 output_filename( pFile->filename, column );
1149 for (i = 0; i < MAX_INCLUDES; i++)
1150 if (pFile->files[i]) output_include( pFile->files[i], owner, column );
1154 /*******************************************************************
1155 * output_sources
1157 static void output_sources(void)
1159 struct incl_file *source;
1160 struct strarray clean_files, subdirs;
1161 int i, column, po_srcs = 0, mc_srcs = 0;
1162 int is_test = find_src_file( "testlist.o" ) != NULL;
1164 strarray_init( &clean_files );
1165 strarray_init( &subdirs );
1167 column = output( "includes = -I." );
1168 if (src_dir) output_filename( strmake( "-I%s", src_dir ), &column );
1169 if (parent_dir)
1171 if (src_dir) output_filename( strmake( "-I%s/%s", src_dir, parent_dir ), &column );
1172 else output_filename( strmake( "-I%s", parent_dir ), &column );
1174 if (top_src_dir && top_obj_dir) output_filename( strmake( "-I%s/include", top_obj_dir ), &column );
1175 for (i = 0; i < include_args.count; i++) output_filename( include_args.str[i], &column );
1176 output( "\n" );
1178 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1180 char *obj = xstrdup( source->name );
1181 char *ext = get_extension( obj );
1183 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1184 *ext++ = 0;
1185 column = 0;
1187 if (!strcmp( ext, "y" )) /* yacc file */
1189 /* add source file dependency for parallel makes */
1190 char *header = strmake( "%s.tab.h", obj );
1192 if (find_include_file( header ))
1194 output( "%s.tab.h: %s\n", obj, source->filename );
1195 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
1196 obj, obj, source->filename );
1197 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1198 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1200 else output( "%s.tab.c: %s\n", obj, source->filename );
1202 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj, source->filename );
1203 output( "%s.tab.o: %s.tab.c\n", obj, obj );
1204 output( "\t$(CC) -c $(includes) $(ALLCFLAGS) -o $@ %s.tab.c\n", obj );
1205 strarray_add( &clean_files, strmake( "%s.tab.c", obj ));
1206 strarray_add( &clean_files, strmake( "%s.tab.o", obj ));
1207 column += output( "%s.tab.o:", obj );
1208 free( header );
1210 else if (!strcmp( ext, "l" )) /* lex file */
1212 output( "%s.yy.c: %s\n", obj, source->filename );
1213 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source->filename );
1214 output( "%s.yy.o: %s.yy.c\n", obj, obj );
1215 output( "\t$(CC) -c $(includes) $(ALLCFLAGS) -o $@ %s.yy.c\n", obj );
1216 strarray_add( &clean_files, strmake( "%s.yy.c", obj ));
1217 strarray_add( &clean_files, strmake( "%s.yy.o", obj ));
1218 column += output( "%s.yy.o:", obj );
1220 else if (!strcmp( ext, "rc" )) /* resource file */
1222 if (source->flags & FLAG_RC_PO)
1224 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj, source->filename );
1225 output( "\t$(WRC) $(includes) $(RCFLAGS) -o $@ %s\n", source->filename );
1226 column += output( "%s.res rsrc.pot:", obj );
1227 po_srcs++;
1229 else
1231 output( "%s.res: $(WRC) %s\n", obj, source->filename );
1232 output( "\t$(WRC) $(includes) $(RCFLAGS) -o $@ %s\n", source->filename );
1233 column += output( "%s.res:", obj );
1235 strarray_add( &clean_files, strmake( "%s.res", obj ));
1237 else if (!strcmp( ext, "mc" )) /* message file */
1239 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj, source->filename );
1240 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source->filename );
1241 strarray_add( &clean_files, strmake( "%s.res", obj ));
1242 mc_srcs++;
1243 column += output( "msg.pot %s.res:", obj );
1245 else if (!strcmp( ext, "idl" )) /* IDL file */
1247 char *targets[8];
1248 unsigned int i, nb_targets = 0;
1250 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1251 source->flags |= FLAG_IDL_HEADER;
1253 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1255 if (!(source->flags & idl_outputs[i].flag)) continue;
1256 output( "%s%s: $(WIDL)\n", obj, idl_outputs[i].ext );
1257 output( "\t$(WIDL) $(includes) %s -o $@ %s\n", idl_outputs[i].widl_arg, source->filename );
1258 targets[nb_targets++] = strmake( "%s%s", obj, idl_outputs[i].ext );
1260 for (i = 0; i < nb_targets; i++)
1262 column += output( "%s%c", targets[i], i < nb_targets - 1 ? ' ' : ':' );
1263 strarray_add( &clean_files, targets[i] );
1265 column += output( " %s", source->filename );
1267 else if (!strcmp( ext, "in" )) /* man page */
1269 if (strendswith( obj, ".man" ) && source->sourcename)
1271 char *dir, *dest = replace_extension( obj, 4, "" );
1272 char *lang = strchr( dest, '.' );
1273 if (lang)
1275 *lang++ = 0;
1276 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, source->sourcename );
1278 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", source->sourcename );
1279 output( "install-man-pages:: %s %s\n", obj, dir );
1280 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n",
1281 obj, dir, dest, source->sourcename );
1282 output( "uninstall::\n" );
1283 output( "\t$(RM) %s/%s.%s\n",
1284 dir, dest, source->sourcename );
1285 free( dest );
1286 strarray_add( &subdirs, dir );
1288 strarray_add( &clean_files, xstrdup(obj) );
1289 output( "%s: %s\n", obj, source->filename );
1290 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1291 column += output( "%s:", obj );
1293 else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" ) || !strcmp( ext, "pot" ))
1295 continue; /* nothing to do for typelib files */
1297 else
1299 for (i = 0; i < object_extensions.count; i++)
1301 strarray_add( &clean_files, strmake( "%s.%s", obj, object_extensions.str[i] ));
1302 output( "%s.%s: %s\n", obj, object_extensions.str[i], source->filename );
1303 if (strstr( object_extensions.str[i], "cross" ))
1304 output( "\t$(CROSSCC) -c $(includes) $(ALLCROSSCFLAGS) -o $@ %s\n", source->filename );
1305 else
1306 output( "\t$(CC) -c $(includes) $(ALLCFLAGS) -o $@ %s\n", source->filename );
1308 if (source->flags & FLAG_C_IMPLIB)
1310 strarray_add( &clean_files, strmake( "%s.cross.o", obj ));
1311 output( "%s.cross.o: %s\n", obj, source->filename );
1312 output( "\t$(CROSSCC) -c $(includes) $(ALLCROSSCFLAGS) -o $@ %s\n", source->filename );
1314 if (is_test && !strcmp( ext, "c" ) && !is_generated_idl( source ))
1316 output( "%s.ok:\n", obj );
1317 output( "\t$(RUNTEST) $(RUNTESTFLAGS) %s && touch $@\n", obj );
1319 for (i = 0; i < object_extensions.count; i++)
1320 column += output( "%s.%s ", obj, object_extensions.str[i] );
1321 if (source->flags & FLAG_C_IMPLIB) column += output( "%s.cross.o", obj );
1322 column += output( ":" );
1324 free( obj );
1326 for (i = 0; i < MAX_INCLUDES; i++)
1327 if (source->files[i]) output_include( source->files[i], source, &column );
1328 output( "\n" );
1331 /* rules for files that depend on multiple sources */
1333 if (po_srcs)
1335 column = output( "rsrc.pot: $(WRC)" );
1336 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1337 if (source->flags & FLAG_RC_PO) output_filename( source->filename, &column );
1338 output( "\n" );
1339 column = output( "\t$(WRC) $(includes) $(RCFLAGS) -O pot -o $@" );
1340 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1341 if (source->flags & FLAG_RC_PO) output_filename( source->filename, &column );
1342 output( "\n" );
1343 strarray_add( &clean_files, "rsrc.pot" );
1346 if (mc_srcs)
1348 column = output( "msg.pot: $(WMC)" );
1349 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1350 if (strendswith( source->name, ".mc" )) output_filename( source->filename, &column );
1351 output( "\n" );
1352 column = output( "\t$(WMC) -O pot -o $@" );
1353 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1354 if (strendswith( source->name, ".mc" )) output_filename( source->filename, &column );
1355 output( "\n" );
1356 strarray_add( &clean_files, "msg.pot" );
1359 if (find_src_file( "dlldata.o" ))
1361 output( "dlldata.c: $(WIDL) Makefile.in\n" );
1362 column = output( "\t$(WIDL) --dlldata-only -o $@" );
1363 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1364 if (source->flags & FLAG_IDL_PROXY) output_filename( source->filename, &column );
1365 output( "\n" );
1366 strarray_add( &clean_files, "dlldata.c" );
1369 if (is_test)
1371 output( "testlist.c: $(MAKECTESTS) Makefile.in\n" );
1372 column = output( "\t$(MAKECTESTS) -o $@" );
1373 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1374 if (strendswith( source->name, ".c" ) && !is_generated_idl( source ))
1375 output_filename( source->name, &column );
1376 output( "\n" );
1377 column = output( "check test:" );
1378 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1379 if (strendswith( source->name, ".c" ) && !is_generated_idl( source ))
1380 output_filename( replace_extension( source->name, 2, ".ok" ), &column );
1381 output( "\n" );
1382 output( "clean testclean::\n" );
1383 column = output( "\t$(RM)" );
1384 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1385 if (strendswith( source->name, ".c" ) && !is_generated_idl( source ))
1386 output_filename( replace_extension( source->name, 2, ".ok" ), &column );
1387 output( "\n" );
1388 strarray_add( &clean_files, "testlist.c" );
1391 if (clean_files.count)
1393 output( "clean::\n" );
1394 column = output( "\t$(RM)" );
1395 for (i = 0; i < clean_files.count; i++) output_filename( clean_files.str[i], &column );
1396 output( "\n" );
1399 if (subdirs.count)
1401 for (i = column = 0; i < subdirs.count; i++) output_filename( subdirs.str[i], &column );
1402 output( ":\n" );
1403 output( "\t$(MKDIR_P) -m 755 $@\n" );
1408 /*******************************************************************
1409 * create_temp_file
1411 static FILE *create_temp_file( char **tmp_name )
1413 char *name = xmalloc( strlen(OutputFileName) + 13 );
1414 unsigned int i, id = getpid();
1415 int fd;
1416 FILE *ret = NULL;
1418 for (i = 0; i < 100; i++)
1420 sprintf( name, "%s.tmp%08x", OutputFileName, id );
1421 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
1423 ret = fdopen( fd, "w" );
1424 break;
1426 if (errno != EEXIST) break;
1427 id += 7777;
1429 if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
1430 *tmp_name = name;
1431 return ret;
1435 /*******************************************************************
1436 * output_dependencies
1438 static void output_dependencies(void)
1440 char *tmp_name = NULL;
1442 if (Separator && ((output_file = fopen( OutputFileName, "r" ))))
1444 char buffer[1024];
1445 FILE *tmp_file = create_temp_file( &tmp_name );
1446 int found = 0;
1448 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
1450 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
1451 fatal_error( "failed to write to %s\n", tmp_name );
1452 found = !strncmp( buffer, Separator, strlen(Separator) );
1454 fclose( output_file );
1455 output_file = tmp_file;
1456 if (!found && !list_empty(&sources)) output( "\n%s\n", Separator );
1458 else
1460 if (!(output_file = fopen( OutputFileName, Separator ? "a" : "w" )))
1461 fatal_perror( "%s", OutputFileName );
1464 if (!list_empty( &sources )) output_sources();
1466 fclose( output_file );
1467 output_file = NULL;
1469 if (tmp_name)
1471 int ret = rename( tmp_name, OutputFileName );
1472 if (ret == -1 && errno == EEXIST)
1474 /* rename doesn't overwrite on windows */
1475 unlink( OutputFileName );
1476 ret = rename( tmp_name, OutputFileName );
1478 if (ret == -1)
1480 unlink( tmp_name );
1481 fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
1483 free( tmp_name );
1488 /*******************************************************************
1489 * parse_option
1491 static void parse_option( const char *opt )
1493 switch(opt[1])
1495 case 'I':
1496 if (opt[2]) strarray_add( &include_args, opt );
1497 break;
1498 case 'C':
1499 src_dir = opt + 2;
1500 break;
1501 case 'S':
1502 top_src_dir = opt + 2;
1503 break;
1504 case 'T':
1505 top_obj_dir = opt + 2;
1506 break;
1507 case 'P':
1508 parent_dir = opt + 2;
1509 break;
1510 case 'f':
1511 if (opt[2]) OutputFileName = opt + 2;
1512 break;
1513 case 'R':
1514 relative_dir_mode = 1;
1515 break;
1516 case 's':
1517 if (opt[2]) Separator = opt + 2;
1518 else Separator = NULL;
1519 break;
1520 case 'x':
1521 if (opt[2]) strarray_add( &object_extensions, xstrdup( opt + 2 ));
1522 break;
1523 default:
1524 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
1525 exit(1);
1530 /*******************************************************************
1531 * main
1533 int main( int argc, char *argv[] )
1535 struct incl_file *pFile;
1536 int i, j;
1538 i = 1;
1539 while (i < argc)
1541 if (argv[i][0] == '-')
1543 parse_option( argv[i] );
1544 for (j = i; j < argc; j++) argv[j] = argv[j+1];
1545 argc--;
1547 else i++;
1550 if (relative_dir_mode)
1552 char *relpath;
1554 if (argc != 3)
1556 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
1557 exit( 1 );
1559 relpath = get_relative_path( argv[1], argv[2] );
1560 printf( "%s\n", relpath ? relpath : "." );
1561 exit( 0 );
1564 /* ignore redundant source paths */
1565 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
1566 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
1568 if (top_src_dir) strarray_insert( &include_args, 0, strmake( "-I%s/include", top_src_dir ));
1569 else if (top_obj_dir) strarray_insert( &include_args, 0, strmake( "-I%s/include", top_obj_dir ));
1571 /* set the default extension list for object files */
1572 if (!object_extensions.count) strarray_add( &object_extensions, "o" );
1574 for (i = 1; i < argc; i++) add_src_file( argv[i] );
1575 add_generated_sources();
1577 LIST_FOR_EACH_ENTRY( pFile, &includes, struct incl_file, entry ) parse_file( pFile, 0 );
1578 output_dependencies();
1579 return 0;