makefiles: Avoid running config.status when not necessary.
[wine.git] / tools / makedep.c
blobc81a57e5a447d280b4c3861091bbcdb04246ce16
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 enum incl_type
40 INCL_NORMAL, /* #include "foo.h" */
41 INCL_SYSTEM, /* #include <foo.h> */
42 INCL_IMPORT, /* idl import "foo.idl" */
43 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
44 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
47 struct dependency
49 int line; /* source line where this header is included */
50 enum incl_type type; /* type of include */
51 char *name; /* header name */
54 struct file
56 struct list entry;
57 char *name; /* full file name relative to cwd */
58 void *args; /* custom arguments for makefile rule */
59 unsigned int flags; /* flags (see below) */
60 unsigned int deps_count; /* files in use */
61 unsigned int deps_size; /* total allocated size */
62 struct dependency *deps; /* all header dependencies */
65 struct incl_file
67 struct list entry;
68 struct file *file;
69 char *name;
70 char *filename;
71 char *sourcename; /* source file name for generated headers */
72 struct incl_file *included_by; /* file that included this one */
73 int included_line; /* line where this file was included */
74 int system; /* is it a system include (#include <name>) */
75 struct incl_file *owner;
76 unsigned int files_count; /* files in use */
77 unsigned int files_size; /* total allocated size */
78 struct incl_file **files;
81 #define FLAG_GENERATED 0x000001 /* generated file */
82 #define FLAG_INSTALL 0x000002 /* file to install */
83 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
84 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
85 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
86 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
87 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
88 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
89 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
90 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
91 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
92 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
93 #define FLAG_SFD_FONTS 0x040000 /* sfd file generated bitmap fonts */
95 static const struct
97 unsigned int flag;
98 const char *ext;
99 } idl_outputs[] =
101 { FLAG_IDL_TYPELIB, ".tlb" },
102 { FLAG_IDL_REGTYPELIB, "_t.res" },
103 { FLAG_IDL_CLIENT, "_c.c" },
104 { FLAG_IDL_IDENT, "_i.c" },
105 { FLAG_IDL_PROXY, "_p.c" },
106 { FLAG_IDL_SERVER, "_s.c" },
107 { FLAG_IDL_REGISTER, "_r.res" },
108 { FLAG_IDL_HEADER, ".h" }
111 #define HASH_SIZE 137
113 static struct list files[HASH_SIZE];
115 struct strarray
117 unsigned int count; /* strings in use */
118 unsigned int size; /* total allocated size */
119 const char **str;
122 static const struct strarray empty_strarray;
124 /* variables common to all makefiles */
125 static struct strarray linguas;
126 static struct strarray dll_flags;
127 static struct strarray target_flags;
128 static struct strarray msvcrt_flags;
129 static struct strarray extra_cflags;
130 static struct strarray cpp_flags;
131 static struct strarray unwind_flags;
132 static struct strarray libs;
133 static struct strarray cmdline_vars;
134 static const char *root_src_dir;
135 static const char *tools_dir;
136 static const char *tools_ext;
137 static const char *exe_ext;
138 static const char *dll_ext;
139 static const char *man_ext;
140 static const char *dll_prefix;
141 static const char *crosstarget;
142 static const char *fontforge;
143 static const char *convert;
144 static const char *rsvg;
145 static const char *icotool;
147 struct makefile
149 struct strarray vars;
150 struct strarray include_args;
151 struct strarray define_args;
152 struct strarray appmode;
153 struct strarray imports;
154 struct strarray delayimports;
155 struct strarray extradllflags;
156 struct list sources;
157 struct list includes;
158 const char *base_dir;
159 const char *src_dir;
160 const char *obj_dir;
161 const char *top_src_dir;
162 const char *top_obj_dir;
163 const char *parent_dir;
164 const char *module;
165 const char *testdll;
166 const char *staticlib;
167 const char *importlib;
168 int use_msvcrt;
169 int is_win16;
172 static struct makefile *top_makefile;
174 static const char *output_makefile_name = "Makefile";
175 static const char *input_makefile_name;
176 static const char *input_file_name;
177 static const char *output_file_name;
178 static const char *temp_file_name;
179 static int relative_dir_mode;
180 static int input_line;
181 static int output_column;
182 static FILE *output_file;
184 static const char Usage[] =
185 "Usage: makedep [options] directories\n"
186 "Options:\n"
187 " -R from to Compute the relative path between two directories\n"
188 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
189 " -ixxx Read input from file 'xxx' (default: Makefile.in)\n";
192 #ifndef __GNUC__
193 #define __attribute__(x)
194 #endif
196 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
197 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
198 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
199 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
201 /*******************************************************************
202 * fatal_error
204 static void fatal_error( const char *msg, ... )
206 va_list valist;
207 va_start( valist, msg );
208 if (input_file_name)
210 fprintf( stderr, "%s:", input_file_name );
211 if (input_line) fprintf( stderr, "%d:", input_line );
212 fprintf( stderr, " error: " );
214 else fprintf( stderr, "makedep: error: " );
215 vfprintf( stderr, msg, valist );
216 va_end( valist );
217 exit(1);
221 /*******************************************************************
222 * fatal_perror
224 static void fatal_perror( const char *msg, ... )
226 va_list valist;
227 va_start( valist, msg );
228 if (input_file_name)
230 fprintf( stderr, "%s:", input_file_name );
231 if (input_line) fprintf( stderr, "%d:", input_line );
232 fprintf( stderr, " error: " );
234 else fprintf( stderr, "makedep: error: " );
235 vfprintf( stderr, msg, valist );
236 perror( " " );
237 va_end( valist );
238 exit(1);
242 /*******************************************************************
243 * cleanup_files
245 static void cleanup_files(void)
247 if (temp_file_name) unlink( temp_file_name );
248 if (output_file_name) unlink( output_file_name );
252 /*******************************************************************
253 * exit_on_signal
255 static void exit_on_signal( int sig )
257 exit( 1 ); /* this will call the atexit functions */
261 /*******************************************************************
262 * xmalloc
264 static void *xmalloc( size_t size )
266 void *res;
267 if (!(res = malloc (size ? size : 1)))
268 fatal_error( "Virtual memory exhausted.\n" );
269 return res;
273 /*******************************************************************
274 * xrealloc
276 static void *xrealloc (void *ptr, size_t size)
278 void *res;
279 assert( size );
280 if (!(res = realloc( ptr, size )))
281 fatal_error( "Virtual memory exhausted.\n" );
282 return res;
285 /*******************************************************************
286 * xstrdup
288 static char *xstrdup( const char *str )
290 char *res = strdup( str );
291 if (!res) fatal_error( "Virtual memory exhausted.\n" );
292 return res;
296 /*******************************************************************
297 * strmake
299 static char *strmake( const char* fmt, ... )
301 int n;
302 size_t size = 100;
303 va_list ap;
305 for (;;)
307 char *p = xmalloc (size);
308 va_start(ap, fmt);
309 n = vsnprintf (p, size, fmt, ap);
310 va_end(ap);
311 if (n == -1) size *= 2;
312 else if ((size_t)n >= size) size = n + 1;
313 else return p;
314 free(p);
319 /*******************************************************************
320 * strendswith
322 static int strendswith( const char* str, const char* end )
324 int l = strlen(str);
325 int m = strlen(end);
327 return l >= m && strcmp(str + l - m, end) == 0;
331 /*******************************************************************
332 * output
334 static void output( const char *format, ... )
336 int ret;
337 va_list valist;
339 va_start( valist, format );
340 ret = vfprintf( output_file, format, valist );
341 va_end( valist );
342 if (ret < 0) fatal_perror( "output" );
343 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
344 else output_column += ret;
348 /*******************************************************************
349 * strarray_add
351 static void strarray_add( struct strarray *array, const char *str )
353 if (array->count == array->size)
355 if (array->size) array->size *= 2;
356 else array->size = 16;
357 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
359 array->str[array->count++] = str;
363 /*******************************************************************
364 * strarray_addall
366 static void strarray_addall( struct strarray *array, struct strarray added )
368 unsigned int i;
370 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
374 /*******************************************************************
375 * strarray_exists
377 static int strarray_exists( struct strarray *array, const char *str )
379 unsigned int i;
381 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
382 return 0;
386 /*******************************************************************
387 * strarray_add_uniq
389 static void strarray_add_uniq( struct strarray *array, const char *str )
391 if (!strarray_exists( array, str )) strarray_add( array, str );
395 /*******************************************************************
396 * strarray_get_value
398 * Find a value in a name/value pair string array.
400 static char *strarray_get_value( const struct strarray *array, const char *name )
402 unsigned int i;
404 for (i = 0; i < array->count; i += 2)
405 if (!strcmp( array->str[i], name )) return xstrdup( array->str[i + 1] );
406 return NULL;
410 /*******************************************************************
411 * strarray_set_value
413 * Define a value in a name/value pair string array.
415 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
417 unsigned int i;
419 /* redefining a variable replaces the previous value */
420 for (i = 0; i < array->count; i += 2)
422 if (strcmp( array->str[i], name )) continue;
423 array->str[i + 1] = value;
424 return;
426 strarray_add( array, name );
427 strarray_add( array, value );
431 /*******************************************************************
432 * output_filename
434 static void output_filename( const char *name )
436 if (output_column + strlen(name) + 1 > 100)
438 output( " \\\n" );
439 output( " " );
441 else if (output_column) output( " " );
442 output( "%s", name );
446 /*******************************************************************
447 * output_filenames
449 static void output_filenames( struct strarray array )
451 unsigned int i;
453 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
457 /*******************************************************************
458 * get_extension
460 static char *get_extension( char *filename )
462 char *ext = strrchr( filename, '.' );
463 if (ext && strchr( ext, '/' )) ext = NULL;
464 return ext;
468 /*******************************************************************
469 * replace_extension
471 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
473 char *ret;
474 int name_len = strlen( name );
475 int ext_len = strlen( old_ext );
477 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
478 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
479 memcpy( ret, name, name_len );
480 strcpy( ret + name_len, new_ext );
481 return ret;
485 /*******************************************************************
486 * strarray_replace_extension
488 static struct strarray strarray_replace_extension( const struct strarray *array,
489 const char *old_ext, const char *new_ext )
491 unsigned int i;
492 struct strarray ret;
494 ret.count = ret.size = array->count;
495 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
496 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
497 return ret;
501 /*******************************************************************
502 * replace_substr
504 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
506 unsigned int pos = start - str;
507 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
508 memcpy( ret, str, pos );
509 strcpy( ret + pos, replace );
510 strcat( ret + pos, start + len );
511 return ret;
515 /*******************************************************************
516 * get_relative_path
518 * Determine where the destination path is located relative to the 'from' path.
520 static char *get_relative_path( const char *from, const char *dest )
522 const char *start;
523 char *ret, *p;
524 unsigned int dotdots = 0;
526 /* a path of "." is equivalent to an empty path */
527 if (!strcmp( from, "." )) from = "";
529 for (;;)
531 while (*from == '/') from++;
532 while (*dest == '/') dest++;
533 start = dest; /* save start of next path element */
534 if (!*from) break;
536 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
537 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
539 /* count remaining elements in 'from' */
542 dotdots++;
543 while (*from && *from != '/') from++;
544 while (*from == '/') from++;
546 while (*from);
547 break;
550 if (!start[0] && !dotdots) return NULL; /* empty path */
552 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
553 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
555 if (start[0]) strcpy( p, start );
556 else p[-1] = 0; /* remove trailing slash */
557 return ret;
561 /*******************************************************************
562 * concat_paths
564 static char *concat_paths( const char *base, const char *path )
566 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
567 if (!path || !path[0]) return xstrdup( base );
568 if (path[0] == '/') return xstrdup( path );
569 return strmake( "%s/%s", base, path );
573 /*******************************************************************
574 * base_dir_path
576 static char *base_dir_path( struct makefile *make, const char *path )
578 return concat_paths( make->base_dir, path );
582 /*******************************************************************
583 * obj_dir_path
585 static char *obj_dir_path( struct makefile *make, const char *path )
587 return concat_paths( make->obj_dir, path );
591 /*******************************************************************
592 * src_dir_path
594 static char *src_dir_path( struct makefile *make, const char *path )
596 if (make->src_dir) return concat_paths( make->src_dir, path );
597 return obj_dir_path( make, path );
601 /*******************************************************************
602 * top_obj_dir_path
604 static char *top_obj_dir_path( struct makefile *make, const char *path )
606 return concat_paths( make->top_obj_dir, path );
610 /*******************************************************************
611 * top_dir_path
613 static char *top_dir_path( struct makefile *make, const char *path )
615 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
616 return top_obj_dir_path( make, path );
620 /*******************************************************************
621 * root_dir_path
623 static char *root_dir_path( const char *path )
625 return concat_paths( root_src_dir, path );
629 /*******************************************************************
630 * tools_dir_path
632 static char *tools_dir_path( struct makefile *make, const char *path )
634 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
635 return top_obj_dir_path( make, strmake( "tools/%s", path ));
639 /*******************************************************************
640 * tools_path
642 static char *tools_path( struct makefile *make, const char *name )
644 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
648 /*******************************************************************
649 * get_line
651 static char *get_line( FILE *file )
653 static char *buffer;
654 static unsigned int size;
656 if (!size)
658 size = 1024;
659 buffer = xmalloc( size );
661 if (!fgets( buffer, size, file )) return NULL;
662 input_line++;
664 for (;;)
666 char *p = buffer + strlen(buffer);
667 /* if line is larger than buffer, resize buffer */
668 while (p == buffer + size - 1 && p[-1] != '\n')
670 buffer = xrealloc( buffer, size * 2 );
671 if (!fgets( buffer + size - 1, size + 1, file )) break;
672 p = buffer + strlen(buffer);
673 size *= 2;
675 if (p > buffer && p[-1] == '\n')
677 *(--p) = 0;
678 if (p > buffer && p[-1] == '\r') *(--p) = 0;
679 if (p > buffer && p[-1] == '\\')
681 *(--p) = 0;
682 /* line ends in backslash, read continuation line */
683 if (!fgets( p, size - (p - buffer), file )) return buffer;
684 input_line++;
685 continue;
688 return buffer;
693 /*******************************************************************
694 * hash_filename
696 static unsigned int hash_filename( const char *name )
698 unsigned int ret = 0;
699 while (*name) ret = (ret << 7) + (ret << 3) + *name++;
700 return ret % HASH_SIZE;
704 /*******************************************************************
705 * add_file
707 static struct file *add_file( const char *name )
709 struct file *file = xmalloc( sizeof(*file) );
710 memset( file, 0, sizeof(*file) );
711 file->name = xstrdup( name );
712 list_add_tail( &files[hash_filename( name )], &file->entry );
713 return file;
717 /*******************************************************************
718 * add_dependency
720 static void add_dependency( struct file *file, const char *name, enum incl_type type )
722 /* enforce some rules for the Wine tree */
724 if (!memcmp( name, "../", 3 ))
725 fatal_error( "#include directive with relative path not allowed\n" );
727 if (!strcmp( name, "config.h" ))
729 if (strendswith( file->name, ".h" ))
730 fatal_error( "config.h must not be included by a header file\n" );
731 if (file->deps_count)
732 fatal_error( "config.h must be included before anything else\n" );
734 else if (!strcmp( name, "wine/port.h" ))
736 if (strendswith( file->name, ".h" ))
737 fatal_error( "wine/port.h must not be included by a header file\n" );
738 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
739 if (file->deps_count > 1)
740 fatal_error( "wine/port.h must be included before everything except config.h\n" );
741 if (strcmp( file->deps[0].name, "config.h" ))
742 fatal_error( "config.h must be included before wine/port.h\n" );
745 if (file->deps_count >= file->deps_size)
747 file->deps_size *= 2;
748 if (file->deps_size < 16) file->deps_size = 16;
749 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
751 file->deps[file->deps_count].line = input_line;
752 file->deps[file->deps_count].type = type;
753 file->deps[file->deps_count].name = xstrdup( name );
754 file->deps_count++;
758 /*******************************************************************
759 * find_src_file
761 static struct incl_file *find_src_file( struct makefile *make, const char *name )
763 struct incl_file *file;
765 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
766 if (!strcmp( name, file->name )) return file;
767 return NULL;
770 /*******************************************************************
771 * find_include_file
773 static struct incl_file *find_include_file( struct makefile *make, const char *name )
775 struct incl_file *file;
777 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
778 if (!strcmp( name, file->name )) return file;
779 return NULL;
782 /*******************************************************************
783 * add_include
785 * Add an include file if it doesn't already exists.
787 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
788 const char *name, int line, int system )
790 struct incl_file *include;
792 if (parent->files_count >= parent->files_size)
794 parent->files_size *= 2;
795 if (parent->files_size < 16) parent->files_size = 16;
796 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
799 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
800 if (!strcmp( name, include->name )) goto found;
802 include = xmalloc( sizeof(*include) );
803 memset( include, 0, sizeof(*include) );
804 include->name = xstrdup(name);
805 include->included_by = parent;
806 include->included_line = line;
807 include->system = system;
808 list_add_tail( &make->includes, &include->entry );
809 found:
810 parent->files[parent->files_count++] = include;
811 return include;
815 /*******************************************************************
816 * add_generated_source
818 * Add a generated source file to the list.
820 static struct incl_file *add_generated_source( struct makefile *make,
821 const char *name, const char *filename )
823 struct incl_file *file;
825 if ((file = find_src_file( make, name ))) return file; /* we already have it */
826 file = xmalloc( sizeof(*file) );
827 memset( file, 0, sizeof(*file) );
828 file->file = add_file( name );
829 file->name = xstrdup( name );
830 file->filename = obj_dir_path( make, filename ? filename : name );
831 file->file->flags = FLAG_GENERATED;
832 list_add_tail( &make->sources, &file->entry );
833 return file;
837 /*******************************************************************
838 * parse_include_directive
840 static void parse_include_directive( struct file *source, char *str )
842 char quote, *include, *p = str;
844 while (*p && isspace(*p)) p++;
845 if (*p != '\"' && *p != '<' ) return;
846 quote = *p++;
847 if (quote == '<') quote = '>';
848 include = p;
849 while (*p && (*p != quote)) p++;
850 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
851 *p = 0;
852 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
856 /*******************************************************************
857 * parse_pragma_directive
859 static void parse_pragma_directive( struct file *source, char *str )
861 char *flag, *p = str;
863 if (!isspace( *p )) return;
864 while (*p && isspace(*p)) p++;
865 p = strtok( p, " \t" );
866 if (strcmp( p, "makedep" )) return;
868 while ((flag = strtok( NULL, " \t" )))
870 if (!strcmp( flag, "depend" ))
872 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
873 return;
875 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
877 if (strendswith( source->name, ".idl" ))
879 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
880 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
881 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
882 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
883 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
884 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
885 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
886 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
888 else if (strendswith( source->name, ".rc" ))
890 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
892 else if (strendswith( source->name, ".sfd" ))
894 if (!strcmp( flag, "font" ))
896 struct strarray *array = source->args;
898 if (!array)
900 source->args = array = xmalloc( sizeof(*array) );
901 *array = empty_strarray;
902 source->flags |= FLAG_SFD_FONTS;
904 strarray_add( array, xstrdup( strtok( NULL, "" )));
905 return;
908 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
913 /*******************************************************************
914 * parse_cpp_directive
916 static void parse_cpp_directive( struct file *source, char *str )
918 while (*str && isspace(*str)) str++;
919 if (*str++ != '#') return;
920 while (*str && isspace(*str)) str++;
922 if (!strncmp( str, "include", 7 ))
923 parse_include_directive( source, str + 7 );
924 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
925 parse_include_directive( source, str + 6 );
926 else if (!strncmp( str, "pragma", 6 ))
927 parse_pragma_directive( source, str + 6 );
931 /*******************************************************************
932 * parse_idl_file
934 static void parse_idl_file( struct file *source, FILE *file )
936 char *buffer, *include;
938 input_line = 0;
940 while ((buffer = get_line( file )))
942 char quote;
943 char *p = buffer;
944 while (*p && isspace(*p)) p++;
946 if (!strncmp( p, "import", 6 ))
948 p += 6;
949 while (*p && isspace(*p)) p++;
950 if (*p != '"') continue;
951 include = ++p;
952 while (*p && (*p != '"')) p++;
953 if (!*p) fatal_error( "malformed import directive\n" );
954 *p = 0;
955 add_dependency( source, include, INCL_IMPORT );
956 continue;
959 /* check for #include inside cpp_quote */
960 if (!strncmp( p, "cpp_quote", 9 ))
962 p += 9;
963 while (*p && isspace(*p)) p++;
964 if (*p++ != '(') continue;
965 while (*p && isspace(*p)) p++;
966 if (*p++ != '"') continue;
967 if (*p++ != '#') continue;
968 while (*p && isspace(*p)) p++;
969 if (strncmp( p, "include", 7 )) continue;
970 p += 7;
971 while (*p && isspace(*p)) p++;
972 if (*p == '\\' && p[1] == '"')
974 p += 2;
975 quote = '"';
977 else
979 if (*p++ != '<' ) continue;
980 quote = '>';
982 include = p;
983 while (*p && (*p != quote)) p++;
984 if (!*p || (quote == '"' && p[-1] != '\\'))
985 fatal_error( "malformed #include directive inside cpp_quote\n" );
986 if (quote == '"') p--; /* remove backslash */
987 *p = 0;
988 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
989 continue;
992 parse_cpp_directive( source, p );
996 /*******************************************************************
997 * parse_c_file
999 static void parse_c_file( struct file *source, FILE *file )
1001 char *buffer;
1003 input_line = 0;
1004 while ((buffer = get_line( file )))
1006 parse_cpp_directive( source, buffer );
1011 /*******************************************************************
1012 * parse_rc_file
1014 static void parse_rc_file( struct file *source, FILE *file )
1016 char *buffer, *include;
1018 input_line = 0;
1019 while ((buffer = get_line( file )))
1021 char quote;
1022 char *p = buffer;
1023 while (*p && isspace(*p)) p++;
1025 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1027 p += 2;
1028 while (*p && isspace(*p)) p++;
1029 if (strncmp( p, "@makedep:", 9 )) continue;
1030 p += 9;
1031 while (*p && isspace(*p)) p++;
1032 quote = '"';
1033 if (*p == quote)
1035 include = ++p;
1036 while (*p && *p != quote) p++;
1038 else
1040 include = p;
1041 while (*p && !isspace(*p) && *p != '*') p++;
1043 if (!*p)
1044 fatal_error( "malformed makedep comment\n" );
1045 *p = 0;
1046 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1047 continue;
1050 parse_cpp_directive( source, buffer );
1055 /*******************************************************************
1056 * parse_in_file
1058 static void parse_in_file( struct file *source, FILE *file )
1060 char *p, *buffer;
1062 /* make sure it gets rebuilt when the version changes */
1063 add_dependency( source, "config.h", INCL_SYSTEM );
1065 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1067 input_line = 0;
1068 while ((buffer = get_line( file )))
1070 if (strncmp( buffer, ".TH", 3 )) continue;
1071 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1072 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1073 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1074 source->args = xstrdup( p );
1075 return;
1080 /*******************************************************************
1081 * parse_sfd_file
1083 static void parse_sfd_file( struct file *source, FILE *file )
1085 char *p, *eol, *buffer;
1087 input_line = 0;
1088 while ((buffer = get_line( file )))
1090 if (strncmp( buffer, "UComments:", 10 )) continue;
1091 p = buffer + 10;
1092 while (*p == ' ') p++;
1093 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1095 p++;
1096 buffer[strlen(buffer) - 1] = 0;
1098 while ((eol = strstr( p, "+AAoA" )))
1100 *eol = 0;
1101 while (*p && isspace(*p)) p++;
1102 if (*p++ == '#')
1104 while (*p && isspace(*p)) p++;
1105 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1107 p = eol + 5;
1109 while (*p && isspace(*p)) p++;
1110 if (*p++ != '#') return;
1111 while (*p && isspace(*p)) p++;
1112 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1113 return;
1118 /*******************************************************************
1119 * load_file
1121 static struct file *load_file( const char *name )
1123 struct file *file;
1124 FILE *f;
1125 unsigned int hash = hash_filename( name );
1127 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1128 if (!strcmp( name, file->name )) return file;
1130 if (!(f = fopen( name, "r" ))) return NULL;
1132 file = add_file( name );
1133 input_file_name = file->name;
1134 input_line = 0;
1136 if (strendswith( name, ".idl" )) parse_idl_file( file, f );
1137 else if (strendswith( name, ".rc" )) parse_rc_file( file, f );
1138 else if (strendswith( name, ".in" )) parse_in_file( file, f );
1139 else if (strendswith( name, ".sfd" )) parse_sfd_file( file, f );
1140 else if (strendswith( name, ".c" ) ||
1141 strendswith( name, ".m" ) ||
1142 strendswith( name, ".h" ) ||
1143 strendswith( name, ".l" ) ||
1144 strendswith( name, ".y" )) parse_c_file( file, f );
1146 fclose( f );
1147 input_file_name = NULL;
1149 return file;
1153 /*******************************************************************
1154 * open_file
1156 static struct file *open_file( struct makefile *make, const char *path, char **filename )
1158 struct file *ret = load_file( base_dir_path( make, path ));
1160 if (ret) *filename = xstrdup( path );
1161 return ret;
1165 /*******************************************************************
1166 * open_local_file
1168 * Open a file in the source directory of the makefile.
1170 static struct file *open_local_file( struct makefile *make, const char *path, char **filename )
1172 char *src_path = root_dir_path( base_dir_path( make, path ));
1173 struct file *ret = load_file( src_path );
1175 /* if not found, try parent dir */
1176 if (!ret && make->parent_dir)
1178 free( src_path );
1179 path = strmake( "%s/%s", make->parent_dir, path );
1180 src_path = root_dir_path( base_dir_path( make, path ));
1181 ret = load_file( src_path );
1184 if (ret) *filename = src_dir_path( make, path );
1185 free( src_path );
1186 return ret;
1190 /*******************************************************************
1191 * open_global_file
1193 * Open a file in the top-level source directory.
1195 static struct file *open_global_file( struct makefile *make, const char *path, char **filename )
1197 char *src_path = root_dir_path( path );
1198 struct file *ret = load_file( src_path );
1200 if (ret) *filename = top_dir_path( make, path );
1201 free( src_path );
1202 return ret;
1206 /*******************************************************************
1207 * open_global_header
1209 * Open a file in the global include source directory.
1211 static struct file *open_global_header( struct makefile *make, const char *path, char **filename )
1213 return open_global_file( make, strmake( "include/%s", path ), filename );
1217 /*******************************************************************
1218 * open_src_file
1220 static struct file *open_src_file( struct makefile *make, struct incl_file *pFile )
1222 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1224 if (!file) fatal_perror( "open %s", pFile->name );
1225 return file;
1229 /*******************************************************************
1230 * open_include_file
1232 static struct file *open_include_file( struct makefile *make, struct incl_file *pFile )
1234 struct file *file = NULL;
1235 char *filename, *p;
1236 unsigned int i, len;
1238 errno = ENOENT;
1240 /* check for generated bison header */
1242 if (strendswith( pFile->name, ".tab.h" ) &&
1243 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1245 pFile->sourcename = filename;
1246 pFile->filename = obj_dir_path( make, pFile->name );
1247 return file;
1250 /* check for corresponding idl file in source dir */
1252 if (strendswith( pFile->name, ".h" ) &&
1253 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1255 pFile->sourcename = filename;
1256 pFile->filename = obj_dir_path( make, pFile->name );
1257 return file;
1260 /* now try in source dir */
1261 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1263 /* check for corresponding idl file in global includes */
1265 if (strendswith( pFile->name, ".h" ) &&
1266 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1268 pFile->sourcename = filename;
1269 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1270 return file;
1273 /* check for corresponding .in file in global includes (for config.h.in) */
1275 if (strendswith( pFile->name, ".h" ) &&
1276 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1278 pFile->sourcename = filename;
1279 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1280 return file;
1283 /* check for corresponding .x file in global includes */
1285 if (strendswith( pFile->name, "tmpl.h" ) &&
1286 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1288 pFile->sourcename = filename;
1289 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1290 return file;
1293 /* check in global includes source dir */
1295 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1297 /* check in global msvcrt includes */
1298 if (make->use_msvcrt &&
1299 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1300 return file;
1302 /* now search in include paths */
1303 for (i = 0; i < make->include_args.count; i++)
1305 const char *dir = make->include_args.str[i] + 2; /* skip -I */
1306 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1308 if (prefix)
1310 len = strlen( prefix );
1311 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1313 while (dir[len] == '/') len++;
1314 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1315 if (file) return file;
1317 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1319 if (*dir != '/')
1321 if ((file = open_file( make, concat_paths( dir, pFile->name ), &pFile->filename )))
1322 return file;
1325 if (pFile->system) return NULL; /* ignore system files we cannot find */
1327 /* try in src file directory */
1328 if ((p = strrchr(pFile->included_by->filename, '/')))
1330 int l = p - pFile->included_by->filename + 1;
1331 filename = xmalloc(l + strlen(pFile->name) + 1);
1332 memcpy( filename, pFile->included_by->filename, l );
1333 strcpy( filename + l, pFile->name );
1334 if ((file = open_file( make, filename, &pFile->filename ))) return file;
1335 free( filename );
1338 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1339 perror( pFile->name );
1340 pFile = pFile->included_by;
1341 while (pFile && pFile->included_by)
1343 const char *parent = pFile->included_by->sourcename;
1344 if (!parent) parent = pFile->included_by->file->name;
1345 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1346 parent, pFile->included_line, pFile->name );
1347 pFile = pFile->included_by;
1349 exit(1);
1353 /*******************************************************************
1354 * add_all_includes
1356 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1358 unsigned int i;
1360 parent->files_count = 0;
1361 parent->files_size = file->deps_count;
1362 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1363 for (i = 0; i < file->deps_count; i++)
1365 switch (file->deps[i].type)
1367 case INCL_NORMAL:
1368 case INCL_IMPORT:
1369 add_include( make, parent, file->deps[i].name, file->deps[i].line, 0 );
1370 break;
1371 case INCL_SYSTEM:
1372 add_include( make, parent, file->deps[i].name, file->deps[i].line, 1 );
1373 break;
1374 case INCL_CPP_QUOTE:
1375 case INCL_CPP_QUOTE_SYSTEM:
1376 break;
1382 /*******************************************************************
1383 * parse_file
1385 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1387 struct file *file;
1389 /* don't try to open certain types of files */
1390 if (strendswith( source->name, ".tlb" ))
1392 source->filename = obj_dir_path( make, source->name );
1393 return;
1396 file = src ? open_src_file( make, source ) : open_include_file( make, source );
1397 if (!file) return;
1399 source->file = file;
1400 source->files_count = 0;
1401 source->files_size = file->deps_count;
1402 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1404 if (source->sourcename)
1406 if (strendswith( source->sourcename, ".idl" ))
1408 unsigned int i;
1410 /* generated .h file always includes these */
1411 add_include( make, source, "rpc.h", 0, 1 );
1412 add_include( make, source, "rpcndr.h", 0, 1 );
1413 for (i = 0; i < file->deps_count; i++)
1415 switch (file->deps[i].type)
1417 case INCL_IMPORT:
1418 if (strendswith( file->deps[i].name, ".idl" ))
1419 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1420 file->deps[i].line, 0 );
1421 else
1422 add_include( make, source, file->deps[i].name, file->deps[i].line, 0 );
1423 break;
1424 case INCL_CPP_QUOTE:
1425 add_include( make, source, file->deps[i].name, file->deps[i].line, 0 );
1426 break;
1427 case INCL_CPP_QUOTE_SYSTEM:
1428 add_include( make, source, file->deps[i].name, file->deps[i].line, 1 );
1429 break;
1430 case INCL_NORMAL:
1431 case INCL_SYSTEM:
1432 break;
1435 return;
1437 if (strendswith( source->sourcename, ".y" ))
1438 return; /* generated .tab.h doesn't include anything */
1441 add_all_includes( make, source, file );
1445 /*******************************************************************
1446 * add_src_file
1448 * Add a source file to the list.
1450 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1452 struct incl_file *file;
1454 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1455 file = xmalloc( sizeof(*file) );
1456 memset( file, 0, sizeof(*file) );
1457 file->name = xstrdup(name);
1458 list_add_tail( &make->sources, &file->entry );
1459 parse_file( make, file, 1 );
1460 return file;
1464 /*******************************************************************
1465 * open_input_makefile
1467 static FILE *open_input_makefile( struct makefile *make )
1469 FILE *ret;
1471 if (make->base_dir)
1472 input_file_name = base_dir_path( make, input_makefile_name );
1473 else
1474 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1476 input_line = 0;
1477 if (!(ret = fopen( input_file_name, "r" )))
1479 input_file_name = root_dir_path( input_file_name );
1480 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1482 return ret;
1486 /*******************************************************************
1487 * get_make_variable
1489 static char *get_make_variable( struct makefile *make, const char *name )
1491 char *ret;
1493 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1494 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1495 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1496 return NULL;
1500 /*******************************************************************
1501 * get_expanded_make_variable
1503 static char *get_expanded_make_variable( struct makefile *make, const char *name )
1505 char *p, *end, *var, *expand, *tmp;
1507 expand = get_make_variable( make, name );
1508 if (!expand) return NULL;
1510 p = expand;
1511 while ((p = strchr( p, '$' )))
1513 if (p[1] == '(')
1515 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1516 *end++ = 0;
1517 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1518 var = get_make_variable( make, p + 2 );
1519 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1520 free( var );
1522 else if (p[1] == '{') /* don't expand ${} variables */
1524 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1525 p = end + 1;
1526 continue;
1528 else if (p[1] == '$')
1530 tmp = replace_substr( expand, p, 2, "$" );
1532 else fatal_error( "syntax error in '%s'\n", expand );
1534 /* switch to the new string */
1535 p = tmp + (p - expand);
1536 free( expand );
1537 expand = tmp;
1540 /* consider empty variables undefined */
1541 p = expand;
1542 while (*p && isspace(*p)) p++;
1543 if (*p) return expand;
1544 free( expand );
1545 return NULL;
1549 /*******************************************************************
1550 * get_expanded_make_var_array
1552 static struct strarray get_expanded_make_var_array( struct makefile *make, const char *name )
1554 struct strarray ret = empty_strarray;
1555 char *value, *token;
1557 if ((value = get_expanded_make_variable( make, name )))
1558 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1559 strarray_add( &ret, token );
1560 return ret;
1564 /*******************************************************************
1565 * set_make_variable
1567 static int set_make_variable( struct strarray *array, const char *assignment )
1569 char *p, *name;
1571 p = name = xstrdup( assignment );
1572 while (isalnum(*p) || *p == '_') p++;
1573 if (name == p) return 0; /* not a variable */
1574 if (isspace(*p))
1576 *p++ = 0;
1577 while (isspace(*p)) p++;
1579 if (*p != '=') return 0; /* not an assignment */
1580 *p++ = 0;
1581 while (isspace(*p)) p++;
1583 strarray_set_value( array, name, p );
1584 return 1;
1588 /*******************************************************************
1589 * parse_makefile
1591 static struct makefile *parse_makefile( const char *path, const char *separator )
1593 char *buffer;
1594 FILE *file;
1595 struct makefile *make = xmalloc( sizeof(*make) );
1597 memset( make, 0, sizeof(*make) );
1598 if (path)
1600 make->top_obj_dir = get_relative_path( path, "" );
1601 make->base_dir = path;
1602 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1605 file = open_input_makefile( make );
1606 while ((buffer = get_line( file )))
1608 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1609 if (*buffer == '\t') continue; /* command */
1610 while (isspace( *buffer )) buffer++;
1611 if (*buffer == '#') continue; /* comment */
1612 set_make_variable( &make->vars, buffer );
1614 fclose( file );
1615 input_file_name = NULL;
1616 return make;
1620 /*******************************************************************
1621 * add_generated_sources
1623 static void add_generated_sources( struct makefile *make )
1625 struct incl_file *source, *next, *file;
1627 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1629 if (source->file->flags & FLAG_IDL_CLIENT)
1631 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1632 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1633 add_all_includes( make, file, file->file );
1635 if (source->file->flags & FLAG_IDL_SERVER)
1637 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1638 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1639 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1640 add_all_includes( make, file, file->file );
1642 if (source->file->flags & FLAG_IDL_IDENT)
1644 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1645 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1646 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1647 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1648 add_all_includes( make, file, file->file );
1650 if (source->file->flags & FLAG_IDL_PROXY)
1652 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1653 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1654 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1655 add_all_includes( make, file, file->file );
1656 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1657 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1658 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1659 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1660 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1661 add_all_includes( make, file, file->file );
1663 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1665 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1667 if (source->file->flags & FLAG_IDL_REGISTER)
1669 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1671 if (strendswith( source->name, ".y" ))
1673 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1674 /* steal the includes list from the source file */
1675 file->files_count = source->files_count;
1676 file->files_size = source->files_size;
1677 file->files = source->files;
1678 source->files_count = source->files_size = 0;
1679 source->files = NULL;
1681 if (strendswith( source->name, ".l" ))
1683 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1684 /* steal the includes list from the source file */
1685 file->files_count = source->files_count;
1686 file->files_size = source->files_size;
1687 file->files = source->files;
1688 source->files_count = source->files_size = 0;
1689 source->files = NULL;
1692 if (get_make_variable( make, "TESTDLL" ))
1694 file = add_generated_source( make, "testlist.o", "testlist.c" );
1695 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1696 add_all_includes( make, file, file->file );
1701 /*******************************************************************
1702 * create_dir
1704 static void create_dir( const char *dir )
1706 char *p, *path;
1708 p = path = xstrdup( dir );
1709 while ((p = strchr( p, '/' )))
1711 *p = 0;
1712 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1713 *p++ = '/';
1714 while (*p == '/') p++;
1716 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1717 free( path );
1721 /*******************************************************************
1722 * output_filenames_obj_dir
1724 static void output_filenames_obj_dir( struct makefile *make, struct strarray array )
1726 unsigned int i;
1728 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1732 /*******************************************************************
1733 * output_include
1735 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1737 int i;
1739 if (pFile->owner == owner) return;
1740 if (!pFile->filename) return;
1741 pFile->owner = owner;
1742 output_filename( pFile->filename );
1743 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1747 /*******************************************************************
1748 * output_sources
1750 static struct strarray output_sources( struct makefile *make, struct strarray *testlist_files )
1752 struct incl_file *source;
1753 unsigned int i;
1754 struct strarray object_files = empty_strarray;
1755 struct strarray crossobj_files = empty_strarray;
1756 struct strarray res_files = empty_strarray;
1757 struct strarray clean_files = empty_strarray;
1758 struct strarray po_files = empty_strarray;
1759 struct strarray mo_files = empty_strarray;
1760 struct strarray mc_files = empty_strarray;
1761 struct strarray ok_files = empty_strarray;
1762 struct strarray dlldata_files = empty_strarray;
1763 struct strarray c2man_files = empty_strarray;
1764 struct strarray implib_objs = empty_strarray;
1765 struct strarray includes = empty_strarray;
1766 struct strarray subdirs = empty_strarray;
1767 struct strarray phony_targets = empty_strarray;
1768 struct strarray all_targets = get_expanded_make_var_array( make, "PROGRAMS" );
1770 for (i = 0; i < linguas.count; i++)
1771 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
1773 strarray_add( &phony_targets, "all" );
1774 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
1775 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
1776 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
1777 if (make->top_obj_dir) strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
1778 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
1779 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
1780 strarray_addall( &includes, make->include_args );
1782 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
1784 struct strarray extradefs;
1785 char *obj = xstrdup( source->name );
1786 char *ext = get_extension( obj );
1788 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1789 *ext++ = 0;
1791 if (make->src_dir && strchr( obj, '/' ))
1793 char *subdir = base_dir_path( make, obj );
1794 *strrchr( subdir, '/' ) = 0;
1795 strarray_add_uniq( &subdirs, subdir );
1798 extradefs = get_expanded_make_var_array( make, strmake( "%s_EXTRADEFS", obj ));
1800 if (!strcmp( ext, "y" )) /* yacc file */
1802 /* add source file dependency for parallel makes */
1803 char *header = strmake( "%s.tab.h", obj );
1805 if (find_include_file( make, header ))
1807 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
1808 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1809 obj, obj_dir_path( make, obj ), source->filename );
1810 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
1811 source->filename, obj_dir_path( make, header ));
1812 strarray_add( &clean_files, header );
1814 else output( "%s.tab.c: %s\n", obj, source->filename );
1816 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1817 continue; /* no dependencies */
1819 else if (!strcmp( ext, "x" )) /* template file */
1821 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
1822 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1823 output( "\t%s%s -H -o $@ %s\n",
1824 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1825 strarray_add( &clean_files, strmake( "%s.h", obj ));
1826 continue; /* no dependencies */
1828 else if (!strcmp( ext, "l" )) /* lex file */
1830 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
1831 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1832 continue; /* no dependencies */
1834 else if (!strcmp( ext, "rc" )) /* resource file */
1836 strarray_add( &res_files, strmake( "%s.res", obj ));
1837 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1838 tools_path( make, "wrc" ), source->filename );
1839 output( "\t%s -o $@", tools_path( make, "wrc" ) );
1840 if (make->is_win16) output_filename( "-m16" );
1841 else output_filenames( target_flags );
1842 output_filename( "--nostdinc" );
1843 output_filenames( includes );
1844 output_filenames( make->define_args );
1845 output_filenames( extradefs );
1846 if (mo_files.count && (source->file->flags & FLAG_RC_PO))
1848 strarray_add( &po_files, source->filename );
1849 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1850 output_filename( source->filename );
1851 output( "\n" );
1852 output( "%s.res:", obj_dir_path( make, obj ));
1853 output_filenames( mo_files );
1854 output( "\n" );
1855 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
1857 else
1859 output_filename( source->filename );
1860 output( "\n" );
1862 output( "%s.res:", obj_dir_path( make, obj ));
1864 else if (!strcmp( ext, "mc" )) /* message file */
1866 strarray_add( &res_files, strmake( "%s.res", obj ));
1867 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1868 tools_path( make, "wmc" ), source->filename );
1869 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
1870 if (mo_files.count)
1872 strarray_add( &mc_files, source->filename );
1873 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1874 output( "\n" );
1875 output( "%s.res:", obj_dir_path( make, obj ));
1876 output_filenames( mo_files );
1877 output( "\n" );
1878 output( "%s ", obj_dir_path( make, "msg.pot" ));
1880 else output( "\n" );
1881 output( "%s.res:", obj_dir_path( make, obj ));
1883 else if (!strcmp( ext, "idl" )) /* IDL file */
1885 struct strarray targets = empty_strarray;
1886 char *dest;
1888 if (!source->file->flags || find_include_file( make, strmake( "%s.h", obj )))
1889 source->file->flags |= FLAG_IDL_HEADER;
1891 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1893 if (!(source->file->flags & idl_outputs[i].flag)) continue;
1894 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1895 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
1896 strarray_add( &targets, dest );
1898 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1899 output_filenames_obj_dir( make, targets );
1900 output( ": %s\n", tools_path( make, "widl" ));
1901 output( "\t%s -o $@", tools_path( make, "widl" ) );
1902 output_filenames( target_flags );
1903 output_filenames( includes );
1904 output_filenames( make->define_args );
1905 output_filenames( extradefs );
1906 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
1907 output_filename( source->filename );
1908 output( "\n" );
1909 output_filenames_obj_dir( make, targets );
1910 output( ": %s", source->filename );
1912 else if (!strcmp( ext, "in" )) /* .in file or man page */
1914 if (strendswith( obj, ".man" ) && source->file->args)
1916 char *dir, *dest = replace_extension( obj, ".man", "" );
1917 char *lang = strchr( dest, '.' );
1918 char *section = source->file->args;
1919 if (lang)
1921 *lang++ = 0;
1922 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1924 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1925 output( "install-man-pages:: %s\n", obj_dir_path( make, obj ));
1926 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj_dir_path( make, obj ), dir, dest, section );
1927 output( "uninstall::\n" );
1928 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1929 free( dest );
1930 free( dir );
1931 strarray_add( &all_targets, xstrdup(obj) );
1932 strarray_add_uniq( &phony_targets, "install-man-pages" );
1933 strarray_add_uniq( &phony_targets, "uninstall" );
1935 else strarray_add( &clean_files, xstrdup(obj) );
1936 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
1937 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1938 output( "%s:", obj_dir_path( make, obj ));
1940 else if (!strcmp( ext, "sfd" )) /* font file */
1942 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
1943 if (fontforge && !make->src_dir)
1945 output( "%s: %s\n", ttf_file, source->filename );
1946 output( "\t%s -script %s %s $@\n",
1947 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
1948 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1950 if (source->file->flags & FLAG_INSTALL)
1952 output( "install install-lib::\n" );
1953 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1954 output( "uninstall::\n" );
1955 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1957 if (source->file->flags & FLAG_SFD_FONTS)
1959 struct strarray *array = source->file->args;
1961 for (i = 0; i < array->count; i++)
1963 char *font = strtok( xstrdup(array->str[i]), " \t" );
1964 char *args = strtok( NULL, "" );
1966 strarray_add( &all_targets, xstrdup( font ));
1967 output( "%s: %s %s\n", obj_dir_path( make, font ),
1968 tools_path( make, "sfnt2fon" ), ttf_file );
1969 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
1970 output( "install install-lib:: %s\n", font );
1971 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n",
1972 obj_dir_path( make, font ), font );
1973 output( "uninstall::\n" );
1974 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1977 if (source->file->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1979 strarray_add_uniq( &phony_targets, "install" );
1980 strarray_add_uniq( &phony_targets, "install-lib" );
1981 strarray_add_uniq( &phony_targets, "uninstall" );
1983 continue; /* no dependencies */
1985 else if (!strcmp( ext, "svg" )) /* svg file */
1987 if (convert && rsvg && icotool && !make->src_dir)
1989 output( "%s.ico %s.bmp: %s\n",
1990 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
1991 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
1992 top_dir_path( make, "tools/buildimage" ), source->filename );
1994 continue; /* no dependencies */
1996 else if (!strcmp( ext, "res" ))
1998 strarray_add( &res_files, source->name );
1999 continue; /* no dependencies */
2001 else
2003 int need_cross = make->testdll ||
2004 (source->file->flags & FLAG_C_IMPLIB) ||
2005 (make->module && make->staticlib);
2007 if ((source->file->flags & FLAG_GENERATED) &&
2008 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2009 strarray_add( &clean_files, source->filename );
2010 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
2011 strarray_add( &object_files, strmake( "%s.o", obj ));
2012 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2013 output( "\t$(CC) -c -o $@ %s", source->filename );
2014 output_filenames( includes );
2015 output_filenames( make->define_args );
2016 output_filenames( extradefs );
2017 if (make->module || make->staticlib || make->testdll)
2019 output_filenames( dll_flags );
2020 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2022 output_filenames( extra_cflags );
2023 output_filenames( cpp_flags );
2024 output_filename( "$(CFLAGS)" );
2025 output( "\n" );
2026 if (crosstarget && need_cross)
2028 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
2029 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2030 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2031 output_filenames( includes );
2032 output_filenames( make->define_args );
2033 output_filenames( extradefs );
2034 output_filename( "-DWINE_CROSSTEST" );
2035 output_filenames( cpp_flags );
2036 output_filename( "$(CFLAGS)" );
2037 output( "\n" );
2039 if (make->testdll && !strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2041 strarray_add( &ok_files, strmake( "%s.ok", obj ));
2042 output( "%s.ok:\n", obj_dir_path( make, obj ));
2043 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2044 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
2045 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
2047 if (!strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2048 strarray_add( &c2man_files, source->filename );
2049 output( "%s.o", obj_dir_path( make, obj ));
2050 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2051 output( ":" );
2053 free( obj );
2055 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
2056 output( "\n" );
2059 /* rules for files that depend on multiple sources */
2061 if (po_files.count)
2063 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
2064 output_filenames( po_files );
2065 output( "\n" );
2066 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
2067 if (make->is_win16) output_filename( "-m16" );
2068 else output_filenames( target_flags );
2069 output_filename( "--nostdinc" );
2070 output_filenames( includes );
2071 output_filenames( make->define_args );
2072 output_filenames( po_files );
2073 output( "\n" );
2074 strarray_add( &clean_files, "rsrc.pot" );
2077 if (mc_files.count)
2079 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
2080 output_filenames( mc_files );
2081 output( "\n" );
2082 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
2083 output_filenames( mc_files );
2084 output( "\n" );
2085 strarray_add( &clean_files, "msg.pot" );
2088 if (dlldata_files.count)
2090 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
2091 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
2092 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
2093 output_filenames( dlldata_files );
2094 output( "\n" );
2097 if (make->module && !make->staticlib)
2099 struct strarray all_libs = empty_strarray;
2100 char *module_path = obj_dir_path( make, make->module );
2101 char *spec_file = NULL;
2103 if (!make->appmode.count)
2104 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2105 for (i = 0; i < make->delayimports.count; i++)
2106 strarray_add( &all_libs, strmake( "-l%s", make->delayimports.str[i] ));
2107 for (i = 0; i < make->imports.count; i++)
2108 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2109 for (i = 0; i < make->delayimports.count; i++)
2110 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2111 strarray_add( &all_libs, "-lwine" );
2112 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2113 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2114 strarray_addall( &all_libs, libs );
2116 if (*dll_ext)
2118 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
2119 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
2120 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2122 else
2124 strarray_add( &all_targets, make->module );
2125 output( "%s:", module_path );
2127 if (spec_file) output_filename( spec_file );
2128 output_filenames_obj_dir( make, object_files );
2129 output_filenames_obj_dir( make, res_files );
2130 output( "\n" );
2131 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2132 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2133 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2134 output_filenames( target_flags );
2135 output_filenames( unwind_flags );
2136 if (spec_file)
2138 output( " -shared %s", spec_file );
2139 output_filenames( make->extradllflags );
2141 else output_filenames( make->appmode );
2142 output_filenames_obj_dir( make, object_files );
2143 output_filenames_obj_dir( make, res_files );
2144 output_filenames( all_libs );
2145 output_filename( "$(LDFLAGS)" );
2146 output( "\n" );
2148 if (spec_file && make->importlib)
2150 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2151 if (*dll_ext)
2153 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
2154 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2155 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2156 output_filenames( target_flags );
2157 if (make->is_win16) output_filename( "-m16" );
2158 output( "\n" );
2159 if (implib_objs.count)
2161 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
2162 output( "%s.def.a:", importlib_path );
2163 output_filenames_obj_dir( make, implib_objs );
2164 output( "\n" );
2165 output( "\t$(RM) $@\n" );
2166 output( "\t$(AR) $(ARFLAGS) $@" );
2167 output_filenames_obj_dir( make, implib_objs );
2168 output( "\n" );
2169 output( "\t$(RANLIB) $@\n" );
2172 else
2174 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
2175 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2176 output_filenames_obj_dir( make, implib_objs );
2177 output( "\n" );
2178 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2179 output_filenames( target_flags );
2180 output_filenames_obj_dir( make, implib_objs );
2181 output( "\n" );
2183 if (crosstarget && !make->is_win16)
2185 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2186 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2187 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2188 output_filenames_obj_dir( make, cross_files );
2189 output( "\n" );
2190 output( "\t%s -b %s -w --implib -o $@ --export %s",
2191 tools_path( make, "winebuild" ), crosstarget, spec_file );
2192 output_filenames_obj_dir( make, cross_files );
2193 output( "\n" );
2197 if (spec_file)
2199 if (c2man_files.count)
2201 output( "manpages::\n" );
2202 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2203 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2204 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2205 output_filename( strmake( "-o %s/man%s",
2206 top_obj_dir_path( make, "documentation" ), man_ext ));
2207 output_filenames( c2man_files );
2208 output( "\n" );
2209 output( "htmlpages::\n" );
2210 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2211 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2212 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2213 output_filename( strmake( "-o %s",
2214 top_obj_dir_path( make, "documentation/html" )));
2215 output_filenames( c2man_files );
2216 output( "\n" );
2217 output( "sgmlpages::\n" );
2218 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2219 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2220 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2221 output_filename( strmake( "-o %s",
2222 top_obj_dir_path( make, "documentation/api-guide" )));
2223 output_filenames( c2man_files );
2224 output( "\n" );
2225 output( "xmlpages::\n" );
2226 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2227 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2228 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2229 output_filename( strmake( "-o %s",
2230 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2231 output_filenames( c2man_files );
2232 output( "\n" );
2233 strarray_add( &phony_targets, "manpages" );
2234 strarray_add( &phony_targets, "htmlpages" );
2235 strarray_add( &phony_targets, "sgmlpages" );
2236 strarray_add( &phony_targets, "xmlpages" );
2238 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2242 if (make->staticlib)
2244 strarray_add( &all_targets, make->staticlib );
2245 output( "%s:", obj_dir_path( make, make->staticlib ));
2246 output_filenames_obj_dir( make, object_files );
2247 output( "\n\t$(RM) $@\n" );
2248 output( "\t$(AR) $(ARFLAGS) $@" );
2249 output_filenames_obj_dir( make, object_files );
2250 output( "\n\t$(RANLIB) $@\n" );
2251 if (crosstarget && make->module)
2253 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2255 strarray_add( &all_targets, name );
2256 output( "%s:", obj_dir_path( make, name ));
2257 output_filenames_obj_dir( make, crossobj_files );
2258 output( "\n\t$(RM) $@\n" );
2259 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2260 output_filenames_obj_dir( make, crossobj_files );
2261 output( "\n\t%s-ranlib $@\n", crosstarget );
2265 if (make->testdll)
2267 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2268 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2269 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2270 struct strarray all_libs = empty_strarray;
2272 for (i = 0; i < make->imports.count; i++)
2273 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2274 strarray_addall( &all_libs, get_expanded_make_var_array( make, "LIBS" ));
2276 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2277 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2278 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2279 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2280 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2281 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2282 output_filenames( target_flags );
2283 output_filenames( unwind_flags );
2284 output_filenames( make->appmode );
2285 output_filenames_obj_dir( make, object_files );
2286 output_filenames_obj_dir( make, res_files );
2287 output_filenames( all_libs );
2288 output_filename( "$(LDFLAGS)" );
2289 output( "\n" );
2290 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2291 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2292 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2293 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2294 output_filenames( target_flags );
2295 output_filenames( unwind_flags );
2296 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2297 output_filenames( make->appmode );
2298 output_filenames_obj_dir( make, object_files );
2299 output_filenames_obj_dir( make, res_files );
2300 output_filenames( all_libs );
2301 output_filename( "$(LDFLAGS)" );
2302 output( "\n" );
2303 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2304 obj_dir_path( make, stripped ), dll_ext );
2305 output_filenames_obj_dir( make, object_files );
2306 output_filenames_obj_dir( make, res_files );
2307 output( "\n" );
2309 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2310 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2311 obj_dir_path( make, stripped ), dll_ext );
2312 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2313 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2315 if (crosstarget)
2317 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2319 strarray_add( &clean_files, crosstest );
2320 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2321 output( "%s:", obj_dir_path( make, crosstest ));
2322 output_filenames_obj_dir( make, crossobj_files );
2323 output_filenames_obj_dir( make, res_files );
2324 output( "\n" );
2325 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2326 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2327 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2328 output_filename( "--lib-suffix=.cross.a" );
2329 output_filenames_obj_dir( make, crossobj_files );
2330 output_filenames_obj_dir( make, res_files );
2331 output_filenames( all_libs );
2332 output_filename( "$(LDFLAGS)" );
2333 output( "\n" );
2334 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2335 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2338 output_filenames_obj_dir( make, ok_files );
2339 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2340 output( "check test:" );
2341 output_filenames_obj_dir( make, ok_files );
2342 output( "\n" );
2343 output( "testclean::\n" );
2344 output( "\t$(RM)" );
2345 output_filenames_obj_dir( make, ok_files );
2346 output( "\n" );
2347 strarray_addall( &clean_files, ok_files );
2348 strarray_add( &phony_targets, "check" );
2349 strarray_add( &phony_targets, "test" );
2350 strarray_add( &phony_targets, "testclean" );
2351 *testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2354 if (all_targets.count)
2356 output( "all:" );
2357 output_filenames_obj_dir( make, all_targets );
2358 output( "\n" );
2361 strarray_addall( &clean_files, object_files );
2362 strarray_addall( &clean_files, crossobj_files );
2363 strarray_addall( &clean_files, res_files );
2364 strarray_addall( &clean_files, all_targets );
2365 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2367 if (clean_files.count)
2369 output( "%s::\n", obj_dir_path( make, "clean" ));
2370 output( "\t$(RM)" );
2371 output_filenames_obj_dir( make, clean_files );
2372 output( "\n" );
2373 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2374 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2377 if (make->top_obj_dir)
2379 output( "depend:\n" );
2380 output( "\t@cd %s && $(MAKE) %s\n", make->top_obj_dir, base_dir_path( make, "depend" ));
2381 strarray_add( &phony_targets, "depend" );
2384 if (phony_targets.count)
2386 output( ".PHONY:" );
2387 output_filenames( phony_targets );
2388 output( "\n" );
2391 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2393 return clean_files;
2397 /*******************************************************************
2398 * create_temp_file
2400 static FILE *create_temp_file( const char *orig )
2402 char *name = xmalloc( strlen(orig) + 13 );
2403 unsigned int i, id = getpid();
2404 int fd;
2405 FILE *ret = NULL;
2407 for (i = 0; i < 100; i++)
2409 sprintf( name, "%s.tmp%08x", orig, id );
2410 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2412 ret = fdopen( fd, "w" );
2413 break;
2415 if (errno != EEXIST) break;
2416 id += 7777;
2418 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2419 temp_file_name = name;
2420 return ret;
2424 /*******************************************************************
2425 * rename_temp_file
2427 static void rename_temp_file( const char *dest )
2429 int ret = rename( temp_file_name, dest );
2430 if (ret == -1 && errno == EEXIST)
2432 /* rename doesn't overwrite on windows */
2433 unlink( dest );
2434 ret = rename( temp_file_name, dest );
2436 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2437 temp_file_name = NULL;
2441 /*******************************************************************
2442 * are_files_identical
2444 static int are_files_identical( FILE *file1, FILE *file2 )
2446 for (;;)
2448 char buffer1[8192], buffer2[8192];
2449 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2450 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2451 if (size1 != size2) return 0;
2452 if (!size1) return feof( file1 ) && feof( file2 );
2453 if (memcmp( buffer1, buffer2, size1 )) return 0;
2458 /*******************************************************************
2459 * rename_temp_file_if_changed
2461 static void rename_temp_file_if_changed( const char *dest )
2463 FILE *file1, *file2;
2464 int do_rename = 1;
2466 if ((file1 = fopen( dest, "r" )))
2468 if ((file2 = fopen( temp_file_name, "r" )))
2470 do_rename = !are_files_identical( file1, file2 );
2471 fclose( file2 );
2473 fclose( file1 );
2475 if (!do_rename)
2477 unlink( temp_file_name );
2478 temp_file_name = NULL;
2480 else rename_temp_file( dest );
2484 /*******************************************************************
2485 * output_testlist
2487 static void output_testlist( const char *dest, struct strarray files )
2489 int i;
2491 output_file = create_temp_file( dest );
2493 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2494 output( "#define WIN32_LEAN_AND_MEAN\n" );
2495 output( "#include <windows.h>\n\n" );
2496 output( "#define STANDALONE\n" );
2497 output( "#include \"wine/test.h\"\n\n" );
2499 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2500 output( "\n" );
2501 output( "const struct test winetest_testlist[] =\n" );
2502 output( "{\n" );
2503 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2504 output( " { 0, 0 }\n" );
2505 output( "};\n" );
2507 if (fclose( output_file )) fatal_perror( "write" );
2508 output_file = NULL;
2509 rename_temp_file_if_changed( dest );
2513 /*******************************************************************
2514 * output_gitignore
2516 static void output_gitignore( const char *dest, struct strarray files )
2518 int i;
2520 output_file = create_temp_file( dest );
2522 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2523 for (i = 0; i < files.count; i++)
2525 if (!strchr( files.str[i], '/' )) output( "/" );
2526 output( "%s\n", files.str[i] );
2529 if (fclose( output_file )) fatal_perror( "write" );
2530 output_file = NULL;
2531 rename_temp_file( dest );
2535 /*******************************************************************
2536 * output_top_variables
2538 static void output_top_variables( struct makefile *make )
2540 unsigned int i;
2541 struct strarray *vars = &top_makefile->vars;
2543 if (!make->base_dir) return; /* don't output variables in the top makefile */
2545 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
2546 output( "all:\n\n" );
2547 for (i = 0; i < vars->count; i += 2)
2548 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
2549 output( "\n" );
2553 /*******************************************************************
2554 * output_dependencies
2556 static void output_dependencies( struct makefile *make )
2558 struct strarray targets, testlist_files = empty_strarray, ignore_files = empty_strarray;
2559 char buffer[1024];
2560 FILE *src_file;
2562 output_file_name = base_dir_path( make, output_makefile_name );
2563 output_file = create_temp_file( output_file_name );
2564 output_top_variables( make );
2566 /* copy the contents of the source makefile */
2567 src_file = open_input_makefile( make );
2568 while (fgets( buffer, sizeof(buffer), src_file ))
2569 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
2570 if (fclose( src_file )) fatal_perror( "close" );
2571 input_file_name = NULL;
2573 targets = output_sources( make, &testlist_files );
2575 fclose( output_file );
2576 output_file = NULL;
2577 rename_temp_file( output_file_name );
2579 strarray_add( &ignore_files, ".gitignore" );
2580 strarray_add( &ignore_files, "Makefile" );
2581 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2582 strarray_addall( &ignore_files, targets );
2584 if (testlist_files.count)
2585 output_testlist( base_dir_path( make, "testlist.c" ), testlist_files );
2586 if (!make->src_dir && make->base_dir)
2587 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
2589 output_file_name = NULL;
2593 /*******************************************************************
2594 * update_makefile
2596 static void update_makefile( const char *path )
2598 static const char *source_vars[] =
2600 "C_SRCS",
2601 "OBJC_SRCS",
2602 "RC_SRCS",
2603 "MC_SRCS",
2604 "IDL_SRCS",
2605 "BISON_SRCS",
2606 "LEX_SRCS",
2607 "XTEMPLATE_SRCS",
2608 "SVG_SRCS",
2609 "FONT_SRCS",
2610 "IN_SRCS",
2611 "MANPAGES",
2612 NULL
2614 const char **var;
2615 unsigned int i;
2616 struct strarray value;
2617 struct incl_file *file;
2618 struct makefile *make;
2620 make = parse_makefile( path, NULL );
2622 if (root_src_dir)
2624 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
2625 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
2627 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
2628 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
2629 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
2631 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
2632 make->module = get_expanded_make_variable( make, "MODULE" );
2633 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
2634 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
2635 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
2637 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
2638 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
2639 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
2640 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
2642 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
2644 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
2645 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
2647 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
2648 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 );
2650 make->include_args = empty_strarray;
2651 make->define_args = empty_strarray;
2652 strarray_add( &make->define_args, "-D__WINESRC__" );
2654 value = get_expanded_make_var_array( make, "EXTRAINCL" );
2655 for (i = 0; i < value.count; i++)
2656 if (!strncmp( value.str[i], "-I", 2 ))
2657 strarray_add_uniq( &make->include_args, value.str[i] );
2658 else
2659 strarray_add_uniq( &make->define_args, value.str[i] );
2660 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
2662 list_init( &make->sources );
2663 list_init( &make->includes );
2665 /* FIXME: target dir has to exist to allow locating srcdir-relative include files */
2666 if (make->base_dir) create_dir( make->base_dir );
2668 for (var = source_vars; *var; var++)
2670 value = get_expanded_make_var_array( make, *var );
2671 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
2674 add_generated_sources( make );
2676 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
2677 for (i = 0; i < value.count; i++)
2679 /* default to .c for unknown extra object files */
2680 if (strendswith( value.str[i], ".o" ))
2681 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2682 else
2683 add_generated_source( make, value.str[i], NULL );
2686 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
2688 output_dependencies( make );
2692 /*******************************************************************
2693 * parse_makeflags
2695 static void parse_makeflags( const char *flags )
2697 const char *p = flags;
2698 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2700 while (*p)
2702 while (isspace(*p)) p++;
2703 var = buffer;
2704 while (*p && !isspace(*p))
2706 if (*p == '\\' && p[1]) p++;
2707 *var++ = *p++;
2709 *var = 0;
2710 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2715 /*******************************************************************
2716 * parse_option
2718 static int parse_option( const char *opt )
2720 if (opt[0] != '-')
2722 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2723 return 0;
2725 switch(opt[1])
2727 case 'f':
2728 if (opt[2]) output_makefile_name = opt + 2;
2729 break;
2730 case 'i':
2731 if (opt[2]) input_makefile_name = opt + 2;
2732 break;
2733 case 'R':
2734 relative_dir_mode = 1;
2735 break;
2736 default:
2737 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2738 exit(1);
2740 return 1;
2744 /*******************************************************************
2745 * main
2747 int main( int argc, char *argv[] )
2749 const char *makeflags = getenv( "MAKEFLAGS" );
2750 int i, j;
2752 if (makeflags) parse_makeflags( makeflags );
2754 i = 1;
2755 while (i < argc)
2757 if (parse_option( argv[i] ))
2759 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2760 argc--;
2762 else i++;
2765 if (relative_dir_mode)
2767 char *relpath;
2769 if (argc != 3)
2771 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2772 exit( 1 );
2774 relpath = get_relative_path( argv[1], argv[2] );
2775 printf( "%s\n", relpath ? relpath : "." );
2776 exit( 0 );
2779 if (argc <= 1)
2781 fprintf( stderr, "%s", Usage );
2782 exit( 1 );
2784 atexit( cleanup_files );
2785 signal( SIGTERM, exit_on_signal );
2786 signal( SIGINT, exit_on_signal );
2787 #ifdef SIGHUP
2788 signal( SIGHUP, exit_on_signal );
2789 #endif
2791 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
2793 if (!input_makefile_name) input_makefile_name = strmake( "%s.in", output_makefile_name );
2795 top_makefile = parse_makefile( NULL, "# End of common header" );
2797 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
2798 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
2799 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
2800 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
2801 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
2802 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
2803 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
2804 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
2806 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
2807 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
2808 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
2809 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
2810 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
2811 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2812 dll_prefix = get_expanded_make_variable( top_makefile, "DLLPREFIX" );
2813 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
2814 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
2815 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
2816 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
2817 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
2819 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
2820 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2821 if (!tools_ext) tools_ext = "";
2822 if (!dll_prefix) dll_prefix = "";
2823 if (!man_ext) man_ext = "3w";
2825 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2826 return 0;