gdi32: Move the font cache list out of freetype.c.
[wine.git] / tools / makedep.c
blob2eed620b429aa5cb998fbf737baff1b70b785b06
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013, 2020 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
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 *basename; /* base target name for generated files */
80 char *sourcename; /* source file name for generated headers */
81 struct incl_file *included_by; /* file that included this one */
82 int included_line; /* line where this file was included */
83 enum incl_type type; /* type of include */
84 int use_msvcrt; /* put msvcrt headers in the search path? */
85 struct incl_file *owner;
86 unsigned int files_count; /* files in use */
87 unsigned int files_size; /* total allocated size */
88 struct incl_file **files;
89 struct strarray dependencies; /* file dependencies */
92 #define FLAG_GENERATED 0x000001 /* generated file */
93 #define FLAG_INSTALL 0x000002 /* file to install */
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 subdirs;
144 static struct strarray disabled_dirs;
145 static struct strarray cross_import_libs;
146 static struct strarray delay_import_libs;
147 static struct strarray top_install_lib;
148 static struct strarray top_install_dev;
149 static const char *root_src_dir;
150 static const char *tools_dir;
151 static const char *tools_ext;
152 static const char *exe_ext;
153 static const char *dll_ext;
154 static const char *man_ext;
155 static const char *crosstarget;
156 static const char *crossdebug;
157 static const char *fontforge;
158 static const char *convert;
159 static const char *flex;
160 static const char *bison;
161 static const char *ar;
162 static const char *ranlib;
163 static const char *rsvg;
164 static const char *icotool;
165 static const char *dlltool;
166 static const char *msgfmt;
167 static const char *ln_s;
168 static const char *sed_cmd;
169 static const char *delay_load_flag;
171 struct makefile
173 /* values determined from input makefile */
174 struct strarray vars;
175 struct strarray include_paths;
176 struct strarray include_args;
177 struct strarray define_args;
178 struct strarray programs;
179 struct strarray scripts;
180 struct strarray imports;
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 *src_dir;
190 const char *obj_dir;
191 const char *parent_dir;
192 const char *module;
193 const char *testdll;
194 const char *sharedlib;
195 const char *staticlib;
196 const char *staticimplib;
197 const char *importlib;
198 int disabled;
199 int use_msvcrt;
200 int is_cross;
201 int is_win16;
202 int is_exe;
204 /* values generated at output time */
205 struct strarray in_files;
206 struct strarray ok_files;
207 struct strarray pot_files;
208 struct strarray clean_files;
209 struct strarray distclean_files;
210 struct strarray uninstall_files;
211 struct strarray object_files;
212 struct strarray crossobj_files;
213 struct strarray unixobj_files;
214 struct strarray res_files;
215 struct strarray font_files;
216 struct strarray c2man_files;
217 struct strarray debug_files;
218 struct strarray dlldata_files;
219 struct strarray implib_objs;
220 struct strarray all_targets;
221 struct strarray phony_targets;
222 struct strarray dependencies;
223 struct strarray install_rules[NB_INSTALL_RULES];
226 static struct makefile *top_makefile;
227 static struct makefile **submakes;
229 static const char separator[] = "### Dependencies";
230 static const char *output_makefile_name = "Makefile";
231 static const char *input_file_name;
232 static const char *output_file_name;
233 static const char *temp_file_name;
234 static int relative_dir_mode;
235 static int input_line;
236 static int output_column;
237 static FILE *output_file;
239 static const char Usage[] =
240 "Usage: makedep [options] [directories]\n"
241 "Options:\n"
242 " -R from to Compute the relative path between two directories\n"
243 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
246 #ifndef __GNUC__
247 #define __attribute__(x)
248 #endif
250 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
251 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
252 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
253 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
255 /*******************************************************************
256 * fatal_error
258 static void fatal_error( const char *msg, ... )
260 va_list valist;
261 va_start( valist, msg );
262 if (input_file_name)
264 fprintf( stderr, "%s:", input_file_name );
265 if (input_line) fprintf( stderr, "%d:", input_line );
266 fprintf( stderr, " error: " );
268 else fprintf( stderr, "makedep: error: " );
269 vfprintf( stderr, msg, valist );
270 va_end( valist );
271 exit(1);
275 /*******************************************************************
276 * fatal_perror
278 static void fatal_perror( const char *msg, ... )
280 va_list valist;
281 va_start( valist, msg );
282 if (input_file_name)
284 fprintf( stderr, "%s:", input_file_name );
285 if (input_line) fprintf( stderr, "%d:", input_line );
286 fprintf( stderr, " error: " );
288 else fprintf( stderr, "makedep: error: " );
289 vfprintf( stderr, msg, valist );
290 perror( " " );
291 va_end( valist );
292 exit(1);
296 /*******************************************************************
297 * cleanup_files
299 static void cleanup_files(void)
301 if (temp_file_name) unlink( temp_file_name );
302 if (output_file_name) unlink( output_file_name );
306 /*******************************************************************
307 * exit_on_signal
309 static void exit_on_signal( int sig )
311 exit( 1 ); /* this will call the atexit functions */
315 /*******************************************************************
316 * xmalloc
318 static void *xmalloc( size_t size )
320 void *res;
321 if (!(res = malloc (size ? size : 1)))
322 fatal_error( "Virtual memory exhausted.\n" );
323 return res;
327 /*******************************************************************
328 * xrealloc
330 static void *xrealloc (void *ptr, size_t size)
332 void *res;
333 assert( size );
334 if (!(res = realloc( ptr, size )))
335 fatal_error( "Virtual memory exhausted.\n" );
336 return res;
339 /*******************************************************************
340 * xstrdup
342 static char *xstrdup( const char *str )
344 char *res = strdup( str );
345 if (!res) fatal_error( "Virtual memory exhausted.\n" );
346 return res;
350 /*******************************************************************
351 * strmake
353 static char *strmake( const char* fmt, ... )
355 int n;
356 size_t size = 100;
357 va_list ap;
359 for (;;)
361 char *p = xmalloc (size);
362 va_start(ap, fmt);
363 n = vsnprintf (p, size, fmt, ap);
364 va_end(ap);
365 if (n == -1) size *= 2;
366 else if ((size_t)n >= size) size = n + 1;
367 else return xrealloc( p, n + 1 );
368 free(p);
373 /*******************************************************************
374 * strendswith
376 static int strendswith( const char* str, const char* end )
378 size_t l = strlen( str );
379 size_t m = strlen( end );
381 return l >= m && strcmp(str + l - m, end) == 0;
385 /*******************************************************************
386 * output
388 static void output( const char *format, ... )
390 int ret;
391 va_list valist;
393 va_start( valist, format );
394 ret = vfprintf( output_file, format, valist );
395 va_end( valist );
396 if (ret < 0) fatal_perror( "output" );
397 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
398 else output_column += ret;
402 /*******************************************************************
403 * strarray_add
405 static void strarray_add( struct strarray *array, const char *str )
407 if (array->count == array->size)
409 if (array->size) array->size *= 2;
410 else array->size = 16;
411 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
413 array->str[array->count++] = str;
417 /*******************************************************************
418 * strarray_addall
420 static void strarray_addall( struct strarray *array, struct strarray added )
422 unsigned int i;
424 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
428 /*******************************************************************
429 * strarray_exists
431 static int strarray_exists( const struct strarray *array, const char *str )
433 unsigned int i;
435 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
436 return 0;
440 /*******************************************************************
441 * strarray_add_uniq
443 static void strarray_add_uniq( struct strarray *array, const char *str )
445 if (!strarray_exists( array, str )) strarray_add( array, str );
449 /*******************************************************************
450 * strarray_addall_uniq
452 static void strarray_addall_uniq( struct strarray *array, struct strarray added )
454 unsigned int i;
456 for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
460 /*******************************************************************
461 * strarray_get_value
463 * Find a value in a name/value pair string array.
465 static const char *strarray_get_value( const struct strarray *array, const char *name )
467 int pos, res, min = 0, max = array->count / 2 - 1;
469 while (min <= max)
471 pos = (min + max) / 2;
472 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
473 if (res < 0) min = pos + 1;
474 else max = pos - 1;
476 return NULL;
480 /*******************************************************************
481 * strarray_set_value
483 * Define a value in a name/value pair string array.
485 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
487 int i, pos, res, min = 0, max = array->count / 2 - 1;
489 while (min <= max)
491 pos = (min + max) / 2;
492 if (!(res = strcmp( array->str[pos * 2], name )))
494 /* redefining a variable replaces the previous value */
495 array->str[pos * 2 + 1] = value;
496 return;
498 if (res < 0) min = pos + 1;
499 else max = pos - 1;
501 strarray_add( array, NULL );
502 strarray_add( array, NULL );
503 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
504 array->str[min * 2] = name;
505 array->str[min * 2 + 1] = value;
509 /*******************************************************************
510 * strarray_set_qsort
512 static void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
514 if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
518 /*******************************************************************
519 * output_filename
521 static void output_filename( const char *name )
523 if (output_column + strlen(name) + 1 > 100)
525 output( " \\\n" );
526 output( " " );
528 else if (output_column) output( " " );
529 output( "%s", name );
533 /*******************************************************************
534 * output_filenames
536 static void output_filenames( struct strarray array )
538 unsigned int i;
540 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
544 /*******************************************************************
545 * output_rm_filenames
547 static void output_rm_filenames( struct strarray array )
549 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
550 unsigned int i, len;
552 if (!array.count) return;
553 output( "\trm -f" );
554 for (i = len = 0; i < array.count; i++)
556 if (len > max_cmdline)
558 output( "\n" );
559 output( "\trm -f" );
560 len = 0;
562 output_filename( array.str[i] );
563 len += strlen( array.str[i] ) + 1;
565 output( "\n" );
569 /*******************************************************************
570 * get_extension
572 static char *get_extension( char *filename )
574 char *ext = strrchr( filename, '.' );
575 if (ext && strchr( ext, '/' )) ext = NULL;
576 return ext;
580 /*******************************************************************
581 * get_base_name
583 static const char *get_base_name( const char *name )
585 char *base;
586 if (!strchr( name, '.' )) return name;
587 base = strdup( name );
588 *strrchr( base, '.' ) = 0;
589 return base;
593 /*******************************************************************
594 * replace_extension
596 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
598 char *ret;
599 size_t name_len = strlen( name );
600 size_t ext_len = strlen( old_ext );
602 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
603 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
604 memcpy( ret, name, name_len );
605 strcpy( ret + name_len, new_ext );
606 return ret;
610 /*******************************************************************
611 * replace_filename
613 static char *replace_filename( const char *path, const char *name )
615 const char *p;
616 char *ret;
617 size_t len;
619 if (!path) return xstrdup( name );
620 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
621 len = p - path + 1;
622 ret = xmalloc( len + strlen( name ) + 1 );
623 memcpy( ret, path, len );
624 strcpy( ret + len, name );
625 return ret;
629 /*******************************************************************
630 * strarray_replace_extension
632 static struct strarray strarray_replace_extension( const struct strarray *array,
633 const char *old_ext, const char *new_ext )
635 unsigned int i;
636 struct strarray ret;
638 ret.count = ret.size = array->count;
639 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
640 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
641 return ret;
645 /*******************************************************************
646 * replace_substr
648 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
650 size_t pos = start - str;
651 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
652 memcpy( ret, str, pos );
653 strcpy( ret + pos, replace );
654 strcat( ret + pos, start + len );
655 return ret;
659 /*******************************************************************
660 * get_relative_path
662 * Determine where the destination path is located relative to the 'from' path.
664 static char *get_relative_path( const char *from, const char *dest )
666 const char *start;
667 char *ret, *p;
668 unsigned int dotdots = 0;
670 /* a path of "." is equivalent to an empty path */
671 if (!strcmp( from, "." )) from = "";
673 for (;;)
675 while (*from == '/') from++;
676 while (*dest == '/') dest++;
677 start = dest; /* save start of next path element */
678 if (!*from) break;
680 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
681 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
683 /* count remaining elements in 'from' */
686 dotdots++;
687 while (*from && *from != '/') from++;
688 while (*from == '/') from++;
690 while (*from);
691 break;
694 if (!start[0] && !dotdots) return NULL; /* empty path */
696 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
697 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
699 if (start[0]) strcpy( p, start );
700 else p[-1] = 0; /* remove trailing slash */
701 return ret;
705 /*******************************************************************
706 * concat_paths
708 static char *concat_paths( const char *base, const char *path )
710 int i, len;
711 char *ret;
713 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
714 if (!path || !path[0]) return xstrdup( base );
715 if (path[0] == '/') return xstrdup( path );
717 len = strlen( base );
718 while (len && base[len - 1] == '/') len--;
719 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
721 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
722 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
723 if (i != len - 1 || base[i] != '.')
725 path += 2;
726 while (*path == '/') path++;
728 /* else ignore "." element */
729 while (i > 0 && base[i - 1] == '/') i--;
730 len = i;
732 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
733 ret = xmalloc( len + strlen( path ) + 2 );
734 memcpy( ret, base, len );
735 ret[len++] = '/';
736 strcpy( ret + len, path );
737 return ret;
741 /*******************************************************************
742 * obj_dir_path
744 static char *obj_dir_path( const struct makefile *make, const char *path )
746 return concat_paths( make->obj_dir, path );
750 /*******************************************************************
751 * src_dir_path
753 static char *src_dir_path( const struct makefile *make, const char *path )
755 if (make->src_dir) return concat_paths( make->src_dir, path );
756 return obj_dir_path( make, path );
760 /*******************************************************************
761 * root_src_dir_path
763 static char *root_src_dir_path( const char *path )
765 return concat_paths( root_src_dir, path );
769 /*******************************************************************
770 * tools_dir_path
772 static char *tools_dir_path( const struct makefile *make, const char *path )
774 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
775 return strmake( "tools/%s", path );
779 /*******************************************************************
780 * tools_path
782 static char *tools_path( const struct makefile *make, const char *name )
784 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
788 /*******************************************************************
789 * strarray_addall_path
791 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
793 unsigned int i;
795 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
799 /*******************************************************************
800 * get_line
802 static char *get_line( FILE *file )
804 static char *buffer;
805 static size_t size;
807 if (!size)
809 size = 1024;
810 buffer = xmalloc( size );
812 if (!fgets( buffer, size, file )) return NULL;
813 input_line++;
815 for (;;)
817 char *p = buffer + strlen(buffer);
818 /* if line is larger than buffer, resize buffer */
819 while (p == buffer + size - 1 && p[-1] != '\n')
821 buffer = xrealloc( buffer, size * 2 );
822 if (!fgets( buffer + size - 1, size + 1, file )) break;
823 p = buffer + strlen(buffer);
824 size *= 2;
826 if (p > buffer && p[-1] == '\n')
828 *(--p) = 0;
829 if (p > buffer && p[-1] == '\r') *(--p) = 0;
830 if (p > buffer && p[-1] == '\\')
832 *(--p) = 0;
833 /* line ends in backslash, read continuation line */
834 if (!fgets( p, size - (p - buffer), file )) return buffer;
835 input_line++;
836 continue;
839 return buffer;
844 /*******************************************************************
845 * hash_filename
847 static unsigned int hash_filename( const char *name )
849 /* FNV-1 hash */
850 unsigned int ret = 2166136261u;
851 while (*name) ret = (ret * 16777619) ^ *name++;
852 return ret % HASH_SIZE;
856 /*******************************************************************
857 * add_file
859 static struct file *add_file( const char *name )
861 struct file *file = xmalloc( sizeof(*file) );
862 memset( file, 0, sizeof(*file) );
863 file->name = xstrdup( name );
864 return file;
868 /*******************************************************************
869 * add_dependency
871 static void add_dependency( struct file *file, const char *name, enum incl_type type )
873 /* enforce some rules for the Wine tree */
875 if (!memcmp( name, "../", 3 ))
876 fatal_error( "#include directive with relative path not allowed\n" );
878 if (!strcmp( name, "config.h" ))
880 if (strendswith( file->name, ".h" ))
881 fatal_error( "config.h must not be included by a header file\n" );
882 if (file->deps_count)
883 fatal_error( "config.h must be included before anything else\n" );
885 else if (!strcmp( name, "wine/port.h" ))
887 if (strendswith( file->name, ".h" ))
888 fatal_error( "wine/port.h must not be included by a header file\n" );
889 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
890 if (file->deps_count > 1)
891 fatal_error( "wine/port.h must be included before everything except config.h\n" );
892 if (strcmp( file->deps[0].name, "config.h" ))
893 fatal_error( "config.h must be included before wine/port.h\n" );
896 if (file->deps_count >= file->deps_size)
898 file->deps_size *= 2;
899 if (file->deps_size < 16) file->deps_size = 16;
900 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
902 file->deps[file->deps_count].line = input_line;
903 file->deps[file->deps_count].type = type;
904 file->deps[file->deps_count].name = xstrdup( name );
905 file->deps_count++;
909 /*******************************************************************
910 * find_src_file
912 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
914 struct incl_file *file;
916 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
917 if (!strcmp( name, file->name )) return file;
918 return NULL;
921 /*******************************************************************
922 * find_include_file
924 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
926 struct incl_file *file;
928 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
929 if (!strcmp( name, file->name )) return file;
930 return NULL;
933 /*******************************************************************
934 * add_include
936 * Add an include file if it doesn't already exists.
938 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
939 const char *name, int line, enum incl_type type )
941 struct incl_file *include;
943 if (parent->files_count >= parent->files_size)
945 parent->files_size *= 2;
946 if (parent->files_size < 16) parent->files_size = 16;
947 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
950 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
951 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
952 goto found;
954 include = xmalloc( sizeof(*include) );
955 memset( include, 0, sizeof(*include) );
956 include->name = xstrdup(name);
957 include->included_by = parent;
958 include->included_line = line;
959 include->type = type;
960 include->use_msvcrt = parent->use_msvcrt;
961 list_add_tail( &make->includes, &include->entry );
962 found:
963 parent->files[parent->files_count++] = include;
964 return include;
968 /*******************************************************************
969 * add_generated_source
971 * Add a generated source file to the list.
973 static struct incl_file *add_generated_source( struct makefile *make,
974 const char *name, const char *filename )
976 struct incl_file *file;
978 if ((file = find_src_file( make, name ))) return file; /* we already have it */
979 file = xmalloc( sizeof(*file) );
980 memset( file, 0, sizeof(*file) );
981 file->file = add_file( name );
982 file->name = xstrdup( name );
983 file->basename = xstrdup( filename ? filename : name );
984 file->filename = obj_dir_path( make, file->basename );
985 file->file->flags = FLAG_GENERATED;
986 file->use_msvcrt = make->use_msvcrt;
987 list_add_tail( &make->sources, &file->entry );
988 return file;
992 /*******************************************************************
993 * parse_include_directive
995 static void parse_include_directive( struct file *source, char *str )
997 char quote, *include, *p = str;
999 while (*p && isspace(*p)) p++;
1000 if (*p != '\"' && *p != '<' ) return;
1001 quote = *p++;
1002 if (quote == '<') quote = '>';
1003 include = p;
1004 while (*p && (*p != quote)) p++;
1005 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
1006 *p = 0;
1007 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1011 /*******************************************************************
1012 * parse_pragma_directive
1014 static void parse_pragma_directive( struct file *source, char *str )
1016 char *flag, *p = str;
1018 if (!isspace( *p )) return;
1019 while (*p && isspace(*p)) p++;
1020 p = strtok( p, " \t" );
1021 if (strcmp( p, "makedep" )) return;
1023 while ((flag = strtok( NULL, " \t" )))
1025 if (!strcmp( flag, "depend" ))
1027 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
1028 return;
1030 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
1032 if (strendswith( source->name, ".idl" ))
1034 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
1035 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
1036 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
1037 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
1038 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
1039 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
1040 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
1041 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
1043 else if (strendswith( source->name, ".rc" ))
1045 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
1047 else if (strendswith( source->name, ".sfd" ))
1049 if (!strcmp( flag, "font" ))
1051 struct strarray *array = source->args;
1053 if (!array)
1055 source->args = array = xmalloc( sizeof(*array) );
1056 *array = empty_strarray;
1057 source->flags |= FLAG_SFD_FONTS;
1059 strarray_add( array, xstrdup( strtok( NULL, "" )));
1060 return;
1063 else
1065 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
1066 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
1072 /*******************************************************************
1073 * parse_cpp_directive
1075 static void parse_cpp_directive( struct file *source, char *str )
1077 while (*str && isspace(*str)) str++;
1078 if (*str++ != '#') return;
1079 while (*str && isspace(*str)) str++;
1081 if (!strncmp( str, "include", 7 ))
1082 parse_include_directive( source, str + 7 );
1083 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
1084 parse_include_directive( source, str + 6 );
1085 else if (!strncmp( str, "pragma", 6 ))
1086 parse_pragma_directive( source, str + 6 );
1090 /*******************************************************************
1091 * parse_idl_file
1093 static void parse_idl_file( struct file *source, FILE *file )
1095 char *buffer, *include;
1097 input_line = 0;
1099 while ((buffer = get_line( file )))
1101 char quote;
1102 char *p = buffer;
1103 while (*p && isspace(*p)) p++;
1105 if (!strncmp( p, "importlib", 9 ))
1107 p += 9;
1108 while (*p && isspace(*p)) p++;
1109 if (*p++ != '(') continue;
1110 while (*p && isspace(*p)) p++;
1111 if (*p++ != '"') continue;
1112 include = p;
1113 while (*p && (*p != '"')) p++;
1114 if (!*p) fatal_error( "malformed importlib directive\n" );
1115 *p = 0;
1116 add_dependency( source, include, INCL_IMPORTLIB );
1117 continue;
1120 if (!strncmp( p, "import", 6 ))
1122 p += 6;
1123 while (*p && isspace(*p)) p++;
1124 if (*p != '"') continue;
1125 include = ++p;
1126 while (*p && (*p != '"')) p++;
1127 if (!*p) fatal_error( "malformed import directive\n" );
1128 *p = 0;
1129 add_dependency( source, include, INCL_IMPORT );
1130 continue;
1133 /* check for #include inside cpp_quote */
1134 if (!strncmp( p, "cpp_quote", 9 ))
1136 p += 9;
1137 while (*p && isspace(*p)) p++;
1138 if (*p++ != '(') continue;
1139 while (*p && isspace(*p)) p++;
1140 if (*p++ != '"') continue;
1141 if (*p++ != '#') continue;
1142 while (*p && isspace(*p)) p++;
1143 if (strncmp( p, "include", 7 )) continue;
1144 p += 7;
1145 while (*p && isspace(*p)) p++;
1146 if (*p == '\\' && p[1] == '"')
1148 p += 2;
1149 quote = '"';
1151 else
1153 if (*p++ != '<' ) continue;
1154 quote = '>';
1156 include = p;
1157 while (*p && (*p != quote)) p++;
1158 if (!*p || (quote == '"' && p[-1] != '\\'))
1159 fatal_error( "malformed #include directive inside cpp_quote\n" );
1160 if (quote == '"') p--; /* remove backslash */
1161 *p = 0;
1162 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1163 continue;
1166 parse_cpp_directive( source, p );
1170 /*******************************************************************
1171 * parse_c_file
1173 static void parse_c_file( struct file *source, FILE *file )
1175 char *buffer;
1177 input_line = 0;
1178 while ((buffer = get_line( file )))
1180 parse_cpp_directive( source, buffer );
1185 /*******************************************************************
1186 * parse_rc_file
1188 static void parse_rc_file( struct file *source, FILE *file )
1190 char *buffer, *include;
1192 input_line = 0;
1193 while ((buffer = get_line( file )))
1195 char quote;
1196 char *p = buffer;
1197 while (*p && isspace(*p)) p++;
1199 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1201 p += 2;
1202 while (*p && isspace(*p)) p++;
1203 if (strncmp( p, "@makedep:", 9 )) continue;
1204 p += 9;
1205 while (*p && isspace(*p)) p++;
1206 quote = '"';
1207 if (*p == quote)
1209 include = ++p;
1210 while (*p && *p != quote) p++;
1212 else
1214 include = p;
1215 while (*p && !isspace(*p) && *p != '*') p++;
1217 if (!*p)
1218 fatal_error( "malformed makedep comment\n" );
1219 *p = 0;
1220 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1221 continue;
1224 parse_cpp_directive( source, buffer );
1229 /*******************************************************************
1230 * parse_in_file
1232 static void parse_in_file( struct file *source, FILE *file )
1234 char *p, *buffer;
1236 /* make sure it gets rebuilt when the version changes */
1237 add_dependency( source, "config.h", INCL_SYSTEM );
1239 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1241 input_line = 0;
1242 while ((buffer = get_line( file )))
1244 if (strncmp( buffer, ".TH", 3 )) continue;
1245 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1246 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1247 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1248 source->args = xstrdup( p );
1249 return;
1254 /*******************************************************************
1255 * parse_sfd_file
1257 static void parse_sfd_file( struct file *source, FILE *file )
1259 char *p, *eol, *buffer;
1261 input_line = 0;
1262 while ((buffer = get_line( file )))
1264 if (strncmp( buffer, "UComments:", 10 )) continue;
1265 p = buffer + 10;
1266 while (*p == ' ') p++;
1267 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1269 p++;
1270 buffer[strlen(buffer) - 1] = 0;
1272 while ((eol = strstr( p, "+AAoA" )))
1274 *eol = 0;
1275 while (*p && isspace(*p)) p++;
1276 if (*p++ == '#')
1278 while (*p && isspace(*p)) p++;
1279 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1281 p = eol + 5;
1283 while (*p && isspace(*p)) p++;
1284 if (*p++ != '#') return;
1285 while (*p && isspace(*p)) p++;
1286 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1287 return;
1292 static const struct
1294 const char *ext;
1295 void (*parse)( struct file *file, FILE *f );
1296 } parse_functions[] =
1298 { ".c", parse_c_file },
1299 { ".h", parse_c_file },
1300 { ".inl", parse_c_file },
1301 { ".l", parse_c_file },
1302 { ".m", parse_c_file },
1303 { ".rh", parse_c_file },
1304 { ".x", parse_c_file },
1305 { ".y", parse_c_file },
1306 { ".idl", parse_idl_file },
1307 { ".rc", parse_rc_file },
1308 { ".in", parse_in_file },
1309 { ".sfd", parse_sfd_file }
1312 /*******************************************************************
1313 * load_file
1315 static struct file *load_file( const char *name )
1317 struct file *file;
1318 FILE *f;
1319 unsigned int i, hash = hash_filename( name );
1321 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1322 if (!strcmp( name, file->name )) return file;
1324 if (!(f = fopen( name, "r" ))) return NULL;
1326 file = add_file( name );
1327 list_add_tail( &files[hash], &file->entry );
1328 input_file_name = file->name;
1329 input_line = 0;
1331 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1333 if (!strendswith( name, parse_functions[i].ext )) continue;
1334 parse_functions[i].parse( file, f );
1335 break;
1338 fclose( f );
1339 input_file_name = NULL;
1341 return file;
1345 /*******************************************************************
1346 * open_include_path_file
1348 * Open a file from a directory on the include path.
1350 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1351 const char *name, char **filename )
1353 char *src_path = concat_paths( dir, name );
1354 struct file *ret = load_file( src_path );
1356 if (ret) *filename = src_path;
1357 return ret;
1361 /*******************************************************************
1362 * open_file_same_dir
1364 * Open a file in the same directory as the parent.
1366 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1368 char *src_path = replace_filename( parent->file->name, name );
1369 struct file *ret = load_file( src_path );
1371 if (ret) *filename = replace_filename( parent->filename, name );
1372 return ret;
1376 /*******************************************************************
1377 * open_local_file
1379 * Open a file in the source directory of the makefile.
1381 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1383 char *src_path = src_dir_path( make, path );
1384 struct file *ret = load_file( src_path );
1386 /* if not found, try parent dir */
1387 if (!ret && make->parent_dir)
1389 free( src_path );
1390 path = strmake( "%s/%s", make->parent_dir, path );
1391 src_path = src_dir_path( make, path );
1392 ret = load_file( src_path );
1395 if (ret) *filename = src_path;
1396 return ret;
1400 /*******************************************************************
1401 * open_global_file
1403 * Open a file in the top-level source directory.
1405 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1407 char *src_path = root_src_dir_path( path );
1408 struct file *ret = load_file( src_path );
1410 if (ret) *filename = src_path;
1411 return ret;
1415 /*******************************************************************
1416 * open_global_header
1418 * Open a file in the global include source directory.
1420 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1422 return open_global_file( make, strmake( "include/%s", path ), filename );
1426 /*******************************************************************
1427 * open_src_file
1429 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1431 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1433 if (!file) fatal_perror( "open %s", pFile->name );
1434 return file;
1438 /*******************************************************************
1439 * open_include_file
1441 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1443 struct file *file = NULL;
1444 char *filename;
1445 unsigned int i, len;
1447 errno = ENOENT;
1449 /* check for generated bison header */
1451 if (strendswith( pFile->name, ".tab.h" ) &&
1452 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1454 pFile->sourcename = filename;
1455 pFile->filename = obj_dir_path( make, pFile->name );
1456 return file;
1459 /* check for corresponding idl file in source dir */
1461 if (strendswith( pFile->name, ".h" ) &&
1462 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1464 pFile->sourcename = filename;
1465 pFile->filename = obj_dir_path( make, pFile->name );
1466 return file;
1469 /* check for corresponding tlb file in source dir */
1471 if (strendswith( pFile->name, ".tlb" ) &&
1472 (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1474 pFile->sourcename = filename;
1475 pFile->filename = obj_dir_path( make, pFile->name );
1476 return file;
1479 /* check for extra targets */
1480 if (strarray_exists( &make->extra_targets, pFile->name ))
1482 pFile->sourcename = src_dir_path( make, pFile->name );
1483 pFile->filename = obj_dir_path( make, pFile->name );
1484 return NULL;
1487 /* now try in source dir */
1488 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1490 /* check for corresponding idl file in global includes */
1492 if (strendswith( pFile->name, ".h" ) &&
1493 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1495 pFile->sourcename = filename;
1496 pFile->filename = strmake( "include/%s", pFile->name );
1497 return file;
1500 /* check for corresponding .in file in global includes (for config.h.in) */
1502 if (strendswith( pFile->name, ".h" ) &&
1503 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1505 pFile->sourcename = filename;
1506 pFile->filename = strmake( "include/%s", pFile->name );
1507 return file;
1510 /* check for corresponding .x file in global includes */
1512 if (strendswith( pFile->name, "tmpl.h" ) &&
1513 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1515 pFile->sourcename = filename;
1516 pFile->filename = strmake( "include/%s", pFile->name );
1517 return file;
1520 /* check for corresponding .tlb file in global includes */
1522 if (strendswith( pFile->name, ".tlb" ) &&
1523 (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1525 pFile->sourcename = filename;
1526 pFile->filename = strmake( "include/%s", pFile->name );
1527 return file;
1530 /* check in global includes source dir */
1532 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1534 /* check in global msvcrt includes */
1535 if (pFile->use_msvcrt &&
1536 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1537 return file;
1539 /* now search in include paths */
1540 for (i = 0; i < make->include_paths.count; i++)
1542 const char *dir = make->include_paths.str[i];
1544 if (root_src_dir)
1546 len = strlen( root_src_dir );
1547 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1549 while (dir[len] == '/') len++;
1550 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1551 if (file) return file;
1553 continue; /* ignore paths that don't point to the top source dir */
1555 if (*dir != '/')
1557 if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1558 return file;
1562 if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt)
1564 if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
1565 fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
1566 pFile->included_by->file->name, pFile->included_line, pFile->name );
1567 exit(1);
1570 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1572 /* try in src file directory */
1573 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1575 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1576 perror( pFile->name );
1577 pFile = pFile->included_by;
1578 while (pFile && pFile->included_by)
1580 const char *parent = pFile->included_by->sourcename;
1581 if (!parent) parent = pFile->included_by->file->name;
1582 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1583 parent, pFile->included_line, pFile->name );
1584 pFile = pFile->included_by;
1586 exit(1);
1590 /*******************************************************************
1591 * add_all_includes
1593 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1595 unsigned int i;
1597 parent->files_count = 0;
1598 parent->files_size = file->deps_count;
1599 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1600 for (i = 0; i < file->deps_count; i++)
1602 switch (file->deps[i].type)
1604 case INCL_NORMAL:
1605 case INCL_IMPORT:
1606 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1607 break;
1608 case INCL_IMPORTLIB:
1609 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1610 break;
1611 case INCL_SYSTEM:
1612 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1613 break;
1614 case INCL_CPP_QUOTE:
1615 case INCL_CPP_QUOTE_SYSTEM:
1616 break;
1622 /*******************************************************************
1623 * parse_file
1625 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1627 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1629 if (!file) return;
1631 source->file = file;
1632 source->files_count = 0;
1633 source->files_size = file->deps_count;
1634 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1635 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1636 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1638 if (source->sourcename)
1640 if (strendswith( source->sourcename, ".idl" ))
1642 unsigned int i;
1644 if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
1646 /* generated .h file always includes these */
1647 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1648 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1649 for (i = 0; i < file->deps_count; i++)
1651 switch (file->deps[i].type)
1653 case INCL_IMPORT:
1654 if (strendswith( file->deps[i].name, ".idl" ))
1655 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1656 file->deps[i].line, INCL_NORMAL );
1657 else
1658 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1659 break;
1660 case INCL_CPP_QUOTE:
1661 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1662 break;
1663 case INCL_CPP_QUOTE_SYSTEM:
1664 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1665 break;
1666 case INCL_NORMAL:
1667 case INCL_SYSTEM:
1668 case INCL_IMPORTLIB:
1669 break;
1672 return;
1674 if (strendswith( source->sourcename, ".y" ))
1675 return; /* generated .tab.h doesn't include anything */
1678 add_all_includes( make, source, file );
1682 /*******************************************************************
1683 * add_src_file
1685 * Add a source file to the list.
1687 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1689 struct incl_file *file;
1691 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1692 file = xmalloc( sizeof(*file) );
1693 memset( file, 0, sizeof(*file) );
1694 file->name = xstrdup(name);
1695 file->use_msvcrt = make->use_msvcrt;
1696 list_add_tail( &make->sources, &file->entry );
1697 parse_file( make, file, 1 );
1698 return file;
1702 /*******************************************************************
1703 * open_input_makefile
1705 static FILE *open_input_makefile( const struct makefile *make )
1707 FILE *ret;
1709 if (make->obj_dir)
1710 input_file_name = root_src_dir_path( obj_dir_path( make, strmake( "%s.in", output_makefile_name )));
1711 else
1712 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1714 input_line = 0;
1715 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1716 return ret;
1720 /*******************************************************************
1721 * get_make_variable
1723 static const char *get_make_variable( const struct makefile *make, const char *name )
1725 const char *ret;
1727 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1728 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1729 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1730 return NULL;
1734 /*******************************************************************
1735 * get_expanded_make_variable
1737 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1739 const char *var;
1740 char *p, *end, *expand, *tmp;
1742 var = get_make_variable( make, name );
1743 if (!var) return NULL;
1745 p = expand = xstrdup( var );
1746 while ((p = strchr( p, '$' )))
1748 if (p[1] == '(')
1750 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1751 *end++ = 0;
1752 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1753 var = get_make_variable( make, p + 2 );
1754 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1755 /* switch to the new string */
1756 p = tmp + (p - expand);
1757 free( expand );
1758 expand = tmp;
1760 else if (p[1] == '{') /* don't expand ${} variables */
1762 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1763 p = end + 1;
1765 else if (p[1] == '$')
1767 p += 2;
1769 else fatal_error( "syntax error in '%s'\n", expand );
1772 /* consider empty variables undefined */
1773 p = expand;
1774 while (*p && isspace(*p)) p++;
1775 if (*p) return expand;
1776 free( expand );
1777 return NULL;
1781 /*******************************************************************
1782 * get_expanded_make_var_array
1784 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1786 struct strarray ret = empty_strarray;
1787 char *value, *token;
1789 if ((value = get_expanded_make_variable( make, name )))
1790 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1791 strarray_add( &ret, token );
1792 return ret;
1796 /*******************************************************************
1797 * get_expanded_file_local_var
1799 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1800 const char *name )
1802 char *p, *var = strmake( "%s_%s", file, name );
1804 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1805 return get_expanded_make_var_array( make, var );
1809 /*******************************************************************
1810 * set_make_variable
1812 static int set_make_variable( struct strarray *array, const char *assignment )
1814 char *p, *name;
1816 p = name = xstrdup( assignment );
1817 while (isalnum(*p) || *p == '_') p++;
1818 if (name == p) return 0; /* not a variable */
1819 if (isspace(*p))
1821 *p++ = 0;
1822 while (isspace(*p)) p++;
1824 if (*p != '=') return 0; /* not an assignment */
1825 *p++ = 0;
1826 while (isspace(*p)) p++;
1828 strarray_set_value( array, name, p );
1829 return 1;
1833 /*******************************************************************
1834 * parse_makefile
1836 static struct makefile *parse_makefile( const char *path )
1838 char *buffer;
1839 FILE *file;
1840 struct makefile *make = xmalloc( sizeof(*make) );
1842 memset( make, 0, sizeof(*make) );
1843 make->obj_dir = path;
1844 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1846 file = open_input_makefile( make );
1847 while ((buffer = get_line( file )))
1849 if (!strncmp( buffer, separator, strlen(separator) )) break;
1850 if (*buffer == '\t') continue; /* command */
1851 while (isspace( *buffer )) buffer++;
1852 if (*buffer == '#') continue; /* comment */
1853 set_make_variable( &make->vars, buffer );
1855 fclose( file );
1856 input_file_name = NULL;
1857 return make;
1861 /*******************************************************************
1862 * add_generated_sources
1864 static void add_generated_sources( struct makefile *make )
1866 unsigned int i;
1867 struct incl_file *source, *next, *file;
1868 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1870 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1872 if (source->file->flags & FLAG_IDL_CLIENT)
1874 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1875 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1876 add_all_includes( make, file, file->file );
1878 if (source->file->flags & FLAG_IDL_SERVER)
1880 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1881 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1882 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1883 add_all_includes( make, file, file->file );
1885 if (source->file->flags & FLAG_IDL_IDENT)
1887 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1888 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1889 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1890 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1891 add_all_includes( make, file, file->file );
1893 if (source->file->flags & FLAG_IDL_PROXY)
1895 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1896 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1897 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1898 add_all_includes( make, file, file->file );
1899 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1900 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1901 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1902 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1903 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1904 add_all_includes( make, file, file->file );
1906 if (source->file->flags & FLAG_IDL_TYPELIB)
1908 add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
1910 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1912 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1914 if (source->file->flags & FLAG_IDL_REGISTER)
1916 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1918 if (source->file->flags & FLAG_IDL_HEADER)
1920 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1922 if (!source->file->flags && strendswith( source->name, ".idl" ))
1924 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1926 if (strendswith( source->name, ".x" ))
1928 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1930 if (strendswith( source->name, ".y" ))
1932 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1933 /* steal the includes list from the source file */
1934 file->files_count = source->files_count;
1935 file->files_size = source->files_size;
1936 file->files = source->files;
1937 source->files_count = source->files_size = 0;
1938 source->files = NULL;
1940 if (strendswith( source->name, ".l" ))
1942 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1943 /* steal the includes list from the source file */
1944 file->files_count = source->files_count;
1945 file->files_size = source->files_size;
1946 file->files = source->files;
1947 source->files_count = source->files_size = 0;
1948 source->files = NULL;
1950 if (source->file->flags & FLAG_C_IMPLIB)
1952 if (!make->staticimplib && make->importlib && *dll_ext)
1953 make->staticimplib = strmake( "lib%s.a", make->importlib );
1955 if (strendswith( source->name, ".po" ))
1957 if (!make->disabled)
1958 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1960 if (strendswith( source->name, ".spec" ))
1962 char *obj = replace_extension( source->name, ".spec", "" );
1963 strarray_addall_uniq( &make->extra_imports,
1964 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1967 if (make->testdll)
1969 file = add_generated_source( make, "testlist.o", "testlist.c" );
1970 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1971 add_all_includes( make, file, file->file );
1973 for (i = 0; i < objs.count; i++)
1975 /* default to .c for unknown extra object files */
1976 if (strendswith( objs.str[i], ".o" ))
1978 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1979 if (make->module || make->staticlib) file->file->flags |= FLAG_C_UNIX;
1981 else if (strendswith( objs.str[i], ".res" ))
1982 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1983 else
1984 add_generated_source( make, objs.str[i], NULL );
1989 /*******************************************************************
1990 * create_dir
1992 static void create_dir( const char *dir )
1994 char *p, *path;
1996 p = path = xstrdup( dir );
1997 while ((p = strchr( p, '/' )))
1999 *p = 0;
2000 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
2001 *p++ = '/';
2002 while (*p == '/') p++;
2004 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
2005 free( path );
2009 /*******************************************************************
2010 * create_file_directories
2012 * Create the base directories of all the files.
2014 static void create_file_directories( const struct makefile *make, struct strarray files )
2016 struct strarray subdirs = empty_strarray;
2017 unsigned int i;
2018 char *dir;
2020 for (i = 0; i < files.count; i++)
2022 if (!strchr( files.str[i], '/' )) continue;
2023 dir = obj_dir_path( make, files.str[i] );
2024 *strrchr( dir, '/' ) = 0;
2025 strarray_add_uniq( &subdirs, dir );
2028 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2032 /*******************************************************************
2033 * output_filenames_obj_dir
2035 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
2037 unsigned int i;
2039 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
2043 /*******************************************************************
2044 * get_dependencies
2046 static void get_dependencies( struct incl_file *file, struct incl_file *source )
2048 unsigned int i;
2050 if (!file->filename) return;
2052 if (file != source)
2054 if (file->owner == source) return; /* already processed */
2055 if (file->type == INCL_IMPORTLIB &&
2056 !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
2057 return; /* library is imported only when building a typelib */
2058 file->owner = source;
2059 strarray_add( &source->dependencies, file->filename );
2061 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
2065 /*******************************************************************
2066 * get_local_dependencies
2068 * Get the local dependencies of a given target.
2070 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
2071 struct strarray targets )
2073 unsigned int i;
2074 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2076 for (i = 0; i < deps.count; i++)
2078 if (strarray_exists( &targets, deps.str[i] ))
2079 deps.str[i] = obj_dir_path( make, deps.str[i] );
2080 else
2081 deps.str[i] = src_dir_path( make, deps.str[i] );
2083 return deps;
2087 /*******************************************************************
2088 * get_static_lib
2090 * Check if makefile builds the named static library and return the full lib path.
2092 static const char *get_static_lib( const struct makefile *make, const char *name )
2094 if (!make->staticlib) return NULL;
2095 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2096 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2097 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2098 return obj_dir_path( make, make->staticlib );
2102 /*******************************************************************
2103 * get_parent_makefile
2105 static struct makefile *get_parent_makefile( struct makefile *make )
2107 char *dir, *p;
2108 int i;
2110 if (!make->obj_dir) return NULL;
2111 dir = xstrdup( make->obj_dir );
2112 if (!(p = strrchr( dir, '/' ))) return NULL;
2113 *p = 0;
2114 for (i = 0; i < subdirs.count; i++)
2115 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2116 return NULL;
2120 /*******************************************************************
2121 * needs_cross_lib
2123 static int needs_cross_lib( const struct makefile *make )
2125 const char *name;
2126 if (!crosstarget) return 0;
2127 if (make->importlib) name = make->importlib;
2128 else if (make->staticlib)
2130 name = replace_extension( make->staticlib, ".a", "" );
2131 if (!strncmp( name, "lib", 3 )) name += 3;
2133 else return 0;
2134 if (strarray_exists( &cross_import_libs, name )) return 1;
2135 if (delay_load_flag && strarray_exists( &delay_import_libs, name )) return 1;
2136 return 0;
2140 /*******************************************************************
2141 * needs_delay_lib
2143 static int needs_delay_lib( const struct makefile *make )
2145 if (delay_load_flag) return 0;
2146 if (*dll_ext && !crosstarget) return 0;
2147 if (!make->importlib) return 0;
2148 return strarray_exists( &delay_import_libs, make->importlib );
2152 /*******************************************************************
2153 * needs_implib_symlink
2155 static int needs_implib_symlink( const struct makefile *make )
2157 if (!make->module) return 0;
2158 if (!make->importlib) return 0;
2159 if (make->is_win16 && make->disabled) return 0;
2160 if (strncmp( make->obj_dir, "dlls/", 5 )) return 0;
2161 if (!strcmp( make->module, make->importlib )) return 0;
2162 if (!strchr( make->importlib, '.' ) &&
2163 !strncmp( make->module, make->importlib, strlen( make->importlib )) &&
2164 !strcmp( make->module + strlen( make->importlib ), ".dll" ))
2165 return 0;
2166 return 1;
2170 /*******************************************************************
2171 * add_unix_libraries
2173 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2175 struct strarray ret = empty_strarray;
2176 struct strarray all_libs = empty_strarray;
2177 unsigned int i, j;
2179 strarray_add( &all_libs, "-lwine_port" );
2180 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2181 strarray_addall( &all_libs, libs );
2183 for (i = 0; i < all_libs.count; i++)
2185 const char *lib = NULL;
2187 if (!strncmp( all_libs.str[i], "-l", 2 ))
2189 const char *name = all_libs.str[i] + 2;
2191 for (j = 0; j < subdirs.count; j++)
2192 if ((lib = get_static_lib( submakes[j], name ))) break;
2195 if (lib)
2197 strarray_add( deps, lib );
2198 strarray_add( &ret, lib );
2200 else strarray_add( &ret, all_libs.str[i] );
2202 return ret;
2206 /*******************************************************************
2207 * add_import_libs
2209 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2210 struct strarray imports, int delay, int is_unix )
2212 struct strarray ret = empty_strarray;
2213 unsigned int i, j;
2214 int is_cross = make->is_cross && !is_unix;
2216 for (i = 0; i < imports.count; i++)
2218 const char *name = get_base_name( imports.str[i] );
2219 const char *lib = NULL;
2221 /* skip module's own importlib, its object files will be linked directly */
2222 if (make->importlib && !strcmp( make->importlib, imports.str[i] )) continue;
2224 for (j = 0; j < subdirs.count; j++)
2226 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2228 if (is_cross || !*dll_ext || submakes[j]->staticimplib)
2229 lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
2230 else
2232 strarray_add( deps, strmake( "%s/lib%s.def", submakes[j]->obj_dir, name ));
2233 if (needs_implib_symlink( submakes[j] ))
2234 strarray_add( deps, strmake( "dlls/lib%s.def", name ));
2236 break;
2239 if ((lib = get_static_lib( submakes[j], name ))) break;
2242 if (lib)
2244 const char *ext = NULL;
2246 if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
2247 else if (is_cross) ext = ".cross.a";
2248 if (ext) lib = replace_extension( lib, ".a", ext );
2249 strarray_add( deps, lib );
2250 strarray_add( &ret, lib );
2251 if (needs_implib_symlink( submakes[j] ))
2252 strarray_add( deps, strmake( "dlls/lib%s%s", name, ext ? ext : ".a" ));
2254 else strarray_add( &ret, strmake( "-l%s", name ));
2256 return ret;
2260 /*******************************************************************
2261 * get_default_imports
2263 static struct strarray get_default_imports( const struct makefile *make )
2265 struct strarray ret = empty_strarray;
2267 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
2268 strarray_add( &ret, "winecrt0" );
2269 if (make->is_win16) strarray_add( &ret, "kernel" );
2270 strarray_add( &ret, "kernel32" );
2271 strarray_add( &ret, "ntdll" );
2272 return ret;
2276 /*******************************************************************
2277 * add_crt_import
2279 static const char *add_crt_import( const struct makefile *make, struct strarray *imports )
2281 unsigned int i;
2282 const char *crt_dll = NULL;
2284 for (i = 0; i < imports->count; i++)
2286 if (strncmp( imports->str[i], "msvcr", 5 ) && strncmp( imports->str[i], "ucrt", 4 )) continue;
2287 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n", crt_dll, imports->str[i] );
2288 crt_dll = imports->str[i];
2290 if (!crt_dll && !strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
2292 crt_dll = !make->testdll && !make->staticlib ? "ucrtbase" : "msvcrt";
2293 strarray_add( imports, crt_dll );
2295 return crt_dll;
2299 /*******************************************************************
2300 * add_install_rule
2302 static void add_install_rule( struct makefile *make, const char *target,
2303 const char *file, const char *dest )
2305 if (strarray_exists( &make->install_lib, target ) ||
2306 strarray_exists( &top_install_lib, make->obj_dir ) ||
2307 strarray_exists( &top_install_lib, obj_dir_path( make, target )))
2309 strarray_add( &make->install_rules[INSTALL_LIB], file );
2310 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2312 else if (strarray_exists( &make->install_dev, target ) ||
2313 strarray_exists( &top_install_dev, make->obj_dir ) ||
2314 strarray_exists( &top_install_dev, obj_dir_path( make, target )))
2316 strarray_add( &make->install_rules[INSTALL_DEV], file );
2317 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2322 /*******************************************************************
2323 * get_include_install_path
2325 * Determine the installation path for a given include file.
2327 static const char *get_include_install_path( const char *name )
2329 if (!strncmp( name, "wine/", 5 )) return name + 5;
2330 if (!strncmp( name, "msvcrt/", 7 )) return name;
2331 return strmake( "windows/%s", name );
2335 /*******************************************************************
2336 * get_shared_library_name
2338 * Determine possible names for a shared library with a version number.
2340 static struct strarray get_shared_lib_names( const char *libname )
2342 struct strarray ret = empty_strarray;
2343 const char *ext, *p;
2344 char *name, *first, *second;
2345 size_t len = 0;
2347 strarray_add( &ret, libname );
2349 for (p = libname; (p = strchr( p, '.' )); p++)
2350 if ((len = strspn( p + 1, "0123456789." ))) break;
2352 if (!len) return ret;
2353 ext = p + 1 + len;
2354 if (*ext && ext[-1] == '.') ext--;
2356 /* keep only the first group of digits */
2357 name = xstrdup( libname );
2358 first = name + (p - libname);
2359 if ((second = strchr( first + 1, '.' )))
2361 strcpy( second, ext );
2362 strarray_add( &ret, xstrdup( name ));
2364 /* now remove all digits */
2365 strcpy( first, ext );
2366 strarray_add( &ret, name );
2367 return ret;
2371 /*******************************************************************
2372 * get_source_defines
2374 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2375 const char *obj )
2377 unsigned int i;
2378 struct strarray ret = empty_strarray;
2380 strarray_addall( &ret, make->include_args );
2381 if (source->use_msvcrt)
2382 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2383 for (i = 0; i < make->include_paths.count; i++)
2384 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2385 strarray_addall( &ret, make->define_args );
2386 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2387 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2388 return ret;
2392 /*******************************************************************
2393 * get_debug_file
2395 static const char *get_debug_file( struct makefile *make, const char *name )
2397 const char *debug_file = NULL;
2398 if (!make->is_cross || !crossdebug) return NULL;
2399 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2400 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2401 if (debug_file) strarray_add( &make->debug_files, debug_file );
2402 return debug_file;
2406 /*******************************************************************
2407 * output_winegcc_command
2409 static void output_winegcc_command( struct makefile *make, int is_cross )
2411 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2412 output_filename( "--wine-objdir ." );
2413 if (tools_dir)
2415 output_filename( "--winebuild" );
2416 output_filename( tools_path( make, "winebuild" ));
2418 if (is_cross)
2420 output_filename( "-b" );
2421 output_filename( crosstarget );
2422 output_filename( "--lib-suffix=.cross.a" );
2424 else
2426 output_filenames( target_flags );
2427 output_filenames( lddll_flags );
2432 /*******************************************************************
2433 * output_symlink_rule
2435 * Output a rule to create a symlink.
2437 static void output_symlink_rule( const char *src_name, const char *link_name )
2439 const char *name;
2441 output( "\trm -f %s && ", link_name );
2443 /* dest path with a directory needs special handling if ln -s isn't supported */
2444 if (strcmp( ln_s, "ln -s" ) && ((name = strrchr( link_name, '/' ))))
2446 char *dir = xstrdup( link_name );
2447 dir[name - link_name] = 0;
2448 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2449 free( dir );
2451 else
2453 output( "%s %s %s\n", ln_s, src_name, link_name );
2458 /*******************************************************************
2459 * output_srcdir_symlink
2461 * Output rule to create a symlink back to the source directory, for source files
2462 * that are needed at run-time.
2464 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2466 char *src_file, *dst_file, *src_name;
2468 if (!make->src_dir) return;
2469 src_file = src_dir_path( make, obj );
2470 dst_file = obj_dir_path( make, obj );
2471 output( "%s: %s\n", dst_file, src_file );
2473 src_name = src_file;
2474 if (src_name[0] != '/' && make->obj_dir)
2475 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2477 output_symlink_rule( src_name, dst_file );
2478 strarray_add( &make->all_targets, obj );
2482 /*******************************************************************
2483 * output_install_commands
2485 static void output_install_commands( struct makefile *make, struct strarray files )
2487 unsigned int i;
2488 char *install_sh = root_src_dir_path( "tools/install-sh" );
2490 for (i = 0; i < files.count; i += 2)
2492 const char *file = files.str[i];
2493 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2495 switch (*files.str[i + 1])
2497 case 'c': /* cross-compiled program */
2498 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2499 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2500 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2501 break;
2502 case 'd': /* data file */
2503 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2504 install_sh, obj_dir_path( make, file ), dest );
2505 break;
2506 case 'D': /* data file in source dir */
2507 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2508 install_sh, src_dir_path( make, file ), dest );
2509 break;
2510 case 'p': /* program file */
2511 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2512 install_sh, obj_dir_path( make, file ), dest );
2513 break;
2514 case 's': /* script */
2515 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2516 install_sh, obj_dir_path( make, file ), dest );
2517 break;
2518 case 'S': /* script in source dir */
2519 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2520 install_sh, src_dir_path( make, file ), dest );
2521 break;
2522 case 't': /* script in tools dir */
2523 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2524 install_sh, tools_dir_path( make, files.str[i] ), dest );
2525 break;
2526 case 'y': /* symlink */
2527 output_symlink_rule( files.str[i], dest );
2528 break;
2529 default:
2530 assert(0);
2532 strarray_add( &make->uninstall_files, dest );
2537 /*******************************************************************
2538 * output_install_rules
2540 * Rules are stored as a (file,dest) pair of values.
2541 * The first char of dest indicates the type of install.
2543 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2545 unsigned int i;
2546 struct strarray files = make->install_rules[rules];
2547 struct strarray targets = empty_strarray;
2549 if (!files.count) return;
2551 for (i = 0; i < files.count; i += 2)
2553 const char *file = files.str[i];
2554 switch (*files.str[i + 1])
2556 case 'c': /* cross-compiled program */
2557 case 'd': /* data file */
2558 case 'p': /* program file */
2559 case 's': /* script */
2560 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2561 break;
2562 case 't': /* script in tools dir */
2563 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2564 break;
2568 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
2569 output_filenames( targets );
2570 output( "\n" );
2571 output_install_commands( make, files );
2572 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2573 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
2577 static int cmp_string_length( const char **a, const char **b )
2579 int paths_a = 0, paths_b = 0;
2580 const char *p;
2582 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2583 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2584 if (paths_b != paths_a) return paths_b - paths_a;
2585 return strcmp( *a, *b );
2588 /*******************************************************************
2589 * output_uninstall_rules
2591 static void output_uninstall_rules( struct makefile *make )
2593 static const char *dirs_order[] =
2594 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };
2596 struct strarray uninstall_dirs = empty_strarray;
2597 unsigned int i, j;
2599 if (!make->uninstall_files.count) return;
2600 output( "uninstall::\n" );
2601 output_rm_filenames( make->uninstall_files );
2602 strarray_add_uniq( &make->phony_targets, "uninstall" );
2604 if (!subdirs.count) return;
2605 for (i = 0; i < make->uninstall_files.count; i++)
2607 char *dir = xstrdup( make->uninstall_files.str[i] );
2608 while (strchr( dir, '/' ))
2610 *strrchr( dir, '/' ) = 0;
2611 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2614 strarray_qsort( &uninstall_dirs, cmp_string_length );
2615 output( "\t-rmdir" );
2616 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2618 for (j = 0; j < uninstall_dirs.count; j++)
2620 if (!uninstall_dirs.str[j]) continue;
2621 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2622 continue;
2623 output_filename( uninstall_dirs.str[j] );
2624 uninstall_dirs.str[j] = NULL;
2627 for (j = 0; j < uninstall_dirs.count; j++)
2628 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2629 output( "\n" );
2633 /*******************************************************************
2634 * output_importlib_symlinks
2636 static struct strarray output_importlib_symlinks( const struct makefile *make )
2638 struct strarray ret = empty_strarray;
2639 const char *lib, *dst, *ext[4];
2640 int i, count = 0;
2642 ext[count++] = (*dll_ext && !make->implib_objs.count) ? "def" : "a";
2643 if (needs_delay_lib( make )) ext[count++] = "delay.a";
2644 if (needs_cross_lib( make )) ext[count++] = "cross.a";
2646 for (i = 0; i < count; i++)
2648 lib = strmake( "lib%s.%s", make->importlib, ext[i] );
2649 dst = strmake( "dlls/%s", lib );
2650 output( "%s: %s\n", dst, obj_dir_path( make, lib ));
2651 output_symlink_rule( concat_paths( make->obj_dir + strlen("dlls/"), lib ), dst );
2652 strarray_add( &ret, dst );
2654 return ret;
2658 /*******************************************************************
2659 * output_po_files
2661 static void output_po_files( const struct makefile *make )
2663 const char *po_dir = src_dir_path( make, "po" );
2664 unsigned int i;
2666 if (linguas.count)
2668 for (i = 0; i < linguas.count; i++)
2669 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2670 output( ": %s/wine.pot\n", po_dir );
2671 output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2672 po_dir );
2673 output( "po:" );
2674 for (i = 0; i < linguas.count; i++)
2675 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2676 output( "\n" );
2678 output( "%s/wine.pot:", po_dir );
2679 output_filenames( make->pot_files );
2680 output( "\n" );
2681 output( "\tmsgcat -o $@" );
2682 output_filenames( make->pot_files );
2683 output( "\n" );
2687 /*******************************************************************
2688 * output_source_y
2690 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2692 /* add source file dependency for parallel makes */
2693 char *header = strmake( "%s.tab.h", obj );
2695 if (find_include_file( make, header ))
2697 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2698 output( "\t%s -p %s_ -o %s.tab.c -d %s\n",
2699 bison, obj, obj_dir_path( make, obj ), source->filename );
2700 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2701 source->filename, obj_dir_path( make, header ));
2702 strarray_add( &make->clean_files, header );
2704 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2706 output( "\t%s -p %s_ -o $@ %s\n", bison, obj, source->filename );
2710 /*******************************************************************
2711 * output_source_l
2713 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2715 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2716 output( "\t%s -o$@ %s\n", flex, source->filename );
2720 /*******************************************************************
2721 * output_source_h
2723 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2725 if (source->file->flags & FLAG_GENERATED)
2726 strarray_add( &make->all_targets, source->name );
2727 else
2728 add_install_rule( make, source->name, source->name,
2729 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2733 /*******************************************************************
2734 * output_source_rc
2736 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2738 struct strarray defines = get_source_defines( make, source, obj );
2739 const char *po_dir = NULL;
2740 unsigned int i;
2742 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2743 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2744 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2745 if (source->file->flags & FLAG_RC_PO)
2747 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2748 output( "%s.pot ", obj_dir_path( make, obj ) );
2750 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2751 output_filename( tools_path( make, "wrc" ));
2752 output_filenames( source->dependencies );
2753 output( "\n" );
2754 output( "\t%s -u -o $@", tools_path( make, "wrc" ) );
2755 if (make->is_win16) output_filename( "-m16" );
2756 else output_filenames( target_flags );
2757 output_filename( "--nostdinc" );
2758 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2759 output_filenames( defines );
2760 output_filename( source->filename );
2761 output( "\n" );
2762 if (po_dir)
2764 output( "%s.res:", obj_dir_path( make, obj ));
2765 for (i = 0; i < linguas.count; i++)
2766 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2767 output( "\n" );
2772 /*******************************************************************
2773 * output_source_mc
2775 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2777 unsigned int i;
2778 char *obj_path = obj_dir_path( make, obj );
2780 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2781 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2782 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2783 output_filename( tools_path( make, "wmc" ));
2784 output_filenames( source->dependencies );
2785 output( "\n" );
2786 output( "\t%s -u -o $@ %s", tools_path( make, "wmc" ), source->filename );
2787 if (linguas.count)
2789 output_filename( "--po-dir=po" );
2790 output( "\n" );
2791 output( "%s.res:", obj_dir_path( make, obj ));
2792 for (i = 0; i < linguas.count; i++)
2793 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2795 output( "\n" );
2799 /*******************************************************************
2800 * output_source_res
2802 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2804 strarray_add( &make->res_files, source->name );
2808 /*******************************************************************
2809 * output_source_idl
2811 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2813 struct strarray defines = get_source_defines( make, source, obj );
2814 struct strarray targets = empty_strarray;
2815 char *dest;
2816 unsigned int i;
2818 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2819 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2821 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2823 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2824 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2825 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2826 strarray_add( &targets, dest );
2828 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2829 if (source->file->flags & FLAG_INSTALL)
2831 add_install_rule( make, source->name, xstrdup( source->name ),
2832 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2833 if (source->file->flags & FLAG_IDL_HEADER)
2834 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2835 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2836 if (source->file->flags & FLAG_IDL_TYPELIB)
2837 add_install_rule( make, source->name, strmake( "%s.tlb", obj ),
2838 strmake( "d$(includedir)/wine/%s.tlb", get_include_install_path( obj ) ));
2840 if (!targets.count) return;
2842 output_filenames_obj_dir( make, targets );
2843 output( ": %s\n", tools_path( make, "widl" ));
2844 output( "\t%s -o $@", tools_path( make, "widl" ) );
2845 output_filenames( target_flags );
2846 output_filename( "--nostdinc" );
2847 output_filenames( defines );
2848 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2849 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2850 output_filename( source->filename );
2851 output( "\n" );
2852 output_filenames_obj_dir( make, targets );
2853 output( ": %s", source->filename );
2854 output_filenames( source->dependencies );
2855 output( "\n" );
2859 /*******************************************************************
2860 * output_source_tlb
2862 static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
2864 strarray_add( &make->all_targets, source->name );
2868 /*******************************************************************
2869 * output_source_x
2871 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2873 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2874 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2875 output( "\t%s%s -H -o $@ %s\n",
2876 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2877 if (source->file->flags & FLAG_INSTALL)
2879 add_install_rule( make, source->name, source->name,
2880 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2881 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2882 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2887 /*******************************************************************
2888 * output_source_sfd
2890 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2892 unsigned int i;
2893 char *ttf_obj = strmake( "%s.ttf", obj );
2894 char *ttf_file = src_dir_path( make, ttf_obj );
2896 if (fontforge && !make->src_dir)
2898 output( "%s: %s\n", ttf_file, source->filename );
2899 output( "\t%s -script %s %s $@\n",
2900 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2901 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2903 if (source->file->flags & FLAG_INSTALL)
2905 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2906 output_srcdir_symlink( make, ttf_obj );
2909 if (source->file->flags & FLAG_SFD_FONTS)
2911 struct strarray *array = source->file->args;
2913 for (i = 0; i < array->count; i++)
2915 char *font = strtok( xstrdup(array->str[i]), " \t" );
2916 char *args = strtok( NULL, "" );
2918 strarray_add( &make->all_targets, xstrdup( font ));
2919 output( "%s: %s %s\n", obj_dir_path( make, font ),
2920 tools_path( make, "sfnt2fon" ), ttf_file );
2921 output( "\t%s -q -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2922 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2928 /*******************************************************************
2929 * output_source_svg
2931 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2933 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2934 unsigned int i;
2936 if (convert && rsvg && icotool && !make->src_dir)
2938 for (i = 0; images[i]; i++)
2939 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2941 if (images[i])
2943 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2944 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2945 root_src_dir_path( "tools/buildimage" ), source->filename );
2951 /*******************************************************************
2952 * output_source_nls
2954 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2956 add_install_rule( make, source->name, source->name,
2957 strmake( "D$(nlsdir)/%s", source->name ));
2958 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2962 /*******************************************************************
2963 * output_source_desktop
2965 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2967 add_install_rule( make, source->name, source->name,
2968 strmake( "D$(datadir)/applications/%s", source->name ));
2972 /*******************************************************************
2973 * output_source_po
2975 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2977 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2978 output( "\t%s -o $@ %s\n", msgfmt, source->filename );
2979 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2983 /*******************************************************************
2984 * output_source_in
2986 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2988 unsigned int i;
2990 if (strendswith( obj, ".man" ) && source->file->args)
2992 struct strarray symlinks;
2993 char *dir, *dest = replace_extension( obj, ".man", "" );
2994 char *lang = strchr( dest, '.' );
2995 char *section = source->file->args;
2996 if (lang)
2998 *lang++ = 0;
2999 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3001 else dir = strmake( "$(mandir)/man%s", section );
3002 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
3003 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3004 for (i = 0; i < symlinks.count; i++)
3005 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
3006 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3007 free( dest );
3008 free( dir );
3010 strarray_add( &make->in_files, xstrdup(obj) );
3011 strarray_add( &make->all_targets, xstrdup(obj) );
3012 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3013 output( "\t%s %s >$@ || (rm -f $@ && false)\n", sed_cmd, source->filename );
3014 output( "%s:", obj_dir_path( make, obj ));
3015 output_filenames( source->dependencies );
3016 output( "\n" );
3017 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
3021 /*******************************************************************
3022 * output_source_spec
3024 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3026 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3027 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
3028 struct strarray all_libs, dep_libs = empty_strarray;
3029 char *dll_name, *obj_name, *output_file;
3030 const char *debug_file;
3032 if (!imports.count) imports = make->imports;
3033 else if (make->use_msvcrt) add_crt_import( make, &imports );
3035 if (!dll_flags.count) dll_flags = make->extradllflags;
3036 all_libs = add_import_libs( make, &dep_libs, imports, 0, 0 );
3037 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3038 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
3039 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
3040 output_file = obj_dir_path( make, dll_name );
3042 strarray_add( &make->clean_files, dll_name );
3043 strarray_add( &make->res_files, strmake( "%s.res", obj ));
3044 output( "%s.res: %s\n", obj_dir_path( make, obj ), obj_dir_path( make, dll_name ));
3045 output( "\techo \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", obj, output_file,
3046 tools_path( make, "wrc" ));
3048 output( "%s:", output_file);
3049 output_filename( source->filename );
3050 output_filename( obj_name );
3051 output_filenames( dep_libs );
3052 output_filename( tools_path( make, "winebuild" ));
3053 output_filename( tools_path( make, "winegcc" ));
3054 output( "\n" );
3055 output_winegcc_command( make, make->is_cross );
3056 output_filename( "-s" );
3057 output_filenames( dll_flags );
3058 output_filename( "-shared" );
3059 output_filename( source->filename );
3060 output_filename( obj_name );
3061 if ((debug_file = get_debug_file( make, output_file )))
3062 output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3063 output_filenames( all_libs );
3064 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3065 output( "\n" );
3069 /*******************************************************************
3070 * output_source_default
3072 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3074 struct strarray defines = get_source_defines( make, source, obj );
3075 int is_dll_src = (make->testdll &&
3076 strendswith( source->name, ".c" ) &&
3077 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3078 int need_cross = (crosstarget &&
3079 !(source->file->flags & FLAG_C_UNIX) &&
3080 (make->is_cross ||
3081 ((source->file->flags & FLAG_C_IMPLIB) &&
3082 (needs_cross_lib( make ) || needs_delay_lib( make ))) ||
3083 (make->staticlib && needs_cross_lib( make ))));
3084 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
3085 (!need_cross ||
3086 (source->file->flags & FLAG_C_IMPLIB) ||
3087 (make->module && make->staticlib)));
3089 if ((source->file->flags & FLAG_GENERATED) &&
3090 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3091 strarray_add( &make->clean_files, source->basename );
3092 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
3094 if (need_obj)
3096 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3097 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3098 else if (!is_dll_src && (!(source->file->flags & FLAG_C_IMPLIB) || (make->importlib && strarray_exists( &make->imports, make->importlib ))))
3099 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3100 else
3101 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3102 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3103 output( "\t$(CC) -c -o $@ %s", source->filename );
3104 output_filenames( defines );
3105 if (make->module || make->staticlib || make->sharedlib || make->testdll)
3107 output_filenames( dll_flags );
3108 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3110 output_filenames( extra_cflags );
3111 output_filenames( cpp_flags );
3112 output_filename( "$(CFLAGS)" );
3113 output( "\n" );
3115 if (need_cross)
3117 strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
3118 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3119 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
3120 output_filenames( defines );
3121 output_filenames( extra_cross_cflags );
3122 if (source->file->flags & FLAG_C_IMPLIB) output_filename( "-fno-builtin" );
3123 output_filenames( cpp_flags );
3124 output_filename( "$(CROSSCFLAGS)" );
3125 output( "\n" );
3127 if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
3129 strarray_add( &make->c2man_files, source->filename );
3130 if (make->testdll && !is_dll_src)
3132 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3133 output( "%s.ok:\n", obj_dir_path( make, obj ));
3134 output( "\t%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3135 root_src_dir_path( "tools/runtest" ), make->testdll,
3136 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3137 make->is_cross ? "" : dll_ext, obj );
3140 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3141 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3142 output( ":" );
3143 output_filenames( source->dependencies );
3144 output( "\n" );
3148 /* dispatch table to output rules for a single source file */
3149 static const struct
3151 const char *ext;
3152 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3153 } output_source_funcs[] =
3155 { "y", output_source_y },
3156 { "l", output_source_l },
3157 { "h", output_source_h },
3158 { "rh", output_source_h },
3159 { "inl", output_source_h },
3160 { "rc", output_source_rc },
3161 { "mc", output_source_mc },
3162 { "res", output_source_res },
3163 { "idl", output_source_idl },
3164 { "tlb", output_source_tlb },
3165 { "sfd", output_source_sfd },
3166 { "svg", output_source_svg },
3167 { "nls", output_source_nls },
3168 { "desktop", output_source_desktop },
3169 { "po", output_source_po },
3170 { "in", output_source_in },
3171 { "x", output_source_x },
3172 { "spec", output_source_spec },
3173 { NULL, output_source_default }
3177 /*******************************************************************
3178 * has_object_file
3180 static int has_object_file( struct makefile *make )
3182 struct incl_file *source;
3183 int i;
3185 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3187 char *ext = get_extension( source->name );
3189 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3190 ext++;
3192 for (i = 0; output_source_funcs[i].ext; i++)
3193 if (!strcmp( ext, output_source_funcs[i].ext )) break;
3195 if (!output_source_funcs[i].ext) return 1; /* default extension builds to an object file */
3197 return 0;
3201 /*******************************************************************
3202 * output_man_pages
3204 static void output_man_pages( struct makefile *make )
3206 if (make->c2man_files.count)
3208 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3210 output( "manpages::\n" );
3211 output( "\t%s -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3212 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3213 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3214 output_filename( strmake( "-o documentation/man%s", man_ext ));
3215 output_filenames( make->c2man_files );
3216 output( "\n" );
3217 output( "htmlpages::\n" );
3218 output( "\t%s -Th -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3219 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3220 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3221 output_filename( "-o documentation/html" );
3222 output_filenames( make->c2man_files );
3223 output( "\n" );
3224 output( "sgmlpages::\n" );
3225 output( "\t%s -Ts -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3226 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3227 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3228 output_filename( "-o documentation/api-guide" );
3229 output_filenames( make->c2man_files );
3230 output( "\n" );
3231 output( "xmlpages::\n" );
3232 output( "\t%s -Tx -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3233 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3234 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3235 output_filename( "-o documentation/api-guide-xml" );
3236 output_filenames( make->c2man_files );
3237 output( "\n" );
3238 strarray_add( &make->phony_targets, "manpages" );
3239 strarray_add( &make->phony_targets, "htmlpages" );
3240 strarray_add( &make->phony_targets, "sgmlpages" );
3241 strarray_add( &make->phony_targets, "xmlpages" );
3246 /*******************************************************************
3247 * output_module
3249 static void output_module( struct makefile *make )
3251 struct strarray all_libs = empty_strarray;
3252 struct strarray dep_libs = empty_strarray;
3253 char *module_path = obj_dir_path( make, make->module );
3254 const char *debug_file = NULL;
3255 char *spec_file = NULL;
3256 unsigned int i;
3258 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3259 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, 0 ));
3260 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0, 0 ));
3261 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3263 if (make->is_cross)
3265 if (delay_load_flag)
3267 for (i = 0; i < make->delayimports.count; i++)
3268 strarray_add( &all_libs, strmake( "%s%s%s", delay_load_flag, make->delayimports.str[i],
3269 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3271 strarray_add( &make->all_targets, strmake( "%s", make->module ));
3272 add_install_rule( make, make->module, strmake( "%s", make->module ),
3273 strmake( "c$(dlldir)/%s", make->module ));
3274 debug_file = get_debug_file( make, module_path );
3275 output( "%s:", module_path );
3277 else if (*dll_ext)
3279 if (!make->use_msvcrt) strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3280 for (i = 0; i < make->delayimports.count; i++)
3281 strarray_add( &all_libs, strmake( "-Wl,-delayload,%s%s", make->delayimports.str[i],
3282 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3283 strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
3284 strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
3285 add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
3286 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
3287 add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
3288 strmake( "d$(dlldir)/fakedlls/%s", make->module ));
3289 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
3291 else
3293 strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3294 strarray_add( &make->all_targets, make->module );
3295 add_install_rule( make, make->module, make->module,
3296 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
3297 debug_file = get_debug_file( make, module_path );
3298 output( "%s:", module_path );
3301 if (spec_file) output_filename( spec_file );
3302 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3303 output_filenames_obj_dir( make, make->res_files );
3304 output_filenames( dep_libs );
3305 output_filename( tools_path( make, "winebuild" ));
3306 output_filename( tools_path( make, "winegcc" ));
3307 output( "\n" );
3308 output_winegcc_command( make, make->is_cross );
3309 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3310 if (spec_file)
3312 output_filename( "-shared" );
3313 output_filename( spec_file );
3315 output_filenames( make->extradllflags );
3316 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3317 output_filenames_obj_dir( make, make->res_files );
3318 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3319 output_filenames( all_libs );
3320 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3321 output( "\n" );
3323 if (make->unixobj_files.count)
3325 struct strarray unix_imports = empty_strarray;
3326 struct strarray unix_libs = empty_strarray;
3327 struct strarray unix_deps = empty_strarray;
3328 char *ext, *unix_lib = xmalloc( strlen( make->module ) + strlen( dll_ext ) + 1 );
3329 strcpy( unix_lib, make->module );
3330 if ((ext = get_extension( unix_lib ))) *ext = 0;
3331 strcat( unix_lib, dll_ext );
3333 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
3334 strarray_add( &unix_imports, "ntdll" );
3335 strarray_add( &unix_imports, "winecrt0" );
3337 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 1 ));
3338 strarray_addall( &unix_libs, add_unix_libraries( make, &unix_deps ));
3340 strarray_add( &make->all_targets, unix_lib );
3341 add_install_rule( make, make->module, unix_lib, strmake( "p$(dlldir)/%s", unix_lib ));
3342 output( "%s:", obj_dir_path( make, unix_lib ));
3343 if (spec_file) output_filename( spec_file );
3344 output_filenames_obj_dir( make, make->unixobj_files );
3345 output_filenames( unix_deps );
3346 output_filename( tools_path( make, "winebuild" ));
3347 output_filename( tools_path( make, "winegcc" ));
3348 output( "\n" );
3349 output_winegcc_command( make, 0 );
3350 output_filename( "-munix" );
3351 output_filename( "-shared" );
3352 if (spec_file) output_filename( spec_file );
3353 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) output_filename( "-nodefaultlibs" );
3354 output_filenames_obj_dir( make, make->unixobj_files );
3355 output_filenames( unix_libs );
3356 output_filename( "$(LDFLAGS)" );
3357 output( "\n" );
3360 if (spec_file && make->importlib)
3362 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3363 if (*dll_ext && !make->implib_objs.count)
3365 strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
3366 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
3367 output( "\t%s -w --def -o $@", tools_path( make, "winebuild" ) );
3368 output_filenames( target_flags );
3369 if (make->is_win16) output_filename( "-m16" );
3370 output_filename( "--export" );
3371 output_filename( spec_file );
3372 output( "\n" );
3373 add_install_rule( make, make->importlib,
3374 strmake( "lib%s.def", make->importlib ),
3375 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
3377 else
3379 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3380 if (!*dll_ext && needs_delay_lib( make ))
3382 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3383 output( "%s.delay.a ", importlib_path );
3385 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3386 output_filenames_obj_dir( make, make->implib_objs );
3387 output( "\n" );
3388 output( "\t%s -w --implib -o $@", tools_path( make, "winebuild" ) );
3389 output_filenames( target_flags );
3390 if (make->is_win16) output_filename( "-m16" );
3391 output_filename( "--export" );
3392 output_filename( spec_file );
3393 output_filenames_obj_dir( make, make->implib_objs );
3394 output( "\n" );
3395 add_install_rule( make, make->importlib,
3396 strmake( "lib%s.a", make->importlib ),
3397 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
3399 if (crosstarget && (needs_cross_lib( make ) || needs_delay_lib( make )))
3401 struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
3402 if (needs_cross_lib( make ))
3404 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3405 output_filename( strmake( "%s.cross.a", importlib_path ));
3407 if (needs_delay_lib( make ))
3409 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3410 output_filename( strmake( "%s.delay.a", importlib_path ));
3412 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3413 output_filenames_obj_dir( make, cross_files );
3414 output( "\n" );
3415 output( "\t%s -b %s -w --implib -o $@", tools_path( make, "winebuild" ), crosstarget );
3416 if (make->is_win16) output_filename( "-m16" );
3417 output_filename( "--export" );
3418 output_filename( spec_file );
3419 output_filenames_obj_dir( make, cross_files );
3420 output( "\n" );
3422 if (needs_implib_symlink( make ))
3423 strarray_addall( &top_makefile->clean_files, output_importlib_symlinks( make ));
3426 if (spec_file)
3427 output_man_pages( make );
3428 else if (*dll_ext && !make->is_win16 && strendswith( make->module, ".exe" ))
3430 char *binary = replace_extension( make->module, ".exe", "" );
3431 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3436 /*******************************************************************
3437 * output_static_lib
3439 static void output_static_lib( struct makefile *make )
3441 strarray_add( &make->all_targets, make->staticlib );
3442 output( "%s:", obj_dir_path( make, make->staticlib ));
3443 output_filenames_obj_dir( make, make->object_files );
3444 output_filenames_obj_dir( make, make->unixobj_files );
3445 output( "\n\trm -f $@\n" );
3446 output( "\t%s rc $@", ar );
3447 output_filenames_obj_dir( make, make->object_files );
3448 output_filenames_obj_dir( make, make->unixobj_files );
3449 output( "\n\t%s $@\n", ranlib );
3450 add_install_rule( make, make->staticlib, make->staticlib,
3451 strmake( "d$(dlldir)/%s", make->staticlib ));
3452 if (needs_cross_lib( make ))
3454 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3456 strarray_add( &make->all_targets, name );
3457 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3458 output_filenames_obj_dir( make, make->crossobj_files );
3459 output( "\n" );
3460 output( "\t%s -b %s -w --staticlib -o $@", tools_path( make, "winebuild" ), crosstarget );
3461 output_filenames_obj_dir( make, make->crossobj_files );
3462 output( "\n" );
3467 /*******************************************************************
3468 * output_shared_lib
3470 static void output_shared_lib( struct makefile *make )
3472 unsigned int i;
3473 char *basename, *p;
3474 struct strarray names = get_shared_lib_names( make->sharedlib );
3475 struct strarray all_libs = empty_strarray;
3476 struct strarray dep_libs = empty_strarray;
3478 basename = xstrdup( make->sharedlib );
3479 if ((p = strchr( basename, '.' ))) *p = 0;
3481 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3482 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3483 strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3485 output( "%s:", obj_dir_path( make, make->sharedlib ));
3486 output_filenames_obj_dir( make, make->object_files );
3487 output_filenames( dep_libs );
3488 output( "\n" );
3489 output( "\t$(CC) -o $@" );
3490 output_filenames_obj_dir( make, make->object_files );
3491 output_filenames( all_libs );
3492 output_filename( "$(LDFLAGS)" );
3493 output( "\n" );
3494 add_install_rule( make, make->sharedlib, make->sharedlib,
3495 strmake( "p$(libdir)/%s", make->sharedlib ));
3496 for (i = 1; i < names.count; i++)
3498 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3499 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ));
3500 add_install_rule( make, names.str[i], names.str[i-1],
3501 strmake( "y$(libdir)/%s", names.str[i] ));
3503 strarray_addall( &make->all_targets, names );
3507 /*******************************************************************
3508 * output_test_module
3510 static void output_test_module( struct makefile *make )
3512 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3513 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3514 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3515 struct strarray dep_libs = empty_strarray;
3516 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0, 0 );
3517 struct makefile *parent = get_parent_makefile( make );
3518 const char *ext = make->is_cross ? "" : dll_ext;
3519 const char *parent_ext = parent && parent->is_cross ? "" : dll_ext;
3520 const char *debug_file;
3521 char *output_file;
3523 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3524 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3525 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3526 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3527 output( "%s:\n", output_file );
3528 output_winegcc_command( make, make->is_cross );
3529 output_filenames( make->extradllflags );
3530 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3531 output_filenames_obj_dir( make, make->res_files );
3532 if ((debug_file = get_debug_file( make, output_file )))
3533 output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3534 output_filenames( all_libs );
3535 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3536 output( "\n" );
3537 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3538 output_winegcc_command( make, make->is_cross );
3539 output_filename( "-s" );
3540 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3541 output_filenames( make->extradllflags );
3542 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3543 output_filenames_obj_dir( make, make->res_files );
3544 output_filenames( all_libs );
3545 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3546 output( "\n" );
3547 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3548 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3549 output_filenames_obj_dir( make, make->res_files );
3550 output_filenames( dep_libs );
3551 output_filename( tools_path( make, "winebuild" ));
3552 output_filename( tools_path( make, "winegcc" ));
3553 output( "\n" );
3555 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3556 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n",
3557 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3559 output_filenames_obj_dir( make, make->ok_files );
3560 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3561 if (parent) output( " %s%s", obj_dir_path( parent, make->testdll ), parent_ext );
3562 output( "\n" );
3563 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3564 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3565 output( "\n" );
3566 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3567 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3568 output( "%s::\n", obj_dir_path( make, "testclean" ));
3569 output( "\trm -f" );
3570 output_filenames_obj_dir( make, make->ok_files );
3571 output( "\n" );
3572 strarray_addall( &make->clean_files, make->ok_files );
3573 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3577 /*******************************************************************
3578 * output_programs
3580 static void output_programs( struct makefile *make )
3582 unsigned int i, j;
3584 for (i = 0; i < make->programs.count; i++)
3586 char *program_installed = NULL;
3587 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3588 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3589 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3590 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3591 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3593 if (!objs.count) objs = make->object_files;
3594 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3595 strarray_addall( &all_libs, add_unix_libraries( make, &deps ));
3597 output( "%s:", obj_dir_path( make, program ) );
3598 output_filenames_obj_dir( make, objs );
3599 output_filenames( deps );
3600 output( "\n" );
3601 output( "\t$(CC) -o $@" );
3602 output_filenames_obj_dir( make, objs );
3603 output_filenames( all_libs );
3604 output_filename( "$(LDFLAGS)" );
3605 output( "\n" );
3606 strarray_add( &make->all_targets, program );
3608 for (j = 0; j < symlinks.count; j++)
3610 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3611 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ));
3613 strarray_addall( &make->all_targets, symlinks );
3615 add_install_rule( make, program, program_installed ? program_installed : program,
3616 strmake( "p$(bindir)/%s", program ));
3617 for (j = 0; j < symlinks.count; j++)
3618 add_install_rule( make, symlinks.str[j], program,
3619 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3624 /*******************************************************************
3625 * output_subdirs
3627 static void output_subdirs( struct makefile *make )
3629 struct strarray all_targets = empty_strarray;
3630 struct strarray makefile_deps = empty_strarray;
3631 struct strarray clean_files = empty_strarray;
3632 struct strarray testclean_files = empty_strarray;
3633 struct strarray distclean_files = empty_strarray;
3634 struct strarray dependencies = empty_strarray;
3635 struct strarray install_lib_deps = empty_strarray;
3636 struct strarray install_dev_deps = empty_strarray;
3637 struct strarray tooldeps_deps = empty_strarray;
3638 struct strarray buildtest_deps = empty_strarray;
3639 unsigned int i;
3641 strarray_addall( &clean_files, make->clean_files );
3642 strarray_addall( &distclean_files, make->distclean_files );
3643 strarray_addall( &all_targets, make->all_targets );
3644 for (i = 0; i < subdirs.count; i++)
3646 strarray_add( &makefile_deps, src_dir_path( submakes[i], strmake ( "%s.in", output_makefile_name )));
3647 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3648 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3649 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3650 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3651 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3652 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3653 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3655 if (submakes[i]->disabled) continue;
3657 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3658 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3659 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3660 if (submakes[i]->testdll)
3661 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3662 if (submakes[i]->install_rules[INSTALL_LIB].count)
3663 strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
3664 if (submakes[i]->install_rules[INSTALL_DEV].count)
3665 strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
3667 strarray_addall( &dependencies, makefile_deps );
3668 output( "all:" );
3669 output_filenames( all_targets );
3670 output( "\n" );
3671 output( "Makefile:" );
3672 output_filenames( makefile_deps );
3673 output( "\n" );
3674 output_filenames( dependencies );
3675 output( ":\n" );
3676 if (install_lib_deps.count)
3678 output( "install install-lib::" );
3679 output_filenames( install_lib_deps );
3680 output( "\n" );
3681 strarray_add_uniq( &make->phony_targets, "install" );
3682 strarray_add_uniq( &make->phony_targets, "install-lib" );
3684 if (install_dev_deps.count)
3686 output( "install install-dev::" );
3687 output_filenames( install_dev_deps );
3688 output( "\n" );
3689 strarray_add_uniq( &make->phony_targets, "install" );
3690 strarray_add_uniq( &make->phony_targets, "install-dev" );
3692 output_uninstall_rules( make );
3693 if (buildtest_deps.count)
3695 output( "buildtests:" );
3696 output_filenames( buildtest_deps );
3697 output( "\n" );
3698 strarray_add_uniq( &make->phony_targets, "buildtests" );
3700 output( "check test:" );
3701 output_filenames( testclean_files );
3702 output( "\n" );
3703 strarray_add_uniq( &make->phony_targets, "check" );
3704 strarray_add_uniq( &make->phony_targets, "test" );
3706 output( "clean::\n");
3707 output_rm_filenames( clean_files );
3708 output( "testclean::\n");
3709 output_rm_filenames( testclean_files );
3710 output( "distclean::\n");
3711 output_rm_filenames( distclean_files );
3712 strarray_add_uniq( &make->phony_targets, "distclean" );
3713 strarray_add_uniq( &make->phony_targets, "testclean" );
3715 if (tooldeps_deps.count)
3717 output( "__tooldeps__:" );
3718 output_filenames( tooldeps_deps );
3719 output( "\n" );
3720 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3723 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3725 if (make->phony_targets.count)
3727 output( ".PHONY:" );
3728 output_filenames( make->phony_targets );
3729 output( "\n" );
3734 /*******************************************************************
3735 * output_sources
3737 static void output_sources( struct makefile *make )
3739 struct strarray all_targets = empty_strarray;
3740 struct incl_file *source;
3741 unsigned int i, j;
3743 strarray_add_uniq( &make->phony_targets, "all" );
3745 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3747 char *obj = xstrdup( source->name );
3748 char *ext = get_extension( obj );
3750 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3751 *ext++ = 0;
3753 for (j = 0; output_source_funcs[j].ext; j++)
3754 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3756 output_source_funcs[j].fn( make, source, obj );
3757 strarray_addall_uniq( &make->dependencies, source->dependencies );
3760 /* special case for winetest: add resource files from other test dirs */
3761 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3763 struct strarray tests = enable_tests;
3764 if (!tests.count)
3765 for (i = 0; i < subdirs.count; i++)
3766 if (submakes[i]->testdll && !submakes[i]->disabled)
3767 strarray_add( &tests, submakes[i]->testdll );
3768 for (i = 0; i < tests.count; i++)
3769 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3772 if (make->dlldata_files.count)
3774 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3775 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3776 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
3777 output_filenames( make->dlldata_files );
3778 output( "\n" );
3781 if (make->staticlib) output_static_lib( make );
3782 else if (make->module) output_module( make );
3783 else if (make->testdll) output_test_module( make );
3784 else if (make->sharedlib) output_shared_lib( make );
3785 else if (make->programs.count) output_programs( make );
3787 for (i = 0; i < make->scripts.count; i++)
3788 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3789 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3791 for (i = 0; i < make->extra_targets.count; i++)
3792 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3793 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3794 else
3795 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3797 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3798 strarray_add( &make->distclean_files, "Makefile" );
3799 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3801 if (!make->obj_dir)
3802 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3803 else if (!strcmp( make->obj_dir, "po" ))
3804 strarray_add( &make->distclean_files, "LINGUAS" );
3806 strarray_addall( &make->clean_files, make->object_files );
3807 strarray_addall( &make->clean_files, make->crossobj_files );
3808 strarray_addall( &make->clean_files, make->unixobj_files );
3809 strarray_addall( &make->clean_files, make->res_files );
3810 strarray_addall( &make->clean_files, make->pot_files );
3811 strarray_addall( &make->clean_files, make->debug_files );
3812 strarray_addall( &make->clean_files, make->all_targets );
3814 if (make == top_makefile)
3816 output_subdirs( make );
3817 return;
3820 strarray_addall( &all_targets, make->all_targets );
3821 strarray_addall( &all_targets, make->font_files );
3822 if (all_targets.count)
3824 output( "%s:", obj_dir_path( make, "all" ));
3825 output_filenames_obj_dir( make, all_targets );
3826 output( "\n" );
3827 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3829 output_install_rules( make, INSTALL_LIB, "install-lib" );
3830 output_install_rules( make, INSTALL_DEV, "install-dev" );
3832 if (make->clean_files.count)
3834 output( "%s::\n", obj_dir_path( make, "clean" ));
3835 output( "\trm -f" );
3836 output_filenames_obj_dir( make, make->clean_files );
3837 output( "\n" );
3838 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3843 /*******************************************************************
3844 * create_temp_file
3846 static FILE *create_temp_file( const char *orig )
3848 char *name = xmalloc( strlen(orig) + 13 );
3849 unsigned int i, id = getpid();
3850 int fd;
3851 FILE *ret = NULL;
3853 for (i = 0; i < 100; i++)
3855 sprintf( name, "%s.tmp%08x", orig, id );
3856 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3858 ret = fdopen( fd, "w" );
3859 break;
3861 if (errno != EEXIST) break;
3862 id += 7777;
3864 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3865 temp_file_name = name;
3866 return ret;
3870 /*******************************************************************
3871 * rename_temp_file
3873 static void rename_temp_file( const char *dest )
3875 int ret = rename( temp_file_name, dest );
3876 if (ret == -1 && errno == EEXIST)
3878 /* rename doesn't overwrite on windows */
3879 unlink( dest );
3880 ret = rename( temp_file_name, dest );
3882 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3883 temp_file_name = NULL;
3887 /*******************************************************************
3888 * are_files_identical
3890 static int are_files_identical( FILE *file1, FILE *file2 )
3892 for (;;)
3894 char buffer1[8192], buffer2[8192];
3895 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3896 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3897 if (size1 != size2) return 0;
3898 if (!size1) return feof( file1 ) && feof( file2 );
3899 if (memcmp( buffer1, buffer2, size1 )) return 0;
3904 /*******************************************************************
3905 * rename_temp_file_if_changed
3907 static void rename_temp_file_if_changed( const char *dest )
3909 FILE *file1, *file2;
3910 int do_rename = 1;
3912 if ((file1 = fopen( dest, "r" )))
3914 if ((file2 = fopen( temp_file_name, "r" )))
3916 do_rename = !are_files_identical( file1, file2 );
3917 fclose( file2 );
3919 fclose( file1 );
3921 if (!do_rename)
3923 unlink( temp_file_name );
3924 temp_file_name = NULL;
3926 else rename_temp_file( dest );
3930 /*******************************************************************
3931 * output_linguas
3933 static void output_linguas( const struct makefile *make )
3935 const char *dest = obj_dir_path( make, "LINGUAS" );
3936 struct incl_file *source;
3938 output_file = create_temp_file( dest );
3940 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3941 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3942 if (strendswith( source->name, ".po" ))
3943 output( "%s\n", replace_extension( source->name, ".po", "" ));
3945 if (fclose( output_file )) fatal_perror( "write" );
3946 output_file = NULL;
3947 rename_temp_file_if_changed( dest );
3951 /*******************************************************************
3952 * output_testlist
3954 static void output_testlist( const struct makefile *make )
3956 const char *dest = obj_dir_path( make, "testlist.c" );
3957 struct strarray files = empty_strarray;
3958 unsigned int i;
3960 for (i = 0; i < make->ok_files.count; i++)
3961 strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
3963 output_file = create_temp_file( dest );
3965 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3966 output( "#define WIN32_LEAN_AND_MEAN\n" );
3967 output( "#include <windows.h>\n\n" );
3968 output( "#define STANDALONE\n" );
3969 output( "#include \"wine/test.h\"\n\n" );
3971 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
3972 output( "\n" );
3973 output( "const struct test winetest_testlist[] =\n" );
3974 output( "{\n" );
3975 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
3976 output( " { 0, 0 }\n" );
3977 output( "};\n" );
3979 if (fclose( output_file )) fatal_perror( "write" );
3980 output_file = NULL;
3981 rename_temp_file_if_changed( dest );
3985 /*******************************************************************
3986 * output_gitignore
3988 static void output_gitignore( const char *dest, struct strarray files )
3990 int i;
3992 output_file = create_temp_file( dest );
3994 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3995 for (i = 0; i < files.count; i++)
3997 if (!strchr( files.str[i], '/' )) output( "/" );
3998 output( "%s\n", files.str[i] );
4001 if (fclose( output_file )) fatal_perror( "write" );
4002 output_file = NULL;
4003 rename_temp_file( dest );
4007 /*******************************************************************
4008 * output_stub_makefile
4010 static void output_stub_makefile( struct makefile *make )
4012 struct strarray targets = empty_strarray;
4013 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
4015 if (make_var) output( "MAKE = %s\n\n", make_var );
4016 output( "all:\n" );
4018 if (make->all_targets.count) strarray_add( &targets, "all" );
4019 if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
4020 strarray_add( &targets, "install" );
4021 if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
4022 if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
4023 if (make->clean_files.count) strarray_add( &targets, "clean" );
4024 if (make->ok_files.count)
4026 strarray_add( &targets, "check" );
4027 strarray_add( &targets, "test" );
4028 strarray_add( &targets, "testclean" );
4031 output_filenames( targets );
4032 output_filenames( make->clean_files );
4033 output( ":\n" );
4034 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4035 output( ".PHONY:" );
4036 output_filenames( targets );
4037 output( "\n" );
4041 /*******************************************************************
4042 * output_dependencies
4044 static void output_dependencies( struct makefile *make )
4046 struct strarray ignore_files = empty_strarray;
4047 char buffer[1024];
4048 FILE *src_file;
4049 int i, found = 0;
4051 if (make->obj_dir) create_dir( make->obj_dir );
4053 output_file_name = obj_dir_path( make, output_makefile_name );
4054 output_file = create_temp_file( output_file_name );
4056 /* copy the contents of the source makefile */
4057 src_file = open_input_makefile( make );
4058 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4060 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4061 found = !strncmp( buffer, separator, strlen(separator) );
4063 if (fclose( src_file )) fatal_perror( "close" );
4064 input_file_name = NULL;
4066 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4068 if (make == top_makefile)
4070 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4071 output_sources( make );
4073 else output_stub_makefile( make );
4075 fclose( output_file );
4076 output_file = NULL;
4077 rename_temp_file( output_file_name );
4079 strarray_addall( &ignore_files, make->distclean_files );
4080 strarray_addall( &ignore_files, make->clean_files );
4081 if (make->testdll) output_testlist( make );
4082 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4083 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4085 create_file_directories( make, ignore_files );
4087 output_file_name = NULL;
4091 /*******************************************************************
4092 * load_sources
4094 static void load_sources( struct makefile *make )
4096 static const char *source_vars[] =
4098 "SOURCES",
4099 "C_SRCS",
4100 "OBJC_SRCS",
4101 "RC_SRCS",
4102 "MC_SRCS",
4103 "IDL_SRCS",
4104 "BISON_SRCS",
4105 "LEX_SRCS",
4106 "HEADER_SRCS",
4107 "XTEMPLATE_SRCS",
4108 "SVG_SRCS",
4109 "FONT_SRCS",
4110 "IN_SRCS",
4111 "PO_SRCS",
4112 "MANPAGES",
4113 NULL
4115 const char **var;
4116 unsigned int i;
4117 struct strarray value;
4118 struct incl_file *file;
4120 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4121 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4123 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4124 make->module = get_expanded_make_variable( make, "MODULE" );
4125 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4126 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4127 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4128 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4130 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4131 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4132 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4133 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4134 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4135 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4136 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4137 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4139 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
4141 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4142 if ((make->module && make->staticlib) || make->testdll || make->is_win16)
4143 strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4145 strarray_addall( &make->extradllflags, get_expanded_make_var_array( make, "APPMODE" ));
4146 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4147 make->use_msvcrt = strarray_exists( &make->extradllflags, "-mno-cygwin" );
4148 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4149 strarray_exists( &make->extradllflags, "-mwindows" );
4151 if (make->module && !make->install_lib.count && !make->install_dev.count)
4153 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4154 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4155 else strarray_add( &make->install_lib, make->module );
4158 make->include_paths = empty_strarray;
4159 make->include_args = empty_strarray;
4160 make->define_args = empty_strarray;
4161 strarray_add( &make->define_args, "-D__WINESRC__" );
4163 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4164 for (i = 0; i < value.count; i++)
4165 if (!strncmp( value.str[i], "-I", 2 ))
4166 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4167 else
4168 strarray_add_uniq( &make->define_args, value.str[i] );
4169 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4171 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4172 if (make->src_dir)
4173 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4174 if (make->parent_dir)
4175 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4176 strarray_add( &make->include_args, "-Iinclude" );
4177 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4179 list_init( &make->sources );
4180 list_init( &make->includes );
4182 for (var = source_vars; *var; var++)
4184 value = get_expanded_make_var_array( make, *var );
4185 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4188 add_generated_sources( make );
4190 if (!make->use_msvcrt && !has_object_file( make ))
4192 strarray_add( &make->extradllflags, "-mno-cygwin" );
4193 make->use_msvcrt = 1;
4196 if (make->use_msvcrt)
4198 const char *crt_dll = add_crt_import( make, &make->imports );
4200 if (crt_dll && !strncmp( crt_dll, "ucrt", 4 )) strarray_add( &make->define_args, "-D_UCRT" );
4201 else
4203 unsigned int msvcrt_version = 0;
4205 if (crt_dll) sscanf( crt_dll, "msvcr%u", &msvcrt_version );
4206 strarray_add( &make->define_args, strmake( "-D_MSVCR_VER=%u", msvcrt_version ));
4210 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4211 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4213 make->is_cross = crosstarget && make->use_msvcrt;
4215 if (make->is_cross)
4217 strarray_addall_uniq( &cross_import_libs, make->imports );
4218 strarray_addall_uniq( &cross_import_libs, make->extra_imports );
4219 if (make->is_win16) strarray_add_uniq( &cross_import_libs, "kernel" );
4220 strarray_add_uniq( &cross_import_libs, "winecrt0" );
4221 strarray_add_uniq( &cross_import_libs, "kernel32" );
4222 strarray_add_uniq( &cross_import_libs, "ntdll" );
4225 if (!*dll_ext || make->is_cross)
4226 for (i = 0; i < make->delayimports.count; i++)
4227 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4231 /*******************************************************************
4232 * parse_makeflags
4234 static void parse_makeflags( const char *flags )
4236 const char *p = flags;
4237 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4239 while (*p)
4241 while (isspace(*p)) p++;
4242 var = buffer;
4243 while (*p && !isspace(*p))
4245 if (*p == '\\' && p[1]) p++;
4246 *var++ = *p++;
4248 *var = 0;
4249 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4254 /*******************************************************************
4255 * parse_option
4257 static int parse_option( const char *opt )
4259 if (opt[0] != '-')
4261 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4262 return 0;
4264 switch(opt[1])
4266 case 'f':
4267 if (opt[2]) output_makefile_name = opt + 2;
4268 break;
4269 case 'R':
4270 relative_dir_mode = 1;
4271 break;
4272 default:
4273 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4274 exit(1);
4276 return 1;
4280 /*******************************************************************
4281 * main
4283 int main( int argc, char *argv[] )
4285 const char *makeflags = getenv( "MAKEFLAGS" );
4286 int i, j;
4288 if (makeflags) parse_makeflags( makeflags );
4290 i = 1;
4291 while (i < argc)
4293 if (parse_option( argv[i] ))
4295 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4296 argc--;
4298 else i++;
4301 if (relative_dir_mode)
4303 char *relpath;
4305 if (argc != 3)
4307 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4308 exit( 1 );
4310 relpath = get_relative_path( argv[1], argv[2] );
4311 printf( "%s\n", relpath ? relpath : "." );
4312 exit( 0 );
4315 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4317 atexit( cleanup_files );
4318 signal( SIGTERM, exit_on_signal );
4319 signal( SIGINT, exit_on_signal );
4320 #ifdef SIGHUP
4321 signal( SIGHUP, exit_on_signal );
4322 #endif
4324 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4326 top_makefile = parse_makefile( NULL );
4328 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4329 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4330 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4331 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4332 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4333 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4334 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4335 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4336 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4337 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4338 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4339 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4341 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4342 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
4343 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
4344 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4345 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
4346 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4347 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4348 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4349 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4350 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4351 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4352 bison = get_expanded_make_variable( top_makefile, "BISON" );
4353 ar = get_expanded_make_variable( top_makefile, "AR" );
4354 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4355 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4356 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4357 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4358 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4359 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4360 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4362 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4363 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4364 if (!exe_ext) exe_ext = "";
4365 if (!tools_ext) tools_ext = "";
4366 if (!man_ext) man_ext = "3w";
4368 top_makefile->src_dir = root_src_dir;
4369 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4370 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4371 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4373 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4375 load_sources( top_makefile );
4376 for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
4378 output_dependencies( top_makefile );
4379 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4381 return 0;