dmstyle: Remove an unused variable.
[wine.git] / tools / makedep.c
blob54aab45f684c66c955141d8f7564c81bc256bf79
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 $@", tools_path( make, "wrc" ) );
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_filename( source->filename );
1831 output( "\n" );
1832 output( "%s.res:", obj_dir_path( make, obj ));
1833 output_filenames( mo_files );
1834 output( "\n" );
1835 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
1837 else
1839 output_filename( source->filename );
1840 output( "\n" );
1842 output( "%s.res:", obj_dir_path( make, obj ));
1844 else if (!strcmp( ext, "mc" )) /* message file */
1846 strarray_add( &res_files, strmake( "%s.res", obj ));
1847 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1848 tools_path( make, "wmc" ), source->filename );
1849 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
1850 if (mo_files.count)
1852 strarray_add( &mc_files, source->filename );
1853 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1854 output( "\n" );
1855 output( "%s.res:", obj_dir_path( make, obj ));
1856 output_filenames( mo_files );
1857 output( "\n" );
1858 output( "%s ", obj_dir_path( make, "msg.pot" ));
1860 else output( "\n" );
1861 output( "%s.res:", obj_dir_path( make, obj ));
1863 else if (!strcmp( ext, "idl" )) /* IDL file */
1865 struct strarray targets = empty_strarray;
1866 char *dest;
1868 if (!source->file->flags || find_include_file( make, strmake( "%s.h", obj )))
1869 source->file->flags |= FLAG_IDL_HEADER;
1871 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1873 if (!(source->file->flags & idl_outputs[i].flag)) continue;
1874 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1875 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
1876 strarray_add( &targets, dest );
1878 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1879 output_filenames_obj_dir( make, targets );
1880 output( ": %s\n", tools_path( make, "widl" ));
1881 output( "\t%s -o $@", tools_path( make, "widl" ) );
1882 output_filenames( target_flags );
1883 output_filenames( includes );
1884 output_filenames( make->define_args );
1885 output_filenames( extradefs );
1886 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
1887 output_filename( source->filename );
1888 output( "\n" );
1889 output_filenames_obj_dir( make, targets );
1890 output( ": %s", source->filename );
1892 else if (!strcmp( ext, "in" )) /* .in file or man page */
1894 if (strendswith( obj, ".man" ) && source->file->args)
1896 char *dir, *dest = replace_extension( obj, ".man", "" );
1897 char *lang = strchr( dest, '.' );
1898 char *section = source->file->args;
1899 if (lang)
1901 *lang++ = 0;
1902 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1904 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1905 output( "install-man-pages:: %s\n", obj_dir_path( make, obj ));
1906 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj_dir_path( make, obj ), dir, dest, section );
1907 output( "uninstall::\n" );
1908 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1909 free( dest );
1910 free( dir );
1911 strarray_add( &all_targets, xstrdup(obj) );
1912 strarray_add_uniq( &phony_targets, "install-man-pages" );
1913 strarray_add_uniq( &phony_targets, "uninstall" );
1915 else strarray_add( &clean_files, xstrdup(obj) );
1916 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
1917 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1918 output( "%s:", obj_dir_path( make, obj ));
1920 else if (!strcmp( ext, "sfd" )) /* font file */
1922 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
1923 if (fontforge && !make->src_dir)
1925 output( "%s: %s\n", ttf_file, source->filename );
1926 output( "\t%s -script %s %s $@\n",
1927 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
1928 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1930 if (source->file->flags & FLAG_INSTALL)
1932 output( "install install-lib::\n" );
1933 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1934 output( "uninstall::\n" );
1935 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1937 if (source->file->flags & FLAG_SFD_FONTS)
1939 struct strarray *array = source->file->args;
1941 for (i = 0; i < array->count; i++)
1943 char *font = strtok( xstrdup(array->str[i]), " \t" );
1944 char *args = strtok( NULL, "" );
1946 strarray_add( &all_targets, xstrdup( font ));
1947 output( "%s: %s %s\n", obj_dir_path( make, font ),
1948 tools_path( make, "sfnt2fon" ), ttf_file );
1949 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
1950 output( "install install-lib:: %s\n", font );
1951 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n",
1952 obj_dir_path( make, font ), font );
1953 output( "uninstall::\n" );
1954 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1957 if (source->file->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1959 strarray_add_uniq( &phony_targets, "install" );
1960 strarray_add_uniq( &phony_targets, "install-lib" );
1961 strarray_add_uniq( &phony_targets, "uninstall" );
1963 continue; /* no dependencies */
1965 else if (!strcmp( ext, "svg" )) /* svg file */
1967 if (convert && rsvg && icotool && !make->src_dir)
1969 output( "%s.ico %s.bmp: %s\n",
1970 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
1971 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
1972 top_dir_path( make, "tools/buildimage" ), source->filename );
1974 continue; /* no dependencies */
1976 else if (!strcmp( ext, "res" ))
1978 strarray_add( &res_files, source->name );
1979 continue; /* no dependencies */
1981 else
1983 int need_cross = make->testdll ||
1984 (source->file->flags & FLAG_C_IMPLIB) ||
1985 (make->module && make->staticlib);
1987 if ((source->file->flags & FLAG_GENERATED) &&
1988 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
1989 strarray_add( &clean_files, source->filename );
1990 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1991 strarray_add( &object_files, strmake( "%s.o", obj ));
1992 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
1993 output( "\t$(CC) -c -o $@ %s", source->filename );
1994 output_filenames( includes );
1995 output_filenames( make->define_args );
1996 output_filenames( extradefs );
1997 if (make->module || make->staticlib || make->testdll)
1999 output_filenames( dll_flags );
2000 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2002 output_filenames( extra_cflags );
2003 output_filenames( cpp_flags );
2004 output_filename( "$(CFLAGS)" );
2005 output( "\n" );
2006 if (crosstarget && need_cross)
2008 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
2009 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2010 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2011 output_filenames( includes );
2012 output_filenames( make->define_args );
2013 output_filenames( extradefs );
2014 output_filename( "-DWINE_CROSSTEST" );
2015 output_filenames( cpp_flags );
2016 output_filename( "$(CFLAGS)" );
2017 output( "\n" );
2019 if (make->testdll && !strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2021 strarray_add( &ok_files, strmake( "%s.ok", obj ));
2022 output( "%s.ok:\n", obj_dir_path( make, obj ));
2023 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2024 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
2025 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
2027 if (!strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2028 strarray_add( &c2man_files, source->filename );
2029 output( "%s.o", obj_dir_path( make, obj ));
2030 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2031 output( ":" );
2033 free( obj );
2035 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
2036 output( "\n" );
2039 /* rules for files that depend on multiple sources */
2041 if (po_files.count)
2043 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
2044 output_filenames( po_files );
2045 output( "\n" );
2046 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
2047 if (make->is_win16) output_filename( "-m16" );
2048 else output_filenames( target_flags );
2049 output_filename( "--nostdinc" );
2050 output_filenames( includes );
2051 output_filenames( make->define_args );
2052 output_filenames( po_files );
2053 output( "\n" );
2054 strarray_add( &clean_files, "rsrc.pot" );
2057 if (mc_files.count)
2059 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
2060 output_filenames( mc_files );
2061 output( "\n" );
2062 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
2063 output_filenames( mc_files );
2064 output( "\n" );
2065 strarray_add( &clean_files, "msg.pot" );
2068 if (dlldata_files.count)
2070 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
2071 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
2072 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
2073 output_filenames( dlldata_files );
2074 output( "\n" );
2077 if (make->module && !make->staticlib)
2079 struct strarray all_libs = empty_strarray;
2080 char *module_path = obj_dir_path( make, make->module );
2081 char *spec_file = NULL;
2083 if (!make->appmode.count)
2084 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2085 for (i = 0; i < make->delayimports.count; i++)
2086 strarray_add( &all_libs, strmake( "-l%s", make->delayimports.str[i] ));
2087 for (i = 0; i < make->imports.count; i++)
2088 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2089 for (i = 0; i < make->delayimports.count; i++)
2090 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2091 strarray_add( &all_libs, "-lwine" );
2092 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2093 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2094 strarray_addall( &all_libs, libs );
2096 if (*dll_ext)
2098 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
2099 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
2100 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2102 else
2104 strarray_add( &all_targets, make->module );
2105 output( "%s:", module_path );
2107 if (spec_file) output_filename( spec_file );
2108 output_filenames_obj_dir( make, object_files );
2109 output_filenames_obj_dir( make, res_files );
2110 output( "\n" );
2111 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2112 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2113 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2114 output_filenames( target_flags );
2115 output_filenames( unwind_flags );
2116 if (spec_file)
2118 output( " -shared %s", spec_file );
2119 output_filenames( make->extradllflags );
2121 else output_filenames( make->appmode );
2122 output_filenames_obj_dir( make, object_files );
2123 output_filenames_obj_dir( make, res_files );
2124 output_filenames( all_libs );
2125 output_filename( "$(LDFLAGS)" );
2126 output( "\n" );
2128 if (spec_file && make->importlib)
2130 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2131 if (*dll_ext)
2133 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
2134 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2135 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2136 output_filenames( target_flags );
2137 if (make->is_win16) output_filename( "-m16" );
2138 output( "\n" );
2139 if (implib_objs.count)
2141 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
2142 output( "%s.def.a:", importlib_path );
2143 output_filenames_obj_dir( make, implib_objs );
2144 output( "\n" );
2145 output( "\t$(RM) $@\n" );
2146 output( "\t$(AR) $(ARFLAGS) $@" );
2147 output_filenames_obj_dir( make, implib_objs );
2148 output( "\n" );
2149 output( "\t$(RANLIB) $@\n" );
2152 else
2154 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
2155 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2156 output_filenames_obj_dir( make, implib_objs );
2157 output( "\n" );
2158 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2159 output_filenames( target_flags );
2160 output_filenames_obj_dir( make, implib_objs );
2161 output( "\n" );
2163 if (crosstarget && !make->is_win16)
2165 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2166 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2167 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2168 output_filenames_obj_dir( make, cross_files );
2169 output( "\n" );
2170 output( "\t%s -b %s -w --implib -o $@ --export %s",
2171 tools_path( make, "winebuild" ), crosstarget, spec_file );
2172 output_filenames_obj_dir( make, cross_files );
2173 output( "\n" );
2177 if (spec_file)
2179 if (c2man_files.count)
2181 output( "manpages::\n" );
2182 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2183 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2184 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2185 output_filename( strmake( "-o %s/man%s",
2186 top_obj_dir_path( make, "documentation" ), man_ext ));
2187 output_filenames( c2man_files );
2188 output( "\n" );
2189 output( "htmlpages::\n" );
2190 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2191 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2192 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2193 output_filename( strmake( "-o %s",
2194 top_obj_dir_path( make, "documentation/html" )));
2195 output_filenames( c2man_files );
2196 output( "\n" );
2197 output( "sgmlpages::\n" );
2198 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2199 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2200 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2201 output_filename( strmake( "-o %s",
2202 top_obj_dir_path( make, "documentation/api-guide" )));
2203 output_filenames( c2man_files );
2204 output( "\n" );
2205 output( "xmlpages::\n" );
2206 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2207 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2208 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2209 output_filename( strmake( "-o %s",
2210 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2211 output_filenames( c2man_files );
2212 output( "\n" );
2213 strarray_add( &phony_targets, "manpages" );
2214 strarray_add( &phony_targets, "htmlpages" );
2215 strarray_add( &phony_targets, "sgmlpages" );
2216 strarray_add( &phony_targets, "xmlpages" );
2218 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2222 if (make->staticlib)
2224 strarray_add( &all_targets, make->staticlib );
2225 output( "%s:", obj_dir_path( make, make->staticlib ));
2226 output_filenames_obj_dir( make, object_files );
2227 output( "\n\t$(RM) $@\n" );
2228 output( "\t$(AR) $(ARFLAGS) $@" );
2229 output_filenames_obj_dir( make, object_files );
2230 output( "\n\t$(RANLIB) $@\n" );
2231 if (crosstarget && make->module)
2233 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2235 strarray_add( &all_targets, name );
2236 output( "%s:", obj_dir_path( make, name ));
2237 output_filenames_obj_dir( make, crossobj_files );
2238 output( "\n\t$(RM) $@\n" );
2239 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2240 output_filenames_obj_dir( make, crossobj_files );
2241 output( "\n\t%s-ranlib $@\n", crosstarget );
2245 if (make->testdll)
2247 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2248 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2249 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2250 struct strarray all_libs = empty_strarray;
2252 for (i = 0; i < make->imports.count; i++)
2253 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2254 strarray_addall( &all_libs, get_expanded_make_var_array( make, "LIBS" ));
2256 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2257 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2258 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2259 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2260 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2261 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2262 output_filenames( target_flags );
2263 output_filenames( unwind_flags );
2264 output_filenames( make->appmode );
2265 output_filenames_obj_dir( make, object_files );
2266 output_filenames_obj_dir( make, res_files );
2267 output_filenames( all_libs );
2268 output_filename( "$(LDFLAGS)" );
2269 output( "\n" );
2270 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2271 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2272 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2273 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2274 output_filenames( target_flags );
2275 output_filenames( unwind_flags );
2276 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2277 output_filenames( make->appmode );
2278 output_filenames_obj_dir( make, object_files );
2279 output_filenames_obj_dir( make, res_files );
2280 output_filenames( all_libs );
2281 output_filename( "$(LDFLAGS)" );
2282 output( "\n" );
2283 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2284 obj_dir_path( make, stripped ), dll_ext );
2285 output_filenames_obj_dir( make, object_files );
2286 output_filenames_obj_dir( make, res_files );
2287 output( "\n" );
2289 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2290 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2291 obj_dir_path( make, stripped ), dll_ext );
2292 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2293 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2295 if (crosstarget)
2297 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2299 strarray_add( &clean_files, crosstest );
2300 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2301 output( "%s:", obj_dir_path( make, crosstest ));
2302 output_filenames_obj_dir( make, crossobj_files );
2303 output_filenames_obj_dir( make, res_files );
2304 output( "\n" );
2305 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
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_filename( "--lib-suffix=.cross.a" );
2309 output_filenames_obj_dir( make, crossobj_files );
2310 output_filenames_obj_dir( make, res_files );
2311 output_filenames( all_libs );
2312 output_filename( "$(LDFLAGS)" );
2313 output( "\n" );
2314 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2315 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2318 output_filenames_obj_dir( make, ok_files );
2319 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2320 output( "check test:" );
2321 output_filenames_obj_dir( make, ok_files );
2322 output( "\n" );
2323 output( "testclean::\n" );
2324 output( "\t$(RM)" );
2325 output_filenames_obj_dir( make, ok_files );
2326 output( "\n" );
2327 strarray_addall( &clean_files, ok_files );
2328 strarray_add( &phony_targets, "check" );
2329 strarray_add( &phony_targets, "test" );
2330 strarray_add( &phony_targets, "testclean" );
2331 *testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2334 if (all_targets.count)
2336 output( "all:" );
2337 output_filenames_obj_dir( make, all_targets );
2338 output( "\n" );
2341 strarray_addall( &clean_files, object_files );
2342 strarray_addall( &clean_files, crossobj_files );
2343 strarray_addall( &clean_files, res_files );
2344 strarray_addall( &clean_files, all_targets );
2345 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2347 if (clean_files.count)
2349 output( "%s::\n", obj_dir_path( make, "clean" ));
2350 output( "\t$(RM)" );
2351 output_filenames_obj_dir( make, clean_files );
2352 output( "\n" );
2353 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2354 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2357 if (make->top_obj_dir)
2359 output( "depend:\n" );
2360 output( "\t@cd %s && $(MAKE) %s\n", make->top_obj_dir, base_dir_path( make, "depend" ));
2361 strarray_add( &phony_targets, "depend" );
2364 if (phony_targets.count)
2366 output( ".PHONY:" );
2367 output_filenames( phony_targets );
2368 output( "\n" );
2371 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2373 return clean_files;
2377 /*******************************************************************
2378 * create_temp_file
2380 static FILE *create_temp_file( const char *orig )
2382 char *name = xmalloc( strlen(orig) + 13 );
2383 unsigned int i, id = getpid();
2384 int fd;
2385 FILE *ret = NULL;
2387 for (i = 0; i < 100; i++)
2389 sprintf( name, "%s.tmp%08x", orig, id );
2390 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2392 ret = fdopen( fd, "w" );
2393 break;
2395 if (errno != EEXIST) break;
2396 id += 7777;
2398 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2399 temp_file_name = name;
2400 return ret;
2404 /*******************************************************************
2405 * rename_temp_file
2407 static void rename_temp_file( const char *dest )
2409 int ret = rename( temp_file_name, dest );
2410 if (ret == -1 && errno == EEXIST)
2412 /* rename doesn't overwrite on windows */
2413 unlink( dest );
2414 ret = rename( temp_file_name, dest );
2416 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2417 temp_file_name = NULL;
2421 /*******************************************************************
2422 * are_files_identical
2424 static int are_files_identical( FILE *file1, FILE *file2 )
2426 for (;;)
2428 char buffer1[8192], buffer2[8192];
2429 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2430 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2431 if (size1 != size2) return 0;
2432 if (!size1) return feof( file1 ) && feof( file2 );
2433 if (memcmp( buffer1, buffer2, size1 )) return 0;
2438 /*******************************************************************
2439 * rename_temp_file_if_changed
2441 static void rename_temp_file_if_changed( const char *dest )
2443 FILE *file1, *file2;
2444 int do_rename = 1;
2446 if ((file1 = fopen( dest, "r" )))
2448 if ((file2 = fopen( temp_file_name, "r" )))
2450 do_rename = !are_files_identical( file1, file2 );
2451 fclose( file2 );
2453 fclose( file1 );
2455 if (!do_rename)
2457 unlink( temp_file_name );
2458 temp_file_name = NULL;
2460 else rename_temp_file( dest );
2464 /*******************************************************************
2465 * output_testlist
2467 static void output_testlist( const char *dest, struct strarray files )
2469 int i;
2471 output_file = create_temp_file( dest );
2473 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2474 output( "#define WIN32_LEAN_AND_MEAN\n" );
2475 output( "#include <windows.h>\n\n" );
2476 output( "#define STANDALONE\n" );
2477 output( "#include \"wine/test.h\"\n\n" );
2479 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2480 output( "\n" );
2481 output( "const struct test winetest_testlist[] =\n" );
2482 output( "{\n" );
2483 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2484 output( " { 0, 0 }\n" );
2485 output( "};\n" );
2487 if (fclose( output_file )) fatal_perror( "write" );
2488 output_file = NULL;
2489 rename_temp_file_if_changed( dest );
2493 /*******************************************************************
2494 * output_gitignore
2496 static void output_gitignore( const char *dest, struct strarray files )
2498 int i;
2500 output_file = create_temp_file( dest );
2502 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2503 for (i = 0; i < files.count; i++)
2505 if (!strchr( files.str[i], '/' )) output( "/" );
2506 output( "%s\n", files.str[i] );
2509 if (fclose( output_file )) fatal_perror( "write" );
2510 output_file = NULL;
2511 rename_temp_file( dest );
2515 /*******************************************************************
2516 * output_dependencies
2518 static void output_dependencies( struct makefile *make, const char *path )
2520 struct strarray targets, testlist_files = empty_strarray, ignore_files = empty_strarray;
2522 if (Separator && ((output_file = fopen( path, "r" ))))
2524 char buffer[1024];
2525 FILE *tmp_file = create_temp_file( path );
2526 int found = 0;
2528 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2530 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2531 found = !strncmp( buffer, Separator, strlen(Separator) );
2533 if (fclose( output_file )) fatal_perror( "write" );
2534 output_file = tmp_file;
2535 if (!found) output( "\n%s\n", Separator );
2537 else
2539 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2540 fatal_perror( "%s", path );
2543 targets = output_sources( make, &testlist_files );
2545 fclose( output_file );
2546 output_file = NULL;
2547 if (temp_file_name) rename_temp_file( path );
2549 strarray_add( &ignore_files, ".gitignore" );
2550 strarray_add( &ignore_files, "Makefile" );
2551 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2552 strarray_addall( &ignore_files, targets );
2554 if (testlist_files.count)
2555 output_testlist( base_dir_path( make, "testlist.c" ), testlist_files );
2556 if (!make->src_dir && make->base_dir)
2557 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
2561 /*******************************************************************
2562 * update_makefile
2564 static void update_makefile( const char *path )
2566 static const char *source_vars[] =
2568 "C_SRCS",
2569 "OBJC_SRCS",
2570 "RC_SRCS",
2571 "MC_SRCS",
2572 "IDL_SRCS",
2573 "BISON_SRCS",
2574 "LEX_SRCS",
2575 "XTEMPLATE_SRCS",
2576 "SVG_SRCS",
2577 "FONT_SRCS",
2578 "IN_SRCS",
2579 "MANPAGES",
2580 NULL
2582 const char **var;
2583 unsigned int i;
2584 struct strarray value;
2585 struct incl_file *file;
2586 struct makefile *make;
2588 make = parse_makefile( path, Separator );
2590 if (root_src_dir)
2592 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
2593 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
2595 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
2596 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
2597 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
2599 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
2600 make->module = get_expanded_make_variable( make, "MODULE" );
2601 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
2602 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
2603 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
2605 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
2606 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
2607 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
2608 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
2610 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
2612 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
2613 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
2615 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
2616 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 );
2618 make->include_args = empty_strarray;
2619 make->define_args = empty_strarray;
2620 strarray_add( &make->define_args, "-D__WINESRC__" );
2622 value = get_expanded_make_var_array( make, "EXTRAINCL" );
2623 for (i = 0; i < value.count; i++)
2624 if (!strncmp( value.str[i], "-I", 2 ))
2625 strarray_add_uniq( &make->include_args, value.str[i] );
2626 else
2627 strarray_add_uniq( &make->define_args, value.str[i] );
2628 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
2630 list_init( &make->sources );
2631 list_init( &make->includes );
2633 for (var = source_vars; *var; var++)
2635 value = get_expanded_make_var_array( make, *var );
2636 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
2639 add_generated_sources( make );
2641 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
2642 for (i = 0; i < value.count; i++)
2644 /* default to .c for unknown extra object files */
2645 if (strendswith( value.str[i], ".o" ))
2646 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2647 else
2648 add_generated_source( make, value.str[i], NULL );
2651 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
2653 output_file_name = base_dir_path( make, makefile_name );
2654 output_dependencies( make, output_file_name );
2655 output_file_name = NULL;
2659 /*******************************************************************
2660 * parse_makeflags
2662 static void parse_makeflags( const char *flags )
2664 const char *p = flags;
2665 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2667 while (*p)
2669 while (isspace(*p)) p++;
2670 var = buffer;
2671 while (*p && !isspace(*p))
2673 if (*p == '\\' && p[1]) p++;
2674 *var++ = *p++;
2676 *var = 0;
2677 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2682 /*******************************************************************
2683 * parse_option
2685 static int parse_option( const char *opt )
2687 if (opt[0] != '-')
2689 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2690 return 0;
2692 switch(opt[1])
2694 case 'f':
2695 if (opt[2]) makefile_name = opt + 2;
2696 break;
2697 case 'R':
2698 relative_dir_mode = 1;
2699 break;
2700 case 's':
2701 if (opt[2]) Separator = opt + 2;
2702 else Separator = NULL;
2703 break;
2704 default:
2705 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2706 exit(1);
2708 return 1;
2712 /*******************************************************************
2713 * main
2715 int main( int argc, char *argv[] )
2717 const char *makeflags = getenv( "MAKEFLAGS" );
2718 int i, j;
2720 if (makeflags) parse_makeflags( makeflags );
2722 i = 1;
2723 while (i < argc)
2725 if (parse_option( argv[i] ))
2727 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2728 argc--;
2730 else i++;
2733 if (relative_dir_mode)
2735 char *relpath;
2737 if (argc != 3)
2739 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2740 exit( 1 );
2742 relpath = get_relative_path( argv[1], argv[2] );
2743 printf( "%s\n", relpath ? relpath : "." );
2744 exit( 0 );
2747 if (argc <= 1)
2749 fprintf( stderr, "%s", Usage );
2750 exit( 1 );
2752 atexit( cleanup_files );
2753 signal( SIGTERM, exit_on_signal );
2754 signal( SIGINT, exit_on_signal );
2755 #ifdef SIGHUP
2756 signal( SIGHUP, exit_on_signal );
2757 #endif
2759 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
2761 top_makefile = parse_makefile( NULL, "# End of common header" );
2763 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
2764 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
2765 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
2766 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
2767 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
2768 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
2769 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
2770 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
2772 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
2773 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
2774 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
2775 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
2776 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
2777 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2778 dll_prefix = get_expanded_make_variable( top_makefile, "DLLPREFIX" );
2779 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
2780 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
2781 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
2782 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
2783 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
2785 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
2786 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2787 if (!tools_ext) tools_ext = "";
2788 if (!dll_prefix) dll_prefix = "";
2789 if (!man_ext) man_ext = "3w";
2791 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2792 return 0;