d3d9/tests: Test the presentation parameters after creating a device.
[wine.git] / tools / makedep.c
blob8ce575b15cad4fbd086c5db1f9ac1f152294c6aa
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 <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
34 #include "tools.h"
35 #include "wine/list.h"
37 enum incl_type
39 INCL_NORMAL, /* #include "foo.h" */
40 INCL_SYSTEM, /* #include <foo.h> */
41 INCL_IMPORT, /* idl import "foo.idl" */
42 INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
43 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
44 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
47 struct dependency
49 int line; /* source line where this header is included */
50 enum incl_type type; /* type of include */
51 char *name; /* header name */
54 struct file
56 struct list entry;
57 char *name; /* full file name relative to cwd */
58 void *args; /* custom arguments for makefile rule */
59 unsigned int flags; /* flags (see below) */
60 unsigned int deps_count; /* files in use */
61 unsigned int deps_size; /* total allocated size */
62 struct dependency *deps; /* all header dependencies */
65 struct incl_file
67 struct list entry;
68 struct list hash_entry;
69 struct file *file;
70 char *name;
71 char *filename;
72 char *basename; /* base target name for generated files */
73 char *sourcename; /* source file name for generated headers */
74 struct incl_file *included_by; /* file that included this one */
75 int included_line; /* line where this file was included */
76 enum incl_type type; /* type of include */
77 unsigned int arch; /* architecture for multi-arch files, otherwise 0 */
78 unsigned int use_msvcrt:1; /* put msvcrt headers in the search path? */
79 unsigned int is_external:1; /* file from external library? */
80 struct incl_file *owner;
81 unsigned int files_count; /* files in use */
82 unsigned int files_size; /* total allocated size */
83 struct incl_file **files;
84 struct strarray dependencies; /* file dependencies */
85 struct strarray importlibdeps; /* importlib dependencies */
88 #define FLAG_GENERATED 0x000001 /* generated file */
89 #define FLAG_INSTALL 0x000002 /* file to install */
90 #define FLAG_TESTDLL 0x000004 /* file is part of a TESTDLL resource */
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 6
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 *fontforge;
150 static const char *convert;
151 static const char *flex;
152 static const char *bison;
153 static const char *ar;
154 static const char *ranlib;
155 static const char *rsvg;
156 static const char *icotool;
157 static const char *msgfmt;
158 static const char *ln_s;
159 static const char *sed_cmd;
160 static const char *wayland_scanner;
161 static int so_dll_supported;
162 static int unix_lib_supported;
163 /* per-architecture global variables */
164 static const char *dll_ext[MAX_ARCHS];
165 static const char *arch_dirs[MAX_ARCHS];
166 static const char *arch_pe_dirs[MAX_ARCHS];
167 static const char *arch_install_dirs[MAX_ARCHS];
168 static const char *strip_progs[MAX_ARCHS];
169 static const char *debug_flags[MAX_ARCHS];
170 static const char *delay_load_flags[MAX_ARCHS];
171 static struct strarray target_flags[MAX_ARCHS];
172 static struct strarray extra_cflags[MAX_ARCHS];
173 static struct strarray extra_cflags_extlib[MAX_ARCHS];
174 static struct strarray disabled_dirs[MAX_ARCHS];
176 struct makefile
178 /* values determined from input makefile */
179 struct strarray vars;
180 struct strarray include_paths;
181 struct strarray include_args;
182 struct strarray define_args;
183 struct strarray unix_cflags;
184 struct strarray programs;
185 struct strarray scripts;
186 struct strarray imports;
187 struct strarray delayimports;
188 struct strarray extradllflags;
189 struct strarray install[NB_INSTALL_RULES];
190 struct strarray extra_targets;
191 struct strarray extra_imports;
192 struct list sources;
193 struct list includes;
194 const char *src_dir;
195 const char *obj_dir;
196 const char *parent_dir;
197 const char *module;
198 const char *testdll;
199 const char *extlib;
200 const char *staticlib;
201 const char *importlib;
202 const char *unixlib;
203 int data_only;
204 int is_win16;
205 int is_exe;
206 int disabled[MAX_ARCHS];
208 /* values generated at output time */
209 struct strarray in_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 ok_files[MAX_ARCHS];
225 struct strarray res_files[MAX_ARCHS];
226 struct strarray all_targets[MAX_ARCHS];
227 struct strarray install_rules[NB_INSTALL_RULES];
230 static struct makefile *top_makefile;
231 static struct makefile *include_makefile;
232 static struct makefile **submakes;
234 static const char separator[] = "### Dependencies";
235 static const char *output_makefile_name = "Makefile";
236 static const char *input_file_name;
237 static const char *output_file_name;
238 static const char *temp_file_name;
239 static int relative_dir_mode;
240 static int silent_rules;
241 static int input_line;
242 static int output_column;
243 static FILE *output_file;
245 static const char Usage[] =
246 "Usage: makedep [options] [directories]\n"
247 "Options:\n"
248 " -R from to Compute the relative path between two directories\n"
249 " -S Generate Automake-style silent rules\n"
250 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
253 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
254 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
255 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
256 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
258 /*******************************************************************
259 * fatal_error
261 static void fatal_error( const char *msg, ... )
263 va_list valist;
264 va_start( valist, msg );
265 if (input_file_name)
267 fprintf( stderr, "%s:", input_file_name );
268 if (input_line) fprintf( stderr, "%d:", input_line );
269 fprintf( stderr, " error: " );
271 else fprintf( stderr, "makedep: error: " );
272 vfprintf( stderr, msg, valist );
273 va_end( valist );
274 exit(1);
278 /*******************************************************************
279 * fatal_perror
281 static void fatal_perror( const char *msg, ... )
283 va_list valist;
284 va_start( valist, msg );
285 if (input_file_name)
287 fprintf( stderr, "%s:", input_file_name );
288 if (input_line) fprintf( stderr, "%d:", input_line );
289 fprintf( stderr, " error: " );
291 else fprintf( stderr, "makedep: error: " );
292 vfprintf( stderr, msg, valist );
293 perror( " " );
294 va_end( valist );
295 exit(1);
299 /*******************************************************************
300 * cleanup_files
302 static void cleanup_files(void)
304 if (temp_file_name) unlink( temp_file_name );
305 if (output_file_name) unlink( output_file_name );
309 /*******************************************************************
310 * exit_on_signal
312 static void exit_on_signal( int sig )
314 exit( 1 ); /* this will call the atexit functions */
318 /*******************************************************************
319 * output
321 static void output( const char *format, ... )
323 int ret;
324 va_list valist;
326 va_start( valist, format );
327 ret = vfprintf( output_file, format, valist );
328 va_end( valist );
329 if (ret < 0) fatal_perror( "output" );
330 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
331 else output_column += ret;
335 /*******************************************************************
336 * strarray_get_value
338 * Find a value in a name/value pair string array.
340 static const char *strarray_get_value( const struct strarray *array, const char *name )
342 int pos, res, min = 0, max = array->count / 2 - 1;
344 while (min <= max)
346 pos = (min + max) / 2;
347 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
348 if (res < 0) min = pos + 1;
349 else max = pos - 1;
351 return NULL;
355 /*******************************************************************
356 * strarray_set_value
358 * Define a value in a name/value pair string array.
360 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
362 int i, pos, res, min = 0, max = array->count / 2 - 1;
364 while (min <= max)
366 pos = (min + max) / 2;
367 if (!(res = strcmp( array->str[pos * 2], name )))
369 /* redefining a variable replaces the previous value */
370 array->str[pos * 2 + 1] = value;
371 return;
373 if (res < 0) min = pos + 1;
374 else max = pos - 1;
376 strarray_add( array, NULL );
377 strarray_add( array, NULL );
378 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
379 array->str[min * 2] = name;
380 array->str[min * 2 + 1] = value;
384 /*******************************************************************
385 * output_filename
387 static void output_filename( const char *name )
389 if (output_column + strlen(name) + 1 > 100)
391 output( " \\\n" );
392 output( " " );
394 else if (output_column) output( " " );
395 output( "%s", name );
399 /*******************************************************************
400 * output_filenames
402 static void output_filenames( struct strarray array )
404 unsigned int i;
406 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
410 /*******************************************************************
411 * output_rm_filenames
413 static void output_rm_filenames( struct strarray array, const char *command )
415 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
416 unsigned int i, len;
418 if (!array.count) return;
419 output( "\t%s", command );
420 for (i = len = 0; i < array.count; i++)
422 if (len > max_cmdline)
424 output( "\n" );
425 output( "\t%s", command );
426 len = 0;
428 output_filename( array.str[i] );
429 len += strlen( array.str[i] ) + 1;
431 output( "\n" );
435 /*******************************************************************
436 * get_extension
438 static char *get_extension( char *filename )
440 char *ext = strrchr( filename, '.' );
441 if (ext && strchr( ext, '/' )) ext = NULL;
442 return ext;
446 /*******************************************************************
447 * get_base_name
449 static const char *get_base_name( const char *name )
451 char *base;
452 if (!strchr( name, '.' )) return name;
453 base = xstrdup( name );
454 *strrchr( base, '.' ) = 0;
455 return base;
459 /*******************************************************************
460 * replace_filename
462 static char *replace_filename( const char *path, const char *name )
464 const char *p;
465 char *ret;
466 size_t len;
468 if (!path) return xstrdup( name );
469 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
470 len = p - path + 1;
471 ret = xmalloc( len + strlen( name ) + 1 );
472 memcpy( ret, path, len );
473 strcpy( ret + len, name );
474 return ret;
478 /*******************************************************************
479 * replace_substr
481 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
483 size_t pos = start - str;
484 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
485 memcpy( ret, str, pos );
486 strcpy( ret + pos, replace );
487 strcat( ret + pos, start + len );
488 return ret;
492 /*******************************************************************
493 * get_relative_path
495 * Determine where the destination path is located relative to the 'from' path.
497 static char *get_relative_path( const char *from, const char *dest )
499 const char *start;
500 char *ret, *p;
501 unsigned int dotdots = 0;
503 /* a path of "." is equivalent to an empty path */
504 if (!strcmp( from, "." )) from = "";
506 for (;;)
508 while (*from == '/') from++;
509 while (*dest == '/') dest++;
510 start = dest; /* save start of next path element */
511 if (!*from) break;
513 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
514 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
516 /* count remaining elements in 'from' */
519 dotdots++;
520 while (*from && *from != '/') from++;
521 while (*from == '/') from++;
523 while (*from);
524 break;
527 if (!start[0] && !dotdots) return NULL; /* empty path */
529 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
530 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
532 if (start[0]) strcpy( p, start );
533 else p[-1] = 0; /* remove trailing slash */
534 return ret;
538 /*******************************************************************
539 * concat_paths
541 static char *concat_paths( const char *base, const char *path )
543 int i, len;
544 char *ret;
546 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
547 if (!path || !path[0]) return xstrdup( base );
548 if (path[0] == '/') return xstrdup( path );
550 len = strlen( base );
551 while (len && base[len - 1] == '/') len--;
552 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
554 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
555 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
556 if (i != len - 1 || base[i] != '.')
558 path += 2;
559 while (*path == '/') path++;
561 /* else ignore "." element */
562 while (i > 0 && base[i - 1] == '/') i--;
563 len = i;
565 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
566 ret = xmalloc( len + strlen( path ) + 2 );
567 memcpy( ret, base, len );
568 ret[len++] = '/';
569 strcpy( ret + len, path );
570 return ret;
574 /*******************************************************************
575 * is_native_arch_disabled
577 * Check if the makefile was disabled for a PE arch that matches the native arch.
579 static int is_native_arch_disabled( struct makefile *make )
581 unsigned int arch;
583 if (archs.count == 1) return 0;
584 if (!so_dll_supported) return 1;
586 for (arch = 1; arch < archs.count; arch++)
587 if (make->disabled[arch] && !strcmp( archs.str[0], archs.str[arch] ))
588 return 1;
589 return 0;
593 /*******************************************************************
594 * is_multiarch
596 * Check if arch is one of the PE architectures in multiarch.
597 * Also return TRUE for native arch iff there's no PE support.
599 static int is_multiarch( unsigned int arch )
601 return archs.count == 1 || arch;
605 /*******************************************************************
606 * is_using_msvcrt
608 * Check if the files of a makefile use msvcrt by default.
610 static int is_using_msvcrt( struct makefile *make )
612 return make->module || make->testdll;
616 /*******************************************************************
617 * arch_module_name
619 static char *arch_module_name( const char *module, unsigned int arch )
621 return strmake( "%s%s%s", arch_dirs[arch], module, dll_ext[arch] );
625 /*******************************************************************
626 * arch_make_variable
628 static char *arch_make_variable( const char *name, unsigned int arch )
630 return arch ? strmake( "$(%s_%s)", archs.str[arch], name ) : strmake( "$(%s)", name );
634 /*******************************************************************
635 * obj_dir_path
637 static char *obj_dir_path( const struct makefile *make, const char *path )
639 return concat_paths( make->obj_dir, path );
643 /*******************************************************************
644 * src_dir_path
646 static char *src_dir_path( const struct makefile *make, const char *path )
648 if (make->src_dir) return concat_paths( make->src_dir, path );
649 return obj_dir_path( make, path );
653 /*******************************************************************
654 * root_src_dir_path
656 static char *root_src_dir_path( const char *path )
658 return concat_paths( root_src_dir, path );
662 /*******************************************************************
663 * tools_dir_path
665 static char *tools_dir_path( const struct makefile *make, const char *path )
667 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
668 return strmake( "tools/%s", path );
672 /*******************************************************************
673 * tools_path
675 static char *tools_path( const struct makefile *make, const char *name )
677 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
681 /*******************************************************************
682 * strarray_addall_path
684 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
686 unsigned int i;
688 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
692 /*******************************************************************
693 * get_line
695 static char *get_line( FILE *file )
697 static char *buffer;
698 static size_t size;
700 if (!size)
702 size = 1024;
703 buffer = xmalloc( size );
705 if (!fgets( buffer, size, file )) return NULL;
706 input_line++;
708 for (;;)
710 char *p = buffer + strlen(buffer);
711 /* if line is larger than buffer, resize buffer */
712 while (p == buffer + size - 1 && p[-1] != '\n')
714 buffer = xrealloc( buffer, size * 2 );
715 if (!fgets( buffer + size - 1, size + 1, file )) break;
716 p = buffer + strlen(buffer);
717 size *= 2;
719 if (p > buffer && p[-1] == '\n')
721 *(--p) = 0;
722 if (p > buffer && p[-1] == '\r') *(--p) = 0;
723 if (p > buffer && p[-1] == '\\')
725 *(--p) = 0;
726 /* line ends in backslash, read continuation line */
727 if (!fgets( p, size - (p - buffer), file )) return buffer;
728 input_line++;
729 continue;
732 return buffer;
737 /*******************************************************************
738 * hash_filename
740 static unsigned int hash_filename( const char *name )
742 /* FNV-1 hash */
743 unsigned int ret = 2166136261u;
744 while (*name) ret = (ret * 16777619) ^ *name++;
745 return ret % HASH_SIZE;
749 /*******************************************************************
750 * add_file
752 static struct file *add_file( const char *name )
754 struct file *file = xmalloc( sizeof(*file) );
755 memset( file, 0, sizeof(*file) );
756 file->name = xstrdup( name );
757 return file;
761 /*******************************************************************
762 * add_dependency
764 static void add_dependency( struct file *file, const char *name, enum incl_type type )
766 if (file->deps_count >= file->deps_size)
768 file->deps_size *= 2;
769 if (file->deps_size < 16) file->deps_size = 16;
770 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
772 file->deps[file->deps_count].line = input_line;
773 file->deps[file->deps_count].type = type;
774 file->deps[file->deps_count].name = xstrdup( name );
775 file->deps_count++;
779 /*******************************************************************
780 * find_src_file
782 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
784 struct incl_file *file;
786 if (make == include_makefile)
788 unsigned int hash = hash_filename( name );
790 LIST_FOR_EACH_ENTRY( file, &global_includes[hash], struct incl_file, hash_entry )
791 if (!strcmp( name, file->name )) return file;
792 return NULL;
795 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
796 if (!strcmp( name, file->name )) return file;
797 return NULL;
800 /*******************************************************************
801 * find_include_file
803 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
805 struct incl_file *file;
807 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
809 const char *filename = file->filename;
810 if (!filename) continue;
811 if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
813 filename += strlen(make->obj_dir);
814 while (*filename == '/') filename++;
816 if (!strcmp( name, filename )) return file;
818 return NULL;
821 /*******************************************************************
822 * add_include
824 * Add an include file if it doesn't already exists.
826 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
827 const char *name, int line, enum incl_type type )
829 struct incl_file *include;
831 if (parent->files_count >= parent->files_size)
833 parent->files_size *= 2;
834 if (parent->files_size < 16) parent->files_size = 16;
835 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
838 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
839 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
840 goto found;
842 include = xmalloc( sizeof(*include) );
843 memset( include, 0, sizeof(*include) );
844 include->name = xstrdup(name);
845 include->included_by = parent;
846 include->included_line = line;
847 include->type = type;
848 include->use_msvcrt = parent->use_msvcrt;
849 list_add_tail( &make->includes, &include->entry );
850 found:
851 parent->files[parent->files_count++] = include;
852 return include;
856 /*******************************************************************
857 * add_generated_source
859 * Add a generated source file to the list.
861 static struct incl_file *add_generated_source( struct makefile *make, const char *name,
862 const char *filename, unsigned int arch )
864 struct incl_file *file = xmalloc( sizeof(*file) );
866 name = strmake( "%s%s", arch_dirs[arch], name );
867 memset( file, 0, sizeof(*file) );
868 file->file = add_file( name );
869 file->arch = arch;
870 file->name = xstrdup( name );
871 file->basename = xstrdup( filename ? filename : name );
872 file->filename = obj_dir_path( make, file->basename );
873 file->file->flags = FLAG_GENERATED;
874 file->use_msvcrt = is_using_msvcrt( make );
875 list_add_tail( &make->sources, &file->entry );
876 if (make == include_makefile)
878 unsigned int hash = hash_filename( name );
879 list_add_tail( &global_includes[hash], &file->hash_entry );
881 return file;
885 /*******************************************************************
886 * skip_spaces
888 static char *skip_spaces( const char *p )
890 while (*p == ' ' || *p == '\t') p++;
891 return (char *)p;
895 /*******************************************************************
896 * parse_include_directive
898 static void parse_include_directive( struct file *source, char *str )
900 char quote, *include, *p = skip_spaces( str );
902 if (*p != '\"' && *p != '<' ) return;
903 quote = *p++;
904 if (quote == '<') quote = '>';
905 include = p;
906 while (*p && (*p != quote)) p++;
907 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
908 *p = 0;
909 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
913 /*******************************************************************
914 * parse_pragma_directive
916 static void parse_pragma_directive( struct file *source, char *str )
918 char *flag, *p = str;
920 if (*p != ' ' && *p != '\t') return;
921 p = strtok( skip_spaces( p ), " \t" );
922 if (strcmp( p, "makedep" )) return;
924 while ((flag = strtok( NULL, " \t" )))
926 if (!strcmp( flag, "depend" ))
928 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
929 return;
931 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
932 else if (!strcmp( flag, "testdll" )) source->flags |= FLAG_TESTDLL;
934 if (strendswith( source->name, ".idl" ))
936 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
937 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
938 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
939 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
940 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
941 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
942 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
943 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
945 else if (strendswith( source->name, ".rc" ))
947 if (!strcmp( flag, "header" )) source->flags |= FLAG_RC_HEADER;
948 else if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
950 else if (strendswith( source->name, ".sfd" ))
952 if (!strcmp( flag, "font" ))
954 struct strarray *array = source->args;
956 if (!array)
958 source->args = array = xmalloc( sizeof(*array) );
959 *array = empty_strarray;
960 source->flags |= FLAG_SFD_FONTS;
962 strarray_add( array, xstrdup( strtok( NULL, "" )));
963 return;
966 else
968 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
969 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
975 /*******************************************************************
976 * parse_cpp_directive
978 static void parse_cpp_directive( struct file *source, char *str )
980 str = skip_spaces( str );
981 if (*str++ != '#') return;
982 str = skip_spaces( str );
984 if (!strncmp( str, "include", 7 ))
985 parse_include_directive( source, str + 7 );
986 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
987 parse_include_directive( source, str + 6 );
988 else if (!strncmp( str, "pragma", 6 ))
989 parse_pragma_directive( source, str + 6 );
993 /*******************************************************************
994 * parse_idl_file
996 static void parse_idl_file( struct file *source, FILE *file )
998 char *buffer, *include;
1000 input_line = 0;
1002 while ((buffer = get_line( file )))
1004 char quote;
1005 char *p = skip_spaces( buffer );
1007 if (!strncmp( p, "importlib", 9 ))
1009 p = skip_spaces( p + 9 );
1010 if (*p++ != '(') continue;
1011 p = skip_spaces( p );
1012 if (*p++ != '"') continue;
1013 include = p;
1014 while (*p && (*p != '"')) p++;
1015 if (!*p) fatal_error( "malformed importlib directive\n" );
1016 *p = 0;
1017 add_dependency( source, include, INCL_IMPORTLIB );
1018 continue;
1021 if (!strncmp( p, "import", 6 ))
1023 p = skip_spaces( p + 6 );
1024 if (*p != '"') continue;
1025 include = ++p;
1026 while (*p && (*p != '"')) p++;
1027 if (!*p) fatal_error( "malformed import directive\n" );
1028 *p = 0;
1029 add_dependency( source, include, INCL_IMPORT );
1030 continue;
1033 /* check for #include inside cpp_quote */
1034 if (!strncmp( p, "cpp_quote", 9 ))
1036 p = skip_spaces( p + 9 );
1037 if (*p++ != '(') continue;
1038 p = skip_spaces( p );
1039 if (*p++ != '"') continue;
1040 if (*p++ != '#') continue;
1041 p = skip_spaces( p );
1042 if (strncmp( p, "include", 7 )) continue;
1043 p = skip_spaces( p + 7 );
1044 if (*p == '\\' && p[1] == '"')
1046 p += 2;
1047 quote = '"';
1049 else
1051 if (*p++ != '<' ) continue;
1052 quote = '>';
1054 include = p;
1055 while (*p && (*p != quote)) p++;
1056 if (!*p || (quote == '"' && p[-1] != '\\'))
1057 fatal_error( "malformed #include directive inside cpp_quote\n" );
1058 if (quote == '"') p--; /* remove backslash */
1059 *p = 0;
1060 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1061 continue;
1064 parse_cpp_directive( source, p );
1068 /*******************************************************************
1069 * parse_c_file
1071 static void parse_c_file( struct file *source, FILE *file )
1073 char *buffer;
1075 input_line = 0;
1076 while ((buffer = get_line( file )))
1078 parse_cpp_directive( source, buffer );
1083 /*******************************************************************
1084 * parse_rc_file
1086 static void parse_rc_file( struct file *source, FILE *file )
1088 char *buffer, *include;
1090 input_line = 0;
1091 while ((buffer = get_line( file )))
1093 char quote;
1094 char *p = skip_spaces( buffer );
1096 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1098 p = skip_spaces( p + 2 );
1099 if (strncmp( p, "@makedep:", 9 )) continue;
1100 p = skip_spaces( p + 9 );
1101 quote = '"';
1102 if (*p == quote)
1104 include = ++p;
1105 while (*p && *p != quote) p++;
1107 else
1109 include = p;
1110 while (*p && *p != ' ' && *p != '\t' && *p != '*') p++;
1112 if (!*p)
1113 fatal_error( "malformed makedep comment\n" );
1114 *p = 0;
1115 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1116 continue;
1119 parse_cpp_directive( source, buffer );
1124 /*******************************************************************
1125 * parse_in_file
1127 static void parse_in_file( struct file *source, FILE *file )
1129 char *p, *buffer;
1131 /* make sure it gets rebuilt when the version changes */
1132 add_dependency( source, "config.h", INCL_SYSTEM );
1134 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1136 input_line = 0;
1137 while ((buffer = get_line( file )))
1139 if (strncmp( buffer, ".TH", 3 )) continue;
1140 p = skip_spaces( buffer + 3 );
1141 if (!(p = strtok( p, " \t" ))) continue; /* program name */
1142 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1143 source->args = xstrdup( p );
1144 return;
1149 /*******************************************************************
1150 * parse_sfd_file
1152 static void parse_sfd_file( struct file *source, FILE *file )
1154 char *p, *eol, *buffer;
1156 input_line = 0;
1157 while ((buffer = get_line( file )))
1159 if (strncmp( buffer, "UComments:", 10 )) continue;
1160 p = buffer + 10;
1161 while (*p == ' ') p++;
1162 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1164 p++;
1165 buffer[strlen(buffer) - 1] = 0;
1167 while ((eol = strstr( p, "+AAoA" )))
1169 *eol = 0;
1170 p = skip_spaces( p );
1171 if (*p++ == '#')
1173 p = skip_spaces( p );
1174 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1176 p = eol + 5;
1178 p = skip_spaces( p );
1179 if (*p++ != '#') return;
1180 p = skip_spaces( p );
1181 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1182 return;
1187 static const struct
1189 const char *ext;
1190 void (*parse)( struct file *file, FILE *f );
1191 } parse_functions[] =
1193 { ".c", parse_c_file },
1194 { ".h", parse_c_file },
1195 { ".inl", parse_c_file },
1196 { ".l", parse_c_file },
1197 { ".m", parse_c_file },
1198 { ".rh", parse_c_file },
1199 { ".x", parse_c_file },
1200 { ".y", parse_c_file },
1201 { ".idl", parse_idl_file },
1202 { ".rc", parse_rc_file },
1203 { ".in", parse_in_file },
1204 { ".sfd", parse_sfd_file }
1207 /*******************************************************************
1208 * load_file
1210 static struct file *load_file( const char *name )
1212 struct file *file;
1213 FILE *f;
1214 unsigned int i, hash = hash_filename( name );
1216 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1217 if (!strcmp( name, file->name )) return file;
1219 if (!(f = fopen( name, "r" ))) return NULL;
1221 file = add_file( name );
1222 list_add_tail( &files[hash], &file->entry );
1223 input_file_name = file->name;
1224 input_line = 0;
1226 for (i = 0; i < ARRAY_SIZE(parse_functions); i++)
1228 if (!strendswith( name, parse_functions[i].ext )) continue;
1229 parse_functions[i].parse( file, f );
1230 break;
1233 fclose( f );
1234 input_file_name = NULL;
1236 return file;
1240 /*******************************************************************
1241 * open_include_path_file
1243 * Open a file from a directory on the include path.
1245 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1246 const char *name, char **filename )
1248 char *src_path = concat_paths( dir, name );
1249 struct file *ret = load_file( src_path );
1251 if (ret) *filename = src_path;
1252 return ret;
1256 /*******************************************************************
1257 * open_file_same_dir
1259 * Open a file in the same directory as the parent.
1261 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1263 char *src_path = replace_filename( parent->file->name, name );
1264 struct file *ret = load_file( src_path );
1266 if (ret) *filename = replace_filename( parent->filename, name );
1267 return ret;
1271 /*******************************************************************
1272 * open_same_dir_generated_file
1274 * Open a generated_file in the same directory as the parent.
1276 static struct file *open_same_dir_generated_file( const struct makefile *make,
1277 const struct incl_file *parent, struct incl_file *file,
1278 const char *ext, const char *src_ext )
1280 char *filename;
1281 struct file *ret = NULL;
1283 if (strendswith( file->name, ext ) &&
1284 (ret = open_file_same_dir( parent, replace_extension( file->name, ext, src_ext ), &filename )))
1286 file->sourcename = filename;
1287 file->filename = obj_dir_path( make, replace_filename( parent->name, file->name ));
1289 return ret;
1293 /*******************************************************************
1294 * open_local_file
1296 * Open a file in the source directory of the makefile.
1298 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1300 char *src_path = src_dir_path( make, path );
1301 struct file *ret = load_file( src_path );
1303 /* if not found, try parent dir */
1304 if (!ret && make->parent_dir)
1306 free( src_path );
1307 path = strmake( "%s/%s", make->parent_dir, path );
1308 src_path = src_dir_path( make, path );
1309 ret = load_file( src_path );
1312 if (ret) *filename = src_path;
1313 return ret;
1317 /*******************************************************************
1318 * open_local_generated_file
1320 * Open a generated file in the directory of the makefile.
1322 static struct file *open_local_generated_file( const struct makefile *make, struct incl_file *file,
1323 const char *ext, const char *src_ext )
1325 struct incl_file *include;
1327 if (strendswith( file->name, ext ) &&
1328 (include = find_src_file( make, replace_extension( file->name, ext, src_ext ) )))
1330 file->sourcename = include->filename;
1331 file->filename = obj_dir_path( make, file->name );
1332 return include->file;
1334 return NULL;
1338 /*******************************************************************
1339 * open_global_file
1341 * Open a file in the top-level source directory.
1343 static struct file *open_global_file( const char *path, char **filename )
1345 char *src_path = root_src_dir_path( path );
1346 struct file *ret = load_file( src_path );
1348 if (ret) *filename = src_path;
1349 return ret;
1353 /*******************************************************************
1354 * open_global_header
1356 * Open a file in the global include source directory.
1358 static struct file *open_global_header( const char *path, char **filename )
1360 struct incl_file *include = find_src_file( include_makefile, path );
1362 if (!include) return NULL;
1363 *filename = include->filename;
1364 return include->file;
1368 /*******************************************************************
1369 * open_global_generated_file
1371 * Open a generated file in the top-level source directory.
1373 static struct file *open_global_generated_file( const struct makefile *make, struct incl_file *file,
1374 const char *ext, const char *src_ext )
1376 struct incl_file *include;
1378 if (strendswith( file->name, ext ) &&
1379 (include = find_src_file( include_makefile, replace_extension( file->name, ext, src_ext ) )))
1381 file->sourcename = include->filename;
1382 file->filename = strmake( "include/%s", file->name );
1383 return include->file;
1385 return NULL;
1389 /*******************************************************************
1390 * open_src_file
1392 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1394 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1396 if (!file) fatal_perror( "open %s", pFile->name );
1397 return file;
1401 /*******************************************************************
1402 * find_importlib_module
1404 static struct makefile *find_importlib_module( const char *name )
1406 unsigned int i, len;
1408 for (i = 0; i < subdirs.count; i++)
1410 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1411 len = strlen(submakes[i]->obj_dir);
1412 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1413 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1415 return NULL;
1419 /*******************************************************************
1420 * open_include_file
1422 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1424 struct file *file = NULL;
1425 unsigned int i, len;
1427 errno = ENOENT;
1429 /* check for generated files */
1430 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1431 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1432 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1433 if (convert && rsvg && icotool)
1435 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1436 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1437 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1439 if ((file = open_local_generated_file( make, pFile, "-client-protocol.h", ".xml" ))) return file;
1441 /* check for extra targets */
1442 if (strarray_exists( &make->extra_targets, pFile->name ))
1444 pFile->sourcename = src_dir_path( make, pFile->name );
1445 pFile->filename = obj_dir_path( make, pFile->name );
1446 return NULL;
1449 /* now try in source dir */
1450 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1452 /* check for global importlib (module dependency) */
1453 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1455 pFile->filename = pFile->name;
1456 return NULL;
1459 /* check for generated files in global includes */
1460 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1461 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1462 if (strendswith( pFile->name, "tmpl.h" ) &&
1463 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1465 /* check in global includes source dir */
1466 if ((file = open_global_header( pFile->name, &pFile->filename ))) return file;
1468 /* check in global msvcrt includes */
1469 if (pFile->use_msvcrt &&
1470 (file = open_global_header( strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1471 return file;
1473 /* now search in include paths */
1474 for (i = 0; i < make->include_paths.count; i++)
1476 const char *dir = make->include_paths.str[i];
1478 if (root_src_dir)
1480 len = strlen( root_src_dir );
1481 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1483 while (dir[len] == '/') len++;
1484 file = open_global_file( concat_paths( dir + len, pFile->name ), &pFile->filename );
1487 else
1489 if (*dir == '/') continue;
1490 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1492 if (!file) continue;
1493 pFile->is_external = 1;
1494 return file;
1497 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1499 /* try in src file directory */
1500 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1501 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1502 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1504 pFile->is_external = pFile->included_by->is_external;
1505 return file;
1508 if (make->extlib) return NULL; /* ignore missing files in external libs */
1510 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1511 perror( pFile->name );
1512 pFile = pFile->included_by;
1513 while (pFile && pFile->included_by)
1515 const char *parent = pFile->included_by->sourcename;
1516 if (!parent) parent = pFile->included_by->file->name;
1517 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1518 parent, pFile->included_line, pFile->name );
1519 pFile = pFile->included_by;
1521 exit(1);
1525 /*******************************************************************
1526 * add_all_includes
1528 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1530 unsigned int i;
1532 for (i = 0; i < file->deps_count; i++)
1534 switch (file->deps[i].type)
1536 case INCL_NORMAL:
1537 case INCL_IMPORT:
1538 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1539 break;
1540 case INCL_IMPORTLIB:
1541 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1542 break;
1543 case INCL_SYSTEM:
1544 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1545 break;
1546 case INCL_CPP_QUOTE:
1547 case INCL_CPP_QUOTE_SYSTEM:
1548 break;
1554 /*******************************************************************
1555 * parse_file
1557 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1559 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1561 if (!file) return;
1563 source->file = file;
1564 source->files_count = 0;
1565 source->files_size = file->deps_count;
1566 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1568 if (strendswith( file->name, ".m" )) file->flags |= FLAG_C_UNIX;
1569 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1570 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1572 if (source->sourcename)
1574 if (strendswith( source->sourcename, ".idl" ))
1576 unsigned int i;
1578 /* generated .h file always includes these */
1579 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1580 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1581 for (i = 0; i < file->deps_count; i++)
1583 switch (file->deps[i].type)
1585 case INCL_IMPORT:
1586 if (strendswith( file->deps[i].name, ".idl" ))
1587 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1588 file->deps[i].line, INCL_NORMAL );
1589 else
1590 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1591 break;
1592 case INCL_CPP_QUOTE:
1593 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1594 break;
1595 case INCL_CPP_QUOTE_SYSTEM:
1596 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1597 break;
1598 case INCL_NORMAL:
1599 case INCL_SYSTEM:
1600 case INCL_IMPORTLIB:
1601 break;
1604 return;
1606 if (strendswith( source->sourcename, ".y" ))
1607 return; /* generated .tab.h doesn't include anything */
1610 add_all_includes( make, source, file );
1614 /*******************************************************************
1615 * add_src_file
1617 * Add a source file to the list.
1619 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1621 struct incl_file *file = xmalloc( sizeof(*file) );
1623 memset( file, 0, sizeof(*file) );
1624 file->name = xstrdup(name);
1625 file->use_msvcrt = is_using_msvcrt( make );
1626 file->is_external = !!make->extlib;
1627 list_add_tail( &make->sources, &file->entry );
1628 if (make == include_makefile)
1630 unsigned int hash = hash_filename( name );
1631 list_add_tail( &global_includes[hash], &file->hash_entry );
1633 parse_file( make, file, 1 );
1634 return file;
1638 /*******************************************************************
1639 * open_input_makefile
1641 static FILE *open_input_makefile( const struct makefile *make )
1643 FILE *ret;
1645 if (make->obj_dir)
1646 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1647 else
1648 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1650 input_line = 0;
1651 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1652 return ret;
1656 /*******************************************************************
1657 * get_make_variable
1659 static const char *get_make_variable( const struct makefile *make, const char *name )
1661 const char *ret;
1663 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1664 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1665 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1666 return NULL;
1670 /*******************************************************************
1671 * get_expanded_make_variable
1673 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1675 const char *var;
1676 char *p, *end, *expand, *tmp;
1678 var = get_make_variable( make, name );
1679 if (!var) return NULL;
1681 p = expand = xstrdup( var );
1682 while ((p = strchr( p, '$' )))
1684 if (p[1] == '(')
1686 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1687 *end++ = 0;
1688 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1689 var = get_make_variable( make, p + 2 );
1690 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1691 /* switch to the new string */
1692 p = tmp + (p - expand);
1693 free( expand );
1694 expand = tmp;
1696 else if (p[1] == '{') /* don't expand ${} variables */
1698 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1699 p = end + 1;
1701 else if (p[1] == '$')
1703 p += 2;
1705 else fatal_error( "syntax error in '%s'\n", expand );
1708 /* consider empty variables undefined */
1709 p = skip_spaces( expand );
1710 if (*p) return expand;
1711 free( expand );
1712 return NULL;
1716 /*******************************************************************
1717 * get_expanded_make_var_array
1719 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1721 struct strarray ret = empty_strarray;
1722 char *value, *token;
1724 if ((value = get_expanded_make_variable( make, name )))
1725 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1726 strarray_add( &ret, token );
1727 return ret;
1731 /*******************************************************************
1732 * get_expanded_file_local_var
1734 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1735 const char *name )
1737 char *p, *var = strmake( "%s_%s", file, name );
1739 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1740 return get_expanded_make_var_array( make, var );
1744 /*******************************************************************
1745 * get_expanded_arch_var
1747 static char *get_expanded_arch_var( const struct makefile *make, const char *name, int arch )
1749 return get_expanded_make_variable( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1753 /*******************************************************************
1754 * get_expanded_arch_var_array
1756 static struct strarray get_expanded_arch_var_array( const struct makefile *make, const char *name, int arch )
1758 return get_expanded_make_var_array( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1762 /*******************************************************************
1763 * set_make_variable
1765 static int set_make_variable( struct strarray *array, const char *assignment )
1767 char *p, *name;
1769 p = name = xstrdup( assignment );
1770 while (isalnum(*p) || *p == '_') p++;
1771 if (name == p) return 0; /* not a variable */
1772 if (isspace(*p))
1774 *p++ = 0;
1775 p = skip_spaces( p );
1777 if (*p != '=') return 0; /* not an assignment */
1778 *p++ = 0;
1779 p = skip_spaces( p );
1781 strarray_set_value( array, name, p );
1782 return 1;
1786 /*******************************************************************
1787 * parse_makefile
1789 static struct makefile *parse_makefile( const char *path )
1791 char *buffer;
1792 FILE *file;
1793 struct makefile *make = xmalloc( sizeof(*make) );
1795 memset( make, 0, sizeof(*make) );
1796 make->obj_dir = path;
1797 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1798 if (path && !strcmp( path, "include" )) include_makefile = make;
1800 file = open_input_makefile( make );
1801 while ((buffer = get_line( file )))
1803 if (!strncmp( buffer, separator, strlen(separator) )) break;
1804 if (*buffer == '\t') continue; /* command */
1805 buffer = skip_spaces( buffer );
1806 if (*buffer == '#') continue; /* comment */
1807 set_make_variable( &make->vars, buffer );
1809 fclose( file );
1810 input_file_name = NULL;
1811 return make;
1815 /*******************************************************************
1816 * add_generated_sources
1818 static void add_generated_sources( struct makefile *make )
1820 unsigned int i, arch;
1821 struct incl_file *source, *next, *file, *dlldata = NULL;
1822 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1824 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1826 for (arch = 0; arch < archs.count; arch++)
1828 if (!is_multiarch( arch )) continue;
1829 if (source->file->flags & FLAG_IDL_CLIENT)
1831 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL, arch );
1832 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1833 add_all_includes( make, file, file->file );
1835 if (source->file->flags & FLAG_IDL_SERVER)
1837 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL, arch );
1838 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1839 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1840 add_all_includes( make, file, file->file );
1842 if (source->file->flags & FLAG_IDL_IDENT)
1844 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL, arch );
1845 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1846 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1847 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1848 add_all_includes( make, file, file->file );
1850 if (source->file->flags & FLAG_IDL_PROXY)
1852 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL, arch );
1853 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1854 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1855 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1856 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1857 add_all_includes( make, file, file->file );
1859 if (source->file->flags & FLAG_IDL_TYPELIB)
1861 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL, arch );
1863 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1865 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL, arch );
1867 if (source->file->flags & FLAG_IDL_REGISTER)
1869 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL, arch );
1873 /* now the arch-independent files */
1875 if ((source->file->flags & FLAG_IDL_PROXY) && !dlldata)
1877 dlldata = add_generated_source( make, "dlldata.o", "dlldata.c", 0 );
1878 add_dependency( dlldata->file, "objbase.h", INCL_NORMAL );
1879 add_dependency( dlldata->file, "rpcproxy.h", INCL_NORMAL );
1880 add_all_includes( make, dlldata, dlldata->file );
1882 if (source->file->flags & FLAG_IDL_HEADER)
1884 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1886 if (!source->file->flags && strendswith( source->name, ".idl" ))
1888 if (!strncmp( source->name, "wine/", 5 )) continue;
1889 source->file->flags = FLAG_IDL_HEADER | FLAG_INSTALL;
1890 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1892 if (strendswith( source->name, ".x" ))
1894 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL, 0 );
1896 if (strendswith( source->name, ".y" ))
1898 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL, 0 );
1899 /* steal the includes list from the source file */
1900 file->files_count = source->files_count;
1901 file->files_size = source->files_size;
1902 file->files = source->files;
1903 source->files_count = source->files_size = 0;
1904 source->files = NULL;
1906 if (strendswith( source->name, ".l" ))
1908 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL, 0 );
1909 /* steal the includes list from the source file */
1910 file->files_count = source->files_count;
1911 file->files_size = source->files_size;
1912 file->files = source->files;
1913 source->files_count = source->files_size = 0;
1914 source->files = NULL;
1916 if (strendswith( source->name, ".po" ))
1918 if (!make->disabled[0])
1919 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1921 if (strendswith( source->name, ".spec" ))
1923 char *obj = replace_extension( source->name, ".spec", "" );
1924 strarray_addall_uniq( &make->extra_imports,
1925 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1927 if (strendswith( source->name, ".xml" ))
1929 char *code_name = replace_extension( source->name , ".xml", "-protocol.c" );
1930 char *header_name = replace_extension( source->name , ".xml", "-client-protocol.h" );
1932 file = add_generated_source( make, code_name, NULL, 0 );
1933 file->file->flags |= FLAG_C_UNIX;
1934 file->use_msvcrt = 0;
1935 file = add_generated_source( make, header_name, NULL, 0 );
1936 file->file->flags |= FLAG_C_UNIX;
1937 file->use_msvcrt = 0;
1939 free( code_name );
1940 free( header_name );
1943 if (make->testdll)
1945 for (arch = 0; arch < archs.count; arch++)
1947 if (!is_multiarch( arch )) continue;
1948 file = add_generated_source( make, "testlist.o", "testlist.c", arch );
1949 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1950 add_all_includes( make, file, file->file );
1953 for (i = 0; i < objs.count; i++)
1955 /* default to .c for unknown extra object files */
1956 if (strendswith( objs.str[i], ".o" ))
1958 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ), 0);
1959 file->file->flags |= FLAG_C_UNIX;
1960 file->use_msvcrt = 0;
1962 else if (strendswith( objs.str[i], ".res" ))
1963 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL, 0 );
1964 else
1965 add_generated_source( make, objs.str[i], NULL, 0 );
1970 /*******************************************************************
1971 * create_dir
1973 static void create_dir( const char *dir )
1975 char *p, *path;
1977 p = path = xstrdup( dir );
1978 while ((p = strchr( p, '/' )))
1980 *p = 0;
1981 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1982 *p++ = '/';
1983 while (*p == '/') p++;
1985 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1986 free( path );
1990 /*******************************************************************
1991 * create_file_directories
1993 * Create the base directories of all the files.
1995 static void create_file_directories( const struct makefile *make, struct strarray files )
1997 struct strarray subdirs = empty_strarray;
1998 unsigned int i;
1999 char *dir;
2001 for (i = 0; i < files.count; i++)
2003 if (!strchr( files.str[i], '/' )) continue;
2004 dir = obj_dir_path( make, files.str[i] );
2005 *strrchr( dir, '/' ) = 0;
2006 strarray_add_uniq( &subdirs, dir );
2009 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2013 /*******************************************************************
2014 * output_filenames_obj_dir
2016 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
2018 unsigned int i;
2020 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
2024 /*******************************************************************
2025 * get_dependencies
2027 static void get_dependencies( struct incl_file *file, struct incl_file *source )
2029 unsigned int i;
2031 if (!file->filename) return;
2033 if (file != source)
2035 if (file->owner == source) return; /* already processed */
2036 if (file->type == INCL_IMPORTLIB)
2038 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
2039 return; /* library is imported only when building a typelib */
2040 strarray_add( &source->importlibdeps, file->filename );
2042 else strarray_add( &source->dependencies, file->filename );
2043 file->owner = source;
2045 /* sanity checks */
2046 if (!strcmp( file->filename, "include/config.h" ) &&
2047 file != source->files[0] && !source->is_external)
2049 input_file_name = source->filename;
2050 input_line = 0;
2051 for (i = 0; i < source->file->deps_count; i++)
2053 if (!strcmp( source->file->deps[i].name, file->name ))
2054 input_line = source->file->deps[i].line;
2056 fatal_error( "%s must be included before other headers\n", file->name );
2060 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
2064 /*******************************************************************
2065 * get_local_dependencies
2067 * Get the local dependencies of a given target.
2069 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
2070 struct strarray targets )
2072 unsigned int i;
2073 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2075 for (i = 0; i < deps.count; i++)
2077 if (strarray_exists( &targets, deps.str[i] ))
2078 deps.str[i] = obj_dir_path( make, deps.str[i] );
2079 else
2080 deps.str[i] = src_dir_path( make, deps.str[i] );
2082 return deps;
2086 /*******************************************************************
2087 * get_static_lib
2089 * Find the makefile that builds the named static library (which may be an import lib).
2091 static struct makefile *get_static_lib( const char *name, unsigned int arch )
2093 unsigned int i;
2095 for (i = 0; i < subdirs.count; i++)
2097 if (submakes[i]->importlib && !strcmp( submakes[i]->importlib, name )) return submakes[i];
2098 if (!submakes[i]->staticlib) continue;
2099 if (submakes[i]->disabled[arch]) continue;
2100 if (strncmp( submakes[i]->staticlib, "lib", 3 )) continue;
2101 if (strncmp( submakes[i]->staticlib + 3, name, strlen(name) )) continue;
2102 if (strcmp( submakes[i]->staticlib + 3 + strlen(name), ".a" )) continue;
2103 return submakes[i];
2105 return NULL;
2109 /*******************************************************************
2110 * get_native_unix_lib
2112 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2114 if (!make->unixlib) return NULL;
2115 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2116 if (make->unixlib[strlen(name)] != '.') return NULL;
2117 return obj_dir_path( make, make->unixlib );
2121 /*******************************************************************
2122 * get_parent_makefile
2124 static struct makefile *get_parent_makefile( struct makefile *make )
2126 char *dir, *p;
2127 unsigned int i;
2129 if (!make->obj_dir) return NULL;
2130 dir = xstrdup( make->obj_dir );
2131 if (!(p = strrchr( dir, '/' ))) return NULL;
2132 *p = 0;
2133 for (i = 0; i < subdirs.count; i++)
2134 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2135 return NULL;
2139 /*******************************************************************
2140 * needs_delay_lib
2142 static int needs_delay_lib( const struct makefile *make, unsigned int arch )
2144 if (delay_load_flags[arch]) return 0;
2145 if (!make->importlib) return 0;
2146 return strarray_exists( &delay_import_libs, make->importlib );
2150 /*******************************************************************
2151 * add_unix_libraries
2153 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2155 struct strarray ret = empty_strarray;
2156 struct strarray all_libs = empty_strarray;
2157 unsigned int i, j;
2159 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2160 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
2162 for (i = 0; i < all_libs.count; i++)
2164 const char *lib = NULL;
2166 if (!strncmp( all_libs.str[i], "-l", 2 ))
2168 for (j = 0; j < subdirs.count; j++)
2170 if (make == submakes[j]) continue;
2171 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2174 if (lib)
2176 strarray_add( deps, lib );
2177 strarray_add( &ret, lib );
2179 else strarray_add( &ret, all_libs.str[i] );
2182 strarray_addall( &ret, libs );
2183 return ret;
2187 /*******************************************************************
2188 * is_crt_module
2190 static int is_crt_module( const char *file )
2192 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2196 /*******************************************************************
2197 * get_default_crt
2199 static const char *get_default_crt( const struct makefile *make )
2201 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2202 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2206 /*******************************************************************
2207 * get_crt_define
2209 static const char *get_crt_define( const struct makefile *make )
2211 const char *crt_dll = NULL;
2212 unsigned int i, version = 0;
2214 for (i = 0; i < make->imports.count; i++)
2216 if (!is_crt_module( make->imports.str[i] )) continue;
2217 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2218 crt_dll, make->imports.str[i] );
2219 crt_dll = make->imports.str[i];
2222 if (!crt_dll)
2224 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2225 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2227 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2228 sscanf( crt_dll, "msvcr%u", &version );
2229 return strmake( "-D_MSVCR_VER=%u", version );
2233 /*******************************************************************
2234 * get_default_imports
2236 static struct strarray get_default_imports( const struct makefile *make, struct strarray imports )
2238 struct strarray ret = empty_strarray;
2239 const char *crt_dll = get_default_crt( make );
2240 unsigned int i;
2242 for (i = 0; i < imports.count; i++)
2243 if (is_crt_module( imports.str[i] ))
2244 crt_dll = imports.str[i];
2246 strarray_add( &ret, "winecrt0" );
2247 if (crt_dll) strarray_add( &ret, crt_dll );
2249 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2250 strarray_add( &ret, "kernel" );
2252 strarray_add( &ret, "kernel32" );
2253 strarray_add( &ret, "ntdll" );
2254 return ret;
2257 enum import_type
2259 IMPORT_TYPE_DIRECT,
2260 IMPORT_TYPE_DELAYED,
2261 IMPORT_TYPE_DEFAULT,
2264 /*******************************************************************
2265 * add_import_libs
2267 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2268 struct strarray imports, enum import_type type, unsigned int arch )
2270 struct strarray ret = empty_strarray;
2271 unsigned int i;
2273 for (i = 0; i < imports.count; i++)
2275 const char *name = imports.str[i];
2276 struct makefile *submake;
2278 /* add crt import lib only when adding the default imports libs */
2279 if (is_crt_module( imports.str[i] ) && type != IMPORT_TYPE_DEFAULT) continue;
2281 if (name[0] == '-')
2283 switch (name[1])
2285 case 'L': strarray_add( &ret, name ); continue;
2286 case 'l': name += 2; break;
2287 default: continue;
2290 else name = get_base_name( name );
2292 if ((submake = get_static_lib( name, arch )))
2294 const char *ext = (type == IMPORT_TYPE_DELAYED && !delay_load_flags[arch]) ? ".delay.a" : ".a";
2295 const char *lib = obj_dir_path( submake, strmake( "%slib%s%s", arch_dirs[arch], name, ext ));
2296 strarray_add_uniq( deps, lib );
2297 strarray_add( &ret, lib );
2299 else strarray_add( &ret, strmake( "-l%s", name ));
2301 return ret;
2305 /*******************************************************************
2306 * add_install_rule
2308 static void add_install_rule( struct makefile *make, const char *target, unsigned int arch,
2309 const char *file, const char *dest )
2311 unsigned int i;
2313 if (make->disabled[arch]) return;
2315 for (i = 0; i < NB_INSTALL_RULES; i++)
2317 if (strarray_exists( &make->install[i], target ) ||
2318 strarray_exists( &top_install[i], make->obj_dir ) ||
2319 strarray_exists( &top_install[i], obj_dir_path( make, target )))
2321 strarray_add( &make->install_rules[i], file );
2322 strarray_add( &make->install_rules[i], dest );
2323 break;
2329 /*******************************************************************
2330 * get_include_install_path
2332 * Determine the installation path for a given include file.
2334 static const char *get_include_install_path( const char *name )
2336 if (!strncmp( name, "wine/", 5 )) return name + 5;
2337 if (!strncmp( name, "msvcrt/", 7 )) return name;
2338 return strmake( "windows/%s", name );
2342 /*******************************************************************
2343 * get_source_defines
2345 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2346 const char *obj )
2348 unsigned int i;
2349 struct strarray ret = empty_strarray;
2351 strarray_addall( &ret, make->include_args );
2352 if (source->use_msvcrt)
2354 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2355 for (i = 0; i < make->include_paths.count; i++)
2356 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2357 strarray_add( &ret, get_crt_define( make ));
2359 strarray_addall( &ret, make->define_args );
2360 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2361 return ret;
2365 /*******************************************************************
2366 * remove_warning_flags
2368 static struct strarray remove_warning_flags( struct strarray flags )
2370 unsigned int i;
2371 struct strarray ret = empty_strarray;
2373 for (i = 0; i < flags.count; i++)
2374 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2375 strarray_add( &ret, flags.str[i] );
2376 return ret;
2380 /*******************************************************************
2381 * get_debug_file
2383 static const char *get_debug_file( struct makefile *make, const char *name, unsigned int arch )
2385 const char *debug_file = NULL;
2386 if (!debug_flags[arch]) return NULL;
2387 if (!strcmp( debug_flags[arch], "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2388 else if (!strncmp( debug_flags[arch], "split", 5 )) debug_file = strmake( "%s.debug", name );
2389 if (debug_file) strarray_add( &make->debug_files, debug_file );
2390 return debug_file;
2394 /*******************************************************************
2395 * cmd_prefix
2397 static const char *cmd_prefix( const char *cmd )
2399 if (!silent_rules) return "";
2400 return strmake( "$(quiet_%s)", cmd );
2404 /*******************************************************************
2405 * output_winegcc_command
2407 static void output_winegcc_command( struct makefile *make, unsigned int arch )
2409 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2410 output_filename( "--wine-objdir ." );
2411 if (tools_dir)
2413 output_filename( "--winebuild" );
2414 output_filename( tools_path( make, "winebuild" ));
2416 output_filenames( target_flags[arch] );
2417 if (arch) return;
2418 output_filename( "-mno-cygwin" );
2419 output_filenames( lddll_flags );
2423 /*******************************************************************
2424 * output_symlink_rule
2426 * Output a rule to create a symlink.
2428 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2430 const char *name = strrchr( link_name, '/' );
2431 char *dir = NULL;
2433 if (name)
2435 dir = xstrdup( link_name );
2436 dir[name - link_name] = 0;
2439 output( "\t%s", cmd_prefix( "LN" ));
2440 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2441 output( "rm -f %s && ", link_name );
2443 /* dest path with a directory needs special handling if ln -s isn't supported */
2444 if (dir && strcmp( ln_s, "ln -s" ))
2445 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2446 else
2447 output( "%s %s %s\n", ln_s, src_name, link_name );
2449 free( dir );
2453 /*******************************************************************
2454 * output_srcdir_symlink
2456 * Output rule to create a symlink back to the source directory, for source files
2457 * that are needed at run-time.
2459 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2461 char *src_file, *dst_file, *src_name;
2463 if (!make->src_dir) return;
2464 src_file = src_dir_path( make, obj );
2465 dst_file = obj_dir_path( make, obj );
2466 output( "%s: %s\n", dst_file, src_file );
2468 src_name = src_file;
2469 if (src_name[0] != '/' && make->obj_dir)
2470 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2472 output_symlink_rule( src_name, dst_file, 0 );
2473 strarray_add( &make->all_targets[0], obj );
2477 /*******************************************************************
2478 * output_install_commands
2480 static void output_install_commands( struct makefile *make, struct strarray files )
2482 unsigned int i, arch;
2483 char *install_sh = root_src_dir_path( "tools/install-sh" );
2485 for (i = 0; i < files.count; i += 2)
2487 const char *file = files.str[i];
2488 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2489 char type = *files.str[i + 1];
2491 switch (type)
2493 case '1': case '2': case '3': case '4': case '5':
2494 case '6': case '7': case '8': case '9': /* arch-dependent program */
2495 arch = type - '0';
2496 output( "\tSTRIPPROG=%s %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2497 strip_progs[arch], install_sh, obj_dir_path( make, file ), dest );
2498 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2499 break;
2500 case 'd': /* data file */
2501 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2502 install_sh, obj_dir_path( make, file ), dest );
2503 break;
2504 case 'D': /* data file in source dir */
2505 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2506 install_sh, src_dir_path( make, file ), dest );
2507 break;
2508 case '0': /* native arch program file */
2509 case 'p': /* program file */
2510 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2511 install_sh, obj_dir_path( make, file ), dest );
2512 break;
2513 case 's': /* script */
2514 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2515 install_sh, obj_dir_path( make, file ), dest );
2516 break;
2517 case 'S': /* script in source dir */
2518 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2519 install_sh, src_dir_path( make, file ), dest );
2520 break;
2521 case 't': /* script in tools dir */
2522 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2523 install_sh, tools_dir_path( make, files.str[i] ), dest );
2524 break;
2525 case 'y': /* symlink */
2526 output_symlink_rule( files.str[i], dest, 1 );
2527 break;
2528 default:
2529 assert(0);
2531 strarray_add( &make->uninstall_files, dest );
2536 /*******************************************************************
2537 * output_install_rules
2539 * Rules are stored as a (file,dest) pair of values.
2540 * The first char of dest indicates the type of install.
2542 static void output_install_rules( struct makefile *make, enum install_rules rules )
2544 unsigned int i;
2545 struct strarray files = make->install_rules[rules];
2546 struct strarray targets = empty_strarray;
2548 if (!files.count) return;
2550 for (i = 0; i < files.count; i += 2)
2552 const char *file = files.str[i];
2553 switch (*files.str[i + 1])
2555 case '0': case '1': case '2': case '3': case '4':
2556 case '5': case '6': case '7': case '8': case '9': /* arch-dependent program */
2557 case 'd': /* data file */
2558 case 'p': /* program file */
2559 case 's': /* script */
2560 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2561 break;
2562 case 't': /* script in tools dir */
2563 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2564 break;
2568 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, install_targets[rules] ));
2569 output_filenames( targets );
2570 output( "\n" );
2571 output_install_commands( make, files );
2572 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2573 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, install_targets[rules] ));
2577 static int cmp_string_length( const char **a, const char **b )
2579 int paths_a = 0, paths_b = 0;
2580 const char *p;
2582 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2583 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2584 if (paths_b != paths_a) return paths_b - paths_a;
2585 return strcmp( *a, *b );
2588 /*******************************************************************
2589 * get_removable_dirs
2591 * Retrieve a list of directories to try to remove after deleting the files.
2593 static struct strarray get_removable_dirs( struct strarray files )
2595 struct strarray dirs = empty_strarray;
2596 unsigned int i;
2598 for (i = 0; i < files.count; i++)
2600 char *dir = xstrdup( files.str[i] );
2601 while (strchr( dir, '/' ))
2603 *strrchr( dir, '/' ) = 0;
2604 strarray_add_uniq( &dirs, xstrdup(dir) );
2607 strarray_qsort( &dirs, cmp_string_length );
2608 return dirs;
2612 /*******************************************************************
2613 * output_uninstall_rules
2615 static void output_uninstall_rules( struct makefile *make )
2617 static const char *dirs_order[] =
2618 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2620 struct strarray uninstall_dirs;
2621 unsigned int i, j;
2623 if (!make->uninstall_files.count) return;
2624 output( "uninstall::\n" );
2625 output_rm_filenames( make->uninstall_files, "rm -f" );
2626 strarray_add_uniq( &make->phony_targets, "uninstall" );
2628 if (!subdirs.count) return;
2629 uninstall_dirs = get_removable_dirs( make->uninstall_files );
2630 output( "\t-rmdir" );
2631 for (i = 0; i < ARRAY_SIZE(dirs_order); i++)
2633 for (j = 0; j < uninstall_dirs.count; j++)
2635 if (!uninstall_dirs.str[j]) continue;
2636 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2637 continue;
2638 output_filename( uninstall_dirs.str[j] );
2639 uninstall_dirs.str[j] = NULL;
2642 for (j = 0; j < uninstall_dirs.count; j++)
2643 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2644 output( "\n" );
2648 /*******************************************************************
2649 * output_po_files
2651 static void output_po_files( struct makefile *make )
2653 const char *po_dir = src_dir_path( make, "po" );
2654 unsigned int i;
2656 if (linguas.count)
2658 for (i = 0; i < linguas.count; i++)
2659 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2660 output( ": %s/wine.pot\n", po_dir );
2661 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2662 cmd_prefix( "MSG" ), po_dir );
2663 output( "po/all:" );
2664 for (i = 0; i < linguas.count; i++)
2665 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2666 output( "\n" );
2668 output( "%s/wine.pot:", po_dir );
2669 output_filenames( make->pot_files );
2670 output( "\n" );
2671 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2672 output_filenames( make->pot_files );
2673 output( "\n" );
2674 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2678 /*******************************************************************
2679 * output_source_y
2681 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2683 char *header = strmake( "%s.tab.h", obj );
2685 if (find_include_file( make, header ))
2687 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2688 output( "\t%s%s -o %s.tab.$$$$.c --defines=$@ %s && rm -f %s.tab.$$$$.c\n",
2689 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ),
2690 source->filename, obj_dir_path( make, obj ));
2691 strarray_add( &make->clean_files, header );
2693 output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2694 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2698 /*******************************************************************
2699 * output_source_l
2701 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2703 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2704 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2708 /*******************************************************************
2709 * output_source_h
2711 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2713 if (source->file->flags & FLAG_GENERATED)
2714 strarray_add( &make->all_targets[0], source->name );
2715 else if ((source->file->flags & FLAG_INSTALL) || strncmp( source->name, "wine/", 5 ))
2716 add_install_rule( make, source->name, 0, source->name,
2717 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2721 /*******************************************************************
2722 * output_source_rc
2724 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2726 struct strarray defines = get_source_defines( make, source, obj );
2727 const char *po_dir = NULL, *res_file = strmake( "%s.res", obj );
2728 unsigned int i, arch;
2730 if (source->file->flags & FLAG_RC_HEADER) return;
2731 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2732 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2733 if (!(source->file->flags & FLAG_TESTDLL))
2735 for (arch = 0; arch < archs.count; arch++)
2736 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2738 else strarray_add( &make->clean_files, res_file );
2740 if (source->file->flags & FLAG_RC_PO)
2742 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2743 output( "%s.pot ", obj_dir_path( make, obj ) );
2745 output( "%s: %s", obj_dir_path( make, res_file ), source->filename );
2746 output_filename( tools_path( make, "wrc" ));
2747 if (make->src_dir) output_filename( "nls/locale.nls" );
2748 output_filenames( source->dependencies );
2749 output( "\n" );
2750 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2751 if (make->is_win16) output_filename( "-m16" );
2752 output_filename( "--nostdinc" );
2753 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2754 output_filenames( defines );
2755 output_filename( source->filename );
2756 output( "\n" );
2757 if (po_dir)
2759 output( "%s:", obj_dir_path( make, res_file ));
2760 for (i = 0; i < linguas.count; i++)
2761 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2762 output( "\n" );
2767 /*******************************************************************
2768 * output_source_mc
2770 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2772 unsigned int i, arch;
2773 char *obj_path = obj_dir_path( make, obj );
2774 char *res_file = strmake( "%s.res", obj );
2776 for (arch = 0; arch < archs.count; arch++)
2777 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2778 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2779 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2780 output_filename( tools_path( make, "wmc" ));
2781 output_filenames( source->dependencies );
2782 if (make->src_dir) output_filename( "nls/locale.nls" );
2783 output( "\n" );
2784 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2785 if (linguas.count)
2787 output_filename( "--po-dir=po" );
2788 output( "\n" );
2789 output( "%s.res:", obj_dir_path( make, obj ));
2790 for (i = 0; i < linguas.count; i++)
2791 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2793 output( "\n" );
2797 /*******************************************************************
2798 * output_source_res
2800 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2802 if (make->disabled[source->arch]) return;
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 deps = empty_strarray;
2815 struct strarray multiarch_targets[MAX_ARCHS] = { empty_strarray };
2816 const char *dest;
2817 unsigned int i, arch;
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 for (arch = 0; arch < archs.count; arch++)
2843 if (!is_multiarch( arch )) continue;
2844 if (make->disabled[arch]) 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 struct strarray arch_deps = empty_strarray;
2855 if (!arch) strarray_addall( &arch_deps, headers );
2856 strarray_addall( &arch_deps, multiarch_targets[arch] );
2857 if (!arch_deps.count) continue;
2858 output_filenames_obj_dir( make, arch_deps );
2859 output( ":\n" );
2860 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2861 output_filenames( target_flags[arch] );
2862 output_filename( "--nostdinc" );
2863 output_filename( "-Ldlls/\\*" );
2864 output_filenames( defines );
2865 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2866 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2867 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRAIDLFLAGS", arch ));
2868 output_filename( source->filename );
2869 output( "\n" );
2870 strarray_addall( &deps, arch_deps );
2873 if (deps.count)
2875 output_filenames_obj_dir( make, deps );
2876 output( ":" );
2877 output_filename( tools_path( make, "widl" ));
2878 output_filename( source->filename );
2879 output_filenames( source->dependencies );
2880 output( "\n" );
2883 if (source->importlibdeps.count)
2885 for (arch = 0; arch < archs.count; arch++)
2887 if (!multiarch_targets[arch].count) continue;
2888 output_filenames_obj_dir( make, multiarch_targets[arch] );
2889 output( ":" );
2890 for (i = 0; i < source->importlibdeps.count; i++)
2892 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2893 const char *module = strmake( "%s%s", arch_pe_dirs[arch], submake->module );
2894 output_filename( obj_dir_path( submake, module ));
2896 output( "\n" );
2902 /*******************************************************************
2903 * output_source_x
2905 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2907 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2908 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2909 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2910 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2911 if (source->file->flags & FLAG_INSTALL)
2913 add_install_rule( make, source->name, 0, source->name,
2914 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2915 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2916 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2921 /*******************************************************************
2922 * output_source_sfd
2924 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2926 unsigned int i;
2927 char *ttf_obj = strmake( "%s.ttf", obj );
2928 char *ttf_file = src_dir_path( make, ttf_obj );
2930 if (fontforge && !make->src_dir)
2932 output( "%s: %s\n", ttf_file, source->filename );
2933 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2934 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2935 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2936 strarray_add( &make->maintainerclean_files, ttf_obj );
2938 if (source->file->flags & FLAG_INSTALL)
2940 add_install_rule( make, source->name, 0, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2941 output_srcdir_symlink( make, ttf_obj );
2944 if (source->file->flags & FLAG_SFD_FONTS)
2946 struct strarray *array = source->file->args;
2948 for (i = 0; i < array->count; i++)
2950 char *font = strtok( xstrdup(array->str[i]), " \t" );
2951 char *args = strtok( NULL, "" );
2953 strarray_add( &make->all_targets[0], xstrdup( font ));
2954 output( "%s: %s %s\n", obj_dir_path( make, font ),
2955 tools_path( make, "sfnt2fon" ), ttf_file );
2956 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2957 tools_path( make, "sfnt2fon" ), ttf_file, args );
2958 add_install_rule( make, source->name, 0, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2964 /*******************************************************************
2965 * output_source_svg
2967 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2969 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2970 unsigned int i;
2972 if (convert && rsvg && icotool)
2974 for (i = 0; images[i]; i++)
2975 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2977 if (images[i])
2979 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2980 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2981 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2982 root_src_dir_path( "tools/buildimage" ), source->filename );
2983 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2989 /*******************************************************************
2990 * output_source_nls
2992 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2994 add_install_rule( make, source->name, 0, source->name,
2995 strmake( "D$(nlsdir)/%s", source->name ));
2996 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
3000 /*******************************************************************
3001 * output_source_desktop
3003 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
3005 add_install_rule( make, source->name, 0, source->name,
3006 strmake( "D$(datadir)/applications/%s", source->name ));
3010 /*******************************************************************
3011 * output_source_po
3013 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
3015 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
3016 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
3017 strarray_add( &make->all_targets[0], strmake( "%s.mo", obj ));
3021 /*******************************************************************
3022 * output_source_in
3024 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
3026 unsigned int i;
3028 if (make == include_makefile) return; /* ignore generated includes */
3029 if (strendswith( obj, ".man" ) && source->file->args)
3031 struct strarray symlinks;
3032 char *dir, *dest = replace_extension( obj, ".man", "" );
3033 char *lang = strchr( dest, '.' );
3034 char *section = source->file->args;
3035 if (lang)
3037 *lang++ = 0;
3038 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3040 else dir = strmake( "$(mandir)/man%s", section );
3041 add_install_rule( make, dest, 0, obj, strmake( "d%s/%s.%s", dir, dest, section ));
3042 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3043 for (i = 0; i < symlinks.count; i++)
3044 add_install_rule( make, symlinks.str[i], 0, strmake( "%s.%s", dest, section ),
3045 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3046 free( dest );
3047 free( dir );
3049 strarray_add( &make->in_files, obj );
3050 strarray_add( &make->all_targets[0], obj );
3051 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3052 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
3053 output( "%s:", obj_dir_path( make, obj ));
3054 output_filenames( source->dependencies );
3055 output( "\n" );
3056 add_install_rule( make, obj, 0, obj, strmake( "d$(datadir)/wine/%s", obj ));
3060 /*******************************************************************
3061 * output_source_spec
3063 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3065 /* nothing to do */
3069 /*******************************************************************
3070 * output_source_testdll
3072 static void output_source_testdll( struct makefile *make, struct incl_file *source, const char *obj )
3074 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3075 struct strarray dll_flags = empty_strarray;
3076 struct strarray default_imports = empty_strarray;
3077 struct strarray all_libs, dep_libs;
3078 const char *dll_name, *obj_name, *res_name, *output_rsrc, *output_file, *debug_file, *ext = ".dll";
3079 struct incl_file *spec_file = find_src_file( make, strmake( "%s.spec", obj ));
3080 unsigned int arch;
3082 if (!imports.count) imports = make->imports;
3083 strarray_addall( &dll_flags, make->extradllflags );
3084 strarray_addall( &dll_flags, get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" ));
3085 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3086 if (strarray_exists( &dll_flags, "-mconsole" )) ext = ".exe";
3088 for (arch = 0; arch < archs.count; arch++)
3090 if (!is_multiarch( arch )) continue;
3091 all_libs = dep_libs = empty_strarray;
3092 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ) );
3093 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3094 if (!arch) strarray_addall( &all_libs, libs );
3095 dll_name = arch_module_name( strmake( "%s%s", obj, ext ), arch );
3096 obj_name = obj_dir_path( make, strmake( "%s%s.o", arch_dirs[arch], obj ));
3097 output_file = obj_dir_path( make, dll_name );
3098 output_rsrc = strmake( "%s.res", dll_name );
3100 if (!find_src_file( make, strmake( "%s.rc", obj ) )) res_name = NULL;
3101 else res_name = obj_dir_path( make, strmake( "%s.res", obj ) );
3103 strarray_add( &make->clean_files, dll_name );
3104 strarray_add( &make->res_files[arch], output_rsrc );
3105 output( "%s:", obj_dir_path( make, output_rsrc ));
3106 output_filename( output_file );
3107 output_filename( tools_path( make, "wrc" ));
3108 output( "\n" );
3109 output( "\t%secho \"%s%s TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, ext, output_file,
3110 tools_path( make, "wrc" ));
3112 output( "%s:", output_file );
3113 if (spec_file) output_filename( spec_file->filename );
3114 output_filename( obj_name );
3115 if (res_name) output_filename( res_name );
3116 output_filenames( dep_libs );
3117 output_filename( tools_path( make, "winebuild" ));
3118 output_filename( tools_path( make, "winegcc" ));
3119 output( "\n" );
3120 output_winegcc_command( make, arch );
3121 output_filename( "-s" );
3122 output_filenames( dll_flags );
3123 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3124 if (!strcmp( ext, ".dll" )) output_filename( "-shared" );
3125 if (spec_file) output_filename( spec_file->filename );
3126 output_filename( obj_name );
3127 if (res_name) output_filename( res_name );
3128 if ((debug_file = get_debug_file( make, dll_name, arch )))
3129 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3130 output_filenames( all_libs );
3131 output_filename( arch_make_variable( "LDFLAGS", arch ));
3132 output( "\n" );
3137 /*******************************************************************
3138 * output_source_xml
3140 static void output_source_xml( struct makefile *make, struct incl_file *source, const char *obj )
3142 if (wayland_scanner)
3144 output( "%s-protocol.c: %s\n", obj_dir_path( make, obj ), source->filename );
3145 output( "\t%s%s private-code %s $@\n", cmd_prefix( "GEN" ), wayland_scanner, source->filename );
3146 output( "%s-client-protocol.h: %s\n", obj_dir_path( make, obj ), source->filename );
3147 output( "\t%s%s client-header %s $@\n", cmd_prefix( "GEN" ), wayland_scanner, source->filename );
3152 /*******************************************************************
3153 * output_source_one_arch
3155 static void output_source_one_arch( struct makefile *make, struct incl_file *source, const char *obj,
3156 struct strarray defines, struct strarray *targets,
3157 unsigned int arch )
3159 const char *obj_name;
3161 if (make->disabled[arch] && !(source->file->flags & FLAG_C_IMPLIB)) return;
3163 if (arch)
3165 if (source->file->flags & FLAG_C_UNIX) return;
3166 if (!is_using_msvcrt( make ) && !make->staticlib && !(source->file->flags & FLAG_C_IMPLIB)) return;
3168 else if (source->file->flags & FLAG_C_UNIX)
3170 if (!unix_lib_supported) return;
3172 else if (archs.count > 1 && is_using_msvcrt( make ))
3174 if (!so_dll_supported) return;
3175 if (!(source->file->flags & FLAG_C_IMPLIB) && (!make->staticlib || make->extlib)) return;
3178 obj_name = strmake( "%s%s.o", source->arch ? "" : arch_dirs[arch], obj );
3179 strarray_add( targets, obj_name );
3181 if (source->file->flags & FLAG_C_UNIX)
3182 strarray_add( &make->unixobj_files, obj_name );
3183 else if (source->file->flags & FLAG_C_IMPLIB)
3184 strarray_add( &make->implib_files[arch], obj_name );
3185 else if (!(source->file->flags & FLAG_TESTDLL))
3186 strarray_add( &make->object_files[arch], obj_name );
3187 else
3188 strarray_add( &make->clean_files, obj_name );
3190 output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename );
3191 output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), arch_make_variable( "CC", arch ), source->filename );
3192 output_filenames( defines );
3193 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3194 output_filenames( make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] );
3195 if (!arch)
3197 if (source->file->flags & FLAG_C_UNIX)
3199 output_filenames( unix_dllflags );
3201 else if (make->module || make->testdll)
3203 output_filenames( dll_flags );
3204 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3205 if (!unix_lib_supported && make->module && is_crt_module( make->module ))
3206 output_filename( "-fno-builtin" );
3209 else
3211 if (make->module && is_crt_module( make->module )) output_filename( "-fno-builtin" );
3214 output_filenames( cpp_flags );
3215 output_filename( arch_make_variable( "CFLAGS", arch ));
3216 output( "\n" );
3218 if (make->testdll && strendswith( source->name, ".c" ) &&
3219 !(source->file->flags & (FLAG_GENERATED | FLAG_TESTDLL)))
3221 const char *ok_file, *test_exe;
3223 ok_file = strmake( "%s%s.ok", arch_dirs[arch], obj );
3224 test_exe = replace_extension( make->testdll, ".dll", "_test.exe" );
3225 strarray_add( &make->ok_files[arch], ok_file );
3226 output( "%s:\n", obj_dir_path( make, ok_file ));
3227 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s %s && touch $@\n",
3228 cmd_prefix( "TEST" ),
3229 root_src_dir_path( "tools/runtest" ), make->testdll,
3230 obj_dir_path( make, arch_module_name( test_exe, arch )), obj );
3235 /*******************************************************************
3236 * output_source_default
3238 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3240 struct strarray defines = get_source_defines( make, source, obj );
3241 struct strarray targets = empty_strarray;
3242 unsigned int arch;
3244 for (arch = 0; arch < archs.count; arch++)
3245 if (!source->arch || source->arch == arch)
3246 output_source_one_arch( make, source, obj, defines, &targets, arch );
3248 if (source->file->flags & FLAG_GENERATED)
3250 if (!make->testdll || !strendswith( source->filename, "testlist.c" ))
3251 strarray_add( &make->clean_files, source->basename );
3253 else if (source->file->flags & FLAG_TESTDLL)
3255 output_source_testdll( make, source, obj );
3257 else
3259 if (make->testdll && strendswith( source->name, ".c" ))
3260 strarray_add( &make->test_files, obj );
3263 if (targets.count && source->dependencies.count)
3265 output_filenames_obj_dir( make, targets );
3266 output( ":" );
3267 output_filenames( source->dependencies );
3268 output( "\n" );
3273 /* dispatch table to output rules for a single source file */
3274 static const struct
3276 const char *ext;
3277 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3278 } output_source_funcs[] =
3280 { "y", output_source_y },
3281 { "l", output_source_l },
3282 { "h", output_source_h },
3283 { "rh", output_source_h },
3284 { "inl", output_source_h },
3285 { "rc", output_source_rc },
3286 { "mc", output_source_mc },
3287 { "res", output_source_res },
3288 { "idl", output_source_idl },
3289 { "sfd", output_source_sfd },
3290 { "svg", output_source_svg },
3291 { "nls", output_source_nls },
3292 { "desktop", output_source_desktop },
3293 { "po", output_source_po },
3294 { "in", output_source_in },
3295 { "x", output_source_x },
3296 { "spec", output_source_spec },
3297 { "xml", output_source_xml },
3298 { NULL, output_source_default }
3302 /*******************************************************************
3303 * output_fake_module
3305 static void output_fake_module( struct makefile *make, const char *spec_file )
3307 unsigned int arch = 0; /* fake modules are always native */
3308 const char *name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3310 if (make->disabled[arch]) return;
3312 strarray_add( &make->all_targets[arch], name );
3313 add_install_rule( make, make->module, arch, name, strmake( "d$(dlldir)/%s", name ));
3315 output( "%s:", obj_dir_path( make, name ));
3316 if (spec_file) output_filename( spec_file );
3317 output_filenames_obj_dir( make, make->res_files[arch] );
3318 output_filename( tools_path( make, "winebuild" ));
3319 output_filename( tools_path( make, "winegcc" ));
3320 output( "\n" );
3321 output_winegcc_command( make, arch );
3322 output_filename( "-Wb,--fake-module" );
3323 if (!make->is_exe) output_filename( "-shared" );
3324 if (spec_file) output_filename( spec_file );
3325 output_filenames( make->extradllflags );
3326 output_filenames_obj_dir( make, make->res_files[arch] );
3327 output( "\n" );
3331 /*******************************************************************
3332 * output_module
3334 static void output_module( struct makefile *make, unsigned int arch )
3336 struct strarray default_imports = empty_strarray;
3337 struct strarray all_libs = empty_strarray;
3338 struct strarray dep_libs = empty_strarray;
3339 struct strarray imports = make->imports;
3340 const char *module_name;
3341 const char *debug_file;
3342 char *spec_file = NULL;
3343 unsigned int i;
3345 if (make->disabled[arch]) return;
3347 if (!make->is_exe)
3349 if (make->data_only || strarray_exists( &make->extradllflags, "-Wl,--subsystem,native" ))
3351 /* spec file is optional */
3352 struct incl_file *spec = find_src_file( make, replace_extension( make->module, ".dll", ".spec" ));
3353 if (spec) spec_file = spec->filename;
3355 else spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3358 if (!make->data_only)
3360 module_name = arch_module_name( make->module, arch );
3362 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3364 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ));
3365 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, IMPORT_TYPE_DELAYED, arch ));
3366 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3367 if (!arch) strarray_addall( &all_libs, libs );
3369 if (delay_load_flags[arch])
3371 for (i = 0; i < make->delayimports.count; i++)
3373 struct makefile *import = get_static_lib( make->delayimports.str[i], arch );
3374 if (import) strarray_add( &all_libs, strmake( "%s%s", delay_load_flags[arch], import->module ));
3378 else module_name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3380 strarray_add( &make->all_targets[arch], module_name );
3381 if (make->data_only)
3382 add_install_rule( make, make->module, arch, module_name,
3383 strmake( "d$(dlldir)/%s%s", arch_pe_dirs[arch], make->module ));
3384 else
3385 add_install_rule( make, make->module, arch, module_name,
3386 strmake( "%c%s%s%s", '0' + arch, arch_install_dirs[arch], make->module,
3387 dll_ext[arch] ));
3389 output( "%s:", obj_dir_path( make, module_name ));
3390 if (spec_file) output_filename( spec_file );
3391 output_filenames_obj_dir( make, make->object_files[arch] );
3392 output_filenames_obj_dir( make, make->res_files[arch] );
3393 output_filenames( dep_libs );
3394 output_filename( tools_path( make, "winebuild" ));
3395 output_filename( tools_path( make, "winegcc" ));
3396 output( "\n" );
3397 output_winegcc_command( make, arch );
3398 if (arch) output_filename( "-Wl,--wine-builtin" );
3399 if (!make->is_exe) output_filename( "-shared" );
3400 if (spec_file) output_filename( spec_file );
3401 output_filenames( make->extradllflags );
3402 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3403 output_filenames_obj_dir( make, make->object_files[arch] );
3404 output_filenames_obj_dir( make, make->res_files[arch] );
3405 debug_file = get_debug_file( make, module_name, arch );
3406 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3407 output_filenames( all_libs );
3408 output_filename( arch_make_variable( "LDFLAGS", arch ));
3409 output( "\n" );
3411 if (!make->data_only && !arch && unix_lib_supported) output_fake_module( make, spec_file );
3415 /*******************************************************************
3416 * output_import_lib
3418 static void output_import_lib( struct makefile *make, unsigned int arch )
3420 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3421 const char *name = strmake( "%slib%s.a", arch_dirs[arch], make->importlib );
3423 strarray_add( &make->clean_files, name );
3424 if (needs_delay_lib( make, arch ))
3426 const char *delay_name = replace_extension( name, ".a", ".delay.a" );
3427 strarray_add( &make->clean_files, delay_name );
3428 output( "%s ", obj_dir_path( make, delay_name ));
3430 output( "%s: %s %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ), spec_file );
3431 output_filenames_obj_dir( make, make->implib_files[arch] );
3432 output( "\n" );
3433 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3434 if (!delay_load_flags[arch]) output_filename( "--without-dlltool" );
3435 output_filenames( target_flags[arch] );
3436 if (make->is_win16) output_filename( "-m16" );
3437 output_filename( "--export" );
3438 output_filename( spec_file );
3439 output_filenames_obj_dir( make, make->implib_files[arch] );
3440 output( "\n" );
3442 add_install_rule( make, make->importlib, arch, name,
3443 strmake( "d%slib%s.a", arch_install_dirs[arch], make->importlib ));
3447 /*******************************************************************
3448 * output_unix_lib
3450 static void output_unix_lib( struct makefile *make )
3452 struct strarray unix_deps = empty_strarray;
3453 struct strarray unix_libs = add_unix_libraries( make, &unix_deps );
3454 unsigned int arch = 0; /* unix libs are always native */
3456 if (make->disabled[arch]) return;
3458 strarray_add( &make->all_targets[arch], make->unixlib );
3459 add_install_rule( make, make->module, arch, make->unixlib,
3460 strmake( "p%s%s", arch_install_dirs[arch], make->unixlib ));
3461 output( "%s:", obj_dir_path( make, make->unixlib ));
3462 output_filenames_obj_dir( make, make->unixobj_files );
3463 output_filenames( unix_deps );
3464 output( "\n" );
3465 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3466 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3467 output_filenames_obj_dir( make, make->unixobj_files );
3468 output_filenames( unix_libs );
3469 output_filename( "$(LDFLAGS)" );
3470 output( "\n" );
3474 /*******************************************************************
3475 * output_static_lib
3477 static void output_static_lib( struct makefile *make, unsigned int arch )
3479 const char *name = strmake( "%s%s", arch_dirs[arch], make->staticlib );
3481 strarray_add( &make->clean_files, name );
3482 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3483 output_filenames_obj_dir( make, make->object_files[arch] );
3484 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3485 output( "\n" );
3486 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3487 output_filenames( target_flags[arch] );
3488 output_filenames_obj_dir( make, make->object_files[arch] );
3489 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3490 output( "\n" );
3491 if (!make->extlib)
3492 add_install_rule( make, make->staticlib, arch, name,
3493 strmake( "d%s%s", arch_install_dirs[arch], make->staticlib ));
3497 /*******************************************************************
3498 * output_test_module
3500 static void output_test_module( struct makefile *make, unsigned int arch )
3502 char *basemodule = replace_extension( make->testdll, ".dll", "" );
3503 char *stripped = arch_module_name( strmake( "%s_test-stripped.exe", basemodule ), arch );
3504 char *testmodule = arch_module_name( strmake( "%s_test.exe", basemodule ), arch );
3505 struct strarray default_imports = get_default_imports( make, make->imports );
3506 struct strarray dep_libs = empty_strarray;
3507 struct strarray all_libs = empty_strarray;
3508 struct makefile *parent = get_parent_makefile( make );
3509 const char *debug_file;
3511 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, IMPORT_TYPE_DIRECT, arch ) );
3512 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3514 strarray_add( &make->all_targets[arch], testmodule );
3515 strarray_add( &make->clean_files, stripped );
3516 output( "%s:\n", obj_dir_path( make, testmodule ));
3517 output_winegcc_command( make, arch );
3518 output_filenames( make->extradllflags );
3519 output_filenames_obj_dir( make, make->object_files[arch] );
3520 output_filenames_obj_dir( make, make->res_files[arch] );
3521 if ((debug_file = get_debug_file( make, testmodule, arch )))
3522 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3523 output_filenames( all_libs );
3524 output_filename( arch_make_variable( "LDFLAGS", arch ));
3525 output( "\n" );
3526 output( "%s:\n", obj_dir_path( make, stripped ));
3527 output_winegcc_command( make, arch );
3528 output_filename( "-s" );
3529 output_filename( strmake( "-Wb,-F,%s_test.exe", basemodule ));
3530 output_filenames( make->extradllflags );
3531 output_filenames_obj_dir( make, make->object_files[arch] );
3532 output_filenames_obj_dir( make, make->res_files[arch] );
3533 output_filenames( all_libs );
3534 output_filename( arch_make_variable( "LDFLAGS", arch ));
3535 output( "\n" );
3536 output( "%s %s:", obj_dir_path( make, testmodule ), obj_dir_path( make, stripped ));
3537 output_filenames_obj_dir( make, make->object_files[arch] );
3538 output_filenames_obj_dir( make, make->res_files[arch] );
3539 output_filenames( dep_libs );
3540 output_filename( tools_path( make, "winebuild" ));
3541 output_filename( tools_path( make, "winegcc" ));
3542 output( "\n" );
3544 output( "programs/winetest/%s%s_test.res: %s\n", arch_dirs[arch], basemodule,
3545 obj_dir_path( make, stripped ));
3546 output( "\t%secho \"%s_test.exe TESTRES \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3547 basemodule, obj_dir_path( make, stripped ), tools_path( make, "wrc" ));
3549 if (make->disabled[arch] || (parent && parent->disabled[arch]))
3551 make->ok_files[arch] = empty_strarray;
3552 return;
3554 output_filenames_obj_dir( make, make->ok_files[arch] );
3555 output( ": %s", obj_dir_path( make, testmodule ));
3556 if (parent)
3558 char *parent_module = arch_module_name( make->testdll, arch );
3559 output_filename( obj_dir_path( parent, parent_module ));
3560 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3562 output( "\n" );
3563 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3564 output_filenames_obj_dir( make, make->ok_files[arch] );
3565 output( "\n" );
3566 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3567 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3568 output( "%s::\n", obj_dir_path( make, "testclean" ));
3569 output( "\trm -f" );
3570 output_filenames_obj_dir( make, make->ok_files[arch] );
3571 output( "\n" );
3572 strarray_addall( &make->clean_files, make->ok_files[arch] );
3573 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3577 /*******************************************************************
3578 * output_programs
3580 static void output_programs( struct makefile *make )
3582 unsigned int i, j;
3583 unsigned int arch = 0; /* programs are always native */
3585 for (i = 0; i < make->programs.count; i++)
3587 char *program_installed = NULL;
3588 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3589 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3590 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3591 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3592 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3594 if (!objs.count) objs = make->object_files[arch];
3595 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3597 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3598 strarray_addall( &all_libs, libs );
3601 output( "%s:", obj_dir_path( make, program ) );
3602 output_filenames_obj_dir( make, objs );
3603 output_filenames( deps );
3604 output( "\n" );
3605 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3606 output_filenames_obj_dir( make, objs );
3607 output_filenames( all_libs );
3608 output_filename( "$(LDFLAGS)" );
3609 output( "\n" );
3610 strarray_add( &make->all_targets[arch], program );
3612 for (j = 0; j < symlinks.count; j++)
3614 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3615 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3617 strarray_addall( &make->all_targets[arch], symlinks );
3619 add_install_rule( make, program, arch, program_installed ? program_installed : program,
3620 strmake( "p$(bindir)/%s", program ));
3621 for (j = 0; j < symlinks.count; j++)
3622 add_install_rule( make, symlinks.str[j], arch, program,
3623 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3628 /*******************************************************************
3629 * output_subdirs
3631 static void output_subdirs( struct makefile *make )
3633 struct strarray all_targets = empty_strarray;
3634 struct strarray makefile_deps = empty_strarray;
3635 struct strarray clean_files = empty_strarray;
3636 struct strarray testclean_files = empty_strarray;
3637 struct strarray distclean_files = empty_strarray;
3638 struct strarray distclean_dirs = empty_strarray;
3639 struct strarray dependencies = empty_strarray;
3640 struct strarray install_deps[NB_INSTALL_RULES] = { empty_strarray };
3641 struct strarray tooldeps_deps = empty_strarray;
3642 struct strarray buildtest_deps = empty_strarray;
3643 unsigned int i, j, arch;
3645 strarray_addall( &clean_files, make->clean_files );
3646 strarray_addall( &distclean_files, make->distclean_files );
3647 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3648 for (i = 0; i < subdirs.count; i++)
3650 struct strarray subclean = empty_strarray;
3651 strarray_addall( &subclean, get_removable_dirs( submakes[i]->clean_files ));
3652 strarray_addall( &subclean, get_removable_dirs( submakes[i]->distclean_files ));
3653 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3654 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3655 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3656 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3657 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3658 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3659 strarray_addall_path( &distclean_dirs, submakes[i]->obj_dir, subclean );
3660 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3661 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3663 for (arch = 0; arch < archs.count; arch++)
3665 if (submakes[i]->disabled[arch]) continue;
3666 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets[arch] );
3667 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files[arch] );
3669 if (submakes[i]->disabled[0]) continue;
3671 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3672 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3673 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3674 if (submakes[i]->testdll)
3675 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3676 for (j = 0; j < NB_INSTALL_RULES; j++)
3677 if (submakes[i]->install_rules[j].count)
3678 strarray_add( &install_deps[j], obj_dir_path( submakes[i], install_targets[j] ));
3680 strarray_addall( &dependencies, makefile_deps );
3681 output( "all:" );
3682 output_filenames( all_targets );
3683 output( "\n" );
3684 output( "Makefile:" );
3685 output_filenames( makefile_deps );
3686 output( "\n" );
3687 output_filenames( dependencies );
3688 output( ":\n" );
3689 for (j = 0; j < NB_INSTALL_RULES; j++)
3691 if (!install_deps[j].count) continue;
3692 if (strcmp( install_targets[j], "install-test" ))
3694 output( "install " );
3695 strarray_add_uniq( &make->phony_targets, "install" );
3697 output( "%s::", install_targets[j] );
3698 output_filenames( install_deps[j] );
3699 output( "\n" );
3700 strarray_add_uniq( &make->phony_targets, install_targets[j] );
3702 output_uninstall_rules( make );
3703 if (buildtest_deps.count)
3705 output( "buildtests:" );
3706 output_filenames( buildtest_deps );
3707 output( "\n" );
3708 strarray_add_uniq( &make->phony_targets, "buildtests" );
3710 output( "check test:" );
3711 output_filenames( testclean_files );
3712 output( "\n" );
3713 strarray_add_uniq( &make->phony_targets, "check" );
3714 strarray_add_uniq( &make->phony_targets, "test" );
3716 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3718 output( "clean::\n");
3719 output_rm_filenames( clean_files, "rm -f" );
3720 output( "testclean::\n");
3721 output_rm_filenames( testclean_files, "rm -f" );
3722 output( "distclean::\n");
3723 output_rm_filenames( distclean_files, "rm -f" );
3724 output_rm_filenames( distclean_dirs, "-rmdir 2>/dev/null" );
3725 output( "maintainer-clean::\n");
3726 output_rm_filenames( make->maintainerclean_files, "rm -f" );
3727 strarray_add_uniq( &make->phony_targets, "distclean" );
3728 strarray_add_uniq( &make->phony_targets, "testclean" );
3729 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3731 if (tooldeps_deps.count)
3733 output( "__tooldeps__:" );
3734 output_filenames( tooldeps_deps );
3735 output( "\n" );
3736 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3739 if (make->phony_targets.count)
3741 output( ".PHONY:" );
3742 output_filenames( make->phony_targets );
3743 output( "\n" );
3748 /*******************************************************************
3749 * output_sources
3751 static void output_sources( struct makefile *make )
3753 struct strarray all_targets = empty_strarray;
3754 struct incl_file *source;
3755 unsigned int i, j, arch;
3757 strarray_add_uniq( &make->phony_targets, "all" );
3759 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3761 char *obj = xstrdup( source->name );
3762 char *ext = get_extension( obj );
3764 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3765 *ext++ = 0;
3767 for (j = 0; output_source_funcs[j].ext; j++)
3768 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3770 output_source_funcs[j].fn( make, source, obj );
3771 strarray_addall_uniq( &make->dependencies, source->dependencies );
3774 /* special case for winetest: add resource files from other test dirs */
3775 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3777 for (arch = 0; arch < archs.count; arch++)
3779 if (!is_multiarch( arch )) continue;
3780 for (i = 0; i < subdirs.count; i++)
3782 if (!submakes[i]->testdll) continue;
3783 if (submakes[i]->disabled[arch]) continue;
3784 if (enable_tests.count && !strarray_exists( &enable_tests, submakes[i]->testdll )) continue;
3785 strarray_add( &make->res_files[arch],
3786 strmake( "%s%s", arch_dirs[arch],
3787 replace_extension( submakes[i]->testdll, ".dll", "_test.res" )));
3792 if (make->dlldata_files.count)
3794 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3795 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3796 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3797 output_filenames( make->dlldata_files );
3798 output( "\n" );
3801 if (make->staticlib)
3803 for (arch = 0; arch < archs.count; arch++)
3804 if (is_multiarch( arch ) || (so_dll_supported && !make->extlib))
3805 output_static_lib( make, arch );
3807 else if (make->module)
3809 for (arch = 0; arch < archs.count; arch++)
3811 if (is_multiarch( arch )) output_module( make, arch );
3812 if (make->importlib && (is_multiarch( arch ) || !is_native_arch_disabled( make )))
3813 output_import_lib( make, arch );
3815 if (make->unixlib) output_unix_lib( make );
3816 if (make->is_exe && !make->is_win16 && unix_lib_supported && strendswith( make->module, ".exe" ))
3818 char *binary = replace_extension( make->module, ".exe", "" );
3819 add_install_rule( make, binary, 0, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3822 else if (make->testdll)
3824 for (arch = 0; arch < archs.count; arch++)
3825 if (is_multiarch( arch )) output_test_module( make, arch );
3827 else if (make->programs.count) output_programs( make );
3829 for (i = 0; i < make->scripts.count; i++)
3830 add_install_rule( make, make->scripts.str[i], 0, make->scripts.str[i],
3831 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3833 for (i = 0; i < make->extra_targets.count; i++)
3834 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3835 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3836 else
3837 strarray_add( &make->all_targets[0], make->extra_targets.str[i] );
3839 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3840 strarray_add( &make->distclean_files, "Makefile" );
3841 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3843 if (!make->obj_dir)
3844 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3845 else if (!strcmp( make->obj_dir, "po" ))
3846 strarray_add( &make->distclean_files, "LINGUAS" );
3848 for (arch = 0; arch < archs.count; arch++)
3850 strarray_addall_uniq( &make->clean_files, make->object_files[arch] );
3851 strarray_addall_uniq( &make->clean_files, make->implib_files[arch] );
3852 strarray_addall_uniq( &make->clean_files, make->res_files[arch] );
3853 strarray_addall_uniq( &make->clean_files, make->all_targets[arch] );
3855 strarray_addall( &make->clean_files, make->unixobj_files );
3856 strarray_addall( &make->clean_files, make->pot_files );
3857 strarray_addall( &make->clean_files, make->debug_files );
3859 if (make == top_makefile)
3861 output_subdirs( make );
3862 return;
3865 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3866 strarray_addall( &all_targets, make->font_files );
3867 if (all_targets.count)
3869 output( "%s:", obj_dir_path( make, "all" ));
3870 output_filenames_obj_dir( make, all_targets );
3871 output( "\n" );
3872 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3874 for (i = 0; i < NB_INSTALL_RULES; i++) output_install_rules( make, i );
3876 if (make->clean_files.count)
3878 output( "%s::\n", obj_dir_path( make, "clean" ));
3879 output( "\trm -f" );
3880 output_filenames_obj_dir( make, make->clean_files );
3881 output( "\n" );
3882 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3887 /*******************************************************************
3888 * create_temp_file
3890 static FILE *create_temp_file( const char *orig )
3892 char *name = xmalloc( strlen(orig) + 13 );
3893 unsigned int i, id = getpid();
3894 int fd;
3895 FILE *ret = NULL;
3897 for (i = 0; i < 100; i++)
3899 snprintf( name, strlen(orig) + 13, "%s.tmp%08x", orig, id );
3900 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3902 ret = fdopen( fd, "w" );
3903 break;
3905 if (errno != EEXIST) break;
3906 id += 7777;
3908 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3909 temp_file_name = name;
3910 return ret;
3914 /*******************************************************************
3915 * rename_temp_file
3917 static void rename_temp_file( const char *dest )
3919 int ret = rename( temp_file_name, dest );
3920 if (ret == -1 && errno == EEXIST)
3922 /* rename doesn't overwrite on windows */
3923 unlink( dest );
3924 ret = rename( temp_file_name, dest );
3926 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3927 temp_file_name = NULL;
3931 /*******************************************************************
3932 * are_files_identical
3934 static int are_files_identical( FILE *file1, FILE *file2 )
3936 for (;;)
3938 char buffer1[8192], buffer2[8192];
3939 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3940 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3941 if (size1 != size2) return 0;
3942 if (!size1) return feof( file1 ) && feof( file2 );
3943 if (memcmp( buffer1, buffer2, size1 )) return 0;
3948 /*******************************************************************
3949 * rename_temp_file_if_changed
3951 static void rename_temp_file_if_changed( const char *dest )
3953 FILE *file1, *file2;
3954 int do_rename = 1;
3956 if ((file1 = fopen( dest, "r" )))
3958 if ((file2 = fopen( temp_file_name, "r" )))
3960 do_rename = !are_files_identical( file1, file2 );
3961 fclose( file2 );
3963 fclose( file1 );
3965 if (!do_rename)
3967 unlink( temp_file_name );
3968 temp_file_name = NULL;
3970 else rename_temp_file( dest );
3974 /*******************************************************************
3975 * output_linguas
3977 static void output_linguas( const struct makefile *make )
3979 const char *dest = obj_dir_path( make, "LINGUAS" );
3980 struct incl_file *source;
3982 output_file = create_temp_file( dest );
3984 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3985 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3986 if (strendswith( source->name, ".po" ))
3987 output( "%s\n", replace_extension( source->name, ".po", "" ));
3989 if (fclose( output_file )) fatal_perror( "write" );
3990 output_file = NULL;
3991 rename_temp_file_if_changed( dest );
3995 /*******************************************************************
3996 * output_testlist
3998 static void output_testlist( const struct makefile *make )
4000 const char *dest = obj_dir_path( make, "testlist.c" );
4001 unsigned int i;
4003 output_file = create_temp_file( dest );
4005 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
4006 output( "#define WIN32_LEAN_AND_MEAN\n" );
4007 output( "#include <windows.h>\n\n" );
4008 output( "#define STANDALONE\n" );
4009 output( "#include \"wine/test.h\"\n\n" );
4011 for (i = 0; i < make->test_files.count; i++)
4012 output( "extern void func_%s(void);\n", make->test_files.str[i] );
4013 output( "\n" );
4014 output( "const struct test winetest_testlist[] =\n" );
4015 output( "{\n" );
4016 for (i = 0; i < make->test_files.count; i++)
4017 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
4018 output( " { 0, 0 }\n" );
4019 output( "};\n" );
4021 if (fclose( output_file )) fatal_perror( "write" );
4022 output_file = NULL;
4023 rename_temp_file_if_changed( dest );
4027 /*******************************************************************
4028 * output_gitignore
4030 static void output_gitignore( const char *dest, struct strarray files )
4032 unsigned int i;
4034 output_file = create_temp_file( dest );
4036 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4037 for (i = 0; i < files.count; i++)
4039 if (!strchr( files.str[i], '/' )) output( "/" );
4040 output( "%s\n", files.str[i] );
4043 if (fclose( output_file )) fatal_perror( "write" );
4044 output_file = NULL;
4045 rename_temp_file( dest );
4049 /*******************************************************************
4050 * output_stub_makefile
4052 static void output_stub_makefile( struct makefile *make )
4054 struct strarray targets = empty_strarray;
4055 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
4056 unsigned int i, arch;
4058 for (arch = 0; arch < archs.count; arch++)
4059 if (make->all_targets[arch].count) strarray_add_uniq( &targets, "all" );
4061 for (i = 0; i < NB_INSTALL_RULES; i++)
4063 if (!make->install_rules[i].count) continue;
4064 strarray_add_uniq( &targets, "install" );
4065 strarray_add( &targets, install_targets[i] );
4067 if (make->clean_files.count) strarray_add( &targets, "clean" );
4068 if (make->test_files.count)
4070 strarray_add( &targets, "check" );
4071 strarray_add( &targets, "test" );
4072 strarray_add( &targets, "testclean" );
4075 if (!targets.count && !make->clean_files.count) return;
4077 output_file_name = obj_dir_path( make, "Makefile" );
4078 output_file = create_temp_file( output_file_name );
4080 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
4082 if (make_var) output( "MAKE = %s\n\n", make_var );
4084 output( "all:\n" );
4085 output_filenames( targets );
4086 output_filenames( make->clean_files );
4087 output( ":\n" );
4088 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4089 output( ".PHONY:" );
4090 output_filenames( targets );
4091 output( "\n" );
4093 fclose( output_file );
4094 output_file = NULL;
4095 rename_temp_file( output_file_name );
4099 /*******************************************************************
4100 * output_silent_rules
4102 static void output_silent_rules(void)
4104 static const char *cmds[] =
4106 "BISON",
4107 "BUILD",
4108 "CC",
4109 "CCLD",
4110 "FLEX",
4111 "GEN",
4112 "LN",
4113 "MSG",
4114 "SED",
4115 "TEST",
4116 "WIDL",
4117 "WMC",
4118 "WRC"
4120 unsigned int i;
4122 output( "V = 0\n" );
4123 for (i = 0; i < ARRAY_SIZE(cmds); i++)
4125 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4126 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4127 output( "quiet_%s_1 =\n", cmds[i] );
4132 /*******************************************************************
4133 * output_top_makefile
4135 static void output_top_makefile( struct makefile *make )
4137 char buffer[1024];
4138 FILE *src_file;
4139 unsigned int i;
4140 int found = 0;
4142 output_file_name = obj_dir_path( make, output_makefile_name );
4143 output_file = create_temp_file( output_file_name );
4145 /* copy the contents of the source makefile */
4146 src_file = open_input_makefile( make );
4147 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4149 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4150 found = !strncmp( buffer, separator, strlen(separator) );
4152 if (fclose( src_file )) fatal_perror( "close" );
4153 input_file_name = NULL;
4155 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4157 if (silent_rules) output_silent_rules();
4158 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4159 output_sources( make );
4160 /* disable implicit rules */
4161 output( ".SUFFIXES:\n" );
4163 fclose( output_file );
4164 output_file = NULL;
4165 rename_temp_file( output_file_name );
4169 /*******************************************************************
4170 * output_dependencies
4172 static void output_dependencies( struct makefile *make )
4174 struct strarray ignore_files = empty_strarray;
4176 if (make->obj_dir) create_dir( make->obj_dir );
4178 if (make == top_makefile) output_top_makefile( make );
4179 else output_stub_makefile( make );
4181 strarray_addall( &ignore_files, make->distclean_files );
4182 strarray_addall( &ignore_files, make->clean_files );
4183 if (make->testdll) output_testlist( make );
4184 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4185 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4187 create_file_directories( make, ignore_files );
4189 output_file_name = NULL;
4193 /*******************************************************************
4194 * load_sources
4196 static void load_sources( struct makefile *make )
4198 unsigned int i, arch;
4199 struct strarray value;
4200 struct incl_file *file;
4202 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4203 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4205 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4206 make->module = get_expanded_make_variable( make, "MODULE" );
4207 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4208 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4209 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4210 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4211 if (unix_lib_supported) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4213 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4214 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4215 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4216 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4217 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4218 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4219 for (i = 0; i < NB_INSTALL_RULES; i++)
4220 make->install[i] = get_expanded_make_var_array( make, install_variables[i] );
4222 if (make->extlib) make->staticlib = make->extlib;
4223 if (make->staticlib) make->module = make->staticlib;
4225 if (make->obj_dir)
4227 make->disabled[0] = strarray_exists( &disabled_dirs[0], make->obj_dir );
4228 for (arch = 1; arch < archs.count; arch++)
4229 make->disabled[arch] = make->disabled[0] || strarray_exists( &disabled_dirs[arch], make->obj_dir );
4231 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4232 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4233 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4234 strarray_exists( &make->extradllflags, "-mwindows" );
4236 if (make->module)
4238 /* add default install rules if nothing was specified */
4239 for (i = 0; i < NB_INSTALL_RULES; i++) if (make->install[i].count) break;
4240 if (i == NB_INSTALL_RULES)
4242 if (make->importlib) strarray_add( &make->install[INSTALL_DEV], make->importlib );
4243 if (make->staticlib) strarray_add( &make->install[INSTALL_DEV], make->staticlib );
4244 else strarray_add( &make->install[INSTALL_LIB], make->module );
4248 make->include_paths = empty_strarray;
4249 make->include_args = empty_strarray;
4250 make->define_args = empty_strarray;
4251 make->unix_cflags = empty_strarray;
4252 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4253 strarray_add( &make->unix_cflags, "-DWINE_UNIX_LIB" );
4255 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4256 for (i = 0; i < value.count; i++)
4258 if (!strncmp( value.str[i], "-I", 2 ))
4260 const char *dir = value.str[i] + 2;
4261 if (!strncmp( dir, "./", 2 ))
4263 dir += 2;
4264 while (*dir == '/') dir++;
4266 strarray_add_uniq( &make->include_paths, dir );
4268 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4269 strarray_add_uniq( &make->define_args, value.str[i] );
4271 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4272 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4274 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4275 if (make->src_dir)
4276 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4277 if (make->parent_dir)
4278 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4279 strarray_add( &make->include_args, "-Iinclude" );
4280 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4282 list_init( &make->sources );
4283 list_init( &make->includes );
4285 value = get_expanded_make_var_array( make, "SOURCES" );
4286 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4288 add_generated_sources( make );
4290 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4291 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4293 for (i = 0; i < make->delayimports.count; i++)
4294 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4298 /*******************************************************************
4299 * parse_makeflags
4301 static void parse_makeflags( const char *flags )
4303 const char *p = flags;
4304 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4306 while (*p)
4308 p = skip_spaces( p );
4309 var = buffer;
4310 while (*p && !isspace(*p))
4312 if (*p == '\\' && p[1]) p++;
4313 *var++ = *p++;
4315 *var = 0;
4316 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4321 /*******************************************************************
4322 * parse_option
4324 static int parse_option( const char *opt )
4326 if (opt[0] != '-')
4328 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4329 return 0;
4331 switch(opt[1])
4333 case 'f':
4334 if (opt[2]) output_makefile_name = opt + 2;
4335 break;
4336 case 'R':
4337 relative_dir_mode = 1;
4338 break;
4339 case 'S':
4340 silent_rules = 1;
4341 break;
4342 default:
4343 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4344 exit(1);
4346 return 1;
4350 /*******************************************************************
4351 * main
4353 int main( int argc, char *argv[] )
4355 const char *makeflags = getenv( "MAKEFLAGS" );
4356 const char *target;
4357 unsigned int i, j, arch;
4359 if (makeflags) parse_makeflags( makeflags );
4361 i = 1;
4362 while (i < argc)
4364 if (parse_option( argv[i] ))
4366 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4367 argc--;
4369 else i++;
4372 if (relative_dir_mode)
4374 char *relpath;
4376 if (argc != 3)
4378 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4379 exit( 1 );
4381 relpath = get_relative_path( argv[1], argv[2] );
4382 printf( "%s\n", relpath ? relpath : "." );
4383 exit( 0 );
4386 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4388 atexit( cleanup_files );
4389 init_signals( exit_on_signal );
4391 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4392 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4394 top_makefile = parse_makefile( NULL );
4396 target_flags[0] = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4397 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4398 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4399 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4400 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4401 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4402 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4403 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4404 for (i = 0; i < NB_INSTALL_RULES; i++)
4405 top_install[i] = get_expanded_make_var_array( top_makefile, strmake( "TOP_%s", install_variables[i] ));
4407 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4408 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4409 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4410 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4411 dll_ext[0] = get_expanded_make_variable( top_makefile, "DLLEXT" );
4412 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4413 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4414 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4415 bison = get_expanded_make_variable( top_makefile, "BISON" );
4416 ar = get_expanded_make_variable( top_makefile, "AR" );
4417 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4418 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4419 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4420 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4421 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4422 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4423 wayland_scanner = get_expanded_make_variable( top_makefile, "WAYLAND_SCANNER" );
4425 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4426 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4427 if (!exe_ext) exe_ext = "";
4428 if (!dll_ext[0]) dll_ext[0] = "";
4429 if (!tools_ext) tools_ext = "";
4431 unix_lib_supported = !!strcmp( exe_ext, ".exe" );
4432 so_dll_supported = !!dll_ext[0][0]; /* non-empty dll ext means supported */
4434 strarray_add( &archs, get_expanded_make_variable( top_makefile, "HOST_ARCH" ));
4435 strarray_addall( &archs, get_expanded_make_var_array( top_makefile, "PE_ARCHS" ));
4437 arch_dirs[0] = "";
4438 arch_pe_dirs[0] = strmake( "%s-windows/", archs.str[0] );
4439 arch_install_dirs[0] = unix_lib_supported ? strmake( "$(dlldir)/%s-unix/", archs.str[0] ) : "$(dlldir)/";
4440 strip_progs[0] = "\"$(STRIP)\"";
4442 for (arch = 1; arch < archs.count; arch++)
4444 target = get_expanded_arch_var( top_makefile, "TARGET", arch );
4445 strarray_add( &target_flags[arch], "-b" );
4446 strarray_add( &target_flags[arch], target );
4447 arch_dirs[arch] = strmake( "%s-windows/", archs.str[arch] );
4448 arch_pe_dirs[arch] = arch_dirs[arch];
4449 arch_install_dirs[arch] = strmake( "$(dlldir)/%s", arch_dirs[arch] );
4450 strip_progs[arch] = strmake( "%s-strip", target );
4451 dll_ext[arch] = "";
4454 for (arch = 0; arch < archs.count; arch++)
4456 extra_cflags[arch] = get_expanded_arch_var_array( top_makefile, "EXTRACFLAGS", arch );
4457 extra_cflags_extlib[arch] = remove_warning_flags( extra_cflags[arch] );
4458 disabled_dirs[arch] = get_expanded_arch_var_array( top_makefile, "DISABLED_SUBDIRS", arch );
4459 if (!is_multiarch( arch )) continue;
4460 delay_load_flags[arch] = get_expanded_arch_var( top_makefile, "DELAYLOADFLAG", arch );
4461 debug_flags[arch] = get_expanded_arch_var( top_makefile, "DEBUG", arch );
4464 if (unix_lib_supported)
4466 delay_load_flags[0] = "-Wl,-delayload,";
4467 debug_flags[0] = NULL;
4470 top_makefile->src_dir = root_src_dir;
4471 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4472 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4474 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4476 load_sources( top_makefile );
4477 load_sources( include_makefile );
4478 for (i = 0; i < subdirs.count; i++)
4479 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4481 output_dependencies( top_makefile );
4482 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4484 return 0;