mmdevapi: Query MemoryWineUnixFuncs virtual memory and store the resulting handle.
[wine.git] / tools / makedep.c
blob8fa539026ca56e94aad3c9c2ec0707542c7a80c7
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 for (arch = 0; arch < archs.count; arch++)
2714 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2715 if (source->file->flags & FLAG_RC_PO)
2717 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2718 output( "%s.pot ", obj_dir_path( make, obj ) );
2720 output( "%s: %s", obj_dir_path( make, res_file ), source->filename );
2721 output_filename( tools_path( make, "wrc" ));
2722 if (make->src_dir) output_filename( "nls/locale.nls" );
2723 output_filenames( source->dependencies );
2724 output( "\n" );
2725 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2726 if (make->is_win16) output_filename( "-m16" );
2727 output_filename( "--nostdinc" );
2728 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2729 output_filenames( defines );
2730 output_filename( source->filename );
2731 output( "\n" );
2732 if (po_dir)
2734 output( "%s:", obj_dir_path( make, res_file ));
2735 for (i = 0; i < linguas.count; i++)
2736 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2737 output( "\n" );
2742 /*******************************************************************
2743 * output_source_mc
2745 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2747 unsigned int i, arch;
2748 char *obj_path = obj_dir_path( make, obj );
2749 char *res_file = strmake( "%s.res", obj );
2751 for (arch = 0; arch < archs.count; arch++)
2752 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2753 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2754 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2755 output_filename( tools_path( make, "wmc" ));
2756 output_filenames( source->dependencies );
2757 if (make->src_dir) output_filename( "nls/locale.nls" );
2758 output( "\n" );
2759 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2760 if (linguas.count)
2762 output_filename( "--po-dir=po" );
2763 output( "\n" );
2764 output( "%s.res:", obj_dir_path( make, obj ));
2765 for (i = 0; i < linguas.count; i++)
2766 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2768 output( "\n" );
2772 /*******************************************************************
2773 * output_source_res
2775 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2777 if (make->disabled[source->arch]) return;
2778 strarray_add( &make->res_files[source->arch], source->name );
2782 /*******************************************************************
2783 * output_source_idl
2785 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2787 struct strarray defines = get_source_defines( make, source, obj );
2788 struct strarray headers = empty_strarray;
2789 struct strarray deps = empty_strarray;
2790 struct strarray multiarch_targets[MAX_ARCHS] = { empty_strarray };
2791 const char *dest;
2792 unsigned int i, arch;
2794 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2795 if (!source->file->flags) return;
2797 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2798 if (source->file->flags & FLAG_INSTALL)
2800 add_install_rule( make, source->name, 0, xstrdup( source->name ),
2801 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2802 if (source->file->flags & FLAG_IDL_HEADER)
2803 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2804 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2806 if (source->file->flags & FLAG_IDL_HEADER)
2808 dest = strmake( "%s.h", obj );
2809 strarray_add( &headers, dest );
2810 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2813 for (i = 0; i < ARRAY_SIZE(idl_outputs); i++)
2815 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2816 for (arch = 0; arch < archs.count; arch++)
2818 if (!is_multiarch( arch )) continue;
2819 if (make->disabled[arch]) continue;
2820 dest = strmake( "%s%s%s", arch_dirs[arch], obj, idl_outputs[i].ext );
2821 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2822 strarray_add( &multiarch_targets[arch], dest );
2826 for (arch = 0; arch < archs.count; arch++)
2828 struct strarray arch_deps = empty_strarray;
2830 if (!arch) strarray_addall( &arch_deps, headers );
2831 strarray_addall( &arch_deps, multiarch_targets[arch] );
2832 if (!arch_deps.count) continue;
2833 output_filenames_obj_dir( make, arch_deps );
2834 output( ":\n" );
2835 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2836 output_filenames( target_flags[arch] );
2837 output_filename( "--nostdinc" );
2838 output_filename( "-Ldlls/\\*" );
2839 output_filenames( defines );
2840 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2841 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2842 output_filename( source->filename );
2843 output( "\n" );
2844 strarray_addall( &deps, arch_deps );
2847 if (deps.count)
2849 output_filenames_obj_dir( make, deps );
2850 output( ":" );
2851 output_filename( tools_path( make, "widl" ));
2852 output_filename( source->filename );
2853 output_filenames( source->dependencies );
2854 output( "\n" );
2857 if (source->importlibdeps.count)
2859 for (arch = 0; arch < archs.count; arch++)
2861 if (!multiarch_targets[arch].count) continue;
2862 output_filenames_obj_dir( make, multiarch_targets[arch] );
2863 output( ":" );
2864 for (i = 0; i < source->importlibdeps.count; i++)
2866 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2867 const char *module = strmake( "%s%s", arch_pe_dirs[arch], submake->module );
2868 output_filename( obj_dir_path( submake, module ));
2870 output( "\n" );
2876 /*******************************************************************
2877 * output_source_x
2879 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2881 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2882 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2883 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2884 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2885 if (source->file->flags & FLAG_INSTALL)
2887 add_install_rule( make, source->name, 0, source->name,
2888 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2889 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2890 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2895 /*******************************************************************
2896 * output_source_sfd
2898 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2900 unsigned int i;
2901 char *ttf_obj = strmake( "%s.ttf", obj );
2902 char *ttf_file = src_dir_path( make, ttf_obj );
2904 if (fontforge && !make->src_dir)
2906 output( "%s: %s\n", ttf_file, source->filename );
2907 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2908 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2909 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2910 strarray_add( &make->maintainerclean_files, ttf_obj );
2912 if (source->file->flags & FLAG_INSTALL)
2914 add_install_rule( make, source->name, 0, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2915 output_srcdir_symlink( make, ttf_obj );
2918 if (source->file->flags & FLAG_SFD_FONTS)
2920 struct strarray *array = source->file->args;
2922 for (i = 0; i < array->count; i++)
2924 char *font = strtok( xstrdup(array->str[i]), " \t" );
2925 char *args = strtok( NULL, "" );
2927 strarray_add( &make->all_targets[0], xstrdup( font ));
2928 output( "%s: %s %s\n", obj_dir_path( make, font ),
2929 tools_path( make, "sfnt2fon" ), ttf_file );
2930 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2931 tools_path( make, "sfnt2fon" ), ttf_file, args );
2932 add_install_rule( make, source->name, 0, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2938 /*******************************************************************
2939 * output_source_svg
2941 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2943 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2944 unsigned int i;
2946 if (convert && rsvg && icotool)
2948 for (i = 0; images[i]; i++)
2949 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2951 if (images[i])
2953 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2954 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2955 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2956 root_src_dir_path( "tools/buildimage" ), source->filename );
2957 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2963 /*******************************************************************
2964 * output_source_nls
2966 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2968 add_install_rule( make, source->name, 0, source->name,
2969 strmake( "D$(nlsdir)/%s", source->name ));
2970 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2974 /*******************************************************************
2975 * output_source_desktop
2977 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2979 add_install_rule( make, source->name, 0, source->name,
2980 strmake( "D$(datadir)/applications/%s", source->name ));
2984 /*******************************************************************
2985 * output_source_po
2987 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2989 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2990 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
2991 strarray_add( &make->all_targets[0], strmake( "%s.mo", obj ));
2995 /*******************************************************************
2996 * output_source_in
2998 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
3000 unsigned int i;
3002 if (make == include_makefile) return; /* ignore generated includes */
3003 if (strendswith( obj, ".man" ) && source->file->args)
3005 struct strarray symlinks;
3006 char *dir, *dest = replace_extension( obj, ".man", "" );
3007 char *lang = strchr( dest, '.' );
3008 char *section = source->file->args;
3009 if (lang)
3011 *lang++ = 0;
3012 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3014 else dir = strmake( "$(mandir)/man%s", section );
3015 add_install_rule( make, dest, 0, obj, strmake( "d%s/%s.%s", dir, dest, section ));
3016 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3017 for (i = 0; i < symlinks.count; i++)
3018 add_install_rule( make, symlinks.str[i], 0, strmake( "%s.%s", dest, section ),
3019 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3020 free( dest );
3021 free( dir );
3023 strarray_add( &make->in_files, obj );
3024 strarray_add( &make->all_targets[0], obj );
3025 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3026 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
3027 output( "%s:", obj_dir_path( make, obj ));
3028 output_filenames( source->dependencies );
3029 output( "\n" );
3030 add_install_rule( make, obj, 0, obj, strmake( "d$(datadir)/wine/%s", obj ));
3034 /*******************************************************************
3035 * output_source_spec
3037 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3039 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3040 struct strarray dll_flags = empty_strarray;
3041 struct strarray default_imports = empty_strarray;
3042 struct strarray all_libs, dep_libs;
3043 const char *dll_name, *obj_name, *res_name, *output_file, *debug_file;
3044 unsigned int arch;
3046 if (!imports.count) imports = make->imports;
3047 strarray_addall( &dll_flags, make->extradllflags );
3048 strarray_addall( &dll_flags, get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" ));
3049 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3051 for (arch = 0; arch < archs.count; arch++)
3053 if (!is_multiarch( arch )) continue;
3054 all_libs = dep_libs = empty_strarray;
3055 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ) );
3056 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3057 if (!arch) strarray_addall( &all_libs, libs );
3058 dll_name = arch_module_name( strmake( "%s.dll", obj ), arch );
3059 obj_name = obj_dir_path( make, strmake( "%s%s.o", arch_dirs[arch], obj ));
3060 res_name = strmake( "%s%s.res", arch_dirs[arch], obj );
3061 output_file = obj_dir_path( make, dll_name );
3063 strarray_add( &make->clean_files, dll_name );
3064 strarray_add( &make->res_files[arch], res_name );
3065 output( "%s:", obj_dir_path( make, res_name ));
3066 output_filename( output_file );
3067 output_filename( tools_path( make, "wrc" ));
3068 output( "\n" );
3069 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
3070 tools_path( make, "wrc" ));
3072 output( "%s:", output_file );
3073 output_filename( source->filename );
3074 output_filename( obj_name );
3075 output_filenames( dep_libs );
3076 output_filename( tools_path( make, "winebuild" ));
3077 output_filename( tools_path( make, "winegcc" ));
3078 output( "\n" );
3079 output_winegcc_command( make, arch );
3080 output_filename( "-s" );
3081 output_filenames( dll_flags );
3082 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3083 output_filename( "-shared" );
3084 output_filename( source->filename );
3085 output_filename( obj_name );
3086 if ((debug_file = get_debug_file( make, dll_name, arch )))
3087 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3088 output_filenames( all_libs );
3089 output_filename( arch_make_variable( "LDFLAGS", arch ));
3090 output( "\n" );
3095 /*******************************************************************
3096 * output_source_one_arch
3098 static void output_source_one_arch( struct makefile *make, struct incl_file *source, const char *obj,
3099 struct strarray defines, struct strarray *targets,
3100 unsigned int arch, int is_dll_src )
3102 const char *obj_name;
3104 if (make->disabled[arch] && !(source->file->flags & FLAG_C_IMPLIB)) return;
3106 if (arch)
3108 if (source->file->flags & FLAG_C_UNIX) return;
3109 if (!is_using_msvcrt( make ) && !make->staticlib && !(source->file->flags & FLAG_C_IMPLIB)) return;
3111 else if (source->file->flags & FLAG_C_UNIX)
3113 if (!*dll_ext) return;
3115 else if (archs.count > 1 && is_using_msvcrt( make ) &&
3116 !(source->file->flags & FLAG_C_IMPLIB) &&
3117 (!make->staticlib || make->extlib)) return;
3119 obj_name = strmake( "%s%s.o", source->arch ? "" : arch_dirs[arch], obj );
3120 strarray_add( targets, obj_name );
3122 if (source->file->flags & FLAG_C_UNIX)
3123 strarray_add( &make->unixobj_files, obj_name );
3124 else if (source->file->flags & FLAG_C_IMPLIB)
3125 strarray_add( &make->implib_files[arch], obj_name );
3126 else if (!is_dll_src)
3127 strarray_add( &make->object_files[arch], obj_name );
3128 else
3129 strarray_add( &make->clean_files, obj_name );
3131 output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename );
3132 output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), arch_make_variable( "CC", arch ), source->filename );
3133 output_filenames( defines );
3134 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3135 output_filenames( make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] );
3136 if (!arch)
3138 if (source->file->flags & FLAG_C_UNIX)
3140 output_filenames( unix_dllflags );
3142 else if (make->module || make->testdll)
3144 output_filenames( dll_flags );
3145 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3146 if (!*dll_ext && make->module && is_crt_module( make->module ))
3147 output_filename( "-fno-builtin" );
3150 else
3152 if (make->module && is_crt_module( make->module )) output_filename( "-fno-builtin" );
3155 output_filenames( cpp_flags );
3156 output_filename( arch_make_variable( "CFLAGS", arch ));
3157 output( "\n" );
3159 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3160 !(source->file->flags & FLAG_GENERATED))
3162 const char *ok_file, *test_exe;
3164 ok_file = strmake( "%s%s.ok", arch_dirs[arch], obj );
3165 test_exe = replace_extension( make->testdll, ".dll", "_test.exe" );
3166 strarray_add( &make->ok_files[arch], ok_file );
3167 output( "%s:\n", obj_dir_path( make, ok_file ));
3168 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s %s && touch $@\n",
3169 cmd_prefix( "TEST" ),
3170 root_src_dir_path( "tools/runtest" ), make->testdll,
3171 obj_dir_path( make, arch_module_name( test_exe, arch )), obj );
3176 /*******************************************************************
3177 * output_source_default
3179 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3181 struct strarray defines = get_source_defines( make, source, obj );
3182 struct strarray targets = empty_strarray;
3183 int is_dll_src = (make->testdll && strendswith( source->name, ".c" ) &&
3184 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3185 unsigned int arch;
3187 for (arch = 0; arch < archs.count; arch++)
3188 if (!source->arch || source->arch == arch)
3189 output_source_one_arch( make, source, obj, defines, &targets, arch, is_dll_src );
3191 if (source->file->flags & FLAG_GENERATED)
3193 if (!make->testdll || !strendswith( source->filename, "testlist.c" ))
3194 strarray_add( &make->clean_files, source->basename );
3196 else
3198 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ))
3199 strarray_add( &make->test_files, obj );
3202 output_filenames_obj_dir( make, targets );
3203 output( ":" );
3204 output_filenames( source->dependencies );
3205 output( "\n" );
3209 /* dispatch table to output rules for a single source file */
3210 static const struct
3212 const char *ext;
3213 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3214 } output_source_funcs[] =
3216 { "y", output_source_y },
3217 { "l", output_source_l },
3218 { "h", output_source_h },
3219 { "rh", output_source_h },
3220 { "inl", output_source_h },
3221 { "rc", output_source_rc },
3222 { "mc", output_source_mc },
3223 { "res", output_source_res },
3224 { "idl", output_source_idl },
3225 { "sfd", output_source_sfd },
3226 { "svg", output_source_svg },
3227 { "nls", output_source_nls },
3228 { "desktop", output_source_desktop },
3229 { "po", output_source_po },
3230 { "in", output_source_in },
3231 { "x", output_source_x },
3232 { "spec", output_source_spec },
3233 { NULL, output_source_default }
3237 /*******************************************************************
3238 * output_fake_module
3240 static void output_fake_module( struct makefile *make )
3242 unsigned int arch = 0; /* fake modules are always native */
3243 const char *spec_file = NULL, *name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3245 if (make->disabled[arch]) return;
3247 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3249 strarray_add( &make->all_targets[arch], name );
3250 add_install_rule( make, make->module, arch, name, strmake( "d$(dlldir)/%s", name ));
3252 output( "%s:", obj_dir_path( make, name ));
3253 if (spec_file) output_filename( spec_file );
3254 output_filenames_obj_dir( make, make->res_files[arch] );
3255 output_filename( tools_path( make, "winebuild" ));
3256 output_filename( tools_path( make, "winegcc" ));
3257 output( "\n" );
3258 output_winegcc_command( make, arch );
3259 output_filename( "-Wb,--fake-module" );
3260 if (spec_file)
3262 output_filename( "-shared" );
3263 output_filename( spec_file );
3265 output_filenames( make->extradllflags );
3266 output_filenames_obj_dir( make, make->res_files[arch] );
3267 output( "\n" );
3271 /*******************************************************************
3272 * output_module
3274 static void output_module( struct makefile *make, unsigned int arch )
3276 struct strarray default_imports = empty_strarray;
3277 struct strarray all_libs = empty_strarray;
3278 struct strarray dep_libs = empty_strarray;
3279 struct strarray imports = make->imports;
3280 const char *module_name;
3281 const char *debug_file;
3282 char *spec_file = NULL;
3283 unsigned int i;
3285 if (make->disabled[arch]) return;
3287 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3289 if (!make->data_only)
3291 module_name = arch_module_name( make->module, arch );
3293 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3295 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ));
3296 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, IMPORT_TYPE_DELAYED, arch ));
3297 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3298 if (!arch) strarray_addall( &all_libs, libs );
3300 if (delay_load_flags[arch])
3302 for (i = 0; i < make->delayimports.count; i++)
3304 struct makefile *import = get_static_lib( make->delayimports.str[i], arch );
3305 if (import) strarray_add( &all_libs, strmake( "%s%s", delay_load_flags[arch], import->module ));
3309 else module_name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3311 strarray_add( &make->all_targets[arch], module_name );
3312 if (make->data_only)
3313 add_install_rule( make, make->module, arch, module_name,
3314 strmake( "d$(dlldir)/%s%s", arch_pe_dirs[arch], make->module ));
3315 else
3316 add_install_rule( make, make->module, arch, module_name,
3317 strmake( "%c%s%s%s", '0' + arch, arch_install_dirs[arch], make->module,
3318 arch ? "" : dll_ext ));
3320 output( "%s:", obj_dir_path( make, module_name ));
3321 if (spec_file) output_filename( spec_file );
3322 output_filenames_obj_dir( make, make->object_files[arch] );
3323 output_filenames_obj_dir( make, make->res_files[arch] );
3324 output_filenames( dep_libs );
3325 output_filename( tools_path( make, "winebuild" ));
3326 output_filename( tools_path( make, "winegcc" ));
3327 output( "\n" );
3328 output_winegcc_command( make, arch );
3329 if (arch) output_filename( "-Wl,--wine-builtin" );
3330 if (spec_file)
3332 output_filename( "-shared" );
3333 output_filename( spec_file );
3335 output_filenames( make->extradllflags );
3336 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3337 output_filenames_obj_dir( make, make->object_files[arch] );
3338 output_filenames_obj_dir( make, make->res_files[arch] );
3339 debug_file = get_debug_file( make, module_name, arch );
3340 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3341 output_filenames( all_libs );
3342 output_filename( arch_make_variable( "LDFLAGS", arch ));
3343 output( "\n" );
3345 if (!make->data_only && !arch && *dll_ext) output_fake_module( make );
3349 /*******************************************************************
3350 * output_import_lib
3352 static void output_import_lib( struct makefile *make, unsigned int arch )
3354 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3355 const char *name = strmake( "%slib%s.a", arch_dirs[arch], make->importlib );
3357 strarray_add( &make->clean_files, name );
3358 if (needs_delay_lib( make, arch ))
3360 const char *delay_name = replace_extension( name, ".a", ".delay.a" );
3361 strarray_add( &make->clean_files, delay_name );
3362 output( "%s ", obj_dir_path( make, delay_name ));
3364 output( "%s: %s %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ), spec_file );
3365 output_filenames_obj_dir( make, make->implib_files[arch] );
3366 output( "\n" );
3367 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3368 if (!delay_load_flags[arch]) output_filename( "--without-dlltool" );
3369 output_filenames( target_flags[arch] );
3370 if (make->is_win16) output_filename( "-m16" );
3371 output_filename( "--export" );
3372 output_filename( spec_file );
3373 output_filenames_obj_dir( make, make->implib_files[arch] );
3374 output( "\n" );
3376 if (!arch && is_native_arch_disabled( make )) return;
3378 add_install_rule( make, make->importlib, arch, name,
3379 strmake( "d%slib%s.a", arch_install_dirs[arch], make->importlib ));
3383 /*******************************************************************
3384 * output_unix_lib
3386 static void output_unix_lib( struct makefile *make )
3388 struct strarray unix_deps = empty_strarray;
3389 struct strarray unix_libs = add_unix_libraries( make, &unix_deps );
3390 unsigned int arch = 0; /* unix libs are always native */
3392 if (make->disabled[arch]) return;
3394 strarray_add( &make->all_targets[arch], make->unixlib );
3395 add_install_rule( make, make->module, arch, make->unixlib,
3396 strmake( "p%s%s", arch_install_dirs[arch], make->unixlib ));
3397 output( "%s:", obj_dir_path( make, make->unixlib ));
3398 output_filenames_obj_dir( make, make->unixobj_files );
3399 output_filenames( unix_deps );
3400 output( "\n" );
3401 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3402 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3403 output_filenames_obj_dir( make, make->unixobj_files );
3404 output_filenames( unix_libs );
3405 output_filename( "$(LDFLAGS)" );
3406 output( "\n" );
3410 /*******************************************************************
3411 * output_static_lib
3413 static void output_static_lib( struct makefile *make, unsigned int arch )
3415 const char *name = strmake( "%s%s", arch_dirs[arch], make->staticlib );
3417 strarray_add( &make->clean_files, name );
3418 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3419 output_filenames_obj_dir( make, make->object_files[arch] );
3420 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3421 output( "\n" );
3422 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3423 output_filenames( target_flags[arch] );
3424 output_filenames_obj_dir( make, make->object_files[arch] );
3425 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3426 output( "\n" );
3427 if (!make->extlib)
3428 add_install_rule( make, make->staticlib, arch, name,
3429 strmake( "d%s%s", arch_install_dirs[arch], make->staticlib ));
3433 /*******************************************************************
3434 * output_test_module
3436 static void output_test_module( struct makefile *make, unsigned int arch )
3438 char *basemodule = replace_extension( make->testdll, ".dll", "" );
3439 char *stripped = arch_module_name( strmake( "%s_test-stripped.exe", basemodule ), arch );
3440 char *testmodule = arch_module_name( strmake( "%s_test.exe", basemodule ), arch );
3441 struct strarray default_imports = get_default_imports( make, make->imports );
3442 struct strarray dep_libs = empty_strarray;
3443 struct strarray all_libs = empty_strarray;
3444 struct makefile *parent = get_parent_makefile( make );
3445 const char *debug_file;
3447 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, IMPORT_TYPE_DIRECT, arch ) );
3448 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3450 strarray_add( &make->all_targets[arch], testmodule );
3451 strarray_add( &make->clean_files, stripped );
3452 output( "%s:\n", obj_dir_path( make, testmodule ));
3453 output_winegcc_command( make, arch );
3454 output_filenames( make->extradllflags );
3455 output_filenames_obj_dir( make, make->object_files[arch] );
3456 output_filenames_obj_dir( make, make->res_files[arch] );
3457 if ((debug_file = get_debug_file( make, testmodule, arch )))
3458 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3459 output_filenames( all_libs );
3460 output_filename( arch_make_variable( "LDFLAGS", arch ));
3461 output( "\n" );
3462 output( "%s:\n", obj_dir_path( make, stripped ));
3463 output_winegcc_command( make, arch );
3464 output_filename( "-s" );
3465 output_filename( strmake( "-Wb,-F,%s_test.exe", basemodule ));
3466 output_filenames( make->extradllflags );
3467 output_filenames_obj_dir( make, make->object_files[arch] );
3468 output_filenames_obj_dir( make, make->res_files[arch] );
3469 output_filenames( all_libs );
3470 output_filename( arch_make_variable( "LDFLAGS", arch ));
3471 output( "\n" );
3472 output( "%s %s:", obj_dir_path( make, testmodule ), obj_dir_path( make, stripped ));
3473 output_filenames_obj_dir( make, make->object_files[arch] );
3474 output_filenames_obj_dir( make, make->res_files[arch] );
3475 output_filenames( dep_libs );
3476 output_filename( tools_path( make, "winebuild" ));
3477 output_filename( tools_path( make, "winegcc" ));
3478 output( "\n" );
3480 output( "programs/winetest/%s%s_test.res: %s\n", arch_dirs[arch], basemodule,
3481 obj_dir_path( make, stripped ));
3482 output( "\t%secho \"%s_test.exe TESTRES \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3483 basemodule, obj_dir_path( make, stripped ), tools_path( make, "wrc" ));
3485 if (make->disabled[arch] || (parent && parent->disabled[arch]))
3487 make->ok_files[arch] = empty_strarray;
3488 return;
3490 output_filenames_obj_dir( make, make->ok_files[arch] );
3491 output( ": %s", obj_dir_path( make, testmodule ));
3492 if (parent)
3494 char *parent_module = arch_module_name( make->testdll, arch );
3495 output_filename( obj_dir_path( parent, parent_module ));
3496 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3498 output( "\n" );
3499 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3500 output_filenames_obj_dir( make, make->ok_files[arch] );
3501 output( "\n" );
3502 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3503 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3504 output( "%s::\n", obj_dir_path( make, "testclean" ));
3505 output( "\trm -f" );
3506 output_filenames_obj_dir( make, make->ok_files[arch] );
3507 output( "\n" );
3508 strarray_addall( &make->clean_files, make->ok_files[arch] );
3509 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3513 /*******************************************************************
3514 * output_programs
3516 static void output_programs( struct makefile *make )
3518 unsigned int i, j;
3519 unsigned int arch = 0; /* programs are always native */
3521 for (i = 0; i < make->programs.count; i++)
3523 char *program_installed = NULL;
3524 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3525 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3526 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3527 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3528 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3530 if (!objs.count) objs = make->object_files[arch];
3531 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3533 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3534 strarray_addall( &all_libs, libs );
3537 output( "%s:", obj_dir_path( make, program ) );
3538 output_filenames_obj_dir( make, objs );
3539 output_filenames( deps );
3540 output( "\n" );
3541 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3542 output_filenames_obj_dir( make, objs );
3543 output_filenames( all_libs );
3544 output_filename( "$(LDFLAGS)" );
3545 output( "\n" );
3546 strarray_add( &make->all_targets[arch], program );
3548 for (j = 0; j < symlinks.count; j++)
3550 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3551 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3553 strarray_addall( &make->all_targets[arch], symlinks );
3555 add_install_rule( make, program, arch, program_installed ? program_installed : program,
3556 strmake( "p$(bindir)/%s", program ));
3557 for (j = 0; j < symlinks.count; j++)
3558 add_install_rule( make, symlinks.str[j], arch, program,
3559 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3564 /*******************************************************************
3565 * output_subdirs
3567 static void output_subdirs( struct makefile *make )
3569 struct strarray all_targets = empty_strarray;
3570 struct strarray makefile_deps = empty_strarray;
3571 struct strarray clean_files = empty_strarray;
3572 struct strarray testclean_files = empty_strarray;
3573 struct strarray distclean_files = empty_strarray;
3574 struct strarray distclean_dirs = empty_strarray;
3575 struct strarray dependencies = empty_strarray;
3576 struct strarray install_deps[NB_INSTALL_RULES] = { empty_strarray };
3577 struct strarray tooldeps_deps = empty_strarray;
3578 struct strarray buildtest_deps = empty_strarray;
3579 unsigned int i, j, arch;
3581 strarray_addall( &clean_files, make->clean_files );
3582 strarray_addall( &distclean_files, make->distclean_files );
3583 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3584 for (i = 0; i < subdirs.count; i++)
3586 struct strarray subclean = empty_strarray;
3587 strarray_addall( &subclean, get_removable_dirs( submakes[i]->clean_files ));
3588 strarray_addall( &subclean, get_removable_dirs( submakes[i]->distclean_files ));
3589 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3590 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3591 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3592 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3593 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3594 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3595 strarray_addall_path( &distclean_dirs, submakes[i]->obj_dir, subclean );
3596 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3597 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3599 for (arch = 0; arch < archs.count; arch++)
3601 if (submakes[i]->disabled[arch]) continue;
3602 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets[arch] );
3603 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files[arch] );
3605 if (submakes[i]->disabled[0]) continue;
3607 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3608 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3609 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3610 if (submakes[i]->testdll)
3611 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3612 for (j = 0; j < NB_INSTALL_RULES; j++)
3613 if (submakes[i]->install_rules[j].count)
3614 strarray_add( &install_deps[j], obj_dir_path( submakes[i], install_targets[j] ));
3616 strarray_addall( &dependencies, makefile_deps );
3617 output( "all:" );
3618 output_filenames( all_targets );
3619 output( "\n" );
3620 output( "Makefile:" );
3621 output_filenames( makefile_deps );
3622 output( "\n" );
3623 output_filenames( dependencies );
3624 output( ":\n" );
3625 for (j = 0; j < NB_INSTALL_RULES; j++)
3627 if (!install_deps[j].count) continue;
3628 if (strcmp( install_targets[j], "install-test" ))
3630 output( "install " );
3631 strarray_add_uniq( &make->phony_targets, "install" );
3633 output( "%s::", install_targets[j] );
3634 output_filenames( install_deps[j] );
3635 output( "\n" );
3636 strarray_add_uniq( &make->phony_targets, install_targets[j] );
3638 output_uninstall_rules( make );
3639 if (buildtest_deps.count)
3641 output( "buildtests:" );
3642 output_filenames( buildtest_deps );
3643 output( "\n" );
3644 strarray_add_uniq( &make->phony_targets, "buildtests" );
3646 output( "check test:" );
3647 output_filenames( testclean_files );
3648 output( "\n" );
3649 strarray_add_uniq( &make->phony_targets, "check" );
3650 strarray_add_uniq( &make->phony_targets, "test" );
3652 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3654 output( "clean::\n");
3655 output_rm_filenames( clean_files, "rm -f" );
3656 output( "testclean::\n");
3657 output_rm_filenames( testclean_files, "rm -f" );
3658 output( "distclean::\n");
3659 output_rm_filenames( distclean_files, "rm -f" );
3660 output_rm_filenames( distclean_dirs, "-rmdir 2>/dev/null" );
3661 output( "maintainer-clean::\n");
3662 output_rm_filenames( make->maintainerclean_files, "rm -f" );
3663 strarray_add_uniq( &make->phony_targets, "distclean" );
3664 strarray_add_uniq( &make->phony_targets, "testclean" );
3665 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3667 if (tooldeps_deps.count)
3669 output( "__tooldeps__:" );
3670 output_filenames( tooldeps_deps );
3671 output( "\n" );
3672 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3675 if (make->phony_targets.count)
3677 output( ".PHONY:" );
3678 output_filenames( make->phony_targets );
3679 output( "\n" );
3684 /*******************************************************************
3685 * output_sources
3687 static void output_sources( struct makefile *make )
3689 struct strarray all_targets = empty_strarray;
3690 struct incl_file *source;
3691 unsigned int i, j, arch;
3693 strarray_add_uniq( &make->phony_targets, "all" );
3695 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3697 char *obj = xstrdup( source->name );
3698 char *ext = get_extension( obj );
3700 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3701 *ext++ = 0;
3703 for (j = 0; output_source_funcs[j].ext; j++)
3704 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3706 output_source_funcs[j].fn( make, source, obj );
3707 strarray_addall_uniq( &make->dependencies, source->dependencies );
3710 /* special case for winetest: add resource files from other test dirs */
3711 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3713 for (arch = 0; arch < archs.count; arch++)
3715 if (!is_multiarch( arch )) continue;
3716 for (i = 0; i < subdirs.count; i++)
3718 if (!submakes[i]->testdll) continue;
3719 if (submakes[i]->disabled[arch]) continue;
3720 if (enable_tests.count && !strarray_exists( &enable_tests, submakes[i]->testdll )) continue;
3721 strarray_add( &make->res_files[arch],
3722 strmake( "%s%s", arch_dirs[arch],
3723 replace_extension( submakes[i]->testdll, ".dll", "_test.res" )));
3728 if (make->dlldata_files.count)
3730 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3731 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3732 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3733 output_filenames( make->dlldata_files );
3734 output( "\n" );
3737 if (make->staticlib)
3739 for (arch = 0; arch < archs.count; arch++)
3740 if (is_multiarch( arch ) || !make->extlib) output_static_lib( make, arch );
3742 else if (make->module)
3744 for (arch = 0; arch < archs.count; arch++) if (is_multiarch( arch )) output_module( make, arch );
3745 if (make->unixlib) output_unix_lib( make );
3746 if (make->importlib) for (arch = 0; arch < archs.count; arch++) output_import_lib( make, arch );
3747 if (make->is_exe && !make->is_win16 && *dll_ext && strendswith( make->module, ".exe" ))
3749 char *binary = replace_extension( make->module, ".exe", "" );
3750 add_install_rule( make, binary, 0, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3753 else if (make->testdll)
3755 for (arch = 0; arch < archs.count; arch++)
3756 if (is_multiarch( arch )) output_test_module( make, arch );
3758 else if (make->programs.count) output_programs( make );
3760 for (i = 0; i < make->scripts.count; i++)
3761 add_install_rule( make, make->scripts.str[i], 0, make->scripts.str[i],
3762 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3764 for (i = 0; i < make->extra_targets.count; i++)
3765 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3766 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3767 else
3768 strarray_add( &make->all_targets[0], make->extra_targets.str[i] );
3770 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3771 strarray_add( &make->distclean_files, "Makefile" );
3772 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3774 if (!make->obj_dir)
3775 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3776 else if (!strcmp( make->obj_dir, "po" ))
3777 strarray_add( &make->distclean_files, "LINGUAS" );
3779 for (arch = 0; arch < archs.count; arch++)
3781 strarray_addall_uniq( &make->clean_files, make->object_files[arch] );
3782 strarray_addall_uniq( &make->clean_files, make->implib_files[arch] );
3783 strarray_addall_uniq( &make->clean_files, make->res_files[arch] );
3784 strarray_addall_uniq( &make->clean_files, make->all_targets[arch] );
3786 strarray_addall( &make->clean_files, make->unixobj_files );
3787 strarray_addall( &make->clean_files, make->pot_files );
3788 strarray_addall( &make->clean_files, make->debug_files );
3790 if (make == top_makefile)
3792 output_subdirs( make );
3793 return;
3796 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3797 strarray_addall( &all_targets, make->font_files );
3798 if (all_targets.count)
3800 output( "%s:", obj_dir_path( make, "all" ));
3801 output_filenames_obj_dir( make, all_targets );
3802 output( "\n" );
3803 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3805 for (i = 0; i < NB_INSTALL_RULES; i++) output_install_rules( make, i );
3807 if (make->clean_files.count)
3809 output( "%s::\n", obj_dir_path( make, "clean" ));
3810 output( "\trm -f" );
3811 output_filenames_obj_dir( make, make->clean_files );
3812 output( "\n" );
3813 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3818 /*******************************************************************
3819 * create_temp_file
3821 static FILE *create_temp_file( const char *orig )
3823 char *name = xmalloc( strlen(orig) + 13 );
3824 unsigned int i, id = getpid();
3825 int fd;
3826 FILE *ret = NULL;
3828 for (i = 0; i < 100; i++)
3830 sprintf( name, "%s.tmp%08x", orig, id );
3831 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3833 ret = fdopen( fd, "w" );
3834 break;
3836 if (errno != EEXIST) break;
3837 id += 7777;
3839 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3840 temp_file_name = name;
3841 return ret;
3845 /*******************************************************************
3846 * rename_temp_file
3848 static void rename_temp_file( const char *dest )
3850 int ret = rename( temp_file_name, dest );
3851 if (ret == -1 && errno == EEXIST)
3853 /* rename doesn't overwrite on windows */
3854 unlink( dest );
3855 ret = rename( temp_file_name, dest );
3857 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3858 temp_file_name = NULL;
3862 /*******************************************************************
3863 * are_files_identical
3865 static int are_files_identical( FILE *file1, FILE *file2 )
3867 for (;;)
3869 char buffer1[8192], buffer2[8192];
3870 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3871 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3872 if (size1 != size2) return 0;
3873 if (!size1) return feof( file1 ) && feof( file2 );
3874 if (memcmp( buffer1, buffer2, size1 )) return 0;
3879 /*******************************************************************
3880 * rename_temp_file_if_changed
3882 static void rename_temp_file_if_changed( const char *dest )
3884 FILE *file1, *file2;
3885 int do_rename = 1;
3887 if ((file1 = fopen( dest, "r" )))
3889 if ((file2 = fopen( temp_file_name, "r" )))
3891 do_rename = !are_files_identical( file1, file2 );
3892 fclose( file2 );
3894 fclose( file1 );
3896 if (!do_rename)
3898 unlink( temp_file_name );
3899 temp_file_name = NULL;
3901 else rename_temp_file( dest );
3905 /*******************************************************************
3906 * output_linguas
3908 static void output_linguas( const struct makefile *make )
3910 const char *dest = obj_dir_path( make, "LINGUAS" );
3911 struct incl_file *source;
3913 output_file = create_temp_file( dest );
3915 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3916 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3917 if (strendswith( source->name, ".po" ))
3918 output( "%s\n", replace_extension( source->name, ".po", "" ));
3920 if (fclose( output_file )) fatal_perror( "write" );
3921 output_file = NULL;
3922 rename_temp_file_if_changed( dest );
3926 /*******************************************************************
3927 * output_testlist
3929 static void output_testlist( const struct makefile *make )
3931 const char *dest = obj_dir_path( make, "testlist.c" );
3932 unsigned int i;
3934 output_file = create_temp_file( dest );
3936 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3937 output( "#define WIN32_LEAN_AND_MEAN\n" );
3938 output( "#include <windows.h>\n\n" );
3939 output( "#define STANDALONE\n" );
3940 output( "#include \"wine/test.h\"\n\n" );
3942 for (i = 0; i < make->test_files.count; i++)
3943 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3944 output( "\n" );
3945 output( "const struct test winetest_testlist[] =\n" );
3946 output( "{\n" );
3947 for (i = 0; i < make->test_files.count; i++)
3948 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3949 output( " { 0, 0 }\n" );
3950 output( "};\n" );
3952 if (fclose( output_file )) fatal_perror( "write" );
3953 output_file = NULL;
3954 rename_temp_file_if_changed( dest );
3958 /*******************************************************************
3959 * output_gitignore
3961 static void output_gitignore( const char *dest, struct strarray files )
3963 unsigned int i;
3965 output_file = create_temp_file( dest );
3967 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3968 for (i = 0; i < files.count; i++)
3970 if (!strchr( files.str[i], '/' )) output( "/" );
3971 output( "%s\n", files.str[i] );
3974 if (fclose( output_file )) fatal_perror( "write" );
3975 output_file = NULL;
3976 rename_temp_file( dest );
3980 /*******************************************************************
3981 * output_stub_makefile
3983 static void output_stub_makefile( struct makefile *make )
3985 struct strarray targets = empty_strarray;
3986 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
3987 unsigned int i, arch;
3989 for (arch = 0; arch < archs.count; arch++)
3990 if (make->all_targets[arch].count) strarray_add_uniq( &targets, "all" );
3992 for (i = 0; i < NB_INSTALL_RULES; i++)
3994 if (!make->install_rules[i].count) continue;
3995 strarray_add_uniq( &targets, "install" );
3996 strarray_add( &targets, install_targets[i] );
3998 if (make->clean_files.count) strarray_add( &targets, "clean" );
3999 if (make->test_files.count)
4001 strarray_add( &targets, "check" );
4002 strarray_add( &targets, "test" );
4003 strarray_add( &targets, "testclean" );
4006 if (!targets.count && !make->clean_files.count) return;
4008 output_file_name = obj_dir_path( make, "Makefile" );
4009 output_file = create_temp_file( output_file_name );
4011 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
4013 if (make_var) output( "MAKE = %s\n\n", make_var );
4015 output( "all:\n" );
4016 output_filenames( targets );
4017 output_filenames( make->clean_files );
4018 output( ":\n" );
4019 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4020 output( ".PHONY:" );
4021 output_filenames( targets );
4022 output( "\n" );
4024 fclose( output_file );
4025 output_file = NULL;
4026 rename_temp_file( output_file_name );
4030 /*******************************************************************
4031 * output_silent_rules
4033 static void output_silent_rules(void)
4035 static const char *cmds[] =
4037 "BISON",
4038 "BUILD",
4039 "CC",
4040 "CCLD",
4041 "FLEX",
4042 "GEN",
4043 "LN",
4044 "MSG",
4045 "SED",
4046 "TEST",
4047 "WIDL",
4048 "WMC",
4049 "WRC"
4051 unsigned int i;
4053 output( "V = 0\n" );
4054 for (i = 0; i < ARRAY_SIZE(cmds); i++)
4056 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4057 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4058 output( "quiet_%s_1 =\n", cmds[i] );
4063 /*******************************************************************
4064 * output_top_makefile
4066 static void output_top_makefile( struct makefile *make )
4068 char buffer[1024];
4069 FILE *src_file;
4070 unsigned int i;
4071 int found = 0;
4073 output_file_name = obj_dir_path( make, output_makefile_name );
4074 output_file = create_temp_file( output_file_name );
4076 /* copy the contents of the source makefile */
4077 src_file = open_input_makefile( make );
4078 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4080 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4081 found = !strncmp( buffer, separator, strlen(separator) );
4083 if (fclose( src_file )) fatal_perror( "close" );
4084 input_file_name = NULL;
4086 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4088 if (silent_rules) output_silent_rules();
4089 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4090 output_sources( make );
4091 /* disable implicit rules */
4092 output( ".SUFFIXES:\n" );
4094 fclose( output_file );
4095 output_file = NULL;
4096 rename_temp_file( output_file_name );
4100 /*******************************************************************
4101 * output_dependencies
4103 static void output_dependencies( struct makefile *make )
4105 struct strarray ignore_files = empty_strarray;
4107 if (make->obj_dir) create_dir( make->obj_dir );
4109 if (make == top_makefile) output_top_makefile( make );
4110 else output_stub_makefile( make );
4112 strarray_addall( &ignore_files, make->distclean_files );
4113 strarray_addall( &ignore_files, make->clean_files );
4114 if (make->testdll) output_testlist( make );
4115 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4116 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4118 create_file_directories( make, ignore_files );
4120 output_file_name = NULL;
4124 /*******************************************************************
4125 * load_sources
4127 static void load_sources( struct makefile *make )
4129 static const char *source_vars[] =
4131 "SOURCES",
4132 "C_SRCS",
4133 "OBJC_SRCS",
4134 "RC_SRCS",
4135 "MC_SRCS",
4136 "IDL_SRCS",
4137 "BISON_SRCS",
4138 "LEX_SRCS",
4139 "HEADER_SRCS",
4140 "XTEMPLATE_SRCS",
4141 "SVG_SRCS",
4142 "FONT_SRCS",
4143 "IN_SRCS",
4144 "PO_SRCS",
4145 "MANPAGES",
4146 NULL
4148 const char **var;
4149 unsigned int i, arch;
4150 struct strarray value;
4151 struct incl_file *file;
4153 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4154 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4156 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4157 make->module = get_expanded_make_variable( make, "MODULE" );
4158 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4159 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4160 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4161 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4162 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4164 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4165 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4166 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4167 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4168 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4169 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4170 for (i = 0; i < NB_INSTALL_RULES; i++)
4171 make->install[i] = get_expanded_make_var_array( make, install_variables[i] );
4173 if (make->extlib) make->staticlib = make->extlib;
4174 if (make->staticlib) make->module = make->staticlib;
4176 if (make->obj_dir)
4178 make->disabled[0] = strarray_exists( &disabled_dirs[0], make->obj_dir );
4179 for (arch = 1; arch < archs.count; arch++)
4180 make->disabled[arch] = make->disabled[0] || strarray_exists( &disabled_dirs[arch], make->obj_dir );
4182 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4183 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4184 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4185 strarray_exists( &make->extradllflags, "-mwindows" );
4187 if (make->module)
4189 /* add default install rules if nothing was specified */
4190 for (i = 0; i < NB_INSTALL_RULES; i++) if (make->install[i].count) break;
4191 if (i == NB_INSTALL_RULES)
4193 if (make->importlib) strarray_add( &make->install[INSTALL_DEV], make->importlib );
4194 if (make->staticlib) strarray_add( &make->install[INSTALL_DEV], make->staticlib );
4195 else strarray_add( &make->install[INSTALL_LIB], make->module );
4199 make->include_paths = empty_strarray;
4200 make->include_args = empty_strarray;
4201 make->define_args = empty_strarray;
4202 make->unix_cflags = empty_strarray;
4203 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4205 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4206 for (i = 0; i < value.count; i++)
4208 if (!strncmp( value.str[i], "-I", 2 ))
4210 const char *dir = value.str[i] + 2;
4211 if (!strncmp( dir, "./", 2 ))
4213 dir += 2;
4214 while (*dir == '/') dir++;
4216 strarray_add_uniq( &make->include_paths, dir );
4218 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4219 strarray_add_uniq( &make->define_args, value.str[i] );
4221 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4222 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4224 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4225 if (make->src_dir)
4226 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4227 if (make->parent_dir)
4228 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4229 strarray_add( &make->include_args, "-Iinclude" );
4230 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4232 list_init( &make->sources );
4233 list_init( &make->includes );
4235 for (var = source_vars; *var; var++)
4237 value = get_expanded_make_var_array( make, *var );
4238 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4241 add_generated_sources( make );
4243 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4244 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4246 for (i = 0; i < make->delayimports.count; i++)
4247 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4251 /*******************************************************************
4252 * parse_makeflags
4254 static void parse_makeflags( const char *flags )
4256 const char *p = flags;
4257 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4259 while (*p)
4261 p = skip_spaces( p );
4262 var = buffer;
4263 while (*p && !isspace(*p))
4265 if (*p == '\\' && p[1]) p++;
4266 *var++ = *p++;
4268 *var = 0;
4269 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4274 /*******************************************************************
4275 * parse_option
4277 static int parse_option( const char *opt )
4279 if (opt[0] != '-')
4281 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4282 return 0;
4284 switch(opt[1])
4286 case 'f':
4287 if (opt[2]) output_makefile_name = opt + 2;
4288 break;
4289 case 'R':
4290 relative_dir_mode = 1;
4291 break;
4292 case 'S':
4293 silent_rules = 1;
4294 break;
4295 default:
4296 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4297 exit(1);
4299 return 1;
4303 /*******************************************************************
4304 * main
4306 int main( int argc, char *argv[] )
4308 const char *makeflags = getenv( "MAKEFLAGS" );
4309 const char *target;
4310 unsigned int i, j, arch;
4312 if (makeflags) parse_makeflags( makeflags );
4314 i = 1;
4315 while (i < argc)
4317 if (parse_option( argv[i] ))
4319 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4320 argc--;
4322 else i++;
4325 if (relative_dir_mode)
4327 char *relpath;
4329 if (argc != 3)
4331 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4332 exit( 1 );
4334 relpath = get_relative_path( argv[1], argv[2] );
4335 printf( "%s\n", relpath ? relpath : "." );
4336 exit( 0 );
4339 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4341 atexit( cleanup_files );
4342 init_signals( exit_on_signal );
4344 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4345 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4347 top_makefile = parse_makefile( NULL );
4349 target_flags[0] = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4350 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4351 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4352 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4353 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4354 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4355 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4356 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4357 for (i = 0; i < NB_INSTALL_RULES; i++)
4358 top_install[i] = get_expanded_make_var_array( top_makefile, strmake( "TOP_%s", install_variables[i] ));
4360 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4361 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4362 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4363 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4364 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4365 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4366 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4367 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4368 bison = get_expanded_make_variable( top_makefile, "BISON" );
4369 ar = get_expanded_make_variable( top_makefile, "AR" );
4370 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4371 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4372 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4373 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4374 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4375 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4377 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4378 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4379 if (!exe_ext) exe_ext = "";
4380 if (!tools_ext) tools_ext = "";
4382 strarray_add( &archs, get_expanded_make_variable( top_makefile, "HOST_ARCH" ));
4383 strarray_addall( &archs, get_expanded_make_var_array( top_makefile, "PE_ARCHS" ));
4385 arch_dirs[0] = "";
4386 arch_pe_dirs[0] = strmake( "%s-windows/", archs.str[0] );
4387 arch_install_dirs[0] = *dll_ext ? strmake( "$(dlldir)/%s-unix/", archs.str[0] ) : "$(dlldir)/";
4388 strip_progs[0] = "\"$(STRIP)\"";
4390 for (arch = 1; arch < archs.count; arch++)
4392 target = get_expanded_arch_var( top_makefile, "TARGET", arch );
4393 strarray_add( &target_flags[arch], "-b" );
4394 strarray_add( &target_flags[arch], target );
4395 arch_dirs[arch] = strmake( "%s-windows/", archs.str[arch] );
4396 arch_pe_dirs[arch] = arch_dirs[arch];
4397 arch_install_dirs[arch] = strmake( "$(dlldir)/%s", arch_dirs[arch] );
4398 strip_progs[arch] = strmake( "%s-strip", target );
4401 for (arch = 0; arch < archs.count; arch++)
4403 extra_cflags[arch] = get_expanded_arch_var_array( top_makefile, "EXTRACFLAGS", arch );
4404 extra_cflags_extlib[arch] = remove_warning_flags( extra_cflags[arch] );
4405 disabled_dirs[arch] = get_expanded_arch_var_array( top_makefile, "DISABLED_SUBDIRS", arch );
4406 if (!is_multiarch( arch )) continue;
4407 delay_load_flags[arch] = get_expanded_arch_var( top_makefile, "DELAYLOADFLAG", arch );
4408 debug_flags[arch] = get_expanded_arch_var( top_makefile, "DEBUG", arch );
4411 if (*dll_ext)
4413 delay_load_flags[0] = "-Wl,-delayload,";
4414 debug_flags[0] = NULL;
4417 top_makefile->src_dir = root_src_dir;
4418 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4419 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4421 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4423 load_sources( top_makefile );
4424 load_sources( include_makefile );
4425 for (i = 0; i < subdirs.count; i++)
4426 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4428 output_dependencies( top_makefile );
4429 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4431 return 0;