include: Add missing SQL prototype.
[wine.git] / tools / makedep.c
blob532145de292ded360c0a430355f4f40a470747c0
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013, 2020 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
34 #include "tools.h"
35 #include "wine/list.h"
37 enum incl_type
39 INCL_NORMAL, /* #include "foo.h" */
40 INCL_SYSTEM, /* #include <foo.h> */
41 INCL_IMPORT, /* idl import "foo.idl" */
42 INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
43 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
44 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
47 struct dependency
49 int line; /* source line where this header is included */
50 enum incl_type type; /* type of include */
51 char *name; /* header name */
54 struct file
56 struct list entry;
57 char *name; /* full file name relative to cwd */
58 void *args; /* custom arguments for makefile rule */
59 unsigned int flags; /* flags (see below) */
60 unsigned int deps_count; /* files in use */
61 unsigned int deps_size; /* total allocated size */
62 struct dependency *deps; /* all header dependencies */
65 struct incl_file
67 struct list entry;
68 struct list hash_entry;
69 struct file *file;
70 char *name;
71 char *filename;
72 char *basename; /* base target name for generated files */
73 char *sourcename; /* source file name for generated headers */
74 struct incl_file *included_by; /* file that included this one */
75 int included_line; /* line where this file was included */
76 enum incl_type type; /* type of include */
77 unsigned int arch; /* architecture for multi-arch files, otherwise 0 */
78 int use_msvcrt:1; /* put msvcrt headers in the search path? */
79 int is_external:1; /* file from external library? */
80 struct incl_file *owner;
81 unsigned int files_count; /* files in use */
82 unsigned int files_size; /* total allocated size */
83 struct incl_file **files;
84 struct strarray dependencies; /* file dependencies */
85 struct strarray importlibdeps; /* importlib dependencies */
88 #define FLAG_GENERATED 0x000001 /* generated file */
89 #define FLAG_INSTALL 0x000002 /* file to install */
90 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
91 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
92 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
93 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
94 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
95 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (_l.res) file */
96 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
97 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
98 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
99 #define FLAG_RC_HEADER 0x020000 /* rc file is a header */
100 #define FLAG_C_IMPLIB 0x040000 /* file is part of an import library */
101 #define FLAG_C_UNIX 0x080000 /* file is part of a Unix library */
102 #define FLAG_SFD_FONTS 0x100000 /* sfd file generated bitmap fonts */
104 static const struct
106 unsigned int flag;
107 const char *ext;
108 } idl_outputs[] =
110 { FLAG_IDL_TYPELIB, "_l.res" },
111 { FLAG_IDL_REGTYPELIB, "_t.res" },
112 { FLAG_IDL_CLIENT, "_c.c" },
113 { FLAG_IDL_IDENT, "_i.c" },
114 { FLAG_IDL_PROXY, "_p.c" },
115 { FLAG_IDL_SERVER, "_s.c" },
116 { FLAG_IDL_REGISTER, "_r.res" },
119 #define HASH_SIZE 197
121 static struct list files[HASH_SIZE];
122 static struct list global_includes[HASH_SIZE];
124 enum install_rules { INSTALL_LIB, INSTALL_DEV, INSTALL_TEST, NB_INSTALL_RULES };
125 static const char *install_targets[NB_INSTALL_RULES] = { "install-lib", "install-dev", "install-test" };
126 static const char *install_variables[NB_INSTALL_RULES] = { "INSTALL_LIB", "INSTALL_DEV", "INSTALL_TEST" };
128 #define MAX_ARCHS 5
130 /* variables common to all makefiles */
131 static struct strarray archs;
132 static struct strarray linguas;
133 static struct strarray dll_flags;
134 static struct strarray unix_dllflags;
135 static struct strarray msvcrt_flags;
136 static struct strarray cpp_flags;
137 static struct strarray lddll_flags;
138 static struct strarray libs;
139 static struct strarray enable_tests;
140 static struct strarray cmdline_vars;
141 static struct strarray subdirs;
142 static struct strarray delay_import_libs;
143 static struct strarray top_install[NB_INSTALL_RULES];
144 static const char *root_src_dir;
145 static const char *tools_dir;
146 static const char *tools_ext;
147 static const char *exe_ext;
148 static const char *dll_ext;
149 static const char *fontforge;
150 static const char *convert;
151 static const char *flex;
152 static const char *bison;
153 static const char *ar;
154 static const char *ranlib;
155 static const char *rsvg;
156 static const char *icotool;
157 static const char *msgfmt;
158 static const char *ln_s;
159 static const char *sed_cmd;
160 /* per-architecture global variables */
161 static const char *arch_dirs[MAX_ARCHS];
162 static const char *arch_pe_dirs[MAX_ARCHS];
163 static const char *arch_install_dirs[MAX_ARCHS];
164 static const char *strip_progs[MAX_ARCHS];
165 static const char *debug_flags[MAX_ARCHS];
166 static const char *delay_load_flags[MAX_ARCHS];
167 static struct strarray target_flags[MAX_ARCHS];
168 static struct strarray extra_cflags[MAX_ARCHS];
169 static struct strarray extra_cflags_extlib[MAX_ARCHS];
170 static struct strarray disabled_dirs[MAX_ARCHS];
172 struct makefile
174 /* values determined from input makefile */
175 struct strarray vars;
176 struct strarray include_paths;
177 struct strarray include_args;
178 struct strarray define_args;
179 struct strarray unix_cflags;
180 struct strarray programs;
181 struct strarray scripts;
182 struct strarray imports;
183 struct strarray delayimports;
184 struct strarray extradllflags;
185 struct strarray install[NB_INSTALL_RULES];
186 struct strarray extra_targets;
187 struct strarray extra_imports;
188 struct list sources;
189 struct list includes;
190 const char *src_dir;
191 const char *obj_dir;
192 const char *parent_dir;
193 const char *module;
194 const char *testdll;
195 const char *extlib;
196 const char *sharedlib;
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 ok_files;
208 struct strarray pot_files;
209 struct strarray test_files;
210 struct strarray clean_files;
211 struct strarray distclean_files;
212 struct strarray maintainerclean_files;
213 struct strarray uninstall_files;
214 struct strarray unixobj_files;
215 struct strarray font_files;
216 struct strarray debug_files;
217 struct strarray dlldata_files;
218 struct strarray phony_targets;
219 struct strarray dependencies;
220 struct strarray object_files[MAX_ARCHS];
221 struct strarray implib_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;
1433 /* check for extra targets */
1434 if (strarray_exists( &make->extra_targets, pFile->name ))
1436 pFile->sourcename = src_dir_path( make, pFile->name );
1437 pFile->filename = obj_dir_path( make, pFile->name );
1438 return NULL;
1441 /* now try in source dir */
1442 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1444 /* check for global importlib (module dependency) */
1445 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1447 pFile->filename = pFile->name;
1448 return NULL;
1451 /* check for generated files in global includes */
1452 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1453 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1454 if (strendswith( pFile->name, "tmpl.h" ) &&
1455 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1457 /* check in global includes source dir */
1458 if ((file = open_global_header( pFile->name, &pFile->filename ))) return file;
1460 /* check in global msvcrt includes */
1461 if (pFile->use_msvcrt &&
1462 (file = open_global_header( strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1463 return file;
1465 /* now search in include paths */
1466 for (i = 0; i < make->include_paths.count; i++)
1468 const char *dir = make->include_paths.str[i];
1470 if (root_src_dir)
1472 len = strlen( root_src_dir );
1473 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1475 while (dir[len] == '/') len++;
1476 file = open_global_file( concat_paths( dir + len, pFile->name ), &pFile->filename );
1479 else
1481 if (*dir == '/') continue;
1482 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1484 if (!file) continue;
1485 pFile->is_external = 1;
1486 return file;
1489 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1491 /* try in src file directory */
1492 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1493 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1494 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1496 pFile->is_external = pFile->included_by->is_external;
1497 return file;
1500 if (make->extlib) return NULL; /* ignore missing files in external libs */
1502 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1503 perror( pFile->name );
1504 pFile = pFile->included_by;
1505 while (pFile && pFile->included_by)
1507 const char *parent = pFile->included_by->sourcename;
1508 if (!parent) parent = pFile->included_by->file->name;
1509 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1510 parent, pFile->included_line, pFile->name );
1511 pFile = pFile->included_by;
1513 exit(1);
1517 /*******************************************************************
1518 * add_all_includes
1520 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1522 unsigned int i;
1524 for (i = 0; i < file->deps_count; i++)
1526 switch (file->deps[i].type)
1528 case INCL_NORMAL:
1529 case INCL_IMPORT:
1530 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1531 break;
1532 case INCL_IMPORTLIB:
1533 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1534 break;
1535 case INCL_SYSTEM:
1536 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1537 break;
1538 case INCL_CPP_QUOTE:
1539 case INCL_CPP_QUOTE_SYSTEM:
1540 break;
1546 /*******************************************************************
1547 * parse_file
1549 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1551 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1553 if (!file) return;
1555 source->file = file;
1556 source->files_count = 0;
1557 source->files_size = file->deps_count;
1558 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1560 if (strendswith( file->name, ".m" )) file->flags |= FLAG_C_UNIX;
1561 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1562 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1564 if (source->sourcename)
1566 if (strendswith( source->sourcename, ".idl" ))
1568 unsigned int i;
1570 /* generated .h file always includes these */
1571 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1572 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1573 for (i = 0; i < file->deps_count; i++)
1575 switch (file->deps[i].type)
1577 case INCL_IMPORT:
1578 if (strendswith( file->deps[i].name, ".idl" ))
1579 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1580 file->deps[i].line, INCL_NORMAL );
1581 else
1582 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1583 break;
1584 case INCL_CPP_QUOTE:
1585 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1586 break;
1587 case INCL_CPP_QUOTE_SYSTEM:
1588 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1589 break;
1590 case INCL_NORMAL:
1591 case INCL_SYSTEM:
1592 case INCL_IMPORTLIB:
1593 break;
1596 return;
1598 if (strendswith( source->sourcename, ".y" ))
1599 return; /* generated .tab.h doesn't include anything */
1602 add_all_includes( make, source, file );
1606 /*******************************************************************
1607 * add_src_file
1609 * Add a source file to the list.
1611 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1613 struct incl_file *file = xmalloc( sizeof(*file) );
1615 memset( file, 0, sizeof(*file) );
1616 file->name = xstrdup(name);
1617 file->use_msvcrt = is_using_msvcrt( make );
1618 file->is_external = !!make->extlib;
1619 list_add_tail( &make->sources, &file->entry );
1620 if (make == include_makefile)
1622 unsigned int hash = hash_filename( name );
1623 list_add_tail( &global_includes[hash], &file->hash_entry );
1625 parse_file( make, file, 1 );
1626 return file;
1630 /*******************************************************************
1631 * open_input_makefile
1633 static FILE *open_input_makefile( const struct makefile *make )
1635 FILE *ret;
1637 if (make->obj_dir)
1638 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1639 else
1640 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1642 input_line = 0;
1643 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1644 return ret;
1648 /*******************************************************************
1649 * get_make_variable
1651 static const char *get_make_variable( const struct makefile *make, const char *name )
1653 const char *ret;
1655 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1656 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1657 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1658 return NULL;
1662 /*******************************************************************
1663 * get_expanded_make_variable
1665 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1667 const char *var;
1668 char *p, *end, *expand, *tmp;
1670 var = get_make_variable( make, name );
1671 if (!var) return NULL;
1673 p = expand = xstrdup( var );
1674 while ((p = strchr( p, '$' )))
1676 if (p[1] == '(')
1678 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1679 *end++ = 0;
1680 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1681 var = get_make_variable( make, p + 2 );
1682 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1683 /* switch to the new string */
1684 p = tmp + (p - expand);
1685 free( expand );
1686 expand = tmp;
1688 else if (p[1] == '{') /* don't expand ${} variables */
1690 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1691 p = end + 1;
1693 else if (p[1] == '$')
1695 p += 2;
1697 else fatal_error( "syntax error in '%s'\n", expand );
1700 /* consider empty variables undefined */
1701 p = skip_spaces( expand );
1702 if (*p) return expand;
1703 free( expand );
1704 return NULL;
1708 /*******************************************************************
1709 * get_expanded_make_var_array
1711 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1713 struct strarray ret = empty_strarray;
1714 char *value, *token;
1716 if ((value = get_expanded_make_variable( make, name )))
1717 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1718 strarray_add( &ret, token );
1719 return ret;
1723 /*******************************************************************
1724 * get_expanded_file_local_var
1726 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1727 const char *name )
1729 char *p, *var = strmake( "%s_%s", file, name );
1731 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1732 return get_expanded_make_var_array( make, var );
1736 /*******************************************************************
1737 * get_expanded_arch_var
1739 static char *get_expanded_arch_var( const struct makefile *make, const char *name, int arch )
1741 return get_expanded_make_variable( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1745 /*******************************************************************
1746 * get_expanded_arch_var_array
1748 static struct strarray get_expanded_arch_var_array( const struct makefile *make, const char *name, int arch )
1750 return get_expanded_make_var_array( make, arch ? strmake( "%s_%s", archs.str[arch], name ) : name );
1754 /*******************************************************************
1755 * set_make_variable
1757 static int set_make_variable( struct strarray *array, const char *assignment )
1759 char *p, *name;
1761 p = name = xstrdup( assignment );
1762 while (isalnum(*p) || *p == '_') p++;
1763 if (name == p) return 0; /* not a variable */
1764 if (isspace(*p))
1766 *p++ = 0;
1767 p = skip_spaces( p );
1769 if (*p != '=') return 0; /* not an assignment */
1770 *p++ = 0;
1771 p = skip_spaces( p );
1773 strarray_set_value( array, name, p );
1774 return 1;
1778 /*******************************************************************
1779 * parse_makefile
1781 static struct makefile *parse_makefile( const char *path )
1783 char *buffer;
1784 FILE *file;
1785 struct makefile *make = xmalloc( sizeof(*make) );
1787 memset( make, 0, sizeof(*make) );
1788 make->obj_dir = path;
1789 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1790 if (path && !strcmp( path, "include" )) include_makefile = make;
1792 file = open_input_makefile( make );
1793 while ((buffer = get_line( file )))
1795 if (!strncmp( buffer, separator, strlen(separator) )) break;
1796 if (*buffer == '\t') continue; /* command */
1797 buffer = skip_spaces( buffer );
1798 if (*buffer == '#') continue; /* comment */
1799 set_make_variable( &make->vars, buffer );
1801 fclose( file );
1802 input_file_name = NULL;
1803 return make;
1807 /*******************************************************************
1808 * add_generated_sources
1810 static void add_generated_sources( struct makefile *make )
1812 unsigned int i, arch;
1813 struct incl_file *source, *next, *file, *dlldata = NULL;
1814 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1816 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1818 for (arch = 0; arch < archs.count; arch++)
1820 if (!is_multiarch( arch )) continue;
1821 if (source->file->flags & FLAG_IDL_CLIENT)
1823 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL, arch );
1824 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1825 add_all_includes( make, file, file->file );
1827 if (source->file->flags & FLAG_IDL_SERVER)
1829 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL, arch );
1830 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1831 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1832 add_all_includes( make, file, file->file );
1834 if (source->file->flags & FLAG_IDL_IDENT)
1836 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL, arch );
1837 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1838 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1839 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1840 add_all_includes( make, file, file->file );
1842 if (source->file->flags & FLAG_IDL_PROXY)
1844 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL, arch );
1845 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1846 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1847 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1848 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1849 add_all_includes( make, file, file->file );
1851 if (source->file->flags & FLAG_IDL_TYPELIB)
1853 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL, arch );
1855 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1857 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL, arch );
1859 if (source->file->flags & FLAG_IDL_REGISTER)
1861 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL, arch );
1865 /* now the arch-independent files */
1867 if ((source->file->flags & FLAG_IDL_PROXY) && !dlldata)
1869 dlldata = add_generated_source( make, "dlldata.o", "dlldata.c", 0 );
1870 add_dependency( dlldata->file, "objbase.h", INCL_NORMAL );
1871 add_dependency( dlldata->file, "rpcproxy.h", INCL_NORMAL );
1872 add_all_includes( make, dlldata, dlldata->file );
1874 if (source->file->flags & FLAG_IDL_HEADER)
1876 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1878 if (!source->file->flags && strendswith( source->name, ".idl" ))
1880 if (!strncmp( source->name, "wine/", 5 )) continue;
1881 source->file->flags = FLAG_IDL_HEADER | FLAG_INSTALL;
1882 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL, 0 );
1884 if (strendswith( source->name, ".x" ))
1886 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL, 0 );
1888 if (strendswith( source->name, ".y" ))
1890 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL, 0 );
1891 /* steal the includes list from the source file */
1892 file->files_count = source->files_count;
1893 file->files_size = source->files_size;
1894 file->files = source->files;
1895 source->files_count = source->files_size = 0;
1896 source->files = NULL;
1898 if (strendswith( source->name, ".l" ))
1900 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL, 0 );
1901 /* steal the includes list from the source file */
1902 file->files_count = source->files_count;
1903 file->files_size = source->files_size;
1904 file->files = source->files;
1905 source->files_count = source->files_size = 0;
1906 source->files = NULL;
1908 if (strendswith( source->name, ".po" ))
1910 if (!make->disabled[0])
1911 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1913 if (strendswith( source->name, ".spec" ))
1915 char *obj = replace_extension( source->name, ".spec", "" );
1916 strarray_addall_uniq( &make->extra_imports,
1917 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1920 if (make->testdll)
1922 for (arch = 0; arch < archs.count; arch++)
1924 if (!is_multiarch( arch )) continue;
1925 file = add_generated_source( make, "testlist.o", "testlist.c", arch );
1926 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1927 add_all_includes( make, file, file->file );
1930 for (i = 0; i < objs.count; i++)
1932 /* default to .c for unknown extra object files */
1933 if (strendswith( objs.str[i], ".o" ))
1935 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ), 0);
1936 file->file->flags |= FLAG_C_UNIX;
1937 file->use_msvcrt = 0;
1939 else if (strendswith( objs.str[i], ".res" ))
1940 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL, 0 );
1941 else
1942 add_generated_source( make, objs.str[i], NULL, 0 );
1947 /*******************************************************************
1948 * create_dir
1950 static void create_dir( const char *dir )
1952 char *p, *path;
1954 p = path = xstrdup( dir );
1955 while ((p = strchr( p, '/' )))
1957 *p = 0;
1958 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1959 *p++ = '/';
1960 while (*p == '/') p++;
1962 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1963 free( path );
1967 /*******************************************************************
1968 * create_file_directories
1970 * Create the base directories of all the files.
1972 static void create_file_directories( const struct makefile *make, struct strarray files )
1974 struct strarray subdirs = empty_strarray;
1975 unsigned int i;
1976 char *dir;
1978 for (i = 0; i < files.count; i++)
1980 if (!strchr( files.str[i], '/' )) continue;
1981 dir = obj_dir_path( make, files.str[i] );
1982 *strrchr( dir, '/' ) = 0;
1983 strarray_add_uniq( &subdirs, dir );
1986 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1990 /*******************************************************************
1991 * output_filenames_obj_dir
1993 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1995 unsigned int i;
1997 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
2001 /*******************************************************************
2002 * get_dependencies
2004 static void get_dependencies( struct incl_file *file, struct incl_file *source )
2006 unsigned int i;
2008 if (!file->filename) return;
2010 if (file != source)
2012 if (file->owner == source) return; /* already processed */
2013 if (file->type == INCL_IMPORTLIB)
2015 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
2016 return; /* library is imported only when building a typelib */
2017 strarray_add( &source->importlibdeps, file->filename );
2019 else strarray_add( &source->dependencies, file->filename );
2020 file->owner = source;
2022 /* sanity checks */
2023 if (!strcmp( file->filename, "include/config.h" ) &&
2024 file != source->files[0] && !source->is_external)
2026 input_file_name = source->filename;
2027 input_line = 0;
2028 for (i = 0; i < source->file->deps_count; i++)
2030 if (!strcmp( source->file->deps[i].name, file->name ))
2031 input_line = source->file->deps[i].line;
2033 fatal_error( "%s must be included before other headers\n", file->name );
2037 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
2041 /*******************************************************************
2042 * get_local_dependencies
2044 * Get the local dependencies of a given target.
2046 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
2047 struct strarray targets )
2049 unsigned int i;
2050 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2052 for (i = 0; i < deps.count; i++)
2054 if (strarray_exists( &targets, deps.str[i] ))
2055 deps.str[i] = obj_dir_path( make, deps.str[i] );
2056 else
2057 deps.str[i] = src_dir_path( make, deps.str[i] );
2059 return deps;
2063 /*******************************************************************
2064 * get_static_lib
2066 * Find the makefile that builds the named static library (which may be an import lib).
2068 static struct makefile *get_static_lib( const char *name, unsigned int arch )
2070 unsigned int i;
2072 for (i = 0; i < subdirs.count; i++)
2074 if (submakes[i]->importlib && !strcmp( submakes[i]->importlib, name )) return submakes[i];
2075 if (!submakes[i]->staticlib) continue;
2076 if (submakes[i]->disabled[arch]) continue;
2077 if (strncmp( submakes[i]->staticlib, "lib", 3 )) continue;
2078 if (strncmp( submakes[i]->staticlib + 3, name, strlen(name) )) continue;
2079 if (strcmp( submakes[i]->staticlib + 3 + strlen(name), ".a" )) continue;
2080 return submakes[i];
2082 return NULL;
2086 /*******************************************************************
2087 * get_native_unix_lib
2089 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2091 if (!make->unixlib) return NULL;
2092 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2093 if (make->unixlib[strlen(name)] != '.') return NULL;
2094 return obj_dir_path( make, make->unixlib );
2098 /*******************************************************************
2099 * get_parent_makefile
2101 static struct makefile *get_parent_makefile( struct makefile *make )
2103 char *dir, *p;
2104 unsigned int i;
2106 if (!make->obj_dir) return NULL;
2107 dir = xstrdup( make->obj_dir );
2108 if (!(p = strrchr( dir, '/' ))) return NULL;
2109 *p = 0;
2110 for (i = 0; i < subdirs.count; i++)
2111 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2112 return NULL;
2116 /*******************************************************************
2117 * needs_delay_lib
2119 static int needs_delay_lib( const struct makefile *make, unsigned int arch )
2121 if (delay_load_flags[arch]) return 0;
2122 if (!make->importlib) return 0;
2123 return strarray_exists( &delay_import_libs, make->importlib );
2127 /*******************************************************************
2128 * add_unix_libraries
2130 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2132 struct strarray ret = empty_strarray;
2133 struct strarray all_libs = empty_strarray;
2134 unsigned int i, j;
2136 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2137 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
2139 for (i = 0; i < all_libs.count; i++)
2141 const char *lib = NULL;
2143 if (!strncmp( all_libs.str[i], "-l", 2 ))
2145 for (j = 0; j < subdirs.count; j++)
2147 if (make == submakes[j]) continue;
2148 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2151 if (lib)
2153 strarray_add( deps, lib );
2154 strarray_add( &ret, lib );
2156 else strarray_add( &ret, all_libs.str[i] );
2159 strarray_addall( &ret, libs );
2160 return ret;
2164 /*******************************************************************
2165 * is_crt_module
2167 static int is_crt_module( const char *file )
2169 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2173 /*******************************************************************
2174 * get_default_crt
2176 static const char *get_default_crt( const struct makefile *make )
2178 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2179 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2183 /*******************************************************************
2184 * get_crt_define
2186 static const char *get_crt_define( const struct makefile *make )
2188 const char *crt_dll = NULL;
2189 unsigned int i, version = 0;
2191 for (i = 0; i < make->imports.count; i++)
2193 if (!is_crt_module( make->imports.str[i] )) continue;
2194 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2195 crt_dll, make->imports.str[i] );
2196 crt_dll = make->imports.str[i];
2199 if (!crt_dll)
2201 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2202 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2204 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2205 sscanf( crt_dll, "msvcr%u", &version );
2206 return strmake( "-D_MSVCR_VER=%u", version );
2210 /*******************************************************************
2211 * get_default_imports
2213 static struct strarray get_default_imports( const struct makefile *make, struct strarray imports )
2215 struct strarray ret = empty_strarray;
2216 const char *crt_dll = get_default_crt( make );
2217 unsigned int i;
2219 for (i = 0; i < imports.count; i++)
2220 if (is_crt_module( imports.str[i] ))
2221 crt_dll = imports.str[i];
2223 strarray_add( &ret, "winecrt0" );
2224 if (crt_dll) strarray_add( &ret, crt_dll );
2226 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2227 strarray_add( &ret, "kernel" );
2229 strarray_add( &ret, "kernel32" );
2230 strarray_add( &ret, "ntdll" );
2231 return ret;
2234 enum import_type
2236 IMPORT_TYPE_DIRECT,
2237 IMPORT_TYPE_DELAYED,
2238 IMPORT_TYPE_DEFAULT,
2241 /*******************************************************************
2242 * add_import_libs
2244 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2245 struct strarray imports, enum import_type type, unsigned int arch )
2247 struct strarray ret = empty_strarray;
2248 unsigned int i;
2250 for (i = 0; i < imports.count; i++)
2252 const char *name = imports.str[i];
2253 struct makefile *submake;
2255 /* add crt import lib only when adding the default imports libs */
2256 if (is_crt_module( imports.str[i] ) && type != IMPORT_TYPE_DEFAULT) continue;
2258 if (name[0] == '-')
2260 switch (name[1])
2262 case 'L': strarray_add( &ret, name ); continue;
2263 case 'l': name += 2; break;
2264 default: continue;
2267 else name = get_base_name( name );
2269 if ((submake = get_static_lib( name, arch )))
2271 const char *ext = (type == IMPORT_TYPE_DELAYED && !delay_load_flags[arch]) ? ".delay.a" : ".a";
2272 const char *lib = obj_dir_path( submake, strmake( "%slib%s%s", arch_dirs[arch], name, ext ));
2273 strarray_add_uniq( deps, lib );
2274 strarray_add( &ret, lib );
2276 else strarray_add( &ret, strmake( "-l%s", name ));
2278 return ret;
2282 /*******************************************************************
2283 * add_install_rule
2285 static void add_install_rule( struct makefile *make, const char *target, unsigned int arch,
2286 const char *file, const char *dest )
2288 unsigned int i;
2290 if (make->disabled[arch]) return;
2292 for (i = 0; i < NB_INSTALL_RULES; i++)
2294 if (strarray_exists( &make->install[i], target ) ||
2295 strarray_exists( &top_install[i], make->obj_dir ) ||
2296 strarray_exists( &top_install[i], obj_dir_path( make, target )))
2298 strarray_add( &make->install_rules[i], file );
2299 strarray_add( &make->install_rules[i], dest );
2300 break;
2306 /*******************************************************************
2307 * get_include_install_path
2309 * Determine the installation path for a given include file.
2311 static const char *get_include_install_path( const char *name )
2313 if (!strncmp( name, "wine/", 5 )) return name + 5;
2314 if (!strncmp( name, "msvcrt/", 7 )) return name;
2315 return strmake( "windows/%s", name );
2319 /*******************************************************************
2320 * get_shared_library_name
2322 * Determine possible names for a shared library with a version number.
2324 static struct strarray get_shared_lib_names( const char *libname )
2326 struct strarray ret = empty_strarray;
2327 const char *ext, *p;
2328 char *name, *first, *second;
2329 size_t len = 0;
2331 strarray_add( &ret, libname );
2333 for (p = libname; (p = strchr( p, '.' )); p++)
2334 if ((len = strspn( p + 1, "0123456789." ))) break;
2336 if (!len) return ret;
2337 ext = p + 1 + len;
2338 if (*ext && ext[-1] == '.') ext--;
2340 /* keep only the first group of digits */
2341 name = xstrdup( libname );
2342 first = name + (p - libname);
2343 if ((second = strchr( first + 1, '.' )))
2345 strcpy( second, ext );
2346 strarray_add( &ret, xstrdup( name ));
2348 return ret;
2352 /*******************************************************************
2353 * get_source_defines
2355 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2356 const char *obj )
2358 unsigned int i;
2359 struct strarray ret = empty_strarray;
2361 strarray_addall( &ret, make->include_args );
2362 if (source->use_msvcrt)
2364 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2365 for (i = 0; i < make->include_paths.count; i++)
2366 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2367 strarray_add( &ret, get_crt_define( make ));
2369 strarray_addall( &ret, make->define_args );
2370 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2371 if (source->file->flags & FLAG_C_UNIX) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2372 return ret;
2376 /*******************************************************************
2377 * remove_warning_flags
2379 static struct strarray remove_warning_flags( struct strarray flags )
2381 unsigned int i;
2382 struct strarray ret = empty_strarray;
2384 for (i = 0; i < flags.count; i++)
2385 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2386 strarray_add( &ret, flags.str[i] );
2387 return ret;
2391 /*******************************************************************
2392 * get_debug_file
2394 static const char *get_debug_file( struct makefile *make, const char *name, unsigned int arch )
2396 const char *debug_file = NULL;
2397 if (!debug_flags[arch]) return NULL;
2398 if (!strcmp( debug_flags[arch], "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2399 else if (!strncmp( debug_flags[arch], "split", 5 )) debug_file = strmake( "%s.debug", name );
2400 if (debug_file) strarray_add( &make->debug_files, debug_file );
2401 return debug_file;
2405 /*******************************************************************
2406 * cmd_prefix
2408 static const char *cmd_prefix( const char *cmd )
2410 if (!silent_rules) return "";
2411 return strmake( "$(quiet_%s)", cmd );
2415 /*******************************************************************
2416 * output_winegcc_command
2418 static void output_winegcc_command( struct makefile *make, unsigned int arch )
2420 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2421 output_filename( "--wine-objdir ." );
2422 if (tools_dir)
2424 output_filename( "--winebuild" );
2425 output_filename( tools_path( make, "winebuild" ));
2427 output_filenames( target_flags[arch] );
2428 if (arch) return;
2429 output_filename( "-mno-cygwin" );
2430 output_filenames( lddll_flags );
2434 /*******************************************************************
2435 * output_symlink_rule
2437 * Output a rule to create a symlink.
2439 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2441 const char *name = strrchr( link_name, '/' );
2442 char *dir = NULL;
2444 if (name)
2446 dir = xstrdup( link_name );
2447 dir[name - link_name] = 0;
2450 output( "\t%s", cmd_prefix( "LN" ));
2451 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2452 output( "rm -f %s && ", link_name );
2454 /* dest path with a directory needs special handling if ln -s isn't supported */
2455 if (dir && strcmp( ln_s, "ln -s" ))
2456 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2457 else
2458 output( "%s %s %s\n", ln_s, src_name, link_name );
2460 free( dir );
2464 /*******************************************************************
2465 * output_srcdir_symlink
2467 * Output rule to create a symlink back to the source directory, for source files
2468 * that are needed at run-time.
2470 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2472 char *src_file, *dst_file, *src_name;
2474 if (!make->src_dir) return;
2475 src_file = src_dir_path( make, obj );
2476 dst_file = obj_dir_path( make, obj );
2477 output( "%s: %s\n", dst_file, src_file );
2479 src_name = src_file;
2480 if (src_name[0] != '/' && make->obj_dir)
2481 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2483 output_symlink_rule( src_name, dst_file, 0 );
2484 strarray_add( &make->all_targets[0], obj );
2488 /*******************************************************************
2489 * output_install_commands
2491 static void output_install_commands( struct makefile *make, struct strarray files )
2493 unsigned int i, arch;
2494 char *install_sh = root_src_dir_path( "tools/install-sh" );
2496 for (i = 0; i < files.count; i += 2)
2498 const char *file = files.str[i];
2499 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2500 char type = *files.str[i + 1];
2502 switch (type)
2504 case '1': case '2': case '3': case '4': case '5':
2505 case '6': case '7': case '8': case '9': /* arch-dependent program */
2506 arch = type - '0';
2507 output( "\tSTRIPPROG=%s %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2508 strip_progs[arch], install_sh, obj_dir_path( make, file ), dest );
2509 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2510 break;
2511 case 'd': /* data file */
2512 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2513 install_sh, obj_dir_path( make, file ), dest );
2514 break;
2515 case 'D': /* data file in source dir */
2516 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2517 install_sh, src_dir_path( make, file ), dest );
2518 break;
2519 case '0': /* native arch program file */
2520 case 'p': /* program file */
2521 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2522 install_sh, obj_dir_path( make, file ), dest );
2523 break;
2524 case 's': /* script */
2525 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2526 install_sh, obj_dir_path( make, file ), dest );
2527 break;
2528 case 'S': /* script in source dir */
2529 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2530 install_sh, src_dir_path( make, file ), dest );
2531 break;
2532 case 't': /* script in tools dir */
2533 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2534 install_sh, tools_dir_path( make, files.str[i] ), dest );
2535 break;
2536 case 'y': /* symlink */
2537 output_symlink_rule( files.str[i], dest, 1 );
2538 break;
2539 default:
2540 assert(0);
2542 strarray_add( &make->uninstall_files, dest );
2547 /*******************************************************************
2548 * output_install_rules
2550 * Rules are stored as a (file,dest) pair of values.
2551 * The first char of dest indicates the type of install.
2553 static void output_install_rules( struct makefile *make, enum install_rules rules )
2555 unsigned int i;
2556 struct strarray files = make->install_rules[rules];
2557 struct strarray targets = empty_strarray;
2559 if (!files.count) return;
2561 for (i = 0; i < files.count; i += 2)
2563 const char *file = files.str[i];
2564 switch (*files.str[i + 1])
2566 case '0': case '1': case '2': case '3': case '4':
2567 case '5': case '6': case '7': case '8': case '9': /* arch-dependent program */
2568 case 'd': /* data file */
2569 case 'p': /* program file */
2570 case 's': /* script */
2571 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2572 break;
2573 case 't': /* script in tools dir */
2574 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2575 break;
2579 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, install_targets[rules] ));
2580 output_filenames( targets );
2581 output( "\n" );
2582 output_install_commands( make, files );
2583 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2584 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, install_targets[rules] ));
2588 static int cmp_string_length( const char **a, const char **b )
2590 int paths_a = 0, paths_b = 0;
2591 const char *p;
2593 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2594 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2595 if (paths_b != paths_a) return paths_b - paths_a;
2596 return strcmp( *a, *b );
2599 /*******************************************************************
2600 * get_removable_dirs
2602 * Retrieve a list of directories to try to remove after deleting the files.
2604 static struct strarray get_removable_dirs( struct strarray files )
2606 struct strarray dirs = empty_strarray;
2607 unsigned int i;
2609 for (i = 0; i < files.count; i++)
2611 char *dir = xstrdup( files.str[i] );
2612 while (strchr( dir, '/' ))
2614 *strrchr( dir, '/' ) = 0;
2615 strarray_add_uniq( &dirs, xstrdup(dir) );
2618 strarray_qsort( &dirs, cmp_string_length );
2619 return dirs;
2623 /*******************************************************************
2624 * output_uninstall_rules
2626 static void output_uninstall_rules( struct makefile *make )
2628 static const char *dirs_order[] =
2629 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2631 struct strarray uninstall_dirs;
2632 unsigned int i, j;
2634 if (!make->uninstall_files.count) return;
2635 output( "uninstall::\n" );
2636 output_rm_filenames( make->uninstall_files, "rm -f" );
2637 strarray_add_uniq( &make->phony_targets, "uninstall" );
2639 if (!subdirs.count) return;
2640 uninstall_dirs = get_removable_dirs( make->uninstall_files );
2641 output( "\t-rmdir" );
2642 for (i = 0; i < ARRAY_SIZE(dirs_order); i++)
2644 for (j = 0; j < uninstall_dirs.count; j++)
2646 if (!uninstall_dirs.str[j]) continue;
2647 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2648 continue;
2649 output_filename( uninstall_dirs.str[j] );
2650 uninstall_dirs.str[j] = NULL;
2653 for (j = 0; j < uninstall_dirs.count; j++)
2654 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2655 output( "\n" );
2659 /*******************************************************************
2660 * output_po_files
2662 static void output_po_files( struct makefile *make )
2664 const char *po_dir = src_dir_path( make, "po" );
2665 unsigned int i;
2667 if (linguas.count)
2669 for (i = 0; i < linguas.count; i++)
2670 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2671 output( ": %s/wine.pot\n", po_dir );
2672 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2673 cmd_prefix( "MSG" ), po_dir );
2674 output( "po/all:" );
2675 for (i = 0; i < linguas.count; i++)
2676 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2677 output( "\n" );
2679 output( "%s/wine.pot:", po_dir );
2680 output_filenames( make->pot_files );
2681 output( "\n" );
2682 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2683 output_filenames( make->pot_files );
2684 output( "\n" );
2685 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2689 /*******************************************************************
2690 * output_source_y
2692 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2694 /* add source file dependency for parallel makes */
2695 char *header = strmake( "%s.tab.h", obj );
2697 if (find_include_file( make, header ))
2699 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2700 output( "\t%s%s -o %s.tab.c -d %s\n",
2701 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2702 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2703 source->filename, obj_dir_path( make, header ));
2704 strarray_add( &make->clean_files, header );
2706 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2708 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2712 /*******************************************************************
2713 * output_source_l
2715 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2717 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2718 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2722 /*******************************************************************
2723 * output_source_h
2725 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2727 if (source->file->flags & FLAG_GENERATED)
2728 strarray_add( &make->all_targets[0], source->name );
2729 else if ((source->file->flags & FLAG_INSTALL) || strncmp( source->name, "wine/", 5 ))
2730 add_install_rule( make, source->name, 0, source->name,
2731 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2735 /*******************************************************************
2736 * output_source_rc
2738 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2740 struct strarray defines = get_source_defines( make, source, obj );
2741 const char *po_dir = NULL, *res_file = strmake( "%s.res", obj );
2742 unsigned int i, arch;
2744 if (source->file->flags & FLAG_RC_HEADER) return;
2745 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2746 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2747 for (arch = 0; arch < archs.count; arch++)
2748 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2749 if (source->file->flags & FLAG_RC_PO)
2751 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2752 output( "%s.pot ", obj_dir_path( make, obj ) );
2754 output( "%s: %s", obj_dir_path( make, res_file ), source->filename );
2755 output_filename( tools_path( make, "wrc" ));
2756 if (make->src_dir) output_filename( "nls/locale.nls" );
2757 output_filenames( source->dependencies );
2758 output( "\n" );
2759 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2760 if (make->is_win16) output_filename( "-m16" );
2761 output_filename( "--nostdinc" );
2762 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2763 output_filenames( defines );
2764 output_filename( source->filename );
2765 output( "\n" );
2766 if (po_dir)
2768 output( "%s:", obj_dir_path( make, res_file ));
2769 for (i = 0; i < linguas.count; i++)
2770 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2771 output( "\n" );
2776 /*******************************************************************
2777 * output_source_mc
2779 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2781 unsigned int i, arch;
2782 char *obj_path = obj_dir_path( make, obj );
2783 char *res_file = strmake( "%s.res", obj );
2785 for (arch = 0; arch < archs.count; arch++)
2786 if (!make->disabled[arch]) strarray_add( &make->res_files[arch], res_file );
2787 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2788 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2789 output_filename( tools_path( make, "wmc" ));
2790 output_filenames( source->dependencies );
2791 if (make->src_dir) output_filename( "nls/locale.nls" );
2792 output( "\n" );
2793 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2794 if (linguas.count)
2796 output_filename( "--po-dir=po" );
2797 output( "\n" );
2798 output( "%s.res:", obj_dir_path( make, obj ));
2799 for (i = 0; i < linguas.count; i++)
2800 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2802 output( "\n" );
2806 /*******************************************************************
2807 * output_source_res
2809 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2811 if (make->disabled[source->arch]) return;
2812 strarray_add( &make->res_files[source->arch], source->name );
2816 /*******************************************************************
2817 * output_source_idl
2819 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2821 struct strarray defines = get_source_defines( make, source, obj );
2822 struct strarray headers = empty_strarray;
2823 struct strarray deps = empty_strarray;
2824 struct strarray multiarch_targets[MAX_ARCHS] = { empty_strarray };
2825 const char *dest;
2826 unsigned int i, arch;
2828 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2829 if (!source->file->flags) return;
2831 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2832 if (source->file->flags & FLAG_INSTALL)
2834 add_install_rule( make, source->name, 0, xstrdup( source->name ),
2835 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2836 if (source->file->flags & FLAG_IDL_HEADER)
2837 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2838 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2840 if (source->file->flags & FLAG_IDL_HEADER)
2842 dest = strmake( "%s.h", obj );
2843 strarray_add( &headers, dest );
2844 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2847 for (i = 0; i < ARRAY_SIZE(idl_outputs); i++)
2849 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2850 for (arch = 0; arch < archs.count; arch++)
2852 if (!is_multiarch( arch )) continue;
2853 if (make->disabled[arch]) continue;
2854 dest = strmake( "%s%s%s", arch_dirs[arch], obj, idl_outputs[i].ext );
2855 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2856 strarray_add( &multiarch_targets[arch], dest );
2860 for (arch = 0; arch < archs.count; arch++)
2862 struct strarray arch_deps = empty_strarray;
2864 if (!arch) strarray_addall( &arch_deps, headers );
2865 strarray_addall( &arch_deps, multiarch_targets[arch] );
2866 if (!arch_deps.count) continue;
2867 output_filenames_obj_dir( make, arch_deps );
2868 output( ":\n" );
2869 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2870 output_filenames( target_flags[arch] );
2871 output_filename( "--nostdinc" );
2872 output_filename( "-Ldlls/\\*" );
2873 output_filenames( defines );
2874 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2875 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2876 output_filename( source->filename );
2877 output( "\n" );
2878 strarray_addall( &deps, arch_deps );
2881 if (deps.count)
2883 output_filenames_obj_dir( make, deps );
2884 output( ":" );
2885 output_filename( tools_path( make, "widl" ));
2886 output_filename( source->filename );
2887 output_filenames( source->dependencies );
2888 output( "\n" );
2891 if (source->importlibdeps.count)
2893 for (arch = 0; arch < archs.count; arch++)
2895 if (!multiarch_targets[arch].count) continue;
2896 output_filenames_obj_dir( make, multiarch_targets[arch] );
2897 output( ":" );
2898 for (i = 0; i < source->importlibdeps.count; i++)
2900 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2901 const char *module = strmake( "%s%s", arch_pe_dirs[arch], submake->module );
2902 output_filename( obj_dir_path( submake, module ));
2904 output( "\n" );
2910 /*******************************************************************
2911 * output_source_x
2913 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2915 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2916 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2917 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2918 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2919 if (source->file->flags & FLAG_INSTALL)
2921 add_install_rule( make, source->name, 0, source->name,
2922 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2923 add_install_rule( make, source->name, 0, strmake( "%s.h", obj ),
2924 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2929 /*******************************************************************
2930 * output_source_sfd
2932 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2934 unsigned int i;
2935 char *ttf_obj = strmake( "%s.ttf", obj );
2936 char *ttf_file = src_dir_path( make, ttf_obj );
2938 if (fontforge && !make->src_dir)
2940 output( "%s: %s\n", ttf_file, source->filename );
2941 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2942 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2943 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2944 strarray_add( &make->maintainerclean_files, ttf_obj );
2946 if (source->file->flags & FLAG_INSTALL)
2948 add_install_rule( make, source->name, 0, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2949 output_srcdir_symlink( make, ttf_obj );
2952 if (source->file->flags & FLAG_SFD_FONTS)
2954 struct strarray *array = source->file->args;
2956 for (i = 0; i < array->count; i++)
2958 char *font = strtok( xstrdup(array->str[i]), " \t" );
2959 char *args = strtok( NULL, "" );
2961 strarray_add( &make->all_targets[0], xstrdup( font ));
2962 output( "%s: %s %s\n", obj_dir_path( make, font ),
2963 tools_path( make, "sfnt2fon" ), ttf_file );
2964 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2965 tools_path( make, "sfnt2fon" ), ttf_file, args );
2966 add_install_rule( make, source->name, 0, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2972 /*******************************************************************
2973 * output_source_svg
2975 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2977 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2978 unsigned int i;
2980 if (convert && rsvg && icotool)
2982 for (i = 0; images[i]; i++)
2983 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2985 if (images[i])
2987 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2988 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2989 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2990 root_src_dir_path( "tools/buildimage" ), source->filename );
2991 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2997 /*******************************************************************
2998 * output_source_nls
3000 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
3002 add_install_rule( make, source->name, 0, source->name,
3003 strmake( "D$(nlsdir)/%s", source->name ));
3004 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
3008 /*******************************************************************
3009 * output_source_desktop
3011 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
3013 add_install_rule( make, source->name, 0, source->name,
3014 strmake( "D$(datadir)/applications/%s", source->name ));
3018 /*******************************************************************
3019 * output_source_po
3021 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
3023 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
3024 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
3025 strarray_add( &make->all_targets[0], strmake( "%s.mo", obj ));
3029 /*******************************************************************
3030 * output_source_in
3032 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
3034 unsigned int i;
3036 if (make == include_makefile) return; /* ignore generated includes */
3037 if (strendswith( obj, ".man" ) && source->file->args)
3039 struct strarray symlinks;
3040 char *dir, *dest = replace_extension( obj, ".man", "" );
3041 char *lang = strchr( dest, '.' );
3042 char *section = source->file->args;
3043 if (lang)
3045 *lang++ = 0;
3046 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3048 else dir = strmake( "$(mandir)/man%s", section );
3049 add_install_rule( make, dest, 0, obj, strmake( "d%s/%s.%s", dir, dest, section ));
3050 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3051 for (i = 0; i < symlinks.count; i++)
3052 add_install_rule( make, symlinks.str[i], 0, strmake( "%s.%s", dest, section ),
3053 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3054 free( dest );
3055 free( dir );
3057 strarray_add( &make->in_files, obj );
3058 strarray_add( &make->all_targets[0], obj );
3059 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3060 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
3061 output( "%s:", obj_dir_path( make, obj ));
3062 output_filenames( source->dependencies );
3063 output( "\n" );
3064 add_install_rule( make, obj, 0, obj, strmake( "d$(datadir)/wine/%s", obj ));
3068 /*******************************************************************
3069 * output_source_spec
3071 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3073 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3074 struct strarray dll_flags = empty_strarray;
3075 struct strarray default_imports = empty_strarray;
3076 struct strarray all_libs, dep_libs;
3077 const char *dll_name, *obj_name, *res_name, *output_file, *debug_file;
3078 unsigned int arch;
3080 if (!imports.count) imports = make->imports;
3081 strarray_addall( &dll_flags, make->extradllflags );
3082 strarray_addall( &dll_flags, get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" ));
3083 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3085 for (arch = 0; arch < archs.count; arch++)
3087 if (!is_multiarch( arch )) continue;
3088 all_libs = dep_libs = empty_strarray;
3089 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ) );
3090 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3091 if (!arch) strarray_addall( &all_libs, libs );
3092 dll_name = arch_module_name( strmake( "%s.dll", obj ), arch );
3093 obj_name = obj_dir_path( make, strmake( "%s%s.o", arch_dirs[arch], obj ));
3094 res_name = strmake( "%s%s.res", arch_dirs[arch], obj );
3095 output_file = obj_dir_path( make, dll_name );
3097 strarray_add( &make->clean_files, dll_name );
3098 strarray_add( &make->res_files[arch], res_name );
3099 output( "%s:", obj_dir_path( make, res_name ));
3100 output_filename( output_file );
3101 output_filename( tools_path( make, "wrc" ));
3102 output( "\n" );
3103 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
3104 tools_path( make, "wrc" ));
3106 output( "%s:", output_file );
3107 output_filename( source->filename );
3108 output_filename( obj_name );
3109 output_filenames( dep_libs );
3110 output_filename( tools_path( make, "winebuild" ));
3111 output_filename( tools_path( make, "winegcc" ));
3112 output( "\n" );
3113 output_winegcc_command( make, arch );
3114 output_filename( "-s" );
3115 output_filenames( dll_flags );
3116 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3117 output_filename( "-shared" );
3118 output_filename( source->filename );
3119 output_filename( obj_name );
3120 if ((debug_file = get_debug_file( make, dll_name, arch )))
3121 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3122 output_filenames( all_libs );
3123 output_filename( arch_make_variable( "LDFLAGS", arch ));
3124 output( "\n" );
3129 /*******************************************************************
3130 * output_source_one_arch
3132 static void output_source_one_arch( struct makefile *make, struct incl_file *source, const char *obj,
3133 struct strarray defines, struct strarray *targets,
3134 unsigned int arch, int is_dll_src )
3136 const char *obj_name;
3138 if (make->disabled[arch] && !(source->file->flags & FLAG_C_IMPLIB)) return;
3140 if (arch)
3142 if (source->file->flags & FLAG_C_UNIX) return;
3143 if (!is_using_msvcrt( make ) && !make->staticlib && !(source->file->flags & FLAG_C_IMPLIB)) return;
3145 else if (source->file->flags & FLAG_C_UNIX)
3147 if (!*dll_ext) return;
3149 else if (archs.count > 1 && is_using_msvcrt( make ) &&
3150 !(source->file->flags & FLAG_C_IMPLIB) &&
3151 (!make->staticlib || make->extlib)) return;
3153 obj_name = strmake( "%s%s.o", source->arch ? "" : arch_dirs[arch], obj );
3154 strarray_add( targets, obj_name );
3156 if (source->file->flags & FLAG_C_UNIX)
3157 strarray_add( &make->unixobj_files, obj_name );
3158 else if (source->file->flags & FLAG_C_IMPLIB)
3159 strarray_add( &make->implib_files[arch], obj_name );
3160 else if (!is_dll_src)
3161 strarray_add( &make->object_files[arch], obj_name );
3162 else
3163 strarray_add( &make->clean_files, obj_name );
3165 output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename );
3166 output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), arch_make_variable( "CC", arch ), source->filename );
3167 output_filenames( defines );
3168 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3169 output_filenames( make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] );
3170 if (!arch)
3172 if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
3174 output_filenames( unix_dllflags );
3176 else if (make->module || make->testdll)
3178 output_filenames( dll_flags );
3179 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3180 if (!*dll_ext && make->module && is_crt_module( make->module ))
3181 output_filename( "-fno-builtin" );
3184 else
3186 if (make->module && is_crt_module( make->module )) output_filename( "-fno-builtin" );
3189 output_filenames( cpp_flags );
3190 output_filename( arch_make_variable( "CFLAGS", arch ));
3191 output( "\n" );
3193 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3194 !(source->file->flags & FLAG_GENERATED))
3196 const char *ok_file, *test_exe;
3198 ok_file = strmake( "%s%s.ok", arch_dirs[arch], obj );
3199 test_exe = replace_extension( make->testdll, ".dll", "_test.exe" );
3200 strarray_add( &make->ok_files, ok_file );
3201 output( "%s:\n", obj_dir_path( make, ok_file ));
3202 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s %s && touch $@\n",
3203 cmd_prefix( "TEST" ),
3204 root_src_dir_path( "tools/runtest" ), make->testdll,
3205 obj_dir_path( make, arch_module_name( test_exe, arch )), obj );
3210 /*******************************************************************
3211 * output_source_default
3213 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3215 struct strarray defines = get_source_defines( make, source, obj );
3216 struct strarray targets = empty_strarray;
3217 int is_dll_src = (make->testdll && strendswith( source->name, ".c" ) &&
3218 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3219 unsigned int arch;
3221 for (arch = 0; arch < archs.count; arch++)
3222 if (!source->arch || source->arch == arch)
3223 output_source_one_arch( make, source, obj, defines, &targets, arch, is_dll_src );
3225 if (source->file->flags & FLAG_GENERATED)
3227 if (!make->testdll || !strendswith( source->filename, "testlist.c" ))
3228 strarray_add( &make->clean_files, source->basename );
3230 else
3232 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ))
3233 strarray_add( &make->test_files, obj );
3236 output_filenames_obj_dir( make, targets );
3237 output( ":" );
3238 output_filenames( source->dependencies );
3239 output( "\n" );
3243 /* dispatch table to output rules for a single source file */
3244 static const struct
3246 const char *ext;
3247 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3248 } output_source_funcs[] =
3250 { "y", output_source_y },
3251 { "l", output_source_l },
3252 { "h", output_source_h },
3253 { "rh", output_source_h },
3254 { "inl", output_source_h },
3255 { "rc", output_source_rc },
3256 { "mc", output_source_mc },
3257 { "res", output_source_res },
3258 { "idl", output_source_idl },
3259 { "sfd", output_source_sfd },
3260 { "svg", output_source_svg },
3261 { "nls", output_source_nls },
3262 { "desktop", output_source_desktop },
3263 { "po", output_source_po },
3264 { "in", output_source_in },
3265 { "x", output_source_x },
3266 { "spec", output_source_spec },
3267 { NULL, output_source_default }
3271 /*******************************************************************
3272 * output_fake_module
3274 static void output_fake_module( struct makefile *make )
3276 unsigned int arch = 0; /* fake modules are always native */
3277 const char *spec_file = NULL, *name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3279 if (make->disabled[arch]) return;
3281 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3283 strarray_add( &make->all_targets[arch], name );
3284 add_install_rule( make, make->module, arch, name, strmake( "d$(dlldir)/%s", name ));
3286 output( "%s:", obj_dir_path( make, name ));
3287 if (spec_file) output_filename( spec_file );
3288 output_filenames_obj_dir( make, make->res_files[arch] );
3289 output_filename( tools_path( make, "winebuild" ));
3290 output_filename( tools_path( make, "winegcc" ));
3291 output( "\n" );
3292 output_winegcc_command( make, arch );
3293 output_filename( "-Wb,--fake-module" );
3294 if (spec_file)
3296 output_filename( "-shared" );
3297 output_filename( spec_file );
3299 output_filenames( make->extradllflags );
3300 output_filenames_obj_dir( make, make->res_files[arch] );
3301 output( "\n" );
3305 /*******************************************************************
3306 * output_module
3308 static void output_module( struct makefile *make, unsigned int arch )
3310 struct strarray default_imports = empty_strarray;
3311 struct strarray all_libs = empty_strarray;
3312 struct strarray dep_libs = empty_strarray;
3313 struct strarray imports = make->imports;
3314 const char *module_name;
3315 const char *debug_file;
3316 char *spec_file = NULL;
3317 unsigned int i;
3319 if (make->disabled[arch]) return;
3321 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3323 if (!make->data_only)
3325 module_name = arch_module_name( make->module, arch );
3327 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3329 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, arch ));
3330 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, IMPORT_TYPE_DELAYED, arch ));
3331 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3332 if (!arch) strarray_addall( &all_libs, libs );
3334 if (delay_load_flags[arch])
3336 for (i = 0; i < make->delayimports.count; i++)
3338 struct makefile *import = get_static_lib( make->delayimports.str[i], arch );
3339 if (import) strarray_add( &all_libs, strmake( "%s%s", delay_load_flags[arch], import->module ));
3343 else module_name = strmake( "%s%s", arch_pe_dirs[arch], make->module );
3345 strarray_add( &make->all_targets[arch], module_name );
3346 if (make->data_only)
3347 add_install_rule( make, make->module, arch, module_name,
3348 strmake( "d$(dlldir)/%s%s", arch_pe_dirs[arch], make->module ));
3349 else
3350 add_install_rule( make, make->module, arch, module_name,
3351 strmake( "%c%s%s%s", '0' + arch, arch_install_dirs[arch], make->module,
3352 arch ? "" : dll_ext ));
3354 output( "%s:", obj_dir_path( make, module_name ));
3355 if (spec_file) output_filename( spec_file );
3356 output_filenames_obj_dir( make, make->object_files[arch] );
3357 output_filenames_obj_dir( make, make->res_files[arch] );
3358 output_filenames( dep_libs );
3359 output_filename( tools_path( make, "winebuild" ));
3360 output_filename( tools_path( make, "winegcc" ));
3361 output( "\n" );
3362 output_winegcc_command( make, arch );
3363 if (arch) output_filename( "-Wl,--wine-builtin" );
3364 if (spec_file)
3366 output_filename( "-shared" );
3367 output_filename( spec_file );
3369 output_filenames( make->extradllflags );
3370 if (arch) output_filenames( get_expanded_arch_var_array( make, "EXTRADLLFLAGS", arch ));
3371 output_filenames_obj_dir( make, make->object_files[arch] );
3372 output_filenames_obj_dir( make, make->res_files[arch] );
3373 debug_file = get_debug_file( make, module_name, arch );
3374 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3375 output_filenames( all_libs );
3376 output_filename( arch_make_variable( "LDFLAGS", arch ));
3377 output( "\n" );
3379 if (!make->data_only && !arch && *dll_ext) output_fake_module( make );
3383 /*******************************************************************
3384 * output_import_lib
3386 static void output_import_lib( struct makefile *make, unsigned int arch )
3388 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3389 const char *name = strmake( "%slib%s.a", arch_dirs[arch], make->importlib );
3391 strarray_add( &make->clean_files, name );
3392 if (needs_delay_lib( make, arch ))
3394 const char *delay_name = replace_extension( name, ".a", ".delay.a" );
3395 strarray_add( &make->clean_files, delay_name );
3396 output( "%s ", obj_dir_path( make, delay_name ));
3398 output( "%s: %s %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ), spec_file );
3399 output_filenames_obj_dir( make, make->implib_files[arch] );
3400 output( "\n" );
3401 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3402 if (!delay_load_flags[arch]) output_filename( "--without-dlltool" );
3403 output_filenames( target_flags[arch] );
3404 if (make->is_win16) output_filename( "-m16" );
3405 output_filename( "--export" );
3406 output_filename( spec_file );
3407 output_filenames_obj_dir( make, make->implib_files[arch] );
3408 output( "\n" );
3410 if (!arch && is_native_arch_disabled( make )) return;
3412 add_install_rule( make, make->importlib, arch, name,
3413 strmake( "d%slib%s.a", arch_install_dirs[arch], make->importlib ));
3417 /*******************************************************************
3418 * output_unix_lib
3420 static void output_unix_lib( struct makefile *make )
3422 struct strarray unix_deps = empty_strarray;
3423 struct strarray unix_libs = add_unix_libraries( make, &unix_deps );
3424 unsigned int arch = 0; /* unix libs are always native */
3426 if (make->disabled[arch]) return;
3428 strarray_add( &make->all_targets[arch], make->unixlib );
3429 add_install_rule( make, make->module, arch, make->unixlib,
3430 strmake( "p%s%s", arch_install_dirs[arch], make->unixlib ));
3431 output( "%s:", obj_dir_path( make, make->unixlib ));
3432 output_filenames_obj_dir( make, make->unixobj_files );
3433 output_filenames( unix_deps );
3434 output( "\n" );
3435 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3436 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3437 output_filenames_obj_dir( make, make->unixobj_files );
3438 output_filenames( unix_libs );
3439 output_filename( "$(LDFLAGS)" );
3440 output( "\n" );
3444 /*******************************************************************
3445 * output_static_lib
3447 static void output_static_lib( struct makefile *make, unsigned int arch )
3449 const char *name = strmake( "%s%s", arch_dirs[arch], make->staticlib );
3451 strarray_add( &make->clean_files, name );
3452 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3453 output_filenames_obj_dir( make, make->object_files[arch] );
3454 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3455 output( "\n" );
3456 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3457 output_filenames( target_flags[arch] );
3458 output_filenames_obj_dir( make, make->object_files[arch] );
3459 if (!arch) output_filenames_obj_dir( make, make->unixobj_files );
3460 output( "\n" );
3461 if (!make->extlib)
3462 add_install_rule( make, make->staticlib, arch, name,
3463 strmake( "d%s%s", arch_install_dirs[arch], make->staticlib ));
3467 /*******************************************************************
3468 * output_shared_lib
3470 static void output_shared_lib( struct makefile *make )
3472 unsigned int i;
3473 char *basename, *p;
3474 struct strarray names = get_shared_lib_names( make->sharedlib );
3475 struct strarray all_libs = empty_strarray;
3476 struct strarray dep_libs = empty_strarray;
3477 unsigned int arch = 0; /* shared libs are always native */
3479 basename = xstrdup( make->sharedlib );
3480 if ((p = strchr( basename, '.' ))) *p = 0;
3482 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3483 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3484 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3485 strarray_addall( &all_libs, libs );
3487 output( "%s:", obj_dir_path( make, make->sharedlib ));
3488 output_filenames_obj_dir( make, make->object_files[arch] );
3489 output_filenames( dep_libs );
3490 output( "\n" );
3491 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3492 output_filenames_obj_dir( make, make->object_files[arch] );
3493 output_filenames( all_libs );
3494 output_filename( "$(LDFLAGS)" );
3495 output( "\n" );
3496 add_install_rule( make, make->sharedlib, arch, make->sharedlib,
3497 strmake( "p%s%s", arch_install_dirs[arch], make->sharedlib ));
3498 for (i = 1; i < names.count; i++)
3500 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3501 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3502 add_install_rule( make, names.str[i], arch, names.str[i-1],
3503 strmake( "y%s%s", arch_install_dirs[arch], names.str[i] ));
3505 strarray_addall( &make->all_targets[arch], names );
3509 /*******************************************************************
3510 * output_test_module
3512 static void output_test_module( struct makefile *make, unsigned int arch )
3514 char *basemodule = replace_extension( make->testdll, ".dll", "" );
3515 char *stripped = arch_module_name( strmake( "%s_test-stripped.exe", basemodule ), arch );
3516 char *testmodule = arch_module_name( strmake( "%s_test.exe", basemodule ), arch );
3517 struct strarray default_imports = get_default_imports( make, make->imports );
3518 struct strarray dep_libs = empty_strarray;
3519 struct strarray all_libs = empty_strarray;
3520 struct makefile *parent = get_parent_makefile( make );
3521 const char *debug_file;
3523 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, IMPORT_TYPE_DIRECT, arch ) );
3524 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, arch ) );
3526 strarray_add( &make->all_targets[arch], testmodule );
3527 strarray_add( &make->clean_files, stripped );
3528 output( "%s:\n", obj_dir_path( make, testmodule ));
3529 output_winegcc_command( make, arch );
3530 output_filenames( make->extradllflags );
3531 output_filenames_obj_dir( make, make->object_files[arch] );
3532 output_filenames_obj_dir( make, make->res_files[arch] );
3533 if ((debug_file = get_debug_file( make, testmodule, arch )))
3534 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3535 output_filenames( all_libs );
3536 output_filename( arch_make_variable( "LDFLAGS", arch ));
3537 output( "\n" );
3538 output( "%s:\n", obj_dir_path( make, stripped ));
3539 output_winegcc_command( make, arch );
3540 output_filename( "-s" );
3541 output_filename( strmake( "-Wb,-F,%s_test.exe", basemodule ));
3542 output_filenames( make->extradllflags );
3543 output_filenames_obj_dir( make, make->object_files[arch] );
3544 output_filenames_obj_dir( make, make->res_files[arch] );
3545 output_filenames( all_libs );
3546 output_filename( arch_make_variable( "LDFLAGS", arch ));
3547 output( "\n" );
3548 output( "%s %s:", obj_dir_path( make, testmodule ), obj_dir_path( make, stripped ));
3549 output_filenames_obj_dir( make, make->object_files[arch] );
3550 output_filenames_obj_dir( make, make->res_files[arch] );
3551 output_filenames( dep_libs );
3552 output_filename( tools_path( make, "winebuild" ));
3553 output_filename( tools_path( make, "winegcc" ));
3554 output( "\n" );
3556 output( "programs/winetest/%s%s_test.res: %s\n", arch_dirs[arch], basemodule,
3557 obj_dir_path( make, stripped ));
3558 output( "\t%secho \"%s_test.exe TESTRES \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3559 basemodule, obj_dir_path( make, stripped ), tools_path( make, "wrc" ));
3561 output_filenames_obj_dir( make, make->ok_files );
3562 output( ": %s", obj_dir_path( make, testmodule ));
3563 if (parent)
3565 char *parent_module = arch_module_name( make->testdll, arch );
3566 output_filename( obj_dir_path( parent, parent_module ));
3567 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3569 output( "\n" );
3570 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3571 if (!make->disabled[arch] && parent && !parent->disabled[arch])
3572 output_filenames_obj_dir( make, make->ok_files );
3573 output( "\n" );
3574 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3575 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3576 output( "%s::\n", obj_dir_path( make, "testclean" ));
3577 output( "\trm -f" );
3578 output_filenames_obj_dir( make, make->ok_files );
3579 output( "\n" );
3580 strarray_addall( &make->clean_files, make->ok_files );
3581 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3585 /*******************************************************************
3586 * output_programs
3588 static void output_programs( struct makefile *make )
3590 unsigned int i, j;
3591 unsigned int arch = 0; /* programs are always native */
3593 for (i = 0; i < make->programs.count; i++)
3595 char *program_installed = NULL;
3596 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3597 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3598 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3599 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3600 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3602 if (!objs.count) objs = make->object_files[arch];
3603 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3605 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3606 strarray_addall( &all_libs, libs );
3609 output( "%s:", obj_dir_path( make, program ) );
3610 output_filenames_obj_dir( make, objs );
3611 output_filenames( deps );
3612 output( "\n" );
3613 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3614 output_filenames_obj_dir( make, objs );
3615 output_filenames( all_libs );
3616 output_filename( "$(LDFLAGS)" );
3617 output( "\n" );
3618 strarray_add( &make->all_targets[arch], program );
3620 for (j = 0; j < symlinks.count; j++)
3622 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3623 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3625 strarray_addall( &make->all_targets[arch], symlinks );
3627 add_install_rule( make, program, arch, program_installed ? program_installed : program,
3628 strmake( "p$(bindir)/%s", program ));
3629 for (j = 0; j < symlinks.count; j++)
3630 add_install_rule( make, symlinks.str[j], arch, program,
3631 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3636 /*******************************************************************
3637 * output_subdirs
3639 static void output_subdirs( struct makefile *make )
3641 struct strarray all_targets = empty_strarray;
3642 struct strarray makefile_deps = empty_strarray;
3643 struct strarray clean_files = empty_strarray;
3644 struct strarray testclean_files = empty_strarray;
3645 struct strarray distclean_files = empty_strarray;
3646 struct strarray distclean_dirs = empty_strarray;
3647 struct strarray dependencies = empty_strarray;
3648 struct strarray install_deps[NB_INSTALL_RULES] = { empty_strarray };
3649 struct strarray tooldeps_deps = empty_strarray;
3650 struct strarray buildtest_deps = empty_strarray;
3651 unsigned int i, j, arch;
3653 strarray_addall( &clean_files, make->clean_files );
3654 strarray_addall( &distclean_files, make->distclean_files );
3655 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3656 for (i = 0; i < subdirs.count; i++)
3658 struct strarray subclean = empty_strarray;
3659 strarray_addall( &subclean, get_removable_dirs( submakes[i]->clean_files ));
3660 strarray_addall( &subclean, get_removable_dirs( submakes[i]->distclean_files ));
3661 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3662 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3663 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3664 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3665 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3666 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3667 strarray_addall_path( &distclean_dirs, submakes[i]->obj_dir, subclean );
3668 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3669 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3670 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3672 for (arch = 0; arch < archs.count; arch++)
3674 if (submakes[i]->disabled[arch]) continue;
3675 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets[arch] );
3677 if (submakes[i]->disabled[0]) continue;
3679 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3680 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3681 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3682 if (submakes[i]->testdll)
3683 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3684 for (j = 0; j < NB_INSTALL_RULES; j++)
3685 if (submakes[i]->install_rules[j].count)
3686 strarray_add( &install_deps[j], obj_dir_path( submakes[i], install_targets[j] ));
3688 strarray_addall( &dependencies, makefile_deps );
3689 output( "all:" );
3690 output_filenames( all_targets );
3691 output( "\n" );
3692 output( "Makefile:" );
3693 output_filenames( makefile_deps );
3694 output( "\n" );
3695 output_filenames( dependencies );
3696 output( ":\n" );
3697 for (j = 0; j < NB_INSTALL_RULES; j++)
3699 if (!install_deps[j].count) continue;
3700 if (strcmp( install_targets[j], "install-test" ))
3702 output( "install " );
3703 strarray_add_uniq( &make->phony_targets, "install" );
3705 output( "%s::", install_targets[j] );
3706 output_filenames( install_deps[j] );
3707 output( "\n" );
3708 strarray_add_uniq( &make->phony_targets, install_targets[j] );
3710 output_uninstall_rules( make );
3711 if (buildtest_deps.count)
3713 output( "buildtests:" );
3714 output_filenames( buildtest_deps );
3715 output( "\n" );
3716 strarray_add_uniq( &make->phony_targets, "buildtests" );
3718 output( "check test:" );
3719 output_filenames( testclean_files );
3720 output( "\n" );
3721 strarray_add_uniq( &make->phony_targets, "check" );
3722 strarray_add_uniq( &make->phony_targets, "test" );
3724 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3726 output( "clean::\n");
3727 output_rm_filenames( clean_files, "rm -f" );
3728 output( "testclean::\n");
3729 output_rm_filenames( testclean_files, "rm -f" );
3730 output( "distclean::\n");
3731 output_rm_filenames( distclean_files, "rm -f" );
3732 output_rm_filenames( distclean_dirs, "-rmdir 2>/dev/null" );
3733 output( "maintainer-clean::\n");
3734 output_rm_filenames( make->maintainerclean_files, "rm -f" );
3735 strarray_add_uniq( &make->phony_targets, "distclean" );
3736 strarray_add_uniq( &make->phony_targets, "testclean" );
3737 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3739 if (tooldeps_deps.count)
3741 output( "__tooldeps__:" );
3742 output_filenames( tooldeps_deps );
3743 output( "\n" );
3744 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3747 if (make->phony_targets.count)
3749 output( ".PHONY:" );
3750 output_filenames( make->phony_targets );
3751 output( "\n" );
3756 /*******************************************************************
3757 * output_sources
3759 static void output_sources( struct makefile *make )
3761 struct strarray all_targets = empty_strarray;
3762 struct incl_file *source;
3763 unsigned int i, j, arch;
3765 strarray_add_uniq( &make->phony_targets, "all" );
3767 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3769 char *obj = xstrdup( source->name );
3770 char *ext = get_extension( obj );
3772 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3773 *ext++ = 0;
3775 for (j = 0; output_source_funcs[j].ext; j++)
3776 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3778 output_source_funcs[j].fn( make, source, obj );
3779 strarray_addall_uniq( &make->dependencies, source->dependencies );
3782 /* special case for winetest: add resource files from other test dirs */
3783 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3785 for (arch = 0; arch < archs.count; arch++)
3787 if (!is_multiarch( arch )) continue;
3788 for (i = 0; i < subdirs.count; i++)
3790 if (!submakes[i]->testdll) continue;
3791 if (submakes[i]->disabled[arch]) continue;
3792 if (enable_tests.count && !strarray_exists( &enable_tests, submakes[i]->testdll )) continue;
3793 strarray_add( &make->res_files[arch],
3794 strmake( "%s%s", arch_dirs[arch],
3795 replace_extension( submakes[i]->testdll, ".dll", "_test.res" )));
3800 if (make->dlldata_files.count)
3802 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3803 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3804 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3805 output_filenames( make->dlldata_files );
3806 output( "\n" );
3809 if (make->staticlib)
3811 for (arch = 0; arch < archs.count; arch++)
3812 if (is_multiarch( arch ) || !make->extlib) output_static_lib( make, arch );
3814 else if (make->module)
3816 for (arch = 0; arch < archs.count; arch++) if (is_multiarch( arch )) output_module( make, arch );
3817 if (make->unixlib) output_unix_lib( make );
3818 if (make->importlib) for (arch = 0; arch < archs.count; arch++) output_import_lib( make, arch );
3819 if (make->is_exe && !make->is_win16 && *dll_ext && strendswith( make->module, ".exe" ))
3821 char *binary = replace_extension( make->module, ".exe", "" );
3822 add_install_rule( make, binary, 0, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3825 else if (make->testdll)
3827 for (arch = 0; arch < archs.count; arch++)
3828 if (is_multiarch( arch )) output_test_module( make, arch );
3830 else if (make->sharedlib) output_shared_lib( make );
3831 else if (make->programs.count) output_programs( make );
3833 for (i = 0; i < make->scripts.count; i++)
3834 add_install_rule( make, make->scripts.str[i], 0, make->scripts.str[i],
3835 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3837 for (i = 0; i < make->extra_targets.count; i++)
3838 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3839 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3840 else
3841 strarray_add( &make->all_targets[0], make->extra_targets.str[i] );
3843 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3844 strarray_add( &make->distclean_files, "Makefile" );
3845 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3847 if (!make->obj_dir)
3848 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3849 else if (!strcmp( make->obj_dir, "po" ))
3850 strarray_add( &make->distclean_files, "LINGUAS" );
3852 for (arch = 0; arch < archs.count; arch++)
3854 strarray_addall_uniq( &make->clean_files, make->object_files[arch] );
3855 strarray_addall_uniq( &make->clean_files, make->implib_files[arch] );
3856 strarray_addall_uniq( &make->clean_files, make->res_files[arch] );
3857 strarray_addall_uniq( &make->clean_files, make->all_targets[arch] );
3859 strarray_addall( &make->clean_files, make->unixobj_files );
3860 strarray_addall( &make->clean_files, make->pot_files );
3861 strarray_addall( &make->clean_files, make->debug_files );
3863 if (make == top_makefile)
3865 output_subdirs( make );
3866 return;
3869 for (arch = 0; arch < archs.count; arch++) strarray_addall( &all_targets, make->all_targets[arch] );
3870 strarray_addall( &all_targets, make->font_files );
3871 if (all_targets.count)
3873 output( "%s:", obj_dir_path( make, "all" ));
3874 output_filenames_obj_dir( make, all_targets );
3875 output( "\n" );
3876 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3878 for (i = 0; i < NB_INSTALL_RULES; i++) output_install_rules( make, i );
3880 if (make->clean_files.count)
3882 output( "%s::\n", obj_dir_path( make, "clean" ));
3883 output( "\trm -f" );
3884 output_filenames_obj_dir( make, make->clean_files );
3885 output( "\n" );
3886 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3891 /*******************************************************************
3892 * create_temp_file
3894 static FILE *create_temp_file( const char *orig )
3896 char *name = xmalloc( strlen(orig) + 13 );
3897 unsigned int i, id = getpid();
3898 int fd;
3899 FILE *ret = NULL;
3901 for (i = 0; i < 100; i++)
3903 sprintf( name, "%s.tmp%08x", orig, id );
3904 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3906 ret = fdopen( fd, "w" );
3907 break;
3909 if (errno != EEXIST) break;
3910 id += 7777;
3912 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3913 temp_file_name = name;
3914 return ret;
3918 /*******************************************************************
3919 * rename_temp_file
3921 static void rename_temp_file( const char *dest )
3923 int ret = rename( temp_file_name, dest );
3924 if (ret == -1 && errno == EEXIST)
3926 /* rename doesn't overwrite on windows */
3927 unlink( dest );
3928 ret = rename( temp_file_name, dest );
3930 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3931 temp_file_name = NULL;
3935 /*******************************************************************
3936 * are_files_identical
3938 static int are_files_identical( FILE *file1, FILE *file2 )
3940 for (;;)
3942 char buffer1[8192], buffer2[8192];
3943 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3944 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3945 if (size1 != size2) return 0;
3946 if (!size1) return feof( file1 ) && feof( file2 );
3947 if (memcmp( buffer1, buffer2, size1 )) return 0;
3952 /*******************************************************************
3953 * rename_temp_file_if_changed
3955 static void rename_temp_file_if_changed( const char *dest )
3957 FILE *file1, *file2;
3958 int do_rename = 1;
3960 if ((file1 = fopen( dest, "r" )))
3962 if ((file2 = fopen( temp_file_name, "r" )))
3964 do_rename = !are_files_identical( file1, file2 );
3965 fclose( file2 );
3967 fclose( file1 );
3969 if (!do_rename)
3971 unlink( temp_file_name );
3972 temp_file_name = NULL;
3974 else rename_temp_file( dest );
3978 /*******************************************************************
3979 * output_linguas
3981 static void output_linguas( const struct makefile *make )
3983 const char *dest = obj_dir_path( make, "LINGUAS" );
3984 struct incl_file *source;
3986 output_file = create_temp_file( dest );
3988 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3989 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3990 if (strendswith( source->name, ".po" ))
3991 output( "%s\n", replace_extension( source->name, ".po", "" ));
3993 if (fclose( output_file )) fatal_perror( "write" );
3994 output_file = NULL;
3995 rename_temp_file_if_changed( dest );
3999 /*******************************************************************
4000 * output_testlist
4002 static void output_testlist( const struct makefile *make )
4004 const char *dest = obj_dir_path( make, "testlist.c" );
4005 unsigned int i;
4007 output_file = create_temp_file( dest );
4009 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
4010 output( "#define WIN32_LEAN_AND_MEAN\n" );
4011 output( "#include <windows.h>\n\n" );
4012 output( "#define STANDALONE\n" );
4013 output( "#include \"wine/test.h\"\n\n" );
4015 for (i = 0; i < make->test_files.count; i++)
4016 output( "extern void func_%s(void);\n", make->test_files.str[i] );
4017 output( "\n" );
4018 output( "const struct test winetest_testlist[] =\n" );
4019 output( "{\n" );
4020 for (i = 0; i < make->test_files.count; i++)
4021 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
4022 output( " { 0, 0 }\n" );
4023 output( "};\n" );
4025 if (fclose( output_file )) fatal_perror( "write" );
4026 output_file = NULL;
4027 rename_temp_file_if_changed( dest );
4031 /*******************************************************************
4032 * output_gitignore
4034 static void output_gitignore( const char *dest, struct strarray files )
4036 unsigned int i;
4038 output_file = create_temp_file( dest );
4040 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4041 for (i = 0; i < files.count; i++)
4043 if (!strchr( files.str[i], '/' )) output( "/" );
4044 output( "%s\n", files.str[i] );
4047 if (fclose( output_file )) fatal_perror( "write" );
4048 output_file = NULL;
4049 rename_temp_file( dest );
4053 /*******************************************************************
4054 * output_stub_makefile
4056 static void output_stub_makefile( struct makefile *make )
4058 struct strarray targets = empty_strarray;
4059 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
4060 unsigned int i, arch;
4062 for (arch = 0; arch < archs.count; arch++)
4063 if (make->all_targets[arch].count) strarray_add_uniq( &targets, "all" );
4065 for (i = 0; i < NB_INSTALL_RULES; i++)
4067 if (!make->install_rules[i].count) continue;
4068 strarray_add_uniq( &targets, "install" );
4069 strarray_add( &targets, install_targets[i] );
4071 if (make->clean_files.count) strarray_add( &targets, "clean" );
4072 if (make->test_files.count)
4074 strarray_add( &targets, "check" );
4075 strarray_add( &targets, "test" );
4076 strarray_add( &targets, "testclean" );
4079 if (!targets.count && !make->clean_files.count) return;
4081 output_file_name = obj_dir_path( make, "Makefile" );
4082 output_file = create_temp_file( output_file_name );
4084 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
4086 if (make_var) output( "MAKE = %s\n\n", make_var );
4088 output( "all:\n" );
4089 output_filenames( targets );
4090 output_filenames( make->clean_files );
4091 output( ":\n" );
4092 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4093 output( ".PHONY:" );
4094 output_filenames( targets );
4095 output( "\n" );
4097 fclose( output_file );
4098 output_file = NULL;
4099 rename_temp_file( output_file_name );
4103 /*******************************************************************
4104 * output_silent_rules
4106 static void output_silent_rules(void)
4108 static const char *cmds[] =
4110 "BISON",
4111 "BUILD",
4112 "CC",
4113 "CCLD",
4114 "FLEX",
4115 "GEN",
4116 "LN",
4117 "MSG",
4118 "SED",
4119 "TEST",
4120 "WIDL",
4121 "WMC",
4122 "WRC"
4124 unsigned int i;
4126 output( "V = 0\n" );
4127 for (i = 0; i < ARRAY_SIZE(cmds); i++)
4129 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4130 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4131 output( "quiet_%s_1 =\n", cmds[i] );
4136 /*******************************************************************
4137 * output_top_makefile
4139 static void output_top_makefile( struct makefile *make )
4141 char buffer[1024];
4142 FILE *src_file;
4143 unsigned int i;
4144 int found = 0;
4146 output_file_name = obj_dir_path( make, output_makefile_name );
4147 output_file = create_temp_file( output_file_name );
4149 /* copy the contents of the source makefile */
4150 src_file = open_input_makefile( make );
4151 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4153 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4154 found = !strncmp( buffer, separator, strlen(separator) );
4156 if (fclose( src_file )) fatal_perror( "close" );
4157 input_file_name = NULL;
4159 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4161 if (silent_rules) output_silent_rules();
4162 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4163 output_sources( make );
4164 /* disable implicit rules */
4165 output( ".SUFFIXES:\n" );
4167 fclose( output_file );
4168 output_file = NULL;
4169 rename_temp_file( output_file_name );
4173 /*******************************************************************
4174 * output_dependencies
4176 static void output_dependencies( struct makefile *make )
4178 struct strarray ignore_files = empty_strarray;
4180 if (make->obj_dir) create_dir( make->obj_dir );
4182 if (make == top_makefile) output_top_makefile( make );
4183 else output_stub_makefile( make );
4185 strarray_addall( &ignore_files, make->distclean_files );
4186 strarray_addall( &ignore_files, make->clean_files );
4187 if (make->testdll) output_testlist( make );
4188 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4189 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4191 create_file_directories( make, ignore_files );
4193 output_file_name = NULL;
4197 /*******************************************************************
4198 * load_sources
4200 static void load_sources( struct makefile *make )
4202 static const char *source_vars[] =
4204 "SOURCES",
4205 "C_SRCS",
4206 "OBJC_SRCS",
4207 "RC_SRCS",
4208 "MC_SRCS",
4209 "IDL_SRCS",
4210 "BISON_SRCS",
4211 "LEX_SRCS",
4212 "HEADER_SRCS",
4213 "XTEMPLATE_SRCS",
4214 "SVG_SRCS",
4215 "FONT_SRCS",
4216 "IN_SRCS",
4217 "PO_SRCS",
4218 "MANPAGES",
4219 NULL
4221 const char **var;
4222 unsigned int i, arch;
4223 struct strarray value;
4224 struct incl_file *file;
4226 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4227 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4229 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4230 make->module = get_expanded_make_variable( make, "MODULE" );
4231 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4232 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4233 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4234 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4235 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4236 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4238 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4239 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4240 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4241 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4242 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4243 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4244 for (i = 0; i < NB_INSTALL_RULES; i++)
4245 make->install[i] = get_expanded_make_var_array( make, install_variables[i] );
4247 if (make->extlib) make->staticlib = make->extlib;
4248 if (make->staticlib) make->module = make->staticlib;
4250 if (make->obj_dir)
4252 make->disabled[0] = strarray_exists( &disabled_dirs[0], make->obj_dir );
4253 for (arch = 1; arch < archs.count; arch++)
4254 make->disabled[arch] = make->disabled[0] || strarray_exists( &disabled_dirs[arch], make->obj_dir );
4256 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4257 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4258 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4259 strarray_exists( &make->extradllflags, "-mwindows" );
4261 if (make->module)
4263 /* add default install rules if nothing was specified */
4264 for (i = 0; i < NB_INSTALL_RULES; i++) if (make->install[i].count) break;
4265 if (i == NB_INSTALL_RULES)
4267 if (make->importlib) strarray_add( &make->install[INSTALL_DEV], make->importlib );
4268 if (make->staticlib) strarray_add( &make->install[INSTALL_DEV], make->staticlib );
4269 else strarray_add( &make->install[INSTALL_LIB], make->module );
4273 make->include_paths = empty_strarray;
4274 make->include_args = empty_strarray;
4275 make->define_args = empty_strarray;
4276 make->unix_cflags = empty_strarray;
4277 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4279 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4280 for (i = 0; i < value.count; i++)
4282 if (!strncmp( value.str[i], "-I", 2 ))
4284 const char *dir = value.str[i] + 2;
4285 if (!strncmp( dir, "./", 2 ))
4287 dir += 2;
4288 while (*dir == '/') dir++;
4290 strarray_add_uniq( &make->include_paths, dir );
4292 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4293 strarray_add_uniq( &make->define_args, value.str[i] );
4295 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4296 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4298 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4299 if (make->src_dir)
4300 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4301 if (make->parent_dir)
4302 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4303 strarray_add( &make->include_args, "-Iinclude" );
4304 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4306 list_init( &make->sources );
4307 list_init( &make->includes );
4309 for (var = source_vars; *var; var++)
4311 value = get_expanded_make_var_array( make, *var );
4312 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4315 add_generated_sources( make );
4317 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4318 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4320 for (i = 0; i < make->delayimports.count; i++)
4321 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4325 /*******************************************************************
4326 * parse_makeflags
4328 static void parse_makeflags( const char *flags )
4330 const char *p = flags;
4331 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4333 while (*p)
4335 p = skip_spaces( p );
4336 var = buffer;
4337 while (*p && !isspace(*p))
4339 if (*p == '\\' && p[1]) p++;
4340 *var++ = *p++;
4342 *var = 0;
4343 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4348 /*******************************************************************
4349 * parse_option
4351 static int parse_option( const char *opt )
4353 if (opt[0] != '-')
4355 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4356 return 0;
4358 switch(opt[1])
4360 case 'f':
4361 if (opt[2]) output_makefile_name = opt + 2;
4362 break;
4363 case 'R':
4364 relative_dir_mode = 1;
4365 break;
4366 case 'S':
4367 silent_rules = 1;
4368 break;
4369 default:
4370 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4371 exit(1);
4373 return 1;
4377 /*******************************************************************
4378 * main
4380 int main( int argc, char *argv[] )
4382 const char *makeflags = getenv( "MAKEFLAGS" );
4383 const char *target;
4384 unsigned int i, j, arch;
4386 if (makeflags) parse_makeflags( makeflags );
4388 i = 1;
4389 while (i < argc)
4391 if (parse_option( argv[i] ))
4393 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4394 argc--;
4396 else i++;
4399 if (relative_dir_mode)
4401 char *relpath;
4403 if (argc != 3)
4405 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4406 exit( 1 );
4408 relpath = get_relative_path( argv[1], argv[2] );
4409 printf( "%s\n", relpath ? relpath : "." );
4410 exit( 0 );
4413 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4415 atexit( cleanup_files );
4416 init_signals( exit_on_signal );
4418 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4419 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4421 top_makefile = parse_makefile( NULL );
4423 target_flags[0] = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4424 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4425 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4426 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4427 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4428 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4429 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4430 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4431 for (i = 0; i < NB_INSTALL_RULES; i++)
4432 top_install[i] = get_expanded_make_var_array( top_makefile, strmake( "TOP_%s", install_variables[i] ));
4434 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4435 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4436 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4437 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4438 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4439 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4440 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4441 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4442 bison = get_expanded_make_variable( top_makefile, "BISON" );
4443 ar = get_expanded_make_variable( top_makefile, "AR" );
4444 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4445 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4446 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4447 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4448 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4449 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4451 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4452 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4453 if (!exe_ext) exe_ext = "";
4454 if (!tools_ext) tools_ext = "";
4456 strarray_add( &archs, get_expanded_make_variable( top_makefile, "HOST_ARCH" ));
4457 strarray_addall( &archs, get_expanded_make_var_array( top_makefile, "PE_ARCHS" ));
4459 arch_dirs[0] = "";
4460 arch_pe_dirs[0] = strmake( "%s-windows/", archs.str[0] );
4461 arch_install_dirs[0] = *dll_ext ? strmake( "$(dlldir)/%s-unix/", archs.str[0] ) : "$(dlldir)/";
4462 strip_progs[0] = "\"$(STRIP)\"";
4464 for (arch = 1; arch < archs.count; arch++)
4466 target = get_expanded_arch_var( top_makefile, "TARGET", arch );
4467 strarray_add( &target_flags[arch], "-b" );
4468 strarray_add( &target_flags[arch], target );
4469 arch_dirs[arch] = strmake( "%s-windows/", archs.str[arch] );
4470 arch_pe_dirs[arch] = arch_dirs[arch];
4471 arch_install_dirs[arch] = strmake( "$(dlldir)/%s", arch_dirs[arch] );
4472 strip_progs[arch] = strmake( "%s-strip", target );
4475 for (arch = 0; arch < archs.count; arch++)
4477 extra_cflags[arch] = get_expanded_arch_var_array( top_makefile, "EXTRACFLAGS", arch );
4478 extra_cflags_extlib[arch] = remove_warning_flags( extra_cflags[arch] );
4479 disabled_dirs[arch] = get_expanded_arch_var_array( top_makefile, "DISABLED_SUBDIRS", arch );
4480 if (!is_multiarch( arch )) continue;
4481 delay_load_flags[arch] = get_expanded_arch_var( top_makefile, "DELAYLOADFLAG", arch );
4482 debug_flags[arch] = get_expanded_arch_var( top_makefile, "DEBUG", arch );
4485 if (*dll_ext)
4487 delay_load_flags[0] = "-Wl,-delayload,";
4488 debug_flags[0] = NULL;
4491 top_makefile->src_dir = root_src_dir;
4492 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4493 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4495 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4497 load_sources( top_makefile );
4498 load_sources( include_makefile );
4499 for (i = 0; i < subdirs.count; i++)
4500 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4502 output_dependencies( top_makefile );
4503 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4505 return 0;