kernel32/tests: Add basic tests for SRWLock commands.
[wine/wine-gecko.git] / tools / makedep.c
blob108cdb05ec9509b8d81f5a57785bbf21535c397d
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013 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 <signal.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "wine/list.h"
38 struct incl_file
40 struct list entry;
41 char *name;
42 char *filename;
43 char *sourcename; /* source file name for generated headers */
44 struct incl_file *included_by; /* file that included this one */
45 int included_line; /* line where this file was included */
46 unsigned int flags; /* flags (see below) */
47 struct incl_file *owner;
48 unsigned int files_count; /* files in use */
49 unsigned int files_size; /* total allocated size */
50 struct incl_file **files;
53 #define FLAG_SYSTEM 0x0001 /* is it a system include (#include <name>) */
54 #define FLAG_GENERATED 0x0002 /* generated file */
55 #define FLAG_IDL_PROXY 0x0004 /* generates a proxy (_p.c) file */
56 #define FLAG_IDL_CLIENT 0x0008 /* generates a client (_c.c) file */
57 #define FLAG_IDL_SERVER 0x0010 /* generates a server (_s.c) file */
58 #define FLAG_IDL_IDENT 0x0020 /* generates an ident (_i.c) file */
59 #define FLAG_IDL_REGISTER 0x0040 /* generates a registration (_r.res) file */
60 #define FLAG_IDL_TYPELIB 0x0080 /* generates a typelib (.tlb) file */
61 #define FLAG_IDL_REGTYPELIB 0x0100 /* generates a registered typelib (_t.res) file */
62 #define FLAG_IDL_HEADER 0x0200 /* generates a header (.h) file */
63 #define FLAG_RC_PO 0x0400 /* rc file contains translations */
64 #define FLAG_C_IMPLIB 0x0800 /* file is part of an import library */
66 static const struct
68 unsigned int flag;
69 const char *ext;
70 } idl_outputs[] =
72 { FLAG_IDL_TYPELIB, ".tlb" },
73 { FLAG_IDL_REGTYPELIB, "_t.res" },
74 { FLAG_IDL_CLIENT, "_c.c" },
75 { FLAG_IDL_IDENT, "_i.c" },
76 { FLAG_IDL_PROXY, "_p.c" },
77 { FLAG_IDL_SERVER, "_s.c" },
78 { FLAG_IDL_REGISTER, "_r.res" },
79 { FLAG_IDL_HEADER, ".h" }
82 static struct list sources = LIST_INIT(sources);
83 static struct list includes = LIST_INIT(includes);
85 struct strarray
87 unsigned int count; /* strings in use */
88 unsigned int size; /* total allocated size */
89 const char **str;
92 static const struct strarray empty_strarray;
94 static struct strarray include_args;
95 static struct strarray define_args;
96 static struct strarray appmode;
97 static struct strarray dllflags;
98 static struct strarray imports;
99 static struct strarray make_vars;
100 static struct strarray cmdline_vars;
102 static const char *base_dir = ".";
103 static const char *src_dir;
104 static const char *top_src_dir;
105 static const char *top_obj_dir;
106 static const char *parent_dir;
107 static const char *tools_dir;
108 static const char *tools_ext;
109 static const char *makefile_name = "Makefile";
110 static const char *Separator = "### Dependencies";
111 static const char *input_file_name;
112 static const char *output_file_name;
113 static const char *temp_file_name;
114 static int parse_makefile_mode;
115 static int relative_dir_mode;
116 static int input_line;
117 static int output_column;
118 static FILE *output_file;
120 static const char Usage[] =
121 "Usage: makedep [options] [files]\n"
122 "Options:\n"
123 " -Idir Search for include files in directory 'dir'\n"
124 " -Cdir Search for source files in directory 'dir'\n"
125 " -Sdir Set the top source directory\n"
126 " -Tdir Set the top object directory\n"
127 " -Pdir Set the parent source directory\n"
128 " -M dirs Parse the makefiles from the specified directories\n"
129 " -R from to Compute the relative path between two directories\n"
130 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
131 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
134 #ifndef __GNUC__
135 #define __attribute__(x)
136 #endif
138 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
139 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
140 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
141 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
143 /*******************************************************************
144 * fatal_error
146 static void fatal_error( const char *msg, ... )
148 va_list valist;
149 va_start( valist, msg );
150 if (input_file_name)
152 fprintf( stderr, "%s:", input_file_name );
153 if (input_line) fprintf( stderr, "%d:", input_line );
154 fprintf( stderr, " error: " );
156 else fprintf( stderr, "makedep: error: " );
157 vfprintf( stderr, msg, valist );
158 va_end( valist );
159 exit(1);
163 /*******************************************************************
164 * fatal_perror
166 static void fatal_perror( const char *msg, ... )
168 va_list valist;
169 va_start( valist, msg );
170 if (input_file_name)
172 fprintf( stderr, "%s:", input_file_name );
173 if (input_line) fprintf( stderr, "%d:", input_line );
174 fprintf( stderr, " error: " );
176 else fprintf( stderr, "makedep: error: " );
177 vfprintf( stderr, msg, valist );
178 perror( " " );
179 va_end( valist );
180 exit(1);
184 /*******************************************************************
185 * cleanup_files
187 static void cleanup_files(void)
189 if (temp_file_name) unlink( temp_file_name );
190 if (output_file_name) unlink( output_file_name );
194 /*******************************************************************
195 * exit_on_signal
197 static void exit_on_signal( int sig )
199 exit( 1 ); /* this will call the atexit functions */
203 /*******************************************************************
204 * xmalloc
206 static void *xmalloc( size_t size )
208 void *res;
209 if (!(res = malloc (size ? size : 1)))
210 fatal_error( "Virtual memory exhausted.\n" );
211 return res;
215 /*******************************************************************
216 * xrealloc
218 static void *xrealloc (void *ptr, size_t size)
220 void *res;
221 assert( size );
222 if (!(res = realloc( ptr, size )))
223 fatal_error( "Virtual memory exhausted.\n" );
224 return res;
227 /*******************************************************************
228 * xstrdup
230 static char *xstrdup( const char *str )
232 char *res = strdup( str );
233 if (!res) fatal_error( "Virtual memory exhausted.\n" );
234 return res;
238 /*******************************************************************
239 * strmake
241 static char *strmake( const char* fmt, ... )
243 int n;
244 size_t size = 100;
245 va_list ap;
247 for (;;)
249 char *p = xmalloc (size);
250 va_start(ap, fmt);
251 n = vsnprintf (p, size, fmt, ap);
252 va_end(ap);
253 if (n == -1) size *= 2;
254 else if ((size_t)n >= size) size = n + 1;
255 else return p;
256 free(p);
261 /*******************************************************************
262 * strendswith
264 static int strendswith( const char* str, const char* end )
266 int l = strlen(str);
267 int m = strlen(end);
269 return l >= m && strcmp(str + l - m, end) == 0;
273 /*******************************************************************
274 * output
276 static void output( const char *format, ... )
278 int ret;
279 va_list valist;
281 va_start( valist, format );
282 ret = vfprintf( output_file, format, valist );
283 va_end( valist );
284 if (ret < 0) fatal_perror( "output" );
285 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
286 else output_column += ret;
290 /*******************************************************************
291 * strarray_add
293 static void strarray_add( struct strarray *array, const char *str )
295 if (array->count == array->size)
297 if (array->size) array->size *= 2;
298 else array->size = 16;
299 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
301 array->str[array->count++] = str;
305 /*******************************************************************
306 * strarray_addall
308 static void strarray_addall( struct strarray *array, struct strarray added )
310 unsigned int i;
312 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
316 /*******************************************************************
317 * strarray_insert
319 static void strarray_insert( struct strarray *array, unsigned int pos, const char *str )
321 unsigned int i;
323 strarray_add( array, NULL );
324 for (i = array->count - 1; i > pos; i--) array->str[i] = array->str[i - 1];
325 array->str[pos] = str;
329 /*******************************************************************
330 * strarray_add_uniq
332 static void strarray_add_uniq( struct strarray *array, const char *str )
334 unsigned int i;
336 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return;
337 strarray_add( array, str );
341 /*******************************************************************
342 * output_filename
344 static void output_filename( const char *name )
346 if (output_column + strlen(name) + 1 > 100)
348 output( " \\\n" );
349 output( " " );
351 else if (output_column) output( " " );
352 output( "%s", name );
356 /*******************************************************************
357 * output_filenames
359 static void output_filenames( struct strarray array )
361 unsigned int i;
363 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
367 /*******************************************************************
368 * get_extension
370 static char *get_extension( char *filename )
372 char *ext = strrchr( filename, '.' );
373 if (ext && strchr( ext, '/' )) ext = NULL;
374 return ext;
378 /*******************************************************************
379 * replace_extension
381 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
383 char *ret;
384 int name_len = strlen( name );
385 int ext_len = strlen( old_ext );
387 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
388 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
389 memcpy( ret, name, name_len );
390 strcpy( ret + name_len, new_ext );
391 return ret;
395 /*******************************************************************
396 * strarray_replace_extension
398 static struct strarray strarray_replace_extension( const struct strarray *array,
399 const char *old_ext, const char *new_ext )
401 unsigned int i;
402 struct strarray ret;
404 ret.count = ret.size = array->count;
405 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
406 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
407 return ret;
411 /*******************************************************************
412 * replace_substr
414 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
416 unsigned int pos = start - str;
417 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
418 memcpy( ret, str, pos );
419 strcpy( ret + pos, replace );
420 strcat( ret + pos, start + len );
421 return ret;
425 /*******************************************************************
426 * get_relative_path
428 * Determine where the destination path is located relative to the 'from' path.
430 static char *get_relative_path( const char *from, const char *dest )
432 const char *start;
433 char *ret, *p;
434 unsigned int dotdots = 0;
436 /* a path of "." is equivalent to an empty path */
437 if (!strcmp( from, "." )) from = "";
439 for (;;)
441 while (*from == '/') from++;
442 while (*dest == '/') dest++;
443 start = dest; /* save start of next path element */
444 if (!*from) break;
446 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
447 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
449 /* count remaining elements in 'from' */
452 dotdots++;
453 while (*from && *from != '/') from++;
454 while (*from == '/') from++;
456 while (*from);
457 break;
460 if (!start[0] && !dotdots) return NULL; /* empty path */
462 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
463 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
465 if (start[0]) strcpy( p, start );
466 else p[-1] = 0; /* remove trailing slash */
467 return ret;
471 /*******************************************************************
472 * src_dir_path
474 static char *src_dir_path( const char *path )
476 if (src_dir) return strmake( "%s/%s", src_dir, path );
477 return xstrdup( path );
481 /*******************************************************************
482 * top_obj_dir_path
484 static char *top_obj_dir_path( const char *path )
486 if (top_obj_dir) return strmake( "%s/%s", top_obj_dir, path );
487 return xstrdup( path );
491 /*******************************************************************
492 * top_dir_path
494 static char *top_dir_path( const char *path )
496 if (top_src_dir) return strmake( "%s/%s", top_src_dir, path );
497 return top_obj_dir_path( path );
501 /*******************************************************************
502 * tools_dir_path
504 static char *tools_dir_path( const char *path )
506 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
507 if (top_obj_dir) return strmake( "%s/tools/%s", top_obj_dir, path );
508 return strmake( "tools/%s", path );
512 /*******************************************************************
513 * tools_path
515 static char *tools_path( const char *name )
517 if (tools_dir) return strmake( "%s/tools/%s/%s%s", tools_dir, name, name, tools_ext );
518 if (top_obj_dir) return strmake( "%s/tools/%s/%s%s", top_obj_dir, name, name, tools_ext );
519 return strmake( "tools/%s/%s%s", name, name, tools_ext );
523 /*******************************************************************
524 * init_paths
526 static void init_paths(void)
528 /* ignore redundant source paths */
529 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
530 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
531 if (tools_dir && top_obj_dir && !strcmp( tools_dir, top_obj_dir )) tools_dir = NULL;
533 strarray_insert( &include_args, 0, strmake( "-I%s", top_dir_path( "include" )));
537 /*******************************************************************
538 * get_line
540 static char *get_line( FILE *file )
542 static char *buffer;
543 static unsigned int size;
545 if (!size)
547 size = 1024;
548 buffer = xmalloc( size );
550 if (!fgets( buffer, size, file )) return NULL;
551 input_line++;
553 for (;;)
555 char *p = buffer + strlen(buffer);
556 /* if line is larger than buffer, resize buffer */
557 while (p == buffer + size - 1 && p[-1] != '\n')
559 buffer = xrealloc( buffer, size * 2 );
560 if (!fgets( buffer + size - 1, size + 1, file )) break;
561 p = buffer + strlen(buffer);
562 size *= 2;
564 if (p > buffer && p[-1] == '\n')
566 *(--p) = 0;
567 if (p > buffer && p[-1] == '\r') *(--p) = 0;
568 if (p > buffer && p[-1] == '\\')
570 *(--p) = 0;
571 /* line ends in backslash, read continuation line */
572 if (!fgets( p, size - (p - buffer), file )) return buffer;
573 input_line++;
574 continue;
577 return buffer;
581 /*******************************************************************
582 * find_src_file
584 static struct incl_file *find_src_file( const char *name )
586 struct incl_file *file;
588 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
589 if (!strcmp( name, file->name )) return file;
590 return NULL;
593 /*******************************************************************
594 * find_include_file
596 static struct incl_file *find_include_file( const char *name )
598 struct incl_file *file;
600 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
601 if (!strcmp( name, file->name )) return file;
602 return NULL;
605 /*******************************************************************
606 * add_include
608 * Add an include file if it doesn't already exists.
610 static struct incl_file *add_include( struct incl_file *parent, const char *name, int system )
612 struct incl_file *include;
613 char *ext;
615 if (parent->files_count >= parent->files_size)
617 parent->files_size *= 2;
618 if (parent->files_size < 16) parent->files_size = 16;
619 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
622 /* enforce some rules for the Wine tree */
624 if (!memcmp( name, "../", 3 ))
625 fatal_error( "#include directive with relative path not allowed\n" );
627 if (!strcmp( name, "config.h" ))
629 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
630 fatal_error( "config.h must not be included by a header file\n" );
631 if (parent->files_count)
632 fatal_error( "config.h must be included before anything else\n" );
634 else if (!strcmp( name, "wine/port.h" ))
636 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
637 fatal_error( "wine/port.h must not be included by a header file\n" );
638 if (!parent->files_count) fatal_error( "config.h must be included before wine/port.h\n" );
639 if (parent->files_count > 1)
640 fatal_error( "wine/port.h must be included before everything except config.h\n" );
641 if (strcmp( parent->files[0]->name, "config.h" ))
642 fatal_error( "config.h must be included before wine/port.h\n" );
645 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
646 if (!strcmp( name, include->name )) goto found;
648 include = xmalloc( sizeof(*include) );
649 memset( include, 0, sizeof(*include) );
650 include->name = xstrdup(name);
651 include->included_by = parent;
652 include->included_line = input_line;
653 if (system) include->flags |= FLAG_SYSTEM;
654 list_add_tail( &includes, &include->entry );
655 found:
656 parent->files[parent->files_count++] = include;
657 return include;
661 /*******************************************************************
662 * open_file
664 static FILE *open_file( const char *path )
666 FILE *ret;
668 if (path[0] != '/' && strcmp( base_dir, "." ))
670 char *full_path = strmake( "%s/%s", base_dir, path );
671 ret = fopen( full_path, "r" );
672 free( full_path );
674 else ret = fopen( path, "r" );
676 return ret;
679 /*******************************************************************
680 * open_src_file
682 static FILE *open_src_file( struct incl_file *pFile )
684 FILE *file;
686 /* first try name as is */
687 if ((file = open_file( pFile->name )))
689 pFile->filename = xstrdup( pFile->name );
690 return file;
692 /* now try in source dir */
693 if (src_dir)
695 pFile->filename = src_dir_path( pFile->name );
696 file = open_file( pFile->filename );
698 /* now try parent dir */
699 if (!file && parent_dir)
701 pFile->filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
702 file = open_file( pFile->filename );
704 if (!file) fatal_perror( "open %s", pFile->name );
705 return file;
709 /*******************************************************************
710 * open_include_file
712 static FILE *open_include_file( struct incl_file *pFile )
714 FILE *file = NULL;
715 char *filename, *p;
716 unsigned int i, len;
718 errno = ENOENT;
720 /* check for generated bison header */
722 if (strendswith( pFile->name, ".tab.h" ))
724 filename = src_dir_path( replace_extension( pFile->name, ".tab.h", ".y" ));
725 if ((file = open_file( filename )))
727 pFile->sourcename = filename;
728 pFile->filename = xstrdup( pFile->name );
729 /* don't bother to parse it */
730 fclose( file );
731 return NULL;
733 free( filename );
736 /* check for corresponding idl file in source dir */
738 if (strendswith( pFile->name, ".h" ))
740 filename = src_dir_path( replace_extension( pFile->name, ".h", ".idl" ));
741 if ((file = open_file( filename )))
743 pFile->sourcename = filename;
744 pFile->filename = xstrdup( pFile->name );
745 return file;
747 free( filename );
750 /* now try in source dir */
751 filename = src_dir_path( pFile->name );
752 if ((file = open_file( filename ))) goto found;
753 free( filename );
755 /* now try in parent source dir */
756 if (parent_dir)
758 filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
759 if ((file = open_file( filename ))) goto found;
760 free( filename );
763 /* check for corresponding idl file in global includes */
765 if (strendswith( pFile->name, ".h" ))
767 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".idl" )));
768 if ((file = open_file( filename )))
770 pFile->sourcename = filename;
771 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
772 return file;
774 free( filename );
777 /* check for corresponding .in file in global includes (for config.h.in) */
779 if (strendswith( pFile->name, ".h" ))
781 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".h.in" )));
782 if ((file = open_file( filename )))
784 pFile->sourcename = filename;
785 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
786 return file;
788 free( filename );
791 /* check for corresponding .x file in global includes */
793 if (strendswith( pFile->name, "tmpl.h" ))
795 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".x" )));
796 if ((file = open_file( filename )))
798 pFile->sourcename = filename;
799 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
800 return file;
802 free( filename );
805 /* now search in include paths */
806 for (i = 0; i < include_args.count; i++)
808 const char *dir = include_args.str[i] + 2; /* skip -I */
809 if (*dir == '/')
811 /* ignore absolute paths that don't point into the source dir */
812 if (!top_src_dir) continue;
813 len = strlen( top_src_dir );
814 if (strncmp( dir, top_src_dir, len )) continue;
815 if (dir[len] && dir[len] != '/') continue;
817 filename = strmake( "%s/%s", dir, pFile->name );
818 if ((file = open_file( filename ))) goto found;
819 free( filename );
821 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
823 /* try in src file directory */
824 if ((p = strrchr(pFile->included_by->filename, '/')))
826 int l = p - pFile->included_by->filename + 1;
827 filename = xmalloc(l + strlen(pFile->name) + 1);
828 memcpy( filename, pFile->included_by->filename, l );
829 strcpy( filename + l, pFile->name );
830 if ((file = open_file( filename ))) goto found;
831 free( filename );
834 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
835 perror( pFile->name );
836 pFile = pFile->included_by;
837 while (pFile && pFile->included_by)
839 const char *parent = pFile->included_by->sourcename;
840 if (!parent) parent = pFile->included_by->name;
841 fprintf( stderr, "%s:%d: note: %s was first included here\n",
842 parent, pFile->included_line, pFile->name );
843 pFile = pFile->included_by;
845 exit(1);
847 found:
848 pFile->filename = filename;
849 return file;
853 /*******************************************************************
854 * parse_include_directive
856 static void parse_include_directive( struct incl_file *source, char *str )
858 char quote, *include, *p = str;
860 while (*p && isspace(*p)) p++;
861 if (*p != '\"' && *p != '<' ) return;
862 quote = *p++;
863 if (quote == '<') quote = '>';
864 include = p;
865 while (*p && (*p != quote)) p++;
866 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
867 *p = 0;
868 add_include( source, include, (quote == '>') );
872 /*******************************************************************
873 * parse_pragma_directive
875 static void parse_pragma_directive( struct incl_file *source, char *str )
877 char *flag, *p = str;
879 if (!isspace( *p )) return;
880 while (*p && isspace(*p)) p++;
881 p = strtok( p, " \t" );
882 if (strcmp( p, "makedep" )) return;
884 while ((flag = strtok( NULL, " \t" )))
886 if (!strcmp( flag, "depend" ))
888 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
889 return;
891 if (strendswith( source->name, ".idl" ))
893 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
894 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
895 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
896 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
897 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
898 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
899 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
900 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
902 else if (strendswith( source->name, ".rc" ))
904 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
906 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
911 /*******************************************************************
912 * parse_cpp_directive
914 static void parse_cpp_directive( struct incl_file *source, char *str )
916 while (*str && isspace(*str)) str++;
917 if (*str++ != '#') return;
918 while (*str && isspace(*str)) str++;
920 if (!strncmp( str, "include", 7 ))
921 parse_include_directive( source, str + 7 );
922 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
923 parse_include_directive( source, str + 6 );
924 else if (!strncmp( str, "pragma", 6 ))
925 parse_pragma_directive( source, str + 6 );
929 /*******************************************************************
930 * parse_idl_file
932 * If for_h_file is non-zero, it means we are not interested in the idl file
933 * itself, but only in the contents of the .h file that will be generated from it.
935 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
937 char *buffer, *include;
939 input_line = 0;
940 if (for_h_file)
942 /* generated .h file always includes these */
943 add_include( pFile, "rpc.h", 1 );
944 add_include( pFile, "rpcndr.h", 1 );
947 while ((buffer = get_line( file )))
949 char quote;
950 char *p = buffer;
951 while (*p && isspace(*p)) p++;
953 if (!strncmp( p, "import", 6 ))
955 p += 6;
956 while (*p && isspace(*p)) p++;
957 if (*p != '"') continue;
958 include = ++p;
959 while (*p && (*p != '"')) p++;
960 if (!*p) fatal_error( "malformed import directive\n" );
961 *p = 0;
962 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
963 add_include( pFile, include, 0 );
964 continue;
967 if (for_h_file) /* only check for #include inside cpp_quote */
969 if (strncmp( p, "cpp_quote", 9 )) continue;
970 p += 9;
971 while (*p && isspace(*p)) p++;
972 if (*p++ != '(') continue;
973 while (*p && isspace(*p)) p++;
974 if (*p++ != '"') continue;
975 if (*p++ != '#') continue;
976 while (*p && isspace(*p)) p++;
977 if (strncmp( p, "include", 7 )) continue;
978 p += 7;
979 while (*p && isspace(*p)) p++;
980 if (*p == '\\' && p[1] == '"')
982 p += 2;
983 quote = '"';
985 else
987 if (*p++ != '<' ) continue;
988 quote = '>';
990 include = p;
991 while (*p && (*p != quote)) p++;
992 if (!*p || (quote == '"' && p[-1] != '\\'))
993 fatal_error( "malformed #include directive inside cpp_quote\n" );
994 if (quote == '"') p--; /* remove backslash */
995 *p = 0;
996 add_include( pFile, include, (quote == '>') );
997 continue;
1000 parse_cpp_directive( pFile, p );
1004 /*******************************************************************
1005 * parse_c_file
1007 static void parse_c_file( struct incl_file *pFile, FILE *file )
1009 char *buffer;
1011 input_line = 0;
1012 while ((buffer = get_line( file )))
1014 parse_cpp_directive( pFile, buffer );
1019 /*******************************************************************
1020 * parse_rc_file
1022 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1024 char *buffer, *include;
1026 input_line = 0;
1027 while ((buffer = get_line( file )))
1029 char quote;
1030 char *p = buffer;
1031 while (*p && isspace(*p)) p++;
1033 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1035 p += 2;
1036 while (*p && isspace(*p)) p++;
1037 if (strncmp( p, "@makedep:", 9 )) continue;
1038 p += 9;
1039 while (*p && isspace(*p)) p++;
1040 quote = '"';
1041 if (*p == quote)
1043 include = ++p;
1044 while (*p && *p != quote) p++;
1046 else
1048 include = p;
1049 while (*p && !isspace(*p) && *p != '*') p++;
1051 if (!*p)
1052 fatal_error( "malformed makedep comment\n" );
1053 *p = 0;
1054 add_include( pFile, include, (quote == '>') );
1055 continue;
1058 parse_cpp_directive( pFile, buffer );
1063 /*******************************************************************
1064 * parse_in_file
1066 static void parse_in_file( struct incl_file *source, FILE *file )
1068 char *p, *buffer;
1070 /* make sure it gets rebuilt when the version changes */
1071 add_include( source, "config.h", 1 );
1073 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1075 input_line = 0;
1076 while ((buffer = get_line( file )))
1078 if (strncmp( buffer, ".TH", 3 )) continue;
1079 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1080 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1081 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1082 source->sourcename = xstrdup( p ); /* abuse source name to store section */
1083 return;
1088 /*******************************************************************
1089 * parse_file
1091 static void parse_file( struct incl_file *source, int src )
1093 FILE *file;
1095 /* don't try to open certain types of files */
1096 if (strendswith( source->name, ".tlb" ))
1098 source->filename = xstrdup( source->name );
1099 return;
1102 file = src ? open_src_file( source ) : open_include_file( source );
1103 if (!file) return;
1104 input_file_name = source->filename;
1106 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1107 parse_idl_file( source, file, 1 );
1108 else if (strendswith( source->filename, ".idl" ))
1109 parse_idl_file( source, file, 0 );
1110 else if (strendswith( source->filename, ".c" ) ||
1111 strendswith( source->filename, ".m" ) ||
1112 strendswith( source->filename, ".h" ) ||
1113 strendswith( source->filename, ".l" ) ||
1114 strendswith( source->filename, ".y" ))
1115 parse_c_file( source, file );
1116 else if (strendswith( source->filename, ".rc" ))
1117 parse_rc_file( source, file );
1118 else if (strendswith( source->filename, ".in" ))
1119 parse_in_file( source, file );
1120 fclose(file);
1121 input_file_name = NULL;
1125 /*******************************************************************
1126 * add_src_file
1128 * Add a source file to the list.
1130 static struct incl_file *add_src_file( const char *name )
1132 struct incl_file *file;
1134 if ((file = find_src_file( name ))) return file; /* we already have it */
1135 file = xmalloc( sizeof(*file) );
1136 memset( file, 0, sizeof(*file) );
1137 file->name = xstrdup(name);
1138 list_add_tail( &sources, &file->entry );
1139 parse_file( file, 1 );
1140 return file;
1144 /*******************************************************************
1145 * get_make_variable
1147 static char *get_make_variable( const char *name )
1149 unsigned int i;
1151 for (i = 0; i < cmdline_vars.count; i += 2)
1152 if (!strcmp( cmdline_vars.str[i], name ))
1153 return xstrdup( cmdline_vars.str[i + 1] );
1155 for (i = 0; i < make_vars.count; i += 2)
1156 if (!strcmp( make_vars.str[i], name ))
1157 return xstrdup( make_vars.str[i + 1] );
1158 return NULL;
1162 /*******************************************************************
1163 * get_expanded_make_variable
1165 static char *get_expanded_make_variable( const char *name )
1167 char *p, *end, *var, *expand, *tmp;
1169 expand = get_make_variable( name );
1170 if (!expand) return NULL;
1172 p = expand;
1173 while ((p = strchr( p, '$' )))
1175 if (p[1] == '(')
1177 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1178 *end++ = 0;
1179 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1180 var = get_make_variable( p + 2 );
1181 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1182 free( var );
1184 else if (p[1] == '{') /* don't expand ${} variables */
1186 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1187 p = end + 1;
1188 continue;
1190 else if (p[1] == '$')
1192 tmp = replace_substr( expand, p, 2, "$" );
1194 else fatal_error( "syntax error in '%s'\n", expand );
1196 /* switch to the new string */
1197 p = tmp + (p - expand);
1198 free( expand );
1199 expand = tmp;
1202 /* consider empty variables undefined */
1203 p = expand;
1204 while (*p && isspace(*p)) p++;
1205 if (*p) return expand;
1206 free( expand );
1207 return NULL;
1211 /*******************************************************************
1212 * get_expanded_make_var_array
1214 static struct strarray get_expanded_make_var_array( const char *name )
1216 struct strarray ret = empty_strarray;
1217 char *value, *token;
1219 if ((value = get_expanded_make_variable( name )))
1220 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1221 strarray_add( &ret, token );
1222 return ret;
1226 /*******************************************************************
1227 * set_make_variable
1229 static int set_make_variable( struct strarray *array, const char *assignment )
1231 unsigned int i;
1232 char *p, *name;
1234 p = name = xstrdup( assignment );
1235 while (isalnum(*p) || *p == '_') p++;
1236 if (name == p) return 0; /* not a variable */
1237 if (isspace(*p))
1239 *p++ = 0;
1240 while (isspace(*p)) p++;
1242 if (*p != '=') return 0; /* not an assignment */
1243 *p++ = 0;
1244 while (isspace(*p)) p++;
1246 /* redefining a variable replaces the previous value */
1247 for (i = 0; i < array->count; i += 2)
1249 if (strcmp( array->str[i], name )) continue;
1250 array->str[i + 1] = p;
1251 return 1;
1253 strarray_add( array, name );
1254 strarray_add( array, p );
1255 return 1;
1259 /*******************************************************************
1260 * parse_makefile
1262 static void parse_makefile(void)
1264 char *buffer;
1265 FILE *file;
1267 input_file_name = strmake( "%s/%s", base_dir, makefile_name );
1268 if (!(file = fopen( input_file_name, "r" )))
1270 fatal_perror( "open" );
1271 exit( 1 );
1274 input_line = 0;
1275 while ((buffer = get_line( file )))
1277 if (Separator && !strncmp( buffer, Separator, strlen(Separator) )) break;
1278 if (*buffer == '\t') continue; /* command */
1279 while (isspace( *buffer )) buffer++;
1280 if (*buffer == '#') continue; /* comment */
1281 set_make_variable( &make_vars, buffer );
1283 fclose( file );
1284 input_file_name = NULL;
1288 /*******************************************************************
1289 * add_generated_source
1291 * Add a generated source file to the list.
1293 static struct incl_file *add_generated_source( const char *name, const char *filename )
1295 struct incl_file *file;
1297 if ((file = find_src_file( name ))) return file; /* we already have it */
1298 file = xmalloc( sizeof(*file) );
1299 memset( file, 0, sizeof(*file) );
1300 file->name = xstrdup( name );
1301 file->filename = xstrdup( filename ? filename : name );
1302 file->flags = FLAG_GENERATED;
1303 list_add_tail( &sources, &file->entry );
1304 return file;
1308 /*******************************************************************
1309 * add_generated_sources
1311 static void add_generated_sources(void)
1313 struct incl_file *source, *next, *file;
1315 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1317 if (source->flags & FLAG_IDL_CLIENT)
1319 file = add_generated_source( replace_extension( source->name, ".idl", "_c.c" ), NULL );
1320 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1322 if (source->flags & FLAG_IDL_SERVER)
1324 file = add_generated_source( replace_extension( source->name, ".idl", "_s.c" ), NULL );
1325 add_include( file, "wine/exception.h", 0 );
1326 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1328 if (source->flags & FLAG_IDL_IDENT)
1330 file = add_generated_source( replace_extension( source->name, ".idl", "_i.c" ), NULL );
1331 add_include( file, "rpc.h", 0 );
1332 add_include( file, "rpcndr.h", 0 );
1333 add_include( file, "guiddef.h", 0 );
1335 if (source->flags & FLAG_IDL_PROXY)
1337 file = add_generated_source( "dlldata.o", "dlldata.c" );
1338 add_include( file, "objbase.h", 0 );
1339 add_include( file, "rpcproxy.h", 0 );
1340 file = add_generated_source( replace_extension( source->name, ".idl", "_p.c" ), NULL );
1341 add_include( file, "objbase.h", 0 );
1342 add_include( file, "rpcproxy.h", 0 );
1343 add_include( file, "wine/exception.h", 0 );
1344 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1346 if (source->flags & FLAG_IDL_REGTYPELIB)
1348 add_generated_source( replace_extension( source->name, ".idl", "_t.res" ), NULL );
1350 if (source->flags & FLAG_IDL_REGISTER)
1352 add_generated_source( replace_extension( source->name, ".idl", "_r.res" ), NULL );
1354 if (strendswith( source->name, ".y" ))
1356 file = add_generated_source( replace_extension( source->name, ".y", ".tab.c" ), NULL );
1357 /* steal the includes list from the source file */
1358 file->files_count = source->files_count;
1359 file->files_size = source->files_size;
1360 file->files = source->files;
1361 source->files_count = source->files_size = 0;
1362 source->files = NULL;
1364 if (strendswith( source->name, ".l" ))
1366 file = add_generated_source( replace_extension( source->name, ".l", ".yy.c" ), NULL );
1367 /* steal the includes list from the source file */
1368 file->files_count = source->files_count;
1369 file->files_size = source->files_size;
1370 file->files = source->files;
1371 source->files_count = source->files_size = 0;
1372 source->files = NULL;
1375 if (get_make_variable( "TESTDLL" ))
1377 file = add_generated_source( "testlist.o", "testlist.c" );
1378 add_include( file, "wine/test.h", 0 );
1383 /*******************************************************************
1384 * output_include
1386 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1388 int i;
1390 if (pFile->owner == owner) return;
1391 if (!pFile->filename) return;
1392 pFile->owner = owner;
1393 output_filename( pFile->filename );
1394 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1398 /*******************************************************************
1399 * output_sources
1401 static struct strarray output_sources(void)
1403 struct incl_file *source;
1404 int i, is_win16 = 0;
1405 const char *dllext = ".so";
1406 struct strarray object_files = empty_strarray;
1407 struct strarray crossobj_files = empty_strarray;
1408 struct strarray res_files = empty_strarray;
1409 struct strarray clean_files = empty_strarray;
1410 struct strarray po_files = empty_strarray;
1411 struct strarray mo_files = empty_strarray;
1412 struct strarray mc_files = empty_strarray;
1413 struct strarray test_files = empty_strarray;
1414 struct strarray dlldata_files = empty_strarray;
1415 struct strarray c2man_files = empty_strarray;
1416 struct strarray implib_objs = empty_strarray;
1417 struct strarray includes = empty_strarray;
1418 struct strarray phony_targets = empty_strarray;
1419 struct strarray linguas = get_expanded_make_var_array( "LINGUAS" );
1420 struct strarray all_targets = get_expanded_make_var_array( "PROGRAMS" );
1421 struct strarray targetflags = get_expanded_make_var_array( "TARGETFLAGS" );
1422 struct strarray delayimports = get_expanded_make_var_array( "DELAYIMPORTS" );
1423 struct strarray extradllflags = get_expanded_make_var_array( "EXTRADLLFLAGS" );
1424 char *module = get_expanded_make_variable( "MODULE" );
1425 char *exeext = get_expanded_make_variable( "EXEEXT" );
1426 char *testdll = get_expanded_make_variable( "TESTDLL" );
1427 char *staticlib = get_expanded_make_variable( "STATICLIB" );
1428 char *crosstarget = get_expanded_make_variable( "CROSSTARGET" );
1430 if (exeext && !strcmp( exeext, ".exe" )) dllext = "";
1431 if (module && strendswith( module, ".a" )) staticlib = module;
1432 for (i = 0; i < extradllflags.count; i++) if (!strcmp( extradllflags.str[i], "-m16" )) is_win16 = 1;
1434 for (i = 0; i < linguas.count; i++)
1435 strarray_add( &mo_files, strmake( "%s/po/%s.mo", top_obj_dir, linguas.str[i] ));
1437 strarray_add( &includes, "-I." );
1438 if (src_dir) strarray_add( &includes, strmake( "-I%s", src_dir ));
1439 if (parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( parent_dir )));
1440 if (top_src_dir && top_obj_dir) strarray_add( &includes, strmake( "-I%s/include", top_obj_dir ));
1441 strarray_addall( &includes, include_args );
1443 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1445 struct strarray extradefs;
1446 char *subdir = NULL;
1447 char *obj = xstrdup( source->name );
1448 char *ext = get_extension( obj );
1450 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1451 *ext++ = 0;
1453 if (src_dir && strchr( obj, '/' ))
1455 subdir = xstrdup( obj );
1456 *strrchr( subdir, '/' ) = 0;
1459 extradefs = get_expanded_make_var_array( strmake( "%s_EXTRADEFS", obj ));
1461 if (!strcmp( ext, "y" )) /* yacc file */
1463 /* add source file dependency for parallel makes */
1464 char *header = strmake( "%s.tab.h", obj );
1466 if (find_include_file( header ))
1468 output( "%s.tab.h: %s\n", obj, source->filename );
1469 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1470 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1471 obj, obj, source->filename );
1472 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1473 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1475 else output( "%s.tab.c: %s\n", obj, source->filename );
1477 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1478 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1479 free( header );
1480 continue; /* no dependencies */
1482 else if (!strcmp( ext, "x" )) /* template file */
1484 output( "%s.h: %s%s %s\n", obj, tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1485 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1486 output( "\t%s%s -H -o $@ %s\n", tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1487 strarray_add( &clean_files, strmake( "%s.h", obj ));
1488 continue; /* no dependencies */
1490 else if (!strcmp( ext, "l" )) /* lex file */
1492 output( "%s.yy.c: %s\n", obj, source->filename );
1493 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1494 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1495 continue; /* no dependencies */
1497 else if (!strcmp( ext, "rc" )) /* resource file */
1499 strarray_add( &res_files, strmake( "%s.res", obj ));
1500 output( "%s.res: %s %s\n", obj, tools_path( "wrc" ), source->filename );
1501 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1502 output( "\t%s -o $@ %s", tools_path( "wrc" ), source->filename );
1503 if (is_win16) output_filename( "-m16" );
1504 else output_filenames( targetflags );
1505 output_filename( "--nostdinc" );
1506 output_filenames( includes );
1507 output_filenames( define_args );
1508 output_filenames( extradefs );
1509 if (mo_files.count && (source->flags & FLAG_RC_PO))
1511 strarray_add( &po_files, source->filename );
1512 output_filename( strmake( "--po-dir=%s/po", top_obj_dir ));
1513 output( "\n" );
1514 output( "%s.res:", obj );
1515 output_filenames( mo_files );
1516 output( "\n" );
1517 output( "rsrc.pot " );
1519 else output( "\n" );
1520 output( "%s.res:", obj );
1522 else if (!strcmp( ext, "mc" )) /* message file */
1524 strarray_add( &res_files, strmake( "%s.res", obj ));
1525 output( "%s.res: %s %s\n", obj, tools_path( "wmc" ), source->filename );
1526 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1527 output( "\t%s -U -O res -o $@ %s", tools_path( "wmc" ), source->filename );
1528 if (mo_files.count)
1530 strarray_add( &mc_files, source->filename );
1531 output_filename( strmake( "--po-dir=%s/po", top_obj_dir ));
1532 output( "\n" );
1533 output( "%s.res:", obj );
1534 output_filenames( mo_files );
1535 output( "\n" );
1536 output( "msg.pot " );
1538 else output( "\n" );
1539 output( "%s.res:", obj );
1541 else if (!strcmp( ext, "idl" )) /* IDL file */
1543 struct strarray targets = empty_strarray;
1544 unsigned int i;
1545 char *dest;
1547 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1548 source->flags |= FLAG_IDL_HEADER;
1550 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1552 if (!(source->flags & idl_outputs[i].flag)) continue;
1553 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1554 if (!find_src_file( dest )) strarray_add( &clean_files, dest );
1555 strarray_add( &targets, dest );
1557 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1558 output_filenames( targets );
1559 output( ": %s\n", tools_path( "widl" ));
1560 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1561 output( "\t%s -o $@ %s", tools_path( "widl" ), source->filename );
1562 output_filenames( targetflags );
1563 output_filenames( includes );
1564 output_filenames( define_args );
1565 output_filenames( extradefs );
1566 output_filenames( get_expanded_make_var_array( "EXTRAIDLFLAGS" ));
1567 output( "\n" );
1568 output_filenames( targets );
1569 output( ": %s", source->filename );
1571 else if (!strcmp( ext, "in" )) /* .in file or man page */
1573 if (strendswith( obj, ".man" ) && source->sourcename)
1575 char *dir, *dest = replace_extension( obj, ".man", "" );
1576 char *lang = strchr( dest, '.' );
1577 if (lang)
1579 *lang++ = 0;
1580 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, source->sourcename );
1582 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", source->sourcename );
1583 output( "install-man-pages:: %s\n", obj );
1584 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n",
1585 obj, dir, dest, source->sourcename );
1586 output( "uninstall::\n" );
1587 output( "\t$(RM) %s/%s.%s\n",
1588 dir, dest, source->sourcename );
1589 free( dest );
1590 free( dir );
1591 strarray_add( &all_targets, xstrdup(obj) );
1592 strarray_add_uniq( &phony_targets, "install-man-pages" );
1594 else strarray_add( &clean_files, xstrdup(obj) );
1595 output( "%s: %s\n", obj, source->filename );
1596 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1597 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1598 output( "%s:", obj );
1600 else if (!strcmp( ext, "sfd" )) /* font file */
1602 char *fontforge = get_expanded_make_variable( "FONTFORGE" );
1603 if (fontforge && !src_dir)
1605 output( "%s.ttf: %s\n", obj, source->filename );
1606 output( "\t%s -script %s %s $@\n",
1607 fontforge, top_dir_path( "fonts/genttf.ff" ), source->filename );
1609 free( fontforge );
1610 continue; /* no dependencies */
1612 else if (!strcmp( ext, "svg" )) /* svg file */
1614 char *convert = get_expanded_make_variable( "CONVERT" );
1615 char *rsvg = get_expanded_make_variable( "RSVG" );
1616 char *icotool = get_expanded_make_variable( "ICOTOOL" );
1617 if (convert && rsvg && icotool && !src_dir)
1619 output( "%s.ico %s.bmp: %s\n", obj, obj, source->filename );
1620 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
1621 convert, icotool, rsvg, top_dir_path( "tools/buildimage" ), source->filename );
1623 free( convert );
1624 free( rsvg );
1625 free( icotool );
1626 continue; /* no dependencies */
1628 else if (!strcmp( ext, "res" ))
1630 strarray_add( &res_files, source->name );
1631 continue; /* no dependencies */
1633 else
1635 int need_cross = testdll || (source->flags & FLAG_C_IMPLIB) || (module && staticlib);
1637 if (source->flags & FLAG_GENERATED) strarray_add( &clean_files, source->filename );
1638 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1639 strarray_add( &object_files, strmake( "%s.o", obj ));
1640 output( "%s.o: %s\n", obj, source->filename );
1641 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1642 output( "\t$(CC) -c -o $@ %s", source->filename );
1643 output_filenames( includes );
1644 output_filenames( define_args );
1645 output_filenames( extradefs );
1646 if (module || staticlib || testdll) output_filenames( dllflags );
1647 output_filenames( get_expanded_make_var_array( "EXTRACFLAGS" ));
1648 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1649 output_filename( "$(CFLAGS)" );
1650 output( "\n" );
1651 if (crosstarget && need_cross)
1653 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1654 output( "%s.cross.o: %s\n", obj, source->filename );
1655 if (subdir) output( "\t$(MKDIR_P) -m 755 %s\n", subdir );
1656 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1657 output_filenames( includes );
1658 output_filenames( define_args );
1659 output_filenames( extradefs );
1660 output_filename( "-DWINE_CROSSTEST" );
1661 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1662 output_filename( "$(CFLAGS)" );
1663 output( "\n" );
1665 if (testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1667 strarray_add( &test_files, source->name );
1668 output( "%s.ok:\n", obj );
1669 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1670 top_dir_path( "tools/runtest" ), top_obj_dir,
1671 testdll, replace_extension( testdll, ".dll", "_test.exe" ), dllext, obj );
1673 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1674 strarray_add( &c2man_files, source->filename );
1675 output( "%s.o", obj );
1676 if (crosstarget && need_cross) output( " %s.cross.o", obj );
1677 output( ":" );
1679 free( obj );
1681 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
1682 output( "\n" );
1685 /* rules for files that depend on multiple sources */
1687 if (po_files.count)
1689 output( "rsrc.pot: %s", tools_path( "wrc" ) );
1690 output_filenames( po_files );
1691 output( "\n" );
1692 output( "\t%s -O pot -o $@", tools_path( "wrc" ));
1693 output_filenames( po_files );
1694 if (is_win16) output_filename( "-m16" );
1695 else output_filenames( targetflags );
1696 output_filename( "--nostdinc" );
1697 output_filenames( includes );
1698 output_filenames( define_args );
1699 output( "\n" );
1700 strarray_add( &clean_files, "rsrc.pot" );
1703 if (mc_files.count)
1705 output( "msg.pot: %s", tools_path( "wmc" ));
1706 output_filenames( mc_files );
1707 output( "\n" );
1708 output( "\t%s -O pot -o $@", tools_path( "wmc" ));
1709 output_filenames( mc_files );
1710 output( "\n" );
1711 strarray_add( &clean_files, "msg.pot" );
1714 if (dlldata_files.count)
1716 output( "dlldata.c: %s %s\n", tools_path( "widl" ), src_dir_path( "Makefile.in" ));
1717 output( "\t%s --dlldata-only -o $@", tools_path( "widl" ));
1718 output_filenames( dlldata_files );
1719 output( "\n" );
1722 if (module && !staticlib)
1724 char *importlib = get_expanded_make_variable( "IMPORTLIB" );
1725 struct strarray all_libs = empty_strarray;
1726 char *spec_file = NULL;
1728 if (!appmode.count) spec_file = src_dir_path( replace_extension( module, ".dll", ".spec" ));
1729 for (i = 0; i < delayimports.count; i++)
1730 strarray_add( &all_libs, strmake( "-l%s", delayimports.str[i] ));
1731 for (i = 0; i < imports.count; i++)
1732 strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1733 for (i = 0; i < delayimports.count; i++)
1734 strarray_add( &all_libs, strmake( "-Wb,-d%s", delayimports.str[i] ));
1735 strarray_add( &all_libs, "-lwine" );
1736 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBPORT" ));
1737 strarray_addall( &all_libs, get_expanded_make_var_array( "EXTRALIBS" ));
1738 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1740 if (*dllext)
1742 strarray_add( &all_targets, strmake( "%s%s", module, dllext ));
1743 strarray_add( &all_targets, strmake( "%s.fake", module ));
1744 output( "%s%s %s.fake:", module, dllext, module );
1746 else
1748 strarray_add( &all_targets, module );
1749 output( "%s:", module );
1751 if (spec_file) output_filename( spec_file );
1752 output_filenames( object_files );
1753 output_filenames( res_files );
1754 output( "\n" );
1755 output( "\t%s -o $@", tools_path( "winegcc" ));
1756 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1757 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1758 output_filenames( targetflags );
1759 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1760 if (spec_file)
1762 output( " -shared %s", spec_file );
1763 output_filenames( extradllflags );
1765 else output_filenames( appmode );
1766 output_filenames( object_files );
1767 output_filenames( res_files );
1768 output_filenames( all_libs );
1769 output_filename( "$(LDFLAGS)" );
1770 output( "\n" );
1772 if (spec_file && importlib)
1774 if (*dllext)
1776 strarray_add( &clean_files, strmake( "lib%s.def", importlib ));
1777 output( "lib%s.def: %s %s\n", importlib, tools_path( "winebuild" ), spec_file );
1778 output( "\t%s -w --def -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1779 output_filenames( targetflags );
1780 if (is_win16) output_filename( "-m16" );
1781 output( "\n" );
1782 if (implib_objs.count)
1784 strarray_add( &clean_files, strmake( "lib%s.def.a", importlib ));
1785 output( "lib%s.def.a:", importlib );
1786 output_filenames( implib_objs );
1787 output( "\n" );
1788 output( "\t$(RM) $@\n" );
1789 output( "\t$(AR) $(ARFLAGS) $@" );
1790 output_filenames( implib_objs );
1791 output( "\n" );
1792 output( "\t$(RANLIB) $@\n" );
1795 else
1797 strarray_add( &clean_files, strmake( "lib%s.a", importlib ));
1798 output( "lib%s.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1799 output_filenames( implib_objs );
1800 output( "\n" );
1801 output( "\t%s -w --implib -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1802 output_filenames( targetflags );
1803 output_filenames( implib_objs );
1804 output( "\n" );
1806 if (crosstarget && !is_win16)
1808 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
1809 strarray_add( &clean_files, strmake( "lib%s.cross.a", importlib ));
1810 output( "lib%s.cross.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1811 output_filenames( cross_files );
1812 output( "\n" );
1813 output( "\t%s -b %s -w --implib -o $@ --export %s",
1814 tools_path( "winebuild" ), crosstarget, spec_file );
1815 output_filenames( cross_files );
1816 output( "\n" );
1820 if (spec_file)
1822 if (c2man_files.count && top_obj_dir)
1824 char *manext = get_expanded_make_variable( "api_manext" );
1826 output( "manpages::\n" );
1827 output( "\t%s -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file, top_obj_dir );
1828 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1829 output_filename( strmake( "-o %s/documentation/man%s", top_obj_dir, manext ? manext : "3w" ));
1830 output_filenames( c2man_files );
1831 output( "\n" );
1832 output( "htmlpages::\n" );
1833 output( "\t%s -Th -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file, top_obj_dir );
1834 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1835 output_filename( strmake( "-o %s/documentation/html", top_obj_dir ));
1836 output_filenames( c2man_files );
1837 output( "\n" );
1838 output( "sgmlpages::\n" );
1839 output( "\t%s -Ts -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file, top_obj_dir );
1840 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1841 output_filename( strmake( "-o %s/documentation/api-guide", top_obj_dir ));
1842 output_filenames( c2man_files );
1843 output( "\n" );
1844 output( "xmlpages::\n" );
1845 output( "\t%s -Tx -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file, top_obj_dir );
1846 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1847 output_filename( strmake( "-o %s/documentation/api-guide-xml", top_obj_dir ));
1848 output_filenames( c2man_files );
1849 output( "\n" );
1850 strarray_add( &phony_targets, "manpages" );
1851 strarray_add( &phony_targets, "htmlpages" );
1852 strarray_add( &phony_targets, "sgmlpages" );
1853 strarray_add( &phony_targets, "xmlpages" );
1855 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1859 if (staticlib)
1861 strarray_add( &all_targets, staticlib );
1862 output( "%s:", staticlib );
1863 output_filenames( object_files );
1864 output( "\n\t$(RM) $@\n" );
1865 output( "\t$(AR) $(ARFLAGS) $@" );
1866 output_filenames( object_files );
1867 output( "\n\t$(RANLIB) $@\n" );
1868 if (crosstarget && module)
1870 char *name = replace_extension( staticlib, ".a", ".cross.a" );
1872 strarray_add( &all_targets, name );
1873 output( "%s:", name );
1874 output_filenames( crossobj_files );
1875 output( "\n\t$(RM) $@\n" );
1876 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
1877 output_filenames( crossobj_files );
1878 output( "\n\t%s-ranlib $@\n", crosstarget );
1882 if (testdll)
1884 struct strarray ok_files = strarray_replace_extension( &test_files, ".c", ".ok" );
1885 char *testmodule = replace_extension( testdll, ".dll", "_test.exe" );
1886 char *stripped = replace_extension( testdll, ".dll", "_test-stripped.exe" );
1887 struct strarray all_libs = empty_strarray;
1889 for (i = 0; i < imports.count; i++) strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1890 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1892 strarray_add( &all_targets, strmake( "%s%s", testmodule, dllext ));
1893 strarray_add( &clean_files, strmake( "%s%s", stripped, dllext ));
1894 output( "%s%s:\n", testmodule, dllext );
1895 output( "\t%s -o $@", tools_path( "winegcc" ));
1896 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1897 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1898 output_filenames( targetflags );
1899 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1900 output_filenames( appmode );
1901 output_filenames( object_files );
1902 output_filenames( res_files );
1903 output_filenames( all_libs );
1904 output_filename( "$(LDFLAGS)" );
1905 output( "\n" );
1906 output( "%s%s:\n", stripped, dllext );
1907 output( "\t%s -o $@", tools_path( "winegcc" ));
1908 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1909 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1910 output_filenames( targetflags );
1911 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1912 output_filename( strmake( "-Wb,-F,%s", testmodule ));
1913 output_filenames( appmode );
1914 output_filenames( object_files );
1915 output_filenames( res_files );
1916 output_filenames( all_libs );
1917 output_filename( "$(LDFLAGS)" );
1918 output( "\n" );
1919 output( "%s%s %s%s:", testmodule, dllext, stripped, dllext );
1920 output_filenames( object_files );
1921 output_filenames( res_files );
1922 output( "\n" );
1924 if (top_obj_dir)
1926 char *testres = replace_extension( testdll, ".dll", "_test.res" );
1927 output( "all: %s/%s\n", top_obj_dir_path( "programs/winetest" ), testres );
1928 output( "%s/%s: %s%s\n", top_obj_dir_path( "programs/winetest" ), testres, stripped, dllext );
1929 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
1930 testmodule, stripped, dllext, tools_path( "wrc" ));
1933 if (crosstarget)
1935 char *crosstest = replace_extension( testdll, ".dll", "_crosstest.exe" );
1937 strarray_add( &clean_files, crosstest );
1938 output( "crosstest: %s\n", crosstest );
1939 output( "%s:", crosstest );
1940 output_filenames( crossobj_files );
1941 output_filenames( res_files );
1942 output( "\n" );
1943 output( "\t%s -o $@ -b %s", tools_path( "winegcc" ), crosstarget );
1944 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1945 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1946 output_filename( "--lib-suffix=.cross.a" );
1947 output_filenames( crossobj_files );
1948 output_filenames( res_files );
1949 output_filenames( all_libs );
1950 output_filename( "$(LDFLAGS)" );
1951 output( "\n" );
1952 strarray_add( &phony_targets, "crosstest" );
1955 output( "testlist.c: %s%s %s\n",
1956 tools_dir_path( "make_ctests" ), tools_ext, src_dir_path( "Makefile.in" ));
1957 output( "\t%s%s -o $@", tools_dir_path( "make_ctests" ), tools_ext );
1958 output_filenames( test_files );
1959 output( "\n" );
1960 output_filenames( ok_files );
1961 output( ": %s%s ../%s%s\n", testmodule, dllext, testdll, dllext );
1962 output( "check test:" );
1963 output_filenames( ok_files );
1964 output( "\n" );
1965 output( "testclean::\n" );
1966 output( "\t$(RM)" );
1967 output_filenames( ok_files );
1968 output( "\n" );
1969 strarray_addall( &clean_files, ok_files );
1970 strarray_add( &phony_targets, "check" );
1971 strarray_add( &phony_targets, "test" );
1972 strarray_add( &phony_targets, "testclean" );
1975 if (all_targets.count)
1977 output( "all:" );
1978 output_filenames( all_targets );
1979 output( "\n" );
1982 strarray_addall( &clean_files, object_files );
1983 strarray_addall( &clean_files, crossobj_files );
1984 strarray_addall( &clean_files, res_files );
1985 strarray_addall( &clean_files, all_targets );
1986 strarray_addall( &clean_files, get_expanded_make_var_array( "EXTRA_TARGETS" ));
1988 if (clean_files.count)
1990 output( "clean::\n" );
1991 output( "\t$(RM)" );
1992 output_filenames( clean_files );
1993 output( "\n" );
1994 strarray_add( &phony_targets, "clean" );
1997 if (top_obj_dir)
1999 output( "depend:\n" );
2000 output( "\t@cd %s && $(MAKE) %s/depend\n", top_obj_dir, base_dir );
2001 strarray_add( &phony_targets, "depend" );
2004 if (phony_targets.count)
2006 output( ".PHONY:" );
2007 output_filenames( phony_targets );
2008 output( "\n" );
2011 return clean_files;
2015 /*******************************************************************
2016 * create_temp_file
2018 static FILE *create_temp_file( const char *orig )
2020 char *name = xmalloc( strlen(orig) + 13 );
2021 unsigned int i, id = getpid();
2022 int fd;
2023 FILE *ret = NULL;
2025 for (i = 0; i < 100; i++)
2027 sprintf( name, "%s.tmp%08x", orig, id );
2028 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2030 ret = fdopen( fd, "w" );
2031 break;
2033 if (errno != EEXIST) break;
2034 id += 7777;
2036 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2037 temp_file_name = name;
2038 return ret;
2042 /*******************************************************************
2043 * rename_temp_file
2045 static void rename_temp_file( const char *dest )
2047 int ret = rename( temp_file_name, dest );
2048 if (ret == -1 && errno == EEXIST)
2050 /* rename doesn't overwrite on windows */
2051 unlink( dest );
2052 ret = rename( temp_file_name, dest );
2054 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2055 temp_file_name = NULL;
2059 /*******************************************************************
2060 * output_gitignore
2062 static void output_gitignore( const char *dest, const struct strarray *files )
2064 int i;
2066 output_file = create_temp_file( dest );
2068 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2069 output( "/.gitignore\n" );
2070 output( "/Makefile\n" );
2071 for (i = 0; i < files->count; i++)
2073 if (!strchr( files->str[i], '/' )) output( "/" );
2074 output( "%s\n", files->str[i] );
2077 if (fclose( output_file )) fatal_perror( "write" );
2078 output_file = NULL;
2079 rename_temp_file( dest );
2083 /*******************************************************************
2084 * output_dependencies
2086 static void output_dependencies( const char *path )
2088 struct strarray targets = empty_strarray;
2090 if (Separator && ((output_file = fopen( path, "r" ))))
2092 char buffer[1024];
2093 FILE *tmp_file = create_temp_file( path );
2094 int found = 0;
2096 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2098 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2099 found = !strncmp( buffer, Separator, strlen(Separator) );
2101 if (fclose( output_file )) fatal_perror( "write" );
2102 output_file = tmp_file;
2103 if (!found) output( "\n%s\n", Separator );
2105 else
2107 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2108 fatal_perror( "%s", path );
2111 targets = output_sources();
2113 fclose( output_file );
2114 output_file = NULL;
2115 if (temp_file_name) rename_temp_file( path );
2117 if (!src_dir) output_gitignore( strmake( "%s/.gitignore", base_dir ), &targets );
2121 /*******************************************************************
2122 * update_makefile
2124 static void update_makefile( const char *path )
2126 static const char *source_vars[] =
2128 "C_SRCS",
2129 "OBJC_SRCS",
2130 "RC_SRCS",
2131 "MC_SRCS",
2132 "IDL_SRCS",
2133 "BISON_SRCS",
2134 "LEX_SRCS",
2135 "XTEMPLATE_SRCS",
2136 "SVG_SRCS",
2137 "FONT_SRCS",
2138 "IN_SRCS",
2139 "MANPAGES",
2140 NULL
2142 const char **var;
2143 unsigned int i;
2144 int use_msvcrt = 0;
2145 struct strarray value;
2146 struct incl_file *file;
2148 base_dir = path;
2149 output_file_name = strmake( "%s/%s", base_dir, makefile_name );
2150 parse_makefile();
2152 src_dir = get_expanded_make_variable( "srcdir" );
2153 top_src_dir = get_expanded_make_variable( "top_srcdir" );
2154 top_obj_dir = get_expanded_make_variable( "top_builddir" );
2155 parent_dir = get_expanded_make_variable( "PARENTSRC" );
2156 tools_dir = get_expanded_make_variable( "TOOLSDIR" );
2157 tools_ext = get_expanded_make_variable( "TOOLSEXT" );
2159 appmode = get_expanded_make_var_array( "APPMODE" );
2160 dllflags = get_expanded_make_var_array( "DLLFLAGS" );
2161 imports = get_expanded_make_var_array( "IMPORTS" );
2163 for (i = 0; i < appmode.count && !use_msvcrt; i++)
2164 use_msvcrt = !strcmp( appmode.str[i], "-mno-cygwin" );
2165 for (i = 0; i < imports.count && !use_msvcrt; i++)
2166 use_msvcrt = !strncmp( imports.str[i], "msvcr", 5 );
2168 include_args = empty_strarray;
2169 define_args = empty_strarray;
2170 strarray_add( &define_args, "-D__WINESRC__" );
2172 if (!tools_ext) tools_ext = "";
2174 value = get_expanded_make_var_array( "EXTRAINCL" );
2175 for (i = 0; i < value.count; i++)
2176 if (!strncmp( value.str[i], "-I", 2 ))
2177 strarray_add_uniq( &include_args, value.str[i] );
2178 else
2179 strarray_add_uniq( &define_args, value.str[i] );
2180 strarray_addall( &define_args, get_expanded_make_var_array( "EXTRADEFS" ));
2182 init_paths();
2184 if (use_msvcrt)
2186 strarray_add( &dllflags, get_expanded_make_variable( "MSVCRTFLAGS" ));
2187 strarray_add( &include_args, strmake( "-I%s", top_dir_path( "include/msvcrt" )));
2190 list_init( &sources );
2191 list_init( &includes );
2193 for (var = source_vars; *var; var++)
2195 value = get_expanded_make_var_array( *var );
2196 for (i = 0; i < value.count; i++) add_src_file( value.str[i] );
2199 add_generated_sources();
2201 value = get_expanded_make_var_array( "EXTRA_OBJS" );
2202 for (i = 0; i < value.count; i++)
2204 /* default to .c for unknown extra object files */
2205 if (strendswith( value.str[i], ".o" ))
2206 add_generated_source( value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2207 else
2208 add_generated_source( value.str[i], NULL );
2211 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( file, 0 );
2212 output_dependencies( output_file_name );
2213 output_file_name = NULL;
2217 /*******************************************************************
2218 * parse_makeflags
2220 static void parse_makeflags( const char *flags )
2222 const char *p = flags;
2223 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2225 while (*p)
2227 while (isspace(*p)) p++;
2228 var = buffer;
2229 while (*p && !isspace(*p))
2231 if (*p == '\\' && p[1]) p++;
2232 *var++ = *p++;
2234 *var = 0;
2235 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2240 /*******************************************************************
2241 * parse_option
2243 static int parse_option( const char *opt )
2245 if (opt[0] != '-')
2247 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2248 return 0;
2250 switch(opt[1])
2252 case 'I':
2253 if (opt[2]) strarray_add_uniq( &include_args, opt );
2254 break;
2255 case 'C':
2256 src_dir = opt + 2;
2257 break;
2258 case 'S':
2259 top_src_dir = opt + 2;
2260 break;
2261 case 'T':
2262 top_obj_dir = opt + 2;
2263 break;
2264 case 'P':
2265 parent_dir = opt + 2;
2266 break;
2267 case 'f':
2268 if (opt[2]) makefile_name = opt + 2;
2269 break;
2270 case 'M':
2271 parse_makefile_mode = 1;
2272 break;
2273 case 'R':
2274 relative_dir_mode = 1;
2275 break;
2276 case 's':
2277 if (opt[2]) Separator = opt + 2;
2278 else Separator = NULL;
2279 break;
2280 case 'x':
2281 break; /* ignored */
2282 default:
2283 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2284 exit(1);
2286 return 1;
2290 /*******************************************************************
2291 * main
2293 int main( int argc, char *argv[] )
2295 const char *makeflags = getenv( "MAKEFLAGS" );
2296 struct incl_file *pFile;
2297 int i, j;
2299 if (makeflags) parse_makeflags( makeflags );
2301 i = 1;
2302 while (i < argc)
2304 if (parse_option( argv[i] ))
2306 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2307 argc--;
2309 else i++;
2312 if (relative_dir_mode)
2314 char *relpath;
2316 if (argc != 3)
2318 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2319 exit( 1 );
2321 relpath = get_relative_path( argv[1], argv[2] );
2322 printf( "%s\n", relpath ? relpath : "." );
2323 exit( 0 );
2326 atexit( cleanup_files );
2327 signal( SIGTERM, exit_on_signal );
2328 signal( SIGINT, exit_on_signal );
2329 #ifdef SIGHUP
2330 signal( SIGHUP, exit_on_signal );
2331 #endif
2333 if (parse_makefile_mode)
2335 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2336 exit( 0 );
2339 init_paths();
2340 for (i = 1; i < argc; i++) add_src_file( argv[i] );
2341 add_generated_sources();
2343 LIST_FOR_EACH_ENTRY( pFile, &includes, struct incl_file, entry ) parse_file( pFile, 0 );
2344 output_dependencies( makefile_name );
2345 return 0;