jscript: Release all globals when the script is uninitialized.
[wine.git] / tools / makedep.c
blob83e4844050a6f821b897bd60eb11edb02ba5ec5d
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 file *file;
70 char *name;
71 char *filename;
72 char *basename; /* base target name for generated files */
73 char *sourcename; /* source file name for generated headers */
74 struct incl_file *included_by; /* file that included this one */
75 int included_line; /* line where this file was included */
76 enum incl_type type; /* type of include */
77 int use_msvcrt:1; /* put msvcrt headers in the search path? */
78 int is_external:1; /* file from external library? */
79 struct incl_file *owner;
80 unsigned int files_count; /* files in use */
81 unsigned int files_size; /* total allocated size */
82 struct incl_file **files;
83 struct strarray dependencies; /* file dependencies */
84 struct strarray importlibdeps; /* importlib dependencies */
87 #define FLAG_GENERATED 0x000001 /* generated file */
88 #define FLAG_INSTALL 0x000002 /* file to install */
89 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
90 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
91 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
92 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
93 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
94 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (_l.res) file */
95 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
96 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
97 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
98 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
99 #define FLAG_C_UNIX 0x040000 /* file is part of a Unix library */
100 #define FLAG_SFD_FONTS 0x080000 /* sfd file generated bitmap fonts */
102 static const struct
104 unsigned int flag;
105 const char *ext;
106 } idl_outputs[] =
108 { FLAG_IDL_TYPELIB, "_l.res" },
109 { FLAG_IDL_REGTYPELIB, "_t.res" },
110 { FLAG_IDL_CLIENT, "_c.c" },
111 { FLAG_IDL_IDENT, "_i.c" },
112 { FLAG_IDL_PROXY, "_p.c" },
113 { FLAG_IDL_SERVER, "_s.c" },
114 { FLAG_IDL_REGISTER, "_r.res" },
115 { FLAG_IDL_HEADER, ".h" }
118 #define HASH_SIZE 997
120 static struct list files[HASH_SIZE];
122 enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
124 /* variables common to all makefiles */
125 static struct strarray linguas;
126 static struct strarray dll_flags;
127 static struct strarray unix_dllflags;
128 static struct strarray target_flags;
129 static struct strarray msvcrt_flags;
130 static struct strarray extra_cflags;
131 static struct strarray extra_cross_cflags;
132 static struct strarray extra_cflags_extlib;
133 static struct strarray extra_cross_cflags_extlib;
134 static struct strarray cpp_flags;
135 static struct strarray lddll_flags;
136 static struct strarray libs;
137 static struct strarray enable_tests;
138 static struct strarray cmdline_vars;
139 static struct strarray subdirs;
140 static struct strarray disabled_dirs;
141 static struct strarray delay_import_libs;
142 static struct strarray top_install_lib;
143 static struct strarray top_install_dev;
144 static const char *root_src_dir;
145 static const char *tools_dir;
146 static const char *tools_ext;
147 static const char *exe_ext;
148 static const char *dll_ext;
149 static const char *host_cpu;
150 static const char *pe_dir;
151 static const char *so_dir;
152 static const char *crosstarget;
153 static const char *crossdebug;
154 static const char *fontforge;
155 static const char *convert;
156 static const char *flex;
157 static const char *bison;
158 static const char *ar;
159 static const char *ranlib;
160 static const char *rsvg;
161 static const char *icotool;
162 static const char *dlltool;
163 static const char *msgfmt;
164 static const char *ln_s;
165 static const char *sed_cmd;
166 static const char *delay_load_flag;
168 struct makefile
170 /* values determined from input makefile */
171 struct strarray vars;
172 struct strarray include_paths;
173 struct strarray include_args;
174 struct strarray define_args;
175 struct strarray programs;
176 struct strarray scripts;
177 struct strarray imports;
178 struct strarray delayimports;
179 struct strarray extradllflags;
180 struct strarray install_lib;
181 struct strarray install_dev;
182 struct strarray extra_targets;
183 struct strarray extra_imports;
184 struct list sources;
185 struct list includes;
186 const char *src_dir;
187 const char *obj_dir;
188 const char *parent_dir;
189 const char *module;
190 const char *testdll;
191 const char *extlib;
192 const char *sharedlib;
193 const char *staticlib;
194 const char *staticimplib;
195 const char *importlib;
196 const char *unixlib;
197 int native_unix_lib;
198 int disabled;
199 int use_msvcrt;
200 int data_only;
201 int is_cross;
202 int is_win16;
203 int is_exe;
205 /* values generated at output time */
206 struct strarray in_files;
207 struct strarray ok_files;
208 struct strarray pot_files;
209 struct strarray test_files;
210 struct strarray clean_files;
211 struct strarray distclean_files;
212 struct strarray maintainerclean_files;
213 struct strarray uninstall_files;
214 struct strarray object_files;
215 struct strarray crossobj_files;
216 struct strarray unixobj_files;
217 struct strarray res_files;
218 struct strarray font_files;
219 struct strarray debug_files;
220 struct strarray dlldata_files;
221 struct strarray implib_files;
222 struct strarray crossimplib_files;
223 struct strarray all_targets;
224 struct strarray phony_targets;
225 struct strarray dependencies;
226 struct strarray install_rules[NB_INSTALL_RULES];
229 static struct makefile *top_makefile;
230 static struct makefile **submakes;
232 static const char separator[] = "### Dependencies";
233 static const char *output_makefile_name = "Makefile";
234 static const char *input_file_name;
235 static const char *output_file_name;
236 static const char *temp_file_name;
237 static int relative_dir_mode;
238 static int silent_rules;
239 static int input_line;
240 static int output_column;
241 static FILE *output_file;
243 static const char Usage[] =
244 "Usage: makedep [options] [directories]\n"
245 "Options:\n"
246 " -R from to Compute the relative path between two directories\n"
247 " -S Generate Automake-style silent rules\n"
248 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
251 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
252 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
253 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
254 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
256 /*******************************************************************
257 * fatal_error
259 static void fatal_error( const char *msg, ... )
261 va_list valist;
262 va_start( valist, msg );
263 if (input_file_name)
265 fprintf( stderr, "%s:", input_file_name );
266 if (input_line) fprintf( stderr, "%d:", input_line );
267 fprintf( stderr, " error: " );
269 else fprintf( stderr, "makedep: error: " );
270 vfprintf( stderr, msg, valist );
271 va_end( valist );
272 exit(1);
276 /*******************************************************************
277 * fatal_perror
279 static void fatal_perror( const char *msg, ... )
281 va_list valist;
282 va_start( valist, msg );
283 if (input_file_name)
285 fprintf( stderr, "%s:", input_file_name );
286 if (input_line) fprintf( stderr, "%d:", input_line );
287 fprintf( stderr, " error: " );
289 else fprintf( stderr, "makedep: error: " );
290 vfprintf( stderr, msg, valist );
291 perror( " " );
292 va_end( valist );
293 exit(1);
297 /*******************************************************************
298 * cleanup_files
300 static void cleanup_files(void)
302 if (temp_file_name) unlink( temp_file_name );
303 if (output_file_name) unlink( output_file_name );
307 /*******************************************************************
308 * exit_on_signal
310 static void exit_on_signal( int sig )
312 exit( 1 ); /* this will call the atexit functions */
316 /*******************************************************************
317 * output
319 static void output( const char *format, ... )
321 int ret;
322 va_list valist;
324 va_start( valist, format );
325 ret = vfprintf( output_file, format, valist );
326 va_end( valist );
327 if (ret < 0) fatal_perror( "output" );
328 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
329 else output_column += ret;
333 /*******************************************************************
334 * strarray_get_value
336 * Find a value in a name/value pair string array.
338 static const char *strarray_get_value( const struct strarray *array, const char *name )
340 int pos, res, min = 0, max = array->count / 2 - 1;
342 while (min <= max)
344 pos = (min + max) / 2;
345 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
346 if (res < 0) min = pos + 1;
347 else max = pos - 1;
349 return NULL;
353 /*******************************************************************
354 * strarray_set_value
356 * Define a value in a name/value pair string array.
358 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
360 int i, pos, res, min = 0, max = array->count / 2 - 1;
362 while (min <= max)
364 pos = (min + max) / 2;
365 if (!(res = strcmp( array->str[pos * 2], name )))
367 /* redefining a variable replaces the previous value */
368 array->str[pos * 2 + 1] = value;
369 return;
371 if (res < 0) min = pos + 1;
372 else max = pos - 1;
374 strarray_add( array, NULL );
375 strarray_add( array, NULL );
376 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
377 array->str[min * 2] = name;
378 array->str[min * 2 + 1] = value;
382 /*******************************************************************
383 * normalize_arch
385 static const char *normalize_arch( const char *arch )
387 unsigned int i, j;
389 static const char *map[][8] =
391 /* normalized aliases */
392 { "i386", "i486", "i586", "i686", "ia32" },
393 { "x86_64", "amd64", "x86-64", "x86_amd64", "x64" },
394 { "aarch64", "arm64" },
395 { "arm" },
398 for (i = 0; i < sizeof(map) / sizeof(map[0]); i++)
399 for (j = 0; map[i][j]; j++)
400 if (!strncmp( arch, map[i][j], strlen(map[i][j]) ))
401 return map[i][0];
402 return NULL;
406 /*******************************************************************
407 * output_filename
409 static void output_filename( const char *name )
411 if (output_column + strlen(name) + 1 > 100)
413 output( " \\\n" );
414 output( " " );
416 else if (output_column) output( " " );
417 output( "%s", name );
421 /*******************************************************************
422 * output_filenames
424 static void output_filenames( struct strarray array )
426 unsigned int i;
428 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
432 /*******************************************************************
433 * output_rm_filenames
435 static void output_rm_filenames( struct strarray array )
437 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
438 unsigned int i, len;
440 if (!array.count) return;
441 output( "\trm -f" );
442 for (i = len = 0; i < array.count; i++)
444 if (len > max_cmdline)
446 output( "\n" );
447 output( "\trm -f" );
448 len = 0;
450 output_filename( array.str[i] );
451 len += strlen( array.str[i] ) + 1;
453 output( "\n" );
457 /*******************************************************************
458 * get_extension
460 static char *get_extension( char *filename )
462 char *ext = strrchr( filename, '.' );
463 if (ext && strchr( ext, '/' )) ext = NULL;
464 return ext;
468 /*******************************************************************
469 * get_base_name
471 static const char *get_base_name( const char *name )
473 char *base;
474 if (!strchr( name, '.' )) return name;
475 base = xstrdup( name );
476 *strrchr( base, '.' ) = 0;
477 return base;
481 /*******************************************************************
482 * replace_filename
484 static char *replace_filename( const char *path, const char *name )
486 const char *p;
487 char *ret;
488 size_t len;
490 if (!path) return xstrdup( name );
491 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
492 len = p - path + 1;
493 ret = xmalloc( len + strlen( name ) + 1 );
494 memcpy( ret, path, len );
495 strcpy( ret + len, name );
496 return ret;
500 /*******************************************************************
501 * replace_substr
503 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
505 size_t pos = start - str;
506 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
507 memcpy( ret, str, pos );
508 strcpy( ret + pos, replace );
509 strcat( ret + pos, start + len );
510 return ret;
514 /*******************************************************************
515 * get_relative_path
517 * Determine where the destination path is located relative to the 'from' path.
519 static char *get_relative_path( const char *from, const char *dest )
521 const char *start;
522 char *ret, *p;
523 unsigned int dotdots = 0;
525 /* a path of "." is equivalent to an empty path */
526 if (!strcmp( from, "." )) from = "";
528 for (;;)
530 while (*from == '/') from++;
531 while (*dest == '/') dest++;
532 start = dest; /* save start of next path element */
533 if (!*from) break;
535 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
536 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
538 /* count remaining elements in 'from' */
541 dotdots++;
542 while (*from && *from != '/') from++;
543 while (*from == '/') from++;
545 while (*from);
546 break;
549 if (!start[0] && !dotdots) return NULL; /* empty path */
551 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
552 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
554 if (start[0]) strcpy( p, start );
555 else p[-1] = 0; /* remove trailing slash */
556 return ret;
560 /*******************************************************************
561 * concat_paths
563 static char *concat_paths( const char *base, const char *path )
565 int i, len;
566 char *ret;
568 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
569 if (!path || !path[0]) return xstrdup( base );
570 if (path[0] == '/') return xstrdup( path );
572 len = strlen( base );
573 while (len && base[len - 1] == '/') len--;
574 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
576 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
577 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
578 if (i != len - 1 || base[i] != '.')
580 path += 2;
581 while (*path == '/') path++;
583 /* else ignore "." element */
584 while (i > 0 && base[i - 1] == '/') i--;
585 len = i;
587 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
588 ret = xmalloc( len + strlen( path ) + 2 );
589 memcpy( ret, base, len );
590 ret[len++] = '/';
591 strcpy( ret + len, path );
592 return ret;
596 /*******************************************************************
597 * obj_dir_path
599 static char *obj_dir_path( const struct makefile *make, const char *path )
601 return concat_paths( make->obj_dir, path );
605 /*******************************************************************
606 * src_dir_path
608 static char *src_dir_path( const struct makefile *make, const char *path )
610 if (make->src_dir) return concat_paths( make->src_dir, path );
611 return obj_dir_path( make, path );
615 /*******************************************************************
616 * root_src_dir_path
618 static char *root_src_dir_path( const char *path )
620 return concat_paths( root_src_dir, path );
624 /*******************************************************************
625 * tools_dir_path
627 static char *tools_dir_path( const struct makefile *make, const char *path )
629 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
630 return strmake( "tools/%s", path );
634 /*******************************************************************
635 * tools_path
637 static char *tools_path( const struct makefile *make, const char *name )
639 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
643 /*******************************************************************
644 * strarray_addall_path
646 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
648 unsigned int i;
650 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
654 /*******************************************************************
655 * get_line
657 static char *get_line( FILE *file )
659 static char *buffer;
660 static size_t size;
662 if (!size)
664 size = 1024;
665 buffer = xmalloc( size );
667 if (!fgets( buffer, size, file )) return NULL;
668 input_line++;
670 for (;;)
672 char *p = buffer + strlen(buffer);
673 /* if line is larger than buffer, resize buffer */
674 while (p == buffer + size - 1 && p[-1] != '\n')
676 buffer = xrealloc( buffer, size * 2 );
677 if (!fgets( buffer + size - 1, size + 1, file )) break;
678 p = buffer + strlen(buffer);
679 size *= 2;
681 if (p > buffer && p[-1] == '\n')
683 *(--p) = 0;
684 if (p > buffer && p[-1] == '\r') *(--p) = 0;
685 if (p > buffer && p[-1] == '\\')
687 *(--p) = 0;
688 /* line ends in backslash, read continuation line */
689 if (!fgets( p, size - (p - buffer), file )) return buffer;
690 input_line++;
691 continue;
694 return buffer;
699 /*******************************************************************
700 * hash_filename
702 static unsigned int hash_filename( const char *name )
704 /* FNV-1 hash */
705 unsigned int ret = 2166136261u;
706 while (*name) ret = (ret * 16777619) ^ *name++;
707 return ret % HASH_SIZE;
711 /*******************************************************************
712 * add_file
714 static struct file *add_file( const char *name )
716 struct file *file = xmalloc( sizeof(*file) );
717 memset( file, 0, sizeof(*file) );
718 file->name = xstrdup( name );
719 return file;
723 /*******************************************************************
724 * add_dependency
726 static void add_dependency( struct file *file, const char *name, enum incl_type type )
728 if (file->deps_count >= file->deps_size)
730 file->deps_size *= 2;
731 if (file->deps_size < 16) file->deps_size = 16;
732 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
734 file->deps[file->deps_count].line = input_line;
735 file->deps[file->deps_count].type = type;
736 file->deps[file->deps_count].name = xstrdup( name );
737 file->deps_count++;
741 /*******************************************************************
742 * find_src_file
744 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
746 struct incl_file *file;
748 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
749 if (!strcmp( name, file->name )) return file;
750 return NULL;
753 /*******************************************************************
754 * find_include_file
756 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
758 struct incl_file *file;
760 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
762 const char *filename = file->filename;
763 if (!filename) continue;
764 if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
766 filename += strlen(make->obj_dir);
767 while (*filename == '/') filename++;
769 if (!strcmp( name, filename )) return file;
771 return NULL;
774 /*******************************************************************
775 * add_include
777 * Add an include file if it doesn't already exists.
779 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
780 const char *name, int line, enum incl_type type )
782 struct incl_file *include;
784 if (parent->files_count >= parent->files_size)
786 parent->files_size *= 2;
787 if (parent->files_size < 16) parent->files_size = 16;
788 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
791 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
792 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
793 goto found;
795 include = xmalloc( sizeof(*include) );
796 memset( include, 0, sizeof(*include) );
797 include->name = xstrdup(name);
798 include->included_by = parent;
799 include->included_line = line;
800 include->type = type;
801 include->use_msvcrt = parent->use_msvcrt;
802 list_add_tail( &make->includes, &include->entry );
803 found:
804 parent->files[parent->files_count++] = include;
805 return include;
809 /*******************************************************************
810 * add_generated_source
812 * Add a generated source file to the list.
814 static struct incl_file *add_generated_source( struct makefile *make,
815 const char *name, const char *filename )
817 struct incl_file *file;
819 if ((file = find_src_file( make, name ))) return file; /* we already have it */
820 file = xmalloc( sizeof(*file) );
821 memset( file, 0, sizeof(*file) );
822 file->file = add_file( name );
823 file->name = xstrdup( name );
824 file->basename = xstrdup( filename ? filename : name );
825 file->filename = obj_dir_path( make, file->basename );
826 file->file->flags = FLAG_GENERATED;
827 file->use_msvcrt = make->use_msvcrt;
828 list_add_tail( &make->sources, &file->entry );
829 return file;
833 /*******************************************************************
834 * parse_include_directive
836 static void parse_include_directive( struct file *source, char *str )
838 char quote, *include, *p = str;
840 while (*p && isspace(*p)) p++;
841 if (*p != '\"' && *p != '<' ) return;
842 quote = *p++;
843 if (quote == '<') quote = '>';
844 include = p;
845 while (*p && (*p != quote)) p++;
846 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
847 *p = 0;
848 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
852 /*******************************************************************
853 * parse_pragma_directive
855 static void parse_pragma_directive( struct file *source, char *str )
857 char *flag, *p = str;
859 if (!isspace( *p )) return;
860 while (*p && isspace(*p)) p++;
861 p = strtok( p, " \t" );
862 if (strcmp( p, "makedep" )) return;
864 while ((flag = strtok( NULL, " \t" )))
866 if (!strcmp( flag, "depend" ))
868 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
869 return;
871 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
873 if (strendswith( source->name, ".idl" ))
875 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
876 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
877 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
878 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
879 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
880 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
881 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
882 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
884 else if (strendswith( source->name, ".rc" ))
886 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
888 else if (strendswith( source->name, ".sfd" ))
890 if (!strcmp( flag, "font" ))
892 struct strarray *array = source->args;
894 if (!array)
896 source->args = array = xmalloc( sizeof(*array) );
897 *array = empty_strarray;
898 source->flags |= FLAG_SFD_FONTS;
900 strarray_add( array, xstrdup( strtok( NULL, "" )));
901 return;
904 else
906 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
907 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
913 /*******************************************************************
914 * parse_cpp_directive
916 static void parse_cpp_directive( struct file *source, char *str )
918 while (*str && isspace(*str)) str++;
919 if (*str++ != '#') return;
920 while (*str && isspace(*str)) str++;
922 if (!strncmp( str, "include", 7 ))
923 parse_include_directive( source, str + 7 );
924 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
925 parse_include_directive( source, str + 6 );
926 else if (!strncmp( str, "pragma", 6 ))
927 parse_pragma_directive( source, str + 6 );
931 /*******************************************************************
932 * parse_idl_file
934 static void parse_idl_file( struct file *source, FILE *file )
936 char *buffer, *include;
938 input_line = 0;
940 while ((buffer = get_line( file )))
942 char quote;
943 char *p = buffer;
944 while (*p && isspace(*p)) p++;
946 if (!strncmp( p, "importlib", 9 ))
948 p += 9;
949 while (*p && isspace(*p)) p++;
950 if (*p++ != '(') continue;
951 while (*p && isspace(*p)) p++;
952 if (*p++ != '"') continue;
953 include = p;
954 while (*p && (*p != '"')) p++;
955 if (!*p) fatal_error( "malformed importlib directive\n" );
956 *p = 0;
957 add_dependency( source, include, INCL_IMPORTLIB );
958 continue;
961 if (!strncmp( p, "import", 6 ))
963 p += 6;
964 while (*p && isspace(*p)) p++;
965 if (*p != '"') continue;
966 include = ++p;
967 while (*p && (*p != '"')) p++;
968 if (!*p) fatal_error( "malformed import directive\n" );
969 *p = 0;
970 add_dependency( source, include, INCL_IMPORT );
971 continue;
974 /* check for #include inside cpp_quote */
975 if (!strncmp( p, "cpp_quote", 9 ))
977 p += 9;
978 while (*p && isspace(*p)) p++;
979 if (*p++ != '(') continue;
980 while (*p && isspace(*p)) p++;
981 if (*p++ != '"') continue;
982 if (*p++ != '#') continue;
983 while (*p && isspace(*p)) p++;
984 if (strncmp( p, "include", 7 )) continue;
985 p += 7;
986 while (*p && isspace(*p)) p++;
987 if (*p == '\\' && p[1] == '"')
989 p += 2;
990 quote = '"';
992 else
994 if (*p++ != '<' ) continue;
995 quote = '>';
997 include = p;
998 while (*p && (*p != quote)) p++;
999 if (!*p || (quote == '"' && p[-1] != '\\'))
1000 fatal_error( "malformed #include directive inside cpp_quote\n" );
1001 if (quote == '"') p--; /* remove backslash */
1002 *p = 0;
1003 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1004 continue;
1007 parse_cpp_directive( source, p );
1011 /*******************************************************************
1012 * parse_c_file
1014 static void parse_c_file( struct file *source, FILE *file )
1016 char *buffer;
1018 input_line = 0;
1019 while ((buffer = get_line( file )))
1021 parse_cpp_directive( source, buffer );
1026 /*******************************************************************
1027 * parse_rc_file
1029 static void parse_rc_file( struct file *source, FILE *file )
1031 char *buffer, *include;
1033 input_line = 0;
1034 while ((buffer = get_line( file )))
1036 char quote;
1037 char *p = buffer;
1038 while (*p && isspace(*p)) p++;
1040 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1042 p += 2;
1043 while (*p && isspace(*p)) p++;
1044 if (strncmp( p, "@makedep:", 9 )) continue;
1045 p += 9;
1046 while (*p && isspace(*p)) p++;
1047 quote = '"';
1048 if (*p == quote)
1050 include = ++p;
1051 while (*p && *p != quote) p++;
1053 else
1055 include = p;
1056 while (*p && !isspace(*p) && *p != '*') p++;
1058 if (!*p)
1059 fatal_error( "malformed makedep comment\n" );
1060 *p = 0;
1061 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1062 continue;
1065 parse_cpp_directive( source, buffer );
1070 /*******************************************************************
1071 * parse_in_file
1073 static void parse_in_file( struct file *source, FILE *file )
1075 char *p, *buffer;
1077 /* make sure it gets rebuilt when the version changes */
1078 add_dependency( source, "config.h", INCL_SYSTEM );
1080 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1082 input_line = 0;
1083 while ((buffer = get_line( file )))
1085 if (strncmp( buffer, ".TH", 3 )) continue;
1086 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1087 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1088 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1089 source->args = xstrdup( p );
1090 return;
1095 /*******************************************************************
1096 * parse_sfd_file
1098 static void parse_sfd_file( struct file *source, FILE *file )
1100 char *p, *eol, *buffer;
1102 input_line = 0;
1103 while ((buffer = get_line( file )))
1105 if (strncmp( buffer, "UComments:", 10 )) continue;
1106 p = buffer + 10;
1107 while (*p == ' ') p++;
1108 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1110 p++;
1111 buffer[strlen(buffer) - 1] = 0;
1113 while ((eol = strstr( p, "+AAoA" )))
1115 *eol = 0;
1116 while (*p && isspace(*p)) p++;
1117 if (*p++ == '#')
1119 while (*p && isspace(*p)) p++;
1120 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1122 p = eol + 5;
1124 while (*p && isspace(*p)) p++;
1125 if (*p++ != '#') return;
1126 while (*p && isspace(*p)) p++;
1127 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1128 return;
1133 static const struct
1135 const char *ext;
1136 void (*parse)( struct file *file, FILE *f );
1137 } parse_functions[] =
1139 { ".c", parse_c_file },
1140 { ".h", parse_c_file },
1141 { ".inl", parse_c_file },
1142 { ".l", parse_c_file },
1143 { ".m", parse_c_file },
1144 { ".rh", parse_c_file },
1145 { ".x", parse_c_file },
1146 { ".y", parse_c_file },
1147 { ".idl", parse_idl_file },
1148 { ".rc", parse_rc_file },
1149 { ".in", parse_in_file },
1150 { ".sfd", parse_sfd_file }
1153 /*******************************************************************
1154 * load_file
1156 static struct file *load_file( const char *name )
1158 struct file *file;
1159 FILE *f;
1160 unsigned int i, hash = hash_filename( name );
1162 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1163 if (!strcmp( name, file->name )) return file;
1165 if (!(f = fopen( name, "r" ))) return NULL;
1167 file = add_file( name );
1168 list_add_tail( &files[hash], &file->entry );
1169 input_file_name = file->name;
1170 input_line = 0;
1172 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1174 if (!strendswith( name, parse_functions[i].ext )) continue;
1175 parse_functions[i].parse( file, f );
1176 break;
1179 fclose( f );
1180 input_file_name = NULL;
1182 return file;
1186 /*******************************************************************
1187 * open_include_path_file
1189 * Open a file from a directory on the include path.
1191 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1192 const char *name, char **filename )
1194 char *src_path = concat_paths( dir, name );
1195 struct file *ret = load_file( src_path );
1197 if (ret) *filename = src_path;
1198 return ret;
1202 /*******************************************************************
1203 * open_file_same_dir
1205 * Open a file in the same directory as the parent.
1207 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1209 char *src_path = replace_filename( parent->file->name, name );
1210 struct file *ret = load_file( src_path );
1212 if (ret) *filename = replace_filename( parent->filename, name );
1213 return ret;
1217 /*******************************************************************
1218 * open_same_dir_generated_file
1220 * Open a generated_file in the same directory as the parent.
1222 static struct file *open_same_dir_generated_file( const struct makefile *make,
1223 const struct incl_file *parent, struct incl_file *file,
1224 const char *ext, const char *src_ext )
1226 char *filename;
1227 struct file *ret = NULL;
1229 if (strendswith( file->name, ext ) &&
1230 (ret = open_file_same_dir( parent, replace_extension( file->name, ext, src_ext ), &filename )))
1232 file->sourcename = filename;
1233 file->filename = obj_dir_path( make, replace_filename( parent->name, file->name ));
1235 return ret;
1239 /*******************************************************************
1240 * open_local_file
1242 * Open a file in the source directory of the makefile.
1244 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1246 char *src_path = src_dir_path( make, path );
1247 struct file *ret = load_file( src_path );
1249 /* if not found, try parent dir */
1250 if (!ret && make->parent_dir)
1252 free( src_path );
1253 path = strmake( "%s/%s", make->parent_dir, path );
1254 src_path = src_dir_path( make, path );
1255 ret = load_file( src_path );
1258 if (ret) *filename = src_path;
1259 return ret;
1263 /*******************************************************************
1264 * open_local_generated_file
1266 * Open a generated file in the directory of the makefile.
1268 static struct file *open_local_generated_file( const struct makefile *make, struct incl_file *file,
1269 const char *ext, const char *src_ext )
1271 char *filename;
1272 struct file *ret = NULL;
1274 if (strendswith( file->name, ext ) &&
1275 (ret = open_local_file( make, replace_extension( file->name, ext, src_ext ), &filename )))
1277 file->sourcename = filename;
1278 file->filename = obj_dir_path( make, file->name );
1280 return ret;
1284 /*******************************************************************
1285 * open_global_file
1287 * Open a file in the top-level source directory.
1289 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1291 char *src_path = root_src_dir_path( path );
1292 struct file *ret = load_file( src_path );
1294 if (ret) *filename = src_path;
1295 return ret;
1299 /*******************************************************************
1300 * open_global_header
1302 * Open a file in the global include source directory.
1304 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1306 if (!strncmp( path, "../", 3 )) return NULL;
1307 return open_global_file( make, strmake( "include/%s", path ), filename );
1311 /*******************************************************************
1312 * open_global_generated_file
1314 * Open a generated file in the top-level source directory.
1316 static struct file *open_global_generated_file( const struct makefile *make, struct incl_file *file,
1317 const char *ext, const char *src_ext )
1319 char *filename;
1320 struct file *ret = NULL;
1322 if (strendswith( file->name, ext ) &&
1323 (ret = open_global_header( make, replace_extension( file->name, ext, src_ext ), &filename )))
1325 file->sourcename = filename;
1326 file->filename = strmake( "include/%s", file->name );
1328 return ret;
1332 /*******************************************************************
1333 * open_src_file
1335 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1337 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1339 if (!file) fatal_perror( "open %s", pFile->name );
1340 return file;
1344 /*******************************************************************
1345 * find_importlib_module
1347 static struct makefile *find_importlib_module( const char *name )
1349 unsigned int i, len;
1351 for (i = 0; i < subdirs.count; i++)
1353 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1354 len = strlen(submakes[i]->obj_dir);
1355 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1356 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1358 return NULL;
1362 /*******************************************************************
1363 * open_include_file
1365 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1367 struct file *file = NULL;
1368 unsigned int i, len;
1370 errno = ENOENT;
1372 /* check for generated files */
1373 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1374 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1375 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1376 if (convert && rsvg && icotool)
1378 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1379 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1380 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1383 /* check for extra targets */
1384 if (strarray_exists( &make->extra_targets, pFile->name ))
1386 pFile->sourcename = src_dir_path( make, pFile->name );
1387 pFile->filename = obj_dir_path( make, pFile->name );
1388 return NULL;
1391 /* now try in source dir */
1392 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1394 /* check for global importlib (module dependency) */
1395 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1397 pFile->filename = pFile->name;
1398 return NULL;
1401 /* check for generated files in global includes */
1402 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1403 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1404 if (strendswith( pFile->name, "tmpl.h" ) &&
1405 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1407 /* check in global includes source dir */
1408 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1410 /* check in global msvcrt includes */
1411 if (pFile->use_msvcrt &&
1412 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1413 return file;
1415 /* now search in include paths */
1416 for (i = 0; i < make->include_paths.count; i++)
1418 const char *dir = make->include_paths.str[i];
1420 if (root_src_dir)
1422 len = strlen( root_src_dir );
1423 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1425 while (dir[len] == '/') len++;
1426 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1429 else
1431 if (*dir == '/') continue;
1432 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1434 if (!file) continue;
1435 pFile->is_external = 1;
1436 return file;
1439 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1441 /* try in src file directory */
1442 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1443 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1444 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1446 pFile->is_external = pFile->included_by->is_external;
1447 return file;
1450 if (make->extlib) return NULL; /* ignore missing files in external libs */
1452 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1453 perror( pFile->name );
1454 pFile = pFile->included_by;
1455 while (pFile && pFile->included_by)
1457 const char *parent = pFile->included_by->sourcename;
1458 if (!parent) parent = pFile->included_by->file->name;
1459 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1460 parent, pFile->included_line, pFile->name );
1461 pFile = pFile->included_by;
1463 exit(1);
1467 /*******************************************************************
1468 * add_all_includes
1470 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1472 unsigned int i;
1474 for (i = 0; i < file->deps_count; i++)
1476 switch (file->deps[i].type)
1478 case INCL_NORMAL:
1479 case INCL_IMPORT:
1480 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1481 break;
1482 case INCL_IMPORTLIB:
1483 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1484 break;
1485 case INCL_SYSTEM:
1486 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1487 break;
1488 case INCL_CPP_QUOTE:
1489 case INCL_CPP_QUOTE_SYSTEM:
1490 break;
1496 /*******************************************************************
1497 * parse_file
1499 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1501 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1503 if (!file) return;
1505 source->file = file;
1506 source->files_count = 0;
1507 source->files_size = file->deps_count;
1508 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1510 if (strendswith( file->name, ".m" )) file->flags |= FLAG_C_UNIX;
1511 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1512 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1514 if (source->sourcename)
1516 if (strendswith( source->sourcename, ".idl" ))
1518 unsigned int i;
1520 /* generated .h file always includes these */
1521 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1522 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1523 for (i = 0; i < file->deps_count; i++)
1525 switch (file->deps[i].type)
1527 case INCL_IMPORT:
1528 if (strendswith( file->deps[i].name, ".idl" ))
1529 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1530 file->deps[i].line, INCL_NORMAL );
1531 else
1532 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1533 break;
1534 case INCL_CPP_QUOTE:
1535 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1536 break;
1537 case INCL_CPP_QUOTE_SYSTEM:
1538 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1539 break;
1540 case INCL_NORMAL:
1541 case INCL_SYSTEM:
1542 case INCL_IMPORTLIB:
1543 break;
1546 return;
1548 if (strendswith( source->sourcename, ".y" ))
1549 return; /* generated .tab.h doesn't include anything */
1552 add_all_includes( make, source, file );
1556 /*******************************************************************
1557 * add_src_file
1559 * Add a source file to the list.
1561 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1563 struct incl_file *file;
1565 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1566 file = xmalloc( sizeof(*file) );
1567 memset( file, 0, sizeof(*file) );
1568 file->name = xstrdup(name);
1569 file->use_msvcrt = make->use_msvcrt;
1570 file->is_external = !!make->extlib;
1571 list_add_tail( &make->sources, &file->entry );
1572 parse_file( make, file, 1 );
1573 return file;
1577 /*******************************************************************
1578 * open_input_makefile
1580 static FILE *open_input_makefile( const struct makefile *make )
1582 FILE *ret;
1584 if (make->obj_dir)
1585 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1586 else
1587 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1589 input_line = 0;
1590 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1591 return ret;
1595 /*******************************************************************
1596 * get_make_variable
1598 static const char *get_make_variable( const struct makefile *make, const char *name )
1600 const char *ret;
1602 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1603 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1604 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1605 return NULL;
1609 /*******************************************************************
1610 * get_expanded_make_variable
1612 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1614 const char *var;
1615 char *p, *end, *expand, *tmp;
1617 var = get_make_variable( make, name );
1618 if (!var) return NULL;
1620 p = expand = xstrdup( var );
1621 while ((p = strchr( p, '$' )))
1623 if (p[1] == '(')
1625 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1626 *end++ = 0;
1627 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1628 var = get_make_variable( make, p + 2 );
1629 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1630 /* switch to the new string */
1631 p = tmp + (p - expand);
1632 free( expand );
1633 expand = tmp;
1635 else if (p[1] == '{') /* don't expand ${} variables */
1637 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1638 p = end + 1;
1640 else if (p[1] == '$')
1642 p += 2;
1644 else fatal_error( "syntax error in '%s'\n", expand );
1647 /* consider empty variables undefined */
1648 p = expand;
1649 while (*p && isspace(*p)) p++;
1650 if (*p) return expand;
1651 free( expand );
1652 return NULL;
1656 /*******************************************************************
1657 * get_expanded_make_var_array
1659 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1661 struct strarray ret = empty_strarray;
1662 char *value, *token;
1664 if ((value = get_expanded_make_variable( make, name )))
1665 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1666 strarray_add( &ret, token );
1667 return ret;
1671 /*******************************************************************
1672 * get_expanded_file_local_var
1674 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1675 const char *name )
1677 char *p, *var = strmake( "%s_%s", file, name );
1679 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1680 return get_expanded_make_var_array( make, var );
1684 /*******************************************************************
1685 * set_make_variable
1687 static int set_make_variable( struct strarray *array, const char *assignment )
1689 char *p, *name;
1691 p = name = xstrdup( assignment );
1692 while (isalnum(*p) || *p == '_') p++;
1693 if (name == p) return 0; /* not a variable */
1694 if (isspace(*p))
1696 *p++ = 0;
1697 while (isspace(*p)) p++;
1699 if (*p != '=') return 0; /* not an assignment */
1700 *p++ = 0;
1701 while (isspace(*p)) p++;
1703 strarray_set_value( array, name, p );
1704 return 1;
1708 /*******************************************************************
1709 * parse_makefile
1711 static struct makefile *parse_makefile( const char *path )
1713 char *buffer;
1714 FILE *file;
1715 struct makefile *make = xmalloc( sizeof(*make) );
1717 memset( make, 0, sizeof(*make) );
1718 make->obj_dir = path;
1719 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1721 file = open_input_makefile( make );
1722 while ((buffer = get_line( file )))
1724 if (!strncmp( buffer, separator, strlen(separator) )) break;
1725 if (*buffer == '\t') continue; /* command */
1726 while (isspace( *buffer )) buffer++;
1727 if (*buffer == '#') continue; /* comment */
1728 set_make_variable( &make->vars, buffer );
1730 fclose( file );
1731 input_file_name = NULL;
1732 return make;
1736 /*******************************************************************
1737 * add_generated_sources
1739 static void add_generated_sources( struct makefile *make )
1741 unsigned int i;
1742 struct incl_file *source, *next, *file;
1743 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1745 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1747 if (source->file->flags & FLAG_IDL_CLIENT)
1749 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1750 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1751 add_all_includes( make, file, file->file );
1753 if (source->file->flags & FLAG_IDL_SERVER)
1755 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1756 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1757 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1758 add_all_includes( make, file, file->file );
1760 if (source->file->flags & FLAG_IDL_IDENT)
1762 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1763 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1764 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1765 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1766 add_all_includes( make, file, file->file );
1768 if (source->file->flags & FLAG_IDL_PROXY)
1770 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1771 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1772 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1773 add_all_includes( make, file, file->file );
1774 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1775 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1776 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1777 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1778 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1779 add_all_includes( make, file, file->file );
1781 if (source->file->flags & FLAG_IDL_TYPELIB)
1783 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL );
1785 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1787 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1789 if (source->file->flags & FLAG_IDL_REGISTER)
1791 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1793 if (source->file->flags & FLAG_IDL_HEADER)
1795 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1797 if (!source->file->flags && strendswith( source->name, ".idl" ))
1799 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1801 if (strendswith( source->name, ".x" ))
1803 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1805 if (strendswith( source->name, ".y" ))
1807 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1808 /* steal the includes list from the source file */
1809 file->files_count = source->files_count;
1810 file->files_size = source->files_size;
1811 file->files = source->files;
1812 source->files_count = source->files_size = 0;
1813 source->files = NULL;
1815 if (strendswith( source->name, ".l" ))
1817 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1818 /* steal the includes list from the source file */
1819 file->files_count = source->files_count;
1820 file->files_size = source->files_size;
1821 file->files = source->files;
1822 source->files_count = source->files_size = 0;
1823 source->files = NULL;
1825 if (source->file->flags & FLAG_C_IMPLIB)
1827 if (!make->staticimplib && make->importlib && *dll_ext)
1828 make->staticimplib = strmake( "lib%s.a", make->importlib );
1830 if (strendswith( source->name, ".po" ))
1832 if (!make->disabled)
1833 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1835 if (strendswith( source->name, ".spec" ))
1837 char *obj = replace_extension( source->name, ".spec", "" );
1838 strarray_addall_uniq( &make->extra_imports,
1839 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1842 if (make->testdll)
1844 file = add_generated_source( make, "testlist.o", "testlist.c" );
1845 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1846 add_all_includes( make, file, file->file );
1848 for (i = 0; i < objs.count; i++)
1850 /* default to .c for unknown extra object files */
1851 if (strendswith( objs.str[i], ".o" ))
1853 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1854 file->file->flags |= FLAG_C_UNIX;
1855 file->use_msvcrt = 0;
1857 else if (strendswith( objs.str[i], ".res" ))
1858 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1859 else
1860 add_generated_source( make, objs.str[i], NULL );
1865 /*******************************************************************
1866 * create_dir
1868 static void create_dir( const char *dir )
1870 char *p, *path;
1872 p = path = xstrdup( dir );
1873 while ((p = strchr( p, '/' )))
1875 *p = 0;
1876 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1877 *p++ = '/';
1878 while (*p == '/') p++;
1880 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1881 free( path );
1885 /*******************************************************************
1886 * create_file_directories
1888 * Create the base directories of all the files.
1890 static void create_file_directories( const struct makefile *make, struct strarray files )
1892 struct strarray subdirs = empty_strarray;
1893 unsigned int i;
1894 char *dir;
1896 for (i = 0; i < files.count; i++)
1898 if (!strchr( files.str[i], '/' )) continue;
1899 dir = obj_dir_path( make, files.str[i] );
1900 *strrchr( dir, '/' ) = 0;
1901 strarray_add_uniq( &subdirs, dir );
1904 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1908 /*******************************************************************
1909 * output_filenames_obj_dir
1911 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1913 unsigned int i;
1915 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1919 /*******************************************************************
1920 * get_dependencies
1922 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1924 unsigned int i;
1926 if (!file->filename) return;
1928 if (file != source)
1930 if (file->owner == source) return; /* already processed */
1931 if (file->type == INCL_IMPORTLIB)
1933 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1934 return; /* library is imported only when building a typelib */
1935 strarray_add( &source->importlibdeps, file->filename );
1937 else strarray_add( &source->dependencies, file->filename );
1938 file->owner = source;
1940 /* sanity checks */
1941 if (!strcmp( file->filename, "include/config.h" ) &&
1942 file != source->files[0] && !source->is_external)
1944 input_file_name = source->filename;
1945 input_line = 0;
1946 for (i = 0; i < source->file->deps_count; i++)
1948 if (!strcmp( source->file->deps[i].name, file->name ))
1949 input_line = source->file->deps[i].line;
1951 fatal_error( "%s must be included before other headers\n", file->name );
1955 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
1959 /*******************************************************************
1960 * get_local_dependencies
1962 * Get the local dependencies of a given target.
1964 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1965 struct strarray targets )
1967 unsigned int i;
1968 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
1970 for (i = 0; i < deps.count; i++)
1972 if (strarray_exists( &targets, deps.str[i] ))
1973 deps.str[i] = obj_dir_path( make, deps.str[i] );
1974 else
1975 deps.str[i] = src_dir_path( make, deps.str[i] );
1977 return deps;
1981 /*******************************************************************
1982 * get_static_lib
1984 * Check if makefile builds the named static library and return the full lib path.
1986 static const char *get_static_lib( const struct makefile *make, const char *name )
1988 if (!make->staticlib) return NULL;
1989 if (make->disabled) return NULL;
1990 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
1991 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
1992 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
1993 return obj_dir_path( make, make->staticlib );
1997 /*******************************************************************
1998 * get_native_unix_lib
2000 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2002 if (!make->native_unix_lib) return NULL;
2003 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2004 if (make->unixlib[strlen(name)] != '.') return NULL;
2005 return obj_dir_path( make, make->unixlib );
2009 /*******************************************************************
2010 * get_parent_makefile
2012 static struct makefile *get_parent_makefile( struct makefile *make )
2014 char *dir, *p;
2015 unsigned int i;
2017 if (!make->obj_dir) return NULL;
2018 dir = xstrdup( make->obj_dir );
2019 if (!(p = strrchr( dir, '/' ))) return NULL;
2020 *p = 0;
2021 for (i = 0; i < subdirs.count; i++)
2022 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2023 return NULL;
2027 /*******************************************************************
2028 * needs_delay_lib
2030 static int needs_delay_lib( const struct makefile *make )
2032 if (delay_load_flag) return 0;
2033 if (*dll_ext && !crosstarget) return 0;
2034 if (!make->importlib) return 0;
2035 return strarray_exists( &delay_import_libs, make->importlib );
2039 /*******************************************************************
2040 * add_unix_libraries
2042 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2044 struct strarray ret = empty_strarray;
2045 struct strarray all_libs = empty_strarray;
2046 unsigned int i, j;
2048 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2049 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2051 for (i = 0; i < all_libs.count; i++)
2053 const char *lib = NULL;
2055 if (!strncmp( all_libs.str[i], "-l", 2 ))
2057 for (j = 0; j < subdirs.count; j++)
2059 if (make == submakes[j]) continue;
2060 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2063 if (lib)
2065 strarray_add( deps, lib );
2066 strarray_add( &ret, lib );
2068 else strarray_add( &ret, all_libs.str[i] );
2071 strarray_addall( &ret, libs );
2072 return ret;
2076 /*******************************************************************
2077 * is_crt_module
2079 static int is_crt_module( const char *file )
2081 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2085 /*******************************************************************
2086 * get_default_crt
2088 static const char *get_default_crt( const struct makefile *make )
2090 if (!make->use_msvcrt) return NULL;
2091 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2092 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2096 /*******************************************************************
2097 * get_crt_define
2099 static const char *get_crt_define( const struct makefile *make )
2101 const char *crt_dll = NULL;
2102 unsigned int i, version = 0;
2104 for (i = 0; i < make->imports.count; i++)
2106 if (!is_crt_module( make->imports.str[i] )) continue;
2107 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2108 crt_dll, make->imports.str[i] );
2109 crt_dll = make->imports.str[i];
2112 if (!crt_dll)
2114 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2115 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2117 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2118 sscanf( crt_dll, "msvcr%u", &version );
2119 return strmake( "-D_MSVCR_VER=%u", version );
2123 /*******************************************************************
2124 * add_default_imports
2126 static struct strarray add_default_imports( const struct makefile *make, struct strarray imports )
2128 struct strarray ret = empty_strarray;
2129 const char *crt_dll = get_default_crt( make );
2130 unsigned int i;
2132 for (i = 0; i < imports.count; i++)
2134 if (is_crt_module( imports.str[i] )) crt_dll = imports.str[i];
2135 else strarray_add( &ret, imports.str[i] );
2138 strarray_add( &ret, "winecrt0" );
2139 if (crt_dll) strarray_add( &ret, crt_dll );
2141 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2142 strarray_add( &ret, "kernel" );
2144 strarray_add( &ret, "kernel32" );
2145 strarray_add( &ret, "ntdll" );
2146 return ret;
2150 /*******************************************************************
2151 * add_import_libs
2153 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2154 struct strarray imports, int delay, int is_cross )
2156 struct strarray ret = empty_strarray;
2157 unsigned int i, j;
2159 for (i = 0; i < imports.count; i++)
2161 const char *name = imports.str[i];
2162 const char *lib = NULL;
2164 if (name[0] == '-')
2166 switch (name[1])
2168 case 'L': strarray_add( &ret, name ); continue;
2169 case 'l': name += 2; break;
2170 default: continue;
2173 else name = get_base_name( name );
2175 for (j = 0; j < subdirs.count; j++)
2177 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2178 lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
2179 else
2180 lib = get_static_lib( submakes[j], name );
2181 if (lib) break;
2184 if (lib)
2186 const char *ext = NULL;
2188 if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
2189 else if (is_cross) ext = ".cross.a";
2190 if (ext) lib = replace_extension( lib, ".a", ext );
2191 strarray_add_uniq( deps, lib );
2192 strarray_add( &ret, lib );
2194 else strarray_add( &ret, strmake( "-l%s", name ));
2196 return ret;
2200 /*******************************************************************
2201 * add_install_rule
2203 static void add_install_rule( struct makefile *make, const char *target,
2204 const char *file, const char *dest )
2206 if (strarray_exists( &make->install_lib, target ) ||
2207 strarray_exists( &top_install_lib, make->obj_dir ) ||
2208 strarray_exists( &top_install_lib, obj_dir_path( make, target )))
2210 strarray_add( &make->install_rules[INSTALL_LIB], file );
2211 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2213 else if (strarray_exists( &make->install_dev, target ) ||
2214 strarray_exists( &top_install_dev, make->obj_dir ) ||
2215 strarray_exists( &top_install_dev, obj_dir_path( make, target )))
2217 strarray_add( &make->install_rules[INSTALL_DEV], file );
2218 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2223 /*******************************************************************
2224 * get_include_install_path
2226 * Determine the installation path for a given include file.
2228 static const char *get_include_install_path( const char *name )
2230 if (!strncmp( name, "wine/", 5 )) return name + 5;
2231 if (!strncmp( name, "msvcrt/", 7 )) return name;
2232 return strmake( "windows/%s", name );
2236 /*******************************************************************
2237 * get_shared_library_name
2239 * Determine possible names for a shared library with a version number.
2241 static struct strarray get_shared_lib_names( const char *libname )
2243 struct strarray ret = empty_strarray;
2244 const char *ext, *p;
2245 char *name, *first, *second;
2246 size_t len = 0;
2248 strarray_add( &ret, libname );
2250 for (p = libname; (p = strchr( p, '.' )); p++)
2251 if ((len = strspn( p + 1, "0123456789." ))) break;
2253 if (!len) return ret;
2254 ext = p + 1 + len;
2255 if (*ext && ext[-1] == '.') ext--;
2257 /* keep only the first group of digits */
2258 name = xstrdup( libname );
2259 first = name + (p - libname);
2260 if ((second = strchr( first + 1, '.' )))
2262 strcpy( second, ext );
2263 strarray_add( &ret, xstrdup( name ));
2265 return ret;
2269 /*******************************************************************
2270 * get_source_defines
2272 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2273 const char *obj )
2275 unsigned int i;
2276 struct strarray ret = empty_strarray;
2278 strarray_addall( &ret, make->include_args );
2279 if (source->use_msvcrt)
2280 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2281 for (i = 0; i < make->include_paths.count; i++)
2282 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2283 strarray_addall( &ret, make->define_args );
2284 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2285 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2286 return ret;
2290 /*******************************************************************
2291 * remove_warning_flags
2293 static struct strarray remove_warning_flags( struct strarray flags )
2295 unsigned int i;
2296 struct strarray ret = empty_strarray;
2298 for (i = 0; i < flags.count; i++)
2299 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2300 strarray_add( &ret, flags.str[i] );
2301 return ret;
2305 /*******************************************************************
2306 * get_debug_file
2308 static const char *get_debug_file( struct makefile *make, const char *name )
2310 const char *debug_file = NULL;
2311 if (!make->is_cross || !crossdebug) return NULL;
2312 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2313 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2314 if (debug_file) strarray_add( &make->debug_files, debug_file );
2315 return debug_file;
2319 /*******************************************************************
2320 * cmd_prefix
2322 static const char *cmd_prefix( const char *cmd )
2324 if (!silent_rules) return "";
2325 return strmake( "$(quiet_%s)", cmd );
2329 /*******************************************************************
2330 * output_winegcc_command
2332 static void output_winegcc_command( struct makefile *make, int is_cross )
2334 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2335 output_filename( "--wine-objdir ." );
2336 if (tools_dir)
2338 output_filename( "--winebuild" );
2339 output_filename( tools_path( make, "winebuild" ));
2341 if (is_cross)
2343 output_filename( "-b" );
2344 output_filename( crosstarget );
2345 output_filename( "--lib-suffix=.cross.a" );
2347 else
2349 output_filenames( target_flags );
2350 output_filenames( lddll_flags );
2355 /*******************************************************************
2356 * output_symlink_rule
2358 * Output a rule to create a symlink.
2360 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2362 const char *name = strrchr( link_name, '/' );
2363 char *dir = NULL;
2365 if (name)
2367 dir = xstrdup( link_name );
2368 dir[name - link_name] = 0;
2371 output( "\t%s", cmd_prefix( "LN" ));
2372 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2373 output( "rm -f %s && ", link_name );
2375 /* dest path with a directory needs special handling if ln -s isn't supported */
2376 if (dir && strcmp( ln_s, "ln -s" ))
2377 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2378 else
2379 output( "%s %s %s\n", ln_s, src_name, link_name );
2381 free( dir );
2385 /*******************************************************************
2386 * output_srcdir_symlink
2388 * Output rule to create a symlink back to the source directory, for source files
2389 * that are needed at run-time.
2391 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2393 char *src_file, *dst_file, *src_name;
2395 if (!make->src_dir) return;
2396 src_file = src_dir_path( make, obj );
2397 dst_file = obj_dir_path( make, obj );
2398 output( "%s: %s\n", dst_file, src_file );
2400 src_name = src_file;
2401 if (src_name[0] != '/' && make->obj_dir)
2402 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2404 output_symlink_rule( src_name, dst_file, 0 );
2405 strarray_add( &make->all_targets, obj );
2409 /*******************************************************************
2410 * output_install_commands
2412 static void output_install_commands( struct makefile *make, struct strarray files )
2414 unsigned int i;
2415 char *install_sh = root_src_dir_path( "tools/install-sh" );
2417 for (i = 0; i < files.count; i += 2)
2419 const char *file = files.str[i];
2420 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2422 switch (*files.str[i + 1])
2424 case 'c': /* cross-compiled program */
2425 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2426 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2427 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2428 break;
2429 case 'd': /* data file */
2430 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2431 install_sh, obj_dir_path( make, file ), dest );
2432 break;
2433 case 'D': /* data file in source dir */
2434 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2435 install_sh, src_dir_path( make, file ), dest );
2436 break;
2437 case 'p': /* program file */
2438 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2439 install_sh, obj_dir_path( make, file ), dest );
2440 break;
2441 case 's': /* script */
2442 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2443 install_sh, obj_dir_path( make, file ), dest );
2444 break;
2445 case 'S': /* script in source dir */
2446 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2447 install_sh, src_dir_path( make, file ), dest );
2448 break;
2449 case 't': /* script in tools dir */
2450 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2451 install_sh, tools_dir_path( make, files.str[i] ), dest );
2452 break;
2453 case 'y': /* symlink */
2454 output_symlink_rule( files.str[i], dest, 1 );
2455 break;
2456 default:
2457 assert(0);
2459 strarray_add( &make->uninstall_files, dest );
2464 /*******************************************************************
2465 * output_install_rules
2467 * Rules are stored as a (file,dest) pair of values.
2468 * The first char of dest indicates the type of install.
2470 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2472 unsigned int i;
2473 struct strarray files = make->install_rules[rules];
2474 struct strarray targets = empty_strarray;
2476 if (!files.count) return;
2478 for (i = 0; i < files.count; i += 2)
2480 const char *file = files.str[i];
2481 switch (*files.str[i + 1])
2483 case 'c': /* cross-compiled program */
2484 case 'd': /* data file */
2485 case 'p': /* program file */
2486 case 's': /* script */
2487 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2488 break;
2489 case 't': /* script in tools dir */
2490 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2491 break;
2495 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
2496 output_filenames( targets );
2497 output( "\n" );
2498 output_install_commands( make, files );
2499 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2500 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
2504 static int cmp_string_length( const char **a, const char **b )
2506 int paths_a = 0, paths_b = 0;
2507 const char *p;
2509 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2510 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2511 if (paths_b != paths_a) return paths_b - paths_a;
2512 return strcmp( *a, *b );
2515 /*******************************************************************
2516 * output_uninstall_rules
2518 static void output_uninstall_rules( struct makefile *make )
2520 static const char *dirs_order[] =
2521 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2523 struct strarray uninstall_dirs = empty_strarray;
2524 unsigned int i, j;
2526 if (!make->uninstall_files.count) return;
2527 output( "uninstall::\n" );
2528 output_rm_filenames( make->uninstall_files );
2529 strarray_add_uniq( &make->phony_targets, "uninstall" );
2531 if (!subdirs.count) return;
2532 for (i = 0; i < make->uninstall_files.count; i++)
2534 char *dir = xstrdup( make->uninstall_files.str[i] );
2535 while (strchr( dir, '/' ))
2537 *strrchr( dir, '/' ) = 0;
2538 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2541 strarray_qsort( &uninstall_dirs, cmp_string_length );
2542 output( "\t-rmdir" );
2543 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2545 for (j = 0; j < uninstall_dirs.count; j++)
2547 if (!uninstall_dirs.str[j]) continue;
2548 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2549 continue;
2550 output_filename( uninstall_dirs.str[j] );
2551 uninstall_dirs.str[j] = NULL;
2554 for (j = 0; j < uninstall_dirs.count; j++)
2555 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2556 output( "\n" );
2560 /*******************************************************************
2561 * output_po_files
2563 static void output_po_files( struct makefile *make )
2565 const char *po_dir = src_dir_path( make, "po" );
2566 unsigned int i;
2568 if (linguas.count)
2570 for (i = 0; i < linguas.count; i++)
2571 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2572 output( ": %s/wine.pot\n", po_dir );
2573 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2574 cmd_prefix( "MSG" ), po_dir );
2575 output( "po/all:" );
2576 for (i = 0; i < linguas.count; i++)
2577 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2578 output( "\n" );
2580 output( "%s/wine.pot:", po_dir );
2581 output_filenames( make->pot_files );
2582 output( "\n" );
2583 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2584 output_filenames( make->pot_files );
2585 output( "\n" );
2586 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2590 /*******************************************************************
2591 * output_source_y
2593 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2595 /* add source file dependency for parallel makes */
2596 char *header = strmake( "%s.tab.h", obj );
2598 if (find_include_file( make, header ))
2600 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2601 output( "\t%s%s -o %s.tab.c -d %s\n",
2602 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2603 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2604 source->filename, obj_dir_path( make, header ));
2605 strarray_add( &make->clean_files, header );
2607 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2609 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2613 /*******************************************************************
2614 * output_source_l
2616 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2618 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2619 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2623 /*******************************************************************
2624 * output_source_h
2626 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2628 if (source->file->flags & FLAG_GENERATED)
2629 strarray_add( &make->all_targets, source->name );
2630 else
2631 add_install_rule( make, source->name, source->name,
2632 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2636 /*******************************************************************
2637 * output_source_rc
2639 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2641 struct strarray defines = get_source_defines( make, source, obj );
2642 const char *po_dir = NULL;
2643 unsigned int i;
2645 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2646 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2647 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2648 if (source->file->flags & FLAG_RC_PO)
2650 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2651 output( "%s.pot ", obj_dir_path( make, obj ) );
2653 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2654 output_filename( tools_path( make, "wrc" ));
2655 if (make->src_dir) output_filename( "nls/locale.nls" );
2656 output_filenames( source->dependencies );
2657 output( "\n" );
2658 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2659 if (make->is_win16) output_filename( "-m16" );
2660 output_filename( "--nostdinc" );
2661 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2662 output_filenames( defines );
2663 output_filename( source->filename );
2664 output( "\n" );
2665 if (po_dir)
2667 output( "%s.res:", obj_dir_path( make, obj ));
2668 for (i = 0; i < linguas.count; i++)
2669 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2670 output( "\n" );
2675 /*******************************************************************
2676 * output_source_mc
2678 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2680 unsigned int i;
2681 char *obj_path = obj_dir_path( make, obj );
2683 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2684 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2685 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2686 output_filename( tools_path( make, "wmc" ));
2687 output_filenames( source->dependencies );
2688 output( "\n" );
2689 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2690 if (linguas.count)
2692 output_filename( "--po-dir=po" );
2693 output( "\n" );
2694 output( "%s.res:", obj_dir_path( make, obj ));
2695 for (i = 0; i < linguas.count; i++)
2696 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2698 output( "\n" );
2702 /*******************************************************************
2703 * output_source_res
2705 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2707 strarray_add( &make->res_files, source->name );
2711 /*******************************************************************
2712 * output_source_idl
2714 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2716 struct strarray defines = get_source_defines( make, source, obj );
2717 struct strarray targets = empty_strarray;
2718 char *dest;
2719 unsigned int i;
2721 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2722 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2724 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2726 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2727 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2728 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2729 strarray_add( &targets, dest );
2731 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2732 if (source->file->flags & FLAG_INSTALL)
2734 add_install_rule( make, source->name, xstrdup( source->name ),
2735 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2736 if (source->file->flags & FLAG_IDL_HEADER)
2737 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2738 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2740 if (!targets.count) return;
2742 output_filenames_obj_dir( make, targets );
2743 output( ": %s\n", tools_path( make, "widl" ));
2744 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2745 output_filenames( target_flags );
2746 output_filename( "--nostdinc" );
2747 output_filename( "-Ldlls/\\*" );
2748 output_filenames( defines );
2749 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2750 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2751 output_filename( source->filename );
2752 output( "\n" );
2753 output_filenames_obj_dir( make, targets );
2754 output( ": %s", source->filename );
2755 output_filenames( source->dependencies );
2756 for (i = 0; i < source->importlibdeps.count; i++)
2758 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2759 output_filename( obj_dir_path( submake, submake->module ));
2761 output( "\n" );
2765 /*******************************************************************
2766 * output_source_x
2768 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2770 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2771 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2772 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2773 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2774 if (source->file->flags & FLAG_INSTALL)
2776 add_install_rule( make, source->name, source->name,
2777 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2778 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2779 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2784 /*******************************************************************
2785 * output_source_sfd
2787 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2789 unsigned int i;
2790 char *ttf_obj = strmake( "%s.ttf", obj );
2791 char *ttf_file = src_dir_path( make, ttf_obj );
2793 if (fontforge && !make->src_dir)
2795 output( "%s: %s\n", ttf_file, source->filename );
2796 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2797 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2798 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2799 strarray_add( &make->maintainerclean_files, ttf_obj );
2801 if (source->file->flags & FLAG_INSTALL)
2803 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2804 output_srcdir_symlink( make, ttf_obj );
2807 if (source->file->flags & FLAG_SFD_FONTS)
2809 struct strarray *array = source->file->args;
2811 for (i = 0; i < array->count; i++)
2813 char *font = strtok( xstrdup(array->str[i]), " \t" );
2814 char *args = strtok( NULL, "" );
2816 strarray_add( &make->all_targets, xstrdup( font ));
2817 output( "%s: %s %s\n", obj_dir_path( make, font ),
2818 tools_path( make, "sfnt2fon" ), ttf_file );
2819 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2820 tools_path( make, "sfnt2fon" ), ttf_file, args );
2821 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2827 /*******************************************************************
2828 * output_source_svg
2830 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2832 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2833 unsigned int i;
2835 if (convert && rsvg && icotool)
2837 for (i = 0; images[i]; i++)
2838 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2840 if (images[i])
2842 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2843 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2844 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2845 root_src_dir_path( "tools/buildimage" ), source->filename );
2846 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2852 /*******************************************************************
2853 * output_source_nls
2855 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2857 add_install_rule( make, source->name, source->name,
2858 strmake( "D$(nlsdir)/%s", source->name ));
2859 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2863 /*******************************************************************
2864 * output_source_desktop
2866 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2868 add_install_rule( make, source->name, source->name,
2869 strmake( "D$(datadir)/applications/%s", source->name ));
2873 /*******************************************************************
2874 * output_source_po
2876 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2878 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2879 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
2880 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2884 /*******************************************************************
2885 * output_source_in
2887 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2889 unsigned int i;
2891 if (strendswith( obj, ".man" ) && source->file->args)
2893 struct strarray symlinks;
2894 char *dir, *dest = replace_extension( obj, ".man", "" );
2895 char *lang = strchr( dest, '.' );
2896 char *section = source->file->args;
2897 if (lang)
2899 *lang++ = 0;
2900 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2902 else dir = strmake( "$(mandir)/man%s", section );
2903 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2904 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2905 for (i = 0; i < symlinks.count; i++)
2906 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2907 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2908 free( dest );
2909 free( dir );
2911 strarray_add( &make->in_files, xstrdup(obj) );
2912 strarray_add( &make->all_targets, xstrdup(obj) );
2913 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2914 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
2915 output( "%s:", obj_dir_path( make, obj ));
2916 output_filenames( source->dependencies );
2917 output( "\n" );
2918 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2922 /*******************************************************************
2923 * output_source_spec
2925 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2927 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2928 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
2929 struct strarray all_libs, dep_libs = empty_strarray;
2930 char *dll_name, *obj_name, *output_file;
2931 const char *debug_file;
2933 if (!imports.count) imports = make->imports;
2934 if (!dll_flags.count) dll_flags = make->extradllflags;
2935 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) imports = add_default_imports( make, imports );
2937 all_libs = add_import_libs( make, &dep_libs, imports, 0, make->is_cross );
2938 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
2939 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
2940 output_file = obj_dir_path( make, dll_name );
2942 strarray_add( &make->clean_files, dll_name );
2943 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2944 output( "%s.res:", obj_dir_path( make, obj ));
2945 output_filename( obj_dir_path( make, dll_name ));
2946 output_filename( tools_path( make, "wrc" ));
2947 output( "\n" );
2948 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
2949 tools_path( make, "wrc" ));
2951 output( "%s:", output_file);
2952 output_filename( source->filename );
2953 output_filename( obj_name );
2954 output_filenames( dep_libs );
2955 output_filename( tools_path( make, "winebuild" ));
2956 output_filename( tools_path( make, "winegcc" ));
2957 output( "\n" );
2958 output_winegcc_command( make, make->is_cross );
2959 output_filename( "-s" );
2960 output_filenames( dll_flags );
2961 output_filename( "-shared" );
2962 output_filename( source->filename );
2963 output_filename( obj_name );
2964 if ((debug_file = get_debug_file( make, dll_name )))
2965 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
2966 output_filenames( all_libs );
2967 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
2968 output( "\n" );
2972 /*******************************************************************
2973 * output_source_default
2975 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
2977 struct strarray defines = get_source_defines( make, source, obj );
2978 int is_dll_src = (make->testdll &&
2979 strendswith( source->name, ".c" ) &&
2980 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
2981 int need_cross = (crosstarget &&
2982 !(source->file->flags & FLAG_C_UNIX) &&
2983 (make->is_cross || make->staticlib ||
2984 (source->file->flags & FLAG_C_IMPLIB)));
2985 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
2986 (!need_cross ||
2987 (source->file->flags & FLAG_C_IMPLIB) ||
2988 (make->staticlib && !make->extlib)));
2990 if ((source->file->flags & FLAG_GENERATED) &&
2991 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2992 strarray_add( &make->clean_files, source->basename );
2994 if (need_obj)
2996 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
2997 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
2998 else if (source->file->flags & FLAG_C_IMPLIB)
2999 strarray_add( &make->implib_files, strmake( "%s.o", obj ));
3000 else if (!is_dll_src)
3001 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3002 else
3003 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3004 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3005 output( "\t%s$(CC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3006 output_filenames( defines );
3007 output_filenames( make->extlib ? extra_cflags_extlib : extra_cflags );
3008 if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
3010 output_filenames( unix_dllflags );
3012 else if (make->module || make->testdll)
3014 output_filenames( dll_flags );
3015 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3016 if (!*dll_ext && make->module && is_crt_module( make->module ))
3017 output_filename( "-fno-builtin" );
3019 output_filenames( cpp_flags );
3020 output_filename( "$(CFLAGS)" );
3021 output( "\n" );
3023 if (need_cross)
3025 if (source->file->flags & FLAG_C_IMPLIB)
3026 strarray_add( &make->crossimplib_files, strmake( "%s.cross.o", obj ));
3027 else if (!is_dll_src)
3028 strarray_add( &make->crossobj_files, strmake( "%s.cross.o", obj ));
3029 else
3030 strarray_add( &make->clean_files, strmake( "%s.cross.o", obj ));
3031 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3032 output( "\t%s$(CROSSCC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3033 output_filenames( defines );
3034 output_filenames( make->extlib ? extra_cross_cflags_extlib : extra_cross_cflags );
3035 if (make->module && is_crt_module( make->module ))
3036 output_filename( "-fno-builtin" );
3037 /* force -Wformat when using 'long' types, until all modules have been converted
3038 * and we can remove -Wno-format */
3039 if (!make->extlib && strarray_exists( &extra_cross_cflags, "-Wno-format" ) &&
3040 !strarray_exists( &defines, "-DWINE_NO_LONG_TYPES" ))
3041 output_filename( "-Wformat" );
3042 output_filenames( cpp_flags );
3043 output_filename( "$(CROSSCFLAGS)" );
3044 output( "\n" );
3046 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3047 !(source->file->flags & FLAG_GENERATED))
3049 strarray_add( &make->test_files, obj );
3050 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3051 output( "%s.ok:\n", obj_dir_path( make, obj ));
3052 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3053 cmd_prefix( "TEST" ),
3054 root_src_dir_path( "tools/runtest" ), make->testdll,
3055 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3056 make->is_cross ? "" : dll_ext, obj );
3058 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3059 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3060 output( ":" );
3061 output_filenames( source->dependencies );
3062 output( "\n" );
3066 /* dispatch table to output rules for a single source file */
3067 static const struct
3069 const char *ext;
3070 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3071 } output_source_funcs[] =
3073 { "y", output_source_y },
3074 { "l", output_source_l },
3075 { "h", output_source_h },
3076 { "rh", output_source_h },
3077 { "inl", output_source_h },
3078 { "rc", output_source_rc },
3079 { "mc", output_source_mc },
3080 { "res", output_source_res },
3081 { "idl", output_source_idl },
3082 { "sfd", output_source_sfd },
3083 { "svg", output_source_svg },
3084 { "nls", output_source_nls },
3085 { "desktop", output_source_desktop },
3086 { "po", output_source_po },
3087 { "in", output_source_in },
3088 { "x", output_source_x },
3089 { "spec", output_source_spec },
3090 { NULL, output_source_default }
3094 /*******************************************************************
3095 * get_unix_lib_name
3097 static char *get_unix_lib_name( struct makefile *make )
3099 struct incl_file *source;
3101 if (!*dll_ext) return NULL;
3102 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3104 if (!(source->file->flags & FLAG_C_UNIX)) continue;
3105 return strmake( "%s%s", get_base_name( make->module ), dll_ext );
3107 return NULL;
3111 /*******************************************************************
3112 * output_module
3114 static void output_module( struct makefile *make )
3116 struct strarray all_libs = empty_strarray;
3117 struct strarray dep_libs = empty_strarray;
3118 struct strarray imports = make->imports;
3119 const char *module_name = make->module;
3120 const char *debug_file;
3121 const char *delay_load = delay_load_flag;
3122 char *spec_file = NULL;
3123 unsigned int i;
3125 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3127 if (!make->data_only)
3129 if (*dll_ext && !make->is_cross && !make->data_only)
3130 module_name = strmake( "%s%s", make->module, dll_ext );
3132 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
3133 imports = add_default_imports( make, imports );
3135 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, make->is_cross ));
3136 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, 0, make->is_cross ));
3138 if (!make->use_msvcrt)
3140 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3141 strarray_addall( &all_libs, libs );
3144 if (!make->is_cross && *dll_ext) delay_load = "-Wl,-delayload,";
3145 if (delay_load)
3147 for (i = 0; i < make->delayimports.count; i++)
3148 strarray_add( &all_libs, strmake( "%s%s%s", delay_load, make->delayimports.str[i],
3149 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3152 strarray_add( &make->all_targets, module_name );
3154 if (make->data_only)
3155 add_install_rule( make, make->module, module_name,
3156 strmake( "d%s/%s", *dll_ext ? pe_dir : "$(dlldir)", module_name ));
3157 else if (make->is_cross)
3158 add_install_rule( make, make->module, module_name, strmake( "c%s/%s", pe_dir, module_name ));
3159 else if (*dll_ext)
3160 add_install_rule( make, make->module, module_name, strmake( "p%s/%s", so_dir, module_name ));
3161 else
3162 add_install_rule( make, make->module, module_name,
3163 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", module_name ));
3165 output( "%s:", obj_dir_path( make, module_name ));
3166 if (spec_file) output_filename( spec_file );
3167 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3168 output_filenames_obj_dir( make, make->res_files );
3169 output_filenames( dep_libs );
3170 output_filename( tools_path( make, "winebuild" ));
3171 output_filename( tools_path( make, "winegcc" ));
3172 output( "\n" );
3173 output_winegcc_command( make, make->is_cross );
3174 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3175 if (spec_file)
3177 output_filename( "-shared" );
3178 output_filename( spec_file );
3180 output_filenames( make->extradllflags );
3181 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3182 output_filenames_obj_dir( make, make->res_files );
3183 debug_file = get_debug_file( make, make->module );
3184 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3185 output_filenames( all_libs );
3186 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3187 output( "\n" );
3189 if (*dll_ext && make->is_exe && !make->is_win16 && strendswith( make->module, ".exe" ))
3191 char *binary = replace_extension( make->module, ".exe", "" );
3192 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3197 /*******************************************************************
3198 * output_fake_module
3200 static void output_fake_module( struct makefile *make )
3202 char *spec_file = NULL;
3204 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3206 strarray_add( &make->all_targets, make->module );
3207 add_install_rule( make, make->module, make->module, strmake( "d%s/%s", pe_dir, make->module ));
3209 output( "%s:", obj_dir_path( make, make->module ));
3210 if (spec_file) output_filename( spec_file );
3211 output_filenames_obj_dir( make, make->res_files );
3212 output_filename( tools_path( make, "winebuild" ));
3213 output_filename( tools_path( make, "winegcc" ));
3214 output( "\n" );
3215 output_winegcc_command( make, make->is_cross );
3216 output_filename( "-Wb,--fake-module" );
3217 if (spec_file)
3219 output_filename( "-shared" );
3220 output_filename( spec_file );
3222 output_filenames( make->extradllflags );
3223 output_filenames_obj_dir( make, make->res_files );
3224 output( "\n" );
3228 /*******************************************************************
3229 * output_import_lib
3231 static void output_import_lib( struct makefile *make )
3233 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3234 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3236 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3237 if (!*dll_ext && needs_delay_lib( make ))
3239 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3240 output( "%s.delay.a ", importlib_path );
3242 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3243 output_filenames_obj_dir( make, make->implib_files );
3244 output( "\n" );
3245 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3246 output_filenames( target_flags );
3247 if (make->is_win16) output_filename( "-m16" );
3248 output_filename( "--export" );
3249 output_filename( spec_file );
3250 output_filenames_obj_dir( make, make->implib_files );
3251 output( "\n" );
3252 add_install_rule( make, make->importlib,
3253 strmake( "lib%s.a", make->importlib ),
3254 strmake( "d%s/lib%s.a", so_dir, make->importlib ));
3256 if (crosstarget)
3258 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3259 output_filename( strmake( "%s.cross.a", importlib_path ));
3260 if (needs_delay_lib( make ))
3262 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3263 output_filename( strmake( "%s.delay.a", importlib_path ));
3265 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3266 output_filenames_obj_dir( make, make->crossimplib_files );
3267 output( "\n" );
3268 output( "\t%s%s -b %s -w --implib -o $@", cmd_prefix( "BUILD" ),
3269 tools_path( make, "winebuild" ), crosstarget );
3270 if (make->is_win16) output_filename( "-m16" );
3271 output_filename( "--export" );
3272 output_filename( spec_file );
3273 output_filenames_obj_dir( make, make->crossimplib_files );
3274 output( "\n" );
3275 add_install_rule( make, make->importlib,
3276 strmake( "lib%s.cross.a", make->importlib ),
3277 strmake( "d%s/lib%s.a", pe_dir, make->importlib ));
3282 /*******************************************************************
3283 * output_unix_lib
3285 static void output_unix_lib( struct makefile *make )
3287 struct strarray unix_libs = empty_strarray;
3288 struct strarray unix_deps = empty_strarray;
3289 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3291 if (!make->native_unix_lib)
3293 struct strarray unix_imports = empty_strarray;
3295 strarray_add( &unix_imports, "ntdll" );
3296 strarray_add( &unix_deps, obj_dir_path( top_makefile, "dlls/ntdll/ntdll.so" ));
3297 strarray_add( &unix_imports, "winecrt0" );
3298 if (spec_file) strarray_add( &unix_deps, spec_file );
3300 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 0 ));
3301 strarray_addall( &unix_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3302 strarray_addall( &unix_libs, libs );
3304 else unix_libs = add_unix_libraries( make, &unix_deps );
3306 strarray_add( &make->all_targets, make->unixlib );
3307 add_install_rule( make, make->module, make->unixlib, strmake( "p%s/%s", so_dir, make->unixlib ));
3308 output( "%s:", obj_dir_path( make, make->unixlib ));
3309 output_filenames_obj_dir( make, make->unixobj_files );
3310 output_filenames( unix_deps );
3312 if (make->native_unix_lib)
3314 output( "\n" );
3315 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3316 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3318 else
3320 output_filename( tools_path( make, "winebuild" ));
3321 output_filename( tools_path( make, "winegcc" ));
3322 output( "\n" );
3323 output_winegcc_command( make, 0 );
3324 output_filename( "-munix" );
3325 output_filename( "-shared" );
3326 if (spec_file) output_filename( spec_file );
3328 output_filenames_obj_dir( make, make->unixobj_files );
3329 output_filenames( unix_libs );
3330 output_filename( "$(LDFLAGS)" );
3331 output( "\n" );
3335 /*******************************************************************
3336 * output_static_lib
3338 static void output_static_lib( struct makefile *make )
3340 if (!crosstarget || !make->extlib)
3342 strarray_add( &make->clean_files, make->staticlib );
3343 output( "%s: %s", obj_dir_path( make, make->staticlib ), tools_path( make, "winebuild" ));
3344 output_filenames_obj_dir( make, make->object_files );
3345 output_filenames_obj_dir( make, make->unixobj_files );
3346 output( "\n" );
3347 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3348 output_filenames( target_flags );
3349 output_filenames_obj_dir( make, make->object_files );
3350 output_filenames_obj_dir( make, make->unixobj_files );
3351 output( "\n" );
3352 add_install_rule( make, make->staticlib, make->staticlib,
3353 strmake( "d%s/%s", so_dir, make->staticlib ));
3355 if (crosstarget)
3357 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3359 strarray_add( &make->clean_files, name );
3360 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3361 output_filenames_obj_dir( make, make->crossobj_files );
3362 output( "\n" );
3363 output( "\t%s%s -b %s -w --staticlib -o $@", cmd_prefix( "BUILD" ),
3364 tools_path( make, "winebuild" ), crosstarget );
3365 output_filenames_obj_dir( make, make->crossobj_files );
3366 output( "\n" );
3367 if (!make->extlib)
3368 add_install_rule( make, make->staticlib, name,
3369 strmake( "d%s/%s", pe_dir, make->staticlib ));
3374 /*******************************************************************
3375 * output_shared_lib
3377 static void output_shared_lib( struct makefile *make )
3379 unsigned int i;
3380 char *basename, *p;
3381 struct strarray names = get_shared_lib_names( make->sharedlib );
3382 struct strarray all_libs = empty_strarray;
3383 struct strarray dep_libs = empty_strarray;
3385 basename = xstrdup( make->sharedlib );
3386 if ((p = strchr( basename, '.' ))) *p = 0;
3388 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3389 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3390 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3391 strarray_addall( &all_libs, libs );
3393 output( "%s:", obj_dir_path( make, make->sharedlib ));
3394 output_filenames_obj_dir( make, make->object_files );
3395 output_filenames( dep_libs );
3396 output( "\n" );
3397 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3398 output_filenames_obj_dir( make, make->object_files );
3399 output_filenames( all_libs );
3400 output_filename( "$(LDFLAGS)" );
3401 output( "\n" );
3402 add_install_rule( make, make->sharedlib, make->sharedlib, strmake( "p%s/%s", so_dir, make->sharedlib ));
3403 for (i = 1; i < names.count; i++)
3405 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3406 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3407 add_install_rule( make, names.str[i], names.str[i-1], strmake( "y%s/%s", so_dir, names.str[i] ));
3409 strarray_addall( &make->all_targets, names );
3413 /*******************************************************************
3414 * output_test_module
3416 static void output_test_module( struct makefile *make )
3418 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3419 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3420 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3421 struct strarray dep_libs = empty_strarray;
3422 struct strarray all_libs = add_import_libs( make, &dep_libs, add_default_imports( make, make->imports ),
3423 0, make->is_cross );
3424 struct makefile *parent = get_parent_makefile( make );
3425 const char *ext = make->is_cross ? "" : dll_ext;
3426 const char *debug_file;
3427 char *output_file;
3429 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3430 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3431 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3432 output( "%s:\n", output_file );
3433 output_winegcc_command( make, make->is_cross );
3434 output_filenames( make->extradllflags );
3435 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3436 output_filenames_obj_dir( make, make->res_files );
3437 if ((debug_file = get_debug_file( make, testmodule )))
3438 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3439 output_filenames( all_libs );
3440 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3441 output( "\n" );
3442 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3443 output_winegcc_command( make, make->is_cross );
3444 output_filename( "-s" );
3445 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3446 output_filenames( make->extradllflags );
3447 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3448 output_filenames_obj_dir( make, make->res_files );
3449 output_filenames( all_libs );
3450 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3451 output( "\n" );
3452 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3453 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3454 output_filenames_obj_dir( make, make->res_files );
3455 output_filenames( dep_libs );
3456 output_filename( tools_path( make, "winebuild" ));
3457 output_filename( tools_path( make, "winegcc" ));
3458 output( "\n" );
3460 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3461 output( "\t%secho \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3462 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3464 output_filenames_obj_dir( make, make->ok_files );
3465 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3466 if (parent)
3468 output_filename( parent->is_cross ? obj_dir_path( parent, make->testdll )
3469 : strmake( "%s%s", obj_dir_path( parent, make->testdll ), dll_ext ));
3470 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3472 output( "\n" );
3473 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3474 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3475 output( "\n" );
3476 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3477 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3478 output( "%s::\n", obj_dir_path( make, "testclean" ));
3479 output( "\trm -f" );
3480 output_filenames_obj_dir( make, make->ok_files );
3481 output( "\n" );
3482 strarray_addall( &make->clean_files, make->ok_files );
3483 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3487 /*******************************************************************
3488 * output_programs
3490 static void output_programs( struct makefile *make )
3492 unsigned int i, j;
3494 for (i = 0; i < make->programs.count; i++)
3496 char *program_installed = NULL;
3497 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3498 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3499 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3500 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3501 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3503 if (!objs.count) objs = make->object_files;
3504 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3506 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3507 strarray_addall( &all_libs, libs );
3510 output( "%s:", obj_dir_path( make, program ) );
3511 output_filenames_obj_dir( make, objs );
3512 output_filenames( deps );
3513 output( "\n" );
3514 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3515 output_filenames_obj_dir( make, objs );
3516 output_filenames( all_libs );
3517 output_filename( "$(LDFLAGS)" );
3518 output( "\n" );
3519 strarray_add( &make->all_targets, program );
3521 for (j = 0; j < symlinks.count; j++)
3523 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3524 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3526 strarray_addall( &make->all_targets, symlinks );
3528 add_install_rule( make, program, program_installed ? program_installed : program,
3529 strmake( "p$(bindir)/%s", program ));
3530 for (j = 0; j < symlinks.count; j++)
3531 add_install_rule( make, symlinks.str[j], program,
3532 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3537 /*******************************************************************
3538 * output_subdirs
3540 static void output_subdirs( struct makefile *make )
3542 struct strarray all_targets = empty_strarray;
3543 struct strarray makefile_deps = empty_strarray;
3544 struct strarray clean_files = empty_strarray;
3545 struct strarray testclean_files = empty_strarray;
3546 struct strarray distclean_files = empty_strarray;
3547 struct strarray dependencies = empty_strarray;
3548 struct strarray install_lib_deps = empty_strarray;
3549 struct strarray install_dev_deps = empty_strarray;
3550 struct strarray tooldeps_deps = empty_strarray;
3551 struct strarray buildtest_deps = empty_strarray;
3552 unsigned int i;
3554 strarray_addall( &clean_files, make->clean_files );
3555 strarray_addall( &distclean_files, make->distclean_files );
3556 strarray_addall( &all_targets, make->all_targets );
3557 for (i = 0; i < subdirs.count; i++)
3559 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3560 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3561 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3562 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3563 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3564 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3565 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3566 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3567 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3569 if (submakes[i]->disabled) continue;
3571 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3572 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3573 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3574 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3575 if (submakes[i]->testdll)
3576 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3577 if (submakes[i]->install_rules[INSTALL_LIB].count)
3578 strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
3579 if (submakes[i]->install_rules[INSTALL_DEV].count)
3580 strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
3582 strarray_addall( &dependencies, makefile_deps );
3583 output( "all:" );
3584 output_filenames( all_targets );
3585 output( "\n" );
3586 output( "Makefile:" );
3587 output_filenames( makefile_deps );
3588 output( "\n" );
3589 output_filenames( dependencies );
3590 output( ":\n" );
3591 if (install_lib_deps.count)
3593 output( "install install-lib::" );
3594 output_filenames( install_lib_deps );
3595 output( "\n" );
3596 strarray_add_uniq( &make->phony_targets, "install" );
3597 strarray_add_uniq( &make->phony_targets, "install-lib" );
3599 if (install_dev_deps.count)
3601 output( "install install-dev::" );
3602 output_filenames( install_dev_deps );
3603 output( "\n" );
3604 strarray_add_uniq( &make->phony_targets, "install" );
3605 strarray_add_uniq( &make->phony_targets, "install-dev" );
3607 output_uninstall_rules( make );
3608 if (buildtest_deps.count)
3610 output( "buildtests:" );
3611 output_filenames( buildtest_deps );
3612 output( "\n" );
3613 strarray_add_uniq( &make->phony_targets, "buildtests" );
3615 output( "check test:" );
3616 output_filenames( testclean_files );
3617 output( "\n" );
3618 strarray_add_uniq( &make->phony_targets, "check" );
3619 strarray_add_uniq( &make->phony_targets, "test" );
3621 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3623 output( "clean::\n");
3624 output_rm_filenames( clean_files );
3625 output( "testclean::\n");
3626 output_rm_filenames( testclean_files );
3627 output( "distclean::\n");
3628 output_rm_filenames( distclean_files );
3629 output( "maintainer-clean::\n");
3630 output_rm_filenames( make->maintainerclean_files );
3631 strarray_add_uniq( &make->phony_targets, "distclean" );
3632 strarray_add_uniq( &make->phony_targets, "testclean" );
3633 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3635 if (tooldeps_deps.count)
3637 output( "__tooldeps__:" );
3638 output_filenames( tooldeps_deps );
3639 output( "\n" );
3640 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3643 if (make->phony_targets.count)
3645 output( ".PHONY:" );
3646 output_filenames( make->phony_targets );
3647 output( "\n" );
3652 /*******************************************************************
3653 * output_sources
3655 static void output_sources( struct makefile *make )
3657 struct strarray all_targets = empty_strarray;
3658 struct incl_file *source;
3659 unsigned int i, j;
3661 strarray_add_uniq( &make->phony_targets, "all" );
3663 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3665 char *obj = xstrdup( source->name );
3666 char *ext = get_extension( obj );
3668 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3669 *ext++ = 0;
3671 for (j = 0; output_source_funcs[j].ext; j++)
3672 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3674 output_source_funcs[j].fn( make, source, obj );
3675 strarray_addall_uniq( &make->dependencies, source->dependencies );
3678 /* special case for winetest: add resource files from other test dirs */
3679 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3681 struct strarray tests = enable_tests;
3682 if (!tests.count)
3683 for (i = 0; i < subdirs.count; i++)
3684 if (submakes[i]->testdll && !submakes[i]->disabled)
3685 strarray_add( &tests, submakes[i]->testdll );
3686 for (i = 0; i < tests.count; i++)
3687 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3690 if (make->dlldata_files.count)
3692 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3693 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3694 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3695 output_filenames( make->dlldata_files );
3696 output( "\n" );
3699 if (make->staticlib) output_static_lib( make );
3700 else if (make->module)
3702 output_module( make );
3703 if (*dll_ext && !make->is_cross && !make->data_only) output_fake_module( make );
3704 if (make->unixlib) output_unix_lib( make );
3705 if (make->importlib) output_import_lib( make );
3707 else if (make->testdll) output_test_module( make );
3708 else if (make->sharedlib) output_shared_lib( make );
3709 else if (make->programs.count) output_programs( make );
3711 for (i = 0; i < make->scripts.count; i++)
3712 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3713 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3715 for (i = 0; i < make->extra_targets.count; i++)
3716 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3717 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3718 else
3719 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3721 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3722 strarray_add( &make->distclean_files, "Makefile" );
3723 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3725 if (!make->obj_dir)
3726 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3727 else if (!strcmp( make->obj_dir, "po" ))
3728 strarray_add( &make->distclean_files, "LINGUAS" );
3730 strarray_addall( &make->clean_files, make->object_files );
3731 strarray_addall( &make->clean_files, make->crossobj_files );
3732 strarray_addall( &make->clean_files, make->implib_files );
3733 strarray_addall( &make->clean_files, make->crossimplib_files );
3734 strarray_addall( &make->clean_files, make->unixobj_files );
3735 strarray_addall( &make->clean_files, make->res_files );
3736 strarray_addall( &make->clean_files, make->pot_files );
3737 strarray_addall( &make->clean_files, make->debug_files );
3738 strarray_addall( &make->clean_files, make->all_targets );
3740 if (make == top_makefile)
3742 output_subdirs( make );
3743 return;
3746 strarray_addall( &all_targets, make->all_targets );
3747 strarray_addall( &all_targets, make->font_files );
3748 if (all_targets.count)
3750 output( "%s:", obj_dir_path( make, "all" ));
3751 output_filenames_obj_dir( make, all_targets );
3752 output( "\n" );
3753 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3755 output_install_rules( make, INSTALL_LIB, "install-lib" );
3756 output_install_rules( make, INSTALL_DEV, "install-dev" );
3758 if (make->clean_files.count)
3760 output( "%s::\n", obj_dir_path( make, "clean" ));
3761 output( "\trm -f" );
3762 output_filenames_obj_dir( make, make->clean_files );
3763 output( "\n" );
3764 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3769 /*******************************************************************
3770 * create_temp_file
3772 static FILE *create_temp_file( const char *orig )
3774 char *name = xmalloc( strlen(orig) + 13 );
3775 unsigned int i, id = getpid();
3776 int fd;
3777 FILE *ret = NULL;
3779 for (i = 0; i < 100; i++)
3781 sprintf( name, "%s.tmp%08x", orig, id );
3782 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3784 ret = fdopen( fd, "w" );
3785 break;
3787 if (errno != EEXIST) break;
3788 id += 7777;
3790 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3791 temp_file_name = name;
3792 return ret;
3796 /*******************************************************************
3797 * rename_temp_file
3799 static void rename_temp_file( const char *dest )
3801 int ret = rename( temp_file_name, dest );
3802 if (ret == -1 && errno == EEXIST)
3804 /* rename doesn't overwrite on windows */
3805 unlink( dest );
3806 ret = rename( temp_file_name, dest );
3808 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3809 temp_file_name = NULL;
3813 /*******************************************************************
3814 * are_files_identical
3816 static int are_files_identical( FILE *file1, FILE *file2 )
3818 for (;;)
3820 char buffer1[8192], buffer2[8192];
3821 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3822 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3823 if (size1 != size2) return 0;
3824 if (!size1) return feof( file1 ) && feof( file2 );
3825 if (memcmp( buffer1, buffer2, size1 )) return 0;
3830 /*******************************************************************
3831 * rename_temp_file_if_changed
3833 static void rename_temp_file_if_changed( const char *dest )
3835 FILE *file1, *file2;
3836 int do_rename = 1;
3838 if ((file1 = fopen( dest, "r" )))
3840 if ((file2 = fopen( temp_file_name, "r" )))
3842 do_rename = !are_files_identical( file1, file2 );
3843 fclose( file2 );
3845 fclose( file1 );
3847 if (!do_rename)
3849 unlink( temp_file_name );
3850 temp_file_name = NULL;
3852 else rename_temp_file( dest );
3856 /*******************************************************************
3857 * output_linguas
3859 static void output_linguas( const struct makefile *make )
3861 const char *dest = obj_dir_path( make, "LINGUAS" );
3862 struct incl_file *source;
3864 output_file = create_temp_file( dest );
3866 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3867 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3868 if (strendswith( source->name, ".po" ))
3869 output( "%s\n", replace_extension( source->name, ".po", "" ));
3871 if (fclose( output_file )) fatal_perror( "write" );
3872 output_file = NULL;
3873 rename_temp_file_if_changed( dest );
3877 /*******************************************************************
3878 * output_testlist
3880 static void output_testlist( const struct makefile *make )
3882 const char *dest = obj_dir_path( make, "testlist.c" );
3883 unsigned int i;
3885 output_file = create_temp_file( dest );
3887 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3888 output( "#define WIN32_LEAN_AND_MEAN\n" );
3889 output( "#include <windows.h>\n\n" );
3890 output( "#define STANDALONE\n" );
3891 output( "#include \"wine/test.h\"\n\n" );
3893 for (i = 0; i < make->test_files.count; i++)
3894 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3895 output( "\n" );
3896 output( "const struct test winetest_testlist[] =\n" );
3897 output( "{\n" );
3898 for (i = 0; i < make->test_files.count; i++)
3899 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3900 output( " { 0, 0 }\n" );
3901 output( "};\n" );
3903 if (fclose( output_file )) fatal_perror( "write" );
3904 output_file = NULL;
3905 rename_temp_file_if_changed( dest );
3909 /*******************************************************************
3910 * output_gitignore
3912 static void output_gitignore( const char *dest, struct strarray files )
3914 unsigned int i;
3916 output_file = create_temp_file( dest );
3918 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3919 for (i = 0; i < files.count; i++)
3921 if (!strchr( files.str[i], '/' )) output( "/" );
3922 output( "%s\n", files.str[i] );
3925 if (fclose( output_file )) fatal_perror( "write" );
3926 output_file = NULL;
3927 rename_temp_file( dest );
3931 /*******************************************************************
3932 * output_stub_makefile
3934 static void output_stub_makefile( struct makefile *make )
3936 struct strarray targets = empty_strarray;
3937 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
3939 if (make->obj_dir) create_dir( make->obj_dir );
3941 output_file_name = obj_dir_path( make, "Makefile" );
3942 output_file = create_temp_file( output_file_name );
3944 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
3946 if (make_var) output( "MAKE = %s\n\n", make_var );
3947 output( "all:\n" );
3949 if (make->all_targets.count) strarray_add( &targets, "all" );
3950 if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
3951 strarray_add( &targets, "install" );
3952 if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
3953 if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
3954 if (make->clean_files.count) strarray_add( &targets, "clean" );
3955 if (make->test_files.count)
3957 strarray_add( &targets, "check" );
3958 strarray_add( &targets, "test" );
3959 strarray_add( &targets, "testclean" );
3962 output_filenames( targets );
3963 output_filenames( make->clean_files );
3964 output( ":\n" );
3965 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
3966 output( ".PHONY:" );
3967 output_filenames( targets );
3968 output( "\n" );
3970 fclose( output_file );
3971 output_file = NULL;
3972 rename_temp_file( output_file_name );
3976 /*******************************************************************
3977 * output_silent_rules
3979 static void output_silent_rules(void)
3981 static const char *cmds[] =
3983 "BISON",
3984 "BUILD",
3985 "CC",
3986 "CCLD",
3987 "FLEX",
3988 "GEN",
3989 "LN",
3990 "MSG",
3991 "SED",
3992 "TEST",
3993 "WIDL",
3994 "WMC",
3995 "WRC"
3997 unsigned int i;
3999 output( "V = 0\n" );
4000 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++)
4002 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4003 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4004 output( "quiet_%s_1 =\n", cmds[i] );
4009 /*******************************************************************
4010 * output_top_makefile
4012 static void output_top_makefile( struct makefile *make )
4014 char buffer[1024];
4015 FILE *src_file;
4016 unsigned int i;
4017 int found = 0;
4019 output_file_name = obj_dir_path( make, output_makefile_name );
4020 output_file = create_temp_file( output_file_name );
4022 /* copy the contents of the source makefile */
4023 src_file = open_input_makefile( make );
4024 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4026 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4027 found = !strncmp( buffer, separator, strlen(separator) );
4029 if (fclose( src_file )) fatal_perror( "close" );
4030 input_file_name = NULL;
4032 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4034 if (silent_rules) output_silent_rules();
4035 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4036 output_sources( make );
4037 /* disable implicit rules */
4038 output( ".SUFFIXES:\n" );
4040 fclose( output_file );
4041 output_file = NULL;
4042 rename_temp_file( output_file_name );
4046 /*******************************************************************
4047 * output_dependencies
4049 static void output_dependencies( struct makefile *make )
4051 struct strarray ignore_files = empty_strarray;
4053 if (make->obj_dir) create_dir( make->obj_dir );
4055 if (make == top_makefile) output_top_makefile( make );
4056 else output_stub_makefile( make );
4058 strarray_addall( &ignore_files, make->distclean_files );
4059 strarray_addall( &ignore_files, make->clean_files );
4060 if (make->testdll) output_testlist( make );
4061 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4062 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4064 create_file_directories( make, ignore_files );
4066 output_file_name = NULL;
4070 /*******************************************************************
4071 * load_sources
4073 static void load_sources( struct makefile *make )
4075 static const char *source_vars[] =
4077 "SOURCES",
4078 "C_SRCS",
4079 "OBJC_SRCS",
4080 "RC_SRCS",
4081 "MC_SRCS",
4082 "IDL_SRCS",
4083 "BISON_SRCS",
4084 "LEX_SRCS",
4085 "HEADER_SRCS",
4086 "XTEMPLATE_SRCS",
4087 "SVG_SRCS",
4088 "FONT_SRCS",
4089 "IN_SRCS",
4090 "PO_SRCS",
4091 "MANPAGES",
4092 NULL
4094 const char **var;
4095 unsigned int i;
4096 struct strarray value;
4097 struct incl_file *file;
4099 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4100 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4102 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4103 make->module = get_expanded_make_variable( make, "MODULE" );
4104 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4105 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4106 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4107 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4108 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4109 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4111 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4112 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4113 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4114 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4115 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4116 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4117 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4118 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4120 if (make->extlib) make->staticlib = make->extlib;
4121 if (make->staticlib) make->module = make->staticlib;
4123 if (host_cpu)
4125 value = get_expanded_file_local_var( make, host_cpu, "EXTRADLLFLAGS" );
4126 if (value.count) make->extradllflags = value;
4129 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4130 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4131 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4132 make->use_msvcrt = (make->module || make->testdll || make->is_win16) &&
4133 !strarray_exists( &make->extradllflags, "-mcygwin" );
4134 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4135 strarray_exists( &make->extradllflags, "-mwindows" );
4136 make->native_unix_lib = !!make->unixlib;
4138 if (make->use_msvcrt) strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4140 if (make->module && !make->install_lib.count && !make->install_dev.count)
4142 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4143 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4144 else strarray_add( &make->install_lib, make->module );
4147 make->include_paths = empty_strarray;
4148 make->include_args = empty_strarray;
4149 make->define_args = empty_strarray;
4150 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4152 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4153 for (i = 0; i < value.count; i++)
4154 if (!strncmp( value.str[i], "-I", 2 ))
4155 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4156 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4157 strarray_add_uniq( &make->define_args, value.str[i] );
4158 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4160 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4161 if (make->src_dir)
4162 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4163 if (make->parent_dir)
4164 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4165 strarray_add( &make->include_args, "-Iinclude" );
4166 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4168 list_init( &make->sources );
4169 list_init( &make->includes );
4171 for (var = source_vars; *var; var++)
4173 value = get_expanded_make_var_array( make, *var );
4174 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4177 add_generated_sources( make );
4178 if (!make->unixlib) make->unixlib = get_unix_lib_name( make );
4180 if (make->use_msvcrt) strarray_add( &make->define_args, get_crt_define( make ));
4182 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4183 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4185 make->is_cross = crosstarget && make->use_msvcrt;
4187 if (!*dll_ext || make->is_cross)
4188 for (i = 0; i < make->delayimports.count; i++)
4189 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4193 /*******************************************************************
4194 * parse_makeflags
4196 static void parse_makeflags( const char *flags )
4198 const char *p = flags;
4199 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4201 while (*p)
4203 while (isspace(*p)) p++;
4204 var = buffer;
4205 while (*p && !isspace(*p))
4207 if (*p == '\\' && p[1]) p++;
4208 *var++ = *p++;
4210 *var = 0;
4211 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4216 /*******************************************************************
4217 * parse_option
4219 static int parse_option( const char *opt )
4221 if (opt[0] != '-')
4223 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4224 return 0;
4226 switch(opt[1])
4228 case 'f':
4229 if (opt[2]) output_makefile_name = opt + 2;
4230 break;
4231 case 'R':
4232 relative_dir_mode = 1;
4233 break;
4234 case 'S':
4235 silent_rules = 1;
4236 break;
4237 default:
4238 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4239 exit(1);
4241 return 1;
4245 /*******************************************************************
4246 * main
4248 int main( int argc, char *argv[] )
4250 const char *makeflags = getenv( "MAKEFLAGS" );
4251 int i, j;
4253 if (makeflags) parse_makeflags( makeflags );
4255 i = 1;
4256 while (i < argc)
4258 if (parse_option( argv[i] ))
4260 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4261 argc--;
4263 else i++;
4266 if (relative_dir_mode)
4268 char *relpath;
4270 if (argc != 3)
4272 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4273 exit( 1 );
4275 relpath = get_relative_path( argv[1], argv[2] );
4276 printf( "%s\n", relpath ? relpath : "." );
4277 exit( 0 );
4280 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4282 atexit( cleanup_files );
4283 signal( SIGTERM, exit_on_signal );
4284 signal( SIGINT, exit_on_signal );
4285 #ifdef SIGHUP
4286 signal( SIGHUP, exit_on_signal );
4287 #endif
4289 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4291 top_makefile = parse_makefile( NULL );
4293 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4294 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4295 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4296 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4297 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4298 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4299 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4300 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4301 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4302 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4303 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4304 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4306 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4307 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4308 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4309 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4310 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4311 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4312 host_cpu = get_expanded_make_variable( top_makefile, "host_cpu" );
4313 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4314 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4315 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4316 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4317 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4318 bison = get_expanded_make_variable( top_makefile, "BISON" );
4319 ar = get_expanded_make_variable( top_makefile, "AR" );
4320 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4321 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4322 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4323 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4324 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4325 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4326 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4328 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4329 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4330 if (!exe_ext) exe_ext = "";
4331 if (!tools_ext) tools_ext = "";
4332 if (host_cpu && (host_cpu = normalize_arch( host_cpu )))
4334 so_dir = strmake( "$(dlldir)/%s-unix", host_cpu );
4335 pe_dir = strmake( "$(dlldir)/%s-windows", host_cpu );
4337 else
4338 so_dir = pe_dir = "$(dlldir)";
4340 extra_cflags_extlib = remove_warning_flags( extra_cflags );
4341 extra_cross_cflags_extlib = remove_warning_flags( extra_cross_cflags );
4343 top_makefile->src_dir = root_src_dir;
4344 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4345 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4346 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4348 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4350 load_sources( top_makefile );
4351 for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
4353 output_dependencies( top_makefile );
4354 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4356 return 0;