evr: Fix typo in video_mixer_init_dxva_videodesc() (Coverity).
[wine.git] / tools / makedep.c
blob825458156fbe77da765d9b737506b07f970de57d
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 *crossdebug;
156 static const char *fontforge;
157 static const char *convert;
158 static const char *flex;
159 static const char *bison;
160 static const char *ar;
161 static const char *ranlib;
162 static const char *rsvg;
163 static const char *icotool;
164 static const char *dlltool;
165 static const char *msgfmt;
166 static const char *ln_s;
167 static const char *sed_cmd;
168 static const char *delay_load_flag;
170 struct makefile
172 /* values determined from input makefile */
173 struct strarray vars;
174 struct strarray include_paths;
175 struct strarray include_args;
176 struct strarray define_args;
177 struct strarray programs;
178 struct strarray scripts;
179 struct strarray imports;
180 struct strarray subdirs;
181 struct strarray delayimports;
182 struct strarray extradllflags;
183 struct strarray install_lib;
184 struct strarray install_dev;
185 struct strarray extra_targets;
186 struct strarray extra_imports;
187 struct list sources;
188 struct list includes;
189 const char *base_dir;
190 const char *src_dir;
191 const char *obj_dir;
192 const char *top_src_dir;
193 const char *top_obj_dir;
194 const char *parent_dir;
195 const char *module;
196 const char *testdll;
197 const char *sharedlib;
198 const char *staticlib;
199 const char *staticimplib;
200 const char *importlib;
201 int disabled;
202 int use_msvcrt;
203 int is_cross;
204 int is_win16;
205 int is_exe;
206 struct makefile **submakes;
208 /* values generated at output time */
209 struct strarray in_files;
210 struct strarray ok_files;
211 struct strarray clean_files;
212 struct strarray distclean_files;
213 struct strarray uninstall_files;
214 struct strarray object_files;
215 struct strarray crossobj_files;
216 struct strarray unixobj_files;
217 struct strarray res_files;
218 struct strarray c2man_files;
219 struct strarray debug_files;
220 struct strarray dlldata_files;
221 struct strarray implib_objs;
222 struct strarray all_targets;
223 struct strarray phony_targets;
224 struct strarray dependencies;
225 struct strarray install_rules[NB_INSTALL_RULES];
228 static struct makefile *top_makefile;
230 static const char separator[] = "### Dependencies";
231 static const char *output_makefile_name = "Makefile";
232 static const char *input_file_name;
233 static const char *output_file_name;
234 static const char *temp_file_name;
235 static int relative_dir_mode;
236 static int input_line;
237 static int output_column;
238 static FILE *output_file;
240 static const char Usage[] =
241 "Usage: makedep [options] [directories]\n"
242 "Options:\n"
243 " -R from to Compute the relative path between two directories\n"
244 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
247 #ifndef __GNUC__
248 #define __attribute__(x)
249 #endif
251 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
252 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
253 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
254 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
256 /*******************************************************************
257 * fatal_error
259 static void fatal_error( const char *msg, ... )
261 va_list valist;
262 va_start( valist, msg );
263 if (input_file_name)
265 fprintf( stderr, "%s:", input_file_name );
266 if (input_line) fprintf( stderr, "%d:", input_line );
267 fprintf( stderr, " error: " );
269 else fprintf( stderr, "makedep: error: " );
270 vfprintf( stderr, msg, valist );
271 va_end( valist );
272 exit(1);
276 /*******************************************************************
277 * fatal_perror
279 static void fatal_perror( const char *msg, ... )
281 va_list valist;
282 va_start( valist, msg );
283 if (input_file_name)
285 fprintf( stderr, "%s:", input_file_name );
286 if (input_line) fprintf( stderr, "%d:", input_line );
287 fprintf( stderr, " error: " );
289 else fprintf( stderr, "makedep: error: " );
290 vfprintf( stderr, msg, valist );
291 perror( " " );
292 va_end( valist );
293 exit(1);
297 /*******************************************************************
298 * cleanup_files
300 static void cleanup_files(void)
302 if (temp_file_name) unlink( temp_file_name );
303 if (output_file_name) unlink( output_file_name );
307 /*******************************************************************
308 * exit_on_signal
310 static void exit_on_signal( int sig )
312 exit( 1 ); /* this will call the atexit functions */
316 /*******************************************************************
317 * xmalloc
319 static void *xmalloc( size_t size )
321 void *res;
322 if (!(res = malloc (size ? size : 1)))
323 fatal_error( "Virtual memory exhausted.\n" );
324 return res;
328 /*******************************************************************
329 * xrealloc
331 static void *xrealloc (void *ptr, size_t size)
333 void *res;
334 assert( size );
335 if (!(res = realloc( ptr, size )))
336 fatal_error( "Virtual memory exhausted.\n" );
337 return res;
340 /*******************************************************************
341 * xstrdup
343 static char *xstrdup( const char *str )
345 char *res = strdup( str );
346 if (!res) fatal_error( "Virtual memory exhausted.\n" );
347 return res;
351 /*******************************************************************
352 * strmake
354 static char *strmake( const char* fmt, ... )
356 int n;
357 size_t size = 100;
358 va_list ap;
360 for (;;)
362 char *p = xmalloc (size);
363 va_start(ap, fmt);
364 n = vsnprintf (p, size, fmt, ap);
365 va_end(ap);
366 if (n == -1) size *= 2;
367 else if ((size_t)n >= size) size = n + 1;
368 else return xrealloc( p, n + 1 );
369 free(p);
374 /*******************************************************************
375 * strendswith
377 static int strendswith( const char* str, const char* end )
379 size_t l = strlen( str );
380 size_t m = strlen( end );
382 return l >= m && strcmp(str + l - m, end) == 0;
386 /*******************************************************************
387 * output
389 static void output( const char *format, ... )
391 int ret;
392 va_list valist;
394 va_start( valist, format );
395 ret = vfprintf( output_file, format, valist );
396 va_end( valist );
397 if (ret < 0) fatal_perror( "output" );
398 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
399 else output_column += ret;
403 /*******************************************************************
404 * strarray_add
406 static void strarray_add( struct strarray *array, const char *str )
408 if (array->count == array->size)
410 if (array->size) array->size *= 2;
411 else array->size = 16;
412 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
414 array->str[array->count++] = str;
418 /*******************************************************************
419 * strarray_addall
421 static void strarray_addall( struct strarray *array, struct strarray added )
423 unsigned int i;
425 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
429 /*******************************************************************
430 * strarray_exists
432 static int strarray_exists( const struct strarray *array, const char *str )
434 unsigned int i;
436 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
437 return 0;
441 /*******************************************************************
442 * strarray_add_uniq
444 static void strarray_add_uniq( struct strarray *array, const char *str )
446 if (!strarray_exists( array, str )) strarray_add( array, str );
450 /*******************************************************************
451 * strarray_addall_uniq
453 static void strarray_addall_uniq( struct strarray *array, struct strarray added )
455 unsigned int i;
457 for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
461 /*******************************************************************
462 * strarray_get_value
464 * Find a value in a name/value pair string array.
466 static const char *strarray_get_value( const struct strarray *array, const char *name )
468 int pos, res, min = 0, max = array->count / 2 - 1;
470 while (min <= max)
472 pos = (min + max) / 2;
473 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
474 if (res < 0) min = pos + 1;
475 else max = pos - 1;
477 return NULL;
481 /*******************************************************************
482 * strarray_set_value
484 * Define a value in a name/value pair string array.
486 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
488 int i, pos, res, min = 0, max = array->count / 2 - 1;
490 while (min <= max)
492 pos = (min + max) / 2;
493 if (!(res = strcmp( array->str[pos * 2], name )))
495 /* redefining a variable replaces the previous value */
496 array->str[pos * 2 + 1] = value;
497 return;
499 if (res < 0) min = pos + 1;
500 else max = pos - 1;
502 strarray_add( array, NULL );
503 strarray_add( array, NULL );
504 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
505 array->str[min * 2] = name;
506 array->str[min * 2 + 1] = value;
510 /*******************************************************************
511 * strarray_set_qsort
513 static void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
515 if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
519 /*******************************************************************
520 * output_filename
522 static void output_filename( const char *name )
524 if (output_column + strlen(name) + 1 > 100)
526 output( " \\\n" );
527 output( " " );
529 else if (output_column) output( " " );
530 output( "%s", name );
534 /*******************************************************************
535 * output_filenames
537 static void output_filenames( struct strarray array )
539 unsigned int i;
541 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
545 /*******************************************************************
546 * output_rm_filenames
548 static void output_rm_filenames( struct strarray array )
550 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
551 unsigned int i, len;
553 if (!array.count) return;
554 output( "\trm -f" );
555 for (i = len = 0; i < array.count; i++)
557 if (len > max_cmdline)
559 output( "\n" );
560 output( "\trm -f" );
561 len = 0;
563 output_filename( array.str[i] );
564 len += strlen( array.str[i] ) + 1;
566 output( "\n" );
570 /*******************************************************************
571 * get_extension
573 static char *get_extension( char *filename )
575 char *ext = strrchr( filename, '.' );
576 if (ext && strchr( ext, '/' )) ext = NULL;
577 return ext;
581 /*******************************************************************
582 * get_base_name
584 static const char *get_base_name( const char *name )
586 char *base;
587 if (!strchr( name, '.' )) return name;
588 base = strdup( name );
589 *strrchr( base, '.' ) = 0;
590 return base;
594 /*******************************************************************
595 * replace_extension
597 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
599 char *ret;
600 size_t name_len = strlen( name );
601 size_t ext_len = strlen( old_ext );
603 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
604 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
605 memcpy( ret, name, name_len );
606 strcpy( ret + name_len, new_ext );
607 return ret;
611 /*******************************************************************
612 * replace_filename
614 static char *replace_filename( const char *path, const char *name )
616 const char *p;
617 char *ret;
618 size_t len;
620 if (!path) return xstrdup( name );
621 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
622 len = p - path + 1;
623 ret = xmalloc( len + strlen( name ) + 1 );
624 memcpy( ret, path, len );
625 strcpy( ret + len, name );
626 return ret;
630 /*******************************************************************
631 * strarray_replace_extension
633 static struct strarray strarray_replace_extension( const struct strarray *array,
634 const char *old_ext, const char *new_ext )
636 unsigned int i;
637 struct strarray ret;
639 ret.count = ret.size = array->count;
640 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
641 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
642 return ret;
646 /*******************************************************************
647 * replace_substr
649 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
651 size_t pos = start - str;
652 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
653 memcpy( ret, str, pos );
654 strcpy( ret + pos, replace );
655 strcat( ret + pos, start + len );
656 return ret;
660 /*******************************************************************
661 * get_relative_path
663 * Determine where the destination path is located relative to the 'from' path.
665 static char *get_relative_path( const char *from, const char *dest )
667 const char *start;
668 char *ret, *p;
669 unsigned int dotdots = 0;
671 /* a path of "." is equivalent to an empty path */
672 if (!strcmp( from, "." )) from = "";
674 for (;;)
676 while (*from == '/') from++;
677 while (*dest == '/') dest++;
678 start = dest; /* save start of next path element */
679 if (!*from) break;
681 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
682 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
684 /* count remaining elements in 'from' */
687 dotdots++;
688 while (*from && *from != '/') from++;
689 while (*from == '/') from++;
691 while (*from);
692 break;
695 if (!start[0] && !dotdots) return NULL; /* empty path */
697 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
698 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
700 if (start[0]) strcpy( p, start );
701 else p[-1] = 0; /* remove trailing slash */
702 return ret;
706 /*******************************************************************
707 * concat_paths
709 static char *concat_paths( const char *base, const char *path )
711 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
712 if (!path || !path[0]) return xstrdup( base );
713 if (path[0] == '/') return xstrdup( path );
714 return strmake( "%s/%s", base, path );
718 /*******************************************************************
719 * base_dir_path
721 static char *base_dir_path( const struct makefile *make, const char *path )
723 return concat_paths( make->base_dir, path );
727 /*******************************************************************
728 * obj_dir_path
730 static char *obj_dir_path( const struct makefile *make, const char *path )
732 return concat_paths( make->obj_dir, path );
736 /*******************************************************************
737 * src_dir_path
739 static char *src_dir_path( const struct makefile *make, const char *path )
741 if (make->src_dir) return concat_paths( make->src_dir, path );
742 return obj_dir_path( make, path );
746 /*******************************************************************
747 * top_obj_dir_path
749 static char *top_obj_dir_path( const struct makefile *make, const char *path )
751 return concat_paths( make->top_obj_dir, path );
755 /*******************************************************************
756 * top_src_dir_path
758 static char *top_src_dir_path( const struct makefile *make, const char *path )
760 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
761 return top_obj_dir_path( make, path );
765 /*******************************************************************
766 * root_dir_path
768 static char *root_dir_path( const char *path )
770 return concat_paths( root_src_dir, path );
774 /*******************************************************************
775 * tools_dir_path
777 static char *tools_dir_path( const struct makefile *make, const char *path )
779 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
780 return top_obj_dir_path( make, strmake( "tools/%s", path ));
784 /*******************************************************************
785 * tools_path
787 static char *tools_path( const struct makefile *make, const char *name )
789 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
793 /*******************************************************************
794 * get_line
796 static char *get_line( FILE *file )
798 static char *buffer;
799 static size_t size;
801 if (!size)
803 size = 1024;
804 buffer = xmalloc( size );
806 if (!fgets( buffer, size, file )) return NULL;
807 input_line++;
809 for (;;)
811 char *p = buffer + strlen(buffer);
812 /* if line is larger than buffer, resize buffer */
813 while (p == buffer + size - 1 && p[-1] != '\n')
815 buffer = xrealloc( buffer, size * 2 );
816 if (!fgets( buffer + size - 1, size + 1, file )) break;
817 p = buffer + strlen(buffer);
818 size *= 2;
820 if (p > buffer && p[-1] == '\n')
822 *(--p) = 0;
823 if (p > buffer && p[-1] == '\r') *(--p) = 0;
824 if (p > buffer && p[-1] == '\\')
826 *(--p) = 0;
827 /* line ends in backslash, read continuation line */
828 if (!fgets( p, size - (p - buffer), file )) return buffer;
829 input_line++;
830 continue;
833 return buffer;
838 /*******************************************************************
839 * hash_filename
841 static unsigned int hash_filename( const char *name )
843 /* FNV-1 hash */
844 unsigned int ret = 2166136261u;
845 while (*name) ret = (ret * 16777619) ^ *name++;
846 return ret % HASH_SIZE;
850 /*******************************************************************
851 * add_file
853 static struct file *add_file( const char *name )
855 struct file *file = xmalloc( sizeof(*file) );
856 memset( file, 0, sizeof(*file) );
857 file->name = xstrdup( name );
858 return file;
862 /*******************************************************************
863 * add_dependency
865 static void add_dependency( struct file *file, const char *name, enum incl_type type )
867 /* enforce some rules for the Wine tree */
869 if (!memcmp( name, "../", 3 ))
870 fatal_error( "#include directive with relative path not allowed\n" );
872 if (!strcmp( name, "config.h" ))
874 if (strendswith( file->name, ".h" ))
875 fatal_error( "config.h must not be included by a header file\n" );
876 if (file->deps_count)
877 fatal_error( "config.h must be included before anything else\n" );
879 else if (!strcmp( name, "wine/port.h" ))
881 if (strendswith( file->name, ".h" ))
882 fatal_error( "wine/port.h must not be included by a header file\n" );
883 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
884 if (file->deps_count > 1)
885 fatal_error( "wine/port.h must be included before everything except config.h\n" );
886 if (strcmp( file->deps[0].name, "config.h" ))
887 fatal_error( "config.h must be included before wine/port.h\n" );
890 if (file->deps_count >= file->deps_size)
892 file->deps_size *= 2;
893 if (file->deps_size < 16) file->deps_size = 16;
894 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
896 file->deps[file->deps_count].line = input_line;
897 file->deps[file->deps_count].type = type;
898 file->deps[file->deps_count].name = xstrdup( name );
899 file->deps_count++;
903 /*******************************************************************
904 * find_src_file
906 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
908 struct incl_file *file;
910 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
911 if (!strcmp( name, file->name )) return file;
912 return NULL;
915 /*******************************************************************
916 * find_include_file
918 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
920 struct incl_file *file;
922 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
923 if (!strcmp( name, file->name )) return file;
924 return NULL;
927 /*******************************************************************
928 * add_include
930 * Add an include file if it doesn't already exists.
932 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
933 const char *name, int line, enum incl_type type )
935 struct incl_file *include;
937 if (parent->files_count >= parent->files_size)
939 parent->files_size *= 2;
940 if (parent->files_size < 16) parent->files_size = 16;
941 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
944 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
945 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
946 goto found;
948 include = xmalloc( sizeof(*include) );
949 memset( include, 0, sizeof(*include) );
950 include->name = xstrdup(name);
951 include->included_by = parent;
952 include->included_line = line;
953 include->type = type;
954 include->use_msvcrt = parent->use_msvcrt;
955 list_add_tail( &make->includes, &include->entry );
956 found:
957 parent->files[parent->files_count++] = include;
958 return include;
962 /*******************************************************************
963 * add_generated_source
965 * Add a generated source file to the list.
967 static struct incl_file *add_generated_source( struct makefile *make,
968 const char *name, const char *filename )
970 struct incl_file *file;
972 if ((file = find_src_file( make, name ))) return file; /* we already have it */
973 file = xmalloc( sizeof(*file) );
974 memset( file, 0, sizeof(*file) );
975 file->file = add_file( name );
976 file->name = xstrdup( name );
977 file->filename = obj_dir_path( make, filename ? filename : name );
978 file->file->flags = FLAG_GENERATED;
979 file->use_msvcrt = make->use_msvcrt;
980 list_add_tail( &make->sources, &file->entry );
981 return file;
985 /*******************************************************************
986 * parse_include_directive
988 static void parse_include_directive( struct file *source, char *str )
990 char quote, *include, *p = str;
992 while (*p && isspace(*p)) p++;
993 if (*p != '\"' && *p != '<' ) return;
994 quote = *p++;
995 if (quote == '<') quote = '>';
996 include = p;
997 while (*p && (*p != quote)) p++;
998 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
999 *p = 0;
1000 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1004 /*******************************************************************
1005 * parse_pragma_directive
1007 static void parse_pragma_directive( struct file *source, char *str )
1009 char *flag, *p = str;
1011 if (!isspace( *p )) return;
1012 while (*p && isspace(*p)) p++;
1013 p = strtok( p, " \t" );
1014 if (strcmp( p, "makedep" )) return;
1016 while ((flag = strtok( NULL, " \t" )))
1018 if (!strcmp( flag, "depend" ))
1020 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
1021 return;
1023 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
1025 if (strendswith( source->name, ".idl" ))
1027 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
1028 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
1029 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
1030 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
1031 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
1032 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
1033 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
1034 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
1036 else if (strendswith( source->name, ".rc" ))
1038 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
1040 else if (strendswith( source->name, ".sfd" ))
1042 if (!strcmp( flag, "font" ))
1044 struct strarray *array = source->args;
1046 if (!array)
1048 source->args = array = xmalloc( sizeof(*array) );
1049 *array = empty_strarray;
1050 source->flags |= FLAG_SFD_FONTS;
1052 strarray_add( array, xstrdup( strtok( NULL, "" )));
1053 return;
1056 else
1058 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
1059 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
1065 /*******************************************************************
1066 * parse_cpp_directive
1068 static void parse_cpp_directive( struct file *source, char *str )
1070 while (*str && isspace(*str)) str++;
1071 if (*str++ != '#') return;
1072 while (*str && isspace(*str)) str++;
1074 if (!strncmp( str, "include", 7 ))
1075 parse_include_directive( source, str + 7 );
1076 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
1077 parse_include_directive( source, str + 6 );
1078 else if (!strncmp( str, "pragma", 6 ))
1079 parse_pragma_directive( source, str + 6 );
1083 /*******************************************************************
1084 * parse_idl_file
1086 static void parse_idl_file( struct file *source, FILE *file )
1088 char *buffer, *include;
1090 input_line = 0;
1092 while ((buffer = get_line( file )))
1094 char quote;
1095 char *p = buffer;
1096 while (*p && isspace(*p)) p++;
1098 if (!strncmp( p, "importlib", 9 ))
1100 p += 9;
1101 while (*p && isspace(*p)) p++;
1102 if (*p++ != '(') continue;
1103 while (*p && isspace(*p)) p++;
1104 if (*p++ != '"') continue;
1105 include = p;
1106 while (*p && (*p != '"')) p++;
1107 if (!*p) fatal_error( "malformed importlib directive\n" );
1108 *p = 0;
1109 add_dependency( source, include, INCL_IMPORTLIB );
1110 continue;
1113 if (!strncmp( p, "import", 6 ))
1115 p += 6;
1116 while (*p && isspace(*p)) p++;
1117 if (*p != '"') continue;
1118 include = ++p;
1119 while (*p && (*p != '"')) p++;
1120 if (!*p) fatal_error( "malformed import directive\n" );
1121 *p = 0;
1122 add_dependency( source, include, INCL_IMPORT );
1123 continue;
1126 /* check for #include inside cpp_quote */
1127 if (!strncmp( p, "cpp_quote", 9 ))
1129 p += 9;
1130 while (*p && isspace(*p)) p++;
1131 if (*p++ != '(') continue;
1132 while (*p && isspace(*p)) p++;
1133 if (*p++ != '"') continue;
1134 if (*p++ != '#') continue;
1135 while (*p && isspace(*p)) p++;
1136 if (strncmp( p, "include", 7 )) continue;
1137 p += 7;
1138 while (*p && isspace(*p)) p++;
1139 if (*p == '\\' && p[1] == '"')
1141 p += 2;
1142 quote = '"';
1144 else
1146 if (*p++ != '<' ) continue;
1147 quote = '>';
1149 include = p;
1150 while (*p && (*p != quote)) p++;
1151 if (!*p || (quote == '"' && p[-1] != '\\'))
1152 fatal_error( "malformed #include directive inside cpp_quote\n" );
1153 if (quote == '"') p--; /* remove backslash */
1154 *p = 0;
1155 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1156 continue;
1159 parse_cpp_directive( source, p );
1163 /*******************************************************************
1164 * parse_c_file
1166 static void parse_c_file( struct file *source, FILE *file )
1168 char *buffer;
1170 input_line = 0;
1171 while ((buffer = get_line( file )))
1173 parse_cpp_directive( source, buffer );
1178 /*******************************************************************
1179 * parse_rc_file
1181 static void parse_rc_file( struct file *source, FILE *file )
1183 char *buffer, *include;
1185 input_line = 0;
1186 while ((buffer = get_line( file )))
1188 char quote;
1189 char *p = buffer;
1190 while (*p && isspace(*p)) p++;
1192 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1194 p += 2;
1195 while (*p && isspace(*p)) p++;
1196 if (strncmp( p, "@makedep:", 9 )) continue;
1197 p += 9;
1198 while (*p && isspace(*p)) p++;
1199 quote = '"';
1200 if (*p == quote)
1202 include = ++p;
1203 while (*p && *p != quote) p++;
1205 else
1207 include = p;
1208 while (*p && !isspace(*p) && *p != '*') p++;
1210 if (!*p)
1211 fatal_error( "malformed makedep comment\n" );
1212 *p = 0;
1213 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1214 continue;
1217 parse_cpp_directive( source, buffer );
1222 /*******************************************************************
1223 * parse_in_file
1225 static void parse_in_file( struct file *source, FILE *file )
1227 char *p, *buffer;
1229 /* make sure it gets rebuilt when the version changes */
1230 add_dependency( source, "config.h", INCL_SYSTEM );
1232 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1234 input_line = 0;
1235 while ((buffer = get_line( file )))
1237 if (strncmp( buffer, ".TH", 3 )) continue;
1238 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1239 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1240 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1241 source->args = xstrdup( p );
1242 return;
1247 /*******************************************************************
1248 * parse_sfd_file
1250 static void parse_sfd_file( struct file *source, FILE *file )
1252 char *p, *eol, *buffer;
1254 input_line = 0;
1255 while ((buffer = get_line( file )))
1257 if (strncmp( buffer, "UComments:", 10 )) continue;
1258 p = buffer + 10;
1259 while (*p == ' ') p++;
1260 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1262 p++;
1263 buffer[strlen(buffer) - 1] = 0;
1265 while ((eol = strstr( p, "+AAoA" )))
1267 *eol = 0;
1268 while (*p && isspace(*p)) p++;
1269 if (*p++ == '#')
1271 while (*p && isspace(*p)) p++;
1272 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1274 p = eol + 5;
1276 while (*p && isspace(*p)) p++;
1277 if (*p++ != '#') return;
1278 while (*p && isspace(*p)) p++;
1279 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1280 return;
1285 static const struct
1287 const char *ext;
1288 void (*parse)( struct file *file, FILE *f );
1289 } parse_functions[] =
1291 { ".c", parse_c_file },
1292 { ".h", parse_c_file },
1293 { ".inl", parse_c_file },
1294 { ".l", parse_c_file },
1295 { ".m", parse_c_file },
1296 { ".rh", parse_c_file },
1297 { ".x", parse_c_file },
1298 { ".y", parse_c_file },
1299 { ".idl", parse_idl_file },
1300 { ".rc", parse_rc_file },
1301 { ".in", parse_in_file },
1302 { ".sfd", parse_sfd_file }
1305 /*******************************************************************
1306 * load_file
1308 static struct file *load_file( const char *name )
1310 struct file *file;
1311 FILE *f;
1312 unsigned int i, hash = hash_filename( name );
1314 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1315 if (!strcmp( name, file->name )) return file;
1317 if (!(f = fopen( name, "r" ))) return NULL;
1319 file = add_file( name );
1320 list_add_tail( &files[hash], &file->entry );
1321 input_file_name = file->name;
1322 input_line = 0;
1324 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1326 if (!strendswith( name, parse_functions[i].ext )) continue;
1327 parse_functions[i].parse( file, f );
1328 break;
1331 fclose( f );
1332 input_file_name = NULL;
1334 return file;
1338 /*******************************************************************
1339 * open_include_path_file
1341 * Open a file from a directory on the include path.
1343 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1344 const char *name, char **filename )
1346 char *src_path = base_dir_path( make, concat_paths( dir, name ));
1347 struct file *ret = load_file( src_path );
1349 if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
1350 return ret;
1354 /*******************************************************************
1355 * open_file_same_dir
1357 * Open a file in the same directory as the parent.
1359 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1361 char *src_path = replace_filename( parent->file->name, name );
1362 struct file *ret = load_file( src_path );
1364 if (ret) *filename = replace_filename( parent->filename, name );
1365 free( src_path );
1366 return ret;
1370 /*******************************************************************
1371 * open_local_file
1373 * Open a file in the source directory of the makefile.
1375 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1377 char *src_path = root_dir_path( base_dir_path( make, path ));
1378 struct file *ret = load_file( src_path );
1380 /* if not found, try parent dir */
1381 if (!ret && make->parent_dir)
1383 free( src_path );
1384 path = strmake( "%s/%s", make->parent_dir, path );
1385 src_path = root_dir_path( base_dir_path( make, path ));
1386 ret = load_file( src_path );
1387 if (ret) ret->flags |= FLAG_PARENTDIR;
1390 if (ret) *filename = src_dir_path( make, path );
1391 free( src_path );
1392 return ret;
1396 /*******************************************************************
1397 * open_global_file
1399 * Open a file in the top-level source directory.
1401 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1403 char *src_path = root_dir_path( path );
1404 struct file *ret = load_file( src_path );
1406 if (ret) *filename = top_src_dir_path( make, path );
1407 free( src_path );
1408 return ret;
1412 /*******************************************************************
1413 * open_global_header
1415 * Open a file in the global include source directory.
1417 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1419 return open_global_file( make, strmake( "include/%s", path ), filename );
1423 /*******************************************************************
1424 * open_src_file
1426 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1428 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1430 if (!file) fatal_perror( "open %s", pFile->name );
1431 return file;
1435 /*******************************************************************
1436 * open_include_file
1438 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1440 struct file *file = NULL;
1441 char *filename;
1442 unsigned int i, len;
1444 errno = ENOENT;
1446 /* check for generated bison header */
1448 if (strendswith( pFile->name, ".tab.h" ) &&
1449 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1451 pFile->sourcename = filename;
1452 pFile->filename = obj_dir_path( make, pFile->name );
1453 return file;
1456 /* check for corresponding idl file in source dir */
1458 if (strendswith( pFile->name, ".h" ) &&
1459 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1461 pFile->sourcename = filename;
1462 pFile->filename = obj_dir_path( make, pFile->name );
1463 return file;
1466 /* check for corresponding tlb file in source dir */
1468 if (strendswith( pFile->name, ".tlb" ) &&
1469 (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1471 pFile->sourcename = filename;
1472 pFile->filename = obj_dir_path( make, pFile->name );
1473 return file;
1476 /* check for extra targets */
1477 if (strarray_exists( &make->extra_targets, pFile->name ))
1479 pFile->sourcename = filename;
1480 pFile->filename = obj_dir_path( make, pFile->name );
1481 return NULL;
1484 /* now try in source dir */
1485 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1487 /* check for corresponding idl file in global includes */
1489 if (strendswith( pFile->name, ".h" ) &&
1490 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1492 pFile->sourcename = filename;
1493 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1494 return file;
1497 /* check for corresponding .in file in global includes (for config.h.in) */
1499 if (strendswith( pFile->name, ".h" ) &&
1500 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1502 pFile->sourcename = filename;
1503 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1504 return file;
1507 /* check for corresponding .x file in global includes */
1509 if (strendswith( pFile->name, "tmpl.h" ) &&
1510 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1512 pFile->sourcename = filename;
1513 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1514 return file;
1517 /* check for corresponding .tlb file in global includes */
1519 if (strendswith( pFile->name, ".tlb" ) &&
1520 (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1522 pFile->sourcename = filename;
1523 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1524 return file;
1527 /* check in global includes source dir */
1529 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1531 /* check in global msvcrt includes */
1532 if (pFile->use_msvcrt &&
1533 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1534 return file;
1536 /* now search in include paths */
1537 for (i = 0; i < make->include_paths.count; i++)
1539 const char *dir = make->include_paths.str[i];
1540 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1542 if (prefix)
1544 len = strlen( prefix );
1545 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1547 while (dir[len] == '/') len++;
1548 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1549 if (file) return file;
1551 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1553 if (*dir != '/')
1555 if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1556 return file;
1560 if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt)
1562 if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
1563 fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
1564 pFile->included_by->file->name, pFile->included_line, pFile->name );
1565 exit(1);
1568 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1570 /* try in src file directory */
1571 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1573 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1574 perror( pFile->name );
1575 pFile = pFile->included_by;
1576 while (pFile && pFile->included_by)
1578 const char *parent = pFile->included_by->sourcename;
1579 if (!parent) parent = pFile->included_by->file->name;
1580 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1581 parent, pFile->included_line, pFile->name );
1582 pFile = pFile->included_by;
1584 exit(1);
1588 /*******************************************************************
1589 * add_all_includes
1591 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1593 unsigned int i;
1595 parent->files_count = 0;
1596 parent->files_size = file->deps_count;
1597 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1598 for (i = 0; i < file->deps_count; i++)
1600 switch (file->deps[i].type)
1602 case INCL_NORMAL:
1603 case INCL_IMPORT:
1604 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1605 break;
1606 case INCL_IMPORTLIB:
1607 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1608 break;
1609 case INCL_SYSTEM:
1610 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1611 break;
1612 case INCL_CPP_QUOTE:
1613 case INCL_CPP_QUOTE_SYSTEM:
1614 break;
1620 /*******************************************************************
1621 * parse_file
1623 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1625 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1627 if (!file) return;
1629 source->file = file;
1630 source->files_count = 0;
1631 source->files_size = file->deps_count;
1632 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1633 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1634 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1636 if (source->sourcename)
1638 if (strendswith( source->sourcename, ".idl" ))
1640 unsigned int i;
1642 if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
1644 /* generated .h file always includes these */
1645 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1646 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1647 for (i = 0; i < file->deps_count; i++)
1649 switch (file->deps[i].type)
1651 case INCL_IMPORT:
1652 if (strendswith( file->deps[i].name, ".idl" ))
1653 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1654 file->deps[i].line, INCL_NORMAL );
1655 else
1656 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1657 break;
1658 case INCL_CPP_QUOTE:
1659 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1660 break;
1661 case INCL_CPP_QUOTE_SYSTEM:
1662 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1663 break;
1664 case INCL_NORMAL:
1665 case INCL_SYSTEM:
1666 case INCL_IMPORTLIB:
1667 break;
1670 return;
1672 if (strendswith( source->sourcename, ".y" ))
1673 return; /* generated .tab.h doesn't include anything */
1676 add_all_includes( make, source, file );
1680 /*******************************************************************
1681 * add_src_file
1683 * Add a source file to the list.
1685 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1687 struct incl_file *file;
1689 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1690 file = xmalloc( sizeof(*file) );
1691 memset( file, 0, sizeof(*file) );
1692 file->name = xstrdup(name);
1693 file->use_msvcrt = make->use_msvcrt;
1694 list_add_tail( &make->sources, &file->entry );
1695 parse_file( make, file, 1 );
1696 return file;
1700 /*******************************************************************
1701 * open_input_makefile
1703 static FILE *open_input_makefile( const struct makefile *make )
1705 FILE *ret;
1707 if (make->base_dir)
1708 input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
1709 else
1710 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1712 input_line = 0;
1713 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1714 return ret;
1718 /*******************************************************************
1719 * get_make_variable
1721 static const char *get_make_variable( const struct makefile *make, const char *name )
1723 const char *ret;
1725 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1726 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1727 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1728 return NULL;
1732 /*******************************************************************
1733 * get_expanded_make_variable
1735 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1737 const char *var;
1738 char *p, *end, *expand, *tmp;
1740 var = get_make_variable( make, name );
1741 if (!var) return NULL;
1743 p = expand = xstrdup( var );
1744 while ((p = strchr( p, '$' )))
1746 if (p[1] == '(')
1748 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1749 *end++ = 0;
1750 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1751 var = get_make_variable( make, p + 2 );
1752 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1753 /* switch to the new string */
1754 p = tmp + (p - expand);
1755 free( expand );
1756 expand = tmp;
1758 else if (p[1] == '{') /* don't expand ${} variables */
1760 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1761 p = end + 1;
1763 else if (p[1] == '$')
1765 p += 2;
1767 else fatal_error( "syntax error in '%s'\n", expand );
1770 /* consider empty variables undefined */
1771 p = expand;
1772 while (*p && isspace(*p)) p++;
1773 if (*p) return expand;
1774 free( expand );
1775 return NULL;
1779 /*******************************************************************
1780 * get_expanded_make_var_array
1782 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1784 struct strarray ret = empty_strarray;
1785 char *value, *token;
1787 if ((value = get_expanded_make_variable( make, name )))
1788 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1789 strarray_add( &ret, token );
1790 return ret;
1794 /*******************************************************************
1795 * get_expanded_file_local_var
1797 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1798 const char *name )
1800 char *p, *var = strmake( "%s_%s", file, name );
1802 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1803 return get_expanded_make_var_array( make, var );
1807 /*******************************************************************
1808 * set_make_variable
1810 static int set_make_variable( struct strarray *array, const char *assignment )
1812 char *p, *name;
1814 p = name = xstrdup( assignment );
1815 while (isalnum(*p) || *p == '_') p++;
1816 if (name == p) return 0; /* not a variable */
1817 if (isspace(*p))
1819 *p++ = 0;
1820 while (isspace(*p)) p++;
1822 if (*p != '=') return 0; /* not an assignment */
1823 *p++ = 0;
1824 while (isspace(*p)) p++;
1826 strarray_set_value( array, name, p );
1827 return 1;
1831 /*******************************************************************
1832 * parse_makefile
1834 static struct makefile *parse_makefile( const char *path )
1836 char *buffer;
1837 FILE *file;
1838 struct makefile *make = xmalloc( sizeof(*make) );
1840 memset( make, 0, sizeof(*make) );
1841 if (path)
1843 make->top_obj_dir = get_relative_path( path, "" );
1844 make->base_dir = path;
1845 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1848 file = open_input_makefile( make );
1849 while ((buffer = get_line( file )))
1851 if (!strncmp( buffer, separator, strlen(separator) )) break;
1852 if (*buffer == '\t') continue; /* command */
1853 while (isspace( *buffer )) buffer++;
1854 if (*buffer == '#') continue; /* comment */
1855 set_make_variable( &make->vars, buffer );
1857 fclose( file );
1858 input_file_name = NULL;
1859 return make;
1863 /*******************************************************************
1864 * add_generated_sources
1866 static void add_generated_sources( struct makefile *make )
1868 unsigned int i;
1869 struct incl_file *source, *next, *file;
1870 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1872 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1874 if (source->file->flags & FLAG_IDL_CLIENT)
1876 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1877 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1878 add_all_includes( make, file, file->file );
1880 if (source->file->flags & FLAG_IDL_SERVER)
1882 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1883 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1884 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1885 add_all_includes( make, file, file->file );
1887 if (source->file->flags & FLAG_IDL_IDENT)
1889 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1890 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1891 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1892 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1893 add_all_includes( make, file, file->file );
1895 if (source->file->flags & FLAG_IDL_PROXY)
1897 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1898 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1899 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1900 add_all_includes( make, file, file->file );
1901 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1902 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1903 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1904 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1905 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1906 add_all_includes( make, file, file->file );
1908 if (source->file->flags & FLAG_IDL_TYPELIB)
1910 add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
1912 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1914 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1916 if (source->file->flags & FLAG_IDL_REGISTER)
1918 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1920 if (source->file->flags & FLAG_IDL_HEADER)
1922 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1924 if (!source->file->flags && strendswith( source->name, ".idl" ))
1926 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1928 if (strendswith( source->name, ".x" ))
1930 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1932 if (strendswith( source->name, ".y" ))
1934 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1935 /* steal the includes list from the source file */
1936 file->files_count = source->files_count;
1937 file->files_size = source->files_size;
1938 file->files = source->files;
1939 source->files_count = source->files_size = 0;
1940 source->files = NULL;
1942 if (strendswith( source->name, ".l" ))
1944 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1945 /* steal the includes list from the source file */
1946 file->files_count = source->files_count;
1947 file->files_size = source->files_size;
1948 file->files = source->files;
1949 source->files_count = source->files_size = 0;
1950 source->files = NULL;
1952 if (source->file->flags & FLAG_C_IMPLIB)
1954 if (!make->staticimplib && make->importlib && *dll_ext)
1955 make->staticimplib = strmake( "lib%s.a", make->importlib );
1957 if (strendswith( source->name, ".po" ))
1959 if (!make->disabled)
1960 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1962 if (strendswith( source->name, ".spec" ))
1964 char *obj = replace_extension( source->name, ".spec", "" );
1965 strarray_addall_uniq( &make->extra_imports,
1966 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1969 if (make->testdll)
1971 file = add_generated_source( make, "testlist.o", "testlist.c" );
1972 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1973 add_all_includes( make, file, file->file );
1975 for (i = 0; i < objs.count; i++)
1977 /* default to .c for unknown extra object files */
1978 if (strendswith( objs.str[i], ".o" ))
1980 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1981 if (make->module || make->staticlib) file->file->flags |= FLAG_C_UNIX;
1983 else if (strendswith( objs.str[i], ".res" ))
1984 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1985 else
1986 add_generated_source( make, objs.str[i], NULL );
1991 /*******************************************************************
1992 * create_dir
1994 static void create_dir( const char *dir )
1996 char *p, *path;
1998 p = path = xstrdup( dir );
1999 while ((p = strchr( p, '/' )))
2001 *p = 0;
2002 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
2003 *p++ = '/';
2004 while (*p == '/') p++;
2006 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
2007 free( path );
2011 /*******************************************************************
2012 * create_file_directories
2014 * Create the base directories of all the files.
2016 static void create_file_directories( const struct makefile *make, struct strarray files )
2018 struct strarray subdirs = empty_strarray;
2019 unsigned int i;
2020 char *dir;
2022 for (i = 0; i < files.count; i++)
2024 if (!strchr( files.str[i], '/' )) continue;
2025 dir = base_dir_path( make, files.str[i] );
2026 *strrchr( dir, '/' ) = 0;
2027 strarray_add_uniq( &subdirs, dir );
2030 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2034 /*******************************************************************
2035 * output_filenames_obj_dir
2037 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
2039 unsigned int i;
2041 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
2045 /*******************************************************************
2046 * get_dependencies
2048 static void get_dependencies( struct incl_file *file, struct incl_file *source )
2050 unsigned int i;
2052 if (!file->filename) return;
2054 if (file != source)
2056 if (file->owner == source) return; /* already processed */
2057 if (file->type == INCL_IMPORTLIB &&
2058 !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
2059 return; /* library is imported only when building a typelib */
2060 file->owner = source;
2061 strarray_add( &source->dependencies, file->filename );
2063 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
2067 /*******************************************************************
2068 * get_local_dependencies
2070 * Get the local dependencies of a given target.
2072 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
2073 struct strarray targets )
2075 unsigned int i;
2076 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2078 for (i = 0; i < deps.count; i++)
2080 if (strarray_exists( &targets, deps.str[i] ))
2081 deps.str[i] = obj_dir_path( make, deps.str[i] );
2082 else
2083 deps.str[i] = src_dir_path( make, deps.str[i] );
2085 return deps;
2089 /*******************************************************************
2090 * get_static_lib
2092 * Check if makefile builds the named static library and return the full lib path.
2094 static const char *get_static_lib( const struct makefile *make, const char *name )
2096 if (!make->staticlib) return NULL;
2097 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2098 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2099 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2100 return base_dir_path( make, make->staticlib );
2104 /*******************************************************************
2105 * get_parent_makefile
2107 static struct makefile *get_parent_makefile( struct makefile *make )
2109 char *dir, *p;
2110 int i;
2112 if (!make->base_dir) return NULL;
2113 dir = xstrdup( make->base_dir );
2114 if (!(p = strrchr( dir, '/' ))) return NULL;
2115 *p = 0;
2116 for (i = 0; i < top_makefile->subdirs.count; i++)
2117 if (!strcmp( top_makefile->submakes[i]->base_dir, dir )) return top_makefile->submakes[i];
2118 return NULL;
2122 /*******************************************************************
2123 * needs_cross_lib
2125 static int needs_cross_lib( const struct makefile *make )
2127 const char *name;
2128 if (!crosstarget) return 0;
2129 if (make->importlib) name = make->importlib;
2130 else if (make->staticlib)
2132 name = replace_extension( make->staticlib, ".a", "" );
2133 if (!strncmp( name, "lib", 3 )) name += 3;
2135 else return 0;
2136 if (strarray_exists( &cross_import_libs, name )) return 1;
2137 if (delay_load_flag && strarray_exists( &delay_import_libs, name )) return 1;
2138 return 0;
2142 /*******************************************************************
2143 * needs_delay_lib
2145 static int needs_delay_lib( const struct makefile *make )
2147 if (delay_load_flag) return 0;
2148 if (*dll_ext && !crosstarget) return 0;
2149 if (!make->importlib) return 0;
2150 return strarray_exists( &delay_import_libs, make->importlib );
2154 /*******************************************************************
2155 * add_default_libraries
2157 static struct strarray add_default_libraries( const struct makefile *make, struct strarray *deps,
2158 int add_unix_libs )
2160 struct strarray ret = empty_strarray;
2161 struct strarray all_libs = empty_strarray;
2162 unsigned int i, j;
2164 if (add_unix_libs)
2166 strarray_add( &all_libs, "-lwine_port" );
2167 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2168 strarray_addall( &all_libs, libs );
2171 for (i = 0; i < all_libs.count; i++)
2173 const char *lib = NULL;
2175 if (!strncmp( all_libs.str[i], "-l", 2 ))
2177 const char *name = all_libs.str[i] + 2;
2179 for (j = 0; j < top_makefile->subdirs.count; j++)
2181 const struct makefile *submake = top_makefile->submakes[j];
2183 if ((lib = get_static_lib( submake, name ))) break;
2187 if (lib)
2189 lib = top_obj_dir_path( make, lib );
2190 strarray_add( deps, lib );
2191 strarray_add( &ret, lib );
2193 else strarray_add( &ret, all_libs.str[i] );
2195 return ret;
2199 /*******************************************************************
2200 * add_import_libs
2202 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2203 struct strarray imports, int delay, int is_unix )
2205 struct strarray ret = empty_strarray;
2206 unsigned int i, j;
2207 int is_cross = make->is_cross && !is_unix;
2209 for (i = 0; i < imports.count; i++)
2211 const char *name = get_base_name( imports.str[i] );
2212 const char *lib = NULL;
2214 /* skip module's own importlib, its object files will be linked directly */
2215 if (make->importlib && !is_unix && !strcmp( make->importlib, imports.str[i] )) continue;
2217 for (j = 0; j < top_makefile->subdirs.count; j++)
2219 const struct makefile *submake = top_makefile->submakes[j];
2221 if (submake->importlib && !strcmp( submake->importlib, name ))
2223 if (is_cross || !*dll_ext || submake->staticimplib)
2224 lib = base_dir_path( submake, strmake( "lib%s.a", name ));
2225 else
2226 strarray_add( deps, top_obj_dir_path( make,
2227 strmake( "%s/lib%s.def", submake->base_dir, name )));
2228 break;
2231 if ((lib = get_static_lib( submake, name ))) break;
2234 if (lib)
2236 if (delay && !delay_load_flag && (is_cross || !*dll_ext))
2237 lib = replace_extension( lib, ".a", ".delay.a" );
2238 else if (is_cross)
2239 lib = replace_extension( lib, ".a", ".cross.a" );
2240 lib = top_obj_dir_path( make, lib );
2241 strarray_add( deps, lib );
2242 strarray_add( &ret, lib );
2244 else strarray_add( &ret, strmake( "-l%s", name ));
2246 return ret;
2250 /*******************************************************************
2251 * get_default_imports
2253 static struct strarray get_default_imports( const struct makefile *make )
2255 struct strarray ret = empty_strarray;
2257 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
2258 strarray_add( &ret, "winecrt0" );
2259 if (make->is_win16) strarray_add( &ret, "kernel" );
2260 strarray_add( &ret, "kernel32" );
2261 strarray_add( &ret, "ntdll" );
2262 return ret;
2266 /*******************************************************************
2267 * add_install_rule
2269 static void add_install_rule( struct makefile *make, const char *target,
2270 const char *file, const char *dest )
2272 if (strarray_exists( &make->install_lib, target ) ||
2273 strarray_exists( &top_install_lib, make->base_dir ) ||
2274 strarray_exists( &top_install_lib, base_dir_path( make, target )))
2276 strarray_add( &make->install_rules[INSTALL_LIB], file );
2277 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2279 else if (strarray_exists( &make->install_dev, target ) ||
2280 strarray_exists( &top_install_dev, make->base_dir ) ||
2281 strarray_exists( &top_install_dev, base_dir_path( make, target )))
2283 strarray_add( &make->install_rules[INSTALL_DEV], file );
2284 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2289 /*******************************************************************
2290 * get_include_install_path
2292 * Determine the installation path for a given include file.
2294 static const char *get_include_install_path( const char *name )
2296 if (!strncmp( name, "wine/", 5 )) return name + 5;
2297 if (!strncmp( name, "msvcrt/", 7 )) return name;
2298 return strmake( "windows/%s", name );
2302 /*******************************************************************
2303 * get_shared_library_name
2305 * Determine possible names for a shared library with a version number.
2307 static struct strarray get_shared_lib_names( const char *libname )
2309 struct strarray ret = empty_strarray;
2310 const char *ext, *p;
2311 char *name, *first, *second;
2312 size_t len = 0;
2314 strarray_add( &ret, libname );
2316 for (p = libname; (p = strchr( p, '.' )); p++)
2317 if ((len = strspn( p + 1, "0123456789." ))) break;
2319 if (!len) return ret;
2320 ext = p + 1 + len;
2321 if (*ext && ext[-1] == '.') ext--;
2323 /* keep only the first group of digits */
2324 name = xstrdup( libname );
2325 first = name + (p - libname);
2326 if ((second = strchr( first + 1, '.' )))
2328 strcpy( second, ext );
2329 strarray_add( &ret, xstrdup( name ));
2331 /* now remove all digits */
2332 strcpy( first, ext );
2333 strarray_add( &ret, name );
2334 return ret;
2338 /*******************************************************************
2339 * get_source_defines
2341 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2342 const char *obj )
2344 unsigned int i;
2345 struct strarray ret = empty_strarray;
2347 strarray_addall( &ret, make->include_args );
2348 if (source->use_msvcrt)
2349 strarray_add( &ret, strmake( "-I%s", top_src_dir_path( make, "include/msvcrt" )));
2350 for (i = 0; i < make->include_paths.count; i++)
2351 strarray_add( &ret, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
2352 strarray_addall( &ret, make->define_args );
2353 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2354 return ret;
2358 /*******************************************************************
2359 * get_debug_file
2361 static const char *get_debug_file( struct makefile *make, const char *name )
2363 const char *debug_file = NULL;
2364 if (!make->is_cross || !crossdebug) return NULL;
2365 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2366 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2367 if (debug_file) strarray_add( &make->debug_files, debug_file );
2368 return debug_file;
2372 /*******************************************************************
2373 * output_winegcc_command
2375 static void output_winegcc_command( struct makefile *make, int is_cross )
2377 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2378 output_filename( "--wine-objdir" );
2379 output_filename( top_obj_dir_path( make, "" ));
2380 if (tools_dir)
2382 output_filename( "--winebuild" );
2383 output_filename( tools_path( make, "winebuild" ));
2385 if (is_cross)
2387 output_filename( "-b" );
2388 output_filename( crosstarget );
2389 output_filename( "--lib-suffix=.cross.a" );
2391 else
2393 output_filenames( target_flags );
2394 output_filenames( lddll_flags );
2399 /*******************************************************************
2400 * output_symlink_rule
2402 * Output a rule to create a symlink.
2404 static void output_symlink_rule( const char *src_name, const char *link_name )
2406 const char *name;
2408 output( "\trm -f %s && ", link_name );
2410 /* dest path with a directory needs special handling if ln -s isn't supported */
2411 if (strcmp( ln_s, "ln -s" ) && ((name = strrchr( link_name, '/' ))))
2413 char *dir = xstrdup( link_name );
2414 dir[name - link_name] = 0;
2415 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2416 free( dir );
2418 else
2420 output( "%s %s %s\n", ln_s, src_name, link_name );
2425 /*******************************************************************
2426 * output_srcdir_symlink
2428 * Output rule to create a symlink back to the source directory, for source files
2429 * that are needed at run-time.
2431 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2433 char *src_file;
2435 if (!make->src_dir) return;
2436 src_file = src_dir_path( make, obj );
2437 output( "%s: %s\n", obj, src_file );
2438 output_symlink_rule( src_file, obj );
2439 strarray_add( &make->all_targets, obj );
2443 /*******************************************************************
2444 * output_install_commands
2446 static void output_install_commands( struct makefile *make, const struct makefile *submake,
2447 struct strarray files )
2449 unsigned int i;
2450 char *install_sh = top_src_dir_path( make, "tools/install-sh" );
2452 for (i = 0; i < files.count; i += 2)
2454 const char *file = files.str[i];
2455 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2457 if (submake) file = base_dir_path( submake, file );
2458 switch (*files.str[i + 1])
2460 case 'c': /* cross-compiled program */
2461 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2462 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2463 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2464 break;
2465 case 'd': /* data file */
2466 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2467 install_sh, obj_dir_path( make, file ), dest );
2468 break;
2469 case 'D': /* data file in source dir */
2470 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2471 install_sh, src_dir_path( make, file ), dest );
2472 break;
2473 case 'p': /* program file */
2474 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2475 install_sh, obj_dir_path( make, file ), dest );
2476 break;
2477 case 's': /* script */
2478 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2479 install_sh, obj_dir_path( make, file ), dest );
2480 break;
2481 case 'S': /* script in source dir */
2482 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2483 install_sh, src_dir_path( make, file ), dest );
2484 break;
2485 case 't': /* script in tools dir */
2486 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2487 install_sh, tools_dir_path( make, files.str[i] ), dest );
2488 break;
2489 case 'y': /* symlink */
2490 output_symlink_rule( files.str[i], dest );
2491 break;
2492 default:
2493 assert(0);
2495 strarray_add( &make->uninstall_files, dest );
2500 /*******************************************************************
2501 * output_install_rules
2503 * Rules are stored as a (file,dest) pair of values.
2504 * The first char of dest indicates the type of install.
2506 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2508 unsigned int i;
2509 struct strarray files = make->install_rules[rules];
2510 struct strarray targets = empty_strarray;
2512 if (!files.count) return;
2514 for (i = 0; i < files.count; i += 2)
2516 const char *file = files.str[i];
2517 switch (*files.str[i + 1])
2519 case 'c': /* cross-compiled program */
2520 case 'd': /* data file */
2521 case 'p': /* program file */
2522 case 's': /* script */
2523 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2524 break;
2525 case 't': /* script in tools dir */
2526 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2527 break;
2531 output( "install %s::", target );
2532 output_filenames( targets );
2533 output( "\n" );
2534 output_install_commands( make, NULL, files );
2536 strarray_add_uniq( &make->phony_targets, "install" );
2537 strarray_add_uniq( &make->phony_targets, target );
2541 static int cmp_string_length( const char **a, const char **b )
2543 int paths_a = 0, paths_b = 0;
2544 const char *p;
2546 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2547 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2548 if (paths_b != paths_a) return paths_b - paths_a;
2549 return strcmp( *a, *b );
2552 /*******************************************************************
2553 * output_uninstall_rules
2555 static void output_uninstall_rules( struct makefile *make )
2557 static const char *dirs_order[] =
2558 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };
2560 struct strarray subdirs = empty_strarray;
2561 unsigned int i, j;
2563 if (!make->uninstall_files.count) return;
2564 output( "uninstall::\n" );
2565 output_rm_filenames( make->uninstall_files );
2566 strarray_add_uniq( &make->phony_targets, "uninstall" );
2568 if (!make->subdirs.count) return;
2569 for (i = 0; i < make->uninstall_files.count; i++)
2571 char *dir = xstrdup( make->uninstall_files.str[i] );
2572 while (strchr( dir, '/' ))
2574 *strrchr( dir, '/' ) = 0;
2575 strarray_add_uniq( &subdirs, xstrdup(dir) );
2578 strarray_qsort( &subdirs, cmp_string_length );
2579 output( "\t-rmdir" );
2580 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2582 for (j = 0; j < subdirs.count; j++)
2584 if (!subdirs.str[j]) continue;
2585 if (strncmp( subdirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2586 continue;
2587 output_filename( subdirs.str[j] );
2588 subdirs.str[j] = NULL;
2591 for (j = 0; j < subdirs.count; j++)
2592 if (subdirs.str[j]) output_filename( subdirs.str[j] );
2593 output( "\n" );
2597 /*******************************************************************
2598 * output_importlib_symlinks
2600 static struct strarray output_importlib_symlinks( const struct makefile *parent,
2601 const struct makefile *make )
2603 struct strarray ret = empty_strarray;
2604 const char *lib, *dst, *ext[4];
2605 int i, count = 0;
2607 if (!make->module) return ret;
2608 if (!make->importlib) return ret;
2609 if (make->is_win16 && make->disabled) return ret;
2610 if (strncmp( make->base_dir, "dlls/", 5 )) return ret;
2611 if (!strcmp( make->module, make->importlib )) return ret;
2612 if (!strchr( make->importlib, '.' ) &&
2613 !strncmp( make->module, make->importlib, strlen( make->importlib )) &&
2614 !strcmp( make->module + strlen( make->importlib ), ".dll" ))
2615 return ret;
2617 ext[count++] = *dll_ext ? "def" : "a";
2618 if (needs_delay_lib( make )) ext[count++] = "delay.a";
2619 if (needs_cross_lib( make )) ext[count++] = "cross.a";
2621 for (i = 0; i < count; i++)
2623 lib = strmake( "lib%s.%s", make->importlib, ext[i] );
2624 dst = concat_paths( obj_dir_path( parent, "dlls" ), lib );
2625 output( "%s: %s\n", dst, base_dir_path( make, lib ));
2626 output_symlink_rule( concat_paths( make->base_dir + strlen("dlls/"), lib ), dst );
2627 strarray_add( &ret, dst );
2629 return ret;
2633 /*******************************************************************
2634 * output_po_files
2636 static void output_po_files( const struct makefile *make )
2638 const char *po_dir = src_dir_path( make, "po" );
2639 struct strarray pot_files = empty_strarray;
2640 struct incl_file *source;
2641 unsigned int i;
2643 for (i = 0; i < make->subdirs.count; i++)
2645 struct makefile *submake = make->submakes[i];
2647 LIST_FOR_EACH_ENTRY( source, &submake->sources, struct incl_file, entry )
2649 if (source->file->flags & FLAG_PARENTDIR) continue;
2650 if (strendswith( source->name, ".rc" ) && (source->file->flags & FLAG_RC_PO))
2652 char *pot_file = replace_extension( source->name, ".rc", ".pot" );
2653 char *pot_path = base_dir_path( submake, pot_file );
2654 output( "%s: tools/wrc include dummy\n", pot_path );
2655 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2656 strarray_add( &pot_files, pot_path );
2658 else if (strendswith( source->name, ".mc" ))
2660 char *pot_file = replace_extension( source->name, ".mc", ".pot" );
2661 char *pot_path = base_dir_path( submake, pot_file );
2662 output( "%s: tools/wmc include dummy\n", pot_path );
2663 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2664 strarray_add( &pot_files, pot_path );
2668 if (linguas.count)
2670 for (i = 0; i < linguas.count; i++)
2671 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2672 output( ": %s/wine.pot\n", po_dir );
2673 output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2674 po_dir );
2675 output( "po:" );
2676 for (i = 0; i < linguas.count; i++)
2677 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2678 output( "\n" );
2680 output( "%s/wine.pot:", po_dir );
2681 output_filenames( pot_files );
2682 output( "\n" );
2683 output( "\tmsgcat -o $@" );
2684 output_filenames( pot_files );
2685 output( "\n" );
2689 /*******************************************************************
2690 * output_source_y
2692 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2694 /* add source file dependency for parallel makes */
2695 char *header = strmake( "%s.tab.h", obj );
2697 if (find_include_file( make, header ))
2699 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2700 output( "\t%s -p %s_ -o %s.tab.c -d %s\n",
2701 bison, obj, obj_dir_path( make, obj ), source->filename );
2702 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2703 source->filename, obj_dir_path( make, header ));
2704 strarray_add( &make->clean_files, header );
2706 else output( "%s.tab.c: %s\n", obj, source->filename );
2708 output( "\t%s -p %s_ -o $@ %s\n", bison, obj, source->filename );
2712 /*******************************************************************
2713 * output_source_l
2715 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2717 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2718 output( "\t%s -o$@ %s\n", flex, source->filename );
2722 /*******************************************************************
2723 * output_source_h
2725 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2727 if (source->file->flags & FLAG_GENERATED)
2728 strarray_add( &make->all_targets, source->name );
2729 else
2730 add_install_rule( make, source->name, source->name,
2731 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2735 /*******************************************************************
2736 * output_source_rc
2738 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2740 struct strarray defines = get_source_defines( make, source, obj );
2741 char *po_dir = NULL;
2742 unsigned int i;
2744 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2745 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = top_obj_dir_path( make, "po" );
2746 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2747 if (source->file->flags & FLAG_RC_PO && !(source->file->flags & FLAG_PARENTDIR))
2749 strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
2750 output( "%s.pot ", obj_dir_path( make, obj ) );
2752 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2753 output_filename( tools_path( make, "wrc" ));
2754 output_filenames( source->dependencies );
2755 output( "\n" );
2756 output( "\t%s -u -o $@", tools_path( make, "wrc" ) );
2757 if (make->is_win16) output_filename( "-m16" );
2758 else output_filenames( target_flags );
2759 output_filename( "--nostdinc" );
2760 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2761 output_filenames( defines );
2762 output_filename( source->filename );
2763 output( "\n" );
2764 if (po_dir)
2766 output( "%s.res:", obj_dir_path( make, obj ));
2767 for (i = 0; i < linguas.count; i++)
2768 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2769 output( "\n" );
2774 /*******************************************************************
2775 * output_source_mc
2777 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2779 unsigned int i;
2780 char *obj_path = obj_dir_path( make, obj );
2782 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2783 strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
2784 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2785 output_filename( tools_path( make, "wmc" ));
2786 output_filenames( source->dependencies );
2787 output( "\n" );
2788 output( "\t%s -u -o $@ %s", tools_path( make, "wmc" ), source->filename );
2789 if (linguas.count)
2791 char *po_dir = top_obj_dir_path( make, "po" );
2792 output_filename( strmake( "--po-dir=%s", po_dir ));
2793 output( "\n" );
2794 output( "%s.res:", obj_dir_path( make, obj ));
2795 for (i = 0; i < linguas.count; i++)
2796 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2798 output( "\n" );
2802 /*******************************************************************
2803 * output_source_res
2805 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2807 strarray_add( &make->res_files, source->name );
2811 /*******************************************************************
2812 * output_source_idl
2814 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2816 struct strarray defines = get_source_defines( make, source, obj );
2817 struct strarray targets = empty_strarray;
2818 char *dest;
2819 unsigned int i;
2821 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2822 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2824 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2826 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2827 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2828 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2829 strarray_add( &targets, dest );
2831 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2832 if (source->file->flags & FLAG_INSTALL)
2834 add_install_rule( make, source->name, xstrdup( source->name ),
2835 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2836 if (source->file->flags & FLAG_IDL_HEADER)
2837 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2838 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2839 if (source->file->flags & FLAG_IDL_TYPELIB)
2840 add_install_rule( make, source->name, strmake( "%s.tlb", obj ),
2841 strmake( "d$(includedir)/wine/%s.tlb", get_include_install_path( obj ) ));
2843 if (!targets.count) return;
2845 output_filenames_obj_dir( make, targets );
2846 output( ": %s\n", tools_path( make, "widl" ));
2847 output( "\t%s -o $@", tools_path( make, "widl" ) );
2848 output_filenames( target_flags );
2849 output_filename( "--nostdinc" );
2850 output_filenames( defines );
2851 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2852 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2853 output_filename( source->filename );
2854 output( "\n" );
2855 output_filenames_obj_dir( make, targets );
2856 output( ": %s", source->filename );
2857 output_filenames( source->dependencies );
2858 output( "\n" );
2862 /*******************************************************************
2863 * output_source_tlb
2865 static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
2867 strarray_add( &make->all_targets, source->name );
2871 /*******************************************************************
2872 * output_source_x
2874 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2876 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2877 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2878 output( "\t%s%s -H -o $@ %s\n",
2879 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2880 if (source->file->flags & FLAG_INSTALL)
2882 add_install_rule( make, source->name, source->name,
2883 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2884 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2885 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2890 /*******************************************************************
2891 * output_source_sfd
2893 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2895 unsigned int i;
2896 char *ttf_obj = strmake( "%s.ttf", obj );
2897 char *ttf_file = src_dir_path( make, ttf_obj );
2899 if (fontforge && !make->src_dir)
2901 output( "%s: %s\n", ttf_file, source->filename );
2902 output( "\t%s -script %s %s $@\n",
2903 fontforge, top_src_dir_path( make, "fonts/genttf.ff" ), source->filename );
2904 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
2906 if (source->file->flags & FLAG_INSTALL)
2908 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2909 output_srcdir_symlink( make, ttf_obj );
2912 if (source->file->flags & FLAG_SFD_FONTS)
2914 struct strarray *array = source->file->args;
2916 for (i = 0; i < array->count; i++)
2918 char *font = strtok( xstrdup(array->str[i]), " \t" );
2919 char *args = strtok( NULL, "" );
2921 strarray_add( &make->all_targets, xstrdup( font ));
2922 output( "%s: %s %s\n", obj_dir_path( make, font ),
2923 tools_path( make, "sfnt2fon" ), ttf_file );
2924 output( "\t%s -q -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2925 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2931 /*******************************************************************
2932 * output_source_svg
2934 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2936 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2937 unsigned int i;
2939 if (convert && rsvg && icotool && !make->src_dir)
2941 for (i = 0; images[i]; i++)
2942 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2944 if (images[i])
2946 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2947 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2948 top_src_dir_path( make, "tools/buildimage" ), source->filename );
2954 /*******************************************************************
2955 * output_source_nls
2957 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2959 add_install_rule( make, source->name, source->name,
2960 strmake( "D$(nlsdir)/%s", source->name ));
2961 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2965 /*******************************************************************
2966 * output_source_desktop
2968 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2970 add_install_rule( make, source->name, source->name,
2971 strmake( "D$(datadir)/applications/%s", source->name ));
2975 /*******************************************************************
2976 * output_source_po
2978 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2980 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2981 output( "\t%s -o $@ %s\n", msgfmt, source->filename );
2982 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2986 /*******************************************************************
2987 * output_source_in
2989 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2991 unsigned int i;
2993 if (strendswith( obj, ".man" ) && source->file->args)
2995 struct strarray symlinks;
2996 char *dir, *dest = replace_extension( obj, ".man", "" );
2997 char *lang = strchr( dest, '.' );
2998 char *section = source->file->args;
2999 if (lang)
3001 *lang++ = 0;
3002 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3004 else dir = strmake( "$(mandir)/man%s", section );
3005 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
3006 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3007 for (i = 0; i < symlinks.count; i++)
3008 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
3009 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3010 free( dest );
3011 free( dir );
3013 strarray_add( &make->in_files, xstrdup(obj) );
3014 strarray_add( &make->all_targets, xstrdup(obj) );
3015 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3016 output( "\t%s %s >$@ || (rm -f $@ && false)\n", sed_cmd, source->filename );
3017 output( "%s:", obj_dir_path( make, obj ));
3018 output_filenames( source->dependencies );
3019 output( "\n" );
3020 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
3024 /*******************************************************************
3025 * output_source_spec
3027 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3029 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3030 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
3031 struct strarray all_libs, dep_libs = empty_strarray;
3032 char *dll_name, *obj_name, *output_file;
3033 const char *debug_file;
3035 if (!imports.count) imports = make->imports;
3036 if (!dll_flags.count) dll_flags = make->extradllflags;
3037 all_libs = add_import_libs( make, &dep_libs, imports, 0, 0 );
3038 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3039 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
3040 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
3041 output_file = obj_dir_path( make, dll_name );
3043 strarray_add( &make->clean_files, dll_name );
3044 strarray_add( &make->res_files, strmake( "%s.res", obj ));
3045 output( "%s.res: %s\n", obj_dir_path( make, obj ), obj_dir_path( make, dll_name ));
3046 output( "\techo \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", obj, output_file,
3047 tools_path( make, "wrc" ));
3049 output( "%s:", output_file);
3050 output_filename( source->filename );
3051 output_filename( obj_name );
3052 output_filenames( dep_libs );
3053 output_filename( tools_path( make, "winebuild" ));
3054 output_filename( tools_path( make, "winegcc" ));
3055 output( "\n" );
3056 output_winegcc_command( make, make->is_cross );
3057 output_filename( "-s" );
3058 output_filenames( dll_flags );
3059 output_filename( "-shared" );
3060 output_filename( source->filename );
3061 output_filename( obj_name );
3062 if ((debug_file = get_debug_file( make, output_file )))
3063 output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3064 output_filenames( all_libs );
3065 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3066 output( "\n" );
3070 /*******************************************************************
3071 * output_source_default
3073 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3075 struct strarray defines = get_source_defines( make, source, obj );
3076 int is_dll_src = (make->testdll &&
3077 strendswith( source->name, ".c" ) &&
3078 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3079 int need_cross = (crosstarget &&
3080 !(source->file->flags & FLAG_C_UNIX) &&
3081 (make->is_cross ||
3082 ((source->file->flags & FLAG_C_IMPLIB) &&
3083 (needs_cross_lib( make ) || needs_delay_lib( make ))) ||
3084 (make->staticlib && needs_cross_lib( make ))));
3085 int need_obj = ((*dll_ext || !make->staticlib || !(source->file->flags & FLAG_C_UNIX)) &&
3086 (!need_cross ||
3087 (source->file->flags & FLAG_C_IMPLIB) ||
3088 (make->module && make->staticlib)));
3090 if ((source->file->flags & FLAG_GENERATED) &&
3091 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3092 strarray_add( &make->clean_files, source->filename );
3093 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
3095 if (need_obj)
3097 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3098 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3099 else if (!is_dll_src && (!(source->file->flags & FLAG_C_IMPLIB) || (make->importlib && strarray_exists( &make->imports, make->importlib ))))
3100 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3101 else
3102 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3103 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3104 output( "\t$(CC) -c -o $@ %s", source->filename );
3105 output_filenames( defines );
3106 if (make->module || make->staticlib || make->sharedlib || make->testdll)
3108 output_filenames( dll_flags );
3109 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3111 output_filenames( extra_cflags );
3112 output_filenames( cpp_flags );
3113 output_filename( "$(CFLAGS)" );
3114 output( "\n" );
3116 if (need_cross)
3118 strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
3119 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3120 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
3121 output_filenames( defines );
3122 output_filenames( extra_cross_cflags );
3123 if (source->file->flags & FLAG_C_IMPLIB) output_filename( "-fno-builtin" );
3124 output_filenames( cpp_flags );
3125 output_filename( "$(CROSSCFLAGS)" );
3126 output( "\n" );
3128 if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
3130 strarray_add( &make->c2man_files, source->filename );
3131 if (make->testdll && !is_dll_src)
3133 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3134 output( "%s.ok:\n", obj_dir_path( make, obj ));
3135 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
3136 top_src_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ),
3137 make->testdll, replace_extension( make->testdll, ".dll", "_test.exe" ),
3138 make->is_cross ? "" : dll_ext, obj );
3141 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3142 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3143 output( ":" );
3144 output_filenames( source->dependencies );
3145 output( "\n" );
3149 /* dispatch table to output rules for a single source file */
3150 static const struct
3152 const char *ext;
3153 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3154 } output_source_funcs[] =
3156 { "y", output_source_y },
3157 { "l", output_source_l },
3158 { "h", output_source_h },
3159 { "rh", output_source_h },
3160 { "inl", output_source_h },
3161 { "rc", output_source_rc },
3162 { "mc", output_source_mc },
3163 { "res", output_source_res },
3164 { "idl", output_source_idl },
3165 { "tlb", output_source_tlb },
3166 { "sfd", output_source_sfd },
3167 { "svg", output_source_svg },
3168 { "nls", output_source_nls },
3169 { "desktop", output_source_desktop },
3170 { "po", output_source_po },
3171 { "in", output_source_in },
3172 { "x", output_source_x },
3173 { "spec", output_source_spec },
3174 { NULL, output_source_default }
3178 /*******************************************************************
3179 * has_object_file
3181 static int has_object_file( struct makefile *make )
3183 struct incl_file *source;
3184 int i;
3186 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3188 char *ext = get_extension( source->name );
3190 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3191 ext++;
3193 for (i = 0; output_source_funcs[i].ext; i++)
3194 if (!strcmp( ext, output_source_funcs[i].ext )) break;
3196 if (!output_source_funcs[i].ext) return 1; /* default extension builds to an object file */
3198 return 0;
3202 /*******************************************************************
3203 * output_man_pages
3205 static void output_man_pages( struct makefile *make )
3207 if (make->c2man_files.count)
3209 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3211 output( "manpages::\n" );
3212 output( "\t%s -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3213 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3214 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3215 output_filename( strmake( "-o %s/man%s",
3216 top_obj_dir_path( make, "documentation" ), man_ext ));
3217 output_filenames( make->c2man_files );
3218 output( "\n" );
3219 output( "htmlpages::\n" );
3220 output( "\t%s -Th -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3221 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3222 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3223 output_filename( strmake( "-o %s",
3224 top_obj_dir_path( make, "documentation/html" )));
3225 output_filenames( make->c2man_files );
3226 output( "\n" );
3227 output( "sgmlpages::\n" );
3228 output( "\t%s -Ts -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3229 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3230 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3231 output_filename( strmake( "-o %s",
3232 top_obj_dir_path( make, "documentation/api-guide" )));
3233 output_filenames( make->c2man_files );
3234 output( "\n" );
3235 output( "xmlpages::\n" );
3236 output( "\t%s -Tx -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3237 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3238 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3239 output_filename( strmake( "-o %s",
3240 top_obj_dir_path( make, "documentation/api-guide-xml" )));
3241 output_filenames( make->c2man_files );
3242 output( "\n" );
3243 strarray_add( &make->phony_targets, "manpages" );
3244 strarray_add( &make->phony_targets, "htmlpages" );
3245 strarray_add( &make->phony_targets, "sgmlpages" );
3246 strarray_add( &make->phony_targets, "xmlpages" );
3248 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
3252 /*******************************************************************
3253 * output_module
3255 static void output_module( struct makefile *make )
3257 struct strarray all_libs = empty_strarray;
3258 struct strarray dep_libs = empty_strarray;
3259 char *module_path = obj_dir_path( make, make->module );
3260 const char *debug_file = NULL;
3261 char *spec_file = NULL;
3262 unsigned int i;
3264 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3265 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, 0 ));
3266 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0, 0 ));
3267 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3269 if (make->is_cross)
3271 if (delay_load_flag)
3273 for (i = 0; i < make->delayimports.count; i++)
3274 strarray_add( &all_libs, strmake( "%s%s%s", delay_load_flag, make->delayimports.str[i],
3275 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3277 strarray_add( &make->all_targets, strmake( "%s", make->module ));
3278 add_install_rule( make, make->module, strmake( "%s", make->module ),
3279 strmake( "c$(dlldir)/%s", make->module ));
3280 debug_file = get_debug_file( make, module_path );
3281 output( "%s:", module_path );
3283 else if (*dll_ext)
3285 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs, !make->use_msvcrt ));
3286 for (i = 0; i < make->delayimports.count; i++)
3287 strarray_add( &all_libs, strmake( "-Wl,-delayload,%s%s", make->delayimports.str[i],
3288 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3289 strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
3290 strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
3291 add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
3292 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
3293 add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
3294 strmake( "d$(dlldir)/fakedlls/%s", make->module ));
3295 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
3297 else
3299 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs, 1 ));
3300 strarray_add( &make->all_targets, make->module );
3301 add_install_rule( make, make->module, make->module,
3302 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
3303 debug_file = get_debug_file( make, module_path );
3304 output( "%s:", module_path );
3307 if (spec_file) output_filename( spec_file );
3308 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3309 output_filenames_obj_dir( make, make->res_files );
3310 output_filenames( dep_libs );
3311 output_filename( tools_path( make, "winebuild" ));
3312 output_filename( tools_path( make, "winegcc" ));
3313 output( "\n" );
3314 output_winegcc_command( make, make->is_cross );
3315 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3316 if (spec_file)
3318 output_filename( "-shared" );
3319 output_filename( spec_file );
3321 output_filenames( make->extradllflags );
3322 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3323 output_filenames_obj_dir( make, make->res_files );
3324 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3325 output_filenames( all_libs );
3326 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3327 output( "\n" );
3329 if (make->unixobj_files.count)
3331 struct strarray unix_libs = empty_strarray;
3332 struct strarray unix_deps = empty_strarray;
3333 char *ext, *unix_lib = xmalloc( strlen( make->module ) + strlen( dll_ext ) + 1 );
3334 strcpy( unix_lib, make->module );
3335 if ((ext = get_extension( unix_lib ))) *ext = 0;
3336 strcat( unix_lib, dll_ext );
3338 if (make->importlib)
3340 struct strarray imp = empty_strarray;
3341 strarray_add( &imp, make->importlib );
3342 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, imp, 1, 1 ));
3344 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, make->delayimports, 1, 1 ));
3345 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, make->imports, 0, 1 ));
3346 add_import_libs( make, &unix_deps, get_default_imports( make ), 0, 1 ); /* dependencies only */
3347 strarray_addall( &unix_libs, add_default_libraries( make, &unix_deps, 1 ));
3349 strarray_add( &make->all_targets, unix_lib );
3350 add_install_rule( make, make->module, unix_lib, strmake( "p$(dlldir)/%s", unix_lib ));
3351 output( "%s:", unix_lib );
3352 if (spec_file) output_filename( spec_file );
3353 output_filenames_obj_dir( make, make->unixobj_files );
3354 output_filenames( unix_deps );
3355 output_filename( tools_path( make, "winebuild" ));
3356 output_filename( tools_path( make, "winegcc" ));
3357 output( "\n" );
3358 output_winegcc_command( make, 0 );
3359 output_filename( "-munix" );
3360 output_filename( "-shared" );
3361 if (spec_file) output_filename( spec_file );
3362 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) output_filename( "-nodefaultlibs" );
3363 output_filenames_obj_dir( make, make->unixobj_files );
3364 output_filenames( unix_libs );
3365 output_filename( "$(LDFLAGS)" );
3366 output( "\n" );
3369 if (spec_file && make->importlib)
3371 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3372 if (*dll_ext && !make->implib_objs.count)
3374 strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
3375 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
3376 output( "\t%s -w --def -o $@", tools_path( make, "winebuild" ) );
3377 output_filenames( target_flags );
3378 if (make->is_win16) output_filename( "-m16" );
3379 output_filename( "--export" );
3380 output_filename( spec_file );
3381 output( "\n" );
3382 add_install_rule( make, make->importlib,
3383 strmake( "lib%s.def", make->importlib ),
3384 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
3386 else
3388 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3389 if (!*dll_ext && needs_delay_lib( make ))
3391 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3392 output( "%s.delay.a ", importlib_path );
3394 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3395 output_filenames_obj_dir( make, make->implib_objs );
3396 output( "\n" );
3397 output( "\t%s -w --implib -o $@", tools_path( make, "winebuild" ) );
3398 output_filenames( target_flags );
3399 if (make->is_win16) output_filename( "-m16" );
3400 output_filename( "--export" );
3401 output_filename( spec_file );
3402 output_filenames_obj_dir( make, make->implib_objs );
3403 output( "\n" );
3404 add_install_rule( make, make->importlib,
3405 strmake( "lib%s.a", make->importlib ),
3406 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
3408 if (crosstarget && (needs_cross_lib( make ) || needs_delay_lib( make )))
3410 struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
3411 if (needs_cross_lib( make ))
3413 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3414 output_filename( strmake( "%s.cross.a", importlib_path ));
3416 if (needs_delay_lib( make ))
3418 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3419 output_filename( strmake( "%s.delay.a", importlib_path ));
3421 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3422 output_filenames_obj_dir( make, cross_files );
3423 output( "\n" );
3424 output( "\t%s -b %s -w --implib -o $@", tools_path( make, "winebuild" ), crosstarget );
3425 if (make->is_win16) output_filename( "-m16" );
3426 output_filename( "--export" );
3427 output_filename( spec_file );
3428 output_filenames_obj_dir( make, cross_files );
3429 output( "\n" );
3433 if (spec_file)
3434 output_man_pages( make );
3435 else if (*dll_ext && !make->is_win16 && strendswith( make->module, ".exe" ))
3437 char *binary = replace_extension( make->module, ".exe", "" );
3438 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3443 /*******************************************************************
3444 * output_static_lib
3446 static void output_static_lib( struct makefile *make )
3448 strarray_add( &make->all_targets, make->staticlib );
3449 output( "%s:", obj_dir_path( make, make->staticlib ));
3450 output_filenames_obj_dir( make, make->object_files );
3451 output_filenames_obj_dir( make, make->unixobj_files );
3452 output( "\n\trm -f $@\n" );
3453 output( "\t%s rc $@", ar );
3454 output_filenames_obj_dir( make, make->object_files );
3455 output_filenames_obj_dir( make, make->unixobj_files );
3456 output( "\n\t%s $@\n", ranlib );
3457 add_install_rule( make, make->staticlib, make->staticlib,
3458 strmake( "d$(dlldir)/%s", make->staticlib ));
3459 if (needs_cross_lib( make ))
3461 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3463 strarray_add( &make->all_targets, name );
3464 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3465 output_filenames_obj_dir( make, make->crossobj_files );
3466 output( "\n" );
3467 output( "\t%s -b %s -w --staticlib -o $@", tools_path( make, "winebuild" ), crosstarget );
3468 output_filenames_obj_dir( make, make->crossobj_files );
3469 output( "\n" );
3474 /*******************************************************************
3475 * output_shared_lib
3477 static void output_shared_lib( struct makefile *make )
3479 unsigned int i;
3480 char *basename, *p;
3481 struct strarray names = get_shared_lib_names( make->sharedlib );
3482 struct strarray all_libs = empty_strarray;
3483 struct strarray dep_libs = empty_strarray;
3485 basename = xstrdup( make->sharedlib );
3486 if ((p = strchr( basename, '.' ))) *p = 0;
3488 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3489 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3490 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs, 1 ));
3492 output( "%s:", obj_dir_path( make, make->sharedlib ));
3493 output_filenames_obj_dir( make, make->object_files );
3494 output_filenames( dep_libs );
3495 output( "\n" );
3496 output( "\t$(CC) -o $@" );
3497 output_filenames_obj_dir( make, make->object_files );
3498 output_filenames( all_libs );
3499 output_filename( "$(LDFLAGS)" );
3500 output( "\n" );
3501 add_install_rule( make, make->sharedlib, make->sharedlib,
3502 strmake( "p$(libdir)/%s", make->sharedlib ));
3503 for (i = 1; i < names.count; i++)
3505 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3506 output_symlink_rule( obj_dir_path( make, names.str[i-1] ), obj_dir_path( make, names.str[i] ));
3507 add_install_rule( make, names.str[i], names.str[i-1],
3508 strmake( "y$(libdir)/%s", names.str[i] ));
3510 strarray_addall( &make->all_targets, names );
3514 /*******************************************************************
3515 * output_test_module
3517 static void output_test_module( struct makefile *make )
3519 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3520 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3521 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3522 struct strarray dep_libs = empty_strarray;
3523 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0, 0 );
3524 struct makefile *parent = get_parent_makefile( make );
3525 const char *ext = make->is_cross ? "" : dll_ext;
3526 const char *parent_ext = parent && parent->is_cross ? "" : dll_ext;
3527 const char *debug_file;
3528 char *output_file;
3530 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3531 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3532 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3533 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3534 output( "%s:\n", output_file );
3535 output_winegcc_command( make, make->is_cross );
3536 output_filenames( make->extradllflags );
3537 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3538 output_filenames_obj_dir( make, make->res_files );
3539 if ((debug_file = get_debug_file( make, output_file )))
3540 output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3541 output_filenames( all_libs );
3542 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3543 output( "\n" );
3544 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3545 output_winegcc_command( make, make->is_cross );
3546 output_filename( "-s" );
3547 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3548 output_filenames( make->extradllflags );
3549 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3550 output_filenames_obj_dir( make, make->res_files );
3551 output_filenames( all_libs );
3552 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3553 output( "\n" );
3554 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3555 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3556 output_filenames_obj_dir( make, make->res_files );
3557 output_filenames( dep_libs );
3558 output_filename( tools_path( make, "winebuild" ));
3559 output_filename( tools_path( make, "winegcc" ));
3560 output( "\n" );
3562 if (!make->disabled && !strarray_exists( &disabled_dirs, "programs/winetest" ))
3563 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
3564 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
3565 obj_dir_path( make, stripped ), ext );
3566 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n",
3567 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3569 output_filenames_obj_dir( make, make->ok_files );
3570 output( ": %s%s ../%s%s\n", testmodule, ext, make->testdll, parent_ext );
3571 output( "check test:" );
3572 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3573 output( "\n" );
3574 strarray_add( &make->phony_targets, "check" );
3575 strarray_add( &make->phony_targets, "test" );
3576 output( "testclean::\n" );
3577 output( "\trm -f" );
3578 output_filenames_obj_dir( make, make->ok_files );
3579 output( "\n" );
3580 strarray_addall( &make->clean_files, make->ok_files );
3581 strarray_add( &make->phony_targets, "testclean" );
3585 /*******************************************************************
3586 * output_programs
3588 static void output_programs( struct makefile *make )
3590 unsigned int i, j;
3591 char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
3592 char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
3594 for (i = 0; i < make->programs.count; i++)
3596 char *program_installed = NULL;
3597 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3598 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3599 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3600 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3601 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3603 if (!objs.count) objs = make->object_files;
3604 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3605 strarray_addall( &all_libs, add_default_libraries( make, &deps, 1 ));
3607 output( "%s:", obj_dir_path( make, program ) );
3608 output_filenames_obj_dir( make, objs );
3609 output_filenames( deps );
3610 output( "\n" );
3611 output( "\t$(CC) -o $@" );
3612 output_filenames_obj_dir( make, objs );
3614 if (strarray_exists( &all_libs, "-lwine" ))
3616 strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
3617 if (ldrpath_local && ldrpath_install)
3619 program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
3620 output_filename( ldrpath_local );
3621 output_filenames( all_libs );
3622 output_filename( "$(LDFLAGS)" );
3623 output( "\n" );
3624 output( "%s:", obj_dir_path( make, program_installed ) );
3625 output_filenames_obj_dir( make, objs );
3626 output_filenames( deps );
3627 output( "\n" );
3628 output( "\t$(CC) -o $@" );
3629 output_filenames_obj_dir( make, objs );
3630 output_filename( ldrpath_install );
3631 strarray_add( &make->all_targets, program_installed );
3635 output_filenames( all_libs );
3636 output_filename( "$(LDFLAGS)" );
3637 output( "\n" );
3638 strarray_add( &make->all_targets, program );
3640 for (j = 0; j < symlinks.count; j++)
3642 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3643 output_symlink_rule( obj_dir_path( make, program ), obj_dir_path( make, symlinks.str[j] ));
3645 strarray_addall( &make->all_targets, symlinks );
3647 add_install_rule( make, program, program_installed ? program_installed : program,
3648 strmake( "p$(bindir)/%s", program ));
3649 for (j = 0; j < symlinks.count; j++)
3650 add_install_rule( make, symlinks.str[j], program,
3651 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3656 /*******************************************************************
3657 * output_subdirs
3659 static void output_subdirs( struct makefile *make )
3661 struct strarray symlinks = empty_strarray;
3662 struct strarray all_deps = empty_strarray;
3663 struct strarray build_deps = empty_strarray;
3664 struct strarray builddeps_deps = empty_strarray;
3665 struct strarray makefile_deps = empty_strarray;
3666 struct strarray clean_files = empty_strarray;
3667 struct strarray testclean_files = empty_strarray;
3668 struct strarray distclean_files = empty_strarray;
3669 struct strarray tools_deps = empty_strarray;
3670 struct strarray tooldeps_deps = empty_strarray;
3671 struct strarray winetest_deps = empty_strarray;
3672 unsigned int i, j;
3674 strarray_addall( &distclean_files, make->distclean_files );
3675 for (i = 0; i < make->subdirs.count; i++)
3677 const struct makefile *submake = make->submakes[i];
3678 char *subdir = base_dir_path( submake, "" );
3680 strarray_add( &makefile_deps, top_src_dir_path( make, base_dir_path( submake,
3681 strmake ( "%s.in", output_makefile_name ))));
3682 for (j = 0; j < submake->clean_files.count; j++)
3683 strarray_add( &clean_files, base_dir_path( submake, submake->clean_files.str[j] ));
3684 for (j = 0; j < submake->distclean_files.count; j++)
3685 strarray_add( &distclean_files, base_dir_path( submake, submake->distclean_files.str[j] ));
3686 for (j = 0; j < submake->ok_files.count; j++)
3687 strarray_add( &testclean_files, base_dir_path( submake, submake->ok_files.str[j] ));
3689 /* import libs are still created for disabled dirs, except for win16 ones */
3690 if (submake->module && submake->importlib && !(submake->disabled && submake->is_win16))
3692 char *importlib_path = base_dir_path( submake, strmake( "lib%s", submake->importlib ));
3693 if (submake->implib_objs.count)
3695 output( "%s.a: dummy\n", importlib_path );
3696 output( "\t@cd %s && $(MAKE) lib%s.a\n", subdir, submake->importlib );
3697 strarray_add( &tools_deps, strmake( "%s.a", importlib_path ));
3698 if (needs_cross_lib( submake ))
3700 output( "%s.cross.a: dummy\n", importlib_path );
3701 output( "\t@cd %s && $(MAKE) lib%s.cross.a\n", subdir, submake->importlib );
3702 strarray_add( &tools_deps, strmake( "%s.cross.a", importlib_path ));
3704 if (needs_delay_lib( submake ))
3706 output( "%s.delay.a: dummy\n", importlib_path );
3707 output( "\t@cd %s && $(MAKE) lib%s.delay.a\n", subdir, submake->importlib );
3708 strarray_add( &tools_deps, strmake( "%s.delay.a", importlib_path ));
3711 else
3713 char *spec_file = top_src_dir_path( make, base_dir_path( submake,
3714 replace_extension( submake->module, ".dll", ".spec" )));
3715 if (*dll_ext)
3717 output( "%s.def: %s", importlib_path, spec_file );
3718 strarray_add( &build_deps, strmake( "%s.def", importlib_path ));
3720 else
3722 if (needs_delay_lib( submake ))
3724 output( "%s.delay.a ", importlib_path );
3725 strarray_add( &build_deps, strmake( "%s.delay.a", importlib_path ));
3727 output( "%s.a: %s", importlib_path, spec_file );
3728 strarray_add( &build_deps, strmake( "%s.a", importlib_path ));
3730 output_filename( tools_path( make, "winebuild" ));
3731 output( "\n" );
3732 output( "\t%s -w -o $@", tools_path( make, "winebuild" ));
3733 output_filename( *dll_ext ? "--def" : "--implib" );
3734 output_filenames( target_flags );
3735 if (submake->is_win16) output_filename( "-m16" );
3736 output_filename( "--export" );
3737 output_filename( spec_file );
3738 output( "\n" );
3739 if (crosstarget && (needs_cross_lib( submake ) || needs_delay_lib( submake )))
3741 if (needs_cross_lib( submake ))
3743 output_filename( strmake( "%s.cross.a", importlib_path ));
3744 strarray_add( &build_deps, strmake( "%s.cross.a", importlib_path ));
3746 if (needs_delay_lib( submake ))
3748 output_filename( strmake( "%s.delay.a", importlib_path ));
3749 strarray_add( &build_deps, strmake( "%s.delay.a", importlib_path ));
3751 output( ": %s", spec_file );
3752 output_filename( tools_path( make, "winebuild" ));
3753 output( "\n" );
3754 output( "\t%s -b %s -w -o $@", tools_path( make, "winebuild" ), crosstarget );
3755 if (submake->is_win16) output_filename( "-m16" );
3756 output_filename( "--implib" );
3757 output_filename( "--export" );
3758 output_filename( spec_file );
3759 output( "\n" );
3762 strarray_addall( &symlinks, output_importlib_symlinks( make, submake ));
3765 if (submake->disabled) continue;
3766 strarray_add( &all_deps, subdir );
3768 if (submake->module)
3770 if (!submake->staticlib)
3772 strarray_add( &builddeps_deps, subdir );
3773 if (!submake->is_exe)
3775 output( "manpages htmlpages sgmlpages xmlpages::\n" );
3776 output( "\t@cd %s && $(MAKE) $@\n", subdir );
3779 else strarray_add( &tools_deps, subdir );
3781 else if (submake->testdll)
3783 output( "%s/test: dummy\n", subdir );
3784 output( "\t@cd %s && $(MAKE) test\n", subdir );
3785 strarray_add( &winetest_deps, subdir );
3786 strarray_add( &builddeps_deps, subdir );
3788 else
3790 if (!strcmp( submake->base_dir, "tools" ) || !strncmp( submake->base_dir, "tools/", 6 ))
3792 strarray_add( &tooldeps_deps, submake->base_dir );
3793 for (j = 0; j < submake->programs.count; j++)
3794 output( "%s/%s%s: %s\n", submake->base_dir,
3795 submake->programs.str[j], tools_ext, submake->base_dir );
3797 if (submake->programs.count || submake->sharedlib)
3799 struct strarray libs = get_expanded_make_var_array( submake, "EXTRALIBS" );
3800 for (j = 0; j < submake->programs.count; j++)
3801 strarray_addall( &libs, get_expanded_file_local_var( submake,
3802 submake->programs.str[j], "LDFLAGS" ));
3803 output( "%s: libs/port", submake->base_dir );
3804 for (j = 0; j < libs.count; j++)
3806 if (!strcmp( libs.str[j], "-lwpp" )) output_filename( "libs/wpp" );
3807 if (!strcmp( libs.str[j], "-lwine" )) output_filename( "libs/wine" );
3809 output( "\n" );
3813 if (submake->install_rules[INSTALL_LIB].count)
3815 output( "install install-lib:: %s\n", submake->base_dir );
3816 output_install_commands( make, submake, submake->install_rules[INSTALL_LIB] );
3818 if (submake->install_rules[INSTALL_DEV].count)
3820 output( "install install-dev:: %s\n", submake->base_dir );
3821 output_install_commands( make, submake, submake->install_rules[INSTALL_DEV] );
3824 output( "all:" );
3825 output_filenames( all_deps );
3826 output( "\n" );
3827 output_filenames( all_deps );
3828 output( ": dummy\n" );
3829 output( "\t@cd $@ && $(MAKE)\n" );
3830 output( "Makefile:" );
3831 output_filenames( makefile_deps );
3832 output( "\n" );
3833 output_filenames( makefile_deps );
3834 output( ":\n" );
3835 if (tooldeps_deps.count)
3837 output( "__tooldeps__:" );
3838 output_filenames( tooldeps_deps );
3839 output( "\n" );
3840 strarray_add( &make->phony_targets, "__tooldeps__" );
3842 if (winetest_deps.count)
3844 output( "buildtests programs/winetest:" );
3845 output_filenames( winetest_deps );
3846 output( "\n" );
3847 output( "check test:" );
3848 for (i = 0; i < winetest_deps.count; i++)
3850 char *target = strmake( "%s/test", winetest_deps.str[i] );
3851 output_filename( target );
3852 strarray_add( &make->phony_targets, target );
3854 output( "\n" );
3855 strarray_add( &make->phony_targets, "buildtests" );
3856 strarray_add( &make->phony_targets, "check" );
3857 strarray_add( &make->phony_targets, "test" );
3860 output( "clean::\n");
3861 output_rm_filenames( clean_files );
3862 output( "testclean::\n");
3863 output_rm_filenames( testclean_files );
3864 output( "distclean::\n");
3865 output_rm_filenames( distclean_files );
3866 output_filenames( tools_deps );
3867 output( ":" );
3868 output_filename( tools_dir_path( make, "widl" ));
3869 output_filename( tools_dir_path( make, "winebuild" ));
3870 output_filename( tools_dir_path( make, "winegcc" ));
3871 output_filename( obj_dir_path( make, "include" ));
3872 output( "\n" );
3873 output_filenames( builddeps_deps );
3874 output( ": __builddeps__\n" );
3875 strarray_add( &make->phony_targets, "distclean" );
3876 strarray_add( &make->phony_targets, "testclean" );
3877 strarray_addall( &make->phony_targets, all_deps );
3879 strarray_addall( &make->clean_files, symlinks );
3880 strarray_addall( &build_deps, tools_deps );
3881 strarray_addall( &build_deps, symlinks );
3882 if (build_deps.count)
3884 output( "__builddeps__:" );
3885 output_filenames( build_deps );
3886 output( "\n" );
3887 strarray_add( &make->phony_targets, "__builddeps__" );
3889 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3893 /*******************************************************************
3894 * output_sources
3896 static void output_sources( struct makefile *make )
3898 struct incl_file *source;
3899 unsigned int i, j;
3901 strarray_add( &make->phony_targets, "all" );
3903 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3905 char *obj = xstrdup( source->name );
3906 char *ext = get_extension( obj );
3908 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3909 *ext++ = 0;
3911 for (j = 0; output_source_funcs[j].ext; j++)
3912 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3914 output_source_funcs[j].fn( make, source, obj );
3915 strarray_addall_uniq( &make->dependencies, source->dependencies );
3918 /* special case for winetest: add resource files from other test dirs */
3919 if (make->base_dir && !strcmp( make->base_dir, "programs/winetest" ))
3921 struct strarray tests = enable_tests;
3922 if (!tests.count)
3923 for (i = 0; i < top_makefile->subdirs.count; i++)
3924 if (top_makefile->submakes[i]->testdll && !top_makefile->submakes[i]->disabled)
3925 strarray_add( &tests, top_makefile->submakes[i]->testdll );
3926 for (i = 0; i < tests.count; i++)
3927 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3930 if (make->dlldata_files.count)
3932 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3933 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3934 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
3935 output_filenames( make->dlldata_files );
3936 output( "\n" );
3939 if (make->staticlib) output_static_lib( make );
3940 else if (make->module) output_module( make );
3941 else if (make->testdll) output_test_module( make );
3942 else if (make->sharedlib) output_shared_lib( make );
3943 else if (make->programs.count) output_programs( make );
3945 for (i = 0; i < make->scripts.count; i++)
3946 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3947 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3949 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3950 strarray_add( &make->distclean_files, "Makefile" );
3951 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3953 if (!make->base_dir)
3954 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3955 else if (!strcmp( make->base_dir, "po" ))
3956 strarray_add( &make->distclean_files, "LINGUAS" );
3958 if (make->subdirs.count) output_subdirs( make );
3960 if (!make->disabled)
3962 if (make->all_targets.count)
3964 output( "all:" );
3965 output_filenames_obj_dir( make, make->all_targets );
3966 output( "\n" );
3968 output_install_rules( make, INSTALL_LIB, "install-lib" );
3969 output_install_rules( make, INSTALL_DEV, "install-dev" );
3970 output_uninstall_rules( make );
3973 if (make->dependencies.count)
3975 output_filenames( make->dependencies );
3976 output( ":\n" );
3979 strarray_addall( &make->clean_files, make->object_files );
3980 strarray_addall( &make->clean_files, make->crossobj_files );
3981 strarray_addall( &make->clean_files, make->unixobj_files );
3982 strarray_addall( &make->clean_files, make->res_files );
3983 strarray_addall( &make->clean_files, make->debug_files );
3984 strarray_addall( &make->clean_files, make->all_targets );
3985 strarray_addall( &make->clean_files, make->extra_targets );
3987 if (make->clean_files.count)
3989 output( "%s::\n", obj_dir_path( make, "clean" ));
3990 output( "\trm -f" );
3991 output_filenames_obj_dir( make, make->clean_files );
3992 output( "\n" );
3993 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
3994 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3997 if (make->phony_targets.count)
3999 output( ".PHONY:" );
4000 output_filenames( make->phony_targets );
4001 output( "\n" );
4006 /*******************************************************************
4007 * create_temp_file
4009 static FILE *create_temp_file( const char *orig )
4011 char *name = xmalloc( strlen(orig) + 13 );
4012 unsigned int i, id = getpid();
4013 int fd;
4014 FILE *ret = NULL;
4016 for (i = 0; i < 100; i++)
4018 sprintf( name, "%s.tmp%08x", orig, id );
4019 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
4021 ret = fdopen( fd, "w" );
4022 break;
4024 if (errno != EEXIST) break;
4025 id += 7777;
4027 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
4028 temp_file_name = name;
4029 return ret;
4033 /*******************************************************************
4034 * rename_temp_file
4036 static void rename_temp_file( const char *dest )
4038 int ret = rename( temp_file_name, dest );
4039 if (ret == -1 && errno == EEXIST)
4041 /* rename doesn't overwrite on windows */
4042 unlink( dest );
4043 ret = rename( temp_file_name, dest );
4045 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
4046 temp_file_name = NULL;
4050 /*******************************************************************
4051 * are_files_identical
4053 static int are_files_identical( FILE *file1, FILE *file2 )
4055 for (;;)
4057 char buffer1[8192], buffer2[8192];
4058 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
4059 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
4060 if (size1 != size2) return 0;
4061 if (!size1) return feof( file1 ) && feof( file2 );
4062 if (memcmp( buffer1, buffer2, size1 )) return 0;
4067 /*******************************************************************
4068 * rename_temp_file_if_changed
4070 static void rename_temp_file_if_changed( const char *dest )
4072 FILE *file1, *file2;
4073 int do_rename = 1;
4075 if ((file1 = fopen( dest, "r" )))
4077 if ((file2 = fopen( temp_file_name, "r" )))
4079 do_rename = !are_files_identical( file1, file2 );
4080 fclose( file2 );
4082 fclose( file1 );
4084 if (!do_rename)
4086 unlink( temp_file_name );
4087 temp_file_name = NULL;
4089 else rename_temp_file( dest );
4093 /*******************************************************************
4094 * output_linguas
4096 static void output_linguas( const struct makefile *make )
4098 const char *dest = base_dir_path( make, "LINGUAS" );
4099 struct incl_file *source;
4101 output_file = create_temp_file( dest );
4103 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4104 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
4105 if (strendswith( source->name, ".po" ))
4106 output( "%s\n", replace_extension( source->name, ".po", "" ));
4108 if (fclose( output_file )) fatal_perror( "write" );
4109 output_file = NULL;
4110 rename_temp_file_if_changed( dest );
4114 /*******************************************************************
4115 * output_testlist
4117 static void output_testlist( const struct makefile *make )
4119 const char *dest = base_dir_path( make, "testlist.c" );
4120 struct strarray files = empty_strarray;
4121 unsigned int i;
4123 for (i = 0; i < make->ok_files.count; i++)
4124 strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
4126 output_file = create_temp_file( dest );
4128 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
4129 output( "#define WIN32_LEAN_AND_MEAN\n" );
4130 output( "#include <windows.h>\n\n" );
4131 output( "#define STANDALONE\n" );
4132 output( "#include \"wine/test.h\"\n\n" );
4134 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
4135 output( "\n" );
4136 output( "const struct test winetest_testlist[] =\n" );
4137 output( "{\n" );
4138 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
4139 output( " { 0, 0 }\n" );
4140 output( "};\n" );
4142 if (fclose( output_file )) fatal_perror( "write" );
4143 output_file = NULL;
4144 rename_temp_file_if_changed( dest );
4148 /*******************************************************************
4149 * output_gitignore
4151 static void output_gitignore( const char *dest, struct strarray files )
4153 int i;
4155 output_file = create_temp_file( dest );
4157 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4158 for (i = 0; i < files.count; i++)
4160 if (!strchr( files.str[i], '/' )) output( "/" );
4161 output( "%s\n", files.str[i] );
4164 if (fclose( output_file )) fatal_perror( "write" );
4165 output_file = NULL;
4166 rename_temp_file( dest );
4170 /*******************************************************************
4171 * output_top_variables
4173 static void output_top_variables( const struct makefile *make )
4175 unsigned int i;
4176 struct strarray *vars = &top_makefile->vars;
4178 if (!make->base_dir) return; /* don't output variables in the top makefile */
4180 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
4181 output( "all:\n\n" );
4182 for (i = 0; i < vars->count; i += 2)
4184 if (!strcmp( vars->str[i], "SUBDIRS" )) continue; /* not inherited */
4185 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
4187 output( "\n" );
4191 /*******************************************************************
4192 * output_dependencies
4194 static void output_dependencies( struct makefile *make )
4196 struct strarray ignore_files = empty_strarray;
4197 char buffer[1024];
4198 FILE *src_file;
4199 int found = 0;
4201 if (make->base_dir) create_dir( make->base_dir );
4203 output_file_name = base_dir_path( make, output_makefile_name );
4204 output_file = create_temp_file( output_file_name );
4205 output_top_variables( make );
4207 /* copy the contents of the source makefile */
4208 src_file = open_input_makefile( make );
4209 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4211 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4212 found = !strncmp( buffer, separator, strlen(separator) );
4214 if (fclose( src_file )) fatal_perror( "close" );
4215 input_file_name = NULL;
4217 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4218 output_sources( make );
4220 fclose( output_file );
4221 output_file = NULL;
4222 rename_temp_file( output_file_name );
4224 strarray_addall( &ignore_files, make->distclean_files );
4225 strarray_addall( &ignore_files, make->clean_files );
4226 if (make->testdll) output_testlist( make );
4227 if (make->base_dir && !strcmp( make->base_dir, "po" )) output_linguas( make );
4228 if (!make->src_dir) output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
4230 create_file_directories( make, ignore_files );
4232 output_file_name = NULL;
4236 /*******************************************************************
4237 * load_sources
4239 static void load_sources( struct makefile *make )
4241 static const char *source_vars[] =
4243 "SOURCES",
4244 "C_SRCS",
4245 "OBJC_SRCS",
4246 "RC_SRCS",
4247 "MC_SRCS",
4248 "IDL_SRCS",
4249 "BISON_SRCS",
4250 "LEX_SRCS",
4251 "HEADER_SRCS",
4252 "XTEMPLATE_SRCS",
4253 "SVG_SRCS",
4254 "FONT_SRCS",
4255 "IN_SRCS",
4256 "PO_SRCS",
4257 "MANPAGES",
4258 NULL
4260 const char **var, *crt_dll = NULL;
4261 unsigned int i;
4262 struct strarray value;
4263 struct incl_file *file;
4265 if (root_src_dir)
4267 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
4268 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
4270 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
4271 strarray_set_value( &make->vars, "top_srcdir", top_src_dir_path( make, "" ));
4272 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4274 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4275 make->module = get_expanded_make_variable( make, "MODULE" );
4276 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4277 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4278 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4279 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4281 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4282 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4283 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4284 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4285 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4286 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4287 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4288 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4290 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
4292 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4293 if ((make->module && make->staticlib) || make->testdll || make->is_win16)
4294 strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4296 strarray_addall( &make->extradllflags, get_expanded_make_var_array( make, "APPMODE" ));
4297 make->disabled = make->base_dir && strarray_exists( &disabled_dirs, make->base_dir );
4298 make->use_msvcrt = strarray_exists( &make->extradllflags, "-mno-cygwin" );
4299 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4300 strarray_exists( &make->extradllflags, "-mwindows" );
4302 if (make->module && !make->install_lib.count && !make->install_dev.count)
4304 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4305 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4306 else strarray_add( &make->install_lib, make->module );
4309 make->include_paths = empty_strarray;
4310 make->include_args = empty_strarray;
4311 make->define_args = empty_strarray;
4312 strarray_add( &make->define_args, "-D__WINESRC__" );
4314 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4315 for (i = 0; i < value.count; i++)
4316 if (!strncmp( value.str[i], "-I", 2 ))
4317 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4318 else
4319 strarray_add_uniq( &make->define_args, value.str[i] );
4320 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4322 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4323 if (make->src_dir)
4324 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4325 if (make->parent_dir)
4326 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4327 strarray_add( &make->include_args, strmake( "-I%s", top_obj_dir_path( make, "include" )));
4328 if (make->top_src_dir)
4329 strarray_add( &make->include_args, strmake( "-I%s", top_src_dir_path( make, "include" )));
4331 list_init( &make->sources );
4332 list_init( &make->includes );
4334 for (var = source_vars; *var; var++)
4336 value = get_expanded_make_var_array( make, *var );
4337 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4340 add_generated_sources( make );
4342 if (!make->use_msvcrt && !has_object_file( make ))
4344 strarray_add( &make->extradllflags, "-mno-cygwin" );
4345 make->use_msvcrt = 1;
4348 if (make->use_msvcrt)
4350 unsigned int msvcrt_version = 0;
4351 for (i = 0; i < make->imports.count; i++)
4353 if (strncmp( make->imports.str[i], "msvcr", 5 ) && strncmp( make->imports.str[i], "ucrt", 4 )) continue;
4354 if (crt_dll) fatal_error( "More than one crt DLL imported: %s %s\n", crt_dll, make->imports.str[i] );
4355 crt_dll = make->imports.str[i];
4356 sscanf( crt_dll, "msvcr%u", &msvcrt_version );
4358 if (!crt_dll && !strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
4360 crt_dll = !make->testdll && !make->staticlib ? "ucrtbase" : "msvcrt";
4361 strarray_add( &make->imports, crt_dll );
4363 if (crt_dll && !strncmp( crt_dll, "ucrt", 4 )) strarray_add( &make->define_args, "-D_UCRT" );
4364 else strarray_add( &make->define_args, strmake( "-D_MSVCR_VER=%u", msvcrt_version ));
4367 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4368 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4370 make->is_cross = crosstarget && make->use_msvcrt;
4372 if (make->is_cross)
4374 strarray_addall_uniq( &cross_import_libs, make->imports );
4375 strarray_addall_uniq( &cross_import_libs, make->extra_imports );
4376 if (make->is_win16) strarray_add_uniq( &cross_import_libs, "kernel" );
4377 strarray_add_uniq( &cross_import_libs, "winecrt0" );
4378 strarray_add_uniq( &cross_import_libs, "kernel32" );
4379 strarray_add_uniq( &cross_import_libs, "ntdll" );
4382 if (!*dll_ext || make->is_cross)
4383 for (i = 0; i < make->delayimports.count; i++)
4384 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4388 /*******************************************************************
4389 * parse_makeflags
4391 static void parse_makeflags( const char *flags )
4393 const char *p = flags;
4394 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4396 while (*p)
4398 while (isspace(*p)) p++;
4399 var = buffer;
4400 while (*p && !isspace(*p))
4402 if (*p == '\\' && p[1]) p++;
4403 *var++ = *p++;
4405 *var = 0;
4406 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4411 /*******************************************************************
4412 * parse_option
4414 static int parse_option( const char *opt )
4416 if (opt[0] != '-')
4418 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4419 return 0;
4421 switch(opt[1])
4423 case 'f':
4424 if (opt[2]) output_makefile_name = opt + 2;
4425 break;
4426 case 'R':
4427 relative_dir_mode = 1;
4428 break;
4429 default:
4430 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4431 exit(1);
4433 return 1;
4437 /*******************************************************************
4438 * main
4440 int main( int argc, char *argv[] )
4442 const char *makeflags = getenv( "MAKEFLAGS" );
4443 int i, j;
4445 if (makeflags) parse_makeflags( makeflags );
4447 i = 1;
4448 while (i < argc)
4450 if (parse_option( argv[i] ))
4452 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4453 argc--;
4455 else i++;
4458 if (relative_dir_mode)
4460 char *relpath;
4462 if (argc != 3)
4464 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4465 exit( 1 );
4467 relpath = get_relative_path( argv[1], argv[2] );
4468 printf( "%s\n", relpath ? relpath : "." );
4469 exit( 0 );
4472 atexit( cleanup_files );
4473 signal( SIGTERM, exit_on_signal );
4474 signal( SIGINT, exit_on_signal );
4475 #ifdef SIGHUP
4476 signal( SIGHUP, exit_on_signal );
4477 #endif
4479 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4481 top_makefile = parse_makefile( NULL );
4483 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4484 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4485 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4486 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4487 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4488 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4489 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4490 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4491 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4492 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4493 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4494 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4496 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4497 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
4498 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
4499 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4500 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
4501 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4502 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4503 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4504 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4505 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4506 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4507 bison = get_expanded_make_variable( top_makefile, "BISON" );
4508 ar = get_expanded_make_variable( top_makefile, "AR" );
4509 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4510 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4511 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4512 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4513 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4514 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4515 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4517 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4518 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4519 if (!exe_ext) exe_ext = "";
4520 if (!tools_ext) tools_ext = "";
4521 if (!man_ext) man_ext = "3w";
4523 if (argc == 1)
4525 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4526 top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4527 top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );
4529 for (i = 0; i < top_makefile->subdirs.count; i++)
4530 top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i] );
4532 load_sources( top_makefile );
4533 for (i = 0; i < top_makefile->subdirs.count; i++)
4534 load_sources( top_makefile->submakes[i] );
4536 for (i = 0; i < top_makefile->subdirs.count; i++)
4537 output_dependencies( top_makefile->submakes[i] );
4539 output_dependencies( top_makefile );
4540 return 0;
4543 for (i = 1; i < argc; i++)
4545 struct makefile *make = parse_makefile( argv[i] );
4546 load_sources( make );
4547 output_dependencies( make );
4549 return 0;