winex11.drv: Send missed KEYUP events on KeymapNotify.
[wine.git] / tools / makedep.c
blob8495d93d8c60d0c460847fc7283890eda888e441
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 5
131 /* variables common to all makefiles */
132 static struct strarray archs;
133 static struct strarray linguas;
134 static struct strarray dll_flags;
135 static struct strarray unix_dllflags;
136 static struct strarray msvcrt_flags;
137 static struct strarray cpp_flags;
138 static struct strarray lddll_flags;
139 static struct strarray libs;
140 static struct strarray enable_tests;
141 static struct strarray cmdline_vars;
142 static struct strarray subdirs;
143 static struct strarray delay_import_libs;
144 static struct strarray top_install[NB_INSTALL_RULES];
145 static const char *root_src_dir;
146 static const char *tools_dir;
147 static const char *tools_ext;
148 static const char *exe_ext;
149 static const char *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 /* add source file dependency for parallel makes */
2684 char *header = strmake( "%s.tab.h", obj );
2686 if (find_include_file( make, header ))
2688 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2689 output( "\t%s%s -o %s.tab.c -d %s\n",
2690 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2691 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2692 source->filename, obj_dir_path( make, header ));
2693 strarray_add( &make->clean_files, header );
2695 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2697 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2701 /*******************************************************************
2702 * output_source_l
2704 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2706 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2707 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2711 /*******************************************************************
2712 * output_source_h
2714 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2716 if (source->file->flags & FLAG_GENERATED)
2717 strarray_add( &make->all_targets[0], source->name );
2718 else if ((source->file->flags & FLAG_INSTALL) || strncmp( source->name, "wine/", 5 ))
2719 add_install_rule( make, source->name, 0, source->name,
2720 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2724 /*******************************************************************
2725 * output_source_rc
2727 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2729 struct strarray defines = get_source_defines( make, source, obj );
2730 const char *po_dir = NULL, *res_file = strmake( "%s.res", obj );
2731 unsigned int i, arch;
2733 if (source->file->flags & FLAG_RC_HEADER) return;
2734 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2735 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2736 if (!(source->file->flags & FLAG_TESTDLL))
2738 for (arch = 0; arch < archs.count; arch++)
2739 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2741 else strarray_add( &make->clean_files, res_file );
2743 if (source->file->flags & FLAG_RC_PO)
2745 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2746 output( "%s.pot ", obj_dir_path( make, obj ) );
2748 output( "%s: %s", obj_dir_path( make, res_file ), source->filename );
2749 output_filename( tools_path( make, "wrc" ));
2750 if (make->src_dir) output_filename( "nls/locale.nls" );
2751 output_filenames( source->dependencies );
2752 output( "\n" );
2753 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2754 if (make->is_win16) output_filename( "-m16" );
2755 output_filename( "--nostdinc" );
2756 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2757 output_filenames( defines );
2758 output_filename( source->filename );
2759 output( "\n" );
2760 if (po_dir)
2762 output( "%s:", obj_dir_path( make, res_file ));
2763 for (i = 0; i < linguas.count; i++)
2764 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2765 output( "\n" );
2770 /*******************************************************************
2771 * output_source_mc
2773 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2775 unsigned int i, arch;
2776 char *obj_path = obj_dir_path( make, obj );
2777 char *res_file = strmake( "%s.res", obj );
2779 for (arch = 0; arch < archs.count; arch++)
2780 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2781 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2782 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2783 output_filename( tools_path( make, "wmc" ));
2784 output_filenames( source->dependencies );
2785 if (make->src_dir) output_filename( "nls/locale.nls" );
2786 output( "\n" );
2787 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2788 if (linguas.count)
2790 output_filename( "--po-dir=po" );
2791 output( "\n" );
2792 output( "%s.res:", obj_dir_path( make, obj ));
2793 for (i = 0; i < linguas.count; i++)
2794 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2796 output( "\n" );
2800 /*******************************************************************
2801 * output_source_res
2803 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2805 if (make->disabled[source->arch]) return;
2806 strarray_add( &make->res_files[source->arch], source->name );
2810 /*******************************************************************
2811 * output_source_idl
2813 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2815 struct strarray defines = get_source_defines( make, source, obj );
2816 struct strarray headers = empty_strarray;
2817 struct strarray deps = empty_strarray;
2818 struct strarray multiarch_targets[MAX_ARCHS] = { empty_strarray };
2819 const char *dest;
2820 unsigned int i, arch;
2822 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2823 if (!source->file->flags) return;
2825 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2826 if (source->file->flags & FLAG_INSTALL)
2828 add_install_rule( make, source->name, 0, xstrdup( source->name ),
2829 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2830 if (source->file->flags & FLAG_IDL_HEADER)
2831 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2832 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2834 if (source->file->flags & FLAG_IDL_HEADER)
2836 dest = strmake( "%s.h", obj );
2837 strarray_add( &headers, dest );
2838 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2841 for (i = 0; i < ARRAY_SIZE(idl_outputs); i++)
2843 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2844 for (arch = 0; arch < archs.count; arch++)
2846 if (!is_multiarch( arch )) continue;
2847 if (make->disabled[arch]) continue;
2848 dest = strmake( "%s%s%s", arch_dirs[arch], obj, idl_outputs[i].ext );
2849 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2850 strarray_add( &multiarch_targets[arch], dest );
2854 for (arch = 0; arch < archs.count; arch++)
2856 struct strarray arch_deps = empty_strarray;
2858 if (!arch) strarray_addall( &arch_deps, headers );
2859 strarray_addall( &arch_deps, multiarch_targets[arch] );
2860 if (!arch_deps.count) continue;
2861 output_filenames_obj_dir( make, arch_deps );
2862 output( ":\n" );
2863 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2864 output_filenames( target_flags[arch] );
2865 output_filename( "--nostdinc" );
2866 output_filename( "-Ldlls/\\*" );
2867 output_filenames( defines );
2868 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2869 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2870 output_filename( source->filename );
2871 output( "\n" );
2872 strarray_addall( &deps, arch_deps );
2875 if (deps.count)
2877 output_filenames_obj_dir( make, deps );
2878 output( ":" );
2879 output_filename( tools_path( make, "widl" ));
2880 output_filename( source->filename );
2881 output_filenames( source->dependencies );
2882 output( "\n" );
2885 if (source->importlibdeps.count)
2887 for (arch = 0; arch < archs.count; arch++)
2889 if (!multiarch_targets[arch].count) continue;
2890 output_filenames_obj_dir( make, multiarch_targets[arch] );
2891 output( ":" );
2892 for (i = 0; i < source->importlibdeps.count; i++)
2894 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2895 const char *module = strmake( "%s%s", arch_pe_dirs[arch], submake->module );
2896 output_filename( obj_dir_path( submake, module ));
2898 output( "\n" );
2904 /*******************************************************************
2905 * output_source_x
2907 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2909 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2910 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2911 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2912 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2913 if (source->file->flags & FLAG_INSTALL)
2915 add_install_rule( make, source->name, 0, source->name,
2916 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2917 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2918 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2923 /*******************************************************************
2924 * output_source_sfd
2926 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2928 unsigned int i;
2929 char *ttf_obj = strmake( "%s.ttf", obj );
2930 char *ttf_file = src_dir_path( make, ttf_obj );
2932 if (fontforge && !make->src_dir)
2934 output( "%s: %s\n", ttf_file, source->filename );
2935 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2936 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2937 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2938 strarray_add( &make->maintainerclean_files, ttf_obj );
2940 if (source->file->flags & FLAG_INSTALL)
2942 add_install_rule( make, source->name, 0, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2943 output_srcdir_symlink( make, ttf_obj );
2946 if (source->file->flags & FLAG_SFD_FONTS)
2948 struct strarray *array = source->file->args;
2950 for (i = 0; i < array->count; i++)
2952 char *font = strtok( xstrdup(array->str[i]), " \t" );
2953 char *args = strtok( NULL, "" );
2955 strarray_add( &make->all_targets[0], xstrdup( font ));
2956 output( "%s: %s %s\n", obj_dir_path( make, font ),
2957 tools_path( make, "sfnt2fon" ), ttf_file );
2958 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2959 tools_path( make, "sfnt2fon" ), ttf_file, args );
2960 add_install_rule( make, source->name, 0, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2966 /*******************************************************************
2967 * output_source_svg
2969 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2971 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2972 unsigned int i;
2974 if (convert && rsvg && icotool)
2976 for (i = 0; images[i]; i++)
2977 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2979 if (images[i])
2981 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2982 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2983 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2984 root_src_dir_path( "tools/buildimage" ), source->filename );
2985 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2991 /*******************************************************************
2992 * output_source_nls
2994 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2996 add_install_rule( make, source->name, 0, source->name,
2997 strmake( "D$(nlsdir)/%s", source->name ));
2998 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
3002 /*******************************************************************
3003 * output_source_desktop
3005 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
3007 add_install_rule( make, source->name, 0, source->name,
3008 strmake( "D$(datadir)/applications/%s", source->name ));
3012 /*******************************************************************
3013 * output_source_po
3015 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
3017 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
3018 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
3019 strarray_add( &make->all_targets[0], strmake( "%s.mo", obj ));
3023 /*******************************************************************
3024 * output_source_in
3026 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
3028 unsigned int i;
3030 if (make == include_makefile) return; /* ignore generated includes */
3031 if (strendswith( obj, ".man" ) && source->file->args)
3033 struct strarray symlinks;
3034 char *dir, *dest = replace_extension( obj, ".man", "" );
3035 char *lang = strchr( dest, '.' );
3036 char *section = source->file->args;
3037 if (lang)
3039 *lang++ = 0;
3040 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3042 else dir = strmake( "$(mandir)/man%s", section );
3043 add_install_rule( make, dest, 0, obj, strmake( "d%s/%s.%s", dir, dest, section ));
3044 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3045 for (i = 0; i < symlinks.count; i++)
3046 add_install_rule( make, symlinks.str[i], 0, strmake( "%s.%s", dest, section ),
3047 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3048 free( dest );
3049 free( dir );
3051 strarray_add( &make->in_files, obj );
3052 strarray_add( &make->all_targets[0], obj );
3053 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3054 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
3055 output( "%s:", obj_dir_path( make, obj ));
3056 output_filenames( source->dependencies );
3057 output( "\n" );
3058 add_install_rule( make, obj, 0, obj, strmake( "d$(datadir)/wine/%s", obj ));
3062 /*******************************************************************
3063 * output_source_spec
3065 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3067 /* nothing to do */
3071 /*******************************************************************
3072 * output_source_testdll
3074 static void output_source_testdll( struct makefile *make, struct incl_file *source, const char *obj )
3076 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3077 struct strarray dll_flags = empty_strarray;
3078 struct strarray default_imports = empty_strarray;
3079 struct strarray all_libs, dep_libs;
3080 const char *dll_name, *obj_name, *res_name, *output_rsrc, *output_file, *debug_file, *ext = ".dll";
3081 struct incl_file *spec_file = find_src_file( make, strmake( "%s.spec", obj ));
3082 unsigned int arch;
3084 if (!imports.count) imports = make->imports;
3085 strarray_addall( &dll_flags, make->extradllflags );
3086 strarray_addall( &dll_flags, get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" ));
3087 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3088 if (strarray_exists( &dll_flags, "-mconsole" )) ext = ".exe";
3090 for (arch = 0; arch < archs.count; arch++)
3092 if (!is_multiarch( arch )) continue;
3093 all_libs = dep_libs = empty_strarray;
3094 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ) );
3095 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3096 if (!arch) strarray_addall( &all_libs, libs );
3097 dll_name = arch_module_name( strmake( "%s%s", obj, ext ), arch );
3098 obj_name = obj_dir_path( make, strmake( "%s%s.o", arch_dirs[arch], obj ));
3099 output_file = obj_dir_path( make, dll_name );
3100 output_rsrc = strmake( "%s.res", dll_name );
3102 if (!find_src_file( make, strmake( "%s.rc", obj ) )) res_name = NULL;
3103 else res_name = obj_dir_path( make, strmake( "%s.res", obj ) );
3105 strarray_add( &make->clean_files, dll_name );
3106 strarray_add( &make->res_files[arch], output_rsrc );
3107 output( "%s:", obj_dir_path( make, output_rsrc ));
3108 output_filename( output_file );
3109 output_filename( tools_path( make, "wrc" ));
3110 output( "\n" );
3111 output( "\t%secho \"%s%s TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, ext, output_file,
3112 tools_path( make, "wrc" ));
3114 output( "%s:", output_file );
3115 if (spec_file) output_filename( spec_file->filename );
3116 output_filename( obj_name );
3117 if (res_name) output_filename( res_name );
3118 output_filenames( dep_libs );
3119 output_filename( tools_path( make, "winebuild" ));
3120 output_filename( tools_path( make, "winegcc" ));
3121 output( "\n" );
3122 output_winegcc_command( make, arch );
3123 output_filename( "-s" );
3124 output_filenames( dll_flags );
3125 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3126 if (!strcmp( ext, ".dll" )) output_filename( "-shared" );
3127 if (spec_file) output_filename( spec_file->filename );
3128 output_filename( obj_name );
3129 if (res_name) output_filename( res_name );
3130 if ((debug_file = get_debug_file( make, dll_name, arch )))
3131 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3132 output_filenames( all_libs );
3133 output_filename( arch_make_variable( "LDFLAGS", arch ));
3134 output( "\n" );
3139 /*******************************************************************
3140 * output_source_xml
3142 static void output_source_xml( struct makefile *make, struct incl_file *source, const char *obj )
3144 if (wayland_scanner)
3146 output( "%s-protocol.c: %s\n", obj_dir_path( make, obj ), source->filename );
3147 output( "\t%s%s private-code %s $@\n", cmd_prefix( "GEN" ), wayland_scanner, source->filename );
3148 output( "%s-client-protocol.h: %s\n", obj_dir_path( make, obj ), source->filename );
3149 output( "\t%s%s client-header %s $@\n", cmd_prefix( "GEN" ), wayland_scanner, source->filename );
3154 /*******************************************************************
3155 * output_source_one_arch
3157 static void output_source_one_arch( struct makefile *make, struct incl_file *source, const char *obj,
3158 struct strarray defines, struct strarray *targets,
3159 unsigned int arch )
3161 const char *obj_name;
3163 if (make->disabled[arch] && !(source->file->flags & FLAG_C_IMPLIB)) return;
3165 if (arch)
3167 if (source->file->flags & FLAG_C_UNIX) return;
3168 if (!is_using_msvcrt( make ) && !make->staticlib && !(source->file->flags & FLAG_C_IMPLIB)) return;
3170 else if (source->file->flags & FLAG_C_UNIX)
3172 if (!unix_lib_supported) return;
3174 else if (archs.count > 1 && is_using_msvcrt( make ))
3176 if (!so_dll_supported) return;
3177 if (!(source->file->flags & FLAG_C_IMPLIB) && (!make->staticlib || make->extlib)) return;
3180 obj_name = strmake( "%s%s.o", source->arch ? "" : arch_dirs[arch], obj );
3181 strarray_add( targets, obj_name );
3183 if (source->file->flags & FLAG_C_UNIX)
3184 strarray_add( &make->unixobj_files, obj_name );
3185 else if (source->file->flags & FLAG_C_IMPLIB)
3186 strarray_add( &make->implib_files[arch], obj_name );
3187 else if (!(source->file->flags & FLAG_TESTDLL))
3188 strarray_add( &make->object_files[arch], obj_name );
3189 else
3190 strarray_add( &make->clean_files, obj_name );
3192 output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename );
3193 output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), arch_make_variable( "CC", arch ), source->filename );
3194 output_filenames( defines );
3195 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3196 output_filenames( make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] );
3197 if (!arch)
3199 if (source->file->flags & FLAG_C_UNIX)
3201 output_filenames( unix_dllflags );
3203 else if (make->module || make->testdll)
3205 output_filenames( dll_flags );
3206 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3207 if (!unix_lib_supported && make->module && is_crt_module( make->module ))
3208 output_filename( "-fno-builtin" );
3211 else
3213 if (make->module && is_crt_module( make->module )) output_filename( "-fno-builtin" );
3216 output_filenames( cpp_flags );
3217 output_filename( arch_make_variable( "CFLAGS", arch ));
3218 output( "\n" );
3220 if (make->testdll && strendswith( source->name, ".c" ) &&
3221 !(source->file->flags & (FLAG_GENERATED | FLAG_TESTDLL)))
3223 const char *ok_file, *test_exe;
3225 ok_file = strmake( "%s%s.ok", arch_dirs[arch], obj );
3226 test_exe = replace_extension( make->testdll, ".dll", "_test.exe" );
3227 strarray_add( &make->ok_files[arch], ok_file );
3228 output( "%s:\n", obj_dir_path( make, ok_file ));
3229 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s %s && touch $@\n",
3230 cmd_prefix( "TEST" ),
3231 root_src_dir_path( "tools/runtest" ), make->testdll,
3232 obj_dir_path( make, arch_module_name( test_exe, arch )), obj );
3237 /*******************************************************************
3238 * output_source_default
3240 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3242 struct strarray defines = get_source_defines( make, source, obj );
3243 struct strarray targets = empty_strarray;
3244 unsigned int arch;
3246 for (arch = 0; arch < archs.count; arch++)
3247 if (!source->arch || source->arch == arch)
3248 output_source_one_arch( make, source, obj, defines, &targets, arch );
3250 if (source->file->flags & FLAG_GENERATED)
3252 if (!make->testdll || !strendswith( source->filename, "testlist.c" ))
3253 strarray_add( &make->clean_files, source->basename );
3255 else if (source->file->flags & FLAG_TESTDLL)
3257 output_source_testdll( make, source, obj );
3259 else
3261 if (make->testdll && strendswith( source->name, ".c" ))
3262 strarray_add( &make->test_files, obj );
3265 if (targets.count && source->dependencies.count)
3267 output_filenames_obj_dir( make, targets );
3268 output( ":" );
3269 output_filenames( source->dependencies );
3270 output( "\n" );
3275 /* dispatch table to output rules for a single source file */
3276 static const struct
3278 const char *ext;
3279 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3280 } output_source_funcs[] =
3282 { "y", output_source_y },
3283 { "l", output_source_l },
3284 { "h", output_source_h },
3285 { "rh", output_source_h },
3286 { "inl", output_source_h },
3287 { "rc", output_source_rc },
3288 { "mc", output_source_mc },
3289 { "res", output_source_res },
3290 { "idl", output_source_idl },
3291 { "sfd", output_source_sfd },
3292 { "svg", output_source_svg },
3293 { "nls", output_source_nls },
3294 { "desktop", output_source_desktop },
3295 { "po", output_source_po },
3296 { "in", output_source_in },
3297 { "x", output_source_x },
3298 { "spec", output_source_spec },
3299 { "xml", output_source_xml },
3300 { NULL, output_source_default }
3304 /*******************************************************************
3305 * output_fake_module
3307 static void output_fake_module( struct makefile *make, const char *spec_file )
3309 unsigned int arch = 0; /* fake modules are always native */
3310 const char *name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3312 if (make->disabled[arch]) return;
3314 strarray_add( &make->all_targets[arch], name );
3315 add_install_rule( make, make->module, arch, name, strmake( "d$(dlldir)/%s", name ));
3317 output( "%s:", obj_dir_path( make, name ));
3318 if (spec_file) output_filename( spec_file );
3319 output_filenames_obj_dir( make, make->res_files[arch] );
3320 output_filename( tools_path( make, "winebuild" ));
3321 output_filename( tools_path( make, "winegcc" ));
3322 output( "\n" );
3323 output_winegcc_command( make, arch );
3324 output_filename( "-Wb,--fake-module" );
3325 if (!make->is_exe) output_filename( "-shared" );
3326 if (spec_file) output_filename( spec_file );
3327 output_filenames( make->extradllflags );
3328 output_filenames_obj_dir( make, make->res_files[arch] );
3329 output( "\n" );
3333 /*******************************************************************
3334 * output_module
3336 static void output_module( struct makefile *make, unsigned int arch )
3338 struct strarray default_imports = empty_strarray;
3339 struct strarray all_libs = empty_strarray;
3340 struct strarray dep_libs = empty_strarray;
3341 struct strarray imports = make->imports;
3342 const char *module_name;
3343 const char *debug_file;
3344 char *spec_file = NULL;
3345 unsigned int i;
3347 if (make->disabled[arch]) return;
3349 if (!make->is_exe)
3351 if (make->data_only || strarray_exists( &make->extradllflags, "-Wl,--subsystem,native" ))
3353 /* spec file is optional */
3354 struct incl_file *spec = find_src_file( make, replace_extension( make->module, ".dll", ".spec" ));
3355 if (spec) spec_file = spec->filename;
3357 else spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3360 if (!make->data_only)
3362 module_name = arch_module_name( make->module, arch );
3364 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3366 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ));
3367 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, IMPORT_TYPE_DELAYED, arch ));
3368 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3369 if (!arch) strarray_addall( &all_libs, libs );
3371 if (delay_load_flags[arch])
3373 for (i = 0; i < make->delayimports.count; i++)
3375 struct makefile *import = get_static_lib( make->delayimports.str[i], arch );
3376 if (import) strarray_add( &all_libs, strmake( "%s%s", delay_load_flags[arch], import->module ));
3380 else module_name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3382 strarray_add( &make->all_targets[arch], module_name );
3383 if (make->data_only)
3384 add_install_rule( make, make->module, arch, module_name,
3385 strmake( "d$(dlldir)/%s%s", arch_pe_dirs[arch], make->module ));
3386 else
3387 add_install_rule( make, make->module, arch, module_name,
3388 strmake( "%c%s%s%s", '0' + arch, arch_install_dirs[arch], make->module,
3389 dll_ext[arch] ));
3391 output( "%s:", obj_dir_path( make, module_name ));
3392 if (spec_file) output_filename( spec_file );
3393 output_filenames_obj_dir( make, make->object_files[arch] );
3394 output_filenames_obj_dir( make, make->res_files[arch] );
3395 output_filenames( dep_libs );
3396 output_filename( tools_path( make, "winebuild" ));
3397 output_filename( tools_path( make, "winegcc" ));
3398 output( "\n" );
3399 output_winegcc_command( make, arch );
3400 if (arch) output_filename( "-Wl,--wine-builtin" );
3401 if (!make->is_exe) output_filename( "-shared" );
3402 if (spec_file) output_filename( spec_file );
3403 output_filenames( make->extradllflags );
3404 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3405 output_filenames_obj_dir( make, make->object_files[arch] );
3406 output_filenames_obj_dir( make, make->res_files[arch] );
3407 debug_file = get_debug_file( make, module_name, arch );
3408 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3409 output_filenames( all_libs );
3410 output_filename( arch_make_variable( "LDFLAGS", arch ));
3411 output( "\n" );
3413 if (!make->data_only && !arch && unix_lib_supported) output_fake_module( make, spec_file );
3417 /*******************************************************************
3418 * output_import_lib
3420 static void output_import_lib( struct makefile *make, unsigned int arch )
3422 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3423 const char *name = strmake( "%slib%s.a", arch_dirs[arch], make->importlib );
3425 strarray_add( &make->clean_files, name );
3426 if (needs_delay_lib( make, arch ))
3428 const char *delay_name = replace_extension( name, ".a", ".delay.a" );
3429 strarray_add( &make->clean_files, delay_name );
3430 output( "%s ", obj_dir_path( make, delay_name ));
3432 output( "%s: %s %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ), spec_file );
3433 output_filenames_obj_dir( make, make->implib_files[arch] );
3434 output( "\n" );
3435 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3436 if (!delay_load_flags[arch]) output_filename( "--without-dlltool" );
3437 output_filenames( target_flags[arch] );
3438 if (make->is_win16) output_filename( "-m16" );
3439 output_filename( "--export" );
3440 output_filename( spec_file );
3441 output_filenames_obj_dir( make, make->implib_files[arch] );
3442 output( "\n" );
3444 add_install_rule( make, make->importlib, arch, name,
3445 strmake( "d%slib%s.a", arch_install_dirs[arch], make->importlib ));
3449 /*******************************************************************
3450 * output_unix_lib
3452 static void output_unix_lib( struct makefile *make )
3454 struct strarray unix_deps = empty_strarray;
3455 struct strarray unix_libs = add_unix_libraries( make, &unix_deps );
3456 unsigned int arch = 0; /* unix libs are always native */
3458 if (make->disabled[arch]) return;
3460 strarray_add( &make->all_targets[arch], make->unixlib );
3461 add_install_rule( make, make->module, arch, make->unixlib,
3462 strmake( "p%s%s", arch_install_dirs[arch], make->unixlib ));
3463 output( "%s:", obj_dir_path( make, make->unixlib ));
3464 output_filenames_obj_dir( make, make->unixobj_files );
3465 output_filenames( unix_deps );
3466 output( "\n" );
3467 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3468 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3469 output_filenames_obj_dir( make, make->unixobj_files );
3470 output_filenames( unix_libs );
3471 output_filename( "$(LDFLAGS)" );
3472 output( "\n" );
3476 /*******************************************************************
3477 * output_static_lib
3479 static void output_static_lib( struct makefile *make, unsigned int arch )
3481 const char *name = strmake( "%s%s", arch_dirs[arch], make->staticlib );
3483 strarray_add( &make->clean_files, name );
3484 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3485 output_filenames_obj_dir( make, make->object_files[arch] );
3486 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3487 output( "\n" );
3488 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3489 output_filenames( target_flags[arch] );
3490 output_filenames_obj_dir( make, make->object_files[arch] );
3491 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3492 output( "\n" );
3493 if (!make->extlib)
3494 add_install_rule( make, make->staticlib, arch, name,
3495 strmake( "d%s%s", arch_install_dirs[arch], make->staticlib ));
3499 /*******************************************************************
3500 * output_test_module
3502 static void output_test_module( struct makefile *make, unsigned int arch )
3504 char *basemodule = replace_extension( make->testdll, ".dll", "" );
3505 char *stripped = arch_module_name( strmake( "%s_test-stripped.exe", basemodule ), arch );
3506 char *testmodule = arch_module_name( strmake( "%s_test.exe", basemodule ), arch );
3507 struct strarray default_imports = get_default_imports( make, make->imports );
3508 struct strarray dep_libs = empty_strarray;
3509 struct strarray all_libs = empty_strarray;
3510 struct makefile *parent = get_parent_makefile( make );
3511 const char *debug_file;
3513 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, IMPORT_TYPE_DIRECT, arch ) );
3514 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3516 strarray_add( &make->all_targets[arch], testmodule );
3517 strarray_add( &make->clean_files, stripped );
3518 output( "%s:\n", obj_dir_path( make, testmodule ));
3519 output_winegcc_command( make, arch );
3520 output_filenames( make->extradllflags );
3521 output_filenames_obj_dir( make, make->object_files[arch] );
3522 output_filenames_obj_dir( make, make->res_files[arch] );
3523 if ((debug_file = get_debug_file( make, testmodule, arch )))
3524 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3525 output_filenames( all_libs );
3526 output_filename( arch_make_variable( "LDFLAGS", arch ));
3527 output( "\n" );
3528 output( "%s:\n", obj_dir_path( make, stripped ));
3529 output_winegcc_command( make, arch );
3530 output_filename( "-s" );
3531 output_filename( strmake( "-Wb,-F,%s_test.exe", basemodule ));
3532 output_filenames( make->extradllflags );
3533 output_filenames_obj_dir( make, make->object_files[arch] );
3534 output_filenames_obj_dir( make, make->res_files[arch] );
3535 output_filenames( all_libs );
3536 output_filename( arch_make_variable( "LDFLAGS", arch ));
3537 output( "\n" );
3538 output( "%s %s:", obj_dir_path( make, testmodule ), obj_dir_path( make, stripped ));
3539 output_filenames_obj_dir( make, make->object_files[arch] );
3540 output_filenames_obj_dir( make, make->res_files[arch] );
3541 output_filenames( dep_libs );
3542 output_filename( tools_path( make, "winebuild" ));
3543 output_filename( tools_path( make, "winegcc" ));
3544 output( "\n" );
3546 output( "programs/winetest/%s%s_test.res: %s\n", arch_dirs[arch], basemodule,
3547 obj_dir_path( make, stripped ));
3548 output( "\t%secho \"%s_test.exe TESTRES \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3549 basemodule, obj_dir_path( make, stripped ), tools_path( make, "wrc" ));
3551 if (make->disabled[arch] || (parent && parent->disabled[arch]))
3553 make->ok_files[arch] = empty_strarray;
3554 return;
3556 output_filenames_obj_dir( make, make->ok_files[arch] );
3557 output( ": %s", obj_dir_path( make, testmodule ));
3558 if (parent)
3560 char *parent_module = arch_module_name( make->testdll, arch );
3561 output_filename( obj_dir_path( parent, parent_module ));
3562 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3564 output( "\n" );
3565 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3566 output_filenames_obj_dir( make, make->ok_files[arch] );
3567 output( "\n" );
3568 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3569 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3570 output( "%s::\n", obj_dir_path( make, "testclean" ));
3571 output( "\trm -f" );
3572 output_filenames_obj_dir( make, make->ok_files[arch] );
3573 output( "\n" );
3574 strarray_addall( &make->clean_files, make->ok_files[arch] );
3575 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3579 /*******************************************************************
3580 * output_programs
3582 static void output_programs( struct makefile *make )
3584 unsigned int i, j;
3585 unsigned int arch = 0; /* programs are always native */
3587 for (i = 0; i < make->programs.count; i++)
3589 char *program_installed = NULL;
3590 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3591 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3592 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3593 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3594 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3596 if (!objs.count) objs = make->object_files[arch];
3597 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3599 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3600 strarray_addall( &all_libs, libs );
3603 output( "%s:", obj_dir_path( make, program ) );
3604 output_filenames_obj_dir( make, objs );
3605 output_filenames( deps );
3606 output( "\n" );
3607 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3608 output_filenames_obj_dir( make, objs );
3609 output_filenames( all_libs );
3610 output_filename( "$(LDFLAGS)" );
3611 output( "\n" );
3612 strarray_add( &make->all_targets[arch], program );
3614 for (j = 0; j < symlinks.count; j++)
3616 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3617 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3619 strarray_addall( &make->all_targets[arch], symlinks );
3621 add_install_rule( make, program, arch, program_installed ? program_installed : program,
3622 strmake( "p$(bindir)/%s", program ));
3623 for (j = 0; j < symlinks.count; j++)
3624 add_install_rule( make, symlinks.str[j], arch, program,
3625 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3630 /*******************************************************************
3631 * output_subdirs
3633 static void output_subdirs( struct makefile *make )
3635 struct strarray all_targets = empty_strarray;
3636 struct strarray makefile_deps = empty_strarray;
3637 struct strarray clean_files = empty_strarray;
3638 struct strarray testclean_files = empty_strarray;
3639 struct strarray distclean_files = empty_strarray;
3640 struct strarray distclean_dirs = empty_strarray;
3641 struct strarray dependencies = empty_strarray;
3642 struct strarray install_deps[NB_INSTALL_RULES] = { empty_strarray };
3643 struct strarray tooldeps_deps = empty_strarray;
3644 struct strarray buildtest_deps = empty_strarray;
3645 unsigned int i, j, arch;
3647 strarray_addall( &clean_files, make->clean_files );
3648 strarray_addall( &distclean_files, make->distclean_files );
3649 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3650 for (i = 0; i < subdirs.count; i++)
3652 struct strarray subclean = empty_strarray;
3653 strarray_addall( &subclean, get_removable_dirs( submakes[i]->clean_files ));
3654 strarray_addall( &subclean, get_removable_dirs( submakes[i]->distclean_files ));
3655 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3656 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3657 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3658 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3659 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3660 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3661 strarray_addall_path( &distclean_dirs, submakes[i]->obj_dir, subclean );
3662 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3663 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3665 for (arch = 0; arch < archs.count; arch++)
3667 if (submakes[i]->disabled[arch]) continue;
3668 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets[arch] );
3669 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files[arch] );
3671 if (submakes[i]->disabled[0]) continue;
3673 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3674 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3675 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3676 if (submakes[i]->testdll)
3677 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3678 for (j = 0; j < NB_INSTALL_RULES; j++)
3679 if (submakes[i]->install_rules[j].count)
3680 strarray_add( &install_deps[j], obj_dir_path( submakes[i], install_targets[j] ));
3682 strarray_addall( &dependencies, makefile_deps );
3683 output( "all:" );
3684 output_filenames( all_targets );
3685 output( "\n" );
3686 output( "Makefile:" );
3687 output_filenames( makefile_deps );
3688 output( "\n" );
3689 output_filenames( dependencies );
3690 output( ":\n" );
3691 for (j = 0; j < NB_INSTALL_RULES; j++)
3693 if (!install_deps[j].count) continue;
3694 if (strcmp( install_targets[j], "install-test" ))
3696 output( "install " );
3697 strarray_add_uniq( &make->phony_targets, "install" );
3699 output( "%s::", install_targets[j] );
3700 output_filenames( install_deps[j] );
3701 output( "\n" );
3702 strarray_add_uniq( &make->phony_targets, install_targets[j] );
3704 output_uninstall_rules( make );
3705 if (buildtest_deps.count)
3707 output( "buildtests:" );
3708 output_filenames( buildtest_deps );
3709 output( "\n" );
3710 strarray_add_uniq( &make->phony_targets, "buildtests" );
3712 output( "check test:" );
3713 output_filenames( testclean_files );
3714 output( "\n" );
3715 strarray_add_uniq( &make->phony_targets, "check" );
3716 strarray_add_uniq( &make->phony_targets, "test" );
3718 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3720 output( "clean::\n");
3721 output_rm_filenames( clean_files, "rm -f" );
3722 output( "testclean::\n");
3723 output_rm_filenames( testclean_files, "rm -f" );
3724 output( "distclean::\n");
3725 output_rm_filenames( distclean_files, "rm -f" );
3726 output_rm_filenames( distclean_dirs, "-rmdir 2>/dev/null" );
3727 output( "maintainer-clean::\n");
3728 output_rm_filenames( make->maintainerclean_files, "rm -f" );
3729 strarray_add_uniq( &make->phony_targets, "distclean" );
3730 strarray_add_uniq( &make->phony_targets, "testclean" );
3731 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3733 if (tooldeps_deps.count)
3735 output( "__tooldeps__:" );
3736 output_filenames( tooldeps_deps );
3737 output( "\n" );
3738 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3741 if (make->phony_targets.count)
3743 output( ".PHONY:" );
3744 output_filenames( make->phony_targets );
3745 output( "\n" );
3750 /*******************************************************************
3751 * output_sources
3753 static void output_sources( struct makefile *make )
3755 struct strarray all_targets = empty_strarray;
3756 struct incl_file *source;
3757 unsigned int i, j, arch;
3759 strarray_add_uniq( &make->phony_targets, "all" );
3761 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3763 char *obj = xstrdup( source->name );
3764 char *ext = get_extension( obj );
3766 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3767 *ext++ = 0;
3769 for (j = 0; output_source_funcs[j].ext; j++)
3770 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3772 output_source_funcs[j].fn( make, source, obj );
3773 strarray_addall_uniq( &make->dependencies, source->dependencies );
3776 /* special case for winetest: add resource files from other test dirs */
3777 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3779 for (arch = 0; arch < archs.count; arch++)
3781 if (!is_multiarch( arch )) continue;
3782 for (i = 0; i < subdirs.count; i++)
3784 if (!submakes[i]->testdll) continue;
3785 if (submakes[i]->disabled[arch]) continue;
3786 if (enable_tests.count && !strarray_exists( &enable_tests, submakes[i]->testdll )) continue;
3787 strarray_add( &make->res_files[arch],
3788 strmake( "%s%s", arch_dirs[arch],
3789 replace_extension( submakes[i]->testdll, ".dll", "_test.res" )));
3794 if (make->dlldata_files.count)
3796 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3797 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3798 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3799 output_filenames( make->dlldata_files );
3800 output( "\n" );
3803 if (make->staticlib)
3805 for (arch = 0; arch < archs.count; arch++)
3806 if (is_multiarch( arch ) || (so_dll_supported && !make->extlib))
3807 output_static_lib( make, arch );
3809 else if (make->module)
3811 for (arch = 0; arch < archs.count; arch++)
3813 if (is_multiarch( arch )) output_module( make, arch );
3814 if (make->importlib && (is_multiarch( arch ) || !is_native_arch_disabled( make )))
3815 output_import_lib( make, arch );
3817 if (make->unixlib) output_unix_lib( make );
3818 if (make->is_exe && !make->is_win16 && unix_lib_supported && strendswith( make->module, ".exe" ))
3820 char *binary = replace_extension( make->module, ".exe", "" );
3821 add_install_rule( make, binary, 0, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3824 else if (make->testdll)
3826 for (arch = 0; arch < archs.count; arch++)
3827 if (is_multiarch( arch )) output_test_module( make, arch );
3829 else if (make->programs.count) output_programs( make );
3831 for (i = 0; i < make->scripts.count; i++)
3832 add_install_rule( make, make->scripts.str[i], 0, make->scripts.str[i],
3833 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3835 for (i = 0; i < make->extra_targets.count; i++)
3836 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3837 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3838 else
3839 strarray_add( &make->all_targets[0], make->extra_targets.str[i] );
3841 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3842 strarray_add( &make->distclean_files, "Makefile" );
3843 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3845 if (!make->obj_dir)
3846 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3847 else if (!strcmp( make->obj_dir, "po" ))
3848 strarray_add( &make->distclean_files, "LINGUAS" );
3850 for (arch = 0; arch < archs.count; arch++)
3852 strarray_addall_uniq( &make->clean_files, make->object_files[arch] );
3853 strarray_addall_uniq( &make->clean_files, make->implib_files[arch] );
3854 strarray_addall_uniq( &make->clean_files, make->res_files[arch] );
3855 strarray_addall_uniq( &make->clean_files, make->all_targets[arch] );
3857 strarray_addall( &make->clean_files, make->unixobj_files );
3858 strarray_addall( &make->clean_files, make->pot_files );
3859 strarray_addall( &make->clean_files, make->debug_files );
3861 if (make == top_makefile)
3863 output_subdirs( make );
3864 return;
3867 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3868 strarray_addall( &all_targets, make->font_files );
3869 if (all_targets.count)
3871 output( "%s:", obj_dir_path( make, "all" ));
3872 output_filenames_obj_dir( make, all_targets );
3873 output( "\n" );
3874 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3876 for (i = 0; i < NB_INSTALL_RULES; i++) output_install_rules( make, i );
3878 if (make->clean_files.count)
3880 output( "%s::\n", obj_dir_path( make, "clean" ));
3881 output( "\trm -f" );
3882 output_filenames_obj_dir( make, make->clean_files );
3883 output( "\n" );
3884 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3889 /*******************************************************************
3890 * create_temp_file
3892 static FILE *create_temp_file( const char *orig )
3894 char *name = xmalloc( strlen(orig) + 13 );
3895 unsigned int i, id = getpid();
3896 int fd;
3897 FILE *ret = NULL;
3899 for (i = 0; i < 100; i++)
3901 sprintf( name, "%s.tmp%08x", orig, id );
3902 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3904 ret = fdopen( fd, "w" );
3905 break;
3907 if (errno != EEXIST) break;
3908 id += 7777;
3910 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3911 temp_file_name = name;
3912 return ret;
3916 /*******************************************************************
3917 * rename_temp_file
3919 static void rename_temp_file( const char *dest )
3921 int ret = rename( temp_file_name, dest );
3922 if (ret == -1 && errno == EEXIST)
3924 /* rename doesn't overwrite on windows */
3925 unlink( dest );
3926 ret = rename( temp_file_name, dest );
3928 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3929 temp_file_name = NULL;
3933 /*******************************************************************
3934 * are_files_identical
3936 static int are_files_identical( FILE *file1, FILE *file2 )
3938 for (;;)
3940 char buffer1[8192], buffer2[8192];
3941 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3942 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3943 if (size1 != size2) return 0;
3944 if (!size1) return feof( file1 ) && feof( file2 );
3945 if (memcmp( buffer1, buffer2, size1 )) return 0;
3950 /*******************************************************************
3951 * rename_temp_file_if_changed
3953 static void rename_temp_file_if_changed( const char *dest )
3955 FILE *file1, *file2;
3956 int do_rename = 1;
3958 if ((file1 = fopen( dest, "r" )))
3960 if ((file2 = fopen( temp_file_name, "r" )))
3962 do_rename = !are_files_identical( file1, file2 );
3963 fclose( file2 );
3965 fclose( file1 );
3967 if (!do_rename)
3969 unlink( temp_file_name );
3970 temp_file_name = NULL;
3972 else rename_temp_file( dest );
3976 /*******************************************************************
3977 * output_linguas
3979 static void output_linguas( const struct makefile *make )
3981 const char *dest = obj_dir_path( make, "LINGUAS" );
3982 struct incl_file *source;
3984 output_file = create_temp_file( dest );
3986 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3987 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3988 if (strendswith( source->name, ".po" ))
3989 output( "%s\n", replace_extension( source->name, ".po", "" ));
3991 if (fclose( output_file )) fatal_perror( "write" );
3992 output_file = NULL;
3993 rename_temp_file_if_changed( dest );
3997 /*******************************************************************
3998 * output_testlist
4000 static void output_testlist( const struct makefile *make )
4002 const char *dest = obj_dir_path( make, "testlist.c" );
4003 unsigned int i;
4005 output_file = create_temp_file( dest );
4007 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
4008 output( "#define WIN32_LEAN_AND_MEAN\n" );
4009 output( "#include <windows.h>\n\n" );
4010 output( "#define STANDALONE\n" );
4011 output( "#include \"wine/test.h\"\n\n" );
4013 for (i = 0; i < make->test_files.count; i++)
4014 output( "extern void func_%s(void);\n", make->test_files.str[i] );
4015 output( "\n" );
4016 output( "const struct test winetest_testlist[] =\n" );
4017 output( "{\n" );
4018 for (i = 0; i < make->test_files.count; i++)
4019 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
4020 output( " { 0, 0 }\n" );
4021 output( "};\n" );
4023 if (fclose( output_file )) fatal_perror( "write" );
4024 output_file = NULL;
4025 rename_temp_file_if_changed( dest );
4029 /*******************************************************************
4030 * output_gitignore
4032 static void output_gitignore( const char *dest, struct strarray files )
4034 unsigned int i;
4036 output_file = create_temp_file( dest );
4038 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4039 for (i = 0; i < files.count; i++)
4041 if (!strchr( files.str[i], '/' )) output( "/" );
4042 output( "%s\n", files.str[i] );
4045 if (fclose( output_file )) fatal_perror( "write" );
4046 output_file = NULL;
4047 rename_temp_file( dest );
4051 /*******************************************************************
4052 * output_stub_makefile
4054 static void output_stub_makefile( struct makefile *make )
4056 struct strarray targets = empty_strarray;
4057 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
4058 unsigned int i, arch;
4060 for (arch = 0; arch < archs.count; arch++)
4061 if (make->all_targets[arch].count) strarray_add_uniq( &targets, "all" );
4063 for (i = 0; i < NB_INSTALL_RULES; i++)
4065 if (!make->install_rules[i].count) continue;
4066 strarray_add_uniq( &targets, "install" );
4067 strarray_add( &targets, install_targets[i] );
4069 if (make->clean_files.count) strarray_add( &targets, "clean" );
4070 if (make->test_files.count)
4072 strarray_add( &targets, "check" );
4073 strarray_add( &targets, "test" );
4074 strarray_add( &targets, "testclean" );
4077 if (!targets.count && !make->clean_files.count) return;
4079 output_file_name = obj_dir_path( make, "Makefile" );
4080 output_file = create_temp_file( output_file_name );
4082 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
4084 if (make_var) output( "MAKE = %s\n\n", make_var );
4086 output( "all:\n" );
4087 output_filenames( targets );
4088 output_filenames( make->clean_files );
4089 output( ":\n" );
4090 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4091 output( ".PHONY:" );
4092 output_filenames( targets );
4093 output( "\n" );
4095 fclose( output_file );
4096 output_file = NULL;
4097 rename_temp_file( output_file_name );
4101 /*******************************************************************
4102 * output_silent_rules
4104 static void output_silent_rules(void)
4106 static const char *cmds[] =
4108 "BISON",
4109 "BUILD",
4110 "CC",
4111 "CCLD",
4112 "FLEX",
4113 "GEN",
4114 "LN",
4115 "MSG",
4116 "SED",
4117 "TEST",
4118 "WIDL",
4119 "WMC",
4120 "WRC"
4122 unsigned int i;
4124 output( "V = 0\n" );
4125 for (i = 0; i < ARRAY_SIZE(cmds); i++)
4127 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4128 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4129 output( "quiet_%s_1 =\n", cmds[i] );
4134 /*******************************************************************
4135 * output_top_makefile
4137 static void output_top_makefile( struct makefile *make )
4139 char buffer[1024];
4140 FILE *src_file;
4141 unsigned int i;
4142 int found = 0;
4144 output_file_name = obj_dir_path( make, output_makefile_name );
4145 output_file = create_temp_file( output_file_name );
4147 /* copy the contents of the source makefile */
4148 src_file = open_input_makefile( make );
4149 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4151 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4152 found = !strncmp( buffer, separator, strlen(separator) );
4154 if (fclose( src_file )) fatal_perror( "close" );
4155 input_file_name = NULL;
4157 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4159 if (silent_rules) output_silent_rules();
4160 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4161 output_sources( make );
4162 /* disable implicit rules */
4163 output( ".SUFFIXES:\n" );
4165 fclose( output_file );
4166 output_file = NULL;
4167 rename_temp_file( output_file_name );
4171 /*******************************************************************
4172 * output_dependencies
4174 static void output_dependencies( struct makefile *make )
4176 struct strarray ignore_files = empty_strarray;
4178 if (make->obj_dir) create_dir( make->obj_dir );
4180 if (make == top_makefile) output_top_makefile( make );
4181 else output_stub_makefile( make );
4183 strarray_addall( &ignore_files, make->distclean_files );
4184 strarray_addall( &ignore_files, make->clean_files );
4185 if (make->testdll) output_testlist( make );
4186 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4187 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4189 create_file_directories( make, ignore_files );
4191 output_file_name = NULL;
4195 /*******************************************************************
4196 * load_sources
4198 static void load_sources( struct makefile *make )
4200 static const char *source_vars[] =
4202 "SOURCES",
4203 "C_SRCS",
4204 "OBJC_SRCS",
4205 "RC_SRCS",
4206 "MC_SRCS",
4207 "IDL_SRCS",
4208 "BISON_SRCS",
4209 "LEX_SRCS",
4210 "HEADER_SRCS",
4211 "XTEMPLATE_SRCS",
4212 "SVG_SRCS",
4213 "FONT_SRCS",
4214 "IN_SRCS",
4215 "PO_SRCS",
4216 "MANPAGES",
4217 NULL
4219 const char **var;
4220 unsigned int i, arch;
4221 struct strarray value;
4222 struct incl_file *file;
4224 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4225 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4227 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4228 make->module = get_expanded_make_variable( make, "MODULE" );
4229 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4230 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4231 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4232 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4233 if (unix_lib_supported) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4235 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4236 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4237 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4238 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4239 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4240 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4241 for (i = 0; i < NB_INSTALL_RULES; i++)
4242 make->install[i] = get_expanded_make_var_array( make, install_variables[i] );
4244 if (make->extlib) make->staticlib = make->extlib;
4245 if (make->staticlib) make->module = make->staticlib;
4247 if (make->obj_dir)
4249 make->disabled[0] = strarray_exists( &disabled_dirs[0], make->obj_dir );
4250 for (arch = 1; arch < archs.count; arch++)
4251 make->disabled[arch] = make->disabled[0] || strarray_exists( &disabled_dirs[arch], make->obj_dir );
4253 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4254 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4255 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4256 strarray_exists( &make->extradllflags, "-mwindows" );
4258 if (make->module)
4260 /* add default install rules if nothing was specified */
4261 for (i = 0; i < NB_INSTALL_RULES; i++) if (make->install[i].count) break;
4262 if (i == NB_INSTALL_RULES)
4264 if (make->importlib) strarray_add( &make->install[INSTALL_DEV], make->importlib );
4265 if (make->staticlib) strarray_add( &make->install[INSTALL_DEV], make->staticlib );
4266 else strarray_add( &make->install[INSTALL_LIB], make->module );
4270 make->include_paths = empty_strarray;
4271 make->include_args = empty_strarray;
4272 make->define_args = empty_strarray;
4273 make->unix_cflags = empty_strarray;
4274 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4275 strarray_add( &make->unix_cflags, "-DWINE_UNIX_LIB" );
4277 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4278 for (i = 0; i < value.count; i++)
4280 if (!strncmp( value.str[i], "-I", 2 ))
4282 const char *dir = value.str[i] + 2;
4283 if (!strncmp( dir, "./", 2 ))
4285 dir += 2;
4286 while (*dir == '/') dir++;
4288 strarray_add_uniq( &make->include_paths, dir );
4290 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4291 strarray_add_uniq( &make->define_args, value.str[i] );
4293 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4294 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4296 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4297 if (make->src_dir)
4298 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4299 if (make->parent_dir)
4300 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4301 strarray_add( &make->include_args, "-Iinclude" );
4302 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4304 list_init( &make->sources );
4305 list_init( &make->includes );
4307 for (var = source_vars; *var; var++)
4309 value = get_expanded_make_var_array( make, *var );
4310 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4313 add_generated_sources( make );
4315 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4316 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4318 for (i = 0; i < make->delayimports.count; i++)
4319 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4323 /*******************************************************************
4324 * parse_makeflags
4326 static void parse_makeflags( const char *flags )
4328 const char *p = flags;
4329 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4331 while (*p)
4333 p = skip_spaces( p );
4334 var = buffer;
4335 while (*p && !isspace(*p))
4337 if (*p == '\\' && p[1]) p++;
4338 *var++ = *p++;
4340 *var = 0;
4341 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4346 /*******************************************************************
4347 * parse_option
4349 static int parse_option( const char *opt )
4351 if (opt[0] != '-')
4353 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4354 return 0;
4356 switch(opt[1])
4358 case 'f':
4359 if (opt[2]) output_makefile_name = opt + 2;
4360 break;
4361 case 'R':
4362 relative_dir_mode = 1;
4363 break;
4364 case 'S':
4365 silent_rules = 1;
4366 break;
4367 default:
4368 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4369 exit(1);
4371 return 1;
4375 /*******************************************************************
4376 * main
4378 int main( int argc, char *argv[] )
4380 const char *makeflags = getenv( "MAKEFLAGS" );
4381 const char *target;
4382 unsigned int i, j, arch;
4384 if (makeflags) parse_makeflags( makeflags );
4386 i = 1;
4387 while (i < argc)
4389 if (parse_option( argv[i] ))
4391 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4392 argc--;
4394 else i++;
4397 if (relative_dir_mode)
4399 char *relpath;
4401 if (argc != 3)
4403 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4404 exit( 1 );
4406 relpath = get_relative_path( argv[1], argv[2] );
4407 printf( "%s\n", relpath ? relpath : "." );
4408 exit( 0 );
4411 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4413 atexit( cleanup_files );
4414 init_signals( exit_on_signal );
4416 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4417 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4419 top_makefile = parse_makefile( NULL );
4421 target_flags[0] = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4422 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4423 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4424 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4425 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4426 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4427 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4428 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4429 for (i = 0; i < NB_INSTALL_RULES; i++)
4430 top_install[i] = get_expanded_make_var_array( top_makefile, strmake( "TOP_%s", install_variables[i] ));
4432 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4433 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4434 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4435 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4436 dll_ext[0] = get_expanded_make_variable( top_makefile, "DLLEXT" );
4437 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4438 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4439 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4440 bison = get_expanded_make_variable( top_makefile, "BISON" );
4441 ar = get_expanded_make_variable( top_makefile, "AR" );
4442 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4443 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4444 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4445 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4446 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4447 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4448 wayland_scanner = get_expanded_make_variable( top_makefile, "WAYLAND_SCANNER" );
4450 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4451 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4452 if (!exe_ext) exe_ext = "";
4453 if (!dll_ext[0]) dll_ext[0] = "";
4454 if (!tools_ext) tools_ext = "";
4456 unix_lib_supported = !!strcmp( exe_ext, ".exe" );
4457 so_dll_supported = !!dll_ext[0][0]; /* non-empty dll ext means supported */
4459 strarray_add( &archs, get_expanded_make_variable( top_makefile, "HOST_ARCH" ));
4460 strarray_addall( &archs, get_expanded_make_var_array( top_makefile, "PE_ARCHS" ));
4462 arch_dirs[0] = "";
4463 arch_pe_dirs[0] = strmake( "%s-windows/", archs.str[0] );
4464 arch_install_dirs[0] = unix_lib_supported ? strmake( "$(dlldir)/%s-unix/", archs.str[0] ) : "$(dlldir)/";
4465 strip_progs[0] = "\"$(STRIP)\"";
4467 for (arch = 1; arch < archs.count; arch++)
4469 target = get_expanded_arch_var( top_makefile, "TARGET", arch );
4470 strarray_add( &target_flags[arch], "-b" );
4471 strarray_add( &target_flags[arch], target );
4472 arch_dirs[arch] = strmake( "%s-windows/", archs.str[arch] );
4473 arch_pe_dirs[arch] = arch_dirs[arch];
4474 arch_install_dirs[arch] = strmake( "$(dlldir)/%s", arch_dirs[arch] );
4475 strip_progs[arch] = strmake( "%s-strip", target );
4476 dll_ext[arch] = "";
4479 for (arch = 0; arch < archs.count; arch++)
4481 extra_cflags[arch] = get_expanded_arch_var_array( top_makefile, "EXTRACFLAGS", arch );
4482 extra_cflags_extlib[arch] = remove_warning_flags( extra_cflags[arch] );
4483 disabled_dirs[arch] = get_expanded_arch_var_array( top_makefile, "DISABLED_SUBDIRS", arch );
4484 if (!is_multiarch( arch )) continue;
4485 delay_load_flags[arch] = get_expanded_arch_var( top_makefile, "DELAYLOADFLAG", arch );
4486 debug_flags[arch] = get_expanded_arch_var( top_makefile, "DEBUG", arch );
4489 if (unix_lib_supported)
4491 delay_load_flags[0] = "-Wl,-delayload,";
4492 debug_flags[0] = NULL;
4495 top_makefile->src_dir = root_src_dir;
4496 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4497 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4499 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4501 load_sources( top_makefile );
4502 load_sources( include_makefile );
4503 for (i = 0; i < subdirs.count; i++)
4504 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4506 output_dependencies( top_makefile );
4507 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4509 return 0;