usp10/tests: A spelling fix in an ok() message.
[wine.git] / tools / makedep.c
blobe37bd8d63a9513ab3f9796b6ffe3413c73be0cc6
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013 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"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <signal.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "wine/list.h"
38 struct strarray
40 unsigned int count; /* strings in use */
41 unsigned int size; /* total allocated size */
42 const char **str;
45 enum incl_type
47 INCL_NORMAL, /* #include "foo.h" */
48 INCL_SYSTEM, /* #include <foo.h> */
49 INCL_IMPORT, /* idl import "foo.idl" */
50 INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
51 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
52 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
55 struct dependency
57 int line; /* source line where this header is included */
58 enum incl_type type; /* type of include */
59 char *name; /* header name */
62 struct file
64 struct list entry;
65 char *name; /* full file name relative to cwd */
66 void *args; /* custom arguments for makefile rule */
67 unsigned int flags; /* flags (see below) */
68 unsigned int deps_count; /* files in use */
69 unsigned int deps_size; /* total allocated size */
70 struct dependency *deps; /* all header dependencies */
73 struct incl_file
75 struct list entry;
76 struct file *file;
77 char *name;
78 char *filename;
79 char *sourcename; /* source file name for generated headers */
80 struct incl_file *included_by; /* file that included this one */
81 int included_line; /* line where this file was included */
82 enum incl_type type; /* type of include */
83 int use_msvcrt; /* put msvcrt headers in the search path? */
84 struct incl_file *owner;
85 unsigned int files_count; /* files in use */
86 unsigned int files_size; /* total allocated size */
87 struct incl_file **files;
88 struct strarray dependencies; /* file dependencies */
91 #define FLAG_GENERATED 0x000001 /* generated file */
92 #define FLAG_INSTALL 0x000002 /* file to install */
93 #define FLAG_PARENTDIR 0x000004 /* file comes from parent dir */
94 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
95 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
96 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
97 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
98 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
99 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
100 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
101 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
102 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
103 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
104 #define FLAG_C_UNIX 0x040000 /* file is part of a Unix library */
105 #define FLAG_SFD_FONTS 0x080000 /* sfd file generated bitmap fonts */
107 static const struct
109 unsigned int flag;
110 const char *ext;
111 } idl_outputs[] =
113 { FLAG_IDL_TYPELIB, ".tlb" },
114 { FLAG_IDL_REGTYPELIB, "_t.res" },
115 { FLAG_IDL_CLIENT, "_c.c" },
116 { FLAG_IDL_IDENT, "_i.c" },
117 { FLAG_IDL_PROXY, "_p.c" },
118 { FLAG_IDL_SERVER, "_s.c" },
119 { FLAG_IDL_REGISTER, "_r.res" },
120 { FLAG_IDL_HEADER, ".h" }
123 #define HASH_SIZE 997
125 static struct list files[HASH_SIZE];
127 static const struct strarray empty_strarray;
129 enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
131 /* variables common to all makefiles */
132 static struct strarray linguas;
133 static struct strarray dll_flags;
134 static struct strarray target_flags;
135 static struct strarray msvcrt_flags;
136 static struct strarray extra_cflags;
137 static struct strarray extra_cross_cflags;
138 static struct strarray cpp_flags;
139 static struct strarray lddll_flags;
140 static struct strarray libs;
141 static struct strarray enable_tests;
142 static struct strarray cmdline_vars;
143 static struct strarray disabled_dirs;
144 static struct strarray cross_import_libs;
145 static struct strarray delay_import_libs;
146 static struct strarray top_install_lib;
147 static struct strarray top_install_dev;
148 static const char *root_src_dir;
149 static const char *tools_dir;
150 static const char *tools_ext;
151 static const char *exe_ext;
152 static const char *dll_ext;
153 static const char *man_ext;
154 static const char *crosstarget;
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;
168 struct makefile
170 /* values determined from input makefile */
171 struct strarray vars;
172 struct strarray include_paths;
173 struct strarray include_args;
174 struct strarray define_args;
175 struct strarray programs;
176 struct strarray scripts;
177 struct strarray imports;
178 struct strarray subdirs;
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 list sources;
185 struct list includes;
186 const char *base_dir;
187 const char *src_dir;
188 const char *obj_dir;
189 const char *top_src_dir;
190 const char *top_obj_dir;
191 const char *parent_dir;
192 const char *module;
193 const char *testdll;
194 const char *sharedlib;
195 const char *staticlib;
196 const char *staticimplib;
197 const char *importlib;
198 int disabled;
199 int use_msvcrt;
200 int is_cross;
201 int is_win16;
202 int is_exe;
203 struct makefile **submakes;
205 /* values generated at output time */
206 struct strarray in_files;
207 struct strarray ok_files;
208 struct strarray clean_files;
209 struct strarray distclean_files;
210 struct strarray uninstall_files;
211 struct strarray object_files;
212 struct strarray crossobj_files;
213 struct strarray unixobj_files;
214 struct strarray res_files;
215 struct strarray c2man_files;
216 struct strarray dlldata_files;
217 struct strarray implib_objs;
218 struct strarray all_targets;
219 struct strarray phony_targets;
220 struct strarray dependencies;
221 struct strarray install_rules[NB_INSTALL_RULES];
224 static struct makefile *top_makefile;
226 static const char separator[] = "### Dependencies";
227 static const char *output_makefile_name = "Makefile";
228 static const char *input_file_name;
229 static const char *output_file_name;
230 static const char *temp_file_name;
231 static int relative_dir_mode;
232 static int input_line;
233 static int output_column;
234 static FILE *output_file;
236 static const char Usage[] =
237 "Usage: makedep [options] [directories]\n"
238 "Options:\n"
239 " -R from to Compute the relative path between two directories\n"
240 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
243 #ifndef __GNUC__
244 #define __attribute__(x)
245 #endif
247 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
248 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
249 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
250 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
252 /*******************************************************************
253 * fatal_error
255 static void fatal_error( const char *msg, ... )
257 va_list valist;
258 va_start( valist, msg );
259 if (input_file_name)
261 fprintf( stderr, "%s:", input_file_name );
262 if (input_line) fprintf( stderr, "%d:", input_line );
263 fprintf( stderr, " error: " );
265 else fprintf( stderr, "makedep: error: " );
266 vfprintf( stderr, msg, valist );
267 va_end( valist );
268 exit(1);
272 /*******************************************************************
273 * fatal_perror
275 static void fatal_perror( const char *msg, ... )
277 va_list valist;
278 va_start( valist, msg );
279 if (input_file_name)
281 fprintf( stderr, "%s:", input_file_name );
282 if (input_line) fprintf( stderr, "%d:", input_line );
283 fprintf( stderr, " error: " );
285 else fprintf( stderr, "makedep: error: " );
286 vfprintf( stderr, msg, valist );
287 perror( " " );
288 va_end( valist );
289 exit(1);
293 /*******************************************************************
294 * cleanup_files
296 static void cleanup_files(void)
298 if (temp_file_name) unlink( temp_file_name );
299 if (output_file_name) unlink( output_file_name );
303 /*******************************************************************
304 * exit_on_signal
306 static void exit_on_signal( int sig )
308 exit( 1 ); /* this will call the atexit functions */
312 /*******************************************************************
313 * xmalloc
315 static void *xmalloc( size_t size )
317 void *res;
318 if (!(res = malloc (size ? size : 1)))
319 fatal_error( "Virtual memory exhausted.\n" );
320 return res;
324 /*******************************************************************
325 * xrealloc
327 static void *xrealloc (void *ptr, size_t size)
329 void *res;
330 assert( size );
331 if (!(res = realloc( ptr, size )))
332 fatal_error( "Virtual memory exhausted.\n" );
333 return res;
336 /*******************************************************************
337 * xstrdup
339 static char *xstrdup( const char *str )
341 char *res = strdup( str );
342 if (!res) fatal_error( "Virtual memory exhausted.\n" );
343 return res;
347 /*******************************************************************
348 * strmake
350 static char *strmake( const char* fmt, ... )
352 int n;
353 size_t size = 100;
354 va_list ap;
356 for (;;)
358 char *p = xmalloc (size);
359 va_start(ap, fmt);
360 n = vsnprintf (p, size, fmt, ap);
361 va_end(ap);
362 if (n == -1) size *= 2;
363 else if ((size_t)n >= size) size = n + 1;
364 else return xrealloc( p, n + 1 );
365 free(p);
370 /*******************************************************************
371 * strendswith
373 static int strendswith( const char* str, const char* end )
375 size_t l = strlen( str );
376 size_t m = strlen( end );
378 return l >= m && strcmp(str + l - m, end) == 0;
382 /*******************************************************************
383 * output
385 static void output( const char *format, ... )
387 int ret;
388 va_list valist;
390 va_start( valist, format );
391 ret = vfprintf( output_file, format, valist );
392 va_end( valist );
393 if (ret < 0) fatal_perror( "output" );
394 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
395 else output_column += ret;
399 /*******************************************************************
400 * strarray_add
402 static void strarray_add( struct strarray *array, const char *str )
404 if (array->count == array->size)
406 if (array->size) array->size *= 2;
407 else array->size = 16;
408 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
410 array->str[array->count++] = str;
414 /*******************************************************************
415 * strarray_addall
417 static void strarray_addall( struct strarray *array, struct strarray added )
419 unsigned int i;
421 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
425 /*******************************************************************
426 * strarray_exists
428 static int strarray_exists( const struct strarray *array, const char *str )
430 unsigned int i;
432 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
433 return 0;
437 /*******************************************************************
438 * strarray_add_uniq
440 static void strarray_add_uniq( struct strarray *array, const char *str )
442 if (!strarray_exists( array, str )) strarray_add( array, str );
446 /*******************************************************************
447 * strarray_addall_uniq
449 static void strarray_addall_uniq( struct strarray *array, struct strarray added )
451 unsigned int i;
453 for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
457 /*******************************************************************
458 * strarray_get_value
460 * Find a value in a name/value pair string array.
462 static const char *strarray_get_value( const struct strarray *array, const char *name )
464 int pos, res, min = 0, max = array->count / 2 - 1;
466 while (min <= max)
468 pos = (min + max) / 2;
469 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
470 if (res < 0) min = pos + 1;
471 else max = pos - 1;
473 return NULL;
477 /*******************************************************************
478 * strarray_set_value
480 * Define a value in a name/value pair string array.
482 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
484 int i, pos, res, min = 0, max = array->count / 2 - 1;
486 while (min <= max)
488 pos = (min + max) / 2;
489 if (!(res = strcmp( array->str[pos * 2], name )))
491 /* redefining a variable replaces the previous value */
492 array->str[pos * 2 + 1] = value;
493 return;
495 if (res < 0) min = pos + 1;
496 else max = pos - 1;
498 strarray_add( array, NULL );
499 strarray_add( array, NULL );
500 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
501 array->str[min * 2] = name;
502 array->str[min * 2 + 1] = value;
506 /*******************************************************************
507 * strarray_set_qsort
509 static void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
511 if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
515 /*******************************************************************
516 * output_filename
518 static void output_filename( const char *name )
520 if (output_column + strlen(name) + 1 > 100)
522 output( " \\\n" );
523 output( " " );
525 else if (output_column) output( " " );
526 output( "%s", name );
530 /*******************************************************************
531 * output_filenames
533 static void output_filenames( struct strarray array )
535 unsigned int i;
537 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
541 /*******************************************************************
542 * output_rm_filenames
544 static void output_rm_filenames( struct strarray array )
546 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
547 unsigned int i, len;
549 if (!array.count) return;
550 output( "\trm -f" );
551 for (i = len = 0; i < array.count; i++)
553 if (len > max_cmdline)
555 output( "\n" );
556 output( "\trm -f" );
557 len = 0;
559 output_filename( array.str[i] );
560 len += strlen( array.str[i] ) + 1;
562 output( "\n" );
566 /*******************************************************************
567 * get_extension
569 static char *get_extension( char *filename )
571 char *ext = strrchr( filename, '.' );
572 if (ext && strchr( ext, '/' )) ext = NULL;
573 return ext;
577 /*******************************************************************
578 * replace_extension
580 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
582 char *ret;
583 size_t name_len = strlen( name );
584 size_t ext_len = strlen( old_ext );
586 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
587 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
588 memcpy( ret, name, name_len );
589 strcpy( ret + name_len, new_ext );
590 return ret;
594 /*******************************************************************
595 * replace_filename
597 static char *replace_filename( const char *path, const char *name )
599 const char *p;
600 char *ret;
601 size_t len;
603 if (!path) return xstrdup( name );
604 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
605 len = p - path + 1;
606 ret = xmalloc( len + strlen( name ) + 1 );
607 memcpy( ret, path, len );
608 strcpy( ret + len, name );
609 return ret;
613 /*******************************************************************
614 * strarray_replace_extension
616 static struct strarray strarray_replace_extension( const struct strarray *array,
617 const char *old_ext, const char *new_ext )
619 unsigned int i;
620 struct strarray ret;
622 ret.count = ret.size = array->count;
623 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
624 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
625 return ret;
629 /*******************************************************************
630 * replace_substr
632 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
634 size_t pos = start - str;
635 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
636 memcpy( ret, str, pos );
637 strcpy( ret + pos, replace );
638 strcat( ret + pos, start + len );
639 return ret;
643 /*******************************************************************
644 * get_relative_path
646 * Determine where the destination path is located relative to the 'from' path.
648 static char *get_relative_path( const char *from, const char *dest )
650 const char *start;
651 char *ret, *p;
652 unsigned int dotdots = 0;
654 /* a path of "." is equivalent to an empty path */
655 if (!strcmp( from, "." )) from = "";
657 for (;;)
659 while (*from == '/') from++;
660 while (*dest == '/') dest++;
661 start = dest; /* save start of next path element */
662 if (!*from) break;
664 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
665 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
667 /* count remaining elements in 'from' */
670 dotdots++;
671 while (*from && *from != '/') from++;
672 while (*from == '/') from++;
674 while (*from);
675 break;
678 if (!start[0] && !dotdots) return NULL; /* empty path */
680 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
681 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
683 if (start[0]) strcpy( p, start );
684 else p[-1] = 0; /* remove trailing slash */
685 return ret;
689 /*******************************************************************
690 * concat_paths
692 static char *concat_paths( const char *base, const char *path )
694 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
695 if (!path || !path[0]) return xstrdup( base );
696 if (path[0] == '/') return xstrdup( path );
697 return strmake( "%s/%s", base, path );
701 /*******************************************************************
702 * base_dir_path
704 static char *base_dir_path( const struct makefile *make, const char *path )
706 return concat_paths( make->base_dir, path );
710 /*******************************************************************
711 * obj_dir_path
713 static char *obj_dir_path( const struct makefile *make, const char *path )
715 return concat_paths( make->obj_dir, path );
719 /*******************************************************************
720 * src_dir_path
722 static char *src_dir_path( const struct makefile *make, const char *path )
724 if (make->src_dir) return concat_paths( make->src_dir, path );
725 return obj_dir_path( make, path );
729 /*******************************************************************
730 * top_obj_dir_path
732 static char *top_obj_dir_path( const struct makefile *make, const char *path )
734 return concat_paths( make->top_obj_dir, path );
738 /*******************************************************************
739 * top_src_dir_path
741 static char *top_src_dir_path( const struct makefile *make, const char *path )
743 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
744 return top_obj_dir_path( make, path );
748 /*******************************************************************
749 * root_dir_path
751 static char *root_dir_path( const char *path )
753 return concat_paths( root_src_dir, path );
757 /*******************************************************************
758 * tools_dir_path
760 static char *tools_dir_path( const struct makefile *make, const char *path )
762 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
763 return top_obj_dir_path( make, strmake( "tools/%s", path ));
767 /*******************************************************************
768 * tools_path
770 static char *tools_path( const struct makefile *make, const char *name )
772 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
776 /*******************************************************************
777 * get_line
779 static char *get_line( FILE *file )
781 static char *buffer;
782 static size_t size;
784 if (!size)
786 size = 1024;
787 buffer = xmalloc( size );
789 if (!fgets( buffer, size, file )) return NULL;
790 input_line++;
792 for (;;)
794 char *p = buffer + strlen(buffer);
795 /* if line is larger than buffer, resize buffer */
796 while (p == buffer + size - 1 && p[-1] != '\n')
798 buffer = xrealloc( buffer, size * 2 );
799 if (!fgets( buffer + size - 1, size + 1, file )) break;
800 p = buffer + strlen(buffer);
801 size *= 2;
803 if (p > buffer && p[-1] == '\n')
805 *(--p) = 0;
806 if (p > buffer && p[-1] == '\r') *(--p) = 0;
807 if (p > buffer && p[-1] == '\\')
809 *(--p) = 0;
810 /* line ends in backslash, read continuation line */
811 if (!fgets( p, size - (p - buffer), file )) return buffer;
812 input_line++;
813 continue;
816 return buffer;
821 /*******************************************************************
822 * hash_filename
824 static unsigned int hash_filename( const char *name )
826 /* FNV-1 hash */
827 unsigned int ret = 2166136261u;
828 while (*name) ret = (ret * 16777619) ^ *name++;
829 return ret % HASH_SIZE;
833 /*******************************************************************
834 * add_file
836 static struct file *add_file( const char *name )
838 struct file *file = xmalloc( sizeof(*file) );
839 memset( file, 0, sizeof(*file) );
840 file->name = xstrdup( name );
841 return file;
845 /*******************************************************************
846 * add_dependency
848 static void add_dependency( struct file *file, const char *name, enum incl_type type )
850 /* enforce some rules for the Wine tree */
852 if (!memcmp( name, "../", 3 ))
853 fatal_error( "#include directive with relative path not allowed\n" );
855 if (!strcmp( name, "config.h" ))
857 if (strendswith( file->name, ".h" ))
858 fatal_error( "config.h must not be included by a header file\n" );
859 if (file->deps_count)
860 fatal_error( "config.h must be included before anything else\n" );
862 else if (!strcmp( name, "wine/port.h" ))
864 if (strendswith( file->name, ".h" ))
865 fatal_error( "wine/port.h must not be included by a header file\n" );
866 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
867 if (file->deps_count > 1)
868 fatal_error( "wine/port.h must be included before everything except config.h\n" );
869 if (strcmp( file->deps[0].name, "config.h" ))
870 fatal_error( "config.h must be included before wine/port.h\n" );
873 if (file->deps_count >= file->deps_size)
875 file->deps_size *= 2;
876 if (file->deps_size < 16) file->deps_size = 16;
877 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
879 file->deps[file->deps_count].line = input_line;
880 file->deps[file->deps_count].type = type;
881 file->deps[file->deps_count].name = xstrdup( name );
882 file->deps_count++;
886 /*******************************************************************
887 * find_src_file
889 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
891 struct incl_file *file;
893 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
894 if (!strcmp( name, file->name )) return file;
895 return NULL;
898 /*******************************************************************
899 * find_include_file
901 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
903 struct incl_file *file;
905 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
906 if (!strcmp( name, file->name )) return file;
907 return NULL;
910 /*******************************************************************
911 * add_include
913 * Add an include file if it doesn't already exists.
915 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
916 const char *name, int line, enum incl_type type )
918 struct incl_file *include;
920 if (parent->files_count >= parent->files_size)
922 parent->files_size *= 2;
923 if (parent->files_size < 16) parent->files_size = 16;
924 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
927 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
928 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
929 goto found;
931 include = xmalloc( sizeof(*include) );
932 memset( include, 0, sizeof(*include) );
933 include->name = xstrdup(name);
934 include->included_by = parent;
935 include->included_line = line;
936 include->type = type;
937 include->use_msvcrt = parent->use_msvcrt;
938 list_add_tail( &make->includes, &include->entry );
939 found:
940 parent->files[parent->files_count++] = include;
941 return include;
945 /*******************************************************************
946 * add_generated_source
948 * Add a generated source file to the list.
950 static struct incl_file *add_generated_source( struct makefile *make,
951 const char *name, const char *filename )
953 struct incl_file *file;
955 if ((file = find_src_file( make, name ))) return file; /* we already have it */
956 file = xmalloc( sizeof(*file) );
957 memset( file, 0, sizeof(*file) );
958 file->file = add_file( name );
959 file->name = xstrdup( name );
960 file->filename = obj_dir_path( make, filename ? filename : name );
961 file->file->flags = FLAG_GENERATED;
962 file->use_msvcrt = make->use_msvcrt;
963 list_add_tail( &make->sources, &file->entry );
964 return file;
968 /*******************************************************************
969 * parse_include_directive
971 static void parse_include_directive( struct file *source, char *str )
973 char quote, *include, *p = str;
975 while (*p && isspace(*p)) p++;
976 if (*p != '\"' && *p != '<' ) return;
977 quote = *p++;
978 if (quote == '<') quote = '>';
979 include = p;
980 while (*p && (*p != quote)) p++;
981 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
982 *p = 0;
983 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
987 /*******************************************************************
988 * parse_pragma_directive
990 static void parse_pragma_directive( struct file *source, char *str )
992 char *flag, *p = str;
994 if (!isspace( *p )) return;
995 while (*p && isspace(*p)) p++;
996 p = strtok( p, " \t" );
997 if (strcmp( p, "makedep" )) return;
999 while ((flag = strtok( NULL, " \t" )))
1001 if (!strcmp( flag, "depend" ))
1003 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
1004 return;
1006 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
1008 if (strendswith( source->name, ".idl" ))
1010 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
1011 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
1012 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
1013 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
1014 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
1015 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
1016 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
1017 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
1019 else if (strendswith( source->name, ".rc" ))
1021 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
1023 else if (strendswith( source->name, ".sfd" ))
1025 if (!strcmp( flag, "font" ))
1027 struct strarray *array = source->args;
1029 if (!array)
1031 source->args = array = xmalloc( sizeof(*array) );
1032 *array = empty_strarray;
1033 source->flags |= FLAG_SFD_FONTS;
1035 strarray_add( array, xstrdup( strtok( NULL, "" )));
1036 return;
1039 else
1041 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
1042 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
1048 /*******************************************************************
1049 * parse_cpp_directive
1051 static void parse_cpp_directive( struct file *source, char *str )
1053 while (*str && isspace(*str)) str++;
1054 if (*str++ != '#') return;
1055 while (*str && isspace(*str)) str++;
1057 if (!strncmp( str, "include", 7 ))
1058 parse_include_directive( source, str + 7 );
1059 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
1060 parse_include_directive( source, str + 6 );
1061 else if (!strncmp( str, "pragma", 6 ))
1062 parse_pragma_directive( source, str + 6 );
1066 /*******************************************************************
1067 * parse_idl_file
1069 static void parse_idl_file( struct file *source, FILE *file )
1071 char *buffer, *include;
1073 input_line = 0;
1075 while ((buffer = get_line( file )))
1077 char quote;
1078 char *p = buffer;
1079 while (*p && isspace(*p)) p++;
1081 if (!strncmp( p, "importlib", 9 ))
1083 p += 9;
1084 while (*p && isspace(*p)) p++;
1085 if (*p++ != '(') continue;
1086 while (*p && isspace(*p)) p++;
1087 if (*p++ != '"') continue;
1088 include = p;
1089 while (*p && (*p != '"')) p++;
1090 if (!*p) fatal_error( "malformed importlib directive\n" );
1091 *p = 0;
1092 add_dependency( source, include, INCL_IMPORTLIB );
1093 continue;
1096 if (!strncmp( p, "import", 6 ))
1098 p += 6;
1099 while (*p && isspace(*p)) p++;
1100 if (*p != '"') continue;
1101 include = ++p;
1102 while (*p && (*p != '"')) p++;
1103 if (!*p) fatal_error( "malformed import directive\n" );
1104 *p = 0;
1105 add_dependency( source, include, INCL_IMPORT );
1106 continue;
1109 /* check for #include inside cpp_quote */
1110 if (!strncmp( p, "cpp_quote", 9 ))
1112 p += 9;
1113 while (*p && isspace(*p)) p++;
1114 if (*p++ != '(') continue;
1115 while (*p && isspace(*p)) p++;
1116 if (*p++ != '"') continue;
1117 if (*p++ != '#') continue;
1118 while (*p && isspace(*p)) p++;
1119 if (strncmp( p, "include", 7 )) continue;
1120 p += 7;
1121 while (*p && isspace(*p)) p++;
1122 if (*p == '\\' && p[1] == '"')
1124 p += 2;
1125 quote = '"';
1127 else
1129 if (*p++ != '<' ) continue;
1130 quote = '>';
1132 include = p;
1133 while (*p && (*p != quote)) p++;
1134 if (!*p || (quote == '"' && p[-1] != '\\'))
1135 fatal_error( "malformed #include directive inside cpp_quote\n" );
1136 if (quote == '"') p--; /* remove backslash */
1137 *p = 0;
1138 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1139 continue;
1142 parse_cpp_directive( source, p );
1146 /*******************************************************************
1147 * parse_c_file
1149 static void parse_c_file( struct file *source, FILE *file )
1151 char *buffer;
1153 input_line = 0;
1154 while ((buffer = get_line( file )))
1156 parse_cpp_directive( source, buffer );
1161 /*******************************************************************
1162 * parse_rc_file
1164 static void parse_rc_file( struct file *source, FILE *file )
1166 char *buffer, *include;
1168 input_line = 0;
1169 while ((buffer = get_line( file )))
1171 char quote;
1172 char *p = buffer;
1173 while (*p && isspace(*p)) p++;
1175 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1177 p += 2;
1178 while (*p && isspace(*p)) p++;
1179 if (strncmp( p, "@makedep:", 9 )) continue;
1180 p += 9;
1181 while (*p && isspace(*p)) p++;
1182 quote = '"';
1183 if (*p == quote)
1185 include = ++p;
1186 while (*p && *p != quote) p++;
1188 else
1190 include = p;
1191 while (*p && !isspace(*p) && *p != '*') p++;
1193 if (!*p)
1194 fatal_error( "malformed makedep comment\n" );
1195 *p = 0;
1196 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1197 continue;
1200 parse_cpp_directive( source, buffer );
1205 /*******************************************************************
1206 * parse_in_file
1208 static void parse_in_file( struct file *source, FILE *file )
1210 char *p, *buffer;
1212 /* make sure it gets rebuilt when the version changes */
1213 add_dependency( source, "config.h", INCL_SYSTEM );
1215 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1217 input_line = 0;
1218 while ((buffer = get_line( file )))
1220 if (strncmp( buffer, ".TH", 3 )) continue;
1221 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1222 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1223 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1224 source->args = xstrdup( p );
1225 return;
1230 /*******************************************************************
1231 * parse_sfd_file
1233 static void parse_sfd_file( struct file *source, FILE *file )
1235 char *p, *eol, *buffer;
1237 input_line = 0;
1238 while ((buffer = get_line( file )))
1240 if (strncmp( buffer, "UComments:", 10 )) continue;
1241 p = buffer + 10;
1242 while (*p == ' ') p++;
1243 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1245 p++;
1246 buffer[strlen(buffer) - 1] = 0;
1248 while ((eol = strstr( p, "+AAoA" )))
1250 *eol = 0;
1251 while (*p && isspace(*p)) p++;
1252 if (*p++ == '#')
1254 while (*p && isspace(*p)) p++;
1255 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1257 p = eol + 5;
1259 while (*p && isspace(*p)) p++;
1260 if (*p++ != '#') return;
1261 while (*p && isspace(*p)) p++;
1262 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1263 return;
1268 static const struct
1270 const char *ext;
1271 void (*parse)( struct file *file, FILE *f );
1272 } parse_functions[] =
1274 { ".c", parse_c_file },
1275 { ".h", parse_c_file },
1276 { ".inl", parse_c_file },
1277 { ".l", parse_c_file },
1278 { ".m", parse_c_file },
1279 { ".rh", parse_c_file },
1280 { ".x", parse_c_file },
1281 { ".y", parse_c_file },
1282 { ".idl", parse_idl_file },
1283 { ".rc", parse_rc_file },
1284 { ".in", parse_in_file },
1285 { ".sfd", parse_sfd_file }
1288 /*******************************************************************
1289 * load_file
1291 static struct file *load_file( const char *name )
1293 struct file *file;
1294 FILE *f;
1295 unsigned int i, hash = hash_filename( name );
1297 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1298 if (!strcmp( name, file->name )) return file;
1300 if (!(f = fopen( name, "r" ))) return NULL;
1302 file = add_file( name );
1303 list_add_tail( &files[hash], &file->entry );
1304 input_file_name = file->name;
1305 input_line = 0;
1307 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1309 if (!strendswith( name, parse_functions[i].ext )) continue;
1310 parse_functions[i].parse( file, f );
1311 break;
1314 fclose( f );
1315 input_file_name = NULL;
1317 return file;
1321 /*******************************************************************
1322 * open_include_path_file
1324 * Open a file from a directory on the include path.
1326 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1327 const char *name, char **filename )
1329 char *src_path = base_dir_path( make, concat_paths( dir, name ));
1330 struct file *ret = load_file( src_path );
1332 if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
1333 return ret;
1337 /*******************************************************************
1338 * open_file_same_dir
1340 * Open a file in the same directory as the parent.
1342 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1344 char *src_path = replace_filename( parent->file->name, name );
1345 struct file *ret = load_file( src_path );
1347 if (ret) *filename = replace_filename( parent->filename, name );
1348 free( src_path );
1349 return ret;
1353 /*******************************************************************
1354 * open_local_file
1356 * Open a file in the source directory of the makefile.
1358 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1360 char *src_path = root_dir_path( base_dir_path( make, path ));
1361 struct file *ret = load_file( src_path );
1363 /* if not found, try parent dir */
1364 if (!ret && make->parent_dir)
1366 free( src_path );
1367 path = strmake( "%s/%s", make->parent_dir, path );
1368 src_path = root_dir_path( base_dir_path( make, path ));
1369 ret = load_file( src_path );
1370 if (ret) ret->flags |= FLAG_PARENTDIR;
1373 if (ret) *filename = src_dir_path( make, path );
1374 free( src_path );
1375 return ret;
1379 /*******************************************************************
1380 * open_global_file
1382 * Open a file in the top-level source directory.
1384 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1386 char *src_path = root_dir_path( path );
1387 struct file *ret = load_file( src_path );
1389 if (ret) *filename = top_src_dir_path( make, path );
1390 free( src_path );
1391 return ret;
1395 /*******************************************************************
1396 * open_global_header
1398 * Open a file in the global include source directory.
1400 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1402 return open_global_file( make, strmake( "include/%s", path ), filename );
1406 /*******************************************************************
1407 * open_src_file
1409 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1411 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1413 if (!file) fatal_perror( "open %s", pFile->name );
1414 return file;
1418 /*******************************************************************
1419 * open_include_file
1421 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1423 struct file *file = NULL;
1424 char *filename;
1425 unsigned int i, len;
1427 errno = ENOENT;
1429 /* check for generated bison header */
1431 if (strendswith( pFile->name, ".tab.h" ) &&
1432 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1434 pFile->sourcename = filename;
1435 pFile->filename = obj_dir_path( make, pFile->name );
1436 return file;
1439 /* check for corresponding idl file in source dir */
1441 if (strendswith( pFile->name, ".h" ) &&
1442 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1444 pFile->sourcename = filename;
1445 pFile->filename = obj_dir_path( make, pFile->name );
1446 return file;
1449 /* check for corresponding tlb file in source dir */
1451 if (strendswith( pFile->name, ".tlb" ) &&
1452 (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1454 pFile->sourcename = filename;
1455 pFile->filename = obj_dir_path( make, pFile->name );
1456 return file;
1459 /* check for extra targets */
1460 if (strarray_exists( &make->extra_targets, pFile->name ))
1462 pFile->sourcename = filename;
1463 pFile->filename = obj_dir_path( make, pFile->name );
1464 return NULL;
1467 /* now try in source dir */
1468 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1470 /* check for corresponding idl file in global includes */
1472 if (strendswith( pFile->name, ".h" ) &&
1473 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1475 pFile->sourcename = filename;
1476 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1477 return file;
1480 /* check for corresponding .in file in global includes (for config.h.in) */
1482 if (strendswith( pFile->name, ".h" ) &&
1483 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1485 pFile->sourcename = filename;
1486 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1487 return file;
1490 /* check for corresponding .x file in global includes */
1492 if (strendswith( pFile->name, "tmpl.h" ) &&
1493 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1495 pFile->sourcename = filename;
1496 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1497 return file;
1500 /* check for corresponding .tlb file in global includes */
1502 if (strendswith( pFile->name, ".tlb" ) &&
1503 (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1505 pFile->sourcename = filename;
1506 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1507 return file;
1510 /* check in global includes source dir */
1512 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1514 /* check in global msvcrt includes */
1515 if (pFile->use_msvcrt &&
1516 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1517 return file;
1519 /* now search in include paths */
1520 for (i = 0; i < make->include_paths.count; i++)
1522 const char *dir = make->include_paths.str[i];
1523 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1525 if (prefix)
1527 len = strlen( prefix );
1528 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1530 while (dir[len] == '/') len++;
1531 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1532 if (file) return file;
1534 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1536 if (*dir != '/')
1538 if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1539 return file;
1543 if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt)
1545 if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
1546 fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
1547 pFile->included_by->file->name, pFile->included_line, pFile->name );
1548 exit(1);
1551 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1553 /* try in src file directory */
1554 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1556 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1557 perror( pFile->name );
1558 pFile = pFile->included_by;
1559 while (pFile && pFile->included_by)
1561 const char *parent = pFile->included_by->sourcename;
1562 if (!parent) parent = pFile->included_by->file->name;
1563 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1564 parent, pFile->included_line, pFile->name );
1565 pFile = pFile->included_by;
1567 exit(1);
1571 /*******************************************************************
1572 * add_all_includes
1574 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1576 unsigned int i;
1578 parent->files_count = 0;
1579 parent->files_size = file->deps_count;
1580 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1581 for (i = 0; i < file->deps_count; i++)
1583 switch (file->deps[i].type)
1585 case INCL_NORMAL:
1586 case INCL_IMPORT:
1587 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1588 break;
1589 case INCL_IMPORTLIB:
1590 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1591 break;
1592 case INCL_SYSTEM:
1593 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1594 break;
1595 case INCL_CPP_QUOTE:
1596 case INCL_CPP_QUOTE_SYSTEM:
1597 break;
1603 /*******************************************************************
1604 * parse_file
1606 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1608 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1610 if (!file) return;
1612 source->file = file;
1613 source->files_count = 0;
1614 source->files_size = file->deps_count;
1615 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1616 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1618 if (source->sourcename)
1620 if (strendswith( source->sourcename, ".idl" ))
1622 unsigned int i;
1624 if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
1626 /* generated .h file always includes these */
1627 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1628 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1629 for (i = 0; i < file->deps_count; i++)
1631 switch (file->deps[i].type)
1633 case INCL_IMPORT:
1634 if (strendswith( file->deps[i].name, ".idl" ))
1635 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1636 file->deps[i].line, INCL_NORMAL );
1637 else
1638 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1639 break;
1640 case INCL_CPP_QUOTE:
1641 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1642 break;
1643 case INCL_CPP_QUOTE_SYSTEM:
1644 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1645 break;
1646 case INCL_NORMAL:
1647 case INCL_SYSTEM:
1648 case INCL_IMPORTLIB:
1649 break;
1652 return;
1654 if (strendswith( source->sourcename, ".y" ))
1655 return; /* generated .tab.h doesn't include anything */
1658 add_all_includes( make, source, file );
1662 /*******************************************************************
1663 * add_src_file
1665 * Add a source file to the list.
1667 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1669 struct incl_file *file;
1671 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1672 file = xmalloc( sizeof(*file) );
1673 memset( file, 0, sizeof(*file) );
1674 file->name = xstrdup(name);
1675 file->use_msvcrt = make->use_msvcrt;
1676 list_add_tail( &make->sources, &file->entry );
1677 parse_file( make, file, 1 );
1678 return file;
1682 /*******************************************************************
1683 * open_input_makefile
1685 static FILE *open_input_makefile( const struct makefile *make )
1687 FILE *ret;
1689 if (make->base_dir)
1690 input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
1691 else
1692 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1694 input_line = 0;
1695 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1696 return ret;
1700 /*******************************************************************
1701 * get_make_variable
1703 static const char *get_make_variable( const struct makefile *make, const char *name )
1705 const char *ret;
1707 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1708 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1709 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1710 return NULL;
1714 /*******************************************************************
1715 * get_expanded_make_variable
1717 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1719 const char *var;
1720 char *p, *end, *expand, *tmp;
1722 var = get_make_variable( make, name );
1723 if (!var) return NULL;
1725 p = expand = xstrdup( var );
1726 while ((p = strchr( p, '$' )))
1728 if (p[1] == '(')
1730 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1731 *end++ = 0;
1732 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1733 var = get_make_variable( make, p + 2 );
1734 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1735 /* switch to the new string */
1736 p = tmp + (p - expand);
1737 free( expand );
1738 expand = tmp;
1740 else if (p[1] == '{') /* don't expand ${} variables */
1742 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1743 p = end + 1;
1745 else if (p[1] == '$')
1747 p += 2;
1749 else fatal_error( "syntax error in '%s'\n", expand );
1752 /* consider empty variables undefined */
1753 p = expand;
1754 while (*p && isspace(*p)) p++;
1755 if (*p) return expand;
1756 free( expand );
1757 return NULL;
1761 /*******************************************************************
1762 * get_expanded_make_var_array
1764 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1766 struct strarray ret = empty_strarray;
1767 char *value, *token;
1769 if ((value = get_expanded_make_variable( make, name )))
1770 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1771 strarray_add( &ret, token );
1772 return ret;
1776 /*******************************************************************
1777 * get_expanded_file_local_var
1779 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1780 const char *name )
1782 char *p, *var = strmake( "%s_%s", file, name );
1784 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1785 return get_expanded_make_var_array( make, var );
1789 /*******************************************************************
1790 * set_make_variable
1792 static int set_make_variable( struct strarray *array, const char *assignment )
1794 char *p, *name;
1796 p = name = xstrdup( assignment );
1797 while (isalnum(*p) || *p == '_') p++;
1798 if (name == p) return 0; /* not a variable */
1799 if (isspace(*p))
1801 *p++ = 0;
1802 while (isspace(*p)) p++;
1804 if (*p != '=') return 0; /* not an assignment */
1805 *p++ = 0;
1806 while (isspace(*p)) p++;
1808 strarray_set_value( array, name, p );
1809 return 1;
1813 /*******************************************************************
1814 * parse_makefile
1816 static struct makefile *parse_makefile( const char *path )
1818 char *buffer;
1819 FILE *file;
1820 struct makefile *make = xmalloc( sizeof(*make) );
1822 memset( make, 0, sizeof(*make) );
1823 if (path)
1825 make->top_obj_dir = get_relative_path( path, "" );
1826 make->base_dir = path;
1827 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1830 file = open_input_makefile( make );
1831 while ((buffer = get_line( file )))
1833 if (!strncmp( buffer, separator, strlen(separator) )) break;
1834 if (*buffer == '\t') continue; /* command */
1835 while (isspace( *buffer )) buffer++;
1836 if (*buffer == '#') continue; /* comment */
1837 set_make_variable( &make->vars, buffer );
1839 fclose( file );
1840 input_file_name = NULL;
1841 return make;
1845 /*******************************************************************
1846 * add_generated_sources
1848 static void add_generated_sources( struct makefile *make )
1850 unsigned int i;
1851 struct incl_file *source, *next, *file;
1852 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1854 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1856 if (source->file->flags & FLAG_IDL_CLIENT)
1858 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1859 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1860 add_all_includes( make, file, file->file );
1862 if (source->file->flags & FLAG_IDL_SERVER)
1864 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1865 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1866 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1867 add_all_includes( make, file, file->file );
1869 if (source->file->flags & FLAG_IDL_IDENT)
1871 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1872 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1873 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1874 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1875 add_all_includes( make, file, file->file );
1877 if (source->file->flags & FLAG_IDL_PROXY)
1879 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1880 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1881 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1882 add_all_includes( make, file, file->file );
1883 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1884 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1885 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1886 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1887 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1888 add_all_includes( make, file, file->file );
1890 if (source->file->flags & FLAG_IDL_TYPELIB)
1892 add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
1894 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1896 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1898 if (source->file->flags & FLAG_IDL_REGISTER)
1900 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1902 if (source->file->flags & FLAG_IDL_HEADER)
1904 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1906 if (!source->file->flags && strendswith( source->name, ".idl" ))
1908 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1910 if (strendswith( source->name, ".x" ))
1912 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1914 if (strendswith( source->name, ".y" ))
1916 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1917 /* steal the includes list from the source file */
1918 file->files_count = source->files_count;
1919 file->files_size = source->files_size;
1920 file->files = source->files;
1921 source->files_count = source->files_size = 0;
1922 source->files = NULL;
1924 if (strendswith( source->name, ".l" ))
1926 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1927 /* steal the includes list from the source file */
1928 file->files_count = source->files_count;
1929 file->files_size = source->files_size;
1930 file->files = source->files;
1931 source->files_count = source->files_size = 0;
1932 source->files = NULL;
1934 if (source->file->flags & FLAG_C_IMPLIB)
1936 if (!make->staticimplib && make->importlib && *dll_ext)
1937 make->staticimplib = strmake( "lib%s.a", make->importlib );
1939 if (strendswith( source->name, ".po" ))
1941 if (!make->disabled)
1942 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1945 if (make->testdll)
1947 file = add_generated_source( make, "testlist.o", "testlist.c" );
1948 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1949 add_all_includes( make, file, file->file );
1951 for (i = 0; i < objs.count; i++)
1953 /* default to .c for unknown extra object files */
1954 if (strendswith( objs.str[i], ".o" ))
1955 add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1956 else if (strendswith( objs.str[i], ".res" ))
1957 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1958 else
1959 add_generated_source( make, objs.str[i], NULL );
1964 /*******************************************************************
1965 * create_dir
1967 static void create_dir( const char *dir )
1969 char *p, *path;
1971 p = path = xstrdup( dir );
1972 while ((p = strchr( p, '/' )))
1974 *p = 0;
1975 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1976 *p++ = '/';
1977 while (*p == '/') p++;
1979 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1980 free( path );
1984 /*******************************************************************
1985 * create_file_directories
1987 * Create the base directories of all the files.
1989 static void create_file_directories( const struct makefile *make, struct strarray files )
1991 struct strarray subdirs = empty_strarray;
1992 unsigned int i;
1993 char *dir;
1995 for (i = 0; i < files.count; i++)
1997 if (!strchr( files.str[i], '/' )) continue;
1998 dir = base_dir_path( make, files.str[i] );
1999 *strrchr( dir, '/' ) = 0;
2000 strarray_add_uniq( &subdirs, dir );
2003 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2007 /*******************************************************************
2008 * output_filenames_obj_dir
2010 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
2012 unsigned int i;
2014 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
2018 /*******************************************************************
2019 * get_dependencies
2021 static void get_dependencies( struct incl_file *file, struct incl_file *source )
2023 unsigned int i;
2025 if (!file->filename) return;
2027 if (file != source)
2029 if (file->owner == source) return; /* already processed */
2030 if (file->type == INCL_IMPORTLIB &&
2031 !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
2032 return; /* library is imported only when building a typelib */
2033 file->owner = source;
2034 strarray_add( &source->dependencies, file->filename );
2036 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
2040 /*******************************************************************
2041 * get_local_dependencies
2043 * Get the local dependencies of a given target.
2045 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
2046 struct strarray targets )
2048 unsigned int i;
2049 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2051 for (i = 0; i < deps.count; i++)
2053 if (strarray_exists( &targets, deps.str[i] ))
2054 deps.str[i] = obj_dir_path( make, deps.str[i] );
2055 else
2056 deps.str[i] = src_dir_path( make, deps.str[i] );
2058 return deps;
2062 /*******************************************************************
2063 * get_static_lib
2065 * Check if makefile builds the named static library and return the full lib path.
2067 static const char *get_static_lib( const struct makefile *make, const char *name )
2069 if (!make->staticlib) return NULL;
2070 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2071 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2072 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2073 return base_dir_path( make, make->staticlib );
2077 /*******************************************************************
2078 * get_parent_makefile
2080 static struct makefile *get_parent_makefile( struct makefile *make )
2082 char *dir, *p;
2083 int i;
2085 if (!make->base_dir) return NULL;
2086 dir = xstrdup( make->base_dir );
2087 if (!(p = strrchr( dir, '/' ))) return NULL;
2088 *p = 0;
2089 for (i = 0; i < top_makefile->subdirs.count; i++)
2090 if (!strcmp( top_makefile->submakes[i]->base_dir, dir )) return top_makefile->submakes[i];
2091 return NULL;
2095 /*******************************************************************
2096 * needs_cross_lib
2098 static int needs_cross_lib( const struct makefile *make )
2100 if (!crosstarget) return 0;
2101 if (make->importlib) return strarray_exists( &cross_import_libs, make->importlib );
2102 if (make->staticlib)
2104 const char *name = replace_extension( make->staticlib, ".a", "" );
2105 if (!strncmp( name, "lib", 3 )) name += 3;
2106 return strarray_exists( &cross_import_libs, name );
2108 return 0;
2112 /*******************************************************************
2113 * needs_delay_lib
2115 static int needs_delay_lib( const struct makefile *make )
2117 if (*dll_ext && !crosstarget) return 0;
2118 if (!make->importlib) return 0;
2119 return strarray_exists( &delay_import_libs, make->importlib );
2123 /*******************************************************************
2124 * add_default_libraries
2126 static struct strarray add_default_libraries( const struct makefile *make, struct strarray *deps )
2128 struct strarray ret = empty_strarray;
2129 struct strarray all_libs = empty_strarray;
2130 unsigned int i, j;
2132 if (!make->use_msvcrt) strarray_add( &all_libs, "-lwine_port" );
2133 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2134 strarray_addall( &all_libs, libs );
2136 for (i = 0; i < all_libs.count; i++)
2138 const char *lib = NULL;
2140 if (!strncmp( all_libs.str[i], "-l", 2 ))
2142 const char *name = all_libs.str[i] + 2;
2144 for (j = 0; j < top_makefile->subdirs.count; j++)
2146 const struct makefile *submake = top_makefile->submakes[j];
2148 if ((lib = get_static_lib( submake, name ))) break;
2152 if (lib)
2154 lib = top_obj_dir_path( make, lib );
2155 strarray_add( deps, lib );
2156 strarray_add( &ret, lib );
2158 else strarray_add( &ret, all_libs.str[i] );
2160 return ret;
2164 /*******************************************************************
2165 * add_import_libs
2167 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2168 struct strarray imports, int delay )
2170 struct strarray ret = empty_strarray;
2171 unsigned int i, j;
2173 for (i = 0; i < imports.count; i++)
2175 const char *name = imports.str[i];
2176 const char *lib = NULL;
2178 for (j = 0; j < top_makefile->subdirs.count; j++)
2180 const struct makefile *submake = top_makefile->submakes[j];
2182 if (submake->importlib && !strcmp( submake->importlib, name ))
2184 if (make->is_cross || !*dll_ext || submake->staticimplib)
2185 lib = base_dir_path( submake, strmake( "lib%s.a", name ));
2186 else
2187 strarray_add( deps, top_obj_dir_path( make,
2188 strmake( "%s/lib%s.def", submake->base_dir, name )));
2189 break;
2192 if ((lib = get_static_lib( submake, name ))) break;
2195 if (lib)
2197 if (delay) lib = replace_extension( lib, ".a", ".delay.a" );
2198 else if (make->is_cross) lib = replace_extension( lib, ".a", ".cross.a" );
2199 lib = top_obj_dir_path( make, lib );
2200 strarray_add( deps, lib );
2201 strarray_add( &ret, lib );
2203 else strarray_add( &ret, strmake( "-l%s", name ));
2205 return ret;
2209 /*******************************************************************
2210 * get_default_imports
2212 static struct strarray get_default_imports( const struct makefile *make )
2214 struct strarray ret = empty_strarray;
2216 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
2217 if (make->use_msvcrt) strarray_add( &ret, "msvcrt" );
2218 strarray_add( &ret, "winecrt0" );
2219 if (make->is_win16) strarray_add( &ret, "kernel" );
2220 strarray_add( &ret, "kernel32" );
2221 strarray_add( &ret, "ntdll" );
2222 return ret;
2226 /*******************************************************************
2227 * add_install_rule
2229 static void add_install_rule( struct makefile *make, const char *target,
2230 const char *file, const char *dest )
2232 if (strarray_exists( &make->install_lib, target ) ||
2233 strarray_exists( &top_install_lib, make->base_dir ) ||
2234 strarray_exists( &top_install_lib, base_dir_path( make, target )))
2236 strarray_add( &make->install_rules[INSTALL_LIB], file );
2237 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2239 else if (strarray_exists( &make->install_dev, target ) ||
2240 strarray_exists( &top_install_dev, make->base_dir ) ||
2241 strarray_exists( &top_install_dev, base_dir_path( make, target )))
2243 strarray_add( &make->install_rules[INSTALL_DEV], file );
2244 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2249 /*******************************************************************
2250 * get_include_install_path
2252 * Determine the installation path for a given include file.
2254 static const char *get_include_install_path( const char *name )
2256 if (!strncmp( name, "wine/", 5 )) return name + 5;
2257 if (!strncmp( name, "msvcrt/", 7 )) return name;
2258 return strmake( "windows/%s", name );
2262 /*******************************************************************
2263 * get_shared_library_name
2265 * Determine possible names for a shared library with a version number.
2267 static struct strarray get_shared_lib_names( const char *libname )
2269 struct strarray ret = empty_strarray;
2270 const char *ext, *p;
2271 char *name, *first, *second;
2272 size_t len = 0;
2274 strarray_add( &ret, libname );
2276 for (p = libname; (p = strchr( p, '.' )); p++)
2277 if ((len = strspn( p + 1, "0123456789." ))) break;
2279 if (!len) return ret;
2280 ext = p + 1 + len;
2281 if (*ext && ext[-1] == '.') ext--;
2283 /* keep only the first group of digits */
2284 name = xstrdup( libname );
2285 first = name + (p - libname);
2286 if ((second = strchr( first + 1, '.' )))
2288 strcpy( second, ext );
2289 strarray_add( &ret, xstrdup( name ));
2291 /* now remove all digits */
2292 strcpy( first, ext );
2293 strarray_add( &ret, name );
2294 return ret;
2298 /*******************************************************************
2299 * get_source_defines
2301 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2302 const char *obj )
2304 unsigned int i;
2305 struct strarray ret = empty_strarray;
2307 strarray_addall( &ret, make->include_args );
2308 if (source->use_msvcrt)
2309 strarray_add( &ret, strmake( "-I%s", top_src_dir_path( make, "include/msvcrt" )));
2310 for (i = 0; i < make->include_paths.count; i++)
2311 strarray_add( &ret, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
2312 strarray_addall( &ret, make->define_args );
2313 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2314 return ret;
2318 /*******************************************************************
2319 * output_winegcc_command
2321 static void output_winegcc_command( struct makefile *make )
2323 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2324 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2325 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2326 if (make->is_cross)
2328 output_filename( "-b" );
2329 output_filename( crosstarget );
2330 output_filename( "--lib-suffix=.cross.a" );
2332 else
2334 output_filenames( target_flags );
2335 output_filenames( lddll_flags );
2340 /*******************************************************************
2341 * output_symlink_rule
2343 * Output a rule to create a symlink.
2345 static void output_symlink_rule( const char *src_name, const char *link_name )
2347 const char *name;
2349 output( "\trm -f %s && ", link_name );
2351 /* dest path with a directory needs special handling if ln -s isn't supported */
2352 if (strcmp( ln_s, "ln -s" ) && ((name = strrchr( link_name, '/' ))))
2354 char *dir = xstrdup( link_name );
2355 dir[name - link_name] = 0;
2356 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2357 free( dir );
2359 else
2361 output( "%s %s %s\n", ln_s, src_name, link_name );
2366 /*******************************************************************
2367 * output_install_commands
2369 static void output_install_commands( struct makefile *make, const struct makefile *submake,
2370 struct strarray files )
2372 unsigned int i;
2373 char *install_sh = top_src_dir_path( make, "tools/install-sh" );
2375 for (i = 0; i < files.count; i += 2)
2377 const char *file = files.str[i];
2378 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2380 if (submake) file = base_dir_path( submake, file );
2381 switch (*files.str[i + 1])
2383 case 'c': /* cross-compiled program */
2384 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2385 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2386 break;
2387 case 'd': /* data file */
2388 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2389 install_sh, obj_dir_path( make, file ), dest );
2390 break;
2391 case 'D': /* data file in source dir */
2392 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2393 install_sh, src_dir_path( make, file ), dest );
2394 break;
2395 case 'p': /* program file */
2396 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2397 install_sh, obj_dir_path( make, file ), dest );
2398 break;
2399 case 's': /* script */
2400 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2401 install_sh, obj_dir_path( make, file ), dest );
2402 break;
2403 case 'S': /* script in source dir */
2404 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2405 install_sh, src_dir_path( make, file ), dest );
2406 break;
2407 case 't': /* script in tools dir */
2408 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2409 install_sh, tools_dir_path( make, files.str[i] ), dest );
2410 break;
2411 case 'y': /* symlink */
2412 output_symlink_rule( files.str[i], dest );
2413 break;
2414 default:
2415 assert(0);
2417 strarray_add( &make->uninstall_files, dest );
2422 /*******************************************************************
2423 * output_install_rules
2425 * Rules are stored as a (file,dest) pair of values.
2426 * The first char of dest indicates the type of install.
2428 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2430 unsigned int i;
2431 struct strarray files = make->install_rules[rules];
2432 struct strarray targets = empty_strarray;
2434 if (!files.count) return;
2436 for (i = 0; i < files.count; i += 2)
2438 const char *file = files.str[i];
2439 switch (*files.str[i + 1])
2441 case 'c': /* cross-compiled program */
2442 case 'd': /* data file */
2443 case 'p': /* program file */
2444 case 's': /* script */
2445 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2446 break;
2447 case 't': /* script in tools dir */
2448 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2449 break;
2453 output( "install %s::", target );
2454 output_filenames( targets );
2455 output( "\n" );
2456 output_install_commands( make, NULL, files );
2458 strarray_add_uniq( &make->phony_targets, "install" );
2459 strarray_add_uniq( &make->phony_targets, target );
2463 static int cmp_string_length( const char **a, const char **b )
2465 int paths_a = 0, paths_b = 0;
2466 const char *p;
2468 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2469 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2470 if (paths_b != paths_a) return paths_b - paths_a;
2471 return strcmp( *a, *b );
2474 /*******************************************************************
2475 * output_uninstall_rules
2477 static void output_uninstall_rules( struct makefile *make )
2479 static const char *dirs_order[] =
2480 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };
2482 struct strarray subdirs = empty_strarray;
2483 unsigned int i, j;
2485 if (!make->uninstall_files.count) return;
2486 output( "uninstall::\n" );
2487 output_rm_filenames( make->uninstall_files );
2488 strarray_add_uniq( &make->phony_targets, "uninstall" );
2490 if (!make->subdirs.count) return;
2491 for (i = 0; i < make->uninstall_files.count; i++)
2493 char *dir = xstrdup( make->uninstall_files.str[i] );
2494 while (strchr( dir, '/' ))
2496 *strrchr( dir, '/' ) = 0;
2497 strarray_add_uniq( &subdirs, xstrdup(dir) );
2500 strarray_qsort( &subdirs, cmp_string_length );
2501 output( "\t-rmdir" );
2502 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2504 for (j = 0; j < subdirs.count; j++)
2506 if (!subdirs.str[j]) continue;
2507 if (strncmp( subdirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2508 continue;
2509 output_filename( subdirs.str[j] );
2510 subdirs.str[j] = NULL;
2513 for (j = 0; j < subdirs.count; j++)
2514 if (subdirs.str[j]) output_filename( subdirs.str[j] );
2515 output( "\n" );
2519 /*******************************************************************
2520 * output_importlib_symlinks
2522 static struct strarray output_importlib_symlinks( const struct makefile *parent,
2523 const struct makefile *make )
2525 struct strarray ret = empty_strarray;
2526 const char *lib, *dst, *ext[4];
2527 int i, count = 0;
2529 if (!make->module) return ret;
2530 if (!make->importlib) return ret;
2531 if (make->is_win16 && make->disabled) return ret;
2532 if (strncmp( make->base_dir, "dlls/", 5 )) return ret;
2533 if (!strcmp( make->module, make->importlib )) return ret;
2534 if (!strchr( make->importlib, '.' ) &&
2535 !strncmp( make->module, make->importlib, strlen( make->importlib )) &&
2536 !strcmp( make->module + strlen( make->importlib ), ".dll" ))
2537 return ret;
2539 ext[count++] = *dll_ext ? "def" : "a";
2540 if (needs_delay_lib( make )) ext[count++] = "delay.a";
2541 if (needs_cross_lib( make )) ext[count++] = "cross.a";
2543 for (i = 0; i < count; i++)
2545 lib = strmake( "lib%s.%s", make->importlib, ext[i] );
2546 dst = concat_paths( obj_dir_path( parent, "dlls" ), lib );
2547 output( "%s: %s\n", dst, base_dir_path( make, lib ));
2548 output_symlink_rule( concat_paths( make->base_dir + strlen("dlls/"), lib ), dst );
2549 strarray_add( &ret, dst );
2551 return ret;
2555 /*******************************************************************
2556 * output_po_files
2558 static void output_po_files( const struct makefile *make )
2560 const char *po_dir = src_dir_path( make, "po" );
2561 struct strarray pot_files = empty_strarray;
2562 struct incl_file *source;
2563 unsigned int i;
2565 for (i = 0; i < make->subdirs.count; i++)
2567 struct makefile *submake = make->submakes[i];
2569 LIST_FOR_EACH_ENTRY( source, &submake->sources, struct incl_file, entry )
2571 if (source->file->flags & FLAG_PARENTDIR) continue;
2572 if (strendswith( source->name, ".rc" ) && (source->file->flags & FLAG_RC_PO))
2574 char *pot_file = replace_extension( source->name, ".rc", ".pot" );
2575 char *pot_path = base_dir_path( submake, pot_file );
2576 output( "%s: tools/wrc include dummy\n", pot_path );
2577 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2578 strarray_add( &pot_files, pot_path );
2580 else if (strendswith( source->name, ".mc" ))
2582 char *pot_file = replace_extension( source->name, ".mc", ".pot" );
2583 char *pot_path = base_dir_path( submake, pot_file );
2584 output( "%s: tools/wmc include dummy\n", pot_path );
2585 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2586 strarray_add( &pot_files, pot_path );
2590 if (linguas.count)
2592 for (i = 0; i < linguas.count; i++)
2593 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2594 output( ": %s/wine.pot\n", po_dir );
2595 output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2596 po_dir );
2597 output( "po:" );
2598 for (i = 0; i < linguas.count; i++)
2599 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2600 output( "\n" );
2602 output( "%s/wine.pot:", po_dir );
2603 output_filenames( pot_files );
2604 output( "\n" );
2605 output( "\tmsgcat -o $@" );
2606 output_filenames( pot_files );
2607 output( "\n" );
2611 /*******************************************************************
2612 * output_source_y
2614 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2616 /* add source file dependency for parallel makes */
2617 char *header = strmake( "%s.tab.h", obj );
2619 if (find_include_file( make, header ))
2621 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2622 output( "\t%s -p %s_ -o %s.tab.c -d %s\n",
2623 bison, obj, obj_dir_path( make, obj ), source->filename );
2624 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2625 source->filename, obj_dir_path( make, header ));
2626 strarray_add( &make->clean_files, header );
2628 else output( "%s.tab.c: %s\n", obj, source->filename );
2630 output( "\t%s -p %s_ -o $@ %s\n", bison, obj, source->filename );
2634 /*******************************************************************
2635 * output_source_l
2637 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2639 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2640 output( "\t%s -o$@ %s\n", flex, source->filename );
2644 /*******************************************************************
2645 * output_source_h
2647 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2649 if (source->file->flags & FLAG_GENERATED)
2650 strarray_add( &make->all_targets, source->name );
2651 else
2652 add_install_rule( make, source->name, source->name,
2653 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2657 /*******************************************************************
2658 * output_source_rc
2660 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2662 struct strarray defines = get_source_defines( make, source, obj );
2663 unsigned int i;
2665 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2666 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2667 output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
2668 output( "\t%s -o $@", tools_path( make, "wrc" ) );
2669 if (make->is_win16) output_filename( "-m16" );
2670 else output_filenames( target_flags );
2671 output_filename( "--nostdinc" );
2672 output_filenames( defines );
2673 if (linguas.count && (source->file->flags & FLAG_RC_PO))
2675 char *po_dir = top_obj_dir_path( make, "po" );
2676 output_filename( strmake( "--po-dir=%s", po_dir ));
2677 output_filename( source->filename );
2678 output( "\n" );
2679 output( "%s.res:", obj_dir_path( make, obj ));
2680 for (i = 0; i < linguas.count; i++)
2681 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2682 output( "\n" );
2684 else
2686 output_filename( source->filename );
2687 output( "\n" );
2689 if (source->file->flags & FLAG_RC_PO && !(source->file->flags & FLAG_PARENTDIR))
2691 strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
2692 output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
2693 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ) );
2694 if (make->is_win16) output_filename( "-m16" );
2695 else output_filenames( target_flags );
2696 output_filename( "--nostdinc" );
2697 output_filenames( defines );
2698 output_filename( source->filename );
2699 output( "\n" );
2700 output( "%s.pot ", obj_dir_path( make, obj ));
2702 output( "%s.res:", obj_dir_path( make, obj ));
2703 output_filename( tools_path( make, "wrc" ));
2704 output_filenames( source->dependencies );
2705 output( "\n" );
2709 /*******************************************************************
2710 * output_source_mc
2712 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2714 unsigned int i;
2716 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2717 strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
2718 output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
2719 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
2720 if (linguas.count)
2722 char *po_dir = top_obj_dir_path( make, "po" );
2723 output_filename( strmake( "--po-dir=%s", po_dir ));
2724 output( "\n" );
2725 output( "%s.res:", obj_dir_path( make, obj ));
2726 for (i = 0; i < linguas.count; i++)
2727 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2729 output( "\n" );
2730 output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
2731 output( "\t%s -O pot -o $@ %s", tools_path( make, "wmc" ), source->filename );
2732 output( "\n" );
2733 output( "%s.pot %s.res:", obj_dir_path( make, obj ), obj_dir_path( make, obj ));
2734 output_filename( tools_path( make, "wmc" ));
2735 output_filenames( source->dependencies );
2736 output( "\n" );
2740 /*******************************************************************
2741 * output_source_res
2743 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2745 strarray_add( &make->res_files, source->name );
2749 /*******************************************************************
2750 * output_source_idl
2752 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2754 struct strarray defines = get_source_defines( make, source, obj );
2755 struct strarray targets = empty_strarray;
2756 char *dest;
2757 unsigned int i;
2759 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2760 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2762 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2764 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2765 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2766 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2767 strarray_add( &targets, dest );
2769 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2770 if (source->file->flags & FLAG_INSTALL)
2772 add_install_rule( make, source->name, xstrdup( source->name ),
2773 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2774 if (source->file->flags & FLAG_IDL_HEADER)
2775 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2776 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2778 if (!targets.count) return;
2780 output_filenames_obj_dir( make, targets );
2781 output( ": %s\n", tools_path( make, "widl" ));
2782 output( "\t%s -o $@", tools_path( make, "widl" ) );
2783 output_filenames( target_flags );
2784 output_filenames( defines );
2785 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2786 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2787 output_filename( source->filename );
2788 output( "\n" );
2789 output_filenames_obj_dir( make, targets );
2790 output( ": %s", source->filename );
2791 output_filenames( source->dependencies );
2792 output( "\n" );
2796 /*******************************************************************
2797 * output_source_tlb
2799 static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
2801 strarray_add( &make->all_targets, source->name );
2805 /*******************************************************************
2806 * output_source_x
2808 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2810 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2811 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2812 output( "\t%s%s -H -o $@ %s\n",
2813 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2814 if (source->file->flags & FLAG_INSTALL)
2816 add_install_rule( make, source->name, source->name,
2817 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2818 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2819 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2824 /*******************************************************************
2825 * output_source_sfd
2827 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2829 unsigned int i;
2830 char *ttf_obj = strmake( "%s.ttf", obj );
2831 char *ttf_file = src_dir_path( make, ttf_obj );
2833 if (fontforge && !make->src_dir)
2835 output( "%s: %s\n", ttf_file, source->filename );
2836 output( "\t%s -script %s %s $@\n",
2837 fontforge, top_src_dir_path( make, "fonts/genttf.ff" ), source->filename );
2838 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
2840 if (source->file->flags & FLAG_INSTALL)
2841 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2843 if (source->file->flags & FLAG_SFD_FONTS)
2845 struct strarray *array = source->file->args;
2847 for (i = 0; i < array->count; i++)
2849 char *font = strtok( xstrdup(array->str[i]), " \t" );
2850 char *args = strtok( NULL, "" );
2852 strarray_add( &make->all_targets, xstrdup( font ));
2853 output( "%s: %s %s\n", obj_dir_path( make, font ),
2854 tools_path( make, "sfnt2fon" ), ttf_file );
2855 output( "\t%s -q -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2856 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2862 /*******************************************************************
2863 * output_source_svg
2865 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2867 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2868 unsigned int i;
2870 if (convert && rsvg && icotool && !make->src_dir)
2872 for (i = 0; images[i]; i++)
2873 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2875 if (images[i])
2877 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2878 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2879 top_src_dir_path( make, "tools/buildimage" ), source->filename );
2885 /*******************************************************************
2886 * output_source_nls
2888 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2890 add_install_rule( make, source->name, source->name,
2891 strmake( "D$(datadir)/wine/%s", source->name ));
2895 /*******************************************************************
2896 * output_source_desktop
2898 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2900 add_install_rule( make, source->name, source->name,
2901 strmake( "D$(datadir)/applications/%s", source->name ));
2905 /*******************************************************************
2906 * output_source_po
2908 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2910 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2911 output( "\t%s -o $@ %s\n", msgfmt, source->filename );
2912 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2916 /*******************************************************************
2917 * output_source_in
2919 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2921 unsigned int i;
2923 if (strendswith( obj, ".man" ) && source->file->args)
2925 struct strarray symlinks;
2926 char *dir, *dest = replace_extension( obj, ".man", "" );
2927 char *lang = strchr( dest, '.' );
2928 char *section = source->file->args;
2929 if (lang)
2931 *lang++ = 0;
2932 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2934 else dir = strmake( "$(mandir)/man%s", section );
2935 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2936 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2937 for (i = 0; i < symlinks.count; i++)
2938 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2939 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2940 free( dest );
2941 free( dir );
2943 strarray_add( &make->in_files, xstrdup(obj) );
2944 strarray_add( &make->all_targets, xstrdup(obj) );
2945 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2946 output( "\t%s %s >$@ || (rm -f $@ && false)\n", sed_cmd, source->filename );
2947 output( "%s:", obj_dir_path( make, obj ));
2948 output_filenames( source->dependencies );
2949 output( "\n" );
2950 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2954 /*******************************************************************
2955 * output_source_spec
2957 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2959 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2960 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
2961 struct strarray all_libs, dep_libs = empty_strarray;
2962 char *dll_name, *obj_name;
2964 if (!imports.count) imports = make->imports;
2965 if (!dll_flags.count) dll_flags = make->extradllflags;
2966 all_libs = add_import_libs( make, &dep_libs, imports, 0 );
2967 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
2968 strarray_addall( &all_libs, libs );
2969 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
2970 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
2972 strarray_add( &make->clean_files, dll_name );
2973 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2974 output( "%s.res: %s\n", obj_dir_path( make, obj ), obj_dir_path( make, dll_name ));
2975 output( "\techo \"%s.dll TESTDLL \\\"%s\\\"\" | %s -o $@\n", obj,
2976 obj_dir_path( make, dll_name ), tools_path( make, "wrc" ));
2978 output( "%s:", obj_dir_path( make, dll_name ));
2979 output_filename( source->filename );
2980 output_filename( obj_name );
2981 output_filenames( dep_libs );
2982 output_filename( tools_path( make, "winebuild" ));
2983 output_filename( tools_path( make, "winegcc" ));
2984 output( "\n" );
2985 output_winegcc_command( make );
2986 output_filename( "-s" );
2987 output_filenames( dll_flags );
2988 output_filename( "-shared" );
2989 output_filename( source->filename );
2990 output_filename( obj_name );
2991 output_filenames( all_libs );
2992 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
2993 output( "\n" );
2997 /*******************************************************************
2998 * output_source_default
3000 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3002 struct strarray defines = get_source_defines( make, source, obj );
3003 int is_dll_src = (make->testdll &&
3004 strendswith( source->name, ".c" ) &&
3005 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3006 int need_cross = (crosstarget &&
3007 !(source->file->flags & FLAG_C_UNIX) &&
3008 (make->is_cross ||
3009 ((source->file->flags & FLAG_C_IMPLIB) &&
3010 (needs_cross_lib( make ) || needs_delay_lib( make ))) ||
3011 (make->staticlib && needs_cross_lib( make ))));
3012 int need_obj = ((*dll_ext || !make->staticlib || !(source->file->flags & FLAG_C_UNIX)) &&
3013 (!need_cross ||
3014 (source->file->flags & FLAG_C_IMPLIB) ||
3015 (make->module && make->staticlib)));
3017 if ((source->file->flags & FLAG_GENERATED) &&
3018 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3019 strarray_add( &make->clean_files, source->filename );
3020 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
3022 if (need_obj)
3024 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3025 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3026 else if (!is_dll_src)
3027 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3028 else
3029 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3030 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3031 output( "\t$(CC) -c -o $@ %s", source->filename );
3032 output_filenames( defines );
3033 if (make->module || make->staticlib || make->sharedlib || make->testdll)
3035 output_filenames( dll_flags );
3036 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3038 output_filenames( extra_cflags );
3039 output_filenames( cpp_flags );
3040 output_filename( "$(CFLAGS)" );
3041 output( "\n" );
3043 if (need_cross)
3045 strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
3046 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3047 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
3048 output_filenames( defines );
3049 output_filenames( extra_cross_cflags );
3050 output_filenames( cpp_flags );
3051 output_filename( "$(CROSSCFLAGS)" );
3052 output( "\n" );
3054 if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
3056 strarray_add( &make->c2man_files, source->filename );
3057 if (make->testdll && !is_dll_src)
3059 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3060 output( "%s.ok:\n", obj_dir_path( make, obj ));
3061 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
3062 top_src_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ),
3063 make->testdll, replace_extension( make->testdll, ".dll", "_test.exe" ),
3064 make->is_cross ? "" : dll_ext, obj );
3067 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3068 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3069 output( ":" );
3070 output_filenames( source->dependencies );
3071 output( "\n" );
3075 /* dispatch table to output rules for a single source file */
3076 static const struct
3078 const char *ext;
3079 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3080 } output_source_funcs[] =
3082 { "y", output_source_y },
3083 { "l", output_source_l },
3084 { "h", output_source_h },
3085 { "rh", output_source_h },
3086 { "inl", output_source_h },
3087 { "rc", output_source_rc },
3088 { "mc", output_source_mc },
3089 { "res", output_source_res },
3090 { "idl", output_source_idl },
3091 { "tlb", output_source_tlb },
3092 { "sfd", output_source_sfd },
3093 { "svg", output_source_svg },
3094 { "nls", output_source_nls },
3095 { "desktop", output_source_desktop },
3096 { "po", output_source_po },
3097 { "in", output_source_in },
3098 { "x", output_source_x },
3099 { "spec", output_source_spec },
3100 { NULL, output_source_default }
3104 /*******************************************************************
3105 * has_object_file
3107 static int has_object_file( struct makefile *make )
3109 struct incl_file *source;
3110 int i;
3112 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3114 char *ext = get_extension( source->name );
3116 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3117 ext++;
3119 for (i = 0; output_source_funcs[i].ext; i++)
3120 if (!strcmp( ext, output_source_funcs[i].ext )) break;
3122 if (!output_source_funcs[i].ext) return 1; /* default extension builds to an object file */
3124 return 0;
3128 /*******************************************************************
3129 * output_man_pages
3131 static void output_man_pages( struct makefile *make )
3133 if (make->c2man_files.count)
3135 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3137 output( "manpages::\n" );
3138 output( "\t%s -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3139 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3140 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3141 output_filename( strmake( "-o %s/man%s",
3142 top_obj_dir_path( make, "documentation" ), man_ext ));
3143 output_filenames( make->c2man_files );
3144 output( "\n" );
3145 output( "htmlpages::\n" );
3146 output( "\t%s -Th -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3147 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3148 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3149 output_filename( strmake( "-o %s",
3150 top_obj_dir_path( make, "documentation/html" )));
3151 output_filenames( make->c2man_files );
3152 output( "\n" );
3153 output( "sgmlpages::\n" );
3154 output( "\t%s -Ts -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3155 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3156 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3157 output_filename( strmake( "-o %s",
3158 top_obj_dir_path( make, "documentation/api-guide" )));
3159 output_filenames( make->c2man_files );
3160 output( "\n" );
3161 output( "xmlpages::\n" );
3162 output( "\t%s -Tx -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3163 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3164 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3165 output_filename( strmake( "-o %s",
3166 top_obj_dir_path( make, "documentation/api-guide-xml" )));
3167 output_filenames( make->c2man_files );
3168 output( "\n" );
3169 strarray_add( &make->phony_targets, "manpages" );
3170 strarray_add( &make->phony_targets, "htmlpages" );
3171 strarray_add( &make->phony_targets, "sgmlpages" );
3172 strarray_add( &make->phony_targets, "xmlpages" );
3174 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
3178 /*******************************************************************
3179 * output_module
3181 static void output_module( struct makefile *make )
3183 struct strarray all_libs = empty_strarray;
3184 struct strarray dep_libs = empty_strarray;
3185 char *module_path = obj_dir_path( make, make->module );
3186 char *spec_file = NULL;
3187 unsigned int i;
3189 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3190 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1 ));
3191 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0 ));
3192 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
3194 if (make->is_cross)
3196 strarray_add( &make->all_targets, strmake( "%s", make->module ));
3197 add_install_rule( make, make->module, strmake( "%s", make->module ),
3198 strmake( "c$(dlldir)/%s", make->module ));
3199 output( "%s:", module_path );
3201 else
3203 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
3205 if (*dll_ext)
3207 for (i = 0; i < make->delayimports.count; i++)
3208 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
3209 strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
3210 strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
3211 add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
3212 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
3213 add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
3214 strmake( "d$(dlldir)/fakedlls/%s", make->module ));
3215 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
3217 else
3219 strarray_add( &make->all_targets, make->module );
3220 add_install_rule( make, make->module, make->module,
3221 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
3222 output( "%s:", module_path );
3226 if (spec_file) output_filename( spec_file );
3227 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3228 output_filenames_obj_dir( make, make->res_files );
3229 output_filenames( dep_libs );
3230 output_filename( tools_path( make, "winebuild" ));
3231 output_filename( tools_path( make, "winegcc" ));
3232 output( "\n" );
3233 output_winegcc_command( make );
3234 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3235 if (spec_file)
3237 output_filename( "-shared" );
3238 output_filename( spec_file );
3240 output_filenames( make->extradllflags );
3241 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3242 output_filenames_obj_dir( make, make->res_files );
3243 output_filenames( all_libs );
3244 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3245 output( "\n" );
3247 if (spec_file && make->importlib)
3249 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3250 if (*dll_ext && !make->implib_objs.count)
3252 strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
3253 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
3254 output( "\t%s -w --def -o $@", tools_path( make, "winebuild" ) );
3255 output_filenames( target_flags );
3256 if (make->is_win16) output_filename( "-m16" );
3257 output_filename( "--export" );
3258 output_filename( spec_file );
3259 output( "\n" );
3260 add_install_rule( make, make->importlib,
3261 strmake( "lib%s.def", make->importlib ),
3262 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
3264 else
3266 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3267 if (!*dll_ext && needs_delay_lib( make ))
3269 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3270 output( "%s.delay.a ", importlib_path );
3272 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3273 output_filenames_obj_dir( make, make->implib_objs );
3274 output( "\n" );
3275 output( "\t%s -w --implib -o $@", tools_path( make, "winebuild" ) );
3276 output_filenames( target_flags );
3277 if (make->is_win16) output_filename( "-m16" );
3278 output_filename( "--export" );
3279 output_filename( spec_file );
3280 output_filenames_obj_dir( make, make->implib_objs );
3281 output( "\n" );
3282 add_install_rule( make, make->importlib,
3283 strmake( "lib%s.a", make->importlib ),
3284 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
3286 if (crosstarget && (needs_cross_lib( make ) || needs_delay_lib( make )))
3288 struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
3289 if (needs_cross_lib( make ))
3291 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3292 output_filename( strmake( "%s.cross.a", importlib_path ));
3294 if (needs_delay_lib( make ))
3296 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3297 output_filename( strmake( "%s.delay.a", importlib_path ));
3299 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3300 output_filenames_obj_dir( make, cross_files );
3301 output( "\n" );
3302 output( "\t%s -b %s -w --implib -o $@", tools_path( make, "winebuild" ), crosstarget );
3303 if (make->is_win16) output_filename( "-m16" );
3304 output_filename( "--export" );
3305 output_filename( spec_file );
3306 output_filenames_obj_dir( make, cross_files );
3307 output( "\n" );
3311 if (spec_file)
3312 output_man_pages( make );
3313 else if (*dll_ext && !make->is_win16)
3315 char *binary = replace_extension( make->module, ".exe", "" );
3316 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3321 /*******************************************************************
3322 * output_static_lib
3324 static void output_static_lib( struct makefile *make )
3326 strarray_add( &make->all_targets, make->staticlib );
3327 output( "%s:", obj_dir_path( make, make->staticlib ));
3328 output_filenames_obj_dir( make, make->object_files );
3329 output_filenames_obj_dir( make, make->unixobj_files );
3330 output( "\n\trm -f $@\n" );
3331 output( "\t%s rc $@", ar );
3332 output_filenames_obj_dir( make, make->object_files );
3333 output_filenames_obj_dir( make, make->unixobj_files );
3334 output( "\n\t%s $@\n", ranlib );
3335 add_install_rule( make, make->staticlib, make->staticlib,
3336 strmake( "d$(dlldir)/%s", make->staticlib ));
3337 if (needs_cross_lib( make ))
3339 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3341 strarray_add( &make->all_targets, name );
3342 output( "%s:", obj_dir_path( make, name ));
3343 output_filenames_obj_dir( make, make->crossobj_files );
3344 output( "\n\trm -f $@\n" );
3345 output( "\t%s-ar rc $@", crosstarget );
3346 output_filenames_obj_dir( make, make->crossobj_files );
3347 output( "\n\t%s-ranlib $@\n", crosstarget );
3352 /*******************************************************************
3353 * output_shared_lib
3355 static void output_shared_lib( struct makefile *make )
3357 unsigned int i;
3358 char *basename, *p;
3359 struct strarray names = get_shared_lib_names( make->sharedlib );
3360 struct strarray all_libs = empty_strarray;
3361 struct strarray dep_libs = empty_strarray;
3363 basename = xstrdup( make->sharedlib );
3364 if ((p = strchr( basename, '.' ))) *p = 0;
3366 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3367 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3368 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
3370 output( "%s:", obj_dir_path( make, make->sharedlib ));
3371 output_filenames_obj_dir( make, make->object_files );
3372 output_filenames( dep_libs );
3373 output( "\n" );
3374 output( "\t$(CC) -o $@" );
3375 output_filenames_obj_dir( make, make->object_files );
3376 output_filenames( all_libs );
3377 output_filename( "$(LDFLAGS)" );
3378 output( "\n" );
3379 add_install_rule( make, make->sharedlib, make->sharedlib,
3380 strmake( "p$(libdir)/%s", make->sharedlib ));
3381 for (i = 1; i < names.count; i++)
3383 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3384 output_symlink_rule( obj_dir_path( make, names.str[i-1] ), obj_dir_path( make, names.str[i] ));
3385 add_install_rule( make, names.str[i], names.str[i-1],
3386 strmake( "y$(libdir)/%s", names.str[i] ));
3388 strarray_addall( &make->all_targets, names );
3392 /*******************************************************************
3393 * output_test_module
3395 static void output_test_module( struct makefile *make )
3397 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3398 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3399 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3400 struct strarray dep_libs = empty_strarray;
3401 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0 );
3402 struct makefile *parent = get_parent_makefile( make );
3403 const char *ext = make->is_cross ? "" : dll_ext;
3404 const char *parent_ext = parent && parent->is_cross ? "" : dll_ext;
3406 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
3407 strarray_addall( &all_libs, libs );
3408 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3409 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3410 output( "%s%s:\n", obj_dir_path( make, testmodule ), ext );
3411 output_winegcc_command( make );
3412 output_filenames( make->extradllflags );
3413 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3414 output_filenames_obj_dir( make, make->res_files );
3415 output_filenames( all_libs );
3416 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3417 output( "\n" );
3418 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3419 output_winegcc_command( make );
3420 output_filename( "-s" );
3421 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3422 output_filenames( make->extradllflags );
3423 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3424 output_filenames_obj_dir( make, make->res_files );
3425 output_filenames( all_libs );
3426 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3427 output( "\n" );
3428 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3429 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3430 output_filenames_obj_dir( make, make->res_files );
3431 output_filenames( dep_libs );
3432 output_filename( tools_path( make, "winebuild" ));
3433 output_filename( tools_path( make, "winegcc" ));
3434 output( "\n" );
3436 if (!make->disabled && !strarray_exists( &disabled_dirs, "programs/winetest" ))
3437 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
3438 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
3439 obj_dir_path( make, stripped ), ext );
3440 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
3441 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3443 output_filenames_obj_dir( make, make->ok_files );
3444 output( ": %s%s ../%s%s\n", testmodule, ext, make->testdll, parent_ext );
3445 output( "check test:" );
3446 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3447 output( "\n" );
3448 strarray_add( &make->phony_targets, "check" );
3449 strarray_add( &make->phony_targets, "test" );
3450 output( "testclean::\n" );
3451 output( "\trm -f" );
3452 output_filenames_obj_dir( make, make->ok_files );
3453 output( "\n" );
3454 strarray_addall( &make->clean_files, make->ok_files );
3455 strarray_add( &make->phony_targets, "testclean" );
3459 /*******************************************************************
3460 * output_programs
3462 static void output_programs( struct makefile *make )
3464 unsigned int i, j;
3465 char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
3466 char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
3468 for (i = 0; i < make->programs.count; i++)
3470 char *program_installed = NULL;
3471 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3472 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3473 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3474 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3475 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3477 if (!objs.count) objs = make->object_files;
3478 strarray_addall( &all_libs, add_default_libraries( make, &deps ));
3480 output( "%s:", obj_dir_path( make, program ) );
3481 output_filenames_obj_dir( make, objs );
3482 output_filenames( deps );
3483 output( "\n" );
3484 output( "\t$(CC) -o $@" );
3485 output_filenames_obj_dir( make, objs );
3487 if (strarray_exists( &all_libs, "-lwine" ))
3489 strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
3490 if (ldrpath_local && ldrpath_install)
3492 program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
3493 output_filename( ldrpath_local );
3494 output_filenames( all_libs );
3495 output_filename( "$(LDFLAGS)" );
3496 output( "\n" );
3497 output( "%s:", obj_dir_path( make, program_installed ) );
3498 output_filenames_obj_dir( make, objs );
3499 output_filenames( deps );
3500 output( "\n" );
3501 output( "\t$(CC) -o $@" );
3502 output_filenames_obj_dir( make, objs );
3503 output_filename( ldrpath_install );
3504 strarray_add( &make->all_targets, program_installed );
3508 output_filenames( all_libs );
3509 output_filename( "$(LDFLAGS)" );
3510 output( "\n" );
3511 strarray_add( &make->all_targets, program );
3513 for (j = 0; j < symlinks.count; j++)
3515 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3516 output_symlink_rule( obj_dir_path( make, program ), obj_dir_path( make, symlinks.str[j] ));
3518 strarray_addall( &make->all_targets, symlinks );
3520 add_install_rule( make, program, program_installed ? program_installed : program,
3521 strmake( "p$(bindir)/%s", program ));
3522 for (j = 0; j < symlinks.count; j++)
3523 add_install_rule( make, symlinks.str[j], program,
3524 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3529 /*******************************************************************
3530 * output_subdirs
3532 static void output_subdirs( struct makefile *make )
3534 struct strarray symlinks = empty_strarray;
3535 struct strarray all_deps = empty_strarray;
3536 struct strarray build_deps = empty_strarray;
3537 struct strarray builddeps_deps = empty_strarray;
3538 struct strarray makefile_deps = empty_strarray;
3539 struct strarray clean_files = empty_strarray;
3540 struct strarray testclean_files = empty_strarray;
3541 struct strarray distclean_files = empty_strarray;
3542 struct strarray tools_deps = empty_strarray;
3543 struct strarray tooldeps_deps = empty_strarray;
3544 struct strarray winetest_deps = empty_strarray;
3545 unsigned int i, j;
3547 strarray_addall( &distclean_files, make->distclean_files );
3548 for (i = 0; i < make->subdirs.count; i++)
3550 const struct makefile *submake = make->submakes[i];
3551 char *subdir = base_dir_path( submake, "" );
3553 strarray_add( &makefile_deps, top_src_dir_path( make, base_dir_path( submake,
3554 strmake ( "%s.in", output_makefile_name ))));
3555 for (j = 0; j < submake->clean_files.count; j++)
3556 strarray_add( &clean_files, base_dir_path( submake, submake->clean_files.str[j] ));
3557 for (j = 0; j < submake->distclean_files.count; j++)
3558 strarray_add( &distclean_files, base_dir_path( submake, submake->distclean_files.str[j] ));
3559 for (j = 0; j < submake->ok_files.count; j++)
3560 strarray_add( &testclean_files, base_dir_path( submake, submake->ok_files.str[j] ));
3562 /* import libs are still created for disabled dirs, except for win16 ones */
3563 if (submake->module && submake->importlib && !(submake->disabled && submake->is_win16))
3565 char *importlib_path = base_dir_path( submake, strmake( "lib%s", submake->importlib ));
3566 if (submake->implib_objs.count)
3568 output( "%s.a: dummy\n", importlib_path );
3569 output( "\t@cd %s && $(MAKE) lib%s.a\n", subdir, submake->importlib );
3570 strarray_add( &tools_deps, strmake( "%s.a", importlib_path ));
3571 if (needs_cross_lib( submake ))
3573 output( "%s.cross.a: dummy\n", importlib_path );
3574 output( "\t@cd %s && $(MAKE) lib%s.cross.a\n", subdir, submake->importlib );
3575 strarray_add( &tools_deps, strmake( "%s.cross.a", importlib_path ));
3577 if (needs_delay_lib( submake ))
3579 output( "%s.delay.a: dummy\n", importlib_path );
3580 output( "\t@cd %s && $(MAKE) lib%s.delay.a\n", subdir, submake->importlib );
3581 strarray_add( &tools_deps, strmake( "%s.delay.a", importlib_path ));
3584 else
3586 char *spec_file = top_src_dir_path( make, base_dir_path( submake,
3587 replace_extension( submake->module, ".dll", ".spec" )));
3588 if (*dll_ext)
3590 output( "%s.def: %s", importlib_path, spec_file );
3591 strarray_add( &build_deps, strmake( "%s.def", importlib_path ));
3593 else
3595 if (needs_delay_lib( submake ))
3597 output( "%s.delay.a ", importlib_path );
3598 strarray_add( &build_deps, strmake( "%s.delay.a", importlib_path ));
3600 output( "%s.a: %s", importlib_path, spec_file );
3601 strarray_add( &build_deps, strmake( "%s.a", importlib_path ));
3603 output_filename( tools_path( make, "winebuild" ));
3604 output( "\n" );
3605 output( "\t%s -w -o $@", tools_path( make, "winebuild" ));
3606 output_filename( *dll_ext ? "--def" : "--implib" );
3607 output_filenames( target_flags );
3608 if (submake->is_win16) output_filename( "-m16" );
3609 output_filename( "--export" );
3610 output_filename( spec_file );
3611 output( "\n" );
3612 if (crosstarget && (needs_cross_lib( submake ) || needs_delay_lib( submake )))
3614 if (needs_cross_lib( submake ))
3616 output_filename( strmake( "%s.cross.a", importlib_path ));
3617 strarray_add( &build_deps, strmake( "%s.cross.a", importlib_path ));
3619 if (needs_delay_lib( submake ))
3621 output_filename( strmake( "%s.delay.a", importlib_path ));
3622 strarray_add( &build_deps, strmake( "%s.delay.a", importlib_path ));
3624 output( ": %s", spec_file );
3625 output_filename( tools_path( make, "winebuild" ));
3626 output( "\n" );
3627 output( "\t%s -b %s -w -o $@", tools_path( make, "winebuild" ), crosstarget );
3628 if (submake->is_win16) output_filename( "-m16" );
3629 output_filename( "--implib" );
3630 output_filename( "--export" );
3631 output_filename( spec_file );
3632 output( "\n" );
3635 strarray_addall( &symlinks, output_importlib_symlinks( make, submake ));
3638 if (submake->disabled) continue;
3639 strarray_add( &all_deps, subdir );
3641 if (submake->module)
3643 if (!submake->staticlib)
3645 strarray_add( &builddeps_deps, subdir );
3646 if (!submake->is_exe)
3648 output( "manpages htmlpages sgmlpages xmlpages::\n" );
3649 output( "\t@cd %s && $(MAKE) $@\n", subdir );
3652 else strarray_add( &tools_deps, subdir );
3654 else if (submake->testdll)
3656 output( "%s/test: dummy\n", subdir );
3657 output( "\t@cd %s && $(MAKE) test\n", subdir );
3658 strarray_add( &winetest_deps, subdir );
3659 strarray_add( &builddeps_deps, subdir );
3661 else
3663 if (!strcmp( submake->base_dir, "tools" ) || !strncmp( submake->base_dir, "tools/", 6 ))
3665 strarray_add( &tooldeps_deps, submake->base_dir );
3666 for (j = 0; j < submake->programs.count; j++)
3667 output( "%s/%s%s: %s\n", submake->base_dir,
3668 submake->programs.str[j], tools_ext, submake->base_dir );
3670 if (submake->programs.count || submake->sharedlib)
3672 struct strarray libs = get_expanded_make_var_array( submake, "EXTRALIBS" );
3673 for (j = 0; j < submake->programs.count; j++)
3674 strarray_addall( &libs, get_expanded_file_local_var( submake,
3675 submake->programs.str[j], "LDFLAGS" ));
3676 output( "%s: libs/port", submake->base_dir );
3677 for (j = 0; j < libs.count; j++)
3679 if (!strcmp( libs.str[j], "-lwpp" )) output_filename( "libs/wpp" );
3680 if (!strcmp( libs.str[j], "-lwine" )) output_filename( "libs/wine" );
3682 output( "\n" );
3686 if (submake->install_rules[INSTALL_LIB].count)
3688 output( "install install-lib:: %s\n", submake->base_dir );
3689 output_install_commands( make, submake, submake->install_rules[INSTALL_LIB] );
3691 if (submake->install_rules[INSTALL_DEV].count)
3693 output( "install install-dev:: %s\n", submake->base_dir );
3694 output_install_commands( make, submake, submake->install_rules[INSTALL_DEV] );
3697 output( "all:" );
3698 output_filenames( all_deps );
3699 output( "\n" );
3700 output_filenames( all_deps );
3701 output( ": dummy\n" );
3702 output( "\t@cd $@ && $(MAKE)\n" );
3703 output( "Makefile:" );
3704 output_filenames( makefile_deps );
3705 output( "\n" );
3706 output_filenames( makefile_deps );
3707 output( ":\n" );
3708 if (tooldeps_deps.count)
3710 output( "__tooldeps__:" );
3711 output_filenames( tooldeps_deps );
3712 output( "\n" );
3713 strarray_add( &make->phony_targets, "__tooldeps__" );
3715 if (winetest_deps.count)
3717 output( "buildtests programs/winetest:" );
3718 output_filenames( winetest_deps );
3719 output( "\n" );
3720 output( "check test:" );
3721 for (i = 0; i < winetest_deps.count; i++)
3723 char *target = strmake( "%s/test", winetest_deps.str[i] );
3724 output_filename( target );
3725 strarray_add( &make->phony_targets, target );
3727 output( "\n" );
3728 strarray_add( &make->phony_targets, "buildtests" );
3729 strarray_add( &make->phony_targets, "check" );
3730 strarray_add( &make->phony_targets, "test" );
3733 output( "clean::\n");
3734 output_rm_filenames( clean_files );
3735 output( "testclean::\n");
3736 output_rm_filenames( testclean_files );
3737 output( "distclean::\n");
3738 output_rm_filenames( distclean_files );
3739 output_filenames( tools_deps );
3740 output( ":" );
3741 output_filename( tools_dir_path( make, "widl" ));
3742 output_filename( tools_dir_path( make, "winebuild" ));
3743 output_filename( tools_dir_path( make, "winegcc" ));
3744 output_filename( obj_dir_path( make, "include" ));
3745 output( "\n" );
3746 output_filenames( builddeps_deps );
3747 output( ": __builddeps__\n" );
3748 strarray_add( &make->phony_targets, "distclean" );
3749 strarray_add( &make->phony_targets, "testclean" );
3750 strarray_addall( &make->phony_targets, all_deps );
3752 strarray_addall( &make->clean_files, symlinks );
3753 strarray_addall( &build_deps, tools_deps );
3754 strarray_addall( &build_deps, symlinks );
3755 if (build_deps.count)
3757 output( "__builddeps__:" );
3758 output_filenames( build_deps );
3759 output( "\n" );
3760 strarray_add( &make->phony_targets, "__builddeps__" );
3762 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3766 /*******************************************************************
3767 * output_sources
3769 static void output_sources( struct makefile *make )
3771 struct incl_file *source;
3772 unsigned int i, j;
3774 strarray_add( &make->phony_targets, "all" );
3776 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3778 char *obj = xstrdup( source->name );
3779 char *ext = get_extension( obj );
3781 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3782 *ext++ = 0;
3784 for (j = 0; output_source_funcs[j].ext; j++)
3785 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3787 output_source_funcs[j].fn( make, source, obj );
3788 strarray_addall_uniq( &make->dependencies, source->dependencies );
3791 /* special case for winetest: add resource files from other test dirs */
3792 if (make->base_dir && !strcmp( make->base_dir, "programs/winetest" ))
3794 struct strarray tests = enable_tests;
3795 if (!tests.count)
3796 for (i = 0; i < top_makefile->subdirs.count; i++)
3797 if (top_makefile->submakes[i]->testdll && !top_makefile->submakes[i]->disabled)
3798 strarray_add( &tests, top_makefile->submakes[i]->testdll );
3799 for (i = 0; i < tests.count; i++)
3800 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3803 if (make->dlldata_files.count)
3805 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3806 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3807 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
3808 output_filenames( make->dlldata_files );
3809 output( "\n" );
3812 if (make->staticlib) output_static_lib( make );
3813 else if (make->module) output_module( make );
3814 else if (make->testdll) output_test_module( make );
3815 else if (make->sharedlib) output_shared_lib( make );
3816 else if (make->programs.count) output_programs( make );
3818 for (i = 0; i < make->scripts.count; i++)
3819 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3820 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3822 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3823 strarray_add( &make->distclean_files, "Makefile" );
3824 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3826 if (!make->base_dir)
3827 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3828 else if (!strcmp( make->base_dir, "po" ))
3829 strarray_add( &make->distclean_files, "LINGUAS" );
3831 if (make->subdirs.count) output_subdirs( make );
3833 if (!make->disabled)
3835 if (make->all_targets.count)
3837 output( "all:" );
3838 output_filenames_obj_dir( make, make->all_targets );
3839 output( "\n" );
3841 output_install_rules( make, INSTALL_LIB, "install-lib" );
3842 output_install_rules( make, INSTALL_DEV, "install-dev" );
3843 output_uninstall_rules( make );
3846 if (make->dependencies.count)
3848 output_filenames( make->dependencies );
3849 output( ":\n" );
3852 strarray_addall( &make->clean_files, make->object_files );
3853 strarray_addall( &make->clean_files, make->crossobj_files );
3854 strarray_addall( &make->clean_files, make->unixobj_files );
3855 strarray_addall( &make->clean_files, make->res_files );
3856 strarray_addall( &make->clean_files, make->all_targets );
3857 strarray_addall( &make->clean_files, make->extra_targets );
3859 if (make->clean_files.count)
3861 output( "%s::\n", obj_dir_path( make, "clean" ));
3862 output( "\trm -f" );
3863 output_filenames_obj_dir( make, make->clean_files );
3864 output( "\n" );
3865 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
3866 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3869 if (make->phony_targets.count)
3871 output( ".PHONY:" );
3872 output_filenames( make->phony_targets );
3873 output( "\n" );
3878 /*******************************************************************
3879 * create_temp_file
3881 static FILE *create_temp_file( const char *orig )
3883 char *name = xmalloc( strlen(orig) + 13 );
3884 unsigned int i, id = getpid();
3885 int fd;
3886 FILE *ret = NULL;
3888 for (i = 0; i < 100; i++)
3890 sprintf( name, "%s.tmp%08x", orig, id );
3891 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3893 ret = fdopen( fd, "w" );
3894 break;
3896 if (errno != EEXIST) break;
3897 id += 7777;
3899 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3900 temp_file_name = name;
3901 return ret;
3905 /*******************************************************************
3906 * rename_temp_file
3908 static void rename_temp_file( const char *dest )
3910 int ret = rename( temp_file_name, dest );
3911 if (ret == -1 && errno == EEXIST)
3913 /* rename doesn't overwrite on windows */
3914 unlink( dest );
3915 ret = rename( temp_file_name, dest );
3917 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3918 temp_file_name = NULL;
3922 /*******************************************************************
3923 * are_files_identical
3925 static int are_files_identical( FILE *file1, FILE *file2 )
3927 for (;;)
3929 char buffer1[8192], buffer2[8192];
3930 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3931 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3932 if (size1 != size2) return 0;
3933 if (!size1) return feof( file1 ) && feof( file2 );
3934 if (memcmp( buffer1, buffer2, size1 )) return 0;
3939 /*******************************************************************
3940 * rename_temp_file_if_changed
3942 static void rename_temp_file_if_changed( const char *dest )
3944 FILE *file1, *file2;
3945 int do_rename = 1;
3947 if ((file1 = fopen( dest, "r" )))
3949 if ((file2 = fopen( temp_file_name, "r" )))
3951 do_rename = !are_files_identical( file1, file2 );
3952 fclose( file2 );
3954 fclose( file1 );
3956 if (!do_rename)
3958 unlink( temp_file_name );
3959 temp_file_name = NULL;
3961 else rename_temp_file( dest );
3965 /*******************************************************************
3966 * output_linguas
3968 static void output_linguas( const struct makefile *make )
3970 const char *dest = base_dir_path( make, "LINGUAS" );
3971 struct incl_file *source;
3973 output_file = create_temp_file( dest );
3975 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3976 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3977 if (strendswith( source->name, ".po" ))
3978 output( "%s\n", replace_extension( source->name, ".po", "" ));
3980 if (fclose( output_file )) fatal_perror( "write" );
3981 output_file = NULL;
3982 rename_temp_file_if_changed( dest );
3986 /*******************************************************************
3987 * output_testlist
3989 static void output_testlist( const struct makefile *make )
3991 const char *dest = base_dir_path( make, "testlist.c" );
3992 struct strarray files = empty_strarray;
3993 unsigned int i;
3995 for (i = 0; i < make->ok_files.count; i++)
3996 strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
3998 output_file = create_temp_file( dest );
4000 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
4001 output( "#define WIN32_LEAN_AND_MEAN\n" );
4002 output( "#include <windows.h>\n\n" );
4003 output( "#define STANDALONE\n" );
4004 output( "#include \"wine/test.h\"\n\n" );
4006 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
4007 output( "\n" );
4008 output( "const struct test winetest_testlist[] =\n" );
4009 output( "{\n" );
4010 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
4011 output( " { 0, 0 }\n" );
4012 output( "};\n" );
4014 if (fclose( output_file )) fatal_perror( "write" );
4015 output_file = NULL;
4016 rename_temp_file_if_changed( dest );
4020 /*******************************************************************
4021 * output_gitignore
4023 static void output_gitignore( const char *dest, struct strarray files )
4025 int i;
4027 output_file = create_temp_file( dest );
4029 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4030 for (i = 0; i < files.count; i++)
4032 if (!strchr( files.str[i], '/' )) output( "/" );
4033 output( "%s\n", files.str[i] );
4036 if (fclose( output_file )) fatal_perror( "write" );
4037 output_file = NULL;
4038 rename_temp_file( dest );
4042 /*******************************************************************
4043 * output_top_variables
4045 static void output_top_variables( const struct makefile *make )
4047 unsigned int i;
4048 struct strarray *vars = &top_makefile->vars;
4050 if (!make->base_dir) return; /* don't output variables in the top makefile */
4052 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
4053 output( "all:\n\n" );
4054 for (i = 0; i < vars->count; i += 2)
4056 if (!strcmp( vars->str[i], "SUBDIRS" )) continue; /* not inherited */
4057 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
4059 output( "\n" );
4063 /*******************************************************************
4064 * output_dependencies
4066 static void output_dependencies( struct makefile *make )
4068 struct strarray ignore_files = empty_strarray;
4069 char buffer[1024];
4070 FILE *src_file;
4071 int found = 0;
4073 if (make->base_dir) create_dir( make->base_dir );
4075 output_file_name = base_dir_path( make, output_makefile_name );
4076 output_file = create_temp_file( output_file_name );
4077 output_top_variables( make );
4079 /* copy the contents of the source makefile */
4080 src_file = open_input_makefile( make );
4081 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4083 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4084 found = !strncmp( buffer, separator, strlen(separator) );
4086 if (fclose( src_file )) fatal_perror( "close" );
4087 input_file_name = NULL;
4089 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4090 output_sources( make );
4092 fclose( output_file );
4093 output_file = NULL;
4094 rename_temp_file( output_file_name );
4096 strarray_addall( &ignore_files, make->distclean_files );
4097 strarray_addall( &ignore_files, make->clean_files );
4098 if (make->testdll) output_testlist( make );
4099 if (make->base_dir && !strcmp( make->base_dir, "po" )) output_linguas( make );
4100 if (!make->src_dir) output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
4102 create_file_directories( make, ignore_files );
4104 output_file_name = NULL;
4108 /*******************************************************************
4109 * load_sources
4111 static void load_sources( struct makefile *make )
4113 static const char *source_vars[] =
4115 "SOURCES",
4116 "C_SRCS",
4117 "OBJC_SRCS",
4118 "RC_SRCS",
4119 "MC_SRCS",
4120 "IDL_SRCS",
4121 "BISON_SRCS",
4122 "LEX_SRCS",
4123 "HEADER_SRCS",
4124 "XTEMPLATE_SRCS",
4125 "SVG_SRCS",
4126 "FONT_SRCS",
4127 "IN_SRCS",
4128 "PO_SRCS",
4129 "MANPAGES",
4130 NULL
4132 const char **var;
4133 unsigned int i;
4134 struct strarray value;
4135 struct incl_file *file;
4137 if (root_src_dir)
4139 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
4140 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
4142 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
4143 strarray_set_value( &make->vars, "top_srcdir", top_src_dir_path( make, "" ));
4144 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4146 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4147 make->module = get_expanded_make_variable( make, "MODULE" );
4148 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4149 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4150 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4151 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4153 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4154 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4155 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4156 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4157 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4158 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4159 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4160 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4162 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
4164 if ((make->module && make->staticlib) || make->testdll)
4165 strarray_add( &make->extradllflags, "-mno-cygwin" );
4167 strarray_addall( &make->extradllflags, get_expanded_make_var_array( make, "APPMODE" ));
4168 make->disabled = make->base_dir && strarray_exists( &disabled_dirs, make->base_dir );
4169 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4170 make->use_msvcrt = strarray_exists( &make->extradllflags, "-mno-cygwin" );
4171 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4172 strarray_exists( &make->extradllflags, "-mwindows" );
4174 if (make->module && !make->install_lib.count && !make->install_dev.count)
4176 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4177 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4178 else strarray_add( &make->install_lib, make->module );
4181 make->include_paths = empty_strarray;
4182 make->include_args = empty_strarray;
4183 make->define_args = empty_strarray;
4184 strarray_add( &make->define_args, "-D__WINESRC__" );
4186 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4187 for (i = 0; i < value.count; i++)
4188 if (!strncmp( value.str[i], "-I", 2 ))
4189 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4190 else
4191 strarray_add_uniq( &make->define_args, value.str[i] );
4192 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4194 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4195 if (make->src_dir)
4196 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4197 if (make->parent_dir)
4198 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4199 strarray_add( &make->include_args, strmake( "-I%s", top_obj_dir_path( make, "include" )));
4200 if (make->top_src_dir)
4201 strarray_add( &make->include_args, strmake( "-I%s", top_src_dir_path( make, "include" )));
4203 list_init( &make->sources );
4204 list_init( &make->includes );
4206 for (var = source_vars; *var; var++)
4208 value = get_expanded_make_var_array( make, *var );
4209 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4212 add_generated_sources( make );
4214 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4215 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4217 if (crosstarget) make->is_cross = (make->testdll || make->use_msvcrt || !has_object_file( make ));
4219 if (make->is_cross)
4221 for (i = 0; i < make->imports.count; i++)
4222 strarray_add_uniq( &cross_import_libs, make->imports.str[i] );
4223 if (make->use_msvcrt) strarray_add_uniq( &cross_import_libs, "msvcrt" );
4224 if (make->is_win16) strarray_add_uniq( &cross_import_libs, "kernel" );
4225 strarray_add_uniq( &cross_import_libs, "winecrt0" );
4226 strarray_add_uniq( &cross_import_libs, "kernel32" );
4227 strarray_add_uniq( &cross_import_libs, "ntdll" );
4230 if (!*dll_ext || make->is_cross)
4231 for (i = 0; i < make->delayimports.count; i++)
4232 strarray_add_uniq( &delay_import_libs, make->delayimports.str[i] );
4236 /*******************************************************************
4237 * parse_makeflags
4239 static void parse_makeflags( const char *flags )
4241 const char *p = flags;
4242 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4244 while (*p)
4246 while (isspace(*p)) p++;
4247 var = buffer;
4248 while (*p && !isspace(*p))
4250 if (*p == '\\' && p[1]) p++;
4251 *var++ = *p++;
4253 *var = 0;
4254 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4259 /*******************************************************************
4260 * parse_option
4262 static int parse_option( const char *opt )
4264 if (opt[0] != '-')
4266 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4267 return 0;
4269 switch(opt[1])
4271 case 'f':
4272 if (opt[2]) output_makefile_name = opt + 2;
4273 break;
4274 case 'R':
4275 relative_dir_mode = 1;
4276 break;
4277 default:
4278 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4279 exit(1);
4281 return 1;
4285 /*******************************************************************
4286 * main
4288 int main( int argc, char *argv[] )
4290 const char *makeflags = getenv( "MAKEFLAGS" );
4291 int i, j;
4293 if (makeflags) parse_makeflags( makeflags );
4295 i = 1;
4296 while (i < argc)
4298 if (parse_option( argv[i] ))
4300 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4301 argc--;
4303 else i++;
4306 if (relative_dir_mode)
4308 char *relpath;
4310 if (argc != 3)
4312 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4313 exit( 1 );
4315 relpath = get_relative_path( argv[1], argv[2] );
4316 printf( "%s\n", relpath ? relpath : "." );
4317 exit( 0 );
4320 atexit( cleanup_files );
4321 signal( SIGTERM, exit_on_signal );
4322 signal( SIGINT, exit_on_signal );
4323 #ifdef SIGHUP
4324 signal( SIGHUP, exit_on_signal );
4325 #endif
4327 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4329 top_makefile = parse_makefile( NULL );
4331 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4332 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4333 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4334 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4335 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4336 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4337 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4338 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4339 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4340 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4341 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4343 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4344 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
4345 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
4346 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4347 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
4348 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4349 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4350 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4351 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4352 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4353 bison = get_expanded_make_variable( top_makefile, "BISON" );
4354 ar = get_expanded_make_variable( top_makefile, "AR" );
4355 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4356 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4357 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4358 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4359 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4360 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4361 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4363 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4364 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4365 if (!exe_ext) exe_ext = "";
4366 if (!tools_ext) tools_ext = "";
4367 if (!man_ext) man_ext = "3w";
4369 if (argc == 1)
4371 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4372 top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4373 top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );
4375 for (i = 0; i < top_makefile->subdirs.count; i++)
4376 top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i] );
4378 load_sources( top_makefile );
4379 for (i = 0; i < top_makefile->subdirs.count; i++)
4380 load_sources( top_makefile->submakes[i] );
4382 for (i = 0; i < top_makefile->subdirs.count; i++)
4383 output_dependencies( top_makefile->submakes[i] );
4385 output_dependencies( top_makefile );
4386 return 0;
4389 for (i = 1; i < argc; i++)
4391 struct makefile *make = parse_makefile( argv[i] );
4392 load_sources( make );
4393 output_dependencies( make );
4395 return 0;