gdi32: Support stock brush in EMR_FILLRGN.
[wine.git] / tools / makedep.c
blob49a7514c9f0c083fc855b7525c4e461461a07cd1
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 int use_msvcrt:1; /* put msvcrt headers in the search path? */
79 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_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
91 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
92 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
93 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
94 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
95 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (_l.res) file */
96 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
97 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
98 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
99 #define FLAG_RC_HEADER 0x020000 /* rc file is a header */
100 #define FLAG_C_IMPLIB 0x040000 /* file is part of an import library */
101 #define FLAG_C_UNIX 0x080000 /* file is part of a Unix library */
102 #define FLAG_SFD_FONTS 0x100000 /* sfd file generated bitmap fonts */
104 static const struct
106 unsigned int flag;
107 const char *ext;
108 } idl_outputs[] =
110 { FLAG_IDL_TYPELIB, "_l.res" },
111 { FLAG_IDL_REGTYPELIB, "_t.res" },
112 { FLAG_IDL_CLIENT, "_c.c" },
113 { FLAG_IDL_IDENT, "_i.c" },
114 { FLAG_IDL_PROXY, "_p.c" },
115 { FLAG_IDL_SERVER, "_s.c" },
116 { FLAG_IDL_REGISTER, "_r.res" },
119 #define HASH_SIZE 197
121 static struct list files[HASH_SIZE];
122 static struct list global_includes[HASH_SIZE];
124 enum install_rules { INSTALL_LIB, INSTALL_DEV, INSTALL_TEST, NB_INSTALL_RULES };
125 static const char *install_targets[NB_INSTALL_RULES] = { "install-lib", "install-dev", "install-test" };
126 static const char *install_variables[NB_INSTALL_RULES] = { "INSTALL_LIB", "INSTALL_DEV", "INSTALL_TEST" };
128 #define MAX_ARCHS 5
130 /* variables common to all makefiles */
131 static struct strarray archs;
132 static struct strarray linguas;
133 static struct strarray dll_flags;
134 static struct strarray unix_dllflags;
135 static struct strarray msvcrt_flags;
136 static struct strarray cpp_flags;
137 static struct strarray lddll_flags;
138 static struct strarray libs;
139 static struct strarray enable_tests;
140 static struct strarray cmdline_vars;
141 static struct strarray subdirs;
142 static struct strarray delay_import_libs;
143 static struct strarray top_install[NB_INSTALL_RULES];
144 static const char *root_src_dir;
145 static const char *tools_dir;
146 static const char *tools_ext;
147 static const char *exe_ext;
148 static const char *dll_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 /* per-architecture global variables */
161 static const char *arch_dirs[MAX_ARCHS];
162 static const char *arch_pe_dirs[MAX_ARCHS];
163 static const char *arch_install_dirs[MAX_ARCHS];
164 static const char *strip_progs[MAX_ARCHS];
165 static const char *debug_flags[MAX_ARCHS];
166 static const char *delay_load_flags[MAX_ARCHS];
167 static struct strarray target_flags[MAX_ARCHS];
168 static struct strarray extra_cflags[MAX_ARCHS];
169 static struct strarray extra_cflags_extlib[MAX_ARCHS];
170 static struct strarray disabled_dirs[MAX_ARCHS];
172 struct makefile
174 /* values determined from input makefile */
175 struct strarray vars;
176 struct strarray include_paths;
177 struct strarray include_args;
178 struct strarray define_args;
179 struct strarray unix_cflags;
180 struct strarray programs;
181 struct strarray scripts;
182 struct strarray imports;
183 struct strarray delayimports;
184 struct strarray extradllflags;
185 struct strarray install[NB_INSTALL_RULES];
186 struct strarray extra_targets;
187 struct strarray extra_imports;
188 struct list sources;
189 struct list includes;
190 const char *src_dir;
191 const char *obj_dir;
192 const char *parent_dir;
193 const char *module;
194 const char *testdll;
195 const char *extlib;
196 const char *staticlib;
197 const char *importlib;
198 const char *unixlib;
199 int data_only;
200 int is_win16;
201 int is_exe;
202 int disabled[MAX_ARCHS];
204 /* values generated at output time */
205 struct strarray in_files;
206 struct strarray pot_files;
207 struct strarray test_files;
208 struct strarray clean_files;
209 struct strarray distclean_files;
210 struct strarray maintainerclean_files;
211 struct strarray uninstall_files;
212 struct strarray unixobj_files;
213 struct strarray font_files;
214 struct strarray debug_files;
215 struct strarray dlldata_files;
216 struct strarray phony_targets;
217 struct strarray dependencies;
218 struct strarray object_files[MAX_ARCHS];
219 struct strarray implib_files[MAX_ARCHS];
220 struct strarray ok_files[MAX_ARCHS];
221 struct strarray res_files[MAX_ARCHS];
222 struct strarray all_targets[MAX_ARCHS];
223 struct strarray install_rules[NB_INSTALL_RULES];
226 static struct makefile *top_makefile;
227 static struct makefile *include_makefile;
228 static struct makefile **submakes;
230 static const char separator[] = "### Dependencies";
231 static const char *output_makefile_name = "Makefile";
232 static const char *input_file_name;
233 static const char *output_file_name;
234 static const char *temp_file_name;
235 static int relative_dir_mode;
236 static int silent_rules;
237 static int input_line;
238 static int output_column;
239 static FILE *output_file;
241 static const char Usage[] =
242 "Usage: makedep [options] [directories]\n"
243 "Options:\n"
244 " -R from to Compute the relative path between two directories\n"
245 " -S Generate Automake-style silent rules\n"
246 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
249 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
250 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
251 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
252 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
254 /*******************************************************************
255 * fatal_error
257 static void fatal_error( const char *msg, ... )
259 va_list valist;
260 va_start( valist, msg );
261 if (input_file_name)
263 fprintf( stderr, "%s:", input_file_name );
264 if (input_line) fprintf( stderr, "%d:", input_line );
265 fprintf( stderr, " error: " );
267 else fprintf( stderr, "makedep: error: " );
268 vfprintf( stderr, msg, valist );
269 va_end( valist );
270 exit(1);
274 /*******************************************************************
275 * fatal_perror
277 static void fatal_perror( const char *msg, ... )
279 va_list valist;
280 va_start( valist, msg );
281 if (input_file_name)
283 fprintf( stderr, "%s:", input_file_name );
284 if (input_line) fprintf( stderr, "%d:", input_line );
285 fprintf( stderr, " error: " );
287 else fprintf( stderr, "makedep: error: " );
288 vfprintf( stderr, msg, valist );
289 perror( " " );
290 va_end( valist );
291 exit(1);
295 /*******************************************************************
296 * cleanup_files
298 static void cleanup_files(void)
300 if (temp_file_name) unlink( temp_file_name );
301 if (output_file_name) unlink( output_file_name );
305 /*******************************************************************
306 * exit_on_signal
308 static void exit_on_signal( int sig )
310 exit( 1 ); /* this will call the atexit functions */
314 /*******************************************************************
315 * output
317 static void output( const char *format, ... )
319 int ret;
320 va_list valist;
322 va_start( valist, format );
323 ret = vfprintf( output_file, format, valist );
324 va_end( valist );
325 if (ret < 0) fatal_perror( "output" );
326 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
327 else output_column += ret;
331 /*******************************************************************
332 * strarray_get_value
334 * Find a value in a name/value pair string array.
336 static const char *strarray_get_value( const struct strarray *array, const char *name )
338 int pos, res, min = 0, max = array->count / 2 - 1;
340 while (min <= max)
342 pos = (min + max) / 2;
343 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
344 if (res < 0) min = pos + 1;
345 else max = pos - 1;
347 return NULL;
351 /*******************************************************************
352 * strarray_set_value
354 * Define a value in a name/value pair string array.
356 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
358 int i, pos, res, min = 0, max = array->count / 2 - 1;
360 while (min <= max)
362 pos = (min + max) / 2;
363 if (!(res = strcmp( array->str[pos * 2], name )))
365 /* redefining a variable replaces the previous value */
366 array->str[pos * 2 + 1] = value;
367 return;
369 if (res < 0) min = pos + 1;
370 else max = pos - 1;
372 strarray_add( array, NULL );
373 strarray_add( array, NULL );
374 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
375 array->str[min * 2] = name;
376 array->str[min * 2 + 1] = value;
380 /*******************************************************************
381 * output_filename
383 static void output_filename( const char *name )
385 if (output_column + strlen(name) + 1 > 100)
387 output( " \\\n" );
388 output( " " );
390 else if (output_column) output( " " );
391 output( "%s", name );
395 /*******************************************************************
396 * output_filenames
398 static void output_filenames( struct strarray array )
400 unsigned int i;
402 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
406 /*******************************************************************
407 * output_rm_filenames
409 static void output_rm_filenames( struct strarray array, const char *command )
411 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
412 unsigned int i, len;
414 if (!array.count) return;
415 output( "\t%s", command );
416 for (i = len = 0; i < array.count; i++)
418 if (len > max_cmdline)
420 output( "\n" );
421 output( "\t%s", command );
422 len = 0;
424 output_filename( array.str[i] );
425 len += strlen( array.str[i] ) + 1;
427 output( "\n" );
431 /*******************************************************************
432 * get_extension
434 static char *get_extension( char *filename )
436 char *ext = strrchr( filename, '.' );
437 if (ext && strchr( ext, '/' )) ext = NULL;
438 return ext;
442 /*******************************************************************
443 * get_base_name
445 static const char *get_base_name( const char *name )
447 char *base;
448 if (!strchr( name, '.' )) return name;
449 base = xstrdup( name );
450 *strrchr( base, '.' ) = 0;
451 return base;
455 /*******************************************************************
456 * replace_filename
458 static char *replace_filename( const char *path, const char *name )
460 const char *p;
461 char *ret;
462 size_t len;
464 if (!path) return xstrdup( name );
465 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
466 len = p - path + 1;
467 ret = xmalloc( len + strlen( name ) + 1 );
468 memcpy( ret, path, len );
469 strcpy( ret + len, name );
470 return ret;
474 /*******************************************************************
475 * replace_substr
477 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
479 size_t pos = start - str;
480 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
481 memcpy( ret, str, pos );
482 strcpy( ret + pos, replace );
483 strcat( ret + pos, start + len );
484 return ret;
488 /*******************************************************************
489 * get_relative_path
491 * Determine where the destination path is located relative to the 'from' path.
493 static char *get_relative_path( const char *from, const char *dest )
495 const char *start;
496 char *ret, *p;
497 unsigned int dotdots = 0;
499 /* a path of "." is equivalent to an empty path */
500 if (!strcmp( from, "." )) from = "";
502 for (;;)
504 while (*from == '/') from++;
505 while (*dest == '/') dest++;
506 start = dest; /* save start of next path element */
507 if (!*from) break;
509 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
510 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
512 /* count remaining elements in 'from' */
515 dotdots++;
516 while (*from && *from != '/') from++;
517 while (*from == '/') from++;
519 while (*from);
520 break;
523 if (!start[0] && !dotdots) return NULL; /* empty path */
525 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
526 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
528 if (start[0]) strcpy( p, start );
529 else p[-1] = 0; /* remove trailing slash */
530 return ret;
534 /*******************************************************************
535 * concat_paths
537 static char *concat_paths( const char *base, const char *path )
539 int i, len;
540 char *ret;
542 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
543 if (!path || !path[0]) return xstrdup( base );
544 if (path[0] == '/') return xstrdup( path );
546 len = strlen( base );
547 while (len && base[len - 1] == '/') len--;
548 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
550 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
551 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
552 if (i != len - 1 || base[i] != '.')
554 path += 2;
555 while (*path == '/') path++;
557 /* else ignore "." element */
558 while (i > 0 && base[i - 1] == '/') i--;
559 len = i;
561 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
562 ret = xmalloc( len + strlen( path ) + 2 );
563 memcpy( ret, base, len );
564 ret[len++] = '/';
565 strcpy( ret + len, path );
566 return ret;
570 /*******************************************************************
571 * is_native_arch_disabled
573 * Check if the makefile was disabled for a PE arch that matches the native arch.
575 static int is_native_arch_disabled( struct makefile *make )
577 unsigned int arch;
579 for (arch = 1; arch < archs.count; arch++)
580 if (make->disabled[arch] && !strcmp( archs.str[0], archs.str[arch] ))
581 return 1;
582 return 0;
586 /*******************************************************************
587 * is_multiarch
589 * Check if arch is one of the PE architectures in multiarch.
590 * Also return TRUE for native arch iff there's no PE support.
592 static int is_multiarch( unsigned int arch )
594 return archs.count == 1 || arch;
598 /*******************************************************************
599 * is_using_msvcrt
601 * Check if the files of a makefile use msvcrt by default.
603 static int is_using_msvcrt( struct makefile *make )
605 return make->module || make->testdll;
609 /*******************************************************************
610 * arch_module_name
612 static char *arch_module_name( const char *module, unsigned int arch )
614 return strmake( "%s%s%s", arch_dirs[arch], module, arch ? "" : dll_ext );
618 /*******************************************************************
619 * arch_make_variable
621 static char *arch_make_variable( const char *name, unsigned int arch )
623 return arch ? strmake( "$(%s_%s)", archs.str[arch], name ) : strmake( "$(%s)", name );
627 /*******************************************************************
628 * obj_dir_path
630 static char *obj_dir_path( const struct makefile *make, const char *path )
632 return concat_paths( make->obj_dir, path );
636 /*******************************************************************
637 * src_dir_path
639 static char *src_dir_path( const struct makefile *make, const char *path )
641 if (make->src_dir) return concat_paths( make->src_dir, path );
642 return obj_dir_path( make, path );
646 /*******************************************************************
647 * root_src_dir_path
649 static char *root_src_dir_path( const char *path )
651 return concat_paths( root_src_dir, path );
655 /*******************************************************************
656 * tools_dir_path
658 static char *tools_dir_path( const struct makefile *make, const char *path )
660 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
661 return strmake( "tools/%s", path );
665 /*******************************************************************
666 * tools_path
668 static char *tools_path( const struct makefile *make, const char *name )
670 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
674 /*******************************************************************
675 * strarray_addall_path
677 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
679 unsigned int i;
681 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
685 /*******************************************************************
686 * get_line
688 static char *get_line( FILE *file )
690 static char *buffer;
691 static size_t size;
693 if (!size)
695 size = 1024;
696 buffer = xmalloc( size );
698 if (!fgets( buffer, size, file )) return NULL;
699 input_line++;
701 for (;;)
703 char *p = buffer + strlen(buffer);
704 /* if line is larger than buffer, resize buffer */
705 while (p == buffer + size - 1 && p[-1] != '\n')
707 buffer = xrealloc( buffer, size * 2 );
708 if (!fgets( buffer + size - 1, size + 1, file )) break;
709 p = buffer + strlen(buffer);
710 size *= 2;
712 if (p > buffer && p[-1] == '\n')
714 *(--p) = 0;
715 if (p > buffer && p[-1] == '\r') *(--p) = 0;
716 if (p > buffer && p[-1] == '\\')
718 *(--p) = 0;
719 /* line ends in backslash, read continuation line */
720 if (!fgets( p, size - (p - buffer), file )) return buffer;
721 input_line++;
722 continue;
725 return buffer;
730 /*******************************************************************
731 * hash_filename
733 static unsigned int hash_filename( const char *name )
735 /* FNV-1 hash */
736 unsigned int ret = 2166136261u;
737 while (*name) ret = (ret * 16777619) ^ *name++;
738 return ret % HASH_SIZE;
742 /*******************************************************************
743 * add_file
745 static struct file *add_file( const char *name )
747 struct file *file = xmalloc( sizeof(*file) );
748 memset( file, 0, sizeof(*file) );
749 file->name = xstrdup( name );
750 return file;
754 /*******************************************************************
755 * add_dependency
757 static void add_dependency( struct file *file, const char *name, enum incl_type type )
759 if (file->deps_count >= file->deps_size)
761 file->deps_size *= 2;
762 if (file->deps_size < 16) file->deps_size = 16;
763 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
765 file->deps[file->deps_count].line = input_line;
766 file->deps[file->deps_count].type = type;
767 file->deps[file->deps_count].name = xstrdup( name );
768 file->deps_count++;
772 /*******************************************************************
773 * find_src_file
775 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
777 struct incl_file *file;
779 if (make == include_makefile)
781 unsigned int hash = hash_filename( name );
783 LIST_FOR_EACH_ENTRY( file, &global_includes[hash], struct incl_file, hash_entry )
784 if (!strcmp( name, file->name )) return file;
785 return NULL;
788 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
789 if (!strcmp( name, file->name )) return file;
790 return NULL;
793 /*******************************************************************
794 * find_include_file
796 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
798 struct incl_file *file;
800 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
802 const char *filename = file->filename;
803 if (!filename) continue;
804 if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
806 filename += strlen(make->obj_dir);
807 while (*filename == '/') filename++;
809 if (!strcmp( name, filename )) return file;
811 return NULL;
814 /*******************************************************************
815 * add_include
817 * Add an include file if it doesn't already exists.
819 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
820 const char *name, int line, enum incl_type type )
822 struct incl_file *include;
824 if (parent->files_count >= parent->files_size)
826 parent->files_size *= 2;
827 if (parent->files_size < 16) parent->files_size = 16;
828 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
831 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
832 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
833 goto found;
835 include = xmalloc( sizeof(*include) );
836 memset( include, 0, sizeof(*include) );
837 include->name = xstrdup(name);
838 include->included_by = parent;
839 include->included_line = line;
840 include->type = type;
841 include->use_msvcrt = parent->use_msvcrt;
842 list_add_tail( &make->includes, &include->entry );
843 found:
844 parent->files[parent->files_count++] = include;
845 return include;
849 /*******************************************************************
850 * add_generated_source
852 * Add a generated source file to the list.
854 static struct incl_file *add_generated_source( struct makefile *make, const char *name,
855 const char *filename, unsigned int arch )
857 struct incl_file *file = xmalloc( sizeof(*file) );
859 name = strmake( "%s%s", arch_dirs[arch], name );
860 memset( file, 0, sizeof(*file) );
861 file->file = add_file( name );
862 file->arch = arch;
863 file->name = xstrdup( name );
864 file->basename = xstrdup( filename ? filename : name );
865 file->filename = obj_dir_path( make, file->basename );
866 file->file->flags = FLAG_GENERATED;
867 file->use_msvcrt = is_using_msvcrt( make );
868 list_add_tail( &make->sources, &file->entry );
869 if (make == include_makefile)
871 unsigned int hash = hash_filename( name );
872 list_add_tail( &global_includes[hash], &file->hash_entry );
874 return file;
878 /*******************************************************************
879 * skip_spaces
881 static char *skip_spaces( const char *p )
883 while (*p == ' ' || *p == '\t') p++;
884 return (char *)p;
888 /*******************************************************************
889 * parse_include_directive
891 static void parse_include_directive( struct file *source, char *str )
893 char quote, *include, *p = skip_spaces( str );
895 if (*p != '\"' && *p != '<' ) return;
896 quote = *p++;
897 if (quote == '<') quote = '>';
898 include = p;
899 while (*p && (*p != quote)) p++;
900 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
901 *p = 0;
902 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
906 /*******************************************************************
907 * parse_pragma_directive
909 static void parse_pragma_directive( struct file *source, char *str )
911 char *flag, *p = str;
913 if (*p != ' ' && *p != '\t') return;
914 p = strtok( skip_spaces( p ), " \t" );
915 if (strcmp( p, "makedep" )) return;
917 while ((flag = strtok( NULL, " \t" )))
919 if (!strcmp( flag, "depend" ))
921 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
922 return;
924 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
926 if (strendswith( source->name, ".idl" ))
928 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
929 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
930 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
931 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
932 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
933 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
934 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
935 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
937 else if (strendswith( source->name, ".rc" ))
939 if (!strcmp( flag, "header" )) source->flags |= FLAG_RC_HEADER;
940 else if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
942 else if (strendswith( source->name, ".sfd" ))
944 if (!strcmp( flag, "font" ))
946 struct strarray *array = source->args;
948 if (!array)
950 source->args = array = xmalloc( sizeof(*array) );
951 *array = empty_strarray;
952 source->flags |= FLAG_SFD_FONTS;
954 strarray_add( array, xstrdup( strtok( NULL, "" )));
955 return;
958 else
960 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
961 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
967 /*******************************************************************
968 * parse_cpp_directive
970 static void parse_cpp_directive( struct file *source, char *str )
972 str = skip_spaces( str );
973 if (*str++ != '#') return;
974 str = skip_spaces( str );
976 if (!strncmp( str, "include", 7 ))
977 parse_include_directive( source, str + 7 );
978 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
979 parse_include_directive( source, str + 6 );
980 else if (!strncmp( str, "pragma", 6 ))
981 parse_pragma_directive( source, str + 6 );
985 /*******************************************************************
986 * parse_idl_file
988 static void parse_idl_file( struct file *source, FILE *file )
990 char *buffer, *include;
992 input_line = 0;
994 while ((buffer = get_line( file )))
996 char quote;
997 char *p = skip_spaces( buffer );
999 if (!strncmp( p, "importlib", 9 ))
1001 p = skip_spaces( p + 9 );
1002 if (*p++ != '(') continue;
1003 p = skip_spaces( p );
1004 if (*p++ != '"') continue;
1005 include = p;
1006 while (*p && (*p != '"')) p++;
1007 if (!*p) fatal_error( "malformed importlib directive\n" );
1008 *p = 0;
1009 add_dependency( source, include, INCL_IMPORTLIB );
1010 continue;
1013 if (!strncmp( p, "import", 6 ))
1015 p = skip_spaces( p + 6 );
1016 if (*p != '"') continue;
1017 include = ++p;
1018 while (*p && (*p != '"')) p++;
1019 if (!*p) fatal_error( "malformed import directive\n" );
1020 *p = 0;
1021 add_dependency( source, include, INCL_IMPORT );
1022 continue;
1025 /* check for #include inside cpp_quote */
1026 if (!strncmp( p, "cpp_quote", 9 ))
1028 p = skip_spaces( p + 9 );
1029 if (*p++ != '(') continue;
1030 p = skip_spaces( p );
1031 if (*p++ != '"') continue;
1032 if (*p++ != '#') continue;
1033 p = skip_spaces( p );
1034 if (strncmp( p, "include", 7 )) continue;
1035 p = skip_spaces( p + 7 );
1036 if (*p == '\\' && p[1] == '"')
1038 p += 2;
1039 quote = '"';
1041 else
1043 if (*p++ != '<' ) continue;
1044 quote = '>';
1046 include = p;
1047 while (*p && (*p != quote)) p++;
1048 if (!*p || (quote == '"' && p[-1] != '\\'))
1049 fatal_error( "malformed #include directive inside cpp_quote\n" );
1050 if (quote == '"') p--; /* remove backslash */
1051 *p = 0;
1052 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1053 continue;
1056 parse_cpp_directive( source, p );
1060 /*******************************************************************
1061 * parse_c_file
1063 static void parse_c_file( struct file *source, FILE *file )
1065 char *buffer;
1067 input_line = 0;
1068 while ((buffer = get_line( file )))
1070 parse_cpp_directive( source, buffer );
1075 /*******************************************************************
1076 * parse_rc_file
1078 static void parse_rc_file( struct file *source, FILE *file )
1080 char *buffer, *include;
1082 input_line = 0;
1083 while ((buffer = get_line( file )))
1085 char quote;
1086 char *p = skip_spaces( buffer );
1088 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1090 p = skip_spaces( p + 2 );
1091 if (strncmp( p, "@makedep:", 9 )) continue;
1092 p = skip_spaces( p + 9 );
1093 quote = '"';
1094 if (*p == quote)
1096 include = ++p;
1097 while (*p && *p != quote) p++;
1099 else
1101 include = p;
1102 while (*p && *p != ' ' && *p != '\t' && *p != '*') p++;
1104 if (!*p)
1105 fatal_error( "malformed makedep comment\n" );
1106 *p = 0;
1107 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1108 continue;
1111 parse_cpp_directive( source, buffer );
1116 /*******************************************************************
1117 * parse_in_file
1119 static void parse_in_file( struct file *source, FILE *file )
1121 char *p, *buffer;
1123 /* make sure it gets rebuilt when the version changes */
1124 add_dependency( source, "config.h", INCL_SYSTEM );
1126 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1128 input_line = 0;
1129 while ((buffer = get_line( file )))
1131 if (strncmp( buffer, ".TH", 3 )) continue;
1132 p = skip_spaces( buffer + 3 );
1133 if (!(p = strtok( p, " \t" ))) continue; /* program name */
1134 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1135 source->args = xstrdup( p );
1136 return;
1141 /*******************************************************************
1142 * parse_sfd_file
1144 static void parse_sfd_file( struct file *source, FILE *file )
1146 char *p, *eol, *buffer;
1148 input_line = 0;
1149 while ((buffer = get_line( file )))
1151 if (strncmp( buffer, "UComments:", 10 )) continue;
1152 p = buffer + 10;
1153 while (*p == ' ') p++;
1154 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1156 p++;
1157 buffer[strlen(buffer) - 1] = 0;
1159 while ((eol = strstr( p, "+AAoA" )))
1161 *eol = 0;
1162 p = skip_spaces( p );
1163 if (*p++ == '#')
1165 p = skip_spaces( p );
1166 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1168 p = eol + 5;
1170 p = skip_spaces( p );
1171 if (*p++ != '#') return;
1172 p = skip_spaces( p );
1173 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1174 return;
1179 static const struct
1181 const char *ext;
1182 void (*parse)( struct file *file, FILE *f );
1183 } parse_functions[] =
1185 { ".c", parse_c_file },
1186 { ".h", parse_c_file },
1187 { ".inl", parse_c_file },
1188 { ".l", parse_c_file },
1189 { ".m", parse_c_file },
1190 { ".rh", parse_c_file },
1191 { ".x", parse_c_file },
1192 { ".y", parse_c_file },
1193 { ".idl", parse_idl_file },
1194 { ".rc", parse_rc_file },
1195 { ".in", parse_in_file },
1196 { ".sfd", parse_sfd_file }
1199 /*******************************************************************
1200 * load_file
1202 static struct file *load_file( const char *name )
1204 struct file *file;
1205 FILE *f;
1206 unsigned int i, hash = hash_filename( name );
1208 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1209 if (!strcmp( name, file->name )) return file;
1211 if (!(f = fopen( name, "r" ))) return NULL;
1213 file = add_file( name );
1214 list_add_tail( &files[hash], &file->entry );
1215 input_file_name = file->name;
1216 input_line = 0;
1218 for (i = 0; i < ARRAY_SIZE(parse_functions); i++)
1220 if (!strendswith( name, parse_functions[i].ext )) continue;
1221 parse_functions[i].parse( file, f );
1222 break;
1225 fclose( f );
1226 input_file_name = NULL;
1228 return file;
1232 /*******************************************************************
1233 * open_include_path_file
1235 * Open a file from a directory on the include path.
1237 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1238 const char *name, char **filename )
1240 char *src_path = concat_paths( dir, name );
1241 struct file *ret = load_file( src_path );
1243 if (ret) *filename = src_path;
1244 return ret;
1248 /*******************************************************************
1249 * open_file_same_dir
1251 * Open a file in the same directory as the parent.
1253 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1255 char *src_path = replace_filename( parent->file->name, name );
1256 struct file *ret = load_file( src_path );
1258 if (ret) *filename = replace_filename( parent->filename, name );
1259 return ret;
1263 /*******************************************************************
1264 * open_same_dir_generated_file
1266 * Open a generated_file in the same directory as the parent.
1268 static struct file *open_same_dir_generated_file( const struct makefile *make,
1269 const struct incl_file *parent, struct incl_file *file,
1270 const char *ext, const char *src_ext )
1272 char *filename;
1273 struct file *ret = NULL;
1275 if (strendswith( file->name, ext ) &&
1276 (ret = open_file_same_dir( parent, replace_extension( file->name, ext, src_ext ), &filename )))
1278 file->sourcename = filename;
1279 file->filename = obj_dir_path( make, replace_filename( parent->name, file->name ));
1281 return ret;
1285 /*******************************************************************
1286 * open_local_file
1288 * Open a file in the source directory of the makefile.
1290 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1292 char *src_path = src_dir_path( make, path );
1293 struct file *ret = load_file( src_path );
1295 /* if not found, try parent dir */
1296 if (!ret && make->parent_dir)
1298 free( src_path );
1299 path = strmake( "%s/%s", make->parent_dir, path );
1300 src_path = src_dir_path( make, path );
1301 ret = load_file( src_path );
1304 if (ret) *filename = src_path;
1305 return ret;
1309 /*******************************************************************
1310 * open_local_generated_file
1312 * Open a generated file in the directory of the makefile.
1314 static struct file *open_local_generated_file( const struct makefile *make, struct incl_file *file,
1315 const char *ext, const char *src_ext )
1317 struct incl_file *include;
1319 if (strendswith( file->name, ext ) &&
1320 (include = find_src_file( make, replace_extension( file->name, ext, src_ext ) )))
1322 file->sourcename = include->filename;
1323 file->filename = obj_dir_path( make, file->name );
1324 return include->file;
1326 return NULL;
1330 /*******************************************************************
1331 * open_global_file
1333 * Open a file in the top-level source directory.
1335 static struct file *open_global_file( const char *path, char **filename )
1337 char *src_path = root_src_dir_path( path );
1338 struct file *ret = load_file( src_path );
1340 if (ret) *filename = src_path;
1341 return ret;
1345 /*******************************************************************
1346 * open_global_header
1348 * Open a file in the global include source directory.
1350 static struct file *open_global_header( const char *path, char **filename )
1352 struct incl_file *include = find_src_file( include_makefile, path );
1354 if (!include) return NULL;
1355 *filename = include->filename;
1356 return include->file;
1360 /*******************************************************************
1361 * open_global_generated_file
1363 * Open a generated file in the top-level source directory.
1365 static struct file *open_global_generated_file( const struct makefile *make, struct incl_file *file,
1366 const char *ext, const char *src_ext )
1368 struct incl_file *include;
1370 if (strendswith( file->name, ext ) &&
1371 (include = find_src_file( include_makefile, replace_extension( file->name, ext, src_ext ) )))
1373 file->sourcename = include->filename;
1374 file->filename = strmake( "include/%s", file->name );
1375 return include->file;
1377 return NULL;
1381 /*******************************************************************
1382 * open_src_file
1384 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1386 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1388 if (!file) fatal_perror( "open %s", pFile->name );
1389 return file;
1393 /*******************************************************************
1394 * find_importlib_module
1396 static struct makefile *find_importlib_module( const char *name )
1398 unsigned int i, len;
1400 for (i = 0; i < subdirs.count; i++)
1402 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1403 len = strlen(submakes[i]->obj_dir);
1404 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1405 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1407 return NULL;
1411 /*******************************************************************
1412 * open_include_file
1414 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1416 struct file *file = NULL;
1417 unsigned int i, len;
1419 errno = ENOENT;
1421 /* check for generated files */
1422 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1423 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1424 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1425 if (convert && rsvg && icotool)
1427 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1428 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1429 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1432 /* check for extra targets */
1433 if (strarray_exists( &make->extra_targets, pFile->name ))
1435 pFile->sourcename = src_dir_path( make, pFile->name );
1436 pFile->filename = obj_dir_path( make, pFile->name );
1437 return NULL;
1440 /* now try in source dir */
1441 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1443 /* check for global importlib (module dependency) */
1444 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1446 pFile->filename = pFile->name;
1447 return NULL;
1450 /* check for generated files in global includes */
1451 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1452 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1453 if (strendswith( pFile->name, "tmpl.h" ) &&
1454 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1456 /* check in global includes source dir */
1457 if ((file = open_global_header( pFile->name, &pFile->filename ))) return file;
1459 /* check in global msvcrt includes */
1460 if (pFile->use_msvcrt &&
1461 (file = open_global_header( strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1462 return file;
1464 /* now search in include paths */
1465 for (i = 0; i < make->include_paths.count; i++)
1467 const char *dir = make->include_paths.str[i];
1469 if (root_src_dir)
1471 len = strlen( root_src_dir );
1472 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1474 while (dir[len] == '/') len++;
1475 file = open_global_file( concat_paths( dir + len, pFile->name ), &pFile->filename );
1478 else
1480 if (*dir == '/') continue;
1481 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1483 if (!file) continue;
1484 pFile->is_external = 1;
1485 return file;
1488 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1490 /* try in src file directory */
1491 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1492 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1493 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1495 pFile->is_external = pFile->included_by->is_external;
1496 return file;
1499 if (make->extlib) return NULL; /* ignore missing files in external libs */
1501 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1502 perror( pFile->name );
1503 pFile = pFile->included_by;
1504 while (pFile && pFile->included_by)
1506 const char *parent = pFile->included_by->sourcename;
1507 if (!parent) parent = pFile->included_by->file->name;
1508 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1509 parent, pFile->included_line, pFile->name );
1510 pFile = pFile->included_by;
1512 exit(1);
1516 /*******************************************************************
1517 * add_all_includes
1519 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1521 unsigned int i;
1523 for (i = 0; i < file->deps_count; i++)
1525 switch (file->deps[i].type)
1527 case INCL_NORMAL:
1528 case INCL_IMPORT:
1529 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1530 break;
1531 case INCL_IMPORTLIB:
1532 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1533 break;
1534 case INCL_SYSTEM:
1535 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1536 break;
1537 case INCL_CPP_QUOTE:
1538 case INCL_CPP_QUOTE_SYSTEM:
1539 break;
1545 /*******************************************************************
1546 * parse_file
1548 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1550 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1552 if (!file) return;
1554 source->file = file;
1555 source->files_count = 0;
1556 source->files_size = file->deps_count;
1557 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1559 if (strendswith( file->name, ".m" )) file->flags |= FLAG_C_UNIX;
1560 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1561 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1563 if (source->sourcename)
1565 if (strendswith( source->sourcename, ".idl" ))
1567 unsigned int i;
1569 /* generated .h file always includes these */
1570 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1571 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1572 for (i = 0; i < file->deps_count; i++)
1574 switch (file->deps[i].type)
1576 case INCL_IMPORT:
1577 if (strendswith( file->deps[i].name, ".idl" ))
1578 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1579 file->deps[i].line, INCL_NORMAL );
1580 else
1581 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1582 break;
1583 case INCL_CPP_QUOTE:
1584 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1585 break;
1586 case INCL_CPP_QUOTE_SYSTEM:
1587 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1588 break;
1589 case INCL_NORMAL:
1590 case INCL_SYSTEM:
1591 case INCL_IMPORTLIB:
1592 break;
1595 return;
1597 if (strendswith( source->sourcename, ".y" ))
1598 return; /* generated .tab.h doesn't include anything */
1601 add_all_includes( make, source, file );
1605 /*******************************************************************
1606 * add_src_file
1608 * Add a source file to the list.
1610 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1612 struct incl_file *file = xmalloc( sizeof(*file) );
1614 memset( file, 0, sizeof(*file) );
1615 file->name = xstrdup(name);
1616 file->use_msvcrt = is_using_msvcrt( make );
1617 file->is_external = !!make->extlib;
1618 list_add_tail( &make->sources, &file->entry );
1619 if (make == include_makefile)
1621 unsigned int hash = hash_filename( name );
1622 list_add_tail( &global_includes[hash], &file->hash_entry );
1624 parse_file( make, file, 1 );
1625 return file;
1629 /*******************************************************************
1630 * open_input_makefile
1632 static FILE *open_input_makefile( const struct makefile *make )
1634 FILE *ret;
1636 if (make->obj_dir)
1637 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1638 else
1639 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1641 input_line = 0;
1642 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1643 return ret;
1647 /*******************************************************************
1648 * get_make_variable
1650 static const char *get_make_variable( const struct makefile *make, const char *name )
1652 const char *ret;
1654 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1655 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1656 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1657 return NULL;
1661 /*******************************************************************
1662 * get_expanded_make_variable
1664 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1666 const char *var;
1667 char *p, *end, *expand, *tmp;
1669 var = get_make_variable( make, name );
1670 if (!var) return NULL;
1672 p = expand = xstrdup( var );
1673 while ((p = strchr( p, '$' )))
1675 if (p[1] == '(')
1677 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1678 *end++ = 0;
1679 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1680 var = get_make_variable( make, p + 2 );
1681 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1682 /* switch to the new string */
1683 p = tmp + (p - expand);
1684 free( expand );
1685 expand = tmp;
1687 else if (p[1] == '{') /* don't expand ${} variables */
1689 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1690 p = end + 1;
1692 else if (p[1] == '$')
1694 p += 2;
1696 else fatal_error( "syntax error in '%s'\n", expand );
1699 /* consider empty variables undefined */
1700 p = skip_spaces( expand );
1701 if (*p) return expand;
1702 free( expand );
1703 return NULL;
1707 /*******************************************************************
1708 * get_expanded_make_var_array
1710 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1712 struct strarray ret = empty_strarray;
1713 char *value, *token;
1715 if ((value = get_expanded_make_variable( make, name )))
1716 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1717 strarray_add( &ret, token );
1718 return ret;
1722 /*******************************************************************
1723 * get_expanded_file_local_var
1725 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1726 const char *name )
1728 char *p, *var = strmake( "%s_%s", file, name );
1730 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1731 return get_expanded_make_var_array( make, var );
1735 /*******************************************************************
1736 * get_expanded_arch_var
1738 static char *get_expanded_arch_var( const struct makefile *make, const char *name, int arch )
1740 return get_expanded_make_variable( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1744 /*******************************************************************
1745 * get_expanded_arch_var_array
1747 static struct strarray get_expanded_arch_var_array( const struct makefile *make, const char *name, int arch )
1749 return get_expanded_make_var_array( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1753 /*******************************************************************
1754 * set_make_variable
1756 static int set_make_variable( struct strarray *array, const char *assignment )
1758 char *p, *name;
1760 p = name = xstrdup( assignment );
1761 while (isalnum(*p) || *p == '_') p++;
1762 if (name == p) return 0; /* not a variable */
1763 if (isspace(*p))
1765 *p++ = 0;
1766 p = skip_spaces( p );
1768 if (*p != '=') return 0; /* not an assignment */
1769 *p++ = 0;
1770 p = skip_spaces( p );
1772 strarray_set_value( array, name, p );
1773 return 1;
1777 /*******************************************************************
1778 * parse_makefile
1780 static struct makefile *parse_makefile( const char *path )
1782 char *buffer;
1783 FILE *file;
1784 struct makefile *make = xmalloc( sizeof(*make) );
1786 memset( make, 0, sizeof(*make) );
1787 make->obj_dir = path;
1788 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1789 if (path && !strcmp( path, "include" )) include_makefile = make;
1791 file = open_input_makefile( make );
1792 while ((buffer = get_line( file )))
1794 if (!strncmp( buffer, separator, strlen(separator) )) break;
1795 if (*buffer == '\t') continue; /* command */
1796 buffer = skip_spaces( buffer );
1797 if (*buffer == '#') continue; /* comment */
1798 set_make_variable( &make->vars, buffer );
1800 fclose( file );
1801 input_file_name = NULL;
1802 return make;
1806 /*******************************************************************
1807 * add_generated_sources
1809 static void add_generated_sources( struct makefile *make )
1811 unsigned int i, arch;
1812 struct incl_file *source, *next, *file, *dlldata = NULL;
1813 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1815 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1817 for (arch = 0; arch < archs.count; arch++)
1819 if (!is_multiarch( arch )) continue;
1820 if (source->file->flags & FLAG_IDL_CLIENT)
1822 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL, arch );
1823 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1824 add_all_includes( make, file, file->file );
1826 if (source->file->flags & FLAG_IDL_SERVER)
1828 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL, arch );
1829 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1830 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1831 add_all_includes( make, file, file->file );
1833 if (source->file->flags & FLAG_IDL_IDENT)
1835 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL, arch );
1836 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1837 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1838 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1839 add_all_includes( make, file, file->file );
1841 if (source->file->flags & FLAG_IDL_PROXY)
1843 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL, arch );
1844 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1845 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1846 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1847 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1848 add_all_includes( make, file, file->file );
1850 if (source->file->flags & FLAG_IDL_TYPELIB)
1852 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL, arch );
1854 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1856 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL, arch );
1858 if (source->file->flags & FLAG_IDL_REGISTER)
1860 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL, arch );
1864 /* now the arch-independent files */
1866 if ((source->file->flags & FLAG_IDL_PROXY) && !dlldata)
1868 dlldata = add_generated_source( make, "dlldata.o", "dlldata.c", 0 );
1869 add_dependency( dlldata->file, "objbase.h", INCL_NORMAL );
1870 add_dependency( dlldata->file, "rpcproxy.h", INCL_NORMAL );
1871 add_all_includes( make, dlldata, dlldata->file );
1873 if (source->file->flags & FLAG_IDL_HEADER)
1875 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1877 if (!source->file->flags && strendswith( source->name, ".idl" ))
1879 if (!strncmp( source->name, "wine/", 5 )) continue;
1880 source->file->flags = FLAG_IDL_HEADER | FLAG_INSTALL;
1881 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1883 if (strendswith( source->name, ".x" ))
1885 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL, 0 );
1887 if (strendswith( source->name, ".y" ))
1889 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL, 0 );
1890 /* steal the includes list from the source file */
1891 file->files_count = source->files_count;
1892 file->files_size = source->files_size;
1893 file->files = source->files;
1894 source->files_count = source->files_size = 0;
1895 source->files = NULL;
1897 if (strendswith( source->name, ".l" ))
1899 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL, 0 );
1900 /* steal the includes list from the source file */
1901 file->files_count = source->files_count;
1902 file->files_size = source->files_size;
1903 file->files = source->files;
1904 source->files_count = source->files_size = 0;
1905 source->files = NULL;
1907 if (strendswith( source->name, ".po" ))
1909 if (!make->disabled[0])
1910 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1912 if (strendswith( source->name, ".spec" ))
1914 char *obj = replace_extension( source->name, ".spec", "" );
1915 strarray_addall_uniq( &make->extra_imports,
1916 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1919 if (make->testdll)
1921 for (arch = 0; arch < archs.count; arch++)
1923 if (!is_multiarch( arch )) continue;
1924 file = add_generated_source( make, "testlist.o", "testlist.c", arch );
1925 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1926 add_all_includes( make, file, file->file );
1929 for (i = 0; i < objs.count; i++)
1931 /* default to .c for unknown extra object files */
1932 if (strendswith( objs.str[i], ".o" ))
1934 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ), 0);
1935 file->file->flags |= FLAG_C_UNIX;
1936 file->use_msvcrt = 0;
1938 else if (strendswith( objs.str[i], ".res" ))
1939 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL, 0 );
1940 else
1941 add_generated_source( make, objs.str[i], NULL, 0 );
1946 /*******************************************************************
1947 * create_dir
1949 static void create_dir( const char *dir )
1951 char *p, *path;
1953 p = path = xstrdup( dir );
1954 while ((p = strchr( p, '/' )))
1956 *p = 0;
1957 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1958 *p++ = '/';
1959 while (*p == '/') p++;
1961 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1962 free( path );
1966 /*******************************************************************
1967 * create_file_directories
1969 * Create the base directories of all the files.
1971 static void create_file_directories( const struct makefile *make, struct strarray files )
1973 struct strarray subdirs = empty_strarray;
1974 unsigned int i;
1975 char *dir;
1977 for (i = 0; i < files.count; i++)
1979 if (!strchr( files.str[i], '/' )) continue;
1980 dir = obj_dir_path( make, files.str[i] );
1981 *strrchr( dir, '/' ) = 0;
1982 strarray_add_uniq( &subdirs, dir );
1985 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1989 /*******************************************************************
1990 * output_filenames_obj_dir
1992 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1994 unsigned int i;
1996 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
2000 /*******************************************************************
2001 * get_dependencies
2003 static void get_dependencies( struct incl_file *file, struct incl_file *source )
2005 unsigned int i;
2007 if (!file->filename) return;
2009 if (file != source)
2011 if (file->owner == source) return; /* already processed */
2012 if (file->type == INCL_IMPORTLIB)
2014 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
2015 return; /* library is imported only when building a typelib */
2016 strarray_add( &source->importlibdeps, file->filename );
2018 else strarray_add( &source->dependencies, file->filename );
2019 file->owner = source;
2021 /* sanity checks */
2022 if (!strcmp( file->filename, "include/config.h" ) &&
2023 file != source->files[0] && !source->is_external)
2025 input_file_name = source->filename;
2026 input_line = 0;
2027 for (i = 0; i < source->file->deps_count; i++)
2029 if (!strcmp( source->file->deps[i].name, file->name ))
2030 input_line = source->file->deps[i].line;
2032 fatal_error( "%s must be included before other headers\n", file->name );
2036 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
2040 /*******************************************************************
2041 * get_local_dependencies
2043 * Get the local dependencies of a given target.
2045 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
2046 struct strarray targets )
2048 unsigned int i;
2049 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2051 for (i = 0; i < deps.count; i++)
2053 if (strarray_exists( &targets, deps.str[i] ))
2054 deps.str[i] = obj_dir_path( make, deps.str[i] );
2055 else
2056 deps.str[i] = src_dir_path( make, deps.str[i] );
2058 return deps;
2062 /*******************************************************************
2063 * get_static_lib
2065 * Find the makefile that builds the named static library (which may be an import lib).
2067 static struct makefile *get_static_lib( const char *name, unsigned int arch )
2069 unsigned int i;
2071 for (i = 0; i < subdirs.count; i++)
2073 if (submakes[i]->importlib && !strcmp( submakes[i]->importlib, name )) return submakes[i];
2074 if (!submakes[i]->staticlib) continue;
2075 if (submakes[i]->disabled[arch]) continue;
2076 if (strncmp( submakes[i]->staticlib, "lib", 3 )) continue;
2077 if (strncmp( submakes[i]->staticlib + 3, name, strlen(name) )) continue;
2078 if (strcmp( submakes[i]->staticlib + 3 + strlen(name), ".a" )) continue;
2079 return submakes[i];
2081 return NULL;
2085 /*******************************************************************
2086 * get_native_unix_lib
2088 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2090 if (!make->unixlib) return NULL;
2091 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2092 if (make->unixlib[strlen(name)] != '.') return NULL;
2093 return obj_dir_path( make, make->unixlib );
2097 /*******************************************************************
2098 * get_parent_makefile
2100 static struct makefile *get_parent_makefile( struct makefile *make )
2102 char *dir, *p;
2103 unsigned int i;
2105 if (!make->obj_dir) return NULL;
2106 dir = xstrdup( make->obj_dir );
2107 if (!(p = strrchr( dir, '/' ))) return NULL;
2108 *p = 0;
2109 for (i = 0; i < subdirs.count; i++)
2110 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2111 return NULL;
2115 /*******************************************************************
2116 * needs_delay_lib
2118 static int needs_delay_lib( const struct makefile *make, unsigned int arch )
2120 if (delay_load_flags[arch]) return 0;
2121 if (!make->importlib) return 0;
2122 return strarray_exists( &delay_import_libs, make->importlib );
2126 /*******************************************************************
2127 * add_unix_libraries
2129 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2131 struct strarray ret = empty_strarray;
2132 struct strarray all_libs = empty_strarray;
2133 unsigned int i, j;
2135 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2136 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
2138 for (i = 0; i < all_libs.count; i++)
2140 const char *lib = NULL;
2142 if (!strncmp( all_libs.str[i], "-l", 2 ))
2144 for (j = 0; j < subdirs.count; j++)
2146 if (make == submakes[j]) continue;
2147 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2150 if (lib)
2152 strarray_add( deps, lib );
2153 strarray_add( &ret, lib );
2155 else strarray_add( &ret, all_libs.str[i] );
2158 strarray_addall( &ret, libs );
2159 return ret;
2163 /*******************************************************************
2164 * is_crt_module
2166 static int is_crt_module( const char *file )
2168 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2172 /*******************************************************************
2173 * get_default_crt
2175 static const char *get_default_crt( const struct makefile *make )
2177 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2178 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2182 /*******************************************************************
2183 * get_crt_define
2185 static const char *get_crt_define( const struct makefile *make )
2187 const char *crt_dll = NULL;
2188 unsigned int i, version = 0;
2190 for (i = 0; i < make->imports.count; i++)
2192 if (!is_crt_module( make->imports.str[i] )) continue;
2193 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2194 crt_dll, make->imports.str[i] );
2195 crt_dll = make->imports.str[i];
2198 if (!crt_dll)
2200 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2201 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2203 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2204 sscanf( crt_dll, "msvcr%u", &version );
2205 return strmake( "-D_MSVCR_VER=%u", version );
2209 /*******************************************************************
2210 * get_default_imports
2212 static struct strarray get_default_imports( const struct makefile *make, struct strarray imports )
2214 struct strarray ret = empty_strarray;
2215 const char *crt_dll = get_default_crt( make );
2216 unsigned int i;
2218 for (i = 0; i < imports.count; i++)
2219 if (is_crt_module( imports.str[i] ))
2220 crt_dll = imports.str[i];
2222 strarray_add( &ret, "winecrt0" );
2223 if (crt_dll) strarray_add( &ret, crt_dll );
2225 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2226 strarray_add( &ret, "kernel" );
2228 strarray_add( &ret, "kernel32" );
2229 strarray_add( &ret, "ntdll" );
2230 return ret;
2233 enum import_type
2235 IMPORT_TYPE_DIRECT,
2236 IMPORT_TYPE_DELAYED,
2237 IMPORT_TYPE_DEFAULT,
2240 /*******************************************************************
2241 * add_import_libs
2243 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2244 struct strarray imports, enum import_type type, unsigned int arch )
2246 struct strarray ret = empty_strarray;
2247 unsigned int i;
2249 for (i = 0; i < imports.count; i++)
2251 const char *name = imports.str[i];
2252 struct makefile *submake;
2254 /* add crt import lib only when adding the default imports libs */
2255 if (is_crt_module( imports.str[i] ) && type != IMPORT_TYPE_DEFAULT) continue;
2257 if (name[0] == '-')
2259 switch (name[1])
2261 case 'L': strarray_add( &ret, name ); continue;
2262 case 'l': name += 2; break;
2263 default: continue;
2266 else name = get_base_name( name );
2268 if ((submake = get_static_lib( name, arch )))
2270 const char *ext = (type == IMPORT_TYPE_DELAYED && !delay_load_flags[arch]) ? ".delay.a" : ".a";
2271 const char *lib = obj_dir_path( submake, strmake( "%slib%s%s", arch_dirs[arch], name, ext ));
2272 strarray_add_uniq( deps, lib );
2273 strarray_add( &ret, lib );
2275 else strarray_add( &ret, strmake( "-l%s", name ));
2277 return ret;
2281 /*******************************************************************
2282 * add_install_rule
2284 static void add_install_rule( struct makefile *make, const char *target, unsigned int arch,
2285 const char *file, const char *dest )
2287 unsigned int i;
2289 if (make->disabled[arch]) return;
2291 for (i = 0; i < NB_INSTALL_RULES; i++)
2293 if (strarray_exists( &make->install[i], target ) ||
2294 strarray_exists( &top_install[i], make->obj_dir ) ||
2295 strarray_exists( &top_install[i], obj_dir_path( make, target )))
2297 strarray_add( &make->install_rules[i], file );
2298 strarray_add( &make->install_rules[i], dest );
2299 break;
2305 /*******************************************************************
2306 * get_include_install_path
2308 * Determine the installation path for a given include file.
2310 static const char *get_include_install_path( const char *name )
2312 if (!strncmp( name, "wine/", 5 )) return name + 5;
2313 if (!strncmp( name, "msvcrt/", 7 )) return name;
2314 return strmake( "windows/%s", name );
2318 /*******************************************************************
2319 * get_source_defines
2321 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2322 const char *obj )
2324 unsigned int i;
2325 struct strarray ret = empty_strarray;
2327 strarray_addall( &ret, make->include_args );
2328 if (source->use_msvcrt)
2330 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2331 for (i = 0; i < make->include_paths.count; i++)
2332 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2333 strarray_add( &ret, get_crt_define( make ));
2335 strarray_addall( &ret, make->define_args );
2336 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2337 if (source->file->flags & FLAG_C_UNIX) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2338 return ret;
2342 /*******************************************************************
2343 * remove_warning_flags
2345 static struct strarray remove_warning_flags( struct strarray flags )
2347 unsigned int i;
2348 struct strarray ret = empty_strarray;
2350 for (i = 0; i < flags.count; i++)
2351 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2352 strarray_add( &ret, flags.str[i] );
2353 return ret;
2357 /*******************************************************************
2358 * get_debug_file
2360 static const char *get_debug_file( struct makefile *make, const char *name, unsigned int arch )
2362 const char *debug_file = NULL;
2363 if (!debug_flags[arch]) return NULL;
2364 if (!strcmp( debug_flags[arch], "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2365 else if (!strncmp( debug_flags[arch], "split", 5 )) debug_file = strmake( "%s.debug", name );
2366 if (debug_file) strarray_add( &make->debug_files, debug_file );
2367 return debug_file;
2371 /*******************************************************************
2372 * cmd_prefix
2374 static const char *cmd_prefix( const char *cmd )
2376 if (!silent_rules) return "";
2377 return strmake( "$(quiet_%s)", cmd );
2381 /*******************************************************************
2382 * output_winegcc_command
2384 static void output_winegcc_command( struct makefile *make, unsigned int arch )
2386 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2387 output_filename( "--wine-objdir ." );
2388 if (tools_dir)
2390 output_filename( "--winebuild" );
2391 output_filename( tools_path( make, "winebuild" ));
2393 output_filenames( target_flags[arch] );
2394 if (arch) return;
2395 output_filename( "-mno-cygwin" );
2396 output_filenames( lddll_flags );
2400 /*******************************************************************
2401 * output_symlink_rule
2403 * Output a rule to create a symlink.
2405 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2407 const char *name = strrchr( link_name, '/' );
2408 char *dir = NULL;
2410 if (name)
2412 dir = xstrdup( link_name );
2413 dir[name - link_name] = 0;
2416 output( "\t%s", cmd_prefix( "LN" ));
2417 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2418 output( "rm -f %s && ", link_name );
2420 /* dest path with a directory needs special handling if ln -s isn't supported */
2421 if (dir && strcmp( ln_s, "ln -s" ))
2422 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2423 else
2424 output( "%s %s %s\n", ln_s, src_name, link_name );
2426 free( dir );
2430 /*******************************************************************
2431 * output_srcdir_symlink
2433 * Output rule to create a symlink back to the source directory, for source files
2434 * that are needed at run-time.
2436 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2438 char *src_file, *dst_file, *src_name;
2440 if (!make->src_dir) return;
2441 src_file = src_dir_path( make, obj );
2442 dst_file = obj_dir_path( make, obj );
2443 output( "%s: %s\n", dst_file, src_file );
2445 src_name = src_file;
2446 if (src_name[0] != '/' && make->obj_dir)
2447 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2449 output_symlink_rule( src_name, dst_file, 0 );
2450 strarray_add( &make->all_targets[0], obj );
2454 /*******************************************************************
2455 * output_install_commands
2457 static void output_install_commands( struct makefile *make, struct strarray files )
2459 unsigned int i, arch;
2460 char *install_sh = root_src_dir_path( "tools/install-sh" );
2462 for (i = 0; i < files.count; i += 2)
2464 const char *file = files.str[i];
2465 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2466 char type = *files.str[i + 1];
2468 switch (type)
2470 case '1': case '2': case '3': case '4': case '5':
2471 case '6': case '7': case '8': case '9': /* arch-dependent program */
2472 arch = type - '0';
2473 output( "\tSTRIPPROG=%s %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2474 strip_progs[arch], install_sh, obj_dir_path( make, file ), dest );
2475 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2476 break;
2477 case 'd': /* data file */
2478 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2479 install_sh, obj_dir_path( make, file ), dest );
2480 break;
2481 case 'D': /* data file in source dir */
2482 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2483 install_sh, src_dir_path( make, file ), dest );
2484 break;
2485 case '0': /* native arch program file */
2486 case 'p': /* program file */
2487 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2488 install_sh, obj_dir_path( make, file ), dest );
2489 break;
2490 case 's': /* script */
2491 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2492 install_sh, obj_dir_path( make, file ), dest );
2493 break;
2494 case 'S': /* script in source dir */
2495 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2496 install_sh, src_dir_path( make, file ), dest );
2497 break;
2498 case 't': /* script in tools dir */
2499 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2500 install_sh, tools_dir_path( make, files.str[i] ), dest );
2501 break;
2502 case 'y': /* symlink */
2503 output_symlink_rule( files.str[i], dest, 1 );
2504 break;
2505 default:
2506 assert(0);
2508 strarray_add( &make->uninstall_files, dest );
2513 /*******************************************************************
2514 * output_install_rules
2516 * Rules are stored as a (file,dest) pair of values.
2517 * The first char of dest indicates the type of install.
2519 static void output_install_rules( struct makefile *make, enum install_rules rules )
2521 unsigned int i;
2522 struct strarray files = make->install_rules[rules];
2523 struct strarray targets = empty_strarray;
2525 if (!files.count) return;
2527 for (i = 0; i < files.count; i += 2)
2529 const char *file = files.str[i];
2530 switch (*files.str[i + 1])
2532 case '0': case '1': case '2': case '3': case '4':
2533 case '5': case '6': case '7': case '8': case '9': /* arch-dependent program */
2534 case 'd': /* data file */
2535 case 'p': /* program file */
2536 case 's': /* script */
2537 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2538 break;
2539 case 't': /* script in tools dir */
2540 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2541 break;
2545 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, install_targets[rules] ));
2546 output_filenames( targets );
2547 output( "\n" );
2548 output_install_commands( make, files );
2549 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2550 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, install_targets[rules] ));
2554 static int cmp_string_length( const char **a, const char **b )
2556 int paths_a = 0, paths_b = 0;
2557 const char *p;
2559 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2560 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2561 if (paths_b != paths_a) return paths_b - paths_a;
2562 return strcmp( *a, *b );
2565 /*******************************************************************
2566 * get_removable_dirs
2568 * Retrieve a list of directories to try to remove after deleting the files.
2570 static struct strarray get_removable_dirs( struct strarray files )
2572 struct strarray dirs = empty_strarray;
2573 unsigned int i;
2575 for (i = 0; i < files.count; i++)
2577 char *dir = xstrdup( files.str[i] );
2578 while (strchr( dir, '/' ))
2580 *strrchr( dir, '/' ) = 0;
2581 strarray_add_uniq( &dirs, xstrdup(dir) );
2584 strarray_qsort( &dirs, cmp_string_length );
2585 return dirs;
2589 /*******************************************************************
2590 * output_uninstall_rules
2592 static void output_uninstall_rules( struct makefile *make )
2594 static const char *dirs_order[] =
2595 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2597 struct strarray uninstall_dirs;
2598 unsigned int i, j;
2600 if (!make->uninstall_files.count) return;
2601 output( "uninstall::\n" );
2602 output_rm_filenames( make->uninstall_files, "rm -f" );
2603 strarray_add_uniq( &make->phony_targets, "uninstall" );
2605 if (!subdirs.count) return;
2606 uninstall_dirs = get_removable_dirs( make->uninstall_files );
2607 output( "\t-rmdir" );
2608 for (i = 0; i < ARRAY_SIZE(dirs_order); i++)
2610 for (j = 0; j < uninstall_dirs.count; j++)
2612 if (!uninstall_dirs.str[j]) continue;
2613 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2614 continue;
2615 output_filename( uninstall_dirs.str[j] );
2616 uninstall_dirs.str[j] = NULL;
2619 for (j = 0; j < uninstall_dirs.count; j++)
2620 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2621 output( "\n" );
2625 /*******************************************************************
2626 * output_po_files
2628 static void output_po_files( struct makefile *make )
2630 const char *po_dir = src_dir_path( make, "po" );
2631 unsigned int i;
2633 if (linguas.count)
2635 for (i = 0; i < linguas.count; i++)
2636 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2637 output( ": %s/wine.pot\n", po_dir );
2638 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2639 cmd_prefix( "MSG" ), po_dir );
2640 output( "po/all:" );
2641 for (i = 0; i < linguas.count; i++)
2642 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2643 output( "\n" );
2645 output( "%s/wine.pot:", po_dir );
2646 output_filenames( make->pot_files );
2647 output( "\n" );
2648 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2649 output_filenames( make->pot_files );
2650 output( "\n" );
2651 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2655 /*******************************************************************
2656 * output_source_y
2658 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2660 /* add source file dependency for parallel makes */
2661 char *header = strmake( "%s.tab.h", obj );
2663 if (find_include_file( make, header ))
2665 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2666 output( "\t%s%s -o %s.tab.c -d %s\n",
2667 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2668 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2669 source->filename, obj_dir_path( make, header ));
2670 strarray_add( &make->clean_files, header );
2672 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2674 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2678 /*******************************************************************
2679 * output_source_l
2681 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2683 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2684 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2688 /*******************************************************************
2689 * output_source_h
2691 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2693 if (source->file->flags & FLAG_GENERATED)
2694 strarray_add( &make->all_targets[0], source->name );
2695 else if ((source->file->flags & FLAG_INSTALL) || strncmp( source->name, "wine/", 5 ))
2696 add_install_rule( make, source->name, 0, source->name,
2697 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2701 /*******************************************************************
2702 * output_source_rc
2704 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2706 struct strarray defines = get_source_defines( make, source, obj );
2707 const char *po_dir = NULL, *res_file = strmake( "%s.res", obj );
2708 unsigned int i, arch;
2710 if (source->file->flags & FLAG_RC_HEADER) return;
2711 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2712 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2713 if (!make->testdll || !find_src_file( make, strmake( "%s.spec", obj ) )) /* RC is for a TESTDLL */
2715 for (arch = 0; arch < archs.count; arch++)
2716 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2718 else strarray_add( &make->clean_files, res_file );
2720 if (source->file->flags & FLAG_RC_PO)
2722 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2723 output( "%s.pot ", obj_dir_path( make, obj ) );
2725 output( "%s: %s", obj_dir_path( make, res_file ), source->filename );
2726 output_filename( tools_path( make, "wrc" ));
2727 if (make->src_dir) output_filename( "nls/locale.nls" );
2728 output_filenames( source->dependencies );
2729 output( "\n" );
2730 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2731 if (make->is_win16) output_filename( "-m16" );
2732 output_filename( "--nostdinc" );
2733 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2734 output_filenames( defines );
2735 output_filename( source->filename );
2736 output( "\n" );
2737 if (po_dir)
2739 output( "%s:", obj_dir_path( make, res_file ));
2740 for (i = 0; i < linguas.count; i++)
2741 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2742 output( "\n" );
2747 /*******************************************************************
2748 * output_source_mc
2750 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2752 unsigned int i, arch;
2753 char *obj_path = obj_dir_path( make, obj );
2754 char *res_file = strmake( "%s.res", obj );
2756 for (arch = 0; arch < archs.count; arch++)
2757 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2758 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2759 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2760 output_filename( tools_path( make, "wmc" ));
2761 output_filenames( source->dependencies );
2762 if (make->src_dir) output_filename( "nls/locale.nls" );
2763 output( "\n" );
2764 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2765 if (linguas.count)
2767 output_filename( "--po-dir=po" );
2768 output( "\n" );
2769 output( "%s.res:", obj_dir_path( make, obj ));
2770 for (i = 0; i < linguas.count; i++)
2771 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2773 output( "\n" );
2777 /*******************************************************************
2778 * output_source_res
2780 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2782 if (make->disabled[source->arch]) return;
2783 strarray_add( &make->res_files[source->arch], source->name );
2787 /*******************************************************************
2788 * output_source_idl
2790 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2792 struct strarray defines = get_source_defines( make, source, obj );
2793 struct strarray headers = empty_strarray;
2794 struct strarray deps = empty_strarray;
2795 struct strarray multiarch_targets[MAX_ARCHS] = { empty_strarray };
2796 const char *dest;
2797 unsigned int i, arch;
2799 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2800 if (!source->file->flags) return;
2802 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2803 if (source->file->flags & FLAG_INSTALL)
2805 add_install_rule( make, source->name, 0, xstrdup( source->name ),
2806 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2807 if (source->file->flags & FLAG_IDL_HEADER)
2808 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2809 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2811 if (source->file->flags & FLAG_IDL_HEADER)
2813 dest = strmake( "%s.h", obj );
2814 strarray_add( &headers, dest );
2815 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2818 for (i = 0; i < ARRAY_SIZE(idl_outputs); i++)
2820 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2821 for (arch = 0; arch < archs.count; arch++)
2823 if (!is_multiarch( arch )) continue;
2824 if (make->disabled[arch]) continue;
2825 dest = strmake( "%s%s%s", arch_dirs[arch], obj, idl_outputs[i].ext );
2826 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2827 strarray_add( &multiarch_targets[arch], dest );
2831 for (arch = 0; arch < archs.count; arch++)
2833 struct strarray arch_deps = empty_strarray;
2835 if (!arch) strarray_addall( &arch_deps, headers );
2836 strarray_addall( &arch_deps, multiarch_targets[arch] );
2837 if (!arch_deps.count) continue;
2838 output_filenames_obj_dir( make, arch_deps );
2839 output( ":\n" );
2840 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2841 output_filenames( target_flags[arch] );
2842 output_filename( "--nostdinc" );
2843 output_filename( "-Ldlls/\\*" );
2844 output_filenames( defines );
2845 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2846 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2847 output_filename( source->filename );
2848 output( "\n" );
2849 strarray_addall( &deps, arch_deps );
2852 if (deps.count)
2854 output_filenames_obj_dir( make, deps );
2855 output( ":" );
2856 output_filename( tools_path( make, "widl" ));
2857 output_filename( source->filename );
2858 output_filenames( source->dependencies );
2859 output( "\n" );
2862 if (source->importlibdeps.count)
2864 for (arch = 0; arch < archs.count; arch++)
2866 if (!multiarch_targets[arch].count) continue;
2867 output_filenames_obj_dir( make, multiarch_targets[arch] );
2868 output( ":" );
2869 for (i = 0; i < source->importlibdeps.count; i++)
2871 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2872 const char *module = strmake( "%s%s", arch_pe_dirs[arch], submake->module );
2873 output_filename( obj_dir_path( submake, module ));
2875 output( "\n" );
2881 /*******************************************************************
2882 * output_source_x
2884 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2886 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2887 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2888 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2889 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2890 if (source->file->flags & FLAG_INSTALL)
2892 add_install_rule( make, source->name, 0, source->name,
2893 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2894 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2895 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2900 /*******************************************************************
2901 * output_source_sfd
2903 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2905 unsigned int i;
2906 char *ttf_obj = strmake( "%s.ttf", obj );
2907 char *ttf_file = src_dir_path( make, ttf_obj );
2909 if (fontforge && !make->src_dir)
2911 output( "%s: %s\n", ttf_file, source->filename );
2912 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2913 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2914 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2915 strarray_add( &make->maintainerclean_files, ttf_obj );
2917 if (source->file->flags & FLAG_INSTALL)
2919 add_install_rule( make, source->name, 0, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2920 output_srcdir_symlink( make, ttf_obj );
2923 if (source->file->flags & FLAG_SFD_FONTS)
2925 struct strarray *array = source->file->args;
2927 for (i = 0; i < array->count; i++)
2929 char *font = strtok( xstrdup(array->str[i]), " \t" );
2930 char *args = strtok( NULL, "" );
2932 strarray_add( &make->all_targets[0], xstrdup( font ));
2933 output( "%s: %s %s\n", obj_dir_path( make, font ),
2934 tools_path( make, "sfnt2fon" ), ttf_file );
2935 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2936 tools_path( make, "sfnt2fon" ), ttf_file, args );
2937 add_install_rule( make, source->name, 0, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2943 /*******************************************************************
2944 * output_source_svg
2946 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2948 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2949 unsigned int i;
2951 if (convert && rsvg && icotool)
2953 for (i = 0; images[i]; i++)
2954 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2956 if (images[i])
2958 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2959 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2960 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2961 root_src_dir_path( "tools/buildimage" ), source->filename );
2962 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2968 /*******************************************************************
2969 * output_source_nls
2971 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2973 add_install_rule( make, source->name, 0, source->name,
2974 strmake( "D$(nlsdir)/%s", source->name ));
2975 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2979 /*******************************************************************
2980 * output_source_desktop
2982 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2984 add_install_rule( make, source->name, 0, source->name,
2985 strmake( "D$(datadir)/applications/%s", source->name ));
2989 /*******************************************************************
2990 * output_source_po
2992 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2994 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2995 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
2996 strarray_add( &make->all_targets[0], strmake( "%s.mo", obj ));
3000 /*******************************************************************
3001 * output_source_in
3003 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
3005 unsigned int i;
3007 if (make == include_makefile) return; /* ignore generated includes */
3008 if (strendswith( obj, ".man" ) && source->file->args)
3010 struct strarray symlinks;
3011 char *dir, *dest = replace_extension( obj, ".man", "" );
3012 char *lang = strchr( dest, '.' );
3013 char *section = source->file->args;
3014 if (lang)
3016 *lang++ = 0;
3017 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3019 else dir = strmake( "$(mandir)/man%s", section );
3020 add_install_rule( make, dest, 0, obj, strmake( "d%s/%s.%s", dir, dest, section ));
3021 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3022 for (i = 0; i < symlinks.count; i++)
3023 add_install_rule( make, symlinks.str[i], 0, strmake( "%s.%s", dest, section ),
3024 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3025 free( dest );
3026 free( dir );
3028 strarray_add( &make->in_files, obj );
3029 strarray_add( &make->all_targets[0], obj );
3030 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3031 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
3032 output( "%s:", obj_dir_path( make, obj ));
3033 output_filenames( source->dependencies );
3034 output( "\n" );
3035 add_install_rule( make, obj, 0, obj, strmake( "d$(datadir)/wine/%s", obj ));
3039 /*******************************************************************
3040 * output_source_spec
3042 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3044 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3045 struct strarray dll_flags = empty_strarray;
3046 struct strarray default_imports = empty_strarray;
3047 struct strarray all_libs, dep_libs;
3048 const char *dll_name, *obj_name, *res_name, *output_rsrc, *output_file, *debug_file;
3049 unsigned int arch;
3051 if (!imports.count) imports = make->imports;
3052 strarray_addall( &dll_flags, make->extradllflags );
3053 strarray_addall( &dll_flags, get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" ));
3054 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3056 for (arch = 0; arch < archs.count; arch++)
3058 if (!is_multiarch( arch )) continue;
3059 all_libs = dep_libs = empty_strarray;
3060 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ) );
3061 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3062 if (!arch) strarray_addall( &all_libs, libs );
3063 dll_name = arch_module_name( strmake( "%s.dll", obj ), arch );
3064 obj_name = obj_dir_path( make, strmake( "%s%s.o", arch_dirs[arch], obj ));
3065 output_file = obj_dir_path( make, dll_name );
3066 output_rsrc = strmake( "%s.res", dll_name );
3068 if (!find_src_file( make, strmake( "%s.rc", obj ) )) res_name = NULL;
3069 else res_name = obj_dir_path( make, strmake( "%s.res", obj ) );
3071 strarray_add( &make->clean_files, dll_name );
3072 strarray_add( &make->res_files[arch], output_rsrc );
3073 output( "%s:", obj_dir_path( make, output_rsrc ));
3074 output_filename( output_file );
3075 output_filename( tools_path( make, "wrc" ));
3076 output( "\n" );
3077 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
3078 tools_path( make, "wrc" ));
3080 output( "%s:", output_file );
3081 output_filename( source->filename );
3082 output_filename( obj_name );
3083 if (res_name) output_filename( res_name );
3084 output_filenames( dep_libs );
3085 output_filename( tools_path( make, "winebuild" ));
3086 output_filename( tools_path( make, "winegcc" ));
3087 output( "\n" );
3088 output_winegcc_command( make, arch );
3089 output_filename( "-s" );
3090 output_filenames( dll_flags );
3091 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3092 output_filename( "-shared" );
3093 output_filename( source->filename );
3094 output_filename( obj_name );
3095 if (res_name) output_filename( res_name );
3096 if ((debug_file = get_debug_file( make, dll_name, arch )))
3097 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3098 output_filenames( all_libs );
3099 output_filename( arch_make_variable( "LDFLAGS", arch ));
3100 output( "\n" );
3105 /*******************************************************************
3106 * output_source_one_arch
3108 static void output_source_one_arch( struct makefile *make, struct incl_file *source, const char *obj,
3109 struct strarray defines, struct strarray *targets,
3110 unsigned int arch, int is_dll_src )
3112 const char *obj_name;
3114 if (make->disabled[arch] && !(source->file->flags & FLAG_C_IMPLIB)) return;
3116 if (arch)
3118 if (source->file->flags & FLAG_C_UNIX) return;
3119 if (!is_using_msvcrt( make ) && !make->staticlib && !(source->file->flags & FLAG_C_IMPLIB)) return;
3121 else if (source->file->flags & FLAG_C_UNIX)
3123 if (!*dll_ext) return;
3125 else if (archs.count > 1 && is_using_msvcrt( make ) &&
3126 !(source->file->flags & FLAG_C_IMPLIB) &&
3127 (!make->staticlib || make->extlib)) return;
3129 obj_name = strmake( "%s%s.o", source->arch ? "" : arch_dirs[arch], obj );
3130 strarray_add( targets, obj_name );
3132 if (source->file->flags & FLAG_C_UNIX)
3133 strarray_add( &make->unixobj_files, obj_name );
3134 else if (source->file->flags & FLAG_C_IMPLIB)
3135 strarray_add( &make->implib_files[arch], obj_name );
3136 else if (!is_dll_src)
3137 strarray_add( &make->object_files[arch], obj_name );
3138 else
3139 strarray_add( &make->clean_files, obj_name );
3141 output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename );
3142 output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), arch_make_variable( "CC", arch ), source->filename );
3143 output_filenames( defines );
3144 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3145 output_filenames( make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] );
3146 if (!arch)
3148 if (source->file->flags & FLAG_C_UNIX)
3150 output_filenames( unix_dllflags );
3152 else if (make->module || make->testdll)
3154 output_filenames( dll_flags );
3155 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3156 if (!*dll_ext && make->module && is_crt_module( make->module ))
3157 output_filename( "-fno-builtin" );
3160 else
3162 if (make->module && is_crt_module( make->module )) output_filename( "-fno-builtin" );
3165 output_filenames( cpp_flags );
3166 output_filename( arch_make_variable( "CFLAGS", arch ));
3167 output( "\n" );
3169 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3170 !(source->file->flags & FLAG_GENERATED))
3172 const char *ok_file, *test_exe;
3174 ok_file = strmake( "%s%s.ok", arch_dirs[arch], obj );
3175 test_exe = replace_extension( make->testdll, ".dll", "_test.exe" );
3176 strarray_add( &make->ok_files[arch], ok_file );
3177 output( "%s:\n", obj_dir_path( make, ok_file ));
3178 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s %s && touch $@\n",
3179 cmd_prefix( "TEST" ),
3180 root_src_dir_path( "tools/runtest" ), make->testdll,
3181 obj_dir_path( make, arch_module_name( test_exe, arch )), obj );
3186 /*******************************************************************
3187 * output_source_default
3189 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3191 struct strarray defines = get_source_defines( make, source, obj );
3192 struct strarray targets = empty_strarray;
3193 int is_dll_src = (make->testdll && strendswith( source->name, ".c" ) &&
3194 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3195 unsigned int arch;
3197 for (arch = 0; arch < archs.count; arch++)
3198 if (!source->arch || source->arch == arch)
3199 output_source_one_arch( make, source, obj, defines, &targets, arch, is_dll_src );
3201 if (source->file->flags & FLAG_GENERATED)
3203 if (!make->testdll || !strendswith( source->filename, "testlist.c" ))
3204 strarray_add( &make->clean_files, source->basename );
3206 else
3208 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ))
3209 strarray_add( &make->test_files, obj );
3212 output_filenames_obj_dir( make, targets );
3213 output( ":" );
3214 output_filenames( source->dependencies );
3215 output( "\n" );
3219 /* dispatch table to output rules for a single source file */
3220 static const struct
3222 const char *ext;
3223 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3224 } output_source_funcs[] =
3226 { "y", output_source_y },
3227 { "l", output_source_l },
3228 { "h", output_source_h },
3229 { "rh", output_source_h },
3230 { "inl", output_source_h },
3231 { "rc", output_source_rc },
3232 { "mc", output_source_mc },
3233 { "res", output_source_res },
3234 { "idl", output_source_idl },
3235 { "sfd", output_source_sfd },
3236 { "svg", output_source_svg },
3237 { "nls", output_source_nls },
3238 { "desktop", output_source_desktop },
3239 { "po", output_source_po },
3240 { "in", output_source_in },
3241 { "x", output_source_x },
3242 { "spec", output_source_spec },
3243 { NULL, output_source_default }
3247 /*******************************************************************
3248 * output_fake_module
3250 static void output_fake_module( struct makefile *make )
3252 unsigned int arch = 0; /* fake modules are always native */
3253 const char *spec_file = NULL, *name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3255 if (make->disabled[arch]) return;
3257 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3259 strarray_add( &make->all_targets[arch], name );
3260 add_install_rule( make, make->module, arch, name, strmake( "d$(dlldir)/%s", name ));
3262 output( "%s:", obj_dir_path( make, name ));
3263 if (spec_file) output_filename( spec_file );
3264 output_filenames_obj_dir( make, make->res_files[arch] );
3265 output_filename( tools_path( make, "winebuild" ));
3266 output_filename( tools_path( make, "winegcc" ));
3267 output( "\n" );
3268 output_winegcc_command( make, arch );
3269 output_filename( "-Wb,--fake-module" );
3270 if (spec_file)
3272 output_filename( "-shared" );
3273 output_filename( spec_file );
3275 output_filenames( make->extradllflags );
3276 output_filenames_obj_dir( make, make->res_files[arch] );
3277 output( "\n" );
3281 /*******************************************************************
3282 * output_module
3284 static void output_module( struct makefile *make, unsigned int arch )
3286 struct strarray default_imports = empty_strarray;
3287 struct strarray all_libs = empty_strarray;
3288 struct strarray dep_libs = empty_strarray;
3289 struct strarray imports = make->imports;
3290 const char *module_name;
3291 const char *debug_file;
3292 char *spec_file = NULL;
3293 unsigned int i;
3295 if (make->disabled[arch]) return;
3297 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3299 if (!make->data_only)
3301 module_name = arch_module_name( make->module, arch );
3303 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3305 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ));
3306 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, IMPORT_TYPE_DELAYED, arch ));
3307 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3308 if (!arch) strarray_addall( &all_libs, libs );
3310 if (delay_load_flags[arch])
3312 for (i = 0; i < make->delayimports.count; i++)
3314 struct makefile *import = get_static_lib( make->delayimports.str[i], arch );
3315 if (import) strarray_add( &all_libs, strmake( "%s%s", delay_load_flags[arch], import->module ));
3319 else module_name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3321 strarray_add( &make->all_targets[arch], module_name );
3322 if (make->data_only)
3323 add_install_rule( make, make->module, arch, module_name,
3324 strmake( "d$(dlldir)/%s%s", arch_pe_dirs[arch], make->module ));
3325 else
3326 add_install_rule( make, make->module, arch, module_name,
3327 strmake( "%c%s%s%s", '0' + arch, arch_install_dirs[arch], make->module,
3328 arch ? "" : dll_ext ));
3330 output( "%s:", obj_dir_path( make, module_name ));
3331 if (spec_file) output_filename( spec_file );
3332 output_filenames_obj_dir( make, make->object_files[arch] );
3333 output_filenames_obj_dir( make, make->res_files[arch] );
3334 output_filenames( dep_libs );
3335 output_filename( tools_path( make, "winebuild" ));
3336 output_filename( tools_path( make, "winegcc" ));
3337 output( "\n" );
3338 output_winegcc_command( make, arch );
3339 if (arch) output_filename( "-Wl,--wine-builtin" );
3340 if (spec_file)
3342 output_filename( "-shared" );
3343 output_filename( spec_file );
3345 output_filenames( make->extradllflags );
3346 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3347 output_filenames_obj_dir( make, make->object_files[arch] );
3348 output_filenames_obj_dir( make, make->res_files[arch] );
3349 debug_file = get_debug_file( make, module_name, arch );
3350 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3351 output_filenames( all_libs );
3352 output_filename( arch_make_variable( "LDFLAGS", arch ));
3353 output( "\n" );
3355 if (!make->data_only && !arch && *dll_ext) output_fake_module( make );
3359 /*******************************************************************
3360 * output_import_lib
3362 static void output_import_lib( struct makefile *make, unsigned int arch )
3364 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3365 const char *name = strmake( "%slib%s.a", arch_dirs[arch], make->importlib );
3367 strarray_add( &make->clean_files, name );
3368 if (needs_delay_lib( make, arch ))
3370 const char *delay_name = replace_extension( name, ".a", ".delay.a" );
3371 strarray_add( &make->clean_files, delay_name );
3372 output( "%s ", obj_dir_path( make, delay_name ));
3374 output( "%s: %s %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ), spec_file );
3375 output_filenames_obj_dir( make, make->implib_files[arch] );
3376 output( "\n" );
3377 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3378 if (!delay_load_flags[arch]) output_filename( "--without-dlltool" );
3379 output_filenames( target_flags[arch] );
3380 if (make->is_win16) output_filename( "-m16" );
3381 output_filename( "--export" );
3382 output_filename( spec_file );
3383 output_filenames_obj_dir( make, make->implib_files[arch] );
3384 output( "\n" );
3386 if (!arch && is_native_arch_disabled( make )) return;
3388 add_install_rule( make, make->importlib, arch, name,
3389 strmake( "d%slib%s.a", arch_install_dirs[arch], make->importlib ));
3393 /*******************************************************************
3394 * output_unix_lib
3396 static void output_unix_lib( struct makefile *make )
3398 struct strarray unix_deps = empty_strarray;
3399 struct strarray unix_libs = add_unix_libraries( make, &unix_deps );
3400 unsigned int arch = 0; /* unix libs are always native */
3402 if (make->disabled[arch]) return;
3404 strarray_add( &make->all_targets[arch], make->unixlib );
3405 add_install_rule( make, make->module, arch, make->unixlib,
3406 strmake( "p%s%s", arch_install_dirs[arch], make->unixlib ));
3407 output( "%s:", obj_dir_path( make, make->unixlib ));
3408 output_filenames_obj_dir( make, make->unixobj_files );
3409 output_filenames( unix_deps );
3410 output( "\n" );
3411 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3412 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3413 output_filenames_obj_dir( make, make->unixobj_files );
3414 output_filenames( unix_libs );
3415 output_filename( "$(LDFLAGS)" );
3416 output( "\n" );
3420 /*******************************************************************
3421 * output_static_lib
3423 static void output_static_lib( struct makefile *make, unsigned int arch )
3425 const char *name = strmake( "%s%s", arch_dirs[arch], make->staticlib );
3427 strarray_add( &make->clean_files, name );
3428 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3429 output_filenames_obj_dir( make, make->object_files[arch] );
3430 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3431 output( "\n" );
3432 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3433 output_filenames( target_flags[arch] );
3434 output_filenames_obj_dir( make, make->object_files[arch] );
3435 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3436 output( "\n" );
3437 if (!make->extlib)
3438 add_install_rule( make, make->staticlib, arch, name,
3439 strmake( "d%s%s", arch_install_dirs[arch], make->staticlib ));
3443 /*******************************************************************
3444 * output_test_module
3446 static void output_test_module( struct makefile *make, unsigned int arch )
3448 char *basemodule = replace_extension( make->testdll, ".dll", "" );
3449 char *stripped = arch_module_name( strmake( "%s_test-stripped.exe", basemodule ), arch );
3450 char *testmodule = arch_module_name( strmake( "%s_test.exe", basemodule ), arch );
3451 struct strarray default_imports = get_default_imports( make, make->imports );
3452 struct strarray dep_libs = empty_strarray;
3453 struct strarray all_libs = empty_strarray;
3454 struct makefile *parent = get_parent_makefile( make );
3455 const char *debug_file;
3457 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, IMPORT_TYPE_DIRECT, arch ) );
3458 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3460 strarray_add( &make->all_targets[arch], testmodule );
3461 strarray_add( &make->clean_files, stripped );
3462 output( "%s:\n", obj_dir_path( make, testmodule ));
3463 output_winegcc_command( make, arch );
3464 output_filenames( make->extradllflags );
3465 output_filenames_obj_dir( make, make->object_files[arch] );
3466 output_filenames_obj_dir( make, make->res_files[arch] );
3467 if ((debug_file = get_debug_file( make, testmodule, arch )))
3468 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3469 output_filenames( all_libs );
3470 output_filename( arch_make_variable( "LDFLAGS", arch ));
3471 output( "\n" );
3472 output( "%s:\n", obj_dir_path( make, stripped ));
3473 output_winegcc_command( make, arch );
3474 output_filename( "-s" );
3475 output_filename( strmake( "-Wb,-F,%s_test.exe", basemodule ));
3476 output_filenames( make->extradllflags );
3477 output_filenames_obj_dir( make, make->object_files[arch] );
3478 output_filenames_obj_dir( make, make->res_files[arch] );
3479 output_filenames( all_libs );
3480 output_filename( arch_make_variable( "LDFLAGS", arch ));
3481 output( "\n" );
3482 output( "%s %s:", obj_dir_path( make, testmodule ), obj_dir_path( make, stripped ));
3483 output_filenames_obj_dir( make, make->object_files[arch] );
3484 output_filenames_obj_dir( make, make->res_files[arch] );
3485 output_filenames( dep_libs );
3486 output_filename( tools_path( make, "winebuild" ));
3487 output_filename( tools_path( make, "winegcc" ));
3488 output( "\n" );
3490 output( "programs/winetest/%s%s_test.res: %s\n", arch_dirs[arch], basemodule,
3491 obj_dir_path( make, stripped ));
3492 output( "\t%secho \"%s_test.exe TESTRES \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3493 basemodule, obj_dir_path( make, stripped ), tools_path( make, "wrc" ));
3495 if (make->disabled[arch] || (parent && parent->disabled[arch]))
3497 make->ok_files[arch] = empty_strarray;
3498 return;
3500 output_filenames_obj_dir( make, make->ok_files[arch] );
3501 output( ": %s", obj_dir_path( make, testmodule ));
3502 if (parent)
3504 char *parent_module = arch_module_name( make->testdll, arch );
3505 output_filename( obj_dir_path( parent, parent_module ));
3506 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3508 output( "\n" );
3509 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3510 output_filenames_obj_dir( make, make->ok_files[arch] );
3511 output( "\n" );
3512 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3513 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3514 output( "%s::\n", obj_dir_path( make, "testclean" ));
3515 output( "\trm -f" );
3516 output_filenames_obj_dir( make, make->ok_files[arch] );
3517 output( "\n" );
3518 strarray_addall( &make->clean_files, make->ok_files[arch] );
3519 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3523 /*******************************************************************
3524 * output_programs
3526 static void output_programs( struct makefile *make )
3528 unsigned int i, j;
3529 unsigned int arch = 0; /* programs are always native */
3531 for (i = 0; i < make->programs.count; i++)
3533 char *program_installed = NULL;
3534 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3535 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3536 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3537 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3538 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3540 if (!objs.count) objs = make->object_files[arch];
3541 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3543 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3544 strarray_addall( &all_libs, libs );
3547 output( "%s:", obj_dir_path( make, program ) );
3548 output_filenames_obj_dir( make, objs );
3549 output_filenames( deps );
3550 output( "\n" );
3551 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3552 output_filenames_obj_dir( make, objs );
3553 output_filenames( all_libs );
3554 output_filename( "$(LDFLAGS)" );
3555 output( "\n" );
3556 strarray_add( &make->all_targets[arch], program );
3558 for (j = 0; j < symlinks.count; j++)
3560 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3561 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3563 strarray_addall( &make->all_targets[arch], symlinks );
3565 add_install_rule( make, program, arch, program_installed ? program_installed : program,
3566 strmake( "p$(bindir)/%s", program ));
3567 for (j = 0; j < symlinks.count; j++)
3568 add_install_rule( make, symlinks.str[j], arch, program,
3569 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3574 /*******************************************************************
3575 * output_subdirs
3577 static void output_subdirs( struct makefile *make )
3579 struct strarray all_targets = empty_strarray;
3580 struct strarray makefile_deps = empty_strarray;
3581 struct strarray clean_files = empty_strarray;
3582 struct strarray testclean_files = empty_strarray;
3583 struct strarray distclean_files = empty_strarray;
3584 struct strarray distclean_dirs = empty_strarray;
3585 struct strarray dependencies = empty_strarray;
3586 struct strarray install_deps[NB_INSTALL_RULES] = { empty_strarray };
3587 struct strarray tooldeps_deps = empty_strarray;
3588 struct strarray buildtest_deps = empty_strarray;
3589 unsigned int i, j, arch;
3591 strarray_addall( &clean_files, make->clean_files );
3592 strarray_addall( &distclean_files, make->distclean_files );
3593 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3594 for (i = 0; i < subdirs.count; i++)
3596 struct strarray subclean = empty_strarray;
3597 strarray_addall( &subclean, get_removable_dirs( submakes[i]->clean_files ));
3598 strarray_addall( &subclean, get_removable_dirs( submakes[i]->distclean_files ));
3599 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3600 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3601 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3602 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3603 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3604 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3605 strarray_addall_path( &distclean_dirs, submakes[i]->obj_dir, subclean );
3606 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3607 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3609 for (arch = 0; arch < archs.count; arch++)
3611 if (submakes[i]->disabled[arch]) continue;
3612 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets[arch] );
3613 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files[arch] );
3615 if (submakes[i]->disabled[0]) continue;
3617 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3618 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3619 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3620 if (submakes[i]->testdll)
3621 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3622 for (j = 0; j < NB_INSTALL_RULES; j++)
3623 if (submakes[i]->install_rules[j].count)
3624 strarray_add( &install_deps[j], obj_dir_path( submakes[i], install_targets[j] ));
3626 strarray_addall( &dependencies, makefile_deps );
3627 output( "all:" );
3628 output_filenames( all_targets );
3629 output( "\n" );
3630 output( "Makefile:" );
3631 output_filenames( makefile_deps );
3632 output( "\n" );
3633 output_filenames( dependencies );
3634 output( ":\n" );
3635 for (j = 0; j < NB_INSTALL_RULES; j++)
3637 if (!install_deps[j].count) continue;
3638 if (strcmp( install_targets[j], "install-test" ))
3640 output( "install " );
3641 strarray_add_uniq( &make->phony_targets, "install" );
3643 output( "%s::", install_targets[j] );
3644 output_filenames( install_deps[j] );
3645 output( "\n" );
3646 strarray_add_uniq( &make->phony_targets, install_targets[j] );
3648 output_uninstall_rules( make );
3649 if (buildtest_deps.count)
3651 output( "buildtests:" );
3652 output_filenames( buildtest_deps );
3653 output( "\n" );
3654 strarray_add_uniq( &make->phony_targets, "buildtests" );
3656 output( "check test:" );
3657 output_filenames( testclean_files );
3658 output( "\n" );
3659 strarray_add_uniq( &make->phony_targets, "check" );
3660 strarray_add_uniq( &make->phony_targets, "test" );
3662 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3664 output( "clean::\n");
3665 output_rm_filenames( clean_files, "rm -f" );
3666 output( "testclean::\n");
3667 output_rm_filenames( testclean_files, "rm -f" );
3668 output( "distclean::\n");
3669 output_rm_filenames( distclean_files, "rm -f" );
3670 output_rm_filenames( distclean_dirs, "-rmdir 2>/dev/null" );
3671 output( "maintainer-clean::\n");
3672 output_rm_filenames( make->maintainerclean_files, "rm -f" );
3673 strarray_add_uniq( &make->phony_targets, "distclean" );
3674 strarray_add_uniq( &make->phony_targets, "testclean" );
3675 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3677 if (tooldeps_deps.count)
3679 output( "__tooldeps__:" );
3680 output_filenames( tooldeps_deps );
3681 output( "\n" );
3682 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3685 if (make->phony_targets.count)
3687 output( ".PHONY:" );
3688 output_filenames( make->phony_targets );
3689 output( "\n" );
3694 /*******************************************************************
3695 * output_sources
3697 static void output_sources( struct makefile *make )
3699 struct strarray all_targets = empty_strarray;
3700 struct incl_file *source;
3701 unsigned int i, j, arch;
3703 strarray_add_uniq( &make->phony_targets, "all" );
3705 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3707 char *obj = xstrdup( source->name );
3708 char *ext = get_extension( obj );
3710 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3711 *ext++ = 0;
3713 for (j = 0; output_source_funcs[j].ext; j++)
3714 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3716 output_source_funcs[j].fn( make, source, obj );
3717 strarray_addall_uniq( &make->dependencies, source->dependencies );
3720 /* special case for winetest: add resource files from other test dirs */
3721 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3723 for (arch = 0; arch < archs.count; arch++)
3725 if (!is_multiarch( arch )) continue;
3726 for (i = 0; i < subdirs.count; i++)
3728 if (!submakes[i]->testdll) continue;
3729 if (submakes[i]->disabled[arch]) continue;
3730 if (enable_tests.count && !strarray_exists( &enable_tests, submakes[i]->testdll )) continue;
3731 strarray_add( &make->res_files[arch],
3732 strmake( "%s%s", arch_dirs[arch],
3733 replace_extension( submakes[i]->testdll, ".dll", "_test.res" )));
3738 if (make->dlldata_files.count)
3740 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3741 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3742 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3743 output_filenames( make->dlldata_files );
3744 output( "\n" );
3747 if (make->staticlib)
3749 for (arch = 0; arch < archs.count; arch++)
3750 if (is_multiarch( arch ) || !make->extlib) output_static_lib( make, arch );
3752 else if (make->module)
3754 for (arch = 0; arch < archs.count; arch++) if (is_multiarch( arch )) output_module( make, arch );
3755 if (make->unixlib) output_unix_lib( make );
3756 if (make->importlib) for (arch = 0; arch < archs.count; arch++) output_import_lib( make, arch );
3757 if (make->is_exe && !make->is_win16 && *dll_ext && strendswith( make->module, ".exe" ))
3759 char *binary = replace_extension( make->module, ".exe", "" );
3760 add_install_rule( make, binary, 0, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3763 else if (make->testdll)
3765 for (arch = 0; arch < archs.count; arch++)
3766 if (is_multiarch( arch )) output_test_module( make, arch );
3768 else if (make->programs.count) output_programs( make );
3770 for (i = 0; i < make->scripts.count; i++)
3771 add_install_rule( make, make->scripts.str[i], 0, make->scripts.str[i],
3772 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3774 for (i = 0; i < make->extra_targets.count; i++)
3775 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3776 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3777 else
3778 strarray_add( &make->all_targets[0], make->extra_targets.str[i] );
3780 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3781 strarray_add( &make->distclean_files, "Makefile" );
3782 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3784 if (!make->obj_dir)
3785 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3786 else if (!strcmp( make->obj_dir, "po" ))
3787 strarray_add( &make->distclean_files, "LINGUAS" );
3789 for (arch = 0; arch < archs.count; arch++)
3791 strarray_addall_uniq( &make->clean_files, make->object_files[arch] );
3792 strarray_addall_uniq( &make->clean_files, make->implib_files[arch] );
3793 strarray_addall_uniq( &make->clean_files, make->res_files[arch] );
3794 strarray_addall_uniq( &make->clean_files, make->all_targets[arch] );
3796 strarray_addall( &make->clean_files, make->unixobj_files );
3797 strarray_addall( &make->clean_files, make->pot_files );
3798 strarray_addall( &make->clean_files, make->debug_files );
3800 if (make == top_makefile)
3802 output_subdirs( make );
3803 return;
3806 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3807 strarray_addall( &all_targets, make->font_files );
3808 if (all_targets.count)
3810 output( "%s:", obj_dir_path( make, "all" ));
3811 output_filenames_obj_dir( make, all_targets );
3812 output( "\n" );
3813 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3815 for (i = 0; i < NB_INSTALL_RULES; i++) output_install_rules( make, i );
3817 if (make->clean_files.count)
3819 output( "%s::\n", obj_dir_path( make, "clean" ));
3820 output( "\trm -f" );
3821 output_filenames_obj_dir( make, make->clean_files );
3822 output( "\n" );
3823 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3828 /*******************************************************************
3829 * create_temp_file
3831 static FILE *create_temp_file( const char *orig )
3833 char *name = xmalloc( strlen(orig) + 13 );
3834 unsigned int i, id = getpid();
3835 int fd;
3836 FILE *ret = NULL;
3838 for (i = 0; i < 100; i++)
3840 sprintf( name, "%s.tmp%08x", orig, id );
3841 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3843 ret = fdopen( fd, "w" );
3844 break;
3846 if (errno != EEXIST) break;
3847 id += 7777;
3849 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3850 temp_file_name = name;
3851 return ret;
3855 /*******************************************************************
3856 * rename_temp_file
3858 static void rename_temp_file( const char *dest )
3860 int ret = rename( temp_file_name, dest );
3861 if (ret == -1 && errno == EEXIST)
3863 /* rename doesn't overwrite on windows */
3864 unlink( dest );
3865 ret = rename( temp_file_name, dest );
3867 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3868 temp_file_name = NULL;
3872 /*******************************************************************
3873 * are_files_identical
3875 static int are_files_identical( FILE *file1, FILE *file2 )
3877 for (;;)
3879 char buffer1[8192], buffer2[8192];
3880 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3881 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3882 if (size1 != size2) return 0;
3883 if (!size1) return feof( file1 ) && feof( file2 );
3884 if (memcmp( buffer1, buffer2, size1 )) return 0;
3889 /*******************************************************************
3890 * rename_temp_file_if_changed
3892 static void rename_temp_file_if_changed( const char *dest )
3894 FILE *file1, *file2;
3895 int do_rename = 1;
3897 if ((file1 = fopen( dest, "r" )))
3899 if ((file2 = fopen( temp_file_name, "r" )))
3901 do_rename = !are_files_identical( file1, file2 );
3902 fclose( file2 );
3904 fclose( file1 );
3906 if (!do_rename)
3908 unlink( temp_file_name );
3909 temp_file_name = NULL;
3911 else rename_temp_file( dest );
3915 /*******************************************************************
3916 * output_linguas
3918 static void output_linguas( const struct makefile *make )
3920 const char *dest = obj_dir_path( make, "LINGUAS" );
3921 struct incl_file *source;
3923 output_file = create_temp_file( dest );
3925 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3926 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3927 if (strendswith( source->name, ".po" ))
3928 output( "%s\n", replace_extension( source->name, ".po", "" ));
3930 if (fclose( output_file )) fatal_perror( "write" );
3931 output_file = NULL;
3932 rename_temp_file_if_changed( dest );
3936 /*******************************************************************
3937 * output_testlist
3939 static void output_testlist( const struct makefile *make )
3941 const char *dest = obj_dir_path( make, "testlist.c" );
3942 unsigned int i;
3944 output_file = create_temp_file( dest );
3946 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3947 output( "#define WIN32_LEAN_AND_MEAN\n" );
3948 output( "#include <windows.h>\n\n" );
3949 output( "#define STANDALONE\n" );
3950 output( "#include \"wine/test.h\"\n\n" );
3952 for (i = 0; i < make->test_files.count; i++)
3953 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3954 output( "\n" );
3955 output( "const struct test winetest_testlist[] =\n" );
3956 output( "{\n" );
3957 for (i = 0; i < make->test_files.count; i++)
3958 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3959 output( " { 0, 0 }\n" );
3960 output( "};\n" );
3962 if (fclose( output_file )) fatal_perror( "write" );
3963 output_file = NULL;
3964 rename_temp_file_if_changed( dest );
3968 /*******************************************************************
3969 * output_gitignore
3971 static void output_gitignore( const char *dest, struct strarray files )
3973 unsigned int i;
3975 output_file = create_temp_file( dest );
3977 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3978 for (i = 0; i < files.count; i++)
3980 if (!strchr( files.str[i], '/' )) output( "/" );
3981 output( "%s\n", files.str[i] );
3984 if (fclose( output_file )) fatal_perror( "write" );
3985 output_file = NULL;
3986 rename_temp_file( dest );
3990 /*******************************************************************
3991 * output_stub_makefile
3993 static void output_stub_makefile( struct makefile *make )
3995 struct strarray targets = empty_strarray;
3996 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
3997 unsigned int i, arch;
3999 for (arch = 0; arch < archs.count; arch++)
4000 if (make->all_targets[arch].count) strarray_add_uniq( &targets, "all" );
4002 for (i = 0; i < NB_INSTALL_RULES; i++)
4004 if (!make->install_rules[i].count) continue;
4005 strarray_add_uniq( &targets, "install" );
4006 strarray_add( &targets, install_targets[i] );
4008 if (make->clean_files.count) strarray_add( &targets, "clean" );
4009 if (make->test_files.count)
4011 strarray_add( &targets, "check" );
4012 strarray_add( &targets, "test" );
4013 strarray_add( &targets, "testclean" );
4016 if (!targets.count && !make->clean_files.count) return;
4018 output_file_name = obj_dir_path( make, "Makefile" );
4019 output_file = create_temp_file( output_file_name );
4021 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
4023 if (make_var) output( "MAKE = %s\n\n", make_var );
4025 output( "all:\n" );
4026 output_filenames( targets );
4027 output_filenames( make->clean_files );
4028 output( ":\n" );
4029 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4030 output( ".PHONY:" );
4031 output_filenames( targets );
4032 output( "\n" );
4034 fclose( output_file );
4035 output_file = NULL;
4036 rename_temp_file( output_file_name );
4040 /*******************************************************************
4041 * output_silent_rules
4043 static void output_silent_rules(void)
4045 static const char *cmds[] =
4047 "BISON",
4048 "BUILD",
4049 "CC",
4050 "CCLD",
4051 "FLEX",
4052 "GEN",
4053 "LN",
4054 "MSG",
4055 "SED",
4056 "TEST",
4057 "WIDL",
4058 "WMC",
4059 "WRC"
4061 unsigned int i;
4063 output( "V = 0\n" );
4064 for (i = 0; i < ARRAY_SIZE(cmds); i++)
4066 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4067 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4068 output( "quiet_%s_1 =\n", cmds[i] );
4073 /*******************************************************************
4074 * output_top_makefile
4076 static void output_top_makefile( struct makefile *make )
4078 char buffer[1024];
4079 FILE *src_file;
4080 unsigned int i;
4081 int found = 0;
4083 output_file_name = obj_dir_path( make, output_makefile_name );
4084 output_file = create_temp_file( output_file_name );
4086 /* copy the contents of the source makefile */
4087 src_file = open_input_makefile( make );
4088 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4090 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4091 found = !strncmp( buffer, separator, strlen(separator) );
4093 if (fclose( src_file )) fatal_perror( "close" );
4094 input_file_name = NULL;
4096 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4098 if (silent_rules) output_silent_rules();
4099 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4100 output_sources( make );
4101 /* disable implicit rules */
4102 output( ".SUFFIXES:\n" );
4104 fclose( output_file );
4105 output_file = NULL;
4106 rename_temp_file( output_file_name );
4110 /*******************************************************************
4111 * output_dependencies
4113 static void output_dependencies( struct makefile *make )
4115 struct strarray ignore_files = empty_strarray;
4117 if (make->obj_dir) create_dir( make->obj_dir );
4119 if (make == top_makefile) output_top_makefile( make );
4120 else output_stub_makefile( make );
4122 strarray_addall( &ignore_files, make->distclean_files );
4123 strarray_addall( &ignore_files, make->clean_files );
4124 if (make->testdll) output_testlist( make );
4125 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4126 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4128 create_file_directories( make, ignore_files );
4130 output_file_name = NULL;
4134 /*******************************************************************
4135 * load_sources
4137 static void load_sources( struct makefile *make )
4139 static const char *source_vars[] =
4141 "SOURCES",
4142 "C_SRCS",
4143 "OBJC_SRCS",
4144 "RC_SRCS",
4145 "MC_SRCS",
4146 "IDL_SRCS",
4147 "BISON_SRCS",
4148 "LEX_SRCS",
4149 "HEADER_SRCS",
4150 "XTEMPLATE_SRCS",
4151 "SVG_SRCS",
4152 "FONT_SRCS",
4153 "IN_SRCS",
4154 "PO_SRCS",
4155 "MANPAGES",
4156 NULL
4158 const char **var;
4159 unsigned int i, arch;
4160 struct strarray value;
4161 struct incl_file *file;
4163 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4164 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4166 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4167 make->module = get_expanded_make_variable( make, "MODULE" );
4168 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4169 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4170 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4171 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4172 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4174 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4175 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4176 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4177 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4178 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4179 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4180 for (i = 0; i < NB_INSTALL_RULES; i++)
4181 make->install[i] = get_expanded_make_var_array( make, install_variables[i] );
4183 if (make->extlib) make->staticlib = make->extlib;
4184 if (make->staticlib) make->module = make->staticlib;
4186 if (make->obj_dir)
4188 make->disabled[0] = strarray_exists( &disabled_dirs[0], make->obj_dir );
4189 for (arch = 1; arch < archs.count; arch++)
4190 make->disabled[arch] = make->disabled[0] || strarray_exists( &disabled_dirs[arch], make->obj_dir );
4192 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4193 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4194 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4195 strarray_exists( &make->extradllflags, "-mwindows" );
4197 if (make->module)
4199 /* add default install rules if nothing was specified */
4200 for (i = 0; i < NB_INSTALL_RULES; i++) if (make->install[i].count) break;
4201 if (i == NB_INSTALL_RULES)
4203 if (make->importlib) strarray_add( &make->install[INSTALL_DEV], make->importlib );
4204 if (make->staticlib) strarray_add( &make->install[INSTALL_DEV], make->staticlib );
4205 else strarray_add( &make->install[INSTALL_LIB], make->module );
4209 make->include_paths = empty_strarray;
4210 make->include_args = empty_strarray;
4211 make->define_args = empty_strarray;
4212 make->unix_cflags = empty_strarray;
4213 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4215 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4216 for (i = 0; i < value.count; i++)
4218 if (!strncmp( value.str[i], "-I", 2 ))
4220 const char *dir = value.str[i] + 2;
4221 if (!strncmp( dir, "./", 2 ))
4223 dir += 2;
4224 while (*dir == '/') dir++;
4226 strarray_add_uniq( &make->include_paths, dir );
4228 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4229 strarray_add_uniq( &make->define_args, value.str[i] );
4231 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4232 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4234 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4235 if (make->src_dir)
4236 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4237 if (make->parent_dir)
4238 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4239 strarray_add( &make->include_args, "-Iinclude" );
4240 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4242 list_init( &make->sources );
4243 list_init( &make->includes );
4245 for (var = source_vars; *var; var++)
4247 value = get_expanded_make_var_array( make, *var );
4248 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4251 add_generated_sources( make );
4253 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4254 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4256 for (i = 0; i < make->delayimports.count; i++)
4257 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4261 /*******************************************************************
4262 * parse_makeflags
4264 static void parse_makeflags( const char *flags )
4266 const char *p = flags;
4267 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4269 while (*p)
4271 p = skip_spaces( p );
4272 var = buffer;
4273 while (*p && !isspace(*p))
4275 if (*p == '\\' && p[1]) p++;
4276 *var++ = *p++;
4278 *var = 0;
4279 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4284 /*******************************************************************
4285 * parse_option
4287 static int parse_option( const char *opt )
4289 if (opt[0] != '-')
4291 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4292 return 0;
4294 switch(opt[1])
4296 case 'f':
4297 if (opt[2]) output_makefile_name = opt + 2;
4298 break;
4299 case 'R':
4300 relative_dir_mode = 1;
4301 break;
4302 case 'S':
4303 silent_rules = 1;
4304 break;
4305 default:
4306 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4307 exit(1);
4309 return 1;
4313 /*******************************************************************
4314 * main
4316 int main( int argc, char *argv[] )
4318 const char *makeflags = getenv( "MAKEFLAGS" );
4319 const char *target;
4320 unsigned int i, j, arch;
4322 if (makeflags) parse_makeflags( makeflags );
4324 i = 1;
4325 while (i < argc)
4327 if (parse_option( argv[i] ))
4329 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4330 argc--;
4332 else i++;
4335 if (relative_dir_mode)
4337 char *relpath;
4339 if (argc != 3)
4341 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4342 exit( 1 );
4344 relpath = get_relative_path( argv[1], argv[2] );
4345 printf( "%s\n", relpath ? relpath : "." );
4346 exit( 0 );
4349 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4351 atexit( cleanup_files );
4352 init_signals( exit_on_signal );
4354 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4355 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4357 top_makefile = parse_makefile( NULL );
4359 target_flags[0] = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4360 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4361 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4362 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4363 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4364 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4365 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4366 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4367 for (i = 0; i < NB_INSTALL_RULES; i++)
4368 top_install[i] = get_expanded_make_var_array( top_makefile, strmake( "TOP_%s", install_variables[i] ));
4370 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4371 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4372 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4373 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4374 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4375 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4376 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4377 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4378 bison = get_expanded_make_variable( top_makefile, "BISON" );
4379 ar = get_expanded_make_variable( top_makefile, "AR" );
4380 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4381 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4382 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4383 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4384 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4385 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4387 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4388 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4389 if (!exe_ext) exe_ext = "";
4390 if (!tools_ext) tools_ext = "";
4392 strarray_add( &archs, get_expanded_make_variable( top_makefile, "HOST_ARCH" ));
4393 strarray_addall( &archs, get_expanded_make_var_array( top_makefile, "PE_ARCHS" ));
4395 arch_dirs[0] = "";
4396 arch_pe_dirs[0] = strmake( "%s-windows/", archs.str[0] );
4397 arch_install_dirs[0] = *dll_ext ? strmake( "$(dlldir)/%s-unix/", archs.str[0] ) : "$(dlldir)/";
4398 strip_progs[0] = "\"$(STRIP)\"";
4400 for (arch = 1; arch < archs.count; arch++)
4402 target = get_expanded_arch_var( top_makefile, "TARGET", arch );
4403 strarray_add( &target_flags[arch], "-b" );
4404 strarray_add( &target_flags[arch], target );
4405 arch_dirs[arch] = strmake( "%s-windows/", archs.str[arch] );
4406 arch_pe_dirs[arch] = arch_dirs[arch];
4407 arch_install_dirs[arch] = strmake( "$(dlldir)/%s", arch_dirs[arch] );
4408 strip_progs[arch] = strmake( "%s-strip", target );
4411 for (arch = 0; arch < archs.count; arch++)
4413 extra_cflags[arch] = get_expanded_arch_var_array( top_makefile, "EXTRACFLAGS", arch );
4414 extra_cflags_extlib[arch] = remove_warning_flags( extra_cflags[arch] );
4415 disabled_dirs[arch] = get_expanded_arch_var_array( top_makefile, "DISABLED_SUBDIRS", arch );
4416 if (!is_multiarch( arch )) continue;
4417 delay_load_flags[arch] = get_expanded_arch_var( top_makefile, "DELAYLOADFLAG", arch );
4418 debug_flags[arch] = get_expanded_arch_var( top_makefile, "DEBUG", arch );
4421 if (*dll_ext)
4423 delay_load_flags[0] = "-Wl,-delayload,";
4424 debug_flags[0] = NULL;
4427 top_makefile->src_dir = root_src_dir;
4428 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4429 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4431 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4433 load_sources( top_makefile );
4434 load_sources( include_makefile );
4435 for (i = 0; i < subdirs.count; i++)
4436 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4438 output_dependencies( top_makefile );
4439 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4441 return 0;