d3dx9/tests: Create the asm shader test files under the temporary files directory.
[wine/multimedia.git] / tools / makedep.c
blob396a1e3a4338a9a5a5fe9028c9c0a3f811066b10
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 *makefile_name = "Makefile";
175 static const char *Separator = "### Dependencies";
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 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\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 * get_make_variable
1467 static char *get_make_variable( struct makefile *make, const char *name )
1469 char *ret;
1471 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1472 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1473 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1474 return NULL;
1478 /*******************************************************************
1479 * get_expanded_make_variable
1481 static char *get_expanded_make_variable( struct makefile *make, const char *name )
1483 char *p, *end, *var, *expand, *tmp;
1485 expand = get_make_variable( make, name );
1486 if (!expand) return NULL;
1488 p = expand;
1489 while ((p = strchr( p, '$' )))
1491 if (p[1] == '(')
1493 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1494 *end++ = 0;
1495 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1496 var = get_make_variable( make, p + 2 );
1497 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1498 free( var );
1500 else if (p[1] == '{') /* don't expand ${} variables */
1502 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1503 p = end + 1;
1504 continue;
1506 else if (p[1] == '$')
1508 tmp = replace_substr( expand, p, 2, "$" );
1510 else fatal_error( "syntax error in '%s'\n", expand );
1512 /* switch to the new string */
1513 p = tmp + (p - expand);
1514 free( expand );
1515 expand = tmp;
1518 /* consider empty variables undefined */
1519 p = expand;
1520 while (*p && isspace(*p)) p++;
1521 if (*p) return expand;
1522 free( expand );
1523 return NULL;
1527 /*******************************************************************
1528 * get_expanded_make_var_array
1530 static struct strarray get_expanded_make_var_array( struct makefile *make, const char *name )
1532 struct strarray ret = empty_strarray;
1533 char *value, *token;
1535 if ((value = get_expanded_make_variable( make, name )))
1536 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1537 strarray_add( &ret, token );
1538 return ret;
1542 /*******************************************************************
1543 * set_make_variable
1545 static int set_make_variable( struct strarray *array, const char *assignment )
1547 char *p, *name;
1549 p = name = xstrdup( assignment );
1550 while (isalnum(*p) || *p == '_') p++;
1551 if (name == p) return 0; /* not a variable */
1552 if (isspace(*p))
1554 *p++ = 0;
1555 while (isspace(*p)) p++;
1557 if (*p != '=') return 0; /* not an assignment */
1558 *p++ = 0;
1559 while (isspace(*p)) p++;
1561 strarray_set_value( array, name, p );
1562 return 1;
1566 /*******************************************************************
1567 * parse_makefile
1569 static struct makefile *parse_makefile( const char *path, const char *separator )
1571 char *buffer;
1572 FILE *file;
1573 struct makefile *make = xmalloc( sizeof(*make) );
1575 memset( make, 0, sizeof(*make) );
1576 if (path)
1578 make->top_obj_dir = get_relative_path( path, "" );
1579 make->base_dir = path;
1580 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1583 input_file_name = base_dir_path( make, makefile_name );
1584 if (!(file = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1586 input_line = 0;
1587 while ((buffer = get_line( file )))
1589 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1590 if (*buffer == '\t') continue; /* command */
1591 while (isspace( *buffer )) buffer++;
1592 if (*buffer == '#') continue; /* comment */
1593 set_make_variable( &make->vars, buffer );
1595 fclose( file );
1596 input_file_name = NULL;
1597 return make;
1601 /*******************************************************************
1602 * add_generated_sources
1604 static void add_generated_sources( struct makefile *make )
1606 struct incl_file *source, *next, *file;
1608 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1610 if (source->file->flags & FLAG_IDL_CLIENT)
1612 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1613 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1614 add_all_includes( make, file, file->file );
1616 if (source->file->flags & FLAG_IDL_SERVER)
1618 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1619 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1620 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1621 add_all_includes( make, file, file->file );
1623 if (source->file->flags & FLAG_IDL_IDENT)
1625 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1626 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1627 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1628 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1629 add_all_includes( make, file, file->file );
1631 if (source->file->flags & FLAG_IDL_PROXY)
1633 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1634 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1635 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1636 add_all_includes( make, file, file->file );
1637 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1638 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1639 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1640 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1641 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1642 add_all_includes( make, file, file->file );
1644 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1646 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1648 if (source->file->flags & FLAG_IDL_REGISTER)
1650 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1652 if (strendswith( source->name, ".y" ))
1654 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1655 /* steal the includes list from the source file */
1656 file->files_count = source->files_count;
1657 file->files_size = source->files_size;
1658 file->files = source->files;
1659 source->files_count = source->files_size = 0;
1660 source->files = NULL;
1662 if (strendswith( source->name, ".l" ))
1664 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1665 /* steal the includes list from the source file */
1666 file->files_count = source->files_count;
1667 file->files_size = source->files_size;
1668 file->files = source->files;
1669 source->files_count = source->files_size = 0;
1670 source->files = NULL;
1673 if (get_make_variable( make, "TESTDLL" ))
1675 file = add_generated_source( make, "testlist.o", "testlist.c" );
1676 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1677 add_all_includes( make, file, file->file );
1682 /*******************************************************************
1683 * create_dir
1685 static void create_dir( const char *dir )
1687 char *p, *path;
1689 p = path = xstrdup( dir );
1690 while ((p = strchr( p, '/' )))
1692 *p = 0;
1693 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1694 *p++ = '/';
1695 while (*p == '/') p++;
1697 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1698 free( path );
1702 /*******************************************************************
1703 * output_filenames_obj_dir
1705 static void output_filenames_obj_dir( struct makefile *make, struct strarray array )
1707 unsigned int i;
1709 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1713 /*******************************************************************
1714 * output_include
1716 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1718 int i;
1720 if (pFile->owner == owner) return;
1721 if (!pFile->filename) return;
1722 pFile->owner = owner;
1723 output_filename( pFile->filename );
1724 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1728 /*******************************************************************
1729 * output_sources
1731 static struct strarray output_sources( struct makefile *make, struct strarray *testlist_files )
1733 struct incl_file *source;
1734 unsigned int i;
1735 struct strarray object_files = empty_strarray;
1736 struct strarray crossobj_files = empty_strarray;
1737 struct strarray res_files = empty_strarray;
1738 struct strarray clean_files = empty_strarray;
1739 struct strarray po_files = empty_strarray;
1740 struct strarray mo_files = empty_strarray;
1741 struct strarray mc_files = empty_strarray;
1742 struct strarray ok_files = empty_strarray;
1743 struct strarray dlldata_files = empty_strarray;
1744 struct strarray c2man_files = empty_strarray;
1745 struct strarray implib_objs = empty_strarray;
1746 struct strarray includes = empty_strarray;
1747 struct strarray subdirs = empty_strarray;
1748 struct strarray phony_targets = empty_strarray;
1749 struct strarray all_targets = get_expanded_make_var_array( make, "PROGRAMS" );
1751 for (i = 0; i < linguas.count; i++)
1752 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
1754 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
1755 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
1756 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
1757 if (make->top_obj_dir) strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
1758 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
1759 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
1760 strarray_addall( &includes, make->include_args );
1762 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
1764 struct strarray extradefs;
1765 char *obj = xstrdup( source->name );
1766 char *ext = get_extension( obj );
1768 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1769 *ext++ = 0;
1771 if (make->src_dir && strchr( obj, '/' ))
1773 char *subdir = base_dir_path( make, obj );
1774 *strrchr( subdir, '/' ) = 0;
1775 strarray_add_uniq( &subdirs, subdir );
1778 extradefs = get_expanded_make_var_array( make, strmake( "%s_EXTRADEFS", obj ));
1780 if (!strcmp( ext, "y" )) /* yacc file */
1782 /* add source file dependency for parallel makes */
1783 char *header = strmake( "%s.tab.h", obj );
1785 if (find_include_file( make, header ))
1787 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
1788 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1789 obj, obj_dir_path( make, obj ), source->filename );
1790 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
1791 source->filename, obj_dir_path( make, header ));
1792 strarray_add( &clean_files, header );
1794 else output( "%s.tab.c: %s\n", obj, source->filename );
1796 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1797 continue; /* no dependencies */
1799 else if (!strcmp( ext, "x" )) /* template file */
1801 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
1802 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1803 output( "\t%s%s -H -o $@ %s\n",
1804 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1805 strarray_add( &clean_files, strmake( "%s.h", obj ));
1806 continue; /* no dependencies */
1808 else if (!strcmp( ext, "l" )) /* lex file */
1810 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
1811 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1812 continue; /* no dependencies */
1814 else if (!strcmp( ext, "rc" )) /* resource file */
1816 strarray_add( &res_files, strmake( "%s.res", obj ));
1817 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1818 tools_path( make, "wrc" ), source->filename );
1819 output( "\t%s -o $@ %s", tools_path( make, "wrc" ), source->filename );
1820 if (make->is_win16) output_filename( "-m16" );
1821 else output_filenames( target_flags );
1822 output_filename( "--nostdinc" );
1823 output_filenames( includes );
1824 output_filenames( make->define_args );
1825 output_filenames( extradefs );
1826 if (mo_files.count && (source->file->flags & FLAG_RC_PO))
1828 strarray_add( &po_files, source->filename );
1829 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1830 output( "\n" );
1831 output( "%s.res:", obj_dir_path( make, obj ));
1832 output_filenames( mo_files );
1833 output( "\n" );
1834 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
1836 else output( "\n" );
1837 output( "%s.res:", obj_dir_path( make, obj ));
1839 else if (!strcmp( ext, "mc" )) /* message file */
1841 strarray_add( &res_files, strmake( "%s.res", obj ));
1842 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1843 tools_path( make, "wmc" ), source->filename );
1844 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
1845 if (mo_files.count)
1847 strarray_add( &mc_files, source->filename );
1848 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1849 output( "\n" );
1850 output( "%s.res:", obj_dir_path( make, obj ));
1851 output_filenames( mo_files );
1852 output( "\n" );
1853 output( "%s ", obj_dir_path( make, "msg.pot" ));
1855 else output( "\n" );
1856 output( "%s.res:", obj_dir_path( make, obj ));
1858 else if (!strcmp( ext, "idl" )) /* IDL file */
1860 struct strarray targets = empty_strarray;
1861 char *dest;
1863 if (!source->file->flags || find_include_file( make, strmake( "%s.h", obj )))
1864 source->file->flags |= FLAG_IDL_HEADER;
1866 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1868 if (!(source->file->flags & idl_outputs[i].flag)) continue;
1869 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1870 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
1871 strarray_add( &targets, dest );
1873 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1874 output_filenames_obj_dir( make, targets );
1875 output( ": %s\n", tools_path( make, "widl" ));
1876 output( "\t%s -o $@ %s", tools_path( make, "widl" ), source->filename );
1877 output_filenames( target_flags );
1878 output_filenames( includes );
1879 output_filenames( make->define_args );
1880 output_filenames( extradefs );
1881 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
1882 output( "\n" );
1883 output_filenames_obj_dir( make, targets );
1884 output( ": %s", source->filename );
1886 else if (!strcmp( ext, "in" )) /* .in file or man page */
1888 if (strendswith( obj, ".man" ) && source->file->args)
1890 char *dir, *dest = replace_extension( obj, ".man", "" );
1891 char *lang = strchr( dest, '.' );
1892 char *section = source->file->args;
1893 if (lang)
1895 *lang++ = 0;
1896 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1898 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1899 output( "install-man-pages:: %s\n", obj_dir_path( make, obj ));
1900 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj_dir_path( make, obj ), dir, dest, section );
1901 output( "uninstall::\n" );
1902 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1903 free( dest );
1904 free( dir );
1905 strarray_add( &all_targets, xstrdup(obj) );
1906 strarray_add_uniq( &phony_targets, "install-man-pages" );
1907 strarray_add_uniq( &phony_targets, "uninstall" );
1909 else strarray_add( &clean_files, xstrdup(obj) );
1910 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
1911 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1912 output( "%s:", obj_dir_path( make, obj ));
1914 else if (!strcmp( ext, "sfd" )) /* font file */
1916 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
1917 if (fontforge && !make->src_dir)
1919 output( "%s: %s\n", ttf_file, source->filename );
1920 output( "\t%s -script %s %s $@\n",
1921 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
1922 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1924 if (source->file->flags & FLAG_INSTALL)
1926 output( "install install-lib::\n" );
1927 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1928 output( "uninstall::\n" );
1929 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1931 if (source->file->flags & FLAG_SFD_FONTS)
1933 struct strarray *array = source->file->args;
1935 for (i = 0; i < array->count; i++)
1937 char *font = strtok( xstrdup(array->str[i]), " \t" );
1938 char *args = strtok( NULL, "" );
1940 strarray_add( &all_targets, xstrdup( font ));
1941 output( "%s: %s %s\n", obj_dir_path( make, font ),
1942 tools_path( make, "sfnt2fon" ), ttf_file );
1943 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
1944 output( "install install-lib:: %s\n", font );
1945 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n",
1946 obj_dir_path( make, font ), font );
1947 output( "uninstall::\n" );
1948 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1951 if (source->file->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1953 strarray_add_uniq( &phony_targets, "install" );
1954 strarray_add_uniq( &phony_targets, "install-lib" );
1955 strarray_add_uniq( &phony_targets, "uninstall" );
1957 continue; /* no dependencies */
1959 else if (!strcmp( ext, "svg" )) /* svg file */
1961 if (convert && rsvg && icotool && !make->src_dir)
1963 output( "%s.ico %s.bmp: %s\n",
1964 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
1965 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
1966 top_dir_path( make, "tools/buildimage" ), source->filename );
1968 continue; /* no dependencies */
1970 else if (!strcmp( ext, "res" ))
1972 strarray_add( &res_files, source->name );
1973 continue; /* no dependencies */
1975 else
1977 int need_cross = make->testdll ||
1978 (source->file->flags & FLAG_C_IMPLIB) ||
1979 (make->module && make->staticlib);
1981 if ((source->file->flags & FLAG_GENERATED) &&
1982 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
1983 strarray_add( &clean_files, source->filename );
1984 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1985 strarray_add( &object_files, strmake( "%s.o", obj ));
1986 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
1987 output( "\t$(CC) -c -o $@ %s", source->filename );
1988 output_filenames( includes );
1989 output_filenames( make->define_args );
1990 output_filenames( extradefs );
1991 if (make->module || make->staticlib || make->testdll)
1993 output_filenames( dll_flags );
1994 if (make->use_msvcrt) output_filenames( msvcrt_flags );
1996 output_filenames( extra_cflags );
1997 output_filenames( cpp_flags );
1998 output_filename( "$(CFLAGS)" );
1999 output( "\n" );
2000 if (crosstarget && need_cross)
2002 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
2003 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2004 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2005 output_filenames( includes );
2006 output_filenames( make->define_args );
2007 output_filenames( extradefs );
2008 output_filename( "-DWINE_CROSSTEST" );
2009 output_filenames( cpp_flags );
2010 output_filename( "$(CFLAGS)" );
2011 output( "\n" );
2013 if (make->testdll && !strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2015 strarray_add( &ok_files, strmake( "%s.ok", obj ));
2016 output( "%s.ok:\n", obj_dir_path( make, obj ));
2017 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2018 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
2019 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
2021 if (!strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2022 strarray_add( &c2man_files, source->filename );
2023 output( "%s.o", obj_dir_path( make, obj ));
2024 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2025 output( ":" );
2027 free( obj );
2029 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
2030 output( "\n" );
2033 /* rules for files that depend on multiple sources */
2035 if (po_files.count)
2037 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
2038 output_filenames( po_files );
2039 output( "\n" );
2040 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
2041 output_filenames( po_files );
2042 if (make->is_win16) output_filename( "-m16" );
2043 else output_filenames( target_flags );
2044 output_filename( "--nostdinc" );
2045 output_filenames( includes );
2046 output_filenames( make->define_args );
2047 output( "\n" );
2048 strarray_add( &clean_files, "rsrc.pot" );
2051 if (mc_files.count)
2053 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
2054 output_filenames( mc_files );
2055 output( "\n" );
2056 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
2057 output_filenames( mc_files );
2058 output( "\n" );
2059 strarray_add( &clean_files, "msg.pot" );
2062 if (dlldata_files.count)
2064 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
2065 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
2066 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
2067 output_filenames( dlldata_files );
2068 output( "\n" );
2071 if (make->module && !make->staticlib)
2073 struct strarray all_libs = empty_strarray;
2074 char *module_path = obj_dir_path( make, make->module );
2075 char *spec_file = NULL;
2077 if (!make->appmode.count)
2078 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2079 for (i = 0; i < make->delayimports.count; i++)
2080 strarray_add( &all_libs, strmake( "-l%s", make->delayimports.str[i] ));
2081 for (i = 0; i < make->imports.count; i++)
2082 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2083 for (i = 0; i < make->delayimports.count; i++)
2084 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2085 strarray_add( &all_libs, "-lwine" );
2086 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2087 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2088 strarray_addall( &all_libs, libs );
2090 if (*dll_ext)
2092 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
2093 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
2094 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2096 else
2098 strarray_add( &all_targets, make->module );
2099 output( "%s:", module_path );
2101 if (spec_file) output_filename( spec_file );
2102 output_filenames_obj_dir( make, object_files );
2103 output_filenames_obj_dir( make, res_files );
2104 output( "\n" );
2105 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2106 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2107 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2108 output_filenames( target_flags );
2109 output_filenames( unwind_flags );
2110 if (spec_file)
2112 output( " -shared %s", spec_file );
2113 output_filenames( make->extradllflags );
2115 else output_filenames( make->appmode );
2116 output_filenames_obj_dir( make, object_files );
2117 output_filenames_obj_dir( make, res_files );
2118 output_filenames( all_libs );
2119 output_filename( "$(LDFLAGS)" );
2120 output( "\n" );
2122 if (spec_file && make->importlib)
2124 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2125 if (*dll_ext)
2127 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
2128 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2129 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2130 output_filenames( target_flags );
2131 if (make->is_win16) output_filename( "-m16" );
2132 output( "\n" );
2133 if (implib_objs.count)
2135 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
2136 output( "%s.def.a:", importlib_path );
2137 output_filenames_obj_dir( make, implib_objs );
2138 output( "\n" );
2139 output( "\t$(RM) $@\n" );
2140 output( "\t$(AR) $(ARFLAGS) $@" );
2141 output_filenames_obj_dir( make, implib_objs );
2142 output( "\n" );
2143 output( "\t$(RANLIB) $@\n" );
2146 else
2148 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
2149 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2150 output_filenames_obj_dir( make, implib_objs );
2151 output( "\n" );
2152 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2153 output_filenames( target_flags );
2154 output_filenames_obj_dir( make, implib_objs );
2155 output( "\n" );
2157 if (crosstarget && !make->is_win16)
2159 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2160 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2161 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2162 output_filenames_obj_dir( make, cross_files );
2163 output( "\n" );
2164 output( "\t%s -b %s -w --implib -o $@ --export %s",
2165 tools_path( make, "winebuild" ), crosstarget, spec_file );
2166 output_filenames_obj_dir( make, cross_files );
2167 output( "\n" );
2171 if (spec_file)
2173 if (c2man_files.count)
2175 output( "manpages::\n" );
2176 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2177 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2178 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2179 output_filename( strmake( "-o %s/man%s",
2180 top_obj_dir_path( make, "documentation" ), man_ext ));
2181 output_filenames( c2man_files );
2182 output( "\n" );
2183 output( "htmlpages::\n" );
2184 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2185 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2186 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2187 output_filename( strmake( "-o %s",
2188 top_obj_dir_path( make, "documentation/html" )));
2189 output_filenames( c2man_files );
2190 output( "\n" );
2191 output( "sgmlpages::\n" );
2192 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2193 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2194 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2195 output_filename( strmake( "-o %s",
2196 top_obj_dir_path( make, "documentation/api-guide" )));
2197 output_filenames( c2man_files );
2198 output( "\n" );
2199 output( "xmlpages::\n" );
2200 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2201 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2202 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2203 output_filename( strmake( "-o %s",
2204 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2205 output_filenames( c2man_files );
2206 output( "\n" );
2207 strarray_add( &phony_targets, "manpages" );
2208 strarray_add( &phony_targets, "htmlpages" );
2209 strarray_add( &phony_targets, "sgmlpages" );
2210 strarray_add( &phony_targets, "xmlpages" );
2212 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2216 if (make->staticlib)
2218 strarray_add( &all_targets, make->staticlib );
2219 output( "%s:", obj_dir_path( make, make->staticlib ));
2220 output_filenames_obj_dir( make, object_files );
2221 output( "\n\t$(RM) $@\n" );
2222 output( "\t$(AR) $(ARFLAGS) $@" );
2223 output_filenames_obj_dir( make, object_files );
2224 output( "\n\t$(RANLIB) $@\n" );
2225 if (crosstarget && make->module)
2227 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2229 strarray_add( &all_targets, name );
2230 output( "%s:", obj_dir_path( make, name ));
2231 output_filenames_obj_dir( make, crossobj_files );
2232 output( "\n\t$(RM) $@\n" );
2233 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2234 output_filenames_obj_dir( make, crossobj_files );
2235 output( "\n\t%s-ranlib $@\n", crosstarget );
2239 if (make->testdll)
2241 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2242 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2243 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2244 struct strarray all_libs = empty_strarray;
2246 for (i = 0; i < make->imports.count; i++)
2247 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2248 strarray_addall( &all_libs, get_expanded_make_var_array( make, "LIBS" ));
2250 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2251 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2252 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2253 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2254 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2255 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2256 output_filenames( target_flags );
2257 output_filenames( unwind_flags );
2258 output_filenames( make->appmode );
2259 output_filenames_obj_dir( make, object_files );
2260 output_filenames_obj_dir( make, res_files );
2261 output_filenames( all_libs );
2262 output_filename( "$(LDFLAGS)" );
2263 output( "\n" );
2264 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2265 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2266 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2267 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2268 output_filenames( target_flags );
2269 output_filenames( unwind_flags );
2270 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2271 output_filenames( make->appmode );
2272 output_filenames_obj_dir( make, object_files );
2273 output_filenames_obj_dir( make, res_files );
2274 output_filenames( all_libs );
2275 output_filename( "$(LDFLAGS)" );
2276 output( "\n" );
2277 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2278 obj_dir_path( make, stripped ), dll_ext );
2279 output_filenames_obj_dir( make, object_files );
2280 output_filenames_obj_dir( make, res_files );
2281 output( "\n" );
2283 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2284 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2285 obj_dir_path( make, stripped ), dll_ext );
2286 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2287 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2289 if (crosstarget)
2291 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2293 strarray_add( &clean_files, crosstest );
2294 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2295 output( "%s:", obj_dir_path( make, crosstest ));
2296 output_filenames_obj_dir( make, crossobj_files );
2297 output_filenames_obj_dir( make, res_files );
2298 output( "\n" );
2299 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2300 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2301 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2302 output_filename( "--lib-suffix=.cross.a" );
2303 output_filenames_obj_dir( make, crossobj_files );
2304 output_filenames_obj_dir( make, res_files );
2305 output_filenames( all_libs );
2306 output_filename( "$(LDFLAGS)" );
2307 output( "\n" );
2308 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2309 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2312 output_filenames_obj_dir( make, ok_files );
2313 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2314 output( "check test:" );
2315 output_filenames_obj_dir( make, ok_files );
2316 output( "\n" );
2317 output( "testclean::\n" );
2318 output( "\t$(RM)" );
2319 output_filenames_obj_dir( make, ok_files );
2320 output( "\n" );
2321 strarray_addall( &clean_files, ok_files );
2322 strarray_add( &phony_targets, "check" );
2323 strarray_add( &phony_targets, "test" );
2324 strarray_add( &phony_targets, "testclean" );
2325 *testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2328 if (all_targets.count)
2330 output( "all:" );
2331 output_filenames_obj_dir( make, all_targets );
2332 output( "\n" );
2335 strarray_addall( &clean_files, object_files );
2336 strarray_addall( &clean_files, crossobj_files );
2337 strarray_addall( &clean_files, res_files );
2338 strarray_addall( &clean_files, all_targets );
2339 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2341 if (clean_files.count)
2343 output( "%s::\n", obj_dir_path( make, "clean" ));
2344 output( "\t$(RM)" );
2345 output_filenames_obj_dir( make, clean_files );
2346 output( "\n" );
2347 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2348 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2351 if (make->top_obj_dir)
2353 output( "depend:\n" );
2354 output( "\t@cd %s && $(MAKE) %s\n", make->top_obj_dir, base_dir_path( make, "depend" ));
2355 strarray_add( &phony_targets, "depend" );
2358 if (phony_targets.count)
2360 output( ".PHONY:" );
2361 output_filenames( phony_targets );
2362 output( "\n" );
2365 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2367 return clean_files;
2371 /*******************************************************************
2372 * create_temp_file
2374 static FILE *create_temp_file( const char *orig )
2376 char *name = xmalloc( strlen(orig) + 13 );
2377 unsigned int i, id = getpid();
2378 int fd;
2379 FILE *ret = NULL;
2381 for (i = 0; i < 100; i++)
2383 sprintf( name, "%s.tmp%08x", orig, id );
2384 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2386 ret = fdopen( fd, "w" );
2387 break;
2389 if (errno != EEXIST) break;
2390 id += 7777;
2392 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2393 temp_file_name = name;
2394 return ret;
2398 /*******************************************************************
2399 * rename_temp_file
2401 static void rename_temp_file( const char *dest )
2403 int ret = rename( temp_file_name, dest );
2404 if (ret == -1 && errno == EEXIST)
2406 /* rename doesn't overwrite on windows */
2407 unlink( dest );
2408 ret = rename( temp_file_name, dest );
2410 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2411 temp_file_name = NULL;
2415 /*******************************************************************
2416 * are_files_identical
2418 static int are_files_identical( FILE *file1, FILE *file2 )
2420 for (;;)
2422 char buffer1[8192], buffer2[8192];
2423 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2424 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2425 if (size1 != size2) return 0;
2426 if (!size1) return feof( file1 ) && feof( file2 );
2427 if (memcmp( buffer1, buffer2, size1 )) return 0;
2432 /*******************************************************************
2433 * rename_temp_file_if_changed
2435 static void rename_temp_file_if_changed( const char *dest )
2437 FILE *file1, *file2;
2438 int do_rename = 1;
2440 if ((file1 = fopen( dest, "r" )))
2442 if ((file2 = fopen( temp_file_name, "r" )))
2444 do_rename = !are_files_identical( file1, file2 );
2445 fclose( file2 );
2447 fclose( file1 );
2449 if (!do_rename)
2451 unlink( temp_file_name );
2452 temp_file_name = NULL;
2454 else rename_temp_file( dest );
2458 /*******************************************************************
2459 * output_testlist
2461 static void output_testlist( const char *dest, struct strarray files )
2463 int i;
2465 output_file = create_temp_file( dest );
2467 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2468 output( "#define WIN32_LEAN_AND_MEAN\n" );
2469 output( "#include <windows.h>\n\n" );
2470 output( "#define STANDALONE\n" );
2471 output( "#include \"wine/test.h\"\n\n" );
2473 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2474 output( "\n" );
2475 output( "const struct test winetest_testlist[] =\n" );
2476 output( "{\n" );
2477 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2478 output( " { 0, 0 }\n" );
2479 output( "};\n" );
2481 if (fclose( output_file )) fatal_perror( "write" );
2482 output_file = NULL;
2483 rename_temp_file_if_changed( dest );
2487 /*******************************************************************
2488 * output_gitignore
2490 static void output_gitignore( const char *dest, struct strarray files )
2492 int i;
2494 output_file = create_temp_file( dest );
2496 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2497 for (i = 0; i < files.count; i++)
2499 if (!strchr( files.str[i], '/' )) output( "/" );
2500 output( "%s\n", files.str[i] );
2503 if (fclose( output_file )) fatal_perror( "write" );
2504 output_file = NULL;
2505 rename_temp_file( dest );
2509 /*******************************************************************
2510 * output_dependencies
2512 static void output_dependencies( struct makefile *make, const char *path )
2514 struct strarray targets, testlist_files = empty_strarray, ignore_files = empty_strarray;
2516 if (Separator && ((output_file = fopen( path, "r" ))))
2518 char buffer[1024];
2519 FILE *tmp_file = create_temp_file( path );
2520 int found = 0;
2522 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2524 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2525 found = !strncmp( buffer, Separator, strlen(Separator) );
2527 if (fclose( output_file )) fatal_perror( "write" );
2528 output_file = tmp_file;
2529 if (!found) output( "\n%s\n", Separator );
2531 else
2533 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2534 fatal_perror( "%s", path );
2537 targets = output_sources( make, &testlist_files );
2539 fclose( output_file );
2540 output_file = NULL;
2541 if (temp_file_name) rename_temp_file( path );
2543 strarray_add( &ignore_files, ".gitignore" );
2544 strarray_add( &ignore_files, "Makefile" );
2545 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2546 strarray_addall( &ignore_files, targets );
2548 if (testlist_files.count)
2549 output_testlist( base_dir_path( make, "testlist.c" ), testlist_files );
2550 if (!make->src_dir && make->base_dir)
2551 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
2555 /*******************************************************************
2556 * update_makefile
2558 static void update_makefile( const char *path )
2560 static const char *source_vars[] =
2562 "C_SRCS",
2563 "OBJC_SRCS",
2564 "RC_SRCS",
2565 "MC_SRCS",
2566 "IDL_SRCS",
2567 "BISON_SRCS",
2568 "LEX_SRCS",
2569 "XTEMPLATE_SRCS",
2570 "SVG_SRCS",
2571 "FONT_SRCS",
2572 "IN_SRCS",
2573 "MANPAGES",
2574 NULL
2576 const char **var;
2577 unsigned int i;
2578 struct strarray value;
2579 struct incl_file *file;
2580 struct makefile *make;
2582 make = parse_makefile( path, Separator );
2584 if (root_src_dir)
2586 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
2587 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
2589 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
2590 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
2591 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
2593 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
2594 make->module = get_expanded_make_variable( make, "MODULE" );
2595 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
2596 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
2597 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
2599 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
2600 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
2601 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
2602 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
2604 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
2606 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
2607 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
2609 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
2610 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 );
2612 make->include_args = empty_strarray;
2613 make->define_args = empty_strarray;
2614 strarray_add( &make->define_args, "-D__WINESRC__" );
2616 value = get_expanded_make_var_array( make, "EXTRAINCL" );
2617 for (i = 0; i < value.count; i++)
2618 if (!strncmp( value.str[i], "-I", 2 ))
2619 strarray_add_uniq( &make->include_args, value.str[i] );
2620 else
2621 strarray_add_uniq( &make->define_args, value.str[i] );
2622 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
2624 list_init( &make->sources );
2625 list_init( &make->includes );
2627 for (var = source_vars; *var; var++)
2629 value = get_expanded_make_var_array( make, *var );
2630 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
2633 add_generated_sources( make );
2635 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
2636 for (i = 0; i < value.count; i++)
2638 /* default to .c for unknown extra object files */
2639 if (strendswith( value.str[i], ".o" ))
2640 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2641 else
2642 add_generated_source( make, value.str[i], NULL );
2645 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
2647 output_file_name = base_dir_path( make, makefile_name );
2648 output_dependencies( make, output_file_name );
2649 output_file_name = NULL;
2653 /*******************************************************************
2654 * parse_makeflags
2656 static void parse_makeflags( const char *flags )
2658 const char *p = flags;
2659 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2661 while (*p)
2663 while (isspace(*p)) p++;
2664 var = buffer;
2665 while (*p && !isspace(*p))
2667 if (*p == '\\' && p[1]) p++;
2668 *var++ = *p++;
2670 *var = 0;
2671 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2676 /*******************************************************************
2677 * parse_option
2679 static int parse_option( const char *opt )
2681 if (opt[0] != '-')
2683 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2684 return 0;
2686 switch(opt[1])
2688 case 'f':
2689 if (opt[2]) makefile_name = opt + 2;
2690 break;
2691 case 'R':
2692 relative_dir_mode = 1;
2693 break;
2694 case 's':
2695 if (opt[2]) Separator = opt + 2;
2696 else Separator = NULL;
2697 break;
2698 default:
2699 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2700 exit(1);
2702 return 1;
2706 /*******************************************************************
2707 * main
2709 int main( int argc, char *argv[] )
2711 const char *makeflags = getenv( "MAKEFLAGS" );
2712 int i, j;
2714 if (makeflags) parse_makeflags( makeflags );
2716 i = 1;
2717 while (i < argc)
2719 if (parse_option( argv[i] ))
2721 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2722 argc--;
2724 else i++;
2727 if (relative_dir_mode)
2729 char *relpath;
2731 if (argc != 3)
2733 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2734 exit( 1 );
2736 relpath = get_relative_path( argv[1], argv[2] );
2737 printf( "%s\n", relpath ? relpath : "." );
2738 exit( 0 );
2741 if (argc <= 1)
2743 fprintf( stderr, "%s", Usage );
2744 exit( 1 );
2746 atexit( cleanup_files );
2747 signal( SIGTERM, exit_on_signal );
2748 signal( SIGINT, exit_on_signal );
2749 #ifdef SIGHUP
2750 signal( SIGHUP, exit_on_signal );
2751 #endif
2753 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
2755 top_makefile = parse_makefile( NULL, "# End of common header" );
2757 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
2758 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
2759 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
2760 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
2761 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
2762 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
2763 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
2764 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
2766 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
2767 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
2768 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
2769 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
2770 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
2771 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2772 dll_prefix = get_expanded_make_variable( top_makefile, "DLLPREFIX" );
2773 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
2774 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
2775 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
2776 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
2777 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
2779 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
2780 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2781 if (!tools_ext) tools_ext = "";
2782 if (!dll_prefix) dll_prefix = "";
2783 if (!man_ext) man_ext = "3w";
2785 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2786 return 0;