msi: Make TransformView_Create static.
[wine.git] / tools / makedep.c
blob00ebfa27e8f7553c34fd5ce4bd945a9bc82e24c8
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013, 2020 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
34 #include "tools.h"
35 #include "wine/list.h"
37 enum incl_type
39 INCL_NORMAL, /* #include "foo.h" */
40 INCL_SYSTEM, /* #include <foo.h> */
41 INCL_IMPORT, /* idl import "foo.idl" */
42 INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
43 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
44 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
47 struct dependency
49 int line; /* source line where this header is included */
50 enum incl_type type; /* type of include */
51 char *name; /* header name */
54 struct file
56 struct list entry;
57 char *name; /* full file name relative to cwd */
58 void *args; /* custom arguments for makefile rule */
59 unsigned int flags; /* flags (see below) */
60 unsigned int deps_count; /* files in use */
61 unsigned int deps_size; /* total allocated size */
62 struct dependency *deps; /* all header dependencies */
65 struct incl_file
67 struct list entry;
68 struct list hash_entry;
69 struct file *file;
70 char *name;
71 char *filename;
72 char *basename; /* base target name for generated files */
73 char *sourcename; /* source file name for generated headers */
74 struct incl_file *included_by; /* file that included this one */
75 int included_line; /* line where this file was included */
76 enum incl_type type; /* type of include */
77 unsigned int arch; /* architecture for multi-arch files, otherwise 0 */
78 unsigned int use_msvcrt:1; /* put msvcrt headers in the search path? */
79 unsigned int is_external:1; /* file from external library? */
80 struct incl_file *owner;
81 unsigned int files_count; /* files in use */
82 unsigned int files_size; /* total allocated size */
83 struct incl_file **files;
84 struct strarray dependencies; /* file dependencies */
85 struct strarray importlibdeps; /* importlib dependencies */
88 #define FLAG_GENERATED 0x000001 /* generated file */
89 #define FLAG_INSTALL 0x000002 /* file to install */
90 #define FLAG_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 static const char *wayland_scanner;
161 /* per-architecture global variables */
162 static const char *arch_dirs[MAX_ARCHS];
163 static const char *arch_pe_dirs[MAX_ARCHS];
164 static const char *arch_install_dirs[MAX_ARCHS];
165 static const char *strip_progs[MAX_ARCHS];
166 static const char *debug_flags[MAX_ARCHS];
167 static const char *delay_load_flags[MAX_ARCHS];
168 static struct strarray target_flags[MAX_ARCHS];
169 static struct strarray extra_cflags[MAX_ARCHS];
170 static struct strarray extra_cflags_extlib[MAX_ARCHS];
171 static struct strarray disabled_dirs[MAX_ARCHS];
173 struct makefile
175 /* values determined from input makefile */
176 struct strarray vars;
177 struct strarray include_paths;
178 struct strarray include_args;
179 struct strarray define_args;
180 struct strarray unix_cflags;
181 struct strarray programs;
182 struct strarray scripts;
183 struct strarray imports;
184 struct strarray delayimports;
185 struct strarray extradllflags;
186 struct strarray install[NB_INSTALL_RULES];
187 struct strarray extra_targets;
188 struct strarray extra_imports;
189 struct list sources;
190 struct list includes;
191 const char *src_dir;
192 const char *obj_dir;
193 const char *parent_dir;
194 const char *module;
195 const char *testdll;
196 const char *extlib;
197 const char *staticlib;
198 const char *importlib;
199 const char *unixlib;
200 int data_only;
201 int is_win16;
202 int is_exe;
203 int disabled[MAX_ARCHS];
205 /* values generated at output time */
206 struct strarray in_files;
207 struct strarray pot_files;
208 struct strarray test_files;
209 struct strarray clean_files;
210 struct strarray distclean_files;
211 struct strarray maintainerclean_files;
212 struct strarray uninstall_files;
213 struct strarray unixobj_files;
214 struct strarray font_files;
215 struct strarray debug_files;
216 struct strarray dlldata_files;
217 struct strarray phony_targets;
218 struct strarray dependencies;
219 struct strarray object_files[MAX_ARCHS];
220 struct strarray implib_files[MAX_ARCHS];
221 struct strarray ok_files[MAX_ARCHS];
222 struct strarray res_files[MAX_ARCHS];
223 struct strarray all_targets[MAX_ARCHS];
224 struct strarray install_rules[NB_INSTALL_RULES];
227 static struct makefile *top_makefile;
228 static struct makefile *include_makefile;
229 static struct makefile **submakes;
231 static const char separator[] = "### Dependencies";
232 static const char *output_makefile_name = "Makefile";
233 static const char *input_file_name;
234 static const char *output_file_name;
235 static const char *temp_file_name;
236 static int relative_dir_mode;
237 static int silent_rules;
238 static int input_line;
239 static int output_column;
240 static FILE *output_file;
242 static const char Usage[] =
243 "Usage: makedep [options] [directories]\n"
244 "Options:\n"
245 " -R from to Compute the relative path between two directories\n"
246 " -S Generate Automake-style silent rules\n"
247 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
250 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
251 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
252 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
253 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
255 /*******************************************************************
256 * fatal_error
258 static void fatal_error( const char *msg, ... )
260 va_list valist;
261 va_start( valist, msg );
262 if (input_file_name)
264 fprintf( stderr, "%s:", input_file_name );
265 if (input_line) fprintf( stderr, "%d:", input_line );
266 fprintf( stderr, " error: " );
268 else fprintf( stderr, "makedep: error: " );
269 vfprintf( stderr, msg, valist );
270 va_end( valist );
271 exit(1);
275 /*******************************************************************
276 * fatal_perror
278 static void fatal_perror( const char *msg, ... )
280 va_list valist;
281 va_start( valist, msg );
282 if (input_file_name)
284 fprintf( stderr, "%s:", input_file_name );
285 if (input_line) fprintf( stderr, "%d:", input_line );
286 fprintf( stderr, " error: " );
288 else fprintf( stderr, "makedep: error: " );
289 vfprintf( stderr, msg, valist );
290 perror( " " );
291 va_end( valist );
292 exit(1);
296 /*******************************************************************
297 * cleanup_files
299 static void cleanup_files(void)
301 if (temp_file_name) unlink( temp_file_name );
302 if (output_file_name) unlink( output_file_name );
306 /*******************************************************************
307 * exit_on_signal
309 static void exit_on_signal( int sig )
311 exit( 1 ); /* this will call the atexit functions */
315 /*******************************************************************
316 * output
318 static void output( const char *format, ... )
320 int ret;
321 va_list valist;
323 va_start( valist, format );
324 ret = vfprintf( output_file, format, valist );
325 va_end( valist );
326 if (ret < 0) fatal_perror( "output" );
327 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
328 else output_column += ret;
332 /*******************************************************************
333 * strarray_get_value
335 * Find a value in a name/value pair string array.
337 static const char *strarray_get_value( const struct strarray *array, const char *name )
339 int pos, res, min = 0, max = array->count / 2 - 1;
341 while (min <= max)
343 pos = (min + max) / 2;
344 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
345 if (res < 0) min = pos + 1;
346 else max = pos - 1;
348 return NULL;
352 /*******************************************************************
353 * strarray_set_value
355 * Define a value in a name/value pair string array.
357 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
359 int i, pos, res, min = 0, max = array->count / 2 - 1;
361 while (min <= max)
363 pos = (min + max) / 2;
364 if (!(res = strcmp( array->str[pos * 2], name )))
366 /* redefining a variable replaces the previous value */
367 array->str[pos * 2 + 1] = value;
368 return;
370 if (res < 0) min = pos + 1;
371 else max = pos - 1;
373 strarray_add( array, NULL );
374 strarray_add( array, NULL );
375 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
376 array->str[min * 2] = name;
377 array->str[min * 2 + 1] = value;
381 /*******************************************************************
382 * output_filename
384 static void output_filename( const char *name )
386 if (output_column + strlen(name) + 1 > 100)
388 output( " \\\n" );
389 output( " " );
391 else if (output_column) output( " " );
392 output( "%s", name );
396 /*******************************************************************
397 * output_filenames
399 static void output_filenames( struct strarray array )
401 unsigned int i;
403 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
407 /*******************************************************************
408 * output_rm_filenames
410 static void output_rm_filenames( struct strarray array, const char *command )
412 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
413 unsigned int i, len;
415 if (!array.count) return;
416 output( "\t%s", command );
417 for (i = len = 0; i < array.count; i++)
419 if (len > max_cmdline)
421 output( "\n" );
422 output( "\t%s", command );
423 len = 0;
425 output_filename( array.str[i] );
426 len += strlen( array.str[i] ) + 1;
428 output( "\n" );
432 /*******************************************************************
433 * get_extension
435 static char *get_extension( char *filename )
437 char *ext = strrchr( filename, '.' );
438 if (ext && strchr( ext, '/' )) ext = NULL;
439 return ext;
443 /*******************************************************************
444 * get_base_name
446 static const char *get_base_name( const char *name )
448 char *base;
449 if (!strchr( name, '.' )) return name;
450 base = xstrdup( name );
451 *strrchr( base, '.' ) = 0;
452 return base;
456 /*******************************************************************
457 * replace_filename
459 static char *replace_filename( const char *path, const char *name )
461 const char *p;
462 char *ret;
463 size_t len;
465 if (!path) return xstrdup( name );
466 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
467 len = p - path + 1;
468 ret = xmalloc( len + strlen( name ) + 1 );
469 memcpy( ret, path, len );
470 strcpy( ret + len, name );
471 return ret;
475 /*******************************************************************
476 * replace_substr
478 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
480 size_t pos = start - str;
481 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
482 memcpy( ret, str, pos );
483 strcpy( ret + pos, replace );
484 strcat( ret + pos, start + len );
485 return ret;
489 /*******************************************************************
490 * get_relative_path
492 * Determine where the destination path is located relative to the 'from' path.
494 static char *get_relative_path( const char *from, const char *dest )
496 const char *start;
497 char *ret, *p;
498 unsigned int dotdots = 0;
500 /* a path of "." is equivalent to an empty path */
501 if (!strcmp( from, "." )) from = "";
503 for (;;)
505 while (*from == '/') from++;
506 while (*dest == '/') dest++;
507 start = dest; /* save start of next path element */
508 if (!*from) break;
510 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
511 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
513 /* count remaining elements in 'from' */
516 dotdots++;
517 while (*from && *from != '/') from++;
518 while (*from == '/') from++;
520 while (*from);
521 break;
524 if (!start[0] && !dotdots) return NULL; /* empty path */
526 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
527 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
529 if (start[0]) strcpy( p, start );
530 else p[-1] = 0; /* remove trailing slash */
531 return ret;
535 /*******************************************************************
536 * concat_paths
538 static char *concat_paths( const char *base, const char *path )
540 int i, len;
541 char *ret;
543 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
544 if (!path || !path[0]) return xstrdup( base );
545 if (path[0] == '/') return xstrdup( path );
547 len = strlen( base );
548 while (len && base[len - 1] == '/') len--;
549 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
551 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
552 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
553 if (i != len - 1 || base[i] != '.')
555 path += 2;
556 while (*path == '/') path++;
558 /* else ignore "." element */
559 while (i > 0 && base[i - 1] == '/') i--;
560 len = i;
562 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
563 ret = xmalloc( len + strlen( path ) + 2 );
564 memcpy( ret, base, len );
565 ret[len++] = '/';
566 strcpy( ret + len, path );
567 return ret;
571 /*******************************************************************
572 * is_native_arch_disabled
574 * Check if the makefile was disabled for a PE arch that matches the native arch.
576 static int is_native_arch_disabled( struct makefile *make )
578 unsigned int arch;
580 for (arch = 1; arch < archs.count; arch++)
581 if (make->disabled[arch] && !strcmp( archs.str[0], archs.str[arch] ))
582 return 1;
583 return 0;
587 /*******************************************************************
588 * is_multiarch
590 * Check if arch is one of the PE architectures in multiarch.
591 * Also return TRUE for native arch iff there's no PE support.
593 static int is_multiarch( unsigned int arch )
595 return archs.count == 1 || arch;
599 /*******************************************************************
600 * is_using_msvcrt
602 * Check if the files of a makefile use msvcrt by default.
604 static int is_using_msvcrt( struct makefile *make )
606 return make->module || make->testdll;
610 /*******************************************************************
611 * arch_module_name
613 static char *arch_module_name( const char *module, unsigned int arch )
615 return strmake( "%s%s%s", arch_dirs[arch], module, arch ? "" : dll_ext );
619 /*******************************************************************
620 * arch_make_variable
622 static char *arch_make_variable( const char *name, unsigned int arch )
624 return arch ? strmake( "$(%s_%s)", archs.str[arch], name ) : strmake( "$(%s)", name );
628 /*******************************************************************
629 * obj_dir_path
631 static char *obj_dir_path( const struct makefile *make, const char *path )
633 return concat_paths( make->obj_dir, path );
637 /*******************************************************************
638 * src_dir_path
640 static char *src_dir_path( const struct makefile *make, const char *path )
642 if (make->src_dir) return concat_paths( make->src_dir, path );
643 return obj_dir_path( make, path );
647 /*******************************************************************
648 * root_src_dir_path
650 static char *root_src_dir_path( const char *path )
652 return concat_paths( root_src_dir, path );
656 /*******************************************************************
657 * tools_dir_path
659 static char *tools_dir_path( const struct makefile *make, const char *path )
661 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
662 return strmake( "tools/%s", path );
666 /*******************************************************************
667 * tools_path
669 static char *tools_path( const struct makefile *make, const char *name )
671 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
675 /*******************************************************************
676 * strarray_addall_path
678 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
680 unsigned int i;
682 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
686 /*******************************************************************
687 * get_line
689 static char *get_line( FILE *file )
691 static char *buffer;
692 static size_t size;
694 if (!size)
696 size = 1024;
697 buffer = xmalloc( size );
699 if (!fgets( buffer, size, file )) return NULL;
700 input_line++;
702 for (;;)
704 char *p = buffer + strlen(buffer);
705 /* if line is larger than buffer, resize buffer */
706 while (p == buffer + size - 1 && p[-1] != '\n')
708 buffer = xrealloc( buffer, size * 2 );
709 if (!fgets( buffer + size - 1, size + 1, file )) break;
710 p = buffer + strlen(buffer);
711 size *= 2;
713 if (p > buffer && p[-1] == '\n')
715 *(--p) = 0;
716 if (p > buffer && p[-1] == '\r') *(--p) = 0;
717 if (p > buffer && p[-1] == '\\')
719 *(--p) = 0;
720 /* line ends in backslash, read continuation line */
721 if (!fgets( p, size - (p - buffer), file )) return buffer;
722 input_line++;
723 continue;
726 return buffer;
731 /*******************************************************************
732 * hash_filename
734 static unsigned int hash_filename( const char *name )
736 /* FNV-1 hash */
737 unsigned int ret = 2166136261u;
738 while (*name) ret = (ret * 16777619) ^ *name++;
739 return ret % HASH_SIZE;
743 /*******************************************************************
744 * add_file
746 static struct file *add_file( const char *name )
748 struct file *file = xmalloc( sizeof(*file) );
749 memset( file, 0, sizeof(*file) );
750 file->name = xstrdup( name );
751 return file;
755 /*******************************************************************
756 * add_dependency
758 static void add_dependency( struct file *file, const char *name, enum incl_type type )
760 if (file->deps_count >= file->deps_size)
762 file->deps_size *= 2;
763 if (file->deps_size < 16) file->deps_size = 16;
764 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
766 file->deps[file->deps_count].line = input_line;
767 file->deps[file->deps_count].type = type;
768 file->deps[file->deps_count].name = xstrdup( name );
769 file->deps_count++;
773 /*******************************************************************
774 * find_src_file
776 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
778 struct incl_file *file;
780 if (make == include_makefile)
782 unsigned int hash = hash_filename( name );
784 LIST_FOR_EACH_ENTRY( file, &global_includes[hash], struct incl_file, hash_entry )
785 if (!strcmp( name, file->name )) return file;
786 return NULL;
789 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
790 if (!strcmp( name, file->name )) return file;
791 return NULL;
794 /*******************************************************************
795 * find_include_file
797 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
799 struct incl_file *file;
801 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
803 const char *filename = file->filename;
804 if (!filename) continue;
805 if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
807 filename += strlen(make->obj_dir);
808 while (*filename == '/') filename++;
810 if (!strcmp( name, filename )) return file;
812 return NULL;
815 /*******************************************************************
816 * add_include
818 * Add an include file if it doesn't already exists.
820 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
821 const char *name, int line, enum incl_type type )
823 struct incl_file *include;
825 if (parent->files_count >= parent->files_size)
827 parent->files_size *= 2;
828 if (parent->files_size < 16) parent->files_size = 16;
829 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
832 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
833 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
834 goto found;
836 include = xmalloc( sizeof(*include) );
837 memset( include, 0, sizeof(*include) );
838 include->name = xstrdup(name);
839 include->included_by = parent;
840 include->included_line = line;
841 include->type = type;
842 include->use_msvcrt = parent->use_msvcrt;
843 list_add_tail( &make->includes, &include->entry );
844 found:
845 parent->files[parent->files_count++] = include;
846 return include;
850 /*******************************************************************
851 * add_generated_source
853 * Add a generated source file to the list.
855 static struct incl_file *add_generated_source( struct makefile *make, const char *name,
856 const char *filename, unsigned int arch )
858 struct incl_file *file = xmalloc( sizeof(*file) );
860 name = strmake( "%s%s", arch_dirs[arch], name );
861 memset( file, 0, sizeof(*file) );
862 file->file = add_file( name );
863 file->arch = arch;
864 file->name = xstrdup( name );
865 file->basename = xstrdup( filename ? filename : name );
866 file->filename = obj_dir_path( make, file->basename );
867 file->file->flags = FLAG_GENERATED;
868 file->use_msvcrt = is_using_msvcrt( make );
869 list_add_tail( &make->sources, &file->entry );
870 if (make == include_makefile)
872 unsigned int hash = hash_filename( name );
873 list_add_tail( &global_includes[hash], &file->hash_entry );
875 return file;
879 /*******************************************************************
880 * skip_spaces
882 static char *skip_spaces( const char *p )
884 while (*p == ' ' || *p == '\t') p++;
885 return (char *)p;
889 /*******************************************************************
890 * parse_include_directive
892 static void parse_include_directive( struct file *source, char *str )
894 char quote, *include, *p = skip_spaces( str );
896 if (*p != '\"' && *p != '<' ) return;
897 quote = *p++;
898 if (quote == '<') quote = '>';
899 include = p;
900 while (*p && (*p != quote)) p++;
901 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
902 *p = 0;
903 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
907 /*******************************************************************
908 * parse_pragma_directive
910 static void parse_pragma_directive( struct file *source, char *str )
912 char *flag, *p = str;
914 if (*p != ' ' && *p != '\t') return;
915 p = strtok( skip_spaces( p ), " \t" );
916 if (strcmp( p, "makedep" )) return;
918 while ((flag = strtok( NULL, " \t" )))
920 if (!strcmp( flag, "depend" ))
922 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
923 return;
925 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
927 if (strendswith( source->name, ".idl" ))
929 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
930 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
931 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
932 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
933 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
934 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
935 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
936 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
938 else if (strendswith( source->name, ".rc" ))
940 if (!strcmp( flag, "header" )) source->flags |= FLAG_RC_HEADER;
941 else if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
943 else if (strendswith( source->name, ".sfd" ))
945 if (!strcmp( flag, "font" ))
947 struct strarray *array = source->args;
949 if (!array)
951 source->args = array = xmalloc( sizeof(*array) );
952 *array = empty_strarray;
953 source->flags |= FLAG_SFD_FONTS;
955 strarray_add( array, xstrdup( strtok( NULL, "" )));
956 return;
959 else
961 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
962 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
968 /*******************************************************************
969 * parse_cpp_directive
971 static void parse_cpp_directive( struct file *source, char *str )
973 str = skip_spaces( str );
974 if (*str++ != '#') return;
975 str = skip_spaces( str );
977 if (!strncmp( str, "include", 7 ))
978 parse_include_directive( source, str + 7 );
979 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
980 parse_include_directive( source, str + 6 );
981 else if (!strncmp( str, "pragma", 6 ))
982 parse_pragma_directive( source, str + 6 );
986 /*******************************************************************
987 * parse_idl_file
989 static void parse_idl_file( struct file *source, FILE *file )
991 char *buffer, *include;
993 input_line = 0;
995 while ((buffer = get_line( file )))
997 char quote;
998 char *p = skip_spaces( buffer );
1000 if (!strncmp( p, "importlib", 9 ))
1002 p = skip_spaces( p + 9 );
1003 if (*p++ != '(') continue;
1004 p = skip_spaces( p );
1005 if (*p++ != '"') continue;
1006 include = p;
1007 while (*p && (*p != '"')) p++;
1008 if (!*p) fatal_error( "malformed importlib directive\n" );
1009 *p = 0;
1010 add_dependency( source, include, INCL_IMPORTLIB );
1011 continue;
1014 if (!strncmp( p, "import", 6 ))
1016 p = skip_spaces( p + 6 );
1017 if (*p != '"') continue;
1018 include = ++p;
1019 while (*p && (*p != '"')) p++;
1020 if (!*p) fatal_error( "malformed import directive\n" );
1021 *p = 0;
1022 add_dependency( source, include, INCL_IMPORT );
1023 continue;
1026 /* check for #include inside cpp_quote */
1027 if (!strncmp( p, "cpp_quote", 9 ))
1029 p = skip_spaces( p + 9 );
1030 if (*p++ != '(') continue;
1031 p = skip_spaces( p );
1032 if (*p++ != '"') continue;
1033 if (*p++ != '#') continue;
1034 p = skip_spaces( p );
1035 if (strncmp( p, "include", 7 )) continue;
1036 p = skip_spaces( p + 7 );
1037 if (*p == '\\' && p[1] == '"')
1039 p += 2;
1040 quote = '"';
1042 else
1044 if (*p++ != '<' ) continue;
1045 quote = '>';
1047 include = p;
1048 while (*p && (*p != quote)) p++;
1049 if (!*p || (quote == '"' && p[-1] != '\\'))
1050 fatal_error( "malformed #include directive inside cpp_quote\n" );
1051 if (quote == '"') p--; /* remove backslash */
1052 *p = 0;
1053 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1054 continue;
1057 parse_cpp_directive( source, p );
1061 /*******************************************************************
1062 * parse_c_file
1064 static void parse_c_file( struct file *source, FILE *file )
1066 char *buffer;
1068 input_line = 0;
1069 while ((buffer = get_line( file )))
1071 parse_cpp_directive( source, buffer );
1076 /*******************************************************************
1077 * parse_rc_file
1079 static void parse_rc_file( struct file *source, FILE *file )
1081 char *buffer, *include;
1083 input_line = 0;
1084 while ((buffer = get_line( file )))
1086 char quote;
1087 char *p = skip_spaces( buffer );
1089 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1091 p = skip_spaces( p + 2 );
1092 if (strncmp( p, "@makedep:", 9 )) continue;
1093 p = skip_spaces( p + 9 );
1094 quote = '"';
1095 if (*p == quote)
1097 include = ++p;
1098 while (*p && *p != quote) p++;
1100 else
1102 include = p;
1103 while (*p && *p != ' ' && *p != '\t' && *p != '*') p++;
1105 if (!*p)
1106 fatal_error( "malformed makedep comment\n" );
1107 *p = 0;
1108 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1109 continue;
1112 parse_cpp_directive( source, buffer );
1117 /*******************************************************************
1118 * parse_in_file
1120 static void parse_in_file( struct file *source, FILE *file )
1122 char *p, *buffer;
1124 /* make sure it gets rebuilt when the version changes */
1125 add_dependency( source, "config.h", INCL_SYSTEM );
1127 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1129 input_line = 0;
1130 while ((buffer = get_line( file )))
1132 if (strncmp( buffer, ".TH", 3 )) continue;
1133 p = skip_spaces( buffer + 3 );
1134 if (!(p = strtok( p, " \t" ))) continue; /* program name */
1135 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1136 source->args = xstrdup( p );
1137 return;
1142 /*******************************************************************
1143 * parse_sfd_file
1145 static void parse_sfd_file( struct file *source, FILE *file )
1147 char *p, *eol, *buffer;
1149 input_line = 0;
1150 while ((buffer = get_line( file )))
1152 if (strncmp( buffer, "UComments:", 10 )) continue;
1153 p = buffer + 10;
1154 while (*p == ' ') p++;
1155 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1157 p++;
1158 buffer[strlen(buffer) - 1] = 0;
1160 while ((eol = strstr( p, "+AAoA" )))
1162 *eol = 0;
1163 p = skip_spaces( p );
1164 if (*p++ == '#')
1166 p = skip_spaces( p );
1167 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1169 p = eol + 5;
1171 p = skip_spaces( p );
1172 if (*p++ != '#') return;
1173 p = skip_spaces( p );
1174 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1175 return;
1180 static const struct
1182 const char *ext;
1183 void (*parse)( struct file *file, FILE *f );
1184 } parse_functions[] =
1186 { ".c", parse_c_file },
1187 { ".h", parse_c_file },
1188 { ".inl", parse_c_file },
1189 { ".l", parse_c_file },
1190 { ".m", parse_c_file },
1191 { ".rh", parse_c_file },
1192 { ".x", parse_c_file },
1193 { ".y", parse_c_file },
1194 { ".idl", parse_idl_file },
1195 { ".rc", parse_rc_file },
1196 { ".in", parse_in_file },
1197 { ".sfd", parse_sfd_file }
1200 /*******************************************************************
1201 * load_file
1203 static struct file *load_file( const char *name )
1205 struct file *file;
1206 FILE *f;
1207 unsigned int i, hash = hash_filename( name );
1209 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1210 if (!strcmp( name, file->name )) return file;
1212 if (!(f = fopen( name, "r" ))) return NULL;
1214 file = add_file( name );
1215 list_add_tail( &files[hash], &file->entry );
1216 input_file_name = file->name;
1217 input_line = 0;
1219 for (i = 0; i < ARRAY_SIZE(parse_functions); i++)
1221 if (!strendswith( name, parse_functions[i].ext )) continue;
1222 parse_functions[i].parse( file, f );
1223 break;
1226 fclose( f );
1227 input_file_name = NULL;
1229 return file;
1233 /*******************************************************************
1234 * open_include_path_file
1236 * Open a file from a directory on the include path.
1238 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1239 const char *name, char **filename )
1241 char *src_path = concat_paths( dir, name );
1242 struct file *ret = load_file( src_path );
1244 if (ret) *filename = src_path;
1245 return ret;
1249 /*******************************************************************
1250 * open_file_same_dir
1252 * Open a file in the same directory as the parent.
1254 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1256 char *src_path = replace_filename( parent->file->name, name );
1257 struct file *ret = load_file( src_path );
1259 if (ret) *filename = replace_filename( parent->filename, name );
1260 return ret;
1264 /*******************************************************************
1265 * open_same_dir_generated_file
1267 * Open a generated_file in the same directory as the parent.
1269 static struct file *open_same_dir_generated_file( const struct makefile *make,
1270 const struct incl_file *parent, struct incl_file *file,
1271 const char *ext, const char *src_ext )
1273 char *filename;
1274 struct file *ret = NULL;
1276 if (strendswith( file->name, ext ) &&
1277 (ret = open_file_same_dir( parent, replace_extension( file->name, ext, src_ext ), &filename )))
1279 file->sourcename = filename;
1280 file->filename = obj_dir_path( make, replace_filename( parent->name, file->name ));
1282 return ret;
1286 /*******************************************************************
1287 * open_local_file
1289 * Open a file in the source directory of the makefile.
1291 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1293 char *src_path = src_dir_path( make, path );
1294 struct file *ret = load_file( src_path );
1296 /* if not found, try parent dir */
1297 if (!ret && make->parent_dir)
1299 free( src_path );
1300 path = strmake( "%s/%s", make->parent_dir, path );
1301 src_path = src_dir_path( make, path );
1302 ret = load_file( src_path );
1305 if (ret) *filename = src_path;
1306 return ret;
1310 /*******************************************************************
1311 * open_local_generated_file
1313 * Open a generated file in the directory of the makefile.
1315 static struct file *open_local_generated_file( const struct makefile *make, struct incl_file *file,
1316 const char *ext, const char *src_ext )
1318 struct incl_file *include;
1320 if (strendswith( file->name, ext ) &&
1321 (include = find_src_file( make, replace_extension( file->name, ext, src_ext ) )))
1323 file->sourcename = include->filename;
1324 file->filename = obj_dir_path( make, file->name );
1325 return include->file;
1327 return NULL;
1331 /*******************************************************************
1332 * open_global_file
1334 * Open a file in the top-level source directory.
1336 static struct file *open_global_file( const char *path, char **filename )
1338 char *src_path = root_src_dir_path( path );
1339 struct file *ret = load_file( src_path );
1341 if (ret) *filename = src_path;
1342 return ret;
1346 /*******************************************************************
1347 * open_global_header
1349 * Open a file in the global include source directory.
1351 static struct file *open_global_header( const char *path, char **filename )
1353 struct incl_file *include = find_src_file( include_makefile, path );
1355 if (!include) return NULL;
1356 *filename = include->filename;
1357 return include->file;
1361 /*******************************************************************
1362 * open_global_generated_file
1364 * Open a generated file in the top-level source directory.
1366 static struct file *open_global_generated_file( const struct makefile *make, struct incl_file *file,
1367 const char *ext, const char *src_ext )
1369 struct incl_file *include;
1371 if (strendswith( file->name, ext ) &&
1372 (include = find_src_file( include_makefile, replace_extension( file->name, ext, src_ext ) )))
1374 file->sourcename = include->filename;
1375 file->filename = strmake( "include/%s", file->name );
1376 return include->file;
1378 return NULL;
1382 /*******************************************************************
1383 * open_src_file
1385 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1387 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1389 if (!file) fatal_perror( "open %s", pFile->name );
1390 return file;
1394 /*******************************************************************
1395 * find_importlib_module
1397 static struct makefile *find_importlib_module( const char *name )
1399 unsigned int i, len;
1401 for (i = 0; i < subdirs.count; i++)
1403 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1404 len = strlen(submakes[i]->obj_dir);
1405 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1406 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1408 return NULL;
1412 /*******************************************************************
1413 * open_include_file
1415 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1417 struct file *file = NULL;
1418 unsigned int i, len;
1420 errno = ENOENT;
1422 /* check for generated files */
1423 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1424 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1425 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1426 if (convert && rsvg && icotool)
1428 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1429 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1430 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1432 if ((file = open_local_generated_file( make, pFile, "-client-protocol.h", ".xml" ))) return file;
1434 /* check for extra targets */
1435 if (strarray_exists( &make->extra_targets, pFile->name ))
1437 pFile->sourcename = src_dir_path( make, pFile->name );
1438 pFile->filename = obj_dir_path( make, pFile->name );
1439 return NULL;
1442 /* now try in source dir */
1443 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1445 /* check for global importlib (module dependency) */
1446 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1448 pFile->filename = pFile->name;
1449 return NULL;
1452 /* check for generated files in global includes */
1453 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1454 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1455 if (strendswith( pFile->name, "tmpl.h" ) &&
1456 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1458 /* check in global includes source dir */
1459 if ((file = open_global_header( pFile->name, &pFile->filename ))) return file;
1461 /* check in global msvcrt includes */
1462 if (pFile->use_msvcrt &&
1463 (file = open_global_header( strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1464 return file;
1466 /* now search in include paths */
1467 for (i = 0; i < make->include_paths.count; i++)
1469 const char *dir = make->include_paths.str[i];
1471 if (root_src_dir)
1473 len = strlen( root_src_dir );
1474 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1476 while (dir[len] == '/') len++;
1477 file = open_global_file( concat_paths( dir + len, pFile->name ), &pFile->filename );
1480 else
1482 if (*dir == '/') continue;
1483 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1485 if (!file) continue;
1486 pFile->is_external = 1;
1487 return file;
1490 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1492 /* try in src file directory */
1493 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1494 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1495 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1497 pFile->is_external = pFile->included_by->is_external;
1498 return file;
1501 if (make->extlib) return NULL; /* ignore missing files in external libs */
1503 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1504 perror( pFile->name );
1505 pFile = pFile->included_by;
1506 while (pFile && pFile->included_by)
1508 const char *parent = pFile->included_by->sourcename;
1509 if (!parent) parent = pFile->included_by->file->name;
1510 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1511 parent, pFile->included_line, pFile->name );
1512 pFile = pFile->included_by;
1514 exit(1);
1518 /*******************************************************************
1519 * add_all_includes
1521 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1523 unsigned int i;
1525 for (i = 0; i < file->deps_count; i++)
1527 switch (file->deps[i].type)
1529 case INCL_NORMAL:
1530 case INCL_IMPORT:
1531 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1532 break;
1533 case INCL_IMPORTLIB:
1534 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1535 break;
1536 case INCL_SYSTEM:
1537 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1538 break;
1539 case INCL_CPP_QUOTE:
1540 case INCL_CPP_QUOTE_SYSTEM:
1541 break;
1547 /*******************************************************************
1548 * parse_file
1550 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1552 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1554 if (!file) return;
1556 source->file = file;
1557 source->files_count = 0;
1558 source->files_size = file->deps_count;
1559 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1561 if (strendswith( file->name, ".m" )) file->flags |= FLAG_C_UNIX;
1562 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1563 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1565 if (source->sourcename)
1567 if (strendswith( source->sourcename, ".idl" ))
1569 unsigned int i;
1571 /* generated .h file always includes these */
1572 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1573 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1574 for (i = 0; i < file->deps_count; i++)
1576 switch (file->deps[i].type)
1578 case INCL_IMPORT:
1579 if (strendswith( file->deps[i].name, ".idl" ))
1580 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1581 file->deps[i].line, INCL_NORMAL );
1582 else
1583 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1584 break;
1585 case INCL_CPP_QUOTE:
1586 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1587 break;
1588 case INCL_CPP_QUOTE_SYSTEM:
1589 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1590 break;
1591 case INCL_NORMAL:
1592 case INCL_SYSTEM:
1593 case INCL_IMPORTLIB:
1594 break;
1597 return;
1599 if (strendswith( source->sourcename, ".y" ))
1600 return; /* generated .tab.h doesn't include anything */
1603 add_all_includes( make, source, file );
1607 /*******************************************************************
1608 * add_src_file
1610 * Add a source file to the list.
1612 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1614 struct incl_file *file = xmalloc( sizeof(*file) );
1616 memset( file, 0, sizeof(*file) );
1617 file->name = xstrdup(name);
1618 file->use_msvcrt = is_using_msvcrt( make );
1619 file->is_external = !!make->extlib;
1620 list_add_tail( &make->sources, &file->entry );
1621 if (make == include_makefile)
1623 unsigned int hash = hash_filename( name );
1624 list_add_tail( &global_includes[hash], &file->hash_entry );
1626 parse_file( make, file, 1 );
1627 return file;
1631 /*******************************************************************
1632 * open_input_makefile
1634 static FILE *open_input_makefile( const struct makefile *make )
1636 FILE *ret;
1638 if (make->obj_dir)
1639 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1640 else
1641 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1643 input_line = 0;
1644 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1645 return ret;
1649 /*******************************************************************
1650 * get_make_variable
1652 static const char *get_make_variable( const struct makefile *make, const char *name )
1654 const char *ret;
1656 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1657 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1658 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1659 return NULL;
1663 /*******************************************************************
1664 * get_expanded_make_variable
1666 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1668 const char *var;
1669 char *p, *end, *expand, *tmp;
1671 var = get_make_variable( make, name );
1672 if (!var) return NULL;
1674 p = expand = xstrdup( var );
1675 while ((p = strchr( p, '$' )))
1677 if (p[1] == '(')
1679 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1680 *end++ = 0;
1681 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1682 var = get_make_variable( make, p + 2 );
1683 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1684 /* switch to the new string */
1685 p = tmp + (p - expand);
1686 free( expand );
1687 expand = tmp;
1689 else if (p[1] == '{') /* don't expand ${} variables */
1691 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1692 p = end + 1;
1694 else if (p[1] == '$')
1696 p += 2;
1698 else fatal_error( "syntax error in '%s'\n", expand );
1701 /* consider empty variables undefined */
1702 p = skip_spaces( expand );
1703 if (*p) return expand;
1704 free( expand );
1705 return NULL;
1709 /*******************************************************************
1710 * get_expanded_make_var_array
1712 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1714 struct strarray ret = empty_strarray;
1715 char *value, *token;
1717 if ((value = get_expanded_make_variable( make, name )))
1718 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1719 strarray_add( &ret, token );
1720 return ret;
1724 /*******************************************************************
1725 * get_expanded_file_local_var
1727 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1728 const char *name )
1730 char *p, *var = strmake( "%s_%s", file, name );
1732 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1733 return get_expanded_make_var_array( make, var );
1737 /*******************************************************************
1738 * get_expanded_arch_var
1740 static char *get_expanded_arch_var( const struct makefile *make, const char *name, int arch )
1742 return get_expanded_make_variable( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1746 /*******************************************************************
1747 * get_expanded_arch_var_array
1749 static struct strarray get_expanded_arch_var_array( const struct makefile *make, const char *name, int arch )
1751 return get_expanded_make_var_array( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1755 /*******************************************************************
1756 * set_make_variable
1758 static int set_make_variable( struct strarray *array, const char *assignment )
1760 char *p, *name;
1762 p = name = xstrdup( assignment );
1763 while (isalnum(*p) || *p == '_') p++;
1764 if (name == p) return 0; /* not a variable */
1765 if (isspace(*p))
1767 *p++ = 0;
1768 p = skip_spaces( p );
1770 if (*p != '=') return 0; /* not an assignment */
1771 *p++ = 0;
1772 p = skip_spaces( p );
1774 strarray_set_value( array, name, p );
1775 return 1;
1779 /*******************************************************************
1780 * parse_makefile
1782 static struct makefile *parse_makefile( const char *path )
1784 char *buffer;
1785 FILE *file;
1786 struct makefile *make = xmalloc( sizeof(*make) );
1788 memset( make, 0, sizeof(*make) );
1789 make->obj_dir = path;
1790 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1791 if (path && !strcmp( path, "include" )) include_makefile = make;
1793 file = open_input_makefile( make );
1794 while ((buffer = get_line( file )))
1796 if (!strncmp( buffer, separator, strlen(separator) )) break;
1797 if (*buffer == '\t') continue; /* command */
1798 buffer = skip_spaces( buffer );
1799 if (*buffer == '#') continue; /* comment */
1800 set_make_variable( &make->vars, buffer );
1802 fclose( file );
1803 input_file_name = NULL;
1804 return make;
1808 /*******************************************************************
1809 * add_generated_sources
1811 static void add_generated_sources( struct makefile *make )
1813 unsigned int i, arch;
1814 struct incl_file *source, *next, *file, *dlldata = NULL;
1815 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1817 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1819 for (arch = 0; arch < archs.count; arch++)
1821 if (!is_multiarch( arch )) continue;
1822 if (source->file->flags & FLAG_IDL_CLIENT)
1824 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL, arch );
1825 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1826 add_all_includes( make, file, file->file );
1828 if (source->file->flags & FLAG_IDL_SERVER)
1830 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL, arch );
1831 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1832 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1833 add_all_includes( make, file, file->file );
1835 if (source->file->flags & FLAG_IDL_IDENT)
1837 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL, arch );
1838 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1839 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1840 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1841 add_all_includes( make, file, file->file );
1843 if (source->file->flags & FLAG_IDL_PROXY)
1845 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL, arch );
1846 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1847 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1848 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1849 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1850 add_all_includes( make, file, file->file );
1852 if (source->file->flags & FLAG_IDL_TYPELIB)
1854 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL, arch );
1856 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1858 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL, arch );
1860 if (source->file->flags & FLAG_IDL_REGISTER)
1862 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL, arch );
1866 /* now the arch-independent files */
1868 if ((source->file->flags & FLAG_IDL_PROXY) && !dlldata)
1870 dlldata = add_generated_source( make, "dlldata.o", "dlldata.c", 0 );
1871 add_dependency( dlldata->file, "objbase.h", INCL_NORMAL );
1872 add_dependency( dlldata->file, "rpcproxy.h", INCL_NORMAL );
1873 add_all_includes( make, dlldata, dlldata->file );
1875 if (source->file->flags & FLAG_IDL_HEADER)
1877 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1879 if (!source->file->flags && strendswith( source->name, ".idl" ))
1881 if (!strncmp( source->name, "wine/", 5 )) continue;
1882 source->file->flags = FLAG_IDL_HEADER | FLAG_INSTALL;
1883 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1885 if (strendswith( source->name, ".x" ))
1887 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL, 0 );
1889 if (strendswith( source->name, ".y" ))
1891 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL, 0 );
1892 /* steal the includes list from the source file */
1893 file->files_count = source->files_count;
1894 file->files_size = source->files_size;
1895 file->files = source->files;
1896 source->files_count = source->files_size = 0;
1897 source->files = NULL;
1899 if (strendswith( source->name, ".l" ))
1901 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL, 0 );
1902 /* steal the includes list from the source file */
1903 file->files_count = source->files_count;
1904 file->files_size = source->files_size;
1905 file->files = source->files;
1906 source->files_count = source->files_size = 0;
1907 source->files = NULL;
1909 if (strendswith( source->name, ".po" ))
1911 if (!make->disabled[0])
1912 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1914 if (strendswith( source->name, ".spec" ))
1916 char *obj = replace_extension( source->name, ".spec", "" );
1917 strarray_addall_uniq( &make->extra_imports,
1918 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1920 if (strendswith( source->name, ".xml" ))
1922 char *code_name = replace_extension( source->name , ".xml", "-protocol.c" );
1923 char *header_name = replace_extension( source->name , ".xml", "-client-protocol.h" );
1925 file = add_generated_source( make, code_name, NULL, 0 );
1926 file->file->flags |= FLAG_C_UNIX;
1927 file->use_msvcrt = 0;
1928 file = add_generated_source( make, header_name, NULL, 0 );
1929 file->file->flags |= FLAG_C_UNIX;
1930 file->use_msvcrt = 0;
1932 free( code_name );
1933 free( header_name );
1936 if (make->testdll)
1938 for (arch = 0; arch < archs.count; arch++)
1940 if (!is_multiarch( arch )) continue;
1941 file = add_generated_source( make, "testlist.o", "testlist.c", arch );
1942 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1943 add_all_includes( make, file, file->file );
1946 for (i = 0; i < objs.count; i++)
1948 /* default to .c for unknown extra object files */
1949 if (strendswith( objs.str[i], ".o" ))
1951 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ), 0);
1952 file->file->flags |= FLAG_C_UNIX;
1953 file->use_msvcrt = 0;
1955 else if (strendswith( objs.str[i], ".res" ))
1956 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL, 0 );
1957 else
1958 add_generated_source( make, objs.str[i], NULL, 0 );
1963 /*******************************************************************
1964 * create_dir
1966 static void create_dir( const char *dir )
1968 char *p, *path;
1970 p = path = xstrdup( dir );
1971 while ((p = strchr( p, '/' )))
1973 *p = 0;
1974 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1975 *p++ = '/';
1976 while (*p == '/') p++;
1978 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1979 free( path );
1983 /*******************************************************************
1984 * create_file_directories
1986 * Create the base directories of all the files.
1988 static void create_file_directories( const struct makefile *make, struct strarray files )
1990 struct strarray subdirs = empty_strarray;
1991 unsigned int i;
1992 char *dir;
1994 for (i = 0; i < files.count; i++)
1996 if (!strchr( files.str[i], '/' )) continue;
1997 dir = obj_dir_path( make, files.str[i] );
1998 *strrchr( dir, '/' ) = 0;
1999 strarray_add_uniq( &subdirs, dir );
2002 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2006 /*******************************************************************
2007 * output_filenames_obj_dir
2009 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
2011 unsigned int i;
2013 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
2017 /*******************************************************************
2018 * get_dependencies
2020 static void get_dependencies( struct incl_file *file, struct incl_file *source )
2022 unsigned int i;
2024 if (!file->filename) return;
2026 if (file != source)
2028 if (file->owner == source) return; /* already processed */
2029 if (file->type == INCL_IMPORTLIB)
2031 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
2032 return; /* library is imported only when building a typelib */
2033 strarray_add( &source->importlibdeps, file->filename );
2035 else strarray_add( &source->dependencies, file->filename );
2036 file->owner = source;
2038 /* sanity checks */
2039 if (!strcmp( file->filename, "include/config.h" ) &&
2040 file != source->files[0] && !source->is_external)
2042 input_file_name = source->filename;
2043 input_line = 0;
2044 for (i = 0; i < source->file->deps_count; i++)
2046 if (!strcmp( source->file->deps[i].name, file->name ))
2047 input_line = source->file->deps[i].line;
2049 fatal_error( "%s must be included before other headers\n", file->name );
2053 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
2057 /*******************************************************************
2058 * get_local_dependencies
2060 * Get the local dependencies of a given target.
2062 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
2063 struct strarray targets )
2065 unsigned int i;
2066 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2068 for (i = 0; i < deps.count; i++)
2070 if (strarray_exists( &targets, deps.str[i] ))
2071 deps.str[i] = obj_dir_path( make, deps.str[i] );
2072 else
2073 deps.str[i] = src_dir_path( make, deps.str[i] );
2075 return deps;
2079 /*******************************************************************
2080 * get_static_lib
2082 * Find the makefile that builds the named static library (which may be an import lib).
2084 static struct makefile *get_static_lib( const char *name, unsigned int arch )
2086 unsigned int i;
2088 for (i = 0; i < subdirs.count; i++)
2090 if (submakes[i]->importlib && !strcmp( submakes[i]->importlib, name )) return submakes[i];
2091 if (!submakes[i]->staticlib) continue;
2092 if (submakes[i]->disabled[arch]) continue;
2093 if (strncmp( submakes[i]->staticlib, "lib", 3 )) continue;
2094 if (strncmp( submakes[i]->staticlib + 3, name, strlen(name) )) continue;
2095 if (strcmp( submakes[i]->staticlib + 3 + strlen(name), ".a" )) continue;
2096 return submakes[i];
2098 return NULL;
2102 /*******************************************************************
2103 * get_native_unix_lib
2105 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2107 if (!make->unixlib) return NULL;
2108 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2109 if (make->unixlib[strlen(name)] != '.') return NULL;
2110 return obj_dir_path( make, make->unixlib );
2114 /*******************************************************************
2115 * get_parent_makefile
2117 static struct makefile *get_parent_makefile( struct makefile *make )
2119 char *dir, *p;
2120 unsigned int i;
2122 if (!make->obj_dir) return NULL;
2123 dir = xstrdup( make->obj_dir );
2124 if (!(p = strrchr( dir, '/' ))) return NULL;
2125 *p = 0;
2126 for (i = 0; i < subdirs.count; i++)
2127 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2128 return NULL;
2132 /*******************************************************************
2133 * needs_delay_lib
2135 static int needs_delay_lib( const struct makefile *make, unsigned int arch )
2137 if (delay_load_flags[arch]) return 0;
2138 if (!make->importlib) return 0;
2139 return strarray_exists( &delay_import_libs, make->importlib );
2143 /*******************************************************************
2144 * add_unix_libraries
2146 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2148 struct strarray ret = empty_strarray;
2149 struct strarray all_libs = empty_strarray;
2150 unsigned int i, j;
2152 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2153 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
2155 for (i = 0; i < all_libs.count; i++)
2157 const char *lib = NULL;
2159 if (!strncmp( all_libs.str[i], "-l", 2 ))
2161 for (j = 0; j < subdirs.count; j++)
2163 if (make == submakes[j]) continue;
2164 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2167 if (lib)
2169 strarray_add( deps, lib );
2170 strarray_add( &ret, lib );
2172 else strarray_add( &ret, all_libs.str[i] );
2175 strarray_addall( &ret, libs );
2176 return ret;
2180 /*******************************************************************
2181 * is_crt_module
2183 static int is_crt_module( const char *file )
2185 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2189 /*******************************************************************
2190 * get_default_crt
2192 static const char *get_default_crt( const struct makefile *make )
2194 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2195 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2199 /*******************************************************************
2200 * get_crt_define
2202 static const char *get_crt_define( const struct makefile *make )
2204 const char *crt_dll = NULL;
2205 unsigned int i, version = 0;
2207 for (i = 0; i < make->imports.count; i++)
2209 if (!is_crt_module( make->imports.str[i] )) continue;
2210 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2211 crt_dll, make->imports.str[i] );
2212 crt_dll = make->imports.str[i];
2215 if (!crt_dll)
2217 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2218 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2220 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2221 sscanf( crt_dll, "msvcr%u", &version );
2222 return strmake( "-D_MSVCR_VER=%u", version );
2226 /*******************************************************************
2227 * get_default_imports
2229 static struct strarray get_default_imports( const struct makefile *make, struct strarray imports )
2231 struct strarray ret = empty_strarray;
2232 const char *crt_dll = get_default_crt( make );
2233 unsigned int i;
2235 for (i = 0; i < imports.count; i++)
2236 if (is_crt_module( imports.str[i] ))
2237 crt_dll = imports.str[i];
2239 strarray_add( &ret, "winecrt0" );
2240 if (crt_dll) strarray_add( &ret, crt_dll );
2242 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2243 strarray_add( &ret, "kernel" );
2245 strarray_add( &ret, "kernel32" );
2246 strarray_add( &ret, "ntdll" );
2247 return ret;
2250 enum import_type
2252 IMPORT_TYPE_DIRECT,
2253 IMPORT_TYPE_DELAYED,
2254 IMPORT_TYPE_DEFAULT,
2257 /*******************************************************************
2258 * add_import_libs
2260 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2261 struct strarray imports, enum import_type type, unsigned int arch )
2263 struct strarray ret = empty_strarray;
2264 unsigned int i;
2266 for (i = 0; i < imports.count; i++)
2268 const char *name = imports.str[i];
2269 struct makefile *submake;
2271 /* add crt import lib only when adding the default imports libs */
2272 if (is_crt_module( imports.str[i] ) && type != IMPORT_TYPE_DEFAULT) continue;
2274 if (name[0] == '-')
2276 switch (name[1])
2278 case 'L': strarray_add( &ret, name ); continue;
2279 case 'l': name += 2; break;
2280 default: continue;
2283 else name = get_base_name( name );
2285 if ((submake = get_static_lib( name, arch )))
2287 const char *ext = (type == IMPORT_TYPE_DELAYED && !delay_load_flags[arch]) ? ".delay.a" : ".a";
2288 const char *lib = obj_dir_path( submake, strmake( "%slib%s%s", arch_dirs[arch], name, ext ));
2289 strarray_add_uniq( deps, lib );
2290 strarray_add( &ret, lib );
2292 else strarray_add( &ret, strmake( "-l%s", name ));
2294 return ret;
2298 /*******************************************************************
2299 * add_install_rule
2301 static void add_install_rule( struct makefile *make, const char *target, unsigned int arch,
2302 const char *file, const char *dest )
2304 unsigned int i;
2306 if (make->disabled[arch]) return;
2308 for (i = 0; i < NB_INSTALL_RULES; i++)
2310 if (strarray_exists( &make->install[i], target ) ||
2311 strarray_exists( &top_install[i], make->obj_dir ) ||
2312 strarray_exists( &top_install[i], obj_dir_path( make, target )))
2314 strarray_add( &make->install_rules[i], file );
2315 strarray_add( &make->install_rules[i], dest );
2316 break;
2322 /*******************************************************************
2323 * get_include_install_path
2325 * Determine the installation path for a given include file.
2327 static const char *get_include_install_path( const char *name )
2329 if (!strncmp( name, "wine/", 5 )) return name + 5;
2330 if (!strncmp( name, "msvcrt/", 7 )) return name;
2331 return strmake( "windows/%s", name );
2335 /*******************************************************************
2336 * get_source_defines
2338 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2339 const char *obj )
2341 unsigned int i;
2342 struct strarray ret = empty_strarray;
2344 strarray_addall( &ret, make->include_args );
2345 if (source->use_msvcrt)
2347 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2348 for (i = 0; i < make->include_paths.count; i++)
2349 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2350 strarray_add( &ret, get_crt_define( make ));
2352 strarray_addall( &ret, make->define_args );
2353 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2354 if (source->file->flags & FLAG_C_UNIX) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2355 return ret;
2359 /*******************************************************************
2360 * remove_warning_flags
2362 static struct strarray remove_warning_flags( struct strarray flags )
2364 unsigned int i;
2365 struct strarray ret = empty_strarray;
2367 for (i = 0; i < flags.count; i++)
2368 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2369 strarray_add( &ret, flags.str[i] );
2370 return ret;
2374 /*******************************************************************
2375 * get_debug_file
2377 static const char *get_debug_file( struct makefile *make, const char *name, unsigned int arch )
2379 const char *debug_file = NULL;
2380 if (!debug_flags[arch]) return NULL;
2381 if (!strcmp( debug_flags[arch], "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2382 else if (!strncmp( debug_flags[arch], "split", 5 )) debug_file = strmake( "%s.debug", name );
2383 if (debug_file) strarray_add( &make->debug_files, debug_file );
2384 return debug_file;
2388 /*******************************************************************
2389 * cmd_prefix
2391 static const char *cmd_prefix( const char *cmd )
2393 if (!silent_rules) return "";
2394 return strmake( "$(quiet_%s)", cmd );
2398 /*******************************************************************
2399 * output_winegcc_command
2401 static void output_winegcc_command( struct makefile *make, unsigned int arch )
2403 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2404 output_filename( "--wine-objdir ." );
2405 if (tools_dir)
2407 output_filename( "--winebuild" );
2408 output_filename( tools_path( make, "winebuild" ));
2410 output_filenames( target_flags[arch] );
2411 if (arch) return;
2412 output_filename( "-mno-cygwin" );
2413 output_filenames( lddll_flags );
2417 /*******************************************************************
2418 * output_symlink_rule
2420 * Output a rule to create a symlink.
2422 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2424 const char *name = strrchr( link_name, '/' );
2425 char *dir = NULL;
2427 if (name)
2429 dir = xstrdup( link_name );
2430 dir[name - link_name] = 0;
2433 output( "\t%s", cmd_prefix( "LN" ));
2434 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2435 output( "rm -f %s && ", link_name );
2437 /* dest path with a directory needs special handling if ln -s isn't supported */
2438 if (dir && strcmp( ln_s, "ln -s" ))
2439 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2440 else
2441 output( "%s %s %s\n", ln_s, src_name, link_name );
2443 free( dir );
2447 /*******************************************************************
2448 * output_srcdir_symlink
2450 * Output rule to create a symlink back to the source directory, for source files
2451 * that are needed at run-time.
2453 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2455 char *src_file, *dst_file, *src_name;
2457 if (!make->src_dir) return;
2458 src_file = src_dir_path( make, obj );
2459 dst_file = obj_dir_path( make, obj );
2460 output( "%s: %s\n", dst_file, src_file );
2462 src_name = src_file;
2463 if (src_name[0] != '/' && make->obj_dir)
2464 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2466 output_symlink_rule( src_name, dst_file, 0 );
2467 strarray_add( &make->all_targets[0], obj );
2471 /*******************************************************************
2472 * output_install_commands
2474 static void output_install_commands( struct makefile *make, struct strarray files )
2476 unsigned int i, arch;
2477 char *install_sh = root_src_dir_path( "tools/install-sh" );
2479 for (i = 0; i < files.count; i += 2)
2481 const char *file = files.str[i];
2482 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2483 char type = *files.str[i + 1];
2485 switch (type)
2487 case '1': case '2': case '3': case '4': case '5':
2488 case '6': case '7': case '8': case '9': /* arch-dependent program */
2489 arch = type - '0';
2490 output( "\tSTRIPPROG=%s %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2491 strip_progs[arch], install_sh, obj_dir_path( make, file ), dest );
2492 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2493 break;
2494 case 'd': /* data file */
2495 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2496 install_sh, obj_dir_path( make, file ), dest );
2497 break;
2498 case 'D': /* data file in source dir */
2499 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2500 install_sh, src_dir_path( make, file ), dest );
2501 break;
2502 case '0': /* native arch program file */
2503 case 'p': /* program file */
2504 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2505 install_sh, obj_dir_path( make, file ), dest );
2506 break;
2507 case 's': /* script */
2508 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2509 install_sh, obj_dir_path( make, file ), dest );
2510 break;
2511 case 'S': /* script in source dir */
2512 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2513 install_sh, src_dir_path( make, file ), dest );
2514 break;
2515 case 't': /* script in tools dir */
2516 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2517 install_sh, tools_dir_path( make, files.str[i] ), dest );
2518 break;
2519 case 'y': /* symlink */
2520 output_symlink_rule( files.str[i], dest, 1 );
2521 break;
2522 default:
2523 assert(0);
2525 strarray_add( &make->uninstall_files, dest );
2530 /*******************************************************************
2531 * output_install_rules
2533 * Rules are stored as a (file,dest) pair of values.
2534 * The first char of dest indicates the type of install.
2536 static void output_install_rules( struct makefile *make, enum install_rules rules )
2538 unsigned int i;
2539 struct strarray files = make->install_rules[rules];
2540 struct strarray targets = empty_strarray;
2542 if (!files.count) return;
2544 for (i = 0; i < files.count; i += 2)
2546 const char *file = files.str[i];
2547 switch (*files.str[i + 1])
2549 case '0': case '1': case '2': case '3': case '4':
2550 case '5': case '6': case '7': case '8': case '9': /* arch-dependent program */
2551 case 'd': /* data file */
2552 case 'p': /* program file */
2553 case 's': /* script */
2554 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2555 break;
2556 case 't': /* script in tools dir */
2557 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2558 break;
2562 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, install_targets[rules] ));
2563 output_filenames( targets );
2564 output( "\n" );
2565 output_install_commands( make, files );
2566 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2567 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, install_targets[rules] ));
2571 static int cmp_string_length( const char **a, const char **b )
2573 int paths_a = 0, paths_b = 0;
2574 const char *p;
2576 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2577 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2578 if (paths_b != paths_a) return paths_b - paths_a;
2579 return strcmp( *a, *b );
2582 /*******************************************************************
2583 * get_removable_dirs
2585 * Retrieve a list of directories to try to remove after deleting the files.
2587 static struct strarray get_removable_dirs( struct strarray files )
2589 struct strarray dirs = empty_strarray;
2590 unsigned int i;
2592 for (i = 0; i < files.count; i++)
2594 char *dir = xstrdup( files.str[i] );
2595 while (strchr( dir, '/' ))
2597 *strrchr( dir, '/' ) = 0;
2598 strarray_add_uniq( &dirs, xstrdup(dir) );
2601 strarray_qsort( &dirs, cmp_string_length );
2602 return dirs;
2606 /*******************************************************************
2607 * output_uninstall_rules
2609 static void output_uninstall_rules( struct makefile *make )
2611 static const char *dirs_order[] =
2612 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2614 struct strarray uninstall_dirs;
2615 unsigned int i, j;
2617 if (!make->uninstall_files.count) return;
2618 output( "uninstall::\n" );
2619 output_rm_filenames( make->uninstall_files, "rm -f" );
2620 strarray_add_uniq( &make->phony_targets, "uninstall" );
2622 if (!subdirs.count) return;
2623 uninstall_dirs = get_removable_dirs( make->uninstall_files );
2624 output( "\t-rmdir" );
2625 for (i = 0; i < ARRAY_SIZE(dirs_order); i++)
2627 for (j = 0; j < uninstall_dirs.count; j++)
2629 if (!uninstall_dirs.str[j]) continue;
2630 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2631 continue;
2632 output_filename( uninstall_dirs.str[j] );
2633 uninstall_dirs.str[j] = NULL;
2636 for (j = 0; j < uninstall_dirs.count; j++)
2637 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2638 output( "\n" );
2642 /*******************************************************************
2643 * output_po_files
2645 static void output_po_files( struct makefile *make )
2647 const char *po_dir = src_dir_path( make, "po" );
2648 unsigned int i;
2650 if (linguas.count)
2652 for (i = 0; i < linguas.count; i++)
2653 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2654 output( ": %s/wine.pot\n", po_dir );
2655 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2656 cmd_prefix( "MSG" ), po_dir );
2657 output( "po/all:" );
2658 for (i = 0; i < linguas.count; i++)
2659 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2660 output( "\n" );
2662 output( "%s/wine.pot:", po_dir );
2663 output_filenames( make->pot_files );
2664 output( "\n" );
2665 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2666 output_filenames( make->pot_files );
2667 output( "\n" );
2668 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2672 /*******************************************************************
2673 * output_source_y
2675 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2677 /* add source file dependency for parallel makes */
2678 char *header = strmake( "%s.tab.h", obj );
2680 if (find_include_file( make, header ))
2682 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2683 output( "\t%s%s -o %s.tab.c -d %s\n",
2684 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2685 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2686 source->filename, obj_dir_path( make, header ));
2687 strarray_add( &make->clean_files, header );
2689 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2691 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2695 /*******************************************************************
2696 * output_source_l
2698 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2700 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2701 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2705 /*******************************************************************
2706 * output_source_h
2708 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2710 if (source->file->flags & FLAG_GENERATED)
2711 strarray_add( &make->all_targets[0], source->name );
2712 else if ((source->file->flags & FLAG_INSTALL) || strncmp( source->name, "wine/", 5 ))
2713 add_install_rule( make, source->name, 0, source->name,
2714 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2718 /*******************************************************************
2719 * output_source_rc
2721 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2723 struct strarray defines = get_source_defines( make, source, obj );
2724 const char *po_dir = NULL, *res_file = strmake( "%s.res", obj );
2725 unsigned int i, arch;
2727 if (source->file->flags & FLAG_RC_HEADER) return;
2728 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2729 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2730 if (!make->testdll || !find_src_file( make, strmake( "%s.spec", obj ) )) /* RC is for a TESTDLL */
2732 for (arch = 0; arch < archs.count; arch++)
2733 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2735 else strarray_add( &make->clean_files, res_file );
2737 if (source->file->flags & FLAG_RC_PO)
2739 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2740 output( "%s.pot ", obj_dir_path( make, obj ) );
2742 output( "%s: %s", obj_dir_path( make, res_file ), source->filename );
2743 output_filename( tools_path( make, "wrc" ));
2744 if (make->src_dir) output_filename( "nls/locale.nls" );
2745 output_filenames( source->dependencies );
2746 output( "\n" );
2747 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2748 if (make->is_win16) output_filename( "-m16" );
2749 output_filename( "--nostdinc" );
2750 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2751 output_filenames( defines );
2752 output_filename( source->filename );
2753 output( "\n" );
2754 if (po_dir)
2756 output( "%s:", obj_dir_path( make, res_file ));
2757 for (i = 0; i < linguas.count; i++)
2758 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2759 output( "\n" );
2764 /*******************************************************************
2765 * output_source_mc
2767 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2769 unsigned int i, arch;
2770 char *obj_path = obj_dir_path( make, obj );
2771 char *res_file = strmake( "%s.res", obj );
2773 for (arch = 0; arch < archs.count; arch++)
2774 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2775 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2776 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2777 output_filename( tools_path( make, "wmc" ));
2778 output_filenames( source->dependencies );
2779 if (make->src_dir) output_filename( "nls/locale.nls" );
2780 output( "\n" );
2781 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2782 if (linguas.count)
2784 output_filename( "--po-dir=po" );
2785 output( "\n" );
2786 output( "%s.res:", obj_dir_path( make, obj ));
2787 for (i = 0; i < linguas.count; i++)
2788 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2790 output( "\n" );
2794 /*******************************************************************
2795 * output_source_res
2797 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2799 if (make->disabled[source->arch]) return;
2800 strarray_add( &make->res_files[source->arch], source->name );
2804 /*******************************************************************
2805 * output_source_idl
2807 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2809 struct strarray defines = get_source_defines( make, source, obj );
2810 struct strarray headers = empty_strarray;
2811 struct strarray deps = empty_strarray;
2812 struct strarray multiarch_targets[MAX_ARCHS] = { empty_strarray };
2813 const char *dest;
2814 unsigned int i, arch;
2816 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2817 if (!source->file->flags) return;
2819 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2820 if (source->file->flags & FLAG_INSTALL)
2822 add_install_rule( make, source->name, 0, xstrdup( source->name ),
2823 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2824 if (source->file->flags & FLAG_IDL_HEADER)
2825 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2826 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2828 if (source->file->flags & FLAG_IDL_HEADER)
2830 dest = strmake( "%s.h", obj );
2831 strarray_add( &headers, dest );
2832 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2835 for (i = 0; i < ARRAY_SIZE(idl_outputs); i++)
2837 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2838 for (arch = 0; arch < archs.count; arch++)
2840 if (!is_multiarch( arch )) continue;
2841 if (make->disabled[arch]) continue;
2842 dest = strmake( "%s%s%s", arch_dirs[arch], obj, idl_outputs[i].ext );
2843 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2844 strarray_add( &multiarch_targets[arch], dest );
2848 for (arch = 0; arch < archs.count; arch++)
2850 struct strarray arch_deps = empty_strarray;
2852 if (!arch) strarray_addall( &arch_deps, headers );
2853 strarray_addall( &arch_deps, multiarch_targets[arch] );
2854 if (!arch_deps.count) continue;
2855 output_filenames_obj_dir( make, arch_deps );
2856 output( ":\n" );
2857 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2858 output_filenames( target_flags[arch] );
2859 output_filename( "--nostdinc" );
2860 output_filename( "-Ldlls/\\*" );
2861 output_filenames( defines );
2862 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2863 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2864 output_filename( source->filename );
2865 output( "\n" );
2866 strarray_addall( &deps, arch_deps );
2869 if (deps.count)
2871 output_filenames_obj_dir( make, deps );
2872 output( ":" );
2873 output_filename( tools_path( make, "widl" ));
2874 output_filename( source->filename );
2875 output_filenames( source->dependencies );
2876 output( "\n" );
2879 if (source->importlibdeps.count)
2881 for (arch = 0; arch < archs.count; arch++)
2883 if (!multiarch_targets[arch].count) continue;
2884 output_filenames_obj_dir( make, multiarch_targets[arch] );
2885 output( ":" );
2886 for (i = 0; i < source->importlibdeps.count; i++)
2888 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2889 const char *module = strmake( "%s%s", arch_pe_dirs[arch], submake->module );
2890 output_filename( obj_dir_path( submake, module ));
2892 output( "\n" );
2898 /*******************************************************************
2899 * output_source_x
2901 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2903 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2904 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2905 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2906 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2907 if (source->file->flags & FLAG_INSTALL)
2909 add_install_rule( make, source->name, 0, source->name,
2910 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2911 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2912 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2917 /*******************************************************************
2918 * output_source_sfd
2920 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2922 unsigned int i;
2923 char *ttf_obj = strmake( "%s.ttf", obj );
2924 char *ttf_file = src_dir_path( make, ttf_obj );
2926 if (fontforge && !make->src_dir)
2928 output( "%s: %s\n", ttf_file, source->filename );
2929 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2930 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2931 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2932 strarray_add( &make->maintainerclean_files, ttf_obj );
2934 if (source->file->flags & FLAG_INSTALL)
2936 add_install_rule( make, source->name, 0, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2937 output_srcdir_symlink( make, ttf_obj );
2940 if (source->file->flags & FLAG_SFD_FONTS)
2942 struct strarray *array = source->file->args;
2944 for (i = 0; i < array->count; i++)
2946 char *font = strtok( xstrdup(array->str[i]), " \t" );
2947 char *args = strtok( NULL, "" );
2949 strarray_add( &make->all_targets[0], xstrdup( font ));
2950 output( "%s: %s %s\n", obj_dir_path( make, font ),
2951 tools_path( make, "sfnt2fon" ), ttf_file );
2952 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2953 tools_path( make, "sfnt2fon" ), ttf_file, args );
2954 add_install_rule( make, source->name, 0, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2960 /*******************************************************************
2961 * output_source_svg
2963 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2965 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2966 unsigned int i;
2968 if (convert && rsvg && icotool)
2970 for (i = 0; images[i]; i++)
2971 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2973 if (images[i])
2975 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2976 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2977 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2978 root_src_dir_path( "tools/buildimage" ), source->filename );
2979 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2985 /*******************************************************************
2986 * output_source_nls
2988 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2990 add_install_rule( make, source->name, 0, source->name,
2991 strmake( "D$(nlsdir)/%s", source->name ));
2992 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2996 /*******************************************************************
2997 * output_source_desktop
2999 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
3001 add_install_rule( make, source->name, 0, source->name,
3002 strmake( "D$(datadir)/applications/%s", source->name ));
3006 /*******************************************************************
3007 * output_source_po
3009 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
3011 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
3012 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
3013 strarray_add( &make->all_targets[0], strmake( "%s.mo", obj ));
3017 /*******************************************************************
3018 * output_source_in
3020 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
3022 unsigned int i;
3024 if (make == include_makefile) return; /* ignore generated includes */
3025 if (strendswith( obj, ".man" ) && source->file->args)
3027 struct strarray symlinks;
3028 char *dir, *dest = replace_extension( obj, ".man", "" );
3029 char *lang = strchr( dest, '.' );
3030 char *section = source->file->args;
3031 if (lang)
3033 *lang++ = 0;
3034 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3036 else dir = strmake( "$(mandir)/man%s", section );
3037 add_install_rule( make, dest, 0, obj, strmake( "d%s/%s.%s", dir, dest, section ));
3038 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3039 for (i = 0; i < symlinks.count; i++)
3040 add_install_rule( make, symlinks.str[i], 0, strmake( "%s.%s", dest, section ),
3041 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3042 free( dest );
3043 free( dir );
3045 strarray_add( &make->in_files, obj );
3046 strarray_add( &make->all_targets[0], obj );
3047 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3048 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
3049 output( "%s:", obj_dir_path( make, obj ));
3050 output_filenames( source->dependencies );
3051 output( "\n" );
3052 add_install_rule( make, obj, 0, obj, strmake( "d$(datadir)/wine/%s", obj ));
3056 /*******************************************************************
3057 * output_source_spec
3059 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3061 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3062 struct strarray dll_flags = empty_strarray;
3063 struct strarray default_imports = empty_strarray;
3064 struct strarray all_libs, dep_libs;
3065 const char *dll_name, *obj_name, *res_name, *output_rsrc, *output_file, *debug_file;
3066 unsigned int arch;
3068 if (!imports.count) imports = make->imports;
3069 strarray_addall( &dll_flags, make->extradllflags );
3070 strarray_addall( &dll_flags, get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" ));
3071 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3073 for (arch = 0; arch < archs.count; arch++)
3075 if (!is_multiarch( arch )) continue;
3076 all_libs = dep_libs = empty_strarray;
3077 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ) );
3078 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3079 if (!arch) strarray_addall( &all_libs, libs );
3080 dll_name = arch_module_name( strmake( "%s.dll", obj ), arch );
3081 obj_name = obj_dir_path( make, strmake( "%s%s.o", arch_dirs[arch], obj ));
3082 output_file = obj_dir_path( make, dll_name );
3083 output_rsrc = strmake( "%s.res", dll_name );
3085 if (!find_src_file( make, strmake( "%s.rc", obj ) )) res_name = NULL;
3086 else res_name = obj_dir_path( make, strmake( "%s.res", obj ) );
3088 strarray_add( &make->clean_files, dll_name );
3089 strarray_add( &make->res_files[arch], output_rsrc );
3090 output( "%s:", obj_dir_path( make, output_rsrc ));
3091 output_filename( output_file );
3092 output_filename( tools_path( make, "wrc" ));
3093 output( "\n" );
3094 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
3095 tools_path( make, "wrc" ));
3097 output( "%s:", output_file );
3098 output_filename( source->filename );
3099 output_filename( obj_name );
3100 if (res_name) output_filename( res_name );
3101 output_filenames( dep_libs );
3102 output_filename( tools_path( make, "winebuild" ));
3103 output_filename( tools_path( make, "winegcc" ));
3104 output( "\n" );
3105 output_winegcc_command( make, arch );
3106 output_filename( "-s" );
3107 output_filenames( dll_flags );
3108 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3109 output_filename( "-shared" );
3110 output_filename( source->filename );
3111 output_filename( obj_name );
3112 if (res_name) output_filename( res_name );
3113 if ((debug_file = get_debug_file( make, dll_name, arch )))
3114 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3115 output_filenames( all_libs );
3116 output_filename( arch_make_variable( "LDFLAGS", arch ));
3117 output( "\n" );
3122 /*******************************************************************
3123 * output_source_xml
3125 static void output_source_xml( struct makefile *make, struct incl_file *source, const char *obj )
3127 if (wayland_scanner)
3129 output( "%s-protocol.c: %s\n", obj_dir_path( make, obj ), source->filename );
3130 output( "\t%s%s private-code %s $@\n", cmd_prefix( "GEN" ), wayland_scanner, source->filename );
3131 output( "%s-client-protocol.h: %s\n", obj_dir_path( make, obj ), source->filename );
3132 output( "\t%s%s client-header %s $@\n", cmd_prefix( "GEN" ), wayland_scanner, source->filename );
3137 /*******************************************************************
3138 * output_source_one_arch
3140 static void output_source_one_arch( struct makefile *make, struct incl_file *source, const char *obj,
3141 struct strarray defines, struct strarray *targets,
3142 unsigned int arch, int is_dll_src )
3144 const char *obj_name;
3146 if (make->disabled[arch] && !(source->file->flags & FLAG_C_IMPLIB)) return;
3148 if (arch)
3150 if (source->file->flags & FLAG_C_UNIX) return;
3151 if (!is_using_msvcrt( make ) && !make->staticlib && !(source->file->flags & FLAG_C_IMPLIB)) return;
3153 else if (source->file->flags & FLAG_C_UNIX)
3155 if (!*dll_ext) return;
3157 else if (archs.count > 1 && is_using_msvcrt( make ) &&
3158 !(source->file->flags & FLAG_C_IMPLIB) &&
3159 (!make->staticlib || make->extlib)) return;
3161 obj_name = strmake( "%s%s.o", source->arch ? "" : arch_dirs[arch], obj );
3162 strarray_add( targets, obj_name );
3164 if (source->file->flags & FLAG_C_UNIX)
3165 strarray_add( &make->unixobj_files, obj_name );
3166 else if (source->file->flags & FLAG_C_IMPLIB)
3167 strarray_add( &make->implib_files[arch], obj_name );
3168 else if (!is_dll_src)
3169 strarray_add( &make->object_files[arch], obj_name );
3170 else
3171 strarray_add( &make->clean_files, obj_name );
3173 output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename );
3174 output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), arch_make_variable( "CC", arch ), source->filename );
3175 output_filenames( defines );
3176 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3177 output_filenames( make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] );
3178 if (!arch)
3180 if (source->file->flags & FLAG_C_UNIX)
3182 output_filenames( unix_dllflags );
3184 else if (make->module || make->testdll)
3186 output_filenames( dll_flags );
3187 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3188 if (!*dll_ext && make->module && is_crt_module( make->module ))
3189 output_filename( "-fno-builtin" );
3192 else
3194 if (make->module && is_crt_module( make->module )) output_filename( "-fno-builtin" );
3197 output_filenames( cpp_flags );
3198 output_filename( arch_make_variable( "CFLAGS", arch ));
3199 output( "\n" );
3201 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3202 !(source->file->flags & FLAG_GENERATED))
3204 const char *ok_file, *test_exe;
3206 ok_file = strmake( "%s%s.ok", arch_dirs[arch], obj );
3207 test_exe = replace_extension( make->testdll, ".dll", "_test.exe" );
3208 strarray_add( &make->ok_files[arch], ok_file );
3209 output( "%s:\n", obj_dir_path( make, ok_file ));
3210 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s %s && touch $@\n",
3211 cmd_prefix( "TEST" ),
3212 root_src_dir_path( "tools/runtest" ), make->testdll,
3213 obj_dir_path( make, arch_module_name( test_exe, arch )), obj );
3218 /*******************************************************************
3219 * output_source_default
3221 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3223 struct strarray defines = get_source_defines( make, source, obj );
3224 struct strarray targets = empty_strarray;
3225 int is_dll_src = (make->testdll && strendswith( source->name, ".c" ) &&
3226 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3227 unsigned int arch;
3229 for (arch = 0; arch < archs.count; arch++)
3230 if (!source->arch || source->arch == arch)
3231 output_source_one_arch( make, source, obj, defines, &targets, arch, is_dll_src );
3233 if (source->file->flags & FLAG_GENERATED)
3235 if (!make->testdll || !strendswith( source->filename, "testlist.c" ))
3236 strarray_add( &make->clean_files, source->basename );
3238 else
3240 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ))
3241 strarray_add( &make->test_files, obj );
3244 if (targets.count && source->dependencies.count)
3246 output_filenames_obj_dir( make, targets );
3247 output( ":" );
3248 output_filenames( source->dependencies );
3249 output( "\n" );
3254 /* dispatch table to output rules for a single source file */
3255 static const struct
3257 const char *ext;
3258 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3259 } output_source_funcs[] =
3261 { "y", output_source_y },
3262 { "l", output_source_l },
3263 { "h", output_source_h },
3264 { "rh", output_source_h },
3265 { "inl", output_source_h },
3266 { "rc", output_source_rc },
3267 { "mc", output_source_mc },
3268 { "res", output_source_res },
3269 { "idl", output_source_idl },
3270 { "sfd", output_source_sfd },
3271 { "svg", output_source_svg },
3272 { "nls", output_source_nls },
3273 { "desktop", output_source_desktop },
3274 { "po", output_source_po },
3275 { "in", output_source_in },
3276 { "x", output_source_x },
3277 { "spec", output_source_spec },
3278 { "xml", output_source_xml },
3279 { NULL, output_source_default }
3283 /*******************************************************************
3284 * output_fake_module
3286 static void output_fake_module( struct makefile *make )
3288 unsigned int arch = 0; /* fake modules are always native */
3289 const char *spec_file = NULL, *name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3291 if (make->disabled[arch]) return;
3293 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3295 strarray_add( &make->all_targets[arch], name );
3296 add_install_rule( make, make->module, arch, name, strmake( "d$(dlldir)/%s", name ));
3298 output( "%s:", obj_dir_path( make, name ));
3299 if (spec_file) output_filename( spec_file );
3300 output_filenames_obj_dir( make, make->res_files[arch] );
3301 output_filename( tools_path( make, "winebuild" ));
3302 output_filename( tools_path( make, "winegcc" ));
3303 output( "\n" );
3304 output_winegcc_command( make, arch );
3305 output_filename( "-Wb,--fake-module" );
3306 if (spec_file)
3308 output_filename( "-shared" );
3309 output_filename( spec_file );
3311 output_filenames( make->extradllflags );
3312 output_filenames_obj_dir( make, make->res_files[arch] );
3313 output( "\n" );
3317 /*******************************************************************
3318 * output_module
3320 static void output_module( struct makefile *make, unsigned int arch )
3322 struct strarray default_imports = empty_strarray;
3323 struct strarray all_libs = empty_strarray;
3324 struct strarray dep_libs = empty_strarray;
3325 struct strarray imports = make->imports;
3326 const char *module_name;
3327 const char *debug_file;
3328 char *spec_file = NULL;
3329 unsigned int i;
3331 if (make->disabled[arch]) return;
3333 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3335 if (!make->data_only)
3337 module_name = arch_module_name( make->module, arch );
3339 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3341 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ));
3342 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, IMPORT_TYPE_DELAYED, arch ));
3343 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3344 if (!arch) strarray_addall( &all_libs, libs );
3346 if (delay_load_flags[arch])
3348 for (i = 0; i < make->delayimports.count; i++)
3350 struct makefile *import = get_static_lib( make->delayimports.str[i], arch );
3351 if (import) strarray_add( &all_libs, strmake( "%s%s", delay_load_flags[arch], import->module ));
3355 else module_name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3357 strarray_add( &make->all_targets[arch], module_name );
3358 if (make->data_only)
3359 add_install_rule( make, make->module, arch, module_name,
3360 strmake( "d$(dlldir)/%s%s", arch_pe_dirs[arch], make->module ));
3361 else
3362 add_install_rule( make, make->module, arch, module_name,
3363 strmake( "%c%s%s%s", '0' + arch, arch_install_dirs[arch], make->module,
3364 arch ? "" : dll_ext ));
3366 output( "%s:", obj_dir_path( make, module_name ));
3367 if (spec_file) output_filename( spec_file );
3368 output_filenames_obj_dir( make, make->object_files[arch] );
3369 output_filenames_obj_dir( make, make->res_files[arch] );
3370 output_filenames( dep_libs );
3371 output_filename( tools_path( make, "winebuild" ));
3372 output_filename( tools_path( make, "winegcc" ));
3373 output( "\n" );
3374 output_winegcc_command( make, arch );
3375 if (arch) output_filename( "-Wl,--wine-builtin" );
3376 if (spec_file)
3378 output_filename( "-shared" );
3379 output_filename( spec_file );
3381 output_filenames( make->extradllflags );
3382 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3383 output_filenames_obj_dir( make, make->object_files[arch] );
3384 output_filenames_obj_dir( make, make->res_files[arch] );
3385 debug_file = get_debug_file( make, module_name, arch );
3386 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3387 output_filenames( all_libs );
3388 output_filename( arch_make_variable( "LDFLAGS", arch ));
3389 output( "\n" );
3391 if (!make->data_only && !arch && *dll_ext) output_fake_module( make );
3395 /*******************************************************************
3396 * output_import_lib
3398 static void output_import_lib( struct makefile *make, unsigned int arch )
3400 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3401 const char *name = strmake( "%slib%s.a", arch_dirs[arch], make->importlib );
3403 strarray_add( &make->clean_files, name );
3404 if (needs_delay_lib( make, arch ))
3406 const char *delay_name = replace_extension( name, ".a", ".delay.a" );
3407 strarray_add( &make->clean_files, delay_name );
3408 output( "%s ", obj_dir_path( make, delay_name ));
3410 output( "%s: %s %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ), spec_file );
3411 output_filenames_obj_dir( make, make->implib_files[arch] );
3412 output( "\n" );
3413 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3414 if (!delay_load_flags[arch]) output_filename( "--without-dlltool" );
3415 output_filenames( target_flags[arch] );
3416 if (make->is_win16) output_filename( "-m16" );
3417 output_filename( "--export" );
3418 output_filename( spec_file );
3419 output_filenames_obj_dir( make, make->implib_files[arch] );
3420 output( "\n" );
3422 if (!arch && is_native_arch_disabled( make )) return;
3424 add_install_rule( make, make->importlib, arch, name,
3425 strmake( "d%slib%s.a", arch_install_dirs[arch], make->importlib ));
3429 /*******************************************************************
3430 * output_unix_lib
3432 static void output_unix_lib( struct makefile *make )
3434 struct strarray unix_deps = empty_strarray;
3435 struct strarray unix_libs = add_unix_libraries( make, &unix_deps );
3436 unsigned int arch = 0; /* unix libs are always native */
3438 if (make->disabled[arch]) return;
3440 strarray_add( &make->all_targets[arch], make->unixlib );
3441 add_install_rule( make, make->module, arch, make->unixlib,
3442 strmake( "p%s%s", arch_install_dirs[arch], make->unixlib ));
3443 output( "%s:", obj_dir_path( make, make->unixlib ));
3444 output_filenames_obj_dir( make, make->unixobj_files );
3445 output_filenames( unix_deps );
3446 output( "\n" );
3447 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3448 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3449 output_filenames_obj_dir( make, make->unixobj_files );
3450 output_filenames( unix_libs );
3451 output_filename( "$(LDFLAGS)" );
3452 output( "\n" );
3456 /*******************************************************************
3457 * output_static_lib
3459 static void output_static_lib( struct makefile *make, unsigned int arch )
3461 const char *name = strmake( "%s%s", arch_dirs[arch], make->staticlib );
3463 strarray_add( &make->clean_files, name );
3464 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3465 output_filenames_obj_dir( make, make->object_files[arch] );
3466 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3467 output( "\n" );
3468 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3469 output_filenames( target_flags[arch] );
3470 output_filenames_obj_dir( make, make->object_files[arch] );
3471 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3472 output( "\n" );
3473 if (!make->extlib)
3474 add_install_rule( make, make->staticlib, arch, name,
3475 strmake( "d%s%s", arch_install_dirs[arch], make->staticlib ));
3479 /*******************************************************************
3480 * output_test_module
3482 static void output_test_module( struct makefile *make, unsigned int arch )
3484 char *basemodule = replace_extension( make->testdll, ".dll", "" );
3485 char *stripped = arch_module_name( strmake( "%s_test-stripped.exe", basemodule ), arch );
3486 char *testmodule = arch_module_name( strmake( "%s_test.exe", basemodule ), arch );
3487 struct strarray default_imports = get_default_imports( make, make->imports );
3488 struct strarray dep_libs = empty_strarray;
3489 struct strarray all_libs = empty_strarray;
3490 struct makefile *parent = get_parent_makefile( make );
3491 const char *debug_file;
3493 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, IMPORT_TYPE_DIRECT, arch ) );
3494 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3496 strarray_add( &make->all_targets[arch], testmodule );
3497 strarray_add( &make->clean_files, stripped );
3498 output( "%s:\n", obj_dir_path( make, testmodule ));
3499 output_winegcc_command( make, arch );
3500 output_filenames( make->extradllflags );
3501 output_filenames_obj_dir( make, make->object_files[arch] );
3502 output_filenames_obj_dir( make, make->res_files[arch] );
3503 if ((debug_file = get_debug_file( make, testmodule, arch )))
3504 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3505 output_filenames( all_libs );
3506 output_filename( arch_make_variable( "LDFLAGS", arch ));
3507 output( "\n" );
3508 output( "%s:\n", obj_dir_path( make, stripped ));
3509 output_winegcc_command( make, arch );
3510 output_filename( "-s" );
3511 output_filename( strmake( "-Wb,-F,%s_test.exe", basemodule ));
3512 output_filenames( make->extradllflags );
3513 output_filenames_obj_dir( make, make->object_files[arch] );
3514 output_filenames_obj_dir( make, make->res_files[arch] );
3515 output_filenames( all_libs );
3516 output_filename( arch_make_variable( "LDFLAGS", arch ));
3517 output( "\n" );
3518 output( "%s %s:", obj_dir_path( make, testmodule ), obj_dir_path( make, stripped ));
3519 output_filenames_obj_dir( make, make->object_files[arch] );
3520 output_filenames_obj_dir( make, make->res_files[arch] );
3521 output_filenames( dep_libs );
3522 output_filename( tools_path( make, "winebuild" ));
3523 output_filename( tools_path( make, "winegcc" ));
3524 output( "\n" );
3526 output( "programs/winetest/%s%s_test.res: %s\n", arch_dirs[arch], basemodule,
3527 obj_dir_path( make, stripped ));
3528 output( "\t%secho \"%s_test.exe TESTRES \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3529 basemodule, obj_dir_path( make, stripped ), tools_path( make, "wrc" ));
3531 if (make->disabled[arch] || (parent && parent->disabled[arch]))
3533 make->ok_files[arch] = empty_strarray;
3534 return;
3536 output_filenames_obj_dir( make, make->ok_files[arch] );
3537 output( ": %s", obj_dir_path( make, testmodule ));
3538 if (parent)
3540 char *parent_module = arch_module_name( make->testdll, arch );
3541 output_filename( obj_dir_path( parent, parent_module ));
3542 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3544 output( "\n" );
3545 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3546 output_filenames_obj_dir( make, make->ok_files[arch] );
3547 output( "\n" );
3548 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3549 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3550 output( "%s::\n", obj_dir_path( make, "testclean" ));
3551 output( "\trm -f" );
3552 output_filenames_obj_dir( make, make->ok_files[arch] );
3553 output( "\n" );
3554 strarray_addall( &make->clean_files, make->ok_files[arch] );
3555 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3559 /*******************************************************************
3560 * output_programs
3562 static void output_programs( struct makefile *make )
3564 unsigned int i, j;
3565 unsigned int arch = 0; /* programs are always native */
3567 for (i = 0; i < make->programs.count; i++)
3569 char *program_installed = NULL;
3570 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3571 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3572 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3573 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3574 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3576 if (!objs.count) objs = make->object_files[arch];
3577 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3579 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3580 strarray_addall( &all_libs, libs );
3583 output( "%s:", obj_dir_path( make, program ) );
3584 output_filenames_obj_dir( make, objs );
3585 output_filenames( deps );
3586 output( "\n" );
3587 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3588 output_filenames_obj_dir( make, objs );
3589 output_filenames( all_libs );
3590 output_filename( "$(LDFLAGS)" );
3591 output( "\n" );
3592 strarray_add( &make->all_targets[arch], program );
3594 for (j = 0; j < symlinks.count; j++)
3596 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3597 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3599 strarray_addall( &make->all_targets[arch], symlinks );
3601 add_install_rule( make, program, arch, program_installed ? program_installed : program,
3602 strmake( "p$(bindir)/%s", program ));
3603 for (j = 0; j < symlinks.count; j++)
3604 add_install_rule( make, symlinks.str[j], arch, program,
3605 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3610 /*******************************************************************
3611 * output_subdirs
3613 static void output_subdirs( struct makefile *make )
3615 struct strarray all_targets = empty_strarray;
3616 struct strarray makefile_deps = empty_strarray;
3617 struct strarray clean_files = empty_strarray;
3618 struct strarray testclean_files = empty_strarray;
3619 struct strarray distclean_files = empty_strarray;
3620 struct strarray distclean_dirs = empty_strarray;
3621 struct strarray dependencies = empty_strarray;
3622 struct strarray install_deps[NB_INSTALL_RULES] = { empty_strarray };
3623 struct strarray tooldeps_deps = empty_strarray;
3624 struct strarray buildtest_deps = empty_strarray;
3625 unsigned int i, j, arch;
3627 strarray_addall( &clean_files, make->clean_files );
3628 strarray_addall( &distclean_files, make->distclean_files );
3629 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3630 for (i = 0; i < subdirs.count; i++)
3632 struct strarray subclean = empty_strarray;
3633 strarray_addall( &subclean, get_removable_dirs( submakes[i]->clean_files ));
3634 strarray_addall( &subclean, get_removable_dirs( submakes[i]->distclean_files ));
3635 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3636 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3637 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3638 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3639 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3640 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3641 strarray_addall_path( &distclean_dirs, submakes[i]->obj_dir, subclean );
3642 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3643 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3645 for (arch = 0; arch < archs.count; arch++)
3647 if (submakes[i]->disabled[arch]) continue;
3648 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets[arch] );
3649 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files[arch] );
3651 if (submakes[i]->disabled[0]) continue;
3653 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3654 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3655 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3656 if (submakes[i]->testdll)
3657 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3658 for (j = 0; j < NB_INSTALL_RULES; j++)
3659 if (submakes[i]->install_rules[j].count)
3660 strarray_add( &install_deps[j], obj_dir_path( submakes[i], install_targets[j] ));
3662 strarray_addall( &dependencies, makefile_deps );
3663 output( "all:" );
3664 output_filenames( all_targets );
3665 output( "\n" );
3666 output( "Makefile:" );
3667 output_filenames( makefile_deps );
3668 output( "\n" );
3669 output_filenames( dependencies );
3670 output( ":\n" );
3671 for (j = 0; j < NB_INSTALL_RULES; j++)
3673 if (!install_deps[j].count) continue;
3674 if (strcmp( install_targets[j], "install-test" ))
3676 output( "install " );
3677 strarray_add_uniq( &make->phony_targets, "install" );
3679 output( "%s::", install_targets[j] );
3680 output_filenames( install_deps[j] );
3681 output( "\n" );
3682 strarray_add_uniq( &make->phony_targets, install_targets[j] );
3684 output_uninstall_rules( make );
3685 if (buildtest_deps.count)
3687 output( "buildtests:" );
3688 output_filenames( buildtest_deps );
3689 output( "\n" );
3690 strarray_add_uniq( &make->phony_targets, "buildtests" );
3692 output( "check test:" );
3693 output_filenames( testclean_files );
3694 output( "\n" );
3695 strarray_add_uniq( &make->phony_targets, "check" );
3696 strarray_add_uniq( &make->phony_targets, "test" );
3698 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3700 output( "clean::\n");
3701 output_rm_filenames( clean_files, "rm -f" );
3702 output( "testclean::\n");
3703 output_rm_filenames( testclean_files, "rm -f" );
3704 output( "distclean::\n");
3705 output_rm_filenames( distclean_files, "rm -f" );
3706 output_rm_filenames( distclean_dirs, "-rmdir 2>/dev/null" );
3707 output( "maintainer-clean::\n");
3708 output_rm_filenames( make->maintainerclean_files, "rm -f" );
3709 strarray_add_uniq( &make->phony_targets, "distclean" );
3710 strarray_add_uniq( &make->phony_targets, "testclean" );
3711 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3713 if (tooldeps_deps.count)
3715 output( "__tooldeps__:" );
3716 output_filenames( tooldeps_deps );
3717 output( "\n" );
3718 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3721 if (make->phony_targets.count)
3723 output( ".PHONY:" );
3724 output_filenames( make->phony_targets );
3725 output( "\n" );
3730 /*******************************************************************
3731 * output_sources
3733 static void output_sources( struct makefile *make )
3735 struct strarray all_targets = empty_strarray;
3736 struct incl_file *source;
3737 unsigned int i, j, arch;
3739 strarray_add_uniq( &make->phony_targets, "all" );
3741 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3743 char *obj = xstrdup( source->name );
3744 char *ext = get_extension( obj );
3746 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3747 *ext++ = 0;
3749 for (j = 0; output_source_funcs[j].ext; j++)
3750 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3752 output_source_funcs[j].fn( make, source, obj );
3753 strarray_addall_uniq( &make->dependencies, source->dependencies );
3756 /* special case for winetest: add resource files from other test dirs */
3757 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3759 for (arch = 0; arch < archs.count; arch++)
3761 if (!is_multiarch( arch )) continue;
3762 for (i = 0; i < subdirs.count; i++)
3764 if (!submakes[i]->testdll) continue;
3765 if (submakes[i]->disabled[arch]) continue;
3766 if (enable_tests.count && !strarray_exists( &enable_tests, submakes[i]->testdll )) continue;
3767 strarray_add( &make->res_files[arch],
3768 strmake( "%s%s", arch_dirs[arch],
3769 replace_extension( submakes[i]->testdll, ".dll", "_test.res" )));
3774 if (make->dlldata_files.count)
3776 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3777 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3778 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3779 output_filenames( make->dlldata_files );
3780 output( "\n" );
3783 if (make->staticlib)
3785 for (arch = 0; arch < archs.count; arch++)
3786 if (is_multiarch( arch ) || !make->extlib) output_static_lib( make, arch );
3788 else if (make->module)
3790 for (arch = 0; arch < archs.count; arch++) if (is_multiarch( arch )) output_module( make, arch );
3791 if (make->unixlib) output_unix_lib( make );
3792 if (make->importlib) for (arch = 0; arch < archs.count; arch++) output_import_lib( make, arch );
3793 if (make->is_exe && !make->is_win16 && *dll_ext && strendswith( make->module, ".exe" ))
3795 char *binary = replace_extension( make->module, ".exe", "" );
3796 add_install_rule( make, binary, 0, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3799 else if (make->testdll)
3801 for (arch = 0; arch < archs.count; arch++)
3802 if (is_multiarch( arch )) output_test_module( make, arch );
3804 else if (make->programs.count) output_programs( make );
3806 for (i = 0; i < make->scripts.count; i++)
3807 add_install_rule( make, make->scripts.str[i], 0, make->scripts.str[i],
3808 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3810 for (i = 0; i < make->extra_targets.count; i++)
3811 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3812 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3813 else
3814 strarray_add( &make->all_targets[0], make->extra_targets.str[i] );
3816 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3817 strarray_add( &make->distclean_files, "Makefile" );
3818 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3820 if (!make->obj_dir)
3821 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3822 else if (!strcmp( make->obj_dir, "po" ))
3823 strarray_add( &make->distclean_files, "LINGUAS" );
3825 for (arch = 0; arch < archs.count; arch++)
3827 strarray_addall_uniq( &make->clean_files, make->object_files[arch] );
3828 strarray_addall_uniq( &make->clean_files, make->implib_files[arch] );
3829 strarray_addall_uniq( &make->clean_files, make->res_files[arch] );
3830 strarray_addall_uniq( &make->clean_files, make->all_targets[arch] );
3832 strarray_addall( &make->clean_files, make->unixobj_files );
3833 strarray_addall( &make->clean_files, make->pot_files );
3834 strarray_addall( &make->clean_files, make->debug_files );
3836 if (make == top_makefile)
3838 output_subdirs( make );
3839 return;
3842 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3843 strarray_addall( &all_targets, make->font_files );
3844 if (all_targets.count)
3846 output( "%s:", obj_dir_path( make, "all" ));
3847 output_filenames_obj_dir( make, all_targets );
3848 output( "\n" );
3849 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3851 for (i = 0; i < NB_INSTALL_RULES; i++) output_install_rules( make, i );
3853 if (make->clean_files.count)
3855 output( "%s::\n", obj_dir_path( make, "clean" ));
3856 output( "\trm -f" );
3857 output_filenames_obj_dir( make, make->clean_files );
3858 output( "\n" );
3859 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3864 /*******************************************************************
3865 * create_temp_file
3867 static FILE *create_temp_file( const char *orig )
3869 char *name = xmalloc( strlen(orig) + 13 );
3870 unsigned int i, id = getpid();
3871 int fd;
3872 FILE *ret = NULL;
3874 for (i = 0; i < 100; i++)
3876 sprintf( name, "%s.tmp%08x", orig, id );
3877 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3879 ret = fdopen( fd, "w" );
3880 break;
3882 if (errno != EEXIST) break;
3883 id += 7777;
3885 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3886 temp_file_name = name;
3887 return ret;
3891 /*******************************************************************
3892 * rename_temp_file
3894 static void rename_temp_file( const char *dest )
3896 int ret = rename( temp_file_name, dest );
3897 if (ret == -1 && errno == EEXIST)
3899 /* rename doesn't overwrite on windows */
3900 unlink( dest );
3901 ret = rename( temp_file_name, dest );
3903 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3904 temp_file_name = NULL;
3908 /*******************************************************************
3909 * are_files_identical
3911 static int are_files_identical( FILE *file1, FILE *file2 )
3913 for (;;)
3915 char buffer1[8192], buffer2[8192];
3916 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3917 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3918 if (size1 != size2) return 0;
3919 if (!size1) return feof( file1 ) && feof( file2 );
3920 if (memcmp( buffer1, buffer2, size1 )) return 0;
3925 /*******************************************************************
3926 * rename_temp_file_if_changed
3928 static void rename_temp_file_if_changed( const char *dest )
3930 FILE *file1, *file2;
3931 int do_rename = 1;
3933 if ((file1 = fopen( dest, "r" )))
3935 if ((file2 = fopen( temp_file_name, "r" )))
3937 do_rename = !are_files_identical( file1, file2 );
3938 fclose( file2 );
3940 fclose( file1 );
3942 if (!do_rename)
3944 unlink( temp_file_name );
3945 temp_file_name = NULL;
3947 else rename_temp_file( dest );
3951 /*******************************************************************
3952 * output_linguas
3954 static void output_linguas( const struct makefile *make )
3956 const char *dest = obj_dir_path( make, "LINGUAS" );
3957 struct incl_file *source;
3959 output_file = create_temp_file( dest );
3961 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3962 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3963 if (strendswith( source->name, ".po" ))
3964 output( "%s\n", replace_extension( source->name, ".po", "" ));
3966 if (fclose( output_file )) fatal_perror( "write" );
3967 output_file = NULL;
3968 rename_temp_file_if_changed( dest );
3972 /*******************************************************************
3973 * output_testlist
3975 static void output_testlist( const struct makefile *make )
3977 const char *dest = obj_dir_path( make, "testlist.c" );
3978 unsigned int i;
3980 output_file = create_temp_file( dest );
3982 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3983 output( "#define WIN32_LEAN_AND_MEAN\n" );
3984 output( "#include <windows.h>\n\n" );
3985 output( "#define STANDALONE\n" );
3986 output( "#include \"wine/test.h\"\n\n" );
3988 for (i = 0; i < make->test_files.count; i++)
3989 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3990 output( "\n" );
3991 output( "const struct test winetest_testlist[] =\n" );
3992 output( "{\n" );
3993 for (i = 0; i < make->test_files.count; i++)
3994 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3995 output( " { 0, 0 }\n" );
3996 output( "};\n" );
3998 if (fclose( output_file )) fatal_perror( "write" );
3999 output_file = NULL;
4000 rename_temp_file_if_changed( dest );
4004 /*******************************************************************
4005 * output_gitignore
4007 static void output_gitignore( const char *dest, struct strarray files )
4009 unsigned int i;
4011 output_file = create_temp_file( dest );
4013 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4014 for (i = 0; i < files.count; i++)
4016 if (!strchr( files.str[i], '/' )) output( "/" );
4017 output( "%s\n", files.str[i] );
4020 if (fclose( output_file )) fatal_perror( "write" );
4021 output_file = NULL;
4022 rename_temp_file( dest );
4026 /*******************************************************************
4027 * output_stub_makefile
4029 static void output_stub_makefile( struct makefile *make )
4031 struct strarray targets = empty_strarray;
4032 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
4033 unsigned int i, arch;
4035 for (arch = 0; arch < archs.count; arch++)
4036 if (make->all_targets[arch].count) strarray_add_uniq( &targets, "all" );
4038 for (i = 0; i < NB_INSTALL_RULES; i++)
4040 if (!make->install_rules[i].count) continue;
4041 strarray_add_uniq( &targets, "install" );
4042 strarray_add( &targets, install_targets[i] );
4044 if (make->clean_files.count) strarray_add( &targets, "clean" );
4045 if (make->test_files.count)
4047 strarray_add( &targets, "check" );
4048 strarray_add( &targets, "test" );
4049 strarray_add( &targets, "testclean" );
4052 if (!targets.count && !make->clean_files.count) return;
4054 output_file_name = obj_dir_path( make, "Makefile" );
4055 output_file = create_temp_file( output_file_name );
4057 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
4059 if (make_var) output( "MAKE = %s\n\n", make_var );
4061 output( "all:\n" );
4062 output_filenames( targets );
4063 output_filenames( make->clean_files );
4064 output( ":\n" );
4065 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4066 output( ".PHONY:" );
4067 output_filenames( targets );
4068 output( "\n" );
4070 fclose( output_file );
4071 output_file = NULL;
4072 rename_temp_file( output_file_name );
4076 /*******************************************************************
4077 * output_silent_rules
4079 static void output_silent_rules(void)
4081 static const char *cmds[] =
4083 "BISON",
4084 "BUILD",
4085 "CC",
4086 "CCLD",
4087 "FLEX",
4088 "GEN",
4089 "LN",
4090 "MSG",
4091 "SED",
4092 "TEST",
4093 "WIDL",
4094 "WMC",
4095 "WRC"
4097 unsigned int i;
4099 output( "V = 0\n" );
4100 for (i = 0; i < ARRAY_SIZE(cmds); i++)
4102 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4103 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4104 output( "quiet_%s_1 =\n", cmds[i] );
4109 /*******************************************************************
4110 * output_top_makefile
4112 static void output_top_makefile( struct makefile *make )
4114 char buffer[1024];
4115 FILE *src_file;
4116 unsigned int i;
4117 int found = 0;
4119 output_file_name = obj_dir_path( make, output_makefile_name );
4120 output_file = create_temp_file( output_file_name );
4122 /* copy the contents of the source makefile */
4123 src_file = open_input_makefile( make );
4124 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4126 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4127 found = !strncmp( buffer, separator, strlen(separator) );
4129 if (fclose( src_file )) fatal_perror( "close" );
4130 input_file_name = NULL;
4132 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4134 if (silent_rules) output_silent_rules();
4135 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4136 output_sources( make );
4137 /* disable implicit rules */
4138 output( ".SUFFIXES:\n" );
4140 fclose( output_file );
4141 output_file = NULL;
4142 rename_temp_file( output_file_name );
4146 /*******************************************************************
4147 * output_dependencies
4149 static void output_dependencies( struct makefile *make )
4151 struct strarray ignore_files = empty_strarray;
4153 if (make->obj_dir) create_dir( make->obj_dir );
4155 if (make == top_makefile) output_top_makefile( make );
4156 else output_stub_makefile( make );
4158 strarray_addall( &ignore_files, make->distclean_files );
4159 strarray_addall( &ignore_files, make->clean_files );
4160 if (make->testdll) output_testlist( make );
4161 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4162 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4164 create_file_directories( make, ignore_files );
4166 output_file_name = NULL;
4170 /*******************************************************************
4171 * load_sources
4173 static void load_sources( struct makefile *make )
4175 static const char *source_vars[] =
4177 "SOURCES",
4178 "C_SRCS",
4179 "OBJC_SRCS",
4180 "RC_SRCS",
4181 "MC_SRCS",
4182 "IDL_SRCS",
4183 "BISON_SRCS",
4184 "LEX_SRCS",
4185 "HEADER_SRCS",
4186 "XTEMPLATE_SRCS",
4187 "SVG_SRCS",
4188 "FONT_SRCS",
4189 "IN_SRCS",
4190 "PO_SRCS",
4191 "MANPAGES",
4192 NULL
4194 const char **var;
4195 unsigned int i, arch;
4196 struct strarray value;
4197 struct incl_file *file;
4199 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4200 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4202 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4203 make->module = get_expanded_make_variable( make, "MODULE" );
4204 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4205 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4206 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4207 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4208 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4210 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4211 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4212 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4213 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4214 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4215 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4216 for (i = 0; i < NB_INSTALL_RULES; i++)
4217 make->install[i] = get_expanded_make_var_array( make, install_variables[i] );
4219 if (make->extlib) make->staticlib = make->extlib;
4220 if (make->staticlib) make->module = make->staticlib;
4222 if (make->obj_dir)
4224 make->disabled[0] = strarray_exists( &disabled_dirs[0], make->obj_dir );
4225 for (arch = 1; arch < archs.count; arch++)
4226 make->disabled[arch] = make->disabled[0] || strarray_exists( &disabled_dirs[arch], make->obj_dir );
4228 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4229 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4230 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4231 strarray_exists( &make->extradllflags, "-mwindows" );
4233 if (make->module)
4235 /* add default install rules if nothing was specified */
4236 for (i = 0; i < NB_INSTALL_RULES; i++) if (make->install[i].count) break;
4237 if (i == NB_INSTALL_RULES)
4239 if (make->importlib) strarray_add( &make->install[INSTALL_DEV], make->importlib );
4240 if (make->staticlib) strarray_add( &make->install[INSTALL_DEV], make->staticlib );
4241 else strarray_add( &make->install[INSTALL_LIB], make->module );
4245 make->include_paths = empty_strarray;
4246 make->include_args = empty_strarray;
4247 make->define_args = empty_strarray;
4248 make->unix_cflags = empty_strarray;
4249 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4251 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4252 for (i = 0; i < value.count; i++)
4254 if (!strncmp( value.str[i], "-I", 2 ))
4256 const char *dir = value.str[i] + 2;
4257 if (!strncmp( dir, "./", 2 ))
4259 dir += 2;
4260 while (*dir == '/') dir++;
4262 strarray_add_uniq( &make->include_paths, dir );
4264 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4265 strarray_add_uniq( &make->define_args, value.str[i] );
4267 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4268 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4270 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4271 if (make->src_dir)
4272 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4273 if (make->parent_dir)
4274 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4275 strarray_add( &make->include_args, "-Iinclude" );
4276 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4278 list_init( &make->sources );
4279 list_init( &make->includes );
4281 for (var = source_vars; *var; var++)
4283 value = get_expanded_make_var_array( make, *var );
4284 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4287 add_generated_sources( make );
4289 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4290 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4292 for (i = 0; i < make->delayimports.count; i++)
4293 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4297 /*******************************************************************
4298 * parse_makeflags
4300 static void parse_makeflags( const char *flags )
4302 const char *p = flags;
4303 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4305 while (*p)
4307 p = skip_spaces( p );
4308 var = buffer;
4309 while (*p && !isspace(*p))
4311 if (*p == '\\' && p[1]) p++;
4312 *var++ = *p++;
4314 *var = 0;
4315 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4320 /*******************************************************************
4321 * parse_option
4323 static int parse_option( const char *opt )
4325 if (opt[0] != '-')
4327 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4328 return 0;
4330 switch(opt[1])
4332 case 'f':
4333 if (opt[2]) output_makefile_name = opt + 2;
4334 break;
4335 case 'R':
4336 relative_dir_mode = 1;
4337 break;
4338 case 'S':
4339 silent_rules = 1;
4340 break;
4341 default:
4342 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4343 exit(1);
4345 return 1;
4349 /*******************************************************************
4350 * main
4352 int main( int argc, char *argv[] )
4354 const char *makeflags = getenv( "MAKEFLAGS" );
4355 const char *target;
4356 unsigned int i, j, arch;
4358 if (makeflags) parse_makeflags( makeflags );
4360 i = 1;
4361 while (i < argc)
4363 if (parse_option( argv[i] ))
4365 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4366 argc--;
4368 else i++;
4371 if (relative_dir_mode)
4373 char *relpath;
4375 if (argc != 3)
4377 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4378 exit( 1 );
4380 relpath = get_relative_path( argv[1], argv[2] );
4381 printf( "%s\n", relpath ? relpath : "." );
4382 exit( 0 );
4385 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4387 atexit( cleanup_files );
4388 init_signals( exit_on_signal );
4390 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4391 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4393 top_makefile = parse_makefile( NULL );
4395 target_flags[0] = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4396 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4397 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4398 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4399 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4400 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4401 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4402 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4403 for (i = 0; i < NB_INSTALL_RULES; i++)
4404 top_install[i] = get_expanded_make_var_array( top_makefile, strmake( "TOP_%s", install_variables[i] ));
4406 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4407 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4408 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4409 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4410 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4411 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4412 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4413 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4414 bison = get_expanded_make_variable( top_makefile, "BISON" );
4415 ar = get_expanded_make_variable( top_makefile, "AR" );
4416 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4417 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4418 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4419 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4420 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4421 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4422 wayland_scanner = get_expanded_make_variable( top_makefile, "WAYLAND_SCANNER" );
4424 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4425 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4426 if (!exe_ext) exe_ext = "";
4427 if (!tools_ext) tools_ext = "";
4429 strarray_add( &archs, get_expanded_make_variable( top_makefile, "HOST_ARCH" ));
4430 strarray_addall( &archs, get_expanded_make_var_array( top_makefile, "PE_ARCHS" ));
4432 arch_dirs[0] = "";
4433 arch_pe_dirs[0] = strmake( "%s-windows/", archs.str[0] );
4434 arch_install_dirs[0] = *dll_ext ? strmake( "$(dlldir)/%s-unix/", archs.str[0] ) : "$(dlldir)/";
4435 strip_progs[0] = "\"$(STRIP)\"";
4437 for (arch = 1; arch < archs.count; arch++)
4439 target = get_expanded_arch_var( top_makefile, "TARGET", arch );
4440 strarray_add( &target_flags[arch], "-b" );
4441 strarray_add( &target_flags[arch], target );
4442 arch_dirs[arch] = strmake( "%s-windows/", archs.str[arch] );
4443 arch_pe_dirs[arch] = arch_dirs[arch];
4444 arch_install_dirs[arch] = strmake( "$(dlldir)/%s", arch_dirs[arch] );
4445 strip_progs[arch] = strmake( "%s-strip", target );
4448 for (arch = 0; arch < archs.count; arch++)
4450 extra_cflags[arch] = get_expanded_arch_var_array( top_makefile, "EXTRACFLAGS", arch );
4451 extra_cflags_extlib[arch] = remove_warning_flags( extra_cflags[arch] );
4452 disabled_dirs[arch] = get_expanded_arch_var_array( top_makefile, "DISABLED_SUBDIRS", arch );
4453 if (!is_multiarch( arch )) continue;
4454 delay_load_flags[arch] = get_expanded_arch_var( top_makefile, "DELAYLOADFLAG", arch );
4455 debug_flags[arch] = get_expanded_arch_var( top_makefile, "DEBUG", arch );
4458 if (*dll_ext)
4460 delay_load_flags[0] = "-Wl,-delayload,";
4461 debug_flags[0] = NULL;
4464 top_makefile->src_dir = root_src_dir;
4465 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4466 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4468 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4470 load_sources( top_makefile );
4471 load_sources( include_makefile );
4472 for (i = 0; i < subdirs.count; i++)
4473 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4475 output_dependencies( top_makefile );
4476 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4478 return 0;