wined3d: Add a missing TRACE to wined3d_texture_unmap().
[wine/multimedia.git] / tools / makedep.c
blob295e5e8386fdda44120d03c0cd0cef3e47be7c46
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 programs;
153 struct strarray appmode;
154 struct strarray imports;
155 struct strarray delayimports;
156 struct strarray extradllflags;
157 struct list sources;
158 struct list includes;
159 const char *base_dir;
160 const char *src_dir;
161 const char *obj_dir;
162 const char *top_src_dir;
163 const char *top_obj_dir;
164 const char *parent_dir;
165 const char *module;
166 const char *testdll;
167 const char *staticlib;
168 const char *importlib;
169 int use_msvcrt;
170 int is_win16;
173 static struct makefile *top_makefile;
175 static const char *output_makefile_name = "Makefile";
176 static const char *input_makefile_name;
177 static const char *input_file_name;
178 static const char *output_file_name;
179 static const char *temp_file_name;
180 static int relative_dir_mode;
181 static int input_line;
182 static int output_column;
183 static FILE *output_file;
185 static const char Usage[] =
186 "Usage: makedep [options] directories\n"
187 "Options:\n"
188 " -R from to Compute the relative path between two directories\n"
189 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
190 " -ixxx Read input from file 'xxx' (default: Makefile.in)\n";
193 #ifndef __GNUC__
194 #define __attribute__(x)
195 #endif
197 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
198 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
199 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
200 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
202 /*******************************************************************
203 * fatal_error
205 static void fatal_error( const char *msg, ... )
207 va_list valist;
208 va_start( valist, msg );
209 if (input_file_name)
211 fprintf( stderr, "%s:", input_file_name );
212 if (input_line) fprintf( stderr, "%d:", input_line );
213 fprintf( stderr, " error: " );
215 else fprintf( stderr, "makedep: error: " );
216 vfprintf( stderr, msg, valist );
217 va_end( valist );
218 exit(1);
222 /*******************************************************************
223 * fatal_perror
225 static void fatal_perror( const char *msg, ... )
227 va_list valist;
228 va_start( valist, msg );
229 if (input_file_name)
231 fprintf( stderr, "%s:", input_file_name );
232 if (input_line) fprintf( stderr, "%d:", input_line );
233 fprintf( stderr, " error: " );
235 else fprintf( stderr, "makedep: error: " );
236 vfprintf( stderr, msg, valist );
237 perror( " " );
238 va_end( valist );
239 exit(1);
243 /*******************************************************************
244 * cleanup_files
246 static void cleanup_files(void)
248 if (temp_file_name) unlink( temp_file_name );
249 if (output_file_name) unlink( output_file_name );
253 /*******************************************************************
254 * exit_on_signal
256 static void exit_on_signal( int sig )
258 exit( 1 ); /* this will call the atexit functions */
262 /*******************************************************************
263 * xmalloc
265 static void *xmalloc( size_t size )
267 void *res;
268 if (!(res = malloc (size ? size : 1)))
269 fatal_error( "Virtual memory exhausted.\n" );
270 return res;
274 /*******************************************************************
275 * xrealloc
277 static void *xrealloc (void *ptr, size_t size)
279 void *res;
280 assert( size );
281 if (!(res = realloc( ptr, size )))
282 fatal_error( "Virtual memory exhausted.\n" );
283 return res;
286 /*******************************************************************
287 * xstrdup
289 static char *xstrdup( const char *str )
291 char *res = strdup( str );
292 if (!res) fatal_error( "Virtual memory exhausted.\n" );
293 return res;
297 /*******************************************************************
298 * strmake
300 static char *strmake( const char* fmt, ... )
302 int n;
303 size_t size = 100;
304 va_list ap;
306 for (;;)
308 char *p = xmalloc (size);
309 va_start(ap, fmt);
310 n = vsnprintf (p, size, fmt, ap);
311 va_end(ap);
312 if (n == -1) size *= 2;
313 else if ((size_t)n >= size) size = n + 1;
314 else return p;
315 free(p);
320 /*******************************************************************
321 * strendswith
323 static int strendswith( const char* str, const char* end )
325 int l = strlen(str);
326 int m = strlen(end);
328 return l >= m && strcmp(str + l - m, end) == 0;
332 /*******************************************************************
333 * output
335 static void output( const char *format, ... )
337 int ret;
338 va_list valist;
340 va_start( valist, format );
341 ret = vfprintf( output_file, format, valist );
342 va_end( valist );
343 if (ret < 0) fatal_perror( "output" );
344 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
345 else output_column += ret;
349 /*******************************************************************
350 * strarray_add
352 static void strarray_add( struct strarray *array, const char *str )
354 if (array->count == array->size)
356 if (array->size) array->size *= 2;
357 else array->size = 16;
358 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
360 array->str[array->count++] = str;
364 /*******************************************************************
365 * strarray_addall
367 static void strarray_addall( struct strarray *array, struct strarray added )
369 unsigned int i;
371 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
375 /*******************************************************************
376 * strarray_exists
378 static int strarray_exists( struct strarray *array, const char *str )
380 unsigned int i;
382 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
383 return 0;
387 /*******************************************************************
388 * strarray_add_uniq
390 static void strarray_add_uniq( struct strarray *array, const char *str )
392 if (!strarray_exists( array, str )) strarray_add( array, str );
396 /*******************************************************************
397 * strarray_get_value
399 * Find a value in a name/value pair string array.
401 static char *strarray_get_value( const struct strarray *array, const char *name )
403 unsigned int i;
405 for (i = 0; i < array->count; i += 2)
406 if (!strcmp( array->str[i], name )) return xstrdup( array->str[i + 1] );
407 return NULL;
411 /*******************************************************************
412 * strarray_set_value
414 * Define a value in a name/value pair string array.
416 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
418 unsigned int i;
420 /* redefining a variable replaces the previous value */
421 for (i = 0; i < array->count; i += 2)
423 if (strcmp( array->str[i], name )) continue;
424 array->str[i + 1] = value;
425 return;
427 strarray_add( array, name );
428 strarray_add( array, value );
432 /*******************************************************************
433 * output_filename
435 static void output_filename( const char *name )
437 if (output_column + strlen(name) + 1 > 100)
439 output( " \\\n" );
440 output( " " );
442 else if (output_column) output( " " );
443 output( "%s", name );
447 /*******************************************************************
448 * output_filenames
450 static void output_filenames( struct strarray array )
452 unsigned int i;
454 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
458 /*******************************************************************
459 * get_extension
461 static char *get_extension( char *filename )
463 char *ext = strrchr( filename, '.' );
464 if (ext && strchr( ext, '/' )) ext = NULL;
465 return ext;
469 /*******************************************************************
470 * replace_extension
472 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
474 char *ret;
475 int name_len = strlen( name );
476 int ext_len = strlen( old_ext );
478 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
479 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
480 memcpy( ret, name, name_len );
481 strcpy( ret + name_len, new_ext );
482 return ret;
486 /*******************************************************************
487 * strarray_replace_extension
489 static struct strarray strarray_replace_extension( const struct strarray *array,
490 const char *old_ext, const char *new_ext )
492 unsigned int i;
493 struct strarray ret;
495 ret.count = ret.size = array->count;
496 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
497 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
498 return ret;
502 /*******************************************************************
503 * replace_substr
505 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
507 unsigned int pos = start - str;
508 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
509 memcpy( ret, str, pos );
510 strcpy( ret + pos, replace );
511 strcat( ret + pos, start + len );
512 return ret;
516 /*******************************************************************
517 * get_relative_path
519 * Determine where the destination path is located relative to the 'from' path.
521 static char *get_relative_path( const char *from, const char *dest )
523 const char *start;
524 char *ret, *p;
525 unsigned int dotdots = 0;
527 /* a path of "." is equivalent to an empty path */
528 if (!strcmp( from, "." )) from = "";
530 for (;;)
532 while (*from == '/') from++;
533 while (*dest == '/') dest++;
534 start = dest; /* save start of next path element */
535 if (!*from) break;
537 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
538 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
540 /* count remaining elements in 'from' */
543 dotdots++;
544 while (*from && *from != '/') from++;
545 while (*from == '/') from++;
547 while (*from);
548 break;
551 if (!start[0] && !dotdots) return NULL; /* empty path */
553 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
554 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
556 if (start[0]) strcpy( p, start );
557 else p[-1] = 0; /* remove trailing slash */
558 return ret;
562 /*******************************************************************
563 * concat_paths
565 static char *concat_paths( const char *base, const char *path )
567 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
568 if (!path || !path[0]) return xstrdup( base );
569 if (path[0] == '/') return xstrdup( path );
570 return strmake( "%s/%s", base, path );
574 /*******************************************************************
575 * base_dir_path
577 static char *base_dir_path( struct makefile *make, const char *path )
579 return concat_paths( make->base_dir, path );
583 /*******************************************************************
584 * obj_dir_path
586 static char *obj_dir_path( struct makefile *make, const char *path )
588 return concat_paths( make->obj_dir, path );
592 /*******************************************************************
593 * src_dir_path
595 static char *src_dir_path( struct makefile *make, const char *path )
597 if (make->src_dir) return concat_paths( make->src_dir, path );
598 return obj_dir_path( make, path );
602 /*******************************************************************
603 * top_obj_dir_path
605 static char *top_obj_dir_path( struct makefile *make, const char *path )
607 return concat_paths( make->top_obj_dir, path );
611 /*******************************************************************
612 * top_dir_path
614 static char *top_dir_path( struct makefile *make, const char *path )
616 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
617 return top_obj_dir_path( make, path );
621 /*******************************************************************
622 * root_dir_path
624 static char *root_dir_path( const char *path )
626 return concat_paths( root_src_dir, path );
630 /*******************************************************************
631 * tools_dir_path
633 static char *tools_dir_path( struct makefile *make, const char *path )
635 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
636 return top_obj_dir_path( make, strmake( "tools/%s", path ));
640 /*******************************************************************
641 * tools_path
643 static char *tools_path( struct makefile *make, const char *name )
645 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
649 /*******************************************************************
650 * get_line
652 static char *get_line( FILE *file )
654 static char *buffer;
655 static unsigned int size;
657 if (!size)
659 size = 1024;
660 buffer = xmalloc( size );
662 if (!fgets( buffer, size, file )) return NULL;
663 input_line++;
665 for (;;)
667 char *p = buffer + strlen(buffer);
668 /* if line is larger than buffer, resize buffer */
669 while (p == buffer + size - 1 && p[-1] != '\n')
671 buffer = xrealloc( buffer, size * 2 );
672 if (!fgets( buffer + size - 1, size + 1, file )) break;
673 p = buffer + strlen(buffer);
674 size *= 2;
676 if (p > buffer && p[-1] == '\n')
678 *(--p) = 0;
679 if (p > buffer && p[-1] == '\r') *(--p) = 0;
680 if (p > buffer && p[-1] == '\\')
682 *(--p) = 0;
683 /* line ends in backslash, read continuation line */
684 if (!fgets( p, size - (p - buffer), file )) return buffer;
685 input_line++;
686 continue;
689 return buffer;
694 /*******************************************************************
695 * hash_filename
697 static unsigned int hash_filename( const char *name )
699 unsigned int ret = 0;
700 while (*name) ret = (ret << 7) + (ret << 3) + *name++;
701 return ret % HASH_SIZE;
705 /*******************************************************************
706 * add_file
708 static struct file *add_file( const char *name )
710 struct file *file = xmalloc( sizeof(*file) );
711 memset( file, 0, sizeof(*file) );
712 file->name = xstrdup( name );
713 list_add_tail( &files[hash_filename( name )], &file->entry );
714 return file;
718 /*******************************************************************
719 * add_dependency
721 static void add_dependency( struct file *file, const char *name, enum incl_type type )
723 /* enforce some rules for the Wine tree */
725 if (!memcmp( name, "../", 3 ))
726 fatal_error( "#include directive with relative path not allowed\n" );
728 if (!strcmp( name, "config.h" ))
730 if (strendswith( file->name, ".h" ))
731 fatal_error( "config.h must not be included by a header file\n" );
732 if (file->deps_count)
733 fatal_error( "config.h must be included before anything else\n" );
735 else if (!strcmp( name, "wine/port.h" ))
737 if (strendswith( file->name, ".h" ))
738 fatal_error( "wine/port.h must not be included by a header file\n" );
739 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
740 if (file->deps_count > 1)
741 fatal_error( "wine/port.h must be included before everything except config.h\n" );
742 if (strcmp( file->deps[0].name, "config.h" ))
743 fatal_error( "config.h must be included before wine/port.h\n" );
746 if (file->deps_count >= file->deps_size)
748 file->deps_size *= 2;
749 if (file->deps_size < 16) file->deps_size = 16;
750 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
752 file->deps[file->deps_count].line = input_line;
753 file->deps[file->deps_count].type = type;
754 file->deps[file->deps_count].name = xstrdup( name );
755 file->deps_count++;
759 /*******************************************************************
760 * find_src_file
762 static struct incl_file *find_src_file( struct makefile *make, const char *name )
764 struct incl_file *file;
766 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
767 if (!strcmp( name, file->name )) return file;
768 return NULL;
771 /*******************************************************************
772 * find_include_file
774 static struct incl_file *find_include_file( struct makefile *make, const char *name )
776 struct incl_file *file;
778 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
779 if (!strcmp( name, file->name )) return file;
780 return NULL;
783 /*******************************************************************
784 * add_include
786 * Add an include file if it doesn't already exists.
788 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
789 const char *name, int line, int system )
791 struct incl_file *include;
793 if (parent->files_count >= parent->files_size)
795 parent->files_size *= 2;
796 if (parent->files_size < 16) parent->files_size = 16;
797 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
800 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
801 if (!strcmp( name, include->name )) goto found;
803 include = xmalloc( sizeof(*include) );
804 memset( include, 0, sizeof(*include) );
805 include->name = xstrdup(name);
806 include->included_by = parent;
807 include->included_line = line;
808 include->system = system;
809 list_add_tail( &make->includes, &include->entry );
810 found:
811 parent->files[parent->files_count++] = include;
812 return include;
816 /*******************************************************************
817 * add_generated_source
819 * Add a generated source file to the list.
821 static struct incl_file *add_generated_source( struct makefile *make,
822 const char *name, const char *filename )
824 struct incl_file *file;
826 if ((file = find_src_file( make, name ))) return file; /* we already have it */
827 file = xmalloc( sizeof(*file) );
828 memset( file, 0, sizeof(*file) );
829 file->file = add_file( name );
830 file->name = xstrdup( name );
831 file->filename = obj_dir_path( make, filename ? filename : name );
832 file->file->flags = FLAG_GENERATED;
833 list_add_tail( &make->sources, &file->entry );
834 return file;
838 /*******************************************************************
839 * parse_include_directive
841 static void parse_include_directive( struct file *source, char *str )
843 char quote, *include, *p = str;
845 while (*p && isspace(*p)) p++;
846 if (*p != '\"' && *p != '<' ) return;
847 quote = *p++;
848 if (quote == '<') quote = '>';
849 include = p;
850 while (*p && (*p != quote)) p++;
851 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
852 *p = 0;
853 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
857 /*******************************************************************
858 * parse_pragma_directive
860 static void parse_pragma_directive( struct file *source, char *str )
862 char *flag, *p = str;
864 if (!isspace( *p )) return;
865 while (*p && isspace(*p)) p++;
866 p = strtok( p, " \t" );
867 if (strcmp( p, "makedep" )) return;
869 while ((flag = strtok( NULL, " \t" )))
871 if (!strcmp( flag, "depend" ))
873 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
874 return;
876 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
878 if (strendswith( source->name, ".idl" ))
880 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
881 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
882 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
883 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
884 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
885 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
886 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
887 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
889 else if (strendswith( source->name, ".rc" ))
891 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
893 else if (strendswith( source->name, ".sfd" ))
895 if (!strcmp( flag, "font" ))
897 struct strarray *array = source->args;
899 if (!array)
901 source->args = array = xmalloc( sizeof(*array) );
902 *array = empty_strarray;
903 source->flags |= FLAG_SFD_FONTS;
905 strarray_add( array, xstrdup( strtok( NULL, "" )));
906 return;
909 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
914 /*******************************************************************
915 * parse_cpp_directive
917 static void parse_cpp_directive( struct file *source, char *str )
919 while (*str && isspace(*str)) str++;
920 if (*str++ != '#') return;
921 while (*str && isspace(*str)) str++;
923 if (!strncmp( str, "include", 7 ))
924 parse_include_directive( source, str + 7 );
925 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
926 parse_include_directive( source, str + 6 );
927 else if (!strncmp( str, "pragma", 6 ))
928 parse_pragma_directive( source, str + 6 );
932 /*******************************************************************
933 * parse_idl_file
935 static void parse_idl_file( struct file *source, FILE *file )
937 char *buffer, *include;
939 input_line = 0;
941 while ((buffer = get_line( file )))
943 char quote;
944 char *p = buffer;
945 while (*p && isspace(*p)) p++;
947 if (!strncmp( p, "import", 6 ))
949 p += 6;
950 while (*p && isspace(*p)) p++;
951 if (*p != '"') continue;
952 include = ++p;
953 while (*p && (*p != '"')) p++;
954 if (!*p) fatal_error( "malformed import directive\n" );
955 *p = 0;
956 add_dependency( source, include, INCL_IMPORT );
957 continue;
960 /* check for #include inside cpp_quote */
961 if (!strncmp( p, "cpp_quote", 9 ))
963 p += 9;
964 while (*p && isspace(*p)) p++;
965 if (*p++ != '(') continue;
966 while (*p && isspace(*p)) p++;
967 if (*p++ != '"') continue;
968 if (*p++ != '#') continue;
969 while (*p && isspace(*p)) p++;
970 if (strncmp( p, "include", 7 )) continue;
971 p += 7;
972 while (*p && isspace(*p)) p++;
973 if (*p == '\\' && p[1] == '"')
975 p += 2;
976 quote = '"';
978 else
980 if (*p++ != '<' ) continue;
981 quote = '>';
983 include = p;
984 while (*p && (*p != quote)) p++;
985 if (!*p || (quote == '"' && p[-1] != '\\'))
986 fatal_error( "malformed #include directive inside cpp_quote\n" );
987 if (quote == '"') p--; /* remove backslash */
988 *p = 0;
989 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
990 continue;
993 parse_cpp_directive( source, p );
997 /*******************************************************************
998 * parse_c_file
1000 static void parse_c_file( struct file *source, FILE *file )
1002 char *buffer;
1004 input_line = 0;
1005 while ((buffer = get_line( file )))
1007 parse_cpp_directive( source, buffer );
1012 /*******************************************************************
1013 * parse_rc_file
1015 static void parse_rc_file( struct file *source, FILE *file )
1017 char *buffer, *include;
1019 input_line = 0;
1020 while ((buffer = get_line( file )))
1022 char quote;
1023 char *p = buffer;
1024 while (*p && isspace(*p)) p++;
1026 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1028 p += 2;
1029 while (*p && isspace(*p)) p++;
1030 if (strncmp( p, "@makedep:", 9 )) continue;
1031 p += 9;
1032 while (*p && isspace(*p)) p++;
1033 quote = '"';
1034 if (*p == quote)
1036 include = ++p;
1037 while (*p && *p != quote) p++;
1039 else
1041 include = p;
1042 while (*p && !isspace(*p) && *p != '*') p++;
1044 if (!*p)
1045 fatal_error( "malformed makedep comment\n" );
1046 *p = 0;
1047 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1048 continue;
1051 parse_cpp_directive( source, buffer );
1056 /*******************************************************************
1057 * parse_in_file
1059 static void parse_in_file( struct file *source, FILE *file )
1061 char *p, *buffer;
1063 /* make sure it gets rebuilt when the version changes */
1064 add_dependency( source, "config.h", INCL_SYSTEM );
1066 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1068 input_line = 0;
1069 while ((buffer = get_line( file )))
1071 if (strncmp( buffer, ".TH", 3 )) continue;
1072 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1073 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1074 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1075 source->args = xstrdup( p );
1076 return;
1081 /*******************************************************************
1082 * parse_sfd_file
1084 static void parse_sfd_file( struct file *source, FILE *file )
1086 char *p, *eol, *buffer;
1088 input_line = 0;
1089 while ((buffer = get_line( file )))
1091 if (strncmp( buffer, "UComments:", 10 )) continue;
1092 p = buffer + 10;
1093 while (*p == ' ') p++;
1094 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1096 p++;
1097 buffer[strlen(buffer) - 1] = 0;
1099 while ((eol = strstr( p, "+AAoA" )))
1101 *eol = 0;
1102 while (*p && isspace(*p)) p++;
1103 if (*p++ == '#')
1105 while (*p && isspace(*p)) p++;
1106 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1108 p = eol + 5;
1110 while (*p && isspace(*p)) p++;
1111 if (*p++ != '#') return;
1112 while (*p && isspace(*p)) p++;
1113 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1114 return;
1119 /*******************************************************************
1120 * load_file
1122 static struct file *load_file( const char *name )
1124 struct file *file;
1125 FILE *f;
1126 unsigned int hash = hash_filename( name );
1128 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1129 if (!strcmp( name, file->name )) return file;
1131 if (!(f = fopen( name, "r" ))) return NULL;
1133 file = add_file( name );
1134 input_file_name = file->name;
1135 input_line = 0;
1137 if (strendswith( name, ".idl" )) parse_idl_file( file, f );
1138 else if (strendswith( name, ".rc" )) parse_rc_file( file, f );
1139 else if (strendswith( name, ".in" )) parse_in_file( file, f );
1140 else if (strendswith( name, ".sfd" )) parse_sfd_file( file, f );
1141 else if (strendswith( name, ".c" ) ||
1142 strendswith( name, ".m" ) ||
1143 strendswith( name, ".h" ) ||
1144 strendswith( name, ".l" ) ||
1145 strendswith( name, ".y" )) parse_c_file( file, f );
1147 fclose( f );
1148 input_file_name = NULL;
1150 return file;
1154 /*******************************************************************
1155 * open_file
1157 static struct file *open_file( struct makefile *make, const char *path, char **filename )
1159 struct file *ret = load_file( base_dir_path( make, path ));
1161 if (ret) *filename = xstrdup( path );
1162 return ret;
1166 /*******************************************************************
1167 * open_local_file
1169 * Open a file in the source directory of the makefile.
1171 static struct file *open_local_file( struct makefile *make, const char *path, char **filename )
1173 char *src_path = root_dir_path( base_dir_path( make, path ));
1174 struct file *ret = load_file( src_path );
1176 /* if not found, try parent dir */
1177 if (!ret && make->parent_dir)
1179 free( src_path );
1180 path = strmake( "%s/%s", make->parent_dir, path );
1181 src_path = root_dir_path( base_dir_path( make, path ));
1182 ret = load_file( src_path );
1185 if (ret) *filename = src_dir_path( make, path );
1186 free( src_path );
1187 return ret;
1191 /*******************************************************************
1192 * open_global_file
1194 * Open a file in the top-level source directory.
1196 static struct file *open_global_file( struct makefile *make, const char *path, char **filename )
1198 char *src_path = root_dir_path( path );
1199 struct file *ret = load_file( src_path );
1201 if (ret) *filename = top_dir_path( make, path );
1202 free( src_path );
1203 return ret;
1207 /*******************************************************************
1208 * open_global_header
1210 * Open a file in the global include source directory.
1212 static struct file *open_global_header( struct makefile *make, const char *path, char **filename )
1214 return open_global_file( make, strmake( "include/%s", path ), filename );
1218 /*******************************************************************
1219 * open_src_file
1221 static struct file *open_src_file( struct makefile *make, struct incl_file *pFile )
1223 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1225 if (!file) fatal_perror( "open %s", pFile->name );
1226 return file;
1230 /*******************************************************************
1231 * open_include_file
1233 static struct file *open_include_file( struct makefile *make, struct incl_file *pFile )
1235 struct file *file = NULL;
1236 char *filename, *p;
1237 unsigned int i, len;
1239 errno = ENOENT;
1241 /* check for generated bison header */
1243 if (strendswith( pFile->name, ".tab.h" ) &&
1244 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1246 pFile->sourcename = filename;
1247 pFile->filename = obj_dir_path( make, pFile->name );
1248 return file;
1251 /* check for corresponding idl file in source dir */
1253 if (strendswith( pFile->name, ".h" ) &&
1254 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1256 pFile->sourcename = filename;
1257 pFile->filename = obj_dir_path( make, pFile->name );
1258 return file;
1261 /* now try in source dir */
1262 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1264 /* check for corresponding idl file in global includes */
1266 if (strendswith( pFile->name, ".h" ) &&
1267 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1269 pFile->sourcename = filename;
1270 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1271 return file;
1274 /* check for corresponding .in file in global includes (for config.h.in) */
1276 if (strendswith( pFile->name, ".h" ) &&
1277 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1279 pFile->sourcename = filename;
1280 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1281 return file;
1284 /* check for corresponding .x file in global includes */
1286 if (strendswith( pFile->name, "tmpl.h" ) &&
1287 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1289 pFile->sourcename = filename;
1290 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1291 return file;
1294 /* check in global includes source dir */
1296 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1298 /* check in global msvcrt includes */
1299 if (make->use_msvcrt &&
1300 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1301 return file;
1303 /* now search in include paths */
1304 for (i = 0; i < make->include_args.count; i++)
1306 const char *dir = make->include_args.str[i] + 2; /* skip -I */
1307 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1309 if (prefix)
1311 len = strlen( prefix );
1312 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1314 while (dir[len] == '/') len++;
1315 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1316 if (file) return file;
1318 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1320 if (*dir != '/')
1322 if ((file = open_file( make, concat_paths( dir, pFile->name ), &pFile->filename )))
1323 return file;
1326 if (pFile->system) return NULL; /* ignore system files we cannot find */
1328 /* try in src file directory */
1329 if ((p = strrchr(pFile->included_by->filename, '/')))
1331 int l = p - pFile->included_by->filename + 1;
1332 filename = xmalloc(l + strlen(pFile->name) + 1);
1333 memcpy( filename, pFile->included_by->filename, l );
1334 strcpy( filename + l, pFile->name );
1335 if ((file = open_file( make, filename, &pFile->filename ))) return file;
1336 free( filename );
1339 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1340 perror( pFile->name );
1341 pFile = pFile->included_by;
1342 while (pFile && pFile->included_by)
1344 const char *parent = pFile->included_by->sourcename;
1345 if (!parent) parent = pFile->included_by->file->name;
1346 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1347 parent, pFile->included_line, pFile->name );
1348 pFile = pFile->included_by;
1350 exit(1);
1354 /*******************************************************************
1355 * add_all_includes
1357 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1359 unsigned int i;
1361 parent->files_count = 0;
1362 parent->files_size = file->deps_count;
1363 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1364 for (i = 0; i < file->deps_count; i++)
1366 switch (file->deps[i].type)
1368 case INCL_NORMAL:
1369 case INCL_IMPORT:
1370 add_include( make, parent, file->deps[i].name, file->deps[i].line, 0 );
1371 break;
1372 case INCL_SYSTEM:
1373 add_include( make, parent, file->deps[i].name, file->deps[i].line, 1 );
1374 break;
1375 case INCL_CPP_QUOTE:
1376 case INCL_CPP_QUOTE_SYSTEM:
1377 break;
1383 /*******************************************************************
1384 * parse_file
1386 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1388 struct file *file;
1390 /* don't try to open certain types of files */
1391 if (strendswith( source->name, ".tlb" ))
1393 source->filename = obj_dir_path( make, source->name );
1394 return;
1397 file = src ? open_src_file( make, source ) : open_include_file( make, source );
1398 if (!file) return;
1400 source->file = file;
1401 source->files_count = 0;
1402 source->files_size = file->deps_count;
1403 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1405 if (source->sourcename)
1407 if (strendswith( source->sourcename, ".idl" ))
1409 unsigned int i;
1411 /* generated .h file always includes these */
1412 add_include( make, source, "rpc.h", 0, 1 );
1413 add_include( make, source, "rpcndr.h", 0, 1 );
1414 for (i = 0; i < file->deps_count; i++)
1416 switch (file->deps[i].type)
1418 case INCL_IMPORT:
1419 if (strendswith( file->deps[i].name, ".idl" ))
1420 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1421 file->deps[i].line, 0 );
1422 else
1423 add_include( make, source, file->deps[i].name, file->deps[i].line, 0 );
1424 break;
1425 case INCL_CPP_QUOTE:
1426 add_include( make, source, file->deps[i].name, file->deps[i].line, 0 );
1427 break;
1428 case INCL_CPP_QUOTE_SYSTEM:
1429 add_include( make, source, file->deps[i].name, file->deps[i].line, 1 );
1430 break;
1431 case INCL_NORMAL:
1432 case INCL_SYSTEM:
1433 break;
1436 return;
1438 if (strendswith( source->sourcename, ".y" ))
1439 return; /* generated .tab.h doesn't include anything */
1442 add_all_includes( make, source, file );
1446 /*******************************************************************
1447 * add_src_file
1449 * Add a source file to the list.
1451 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1453 struct incl_file *file;
1455 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1456 file = xmalloc( sizeof(*file) );
1457 memset( file, 0, sizeof(*file) );
1458 file->name = xstrdup(name);
1459 list_add_tail( &make->sources, &file->entry );
1460 parse_file( make, file, 1 );
1461 return file;
1465 /*******************************************************************
1466 * open_input_makefile
1468 static FILE *open_input_makefile( struct makefile *make )
1470 FILE *ret;
1472 if (make->base_dir)
1473 input_file_name = base_dir_path( make, input_makefile_name );
1474 else
1475 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1477 input_line = 0;
1478 if (!(ret = fopen( input_file_name, "r" )))
1480 input_file_name = root_dir_path( input_file_name );
1481 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1483 return ret;
1487 /*******************************************************************
1488 * get_make_variable
1490 static char *get_make_variable( struct makefile *make, const char *name )
1492 char *ret;
1494 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1495 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1496 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1497 return NULL;
1501 /*******************************************************************
1502 * get_expanded_make_variable
1504 static char *get_expanded_make_variable( struct makefile *make, const char *name )
1506 char *p, *end, *var, *expand, *tmp;
1508 expand = get_make_variable( make, name );
1509 if (!expand) return NULL;
1511 p = expand;
1512 while ((p = strchr( p, '$' )))
1514 if (p[1] == '(')
1516 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1517 *end++ = 0;
1518 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1519 var = get_make_variable( make, p + 2 );
1520 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1521 free( var );
1522 /* switch to the new string */
1523 p = tmp + (p - expand);
1524 free( expand );
1525 expand = tmp;
1527 else if (p[1] == '{') /* don't expand ${} variables */
1529 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1530 p = end + 1;
1532 else if (p[1] == '$')
1534 p += 2;
1536 else fatal_error( "syntax error in '%s'\n", expand );
1539 /* consider empty variables undefined */
1540 p = expand;
1541 while (*p && isspace(*p)) p++;
1542 if (*p) return expand;
1543 free( expand );
1544 return NULL;
1548 /*******************************************************************
1549 * get_expanded_make_var_array
1551 static struct strarray get_expanded_make_var_array( struct makefile *make, const char *name )
1553 struct strarray ret = empty_strarray;
1554 char *value, *token;
1556 if ((value = get_expanded_make_variable( make, name )))
1557 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1558 strarray_add( &ret, token );
1559 return ret;
1563 /*******************************************************************
1564 * file_local_var
1566 static char *file_local_var( const char *file, const char *name )
1568 char *p, *var;
1570 var = strmake( "%s_%s", file, name );
1571 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1572 return var;
1576 /*******************************************************************
1577 * set_make_variable
1579 static int set_make_variable( struct strarray *array, const char *assignment )
1581 char *p, *name;
1583 p = name = xstrdup( assignment );
1584 while (isalnum(*p) || *p == '_') p++;
1585 if (name == p) return 0; /* not a variable */
1586 if (isspace(*p))
1588 *p++ = 0;
1589 while (isspace(*p)) p++;
1591 if (*p != '=') return 0; /* not an assignment */
1592 *p++ = 0;
1593 while (isspace(*p)) p++;
1595 strarray_set_value( array, name, p );
1596 return 1;
1600 /*******************************************************************
1601 * parse_makefile
1603 static struct makefile *parse_makefile( const char *path, const char *separator )
1605 char *buffer;
1606 FILE *file;
1607 struct makefile *make = xmalloc( sizeof(*make) );
1609 memset( make, 0, sizeof(*make) );
1610 if (path)
1612 make->top_obj_dir = get_relative_path( path, "" );
1613 make->base_dir = path;
1614 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1617 file = open_input_makefile( make );
1618 while ((buffer = get_line( file )))
1620 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1621 if (*buffer == '\t') continue; /* command */
1622 while (isspace( *buffer )) buffer++;
1623 if (*buffer == '#') continue; /* comment */
1624 set_make_variable( &make->vars, buffer );
1626 fclose( file );
1627 input_file_name = NULL;
1628 return make;
1632 /*******************************************************************
1633 * add_generated_sources
1635 static void add_generated_sources( struct makefile *make )
1637 struct incl_file *source, *next, *file;
1639 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1641 if (source->file->flags & FLAG_IDL_CLIENT)
1643 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1644 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1645 add_all_includes( make, file, file->file );
1647 if (source->file->flags & FLAG_IDL_SERVER)
1649 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1650 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1651 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1652 add_all_includes( make, file, file->file );
1654 if (source->file->flags & FLAG_IDL_IDENT)
1656 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1657 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1658 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1659 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1660 add_all_includes( make, file, file->file );
1662 if (source->file->flags & FLAG_IDL_PROXY)
1664 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1665 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1666 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1667 add_all_includes( make, file, file->file );
1668 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1669 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1670 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1671 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1672 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1673 add_all_includes( make, file, file->file );
1675 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1677 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1679 if (source->file->flags & FLAG_IDL_REGISTER)
1681 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1683 if (strendswith( source->name, ".y" ))
1685 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1686 /* steal the includes list from the source file */
1687 file->files_count = source->files_count;
1688 file->files_size = source->files_size;
1689 file->files = source->files;
1690 source->files_count = source->files_size = 0;
1691 source->files = NULL;
1693 if (strendswith( source->name, ".l" ))
1695 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1696 /* steal the includes list from the source file */
1697 file->files_count = source->files_count;
1698 file->files_size = source->files_size;
1699 file->files = source->files;
1700 source->files_count = source->files_size = 0;
1701 source->files = NULL;
1704 if (get_make_variable( make, "TESTDLL" ))
1706 file = add_generated_source( make, "testlist.o", "testlist.c" );
1707 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1708 add_all_includes( make, file, file->file );
1713 /*******************************************************************
1714 * create_dir
1716 static void create_dir( const char *dir )
1718 char *p, *path;
1720 p = path = xstrdup( dir );
1721 while ((p = strchr( p, '/' )))
1723 *p = 0;
1724 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1725 *p++ = '/';
1726 while (*p == '/') p++;
1728 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1729 free( path );
1733 /*******************************************************************
1734 * output_filenames_obj_dir
1736 static void output_filenames_obj_dir( struct makefile *make, struct strarray array )
1738 unsigned int i;
1740 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1744 /*******************************************************************
1745 * output_include
1747 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1749 int i;
1751 if (pFile->owner == owner) return;
1752 if (!pFile->filename) return;
1753 pFile->owner = owner;
1754 output_filename( pFile->filename );
1755 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1759 /*******************************************************************
1760 * output_sources
1762 static struct strarray output_sources( struct makefile *make, struct strarray *testlist_files )
1764 struct incl_file *source;
1765 unsigned int i;
1766 struct strarray object_files = empty_strarray;
1767 struct strarray crossobj_files = empty_strarray;
1768 struct strarray res_files = empty_strarray;
1769 struct strarray clean_files = empty_strarray;
1770 struct strarray po_files = empty_strarray;
1771 struct strarray mo_files = empty_strarray;
1772 struct strarray mc_files = empty_strarray;
1773 struct strarray ok_files = empty_strarray;
1774 struct strarray dlldata_files = empty_strarray;
1775 struct strarray c2man_files = empty_strarray;
1776 struct strarray implib_objs = empty_strarray;
1777 struct strarray includes = empty_strarray;
1778 struct strarray subdirs = empty_strarray;
1779 struct strarray phony_targets = empty_strarray;
1780 struct strarray all_targets = empty_strarray;
1781 char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
1782 char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
1784 for (i = 0; i < linguas.count; i++)
1785 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
1787 strarray_add( &phony_targets, "all" );
1788 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
1789 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
1790 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
1791 if (make->top_obj_dir) strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
1792 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
1793 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
1794 strarray_addall( &includes, make->include_args );
1796 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
1798 struct strarray extradefs;
1799 char *obj = xstrdup( source->name );
1800 char *ext = get_extension( obj );
1802 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1803 *ext++ = 0;
1805 if (make->src_dir && strchr( obj, '/' ))
1807 char *subdir = base_dir_path( make, obj );
1808 *strrchr( subdir, '/' ) = 0;
1809 strarray_add_uniq( &subdirs, subdir );
1812 extradefs = get_expanded_make_var_array( make, file_local_var( obj, "EXTRADEFS" ));
1814 if (!strcmp( ext, "y" )) /* yacc file */
1816 /* add source file dependency for parallel makes */
1817 char *header = strmake( "%s.tab.h", obj );
1819 if (find_include_file( make, header ))
1821 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
1822 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1823 obj, obj_dir_path( make, obj ), source->filename );
1824 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
1825 source->filename, obj_dir_path( make, header ));
1826 strarray_add( &clean_files, header );
1828 else output( "%s.tab.c: %s\n", obj, source->filename );
1830 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1831 continue; /* no dependencies */
1833 else if (!strcmp( ext, "x" )) /* template file */
1835 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
1836 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1837 output( "\t%s%s -H -o $@ %s\n",
1838 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1839 strarray_add( &clean_files, strmake( "%s.h", obj ));
1840 continue; /* no dependencies */
1842 else if (!strcmp( ext, "l" )) /* lex file */
1844 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
1845 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1846 continue; /* no dependencies */
1848 else if (!strcmp( ext, "rc" )) /* resource file */
1850 strarray_add( &res_files, strmake( "%s.res", obj ));
1851 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1852 tools_path( make, "wrc" ), source->filename );
1853 output( "\t%s -o $@", tools_path( make, "wrc" ) );
1854 if (make->is_win16) output_filename( "-m16" );
1855 else output_filenames( target_flags );
1856 output_filename( "--nostdinc" );
1857 output_filenames( includes );
1858 output_filenames( make->define_args );
1859 output_filenames( extradefs );
1860 if (mo_files.count && (source->file->flags & FLAG_RC_PO))
1862 strarray_add( &po_files, source->filename );
1863 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1864 output_filename( source->filename );
1865 output( "\n" );
1866 output( "%s.res:", obj_dir_path( make, obj ));
1867 output_filenames( mo_files );
1868 output( "\n" );
1869 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
1871 else
1873 output_filename( source->filename );
1874 output( "\n" );
1876 output( "%s.res:", obj_dir_path( make, obj ));
1878 else if (!strcmp( ext, "mc" )) /* message file */
1880 strarray_add( &res_files, strmake( "%s.res", obj ));
1881 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1882 tools_path( make, "wmc" ), source->filename );
1883 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
1884 if (mo_files.count)
1886 strarray_add( &mc_files, source->filename );
1887 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1888 output( "\n" );
1889 output( "%s.res:", obj_dir_path( make, obj ));
1890 output_filenames( mo_files );
1891 output( "\n" );
1892 output( "%s ", obj_dir_path( make, "msg.pot" ));
1894 else output( "\n" );
1895 output( "%s.res:", obj_dir_path( make, obj ));
1897 else if (!strcmp( ext, "idl" )) /* IDL file */
1899 struct strarray targets = empty_strarray;
1900 char *dest;
1902 if (!source->file->flags || find_include_file( make, strmake( "%s.h", obj )))
1903 source->file->flags |= FLAG_IDL_HEADER;
1905 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1907 if (!(source->file->flags & idl_outputs[i].flag)) continue;
1908 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1909 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
1910 strarray_add( &targets, dest );
1912 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1913 output_filenames_obj_dir( make, targets );
1914 output( ": %s\n", tools_path( make, "widl" ));
1915 output( "\t%s -o $@", tools_path( make, "widl" ) );
1916 output_filenames( target_flags );
1917 output_filenames( includes );
1918 output_filenames( make->define_args );
1919 output_filenames( extradefs );
1920 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
1921 output_filename( source->filename );
1922 output( "\n" );
1923 output_filenames_obj_dir( make, targets );
1924 output( ": %s", source->filename );
1926 else if (!strcmp( ext, "in" )) /* .in file or man page */
1928 if (strendswith( obj, ".man" ) && source->file->args)
1930 char *dir, *dest = replace_extension( obj, ".man", "" );
1931 char *lang = strchr( dest, '.' );
1932 char *section = source->file->args;
1933 if (lang)
1935 *lang++ = 0;
1936 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1938 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1939 output( "install-man-pages:: %s\n", obj_dir_path( make, obj ));
1940 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj_dir_path( make, obj ), dir, dest, section );
1941 output( "uninstall::\n" );
1942 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1943 free( dest );
1944 free( dir );
1945 strarray_add( &all_targets, xstrdup(obj) );
1946 strarray_add_uniq( &phony_targets, "install-man-pages" );
1947 strarray_add_uniq( &phony_targets, "uninstall" );
1949 else strarray_add( &clean_files, xstrdup(obj) );
1950 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
1951 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1952 output( "%s:", obj_dir_path( make, obj ));
1954 else if (!strcmp( ext, "sfd" )) /* font file */
1956 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
1957 if (fontforge && !make->src_dir)
1959 output( "%s: %s\n", ttf_file, source->filename );
1960 output( "\t%s -script %s %s $@\n",
1961 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
1962 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1964 if (source->file->flags & FLAG_INSTALL)
1966 output( "install install-lib::\n" );
1967 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1968 output( "uninstall::\n" );
1969 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1971 if (source->file->flags & FLAG_SFD_FONTS)
1973 struct strarray *array = source->file->args;
1975 for (i = 0; i < array->count; i++)
1977 char *font = strtok( xstrdup(array->str[i]), " \t" );
1978 char *args = strtok( NULL, "" );
1980 strarray_add( &all_targets, xstrdup( font ));
1981 output( "%s: %s %s\n", obj_dir_path( make, font ),
1982 tools_path( make, "sfnt2fon" ), ttf_file );
1983 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
1984 output( "install install-lib:: %s\n", font );
1985 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n",
1986 obj_dir_path( make, font ), font );
1987 output( "uninstall::\n" );
1988 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1991 if (source->file->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1993 strarray_add_uniq( &phony_targets, "install" );
1994 strarray_add_uniq( &phony_targets, "install-lib" );
1995 strarray_add_uniq( &phony_targets, "uninstall" );
1997 continue; /* no dependencies */
1999 else if (!strcmp( ext, "svg" )) /* svg file */
2001 if (convert && rsvg && icotool && !make->src_dir)
2003 output( "%s.ico %s.bmp: %s\n",
2004 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
2005 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2006 top_dir_path( make, "tools/buildimage" ), source->filename );
2008 continue; /* no dependencies */
2010 else if (!strcmp( ext, "res" ))
2012 strarray_add( &res_files, source->name );
2013 continue; /* no dependencies */
2015 else
2017 int need_cross = make->testdll ||
2018 (source->file->flags & FLAG_C_IMPLIB) ||
2019 (make->module && make->staticlib);
2021 if ((source->file->flags & FLAG_GENERATED) &&
2022 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2023 strarray_add( &clean_files, source->filename );
2024 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
2025 strarray_add( &object_files, strmake( "%s.o", obj ));
2026 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2027 output( "\t$(CC) -c -o $@ %s", source->filename );
2028 output_filenames( includes );
2029 output_filenames( make->define_args );
2030 output_filenames( extradefs );
2031 if (make->module || make->staticlib || make->testdll)
2033 output_filenames( dll_flags );
2034 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2036 output_filenames( extra_cflags );
2037 output_filenames( cpp_flags );
2038 output_filename( "$(CFLAGS)" );
2039 output( "\n" );
2040 if (crosstarget && need_cross)
2042 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
2043 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2044 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2045 output_filenames( includes );
2046 output_filenames( make->define_args );
2047 output_filenames( extradefs );
2048 output_filename( "-DWINE_CROSSTEST" );
2049 output_filenames( cpp_flags );
2050 output_filename( "$(CFLAGS)" );
2051 output( "\n" );
2053 if (make->testdll && !strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2055 strarray_add( &ok_files, strmake( "%s.ok", obj ));
2056 output( "%s.ok:\n", obj_dir_path( make, obj ));
2057 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2058 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
2059 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
2061 if (!strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2062 strarray_add( &c2man_files, source->filename );
2063 output( "%s.o", obj_dir_path( make, obj ));
2064 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2065 output( ":" );
2067 free( obj );
2069 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
2070 output( "\n" );
2073 /* rules for files that depend on multiple sources */
2075 if (po_files.count)
2077 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
2078 output_filenames( po_files );
2079 output( "\n" );
2080 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
2081 if (make->is_win16) output_filename( "-m16" );
2082 else output_filenames( target_flags );
2083 output_filename( "--nostdinc" );
2084 output_filenames( includes );
2085 output_filenames( make->define_args );
2086 output_filenames( po_files );
2087 output( "\n" );
2088 strarray_add( &clean_files, "rsrc.pot" );
2091 if (mc_files.count)
2093 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
2094 output_filenames( mc_files );
2095 output( "\n" );
2096 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
2097 output_filenames( mc_files );
2098 output( "\n" );
2099 strarray_add( &clean_files, "msg.pot" );
2102 if (dlldata_files.count)
2104 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
2105 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
2106 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
2107 output_filenames( dlldata_files );
2108 output( "\n" );
2111 if (make->module && !make->staticlib)
2113 struct strarray all_libs = empty_strarray;
2114 char *module_path = obj_dir_path( make, make->module );
2115 char *spec_file = NULL;
2117 if (!make->appmode.count)
2118 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2119 for (i = 0; i < make->delayimports.count; i++)
2120 strarray_add( &all_libs, strmake( "-l%s", make->delayimports.str[i] ));
2121 for (i = 0; i < make->imports.count; i++)
2122 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2123 for (i = 0; i < make->delayimports.count; i++)
2124 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2125 strarray_add( &all_libs, "-lwine" );
2126 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2127 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2128 strarray_addall( &all_libs, libs );
2130 if (*dll_ext)
2132 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
2133 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
2134 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2136 else
2138 strarray_add( &all_targets, make->module );
2139 output( "%s:", module_path );
2141 if (spec_file) output_filename( spec_file );
2142 output_filenames_obj_dir( make, object_files );
2143 output_filenames_obj_dir( make, res_files );
2144 output( "\n" );
2145 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2146 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2147 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2148 output_filenames( target_flags );
2149 output_filenames( unwind_flags );
2150 if (spec_file)
2152 output( " -shared %s", spec_file );
2153 output_filenames( make->extradllflags );
2155 else output_filenames( make->appmode );
2156 output_filenames_obj_dir( make, object_files );
2157 output_filenames_obj_dir( make, res_files );
2158 output_filenames( all_libs );
2159 output_filename( "$(LDFLAGS)" );
2160 output( "\n" );
2162 if (spec_file && make->importlib)
2164 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2165 if (*dll_ext)
2167 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
2168 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2169 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2170 output_filenames( target_flags );
2171 if (make->is_win16) output_filename( "-m16" );
2172 output( "\n" );
2173 if (implib_objs.count)
2175 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
2176 output( "%s.def.a:", importlib_path );
2177 output_filenames_obj_dir( make, implib_objs );
2178 output( "\n" );
2179 output( "\t$(RM) $@\n" );
2180 output( "\t$(AR) $(ARFLAGS) $@" );
2181 output_filenames_obj_dir( make, implib_objs );
2182 output( "\n" );
2183 output( "\t$(RANLIB) $@\n" );
2186 else
2188 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
2189 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2190 output_filenames_obj_dir( make, implib_objs );
2191 output( "\n" );
2192 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2193 output_filenames( target_flags );
2194 output_filenames_obj_dir( make, implib_objs );
2195 output( "\n" );
2197 if (crosstarget && !make->is_win16)
2199 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2200 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2201 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2202 output_filenames_obj_dir( make, cross_files );
2203 output( "\n" );
2204 output( "\t%s -b %s -w --implib -o $@ --export %s",
2205 tools_path( make, "winebuild" ), crosstarget, spec_file );
2206 output_filenames_obj_dir( make, cross_files );
2207 output( "\n" );
2211 if (spec_file)
2213 if (c2man_files.count)
2215 output( "manpages::\n" );
2216 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2217 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2218 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2219 output_filename( strmake( "-o %s/man%s",
2220 top_obj_dir_path( make, "documentation" ), man_ext ));
2221 output_filenames( c2man_files );
2222 output( "\n" );
2223 output( "htmlpages::\n" );
2224 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2225 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2226 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2227 output_filename( strmake( "-o %s",
2228 top_obj_dir_path( make, "documentation/html" )));
2229 output_filenames( c2man_files );
2230 output( "\n" );
2231 output( "sgmlpages::\n" );
2232 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2233 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2234 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2235 output_filename( strmake( "-o %s",
2236 top_obj_dir_path( make, "documentation/api-guide" )));
2237 output_filenames( c2man_files );
2238 output( "\n" );
2239 output( "xmlpages::\n" );
2240 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2241 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2242 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2243 output_filename( strmake( "-o %s",
2244 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2245 output_filenames( c2man_files );
2246 output( "\n" );
2247 strarray_add( &phony_targets, "manpages" );
2248 strarray_add( &phony_targets, "htmlpages" );
2249 strarray_add( &phony_targets, "sgmlpages" );
2250 strarray_add( &phony_targets, "xmlpages" );
2252 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2256 if (make->staticlib)
2258 strarray_add( &all_targets, make->staticlib );
2259 output( "%s:", obj_dir_path( make, make->staticlib ));
2260 output_filenames_obj_dir( make, object_files );
2261 output( "\n\t$(RM) $@\n" );
2262 output( "\t$(AR) $(ARFLAGS) $@" );
2263 output_filenames_obj_dir( make, object_files );
2264 output( "\n\t$(RANLIB) $@\n" );
2265 if (crosstarget && make->module)
2267 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2269 strarray_add( &all_targets, name );
2270 output( "%s:", obj_dir_path( make, name ));
2271 output_filenames_obj_dir( make, crossobj_files );
2272 output( "\n\t$(RM) $@\n" );
2273 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2274 output_filenames_obj_dir( make, crossobj_files );
2275 output( "\n\t%s-ranlib $@\n", crosstarget );
2279 if (make->testdll)
2281 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2282 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2283 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2284 struct strarray all_libs = empty_strarray;
2286 for (i = 0; i < make->imports.count; i++)
2287 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2288 strarray_addall( &all_libs, libs );
2290 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2291 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2292 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2293 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2294 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2295 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2296 output_filenames( target_flags );
2297 output_filenames( unwind_flags );
2298 output_filenames( make->appmode );
2299 output_filenames_obj_dir( make, object_files );
2300 output_filenames_obj_dir( make, res_files );
2301 output_filenames( all_libs );
2302 output_filename( "$(LDFLAGS)" );
2303 output( "\n" );
2304 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2305 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2306 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2307 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2308 output_filenames( target_flags );
2309 output_filenames( unwind_flags );
2310 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2311 output_filenames( make->appmode );
2312 output_filenames_obj_dir( make, object_files );
2313 output_filenames_obj_dir( make, res_files );
2314 output_filenames( all_libs );
2315 output_filename( "$(LDFLAGS)" );
2316 output( "\n" );
2317 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2318 obj_dir_path( make, stripped ), dll_ext );
2319 output_filenames_obj_dir( make, object_files );
2320 output_filenames_obj_dir( make, res_files );
2321 output( "\n" );
2323 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2324 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2325 obj_dir_path( make, stripped ), dll_ext );
2326 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2327 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2329 if (crosstarget)
2331 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2333 strarray_add( &clean_files, crosstest );
2334 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2335 output( "%s:", obj_dir_path( make, crosstest ));
2336 output_filenames_obj_dir( make, crossobj_files );
2337 output_filenames_obj_dir( make, res_files );
2338 output( "\n" );
2339 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2340 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2341 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2342 output_filename( "--lib-suffix=.cross.a" );
2343 output_filenames_obj_dir( make, crossobj_files );
2344 output_filenames_obj_dir( make, res_files );
2345 output_filenames( all_libs );
2346 output_filename( "$(LDFLAGS)" );
2347 output( "\n" );
2348 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2349 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2352 output_filenames_obj_dir( make, ok_files );
2353 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2354 output( "check test:" );
2355 output_filenames_obj_dir( make, ok_files );
2356 output( "\n" );
2357 output( "testclean::\n" );
2358 output( "\t$(RM)" );
2359 output_filenames_obj_dir( make, ok_files );
2360 output( "\n" );
2361 strarray_addall( &clean_files, ok_files );
2362 strarray_add( &phony_targets, "check" );
2363 strarray_add( &phony_targets, "test" );
2364 strarray_add( &phony_targets, "testclean" );
2365 *testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2368 for (i = 0; i < make->programs.count; i++)
2370 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
2371 struct strarray all_libs = empty_strarray;
2372 struct strarray objs = get_expanded_make_var_array( make,
2373 file_local_var( make->programs.str[i], "OBJS" ));
2375 if (!objs.count) objs = object_files;
2376 output( "%s:", obj_dir_path( make, program ) );
2377 output_filenames_obj_dir( make, objs );
2378 output( "\n" );
2379 output( "\t$(CC) -o $@" );
2380 output_filenames_obj_dir( make, objs );
2381 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2382 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2383 strarray_addall( &all_libs, libs );
2384 strarray_addall( &all_libs, get_expanded_make_var_array( make,
2385 file_local_var( make->programs.str[i], "LDFLAGS" )));
2387 if (strarray_exists( &all_libs, "-lwine" ))
2389 strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
2390 if (ldrpath_local && ldrpath_install)
2392 char *program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
2394 output_filename( ldrpath_local );
2395 output_filenames( all_libs );
2396 output_filename( "$(LDFLAGS)" );
2397 output( "\n" );
2398 output( "%s:", obj_dir_path( make, program_installed ) );
2399 output_filenames_obj_dir( make, objs );
2400 output( "\n" );
2401 output( "\t$(CC) -o $@" );
2402 output_filenames_obj_dir( make, objs );
2403 output_filename( ldrpath_install );
2404 strarray_add( &all_targets, program_installed );
2408 output_filenames( all_libs );
2409 output_filename( "$(LDFLAGS)" );
2410 output( "\n" );
2411 strarray_add( &all_targets, program );
2414 if (all_targets.count)
2416 output( "all:" );
2417 output_filenames_obj_dir( make, all_targets );
2418 output( "\n" );
2421 strarray_addall( &clean_files, object_files );
2422 strarray_addall( &clean_files, crossobj_files );
2423 strarray_addall( &clean_files, res_files );
2424 strarray_addall( &clean_files, all_targets );
2425 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2427 if (clean_files.count)
2429 output( "%s::\n", obj_dir_path( make, "clean" ));
2430 output( "\t$(RM)" );
2431 output_filenames_obj_dir( make, clean_files );
2432 output( "\n" );
2433 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2434 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2437 if (make->top_obj_dir)
2439 output( "depend:\n" );
2440 output( "\t@cd %s && $(MAKE) %s\n", make->top_obj_dir, base_dir_path( make, "depend" ));
2441 strarray_add( &phony_targets, "depend" );
2444 if (phony_targets.count)
2446 output( ".PHONY:" );
2447 output_filenames( phony_targets );
2448 output( "\n" );
2451 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2453 return clean_files;
2457 /*******************************************************************
2458 * create_temp_file
2460 static FILE *create_temp_file( const char *orig )
2462 char *name = xmalloc( strlen(orig) + 13 );
2463 unsigned int i, id = getpid();
2464 int fd;
2465 FILE *ret = NULL;
2467 for (i = 0; i < 100; i++)
2469 sprintf( name, "%s.tmp%08x", orig, id );
2470 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2472 ret = fdopen( fd, "w" );
2473 break;
2475 if (errno != EEXIST) break;
2476 id += 7777;
2478 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2479 temp_file_name = name;
2480 return ret;
2484 /*******************************************************************
2485 * rename_temp_file
2487 static void rename_temp_file( const char *dest )
2489 int ret = rename( temp_file_name, dest );
2490 if (ret == -1 && errno == EEXIST)
2492 /* rename doesn't overwrite on windows */
2493 unlink( dest );
2494 ret = rename( temp_file_name, dest );
2496 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2497 temp_file_name = NULL;
2501 /*******************************************************************
2502 * are_files_identical
2504 static int are_files_identical( FILE *file1, FILE *file2 )
2506 for (;;)
2508 char buffer1[8192], buffer2[8192];
2509 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2510 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2511 if (size1 != size2) return 0;
2512 if (!size1) return feof( file1 ) && feof( file2 );
2513 if (memcmp( buffer1, buffer2, size1 )) return 0;
2518 /*******************************************************************
2519 * rename_temp_file_if_changed
2521 static void rename_temp_file_if_changed( const char *dest )
2523 FILE *file1, *file2;
2524 int do_rename = 1;
2526 if ((file1 = fopen( dest, "r" )))
2528 if ((file2 = fopen( temp_file_name, "r" )))
2530 do_rename = !are_files_identical( file1, file2 );
2531 fclose( file2 );
2533 fclose( file1 );
2535 if (!do_rename)
2537 unlink( temp_file_name );
2538 temp_file_name = NULL;
2540 else rename_temp_file( dest );
2544 /*******************************************************************
2545 * output_testlist
2547 static void output_testlist( const char *dest, struct strarray files )
2549 int i;
2551 output_file = create_temp_file( dest );
2553 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2554 output( "#define WIN32_LEAN_AND_MEAN\n" );
2555 output( "#include <windows.h>\n\n" );
2556 output( "#define STANDALONE\n" );
2557 output( "#include \"wine/test.h\"\n\n" );
2559 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2560 output( "\n" );
2561 output( "const struct test winetest_testlist[] =\n" );
2562 output( "{\n" );
2563 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2564 output( " { 0, 0 }\n" );
2565 output( "};\n" );
2567 if (fclose( output_file )) fatal_perror( "write" );
2568 output_file = NULL;
2569 rename_temp_file_if_changed( dest );
2573 /*******************************************************************
2574 * output_gitignore
2576 static void output_gitignore( const char *dest, struct strarray files )
2578 int i;
2580 output_file = create_temp_file( dest );
2582 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2583 for (i = 0; i < files.count; i++)
2585 if (!strchr( files.str[i], '/' )) output( "/" );
2586 output( "%s\n", files.str[i] );
2589 if (fclose( output_file )) fatal_perror( "write" );
2590 output_file = NULL;
2591 rename_temp_file( dest );
2595 /*******************************************************************
2596 * output_top_variables
2598 static void output_top_variables( struct makefile *make )
2600 unsigned int i;
2601 struct strarray *vars = &top_makefile->vars;
2603 if (!make->base_dir) return; /* don't output variables in the top makefile */
2605 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
2606 output( "all:\n\n" );
2607 for (i = 0; i < vars->count; i += 2)
2608 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
2609 output( "\n" );
2613 /*******************************************************************
2614 * output_dependencies
2616 static void output_dependencies( struct makefile *make )
2618 struct strarray targets, testlist_files = empty_strarray, ignore_files = empty_strarray;
2619 char buffer[1024];
2620 FILE *src_file;
2622 output_file_name = base_dir_path( make, output_makefile_name );
2623 output_file = create_temp_file( output_file_name );
2624 output_top_variables( make );
2626 /* copy the contents of the source makefile */
2627 src_file = open_input_makefile( make );
2628 while (fgets( buffer, sizeof(buffer), src_file ))
2629 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
2630 if (fclose( src_file )) fatal_perror( "close" );
2631 input_file_name = NULL;
2633 targets = output_sources( make, &testlist_files );
2635 fclose( output_file );
2636 output_file = NULL;
2637 rename_temp_file( output_file_name );
2639 strarray_add( &ignore_files, ".gitignore" );
2640 strarray_add( &ignore_files, "Makefile" );
2641 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2642 strarray_addall( &ignore_files, targets );
2644 if (testlist_files.count)
2645 output_testlist( base_dir_path( make, "testlist.c" ), testlist_files );
2646 if (!make->src_dir && make->base_dir)
2647 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
2649 output_file_name = NULL;
2653 /*******************************************************************
2654 * update_makefile
2656 static void update_makefile( const char *path )
2658 static const char *source_vars[] =
2660 "C_SRCS",
2661 "OBJC_SRCS",
2662 "RC_SRCS",
2663 "MC_SRCS",
2664 "IDL_SRCS",
2665 "BISON_SRCS",
2666 "LEX_SRCS",
2667 "XTEMPLATE_SRCS",
2668 "SVG_SRCS",
2669 "FONT_SRCS",
2670 "IN_SRCS",
2671 "MANPAGES",
2672 NULL
2674 const char **var;
2675 unsigned int i;
2676 struct strarray value;
2677 struct incl_file *file;
2678 struct makefile *make;
2680 make = parse_makefile( path, NULL );
2682 if (root_src_dir)
2684 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
2685 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
2687 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
2688 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
2689 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
2691 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
2692 make->module = get_expanded_make_variable( make, "MODULE" );
2693 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
2694 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
2695 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
2697 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
2698 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
2699 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
2700 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
2701 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
2703 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
2705 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
2706 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
2708 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
2709 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 );
2711 make->include_args = empty_strarray;
2712 make->define_args = empty_strarray;
2713 strarray_add( &make->define_args, "-D__WINESRC__" );
2715 value = get_expanded_make_var_array( make, "EXTRAINCL" );
2716 for (i = 0; i < value.count; i++)
2717 if (!strncmp( value.str[i], "-I", 2 ))
2718 strarray_add_uniq( &make->include_args, value.str[i] );
2719 else
2720 strarray_add_uniq( &make->define_args, value.str[i] );
2721 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
2723 list_init( &make->sources );
2724 list_init( &make->includes );
2726 /* FIXME: target dir has to exist to allow locating srcdir-relative include files */
2727 if (make->base_dir) create_dir( make->base_dir );
2729 for (var = source_vars; *var; var++)
2731 value = get_expanded_make_var_array( make, *var );
2732 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
2735 add_generated_sources( make );
2737 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
2738 for (i = 0; i < value.count; i++)
2740 /* default to .c for unknown extra object files */
2741 if (strendswith( value.str[i], ".o" ))
2742 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2743 else
2744 add_generated_source( make, value.str[i], NULL );
2747 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
2749 output_dependencies( make );
2753 /*******************************************************************
2754 * parse_makeflags
2756 static void parse_makeflags( const char *flags )
2758 const char *p = flags;
2759 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2761 while (*p)
2763 while (isspace(*p)) p++;
2764 var = buffer;
2765 while (*p && !isspace(*p))
2767 if (*p == '\\' && p[1]) p++;
2768 *var++ = *p++;
2770 *var = 0;
2771 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2776 /*******************************************************************
2777 * parse_option
2779 static int parse_option( const char *opt )
2781 if (opt[0] != '-')
2783 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2784 return 0;
2786 switch(opt[1])
2788 case 'f':
2789 if (opt[2]) output_makefile_name = opt + 2;
2790 break;
2791 case 'i':
2792 if (opt[2]) input_makefile_name = opt + 2;
2793 break;
2794 case 'R':
2795 relative_dir_mode = 1;
2796 break;
2797 default:
2798 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2799 exit(1);
2801 return 1;
2805 /*******************************************************************
2806 * main
2808 int main( int argc, char *argv[] )
2810 const char *makeflags = getenv( "MAKEFLAGS" );
2811 int i, j;
2813 if (makeflags) parse_makeflags( makeflags );
2815 i = 1;
2816 while (i < argc)
2818 if (parse_option( argv[i] ))
2820 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2821 argc--;
2823 else i++;
2826 if (relative_dir_mode)
2828 char *relpath;
2830 if (argc != 3)
2832 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2833 exit( 1 );
2835 relpath = get_relative_path( argv[1], argv[2] );
2836 printf( "%s\n", relpath ? relpath : "." );
2837 exit( 0 );
2840 if (argc <= 1)
2842 fprintf( stderr, "%s", Usage );
2843 exit( 1 );
2845 atexit( cleanup_files );
2846 signal( SIGTERM, exit_on_signal );
2847 signal( SIGINT, exit_on_signal );
2848 #ifdef SIGHUP
2849 signal( SIGHUP, exit_on_signal );
2850 #endif
2852 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
2854 if (!input_makefile_name) input_makefile_name = strmake( "%s.in", output_makefile_name );
2856 top_makefile = parse_makefile( NULL, "# End of common header" );
2858 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
2859 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
2860 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
2861 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
2862 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
2863 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
2864 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
2865 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
2867 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
2868 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
2869 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
2870 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
2871 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
2872 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2873 dll_prefix = get_expanded_make_variable( top_makefile, "DLLPREFIX" );
2874 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
2875 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
2876 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
2877 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
2878 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
2880 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
2881 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2882 if (!exe_ext) exe_ext = "";
2883 if (!tools_ext) tools_ext = "";
2884 if (!dll_prefix) dll_prefix = "";
2885 if (!man_ext) man_ext = "3w";
2887 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2888 return 0;