po: Update Dutch translation.
[wine.git] / tools / makedep.c
blobb84090fef21de75ebdb75de921be239cb04a4365
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 <signal.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
35 #include "tools.h"
36 #include "wine/list.h"
38 enum incl_type
40 INCL_NORMAL, /* #include "foo.h" */
41 INCL_SYSTEM, /* #include <foo.h> */
42 INCL_IMPORT, /* idl import "foo.idl" */
43 INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
44 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
45 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
48 struct dependency
50 int line; /* source line where this header is included */
51 enum incl_type type; /* type of include */
52 char *name; /* header name */
55 struct file
57 struct list entry;
58 char *name; /* full file name relative to cwd */
59 void *args; /* custom arguments for makefile rule */
60 unsigned int flags; /* flags (see below) */
61 unsigned int deps_count; /* files in use */
62 unsigned int deps_size; /* total allocated size */
63 struct dependency *deps; /* all header dependencies */
66 struct incl_file
68 struct list entry;
69 struct list hash_entry;
70 struct file *file;
71 char *name;
72 char *filename;
73 char *basename; /* base target name for generated files */
74 char *sourcename; /* source file name for generated headers */
75 struct incl_file *included_by; /* file that included this one */
76 int included_line; /* line where this file was included */
77 enum incl_type type; /* type of include */
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" },
117 { FLAG_IDL_HEADER, ".h" }
120 #define HASH_SIZE 197
122 static struct list files[HASH_SIZE];
123 static struct list global_includes[HASH_SIZE];
125 enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
127 /* variables common to all makefiles */
128 static struct strarray linguas;
129 static struct strarray dll_flags;
130 static struct strarray unix_dllflags;
131 static struct strarray target_flags;
132 static struct strarray msvcrt_flags;
133 static struct strarray extra_cflags;
134 static struct strarray extra_cross_cflags;
135 static struct strarray extra_cflags_extlib;
136 static struct strarray extra_cross_cflags_extlib;
137 static struct strarray cpp_flags;
138 static struct strarray lddll_flags;
139 static struct strarray libs;
140 static struct strarray enable_tests;
141 static struct strarray cmdline_vars;
142 static struct strarray subdirs;
143 static struct strarray disabled_dirs;
144 static struct strarray delay_import_libs;
145 static struct strarray top_install_lib;
146 static struct strarray top_install_dev;
147 static const char *root_src_dir;
148 static const char *tools_dir;
149 static const char *tools_ext;
150 static const char *exe_ext;
151 static const char *dll_ext;
152 static const char *host_cpu;
153 static const char *pe_dir;
154 static const char *so_dir;
155 static const char *crosstarget;
156 static const char *crossdebug;
157 static const char *fontforge;
158 static const char *convert;
159 static const char *flex;
160 static const char *bison;
161 static const char *ar;
162 static const char *ranlib;
163 static const char *rsvg;
164 static const char *icotool;
165 static const char *dlltool;
166 static const char *msgfmt;
167 static const char *ln_s;
168 static const char *sed_cmd;
169 static const char *delay_load_flag;
171 struct makefile
173 /* values determined from input makefile */
174 struct strarray vars;
175 struct strarray include_paths;
176 struct strarray include_args;
177 struct strarray define_args;
178 struct strarray unix_cflags;
179 struct strarray programs;
180 struct strarray scripts;
181 struct strarray imports;
182 struct strarray delayimports;
183 struct strarray extradllflags;
184 struct strarray install_lib;
185 struct strarray install_dev;
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 *staticimplib;
199 const char *importlib;
200 const char *unixlib;
201 int native_unix_lib;
202 int disabled;
203 int use_msvcrt;
204 int data_only;
205 int is_cross;
206 int is_win16;
207 int is_exe;
209 /* values generated at output time */
210 struct strarray in_files;
211 struct strarray ok_files;
212 struct strarray pot_files;
213 struct strarray test_files;
214 struct strarray clean_files;
215 struct strarray distclean_files;
216 struct strarray maintainerclean_files;
217 struct strarray uninstall_files;
218 struct strarray object_files;
219 struct strarray crossobj_files;
220 struct strarray unixobj_files;
221 struct strarray res_files;
222 struct strarray font_files;
223 struct strarray debug_files;
224 struct strarray dlldata_files;
225 struct strarray implib_files;
226 struct strarray crossimplib_files;
227 struct strarray all_targets;
228 struct strarray phony_targets;
229 struct strarray dependencies;
230 struct strarray install_rules[NB_INSTALL_RULES];
233 static struct makefile *top_makefile;
234 static struct makefile *include_makefile;
235 static struct makefile **submakes;
237 static const char separator[] = "### Dependencies";
238 static const char *output_makefile_name = "Makefile";
239 static const char *input_file_name;
240 static const char *output_file_name;
241 static const char *temp_file_name;
242 static int relative_dir_mode;
243 static int silent_rules;
244 static int input_line;
245 static int output_column;
246 static FILE *output_file;
248 static const char Usage[] =
249 "Usage: makedep [options] [directories]\n"
250 "Options:\n"
251 " -R from to Compute the relative path between two directories\n"
252 " -S Generate Automake-style silent rules\n"
253 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
256 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
257 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
258 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
259 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
261 /*******************************************************************
262 * fatal_error
264 static void fatal_error( const char *msg, ... )
266 va_list valist;
267 va_start( valist, msg );
268 if (input_file_name)
270 fprintf( stderr, "%s:", input_file_name );
271 if (input_line) fprintf( stderr, "%d:", input_line );
272 fprintf( stderr, " error: " );
274 else fprintf( stderr, "makedep: error: " );
275 vfprintf( stderr, msg, valist );
276 va_end( valist );
277 exit(1);
281 /*******************************************************************
282 * fatal_perror
284 static void fatal_perror( const char *msg, ... )
286 va_list valist;
287 va_start( valist, msg );
288 if (input_file_name)
290 fprintf( stderr, "%s:", input_file_name );
291 if (input_line) fprintf( stderr, "%d:", input_line );
292 fprintf( stderr, " error: " );
294 else fprintf( stderr, "makedep: error: " );
295 vfprintf( stderr, msg, valist );
296 perror( " " );
297 va_end( valist );
298 exit(1);
302 /*******************************************************************
303 * cleanup_files
305 static void cleanup_files(void)
307 if (temp_file_name) unlink( temp_file_name );
308 if (output_file_name) unlink( output_file_name );
312 /*******************************************************************
313 * exit_on_signal
315 static void exit_on_signal( int sig )
317 exit( 1 ); /* this will call the atexit functions */
321 /*******************************************************************
322 * output
324 static void output( const char *format, ... )
326 int ret;
327 va_list valist;
329 va_start( valist, format );
330 ret = vfprintf( output_file, format, valist );
331 va_end( valist );
332 if (ret < 0) fatal_perror( "output" );
333 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
334 else output_column += ret;
338 /*******************************************************************
339 * strarray_get_value
341 * Find a value in a name/value pair string array.
343 static const char *strarray_get_value( const struct strarray *array, const char *name )
345 int pos, res, min = 0, max = array->count / 2 - 1;
347 while (min <= max)
349 pos = (min + max) / 2;
350 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
351 if (res < 0) min = pos + 1;
352 else max = pos - 1;
354 return NULL;
358 /*******************************************************************
359 * strarray_set_value
361 * Define a value in a name/value pair string array.
363 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
365 int i, pos, res, min = 0, max = array->count / 2 - 1;
367 while (min <= max)
369 pos = (min + max) / 2;
370 if (!(res = strcmp( array->str[pos * 2], name )))
372 /* redefining a variable replaces the previous value */
373 array->str[pos * 2 + 1] = value;
374 return;
376 if (res < 0) min = pos + 1;
377 else max = pos - 1;
379 strarray_add( array, NULL );
380 strarray_add( array, NULL );
381 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
382 array->str[min * 2] = name;
383 array->str[min * 2 + 1] = value;
387 /*******************************************************************
388 * normalize_arch
390 static const char *normalize_arch( const char *arch )
392 unsigned int i, j;
394 static const char *map[][8] =
396 /* normalized aliases */
397 { "i386", "i486", "i586", "i686", "ia32" },
398 { "x86_64", "amd64", "x86-64", "x86_amd64", "x64" },
399 { "aarch64", "arm64" },
400 { "arm" },
403 for (i = 0; i < sizeof(map) / sizeof(map[0]); i++)
404 for (j = 0; map[i][j]; j++)
405 if (!strncmp( arch, map[i][j], strlen(map[i][j]) ))
406 return map[i][0];
407 return NULL;
411 /*******************************************************************
412 * output_filename
414 static void output_filename( const char *name )
416 if (output_column + strlen(name) + 1 > 100)
418 output( " \\\n" );
419 output( " " );
421 else if (output_column) output( " " );
422 output( "%s", name );
426 /*******************************************************************
427 * output_filenames
429 static void output_filenames( struct strarray array )
431 unsigned int i;
433 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
437 /*******************************************************************
438 * output_rm_filenames
440 static void output_rm_filenames( struct strarray array )
442 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
443 unsigned int i, len;
445 if (!array.count) return;
446 output( "\trm -f" );
447 for (i = len = 0; i < array.count; i++)
449 if (len > max_cmdline)
451 output( "\n" );
452 output( "\trm -f" );
453 len = 0;
455 output_filename( array.str[i] );
456 len += strlen( array.str[i] ) + 1;
458 output( "\n" );
462 /*******************************************************************
463 * get_extension
465 static char *get_extension( char *filename )
467 char *ext = strrchr( filename, '.' );
468 if (ext && strchr( ext, '/' )) ext = NULL;
469 return ext;
473 /*******************************************************************
474 * get_base_name
476 static const char *get_base_name( const char *name )
478 char *base;
479 if (!strchr( name, '.' )) return name;
480 base = xstrdup( name );
481 *strrchr( base, '.' ) = 0;
482 return base;
486 /*******************************************************************
487 * replace_filename
489 static char *replace_filename( const char *path, const char *name )
491 const char *p;
492 char *ret;
493 size_t len;
495 if (!path) return xstrdup( name );
496 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
497 len = p - path + 1;
498 ret = xmalloc( len + strlen( name ) + 1 );
499 memcpy( ret, path, len );
500 strcpy( ret + len, name );
501 return ret;
505 /*******************************************************************
506 * replace_substr
508 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
510 size_t pos = start - str;
511 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
512 memcpy( ret, str, pos );
513 strcpy( ret + pos, replace );
514 strcat( ret + pos, start + len );
515 return ret;
519 /*******************************************************************
520 * get_relative_path
522 * Determine where the destination path is located relative to the 'from' path.
524 static char *get_relative_path( const char *from, const char *dest )
526 const char *start;
527 char *ret, *p;
528 unsigned int dotdots = 0;
530 /* a path of "." is equivalent to an empty path */
531 if (!strcmp( from, "." )) from = "";
533 for (;;)
535 while (*from == '/') from++;
536 while (*dest == '/') dest++;
537 start = dest; /* save start of next path element */
538 if (!*from) break;
540 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
541 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
543 /* count remaining elements in 'from' */
546 dotdots++;
547 while (*from && *from != '/') from++;
548 while (*from == '/') from++;
550 while (*from);
551 break;
554 if (!start[0] && !dotdots) return NULL; /* empty path */
556 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
557 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
559 if (start[0]) strcpy( p, start );
560 else p[-1] = 0; /* remove trailing slash */
561 return ret;
565 /*******************************************************************
566 * concat_paths
568 static char *concat_paths( const char *base, const char *path )
570 int i, len;
571 char *ret;
573 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
574 if (!path || !path[0]) return xstrdup( base );
575 if (path[0] == '/') return xstrdup( path );
577 len = strlen( base );
578 while (len && base[len - 1] == '/') len--;
579 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
581 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
582 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
583 if (i != len - 1 || base[i] != '.')
585 path += 2;
586 while (*path == '/') path++;
588 /* else ignore "." element */
589 while (i > 0 && base[i - 1] == '/') i--;
590 len = i;
592 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
593 ret = xmalloc( len + strlen( path ) + 2 );
594 memcpy( ret, base, len );
595 ret[len++] = '/';
596 strcpy( ret + len, path );
597 return ret;
601 /*******************************************************************
602 * obj_dir_path
604 static char *obj_dir_path( const struct makefile *make, const char *path )
606 return concat_paths( make->obj_dir, path );
610 /*******************************************************************
611 * src_dir_path
613 static char *src_dir_path( const struct makefile *make, const char *path )
615 if (make->src_dir) return concat_paths( make->src_dir, path );
616 return obj_dir_path( make, path );
620 /*******************************************************************
621 * root_src_dir_path
623 static char *root_src_dir_path( const char *path )
625 return concat_paths( root_src_dir, path );
629 /*******************************************************************
630 * tools_dir_path
632 static char *tools_dir_path( const struct makefile *make, const char *path )
634 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
635 return strmake( "tools/%s", path );
639 /*******************************************************************
640 * tools_path
642 static char *tools_path( const struct makefile *make, const char *name )
644 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
648 /*******************************************************************
649 * strarray_addall_path
651 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
653 unsigned int i;
655 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
659 /*******************************************************************
660 * get_line
662 static char *get_line( FILE *file )
664 static char *buffer;
665 static size_t size;
667 if (!size)
669 size = 1024;
670 buffer = xmalloc( size );
672 if (!fgets( buffer, size, file )) return NULL;
673 input_line++;
675 for (;;)
677 char *p = buffer + strlen(buffer);
678 /* if line is larger than buffer, resize buffer */
679 while (p == buffer + size - 1 && p[-1] != '\n')
681 buffer = xrealloc( buffer, size * 2 );
682 if (!fgets( buffer + size - 1, size + 1, file )) break;
683 p = buffer + strlen(buffer);
684 size *= 2;
686 if (p > buffer && p[-1] == '\n')
688 *(--p) = 0;
689 if (p > buffer && p[-1] == '\r') *(--p) = 0;
690 if (p > buffer && p[-1] == '\\')
692 *(--p) = 0;
693 /* line ends in backslash, read continuation line */
694 if (!fgets( p, size - (p - buffer), file )) return buffer;
695 input_line++;
696 continue;
699 return buffer;
704 /*******************************************************************
705 * hash_filename
707 static unsigned int hash_filename( const char *name )
709 /* FNV-1 hash */
710 unsigned int ret = 2166136261u;
711 while (*name) ret = (ret * 16777619) ^ *name++;
712 return ret % HASH_SIZE;
716 /*******************************************************************
717 * add_file
719 static struct file *add_file( const char *name )
721 struct file *file = xmalloc( sizeof(*file) );
722 memset( file, 0, sizeof(*file) );
723 file->name = xstrdup( name );
724 return file;
728 /*******************************************************************
729 * add_dependency
731 static void add_dependency( struct file *file, const char *name, enum incl_type type )
733 if (file->deps_count >= file->deps_size)
735 file->deps_size *= 2;
736 if (file->deps_size < 16) file->deps_size = 16;
737 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
739 file->deps[file->deps_count].line = input_line;
740 file->deps[file->deps_count].type = type;
741 file->deps[file->deps_count].name = xstrdup( name );
742 file->deps_count++;
746 /*******************************************************************
747 * find_src_file
749 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
751 struct incl_file *file;
753 if (make == include_makefile)
755 unsigned int hash = hash_filename( name );
757 LIST_FOR_EACH_ENTRY( file, &global_includes[hash], struct incl_file, hash_entry )
758 if (!strcmp( name, file->name )) return file;
759 return NULL;
762 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
763 if (!strcmp( name, file->name )) return file;
764 return NULL;
767 /*******************************************************************
768 * find_include_file
770 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
772 struct incl_file *file;
774 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
776 const char *filename = file->filename;
777 if (!filename) continue;
778 if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
780 filename += strlen(make->obj_dir);
781 while (*filename == '/') filename++;
783 if (!strcmp( name, filename )) return file;
785 return NULL;
788 /*******************************************************************
789 * add_include
791 * Add an include file if it doesn't already exists.
793 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
794 const char *name, int line, enum incl_type type )
796 struct incl_file *include;
798 if (parent->files_count >= parent->files_size)
800 parent->files_size *= 2;
801 if (parent->files_size < 16) parent->files_size = 16;
802 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
805 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
806 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
807 goto found;
809 include = xmalloc( sizeof(*include) );
810 memset( include, 0, sizeof(*include) );
811 include->name = xstrdup(name);
812 include->included_by = parent;
813 include->included_line = line;
814 include->type = type;
815 include->use_msvcrt = parent->use_msvcrt;
816 list_add_tail( &make->includes, &include->entry );
817 found:
818 parent->files[parent->files_count++] = include;
819 return include;
823 /*******************************************************************
824 * add_generated_source
826 * Add a generated source file to the list.
828 static struct incl_file *add_generated_source( struct makefile *make,
829 const char *name, const char *filename )
831 struct incl_file *file = xmalloc( sizeof(*file) );
833 memset( file, 0, sizeof(*file) );
834 file->file = add_file( name );
835 file->name = xstrdup( name );
836 file->basename = xstrdup( filename ? filename : name );
837 file->filename = obj_dir_path( make, file->basename );
838 file->file->flags = FLAG_GENERATED;
839 file->use_msvcrt = make->use_msvcrt;
840 list_add_tail( &make->sources, &file->entry );
841 if (make == include_makefile)
843 unsigned int hash = hash_filename( name );
844 list_add_tail( &global_includes[hash], &file->hash_entry );
846 return file;
850 /*******************************************************************
851 * parse_include_directive
853 static void parse_include_directive( struct file *source, char *str )
855 char quote, *include, *p = str;
857 while (*p && isspace(*p)) p++;
858 if (*p != '\"' && *p != '<' ) return;
859 quote = *p++;
860 if (quote == '<') quote = '>';
861 include = p;
862 while (*p && (*p != quote)) p++;
863 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
864 *p = 0;
865 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
869 /*******************************************************************
870 * parse_pragma_directive
872 static void parse_pragma_directive( struct file *source, char *str )
874 char *flag, *p = str;
876 if (!isspace( *p )) return;
877 while (*p && isspace(*p)) p++;
878 p = strtok( p, " \t" );
879 if (strcmp( p, "makedep" )) return;
881 while ((flag = strtok( NULL, " \t" )))
883 if (!strcmp( flag, "depend" ))
885 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
886 return;
888 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
890 if (strendswith( source->name, ".idl" ))
892 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
893 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
894 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
895 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
896 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
897 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
898 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
899 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
901 else if (strendswith( source->name, ".rc" ))
903 if (!strcmp( flag, "header" )) source->flags |= FLAG_RC_HEADER;
904 else if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
906 else if (strendswith( source->name, ".sfd" ))
908 if (!strcmp( flag, "font" ))
910 struct strarray *array = source->args;
912 if (!array)
914 source->args = array = xmalloc( sizeof(*array) );
915 *array = empty_strarray;
916 source->flags |= FLAG_SFD_FONTS;
918 strarray_add( array, xstrdup( strtok( NULL, "" )));
919 return;
922 else
924 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
925 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
931 /*******************************************************************
932 * parse_cpp_directive
934 static void parse_cpp_directive( struct file *source, char *str )
936 while (*str && isspace(*str)) str++;
937 if (*str++ != '#') return;
938 while (*str && isspace(*str)) str++;
940 if (!strncmp( str, "include", 7 ))
941 parse_include_directive( source, str + 7 );
942 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
943 parse_include_directive( source, str + 6 );
944 else if (!strncmp( str, "pragma", 6 ))
945 parse_pragma_directive( source, str + 6 );
949 /*******************************************************************
950 * parse_idl_file
952 static void parse_idl_file( struct file *source, FILE *file )
954 char *buffer, *include;
956 input_line = 0;
958 while ((buffer = get_line( file )))
960 char quote;
961 char *p = buffer;
962 while (*p && isspace(*p)) p++;
964 if (!strncmp( p, "importlib", 9 ))
966 p += 9;
967 while (*p && isspace(*p)) p++;
968 if (*p++ != '(') continue;
969 while (*p && isspace(*p)) p++;
970 if (*p++ != '"') continue;
971 include = p;
972 while (*p && (*p != '"')) p++;
973 if (!*p) fatal_error( "malformed importlib directive\n" );
974 *p = 0;
975 add_dependency( source, include, INCL_IMPORTLIB );
976 continue;
979 if (!strncmp( p, "import", 6 ))
981 p += 6;
982 while (*p && isspace(*p)) p++;
983 if (*p != '"') continue;
984 include = ++p;
985 while (*p && (*p != '"')) p++;
986 if (!*p) fatal_error( "malformed import directive\n" );
987 *p = 0;
988 add_dependency( source, include, INCL_IMPORT );
989 continue;
992 /* check for #include inside cpp_quote */
993 if (!strncmp( p, "cpp_quote", 9 ))
995 p += 9;
996 while (*p && isspace(*p)) p++;
997 if (*p++ != '(') continue;
998 while (*p && isspace(*p)) p++;
999 if (*p++ != '"') continue;
1000 if (*p++ != '#') continue;
1001 while (*p && isspace(*p)) p++;
1002 if (strncmp( p, "include", 7 )) continue;
1003 p += 7;
1004 while (*p && isspace(*p)) p++;
1005 if (*p == '\\' && p[1] == '"')
1007 p += 2;
1008 quote = '"';
1010 else
1012 if (*p++ != '<' ) continue;
1013 quote = '>';
1015 include = p;
1016 while (*p && (*p != quote)) p++;
1017 if (!*p || (quote == '"' && p[-1] != '\\'))
1018 fatal_error( "malformed #include directive inside cpp_quote\n" );
1019 if (quote == '"') p--; /* remove backslash */
1020 *p = 0;
1021 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1022 continue;
1025 parse_cpp_directive( source, p );
1029 /*******************************************************************
1030 * parse_c_file
1032 static void parse_c_file( struct file *source, FILE *file )
1034 char *buffer;
1036 input_line = 0;
1037 while ((buffer = get_line( file )))
1039 parse_cpp_directive( source, buffer );
1044 /*******************************************************************
1045 * parse_rc_file
1047 static void parse_rc_file( struct file *source, FILE *file )
1049 char *buffer, *include;
1051 input_line = 0;
1052 while ((buffer = get_line( file )))
1054 char quote;
1055 char *p = buffer;
1056 while (*p && isspace(*p)) p++;
1058 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1060 p += 2;
1061 while (*p && isspace(*p)) p++;
1062 if (strncmp( p, "@makedep:", 9 )) continue;
1063 p += 9;
1064 while (*p && isspace(*p)) p++;
1065 quote = '"';
1066 if (*p == quote)
1068 include = ++p;
1069 while (*p && *p != quote) p++;
1071 else
1073 include = p;
1074 while (*p && !isspace(*p) && *p != '*') p++;
1076 if (!*p)
1077 fatal_error( "malformed makedep comment\n" );
1078 *p = 0;
1079 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1080 continue;
1083 parse_cpp_directive( source, buffer );
1088 /*******************************************************************
1089 * parse_in_file
1091 static void parse_in_file( struct file *source, FILE *file )
1093 char *p, *buffer;
1095 /* make sure it gets rebuilt when the version changes */
1096 add_dependency( source, "config.h", INCL_SYSTEM );
1098 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1100 input_line = 0;
1101 while ((buffer = get_line( file )))
1103 if (strncmp( buffer, ".TH", 3 )) continue;
1104 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1105 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1106 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1107 source->args = xstrdup( p );
1108 return;
1113 /*******************************************************************
1114 * parse_sfd_file
1116 static void parse_sfd_file( struct file *source, FILE *file )
1118 char *p, *eol, *buffer;
1120 input_line = 0;
1121 while ((buffer = get_line( file )))
1123 if (strncmp( buffer, "UComments:", 10 )) continue;
1124 p = buffer + 10;
1125 while (*p == ' ') p++;
1126 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1128 p++;
1129 buffer[strlen(buffer) - 1] = 0;
1131 while ((eol = strstr( p, "+AAoA" )))
1133 *eol = 0;
1134 while (*p && isspace(*p)) p++;
1135 if (*p++ == '#')
1137 while (*p && isspace(*p)) p++;
1138 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1140 p = eol + 5;
1142 while (*p && isspace(*p)) p++;
1143 if (*p++ != '#') return;
1144 while (*p && isspace(*p)) p++;
1145 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1146 return;
1151 static const struct
1153 const char *ext;
1154 void (*parse)( struct file *file, FILE *f );
1155 } parse_functions[] =
1157 { ".c", parse_c_file },
1158 { ".h", parse_c_file },
1159 { ".inl", parse_c_file },
1160 { ".l", parse_c_file },
1161 { ".m", parse_c_file },
1162 { ".rh", parse_c_file },
1163 { ".x", parse_c_file },
1164 { ".y", parse_c_file },
1165 { ".idl", parse_idl_file },
1166 { ".rc", parse_rc_file },
1167 { ".in", parse_in_file },
1168 { ".sfd", parse_sfd_file }
1171 /*******************************************************************
1172 * load_file
1174 static struct file *load_file( const char *name )
1176 struct file *file;
1177 FILE *f;
1178 unsigned int i, hash = hash_filename( name );
1180 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1181 if (!strcmp( name, file->name )) return file;
1183 if (!(f = fopen( name, "r" ))) return NULL;
1185 file = add_file( name );
1186 list_add_tail( &files[hash], &file->entry );
1187 input_file_name = file->name;
1188 input_line = 0;
1190 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1192 if (!strendswith( name, parse_functions[i].ext )) continue;
1193 parse_functions[i].parse( file, f );
1194 break;
1197 fclose( f );
1198 input_file_name = NULL;
1200 return file;
1204 /*******************************************************************
1205 * open_include_path_file
1207 * Open a file from a directory on the include path.
1209 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1210 const char *name, char **filename )
1212 char *src_path = concat_paths( dir, name );
1213 struct file *ret = load_file( src_path );
1215 if (ret) *filename = src_path;
1216 return ret;
1220 /*******************************************************************
1221 * open_file_same_dir
1223 * Open a file in the same directory as the parent.
1225 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1227 char *src_path = replace_filename( parent->file->name, name );
1228 struct file *ret = load_file( src_path );
1230 if (ret) *filename = replace_filename( parent->filename, name );
1231 return ret;
1235 /*******************************************************************
1236 * open_same_dir_generated_file
1238 * Open a generated_file in the same directory as the parent.
1240 static struct file *open_same_dir_generated_file( const struct makefile *make,
1241 const struct incl_file *parent, struct incl_file *file,
1242 const char *ext, const char *src_ext )
1244 char *filename;
1245 struct file *ret = NULL;
1247 if (strendswith( file->name, ext ) &&
1248 (ret = open_file_same_dir( parent, replace_extension( file->name, ext, src_ext ), &filename )))
1250 file->sourcename = filename;
1251 file->filename = obj_dir_path( make, replace_filename( parent->name, file->name ));
1253 return ret;
1257 /*******************************************************************
1258 * open_local_file
1260 * Open a file in the source directory of the makefile.
1262 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1264 char *src_path = src_dir_path( make, path );
1265 struct file *ret = load_file( src_path );
1267 /* if not found, try parent dir */
1268 if (!ret && make->parent_dir)
1270 free( src_path );
1271 path = strmake( "%s/%s", make->parent_dir, path );
1272 src_path = src_dir_path( make, path );
1273 ret = load_file( src_path );
1276 if (ret) *filename = src_path;
1277 return ret;
1281 /*******************************************************************
1282 * open_local_generated_file
1284 * Open a generated file in the directory of the makefile.
1286 static struct file *open_local_generated_file( const struct makefile *make, struct incl_file *file,
1287 const char *ext, const char *src_ext )
1289 struct incl_file *include;
1291 if (strendswith( file->name, ext ) &&
1292 (include = find_src_file( make, replace_extension( file->name, ext, src_ext ) )))
1294 file->sourcename = include->filename;
1295 file->filename = obj_dir_path( make, file->name );
1296 return include->file;
1298 return NULL;
1302 /*******************************************************************
1303 * open_global_file
1305 * Open a file in the top-level source directory.
1307 static struct file *open_global_file( const char *path, char **filename )
1309 char *src_path = root_src_dir_path( path );
1310 struct file *ret = load_file( src_path );
1312 if (ret) *filename = src_path;
1313 return ret;
1317 /*******************************************************************
1318 * open_global_header
1320 * Open a file in the global include source directory.
1322 static struct file *open_global_header( const char *path, char **filename )
1324 struct incl_file *include = find_src_file( include_makefile, path );
1326 if (!include) return NULL;
1327 *filename = include->filename;
1328 return include->file;
1332 /*******************************************************************
1333 * open_global_generated_file
1335 * Open a generated file in the top-level source directory.
1337 static struct file *open_global_generated_file( const struct makefile *make, struct incl_file *file,
1338 const char *ext, const char *src_ext )
1340 struct incl_file *include;
1342 if (strendswith( file->name, ext ) &&
1343 (include = find_src_file( include_makefile, replace_extension( file->name, ext, src_ext ) )))
1345 file->sourcename = include->filename;
1346 file->filename = strmake( "include/%s", file->name );
1347 return include->file;
1349 return NULL;
1353 /*******************************************************************
1354 * open_src_file
1356 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1358 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1360 if (!file) fatal_perror( "open %s", pFile->name );
1361 return file;
1365 /*******************************************************************
1366 * find_importlib_module
1368 static struct makefile *find_importlib_module( const char *name )
1370 unsigned int i, len;
1372 for (i = 0; i < subdirs.count; i++)
1374 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1375 len = strlen(submakes[i]->obj_dir);
1376 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1377 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1379 return NULL;
1383 /*******************************************************************
1384 * open_include_file
1386 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1388 struct file *file = NULL;
1389 unsigned int i, len;
1391 errno = ENOENT;
1393 /* check for generated files */
1394 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1395 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1396 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1397 if (convert && rsvg && icotool)
1399 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1400 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1401 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1404 /* check for extra targets */
1405 if (strarray_exists( &make->extra_targets, pFile->name ))
1407 pFile->sourcename = src_dir_path( make, pFile->name );
1408 pFile->filename = obj_dir_path( make, pFile->name );
1409 return NULL;
1412 /* now try in source dir */
1413 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1415 /* check for global importlib (module dependency) */
1416 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1418 pFile->filename = pFile->name;
1419 return NULL;
1422 /* check for generated files in global includes */
1423 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1424 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1425 if (strendswith( pFile->name, "tmpl.h" ) &&
1426 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1428 /* check in global includes source dir */
1429 if ((file = open_global_header( pFile->name, &pFile->filename ))) return file;
1431 /* check in global msvcrt includes */
1432 if (pFile->use_msvcrt &&
1433 (file = open_global_header( strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1434 return file;
1436 /* now search in include paths */
1437 for (i = 0; i < make->include_paths.count; i++)
1439 const char *dir = make->include_paths.str[i];
1441 if (root_src_dir)
1443 len = strlen( root_src_dir );
1444 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1446 while (dir[len] == '/') len++;
1447 file = open_global_file( concat_paths( dir + len, pFile->name ), &pFile->filename );
1450 else
1452 if (*dir == '/') continue;
1453 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1455 if (!file) continue;
1456 pFile->is_external = 1;
1457 return file;
1460 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1462 /* try in src file directory */
1463 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1464 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1465 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1467 pFile->is_external = pFile->included_by->is_external;
1468 return file;
1471 if (make->extlib) return NULL; /* ignore missing files in external libs */
1473 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1474 perror( pFile->name );
1475 pFile = pFile->included_by;
1476 while (pFile && pFile->included_by)
1478 const char *parent = pFile->included_by->sourcename;
1479 if (!parent) parent = pFile->included_by->file->name;
1480 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1481 parent, pFile->included_line, pFile->name );
1482 pFile = pFile->included_by;
1484 exit(1);
1488 /*******************************************************************
1489 * add_all_includes
1491 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1493 unsigned int i;
1495 for (i = 0; i < file->deps_count; i++)
1497 switch (file->deps[i].type)
1499 case INCL_NORMAL:
1500 case INCL_IMPORT:
1501 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1502 break;
1503 case INCL_IMPORTLIB:
1504 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1505 break;
1506 case INCL_SYSTEM:
1507 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1508 break;
1509 case INCL_CPP_QUOTE:
1510 case INCL_CPP_QUOTE_SYSTEM:
1511 break;
1517 /*******************************************************************
1518 * parse_file
1520 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1522 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1524 if (!file) return;
1526 source->file = file;
1527 source->files_count = 0;
1528 source->files_size = file->deps_count;
1529 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1531 if (strendswith( file->name, ".m" )) file->flags |= FLAG_C_UNIX;
1532 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1533 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1535 if (source->sourcename)
1537 if (strendswith( source->sourcename, ".idl" ))
1539 unsigned int i;
1541 /* generated .h file always includes these */
1542 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1543 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1544 for (i = 0; i < file->deps_count; i++)
1546 switch (file->deps[i].type)
1548 case INCL_IMPORT:
1549 if (strendswith( file->deps[i].name, ".idl" ))
1550 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1551 file->deps[i].line, INCL_NORMAL );
1552 else
1553 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1554 break;
1555 case INCL_CPP_QUOTE:
1556 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1557 break;
1558 case INCL_CPP_QUOTE_SYSTEM:
1559 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1560 break;
1561 case INCL_NORMAL:
1562 case INCL_SYSTEM:
1563 case INCL_IMPORTLIB:
1564 break;
1567 return;
1569 if (strendswith( source->sourcename, ".y" ))
1570 return; /* generated .tab.h doesn't include anything */
1573 add_all_includes( make, source, file );
1577 /*******************************************************************
1578 * add_src_file
1580 * Add a source file to the list.
1582 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1584 struct incl_file *file = xmalloc( sizeof(*file) );
1586 memset( file, 0, sizeof(*file) );
1587 file->name = xstrdup(name);
1588 file->use_msvcrt = make->use_msvcrt;
1589 file->is_external = !!make->extlib;
1590 list_add_tail( &make->sources, &file->entry );
1591 if (make == include_makefile)
1593 unsigned int hash = hash_filename( name );
1594 list_add_tail( &global_includes[hash], &file->hash_entry );
1596 parse_file( make, file, 1 );
1597 return file;
1601 /*******************************************************************
1602 * open_input_makefile
1604 static FILE *open_input_makefile( const struct makefile *make )
1606 FILE *ret;
1608 if (make->obj_dir)
1609 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1610 else
1611 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1613 input_line = 0;
1614 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1615 return ret;
1619 /*******************************************************************
1620 * get_make_variable
1622 static const char *get_make_variable( const struct makefile *make, const char *name )
1624 const char *ret;
1626 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1627 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1628 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1629 return NULL;
1633 /*******************************************************************
1634 * get_expanded_make_variable
1636 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1638 const char *var;
1639 char *p, *end, *expand, *tmp;
1641 var = get_make_variable( make, name );
1642 if (!var) return NULL;
1644 p = expand = xstrdup( var );
1645 while ((p = strchr( p, '$' )))
1647 if (p[1] == '(')
1649 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1650 *end++ = 0;
1651 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1652 var = get_make_variable( make, p + 2 );
1653 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1654 /* switch to the new string */
1655 p = tmp + (p - expand);
1656 free( expand );
1657 expand = tmp;
1659 else if (p[1] == '{') /* don't expand ${} variables */
1661 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1662 p = end + 1;
1664 else if (p[1] == '$')
1666 p += 2;
1668 else fatal_error( "syntax error in '%s'\n", expand );
1671 /* consider empty variables undefined */
1672 p = expand;
1673 while (*p && isspace(*p)) p++;
1674 if (*p) return expand;
1675 free( expand );
1676 return NULL;
1680 /*******************************************************************
1681 * get_expanded_make_var_array
1683 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1685 struct strarray ret = empty_strarray;
1686 char *value, *token;
1688 if ((value = get_expanded_make_variable( make, name )))
1689 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1690 strarray_add( &ret, token );
1691 return ret;
1695 /*******************************************************************
1696 * get_expanded_file_local_var
1698 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1699 const char *name )
1701 char *p, *var = strmake( "%s_%s", file, name );
1703 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1704 return get_expanded_make_var_array( make, var );
1708 /*******************************************************************
1709 * set_make_variable
1711 static int set_make_variable( struct strarray *array, const char *assignment )
1713 char *p, *name;
1715 p = name = xstrdup( assignment );
1716 while (isalnum(*p) || *p == '_') p++;
1717 if (name == p) return 0; /* not a variable */
1718 if (isspace(*p))
1720 *p++ = 0;
1721 while (isspace(*p)) p++;
1723 if (*p != '=') return 0; /* not an assignment */
1724 *p++ = 0;
1725 while (isspace(*p)) p++;
1727 strarray_set_value( array, name, p );
1728 return 1;
1732 /*******************************************************************
1733 * parse_makefile
1735 static struct makefile *parse_makefile( const char *path )
1737 char *buffer;
1738 FILE *file;
1739 struct makefile *make = xmalloc( sizeof(*make) );
1741 memset( make, 0, sizeof(*make) );
1742 make->obj_dir = path;
1743 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1744 if (path && !strcmp( path, "include" )) include_makefile = make;
1746 file = open_input_makefile( make );
1747 while ((buffer = get_line( file )))
1749 if (!strncmp( buffer, separator, strlen(separator) )) break;
1750 if (*buffer == '\t') continue; /* command */
1751 while (isspace( *buffer )) buffer++;
1752 if (*buffer == '#') continue; /* comment */
1753 set_make_variable( &make->vars, buffer );
1755 fclose( file );
1756 input_file_name = NULL;
1757 return make;
1761 /*******************************************************************
1762 * add_generated_sources
1764 static void add_generated_sources( struct makefile *make )
1766 unsigned int i;
1767 struct incl_file *source, *next, *file, *dlldata = NULL;
1768 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1770 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1772 if (source->file->flags & FLAG_IDL_CLIENT)
1774 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1775 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1776 add_all_includes( make, file, file->file );
1778 if (source->file->flags & FLAG_IDL_SERVER)
1780 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1781 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1782 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1783 add_all_includes( make, file, file->file );
1785 if (source->file->flags & FLAG_IDL_IDENT)
1787 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1788 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1789 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1790 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1791 add_all_includes( make, file, file->file );
1793 if (source->file->flags & FLAG_IDL_PROXY)
1795 if (!dlldata)
1797 dlldata = add_generated_source( make, "dlldata.o", "dlldata.c" );
1798 add_dependency( dlldata->file, "objbase.h", INCL_NORMAL );
1799 add_dependency( dlldata->file, "rpcproxy.h", INCL_NORMAL );
1800 add_all_includes( make, dlldata, dlldata->file );
1802 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1803 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1804 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1805 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1806 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1807 add_all_includes( make, file, file->file );
1809 if (source->file->flags & FLAG_IDL_TYPELIB)
1811 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL );
1813 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1815 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1817 if (source->file->flags & FLAG_IDL_REGISTER)
1819 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1821 if (source->file->flags & FLAG_IDL_HEADER)
1823 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1825 if (!source->file->flags && strendswith( source->name, ".idl" ))
1827 if (!strncmp( source->name, "wine/", 5 )) continue;
1828 source->file->flags = FLAG_IDL_HEADER | FLAG_INSTALL;
1829 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1831 if (strendswith( source->name, ".x" ))
1833 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1835 if (strendswith( source->name, ".y" ))
1837 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1838 /* steal the includes list from the source file */
1839 file->files_count = source->files_count;
1840 file->files_size = source->files_size;
1841 file->files = source->files;
1842 source->files_count = source->files_size = 0;
1843 source->files = NULL;
1845 if (strendswith( source->name, ".l" ))
1847 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1848 /* steal the includes list from the source file */
1849 file->files_count = source->files_count;
1850 file->files_size = source->files_size;
1851 file->files = source->files;
1852 source->files_count = source->files_size = 0;
1853 source->files = NULL;
1855 if (source->file->flags & FLAG_C_IMPLIB)
1857 if (!make->staticimplib && make->importlib && *dll_ext)
1858 make->staticimplib = strmake( "lib%s.a", make->importlib );
1860 if (strendswith( source->name, ".po" ))
1862 if (!make->disabled)
1863 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1865 if (strendswith( source->name, ".spec" ))
1867 char *obj = replace_extension( source->name, ".spec", "" );
1868 strarray_addall_uniq( &make->extra_imports,
1869 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1872 if (make->testdll)
1874 file = add_generated_source( make, "testlist.o", "testlist.c" );
1875 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1876 add_all_includes( make, file, file->file );
1878 for (i = 0; i < objs.count; i++)
1880 /* default to .c for unknown extra object files */
1881 if (strendswith( objs.str[i], ".o" ))
1883 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1884 file->file->flags |= FLAG_C_UNIX;
1885 file->use_msvcrt = 0;
1887 else if (strendswith( objs.str[i], ".res" ))
1888 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1889 else
1890 add_generated_source( make, objs.str[i], NULL );
1895 /*******************************************************************
1896 * create_dir
1898 static void create_dir( const char *dir )
1900 char *p, *path;
1902 p = path = xstrdup( dir );
1903 while ((p = strchr( p, '/' )))
1905 *p = 0;
1906 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1907 *p++ = '/';
1908 while (*p == '/') p++;
1910 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1911 free( path );
1915 /*******************************************************************
1916 * create_file_directories
1918 * Create the base directories of all the files.
1920 static void create_file_directories( const struct makefile *make, struct strarray files )
1922 struct strarray subdirs = empty_strarray;
1923 unsigned int i;
1924 char *dir;
1926 for (i = 0; i < files.count; i++)
1928 if (!strchr( files.str[i], '/' )) continue;
1929 dir = obj_dir_path( make, files.str[i] );
1930 *strrchr( dir, '/' ) = 0;
1931 strarray_add_uniq( &subdirs, dir );
1934 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1938 /*******************************************************************
1939 * output_filenames_obj_dir
1941 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1943 unsigned int i;
1945 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1949 /*******************************************************************
1950 * get_dependencies
1952 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1954 unsigned int i;
1956 if (!file->filename) return;
1958 if (file != source)
1960 if (file->owner == source) return; /* already processed */
1961 if (file->type == INCL_IMPORTLIB)
1963 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1964 return; /* library is imported only when building a typelib */
1965 strarray_add( &source->importlibdeps, file->filename );
1967 else strarray_add( &source->dependencies, file->filename );
1968 file->owner = source;
1970 /* sanity checks */
1971 if (!strcmp( file->filename, "include/config.h" ) &&
1972 file != source->files[0] && !source->is_external)
1974 input_file_name = source->filename;
1975 input_line = 0;
1976 for (i = 0; i < source->file->deps_count; i++)
1978 if (!strcmp( source->file->deps[i].name, file->name ))
1979 input_line = source->file->deps[i].line;
1981 fatal_error( "%s must be included before other headers\n", file->name );
1985 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
1989 /*******************************************************************
1990 * get_local_dependencies
1992 * Get the local dependencies of a given target.
1994 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1995 struct strarray targets )
1997 unsigned int i;
1998 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2000 for (i = 0; i < deps.count; i++)
2002 if (strarray_exists( &targets, deps.str[i] ))
2003 deps.str[i] = obj_dir_path( make, deps.str[i] );
2004 else
2005 deps.str[i] = src_dir_path( make, deps.str[i] );
2007 return deps;
2011 /*******************************************************************
2012 * get_static_lib
2014 * Check if makefile builds the named static library and return the full lib path.
2016 static const char *get_static_lib( const struct makefile *make, const char *name )
2018 if (!make->staticlib) return NULL;
2019 if (make->disabled) return NULL;
2020 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2021 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2022 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2023 return obj_dir_path( make, make->staticlib );
2027 /*******************************************************************
2028 * get_native_unix_lib
2030 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2032 if (!make->native_unix_lib) return NULL;
2033 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2034 if (make->unixlib[strlen(name)] != '.') return NULL;
2035 return obj_dir_path( make, make->unixlib );
2039 /*******************************************************************
2040 * get_parent_makefile
2042 static struct makefile *get_parent_makefile( struct makefile *make )
2044 char *dir, *p;
2045 unsigned int i;
2047 if (!make->obj_dir) return NULL;
2048 dir = xstrdup( make->obj_dir );
2049 if (!(p = strrchr( dir, '/' ))) return NULL;
2050 *p = 0;
2051 for (i = 0; i < subdirs.count; i++)
2052 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2053 return NULL;
2057 /*******************************************************************
2058 * needs_delay_lib
2060 static int needs_delay_lib( const struct makefile *make )
2062 if (delay_load_flag) return 0;
2063 if (*dll_ext && !crosstarget) return 0;
2064 if (!make->importlib) return 0;
2065 return strarray_exists( &delay_import_libs, make->importlib );
2069 /*******************************************************************
2070 * add_unix_libraries
2072 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2074 struct strarray ret = empty_strarray;
2075 struct strarray all_libs = empty_strarray;
2076 unsigned int i, j;
2078 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2079 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
2081 for (i = 0; i < all_libs.count; i++)
2083 const char *lib = NULL;
2085 if (!strncmp( all_libs.str[i], "-l", 2 ))
2087 for (j = 0; j < subdirs.count; j++)
2089 if (make == submakes[j]) continue;
2090 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2093 if (lib)
2095 strarray_add( deps, lib );
2096 strarray_add( &ret, lib );
2098 else strarray_add( &ret, all_libs.str[i] );
2101 strarray_addall( &ret, libs );
2102 return ret;
2106 /*******************************************************************
2107 * is_crt_module
2109 static int is_crt_module( const char *file )
2111 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2115 /*******************************************************************
2116 * get_default_crt
2118 static const char *get_default_crt( const struct makefile *make )
2120 if (!make->use_msvcrt) return NULL;
2121 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2122 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2126 /*******************************************************************
2127 * get_crt_define
2129 static const char *get_crt_define( const struct makefile *make )
2131 const char *crt_dll = NULL;
2132 unsigned int i, version = 0;
2134 for (i = 0; i < make->imports.count; i++)
2136 if (!is_crt_module( make->imports.str[i] )) continue;
2137 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2138 crt_dll, make->imports.str[i] );
2139 crt_dll = make->imports.str[i];
2142 if (!crt_dll)
2144 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2145 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2147 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2148 sscanf( crt_dll, "msvcr%u", &version );
2149 return strmake( "-D_MSVCR_VER=%u", version );
2153 /*******************************************************************
2154 * add_default_imports
2156 static struct strarray add_default_imports( const struct makefile *make, struct strarray imports )
2158 struct strarray ret = empty_strarray;
2159 const char *crt_dll = get_default_crt( make );
2160 unsigned int i;
2162 for (i = 0; i < imports.count; i++)
2164 if (is_crt_module( imports.str[i] )) crt_dll = imports.str[i];
2165 else strarray_add( &ret, imports.str[i] );
2168 strarray_add( &ret, "winecrt0" );
2169 if (crt_dll) strarray_add( &ret, crt_dll );
2171 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2172 strarray_add( &ret, "kernel" );
2174 strarray_add( &ret, "kernel32" );
2175 strarray_add( &ret, "ntdll" );
2176 return ret;
2180 /*******************************************************************
2181 * add_import_libs
2183 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2184 struct strarray imports, int delay, int is_cross )
2186 struct strarray ret = empty_strarray;
2187 unsigned int i, j;
2189 for (i = 0; i < imports.count; i++)
2191 const char *name = imports.str[i];
2192 const char *lib = NULL;
2194 if (name[0] == '-')
2196 switch (name[1])
2198 case 'L': strarray_add( &ret, name ); continue;
2199 case 'l': name += 2; break;
2200 default: continue;
2203 else name = get_base_name( name );
2205 for (j = 0; j < subdirs.count; j++)
2207 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2208 lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
2209 else
2210 lib = get_static_lib( submakes[j], name );
2211 if (lib) break;
2214 if (lib)
2216 const char *ext = NULL;
2218 if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
2219 else if (is_cross) ext = ".cross.a";
2220 if (ext) lib = replace_extension( lib, ".a", ext );
2221 strarray_add_uniq( deps, lib );
2222 strarray_add( &ret, lib );
2224 else strarray_add( &ret, strmake( "-l%s", name ));
2226 return ret;
2230 /*******************************************************************
2231 * add_install_rule
2233 static void add_install_rule( struct makefile *make, const char *target,
2234 const char *file, const char *dest )
2236 if (strarray_exists( &make->install_lib, target ) ||
2237 strarray_exists( &top_install_lib, make->obj_dir ) ||
2238 strarray_exists( &top_install_lib, obj_dir_path( make, target )))
2240 strarray_add( &make->install_rules[INSTALL_LIB], file );
2241 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2243 else if (strarray_exists( &make->install_dev, target ) ||
2244 strarray_exists( &top_install_dev, make->obj_dir ) ||
2245 strarray_exists( &top_install_dev, obj_dir_path( make, target )))
2247 strarray_add( &make->install_rules[INSTALL_DEV], file );
2248 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2253 /*******************************************************************
2254 * get_include_install_path
2256 * Determine the installation path for a given include file.
2258 static const char *get_include_install_path( const char *name )
2260 if (!strncmp( name, "wine/", 5 )) return name + 5;
2261 if (!strncmp( name, "msvcrt/", 7 )) return name;
2262 return strmake( "windows/%s", name );
2266 /*******************************************************************
2267 * get_shared_library_name
2269 * Determine possible names for a shared library with a version number.
2271 static struct strarray get_shared_lib_names( const char *libname )
2273 struct strarray ret = empty_strarray;
2274 const char *ext, *p;
2275 char *name, *first, *second;
2276 size_t len = 0;
2278 strarray_add( &ret, libname );
2280 for (p = libname; (p = strchr( p, '.' )); p++)
2281 if ((len = strspn( p + 1, "0123456789." ))) break;
2283 if (!len) return ret;
2284 ext = p + 1 + len;
2285 if (*ext && ext[-1] == '.') ext--;
2287 /* keep only the first group of digits */
2288 name = xstrdup( libname );
2289 first = name + (p - libname);
2290 if ((second = strchr( first + 1, '.' )))
2292 strcpy( second, ext );
2293 strarray_add( &ret, xstrdup( name ));
2295 return ret;
2299 /*******************************************************************
2300 * get_source_defines
2302 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2303 const char *obj )
2305 unsigned int i;
2306 struct strarray ret = empty_strarray;
2308 strarray_addall( &ret, make->include_args );
2309 if (source->use_msvcrt)
2311 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2312 for (i = 0; i < make->include_paths.count; i++)
2313 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2315 strarray_addall( &ret, make->define_args );
2316 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2317 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2318 return ret;
2322 /*******************************************************************
2323 * remove_warning_flags
2325 static struct strarray remove_warning_flags( struct strarray flags )
2327 unsigned int i;
2328 struct strarray ret = empty_strarray;
2330 for (i = 0; i < flags.count; i++)
2331 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2332 strarray_add( &ret, flags.str[i] );
2333 return ret;
2337 /*******************************************************************
2338 * get_debug_file
2340 static const char *get_debug_file( struct makefile *make, const char *name )
2342 const char *debug_file = NULL;
2343 if (!make->is_cross || !crossdebug) return NULL;
2344 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2345 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2346 if (debug_file) strarray_add( &make->debug_files, debug_file );
2347 return debug_file;
2351 /*******************************************************************
2352 * cmd_prefix
2354 static const char *cmd_prefix( const char *cmd )
2356 if (!silent_rules) return "";
2357 return strmake( "$(quiet_%s)", cmd );
2361 /*******************************************************************
2362 * output_winegcc_command
2364 static void output_winegcc_command( struct makefile *make, int is_cross )
2366 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2367 output_filename( "--wine-objdir ." );
2368 if (tools_dir)
2370 output_filename( "--winebuild" );
2371 output_filename( tools_path( make, "winebuild" ));
2373 if (is_cross)
2375 output_filename( "-b" );
2376 output_filename( crosstarget );
2377 output_filename( "--lib-suffix=.cross.a" );
2379 else
2381 output_filenames( target_flags );
2382 output_filenames( lddll_flags );
2387 /*******************************************************************
2388 * output_symlink_rule
2390 * Output a rule to create a symlink.
2392 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2394 const char *name = strrchr( link_name, '/' );
2395 char *dir = NULL;
2397 if (name)
2399 dir = xstrdup( link_name );
2400 dir[name - link_name] = 0;
2403 output( "\t%s", cmd_prefix( "LN" ));
2404 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2405 output( "rm -f %s && ", link_name );
2407 /* dest path with a directory needs special handling if ln -s isn't supported */
2408 if (dir && strcmp( ln_s, "ln -s" ))
2409 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2410 else
2411 output( "%s %s %s\n", ln_s, src_name, link_name );
2413 free( dir );
2417 /*******************************************************************
2418 * output_srcdir_symlink
2420 * Output rule to create a symlink back to the source directory, for source files
2421 * that are needed at run-time.
2423 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2425 char *src_file, *dst_file, *src_name;
2427 if (!make->src_dir) return;
2428 src_file = src_dir_path( make, obj );
2429 dst_file = obj_dir_path( make, obj );
2430 output( "%s: %s\n", dst_file, src_file );
2432 src_name = src_file;
2433 if (src_name[0] != '/' && make->obj_dir)
2434 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2436 output_symlink_rule( src_name, dst_file, 0 );
2437 strarray_add( &make->all_targets, obj );
2441 /*******************************************************************
2442 * output_install_commands
2444 static void output_install_commands( struct makefile *make, struct strarray files )
2446 unsigned int i;
2447 char *install_sh = root_src_dir_path( "tools/install-sh" );
2449 for (i = 0; i < files.count; i += 2)
2451 const char *file = files.str[i];
2452 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2454 switch (*files.str[i + 1])
2456 case 'c': /* cross-compiled program */
2457 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2458 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2459 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2460 break;
2461 case 'd': /* data file */
2462 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2463 install_sh, obj_dir_path( make, file ), dest );
2464 break;
2465 case 'D': /* data file in source dir */
2466 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2467 install_sh, src_dir_path( make, file ), dest );
2468 break;
2469 case 'p': /* program file */
2470 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2471 install_sh, obj_dir_path( make, file ), dest );
2472 break;
2473 case 's': /* script */
2474 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2475 install_sh, obj_dir_path( make, file ), dest );
2476 break;
2477 case 'S': /* script in source dir */
2478 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2479 install_sh, src_dir_path( make, file ), dest );
2480 break;
2481 case 't': /* script in tools dir */
2482 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2483 install_sh, tools_dir_path( make, files.str[i] ), dest );
2484 break;
2485 case 'y': /* symlink */
2486 output_symlink_rule( files.str[i], dest, 1 );
2487 break;
2488 default:
2489 assert(0);
2491 strarray_add( &make->uninstall_files, dest );
2496 /*******************************************************************
2497 * output_install_rules
2499 * Rules are stored as a (file,dest) pair of values.
2500 * The first char of dest indicates the type of install.
2502 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2504 unsigned int i;
2505 struct strarray files = make->install_rules[rules];
2506 struct strarray targets = empty_strarray;
2508 if (!files.count) return;
2510 for (i = 0; i < files.count; i += 2)
2512 const char *file = files.str[i];
2513 switch (*files.str[i + 1])
2515 case 'c': /* cross-compiled program */
2516 case 'd': /* data file */
2517 case 'p': /* program file */
2518 case 's': /* script */
2519 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2520 break;
2521 case 't': /* script in tools dir */
2522 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2523 break;
2527 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
2528 output_filenames( targets );
2529 output( "\n" );
2530 output_install_commands( make, files );
2531 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2532 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
2536 static int cmp_string_length( const char **a, const char **b )
2538 int paths_a = 0, paths_b = 0;
2539 const char *p;
2541 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2542 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2543 if (paths_b != paths_a) return paths_b - paths_a;
2544 return strcmp( *a, *b );
2547 /*******************************************************************
2548 * output_uninstall_rules
2550 static void output_uninstall_rules( struct makefile *make )
2552 static const char *dirs_order[] =
2553 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2555 struct strarray uninstall_dirs = empty_strarray;
2556 unsigned int i, j;
2558 if (!make->uninstall_files.count) return;
2559 output( "uninstall::\n" );
2560 output_rm_filenames( make->uninstall_files );
2561 strarray_add_uniq( &make->phony_targets, "uninstall" );
2563 if (!subdirs.count) return;
2564 for (i = 0; i < make->uninstall_files.count; i++)
2566 char *dir = xstrdup( make->uninstall_files.str[i] );
2567 while (strchr( dir, '/' ))
2569 *strrchr( dir, '/' ) = 0;
2570 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2573 strarray_qsort( &uninstall_dirs, cmp_string_length );
2574 output( "\t-rmdir" );
2575 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2577 for (j = 0; j < uninstall_dirs.count; j++)
2579 if (!uninstall_dirs.str[j]) continue;
2580 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2581 continue;
2582 output_filename( uninstall_dirs.str[j] );
2583 uninstall_dirs.str[j] = NULL;
2586 for (j = 0; j < uninstall_dirs.count; j++)
2587 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2588 output( "\n" );
2592 /*******************************************************************
2593 * output_po_files
2595 static void output_po_files( struct makefile *make )
2597 const char *po_dir = src_dir_path( make, "po" );
2598 unsigned int i;
2600 if (linguas.count)
2602 for (i = 0; i < linguas.count; i++)
2603 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2604 output( ": %s/wine.pot\n", po_dir );
2605 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2606 cmd_prefix( "MSG" ), po_dir );
2607 output( "po/all:" );
2608 for (i = 0; i < linguas.count; i++)
2609 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2610 output( "\n" );
2612 output( "%s/wine.pot:", po_dir );
2613 output_filenames( make->pot_files );
2614 output( "\n" );
2615 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2616 output_filenames( make->pot_files );
2617 output( "\n" );
2618 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2622 /*******************************************************************
2623 * output_source_y
2625 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2627 /* add source file dependency for parallel makes */
2628 char *header = strmake( "%s.tab.h", obj );
2630 if (find_include_file( make, header ))
2632 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2633 output( "\t%s%s -o %s.tab.c -d %s\n",
2634 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2635 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2636 source->filename, obj_dir_path( make, header ));
2637 strarray_add( &make->clean_files, header );
2639 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2641 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2645 /*******************************************************************
2646 * output_source_l
2648 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2650 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2651 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2655 /*******************************************************************
2656 * output_source_h
2658 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2660 if (source->file->flags & FLAG_GENERATED)
2661 strarray_add( &make->all_targets, source->name );
2662 else if ((source->file->flags & FLAG_INSTALL) || strncmp( source->name, "wine/", 5 ))
2663 add_install_rule( make, source->name, source->name,
2664 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2668 /*******************************************************************
2669 * output_source_rc
2671 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2673 struct strarray defines = get_source_defines( make, source, obj );
2674 const char *po_dir = NULL;
2675 unsigned int i;
2677 if (source->file->flags & FLAG_RC_HEADER) return;
2678 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2679 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2680 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2681 if (source->file->flags & FLAG_RC_PO)
2683 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2684 output( "%s.pot ", obj_dir_path( make, obj ) );
2686 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2687 output_filename( tools_path( make, "wrc" ));
2688 if (make->src_dir) output_filename( "nls/locale.nls" );
2689 output_filenames( source->dependencies );
2690 output( "\n" );
2691 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2692 if (make->is_win16) output_filename( "-m16" );
2693 output_filename( "--nostdinc" );
2694 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2695 output_filenames( defines );
2696 output_filename( source->filename );
2697 output( "\n" );
2698 if (po_dir)
2700 output( "%s.res:", obj_dir_path( make, obj ));
2701 for (i = 0; i < linguas.count; i++)
2702 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2703 output( "\n" );
2708 /*******************************************************************
2709 * output_source_mc
2711 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2713 unsigned int i;
2714 char *obj_path = obj_dir_path( make, obj );
2716 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2717 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2718 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2719 output_filename( tools_path( make, "wmc" ));
2720 output_filenames( source->dependencies );
2721 output( "\n" );
2722 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2723 if (linguas.count)
2725 output_filename( "--po-dir=po" );
2726 output( "\n" );
2727 output( "%s.res:", obj_dir_path( make, obj ));
2728 for (i = 0; i < linguas.count; i++)
2729 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2731 output( "\n" );
2735 /*******************************************************************
2736 * output_source_res
2738 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2740 strarray_add( &make->res_files, source->name );
2744 /*******************************************************************
2745 * output_source_idl
2747 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2749 struct strarray defines = get_source_defines( make, source, obj );
2750 struct strarray targets = empty_strarray;
2751 char *dest;
2752 unsigned int i;
2754 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2755 if (!source->file->flags) return;
2757 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2759 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2760 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2761 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2762 strarray_add( &targets, dest );
2764 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2765 if (source->file->flags & FLAG_INSTALL)
2767 add_install_rule( make, source->name, xstrdup( source->name ),
2768 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2769 if (source->file->flags & FLAG_IDL_HEADER)
2770 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2771 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2773 if (!targets.count) return;
2775 output_filenames_obj_dir( make, targets );
2776 output( ": %s\n", tools_path( make, "widl" ));
2777 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2778 output_filenames( target_flags );
2779 output_filename( "--nostdinc" );
2780 output_filename( "-Ldlls/\\*" );
2781 output_filenames( defines );
2782 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2783 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2784 output_filename( source->filename );
2785 output( "\n" );
2786 output_filenames_obj_dir( make, targets );
2787 output( ": %s", source->filename );
2788 output_filenames( source->dependencies );
2789 for (i = 0; i < source->importlibdeps.count; i++)
2791 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2792 output_filename( obj_dir_path( submake, submake->module ));
2794 output( "\n" );
2798 /*******************************************************************
2799 * output_source_x
2801 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2803 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2804 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2805 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2806 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2807 if (source->file->flags & FLAG_INSTALL)
2809 add_install_rule( make, source->name, source->name,
2810 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2811 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2812 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2817 /*******************************************************************
2818 * output_source_sfd
2820 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2822 unsigned int i;
2823 char *ttf_obj = strmake( "%s.ttf", obj );
2824 char *ttf_file = src_dir_path( make, ttf_obj );
2826 if (fontforge && !make->src_dir)
2828 output( "%s: %s\n", ttf_file, source->filename );
2829 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2830 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2831 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2832 strarray_add( &make->maintainerclean_files, ttf_obj );
2834 if (source->file->flags & FLAG_INSTALL)
2836 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2837 output_srcdir_symlink( make, ttf_obj );
2840 if (source->file->flags & FLAG_SFD_FONTS)
2842 struct strarray *array = source->file->args;
2844 for (i = 0; i < array->count; i++)
2846 char *font = strtok( xstrdup(array->str[i]), " \t" );
2847 char *args = strtok( NULL, "" );
2849 strarray_add( &make->all_targets, xstrdup( font ));
2850 output( "%s: %s %s\n", obj_dir_path( make, font ),
2851 tools_path( make, "sfnt2fon" ), ttf_file );
2852 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2853 tools_path( make, "sfnt2fon" ), ttf_file, args );
2854 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2860 /*******************************************************************
2861 * output_source_svg
2863 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2865 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2866 unsigned int i;
2868 if (convert && rsvg && icotool)
2870 for (i = 0; images[i]; i++)
2871 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2873 if (images[i])
2875 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2876 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2877 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2878 root_src_dir_path( "tools/buildimage" ), source->filename );
2879 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2885 /*******************************************************************
2886 * output_source_nls
2888 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2890 add_install_rule( make, source->name, source->name,
2891 strmake( "D$(nlsdir)/%s", source->name ));
2892 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2896 /*******************************************************************
2897 * output_source_desktop
2899 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2901 add_install_rule( make, source->name, source->name,
2902 strmake( "D$(datadir)/applications/%s", source->name ));
2906 /*******************************************************************
2907 * output_source_po
2909 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2911 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2912 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
2913 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2917 /*******************************************************************
2918 * output_source_in
2920 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2922 unsigned int i;
2924 if (make == include_makefile) return; /* ignore generated includes */
2925 if (strendswith( obj, ".man" ) && source->file->args)
2927 struct strarray symlinks;
2928 char *dir, *dest = replace_extension( obj, ".man", "" );
2929 char *lang = strchr( dest, '.' );
2930 char *section = source->file->args;
2931 if (lang)
2933 *lang++ = 0;
2934 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2936 else dir = strmake( "$(mandir)/man%s", section );
2937 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2938 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2939 for (i = 0; i < symlinks.count; i++)
2940 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2941 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2942 free( dest );
2943 free( dir );
2945 strarray_add( &make->in_files, xstrdup(obj) );
2946 strarray_add( &make->all_targets, xstrdup(obj) );
2947 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2948 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
2949 output( "%s:", obj_dir_path( make, obj ));
2950 output_filenames( source->dependencies );
2951 output( "\n" );
2952 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2956 /*******************************************************************
2957 * output_source_spec
2959 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2961 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2962 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
2963 struct strarray all_libs, dep_libs = empty_strarray;
2964 char *dll_name, *obj_name, *output_file;
2965 const char *debug_file;
2967 if (!imports.count) imports = make->imports;
2968 if (!dll_flags.count) dll_flags = make->extradllflags;
2969 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) imports = add_default_imports( make, imports );
2971 all_libs = add_import_libs( make, &dep_libs, imports, 0, make->is_cross );
2972 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
2973 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
2974 output_file = obj_dir_path( make, dll_name );
2976 strarray_add( &make->clean_files, dll_name );
2977 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2978 output( "%s.res:", obj_dir_path( make, obj ));
2979 output_filename( obj_dir_path( make, dll_name ));
2980 output_filename( tools_path( make, "wrc" ));
2981 output( "\n" );
2982 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
2983 tools_path( make, "wrc" ));
2985 output( "%s:", output_file);
2986 output_filename( source->filename );
2987 output_filename( obj_name );
2988 output_filenames( dep_libs );
2989 output_filename( tools_path( make, "winebuild" ));
2990 output_filename( tools_path( make, "winegcc" ));
2991 output( "\n" );
2992 output_winegcc_command( make, make->is_cross );
2993 output_filename( "-s" );
2994 output_filenames( dll_flags );
2995 output_filename( "-shared" );
2996 output_filename( source->filename );
2997 output_filename( obj_name );
2998 if ((debug_file = get_debug_file( make, dll_name )))
2999 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3000 output_filenames( all_libs );
3001 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3002 output( "\n" );
3006 /*******************************************************************
3007 * output_source_default
3009 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3011 struct strarray defines = get_source_defines( make, source, obj );
3012 int is_dll_src = (make->testdll &&
3013 strendswith( source->name, ".c" ) &&
3014 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3015 int need_cross = (crosstarget &&
3016 !(source->file->flags & FLAG_C_UNIX) &&
3017 (make->is_cross || make->staticlib ||
3018 (source->file->flags & FLAG_C_IMPLIB)));
3019 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
3020 (!need_cross ||
3021 (source->file->flags & FLAG_C_IMPLIB) ||
3022 (make->staticlib && !make->extlib)));
3024 if ((source->file->flags & FLAG_GENERATED) &&
3025 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3026 strarray_add( &make->clean_files, source->basename );
3028 if (need_obj)
3030 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3031 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3032 else if (source->file->flags & FLAG_C_IMPLIB)
3033 strarray_add( &make->implib_files, strmake( "%s.o", obj ));
3034 else if (!is_dll_src)
3035 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3036 else
3037 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3038 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3039 output( "\t%s$(CC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3040 output_filenames( defines );
3041 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3042 output_filenames( make->extlib ? extra_cflags_extlib : extra_cflags );
3043 if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
3045 output_filenames( unix_dllflags );
3047 else if (make->module || make->testdll)
3049 output_filenames( dll_flags );
3050 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3051 if (!*dll_ext && make->module && is_crt_module( make->module ))
3052 output_filename( "-fno-builtin" );
3054 output_filenames( cpp_flags );
3055 output_filename( "$(CFLAGS)" );
3056 output( "\n" );
3058 if (need_cross)
3060 if (source->file->flags & FLAG_C_IMPLIB)
3061 strarray_add( &make->crossimplib_files, strmake( "%s.cross.o", obj ));
3062 else if (!is_dll_src)
3063 strarray_add( &make->crossobj_files, strmake( "%s.cross.o", obj ));
3064 else
3065 strarray_add( &make->clean_files, strmake( "%s.cross.o", obj ));
3066 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3067 output( "\t%s$(CROSSCC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3068 output_filenames( defines );
3069 output_filenames( make->extlib ? extra_cross_cflags_extlib : extra_cross_cflags );
3070 if (make->module && is_crt_module( make->module ))
3071 output_filename( "-fno-builtin" );
3072 /* force -Wformat when using 'long' types, until all modules have been converted
3073 * and we can remove -Wno-format */
3074 if (!make->extlib && strarray_exists( &extra_cross_cflags, "-Wno-format" ) &&
3075 !strarray_exists( &defines, "-DWINE_NO_LONG_TYPES" ))
3076 output_filename( "-Wformat" );
3077 output_filenames( cpp_flags );
3078 output_filename( "$(CROSSCFLAGS)" );
3079 output( "\n" );
3081 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3082 !(source->file->flags & FLAG_GENERATED))
3084 strarray_add( &make->test_files, obj );
3085 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3086 output( "%s.ok:\n", obj_dir_path( make, obj ));
3087 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3088 cmd_prefix( "TEST" ),
3089 root_src_dir_path( "tools/runtest" ), make->testdll,
3090 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3091 make->is_cross ? "" : dll_ext, obj );
3093 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3094 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3095 output( ":" );
3096 output_filenames( source->dependencies );
3097 output( "\n" );
3101 /* dispatch table to output rules for a single source file */
3102 static const struct
3104 const char *ext;
3105 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3106 } output_source_funcs[] =
3108 { "y", output_source_y },
3109 { "l", output_source_l },
3110 { "h", output_source_h },
3111 { "rh", output_source_h },
3112 { "inl", output_source_h },
3113 { "rc", output_source_rc },
3114 { "mc", output_source_mc },
3115 { "res", output_source_res },
3116 { "idl", output_source_idl },
3117 { "sfd", output_source_sfd },
3118 { "svg", output_source_svg },
3119 { "nls", output_source_nls },
3120 { "desktop", output_source_desktop },
3121 { "po", output_source_po },
3122 { "in", output_source_in },
3123 { "x", output_source_x },
3124 { "spec", output_source_spec },
3125 { NULL, output_source_default }
3129 /*******************************************************************
3130 * get_unix_lib_name
3132 static char *get_unix_lib_name( struct makefile *make )
3134 struct incl_file *source;
3136 if (!*dll_ext) return NULL;
3137 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3139 if (!(source->file->flags & FLAG_C_UNIX)) continue;
3140 return strmake( "%s%s", get_base_name( make->module ), dll_ext );
3142 return NULL;
3146 /*******************************************************************
3147 * output_module
3149 static void output_module( struct makefile *make )
3151 struct strarray all_libs = empty_strarray;
3152 struct strarray dep_libs = empty_strarray;
3153 struct strarray imports = make->imports;
3154 const char *module_name = make->module;
3155 const char *debug_file;
3156 const char *delay_load = delay_load_flag;
3157 char *spec_file = NULL;
3158 unsigned int i;
3160 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3162 if (!make->data_only)
3164 if (*dll_ext && !make->is_cross && !make->data_only)
3165 module_name = strmake( "%s%s", make->module, dll_ext );
3167 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
3168 imports = add_default_imports( make, imports );
3170 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, make->is_cross ));
3171 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, 0, make->is_cross ));
3173 if (!make->use_msvcrt)
3175 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3176 strarray_addall( &all_libs, libs );
3179 if (!make->is_cross && *dll_ext) delay_load = "-Wl,-delayload,";
3180 if (delay_load)
3182 for (i = 0; i < make->delayimports.count; i++)
3183 strarray_add( &all_libs, strmake( "%s%s%s", delay_load, make->delayimports.str[i],
3184 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3187 strarray_add( &make->all_targets, module_name );
3189 if (make->data_only)
3190 add_install_rule( make, make->module, module_name,
3191 strmake( "d%s/%s", *dll_ext ? pe_dir : "$(dlldir)", module_name ));
3192 else if (make->is_cross)
3193 add_install_rule( make, make->module, module_name, strmake( "c%s/%s", pe_dir, module_name ));
3194 else if (*dll_ext)
3195 add_install_rule( make, make->module, module_name, strmake( "p%s/%s", so_dir, module_name ));
3196 else
3197 add_install_rule( make, make->module, module_name,
3198 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", module_name ));
3200 output( "%s:", obj_dir_path( make, module_name ));
3201 if (spec_file) output_filename( spec_file );
3202 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3203 output_filenames_obj_dir( make, make->res_files );
3204 output_filenames( dep_libs );
3205 output_filename( tools_path( make, "winebuild" ));
3206 output_filename( tools_path( make, "winegcc" ));
3207 output( "\n" );
3208 output_winegcc_command( make, make->is_cross );
3209 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3210 if (spec_file)
3212 output_filename( "-shared" );
3213 output_filename( spec_file );
3215 output_filenames( make->extradllflags );
3216 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3217 output_filenames_obj_dir( make, make->res_files );
3218 debug_file = get_debug_file( make, make->module );
3219 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3220 output_filenames( all_libs );
3221 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3222 output( "\n" );
3224 if (*dll_ext && make->is_exe && !make->is_win16 && strendswith( make->module, ".exe" ))
3226 char *binary = replace_extension( make->module, ".exe", "" );
3227 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3232 /*******************************************************************
3233 * output_fake_module
3235 static void output_fake_module( struct makefile *make )
3237 char *spec_file = NULL;
3239 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3241 strarray_add( &make->all_targets, make->module );
3242 add_install_rule( make, make->module, make->module, strmake( "d%s/%s", pe_dir, make->module ));
3244 output( "%s:", obj_dir_path( make, make->module ));
3245 if (spec_file) output_filename( spec_file );
3246 output_filenames_obj_dir( make, make->res_files );
3247 output_filename( tools_path( make, "winebuild" ));
3248 output_filename( tools_path( make, "winegcc" ));
3249 output( "\n" );
3250 output_winegcc_command( make, make->is_cross );
3251 output_filename( "-Wb,--fake-module" );
3252 if (spec_file)
3254 output_filename( "-shared" );
3255 output_filename( spec_file );
3257 output_filenames( make->extradllflags );
3258 output_filenames_obj_dir( make, make->res_files );
3259 output( "\n" );
3263 /*******************************************************************
3264 * output_import_lib
3266 static void output_import_lib( struct makefile *make )
3268 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3269 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3271 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3272 if (!*dll_ext && needs_delay_lib( make ))
3274 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3275 output( "%s.delay.a ", importlib_path );
3277 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3278 output_filenames_obj_dir( make, make->implib_files );
3279 output( "\n" );
3280 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3281 output_filenames( target_flags );
3282 if (make->is_win16) output_filename( "-m16" );
3283 output_filename( "--export" );
3284 output_filename( spec_file );
3285 output_filenames_obj_dir( make, make->implib_files );
3286 output( "\n" );
3287 add_install_rule( make, make->importlib,
3288 strmake( "lib%s.a", make->importlib ),
3289 strmake( "d%s/lib%s.a", so_dir, make->importlib ));
3291 if (crosstarget)
3293 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3294 output_filename( strmake( "%s.cross.a", importlib_path ));
3295 if (needs_delay_lib( make ))
3297 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3298 output_filename( strmake( "%s.delay.a", importlib_path ));
3300 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3301 output_filenames_obj_dir( make, make->crossimplib_files );
3302 output( "\n" );
3303 output( "\t%s%s -b %s -w --implib -o $@", cmd_prefix( "BUILD" ),
3304 tools_path( make, "winebuild" ), crosstarget );
3305 if (make->is_win16) output_filename( "-m16" );
3306 output_filename( "--export" );
3307 output_filename( spec_file );
3308 output_filenames_obj_dir( make, make->crossimplib_files );
3309 output( "\n" );
3310 add_install_rule( make, make->importlib,
3311 strmake( "lib%s.cross.a", make->importlib ),
3312 strmake( "d%s/lib%s.a", pe_dir, make->importlib ));
3317 /*******************************************************************
3318 * output_unix_lib
3320 static void output_unix_lib( struct makefile *make )
3322 struct strarray unix_libs = empty_strarray;
3323 struct strarray unix_deps = empty_strarray;
3324 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3326 if (!make->native_unix_lib)
3328 struct strarray unix_imports = empty_strarray;
3330 strarray_add( &unix_imports, "ntdll" );
3331 strarray_add( &unix_deps, obj_dir_path( top_makefile, "dlls/ntdll/ntdll.so" ));
3332 strarray_add( &unix_imports, "winecrt0" );
3333 if (spec_file) strarray_add( &unix_deps, spec_file );
3335 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 0 ));
3336 strarray_addall( &unix_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3337 strarray_addall( &unix_libs, libs );
3339 else unix_libs = add_unix_libraries( make, &unix_deps );
3341 strarray_add( &make->all_targets, make->unixlib );
3342 add_install_rule( make, make->module, make->unixlib, strmake( "p%s/%s", so_dir, make->unixlib ));
3343 output( "%s:", obj_dir_path( make, make->unixlib ));
3344 output_filenames_obj_dir( make, make->unixobj_files );
3345 output_filenames( unix_deps );
3347 if (make->native_unix_lib)
3349 output( "\n" );
3350 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3351 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3353 else
3355 output_filename( tools_path( make, "winebuild" ));
3356 output_filename( tools_path( make, "winegcc" ));
3357 output( "\n" );
3358 output_winegcc_command( make, 0 );
3359 output_filename( "-munix" );
3360 output_filename( "-shared" );
3361 if (spec_file) output_filename( spec_file );
3363 output_filenames_obj_dir( make, make->unixobj_files );
3364 output_filenames( unix_libs );
3365 output_filename( "$(LDFLAGS)" );
3366 output( "\n" );
3370 /*******************************************************************
3371 * output_static_lib
3373 static void output_static_lib( struct makefile *make )
3375 if (!crosstarget || !make->extlib)
3377 strarray_add( &make->clean_files, make->staticlib );
3378 output( "%s: %s", obj_dir_path( make, make->staticlib ), tools_path( make, "winebuild" ));
3379 output_filenames_obj_dir( make, make->object_files );
3380 output_filenames_obj_dir( make, make->unixobj_files );
3381 output( "\n" );
3382 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3383 output_filenames( target_flags );
3384 output_filenames_obj_dir( make, make->object_files );
3385 output_filenames_obj_dir( make, make->unixobj_files );
3386 output( "\n" );
3387 if (!make->extlib)
3388 add_install_rule( make, make->staticlib, make->staticlib,
3389 strmake( "d%s/%s", so_dir, make->staticlib ));
3391 if (crosstarget)
3393 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3395 strarray_add( &make->clean_files, name );
3396 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3397 output_filenames_obj_dir( make, make->crossobj_files );
3398 output( "\n" );
3399 output( "\t%s%s -b %s -w --staticlib -o $@", cmd_prefix( "BUILD" ),
3400 tools_path( make, "winebuild" ), crosstarget );
3401 output_filenames_obj_dir( make, make->crossobj_files );
3402 output( "\n" );
3403 if (!make->extlib)
3404 add_install_rule( make, make->staticlib, name,
3405 strmake( "d%s/%s", pe_dir, make->staticlib ));
3410 /*******************************************************************
3411 * output_shared_lib
3413 static void output_shared_lib( struct makefile *make )
3415 unsigned int i;
3416 char *basename, *p;
3417 struct strarray names = get_shared_lib_names( make->sharedlib );
3418 struct strarray all_libs = empty_strarray;
3419 struct strarray dep_libs = empty_strarray;
3421 basename = xstrdup( make->sharedlib );
3422 if ((p = strchr( basename, '.' ))) *p = 0;
3424 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3425 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3426 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3427 strarray_addall( &all_libs, libs );
3429 output( "%s:", obj_dir_path( make, make->sharedlib ));
3430 output_filenames_obj_dir( make, make->object_files );
3431 output_filenames( dep_libs );
3432 output( "\n" );
3433 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3434 output_filenames_obj_dir( make, make->object_files );
3435 output_filenames( all_libs );
3436 output_filename( "$(LDFLAGS)" );
3437 output( "\n" );
3438 add_install_rule( make, make->sharedlib, make->sharedlib, strmake( "p%s/%s", so_dir, make->sharedlib ));
3439 for (i = 1; i < names.count; i++)
3441 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3442 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3443 add_install_rule( make, names.str[i], names.str[i-1], strmake( "y%s/%s", so_dir, names.str[i] ));
3445 strarray_addall( &make->all_targets, names );
3449 /*******************************************************************
3450 * output_test_module
3452 static void output_test_module( struct makefile *make )
3454 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3455 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3456 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3457 struct strarray dep_libs = empty_strarray;
3458 struct strarray all_libs = add_import_libs( make, &dep_libs, add_default_imports( make, make->imports ),
3459 0, make->is_cross );
3460 struct makefile *parent = get_parent_makefile( make );
3461 const char *ext = make->is_cross ? "" : dll_ext;
3462 const char *debug_file;
3463 char *output_file;
3465 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3466 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3467 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3468 output( "%s:\n", output_file );
3469 output_winegcc_command( make, make->is_cross );
3470 output_filenames( make->extradllflags );
3471 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3472 output_filenames_obj_dir( make, make->res_files );
3473 if ((debug_file = get_debug_file( make, testmodule )))
3474 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3475 output_filenames( all_libs );
3476 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3477 output( "\n" );
3478 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3479 output_winegcc_command( make, make->is_cross );
3480 output_filename( "-s" );
3481 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3482 output_filenames( make->extradllflags );
3483 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3484 output_filenames_obj_dir( make, make->res_files );
3485 output_filenames( all_libs );
3486 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3487 output( "\n" );
3488 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3489 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3490 output_filenames_obj_dir( make, make->res_files );
3491 output_filenames( dep_libs );
3492 output_filename( tools_path( make, "winebuild" ));
3493 output_filename( tools_path( make, "winegcc" ));
3494 output( "\n" );
3496 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3497 output( "\t%secho \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3498 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3500 output_filenames_obj_dir( make, make->ok_files );
3501 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3502 if (parent)
3504 output_filename( parent->is_cross ? obj_dir_path( parent, make->testdll )
3505 : strmake( "%s%s", obj_dir_path( parent, make->testdll ), dll_ext ));
3506 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3508 output( "\n" );
3509 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3510 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3511 output( "\n" );
3512 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3513 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3514 output( "%s::\n", obj_dir_path( make, "testclean" ));
3515 output( "\trm -f" );
3516 output_filenames_obj_dir( make, make->ok_files );
3517 output( "\n" );
3518 strarray_addall( &make->clean_files, make->ok_files );
3519 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3523 /*******************************************************************
3524 * output_programs
3526 static void output_programs( struct makefile *make )
3528 unsigned int i, j;
3530 for (i = 0; i < make->programs.count; i++)
3532 char *program_installed = NULL;
3533 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3534 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3535 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3536 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3537 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3539 if (!objs.count) objs = make->object_files;
3540 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3542 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3543 strarray_addall( &all_libs, libs );
3546 output( "%s:", obj_dir_path( make, program ) );
3547 output_filenames_obj_dir( make, objs );
3548 output_filenames( deps );
3549 output( "\n" );
3550 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3551 output_filenames_obj_dir( make, objs );
3552 output_filenames( all_libs );
3553 output_filename( "$(LDFLAGS)" );
3554 output( "\n" );
3555 strarray_add( &make->all_targets, program );
3557 for (j = 0; j < symlinks.count; j++)
3559 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3560 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3562 strarray_addall( &make->all_targets, symlinks );
3564 add_install_rule( make, program, program_installed ? program_installed : program,
3565 strmake( "p$(bindir)/%s", program ));
3566 for (j = 0; j < symlinks.count; j++)
3567 add_install_rule( make, symlinks.str[j], program,
3568 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3573 /*******************************************************************
3574 * output_subdirs
3576 static void output_subdirs( struct makefile *make )
3578 struct strarray all_targets = empty_strarray;
3579 struct strarray makefile_deps = empty_strarray;
3580 struct strarray clean_files = empty_strarray;
3581 struct strarray testclean_files = empty_strarray;
3582 struct strarray distclean_files = empty_strarray;
3583 struct strarray dependencies = empty_strarray;
3584 struct strarray install_lib_deps = empty_strarray;
3585 struct strarray install_dev_deps = empty_strarray;
3586 struct strarray tooldeps_deps = empty_strarray;
3587 struct strarray buildtest_deps = empty_strarray;
3588 unsigned int i;
3590 strarray_addall( &clean_files, make->clean_files );
3591 strarray_addall( &distclean_files, make->distclean_files );
3592 strarray_addall( &all_targets, make->all_targets );
3593 for (i = 0; i < subdirs.count; i++)
3595 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3596 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3597 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3598 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3599 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3600 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3601 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3602 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3603 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3605 if (submakes[i]->disabled) continue;
3607 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3608 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3609 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3610 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3611 if (submakes[i]->testdll)
3612 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3613 if (submakes[i]->install_rules[INSTALL_LIB].count)
3614 strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
3615 if (submakes[i]->install_rules[INSTALL_DEV].count)
3616 strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
3618 strarray_addall( &dependencies, makefile_deps );
3619 output( "all:" );
3620 output_filenames( all_targets );
3621 output( "\n" );
3622 output( "Makefile:" );
3623 output_filenames( makefile_deps );
3624 output( "\n" );
3625 output_filenames( dependencies );
3626 output( ":\n" );
3627 if (install_lib_deps.count)
3629 output( "install install-lib::" );
3630 output_filenames( install_lib_deps );
3631 output( "\n" );
3632 strarray_add_uniq( &make->phony_targets, "install" );
3633 strarray_add_uniq( &make->phony_targets, "install-lib" );
3635 if (install_dev_deps.count)
3637 output( "install install-dev::" );
3638 output_filenames( install_dev_deps );
3639 output( "\n" );
3640 strarray_add_uniq( &make->phony_targets, "install" );
3641 strarray_add_uniq( &make->phony_targets, "install-dev" );
3643 output_uninstall_rules( make );
3644 if (buildtest_deps.count)
3646 output( "buildtests:" );
3647 output_filenames( buildtest_deps );
3648 output( "\n" );
3649 strarray_add_uniq( &make->phony_targets, "buildtests" );
3651 output( "check test:" );
3652 output_filenames( testclean_files );
3653 output( "\n" );
3654 strarray_add_uniq( &make->phony_targets, "check" );
3655 strarray_add_uniq( &make->phony_targets, "test" );
3657 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3659 output( "clean::\n");
3660 output_rm_filenames( clean_files );
3661 output( "testclean::\n");
3662 output_rm_filenames( testclean_files );
3663 output( "distclean::\n");
3664 output_rm_filenames( distclean_files );
3665 output( "maintainer-clean::\n");
3666 output_rm_filenames( make->maintainerclean_files );
3667 strarray_add_uniq( &make->phony_targets, "distclean" );
3668 strarray_add_uniq( &make->phony_targets, "testclean" );
3669 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3671 if (tooldeps_deps.count)
3673 output( "__tooldeps__:" );
3674 output_filenames( tooldeps_deps );
3675 output( "\n" );
3676 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3679 if (make->phony_targets.count)
3681 output( ".PHONY:" );
3682 output_filenames( make->phony_targets );
3683 output( "\n" );
3688 /*******************************************************************
3689 * output_sources
3691 static void output_sources( struct makefile *make )
3693 struct strarray all_targets = empty_strarray;
3694 struct incl_file *source;
3695 unsigned int i, j;
3697 strarray_add_uniq( &make->phony_targets, "all" );
3699 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3701 char *obj = xstrdup( source->name );
3702 char *ext = get_extension( obj );
3704 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3705 *ext++ = 0;
3707 for (j = 0; output_source_funcs[j].ext; j++)
3708 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3710 output_source_funcs[j].fn( make, source, obj );
3711 strarray_addall_uniq( &make->dependencies, source->dependencies );
3714 /* special case for winetest: add resource files from other test dirs */
3715 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3717 struct strarray tests = enable_tests;
3718 if (!tests.count)
3719 for (i = 0; i < subdirs.count; i++)
3720 if (submakes[i]->testdll && !submakes[i]->disabled)
3721 strarray_add( &tests, submakes[i]->testdll );
3722 for (i = 0; i < tests.count; i++)
3723 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3726 if (make->dlldata_files.count)
3728 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3729 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3730 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3731 output_filenames( make->dlldata_files );
3732 output( "\n" );
3735 if (make->staticlib) output_static_lib( make );
3736 else if (make->module)
3738 output_module( make );
3739 if (*dll_ext && !make->is_cross && !make->data_only) output_fake_module( make );
3740 if (make->unixlib) output_unix_lib( make );
3741 if (make->importlib) output_import_lib( make );
3743 else if (make->testdll) output_test_module( make );
3744 else if (make->sharedlib) output_shared_lib( make );
3745 else if (make->programs.count) output_programs( make );
3747 for (i = 0; i < make->scripts.count; i++)
3748 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3749 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3751 for (i = 0; i < make->extra_targets.count; i++)
3752 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3753 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3754 else
3755 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3757 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3758 strarray_add( &make->distclean_files, "Makefile" );
3759 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3761 if (!make->obj_dir)
3762 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3763 else if (!strcmp( make->obj_dir, "po" ))
3764 strarray_add( &make->distclean_files, "LINGUAS" );
3766 strarray_addall( &make->clean_files, make->object_files );
3767 strarray_addall( &make->clean_files, make->crossobj_files );
3768 strarray_addall( &make->clean_files, make->implib_files );
3769 strarray_addall( &make->clean_files, make->crossimplib_files );
3770 strarray_addall( &make->clean_files, make->unixobj_files );
3771 strarray_addall( &make->clean_files, make->res_files );
3772 strarray_addall( &make->clean_files, make->pot_files );
3773 strarray_addall( &make->clean_files, make->debug_files );
3774 strarray_addall( &make->clean_files, make->all_targets );
3776 if (make == top_makefile)
3778 output_subdirs( make );
3779 return;
3782 strarray_addall( &all_targets, make->all_targets );
3783 strarray_addall( &all_targets, make->font_files );
3784 if (all_targets.count)
3786 output( "%s:", obj_dir_path( make, "all" ));
3787 output_filenames_obj_dir( make, all_targets );
3788 output( "\n" );
3789 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3791 output_install_rules( make, INSTALL_LIB, "install-lib" );
3792 output_install_rules( make, INSTALL_DEV, "install-dev" );
3794 if (make->clean_files.count)
3796 output( "%s::\n", obj_dir_path( make, "clean" ));
3797 output( "\trm -f" );
3798 output_filenames_obj_dir( make, make->clean_files );
3799 output( "\n" );
3800 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3805 /*******************************************************************
3806 * create_temp_file
3808 static FILE *create_temp_file( const char *orig )
3810 char *name = xmalloc( strlen(orig) + 13 );
3811 unsigned int i, id = getpid();
3812 int fd;
3813 FILE *ret = NULL;
3815 for (i = 0; i < 100; i++)
3817 sprintf( name, "%s.tmp%08x", orig, id );
3818 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3820 ret = fdopen( fd, "w" );
3821 break;
3823 if (errno != EEXIST) break;
3824 id += 7777;
3826 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3827 temp_file_name = name;
3828 return ret;
3832 /*******************************************************************
3833 * rename_temp_file
3835 static void rename_temp_file( const char *dest )
3837 int ret = rename( temp_file_name, dest );
3838 if (ret == -1 && errno == EEXIST)
3840 /* rename doesn't overwrite on windows */
3841 unlink( dest );
3842 ret = rename( temp_file_name, dest );
3844 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3845 temp_file_name = NULL;
3849 /*******************************************************************
3850 * are_files_identical
3852 static int are_files_identical( FILE *file1, FILE *file2 )
3854 for (;;)
3856 char buffer1[8192], buffer2[8192];
3857 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3858 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3859 if (size1 != size2) return 0;
3860 if (!size1) return feof( file1 ) && feof( file2 );
3861 if (memcmp( buffer1, buffer2, size1 )) return 0;
3866 /*******************************************************************
3867 * rename_temp_file_if_changed
3869 static void rename_temp_file_if_changed( const char *dest )
3871 FILE *file1, *file2;
3872 int do_rename = 1;
3874 if ((file1 = fopen( dest, "r" )))
3876 if ((file2 = fopen( temp_file_name, "r" )))
3878 do_rename = !are_files_identical( file1, file2 );
3879 fclose( file2 );
3881 fclose( file1 );
3883 if (!do_rename)
3885 unlink( temp_file_name );
3886 temp_file_name = NULL;
3888 else rename_temp_file( dest );
3892 /*******************************************************************
3893 * output_linguas
3895 static void output_linguas( const struct makefile *make )
3897 const char *dest = obj_dir_path( make, "LINGUAS" );
3898 struct incl_file *source;
3900 output_file = create_temp_file( dest );
3902 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3903 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3904 if (strendswith( source->name, ".po" ))
3905 output( "%s\n", replace_extension( source->name, ".po", "" ));
3907 if (fclose( output_file )) fatal_perror( "write" );
3908 output_file = NULL;
3909 rename_temp_file_if_changed( dest );
3913 /*******************************************************************
3914 * output_testlist
3916 static void output_testlist( const struct makefile *make )
3918 const char *dest = obj_dir_path( make, "testlist.c" );
3919 unsigned int i;
3921 output_file = create_temp_file( dest );
3923 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3924 output( "#define WIN32_LEAN_AND_MEAN\n" );
3925 output( "#include <windows.h>\n\n" );
3926 output( "#define STANDALONE\n" );
3927 output( "#include \"wine/test.h\"\n\n" );
3929 for (i = 0; i < make->test_files.count; i++)
3930 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3931 output( "\n" );
3932 output( "const struct test winetest_testlist[] =\n" );
3933 output( "{\n" );
3934 for (i = 0; i < make->test_files.count; i++)
3935 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3936 output( " { 0, 0 }\n" );
3937 output( "};\n" );
3939 if (fclose( output_file )) fatal_perror( "write" );
3940 output_file = NULL;
3941 rename_temp_file_if_changed( dest );
3945 /*******************************************************************
3946 * output_gitignore
3948 static void output_gitignore( const char *dest, struct strarray files )
3950 unsigned int i;
3952 output_file = create_temp_file( dest );
3954 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3955 for (i = 0; i < files.count; i++)
3957 if (!strchr( files.str[i], '/' )) output( "/" );
3958 output( "%s\n", files.str[i] );
3961 if (fclose( output_file )) fatal_perror( "write" );
3962 output_file = NULL;
3963 rename_temp_file( dest );
3967 /*******************************************************************
3968 * output_stub_makefile
3970 static void output_stub_makefile( struct makefile *make )
3972 struct strarray targets = empty_strarray;
3973 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
3975 if (make->obj_dir) create_dir( make->obj_dir );
3977 output_file_name = obj_dir_path( make, "Makefile" );
3978 output_file = create_temp_file( output_file_name );
3980 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
3982 if (make_var) output( "MAKE = %s\n\n", make_var );
3983 output( "all:\n" );
3985 if (make->all_targets.count) strarray_add( &targets, "all" );
3986 if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
3987 strarray_add( &targets, "install" );
3988 if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
3989 if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
3990 if (make->clean_files.count) strarray_add( &targets, "clean" );
3991 if (make->test_files.count)
3993 strarray_add( &targets, "check" );
3994 strarray_add( &targets, "test" );
3995 strarray_add( &targets, "testclean" );
3998 output_filenames( targets );
3999 output_filenames( make->clean_files );
4000 output( ":\n" );
4001 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4002 output( ".PHONY:" );
4003 output_filenames( targets );
4004 output( "\n" );
4006 fclose( output_file );
4007 output_file = NULL;
4008 rename_temp_file( output_file_name );
4012 /*******************************************************************
4013 * output_silent_rules
4015 static void output_silent_rules(void)
4017 static const char *cmds[] =
4019 "BISON",
4020 "BUILD",
4021 "CC",
4022 "CCLD",
4023 "FLEX",
4024 "GEN",
4025 "LN",
4026 "MSG",
4027 "SED",
4028 "TEST",
4029 "WIDL",
4030 "WMC",
4031 "WRC"
4033 unsigned int i;
4035 output( "V = 0\n" );
4036 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++)
4038 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4039 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4040 output( "quiet_%s_1 =\n", cmds[i] );
4045 /*******************************************************************
4046 * output_top_makefile
4048 static void output_top_makefile( struct makefile *make )
4050 char buffer[1024];
4051 FILE *src_file;
4052 unsigned int i;
4053 int found = 0;
4055 output_file_name = obj_dir_path( make, output_makefile_name );
4056 output_file = create_temp_file( output_file_name );
4058 /* copy the contents of the source makefile */
4059 src_file = open_input_makefile( make );
4060 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4062 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4063 found = !strncmp( buffer, separator, strlen(separator) );
4065 if (fclose( src_file )) fatal_perror( "close" );
4066 input_file_name = NULL;
4068 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4070 if (silent_rules) output_silent_rules();
4071 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4072 output_sources( make );
4073 /* disable implicit rules */
4074 output( ".SUFFIXES:\n" );
4076 fclose( output_file );
4077 output_file = NULL;
4078 rename_temp_file( output_file_name );
4082 /*******************************************************************
4083 * output_dependencies
4085 static void output_dependencies( struct makefile *make )
4087 struct strarray ignore_files = empty_strarray;
4089 if (make->obj_dir) create_dir( make->obj_dir );
4091 if (make == top_makefile) output_top_makefile( make );
4092 else output_stub_makefile( make );
4094 strarray_addall( &ignore_files, make->distclean_files );
4095 strarray_addall( &ignore_files, make->clean_files );
4096 if (make->testdll) output_testlist( make );
4097 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4098 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4100 create_file_directories( make, ignore_files );
4102 output_file_name = NULL;
4106 /*******************************************************************
4107 * load_sources
4109 static void load_sources( struct makefile *make )
4111 static const char *source_vars[] =
4113 "SOURCES",
4114 "C_SRCS",
4115 "OBJC_SRCS",
4116 "RC_SRCS",
4117 "MC_SRCS",
4118 "IDL_SRCS",
4119 "BISON_SRCS",
4120 "LEX_SRCS",
4121 "HEADER_SRCS",
4122 "XTEMPLATE_SRCS",
4123 "SVG_SRCS",
4124 "FONT_SRCS",
4125 "IN_SRCS",
4126 "PO_SRCS",
4127 "MANPAGES",
4128 NULL
4130 const char **var;
4131 unsigned int i;
4132 struct strarray value;
4133 struct incl_file *file;
4135 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4136 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4138 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4139 make->module = get_expanded_make_variable( make, "MODULE" );
4140 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4141 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4142 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4143 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4144 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4145 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4147 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4148 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4149 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4150 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4151 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4152 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4153 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4154 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4156 if (make->extlib) make->staticlib = make->extlib;
4157 if (make->staticlib) make->module = make->staticlib;
4159 if (host_cpu)
4161 value = get_expanded_file_local_var( make, host_cpu, "EXTRADLLFLAGS" );
4162 if (value.count) make->extradllflags = value;
4165 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4166 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4167 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4168 make->use_msvcrt = (make->module || make->testdll || make->is_win16) &&
4169 !strarray_exists( &make->extradllflags, "-mcygwin" );
4170 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4171 strarray_exists( &make->extradllflags, "-mwindows" );
4172 make->native_unix_lib = !!make->unixlib;
4174 if (make->use_msvcrt) strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4176 if (make->module && !make->install_lib.count && !make->install_dev.count)
4178 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4179 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4180 else strarray_add( &make->install_lib, make->module );
4183 make->include_paths = empty_strarray;
4184 make->include_args = empty_strarray;
4185 make->define_args = empty_strarray;
4186 make->unix_cflags = empty_strarray;
4187 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4189 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4190 for (i = 0; i < value.count; i++)
4192 if (!strncmp( value.str[i], "-I", 2 ))
4194 const char *dir = value.str[i] + 2;
4195 if (!strncmp( dir, "./", 2 ))
4197 dir += 2;
4198 while (*dir == '/') dir++;
4200 strarray_add_uniq( &make->include_paths, dir );
4202 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4203 strarray_add_uniq( &make->define_args, value.str[i] );
4205 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4206 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4208 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4209 if (make->src_dir)
4210 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4211 if (make->parent_dir)
4212 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4213 strarray_add( &make->include_args, "-Iinclude" );
4214 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4216 list_init( &make->sources );
4217 list_init( &make->includes );
4219 for (var = source_vars; *var; var++)
4221 value = get_expanded_make_var_array( make, *var );
4222 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4225 add_generated_sources( make );
4226 if (!make->unixlib) make->unixlib = get_unix_lib_name( make );
4228 if (make->use_msvcrt) strarray_add( &make->define_args, get_crt_define( make ));
4230 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4231 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4233 make->is_cross = crosstarget && make->use_msvcrt;
4235 if (!*dll_ext || make->is_cross)
4236 for (i = 0; i < make->delayimports.count; i++)
4237 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4241 /*******************************************************************
4242 * parse_makeflags
4244 static void parse_makeflags( const char *flags )
4246 const char *p = flags;
4247 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4249 while (*p)
4251 while (isspace(*p)) p++;
4252 var = buffer;
4253 while (*p && !isspace(*p))
4255 if (*p == '\\' && p[1]) p++;
4256 *var++ = *p++;
4258 *var = 0;
4259 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4264 /*******************************************************************
4265 * parse_option
4267 static int parse_option( const char *opt )
4269 if (opt[0] != '-')
4271 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4272 return 0;
4274 switch(opt[1])
4276 case 'f':
4277 if (opt[2]) output_makefile_name = opt + 2;
4278 break;
4279 case 'R':
4280 relative_dir_mode = 1;
4281 break;
4282 case 'S':
4283 silent_rules = 1;
4284 break;
4285 default:
4286 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4287 exit(1);
4289 return 1;
4293 /*******************************************************************
4294 * main
4296 int main( int argc, char *argv[] )
4298 const char *makeflags = getenv( "MAKEFLAGS" );
4299 int i, j;
4301 if (makeflags) parse_makeflags( makeflags );
4303 i = 1;
4304 while (i < argc)
4306 if (parse_option( argv[i] ))
4308 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4309 argc--;
4311 else i++;
4314 if (relative_dir_mode)
4316 char *relpath;
4318 if (argc != 3)
4320 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4321 exit( 1 );
4323 relpath = get_relative_path( argv[1], argv[2] );
4324 printf( "%s\n", relpath ? relpath : "." );
4325 exit( 0 );
4328 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4330 atexit( cleanup_files );
4331 signal( SIGTERM, exit_on_signal );
4332 signal( SIGINT, exit_on_signal );
4333 #ifdef SIGHUP
4334 signal( SIGHUP, exit_on_signal );
4335 #endif
4337 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4338 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4340 top_makefile = parse_makefile( NULL );
4342 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4343 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4344 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4345 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4346 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4347 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4348 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4349 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4350 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4351 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4352 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4353 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4355 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4356 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4357 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4358 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4359 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4360 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4361 host_cpu = get_expanded_make_variable( top_makefile, "host_cpu" );
4362 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4363 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4364 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4365 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4366 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4367 bison = get_expanded_make_variable( top_makefile, "BISON" );
4368 ar = get_expanded_make_variable( top_makefile, "AR" );
4369 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4370 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4371 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4372 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4373 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4374 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4375 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4377 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4378 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4379 if (!exe_ext) exe_ext = "";
4380 if (!tools_ext) tools_ext = "";
4381 if (host_cpu && (host_cpu = normalize_arch( host_cpu )))
4383 so_dir = strmake( "$(dlldir)/%s-unix", host_cpu );
4384 pe_dir = strmake( "$(dlldir)/%s-windows", host_cpu );
4386 else
4387 so_dir = pe_dir = "$(dlldir)";
4389 extra_cflags_extlib = remove_warning_flags( extra_cflags );
4390 extra_cross_cflags_extlib = remove_warning_flags( extra_cross_cflags );
4392 top_makefile->src_dir = root_src_dir;
4393 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4394 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4395 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4397 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4399 load_sources( top_makefile );
4400 load_sources( include_makefile );
4401 for (i = 0; i < subdirs.count; i++)
4402 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4404 output_dependencies( top_makefile );
4405 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4407 return 0;