winevulkan: Move need for output member copy check to needs_conversion.
[wine.git] / tools / makedep.c
blob3ae1e2db4cfa483c4709d4689d189e5fb2199b30
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013, 2020 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"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
35 #include "tools.h"
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_IMPORTLIB, /* idl importlib "foo.tlb" */
44 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
45 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
48 struct dependency
50 int line; /* source line where this header is included */
51 enum incl_type type; /* type of include */
52 char *name; /* header name */
55 struct file
57 struct list entry;
58 char *name; /* full file name relative to cwd */
59 void *args; /* custom arguments for makefile rule */
60 unsigned int flags; /* flags (see below) */
61 unsigned int deps_count; /* files in use */
62 unsigned int deps_size; /* total allocated size */
63 struct dependency *deps; /* all header dependencies */
66 struct incl_file
68 struct list entry;
69 struct list hash_entry;
70 struct file *file;
71 char *name;
72 char *filename;
73 char *basename; /* base target name for generated files */
74 char *sourcename; /* source file name for generated headers */
75 struct incl_file *included_by; /* file that included this one */
76 int included_line; /* line where this file was included */
77 enum incl_type type; /* type of include */
78 unsigned int arch; /* architecture for multi-arch files, otherwise 0 */
79 int use_msvcrt:1; /* put msvcrt headers in the search path? */
80 int is_external:1; /* file from external library? */
81 struct incl_file *owner;
82 unsigned int files_count; /* files in use */
83 unsigned int files_size; /* total allocated size */
84 struct incl_file **files;
85 struct strarray dependencies; /* file dependencies */
86 struct strarray importlibdeps; /* importlib dependencies */
89 #define FLAG_GENERATED 0x000001 /* generated file */
90 #define FLAG_INSTALL 0x000002 /* file to install */
91 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
92 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
93 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
94 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
95 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
96 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (_l.res) file */
97 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
98 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
99 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
100 #define FLAG_RC_HEADER 0x020000 /* rc file is a header */
101 #define FLAG_C_IMPLIB 0x040000 /* file is part of an import library */
102 #define FLAG_C_UNIX 0x080000 /* file is part of a Unix library */
103 #define FLAG_SFD_FONTS 0x100000 /* sfd file generated bitmap fonts */
105 static const struct
107 unsigned int flag;
108 const char *ext;
109 } idl_outputs[] =
111 { FLAG_IDL_TYPELIB, "_l.res" },
112 { FLAG_IDL_REGTYPELIB, "_t.res" },
113 { FLAG_IDL_CLIENT, "_c.c" },
114 { FLAG_IDL_IDENT, "_i.c" },
115 { FLAG_IDL_PROXY, "_p.c" },
116 { FLAG_IDL_SERVER, "_s.c" },
117 { FLAG_IDL_REGISTER, "_r.res" },
120 #define HASH_SIZE 197
122 static struct list files[HASH_SIZE];
123 static struct list global_includes[HASH_SIZE];
125 enum install_rules { INSTALL_LIB, INSTALL_DEV, INSTALL_TEST, NB_INSTALL_RULES };
126 static const char *install_targets[NB_INSTALL_RULES] = { "install-lib", "install-dev", "install-test" };
127 static const char *install_variables[NB_INSTALL_RULES] = { "INSTALL_LIB", "INSTALL_DEV", "INSTALL_TEST" };
129 #define MAX_ARCHS 5
131 /* variables common to all makefiles */
132 static struct strarray archs;
133 static struct strarray linguas;
134 static struct strarray dll_flags;
135 static struct strarray unix_dllflags;
136 static struct strarray msvcrt_flags;
137 static struct strarray cpp_flags;
138 static struct strarray lddll_flags;
139 static struct strarray libs;
140 static struct strarray enable_tests;
141 static struct strarray cmdline_vars;
142 static struct strarray subdirs;
143 static struct strarray delay_import_libs;
144 static struct strarray top_install[NB_INSTALL_RULES];
145 static const char *root_src_dir;
146 static const char *tools_dir;
147 static const char *tools_ext;
148 static const char *exe_ext;
149 static const char *dll_ext;
150 static const char *fontforge;
151 static const char *convert;
152 static const char *flex;
153 static const char *bison;
154 static const char *ar;
155 static const char *ranlib;
156 static const char *rsvg;
157 static const char *icotool;
158 static const char *msgfmt;
159 static const char *ln_s;
160 static const char *sed_cmd;
161 /* per-architecture global variables */
162 static const char *arch_dirs[MAX_ARCHS];
163 static const char *arch_pe_dirs[MAX_ARCHS];
164 static const char *arch_install_dirs[MAX_ARCHS];
165 static const char *strip_progs[MAX_ARCHS];
166 static const char *debug_flags[MAX_ARCHS];
167 static const char *delay_load_flags[MAX_ARCHS];
168 static struct strarray target_flags[MAX_ARCHS];
169 static struct strarray extra_cflags[MAX_ARCHS];
170 static struct strarray extra_cflags_extlib[MAX_ARCHS];
171 static struct strarray disabled_dirs[MAX_ARCHS];
173 struct makefile
175 /* values determined from input makefile */
176 struct strarray vars;
177 struct strarray include_paths;
178 struct strarray include_args;
179 struct strarray define_args;
180 struct strarray unix_cflags;
181 struct strarray programs;
182 struct strarray scripts;
183 struct strarray imports;
184 struct strarray delayimports;
185 struct strarray extradllflags;
186 struct strarray install[NB_INSTALL_RULES];
187 struct strarray extra_targets;
188 struct strarray extra_imports;
189 struct list sources;
190 struct list includes;
191 const char *src_dir;
192 const char *obj_dir;
193 const char *parent_dir;
194 const char *module;
195 const char *testdll;
196 const char *extlib;
197 const char *sharedlib;
198 const char *staticlib;
199 const char *importlib;
200 const char *unixlib;
201 int use_msvcrt;
202 int data_only;
203 int is_win16;
204 int is_exe;
205 int disabled[MAX_ARCHS];
207 /* values generated at output time */
208 struct strarray in_files;
209 struct strarray ok_files;
210 struct strarray pot_files;
211 struct strarray test_files;
212 struct strarray clean_files;
213 struct strarray distclean_files;
214 struct strarray maintainerclean_files;
215 struct strarray uninstall_files;
216 struct strarray unixobj_files;
217 struct strarray font_files;
218 struct strarray debug_files;
219 struct strarray dlldata_files;
220 struct strarray phony_targets;
221 struct strarray dependencies;
222 struct strarray object_files[MAX_ARCHS];
223 struct strarray implib_files[MAX_ARCHS];
224 struct strarray res_files[MAX_ARCHS];
225 struct strarray all_targets[MAX_ARCHS];
226 struct strarray install_rules[NB_INSTALL_RULES];
229 static struct makefile *top_makefile;
230 static struct makefile *include_makefile;
231 static struct makefile **submakes;
233 static const char separator[] = "### Dependencies";
234 static const char *output_makefile_name = "Makefile";
235 static const char *input_file_name;
236 static const char *output_file_name;
237 static const char *temp_file_name;
238 static int relative_dir_mode;
239 static int silent_rules;
240 static int input_line;
241 static int output_column;
242 static FILE *output_file;
244 static const char Usage[] =
245 "Usage: makedep [options] [directories]\n"
246 "Options:\n"
247 " -R from to Compute the relative path between two directories\n"
248 " -S Generate Automake-style silent rules\n"
249 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
252 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
253 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
254 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
255 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
257 /*******************************************************************
258 * fatal_error
260 static void fatal_error( const char *msg, ... )
262 va_list valist;
263 va_start( valist, msg );
264 if (input_file_name)
266 fprintf( stderr, "%s:", input_file_name );
267 if (input_line) fprintf( stderr, "%d:", input_line );
268 fprintf( stderr, " error: " );
270 else fprintf( stderr, "makedep: error: " );
271 vfprintf( stderr, msg, valist );
272 va_end( valist );
273 exit(1);
277 /*******************************************************************
278 * fatal_perror
280 static void fatal_perror( const char *msg, ... )
282 va_list valist;
283 va_start( valist, msg );
284 if (input_file_name)
286 fprintf( stderr, "%s:", input_file_name );
287 if (input_line) fprintf( stderr, "%d:", input_line );
288 fprintf( stderr, " error: " );
290 else fprintf( stderr, "makedep: error: " );
291 vfprintf( stderr, msg, valist );
292 perror( " " );
293 va_end( valist );
294 exit(1);
298 /*******************************************************************
299 * cleanup_files
301 static void cleanup_files(void)
303 if (temp_file_name) unlink( temp_file_name );
304 if (output_file_name) unlink( output_file_name );
308 /*******************************************************************
309 * exit_on_signal
311 static void exit_on_signal( int sig )
313 exit( 1 ); /* this will call the atexit functions */
317 /*******************************************************************
318 * output
320 static void output( const char *format, ... )
322 int ret;
323 va_list valist;
325 va_start( valist, format );
326 ret = vfprintf( output_file, format, valist );
327 va_end( valist );
328 if (ret < 0) fatal_perror( "output" );
329 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
330 else output_column += ret;
334 /*******************************************************************
335 * strarray_get_value
337 * Find a value in a name/value pair string array.
339 static const char *strarray_get_value( const struct strarray *array, const char *name )
341 int pos, res, min = 0, max = array->count / 2 - 1;
343 while (min <= max)
345 pos = (min + max) / 2;
346 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
347 if (res < 0) min = pos + 1;
348 else max = pos - 1;
350 return NULL;
354 /*******************************************************************
355 * strarray_set_value
357 * Define a value in a name/value pair string array.
359 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
361 int i, pos, res, min = 0, max = array->count / 2 - 1;
363 while (min <= max)
365 pos = (min + max) / 2;
366 if (!(res = strcmp( array->str[pos * 2], name )))
368 /* redefining a variable replaces the previous value */
369 array->str[pos * 2 + 1] = value;
370 return;
372 if (res < 0) min = pos + 1;
373 else max = pos - 1;
375 strarray_add( array, NULL );
376 strarray_add( array, NULL );
377 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
378 array->str[min * 2] = name;
379 array->str[min * 2 + 1] = value;
383 /*******************************************************************
384 * output_filename
386 static void output_filename( const char *name )
388 if (output_column + strlen(name) + 1 > 100)
390 output( " \\\n" );
391 output( " " );
393 else if (output_column) output( " " );
394 output( "%s", name );
398 /*******************************************************************
399 * output_filenames
401 static void output_filenames( struct strarray array )
403 unsigned int i;
405 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
409 /*******************************************************************
410 * output_rm_filenames
412 static void output_rm_filenames( struct strarray array, const char *command )
414 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
415 unsigned int i, len;
417 if (!array.count) return;
418 output( "\t%s", command );
419 for (i = len = 0; i < array.count; i++)
421 if (len > max_cmdline)
423 output( "\n" );
424 output( "\t%s", command );
425 len = 0;
427 output_filename( array.str[i] );
428 len += strlen( array.str[i] ) + 1;
430 output( "\n" );
434 /*******************************************************************
435 * get_extension
437 static char *get_extension( char *filename )
439 char *ext = strrchr( filename, '.' );
440 if (ext && strchr( ext, '/' )) ext = NULL;
441 return ext;
445 /*******************************************************************
446 * get_base_name
448 static const char *get_base_name( const char *name )
450 char *base;
451 if (!strchr( name, '.' )) return name;
452 base = xstrdup( name );
453 *strrchr( base, '.' ) = 0;
454 return base;
458 /*******************************************************************
459 * replace_filename
461 static char *replace_filename( const char *path, const char *name )
463 const char *p;
464 char *ret;
465 size_t len;
467 if (!path) return xstrdup( name );
468 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
469 len = p - path + 1;
470 ret = xmalloc( len + strlen( name ) + 1 );
471 memcpy( ret, path, len );
472 strcpy( ret + len, name );
473 return ret;
477 /*******************************************************************
478 * replace_substr
480 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
482 size_t pos = start - str;
483 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
484 memcpy( ret, str, pos );
485 strcpy( ret + pos, replace );
486 strcat( ret + pos, start + len );
487 return ret;
491 /*******************************************************************
492 * get_relative_path
494 * Determine where the destination path is located relative to the 'from' path.
496 static char *get_relative_path( const char *from, const char *dest )
498 const char *start;
499 char *ret, *p;
500 unsigned int dotdots = 0;
502 /* a path of "." is equivalent to an empty path */
503 if (!strcmp( from, "." )) from = "";
505 for (;;)
507 while (*from == '/') from++;
508 while (*dest == '/') dest++;
509 start = dest; /* save start of next path element */
510 if (!*from) break;
512 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
513 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
515 /* count remaining elements in 'from' */
518 dotdots++;
519 while (*from && *from != '/') from++;
520 while (*from == '/') from++;
522 while (*from);
523 break;
526 if (!start[0] && !dotdots) return NULL; /* empty path */
528 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
529 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
531 if (start[0]) strcpy( p, start );
532 else p[-1] = 0; /* remove trailing slash */
533 return ret;
537 /*******************************************************************
538 * concat_paths
540 static char *concat_paths( const char *base, const char *path )
542 int i, len;
543 char *ret;
545 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
546 if (!path || !path[0]) return xstrdup( base );
547 if (path[0] == '/') return xstrdup( path );
549 len = strlen( base );
550 while (len && base[len - 1] == '/') len--;
551 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
553 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
554 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
555 if (i != len - 1 || base[i] != '.')
557 path += 2;
558 while (*path == '/') path++;
560 /* else ignore "." element */
561 while (i > 0 && base[i - 1] == '/') i--;
562 len = i;
564 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
565 ret = xmalloc( len + strlen( path ) + 2 );
566 memcpy( ret, base, len );
567 ret[len++] = '/';
568 strcpy( ret + len, path );
569 return ret;
573 /*******************************************************************
574 * is_native_arch_disabled
576 * Check if the makefile was disabled for a PE arch that matches the native arch.
578 static int is_native_arch_disabled( struct makefile *make )
580 unsigned int arch;
582 for (arch = 1; arch < archs.count; arch++)
583 if (make->disabled[arch] && !strcmp( archs.str[0], archs.str[arch] ))
584 return 1;
585 return 0;
589 /*******************************************************************
590 * is_multiarch
592 * Check if arch is one of the PE architectures in multiarch.
593 * Also return TRUE for native arch iff there's no PE support.
595 static int is_multiarch( unsigned int arch )
597 return archs.count == 1 || arch;
601 /*******************************************************************
602 * arch_module_name
604 static char *arch_module_name( const char *module, unsigned int arch )
606 return strmake( "%s%s%s", arch_dirs[arch], module, arch ? "" : dll_ext );
610 /*******************************************************************
611 * arch_make_variable
613 static char *arch_make_variable( const char *name, unsigned int arch )
615 return arch ? strmake( "$(%s_%s)", archs.str[arch], name ) : strmake( "$(%s)", name );
619 /*******************************************************************
620 * obj_dir_path
622 static char *obj_dir_path( const struct makefile *make, const char *path )
624 return concat_paths( make->obj_dir, path );
628 /*******************************************************************
629 * src_dir_path
631 static char *src_dir_path( const struct makefile *make, const char *path )
633 if (make->src_dir) return concat_paths( make->src_dir, path );
634 return obj_dir_path( make, path );
638 /*******************************************************************
639 * root_src_dir_path
641 static char *root_src_dir_path( const char *path )
643 return concat_paths( root_src_dir, path );
647 /*******************************************************************
648 * tools_dir_path
650 static char *tools_dir_path( const struct makefile *make, const char *path )
652 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
653 return strmake( "tools/%s", path );
657 /*******************************************************************
658 * tools_path
660 static char *tools_path( const struct makefile *make, const char *name )
662 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
666 /*******************************************************************
667 * strarray_addall_path
669 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
671 unsigned int i;
673 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
677 /*******************************************************************
678 * get_line
680 static char *get_line( FILE *file )
682 static char *buffer;
683 static size_t size;
685 if (!size)
687 size = 1024;
688 buffer = xmalloc( size );
690 if (!fgets( buffer, size, file )) return NULL;
691 input_line++;
693 for (;;)
695 char *p = buffer + strlen(buffer);
696 /* if line is larger than buffer, resize buffer */
697 while (p == buffer + size - 1 && p[-1] != '\n')
699 buffer = xrealloc( buffer, size * 2 );
700 if (!fgets( buffer + size - 1, size + 1, file )) break;
701 p = buffer + strlen(buffer);
702 size *= 2;
704 if (p > buffer && p[-1] == '\n')
706 *(--p) = 0;
707 if (p > buffer && p[-1] == '\r') *(--p) = 0;
708 if (p > buffer && p[-1] == '\\')
710 *(--p) = 0;
711 /* line ends in backslash, read continuation line */
712 if (!fgets( p, size - (p - buffer), file )) return buffer;
713 input_line++;
714 continue;
717 return buffer;
722 /*******************************************************************
723 * hash_filename
725 static unsigned int hash_filename( const char *name )
727 /* FNV-1 hash */
728 unsigned int ret = 2166136261u;
729 while (*name) ret = (ret * 16777619) ^ *name++;
730 return ret % HASH_SIZE;
734 /*******************************************************************
735 * add_file
737 static struct file *add_file( const char *name )
739 struct file *file = xmalloc( sizeof(*file) );
740 memset( file, 0, sizeof(*file) );
741 file->name = xstrdup( name );
742 return file;
746 /*******************************************************************
747 * add_dependency
749 static void add_dependency( struct file *file, const char *name, enum incl_type type )
751 if (file->deps_count >= file->deps_size)
753 file->deps_size *= 2;
754 if (file->deps_size < 16) file->deps_size = 16;
755 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
757 file->deps[file->deps_count].line = input_line;
758 file->deps[file->deps_count].type = type;
759 file->deps[file->deps_count].name = xstrdup( name );
760 file->deps_count++;
764 /*******************************************************************
765 * find_src_file
767 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
769 struct incl_file *file;
771 if (make == include_makefile)
773 unsigned int hash = hash_filename( name );
775 LIST_FOR_EACH_ENTRY( file, &global_includes[hash], struct incl_file, hash_entry )
776 if (!strcmp( name, file->name )) return file;
777 return NULL;
780 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
781 if (!strcmp( name, file->name )) return file;
782 return NULL;
785 /*******************************************************************
786 * find_include_file
788 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
790 struct incl_file *file;
792 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
794 const char *filename = file->filename;
795 if (!filename) continue;
796 if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
798 filename += strlen(make->obj_dir);
799 while (*filename == '/') filename++;
801 if (!strcmp( name, filename )) return file;
803 return NULL;
806 /*******************************************************************
807 * add_include
809 * Add an include file if it doesn't already exists.
811 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
812 const char *name, int line, enum incl_type type )
814 struct incl_file *include;
816 if (parent->files_count >= parent->files_size)
818 parent->files_size *= 2;
819 if (parent->files_size < 16) parent->files_size = 16;
820 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
823 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
824 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
825 goto found;
827 include = xmalloc( sizeof(*include) );
828 memset( include, 0, sizeof(*include) );
829 include->name = xstrdup(name);
830 include->included_by = parent;
831 include->included_line = line;
832 include->type = type;
833 include->use_msvcrt = parent->use_msvcrt;
834 list_add_tail( &make->includes, &include->entry );
835 found:
836 parent->files[parent->files_count++] = include;
837 return include;
841 /*******************************************************************
842 * add_generated_source
844 * Add a generated source file to the list.
846 static struct incl_file *add_generated_source( struct makefile *make, const char *name,
847 const char *filename, unsigned int arch )
849 struct incl_file *file = xmalloc( sizeof(*file) );
851 name = strmake( "%s%s", arch_dirs[arch], name );
852 memset( file, 0, sizeof(*file) );
853 file->file = add_file( name );
854 file->arch = arch;
855 file->name = xstrdup( name );
856 file->basename = xstrdup( filename ? filename : name );
857 file->filename = obj_dir_path( make, file->basename );
858 file->file->flags = FLAG_GENERATED;
859 file->use_msvcrt = make->use_msvcrt;
860 list_add_tail( &make->sources, &file->entry );
861 if (make == include_makefile)
863 unsigned int hash = hash_filename( name );
864 list_add_tail( &global_includes[hash], &file->hash_entry );
866 return file;
870 /*******************************************************************
871 * parse_include_directive
873 static void parse_include_directive( struct file *source, char *str )
875 char quote, *include, *p = str;
877 while (*p && isspace(*p)) p++;
878 if (*p != '\"' && *p != '<' ) return;
879 quote = *p++;
880 if (quote == '<') quote = '>';
881 include = p;
882 while (*p && (*p != quote)) p++;
883 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
884 *p = 0;
885 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
889 /*******************************************************************
890 * parse_pragma_directive
892 static void parse_pragma_directive( struct file *source, char *str )
894 char *flag, *p = str;
896 if (!isspace( *p )) return;
897 while (*p && isspace(*p)) p++;
898 p = strtok( p, " \t" );
899 if (strcmp( p, "makedep" )) return;
901 while ((flag = strtok( NULL, " \t" )))
903 if (!strcmp( flag, "depend" ))
905 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
906 return;
908 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
910 if (strendswith( source->name, ".idl" ))
912 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
913 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
914 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
915 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
916 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
917 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
918 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
919 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
921 else if (strendswith( source->name, ".rc" ))
923 if (!strcmp( flag, "header" )) source->flags |= FLAG_RC_HEADER;
924 else if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
926 else if (strendswith( source->name, ".sfd" ))
928 if (!strcmp( flag, "font" ))
930 struct strarray *array = source->args;
932 if (!array)
934 source->args = array = xmalloc( sizeof(*array) );
935 *array = empty_strarray;
936 source->flags |= FLAG_SFD_FONTS;
938 strarray_add( array, xstrdup( strtok( NULL, "" )));
939 return;
942 else
944 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
945 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
951 /*******************************************************************
952 * parse_cpp_directive
954 static void parse_cpp_directive( struct file *source, char *str )
956 while (*str && isspace(*str)) str++;
957 if (*str++ != '#') return;
958 while (*str && isspace(*str)) str++;
960 if (!strncmp( str, "include", 7 ))
961 parse_include_directive( source, str + 7 );
962 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
963 parse_include_directive( source, str + 6 );
964 else if (!strncmp( str, "pragma", 6 ))
965 parse_pragma_directive( source, str + 6 );
969 /*******************************************************************
970 * parse_idl_file
972 static void parse_idl_file( struct file *source, FILE *file )
974 char *buffer, *include;
976 input_line = 0;
978 while ((buffer = get_line( file )))
980 char quote;
981 char *p = buffer;
982 while (*p && isspace(*p)) p++;
984 if (!strncmp( p, "importlib", 9 ))
986 p += 9;
987 while (*p && isspace(*p)) p++;
988 if (*p++ != '(') continue;
989 while (*p && isspace(*p)) p++;
990 if (*p++ != '"') continue;
991 include = p;
992 while (*p && (*p != '"')) p++;
993 if (!*p) fatal_error( "malformed importlib directive\n" );
994 *p = 0;
995 add_dependency( source, include, INCL_IMPORTLIB );
996 continue;
999 if (!strncmp( p, "import", 6 ))
1001 p += 6;
1002 while (*p && isspace(*p)) p++;
1003 if (*p != '"') continue;
1004 include = ++p;
1005 while (*p && (*p != '"')) p++;
1006 if (!*p) fatal_error( "malformed import directive\n" );
1007 *p = 0;
1008 add_dependency( source, include, INCL_IMPORT );
1009 continue;
1012 /* check for #include inside cpp_quote */
1013 if (!strncmp( p, "cpp_quote", 9 ))
1015 p += 9;
1016 while (*p && isspace(*p)) p++;
1017 if (*p++ != '(') continue;
1018 while (*p && isspace(*p)) p++;
1019 if (*p++ != '"') continue;
1020 if (*p++ != '#') continue;
1021 while (*p && isspace(*p)) p++;
1022 if (strncmp( p, "include", 7 )) continue;
1023 p += 7;
1024 while (*p && isspace(*p)) p++;
1025 if (*p == '\\' && p[1] == '"')
1027 p += 2;
1028 quote = '"';
1030 else
1032 if (*p++ != '<' ) continue;
1033 quote = '>';
1035 include = p;
1036 while (*p && (*p != quote)) p++;
1037 if (!*p || (quote == '"' && p[-1] != '\\'))
1038 fatal_error( "malformed #include directive inside cpp_quote\n" );
1039 if (quote == '"') p--; /* remove backslash */
1040 *p = 0;
1041 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1042 continue;
1045 parse_cpp_directive( source, p );
1049 /*******************************************************************
1050 * parse_c_file
1052 static void parse_c_file( struct file *source, FILE *file )
1054 char *buffer;
1056 input_line = 0;
1057 while ((buffer = get_line( file )))
1059 parse_cpp_directive( source, buffer );
1064 /*******************************************************************
1065 * parse_rc_file
1067 static void parse_rc_file( struct file *source, FILE *file )
1069 char *buffer, *include;
1071 input_line = 0;
1072 while ((buffer = get_line( file )))
1074 char quote;
1075 char *p = buffer;
1076 while (*p && isspace(*p)) p++;
1078 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1080 p += 2;
1081 while (*p && isspace(*p)) p++;
1082 if (strncmp( p, "@makedep:", 9 )) continue;
1083 p += 9;
1084 while (*p && isspace(*p)) p++;
1085 quote = '"';
1086 if (*p == quote)
1088 include = ++p;
1089 while (*p && *p != quote) p++;
1091 else
1093 include = p;
1094 while (*p && !isspace(*p) && *p != '*') p++;
1096 if (!*p)
1097 fatal_error( "malformed makedep comment\n" );
1098 *p = 0;
1099 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1100 continue;
1103 parse_cpp_directive( source, buffer );
1108 /*******************************************************************
1109 * parse_in_file
1111 static void parse_in_file( struct file *source, FILE *file )
1113 char *p, *buffer;
1115 /* make sure it gets rebuilt when the version changes */
1116 add_dependency( source, "config.h", INCL_SYSTEM );
1118 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1120 input_line = 0;
1121 while ((buffer = get_line( file )))
1123 if (strncmp( buffer, ".TH", 3 )) continue;
1124 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1125 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1126 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1127 source->args = xstrdup( p );
1128 return;
1133 /*******************************************************************
1134 * parse_sfd_file
1136 static void parse_sfd_file( struct file *source, FILE *file )
1138 char *p, *eol, *buffer;
1140 input_line = 0;
1141 while ((buffer = get_line( file )))
1143 if (strncmp( buffer, "UComments:", 10 )) continue;
1144 p = buffer + 10;
1145 while (*p == ' ') p++;
1146 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1148 p++;
1149 buffer[strlen(buffer) - 1] = 0;
1151 while ((eol = strstr( p, "+AAoA" )))
1153 *eol = 0;
1154 while (*p && isspace(*p)) p++;
1155 if (*p++ == '#')
1157 while (*p && isspace(*p)) p++;
1158 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1160 p = eol + 5;
1162 while (*p && isspace(*p)) p++;
1163 if (*p++ != '#') return;
1164 while (*p && isspace(*p)) p++;
1165 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1166 return;
1171 static const struct
1173 const char *ext;
1174 void (*parse)( struct file *file, FILE *f );
1175 } parse_functions[] =
1177 { ".c", parse_c_file },
1178 { ".h", parse_c_file },
1179 { ".inl", parse_c_file },
1180 { ".l", parse_c_file },
1181 { ".m", parse_c_file },
1182 { ".rh", parse_c_file },
1183 { ".x", parse_c_file },
1184 { ".y", parse_c_file },
1185 { ".idl", parse_idl_file },
1186 { ".rc", parse_rc_file },
1187 { ".in", parse_in_file },
1188 { ".sfd", parse_sfd_file }
1191 /*******************************************************************
1192 * load_file
1194 static struct file *load_file( const char *name )
1196 struct file *file;
1197 FILE *f;
1198 unsigned int i, hash = hash_filename( name );
1200 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1201 if (!strcmp( name, file->name )) return file;
1203 if (!(f = fopen( name, "r" ))) return NULL;
1205 file = add_file( name );
1206 list_add_tail( &files[hash], &file->entry );
1207 input_file_name = file->name;
1208 input_line = 0;
1210 for (i = 0; i < ARRAY_SIZE(parse_functions); i++)
1212 if (!strendswith( name, parse_functions[i].ext )) continue;
1213 parse_functions[i].parse( file, f );
1214 break;
1217 fclose( f );
1218 input_file_name = NULL;
1220 return file;
1224 /*******************************************************************
1225 * open_include_path_file
1227 * Open a file from a directory on the include path.
1229 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1230 const char *name, char **filename )
1232 char *src_path = concat_paths( dir, name );
1233 struct file *ret = load_file( src_path );
1235 if (ret) *filename = src_path;
1236 return ret;
1240 /*******************************************************************
1241 * open_file_same_dir
1243 * Open a file in the same directory as the parent.
1245 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1247 char *src_path = replace_filename( parent->file->name, name );
1248 struct file *ret = load_file( src_path );
1250 if (ret) *filename = replace_filename( parent->filename, name );
1251 return ret;
1255 /*******************************************************************
1256 * open_same_dir_generated_file
1258 * Open a generated_file in the same directory as the parent.
1260 static struct file *open_same_dir_generated_file( const struct makefile *make,
1261 const struct incl_file *parent, struct incl_file *file,
1262 const char *ext, const char *src_ext )
1264 char *filename;
1265 struct file *ret = NULL;
1267 if (strendswith( file->name, ext ) &&
1268 (ret = open_file_same_dir( parent, replace_extension( file->name, ext, src_ext ), &filename )))
1270 file->sourcename = filename;
1271 file->filename = obj_dir_path( make, replace_filename( parent->name, file->name ));
1273 return ret;
1277 /*******************************************************************
1278 * open_local_file
1280 * Open a file in the source directory of the makefile.
1282 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1284 char *src_path = src_dir_path( make, path );
1285 struct file *ret = load_file( src_path );
1287 /* if not found, try parent dir */
1288 if (!ret && make->parent_dir)
1290 free( src_path );
1291 path = strmake( "%s/%s", make->parent_dir, path );
1292 src_path = src_dir_path( make, path );
1293 ret = load_file( src_path );
1296 if (ret) *filename = src_path;
1297 return ret;
1301 /*******************************************************************
1302 * open_local_generated_file
1304 * Open a generated file in the directory of the makefile.
1306 static struct file *open_local_generated_file( const struct makefile *make, struct incl_file *file,
1307 const char *ext, const char *src_ext )
1309 struct incl_file *include;
1311 if (strendswith( file->name, ext ) &&
1312 (include = find_src_file( make, replace_extension( file->name, ext, src_ext ) )))
1314 file->sourcename = include->filename;
1315 file->filename = obj_dir_path( make, file->name );
1316 return include->file;
1318 return NULL;
1322 /*******************************************************************
1323 * open_global_file
1325 * Open a file in the top-level source directory.
1327 static struct file *open_global_file( const char *path, char **filename )
1329 char *src_path = root_src_dir_path( path );
1330 struct file *ret = load_file( src_path );
1332 if (ret) *filename = src_path;
1333 return ret;
1337 /*******************************************************************
1338 * open_global_header
1340 * Open a file in the global include source directory.
1342 static struct file *open_global_header( const char *path, char **filename )
1344 struct incl_file *include = find_src_file( include_makefile, path );
1346 if (!include) return NULL;
1347 *filename = include->filename;
1348 return include->file;
1352 /*******************************************************************
1353 * open_global_generated_file
1355 * Open a generated file in the top-level source directory.
1357 static struct file *open_global_generated_file( const struct makefile *make, struct incl_file *file,
1358 const char *ext, const char *src_ext )
1360 struct incl_file *include;
1362 if (strendswith( file->name, ext ) &&
1363 (include = find_src_file( include_makefile, replace_extension( file->name, ext, src_ext ) )))
1365 file->sourcename = include->filename;
1366 file->filename = strmake( "include/%s", file->name );
1367 return include->file;
1369 return NULL;
1373 /*******************************************************************
1374 * open_src_file
1376 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1378 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1380 if (!file) fatal_perror( "open %s", pFile->name );
1381 return file;
1385 /*******************************************************************
1386 * find_importlib_module
1388 static struct makefile *find_importlib_module( const char *name )
1390 unsigned int i, len;
1392 for (i = 0; i < subdirs.count; i++)
1394 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1395 len = strlen(submakes[i]->obj_dir);
1396 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1397 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1399 return NULL;
1403 /*******************************************************************
1404 * open_include_file
1406 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1408 struct file *file = NULL;
1409 unsigned int i, len;
1411 errno = ENOENT;
1413 /* check for generated files */
1414 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1415 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1416 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1417 if (convert && rsvg && icotool)
1419 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1420 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1421 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1424 /* check for extra targets */
1425 if (strarray_exists( &make->extra_targets, pFile->name ))
1427 pFile->sourcename = src_dir_path( make, pFile->name );
1428 pFile->filename = obj_dir_path( make, pFile->name );
1429 return NULL;
1432 /* now try in source dir */
1433 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1435 /* check for global importlib (module dependency) */
1436 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1438 pFile->filename = pFile->name;
1439 return NULL;
1442 /* check for generated files in global includes */
1443 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1444 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1445 if (strendswith( pFile->name, "tmpl.h" ) &&
1446 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1448 /* check in global includes source dir */
1449 if ((file = open_global_header( pFile->name, &pFile->filename ))) return file;
1451 /* check in global msvcrt includes */
1452 if (pFile->use_msvcrt &&
1453 (file = open_global_header( strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1454 return file;
1456 /* now search in include paths */
1457 for (i = 0; i < make->include_paths.count; i++)
1459 const char *dir = make->include_paths.str[i];
1461 if (root_src_dir)
1463 len = strlen( root_src_dir );
1464 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1466 while (dir[len] == '/') len++;
1467 file = open_global_file( concat_paths( dir + len, pFile->name ), &pFile->filename );
1470 else
1472 if (*dir == '/') continue;
1473 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1475 if (!file) continue;
1476 pFile->is_external = 1;
1477 return file;
1480 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1482 /* try in src file directory */
1483 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1484 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1485 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1487 pFile->is_external = pFile->included_by->is_external;
1488 return file;
1491 if (make->extlib) return NULL; /* ignore missing files in external libs */
1493 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1494 perror( pFile->name );
1495 pFile = pFile->included_by;
1496 while (pFile && pFile->included_by)
1498 const char *parent = pFile->included_by->sourcename;
1499 if (!parent) parent = pFile->included_by->file->name;
1500 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1501 parent, pFile->included_line, pFile->name );
1502 pFile = pFile->included_by;
1504 exit(1);
1508 /*******************************************************************
1509 * add_all_includes
1511 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1513 unsigned int i;
1515 for (i = 0; i < file->deps_count; i++)
1517 switch (file->deps[i].type)
1519 case INCL_NORMAL:
1520 case INCL_IMPORT:
1521 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1522 break;
1523 case INCL_IMPORTLIB:
1524 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1525 break;
1526 case INCL_SYSTEM:
1527 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1528 break;
1529 case INCL_CPP_QUOTE:
1530 case INCL_CPP_QUOTE_SYSTEM:
1531 break;
1537 /*******************************************************************
1538 * parse_file
1540 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1542 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1544 if (!file) return;
1546 source->file = file;
1547 source->files_count = 0;
1548 source->files_size = file->deps_count;
1549 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1551 if (strendswith( file->name, ".m" )) file->flags |= FLAG_C_UNIX;
1552 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1553 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1555 if (source->sourcename)
1557 if (strendswith( source->sourcename, ".idl" ))
1559 unsigned int i;
1561 /* generated .h file always includes these */
1562 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1563 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1564 for (i = 0; i < file->deps_count; i++)
1566 switch (file->deps[i].type)
1568 case INCL_IMPORT:
1569 if (strendswith( file->deps[i].name, ".idl" ))
1570 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1571 file->deps[i].line, INCL_NORMAL );
1572 else
1573 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1574 break;
1575 case INCL_CPP_QUOTE:
1576 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1577 break;
1578 case INCL_CPP_QUOTE_SYSTEM:
1579 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1580 break;
1581 case INCL_NORMAL:
1582 case INCL_SYSTEM:
1583 case INCL_IMPORTLIB:
1584 break;
1587 return;
1589 if (strendswith( source->sourcename, ".y" ))
1590 return; /* generated .tab.h doesn't include anything */
1593 add_all_includes( make, source, file );
1597 /*******************************************************************
1598 * add_src_file
1600 * Add a source file to the list.
1602 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1604 struct incl_file *file = xmalloc( sizeof(*file) );
1606 memset( file, 0, sizeof(*file) );
1607 file->name = xstrdup(name);
1608 file->use_msvcrt = make->use_msvcrt;
1609 file->is_external = !!make->extlib;
1610 list_add_tail( &make->sources, &file->entry );
1611 if (make == include_makefile)
1613 unsigned int hash = hash_filename( name );
1614 list_add_tail( &global_includes[hash], &file->hash_entry );
1616 parse_file( make, file, 1 );
1617 return file;
1621 /*******************************************************************
1622 * open_input_makefile
1624 static FILE *open_input_makefile( const struct makefile *make )
1626 FILE *ret;
1628 if (make->obj_dir)
1629 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1630 else
1631 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1633 input_line = 0;
1634 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1635 return ret;
1639 /*******************************************************************
1640 * get_make_variable
1642 static const char *get_make_variable( const struct makefile *make, const char *name )
1644 const char *ret;
1646 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1647 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1648 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1649 return NULL;
1653 /*******************************************************************
1654 * get_expanded_make_variable
1656 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1658 const char *var;
1659 char *p, *end, *expand, *tmp;
1661 var = get_make_variable( make, name );
1662 if (!var) return NULL;
1664 p = expand = xstrdup( var );
1665 while ((p = strchr( p, '$' )))
1667 if (p[1] == '(')
1669 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1670 *end++ = 0;
1671 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1672 var = get_make_variable( make, p + 2 );
1673 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1674 /* switch to the new string */
1675 p = tmp + (p - expand);
1676 free( expand );
1677 expand = tmp;
1679 else if (p[1] == '{') /* don't expand ${} variables */
1681 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1682 p = end + 1;
1684 else if (p[1] == '$')
1686 p += 2;
1688 else fatal_error( "syntax error in '%s'\n", expand );
1691 /* consider empty variables undefined */
1692 p = expand;
1693 while (*p && isspace(*p)) p++;
1694 if (*p) return expand;
1695 free( expand );
1696 return NULL;
1700 /*******************************************************************
1701 * get_expanded_make_var_array
1703 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1705 struct strarray ret = empty_strarray;
1706 char *value, *token;
1708 if ((value = get_expanded_make_variable( make, name )))
1709 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1710 strarray_add( &ret, token );
1711 return ret;
1715 /*******************************************************************
1716 * get_expanded_file_local_var
1718 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1719 const char *name )
1721 char *p, *var = strmake( "%s_%s", file, name );
1723 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1724 return get_expanded_make_var_array( make, var );
1728 /*******************************************************************
1729 * get_expanded_arch_var
1731 static char *get_expanded_arch_var( const struct makefile *make, const char *name, int arch )
1733 return get_expanded_make_variable( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1737 /*******************************************************************
1738 * get_expanded_arch_var_array
1740 static struct strarray get_expanded_arch_var_array( const struct makefile *make, const char *name, int arch )
1742 return get_expanded_make_var_array( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1746 /*******************************************************************
1747 * set_make_variable
1749 static int set_make_variable( struct strarray *array, const char *assignment )
1751 char *p, *name;
1753 p = name = xstrdup( assignment );
1754 while (isalnum(*p) || *p == '_') p++;
1755 if (name == p) return 0; /* not a variable */
1756 if (isspace(*p))
1758 *p++ = 0;
1759 while (isspace(*p)) p++;
1761 if (*p != '=') return 0; /* not an assignment */
1762 *p++ = 0;
1763 while (isspace(*p)) p++;
1765 strarray_set_value( array, name, p );
1766 return 1;
1770 /*******************************************************************
1771 * parse_makefile
1773 static struct makefile *parse_makefile( const char *path )
1775 char *buffer;
1776 FILE *file;
1777 struct makefile *make = xmalloc( sizeof(*make) );
1779 memset( make, 0, sizeof(*make) );
1780 make->obj_dir = path;
1781 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1782 if (path && !strcmp( path, "include" )) include_makefile = make;
1784 file = open_input_makefile( make );
1785 while ((buffer = get_line( file )))
1787 if (!strncmp( buffer, separator, strlen(separator) )) break;
1788 if (*buffer == '\t') continue; /* command */
1789 while (isspace( *buffer )) buffer++;
1790 if (*buffer == '#') continue; /* comment */
1791 set_make_variable( &make->vars, buffer );
1793 fclose( file );
1794 input_file_name = NULL;
1795 return make;
1799 /*******************************************************************
1800 * add_generated_sources
1802 static void add_generated_sources( struct makefile *make )
1804 unsigned int i, arch;
1805 struct incl_file *source, *next, *file, *dlldata = NULL;
1806 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1807 int multiarch = archs.count > 1 && make->use_msvcrt;
1809 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1811 for (arch = 0; arch < archs.count; arch++)
1813 if (!arch != !multiarch) continue;
1814 if (source->file->flags & FLAG_IDL_CLIENT)
1816 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL, arch );
1817 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1818 add_all_includes( make, file, file->file );
1820 if (source->file->flags & FLAG_IDL_SERVER)
1822 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL, arch );
1823 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1824 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1825 add_all_includes( make, file, file->file );
1827 if (source->file->flags & FLAG_IDL_IDENT)
1829 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL, arch );
1830 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1831 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1832 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1833 add_all_includes( make, file, file->file );
1835 if (source->file->flags & FLAG_IDL_PROXY)
1837 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL, arch );
1838 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1839 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1840 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1841 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1842 add_all_includes( make, file, file->file );
1844 if (source->file->flags & FLAG_IDL_TYPELIB)
1846 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL, arch );
1848 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1850 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL, arch );
1852 if (source->file->flags & FLAG_IDL_REGISTER)
1854 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL, arch );
1858 /* now the arch-independent files */
1860 if ((source->file->flags & FLAG_IDL_PROXY) && !dlldata)
1862 dlldata = add_generated_source( make, "dlldata.o", "dlldata.c", 0 );
1863 add_dependency( dlldata->file, "objbase.h", INCL_NORMAL );
1864 add_dependency( dlldata->file, "rpcproxy.h", INCL_NORMAL );
1865 add_all_includes( make, dlldata, dlldata->file );
1867 if (source->file->flags & FLAG_IDL_HEADER)
1869 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1871 if (!source->file->flags && strendswith( source->name, ".idl" ))
1873 if (!strncmp( source->name, "wine/", 5 )) continue;
1874 source->file->flags = FLAG_IDL_HEADER | FLAG_INSTALL;
1875 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1877 if (strendswith( source->name, ".x" ))
1879 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL, 0 );
1881 if (strendswith( source->name, ".y" ))
1883 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL, 0 );
1884 /* steal the includes list from the source file */
1885 file->files_count = source->files_count;
1886 file->files_size = source->files_size;
1887 file->files = source->files;
1888 source->files_count = source->files_size = 0;
1889 source->files = NULL;
1891 if (strendswith( source->name, ".l" ))
1893 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL, 0 );
1894 /* steal the includes list from the source file */
1895 file->files_count = source->files_count;
1896 file->files_size = source->files_size;
1897 file->files = source->files;
1898 source->files_count = source->files_size = 0;
1899 source->files = NULL;
1901 if (strendswith( source->name, ".po" ))
1903 if (!make->disabled[0])
1904 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1906 if (strendswith( source->name, ".spec" ))
1908 char *obj = replace_extension( source->name, ".spec", "" );
1909 strarray_addall_uniq( &make->extra_imports,
1910 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1913 if (make->testdll)
1915 for (arch = 0; arch < archs.count; arch++)
1917 if (!arch != !multiarch) continue;
1918 file = add_generated_source( make, "testlist.o", "testlist.c", arch );
1919 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1920 add_all_includes( make, file, file->file );
1923 for (i = 0; i < objs.count; i++)
1925 /* default to .c for unknown extra object files */
1926 if (strendswith( objs.str[i], ".o" ))
1928 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ), 0);
1929 file->file->flags |= FLAG_C_UNIX;
1930 file->use_msvcrt = 0;
1932 else if (strendswith( objs.str[i], ".res" ))
1933 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL, 0 );
1934 else
1935 add_generated_source( make, objs.str[i], NULL, 0 );
1940 /*******************************************************************
1941 * create_dir
1943 static void create_dir( const char *dir )
1945 char *p, *path;
1947 p = path = xstrdup( dir );
1948 while ((p = strchr( p, '/' )))
1950 *p = 0;
1951 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1952 *p++ = '/';
1953 while (*p == '/') p++;
1955 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1956 free( path );
1960 /*******************************************************************
1961 * create_file_directories
1963 * Create the base directories of all the files.
1965 static void create_file_directories( const struct makefile *make, struct strarray files )
1967 struct strarray subdirs = empty_strarray;
1968 unsigned int i;
1969 char *dir;
1971 for (i = 0; i < files.count; i++)
1973 if (!strchr( files.str[i], '/' )) continue;
1974 dir = obj_dir_path( make, files.str[i] );
1975 *strrchr( dir, '/' ) = 0;
1976 strarray_add_uniq( &subdirs, dir );
1979 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1983 /*******************************************************************
1984 * output_filenames_obj_dir
1986 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1988 unsigned int i;
1990 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1994 /*******************************************************************
1995 * get_dependencies
1997 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1999 unsigned int i;
2001 if (!file->filename) return;
2003 if (file != source)
2005 if (file->owner == source) return; /* already processed */
2006 if (file->type == INCL_IMPORTLIB)
2008 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
2009 return; /* library is imported only when building a typelib */
2010 strarray_add( &source->importlibdeps, file->filename );
2012 else strarray_add( &source->dependencies, file->filename );
2013 file->owner = source;
2015 /* sanity checks */
2016 if (!strcmp( file->filename, "include/config.h" ) &&
2017 file != source->files[0] && !source->is_external)
2019 input_file_name = source->filename;
2020 input_line = 0;
2021 for (i = 0; i < source->file->deps_count; i++)
2023 if (!strcmp( source->file->deps[i].name, file->name ))
2024 input_line = source->file->deps[i].line;
2026 fatal_error( "%s must be included before other headers\n", file->name );
2030 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
2034 /*******************************************************************
2035 * get_local_dependencies
2037 * Get the local dependencies of a given target.
2039 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
2040 struct strarray targets )
2042 unsigned int i;
2043 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2045 for (i = 0; i < deps.count; i++)
2047 if (strarray_exists( &targets, deps.str[i] ))
2048 deps.str[i] = obj_dir_path( make, deps.str[i] );
2049 else
2050 deps.str[i] = src_dir_path( make, deps.str[i] );
2052 return deps;
2056 /*******************************************************************
2057 * get_static_lib
2059 * Check if makefile builds the named static library and return the full lib path.
2061 static const char *get_static_lib( const struct makefile *make, const char *name, unsigned int arch )
2063 if (!make->staticlib) return NULL;
2064 if (make->disabled[arch]) return NULL;
2065 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2066 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2067 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2068 return obj_dir_path( make, strmake( "%s%s", arch_dirs[arch], make->staticlib ));
2072 /*******************************************************************
2073 * get_native_unix_lib
2075 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2077 if (!make->unixlib) return NULL;
2078 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2079 if (make->unixlib[strlen(name)] != '.') return NULL;
2080 return obj_dir_path( make, make->unixlib );
2084 /*******************************************************************
2085 * get_parent_makefile
2087 static struct makefile *get_parent_makefile( struct makefile *make )
2089 char *dir, *p;
2090 unsigned int i;
2092 if (!make->obj_dir) return NULL;
2093 dir = xstrdup( make->obj_dir );
2094 if (!(p = strrchr( dir, '/' ))) return NULL;
2095 *p = 0;
2096 for (i = 0; i < subdirs.count; i++)
2097 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2098 return NULL;
2102 /*******************************************************************
2103 * needs_delay_lib
2105 static int needs_delay_lib( const struct makefile *make, unsigned int arch )
2107 if (delay_load_flags[arch]) return 0;
2108 if (!make->importlib) return 0;
2109 return strarray_exists( &delay_import_libs, make->importlib );
2113 /*******************************************************************
2114 * add_unix_libraries
2116 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2118 struct strarray ret = empty_strarray;
2119 struct strarray all_libs = empty_strarray;
2120 unsigned int i, j;
2122 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2123 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
2125 for (i = 0; i < all_libs.count; i++)
2127 const char *lib = NULL;
2129 if (!strncmp( all_libs.str[i], "-l", 2 ))
2131 for (j = 0; j < subdirs.count; j++)
2133 if (make == submakes[j]) continue;
2134 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2137 if (lib)
2139 strarray_add( deps, lib );
2140 strarray_add( &ret, lib );
2142 else strarray_add( &ret, all_libs.str[i] );
2145 strarray_addall( &ret, libs );
2146 return ret;
2150 /*******************************************************************
2151 * is_crt_module
2153 static int is_crt_module( const char *file )
2155 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2159 /*******************************************************************
2160 * get_default_crt
2162 static const char *get_default_crt( const struct makefile *make )
2164 if (!make->use_msvcrt) return NULL;
2165 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2166 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2170 /*******************************************************************
2171 * get_crt_define
2173 static const char *get_crt_define( const struct makefile *make )
2175 const char *crt_dll = NULL;
2176 unsigned int i, version = 0;
2178 for (i = 0; i < make->imports.count; i++)
2180 if (!is_crt_module( make->imports.str[i] )) continue;
2181 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2182 crt_dll, make->imports.str[i] );
2183 crt_dll = make->imports.str[i];
2186 if (!crt_dll)
2188 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2189 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2191 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2192 sscanf( crt_dll, "msvcr%u", &version );
2193 return strmake( "-D_MSVCR_VER=%u", version );
2197 /*******************************************************************
2198 * get_default_imports
2200 static struct strarray get_default_imports( const struct makefile *make, struct strarray imports )
2202 struct strarray ret = empty_strarray;
2203 const char *crt_dll = get_default_crt( make );
2204 unsigned int i;
2206 for (i = 0; i < imports.count; i++)
2207 if (is_crt_module( imports.str[i] ))
2208 crt_dll = imports.str[i];
2210 strarray_add( &ret, "winecrt0" );
2211 if (crt_dll) strarray_add( &ret, crt_dll );
2213 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2214 strarray_add( &ret, "kernel" );
2216 strarray_add( &ret, "kernel32" );
2217 strarray_add( &ret, "ntdll" );
2218 return ret;
2221 enum import_type
2223 IMPORT_TYPE_DIRECT,
2224 IMPORT_TYPE_DELAYED,
2225 IMPORT_TYPE_DEFAULT,
2228 /*******************************************************************
2229 * add_import_libs
2231 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2232 struct strarray imports, enum import_type type, unsigned int arch )
2234 struct strarray ret = empty_strarray;
2235 unsigned int i, j;
2237 for (i = 0; i < imports.count; i++)
2239 const char *name = imports.str[i];
2240 const char *lib = NULL;
2242 /* add crt import lib only when adding the default imports libs */
2243 if (is_crt_module( imports.str[i] ) && type != IMPORT_TYPE_DEFAULT) continue;
2245 if (name[0] == '-')
2247 switch (name[1])
2249 case 'L': strarray_add( &ret, name ); continue;
2250 case 'l': name += 2; break;
2251 default: continue;
2254 else name = get_base_name( name );
2256 for (j = 0; j < subdirs.count; j++)
2258 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2259 lib = obj_dir_path( submakes[j], strmake( "%slib%s.a", arch_dirs[arch], name ));
2260 else
2261 lib = get_static_lib( submakes[j], name, arch );
2262 if (lib) break;
2265 if (lib)
2267 const char *ext = NULL;
2269 if (type == IMPORT_TYPE_DELAYED && !delay_load_flags[arch]) ext = ".delay.a";
2270 if (ext) lib = replace_extension( lib, ".a", ext );
2271 strarray_add_uniq( deps, lib );
2272 strarray_add( &ret, lib );
2274 else strarray_add( &ret, strmake( "-l%s", name ));
2276 return ret;
2280 /*******************************************************************
2281 * add_install_rule
2283 static void add_install_rule( struct makefile *make, const char *target, unsigned int arch,
2284 const char *file, const char *dest )
2286 unsigned int i;
2288 if (make->disabled[arch]) return;
2290 for (i = 0; i < NB_INSTALL_RULES; i++)
2292 if (strarray_exists( &make->install[i], target ) ||
2293 strarray_exists( &top_install[i], make->obj_dir ) ||
2294 strarray_exists( &top_install[i], obj_dir_path( make, target )))
2296 strarray_add( &make->install_rules[i], file );
2297 strarray_add( &make->install_rules[i], dest );
2298 break;
2304 /*******************************************************************
2305 * get_include_install_path
2307 * Determine the installation path for a given include file.
2309 static const char *get_include_install_path( const char *name )
2311 if (!strncmp( name, "wine/", 5 )) return name + 5;
2312 if (!strncmp( name, "msvcrt/", 7 )) return name;
2313 return strmake( "windows/%s", name );
2317 /*******************************************************************
2318 * get_shared_library_name
2320 * Determine possible names for a shared library with a version number.
2322 static struct strarray get_shared_lib_names( const char *libname )
2324 struct strarray ret = empty_strarray;
2325 const char *ext, *p;
2326 char *name, *first, *second;
2327 size_t len = 0;
2329 strarray_add( &ret, libname );
2331 for (p = libname; (p = strchr( p, '.' )); p++)
2332 if ((len = strspn( p + 1, "0123456789." ))) break;
2334 if (!len) return ret;
2335 ext = p + 1 + len;
2336 if (*ext && ext[-1] == '.') ext--;
2338 /* keep only the first group of digits */
2339 name = xstrdup( libname );
2340 first = name + (p - libname);
2341 if ((second = strchr( first + 1, '.' )))
2343 strcpy( second, ext );
2344 strarray_add( &ret, xstrdup( name ));
2346 return ret;
2350 /*******************************************************************
2351 * get_source_defines
2353 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2354 const char *obj )
2356 unsigned int i;
2357 struct strarray ret = empty_strarray;
2359 strarray_addall( &ret, make->include_args );
2360 if (source->use_msvcrt)
2362 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2363 for (i = 0; i < make->include_paths.count; i++)
2364 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2366 strarray_addall( &ret, make->define_args );
2367 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2368 if (source->file->flags & FLAG_C_UNIX) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2369 return ret;
2373 /*******************************************************************
2374 * remove_warning_flags
2376 static struct strarray remove_warning_flags( struct strarray flags )
2378 unsigned int i;
2379 struct strarray ret = empty_strarray;
2381 for (i = 0; i < flags.count; i++)
2382 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2383 strarray_add( &ret, flags.str[i] );
2384 return ret;
2388 /*******************************************************************
2389 * get_debug_file
2391 static const char *get_debug_file( struct makefile *make, const char *name, unsigned int arch )
2393 const char *debug_file = NULL;
2394 if (!debug_flags[arch]) return NULL;
2395 if (!strcmp( debug_flags[arch], "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2396 else if (!strncmp( debug_flags[arch], "split", 5 )) debug_file = strmake( "%s.debug", name );
2397 if (debug_file) strarray_add( &make->debug_files, debug_file );
2398 return debug_file;
2402 /*******************************************************************
2403 * cmd_prefix
2405 static const char *cmd_prefix( const char *cmd )
2407 if (!silent_rules) return "";
2408 return strmake( "$(quiet_%s)", cmd );
2412 /*******************************************************************
2413 * output_winegcc_command
2415 static void output_winegcc_command( struct makefile *make, unsigned int arch )
2417 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2418 output_filename( "--wine-objdir ." );
2419 if (tools_dir)
2421 output_filename( "--winebuild" );
2422 output_filename( tools_path( make, "winebuild" ));
2424 output_filenames( target_flags[arch] );
2425 if (!arch) output_filenames( lddll_flags );
2429 /*******************************************************************
2430 * output_symlink_rule
2432 * Output a rule to create a symlink.
2434 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2436 const char *name = strrchr( link_name, '/' );
2437 char *dir = NULL;
2439 if (name)
2441 dir = xstrdup( link_name );
2442 dir[name - link_name] = 0;
2445 output( "\t%s", cmd_prefix( "LN" ));
2446 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2447 output( "rm -f %s && ", link_name );
2449 /* dest path with a directory needs special handling if ln -s isn't supported */
2450 if (dir && strcmp( ln_s, "ln -s" ))
2451 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2452 else
2453 output( "%s %s %s\n", ln_s, src_name, link_name );
2455 free( dir );
2459 /*******************************************************************
2460 * output_srcdir_symlink
2462 * Output rule to create a symlink back to the source directory, for source files
2463 * that are needed at run-time.
2465 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2467 char *src_file, *dst_file, *src_name;
2469 if (!make->src_dir) return;
2470 src_file = src_dir_path( make, obj );
2471 dst_file = obj_dir_path( make, obj );
2472 output( "%s: %s\n", dst_file, src_file );
2474 src_name = src_file;
2475 if (src_name[0] != '/' && make->obj_dir)
2476 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2478 output_symlink_rule( src_name, dst_file, 0 );
2479 strarray_add( &make->all_targets[0], obj );
2483 /*******************************************************************
2484 * output_install_commands
2486 static void output_install_commands( struct makefile *make, struct strarray files )
2488 unsigned int i, arch;
2489 char *install_sh = root_src_dir_path( "tools/install-sh" );
2491 for (i = 0; i < files.count; i += 2)
2493 const char *file = files.str[i];
2494 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2495 char type = *files.str[i + 1];
2497 switch (type)
2499 case '1': case '2': case '3': case '4': case '5':
2500 case '6': case '7': case '8': case '9': /* arch-dependent program */
2501 arch = type - '0';
2502 output( "\tSTRIPPROG=%s %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2503 strip_progs[arch], install_sh, obj_dir_path( make, file ), dest );
2504 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2505 break;
2506 case 'd': /* data file */
2507 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2508 install_sh, obj_dir_path( make, file ), dest );
2509 break;
2510 case 'D': /* data file in source dir */
2511 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2512 install_sh, src_dir_path( make, file ), dest );
2513 break;
2514 case '0': /* native arch program file */
2515 case 'p': /* program file */
2516 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2517 install_sh, obj_dir_path( make, file ), dest );
2518 break;
2519 case 's': /* script */
2520 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2521 install_sh, obj_dir_path( make, file ), dest );
2522 break;
2523 case 'S': /* script in source dir */
2524 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2525 install_sh, src_dir_path( make, file ), dest );
2526 break;
2527 case 't': /* script in tools dir */
2528 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2529 install_sh, tools_dir_path( make, files.str[i] ), dest );
2530 break;
2531 case 'y': /* symlink */
2532 output_symlink_rule( files.str[i], dest, 1 );
2533 break;
2534 default:
2535 assert(0);
2537 strarray_add( &make->uninstall_files, dest );
2542 /*******************************************************************
2543 * output_install_rules
2545 * Rules are stored as a (file,dest) pair of values.
2546 * The first char of dest indicates the type of install.
2548 static void output_install_rules( struct makefile *make, enum install_rules rules )
2550 unsigned int i;
2551 struct strarray files = make->install_rules[rules];
2552 struct strarray targets = empty_strarray;
2554 if (!files.count) return;
2556 for (i = 0; i < files.count; i += 2)
2558 const char *file = files.str[i];
2559 switch (*files.str[i + 1])
2561 case '0': case '1': case '2': case '3': case '4':
2562 case '5': case '6': case '7': case '8': case '9': /* arch-dependent program */
2563 case 'd': /* data file */
2564 case 'p': /* program file */
2565 case 's': /* script */
2566 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2567 break;
2568 case 't': /* script in tools dir */
2569 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2570 break;
2574 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, install_targets[rules] ));
2575 output_filenames( targets );
2576 output( "\n" );
2577 output_install_commands( make, files );
2578 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2579 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, install_targets[rules] ));
2583 static int cmp_string_length( const char **a, const char **b )
2585 int paths_a = 0, paths_b = 0;
2586 const char *p;
2588 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2589 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2590 if (paths_b != paths_a) return paths_b - paths_a;
2591 return strcmp( *a, *b );
2594 /*******************************************************************
2595 * get_removable_dirs
2597 * Retrieve a list of directories to try to remove after deleting the files.
2599 static struct strarray get_removable_dirs( struct strarray files )
2601 struct strarray dirs = empty_strarray;
2602 unsigned int i;
2604 for (i = 0; i < files.count; i++)
2606 char *dir = xstrdup( files.str[i] );
2607 while (strchr( dir, '/' ))
2609 *strrchr( dir, '/' ) = 0;
2610 strarray_add_uniq( &dirs, xstrdup(dir) );
2613 strarray_qsort( &dirs, cmp_string_length );
2614 return dirs;
2618 /*******************************************************************
2619 * output_uninstall_rules
2621 static void output_uninstall_rules( struct makefile *make )
2623 static const char *dirs_order[] =
2624 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2626 struct strarray uninstall_dirs;
2627 unsigned int i, j;
2629 if (!make->uninstall_files.count) return;
2630 output( "uninstall::\n" );
2631 output_rm_filenames( make->uninstall_files, "rm -f" );
2632 strarray_add_uniq( &make->phony_targets, "uninstall" );
2634 if (!subdirs.count) return;
2635 uninstall_dirs = get_removable_dirs( make->uninstall_files );
2636 output( "\t-rmdir" );
2637 for (i = 0; i < ARRAY_SIZE(dirs_order); i++)
2639 for (j = 0; j < uninstall_dirs.count; j++)
2641 if (!uninstall_dirs.str[j]) continue;
2642 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2643 continue;
2644 output_filename( uninstall_dirs.str[j] );
2645 uninstall_dirs.str[j] = NULL;
2648 for (j = 0; j < uninstall_dirs.count; j++)
2649 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2650 output( "\n" );
2654 /*******************************************************************
2655 * output_po_files
2657 static void output_po_files( struct makefile *make )
2659 const char *po_dir = src_dir_path( make, "po" );
2660 unsigned int i;
2662 if (linguas.count)
2664 for (i = 0; i < linguas.count; i++)
2665 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2666 output( ": %s/wine.pot\n", po_dir );
2667 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2668 cmd_prefix( "MSG" ), po_dir );
2669 output( "po/all:" );
2670 for (i = 0; i < linguas.count; i++)
2671 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2672 output( "\n" );
2674 output( "%s/wine.pot:", po_dir );
2675 output_filenames( make->pot_files );
2676 output( "\n" );
2677 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2678 output_filenames( make->pot_files );
2679 output( "\n" );
2680 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2684 /*******************************************************************
2685 * output_source_y
2687 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2689 /* add source file dependency for parallel makes */
2690 char *header = strmake( "%s.tab.h", obj );
2692 if (find_include_file( make, header ))
2694 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2695 output( "\t%s%s -o %s.tab.c -d %s\n",
2696 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2697 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2698 source->filename, obj_dir_path( make, header ));
2699 strarray_add( &make->clean_files, header );
2701 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2703 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2707 /*******************************************************************
2708 * output_source_l
2710 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2712 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2713 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2717 /*******************************************************************
2718 * output_source_h
2720 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2722 if (source->file->flags & FLAG_GENERATED)
2723 strarray_add( &make->all_targets[0], source->name );
2724 else if ((source->file->flags & FLAG_INSTALL) || strncmp( source->name, "wine/", 5 ))
2725 add_install_rule( make, source->name, 0, source->name,
2726 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2730 /*******************************************************************
2731 * output_source_rc
2733 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2735 struct strarray defines = get_source_defines( make, source, obj );
2736 const char *po_dir = NULL, *res_file = strmake( "%s.res", obj );
2737 unsigned int i, arch;
2739 if (source->file->flags & FLAG_RC_HEADER) return;
2740 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2741 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2742 for (arch = 0; arch < archs.count; arch++) strarray_add( &make->res_files[arch], res_file );
2743 if (source->file->flags & FLAG_RC_PO)
2745 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2746 output( "%s.pot ", obj_dir_path( make, obj ) );
2748 output( "%s: %s", obj_dir_path( make, res_file ), source->filename );
2749 output_filename( tools_path( make, "wrc" ));
2750 if (make->src_dir) output_filename( "nls/locale.nls" );
2751 output_filenames( source->dependencies );
2752 output( "\n" );
2753 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2754 if (make->is_win16) output_filename( "-m16" );
2755 output_filename( "--nostdinc" );
2756 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2757 output_filenames( defines );
2758 output_filename( source->filename );
2759 output( "\n" );
2760 if (po_dir)
2762 output( "%s:", obj_dir_path( make, res_file ));
2763 for (i = 0; i < linguas.count; i++)
2764 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2765 output( "\n" );
2770 /*******************************************************************
2771 * output_source_mc
2773 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2775 unsigned int i, arch;
2776 char *obj_path = obj_dir_path( make, obj );
2777 char *res_file = strmake( "%s.res", obj );
2779 for (arch = 0; arch < archs.count; arch++) strarray_add( &make->res_files[arch], res_file );
2780 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2781 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2782 output_filename( tools_path( make, "wmc" ));
2783 output_filenames( source->dependencies );
2784 output( "\n" );
2785 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2786 if (linguas.count)
2788 output_filename( "--po-dir=po" );
2789 output( "\n" );
2790 output( "%s.res:", obj_dir_path( make, obj ));
2791 for (i = 0; i < linguas.count; i++)
2792 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2794 output( "\n" );
2798 /*******************************************************************
2799 * output_source_res
2801 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2803 strarray_add( &make->res_files[source->arch], source->name );
2807 /*******************************************************************
2808 * output_source_idl
2810 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2812 struct strarray defines = get_source_defines( make, source, obj );
2813 struct strarray headers = empty_strarray;
2814 struct strarray multiarch_targets[MAX_ARCHS] = { empty_strarray };
2815 const char *dest;
2816 unsigned int i, arch;
2817 int multiarch;
2819 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2820 if (!source->file->flags) return;
2822 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2823 if (source->file->flags & FLAG_INSTALL)
2825 add_install_rule( make, source->name, 0, xstrdup( source->name ),
2826 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2827 if (source->file->flags & FLAG_IDL_HEADER)
2828 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2829 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2831 if (source->file->flags & FLAG_IDL_HEADER)
2833 dest = strmake( "%s.h", obj );
2834 strarray_add( &headers, dest );
2835 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2838 for (i = 0; i < ARRAY_SIZE(idl_outputs); i++)
2840 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2841 multiarch = (make->use_msvcrt && archs.count > 1);
2842 for (arch = 0; arch < archs.count; arch++)
2844 if (!arch != !multiarch) continue;
2845 dest = strmake( "%s%s%s", arch_dirs[arch], obj, idl_outputs[i].ext );
2846 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2847 strarray_add( &multiarch_targets[arch], dest );
2851 for (arch = 0; arch < archs.count; arch++)
2853 if (multiarch_targets[arch].count + (arch ? 0 : headers.count) == 0) continue;
2854 if (!arch) output_filenames_obj_dir( make, headers );
2855 output_filenames_obj_dir( make, multiarch_targets[arch] );
2856 output( ":\n" );
2857 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2858 output_filenames( target_flags[arch] );
2859 output_filename( "--nostdinc" );
2860 output_filename( "-Ldlls/\\*" );
2861 output_filenames( defines );
2862 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2863 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2864 output_filename( source->filename );
2865 output( "\n" );
2868 output_filenames_obj_dir( make, headers );
2869 for (arch = 0; arch < archs.count; arch++) output_filenames_obj_dir( make, multiarch_targets[arch] );
2870 output( ":" );
2871 output_filename( tools_path( make, "widl" ));
2872 output_filename( source->filename );
2873 output_filenames( source->dependencies );
2874 output( "\n" );
2876 if (source->importlibdeps.count)
2878 for (arch = 0; arch < archs.count; arch++)
2880 if (!multiarch_targets[arch].count) continue;
2881 output_filenames_obj_dir( make, multiarch_targets[arch] );
2882 output( ":" );
2883 for (i = 0; i < source->importlibdeps.count; i++)
2885 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2886 const char *module = strmake( "%s%s", arch_pe_dirs[arch], submake->module );
2887 output_filename( obj_dir_path( submake, module ));
2889 output( "\n" );
2895 /*******************************************************************
2896 * output_source_x
2898 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2900 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2901 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2902 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2903 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2904 if (source->file->flags & FLAG_INSTALL)
2906 add_install_rule( make, source->name, 0, source->name,
2907 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2908 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2909 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2914 /*******************************************************************
2915 * output_source_sfd
2917 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2919 unsigned int i;
2920 char *ttf_obj = strmake( "%s.ttf", obj );
2921 char *ttf_file = src_dir_path( make, ttf_obj );
2923 if (fontforge && !make->src_dir)
2925 output( "%s: %s\n", ttf_file, source->filename );
2926 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2927 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2928 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2929 strarray_add( &make->maintainerclean_files, ttf_obj );
2931 if (source->file->flags & FLAG_INSTALL)
2933 add_install_rule( make, source->name, 0, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2934 output_srcdir_symlink( make, ttf_obj );
2937 if (source->file->flags & FLAG_SFD_FONTS)
2939 struct strarray *array = source->file->args;
2941 for (i = 0; i < array->count; i++)
2943 char *font = strtok( xstrdup(array->str[i]), " \t" );
2944 char *args = strtok( NULL, "" );
2946 strarray_add( &make->all_targets[0], xstrdup( font ));
2947 output( "%s: %s %s\n", obj_dir_path( make, font ),
2948 tools_path( make, "sfnt2fon" ), ttf_file );
2949 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2950 tools_path( make, "sfnt2fon" ), ttf_file, args );
2951 add_install_rule( make, source->name, 0, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2957 /*******************************************************************
2958 * output_source_svg
2960 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2962 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2963 unsigned int i;
2965 if (convert && rsvg && icotool)
2967 for (i = 0; images[i]; i++)
2968 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2970 if (images[i])
2972 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2973 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2974 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2975 root_src_dir_path( "tools/buildimage" ), source->filename );
2976 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2982 /*******************************************************************
2983 * output_source_nls
2985 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2987 add_install_rule( make, source->name, 0, source->name,
2988 strmake( "D$(nlsdir)/%s", source->name ));
2989 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2993 /*******************************************************************
2994 * output_source_desktop
2996 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2998 add_install_rule( make, source->name, 0, source->name,
2999 strmake( "D$(datadir)/applications/%s", source->name ));
3003 /*******************************************************************
3004 * output_source_po
3006 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
3008 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
3009 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
3010 strarray_add( &make->all_targets[0], strmake( "%s.mo", obj ));
3014 /*******************************************************************
3015 * output_source_in
3017 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
3019 unsigned int i;
3021 if (make == include_makefile) return; /* ignore generated includes */
3022 if (strendswith( obj, ".man" ) && source->file->args)
3024 struct strarray symlinks;
3025 char *dir, *dest = replace_extension( obj, ".man", "" );
3026 char *lang = strchr( dest, '.' );
3027 char *section = source->file->args;
3028 if (lang)
3030 *lang++ = 0;
3031 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3033 else dir = strmake( "$(mandir)/man%s", section );
3034 add_install_rule( make, dest, 0, obj, strmake( "d%s/%s.%s", dir, dest, section ));
3035 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3036 for (i = 0; i < symlinks.count; i++)
3037 add_install_rule( make, symlinks.str[i], 0, strmake( "%s.%s", dest, section ),
3038 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3039 free( dest );
3040 free( dir );
3042 strarray_add( &make->in_files, obj );
3043 strarray_add( &make->all_targets[0], obj );
3044 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3045 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
3046 output( "%s:", obj_dir_path( make, obj ));
3047 output_filenames( source->dependencies );
3048 output( "\n" );
3049 add_install_rule( make, obj, 0, obj, strmake( "d$(datadir)/wine/%s", obj ));
3053 /*******************************************************************
3054 * output_source_spec
3056 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3058 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3059 struct strarray dll_flags = empty_strarray;
3060 struct strarray default_imports = empty_strarray;
3061 struct strarray all_libs, dep_libs;
3062 const char *dll_name, *obj_name, *res_name, *output_file, *debug_file;
3063 unsigned int arch;
3065 if (!imports.count) imports = make->imports;
3066 strarray_addall( &dll_flags, make->extradllflags );
3067 strarray_addall( &dll_flags, get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" ));
3068 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3070 for (arch = 0; arch < archs.count; arch++)
3072 if (!is_multiarch( arch )) continue;
3073 all_libs = dep_libs = empty_strarray;
3074 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ) );
3075 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3076 dll_name = arch_module_name( strmake( "%s.dll", obj ), arch );
3077 obj_name = obj_dir_path( make, strmake( "%s%s.o", arch_dirs[arch], obj ));
3078 res_name = strmake( "%s%s.res", arch_dirs[arch], obj );
3079 output_file = obj_dir_path( make, dll_name );
3081 strarray_add( &make->clean_files, dll_name );
3082 strarray_add( &make->res_files[arch], res_name );
3083 output( "%s:", obj_dir_path( make, res_name ));
3084 output_filename( output_file );
3085 output_filename( tools_path( make, "wrc" ));
3086 output( "\n" );
3087 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
3088 tools_path( make, "wrc" ));
3090 output( "%s:", output_file );
3091 output_filename( source->filename );
3092 output_filename( obj_name );
3093 output_filenames( dep_libs );
3094 output_filename( tools_path( make, "winebuild" ));
3095 output_filename( tools_path( make, "winegcc" ));
3096 output( "\n" );
3097 output_winegcc_command( make, arch );
3098 output_filename( "-s" );
3099 output_filenames( dll_flags );
3100 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3101 output_filename( "-shared" );
3102 output_filename( source->filename );
3103 output_filename( obj_name );
3104 if ((debug_file = get_debug_file( make, dll_name, arch )))
3105 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3106 output_filenames( all_libs );
3107 output_filename( arch_make_variable( "LDFLAGS", arch ));
3108 output( "\n" );
3113 /*******************************************************************
3114 * output_source_one_arch
3116 static void output_source_one_arch( struct makefile *make, struct incl_file *source, const char *obj,
3117 struct strarray defines, struct strarray *targets,
3118 unsigned int arch, int is_dll_src )
3120 const char *obj_name;
3122 if (arch)
3124 if (source->file->flags & FLAG_C_UNIX) return;
3125 if (!make->use_msvcrt && !make->staticlib && !(source->file->flags & FLAG_C_IMPLIB)) return;
3127 else if (source->file->flags & FLAG_C_UNIX)
3129 if (!*dll_ext) return;
3131 else if (archs.count > 1 && make->use_msvcrt &&
3132 !(source->file->flags & FLAG_C_IMPLIB) &&
3133 (!make->staticlib || make->extlib)) return;
3135 obj_name = strmake( "%s%s.o", source->arch ? "" : arch_dirs[arch], obj );
3136 strarray_add( targets, obj_name );
3138 if (source->file->flags & FLAG_C_UNIX)
3139 strarray_add( &make->unixobj_files, obj_name );
3140 else if (source->file->flags & FLAG_C_IMPLIB)
3141 strarray_add( &make->implib_files[arch], obj_name );
3142 else if (!is_dll_src)
3143 strarray_add( &make->object_files[arch], obj_name );
3144 else
3145 strarray_add( &make->clean_files, obj_name );
3147 output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename );
3148 output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), arch_make_variable( "CC", arch ), source->filename );
3149 output_filenames( defines );
3150 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3151 output_filenames( make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] );
3152 if (!arch)
3154 if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
3156 output_filenames( unix_dllflags );
3158 else if (make->module || make->testdll)
3160 output_filenames( dll_flags );
3161 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3162 if (!*dll_ext && make->module && is_crt_module( make->module ))
3163 output_filename( "-fno-builtin" );
3166 else
3168 if (make->module && is_crt_module( make->module )) output_filename( "-fno-builtin" );
3171 /* force -Wformat when using 'long' types, until all modules have been converted
3172 * and we can remove -Wno-format */
3173 if (!make->extlib && strarray_exists( &extra_cflags[arch], "-Wno-format" ) &&
3174 !strarray_exists( &defines, "-DWINE_NO_LONG_TYPES" ))
3175 output_filename( "-Wformat" );
3177 output_filenames( cpp_flags );
3178 output_filename( arch_make_variable( "CFLAGS", arch ));
3179 output( "\n" );
3181 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3182 !(source->file->flags & FLAG_GENERATED))
3184 const char *ok_file, *test_exe;
3186 ok_file = strmake( "%s%s.ok", arch_dirs[arch], obj );
3187 test_exe = replace_extension( make->testdll, ".dll", "_test.exe" );
3188 strarray_add( &make->ok_files, ok_file );
3189 output( "%s:\n", obj_dir_path( make, ok_file ));
3190 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s %s && touch $@\n",
3191 cmd_prefix( "TEST" ),
3192 root_src_dir_path( "tools/runtest" ), make->testdll,
3193 obj_dir_path( make, arch_module_name( test_exe, arch )), obj );
3198 /*******************************************************************
3199 * output_source_default
3201 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3203 struct strarray defines = get_source_defines( make, source, obj );
3204 struct strarray targets = empty_strarray;
3205 int is_dll_src = (make->testdll && strendswith( source->name, ".c" ) &&
3206 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3207 unsigned int arch;
3209 for (arch = 0; arch < archs.count; arch++)
3210 if (!source->arch || source->arch == arch)
3211 output_source_one_arch( make, source, obj, defines, &targets, arch, is_dll_src );
3213 if (source->file->flags & FLAG_GENERATED)
3215 if (!make->testdll || !strendswith( source->filename, "testlist.c" ))
3216 strarray_add( &make->clean_files, source->basename );
3218 else
3220 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ))
3221 strarray_add( &make->test_files, obj );
3224 output_filenames_obj_dir( make, targets );
3225 output( ":" );
3226 output_filenames( source->dependencies );
3227 output( "\n" );
3231 /* dispatch table to output rules for a single source file */
3232 static const struct
3234 const char *ext;
3235 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3236 } output_source_funcs[] =
3238 { "y", output_source_y },
3239 { "l", output_source_l },
3240 { "h", output_source_h },
3241 { "rh", output_source_h },
3242 { "inl", output_source_h },
3243 { "rc", output_source_rc },
3244 { "mc", output_source_mc },
3245 { "res", output_source_res },
3246 { "idl", output_source_idl },
3247 { "sfd", output_source_sfd },
3248 { "svg", output_source_svg },
3249 { "nls", output_source_nls },
3250 { "desktop", output_source_desktop },
3251 { "po", output_source_po },
3252 { "in", output_source_in },
3253 { "x", output_source_x },
3254 { "spec", output_source_spec },
3255 { NULL, output_source_default }
3259 /*******************************************************************
3260 * output_fake_module
3262 static void output_fake_module( struct makefile *make )
3264 unsigned int arch = 0; /* fake modules are always native */
3265 const char *spec_file = NULL, *name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3267 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3269 strarray_add( &make->all_targets[arch], name );
3270 add_install_rule( make, make->module, arch, name, strmake( "d$(dlldir)/%s", name ));
3272 output( "%s:", obj_dir_path( make, name ));
3273 if (spec_file) output_filename( spec_file );
3274 output_filenames_obj_dir( make, make->res_files[arch] );
3275 output_filename( tools_path( make, "winebuild" ));
3276 output_filename( tools_path( make, "winegcc" ));
3277 output( "\n" );
3278 output_winegcc_command( make, arch );
3279 output_filename( "-Wb,--fake-module" );
3280 if (spec_file)
3282 output_filename( "-shared" );
3283 output_filename( spec_file );
3285 output_filenames( make->extradllflags );
3286 output_filenames_obj_dir( make, make->res_files[arch] );
3287 output( "\n" );
3291 /*******************************************************************
3292 * output_module
3294 static void output_module( struct makefile *make, unsigned int arch )
3296 struct strarray default_imports = empty_strarray;
3297 struct strarray all_libs = empty_strarray;
3298 struct strarray dep_libs = empty_strarray;
3299 struct strarray imports = make->imports;
3300 const char *module_name;
3301 const char *debug_file;
3302 char *spec_file = NULL;
3303 unsigned int i;
3305 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3307 if (!make->data_only)
3309 module_name = arch_module_name( make->module, arch );
3311 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3313 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ));
3314 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, IMPORT_TYPE_DELAYED, arch ));
3315 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3317 if (!make->use_msvcrt)
3319 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3320 strarray_addall( &all_libs, libs );
3323 if (delay_load_flags[arch])
3325 for (i = 0; i < make->delayimports.count; i++)
3326 strarray_add( &all_libs, strmake( "%s%s%s", delay_load_flags[arch], make->delayimports.str[i],
3327 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3330 else module_name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3332 strarray_add( &make->all_targets[arch], module_name );
3333 if (make->data_only)
3334 add_install_rule( make, make->module, arch, module_name,
3335 strmake( "d$(dlldir)/%s%s", arch_pe_dirs[arch], make->module ));
3336 else
3337 add_install_rule( make, make->module, arch, module_name,
3338 strmake( "%c%s%s%s", '0' + arch, arch_install_dirs[arch], make->module,
3339 arch ? "" : dll_ext ));
3341 output( "%s:", obj_dir_path( make, module_name ));
3342 if (spec_file) output_filename( spec_file );
3343 output_filenames_obj_dir( make, make->object_files[arch] );
3344 output_filenames_obj_dir( make, make->res_files[arch] );
3345 output_filenames( dep_libs );
3346 output_filename( tools_path( make, "winebuild" ));
3347 output_filename( tools_path( make, "winegcc" ));
3348 output( "\n" );
3349 output_winegcc_command( make, arch );
3350 if (arch) output_filename( "-Wl,--wine-builtin" );
3351 if (spec_file)
3353 output_filename( "-shared" );
3354 output_filename( spec_file );
3356 output_filenames( make->extradllflags );
3357 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3358 output_filenames_obj_dir( make, make->object_files[arch] );
3359 output_filenames_obj_dir( make, make->res_files[arch] );
3360 debug_file = get_debug_file( make, module_name, arch );
3361 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3362 output_filenames( all_libs );
3363 output_filename( arch_make_variable( "LDFLAGS", arch ));
3364 output( "\n" );
3366 if (!make->data_only && !arch && *dll_ext) output_fake_module( make );
3370 /*******************************************************************
3371 * output_import_lib
3373 static void output_import_lib( struct makefile *make, unsigned int arch )
3375 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3376 const char *name = strmake( "%slib%s.a", arch_dirs[arch], make->importlib );
3378 strarray_add( &make->clean_files, name );
3379 if (needs_delay_lib( make, arch ))
3381 const char *delay_name = replace_extension( name, ".a", ".delay.a" );
3382 strarray_add( &make->clean_files, delay_name );
3383 output( "%s ", obj_dir_path( make, delay_name ));
3385 output( "%s: %s %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ), spec_file );
3386 output_filenames_obj_dir( make, make->implib_files[arch] );
3387 output( "\n" );
3388 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3389 if (!delay_load_flags[arch]) output_filename( "--without-dlltool" );
3390 output_filenames( target_flags[arch] );
3391 if (make->is_win16) output_filename( "-m16" );
3392 output_filename( "--export" );
3393 output_filename( spec_file );
3394 output_filenames_obj_dir( make, make->implib_files[arch] );
3395 output( "\n" );
3397 if (!arch && is_native_arch_disabled( make )) return;
3399 add_install_rule( make, make->importlib, arch, name,
3400 strmake( "d%slib%s.a", arch_install_dirs[arch], make->importlib ));
3404 /*******************************************************************
3405 * output_unix_lib
3407 static void output_unix_lib( struct makefile *make )
3409 struct strarray unix_deps = empty_strarray;
3410 struct strarray unix_libs = add_unix_libraries( make, &unix_deps );
3411 unsigned int arch = 0; /* unix libs are always native */
3413 strarray_add( &make->all_targets[arch], make->unixlib );
3414 add_install_rule( make, make->module, arch, make->unixlib,
3415 strmake( "p%s%s", arch_install_dirs[arch], make->unixlib ));
3416 output( "%s:", obj_dir_path( make, make->unixlib ));
3417 output_filenames_obj_dir( make, make->unixobj_files );
3418 output_filenames( unix_deps );
3419 output( "\n" );
3420 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3421 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3422 output_filenames_obj_dir( make, make->unixobj_files );
3423 output_filenames( unix_libs );
3424 output_filename( "$(LDFLAGS)" );
3425 output( "\n" );
3429 /*******************************************************************
3430 * output_static_lib
3432 static void output_static_lib( struct makefile *make, unsigned int arch )
3434 const char *name = strmake( "%s%s", arch_dirs[arch], make->staticlib );
3436 strarray_add( &make->clean_files, name );
3437 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3438 output_filenames_obj_dir( make, make->object_files[arch] );
3439 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3440 output( "\n" );
3441 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3442 output_filenames( target_flags[arch] );
3443 output_filenames_obj_dir( make, make->object_files[arch] );
3444 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3445 output( "\n" );
3446 if (!make->extlib)
3447 add_install_rule( make, make->staticlib, arch, name,
3448 strmake( "d%s%s", arch_install_dirs[arch], make->staticlib ));
3452 /*******************************************************************
3453 * output_shared_lib
3455 static void output_shared_lib( struct makefile *make )
3457 unsigned int i;
3458 char *basename, *p;
3459 struct strarray names = get_shared_lib_names( make->sharedlib );
3460 struct strarray all_libs = empty_strarray;
3461 struct strarray dep_libs = empty_strarray;
3462 unsigned int arch = 0; /* shared libs are always native */
3464 basename = xstrdup( make->sharedlib );
3465 if ((p = strchr( basename, '.' ))) *p = 0;
3467 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3468 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3469 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3470 strarray_addall( &all_libs, libs );
3472 output( "%s:", obj_dir_path( make, make->sharedlib ));
3473 output_filenames_obj_dir( make, make->object_files[arch] );
3474 output_filenames( dep_libs );
3475 output( "\n" );
3476 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3477 output_filenames_obj_dir( make, make->object_files[arch] );
3478 output_filenames( all_libs );
3479 output_filename( "$(LDFLAGS)" );
3480 output( "\n" );
3481 add_install_rule( make, make->sharedlib, arch, make->sharedlib,
3482 strmake( "p%s%s", arch_install_dirs[arch], make->sharedlib ));
3483 for (i = 1; i < names.count; i++)
3485 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3486 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3487 add_install_rule( make, names.str[i], arch, names.str[i-1],
3488 strmake( "y%s%s", arch_install_dirs[arch], names.str[i] ));
3490 strarray_addall( &make->all_targets[arch], names );
3494 /*******************************************************************
3495 * output_test_module
3497 static void output_test_module( struct makefile *make, unsigned int arch )
3499 char *basemodule = replace_extension( make->testdll, ".dll", "" );
3500 char *stripped = arch_module_name( strmake( "%s_test-stripped.exe", basemodule ), arch );
3501 char *testmodule = arch_module_name( strmake( "%s_test.exe", basemodule ), arch );
3502 struct strarray default_imports = get_default_imports( make, make->imports );
3503 struct strarray dep_libs = empty_strarray;
3504 struct strarray all_libs = empty_strarray;
3505 struct makefile *parent = get_parent_makefile( make );
3506 const char *debug_file;
3508 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, IMPORT_TYPE_DIRECT, arch ) );
3509 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3511 strarray_add( &make->all_targets[arch], testmodule );
3512 strarray_add( &make->clean_files, stripped );
3513 output( "%s:\n", obj_dir_path( make, testmodule ));
3514 output_winegcc_command( make, arch );
3515 output_filenames( make->extradllflags );
3516 output_filenames_obj_dir( make, make->object_files[arch] );
3517 output_filenames_obj_dir( make, make->res_files[arch] );
3518 if ((debug_file = get_debug_file( make, testmodule, arch )))
3519 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3520 output_filenames( all_libs );
3521 output_filename( arch_make_variable( "LDFLAGS", arch ));
3522 output( "\n" );
3523 output( "%s:\n", obj_dir_path( make, stripped ));
3524 output_winegcc_command( make, arch );
3525 output_filename( "-s" );
3526 output_filename( strmake( "-Wb,-F,%s_test.exe", basemodule ));
3527 output_filenames( make->extradllflags );
3528 output_filenames_obj_dir( make, make->object_files[arch] );
3529 output_filenames_obj_dir( make, make->res_files[arch] );
3530 output_filenames( all_libs );
3531 output_filename( arch_make_variable( "LDFLAGS", arch ));
3532 output( "\n" );
3533 output( "%s %s:", obj_dir_path( make, testmodule ), obj_dir_path( make, stripped ));
3534 output_filenames_obj_dir( make, make->object_files[arch] );
3535 output_filenames_obj_dir( make, make->res_files[arch] );
3536 output_filenames( dep_libs );
3537 output_filename( tools_path( make, "winebuild" ));
3538 output_filename( tools_path( make, "winegcc" ));
3539 output( "\n" );
3541 output( "programs/winetest/%s%s_test.res: %s\n", arch_dirs[arch], basemodule,
3542 obj_dir_path( make, stripped ));
3543 output( "\t%secho \"%s_test.exe TESTRES \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3544 basemodule, obj_dir_path( make, stripped ), tools_path( make, "wrc" ));
3546 output_filenames_obj_dir( make, make->ok_files );
3547 output( ": %s", obj_dir_path( make, testmodule ));
3548 if (parent)
3550 char *parent_module = arch_module_name( make->testdll, parent->use_msvcrt ? arch : 0 );
3551 output_filename( obj_dir_path( parent, parent_module ));
3552 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3554 output( "\n" );
3555 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3556 if (!make->disabled[arch] && parent && !parent->disabled[arch])
3557 output_filenames_obj_dir( make, make->ok_files );
3558 output( "\n" );
3559 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3560 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3561 output( "%s::\n", obj_dir_path( make, "testclean" ));
3562 output( "\trm -f" );
3563 output_filenames_obj_dir( make, make->ok_files );
3564 output( "\n" );
3565 strarray_addall( &make->clean_files, make->ok_files );
3566 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3570 /*******************************************************************
3571 * output_programs
3573 static void output_programs( struct makefile *make )
3575 unsigned int i, j;
3576 unsigned int arch = 0; /* programs are always native */
3578 for (i = 0; i < make->programs.count; i++)
3580 char *program_installed = NULL;
3581 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3582 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3583 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3584 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3585 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3587 if (!objs.count) objs = make->object_files[arch];
3588 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3590 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3591 strarray_addall( &all_libs, libs );
3594 output( "%s:", obj_dir_path( make, program ) );
3595 output_filenames_obj_dir( make, objs );
3596 output_filenames( deps );
3597 output( "\n" );
3598 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3599 output_filenames_obj_dir( make, objs );
3600 output_filenames( all_libs );
3601 output_filename( "$(LDFLAGS)" );
3602 output( "\n" );
3603 strarray_add( &make->all_targets[arch], program );
3605 for (j = 0; j < symlinks.count; j++)
3607 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3608 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3610 strarray_addall( &make->all_targets[arch], symlinks );
3612 add_install_rule( make, program, arch, program_installed ? program_installed : program,
3613 strmake( "p$(bindir)/%s", program ));
3614 for (j = 0; j < symlinks.count; j++)
3615 add_install_rule( make, symlinks.str[j], arch, program,
3616 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3621 /*******************************************************************
3622 * output_subdirs
3624 static void output_subdirs( struct makefile *make )
3626 struct strarray all_targets = empty_strarray;
3627 struct strarray makefile_deps = empty_strarray;
3628 struct strarray clean_files = empty_strarray;
3629 struct strarray testclean_files = empty_strarray;
3630 struct strarray distclean_files = empty_strarray;
3631 struct strarray distclean_dirs = empty_strarray;
3632 struct strarray dependencies = empty_strarray;
3633 struct strarray install_deps[NB_INSTALL_RULES] = { empty_strarray };
3634 struct strarray tooldeps_deps = empty_strarray;
3635 struct strarray buildtest_deps = empty_strarray;
3636 unsigned int i, j, arch;
3638 strarray_addall( &clean_files, make->clean_files );
3639 strarray_addall( &distclean_files, make->distclean_files );
3640 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3641 for (i = 0; i < subdirs.count; i++)
3643 struct strarray subclean = empty_strarray;
3644 strarray_addall( &subclean, get_removable_dirs( submakes[i]->clean_files ));
3645 strarray_addall( &subclean, get_removable_dirs( submakes[i]->distclean_files ));
3646 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3647 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3648 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3649 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3650 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3651 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3652 strarray_addall_path( &distclean_dirs, submakes[i]->obj_dir, subclean );
3653 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3654 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3655 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3657 for (arch = 0; arch < archs.count; arch++)
3659 if (submakes[i]->disabled[arch]) continue;
3660 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets[arch] );
3662 if (submakes[i]->disabled[0]) continue;
3664 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3665 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3666 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3667 if (submakes[i]->testdll)
3668 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3669 for (j = 0; j < NB_INSTALL_RULES; j++)
3670 if (submakes[i]->install_rules[j].count)
3671 strarray_add( &install_deps[j], obj_dir_path( submakes[i], install_targets[j] ));
3673 strarray_addall( &dependencies, makefile_deps );
3674 output( "all:" );
3675 output_filenames( all_targets );
3676 output( "\n" );
3677 output( "Makefile:" );
3678 output_filenames( makefile_deps );
3679 output( "\n" );
3680 output_filenames( dependencies );
3681 output( ":\n" );
3682 for (j = 0; j < NB_INSTALL_RULES; j++)
3684 if (!install_deps[j].count) continue;
3685 if (strcmp( install_targets[j], "install-test" ))
3687 output( "install " );
3688 strarray_add_uniq( &make->phony_targets, "install" );
3690 output( "%s::", install_targets[j] );
3691 output_filenames( install_deps[j] );
3692 output( "\n" );
3693 strarray_add_uniq( &make->phony_targets, install_targets[j] );
3695 output_uninstall_rules( make );
3696 if (buildtest_deps.count)
3698 output( "buildtests:" );
3699 output_filenames( buildtest_deps );
3700 output( "\n" );
3701 strarray_add_uniq( &make->phony_targets, "buildtests" );
3703 output( "check test:" );
3704 output_filenames( testclean_files );
3705 output( "\n" );
3706 strarray_add_uniq( &make->phony_targets, "check" );
3707 strarray_add_uniq( &make->phony_targets, "test" );
3709 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3711 output( "clean::\n");
3712 output_rm_filenames( clean_files, "rm -f" );
3713 output( "testclean::\n");
3714 output_rm_filenames( testclean_files, "rm -f" );
3715 output( "distclean::\n");
3716 output_rm_filenames( distclean_files, "rm -f" );
3717 output_rm_filenames( distclean_dirs, "-rmdir 2>/dev/null" );
3718 output( "maintainer-clean::\n");
3719 output_rm_filenames( make->maintainerclean_files, "rm -f" );
3720 strarray_add_uniq( &make->phony_targets, "distclean" );
3721 strarray_add_uniq( &make->phony_targets, "testclean" );
3722 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3724 if (tooldeps_deps.count)
3726 output( "__tooldeps__:" );
3727 output_filenames( tooldeps_deps );
3728 output( "\n" );
3729 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3732 if (make->phony_targets.count)
3734 output( ".PHONY:" );
3735 output_filenames( make->phony_targets );
3736 output( "\n" );
3741 /*******************************************************************
3742 * output_sources
3744 static void output_sources( struct makefile *make )
3746 struct strarray all_targets = empty_strarray;
3747 struct incl_file *source;
3748 unsigned int i, j, arch;
3750 strarray_add_uniq( &make->phony_targets, "all" );
3752 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3754 char *obj = xstrdup( source->name );
3755 char *ext = get_extension( obj );
3757 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3758 *ext++ = 0;
3760 for (j = 0; output_source_funcs[j].ext; j++)
3761 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3763 output_source_funcs[j].fn( make, source, obj );
3764 strarray_addall_uniq( &make->dependencies, source->dependencies );
3767 /* special case for winetest: add resource files from other test dirs */
3768 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3770 for (arch = 0; arch < archs.count; arch++)
3772 if (!is_multiarch( arch )) continue;
3773 for (i = 0; i < subdirs.count; i++)
3775 if (!submakes[i]->testdll) continue;
3776 if (submakes[i]->disabled[arch]) continue;
3777 if (enable_tests.count && !strarray_exists( &enable_tests, submakes[i]->testdll )) continue;
3778 strarray_add( &make->res_files[arch],
3779 strmake( "%s%s", arch_dirs[arch],
3780 replace_extension( submakes[i]->testdll, ".dll", "_test.res" )));
3785 if (make->dlldata_files.count)
3787 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3788 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3789 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3790 output_filenames( make->dlldata_files );
3791 output( "\n" );
3794 if (make->staticlib)
3796 for (arch = 0; arch < archs.count; arch++)
3797 if (is_multiarch( arch ) || !make->extlib) output_static_lib( make, arch );
3799 else if (make->module)
3801 if (!make->use_msvcrt) output_module( make, 0 );
3802 else
3804 for (arch = 0; arch < archs.count; arch++)
3805 if (is_multiarch( arch )) output_module( make, arch );
3807 if (make->unixlib) output_unix_lib( make );
3808 if (make->importlib) for (arch = 0; arch < archs.count; arch++) output_import_lib( make, arch );
3809 if (make->is_exe && !make->is_win16 && *dll_ext && strendswith( make->module, ".exe" ))
3811 char *binary = replace_extension( make->module, ".exe", "" );
3812 add_install_rule( make, binary, 0, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3815 else if (make->testdll)
3817 for (arch = 0; arch < archs.count; arch++)
3818 if (is_multiarch( arch )) output_test_module( make, arch );
3820 else if (make->sharedlib) output_shared_lib( make );
3821 else if (make->programs.count) output_programs( make );
3823 for (i = 0; i < make->scripts.count; i++)
3824 add_install_rule( make, make->scripts.str[i], 0, make->scripts.str[i],
3825 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3827 for (i = 0; i < make->extra_targets.count; i++)
3828 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3829 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3830 else
3831 strarray_add( &make->all_targets[0], make->extra_targets.str[i] );
3833 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3834 strarray_add( &make->distclean_files, "Makefile" );
3835 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3837 if (!make->obj_dir)
3838 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3839 else if (!strcmp( make->obj_dir, "po" ))
3840 strarray_add( &make->distclean_files, "LINGUAS" );
3842 for (arch = 0; arch < archs.count; arch++)
3844 strarray_addall_uniq( &make->clean_files, make->object_files[arch] );
3845 strarray_addall_uniq( &make->clean_files, make->implib_files[arch] );
3846 strarray_addall_uniq( &make->clean_files, make->res_files[arch] );
3847 strarray_addall_uniq( &make->clean_files, make->all_targets[arch] );
3849 strarray_addall( &make->clean_files, make->unixobj_files );
3850 strarray_addall( &make->clean_files, make->pot_files );
3851 strarray_addall( &make->clean_files, make->debug_files );
3853 if (make == top_makefile)
3855 output_subdirs( make );
3856 return;
3859 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3860 strarray_addall( &all_targets, make->font_files );
3861 if (all_targets.count)
3863 output( "%s:", obj_dir_path( make, "all" ));
3864 output_filenames_obj_dir( make, all_targets );
3865 output( "\n" );
3866 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3868 for (i = 0; i < NB_INSTALL_RULES; i++) output_install_rules( make, i );
3870 if (make->clean_files.count)
3872 output( "%s::\n", obj_dir_path( make, "clean" ));
3873 output( "\trm -f" );
3874 output_filenames_obj_dir( make, make->clean_files );
3875 output( "\n" );
3876 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3881 /*******************************************************************
3882 * create_temp_file
3884 static FILE *create_temp_file( const char *orig )
3886 char *name = xmalloc( strlen(orig) + 13 );
3887 unsigned int i, id = getpid();
3888 int fd;
3889 FILE *ret = NULL;
3891 for (i = 0; i < 100; i++)
3893 sprintf( name, "%s.tmp%08x", orig, id );
3894 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3896 ret = fdopen( fd, "w" );
3897 break;
3899 if (errno != EEXIST) break;
3900 id += 7777;
3902 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3903 temp_file_name = name;
3904 return ret;
3908 /*******************************************************************
3909 * rename_temp_file
3911 static void rename_temp_file( const char *dest )
3913 int ret = rename( temp_file_name, dest );
3914 if (ret == -1 && errno == EEXIST)
3916 /* rename doesn't overwrite on windows */
3917 unlink( dest );
3918 ret = rename( temp_file_name, dest );
3920 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3921 temp_file_name = NULL;
3925 /*******************************************************************
3926 * are_files_identical
3928 static int are_files_identical( FILE *file1, FILE *file2 )
3930 for (;;)
3932 char buffer1[8192], buffer2[8192];
3933 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3934 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3935 if (size1 != size2) return 0;
3936 if (!size1) return feof( file1 ) && feof( file2 );
3937 if (memcmp( buffer1, buffer2, size1 )) return 0;
3942 /*******************************************************************
3943 * rename_temp_file_if_changed
3945 static void rename_temp_file_if_changed( const char *dest )
3947 FILE *file1, *file2;
3948 int do_rename = 1;
3950 if ((file1 = fopen( dest, "r" )))
3952 if ((file2 = fopen( temp_file_name, "r" )))
3954 do_rename = !are_files_identical( file1, file2 );
3955 fclose( file2 );
3957 fclose( file1 );
3959 if (!do_rename)
3961 unlink( temp_file_name );
3962 temp_file_name = NULL;
3964 else rename_temp_file( dest );
3968 /*******************************************************************
3969 * output_linguas
3971 static void output_linguas( const struct makefile *make )
3973 const char *dest = obj_dir_path( make, "LINGUAS" );
3974 struct incl_file *source;
3976 output_file = create_temp_file( dest );
3978 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3979 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3980 if (strendswith( source->name, ".po" ))
3981 output( "%s\n", replace_extension( source->name, ".po", "" ));
3983 if (fclose( output_file )) fatal_perror( "write" );
3984 output_file = NULL;
3985 rename_temp_file_if_changed( dest );
3989 /*******************************************************************
3990 * output_testlist
3992 static void output_testlist( const struct makefile *make )
3994 const char *dest = obj_dir_path( make, "testlist.c" );
3995 unsigned int i;
3997 output_file = create_temp_file( dest );
3999 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
4000 output( "#define WIN32_LEAN_AND_MEAN\n" );
4001 output( "#include <windows.h>\n\n" );
4002 output( "#define STANDALONE\n" );
4003 output( "#include \"wine/test.h\"\n\n" );
4005 for (i = 0; i < make->test_files.count; i++)
4006 output( "extern void func_%s(void);\n", make->test_files.str[i] );
4007 output( "\n" );
4008 output( "const struct test winetest_testlist[] =\n" );
4009 output( "{\n" );
4010 for (i = 0; i < make->test_files.count; i++)
4011 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
4012 output( " { 0, 0 }\n" );
4013 output( "};\n" );
4015 if (fclose( output_file )) fatal_perror( "write" );
4016 output_file = NULL;
4017 rename_temp_file_if_changed( dest );
4021 /*******************************************************************
4022 * output_gitignore
4024 static void output_gitignore( const char *dest, struct strarray files )
4026 unsigned int i;
4028 output_file = create_temp_file( dest );
4030 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4031 for (i = 0; i < files.count; i++)
4033 if (!strchr( files.str[i], '/' )) output( "/" );
4034 output( "%s\n", files.str[i] );
4037 if (fclose( output_file )) fatal_perror( "write" );
4038 output_file = NULL;
4039 rename_temp_file( dest );
4043 /*******************************************************************
4044 * output_stub_makefile
4046 static void output_stub_makefile( struct makefile *make )
4048 struct strarray targets = empty_strarray;
4049 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
4050 unsigned int i, arch;
4052 if (make->obj_dir) create_dir( make->obj_dir );
4054 output_file_name = obj_dir_path( make, "Makefile" );
4055 output_file = create_temp_file( output_file_name );
4057 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
4059 if (make_var) output( "MAKE = %s\n\n", make_var );
4060 output( "all:\n" );
4062 for (arch = 0; arch < archs.count; arch++)
4063 if (make->all_targets[arch].count) strarray_add_uniq( &targets, "all" );
4065 for (i = 0; i < NB_INSTALL_RULES; i++)
4067 if (!make->install_rules[i].count) continue;
4068 strarray_add_uniq( &targets, "install" );
4069 strarray_add( &targets, install_targets[i] );
4071 if (make->clean_files.count) strarray_add( &targets, "clean" );
4072 if (make->test_files.count)
4074 strarray_add( &targets, "check" );
4075 strarray_add( &targets, "test" );
4076 strarray_add( &targets, "testclean" );
4079 output_filenames( targets );
4080 output_filenames( make->clean_files );
4081 output( ":\n" );
4082 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4083 output( ".PHONY:" );
4084 output_filenames( targets );
4085 output( "\n" );
4087 fclose( output_file );
4088 output_file = NULL;
4089 rename_temp_file( output_file_name );
4093 /*******************************************************************
4094 * output_silent_rules
4096 static void output_silent_rules(void)
4098 static const char *cmds[] =
4100 "BISON",
4101 "BUILD",
4102 "CC",
4103 "CCLD",
4104 "FLEX",
4105 "GEN",
4106 "LN",
4107 "MSG",
4108 "SED",
4109 "TEST",
4110 "WIDL",
4111 "WMC",
4112 "WRC"
4114 unsigned int i;
4116 output( "V = 0\n" );
4117 for (i = 0; i < ARRAY_SIZE(cmds); i++)
4119 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4120 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4121 output( "quiet_%s_1 =\n", cmds[i] );
4126 /*******************************************************************
4127 * output_top_makefile
4129 static void output_top_makefile( struct makefile *make )
4131 char buffer[1024];
4132 FILE *src_file;
4133 unsigned int i;
4134 int found = 0;
4136 output_file_name = obj_dir_path( make, output_makefile_name );
4137 output_file = create_temp_file( output_file_name );
4139 /* copy the contents of the source makefile */
4140 src_file = open_input_makefile( make );
4141 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4143 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4144 found = !strncmp( buffer, separator, strlen(separator) );
4146 if (fclose( src_file )) fatal_perror( "close" );
4147 input_file_name = NULL;
4149 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4151 if (silent_rules) output_silent_rules();
4152 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4153 output_sources( make );
4154 /* disable implicit rules */
4155 output( ".SUFFIXES:\n" );
4157 fclose( output_file );
4158 output_file = NULL;
4159 rename_temp_file( output_file_name );
4163 /*******************************************************************
4164 * output_dependencies
4166 static void output_dependencies( struct makefile *make )
4168 struct strarray ignore_files = empty_strarray;
4170 if (make->obj_dir) create_dir( make->obj_dir );
4172 if (make == top_makefile) output_top_makefile( make );
4173 else output_stub_makefile( make );
4175 strarray_addall( &ignore_files, make->distclean_files );
4176 strarray_addall( &ignore_files, make->clean_files );
4177 if (make->testdll) output_testlist( make );
4178 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4179 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4181 create_file_directories( make, ignore_files );
4183 output_file_name = NULL;
4187 /*******************************************************************
4188 * load_sources
4190 static void load_sources( struct makefile *make )
4192 static const char *source_vars[] =
4194 "SOURCES",
4195 "C_SRCS",
4196 "OBJC_SRCS",
4197 "RC_SRCS",
4198 "MC_SRCS",
4199 "IDL_SRCS",
4200 "BISON_SRCS",
4201 "LEX_SRCS",
4202 "HEADER_SRCS",
4203 "XTEMPLATE_SRCS",
4204 "SVG_SRCS",
4205 "FONT_SRCS",
4206 "IN_SRCS",
4207 "PO_SRCS",
4208 "MANPAGES",
4209 NULL
4211 const char **var;
4212 unsigned int i, arch;
4213 struct strarray value;
4214 struct incl_file *file;
4216 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4217 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4219 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4220 make->module = get_expanded_make_variable( make, "MODULE" );
4221 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4222 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4223 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4224 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4225 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4226 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4228 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4229 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4230 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4231 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4232 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4233 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4234 for (i = 0; i < NB_INSTALL_RULES; i++)
4235 make->install[i] = get_expanded_make_var_array( make, install_variables[i] );
4237 if (make->extlib) make->staticlib = make->extlib;
4238 if (make->staticlib) make->module = make->staticlib;
4240 if (make->obj_dir)
4242 make->disabled[0] = strarray_exists( &disabled_dirs[0], make->obj_dir );
4243 for (arch = 1; arch < archs.count; arch++)
4244 make->disabled[arch] = make->disabled[0] || strarray_exists( &disabled_dirs[arch], make->obj_dir );
4246 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4247 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4248 make->use_msvcrt = (make->module || make->testdll || make->is_win16) &&
4249 !strarray_exists( &make->extradllflags, "-mcygwin" );
4250 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4251 strarray_exists( &make->extradllflags, "-mwindows" );
4253 if (make->use_msvcrt) strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4255 if (make->module)
4257 /* add default install rules if nothing was specified */
4258 for (i = 0; i < NB_INSTALL_RULES; i++) if (make->install[i].count) break;
4259 if (i == NB_INSTALL_RULES)
4261 if (make->importlib) strarray_add( &make->install[INSTALL_DEV], make->importlib );
4262 if (make->staticlib) strarray_add( &make->install[INSTALL_DEV], make->staticlib );
4263 else strarray_add( &make->install[INSTALL_LIB], make->module );
4267 make->include_paths = empty_strarray;
4268 make->include_args = empty_strarray;
4269 make->define_args = empty_strarray;
4270 make->unix_cflags = empty_strarray;
4271 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4273 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4274 for (i = 0; i < value.count; i++)
4276 if (!strncmp( value.str[i], "-I", 2 ))
4278 const char *dir = value.str[i] + 2;
4279 if (!strncmp( dir, "./", 2 ))
4281 dir += 2;
4282 while (*dir == '/') dir++;
4284 strarray_add_uniq( &make->include_paths, dir );
4286 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4287 strarray_add_uniq( &make->define_args, value.str[i] );
4289 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4290 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4292 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4293 if (make->src_dir)
4294 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4295 if (make->parent_dir)
4296 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4297 strarray_add( &make->include_args, "-Iinclude" );
4298 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4300 list_init( &make->sources );
4301 list_init( &make->includes );
4303 for (var = source_vars; *var; var++)
4305 value = get_expanded_make_var_array( make, *var );
4306 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4309 add_generated_sources( make );
4311 if (make->use_msvcrt) strarray_add( &make->define_args, get_crt_define( make ));
4313 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4314 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4316 for (i = 0; i < make->delayimports.count; i++)
4317 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4321 /*******************************************************************
4322 * parse_makeflags
4324 static void parse_makeflags( const char *flags )
4326 const char *p = flags;
4327 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4329 while (*p)
4331 while (isspace(*p)) p++;
4332 var = buffer;
4333 while (*p && !isspace(*p))
4335 if (*p == '\\' && p[1]) p++;
4336 *var++ = *p++;
4338 *var = 0;
4339 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4344 /*******************************************************************
4345 * parse_option
4347 static int parse_option( const char *opt )
4349 if (opt[0] != '-')
4351 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4352 return 0;
4354 switch(opt[1])
4356 case 'f':
4357 if (opt[2]) output_makefile_name = opt + 2;
4358 break;
4359 case 'R':
4360 relative_dir_mode = 1;
4361 break;
4362 case 'S':
4363 silent_rules = 1;
4364 break;
4365 default:
4366 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4367 exit(1);
4369 return 1;
4373 /*******************************************************************
4374 * main
4376 int main( int argc, char *argv[] )
4378 const char *makeflags = getenv( "MAKEFLAGS" );
4379 const char *target;
4380 unsigned int i, j, arch;
4382 if (makeflags) parse_makeflags( makeflags );
4384 i = 1;
4385 while (i < argc)
4387 if (parse_option( argv[i] ))
4389 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4390 argc--;
4392 else i++;
4395 if (relative_dir_mode)
4397 char *relpath;
4399 if (argc != 3)
4401 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4402 exit( 1 );
4404 relpath = get_relative_path( argv[1], argv[2] );
4405 printf( "%s\n", relpath ? relpath : "." );
4406 exit( 0 );
4409 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4411 atexit( cleanup_files );
4412 signal( SIGTERM, exit_on_signal );
4413 signal( SIGINT, exit_on_signal );
4414 #ifdef SIGHUP
4415 signal( SIGHUP, exit_on_signal );
4416 #endif
4418 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4419 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4421 top_makefile = parse_makefile( NULL );
4423 target_flags[0] = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4424 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4425 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4426 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4427 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4428 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4429 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4430 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4431 for (i = 0; i < NB_INSTALL_RULES; i++)
4432 top_install[i] = get_expanded_make_var_array( top_makefile, strmake( "TOP_%s", install_variables[i] ));
4434 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4435 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4436 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4437 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4438 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4439 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4440 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4441 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4442 bison = get_expanded_make_variable( top_makefile, "BISON" );
4443 ar = get_expanded_make_variable( top_makefile, "AR" );
4444 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4445 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4446 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4447 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4448 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4449 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4451 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4452 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4453 if (!exe_ext) exe_ext = "";
4454 if (!tools_ext) tools_ext = "";
4456 strarray_add( &archs, get_expanded_make_variable( top_makefile, "HOST_ARCH" ));
4457 strarray_addall( &archs, get_expanded_make_var_array( top_makefile, "PE_ARCHS" ));
4459 arch_dirs[0] = "";
4460 arch_pe_dirs[0] = strmake( "%s-windows/", archs.str[0] );
4461 arch_install_dirs[0] = *dll_ext ? strmake( "$(dlldir)/%s-unix/", archs.str[0] ) : "$(dlldir)/";
4462 strip_progs[0] = "\"$(STRIP)\"";
4464 for (arch = 1; arch < archs.count; arch++)
4466 target = get_expanded_arch_var( top_makefile, "TARGET", arch );
4467 strarray_add( &target_flags[arch], "-b" );
4468 strarray_add( &target_flags[arch], target );
4469 arch_dirs[arch] = strmake( "%s-windows/", archs.str[arch] );
4470 arch_pe_dirs[arch] = arch_dirs[arch];
4471 arch_install_dirs[arch] = strmake( "$(dlldir)/%s", arch_dirs[arch] );
4472 strip_progs[arch] = strmake( "%s-strip", target );
4475 for (arch = 0; arch < archs.count; arch++)
4477 extra_cflags[arch] = get_expanded_arch_var_array( top_makefile, "EXTRACFLAGS", arch );
4478 extra_cflags_extlib[arch] = remove_warning_flags( extra_cflags[arch] );
4479 disabled_dirs[arch] = get_expanded_arch_var_array( top_makefile, "DISABLED_SUBDIRS", arch );
4480 if (!is_multiarch( arch )) continue;
4481 delay_load_flags[arch] = get_expanded_arch_var( top_makefile, "DELAYLOADFLAG", arch );
4482 debug_flags[arch] = get_expanded_arch_var( top_makefile, "DEBUG", arch );
4485 if (*dll_ext)
4487 delay_load_flags[0] = "-Wl,-delayload,";
4488 debug_flags[0] = NULL;
4491 top_makefile->src_dir = root_src_dir;
4492 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4493 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4495 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4497 load_sources( top_makefile );
4498 load_sources( include_makefile );
4499 for (i = 0; i < subdirs.count; i++)
4500 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4502 output_dependencies( top_makefile );
4503 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4505 return 0;