user32: Uppercase the RawInput device names sans the GUID.
[wine.git] / tools / makedep.c
blobe69dc3ce0a4a71ccdd39ed82353b3955fbff4003
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 *man_ext;
150 static const char *host_cpu;
151 static const char *pe_dir;
152 static const char *so_dir;
153 static const char *crosstarget;
154 static const char *crossdebug;
155 static const char *fontforge;
156 static const char *convert;
157 static const char *flex;
158 static const char *bison;
159 static const char *ar;
160 static const char *ranlib;
161 static const char *rsvg;
162 static const char *icotool;
163 static const char *dlltool;
164 static const char *msgfmt;
165 static const char *ln_s;
166 static const char *sed_cmd;
167 static const char *delay_load_flag;
169 struct makefile
171 /* values determined from input makefile */
172 struct strarray vars;
173 struct strarray include_paths;
174 struct strarray include_args;
175 struct strarray define_args;
176 struct strarray programs;
177 struct strarray scripts;
178 struct strarray imports;
179 struct strarray delayimports;
180 struct strarray extradllflags;
181 struct strarray install_lib;
182 struct strarray install_dev;
183 struct strarray extra_targets;
184 struct strarray extra_imports;
185 struct list sources;
186 struct list includes;
187 const char *src_dir;
188 const char *obj_dir;
189 const char *parent_dir;
190 const char *module;
191 const char *testdll;
192 const char *extlib;
193 const char *sharedlib;
194 const char *staticlib;
195 const char *staticimplib;
196 const char *importlib;
197 const char *unixlib;
198 int native_unix_lib;
199 int disabled;
200 int use_msvcrt;
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 uninstall_files;
213 struct strarray object_files;
214 struct strarray crossobj_files;
215 struct strarray unixobj_files;
216 struct strarray res_files;
217 struct strarray font_files;
218 struct strarray c2man_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 )
761 if (!strcmp( name, file->name )) return file;
762 return NULL;
765 /*******************************************************************
766 * add_include
768 * Add an include file if it doesn't already exists.
770 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
771 const char *name, int line, enum incl_type type )
773 struct incl_file *include;
775 if (parent->files_count >= parent->files_size)
777 parent->files_size *= 2;
778 if (parent->files_size < 16) parent->files_size = 16;
779 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
782 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
783 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
784 goto found;
786 include = xmalloc( sizeof(*include) );
787 memset( include, 0, sizeof(*include) );
788 include->name = xstrdup(name);
789 include->included_by = parent;
790 include->included_line = line;
791 include->type = type;
792 include->use_msvcrt = parent->use_msvcrt;
793 list_add_tail( &make->includes, &include->entry );
794 found:
795 parent->files[parent->files_count++] = include;
796 return include;
800 /*******************************************************************
801 * add_generated_source
803 * Add a generated source file to the list.
805 static struct incl_file *add_generated_source( struct makefile *make,
806 const char *name, const char *filename )
808 struct incl_file *file;
810 if ((file = find_src_file( make, name ))) return file; /* we already have it */
811 file = xmalloc( sizeof(*file) );
812 memset( file, 0, sizeof(*file) );
813 file->file = add_file( name );
814 file->name = xstrdup( name );
815 file->basename = xstrdup( filename ? filename : name );
816 file->filename = obj_dir_path( make, file->basename );
817 file->file->flags = FLAG_GENERATED;
818 file->use_msvcrt = make->use_msvcrt;
819 list_add_tail( &make->sources, &file->entry );
820 return file;
824 /*******************************************************************
825 * parse_include_directive
827 static void parse_include_directive( struct file *source, char *str )
829 char quote, *include, *p = str;
831 while (*p && isspace(*p)) p++;
832 if (*p != '\"' && *p != '<' ) return;
833 quote = *p++;
834 if (quote == '<') quote = '>';
835 include = p;
836 while (*p && (*p != quote)) p++;
837 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
838 *p = 0;
839 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
843 /*******************************************************************
844 * parse_pragma_directive
846 static void parse_pragma_directive( struct file *source, char *str )
848 char *flag, *p = str;
850 if (!isspace( *p )) return;
851 while (*p && isspace(*p)) p++;
852 p = strtok( p, " \t" );
853 if (strcmp( p, "makedep" )) return;
855 while ((flag = strtok( NULL, " \t" )))
857 if (!strcmp( flag, "depend" ))
859 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
860 return;
862 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
864 if (strendswith( source->name, ".idl" ))
866 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
867 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
868 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
869 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
870 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
871 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
872 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
873 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
875 else if (strendswith( source->name, ".rc" ))
877 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
879 else if (strendswith( source->name, ".sfd" ))
881 if (!strcmp( flag, "font" ))
883 struct strarray *array = source->args;
885 if (!array)
887 source->args = array = xmalloc( sizeof(*array) );
888 *array = empty_strarray;
889 source->flags |= FLAG_SFD_FONTS;
891 strarray_add( array, xstrdup( strtok( NULL, "" )));
892 return;
895 else
897 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
898 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
904 /*******************************************************************
905 * parse_cpp_directive
907 static void parse_cpp_directive( struct file *source, char *str )
909 while (*str && isspace(*str)) str++;
910 if (*str++ != '#') return;
911 while (*str && isspace(*str)) str++;
913 if (!strncmp( str, "include", 7 ))
914 parse_include_directive( source, str + 7 );
915 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
916 parse_include_directive( source, str + 6 );
917 else if (!strncmp( str, "pragma", 6 ))
918 parse_pragma_directive( source, str + 6 );
922 /*******************************************************************
923 * parse_idl_file
925 static void parse_idl_file( struct file *source, FILE *file )
927 char *buffer, *include;
929 input_line = 0;
931 while ((buffer = get_line( file )))
933 char quote;
934 char *p = buffer;
935 while (*p && isspace(*p)) p++;
937 if (!strncmp( p, "importlib", 9 ))
939 p += 9;
940 while (*p && isspace(*p)) p++;
941 if (*p++ != '(') continue;
942 while (*p && isspace(*p)) p++;
943 if (*p++ != '"') continue;
944 include = p;
945 while (*p && (*p != '"')) p++;
946 if (!*p) fatal_error( "malformed importlib directive\n" );
947 *p = 0;
948 add_dependency( source, include, INCL_IMPORTLIB );
949 continue;
952 if (!strncmp( p, "import", 6 ))
954 p += 6;
955 while (*p && isspace(*p)) p++;
956 if (*p != '"') continue;
957 include = ++p;
958 while (*p && (*p != '"')) p++;
959 if (!*p) fatal_error( "malformed import directive\n" );
960 *p = 0;
961 add_dependency( source, include, INCL_IMPORT );
962 continue;
965 /* check for #include inside cpp_quote */
966 if (!strncmp( p, "cpp_quote", 9 ))
968 p += 9;
969 while (*p && isspace(*p)) p++;
970 if (*p++ != '(') continue;
971 while (*p && isspace(*p)) p++;
972 if (*p++ != '"') continue;
973 if (*p++ != '#') continue;
974 while (*p && isspace(*p)) p++;
975 if (strncmp( p, "include", 7 )) continue;
976 p += 7;
977 while (*p && isspace(*p)) p++;
978 if (*p == '\\' && p[1] == '"')
980 p += 2;
981 quote = '"';
983 else
985 if (*p++ != '<' ) continue;
986 quote = '>';
988 include = p;
989 while (*p && (*p != quote)) p++;
990 if (!*p || (quote == '"' && p[-1] != '\\'))
991 fatal_error( "malformed #include directive inside cpp_quote\n" );
992 if (quote == '"') p--; /* remove backslash */
993 *p = 0;
994 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
995 continue;
998 parse_cpp_directive( source, p );
1002 /*******************************************************************
1003 * parse_c_file
1005 static void parse_c_file( struct file *source, FILE *file )
1007 char *buffer;
1009 input_line = 0;
1010 while ((buffer = get_line( file )))
1012 parse_cpp_directive( source, buffer );
1017 /*******************************************************************
1018 * parse_rc_file
1020 static void parse_rc_file( struct file *source, FILE *file )
1022 char *buffer, *include;
1024 input_line = 0;
1025 while ((buffer = get_line( file )))
1027 char quote;
1028 char *p = buffer;
1029 while (*p && isspace(*p)) p++;
1031 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1033 p += 2;
1034 while (*p && isspace(*p)) p++;
1035 if (strncmp( p, "@makedep:", 9 )) continue;
1036 p += 9;
1037 while (*p && isspace(*p)) p++;
1038 quote = '"';
1039 if (*p == quote)
1041 include = ++p;
1042 while (*p && *p != quote) p++;
1044 else
1046 include = p;
1047 while (*p && !isspace(*p) && *p != '*') p++;
1049 if (!*p)
1050 fatal_error( "malformed makedep comment\n" );
1051 *p = 0;
1052 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1053 continue;
1056 parse_cpp_directive( source, buffer );
1061 /*******************************************************************
1062 * parse_in_file
1064 static void parse_in_file( struct file *source, FILE *file )
1066 char *p, *buffer;
1068 /* make sure it gets rebuilt when the version changes */
1069 add_dependency( source, "config.h", INCL_SYSTEM );
1071 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1073 input_line = 0;
1074 while ((buffer = get_line( file )))
1076 if (strncmp( buffer, ".TH", 3 )) continue;
1077 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1078 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1079 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1080 source->args = xstrdup( p );
1081 return;
1086 /*******************************************************************
1087 * parse_sfd_file
1089 static void parse_sfd_file( struct file *source, FILE *file )
1091 char *p, *eol, *buffer;
1093 input_line = 0;
1094 while ((buffer = get_line( file )))
1096 if (strncmp( buffer, "UComments:", 10 )) continue;
1097 p = buffer + 10;
1098 while (*p == ' ') p++;
1099 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1101 p++;
1102 buffer[strlen(buffer) - 1] = 0;
1104 while ((eol = strstr( p, "+AAoA" )))
1106 *eol = 0;
1107 while (*p && isspace(*p)) p++;
1108 if (*p++ == '#')
1110 while (*p && isspace(*p)) p++;
1111 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1113 p = eol + 5;
1115 while (*p && isspace(*p)) p++;
1116 if (*p++ != '#') return;
1117 while (*p && isspace(*p)) p++;
1118 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1119 return;
1124 static const struct
1126 const char *ext;
1127 void (*parse)( struct file *file, FILE *f );
1128 } parse_functions[] =
1130 { ".c", parse_c_file },
1131 { ".h", parse_c_file },
1132 { ".inl", parse_c_file },
1133 { ".l", parse_c_file },
1134 { ".m", parse_c_file },
1135 { ".rh", parse_c_file },
1136 { ".x", parse_c_file },
1137 { ".y", parse_c_file },
1138 { ".idl", parse_idl_file },
1139 { ".rc", parse_rc_file },
1140 { ".in", parse_in_file },
1141 { ".sfd", parse_sfd_file }
1144 /*******************************************************************
1145 * load_file
1147 static struct file *load_file( const char *name )
1149 struct file *file;
1150 FILE *f;
1151 unsigned int i, hash = hash_filename( name );
1153 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1154 if (!strcmp( name, file->name )) return file;
1156 if (!(f = fopen( name, "r" ))) return NULL;
1158 file = add_file( name );
1159 list_add_tail( &files[hash], &file->entry );
1160 input_file_name = file->name;
1161 input_line = 0;
1163 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1165 if (!strendswith( name, parse_functions[i].ext )) continue;
1166 parse_functions[i].parse( file, f );
1167 break;
1170 fclose( f );
1171 input_file_name = NULL;
1173 return file;
1177 /*******************************************************************
1178 * open_include_path_file
1180 * Open a file from a directory on the include path.
1182 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1183 const char *name, char **filename )
1185 char *src_path = concat_paths( dir, name );
1186 struct file *ret = load_file( src_path );
1188 if (ret) *filename = src_path;
1189 return ret;
1193 /*******************************************************************
1194 * open_file_same_dir
1196 * Open a file in the same directory as the parent.
1198 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1200 char *src_path = replace_filename( parent->file->name, name );
1201 struct file *ret = load_file( src_path );
1203 if (ret) *filename = replace_filename( parent->filename, name );
1204 return ret;
1208 /*******************************************************************
1209 * open_local_file
1211 * Open a file in the source directory of the makefile.
1213 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1215 char *src_path = src_dir_path( make, path );
1216 struct file *ret = load_file( src_path );
1218 /* if not found, try parent dir */
1219 if (!ret && make->parent_dir)
1221 free( src_path );
1222 path = strmake( "%s/%s", make->parent_dir, path );
1223 src_path = src_dir_path( make, path );
1224 ret = load_file( src_path );
1227 if (ret) *filename = src_path;
1228 return ret;
1232 /*******************************************************************
1233 * open_global_file
1235 * Open a file in the top-level source directory.
1237 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1239 char *src_path = root_src_dir_path( path );
1240 struct file *ret = load_file( src_path );
1242 if (ret) *filename = src_path;
1243 return ret;
1247 /*******************************************************************
1248 * open_global_header
1250 * Open a file in the global include source directory.
1252 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1254 if (!strncmp( path, "../", 3 )) return NULL;
1255 return open_global_file( make, strmake( "include/%s", path ), filename );
1259 /*******************************************************************
1260 * open_src_file
1262 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1264 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1266 if (!file) fatal_perror( "open %s", pFile->name );
1267 return file;
1271 /*******************************************************************
1272 * find_importlib_module
1274 static struct makefile *find_importlib_module( const char *name )
1276 unsigned int i, len;
1278 for (i = 0; i < subdirs.count; i++)
1280 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1281 len = strlen(submakes[i]->obj_dir);
1282 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1283 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1285 return NULL;
1289 /*******************************************************************
1290 * has_external_import
1292 static int has_external_import( const struct makefile *make )
1294 unsigned int i;
1296 for (i = 0; i < make->imports.count; i++)
1298 if (!strncmp( make->imports.str[i], "-l", 2 ))
1299 return 1;
1301 return 0;
1305 /*******************************************************************
1306 * open_include_file
1308 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1310 struct file *file = NULL;
1311 char *filename;
1312 unsigned int i, len;
1314 errno = ENOENT;
1316 /* check for generated bison header */
1318 if (strendswith( pFile->name, ".tab.h" ) &&
1319 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1321 pFile->sourcename = filename;
1322 pFile->filename = obj_dir_path( make, pFile->name );
1323 return file;
1326 /* check for corresponding idl file in source dir */
1328 if (strendswith( pFile->name, ".h" ) &&
1329 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1331 pFile->sourcename = filename;
1332 pFile->filename = obj_dir_path( make, pFile->name );
1333 return file;
1336 /* check for extra targets */
1337 if (strarray_exists( &make->extra_targets, pFile->name ))
1339 pFile->sourcename = src_dir_path( make, pFile->name );
1340 pFile->filename = obj_dir_path( make, pFile->name );
1341 return NULL;
1344 /* now try in source dir */
1345 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1347 /* check for global importlib (module dependency) */
1349 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1351 pFile->filename = pFile->name;
1352 return NULL;
1355 /* check for corresponding idl file in global includes */
1357 if (strendswith( pFile->name, ".h" ) &&
1358 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1360 pFile->sourcename = filename;
1361 pFile->filename = strmake( "include/%s", pFile->name );
1362 return file;
1365 /* check for corresponding .in file in global includes (for config.h.in) */
1367 if (strendswith( pFile->name, ".h" ) &&
1368 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1370 pFile->sourcename = filename;
1371 pFile->filename = strmake( "include/%s", pFile->name );
1372 return file;
1375 /* check for corresponding .x file in global includes */
1377 if (strendswith( pFile->name, "tmpl.h" ) &&
1378 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1380 pFile->sourcename = filename;
1381 pFile->filename = strmake( "include/%s", pFile->name );
1382 return file;
1385 /* check in global includes source dir */
1387 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1389 /* check in global msvcrt includes */
1390 if (pFile->use_msvcrt &&
1391 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1392 return file;
1394 /* now search in include paths */
1395 for (i = 0; i < make->include_paths.count; i++)
1397 const char *dir = make->include_paths.str[i];
1399 if (root_src_dir)
1401 len = strlen( root_src_dir );
1402 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1404 while (dir[len] == '/') len++;
1405 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1408 else
1410 if (*dir == '/') continue;
1411 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1413 if (!file) continue;
1414 pFile->is_external = 1;
1415 return file;
1418 if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt &&
1419 !make->extlib && !pFile->included_by->is_external)
1421 if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
1422 if (!strcmp( pFile->name, "x86intrin.h" )) return NULL;
1423 if (has_external_import( make )) return NULL;
1424 fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
1425 pFile->included_by->file->name, pFile->included_line, pFile->name );
1426 exit(1);
1429 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1431 /* try in src file directory */
1432 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1434 pFile->is_external = pFile->included_by->is_external;
1435 return file;
1438 if (make->extlib) return NULL; /* ignore missing files in external libs */
1440 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1441 perror( pFile->name );
1442 pFile = pFile->included_by;
1443 while (pFile && pFile->included_by)
1445 const char *parent = pFile->included_by->sourcename;
1446 if (!parent) parent = pFile->included_by->file->name;
1447 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1448 parent, pFile->included_line, pFile->name );
1449 pFile = pFile->included_by;
1451 exit(1);
1455 /*******************************************************************
1456 * add_all_includes
1458 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1460 unsigned int i;
1462 for (i = 0; i < file->deps_count; i++)
1464 switch (file->deps[i].type)
1466 case INCL_NORMAL:
1467 case INCL_IMPORT:
1468 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1469 break;
1470 case INCL_IMPORTLIB:
1471 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1472 break;
1473 case INCL_SYSTEM:
1474 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1475 break;
1476 case INCL_CPP_QUOTE:
1477 case INCL_CPP_QUOTE_SYSTEM:
1478 break;
1484 /*******************************************************************
1485 * parse_file
1487 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1489 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1491 if (!file) return;
1493 source->file = file;
1494 source->files_count = 0;
1495 source->files_size = file->deps_count;
1496 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1497 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1498 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1500 if (source->sourcename)
1502 if (strendswith( source->sourcename, ".idl" ))
1504 unsigned int i;
1506 /* generated .h file always includes these */
1507 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1508 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1509 for (i = 0; i < file->deps_count; i++)
1511 switch (file->deps[i].type)
1513 case INCL_IMPORT:
1514 if (strendswith( file->deps[i].name, ".idl" ))
1515 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1516 file->deps[i].line, INCL_NORMAL );
1517 else
1518 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1519 break;
1520 case INCL_CPP_QUOTE:
1521 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1522 break;
1523 case INCL_CPP_QUOTE_SYSTEM:
1524 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1525 break;
1526 case INCL_NORMAL:
1527 case INCL_SYSTEM:
1528 case INCL_IMPORTLIB:
1529 break;
1532 return;
1534 if (strendswith( source->sourcename, ".y" ))
1535 return; /* generated .tab.h doesn't include anything */
1538 add_all_includes( make, source, file );
1542 /*******************************************************************
1543 * add_src_file
1545 * Add a source file to the list.
1547 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1549 struct incl_file *file;
1551 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1552 file = xmalloc( sizeof(*file) );
1553 memset( file, 0, sizeof(*file) );
1554 file->name = xstrdup(name);
1555 file->use_msvcrt = make->use_msvcrt;
1556 file->is_external = !!make->extlib;
1557 list_add_tail( &make->sources, &file->entry );
1558 parse_file( make, file, 1 );
1559 return file;
1563 /*******************************************************************
1564 * open_input_makefile
1566 static FILE *open_input_makefile( const struct makefile *make )
1568 FILE *ret;
1570 if (make->obj_dir)
1571 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1572 else
1573 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1575 input_line = 0;
1576 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1577 return ret;
1581 /*******************************************************************
1582 * get_make_variable
1584 static const char *get_make_variable( const struct makefile *make, const char *name )
1586 const char *ret;
1588 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1589 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1590 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1591 return NULL;
1595 /*******************************************************************
1596 * get_expanded_make_variable
1598 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1600 const char *var;
1601 char *p, *end, *expand, *tmp;
1603 var = get_make_variable( make, name );
1604 if (!var) return NULL;
1606 p = expand = xstrdup( var );
1607 while ((p = strchr( p, '$' )))
1609 if (p[1] == '(')
1611 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1612 *end++ = 0;
1613 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1614 var = get_make_variable( make, p + 2 );
1615 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1616 /* switch to the new string */
1617 p = tmp + (p - expand);
1618 free( expand );
1619 expand = tmp;
1621 else if (p[1] == '{') /* don't expand ${} variables */
1623 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1624 p = end + 1;
1626 else if (p[1] == '$')
1628 p += 2;
1630 else fatal_error( "syntax error in '%s'\n", expand );
1633 /* consider empty variables undefined */
1634 p = expand;
1635 while (*p && isspace(*p)) p++;
1636 if (*p) return expand;
1637 free( expand );
1638 return NULL;
1642 /*******************************************************************
1643 * get_expanded_make_var_array
1645 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1647 struct strarray ret = empty_strarray;
1648 char *value, *token;
1650 if ((value = get_expanded_make_variable( make, name )))
1651 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1652 strarray_add( &ret, token );
1653 return ret;
1657 /*******************************************************************
1658 * get_expanded_file_local_var
1660 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1661 const char *name )
1663 char *p, *var = strmake( "%s_%s", file, name );
1665 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1666 return get_expanded_make_var_array( make, var );
1670 /*******************************************************************
1671 * set_make_variable
1673 static int set_make_variable( struct strarray *array, const char *assignment )
1675 char *p, *name;
1677 p = name = xstrdup( assignment );
1678 while (isalnum(*p) || *p == '_') p++;
1679 if (name == p) return 0; /* not a variable */
1680 if (isspace(*p))
1682 *p++ = 0;
1683 while (isspace(*p)) p++;
1685 if (*p != '=') return 0; /* not an assignment */
1686 *p++ = 0;
1687 while (isspace(*p)) p++;
1689 strarray_set_value( array, name, p );
1690 return 1;
1694 /*******************************************************************
1695 * parse_makefile
1697 static struct makefile *parse_makefile( const char *path )
1699 char *buffer;
1700 FILE *file;
1701 struct makefile *make = xmalloc( sizeof(*make) );
1703 memset( make, 0, sizeof(*make) );
1704 make->obj_dir = path;
1705 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1707 file = open_input_makefile( make );
1708 while ((buffer = get_line( file )))
1710 if (!strncmp( buffer, separator, strlen(separator) )) break;
1711 if (*buffer == '\t') continue; /* command */
1712 while (isspace( *buffer )) buffer++;
1713 if (*buffer == '#') continue; /* comment */
1714 set_make_variable( &make->vars, buffer );
1716 fclose( file );
1717 input_file_name = NULL;
1718 return make;
1722 /*******************************************************************
1723 * add_generated_sources
1725 static void add_generated_sources( struct makefile *make )
1727 unsigned int i;
1728 struct incl_file *source, *next, *file;
1729 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1731 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1733 if (source->file->flags & FLAG_IDL_CLIENT)
1735 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1736 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1737 add_all_includes( make, file, file->file );
1739 if (source->file->flags & FLAG_IDL_SERVER)
1741 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1742 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1743 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1744 add_all_includes( make, file, file->file );
1746 if (source->file->flags & FLAG_IDL_IDENT)
1748 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1749 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1750 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1751 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1752 add_all_includes( make, file, file->file );
1754 if (source->file->flags & FLAG_IDL_PROXY)
1756 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1757 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1758 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1759 add_all_includes( make, file, file->file );
1760 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1761 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1762 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1763 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1764 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1765 add_all_includes( make, file, file->file );
1767 if (source->file->flags & FLAG_IDL_TYPELIB)
1769 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL );
1771 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1773 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1775 if (source->file->flags & FLAG_IDL_REGISTER)
1777 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1779 if (source->file->flags & FLAG_IDL_HEADER)
1781 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1783 if (!source->file->flags && strendswith( source->name, ".idl" ))
1785 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1787 if (strendswith( source->name, ".x" ))
1789 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1791 if (strendswith( source->name, ".y" ))
1793 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1794 /* steal the includes list from the source file */
1795 file->files_count = source->files_count;
1796 file->files_size = source->files_size;
1797 file->files = source->files;
1798 source->files_count = source->files_size = 0;
1799 source->files = NULL;
1801 if (strendswith( source->name, ".l" ))
1803 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1804 /* steal the includes list from the source file */
1805 file->files_count = source->files_count;
1806 file->files_size = source->files_size;
1807 file->files = source->files;
1808 source->files_count = source->files_size = 0;
1809 source->files = NULL;
1811 if (source->file->flags & FLAG_C_IMPLIB)
1813 if (!make->staticimplib && make->importlib && *dll_ext)
1814 make->staticimplib = strmake( "lib%s.a", make->importlib );
1816 if (strendswith( source->name, ".po" ))
1818 if (!make->disabled)
1819 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1821 if (strendswith( source->name, ".spec" ))
1823 char *obj = replace_extension( source->name, ".spec", "" );
1824 strarray_addall_uniq( &make->extra_imports,
1825 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1828 if (make->testdll)
1830 file = add_generated_source( make, "testlist.o", "testlist.c" );
1831 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1832 add_all_includes( make, file, file->file );
1834 for (i = 0; i < objs.count; i++)
1836 /* default to .c for unknown extra object files */
1837 if (strendswith( objs.str[i], ".o" ))
1839 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1840 file->file->flags |= FLAG_C_UNIX;
1841 file->use_msvcrt = 0;
1843 else if (strendswith( objs.str[i], ".res" ))
1844 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1845 else
1846 add_generated_source( make, objs.str[i], NULL );
1851 /*******************************************************************
1852 * create_dir
1854 static void create_dir( const char *dir )
1856 char *p, *path;
1858 p = path = xstrdup( dir );
1859 while ((p = strchr( p, '/' )))
1861 *p = 0;
1862 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1863 *p++ = '/';
1864 while (*p == '/') p++;
1866 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1867 free( path );
1871 /*******************************************************************
1872 * create_file_directories
1874 * Create the base directories of all the files.
1876 static void create_file_directories( const struct makefile *make, struct strarray files )
1878 struct strarray subdirs = empty_strarray;
1879 unsigned int i;
1880 char *dir;
1882 for (i = 0; i < files.count; i++)
1884 if (!strchr( files.str[i], '/' )) continue;
1885 dir = obj_dir_path( make, files.str[i] );
1886 *strrchr( dir, '/' ) = 0;
1887 strarray_add_uniq( &subdirs, dir );
1890 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1894 /*******************************************************************
1895 * output_filenames_obj_dir
1897 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1899 unsigned int i;
1901 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1905 /*******************************************************************
1906 * get_dependencies
1908 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1910 unsigned int i;
1912 if (!file->filename) return;
1914 if (file != source)
1916 if (file->owner == source) return; /* already processed */
1917 if (file->type == INCL_IMPORTLIB)
1919 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1920 return; /* library is imported only when building a typelib */
1921 strarray_add( &source->importlibdeps, file->filename );
1923 else strarray_add( &source->dependencies, file->filename );
1924 file->owner = source;
1926 /* sanity checks */
1927 if (!strcmp( file->filename, "include/config.h" ) &&
1928 file != source->files[0] && !source->is_external)
1930 input_file_name = source->filename;
1931 input_line = 0;
1932 for (i = 0; i < source->file->deps_count; i++)
1934 if (!strcmp( source->file->deps[i].name, file->name ))
1935 input_line = source->file->deps[i].line;
1937 fatal_error( "%s must be included before other headers\n", file->name );
1941 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
1945 /*******************************************************************
1946 * get_local_dependencies
1948 * Get the local dependencies of a given target.
1950 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1951 struct strarray targets )
1953 unsigned int i;
1954 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
1956 for (i = 0; i < deps.count; i++)
1958 if (strarray_exists( &targets, deps.str[i] ))
1959 deps.str[i] = obj_dir_path( make, deps.str[i] );
1960 else
1961 deps.str[i] = src_dir_path( make, deps.str[i] );
1963 return deps;
1967 /*******************************************************************
1968 * get_static_lib
1970 * Check if makefile builds the named static library and return the full lib path.
1972 static const char *get_static_lib( const struct makefile *make, const char *name )
1974 if (!make->staticlib) return NULL;
1975 if (make->disabled) return NULL;
1976 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
1977 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
1978 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
1979 return obj_dir_path( make, make->staticlib );
1983 /*******************************************************************
1984 * get_native_unix_lib
1986 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
1988 if (!make->native_unix_lib) return NULL;
1989 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
1990 if (make->unixlib[strlen(name)] != '.') return NULL;
1991 return obj_dir_path( make, make->unixlib );
1995 /*******************************************************************
1996 * get_parent_makefile
1998 static struct makefile *get_parent_makefile( struct makefile *make )
2000 char *dir, *p;
2001 int i;
2003 if (!make->obj_dir) return NULL;
2004 dir = xstrdup( make->obj_dir );
2005 if (!(p = strrchr( dir, '/' ))) return NULL;
2006 *p = 0;
2007 for (i = 0; i < subdirs.count; i++)
2008 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2009 return NULL;
2013 /*******************************************************************
2014 * needs_delay_lib
2016 static int needs_delay_lib( const struct makefile *make )
2018 if (delay_load_flag) return 0;
2019 if (*dll_ext && !crosstarget) return 0;
2020 if (!make->importlib) return 0;
2021 return strarray_exists( &delay_import_libs, make->importlib );
2025 /*******************************************************************
2026 * add_unix_libraries
2028 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2030 struct strarray ret = empty_strarray;
2031 struct strarray all_libs = empty_strarray;
2032 unsigned int i, j;
2034 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2035 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2037 for (i = 0; i < all_libs.count; i++)
2039 const char *lib = NULL;
2041 if (!strncmp( all_libs.str[i], "-l", 2 ))
2043 for (j = 0; j < subdirs.count; j++)
2045 if (make == submakes[j]) continue;
2046 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2049 if (lib)
2051 strarray_add( deps, lib );
2052 strarray_add( &ret, lib );
2054 else strarray_add( &ret, all_libs.str[i] );
2057 strarray_addall( &ret, libs );
2058 return ret;
2062 /*******************************************************************
2063 * is_crt_module
2065 static int is_crt_module( const char *file )
2067 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2071 /*******************************************************************
2072 * get_default_crt
2074 static const char *get_default_crt( const struct makefile *make )
2076 if (!make->use_msvcrt) return NULL;
2077 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2078 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2082 /*******************************************************************
2083 * get_crt_define
2085 static const char *get_crt_define( const struct makefile *make )
2087 const char *crt_dll = NULL;
2088 unsigned int i, version = 0;
2090 for (i = 0; i < make->imports.count; i++)
2092 if (!is_crt_module( make->imports.str[i] )) continue;
2093 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2094 crt_dll, make->imports.str[i] );
2095 crt_dll = make->imports.str[i];
2098 if (!crt_dll)
2100 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2101 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2103 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2104 sscanf( crt_dll, "msvcr%u", &version );
2105 return strmake( "-D_MSVCR_VER=%u", version );
2109 /*******************************************************************
2110 * add_default_imports
2112 static struct strarray add_default_imports( const struct makefile *make, struct strarray imports )
2114 struct strarray ret = empty_strarray;
2115 const char *crt_dll = get_default_crt( make );
2116 unsigned int i;
2118 for (i = 0; i < imports.count; i++)
2120 if (is_crt_module( imports.str[i] )) crt_dll = imports.str[i];
2121 else strarray_add( &ret, imports.str[i] );
2124 strarray_add( &ret, "winecrt0" );
2125 if (crt_dll) strarray_add( &ret, crt_dll );
2127 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2128 strarray_add( &ret, "kernel" );
2130 strarray_add( &ret, "kernel32" );
2131 strarray_add( &ret, "ntdll" );
2132 return ret;
2136 /*******************************************************************
2137 * add_import_libs
2139 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2140 struct strarray imports, int delay, int is_cross )
2142 struct strarray ret = empty_strarray;
2143 unsigned int i, j;
2145 for (i = 0; i < imports.count; i++)
2147 const char *name = imports.str[i];
2148 const char *lib = NULL;
2150 if (name[0] == '-')
2152 switch (name[1])
2154 case 'L': strarray_add( &ret, name ); continue;
2155 case 'l': name += 2; break;
2156 default: continue;
2159 else name = get_base_name( name );
2161 for (j = 0; j < subdirs.count; j++)
2163 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2164 lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
2165 else
2166 lib = get_static_lib( submakes[j], name );
2167 if (lib) break;
2170 if (lib)
2172 const char *ext = NULL;
2174 if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
2175 else if (is_cross) ext = ".cross.a";
2176 if (ext) lib = replace_extension( lib, ".a", ext );
2177 strarray_add_uniq( deps, lib );
2178 strarray_add( &ret, lib );
2180 else strarray_add( &ret, strmake( "-l%s", name ));
2182 return ret;
2186 /*******************************************************************
2187 * add_install_rule
2189 static void add_install_rule( struct makefile *make, const char *target,
2190 const char *file, const char *dest )
2192 if (strarray_exists( &make->install_lib, target ) ||
2193 strarray_exists( &top_install_lib, make->obj_dir ) ||
2194 strarray_exists( &top_install_lib, obj_dir_path( make, target )))
2196 strarray_add( &make->install_rules[INSTALL_LIB], file );
2197 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2199 else if (strarray_exists( &make->install_dev, target ) ||
2200 strarray_exists( &top_install_dev, make->obj_dir ) ||
2201 strarray_exists( &top_install_dev, obj_dir_path( make, target )))
2203 strarray_add( &make->install_rules[INSTALL_DEV], file );
2204 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2209 /*******************************************************************
2210 * get_include_install_path
2212 * Determine the installation path for a given include file.
2214 static const char *get_include_install_path( const char *name )
2216 if (!strncmp( name, "wine/", 5 )) return name + 5;
2217 if (!strncmp( name, "msvcrt/", 7 )) return name;
2218 return strmake( "windows/%s", name );
2222 /*******************************************************************
2223 * get_shared_library_name
2225 * Determine possible names for a shared library with a version number.
2227 static struct strarray get_shared_lib_names( const char *libname )
2229 struct strarray ret = empty_strarray;
2230 const char *ext, *p;
2231 char *name, *first, *second;
2232 size_t len = 0;
2234 strarray_add( &ret, libname );
2236 for (p = libname; (p = strchr( p, '.' )); p++)
2237 if ((len = strspn( p + 1, "0123456789." ))) break;
2239 if (!len) return ret;
2240 ext = p + 1 + len;
2241 if (*ext && ext[-1] == '.') ext--;
2243 /* keep only the first group of digits */
2244 name = xstrdup( libname );
2245 first = name + (p - libname);
2246 if ((second = strchr( first + 1, '.' )))
2248 strcpy( second, ext );
2249 strarray_add( &ret, xstrdup( name ));
2251 return ret;
2255 /*******************************************************************
2256 * get_source_defines
2258 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2259 const char *obj )
2261 unsigned int i;
2262 struct strarray ret = empty_strarray;
2264 strarray_addall( &ret, make->include_args );
2265 if (source->use_msvcrt)
2266 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2267 for (i = 0; i < make->include_paths.count; i++)
2268 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2269 strarray_addall( &ret, make->define_args );
2270 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2271 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2272 return ret;
2276 /*******************************************************************
2277 * remove_warning_flags
2279 static struct strarray remove_warning_flags( struct strarray flags )
2281 unsigned int i;
2282 struct strarray ret = empty_strarray;
2284 for (i = 0; i < flags.count; i++)
2285 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2286 strarray_add( &ret, flags.str[i] );
2287 return ret;
2291 /*******************************************************************
2292 * get_debug_file
2294 static const char *get_debug_file( struct makefile *make, const char *name )
2296 const char *debug_file = NULL;
2297 if (!make->is_cross || !crossdebug) return NULL;
2298 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2299 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2300 if (debug_file) strarray_add( &make->debug_files, debug_file );
2301 return debug_file;
2305 /*******************************************************************
2306 * cmd_prefix
2308 static const char *cmd_prefix( const char *cmd )
2310 if (!silent_rules) return "";
2311 return strmake( "$(quiet_%s)", cmd );
2315 /*******************************************************************
2316 * output_winegcc_command
2318 static void output_winegcc_command( struct makefile *make, int is_cross )
2320 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2321 output_filename( "--wine-objdir ." );
2322 if (tools_dir)
2324 output_filename( "--winebuild" );
2325 output_filename( tools_path( make, "winebuild" ));
2327 if (is_cross)
2329 output_filename( "-b" );
2330 output_filename( crosstarget );
2331 output_filename( "--lib-suffix=.cross.a" );
2333 else
2335 output_filenames( target_flags );
2336 output_filenames( lddll_flags );
2341 /*******************************************************************
2342 * output_symlink_rule
2344 * Output a rule to create a symlink.
2346 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2348 const char *name = strrchr( link_name, '/' );
2349 char *dir = NULL;
2351 if (name)
2353 dir = xstrdup( link_name );
2354 dir[name - link_name] = 0;
2357 output( "\t%s", cmd_prefix( "LN" ));
2358 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2359 output( "rm -f %s && ", link_name );
2361 /* dest path with a directory needs special handling if ln -s isn't supported */
2362 if (dir && strcmp( ln_s, "ln -s" ))
2363 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2364 else
2365 output( "%s %s %s\n", ln_s, src_name, link_name );
2367 free( dir );
2371 /*******************************************************************
2372 * output_srcdir_symlink
2374 * Output rule to create a symlink back to the source directory, for source files
2375 * that are needed at run-time.
2377 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2379 char *src_file, *dst_file, *src_name;
2381 if (!make->src_dir) return;
2382 src_file = src_dir_path( make, obj );
2383 dst_file = obj_dir_path( make, obj );
2384 output( "%s: %s\n", dst_file, src_file );
2386 src_name = src_file;
2387 if (src_name[0] != '/' && make->obj_dir)
2388 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2390 output_symlink_rule( src_name, dst_file, 0 );
2391 strarray_add( &make->all_targets, obj );
2395 /*******************************************************************
2396 * output_install_commands
2398 static void output_install_commands( struct makefile *make, struct strarray files )
2400 unsigned int i;
2401 char *install_sh = root_src_dir_path( "tools/install-sh" );
2403 for (i = 0; i < files.count; i += 2)
2405 const char *file = files.str[i];
2406 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2408 switch (*files.str[i + 1])
2410 case 'c': /* cross-compiled program */
2411 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2412 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2413 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2414 break;
2415 case 'd': /* data file */
2416 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2417 install_sh, obj_dir_path( make, file ), dest );
2418 break;
2419 case 'D': /* data file in source dir */
2420 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2421 install_sh, src_dir_path( make, file ), dest );
2422 break;
2423 case 'p': /* program file */
2424 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2425 install_sh, obj_dir_path( make, file ), dest );
2426 break;
2427 case 's': /* script */
2428 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2429 install_sh, obj_dir_path( make, file ), dest );
2430 break;
2431 case 'S': /* script in source dir */
2432 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2433 install_sh, src_dir_path( make, file ), dest );
2434 break;
2435 case 't': /* script in tools dir */
2436 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2437 install_sh, tools_dir_path( make, files.str[i] ), dest );
2438 break;
2439 case 'y': /* symlink */
2440 output_symlink_rule( files.str[i], dest, 1 );
2441 break;
2442 default:
2443 assert(0);
2445 strarray_add( &make->uninstall_files, dest );
2450 /*******************************************************************
2451 * output_install_rules
2453 * Rules are stored as a (file,dest) pair of values.
2454 * The first char of dest indicates the type of install.
2456 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2458 unsigned int i;
2459 struct strarray files = make->install_rules[rules];
2460 struct strarray targets = empty_strarray;
2462 if (!files.count) return;
2464 for (i = 0; i < files.count; i += 2)
2466 const char *file = files.str[i];
2467 switch (*files.str[i + 1])
2469 case 'c': /* cross-compiled program */
2470 case 'd': /* data file */
2471 case 'p': /* program file */
2472 case 's': /* script */
2473 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2474 break;
2475 case 't': /* script in tools dir */
2476 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2477 break;
2481 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
2482 output_filenames( targets );
2483 output( "\n" );
2484 output_install_commands( make, files );
2485 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2486 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
2490 static int cmp_string_length( const char **a, const char **b )
2492 int paths_a = 0, paths_b = 0;
2493 const char *p;
2495 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2496 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2497 if (paths_b != paths_a) return paths_b - paths_a;
2498 return strcmp( *a, *b );
2501 /*******************************************************************
2502 * output_uninstall_rules
2504 static void output_uninstall_rules( struct makefile *make )
2506 static const char *dirs_order[] =
2507 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2509 struct strarray uninstall_dirs = empty_strarray;
2510 unsigned int i, j;
2512 if (!make->uninstall_files.count) return;
2513 output( "uninstall::\n" );
2514 output_rm_filenames( make->uninstall_files );
2515 strarray_add_uniq( &make->phony_targets, "uninstall" );
2517 if (!subdirs.count) return;
2518 for (i = 0; i < make->uninstall_files.count; i++)
2520 char *dir = xstrdup( make->uninstall_files.str[i] );
2521 while (strchr( dir, '/' ))
2523 *strrchr( dir, '/' ) = 0;
2524 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2527 strarray_qsort( &uninstall_dirs, cmp_string_length );
2528 output( "\t-rmdir" );
2529 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2531 for (j = 0; j < uninstall_dirs.count; j++)
2533 if (!uninstall_dirs.str[j]) continue;
2534 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2535 continue;
2536 output_filename( uninstall_dirs.str[j] );
2537 uninstall_dirs.str[j] = NULL;
2540 for (j = 0; j < uninstall_dirs.count; j++)
2541 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2542 output( "\n" );
2546 /*******************************************************************
2547 * output_po_files
2549 static void output_po_files( const struct makefile *make )
2551 const char *po_dir = src_dir_path( make, "po" );
2552 unsigned int i;
2554 if (linguas.count)
2556 for (i = 0; i < linguas.count; i++)
2557 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2558 output( ": %s/wine.pot\n", po_dir );
2559 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2560 cmd_prefix( "MSG" ), po_dir );
2561 output( "po:" );
2562 for (i = 0; i < linguas.count; i++)
2563 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2564 output( "\n" );
2566 output( "%s/wine.pot:", po_dir );
2567 output_filenames( make->pot_files );
2568 output( "\n" );
2569 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2570 output_filenames( make->pot_files );
2571 output( "\n" );
2575 /*******************************************************************
2576 * output_source_y
2578 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2580 /* add source file dependency for parallel makes */
2581 char *header = strmake( "%s.tab.h", obj );
2583 if (find_include_file( make, header ))
2585 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2586 output( "\t%s%s -p %s_ -o %s.tab.c -d %s\n",
2587 cmd_prefix( "BISON" ), bison, obj, obj_dir_path( make, obj ), source->filename );
2588 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2589 source->filename, obj_dir_path( make, header ));
2590 strarray_add( &make->clean_files, header );
2592 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2594 output( "\t%s%s -p %s_ -o $@ %s\n", cmd_prefix( "BISON" ), bison, obj, source->filename );
2598 /*******************************************************************
2599 * output_source_l
2601 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2603 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2604 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2608 /*******************************************************************
2609 * output_source_h
2611 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2613 if (source->file->flags & FLAG_GENERATED)
2614 strarray_add( &make->all_targets, source->name );
2615 else
2616 add_install_rule( make, source->name, source->name,
2617 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2621 /*******************************************************************
2622 * output_source_rc
2624 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2626 struct strarray defines = get_source_defines( make, source, obj );
2627 const char *po_dir = NULL;
2628 unsigned int i;
2630 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2631 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2632 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2633 if (source->file->flags & FLAG_RC_PO)
2635 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2636 output( "%s.pot ", obj_dir_path( make, obj ) );
2638 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2639 output_filename( tools_path( make, "wrc" ));
2640 output_filenames( source->dependencies );
2641 output( "\n" );
2642 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2643 if (make->is_win16) output_filename( "-m16" );
2644 output_filename( "--nostdinc" );
2645 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2646 output_filenames( defines );
2647 output_filename( source->filename );
2648 output( "\n" );
2649 if (po_dir)
2651 output( "%s.res:", obj_dir_path( make, obj ));
2652 for (i = 0; i < linguas.count; i++)
2653 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2654 output( "\n" );
2659 /*******************************************************************
2660 * output_source_mc
2662 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2664 unsigned int i;
2665 char *obj_path = obj_dir_path( make, obj );
2667 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2668 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2669 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2670 output_filename( tools_path( make, "wmc" ));
2671 output_filenames( source->dependencies );
2672 output( "\n" );
2673 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2674 if (linguas.count)
2676 output_filename( "--po-dir=po" );
2677 output( "\n" );
2678 output( "%s.res:", obj_dir_path( make, obj ));
2679 for (i = 0; i < linguas.count; i++)
2680 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2682 output( "\n" );
2686 /*******************************************************************
2687 * output_source_res
2689 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2691 strarray_add( &make->res_files, source->name );
2695 /*******************************************************************
2696 * output_source_idl
2698 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2700 struct strarray defines = get_source_defines( make, source, obj );
2701 struct strarray targets = empty_strarray;
2702 char *dest;
2703 unsigned int i;
2705 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2706 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2708 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2710 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2711 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2712 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2713 strarray_add( &targets, dest );
2715 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2716 if (source->file->flags & FLAG_INSTALL)
2718 add_install_rule( make, source->name, xstrdup( source->name ),
2719 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2720 if (source->file->flags & FLAG_IDL_HEADER)
2721 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2722 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2724 if (!targets.count) return;
2726 output_filenames_obj_dir( make, targets );
2727 output( ": %s\n", tools_path( make, "widl" ));
2728 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2729 output_filenames( target_flags );
2730 output_filename( "--nostdinc" );
2731 output_filename( "-Ldlls/\\*" );
2732 output_filenames( defines );
2733 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2734 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2735 output_filename( source->filename );
2736 output( "\n" );
2737 output_filenames_obj_dir( make, targets );
2738 output( ": %s", source->filename );
2739 output_filenames( source->dependencies );
2740 for (i = 0; i < source->importlibdeps.count; i++)
2742 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2743 output_filename( obj_dir_path( submake, submake->module ));
2745 output( "\n" );
2749 /*******************************************************************
2750 * output_source_x
2752 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2754 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2755 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2756 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2757 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2758 if (source->file->flags & FLAG_INSTALL)
2760 add_install_rule( make, source->name, source->name,
2761 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2762 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2763 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2768 /*******************************************************************
2769 * output_source_sfd
2771 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2773 unsigned int i;
2774 char *ttf_obj = strmake( "%s.ttf", obj );
2775 char *ttf_file = src_dir_path( make, ttf_obj );
2777 if (fontforge && !make->src_dir)
2779 output( "%s: %s\n", ttf_file, source->filename );
2780 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2781 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2782 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2784 if (source->file->flags & FLAG_INSTALL)
2786 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2787 output_srcdir_symlink( make, ttf_obj );
2790 if (source->file->flags & FLAG_SFD_FONTS)
2792 struct strarray *array = source->file->args;
2794 for (i = 0; i < array->count; i++)
2796 char *font = strtok( xstrdup(array->str[i]), " \t" );
2797 char *args = strtok( NULL, "" );
2799 strarray_add( &make->all_targets, xstrdup( font ));
2800 output( "%s: %s %s\n", obj_dir_path( make, font ),
2801 tools_path( make, "sfnt2fon" ), ttf_file );
2802 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2803 tools_path( make, "sfnt2fon" ), ttf_file, args );
2804 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2810 /*******************************************************************
2811 * output_source_svg
2813 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2815 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2816 unsigned int i;
2818 if (convert && rsvg && icotool && !make->src_dir)
2820 for (i = 0; images[i]; i++)
2821 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2823 if (images[i])
2825 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2826 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2827 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2828 root_src_dir_path( "tools/buildimage" ), source->filename );
2834 /*******************************************************************
2835 * output_source_nls
2837 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2839 add_install_rule( make, source->name, source->name,
2840 strmake( "D$(nlsdir)/%s", source->name ));
2841 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2845 /*******************************************************************
2846 * output_source_desktop
2848 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2850 add_install_rule( make, source->name, source->name,
2851 strmake( "D$(datadir)/applications/%s", source->name ));
2855 /*******************************************************************
2856 * output_source_po
2858 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2860 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2861 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
2862 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2866 /*******************************************************************
2867 * output_source_in
2869 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2871 unsigned int i;
2873 if (strendswith( obj, ".man" ) && source->file->args)
2875 struct strarray symlinks;
2876 char *dir, *dest = replace_extension( obj, ".man", "" );
2877 char *lang = strchr( dest, '.' );
2878 char *section = source->file->args;
2879 if (lang)
2881 *lang++ = 0;
2882 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2884 else dir = strmake( "$(mandir)/man%s", section );
2885 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2886 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2887 for (i = 0; i < symlinks.count; i++)
2888 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2889 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2890 free( dest );
2891 free( dir );
2893 strarray_add( &make->in_files, xstrdup(obj) );
2894 strarray_add( &make->all_targets, xstrdup(obj) );
2895 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2896 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
2897 output( "%s:", obj_dir_path( make, obj ));
2898 output_filenames( source->dependencies );
2899 output( "\n" );
2900 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2904 /*******************************************************************
2905 * output_source_spec
2907 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2909 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2910 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
2911 struct strarray all_libs, dep_libs = empty_strarray;
2912 char *dll_name, *obj_name, *output_file;
2913 const char *debug_file;
2915 if (!imports.count) imports = make->imports;
2916 if (!dll_flags.count) dll_flags = make->extradllflags;
2917 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) imports = add_default_imports( make, imports );
2919 all_libs = add_import_libs( make, &dep_libs, imports, 0, make->is_cross );
2920 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
2921 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
2922 output_file = obj_dir_path( make, dll_name );
2924 strarray_add( &make->clean_files, dll_name );
2925 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2926 output( "%s.res:", obj_dir_path( make, obj ));
2927 output_filename( obj_dir_path( make, dll_name ));
2928 output_filename( tools_path( make, "wrc" ));
2929 output( "\n" );
2930 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
2931 tools_path( make, "wrc" ));
2933 output( "%s:", output_file);
2934 output_filename( source->filename );
2935 output_filename( obj_name );
2936 output_filenames( dep_libs );
2937 output_filename( tools_path( make, "winebuild" ));
2938 output_filename( tools_path( make, "winegcc" ));
2939 output( "\n" );
2940 output_winegcc_command( make, make->is_cross );
2941 output_filename( "-s" );
2942 output_filenames( dll_flags );
2943 output_filename( "-shared" );
2944 output_filename( source->filename );
2945 output_filename( obj_name );
2946 if ((debug_file = get_debug_file( make, dll_name )))
2947 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
2948 output_filenames( all_libs );
2949 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
2950 output( "\n" );
2954 /*******************************************************************
2955 * output_source_default
2957 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
2959 struct strarray defines = get_source_defines( make, source, obj );
2960 int is_dll_src = (make->testdll &&
2961 strendswith( source->name, ".c" ) &&
2962 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
2963 int need_cross = (crosstarget &&
2964 !(source->file->flags & FLAG_C_UNIX) &&
2965 (make->is_cross || make->staticlib ||
2966 (source->file->flags & FLAG_C_IMPLIB)));
2967 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
2968 (!need_cross ||
2969 (source->file->flags & FLAG_C_IMPLIB) ||
2970 (make->staticlib && !make->extlib)));
2972 if ((source->file->flags & FLAG_GENERATED) &&
2973 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2974 strarray_add( &make->clean_files, source->basename );
2976 if (need_obj)
2978 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
2979 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
2980 else if (source->file->flags & FLAG_C_IMPLIB)
2981 strarray_add( &make->implib_files, strmake( "%s.o", obj ));
2982 else if (!is_dll_src)
2983 strarray_add( &make->object_files, strmake( "%s.o", obj ));
2984 else
2985 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
2986 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2987 output( "\t%s$(CC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
2988 output_filenames( defines );
2989 if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
2991 output_filenames( unix_dllflags );
2993 else if (make->module || make->testdll)
2995 output_filenames( dll_flags );
2996 if (source->use_msvcrt) output_filenames( msvcrt_flags );
2997 if (!*dll_ext && make->module && is_crt_module( make->module ))
2998 output_filename( "-fno-builtin" );
3000 output_filenames( make->extlib ? extra_cflags_extlib : extra_cflags );
3001 output_filenames( cpp_flags );
3002 output_filename( "$(CFLAGS)" );
3003 output( "\n" );
3005 if (need_cross)
3007 if (source->file->flags & FLAG_C_IMPLIB)
3008 strarray_add( &make->crossimplib_files, strmake( "%s.cross.o", obj ));
3009 else if (!is_dll_src)
3010 strarray_add( &make->crossobj_files, strmake( "%s.cross.o", obj ));
3011 else
3012 strarray_add( &make->clean_files, strmake( "%s.cross.o", obj ));
3013 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3014 output( "\t%s$(CROSSCC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3015 output_filenames( defines );
3016 output_filenames( make->extlib ? extra_cross_cflags_extlib : extra_cross_cflags );
3017 if (make->module && is_crt_module( make->module ))
3018 output_filename( "-fno-builtin" );
3019 output_filenames( cpp_flags );
3020 output_filename( "$(CROSSCFLAGS)" );
3021 output( "\n" );
3023 if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
3025 strarray_add( &make->c2man_files, source->filename );
3026 if (make->testdll && !is_dll_src)
3028 strarray_add( &make->test_files, obj );
3029 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3030 output( "%s.ok:\n", obj_dir_path( make, obj ));
3031 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3032 cmd_prefix( "TEST" ),
3033 root_src_dir_path( "tools/runtest" ), make->testdll,
3034 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3035 make->is_cross ? "" : dll_ext, obj );
3038 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3039 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3040 output( ":" );
3041 output_filenames( source->dependencies );
3042 output( "\n" );
3046 /* dispatch table to output rules for a single source file */
3047 static const struct
3049 const char *ext;
3050 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3051 } output_source_funcs[] =
3053 { "y", output_source_y },
3054 { "l", output_source_l },
3055 { "h", output_source_h },
3056 { "rh", output_source_h },
3057 { "inl", output_source_h },
3058 { "rc", output_source_rc },
3059 { "mc", output_source_mc },
3060 { "res", output_source_res },
3061 { "idl", output_source_idl },
3062 { "sfd", output_source_sfd },
3063 { "svg", output_source_svg },
3064 { "nls", output_source_nls },
3065 { "desktop", output_source_desktop },
3066 { "po", output_source_po },
3067 { "in", output_source_in },
3068 { "x", output_source_x },
3069 { "spec", output_source_spec },
3070 { NULL, output_source_default }
3074 /*******************************************************************
3075 * get_unix_lib_name
3077 static char *get_unix_lib_name( struct makefile *make )
3079 struct incl_file *source;
3081 if (!*dll_ext) return NULL;
3082 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3084 if (!(source->file->flags & FLAG_C_UNIX)) continue;
3085 return strmake( "%s%s", get_base_name( make->module ), dll_ext );
3087 return NULL;
3091 /*******************************************************************
3092 * output_man_pages
3094 static void output_man_pages( struct makefile *make )
3096 if (make->c2man_files.count)
3098 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3100 output( "manpages::\n" );
3101 output( "\t%s -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3102 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3103 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3104 output_filename( strmake( "-o documentation/man%s", man_ext ));
3105 output_filenames( make->c2man_files );
3106 output( "\n" );
3107 output( "htmlpages::\n" );
3108 output( "\t%s -Th -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3109 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3110 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3111 output_filename( "-o documentation/html" );
3112 output_filenames( make->c2man_files );
3113 output( "\n" );
3114 output( "sgmlpages::\n" );
3115 output( "\t%s -Ts -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3116 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3117 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3118 output_filename( "-o documentation/api-guide" );
3119 output_filenames( make->c2man_files );
3120 output( "\n" );
3121 output( "xmlpages::\n" );
3122 output( "\t%s -Tx -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3123 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3124 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3125 output_filename( "-o documentation/api-guide-xml" );
3126 output_filenames( make->c2man_files );
3127 output( "\n" );
3128 strarray_add( &make->phony_targets, "manpages" );
3129 strarray_add( &make->phony_targets, "htmlpages" );
3130 strarray_add( &make->phony_targets, "sgmlpages" );
3131 strarray_add( &make->phony_targets, "xmlpages" );
3136 /*******************************************************************
3137 * output_module
3139 static void output_module( struct makefile *make )
3141 struct strarray all_libs = empty_strarray;
3142 struct strarray dep_libs = empty_strarray;
3143 struct strarray imports = make->imports;
3144 char *module_name = strmake( "%s%s", make->module, make->is_cross ? "" : dll_ext );
3145 const char *debug_file;
3146 const char *delay_load = delay_load_flag;
3147 char *spec_file = NULL;
3148 unsigned int i;
3150 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3151 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
3152 imports = add_default_imports( make, imports );
3154 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, make->is_cross ));
3155 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, 0, make->is_cross ));
3157 if (!make->use_msvcrt)
3159 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3160 strarray_addall( &all_libs, libs );
3163 if (!make->is_cross && *dll_ext) delay_load = "-Wl,-delayload,";
3164 if (delay_load)
3166 for (i = 0; i < make->delayimports.count; i++)
3167 strarray_add( &all_libs, strmake( "%s%s%s", delay_load, make->delayimports.str[i],
3168 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3170 strarray_add( &make->all_targets, module_name );
3172 if (make->is_cross)
3173 add_install_rule( make, make->module, module_name, strmake( "c%s/%s", pe_dir, module_name ));
3174 else if (*dll_ext)
3175 add_install_rule( make, make->module, module_name, strmake( "p%s/%s", so_dir, module_name ));
3176 else
3177 add_install_rule( make, make->module, module_name,
3178 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", module_name ));
3180 output( "%s:", obj_dir_path( make, module_name ));
3181 if (spec_file) output_filename( spec_file );
3182 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3183 output_filenames_obj_dir( make, make->res_files );
3184 output_filenames( dep_libs );
3185 output_filename( tools_path( make, "winebuild" ));
3186 output_filename( tools_path( make, "winegcc" ));
3187 output( "\n" );
3188 output_winegcc_command( make, make->is_cross );
3189 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3190 if (spec_file)
3192 output_filename( "-shared" );
3193 output_filename( spec_file );
3195 output_filenames( make->extradllflags );
3196 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3197 output_filenames_obj_dir( make, make->res_files );
3198 debug_file = get_debug_file( make, make->module );
3199 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3200 output_filenames( all_libs );
3201 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3202 output( "\n" );
3204 if (spec_file)
3205 output_man_pages( make );
3206 else if (*dll_ext && !make->is_win16 && strendswith( make->module, ".exe" ))
3208 char *binary = replace_extension( make->module, ".exe", "" );
3209 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3214 /*******************************************************************
3215 * output_fake_module
3217 static void output_fake_module( struct makefile *make )
3219 char *spec_file = NULL;
3221 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3223 strarray_add( &make->all_targets, make->module );
3224 add_install_rule( make, make->module, make->module, strmake( "d%s/%s", pe_dir, make->module ));
3226 output( "%s:", obj_dir_path( make, make->module ));
3227 if (spec_file) output_filename( spec_file );
3228 output_filenames_obj_dir( make, make->res_files );
3229 output_filename( tools_path( make, "winebuild" ));
3230 output_filename( tools_path( make, "winegcc" ));
3231 output( "\n" );
3232 output_winegcc_command( make, make->is_cross );
3233 output_filename( "-Wb,--fake-module" );
3234 if (spec_file)
3236 output_filename( "-shared" );
3237 output_filename( spec_file );
3239 output_filenames( make->extradllflags );
3240 output_filenames_obj_dir( make, make->res_files );
3241 output( "\n" );
3245 /*******************************************************************
3246 * output_import_lib
3248 static void output_import_lib( struct makefile *make )
3250 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3251 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3253 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3254 if (!*dll_ext && needs_delay_lib( make ))
3256 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3257 output( "%s.delay.a ", importlib_path );
3259 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3260 output_filenames_obj_dir( make, make->implib_files );
3261 output( "\n" );
3262 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3263 output_filenames( target_flags );
3264 if (make->is_win16) output_filename( "-m16" );
3265 output_filename( "--export" );
3266 output_filename( spec_file );
3267 output_filenames_obj_dir( make, make->implib_files );
3268 output( "\n" );
3269 add_install_rule( make, make->importlib,
3270 strmake( "lib%s.a", make->importlib ),
3271 strmake( "d%s/lib%s.a", so_dir, make->importlib ));
3273 if (crosstarget)
3275 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3276 output_filename( strmake( "%s.cross.a", importlib_path ));
3277 if (needs_delay_lib( make ))
3279 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3280 output_filename( strmake( "%s.delay.a", importlib_path ));
3282 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3283 output_filenames_obj_dir( make, make->crossimplib_files );
3284 output( "\n" );
3285 output( "\t%s%s -b %s -w --implib -o $@", cmd_prefix( "BUILD" ),
3286 tools_path( make, "winebuild" ), crosstarget );
3287 if (make->is_win16) output_filename( "-m16" );
3288 output_filename( "--export" );
3289 output_filename( spec_file );
3290 output_filenames_obj_dir( make, make->crossimplib_files );
3291 output( "\n" );
3292 add_install_rule( make, make->importlib,
3293 strmake( "lib%s.cross.a", make->importlib ),
3294 strmake( "d%s/lib%s.a", pe_dir, make->importlib ));
3299 /*******************************************************************
3300 * output_unix_lib
3302 static void output_unix_lib( struct makefile *make )
3304 struct strarray unix_libs = empty_strarray;
3305 struct strarray unix_deps = empty_strarray;
3306 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3308 if (!make->native_unix_lib)
3310 struct strarray unix_imports = empty_strarray;
3312 strarray_add( &unix_imports, "ntdll" );
3313 strarray_add( &unix_deps, obj_dir_path( top_makefile, "dlls/ntdll/ntdll.so" ));
3314 strarray_add( &unix_imports, "winecrt0" );
3315 if (spec_file) strarray_add( &unix_deps, spec_file );
3317 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 0 ));
3318 strarray_addall( &unix_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3319 strarray_addall( &unix_libs, libs );
3321 else unix_libs = add_unix_libraries( make, &unix_deps );
3323 strarray_add( &make->all_targets, make->unixlib );
3324 add_install_rule( make, make->module, make->unixlib, strmake( "p%s/%s", so_dir, make->unixlib ));
3325 output( "%s:", obj_dir_path( make, make->unixlib ));
3326 output_filenames_obj_dir( make, make->unixobj_files );
3327 output_filenames( unix_deps );
3329 if (make->native_unix_lib)
3331 output( "\n" );
3332 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3333 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3335 else
3337 output_filename( tools_path( make, "winebuild" ));
3338 output_filename( tools_path( make, "winegcc" ));
3339 output( "\n" );
3340 output_winegcc_command( make, 0 );
3341 output_filename( "-munix" );
3342 output_filename( "-shared" );
3343 if (spec_file) output_filename( spec_file );
3345 output_filenames_obj_dir( make, make->unixobj_files );
3346 output_filenames( unix_libs );
3347 output_filename( "$(LDFLAGS)" );
3348 output( "\n" );
3352 /*******************************************************************
3353 * output_static_lib
3355 static void output_static_lib( struct makefile *make )
3357 if (!crosstarget || !make->extlib)
3359 strarray_add( &make->clean_files, make->staticlib );
3360 output( "%s: %s", obj_dir_path( make, make->staticlib ), tools_path( make, "winebuild" ));
3361 output_filenames_obj_dir( make, make->object_files );
3362 output_filenames_obj_dir( make, make->unixobj_files );
3363 output( "\n" );
3364 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3365 output_filenames( target_flags );
3366 output_filenames_obj_dir( make, make->object_files );
3367 output_filenames_obj_dir( make, make->unixobj_files );
3368 output( "\n" );
3369 add_install_rule( make, make->staticlib, make->staticlib,
3370 strmake( "d%s/%s", so_dir, make->staticlib ));
3372 if (crosstarget)
3374 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3376 strarray_add( &make->clean_files, name );
3377 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3378 output_filenames_obj_dir( make, make->crossobj_files );
3379 output( "\n" );
3380 output( "\t%s%s -b %s -w --staticlib -o $@", cmd_prefix( "BUILD" ),
3381 tools_path( make, "winebuild" ), crosstarget );
3382 output_filenames_obj_dir( make, make->crossobj_files );
3383 output( "\n" );
3384 if (!make->extlib)
3385 add_install_rule( make, make->staticlib, name,
3386 strmake( "d%s/%s", pe_dir, make->staticlib ));
3391 /*******************************************************************
3392 * output_shared_lib
3394 static void output_shared_lib( struct makefile *make )
3396 unsigned int i;
3397 char *basename, *p;
3398 struct strarray names = get_shared_lib_names( make->sharedlib );
3399 struct strarray all_libs = empty_strarray;
3400 struct strarray dep_libs = empty_strarray;
3402 basename = xstrdup( make->sharedlib );
3403 if ((p = strchr( basename, '.' ))) *p = 0;
3405 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3406 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3407 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3408 strarray_addall( &all_libs, libs );
3410 output( "%s:", obj_dir_path( make, make->sharedlib ));
3411 output_filenames_obj_dir( make, make->object_files );
3412 output_filenames( dep_libs );
3413 output( "\n" );
3414 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3415 output_filenames_obj_dir( make, make->object_files );
3416 output_filenames( all_libs );
3417 output_filename( "$(LDFLAGS)" );
3418 output( "\n" );
3419 add_install_rule( make, make->sharedlib, make->sharedlib, strmake( "p%s/%s", so_dir, make->sharedlib ));
3420 for (i = 1; i < names.count; i++)
3422 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3423 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3424 add_install_rule( make, names.str[i], names.str[i-1], strmake( "y%s/%s", so_dir, names.str[i] ));
3426 strarray_addall( &make->all_targets, names );
3430 /*******************************************************************
3431 * output_test_module
3433 static void output_test_module( struct makefile *make )
3435 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3436 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3437 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3438 struct strarray dep_libs = empty_strarray;
3439 struct strarray all_libs = add_import_libs( make, &dep_libs, add_default_imports( make, make->imports ),
3440 0, make->is_cross );
3441 struct makefile *parent = get_parent_makefile( make );
3442 const char *ext = make->is_cross ? "" : dll_ext;
3443 const char *debug_file;
3444 char *output_file;
3446 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3447 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3448 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3449 output( "%s:\n", output_file );
3450 output_winegcc_command( make, make->is_cross );
3451 output_filenames( make->extradllflags );
3452 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3453 output_filenames_obj_dir( make, make->res_files );
3454 if ((debug_file = get_debug_file( make, testmodule )))
3455 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3456 output_filenames( all_libs );
3457 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3458 output( "\n" );
3459 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3460 output_winegcc_command( make, make->is_cross );
3461 output_filename( "-s" );
3462 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3463 output_filenames( make->extradllflags );
3464 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3465 output_filenames_obj_dir( make, make->res_files );
3466 output_filenames( all_libs );
3467 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3468 output( "\n" );
3469 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3470 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3471 output_filenames_obj_dir( make, make->res_files );
3472 output_filenames( dep_libs );
3473 output_filename( tools_path( make, "winebuild" ));
3474 output_filename( tools_path( make, "winegcc" ));
3475 output( "\n" );
3477 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3478 output( "\t%secho \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3479 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3481 output_filenames_obj_dir( make, make->ok_files );
3482 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3483 if (parent)
3485 output_filename( parent->is_cross ? obj_dir_path( parent, make->testdll )
3486 : strmake( "%s%s", obj_dir_path( parent, make->testdll ), dll_ext ));
3487 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3489 output( "\n" );
3490 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3491 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3492 output( "\n" );
3493 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3494 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3495 output( "%s::\n", obj_dir_path( make, "testclean" ));
3496 output( "\trm -f" );
3497 output_filenames_obj_dir( make, make->ok_files );
3498 output( "\n" );
3499 strarray_addall( &make->clean_files, make->ok_files );
3500 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3504 /*******************************************************************
3505 * output_programs
3507 static void output_programs( struct makefile *make )
3509 unsigned int i, j;
3511 for (i = 0; i < make->programs.count; i++)
3513 char *program_installed = NULL;
3514 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3515 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3516 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3517 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3518 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3520 if (!objs.count) objs = make->object_files;
3521 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3523 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3524 strarray_addall( &all_libs, libs );
3527 output( "%s:", obj_dir_path( make, program ) );
3528 output_filenames_obj_dir( make, objs );
3529 output_filenames( deps );
3530 output( "\n" );
3531 output( "\t%s$(CC) -o $@", cmd_prefix( "CC" ));
3532 output_filenames_obj_dir( make, objs );
3533 output_filenames( all_libs );
3534 output_filename( "$(LDFLAGS)" );
3535 output( "\n" );
3536 strarray_add( &make->all_targets, program );
3538 for (j = 0; j < symlinks.count; j++)
3540 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3541 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3543 strarray_addall( &make->all_targets, symlinks );
3545 add_install_rule( make, program, program_installed ? program_installed : program,
3546 strmake( "p$(bindir)/%s", program ));
3547 for (j = 0; j < symlinks.count; j++)
3548 add_install_rule( make, symlinks.str[j], program,
3549 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3554 /*******************************************************************
3555 * output_subdirs
3557 static void output_subdirs( struct makefile *make )
3559 struct strarray all_targets = empty_strarray;
3560 struct strarray makefile_deps = empty_strarray;
3561 struct strarray clean_files = empty_strarray;
3562 struct strarray testclean_files = empty_strarray;
3563 struct strarray distclean_files = empty_strarray;
3564 struct strarray dependencies = empty_strarray;
3565 struct strarray install_lib_deps = empty_strarray;
3566 struct strarray install_dev_deps = empty_strarray;
3567 struct strarray tooldeps_deps = empty_strarray;
3568 struct strarray buildtest_deps = empty_strarray;
3569 unsigned int i;
3571 strarray_addall( &clean_files, make->clean_files );
3572 strarray_addall( &distclean_files, make->distclean_files );
3573 strarray_addall( &all_targets, make->all_targets );
3574 for (i = 0; i < subdirs.count; i++)
3576 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3577 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3578 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3579 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3580 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3581 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3582 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3583 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3585 if (submakes[i]->disabled) continue;
3587 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3588 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3589 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3590 if (submakes[i]->testdll)
3591 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3592 if (submakes[i]->install_rules[INSTALL_LIB].count)
3593 strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
3594 if (submakes[i]->install_rules[INSTALL_DEV].count)
3595 strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
3597 strarray_addall( &dependencies, makefile_deps );
3598 output( "all:" );
3599 output_filenames( all_targets );
3600 output( "\n" );
3601 output( "Makefile:" );
3602 output_filenames( makefile_deps );
3603 output( "\n" );
3604 output_filenames( dependencies );
3605 output( ":\n" );
3606 if (install_lib_deps.count)
3608 output( "install install-lib::" );
3609 output_filenames( install_lib_deps );
3610 output( "\n" );
3611 strarray_add_uniq( &make->phony_targets, "install" );
3612 strarray_add_uniq( &make->phony_targets, "install-lib" );
3614 if (install_dev_deps.count)
3616 output( "install install-dev::" );
3617 output_filenames( install_dev_deps );
3618 output( "\n" );
3619 strarray_add_uniq( &make->phony_targets, "install" );
3620 strarray_add_uniq( &make->phony_targets, "install-dev" );
3622 output_uninstall_rules( make );
3623 if (buildtest_deps.count)
3625 output( "buildtests:" );
3626 output_filenames( buildtest_deps );
3627 output( "\n" );
3628 strarray_add_uniq( &make->phony_targets, "buildtests" );
3630 output( "check test:" );
3631 output_filenames( testclean_files );
3632 output( "\n" );
3633 strarray_add_uniq( &make->phony_targets, "check" );
3634 strarray_add_uniq( &make->phony_targets, "test" );
3636 output( "clean::\n");
3637 output_rm_filenames( clean_files );
3638 output( "testclean::\n");
3639 output_rm_filenames( testclean_files );
3640 output( "distclean::\n");
3641 output_rm_filenames( distclean_files );
3642 strarray_add_uniq( &make->phony_targets, "distclean" );
3643 strarray_add_uniq( &make->phony_targets, "testclean" );
3645 if (tooldeps_deps.count)
3647 output( "__tooldeps__:" );
3648 output_filenames( tooldeps_deps );
3649 output( "\n" );
3650 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3653 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3655 if (make->phony_targets.count)
3657 output( ".PHONY:" );
3658 output_filenames( make->phony_targets );
3659 output( "\n" );
3664 /*******************************************************************
3665 * output_sources
3667 static void output_sources( struct makefile *make )
3669 struct strarray all_targets = empty_strarray;
3670 struct incl_file *source;
3671 unsigned int i, j;
3673 strarray_add_uniq( &make->phony_targets, "all" );
3675 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3677 char *obj = xstrdup( source->name );
3678 char *ext = get_extension( obj );
3680 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3681 *ext++ = 0;
3683 for (j = 0; output_source_funcs[j].ext; j++)
3684 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3686 output_source_funcs[j].fn( make, source, obj );
3687 strarray_addall_uniq( &make->dependencies, source->dependencies );
3690 /* special case for winetest: add resource files from other test dirs */
3691 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3693 struct strarray tests = enable_tests;
3694 if (!tests.count)
3695 for (i = 0; i < subdirs.count; i++)
3696 if (submakes[i]->testdll && !submakes[i]->disabled)
3697 strarray_add( &tests, submakes[i]->testdll );
3698 for (i = 0; i < tests.count; i++)
3699 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3702 if (make->dlldata_files.count)
3704 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3705 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3706 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3707 output_filenames( make->dlldata_files );
3708 output( "\n" );
3711 if (make->staticlib) output_static_lib( make );
3712 else if (make->module)
3714 output_module( make );
3715 if (*dll_ext && !make->is_cross) output_fake_module( make );
3716 if (make->unixlib) output_unix_lib( make );
3717 if (make->importlib) output_import_lib( make );
3719 else if (make->testdll) output_test_module( make );
3720 else if (make->sharedlib) output_shared_lib( make );
3721 else if (make->programs.count) output_programs( make );
3723 for (i = 0; i < make->scripts.count; i++)
3724 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3725 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3727 for (i = 0; i < make->extra_targets.count; i++)
3728 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3729 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3730 else
3731 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3733 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3734 strarray_add( &make->distclean_files, "Makefile" );
3735 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3737 if (!make->obj_dir)
3738 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3739 else if (!strcmp( make->obj_dir, "po" ))
3740 strarray_add( &make->distclean_files, "LINGUAS" );
3742 strarray_addall( &make->clean_files, make->object_files );
3743 strarray_addall( &make->clean_files, make->crossobj_files );
3744 strarray_addall( &make->clean_files, make->implib_files );
3745 strarray_addall( &make->clean_files, make->crossimplib_files );
3746 strarray_addall( &make->clean_files, make->unixobj_files );
3747 strarray_addall( &make->clean_files, make->res_files );
3748 strarray_addall( &make->clean_files, make->pot_files );
3749 strarray_addall( &make->clean_files, make->debug_files );
3750 strarray_addall( &make->clean_files, make->all_targets );
3752 if (make == top_makefile)
3754 output_subdirs( make );
3755 return;
3758 strarray_addall( &all_targets, make->all_targets );
3759 strarray_addall( &all_targets, make->font_files );
3760 if (all_targets.count)
3762 output( "%s:", obj_dir_path( make, "all" ));
3763 output_filenames_obj_dir( make, all_targets );
3764 output( "\n" );
3765 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3767 output_install_rules( make, INSTALL_LIB, "install-lib" );
3768 output_install_rules( make, INSTALL_DEV, "install-dev" );
3770 if (make->clean_files.count)
3772 output( "%s::\n", obj_dir_path( make, "clean" ));
3773 output( "\trm -f" );
3774 output_filenames_obj_dir( make, make->clean_files );
3775 output( "\n" );
3776 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3781 /*******************************************************************
3782 * create_temp_file
3784 static FILE *create_temp_file( const char *orig )
3786 char *name = xmalloc( strlen(orig) + 13 );
3787 unsigned int i, id = getpid();
3788 int fd;
3789 FILE *ret = NULL;
3791 for (i = 0; i < 100; i++)
3793 sprintf( name, "%s.tmp%08x", orig, id );
3794 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3796 ret = fdopen( fd, "w" );
3797 break;
3799 if (errno != EEXIST) break;
3800 id += 7777;
3802 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3803 temp_file_name = name;
3804 return ret;
3808 /*******************************************************************
3809 * rename_temp_file
3811 static void rename_temp_file( const char *dest )
3813 int ret = rename( temp_file_name, dest );
3814 if (ret == -1 && errno == EEXIST)
3816 /* rename doesn't overwrite on windows */
3817 unlink( dest );
3818 ret = rename( temp_file_name, dest );
3820 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3821 temp_file_name = NULL;
3825 /*******************************************************************
3826 * are_files_identical
3828 static int are_files_identical( FILE *file1, FILE *file2 )
3830 for (;;)
3832 char buffer1[8192], buffer2[8192];
3833 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3834 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3835 if (size1 != size2) return 0;
3836 if (!size1) return feof( file1 ) && feof( file2 );
3837 if (memcmp( buffer1, buffer2, size1 )) return 0;
3842 /*******************************************************************
3843 * rename_temp_file_if_changed
3845 static void rename_temp_file_if_changed( const char *dest )
3847 FILE *file1, *file2;
3848 int do_rename = 1;
3850 if ((file1 = fopen( dest, "r" )))
3852 if ((file2 = fopen( temp_file_name, "r" )))
3854 do_rename = !are_files_identical( file1, file2 );
3855 fclose( file2 );
3857 fclose( file1 );
3859 if (!do_rename)
3861 unlink( temp_file_name );
3862 temp_file_name = NULL;
3864 else rename_temp_file( dest );
3868 /*******************************************************************
3869 * output_linguas
3871 static void output_linguas( const struct makefile *make )
3873 const char *dest = obj_dir_path( make, "LINGUAS" );
3874 struct incl_file *source;
3876 output_file = create_temp_file( dest );
3878 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3879 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3880 if (strendswith( source->name, ".po" ))
3881 output( "%s\n", replace_extension( source->name, ".po", "" ));
3883 if (fclose( output_file )) fatal_perror( "write" );
3884 output_file = NULL;
3885 rename_temp_file_if_changed( dest );
3889 /*******************************************************************
3890 * output_testlist
3892 static void output_testlist( const struct makefile *make )
3894 const char *dest = obj_dir_path( make, "testlist.c" );
3895 unsigned int i;
3897 output_file = create_temp_file( dest );
3899 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3900 output( "#define WIN32_LEAN_AND_MEAN\n" );
3901 output( "#include <windows.h>\n\n" );
3902 output( "#define STANDALONE\n" );
3903 output( "#include \"wine/test.h\"\n\n" );
3905 for (i = 0; i < make->test_files.count; i++)
3906 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3907 output( "\n" );
3908 output( "const struct test winetest_testlist[] =\n" );
3909 output( "{\n" );
3910 for (i = 0; i < make->test_files.count; i++)
3911 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3912 output( " { 0, 0 }\n" );
3913 output( "};\n" );
3915 if (fclose( output_file )) fatal_perror( "write" );
3916 output_file = NULL;
3917 rename_temp_file_if_changed( dest );
3921 /*******************************************************************
3922 * output_gitignore
3924 static void output_gitignore( const char *dest, struct strarray files )
3926 int i;
3928 output_file = create_temp_file( dest );
3930 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3931 for (i = 0; i < files.count; i++)
3933 if (!strchr( files.str[i], '/' )) output( "/" );
3934 output( "%s\n", files.str[i] );
3937 if (fclose( output_file )) fatal_perror( "write" );
3938 output_file = NULL;
3939 rename_temp_file( dest );
3943 /*******************************************************************
3944 * output_stub_makefile
3946 static void output_stub_makefile( struct makefile *make )
3948 struct strarray targets = empty_strarray;
3949 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
3951 if (make->obj_dir) create_dir( make->obj_dir );
3953 output_file_name = obj_dir_path( make, "Makefile" );
3954 output_file = create_temp_file( output_file_name );
3956 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
3958 if (make_var) output( "MAKE = %s\n\n", make_var );
3959 output( "all:\n" );
3961 if (make->all_targets.count) strarray_add( &targets, "all" );
3962 if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
3963 strarray_add( &targets, "install" );
3964 if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
3965 if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
3966 if (make->clean_files.count) strarray_add( &targets, "clean" );
3967 if (make->test_files.count)
3969 strarray_add( &targets, "check" );
3970 strarray_add( &targets, "test" );
3971 strarray_add( &targets, "testclean" );
3974 output_filenames( targets );
3975 output_filenames( make->clean_files );
3976 output( ":\n" );
3977 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
3978 output( ".PHONY:" );
3979 output_filenames( targets );
3980 output( "\n" );
3982 fclose( output_file );
3983 output_file = NULL;
3984 rename_temp_file( output_file_name );
3988 /*******************************************************************
3989 * output_silent_rules
3991 static void output_silent_rules(void)
3993 static const char *cmds[] =
3995 "BISON",
3996 "BUILD",
3997 "CC",
3998 "CCLD",
3999 "FLEX",
4000 "GEN",
4001 "LN",
4002 "MSG",
4003 "SED",
4004 "TEST",
4005 "WIDL",
4006 "WMC",
4007 "WRC"
4009 unsigned int i;
4011 output( "V = 0\n" );
4012 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++)
4014 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4015 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4016 output( "quiet_%s_1 =\n", cmds[i] );
4021 /*******************************************************************
4022 * output_top_makefile
4024 static void output_top_makefile( struct makefile *make )
4026 char buffer[1024];
4027 FILE *src_file;
4028 int i, found = 0;
4030 output_file_name = obj_dir_path( make, output_makefile_name );
4031 output_file = create_temp_file( output_file_name );
4033 /* copy the contents of the source makefile */
4034 src_file = open_input_makefile( make );
4035 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4037 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4038 found = !strncmp( buffer, separator, strlen(separator) );
4040 if (fclose( src_file )) fatal_perror( "close" );
4041 input_file_name = NULL;
4043 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4045 if (silent_rules) output_silent_rules();
4046 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4047 output_sources( make );
4048 /* disable implicit rules */
4049 output( ".SUFFIXES:\n" );
4051 fclose( output_file );
4052 output_file = NULL;
4053 rename_temp_file( output_file_name );
4057 /*******************************************************************
4058 * output_dependencies
4060 static void output_dependencies( struct makefile *make )
4062 struct strarray ignore_files = empty_strarray;
4064 if (make->obj_dir) create_dir( make->obj_dir );
4066 if (make == top_makefile) output_top_makefile( make );
4067 else output_stub_makefile( make );
4069 strarray_addall( &ignore_files, make->distclean_files );
4070 strarray_addall( &ignore_files, make->clean_files );
4071 if (make->testdll) output_testlist( make );
4072 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4073 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4075 create_file_directories( make, ignore_files );
4077 output_file_name = NULL;
4081 /*******************************************************************
4082 * load_sources
4084 static void load_sources( struct makefile *make )
4086 static const char *source_vars[] =
4088 "SOURCES",
4089 "C_SRCS",
4090 "OBJC_SRCS",
4091 "RC_SRCS",
4092 "MC_SRCS",
4093 "IDL_SRCS",
4094 "BISON_SRCS",
4095 "LEX_SRCS",
4096 "HEADER_SRCS",
4097 "XTEMPLATE_SRCS",
4098 "SVG_SRCS",
4099 "FONT_SRCS",
4100 "IN_SRCS",
4101 "PO_SRCS",
4102 "MANPAGES",
4103 NULL
4105 const char **var;
4106 unsigned int i;
4107 struct strarray value;
4108 struct incl_file *file;
4110 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4111 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4113 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4114 make->module = get_expanded_make_variable( make, "MODULE" );
4115 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4116 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4117 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4118 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4119 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4120 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4122 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4123 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4124 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4125 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4126 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4127 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4128 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4129 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4131 if (make->extlib) make->staticlib = make->extlib;
4132 if (make->staticlib) make->module = make->staticlib;
4134 if (host_cpu)
4136 value = get_expanded_file_local_var( make, host_cpu, "EXTRADLLFLAGS" );
4137 if (value.count) make->extradllflags = value;
4140 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4141 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4142 make->use_msvcrt = (make->module || make->testdll || make->is_win16) &&
4143 !strarray_exists( &make->extradllflags, "-mcygwin" );
4144 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4145 strarray_exists( &make->extradllflags, "-mwindows" );
4146 make->native_unix_lib = !!make->unixlib;
4148 if (make->use_msvcrt) strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4150 if (make->module && !make->install_lib.count && !make->install_dev.count)
4152 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4153 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4154 else strarray_add( &make->install_lib, make->module );
4157 make->include_paths = empty_strarray;
4158 make->include_args = empty_strarray;
4159 make->define_args = empty_strarray;
4160 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4162 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4163 for (i = 0; i < value.count; i++)
4164 if (!strncmp( value.str[i], "-I", 2 ))
4165 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4166 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4167 strarray_add_uniq( &make->define_args, value.str[i] );
4168 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4170 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4171 if (make->src_dir)
4172 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4173 if (make->parent_dir)
4174 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4175 strarray_add( &make->include_args, "-Iinclude" );
4176 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4178 list_init( &make->sources );
4179 list_init( &make->includes );
4181 for (var = source_vars; *var; var++)
4183 value = get_expanded_make_var_array( make, *var );
4184 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4187 add_generated_sources( make );
4188 if (!make->unixlib) make->unixlib = get_unix_lib_name( make );
4190 if (make->use_msvcrt) strarray_add( &make->define_args, get_crt_define( make ));
4192 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4193 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4195 make->is_cross = crosstarget && make->use_msvcrt;
4197 if (!*dll_ext || make->is_cross)
4198 for (i = 0; i < make->delayimports.count; i++)
4199 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4203 /*******************************************************************
4204 * parse_makeflags
4206 static void parse_makeflags( const char *flags )
4208 const char *p = flags;
4209 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4211 while (*p)
4213 while (isspace(*p)) p++;
4214 var = buffer;
4215 while (*p && !isspace(*p))
4217 if (*p == '\\' && p[1]) p++;
4218 *var++ = *p++;
4220 *var = 0;
4221 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4226 /*******************************************************************
4227 * parse_option
4229 static int parse_option( const char *opt )
4231 if (opt[0] != '-')
4233 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4234 return 0;
4236 switch(opt[1])
4238 case 'f':
4239 if (opt[2]) output_makefile_name = opt + 2;
4240 break;
4241 case 'R':
4242 relative_dir_mode = 1;
4243 break;
4244 case 'S':
4245 silent_rules = 1;
4246 break;
4247 default:
4248 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4249 exit(1);
4251 return 1;
4255 /*******************************************************************
4256 * main
4258 int main( int argc, char *argv[] )
4260 const char *makeflags = getenv( "MAKEFLAGS" );
4261 int i, j;
4263 if (makeflags) parse_makeflags( makeflags );
4265 i = 1;
4266 while (i < argc)
4268 if (parse_option( argv[i] ))
4270 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4271 argc--;
4273 else i++;
4276 if (relative_dir_mode)
4278 char *relpath;
4280 if (argc != 3)
4282 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4283 exit( 1 );
4285 relpath = get_relative_path( argv[1], argv[2] );
4286 printf( "%s\n", relpath ? relpath : "." );
4287 exit( 0 );
4290 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4292 atexit( cleanup_files );
4293 signal( SIGTERM, exit_on_signal );
4294 signal( SIGINT, exit_on_signal );
4295 #ifdef SIGHUP
4296 signal( SIGHUP, exit_on_signal );
4297 #endif
4299 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4301 top_makefile = parse_makefile( NULL );
4303 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4304 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4305 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4306 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4307 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4308 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4309 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4310 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4311 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4312 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4313 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4314 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4316 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4317 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4318 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4319 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4320 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4321 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
4322 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4323 host_cpu = get_expanded_make_variable( top_makefile, "host_cpu" );
4324 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4325 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4326 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4327 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4328 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4329 bison = get_expanded_make_variable( top_makefile, "BISON" );
4330 ar = get_expanded_make_variable( top_makefile, "AR" );
4331 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4332 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4333 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4334 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4335 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4336 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4337 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4339 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4340 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4341 if (!exe_ext) exe_ext = "";
4342 if (!tools_ext) tools_ext = "";
4343 if (!man_ext) man_ext = "3w";
4344 if (host_cpu && (host_cpu = normalize_arch( host_cpu )))
4346 so_dir = strmake( "$(dlldir)/%s-unix", host_cpu );
4347 pe_dir = strmake( "$(dlldir)/%s-windows", host_cpu );
4349 else
4350 so_dir = pe_dir = "$(dlldir)";
4352 extra_cflags_extlib = remove_warning_flags( extra_cflags );
4353 extra_cross_cflags_extlib = remove_warning_flags( extra_cross_cflags );
4355 top_makefile->src_dir = root_src_dir;
4356 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4357 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4358 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4360 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4362 load_sources( top_makefile );
4363 for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
4365 output_dependencies( top_makefile );
4366 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4368 return 0;