windowscodecs: Correctly handle 8bpp custom conversions.
[wine.git] / tools / makedep.c
blob25277efd7bbfd852d2d5c558e181455ebe0d344f
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 * has_external_import
1365 static int has_external_import( const struct makefile *make )
1367 unsigned int i;
1369 for (i = 0; i < make->imports.count; i++)
1371 if (!strncmp( make->imports.str[i], "-l", 2 ))
1372 return 1;
1374 return 0;
1378 /*******************************************************************
1379 * open_include_file
1381 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1383 struct file *file = NULL;
1384 unsigned int i, len;
1386 errno = ENOENT;
1388 /* check for generated files */
1389 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1390 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1391 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1392 if (convert && rsvg && icotool)
1394 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1395 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1396 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1399 /* check for extra targets */
1400 if (strarray_exists( &make->extra_targets, pFile->name ))
1402 pFile->sourcename = src_dir_path( make, pFile->name );
1403 pFile->filename = obj_dir_path( make, pFile->name );
1404 return NULL;
1407 /* now try in source dir */
1408 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1410 /* check for global importlib (module dependency) */
1411 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1413 pFile->filename = pFile->name;
1414 return NULL;
1417 /* check for generated files in global includes */
1418 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1419 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1420 if (strendswith( pFile->name, "tmpl.h" ) &&
1421 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1423 /* check in global includes source dir */
1424 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1426 /* check in global msvcrt includes */
1427 if (pFile->use_msvcrt &&
1428 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1429 return file;
1431 /* now search in include paths */
1432 for (i = 0; i < make->include_paths.count; i++)
1434 const char *dir = make->include_paths.str[i];
1436 if (root_src_dir)
1438 len = strlen( root_src_dir );
1439 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1441 while (dir[len] == '/') len++;
1442 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1445 else
1447 if (*dir == '/') continue;
1448 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1450 if (!file) continue;
1451 pFile->is_external = 1;
1452 return file;
1455 if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt &&
1456 !make->extlib && !pFile->included_by->is_external)
1458 if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
1459 if (!strcmp( pFile->name, "x86intrin.h" )) return NULL;
1460 if (has_external_import( make )) return NULL;
1461 fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
1462 pFile->included_by->file->name, pFile->included_line, pFile->name );
1463 exit(1);
1466 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1468 /* try in src file directory */
1469 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1470 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1471 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1473 pFile->is_external = pFile->included_by->is_external;
1474 return file;
1477 if (make->extlib) return NULL; /* ignore missing files in external libs */
1479 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1480 perror( pFile->name );
1481 pFile = pFile->included_by;
1482 while (pFile && pFile->included_by)
1484 const char *parent = pFile->included_by->sourcename;
1485 if (!parent) parent = pFile->included_by->file->name;
1486 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1487 parent, pFile->included_line, pFile->name );
1488 pFile = pFile->included_by;
1490 exit(1);
1494 /*******************************************************************
1495 * add_all_includes
1497 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1499 unsigned int i;
1501 for (i = 0; i < file->deps_count; i++)
1503 switch (file->deps[i].type)
1505 case INCL_NORMAL:
1506 case INCL_IMPORT:
1507 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1508 break;
1509 case INCL_IMPORTLIB:
1510 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1511 break;
1512 case INCL_SYSTEM:
1513 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1514 break;
1515 case INCL_CPP_QUOTE:
1516 case INCL_CPP_QUOTE_SYSTEM:
1517 break;
1523 /*******************************************************************
1524 * parse_file
1526 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1528 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1530 if (!file) return;
1532 source->file = file;
1533 source->files_count = 0;
1534 source->files_size = file->deps_count;
1535 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1536 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1537 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1539 if (source->sourcename)
1541 if (strendswith( source->sourcename, ".idl" ))
1543 unsigned int i;
1545 /* generated .h file always includes these */
1546 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1547 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1548 for (i = 0; i < file->deps_count; i++)
1550 switch (file->deps[i].type)
1552 case INCL_IMPORT:
1553 if (strendswith( file->deps[i].name, ".idl" ))
1554 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1555 file->deps[i].line, INCL_NORMAL );
1556 else
1557 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1558 break;
1559 case INCL_CPP_QUOTE:
1560 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1561 break;
1562 case INCL_CPP_QUOTE_SYSTEM:
1563 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1564 break;
1565 case INCL_NORMAL:
1566 case INCL_SYSTEM:
1567 case INCL_IMPORTLIB:
1568 break;
1571 return;
1573 if (strendswith( source->sourcename, ".y" ))
1574 return; /* generated .tab.h doesn't include anything */
1577 add_all_includes( make, source, file );
1581 /*******************************************************************
1582 * add_src_file
1584 * Add a source file to the list.
1586 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1588 struct incl_file *file;
1590 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1591 file = xmalloc( sizeof(*file) );
1592 memset( file, 0, sizeof(*file) );
1593 file->name = xstrdup(name);
1594 file->use_msvcrt = make->use_msvcrt;
1595 file->is_external = !!make->extlib;
1596 list_add_tail( &make->sources, &file->entry );
1597 parse_file( make, file, 1 );
1598 return file;
1602 /*******************************************************************
1603 * open_input_makefile
1605 static FILE *open_input_makefile( const struct makefile *make )
1607 FILE *ret;
1609 if (make->obj_dir)
1610 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1611 else
1612 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1614 input_line = 0;
1615 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1616 return ret;
1620 /*******************************************************************
1621 * get_make_variable
1623 static const char *get_make_variable( const struct makefile *make, const char *name )
1625 const char *ret;
1627 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1628 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1629 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1630 return NULL;
1634 /*******************************************************************
1635 * get_expanded_make_variable
1637 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1639 const char *var;
1640 char *p, *end, *expand, *tmp;
1642 var = get_make_variable( make, name );
1643 if (!var) return NULL;
1645 p = expand = xstrdup( var );
1646 while ((p = strchr( p, '$' )))
1648 if (p[1] == '(')
1650 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1651 *end++ = 0;
1652 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1653 var = get_make_variable( make, p + 2 );
1654 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1655 /* switch to the new string */
1656 p = tmp + (p - expand);
1657 free( expand );
1658 expand = tmp;
1660 else if (p[1] == '{') /* don't expand ${} variables */
1662 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1663 p = end + 1;
1665 else if (p[1] == '$')
1667 p += 2;
1669 else fatal_error( "syntax error in '%s'\n", expand );
1672 /* consider empty variables undefined */
1673 p = expand;
1674 while (*p && isspace(*p)) p++;
1675 if (*p) return expand;
1676 free( expand );
1677 return NULL;
1681 /*******************************************************************
1682 * get_expanded_make_var_array
1684 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1686 struct strarray ret = empty_strarray;
1687 char *value, *token;
1689 if ((value = get_expanded_make_variable( make, name )))
1690 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1691 strarray_add( &ret, token );
1692 return ret;
1696 /*******************************************************************
1697 * get_expanded_file_local_var
1699 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1700 const char *name )
1702 char *p, *var = strmake( "%s_%s", file, name );
1704 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1705 return get_expanded_make_var_array( make, var );
1709 /*******************************************************************
1710 * set_make_variable
1712 static int set_make_variable( struct strarray *array, const char *assignment )
1714 char *p, *name;
1716 p = name = xstrdup( assignment );
1717 while (isalnum(*p) || *p == '_') p++;
1718 if (name == p) return 0; /* not a variable */
1719 if (isspace(*p))
1721 *p++ = 0;
1722 while (isspace(*p)) p++;
1724 if (*p != '=') return 0; /* not an assignment */
1725 *p++ = 0;
1726 while (isspace(*p)) p++;
1728 strarray_set_value( array, name, p );
1729 return 1;
1733 /*******************************************************************
1734 * parse_makefile
1736 static struct makefile *parse_makefile( const char *path )
1738 char *buffer;
1739 FILE *file;
1740 struct makefile *make = xmalloc( sizeof(*make) );
1742 memset( make, 0, sizeof(*make) );
1743 make->obj_dir = path;
1744 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1746 file = open_input_makefile( make );
1747 while ((buffer = get_line( file )))
1749 if (!strncmp( buffer, separator, strlen(separator) )) break;
1750 if (*buffer == '\t') continue; /* command */
1751 while (isspace( *buffer )) buffer++;
1752 if (*buffer == '#') continue; /* comment */
1753 set_make_variable( &make->vars, buffer );
1755 fclose( file );
1756 input_file_name = NULL;
1757 return make;
1761 /*******************************************************************
1762 * add_generated_sources
1764 static void add_generated_sources( struct makefile *make )
1766 unsigned int i;
1767 struct incl_file *source, *next, *file;
1768 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1770 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1772 if (source->file->flags & FLAG_IDL_CLIENT)
1774 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1775 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1776 add_all_includes( make, file, file->file );
1778 if (source->file->flags & FLAG_IDL_SERVER)
1780 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1781 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1782 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1783 add_all_includes( make, file, file->file );
1785 if (source->file->flags & FLAG_IDL_IDENT)
1787 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1788 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1789 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1790 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1791 add_all_includes( make, file, file->file );
1793 if (source->file->flags & FLAG_IDL_PROXY)
1795 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1796 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1797 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1798 add_all_includes( make, file, file->file );
1799 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1800 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1801 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1802 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1803 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1804 add_all_includes( make, file, file->file );
1806 if (source->file->flags & FLAG_IDL_TYPELIB)
1808 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL );
1810 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1812 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1814 if (source->file->flags & FLAG_IDL_REGISTER)
1816 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1818 if (source->file->flags & FLAG_IDL_HEADER)
1820 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1822 if (!source->file->flags && strendswith( source->name, ".idl" ))
1824 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1826 if (strendswith( source->name, ".x" ))
1828 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1830 if (strendswith( source->name, ".y" ))
1832 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1833 /* steal the includes list from the source file */
1834 file->files_count = source->files_count;
1835 file->files_size = source->files_size;
1836 file->files = source->files;
1837 source->files_count = source->files_size = 0;
1838 source->files = NULL;
1840 if (strendswith( source->name, ".l" ))
1842 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1843 /* steal the includes list from the source file */
1844 file->files_count = source->files_count;
1845 file->files_size = source->files_size;
1846 file->files = source->files;
1847 source->files_count = source->files_size = 0;
1848 source->files = NULL;
1850 if (source->file->flags & FLAG_C_IMPLIB)
1852 if (!make->staticimplib && make->importlib && *dll_ext)
1853 make->staticimplib = strmake( "lib%s.a", make->importlib );
1855 if (strendswith( source->name, ".po" ))
1857 if (!make->disabled)
1858 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1860 if (strendswith( source->name, ".spec" ))
1862 char *obj = replace_extension( source->name, ".spec", "" );
1863 strarray_addall_uniq( &make->extra_imports,
1864 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1867 if (make->testdll)
1869 file = add_generated_source( make, "testlist.o", "testlist.c" );
1870 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1871 add_all_includes( make, file, file->file );
1873 for (i = 0; i < objs.count; i++)
1875 /* default to .c for unknown extra object files */
1876 if (strendswith( objs.str[i], ".o" ))
1878 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1879 file->file->flags |= FLAG_C_UNIX;
1880 file->use_msvcrt = 0;
1882 else if (strendswith( objs.str[i], ".res" ))
1883 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1884 else
1885 add_generated_source( make, objs.str[i], NULL );
1890 /*******************************************************************
1891 * create_dir
1893 static void create_dir( const char *dir )
1895 char *p, *path;
1897 p = path = xstrdup( dir );
1898 while ((p = strchr( p, '/' )))
1900 *p = 0;
1901 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1902 *p++ = '/';
1903 while (*p == '/') p++;
1905 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1906 free( path );
1910 /*******************************************************************
1911 * create_file_directories
1913 * Create the base directories of all the files.
1915 static void create_file_directories( const struct makefile *make, struct strarray files )
1917 struct strarray subdirs = empty_strarray;
1918 unsigned int i;
1919 char *dir;
1921 for (i = 0; i < files.count; i++)
1923 if (!strchr( files.str[i], '/' )) continue;
1924 dir = obj_dir_path( make, files.str[i] );
1925 *strrchr( dir, '/' ) = 0;
1926 strarray_add_uniq( &subdirs, dir );
1929 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1933 /*******************************************************************
1934 * output_filenames_obj_dir
1936 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1938 unsigned int i;
1940 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1944 /*******************************************************************
1945 * get_dependencies
1947 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1949 unsigned int i;
1951 if (!file->filename) return;
1953 if (file != source)
1955 if (file->owner == source) return; /* already processed */
1956 if (file->type == INCL_IMPORTLIB)
1958 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1959 return; /* library is imported only when building a typelib */
1960 strarray_add( &source->importlibdeps, file->filename );
1962 else strarray_add( &source->dependencies, file->filename );
1963 file->owner = source;
1965 /* sanity checks */
1966 if (!strcmp( file->filename, "include/config.h" ) &&
1967 file != source->files[0] && !source->is_external)
1969 input_file_name = source->filename;
1970 input_line = 0;
1971 for (i = 0; i < source->file->deps_count; i++)
1973 if (!strcmp( source->file->deps[i].name, file->name ))
1974 input_line = source->file->deps[i].line;
1976 fatal_error( "%s must be included before other headers\n", file->name );
1980 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
1984 /*******************************************************************
1985 * get_local_dependencies
1987 * Get the local dependencies of a given target.
1989 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1990 struct strarray targets )
1992 unsigned int i;
1993 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
1995 for (i = 0; i < deps.count; i++)
1997 if (strarray_exists( &targets, deps.str[i] ))
1998 deps.str[i] = obj_dir_path( make, deps.str[i] );
1999 else
2000 deps.str[i] = src_dir_path( make, deps.str[i] );
2002 return deps;
2006 /*******************************************************************
2007 * get_static_lib
2009 * Check if makefile builds the named static library and return the full lib path.
2011 static const char *get_static_lib( const struct makefile *make, const char *name )
2013 if (!make->staticlib) return NULL;
2014 if (make->disabled) return NULL;
2015 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2016 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2017 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2018 return obj_dir_path( make, make->staticlib );
2022 /*******************************************************************
2023 * get_native_unix_lib
2025 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2027 if (!make->native_unix_lib) return NULL;
2028 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2029 if (make->unixlib[strlen(name)] != '.') return NULL;
2030 return obj_dir_path( make, make->unixlib );
2034 /*******************************************************************
2035 * get_parent_makefile
2037 static struct makefile *get_parent_makefile( struct makefile *make )
2039 char *dir, *p;
2040 unsigned int i;
2042 if (!make->obj_dir) return NULL;
2043 dir = xstrdup( make->obj_dir );
2044 if (!(p = strrchr( dir, '/' ))) return NULL;
2045 *p = 0;
2046 for (i = 0; i < subdirs.count; i++)
2047 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2048 return NULL;
2052 /*******************************************************************
2053 * needs_delay_lib
2055 static int needs_delay_lib( const struct makefile *make )
2057 if (delay_load_flag) return 0;
2058 if (*dll_ext && !crosstarget) return 0;
2059 if (!make->importlib) return 0;
2060 return strarray_exists( &delay_import_libs, make->importlib );
2064 /*******************************************************************
2065 * add_unix_libraries
2067 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2069 struct strarray ret = empty_strarray;
2070 struct strarray all_libs = empty_strarray;
2071 unsigned int i, j;
2073 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2074 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2076 for (i = 0; i < all_libs.count; i++)
2078 const char *lib = NULL;
2080 if (!strncmp( all_libs.str[i], "-l", 2 ))
2082 for (j = 0; j < subdirs.count; j++)
2084 if (make == submakes[j]) continue;
2085 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2088 if (lib)
2090 strarray_add( deps, lib );
2091 strarray_add( &ret, lib );
2093 else strarray_add( &ret, all_libs.str[i] );
2096 strarray_addall( &ret, libs );
2097 return ret;
2101 /*******************************************************************
2102 * is_crt_module
2104 static int is_crt_module( const char *file )
2106 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2110 /*******************************************************************
2111 * get_default_crt
2113 static const char *get_default_crt( const struct makefile *make )
2115 if (!make->use_msvcrt) return NULL;
2116 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2117 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2121 /*******************************************************************
2122 * get_crt_define
2124 static const char *get_crt_define( const struct makefile *make )
2126 const char *crt_dll = NULL;
2127 unsigned int i, version = 0;
2129 for (i = 0; i < make->imports.count; i++)
2131 if (!is_crt_module( make->imports.str[i] )) continue;
2132 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2133 crt_dll, make->imports.str[i] );
2134 crt_dll = make->imports.str[i];
2137 if (!crt_dll)
2139 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2140 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2142 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2143 sscanf( crt_dll, "msvcr%u", &version );
2144 return strmake( "-D_MSVCR_VER=%u", version );
2148 /*******************************************************************
2149 * add_default_imports
2151 static struct strarray add_default_imports( const struct makefile *make, struct strarray imports )
2153 struct strarray ret = empty_strarray;
2154 const char *crt_dll = get_default_crt( make );
2155 unsigned int i;
2157 for (i = 0; i < imports.count; i++)
2159 if (is_crt_module( imports.str[i] )) crt_dll = imports.str[i];
2160 else strarray_add( &ret, imports.str[i] );
2163 strarray_add( &ret, "winecrt0" );
2164 if (crt_dll) strarray_add( &ret, crt_dll );
2166 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2167 strarray_add( &ret, "kernel" );
2169 strarray_add( &ret, "kernel32" );
2170 strarray_add( &ret, "ntdll" );
2171 return ret;
2175 /*******************************************************************
2176 * add_import_libs
2178 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2179 struct strarray imports, int delay, int is_cross )
2181 struct strarray ret = empty_strarray;
2182 unsigned int i, j;
2184 for (i = 0; i < imports.count; i++)
2186 const char *name = imports.str[i];
2187 const char *lib = NULL;
2189 if (name[0] == '-')
2191 switch (name[1])
2193 case 'L': strarray_add( &ret, name ); continue;
2194 case 'l': name += 2; break;
2195 default: continue;
2198 else name = get_base_name( name );
2200 for (j = 0; j < subdirs.count; j++)
2202 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2203 lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
2204 else
2205 lib = get_static_lib( submakes[j], name );
2206 if (lib) break;
2209 if (lib)
2211 const char *ext = NULL;
2213 if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
2214 else if (is_cross) ext = ".cross.a";
2215 if (ext) lib = replace_extension( lib, ".a", ext );
2216 strarray_add_uniq( deps, lib );
2217 strarray_add( &ret, lib );
2219 else strarray_add( &ret, strmake( "-l%s", name ));
2221 return ret;
2225 /*******************************************************************
2226 * add_install_rule
2228 static void add_install_rule( struct makefile *make, const char *target,
2229 const char *file, const char *dest )
2231 if (strarray_exists( &make->install_lib, target ) ||
2232 strarray_exists( &top_install_lib, make->obj_dir ) ||
2233 strarray_exists( &top_install_lib, obj_dir_path( make, target )))
2235 strarray_add( &make->install_rules[INSTALL_LIB], file );
2236 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2238 else if (strarray_exists( &make->install_dev, target ) ||
2239 strarray_exists( &top_install_dev, make->obj_dir ) ||
2240 strarray_exists( &top_install_dev, obj_dir_path( make, target )))
2242 strarray_add( &make->install_rules[INSTALL_DEV], file );
2243 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2248 /*******************************************************************
2249 * get_include_install_path
2251 * Determine the installation path for a given include file.
2253 static const char *get_include_install_path( const char *name )
2255 if (!strncmp( name, "wine/", 5 )) return name + 5;
2256 if (!strncmp( name, "msvcrt/", 7 )) return name;
2257 return strmake( "windows/%s", name );
2261 /*******************************************************************
2262 * get_shared_library_name
2264 * Determine possible names for a shared library with a version number.
2266 static struct strarray get_shared_lib_names( const char *libname )
2268 struct strarray ret = empty_strarray;
2269 const char *ext, *p;
2270 char *name, *first, *second;
2271 size_t len = 0;
2273 strarray_add( &ret, libname );
2275 for (p = libname; (p = strchr( p, '.' )); p++)
2276 if ((len = strspn( p + 1, "0123456789." ))) break;
2278 if (!len) return ret;
2279 ext = p + 1 + len;
2280 if (*ext && ext[-1] == '.') ext--;
2282 /* keep only the first group of digits */
2283 name = xstrdup( libname );
2284 first = name + (p - libname);
2285 if ((second = strchr( first + 1, '.' )))
2287 strcpy( second, ext );
2288 strarray_add( &ret, xstrdup( name ));
2290 return ret;
2294 /*******************************************************************
2295 * get_source_defines
2297 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2298 const char *obj )
2300 unsigned int i;
2301 struct strarray ret = empty_strarray;
2303 strarray_addall( &ret, make->include_args );
2304 if (source->use_msvcrt)
2305 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2306 for (i = 0; i < make->include_paths.count; i++)
2307 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2308 strarray_addall( &ret, make->define_args );
2309 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2310 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2311 return ret;
2315 /*******************************************************************
2316 * remove_warning_flags
2318 static struct strarray remove_warning_flags( struct strarray flags )
2320 unsigned int i;
2321 struct strarray ret = empty_strarray;
2323 for (i = 0; i < flags.count; i++)
2324 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2325 strarray_add( &ret, flags.str[i] );
2326 return ret;
2330 /*******************************************************************
2331 * get_debug_file
2333 static const char *get_debug_file( struct makefile *make, const char *name )
2335 const char *debug_file = NULL;
2336 if (!make->is_cross || !crossdebug) return NULL;
2337 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2338 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2339 if (debug_file) strarray_add( &make->debug_files, debug_file );
2340 return debug_file;
2344 /*******************************************************************
2345 * cmd_prefix
2347 static const char *cmd_prefix( const char *cmd )
2349 if (!silent_rules) return "";
2350 return strmake( "$(quiet_%s)", cmd );
2354 /*******************************************************************
2355 * output_winegcc_command
2357 static void output_winegcc_command( struct makefile *make, int is_cross )
2359 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2360 output_filename( "--wine-objdir ." );
2361 if (tools_dir)
2363 output_filename( "--winebuild" );
2364 output_filename( tools_path( make, "winebuild" ));
2366 if (is_cross)
2368 output_filename( "-b" );
2369 output_filename( crosstarget );
2370 output_filename( "--lib-suffix=.cross.a" );
2372 else
2374 output_filenames( target_flags );
2375 output_filenames( lddll_flags );
2380 /*******************************************************************
2381 * output_symlink_rule
2383 * Output a rule to create a symlink.
2385 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2387 const char *name = strrchr( link_name, '/' );
2388 char *dir = NULL;
2390 if (name)
2392 dir = xstrdup( link_name );
2393 dir[name - link_name] = 0;
2396 output( "\t%s", cmd_prefix( "LN" ));
2397 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2398 output( "rm -f %s && ", link_name );
2400 /* dest path with a directory needs special handling if ln -s isn't supported */
2401 if (dir && strcmp( ln_s, "ln -s" ))
2402 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2403 else
2404 output( "%s %s %s\n", ln_s, src_name, link_name );
2406 free( dir );
2410 /*******************************************************************
2411 * output_srcdir_symlink
2413 * Output rule to create a symlink back to the source directory, for source files
2414 * that are needed at run-time.
2416 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2418 char *src_file, *dst_file, *src_name;
2420 if (!make->src_dir) return;
2421 src_file = src_dir_path( make, obj );
2422 dst_file = obj_dir_path( make, obj );
2423 output( "%s: %s\n", dst_file, src_file );
2425 src_name = src_file;
2426 if (src_name[0] != '/' && make->obj_dir)
2427 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2429 output_symlink_rule( src_name, dst_file, 0 );
2430 strarray_add( &make->all_targets, obj );
2434 /*******************************************************************
2435 * output_install_commands
2437 static void output_install_commands( struct makefile *make, struct strarray files )
2439 unsigned int i;
2440 char *install_sh = root_src_dir_path( "tools/install-sh" );
2442 for (i = 0; i < files.count; i += 2)
2444 const char *file = files.str[i];
2445 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2447 switch (*files.str[i + 1])
2449 case 'c': /* cross-compiled program */
2450 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2451 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2452 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2453 break;
2454 case 'd': /* data file */
2455 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2456 install_sh, obj_dir_path( make, file ), dest );
2457 break;
2458 case 'D': /* data file in source dir */
2459 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2460 install_sh, src_dir_path( make, file ), dest );
2461 break;
2462 case 'p': /* program file */
2463 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2464 install_sh, obj_dir_path( make, file ), dest );
2465 break;
2466 case 's': /* script */
2467 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2468 install_sh, obj_dir_path( make, file ), dest );
2469 break;
2470 case 'S': /* script in source dir */
2471 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2472 install_sh, src_dir_path( make, file ), dest );
2473 break;
2474 case 't': /* script in tools dir */
2475 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2476 install_sh, tools_dir_path( make, files.str[i] ), dest );
2477 break;
2478 case 'y': /* symlink */
2479 output_symlink_rule( files.str[i], dest, 1 );
2480 break;
2481 default:
2482 assert(0);
2484 strarray_add( &make->uninstall_files, dest );
2489 /*******************************************************************
2490 * output_install_rules
2492 * Rules are stored as a (file,dest) pair of values.
2493 * The first char of dest indicates the type of install.
2495 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2497 unsigned int i;
2498 struct strarray files = make->install_rules[rules];
2499 struct strarray targets = empty_strarray;
2501 if (!files.count) return;
2503 for (i = 0; i < files.count; i += 2)
2505 const char *file = files.str[i];
2506 switch (*files.str[i + 1])
2508 case 'c': /* cross-compiled program */
2509 case 'd': /* data file */
2510 case 'p': /* program file */
2511 case 's': /* script */
2512 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2513 break;
2514 case 't': /* script in tools dir */
2515 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2516 break;
2520 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
2521 output_filenames( targets );
2522 output( "\n" );
2523 output_install_commands( make, files );
2524 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2525 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
2529 static int cmp_string_length( const char **a, const char **b )
2531 int paths_a = 0, paths_b = 0;
2532 const char *p;
2534 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2535 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2536 if (paths_b != paths_a) return paths_b - paths_a;
2537 return strcmp( *a, *b );
2540 /*******************************************************************
2541 * output_uninstall_rules
2543 static void output_uninstall_rules( struct makefile *make )
2545 static const char *dirs_order[] =
2546 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2548 struct strarray uninstall_dirs = empty_strarray;
2549 unsigned int i, j;
2551 if (!make->uninstall_files.count) return;
2552 output( "uninstall::\n" );
2553 output_rm_filenames( make->uninstall_files );
2554 strarray_add_uniq( &make->phony_targets, "uninstall" );
2556 if (!subdirs.count) return;
2557 for (i = 0; i < make->uninstall_files.count; i++)
2559 char *dir = xstrdup( make->uninstall_files.str[i] );
2560 while (strchr( dir, '/' ))
2562 *strrchr( dir, '/' ) = 0;
2563 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2566 strarray_qsort( &uninstall_dirs, cmp_string_length );
2567 output( "\t-rmdir" );
2568 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2570 for (j = 0; j < uninstall_dirs.count; j++)
2572 if (!uninstall_dirs.str[j]) continue;
2573 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2574 continue;
2575 output_filename( uninstall_dirs.str[j] );
2576 uninstall_dirs.str[j] = NULL;
2579 for (j = 0; j < uninstall_dirs.count; j++)
2580 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2581 output( "\n" );
2585 /*******************************************************************
2586 * output_po_files
2588 static void output_po_files( struct makefile *make )
2590 const char *po_dir = src_dir_path( make, "po" );
2591 unsigned int i;
2593 if (linguas.count)
2595 for (i = 0; i < linguas.count; i++)
2596 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2597 output( ": %s/wine.pot\n", po_dir );
2598 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2599 cmd_prefix( "MSG" ), po_dir );
2600 output( "po/all:" );
2601 for (i = 0; i < linguas.count; i++)
2602 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2603 output( "\n" );
2605 output( "%s/wine.pot:", po_dir );
2606 output_filenames( make->pot_files );
2607 output( "\n" );
2608 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2609 output_filenames( make->pot_files );
2610 output( "\n" );
2611 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2615 /*******************************************************************
2616 * output_source_y
2618 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2620 /* add source file dependency for parallel makes */
2621 char *header = strmake( "%s.tab.h", obj );
2623 if (find_include_file( make, header ))
2625 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2626 output( "\t%s%s -o %s.tab.c -d %s\n",
2627 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2628 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2629 source->filename, obj_dir_path( make, header ));
2630 strarray_add( &make->clean_files, header );
2632 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2634 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2638 /*******************************************************************
2639 * output_source_l
2641 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2643 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2644 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2648 /*******************************************************************
2649 * output_source_h
2651 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2653 if (source->file->flags & FLAG_GENERATED)
2654 strarray_add( &make->all_targets, source->name );
2655 else
2656 add_install_rule( make, source->name, source->name,
2657 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2661 /*******************************************************************
2662 * output_source_rc
2664 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2666 struct strarray defines = get_source_defines( make, source, obj );
2667 const char *po_dir = NULL;
2668 unsigned int i;
2670 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2671 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2672 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2673 if (source->file->flags & FLAG_RC_PO)
2675 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2676 output( "%s.pot ", obj_dir_path( make, obj ) );
2678 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2679 output_filename( tools_path( make, "wrc" ));
2680 output_filenames( source->dependencies );
2681 output( "\n" );
2682 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2683 if (make->is_win16) output_filename( "-m16" );
2684 output_filename( "--nostdinc" );
2685 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2686 output_filenames( defines );
2687 output_filename( source->filename );
2688 output( "\n" );
2689 if (po_dir)
2691 output( "%s.res:", obj_dir_path( make, obj ));
2692 for (i = 0; i < linguas.count; i++)
2693 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2694 output( "\n" );
2699 /*******************************************************************
2700 * output_source_mc
2702 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2704 unsigned int i;
2705 char *obj_path = obj_dir_path( make, obj );
2707 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2708 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2709 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2710 output_filename( tools_path( make, "wmc" ));
2711 output_filenames( source->dependencies );
2712 output( "\n" );
2713 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2714 if (linguas.count)
2716 output_filename( "--po-dir=po" );
2717 output( "\n" );
2718 output( "%s.res:", obj_dir_path( make, obj ));
2719 for (i = 0; i < linguas.count; i++)
2720 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2722 output( "\n" );
2726 /*******************************************************************
2727 * output_source_res
2729 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2731 strarray_add( &make->res_files, source->name );
2735 /*******************************************************************
2736 * output_source_idl
2738 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2740 struct strarray defines = get_source_defines( make, source, obj );
2741 struct strarray targets = empty_strarray;
2742 char *dest;
2743 unsigned int i;
2745 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2746 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2748 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2750 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2751 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2752 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2753 strarray_add( &targets, dest );
2755 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2756 if (source->file->flags & FLAG_INSTALL)
2758 add_install_rule( make, source->name, xstrdup( source->name ),
2759 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2760 if (source->file->flags & FLAG_IDL_HEADER)
2761 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2762 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2764 if (!targets.count) return;
2766 output_filenames_obj_dir( make, targets );
2767 output( ": %s\n", tools_path( make, "widl" ));
2768 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2769 output_filenames( target_flags );
2770 output_filename( "--nostdinc" );
2771 output_filename( "-Ldlls/\\*" );
2772 output_filenames( defines );
2773 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2774 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2775 output_filename( source->filename );
2776 output( "\n" );
2777 output_filenames_obj_dir( make, targets );
2778 output( ": %s", source->filename );
2779 output_filenames( source->dependencies );
2780 for (i = 0; i < source->importlibdeps.count; i++)
2782 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2783 output_filename( obj_dir_path( submake, submake->module ));
2785 output( "\n" );
2789 /*******************************************************************
2790 * output_source_x
2792 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2794 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2795 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2796 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2797 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2798 if (source->file->flags & FLAG_INSTALL)
2800 add_install_rule( make, source->name, source->name,
2801 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2802 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2803 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2808 /*******************************************************************
2809 * output_source_sfd
2811 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2813 unsigned int i;
2814 char *ttf_obj = strmake( "%s.ttf", obj );
2815 char *ttf_file = src_dir_path( make, ttf_obj );
2817 if (fontforge && !make->src_dir)
2819 output( "%s: %s\n", ttf_file, source->filename );
2820 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2821 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2822 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2823 strarray_add( &make->maintainerclean_files, ttf_obj );
2825 if (source->file->flags & FLAG_INSTALL)
2827 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2828 output_srcdir_symlink( make, ttf_obj );
2831 if (source->file->flags & FLAG_SFD_FONTS)
2833 struct strarray *array = source->file->args;
2835 for (i = 0; i < array->count; i++)
2837 char *font = strtok( xstrdup(array->str[i]), " \t" );
2838 char *args = strtok( NULL, "" );
2840 strarray_add( &make->all_targets, xstrdup( font ));
2841 output( "%s: %s %s\n", obj_dir_path( make, font ),
2842 tools_path( make, "sfnt2fon" ), ttf_file );
2843 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2844 tools_path( make, "sfnt2fon" ), ttf_file, args );
2845 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2851 /*******************************************************************
2852 * output_source_svg
2854 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2856 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2857 unsigned int i;
2859 if (convert && rsvg && icotool)
2861 for (i = 0; images[i]; i++)
2862 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2864 if (images[i])
2866 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2867 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2868 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2869 root_src_dir_path( "tools/buildimage" ), source->filename );
2870 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2876 /*******************************************************************
2877 * output_source_nls
2879 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2881 add_install_rule( make, source->name, source->name,
2882 strmake( "D$(nlsdir)/%s", source->name ));
2883 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2887 /*******************************************************************
2888 * output_source_desktop
2890 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2892 add_install_rule( make, source->name, source->name,
2893 strmake( "D$(datadir)/applications/%s", source->name ));
2897 /*******************************************************************
2898 * output_source_po
2900 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2902 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2903 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
2904 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2908 /*******************************************************************
2909 * output_source_in
2911 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2913 unsigned int i;
2915 if (strendswith( obj, ".man" ) && source->file->args)
2917 struct strarray symlinks;
2918 char *dir, *dest = replace_extension( obj, ".man", "" );
2919 char *lang = strchr( dest, '.' );
2920 char *section = source->file->args;
2921 if (lang)
2923 *lang++ = 0;
2924 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2926 else dir = strmake( "$(mandir)/man%s", section );
2927 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2928 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2929 for (i = 0; i < symlinks.count; i++)
2930 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2931 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2932 free( dest );
2933 free( dir );
2935 strarray_add( &make->in_files, xstrdup(obj) );
2936 strarray_add( &make->all_targets, xstrdup(obj) );
2937 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2938 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
2939 output( "%s:", obj_dir_path( make, obj ));
2940 output_filenames( source->dependencies );
2941 output( "\n" );
2942 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2946 /*******************************************************************
2947 * output_source_spec
2949 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2951 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2952 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
2953 struct strarray all_libs, dep_libs = empty_strarray;
2954 char *dll_name, *obj_name, *output_file;
2955 const char *debug_file;
2957 if (!imports.count) imports = make->imports;
2958 if (!dll_flags.count) dll_flags = make->extradllflags;
2959 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) imports = add_default_imports( make, imports );
2961 all_libs = add_import_libs( make, &dep_libs, imports, 0, make->is_cross );
2962 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
2963 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
2964 output_file = obj_dir_path( make, dll_name );
2966 strarray_add( &make->clean_files, dll_name );
2967 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2968 output( "%s.res:", obj_dir_path( make, obj ));
2969 output_filename( obj_dir_path( make, dll_name ));
2970 output_filename( tools_path( make, "wrc" ));
2971 output( "\n" );
2972 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
2973 tools_path( make, "wrc" ));
2975 output( "%s:", output_file);
2976 output_filename( source->filename );
2977 output_filename( obj_name );
2978 output_filenames( dep_libs );
2979 output_filename( tools_path( make, "winebuild" ));
2980 output_filename( tools_path( make, "winegcc" ));
2981 output( "\n" );
2982 output_winegcc_command( make, make->is_cross );
2983 output_filename( "-s" );
2984 output_filenames( dll_flags );
2985 output_filename( "-shared" );
2986 output_filename( source->filename );
2987 output_filename( obj_name );
2988 if ((debug_file = get_debug_file( make, dll_name )))
2989 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
2990 output_filenames( all_libs );
2991 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
2992 output( "\n" );
2996 /*******************************************************************
2997 * output_source_default
2999 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3001 struct strarray defines = get_source_defines( make, source, obj );
3002 int is_dll_src = (make->testdll &&
3003 strendswith( source->name, ".c" ) &&
3004 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3005 int need_cross = (crosstarget &&
3006 !(source->file->flags & FLAG_C_UNIX) &&
3007 (make->is_cross || make->staticlib ||
3008 (source->file->flags & FLAG_C_IMPLIB)));
3009 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
3010 (!need_cross ||
3011 (source->file->flags & FLAG_C_IMPLIB) ||
3012 (make->staticlib && !make->extlib)));
3014 if ((source->file->flags & FLAG_GENERATED) &&
3015 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3016 strarray_add( &make->clean_files, source->basename );
3018 if (need_obj)
3020 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3021 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3022 else if (source->file->flags & FLAG_C_IMPLIB)
3023 strarray_add( &make->implib_files, strmake( "%s.o", obj ));
3024 else if (!is_dll_src)
3025 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3026 else
3027 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3028 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3029 output( "\t%s$(CC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3030 output_filenames( defines );
3031 output_filenames( make->extlib ? extra_cflags_extlib : extra_cflags );
3032 if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
3034 output_filenames( unix_dllflags );
3036 else if (make->module || make->testdll)
3038 output_filenames( dll_flags );
3039 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3040 if (!*dll_ext && make->module && is_crt_module( make->module ))
3041 output_filename( "-fno-builtin" );
3043 output_filenames( cpp_flags );
3044 output_filename( "$(CFLAGS)" );
3045 output( "\n" );
3047 if (need_cross)
3049 if (source->file->flags & FLAG_C_IMPLIB)
3050 strarray_add( &make->crossimplib_files, strmake( "%s.cross.o", obj ));
3051 else if (!is_dll_src)
3052 strarray_add( &make->crossobj_files, strmake( "%s.cross.o", obj ));
3053 else
3054 strarray_add( &make->clean_files, strmake( "%s.cross.o", obj ));
3055 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3056 output( "\t%s$(CROSSCC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3057 output_filenames( defines );
3058 output_filenames( make->extlib ? extra_cross_cflags_extlib : extra_cross_cflags );
3059 if (make->module && is_crt_module( make->module ))
3060 output_filename( "-fno-builtin" );
3061 /* force -Wformat when using 'long' types, until all modules have been converted
3062 * and we can remove -Wno-format */
3063 if (!make->extlib && strarray_exists( &extra_cross_cflags, "-Wno-format" ) &&
3064 !strarray_exists( &defines, "-DWINE_NO_LONG_TYPES" ))
3065 output_filename( "-Wformat" );
3066 output_filenames( cpp_flags );
3067 output_filename( "$(CROSSCFLAGS)" );
3068 output( "\n" );
3070 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3071 !(source->file->flags & FLAG_GENERATED))
3073 strarray_add( &make->test_files, obj );
3074 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3075 output( "%s.ok:\n", obj_dir_path( make, obj ));
3076 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3077 cmd_prefix( "TEST" ),
3078 root_src_dir_path( "tools/runtest" ), make->testdll,
3079 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3080 make->is_cross ? "" : dll_ext, obj );
3082 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3083 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3084 output( ":" );
3085 output_filenames( source->dependencies );
3086 output( "\n" );
3090 /* dispatch table to output rules for a single source file */
3091 static const struct
3093 const char *ext;
3094 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3095 } output_source_funcs[] =
3097 { "y", output_source_y },
3098 { "l", output_source_l },
3099 { "h", output_source_h },
3100 { "rh", output_source_h },
3101 { "inl", output_source_h },
3102 { "rc", output_source_rc },
3103 { "mc", output_source_mc },
3104 { "res", output_source_res },
3105 { "idl", output_source_idl },
3106 { "sfd", output_source_sfd },
3107 { "svg", output_source_svg },
3108 { "nls", output_source_nls },
3109 { "desktop", output_source_desktop },
3110 { "po", output_source_po },
3111 { "in", output_source_in },
3112 { "x", output_source_x },
3113 { "spec", output_source_spec },
3114 { NULL, output_source_default }
3118 /*******************************************************************
3119 * get_unix_lib_name
3121 static char *get_unix_lib_name( struct makefile *make )
3123 struct incl_file *source;
3125 if (!*dll_ext) return NULL;
3126 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3128 if (!(source->file->flags & FLAG_C_UNIX)) continue;
3129 return strmake( "%s%s", get_base_name( make->module ), dll_ext );
3131 return NULL;
3135 /*******************************************************************
3136 * output_module
3138 static void output_module( struct makefile *make )
3140 struct strarray all_libs = empty_strarray;
3141 struct strarray dep_libs = empty_strarray;
3142 struct strarray imports = make->imports;
3143 const char *module_name = make->module;
3144 const char *debug_file;
3145 const char *delay_load = delay_load_flag;
3146 char *spec_file = NULL;
3147 unsigned int i;
3149 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3151 if (!make->data_only)
3153 if (*dll_ext && !make->is_cross && !make->data_only)
3154 module_name = strmake( "%s%s", make->module, dll_ext );
3156 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
3157 imports = add_default_imports( make, imports );
3159 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, make->is_cross ));
3160 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, 0, make->is_cross ));
3162 if (!make->use_msvcrt)
3164 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3165 strarray_addall( &all_libs, libs );
3168 if (!make->is_cross && *dll_ext) delay_load = "-Wl,-delayload,";
3169 if (delay_load)
3171 for (i = 0; i < make->delayimports.count; i++)
3172 strarray_add( &all_libs, strmake( "%s%s%s", delay_load, make->delayimports.str[i],
3173 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3176 strarray_add( &make->all_targets, module_name );
3178 if (make->data_only)
3179 add_install_rule( make, make->module, module_name,
3180 strmake( "d%s/%s", *dll_ext ? pe_dir : "$(dlldir)", module_name ));
3181 else if (make->is_cross)
3182 add_install_rule( make, make->module, module_name, strmake( "c%s/%s", pe_dir, module_name ));
3183 else if (*dll_ext)
3184 add_install_rule( make, make->module, module_name, strmake( "p%s/%s", so_dir, module_name ));
3185 else
3186 add_install_rule( make, make->module, module_name,
3187 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", module_name ));
3189 output( "%s:", obj_dir_path( make, module_name ));
3190 if (spec_file) output_filename( spec_file );
3191 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3192 output_filenames_obj_dir( make, make->res_files );
3193 output_filenames( dep_libs );
3194 output_filename( tools_path( make, "winebuild" ));
3195 output_filename( tools_path( make, "winegcc" ));
3196 output( "\n" );
3197 output_winegcc_command( make, make->is_cross );
3198 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3199 if (spec_file)
3201 output_filename( "-shared" );
3202 output_filename( spec_file );
3204 output_filenames( make->extradllflags );
3205 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3206 output_filenames_obj_dir( make, make->res_files );
3207 debug_file = get_debug_file( make, make->module );
3208 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3209 output_filenames( all_libs );
3210 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3211 output( "\n" );
3213 if (*dll_ext && make->is_exe && !make->is_win16 && strendswith( make->module, ".exe" ))
3215 char *binary = replace_extension( make->module, ".exe", "" );
3216 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3221 /*******************************************************************
3222 * output_fake_module
3224 static void output_fake_module( struct makefile *make )
3226 char *spec_file = NULL;
3228 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3230 strarray_add( &make->all_targets, make->module );
3231 add_install_rule( make, make->module, make->module, strmake( "d%s/%s", pe_dir, make->module ));
3233 output( "%s:", obj_dir_path( make, make->module ));
3234 if (spec_file) output_filename( spec_file );
3235 output_filenames_obj_dir( make, make->res_files );
3236 output_filename( tools_path( make, "winebuild" ));
3237 output_filename( tools_path( make, "winegcc" ));
3238 output( "\n" );
3239 output_winegcc_command( make, make->is_cross );
3240 output_filename( "-Wb,--fake-module" );
3241 if (spec_file)
3243 output_filename( "-shared" );
3244 output_filename( spec_file );
3246 output_filenames( make->extradllflags );
3247 output_filenames_obj_dir( make, make->res_files );
3248 output( "\n" );
3252 /*******************************************************************
3253 * output_import_lib
3255 static void output_import_lib( struct makefile *make )
3257 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3258 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3260 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3261 if (!*dll_ext && needs_delay_lib( make ))
3263 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3264 output( "%s.delay.a ", importlib_path );
3266 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3267 output_filenames_obj_dir( make, make->implib_files );
3268 output( "\n" );
3269 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3270 output_filenames( target_flags );
3271 if (make->is_win16) output_filename( "-m16" );
3272 output_filename( "--export" );
3273 output_filename( spec_file );
3274 output_filenames_obj_dir( make, make->implib_files );
3275 output( "\n" );
3276 add_install_rule( make, make->importlib,
3277 strmake( "lib%s.a", make->importlib ),
3278 strmake( "d%s/lib%s.a", so_dir, make->importlib ));
3280 if (crosstarget)
3282 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3283 output_filename( strmake( "%s.cross.a", importlib_path ));
3284 if (needs_delay_lib( make ))
3286 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3287 output_filename( strmake( "%s.delay.a", importlib_path ));
3289 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3290 output_filenames_obj_dir( make, make->crossimplib_files );
3291 output( "\n" );
3292 output( "\t%s%s -b %s -w --implib -o $@", cmd_prefix( "BUILD" ),
3293 tools_path( make, "winebuild" ), crosstarget );
3294 if (make->is_win16) output_filename( "-m16" );
3295 output_filename( "--export" );
3296 output_filename( spec_file );
3297 output_filenames_obj_dir( make, make->crossimplib_files );
3298 output( "\n" );
3299 add_install_rule( make, make->importlib,
3300 strmake( "lib%s.cross.a", make->importlib ),
3301 strmake( "d%s/lib%s.a", pe_dir, make->importlib ));
3306 /*******************************************************************
3307 * output_unix_lib
3309 static void output_unix_lib( struct makefile *make )
3311 struct strarray unix_libs = empty_strarray;
3312 struct strarray unix_deps = empty_strarray;
3313 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3315 if (!make->native_unix_lib)
3317 struct strarray unix_imports = empty_strarray;
3319 strarray_add( &unix_imports, "ntdll" );
3320 strarray_add( &unix_deps, obj_dir_path( top_makefile, "dlls/ntdll/ntdll.so" ));
3321 strarray_add( &unix_imports, "winecrt0" );
3322 if (spec_file) strarray_add( &unix_deps, spec_file );
3324 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 0 ));
3325 strarray_addall( &unix_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3326 strarray_addall( &unix_libs, libs );
3328 else unix_libs = add_unix_libraries( make, &unix_deps );
3330 strarray_add( &make->all_targets, make->unixlib );
3331 add_install_rule( make, make->module, make->unixlib, strmake( "p%s/%s", so_dir, make->unixlib ));
3332 output( "%s:", obj_dir_path( make, make->unixlib ));
3333 output_filenames_obj_dir( make, make->unixobj_files );
3334 output_filenames( unix_deps );
3336 if (make->native_unix_lib)
3338 output( "\n" );
3339 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3340 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3342 else
3344 output_filename( tools_path( make, "winebuild" ));
3345 output_filename( tools_path( make, "winegcc" ));
3346 output( "\n" );
3347 output_winegcc_command( make, 0 );
3348 output_filename( "-munix" );
3349 output_filename( "-shared" );
3350 if (spec_file) output_filename( spec_file );
3352 output_filenames_obj_dir( make, make->unixobj_files );
3353 output_filenames( unix_libs );
3354 output_filename( "$(LDFLAGS)" );
3355 output( "\n" );
3359 /*******************************************************************
3360 * output_static_lib
3362 static void output_static_lib( struct makefile *make )
3364 if (!crosstarget || !make->extlib)
3366 strarray_add( &make->clean_files, make->staticlib );
3367 output( "%s: %s", obj_dir_path( make, make->staticlib ), tools_path( make, "winebuild" ));
3368 output_filenames_obj_dir( make, make->object_files );
3369 output_filenames_obj_dir( make, make->unixobj_files );
3370 output( "\n" );
3371 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3372 output_filenames( target_flags );
3373 output_filenames_obj_dir( make, make->object_files );
3374 output_filenames_obj_dir( make, make->unixobj_files );
3375 output( "\n" );
3376 add_install_rule( make, make->staticlib, make->staticlib,
3377 strmake( "d%s/%s", so_dir, make->staticlib ));
3379 if (crosstarget)
3381 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3383 strarray_add( &make->clean_files, name );
3384 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3385 output_filenames_obj_dir( make, make->crossobj_files );
3386 output( "\n" );
3387 output( "\t%s%s -b %s -w --staticlib -o $@", cmd_prefix( "BUILD" ),
3388 tools_path( make, "winebuild" ), crosstarget );
3389 output_filenames_obj_dir( make, make->crossobj_files );
3390 output( "\n" );
3391 if (!make->extlib)
3392 add_install_rule( make, make->staticlib, name,
3393 strmake( "d%s/%s", pe_dir, make->staticlib ));
3398 /*******************************************************************
3399 * output_shared_lib
3401 static void output_shared_lib( struct makefile *make )
3403 unsigned int i;
3404 char *basename, *p;
3405 struct strarray names = get_shared_lib_names( make->sharedlib );
3406 struct strarray all_libs = empty_strarray;
3407 struct strarray dep_libs = empty_strarray;
3409 basename = xstrdup( make->sharedlib );
3410 if ((p = strchr( basename, '.' ))) *p = 0;
3412 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3413 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3414 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3415 strarray_addall( &all_libs, libs );
3417 output( "%s:", obj_dir_path( make, make->sharedlib ));
3418 output_filenames_obj_dir( make, make->object_files );
3419 output_filenames( dep_libs );
3420 output( "\n" );
3421 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3422 output_filenames_obj_dir( make, make->object_files );
3423 output_filenames( all_libs );
3424 output_filename( "$(LDFLAGS)" );
3425 output( "\n" );
3426 add_install_rule( make, make->sharedlib, make->sharedlib, strmake( "p%s/%s", so_dir, make->sharedlib ));
3427 for (i = 1; i < names.count; i++)
3429 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3430 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3431 add_install_rule( make, names.str[i], names.str[i-1], strmake( "y%s/%s", so_dir, names.str[i] ));
3433 strarray_addall( &make->all_targets, names );
3437 /*******************************************************************
3438 * output_test_module
3440 static void output_test_module( struct makefile *make )
3442 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3443 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3444 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3445 struct strarray dep_libs = empty_strarray;
3446 struct strarray all_libs = add_import_libs( make, &dep_libs, add_default_imports( make, make->imports ),
3447 0, make->is_cross );
3448 struct makefile *parent = get_parent_makefile( make );
3449 const char *ext = make->is_cross ? "" : dll_ext;
3450 const char *debug_file;
3451 char *output_file;
3453 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3454 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3455 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3456 output( "%s:\n", output_file );
3457 output_winegcc_command( make, make->is_cross );
3458 output_filenames( make->extradllflags );
3459 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3460 output_filenames_obj_dir( make, make->res_files );
3461 if ((debug_file = get_debug_file( make, testmodule )))
3462 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3463 output_filenames( all_libs );
3464 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3465 output( "\n" );
3466 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3467 output_winegcc_command( make, make->is_cross );
3468 output_filename( "-s" );
3469 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3470 output_filenames( make->extradllflags );
3471 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3472 output_filenames_obj_dir( make, make->res_files );
3473 output_filenames( all_libs );
3474 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3475 output( "\n" );
3476 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3477 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3478 output_filenames_obj_dir( make, make->res_files );
3479 output_filenames( dep_libs );
3480 output_filename( tools_path( make, "winebuild" ));
3481 output_filename( tools_path( make, "winegcc" ));
3482 output( "\n" );
3484 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3485 output( "\t%secho \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3486 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3488 output_filenames_obj_dir( make, make->ok_files );
3489 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3490 if (parent)
3492 output_filename( parent->is_cross ? obj_dir_path( parent, make->testdll )
3493 : strmake( "%s%s", obj_dir_path( parent, make->testdll ), dll_ext ));
3494 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3496 output( "\n" );
3497 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3498 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3499 output( "\n" );
3500 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3501 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3502 output( "%s::\n", obj_dir_path( make, "testclean" ));
3503 output( "\trm -f" );
3504 output_filenames_obj_dir( make, make->ok_files );
3505 output( "\n" );
3506 strarray_addall( &make->clean_files, make->ok_files );
3507 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3511 /*******************************************************************
3512 * output_programs
3514 static void output_programs( struct makefile *make )
3516 unsigned int i, j;
3518 for (i = 0; i < make->programs.count; i++)
3520 char *program_installed = NULL;
3521 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3522 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3523 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3524 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3525 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3527 if (!objs.count) objs = make->object_files;
3528 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3530 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3531 strarray_addall( &all_libs, libs );
3534 output( "%s:", obj_dir_path( make, program ) );
3535 output_filenames_obj_dir( make, objs );
3536 output_filenames( deps );
3537 output( "\n" );
3538 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3539 output_filenames_obj_dir( make, objs );
3540 output_filenames( all_libs );
3541 output_filename( "$(LDFLAGS)" );
3542 output( "\n" );
3543 strarray_add( &make->all_targets, program );
3545 for (j = 0; j < symlinks.count; j++)
3547 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3548 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3550 strarray_addall( &make->all_targets, symlinks );
3552 add_install_rule( make, program, program_installed ? program_installed : program,
3553 strmake( "p$(bindir)/%s", program ));
3554 for (j = 0; j < symlinks.count; j++)
3555 add_install_rule( make, symlinks.str[j], program,
3556 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3561 /*******************************************************************
3562 * output_subdirs
3564 static void output_subdirs( struct makefile *make )
3566 struct strarray all_targets = empty_strarray;
3567 struct strarray makefile_deps = empty_strarray;
3568 struct strarray clean_files = empty_strarray;
3569 struct strarray testclean_files = empty_strarray;
3570 struct strarray distclean_files = empty_strarray;
3571 struct strarray dependencies = empty_strarray;
3572 struct strarray install_lib_deps = empty_strarray;
3573 struct strarray install_dev_deps = empty_strarray;
3574 struct strarray tooldeps_deps = empty_strarray;
3575 struct strarray buildtest_deps = empty_strarray;
3576 unsigned int i;
3578 strarray_addall( &clean_files, make->clean_files );
3579 strarray_addall( &distclean_files, make->distclean_files );
3580 strarray_addall( &all_targets, make->all_targets );
3581 for (i = 0; i < subdirs.count; i++)
3583 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3584 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3585 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3586 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3587 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3588 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3589 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3590 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3591 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3593 if (submakes[i]->disabled) continue;
3595 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3596 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3597 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3598 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3599 if (submakes[i]->testdll)
3600 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3601 if (submakes[i]->install_rules[INSTALL_LIB].count)
3602 strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
3603 if (submakes[i]->install_rules[INSTALL_DEV].count)
3604 strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
3606 strarray_addall( &dependencies, makefile_deps );
3607 output( "all:" );
3608 output_filenames( all_targets );
3609 output( "\n" );
3610 output( "Makefile:" );
3611 output_filenames( makefile_deps );
3612 output( "\n" );
3613 output_filenames( dependencies );
3614 output( ":\n" );
3615 if (install_lib_deps.count)
3617 output( "install install-lib::" );
3618 output_filenames( install_lib_deps );
3619 output( "\n" );
3620 strarray_add_uniq( &make->phony_targets, "install" );
3621 strarray_add_uniq( &make->phony_targets, "install-lib" );
3623 if (install_dev_deps.count)
3625 output( "install install-dev::" );
3626 output_filenames( install_dev_deps );
3627 output( "\n" );
3628 strarray_add_uniq( &make->phony_targets, "install" );
3629 strarray_add_uniq( &make->phony_targets, "install-dev" );
3631 output_uninstall_rules( make );
3632 if (buildtest_deps.count)
3634 output( "buildtests:" );
3635 output_filenames( buildtest_deps );
3636 output( "\n" );
3637 strarray_add_uniq( &make->phony_targets, "buildtests" );
3639 output( "check test:" );
3640 output_filenames( testclean_files );
3641 output( "\n" );
3642 strarray_add_uniq( &make->phony_targets, "check" );
3643 strarray_add_uniq( &make->phony_targets, "test" );
3645 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3647 output( "clean::\n");
3648 output_rm_filenames( clean_files );
3649 output( "testclean::\n");
3650 output_rm_filenames( testclean_files );
3651 output( "distclean::\n");
3652 output_rm_filenames( distclean_files );
3653 output( "maintainer-clean::\n");
3654 output_rm_filenames( make->maintainerclean_files );
3655 strarray_add_uniq( &make->phony_targets, "distclean" );
3656 strarray_add_uniq( &make->phony_targets, "testclean" );
3657 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3659 if (tooldeps_deps.count)
3661 output( "__tooldeps__:" );
3662 output_filenames( tooldeps_deps );
3663 output( "\n" );
3664 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3667 if (make->phony_targets.count)
3669 output( ".PHONY:" );
3670 output_filenames( make->phony_targets );
3671 output( "\n" );
3676 /*******************************************************************
3677 * output_sources
3679 static void output_sources( struct makefile *make )
3681 struct strarray all_targets = empty_strarray;
3682 struct incl_file *source;
3683 unsigned int i, j;
3685 strarray_add_uniq( &make->phony_targets, "all" );
3687 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3689 char *obj = xstrdup( source->name );
3690 char *ext = get_extension( obj );
3692 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3693 *ext++ = 0;
3695 for (j = 0; output_source_funcs[j].ext; j++)
3696 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3698 output_source_funcs[j].fn( make, source, obj );
3699 strarray_addall_uniq( &make->dependencies, source->dependencies );
3702 /* special case for winetest: add resource files from other test dirs */
3703 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3705 struct strarray tests = enable_tests;
3706 if (!tests.count)
3707 for (i = 0; i < subdirs.count; i++)
3708 if (submakes[i]->testdll && !submakes[i]->disabled)
3709 strarray_add( &tests, submakes[i]->testdll );
3710 for (i = 0; i < tests.count; i++)
3711 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3714 if (make->dlldata_files.count)
3716 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3717 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3718 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3719 output_filenames( make->dlldata_files );
3720 output( "\n" );
3723 if (make->staticlib) output_static_lib( make );
3724 else if (make->module)
3726 output_module( make );
3727 if (*dll_ext && !make->is_cross && !make->data_only) output_fake_module( make );
3728 if (make->unixlib) output_unix_lib( make );
3729 if (make->importlib) output_import_lib( make );
3731 else if (make->testdll) output_test_module( make );
3732 else if (make->sharedlib) output_shared_lib( make );
3733 else if (make->programs.count) output_programs( make );
3735 for (i = 0; i < make->scripts.count; i++)
3736 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3737 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3739 for (i = 0; i < make->extra_targets.count; i++)
3740 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3741 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3742 else
3743 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3745 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3746 strarray_add( &make->distclean_files, "Makefile" );
3747 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3749 if (!make->obj_dir)
3750 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3751 else if (!strcmp( make->obj_dir, "po" ))
3752 strarray_add( &make->distclean_files, "LINGUAS" );
3754 strarray_addall( &make->clean_files, make->object_files );
3755 strarray_addall( &make->clean_files, make->crossobj_files );
3756 strarray_addall( &make->clean_files, make->implib_files );
3757 strarray_addall( &make->clean_files, make->crossimplib_files );
3758 strarray_addall( &make->clean_files, make->unixobj_files );
3759 strarray_addall( &make->clean_files, make->res_files );
3760 strarray_addall( &make->clean_files, make->pot_files );
3761 strarray_addall( &make->clean_files, make->debug_files );
3762 strarray_addall( &make->clean_files, make->all_targets );
3764 if (make == top_makefile)
3766 output_subdirs( make );
3767 return;
3770 strarray_addall( &all_targets, make->all_targets );
3771 strarray_addall( &all_targets, make->font_files );
3772 if (all_targets.count)
3774 output( "%s:", obj_dir_path( make, "all" ));
3775 output_filenames_obj_dir( make, all_targets );
3776 output( "\n" );
3777 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3779 output_install_rules( make, INSTALL_LIB, "install-lib" );
3780 output_install_rules( make, INSTALL_DEV, "install-dev" );
3782 if (make->clean_files.count)
3784 output( "%s::\n", obj_dir_path( make, "clean" ));
3785 output( "\trm -f" );
3786 output_filenames_obj_dir( make, make->clean_files );
3787 output( "\n" );
3788 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3793 /*******************************************************************
3794 * create_temp_file
3796 static FILE *create_temp_file( const char *orig )
3798 char *name = xmalloc( strlen(orig) + 13 );
3799 unsigned int i, id = getpid();
3800 int fd;
3801 FILE *ret = NULL;
3803 for (i = 0; i < 100; i++)
3805 sprintf( name, "%s.tmp%08x", orig, id );
3806 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3808 ret = fdopen( fd, "w" );
3809 break;
3811 if (errno != EEXIST) break;
3812 id += 7777;
3814 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3815 temp_file_name = name;
3816 return ret;
3820 /*******************************************************************
3821 * rename_temp_file
3823 static void rename_temp_file( const char *dest )
3825 int ret = rename( temp_file_name, dest );
3826 if (ret == -1 && errno == EEXIST)
3828 /* rename doesn't overwrite on windows */
3829 unlink( dest );
3830 ret = rename( temp_file_name, dest );
3832 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3833 temp_file_name = NULL;
3837 /*******************************************************************
3838 * are_files_identical
3840 static int are_files_identical( FILE *file1, FILE *file2 )
3842 for (;;)
3844 char buffer1[8192], buffer2[8192];
3845 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3846 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3847 if (size1 != size2) return 0;
3848 if (!size1) return feof( file1 ) && feof( file2 );
3849 if (memcmp( buffer1, buffer2, size1 )) return 0;
3854 /*******************************************************************
3855 * rename_temp_file_if_changed
3857 static void rename_temp_file_if_changed( const char *dest )
3859 FILE *file1, *file2;
3860 int do_rename = 1;
3862 if ((file1 = fopen( dest, "r" )))
3864 if ((file2 = fopen( temp_file_name, "r" )))
3866 do_rename = !are_files_identical( file1, file2 );
3867 fclose( file2 );
3869 fclose( file1 );
3871 if (!do_rename)
3873 unlink( temp_file_name );
3874 temp_file_name = NULL;
3876 else rename_temp_file( dest );
3880 /*******************************************************************
3881 * output_linguas
3883 static void output_linguas( const struct makefile *make )
3885 const char *dest = obj_dir_path( make, "LINGUAS" );
3886 struct incl_file *source;
3888 output_file = create_temp_file( dest );
3890 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3891 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3892 if (strendswith( source->name, ".po" ))
3893 output( "%s\n", replace_extension( source->name, ".po", "" ));
3895 if (fclose( output_file )) fatal_perror( "write" );
3896 output_file = NULL;
3897 rename_temp_file_if_changed( dest );
3901 /*******************************************************************
3902 * output_testlist
3904 static void output_testlist( const struct makefile *make )
3906 const char *dest = obj_dir_path( make, "testlist.c" );
3907 unsigned int i;
3909 output_file = create_temp_file( dest );
3911 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3912 output( "#define WIN32_LEAN_AND_MEAN\n" );
3913 output( "#include <windows.h>\n\n" );
3914 output( "#define STANDALONE\n" );
3915 output( "#include \"wine/test.h\"\n\n" );
3917 for (i = 0; i < make->test_files.count; i++)
3918 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3919 output( "\n" );
3920 output( "const struct test winetest_testlist[] =\n" );
3921 output( "{\n" );
3922 for (i = 0; i < make->test_files.count; i++)
3923 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3924 output( " { 0, 0 }\n" );
3925 output( "};\n" );
3927 if (fclose( output_file )) fatal_perror( "write" );
3928 output_file = NULL;
3929 rename_temp_file_if_changed( dest );
3933 /*******************************************************************
3934 * output_gitignore
3936 static void output_gitignore( const char *dest, struct strarray files )
3938 unsigned int i;
3940 output_file = create_temp_file( dest );
3942 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3943 for (i = 0; i < files.count; i++)
3945 if (!strchr( files.str[i], '/' )) output( "/" );
3946 output( "%s\n", files.str[i] );
3949 if (fclose( output_file )) fatal_perror( "write" );
3950 output_file = NULL;
3951 rename_temp_file( dest );
3955 /*******************************************************************
3956 * output_stub_makefile
3958 static void output_stub_makefile( struct makefile *make )
3960 struct strarray targets = empty_strarray;
3961 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
3963 if (make->obj_dir) create_dir( make->obj_dir );
3965 output_file_name = obj_dir_path( make, "Makefile" );
3966 output_file = create_temp_file( output_file_name );
3968 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
3970 if (make_var) output( "MAKE = %s\n\n", make_var );
3971 output( "all:\n" );
3973 if (make->all_targets.count) strarray_add( &targets, "all" );
3974 if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
3975 strarray_add( &targets, "install" );
3976 if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
3977 if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
3978 if (make->clean_files.count) strarray_add( &targets, "clean" );
3979 if (make->test_files.count)
3981 strarray_add( &targets, "check" );
3982 strarray_add( &targets, "test" );
3983 strarray_add( &targets, "testclean" );
3986 output_filenames( targets );
3987 output_filenames( make->clean_files );
3988 output( ":\n" );
3989 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
3990 output( ".PHONY:" );
3991 output_filenames( targets );
3992 output( "\n" );
3994 fclose( output_file );
3995 output_file = NULL;
3996 rename_temp_file( output_file_name );
4000 /*******************************************************************
4001 * output_silent_rules
4003 static void output_silent_rules(void)
4005 static const char *cmds[] =
4007 "BISON",
4008 "BUILD",
4009 "CC",
4010 "CCLD",
4011 "FLEX",
4012 "GEN",
4013 "LN",
4014 "MSG",
4015 "SED",
4016 "TEST",
4017 "WIDL",
4018 "WMC",
4019 "WRC"
4021 unsigned int i;
4023 output( "V = 0\n" );
4024 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++)
4026 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4027 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4028 output( "quiet_%s_1 =\n", cmds[i] );
4033 /*******************************************************************
4034 * output_top_makefile
4036 static void output_top_makefile( struct makefile *make )
4038 char buffer[1024];
4039 FILE *src_file;
4040 unsigned int i;
4041 int found = 0;
4043 output_file_name = obj_dir_path( make, output_makefile_name );
4044 output_file = create_temp_file( output_file_name );
4046 /* copy the contents of the source makefile */
4047 src_file = open_input_makefile( make );
4048 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4050 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4051 found = !strncmp( buffer, separator, strlen(separator) );
4053 if (fclose( src_file )) fatal_perror( "close" );
4054 input_file_name = NULL;
4056 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4058 if (silent_rules) output_silent_rules();
4059 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4060 output_sources( make );
4061 /* disable implicit rules */
4062 output( ".SUFFIXES:\n" );
4064 fclose( output_file );
4065 output_file = NULL;
4066 rename_temp_file( output_file_name );
4070 /*******************************************************************
4071 * output_dependencies
4073 static void output_dependencies( struct makefile *make )
4075 struct strarray ignore_files = empty_strarray;
4077 if (make->obj_dir) create_dir( make->obj_dir );
4079 if (make == top_makefile) output_top_makefile( make );
4080 else output_stub_makefile( make );
4082 strarray_addall( &ignore_files, make->distclean_files );
4083 strarray_addall( &ignore_files, make->clean_files );
4084 if (make->testdll) output_testlist( make );
4085 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4086 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4088 create_file_directories( make, ignore_files );
4090 output_file_name = NULL;
4094 /*******************************************************************
4095 * load_sources
4097 static void load_sources( struct makefile *make )
4099 static const char *source_vars[] =
4101 "SOURCES",
4102 "C_SRCS",
4103 "OBJC_SRCS",
4104 "RC_SRCS",
4105 "MC_SRCS",
4106 "IDL_SRCS",
4107 "BISON_SRCS",
4108 "LEX_SRCS",
4109 "HEADER_SRCS",
4110 "XTEMPLATE_SRCS",
4111 "SVG_SRCS",
4112 "FONT_SRCS",
4113 "IN_SRCS",
4114 "PO_SRCS",
4115 "MANPAGES",
4116 NULL
4118 const char **var;
4119 unsigned int i;
4120 struct strarray value;
4121 struct incl_file *file;
4123 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4124 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4126 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4127 make->module = get_expanded_make_variable( make, "MODULE" );
4128 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4129 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4130 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4131 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4132 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4133 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4135 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4136 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4137 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4138 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4139 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4140 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4141 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4142 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4144 if (make->extlib) make->staticlib = make->extlib;
4145 if (make->staticlib) make->module = make->staticlib;
4147 if (host_cpu)
4149 value = get_expanded_file_local_var( make, host_cpu, "EXTRADLLFLAGS" );
4150 if (value.count) make->extradllflags = value;
4153 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4154 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4155 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4156 make->use_msvcrt = (make->module || make->testdll || make->is_win16) &&
4157 !strarray_exists( &make->extradllflags, "-mcygwin" );
4158 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4159 strarray_exists( &make->extradllflags, "-mwindows" );
4160 make->native_unix_lib = !!make->unixlib;
4162 if (make->use_msvcrt) strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4164 if (make->module && !make->install_lib.count && !make->install_dev.count)
4166 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4167 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4168 else strarray_add( &make->install_lib, make->module );
4171 make->include_paths = empty_strarray;
4172 make->include_args = empty_strarray;
4173 make->define_args = empty_strarray;
4174 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4176 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4177 for (i = 0; i < value.count; i++)
4178 if (!strncmp( value.str[i], "-I", 2 ))
4179 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4180 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4181 strarray_add_uniq( &make->define_args, value.str[i] );
4182 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4184 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4185 if (make->src_dir)
4186 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4187 if (make->parent_dir)
4188 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4189 strarray_add( &make->include_args, "-Iinclude" );
4190 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4192 list_init( &make->sources );
4193 list_init( &make->includes );
4195 for (var = source_vars; *var; var++)
4197 value = get_expanded_make_var_array( make, *var );
4198 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4201 add_generated_sources( make );
4202 if (!make->unixlib) make->unixlib = get_unix_lib_name( make );
4204 if (make->use_msvcrt) strarray_add( &make->define_args, get_crt_define( make ));
4206 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4207 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4209 make->is_cross = crosstarget && make->use_msvcrt;
4211 if (!*dll_ext || make->is_cross)
4212 for (i = 0; i < make->delayimports.count; i++)
4213 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4217 /*******************************************************************
4218 * parse_makeflags
4220 static void parse_makeflags( const char *flags )
4222 const char *p = flags;
4223 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4225 while (*p)
4227 while (isspace(*p)) p++;
4228 var = buffer;
4229 while (*p && !isspace(*p))
4231 if (*p == '\\' && p[1]) p++;
4232 *var++ = *p++;
4234 *var = 0;
4235 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4240 /*******************************************************************
4241 * parse_option
4243 static int parse_option( const char *opt )
4245 if (opt[0] != '-')
4247 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4248 return 0;
4250 switch(opt[1])
4252 case 'f':
4253 if (opt[2]) output_makefile_name = opt + 2;
4254 break;
4255 case 'R':
4256 relative_dir_mode = 1;
4257 break;
4258 case 'S':
4259 silent_rules = 1;
4260 break;
4261 default:
4262 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4263 exit(1);
4265 return 1;
4269 /*******************************************************************
4270 * main
4272 int main( int argc, char *argv[] )
4274 const char *makeflags = getenv( "MAKEFLAGS" );
4275 int i, j;
4277 if (makeflags) parse_makeflags( makeflags );
4279 i = 1;
4280 while (i < argc)
4282 if (parse_option( argv[i] ))
4284 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4285 argc--;
4287 else i++;
4290 if (relative_dir_mode)
4292 char *relpath;
4294 if (argc != 3)
4296 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4297 exit( 1 );
4299 relpath = get_relative_path( argv[1], argv[2] );
4300 printf( "%s\n", relpath ? relpath : "." );
4301 exit( 0 );
4304 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4306 atexit( cleanup_files );
4307 signal( SIGTERM, exit_on_signal );
4308 signal( SIGINT, exit_on_signal );
4309 #ifdef SIGHUP
4310 signal( SIGHUP, exit_on_signal );
4311 #endif
4313 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4315 top_makefile = parse_makefile( NULL );
4317 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4318 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4319 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4320 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4321 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4322 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4323 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4324 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4325 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4326 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4327 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4328 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4330 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4331 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4332 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4333 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4334 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4335 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4336 host_cpu = get_expanded_make_variable( top_makefile, "host_cpu" );
4337 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4338 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4339 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4340 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4341 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4342 bison = get_expanded_make_variable( top_makefile, "BISON" );
4343 ar = get_expanded_make_variable( top_makefile, "AR" );
4344 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4345 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4346 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4347 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4348 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4349 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4350 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4352 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4353 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4354 if (!exe_ext) exe_ext = "";
4355 if (!tools_ext) tools_ext = "";
4356 if (host_cpu && (host_cpu = normalize_arch( host_cpu )))
4358 so_dir = strmake( "$(dlldir)/%s-unix", host_cpu );
4359 pe_dir = strmake( "$(dlldir)/%s-windows", host_cpu );
4361 else
4362 so_dir = pe_dir = "$(dlldir)";
4364 extra_cflags_extlib = remove_warning_flags( extra_cflags );
4365 extra_cross_cflags_extlib = remove_warning_flags( extra_cross_cflags );
4367 top_makefile->src_dir = root_src_dir;
4368 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4369 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4370 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4372 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4374 load_sources( top_makefile );
4375 for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
4377 output_dependencies( top_makefile );
4378 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4380 return 0;