inetcpl.cpl: Trace the actual AutoConfigURL and not the value name.
[wine.git] / tools / makedep.c
blob90a21ef825c62203c02f8f1a9621442a78dc527d
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, int create_dir )
2439 const char *name = strrchr( link_name, '/' );
2440 char *dir = NULL;
2442 if (name)
2444 dir = xstrdup( link_name );
2445 dir[name - link_name] = 0;
2448 output( "\t" );
2449 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2450 output( "rm -f %s && ", link_name );
2452 /* dest path with a directory needs special handling if ln -s isn't supported */
2453 if (dir && strcmp( ln_s, "ln -s" ))
2454 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2455 else
2456 output( "%s %s %s\n", ln_s, src_name, link_name );
2458 free( dir );
2462 /*******************************************************************
2463 * output_srcdir_symlink
2465 * Output rule to create a symlink back to the source directory, for source files
2466 * that are needed at run-time.
2468 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2470 char *src_file, *dst_file, *src_name;
2472 if (!make->src_dir) return;
2473 src_file = src_dir_path( make, obj );
2474 dst_file = obj_dir_path( make, obj );
2475 output( "%s: %s\n", dst_file, src_file );
2477 src_name = src_file;
2478 if (src_name[0] != '/' && make->obj_dir)
2479 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2481 output_symlink_rule( src_name, dst_file, 0 );
2482 strarray_add( &make->all_targets, obj );
2486 /*******************************************************************
2487 * output_install_commands
2489 static void output_install_commands( struct makefile *make, struct strarray files )
2491 unsigned int i;
2492 char *install_sh = root_src_dir_path( "tools/install-sh" );
2494 for (i = 0; i < files.count; i += 2)
2496 const char *file = files.str[i];
2497 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2499 switch (*files.str[i + 1])
2501 case 'c': /* cross-compiled program */
2502 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2503 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2504 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2505 break;
2506 case 'd': /* data file */
2507 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2508 install_sh, obj_dir_path( make, file ), dest );
2509 break;
2510 case 'D': /* data file in source dir */
2511 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2512 install_sh, src_dir_path( make, file ), dest );
2513 break;
2514 case 'p': /* program file */
2515 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2516 install_sh, obj_dir_path( make, file ), dest );
2517 break;
2518 case 's': /* script */
2519 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2520 install_sh, obj_dir_path( make, file ), dest );
2521 break;
2522 case 'S': /* script in source dir */
2523 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2524 install_sh, src_dir_path( make, file ), dest );
2525 break;
2526 case 't': /* script in tools dir */
2527 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2528 install_sh, tools_dir_path( make, files.str[i] ), dest );
2529 break;
2530 case 'y': /* symlink */
2531 output_symlink_rule( files.str[i], dest, 1 );
2532 break;
2533 default:
2534 assert(0);
2536 strarray_add( &make->uninstall_files, dest );
2541 /*******************************************************************
2542 * output_install_rules
2544 * Rules are stored as a (file,dest) pair of values.
2545 * The first char of dest indicates the type of install.
2547 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2549 unsigned int i;
2550 struct strarray files = make->install_rules[rules];
2551 struct strarray targets = empty_strarray;
2553 if (!files.count) return;
2555 for (i = 0; i < files.count; i += 2)
2557 const char *file = files.str[i];
2558 switch (*files.str[i + 1])
2560 case 'c': /* cross-compiled program */
2561 case 'd': /* data file */
2562 case 'p': /* program file */
2563 case 's': /* script */
2564 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2565 break;
2566 case 't': /* script in tools dir */
2567 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2568 break;
2572 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
2573 output_filenames( targets );
2574 output( "\n" );
2575 output_install_commands( make, files );
2576 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2577 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
2581 static int cmp_string_length( const char **a, const char **b )
2583 int paths_a = 0, paths_b = 0;
2584 const char *p;
2586 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2587 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2588 if (paths_b != paths_a) return paths_b - paths_a;
2589 return strcmp( *a, *b );
2592 /*******************************************************************
2593 * output_uninstall_rules
2595 static void output_uninstall_rules( struct makefile *make )
2597 static const char *dirs_order[] =
2598 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };
2600 struct strarray uninstall_dirs = empty_strarray;
2601 unsigned int i, j;
2603 if (!make->uninstall_files.count) return;
2604 output( "uninstall::\n" );
2605 output_rm_filenames( make->uninstall_files );
2606 strarray_add_uniq( &make->phony_targets, "uninstall" );
2608 if (!subdirs.count) return;
2609 for (i = 0; i < make->uninstall_files.count; i++)
2611 char *dir = xstrdup( make->uninstall_files.str[i] );
2612 while (strchr( dir, '/' ))
2614 *strrchr( dir, '/' ) = 0;
2615 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2618 strarray_qsort( &uninstall_dirs, cmp_string_length );
2619 output( "\t-rmdir" );
2620 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2622 for (j = 0; j < uninstall_dirs.count; j++)
2624 if (!uninstall_dirs.str[j]) continue;
2625 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2626 continue;
2627 output_filename( uninstall_dirs.str[j] );
2628 uninstall_dirs.str[j] = NULL;
2631 for (j = 0; j < uninstall_dirs.count; j++)
2632 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2633 output( "\n" );
2637 /*******************************************************************
2638 * output_importlib_symlinks
2640 static struct strarray output_importlib_symlinks( const struct makefile *make )
2642 struct strarray ret = empty_strarray;
2643 const char *lib, *dst, *ext[4];
2644 int i, count = 0;
2646 ext[count++] = (*dll_ext && !make->implib_objs.count) ? "def" : "a";
2647 if (needs_delay_lib( make )) ext[count++] = "delay.a";
2648 if (needs_cross_lib( make )) ext[count++] = "cross.a";
2650 for (i = 0; i < count; i++)
2652 lib = strmake( "lib%s.%s", make->importlib, ext[i] );
2653 dst = strmake( "dlls/%s", lib );
2654 output( "%s: %s\n", dst, obj_dir_path( make, lib ));
2655 output_symlink_rule( concat_paths( make->obj_dir + strlen("dlls/"), lib ), dst, 0 );
2656 strarray_add( &ret, dst );
2658 return ret;
2662 /*******************************************************************
2663 * output_po_files
2665 static void output_po_files( const struct makefile *make )
2667 const char *po_dir = src_dir_path( make, "po" );
2668 unsigned int i;
2670 if (linguas.count)
2672 for (i = 0; i < linguas.count; i++)
2673 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2674 output( ": %s/wine.pot\n", po_dir );
2675 output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2676 po_dir );
2677 output( "po:" );
2678 for (i = 0; i < linguas.count; i++)
2679 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2680 output( "\n" );
2682 output( "%s/wine.pot:", po_dir );
2683 output_filenames( make->pot_files );
2684 output( "\n" );
2685 output( "\tmsgcat -o $@" );
2686 output_filenames( make->pot_files );
2687 output( "\n" );
2691 /*******************************************************************
2692 * output_source_y
2694 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2696 /* add source file dependency for parallel makes */
2697 char *header = strmake( "%s.tab.h", obj );
2699 if (find_include_file( make, header ))
2701 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2702 output( "\t%s -p %s_ -o %s.tab.c -d %s\n",
2703 bison, obj, obj_dir_path( make, obj ), source->filename );
2704 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2705 source->filename, obj_dir_path( make, header ));
2706 strarray_add( &make->clean_files, header );
2708 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2710 output( "\t%s -p %s_ -o $@ %s\n", bison, obj, source->filename );
2714 /*******************************************************************
2715 * output_source_l
2717 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2719 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2720 output( "\t%s -o$@ %s\n", flex, source->filename );
2724 /*******************************************************************
2725 * output_source_h
2727 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2729 if (source->file->flags & FLAG_GENERATED)
2730 strarray_add( &make->all_targets, source->name );
2731 else
2732 add_install_rule( make, source->name, source->name,
2733 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2737 /*******************************************************************
2738 * output_source_rc
2740 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2742 struct strarray defines = get_source_defines( make, source, obj );
2743 const char *po_dir = NULL;
2744 unsigned int i;
2746 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2747 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2748 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2749 if (source->file->flags & FLAG_RC_PO)
2751 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2752 output( "%s.pot ", obj_dir_path( make, obj ) );
2754 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2755 output_filename( tools_path( make, "wrc" ));
2756 output_filenames( source->dependencies );
2757 output( "\n" );
2758 output( "\t%s -u -o $@", tools_path( make, "wrc" ) );
2759 if (make->is_win16) output_filename( "-m16" );
2760 else output_filenames( target_flags );
2761 output_filename( "--nostdinc" );
2762 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2763 output_filenames( defines );
2764 output_filename( source->filename );
2765 output( "\n" );
2766 if (po_dir)
2768 output( "%s.res:", obj_dir_path( make, obj ));
2769 for (i = 0; i < linguas.count; i++)
2770 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2771 output( "\n" );
2776 /*******************************************************************
2777 * output_source_mc
2779 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2781 unsigned int i;
2782 char *obj_path = obj_dir_path( make, obj );
2784 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2785 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2786 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2787 output_filename( tools_path( make, "wmc" ));
2788 output_filenames( source->dependencies );
2789 output( "\n" );
2790 output( "\t%s -u -o $@ %s", tools_path( make, "wmc" ), source->filename );
2791 if (linguas.count)
2793 output_filename( "--po-dir=po" );
2794 output( "\n" );
2795 output( "%s.res:", obj_dir_path( make, obj ));
2796 for (i = 0; i < linguas.count; i++)
2797 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2799 output( "\n" );
2803 /*******************************************************************
2804 * output_source_res
2806 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2808 strarray_add( &make->res_files, source->name );
2812 /*******************************************************************
2813 * output_source_idl
2815 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2817 struct strarray defines = get_source_defines( make, source, obj );
2818 struct strarray targets = empty_strarray;
2819 char *dest;
2820 unsigned int i;
2822 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2823 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2825 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2827 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2828 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2829 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2830 strarray_add( &targets, dest );
2832 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2833 if (source->file->flags & FLAG_INSTALL)
2835 add_install_rule( make, source->name, xstrdup( source->name ),
2836 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2837 if (source->file->flags & FLAG_IDL_HEADER)
2838 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2839 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2840 if (source->file->flags & FLAG_IDL_TYPELIB)
2841 add_install_rule( make, source->name, strmake( "%s.tlb", obj ),
2842 strmake( "d$(includedir)/wine/%s.tlb", get_include_install_path( obj ) ));
2844 if (!targets.count) return;
2846 output_filenames_obj_dir( make, targets );
2847 output( ": %s\n", tools_path( make, "widl" ));
2848 output( "\t%s -o $@", tools_path( make, "widl" ) );
2849 output_filenames( target_flags );
2850 output_filename( "--nostdinc" );
2851 output_filenames( defines );
2852 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2853 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2854 output_filename( source->filename );
2855 output( "\n" );
2856 output_filenames_obj_dir( make, targets );
2857 output( ": %s", source->filename );
2858 output_filenames( source->dependencies );
2859 output( "\n" );
2863 /*******************************************************************
2864 * output_source_tlb
2866 static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
2868 strarray_add( &make->all_targets, source->name );
2872 /*******************************************************************
2873 * output_source_x
2875 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2877 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2878 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2879 output( "\t%s%s -H -o $@ %s\n",
2880 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2881 if (source->file->flags & FLAG_INSTALL)
2883 add_install_rule( make, source->name, source->name,
2884 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2885 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2886 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2891 /*******************************************************************
2892 * output_source_sfd
2894 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2896 unsigned int i;
2897 char *ttf_obj = strmake( "%s.ttf", obj );
2898 char *ttf_file = src_dir_path( make, ttf_obj );
2900 if (fontforge && !make->src_dir)
2902 output( "%s: %s\n", ttf_file, source->filename );
2903 output( "\t%s -script %s %s $@\n",
2904 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2905 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2907 if (source->file->flags & FLAG_INSTALL)
2909 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2910 output_srcdir_symlink( make, ttf_obj );
2913 if (source->file->flags & FLAG_SFD_FONTS)
2915 struct strarray *array = source->file->args;
2917 for (i = 0; i < array->count; i++)
2919 char *font = strtok( xstrdup(array->str[i]), " \t" );
2920 char *args = strtok( NULL, "" );
2922 strarray_add( &make->all_targets, xstrdup( font ));
2923 output( "%s: %s %s\n", obj_dir_path( make, font ),
2924 tools_path( make, "sfnt2fon" ), ttf_file );
2925 output( "\t%s -q -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2926 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2932 /*******************************************************************
2933 * output_source_svg
2935 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2937 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2938 unsigned int i;
2940 if (convert && rsvg && icotool && !make->src_dir)
2942 for (i = 0; images[i]; i++)
2943 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2945 if (images[i])
2947 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2948 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2949 root_src_dir_path( "tools/buildimage" ), source->filename );
2955 /*******************************************************************
2956 * output_source_nls
2958 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2960 add_install_rule( make, source->name, source->name,
2961 strmake( "D$(nlsdir)/%s", source->name ));
2962 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2966 /*******************************************************************
2967 * output_source_desktop
2969 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2971 add_install_rule( make, source->name, source->name,
2972 strmake( "D$(datadir)/applications/%s", source->name ));
2976 /*******************************************************************
2977 * output_source_po
2979 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2981 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2982 output( "\t%s -o $@ %s\n", msgfmt, source->filename );
2983 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2987 /*******************************************************************
2988 * output_source_in
2990 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2992 unsigned int i;
2994 if (strendswith( obj, ".man" ) && source->file->args)
2996 struct strarray symlinks;
2997 char *dir, *dest = replace_extension( obj, ".man", "" );
2998 char *lang = strchr( dest, '.' );
2999 char *section = source->file->args;
3000 if (lang)
3002 *lang++ = 0;
3003 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3005 else dir = strmake( "$(mandir)/man%s", section );
3006 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
3007 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3008 for (i = 0; i < symlinks.count; i++)
3009 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
3010 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3011 free( dest );
3012 free( dir );
3014 strarray_add( &make->in_files, xstrdup(obj) );
3015 strarray_add( &make->all_targets, xstrdup(obj) );
3016 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3017 output( "\t%s %s >$@ || (rm -f $@ && false)\n", sed_cmd, source->filename );
3018 output( "%s:", obj_dir_path( make, obj ));
3019 output_filenames( source->dependencies );
3020 output( "\n" );
3021 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
3025 /*******************************************************************
3026 * output_source_spec
3028 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3030 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3031 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
3032 struct strarray all_libs, dep_libs = empty_strarray;
3033 char *dll_name, *obj_name, *output_file;
3034 const char *debug_file;
3036 if (!imports.count) imports = make->imports;
3037 else if (make->use_msvcrt) add_crt_import( make, &imports );
3039 if (!dll_flags.count) dll_flags = make->extradllflags;
3040 all_libs = add_import_libs( make, &dep_libs, imports, 0, 0 );
3041 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3042 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
3043 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
3044 output_file = obj_dir_path( make, dll_name );
3046 strarray_add( &make->clean_files, dll_name );
3047 strarray_add( &make->res_files, strmake( "%s.res", obj ));
3048 output( "%s.res: %s\n", obj_dir_path( make, obj ), obj_dir_path( make, dll_name ));
3049 output( "\techo \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", obj, output_file,
3050 tools_path( make, "wrc" ));
3052 output( "%s:", output_file);
3053 output_filename( source->filename );
3054 output_filename( obj_name );
3055 output_filenames( dep_libs );
3056 output_filename( tools_path( make, "winebuild" ));
3057 output_filename( tools_path( make, "winegcc" ));
3058 output( "\n" );
3059 output_winegcc_command( make, make->is_cross );
3060 output_filename( "-s" );
3061 output_filenames( dll_flags );
3062 output_filename( "-shared" );
3063 output_filename( source->filename );
3064 output_filename( obj_name );
3065 if ((debug_file = get_debug_file( make, output_file )))
3066 output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3067 output_filenames( all_libs );
3068 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3069 output( "\n" );
3073 /*******************************************************************
3074 * output_source_default
3076 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3078 struct strarray defines = get_source_defines( make, source, obj );
3079 int is_dll_src = (make->testdll &&
3080 strendswith( source->name, ".c" ) &&
3081 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3082 int need_cross = (crosstarget &&
3083 !(source->file->flags & FLAG_C_UNIX) &&
3084 (make->is_cross ||
3085 ((source->file->flags & FLAG_C_IMPLIB) &&
3086 (needs_cross_lib( make ) || needs_delay_lib( make ))) ||
3087 (make->staticlib && needs_cross_lib( make ))));
3088 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
3089 (!need_cross ||
3090 (source->file->flags & FLAG_C_IMPLIB) ||
3091 (make->module && make->staticlib)));
3093 if ((source->file->flags & FLAG_GENERATED) &&
3094 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3095 strarray_add( &make->clean_files, source->basename );
3096 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
3098 if (need_obj)
3100 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3101 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3102 else if (!is_dll_src && (!(source->file->flags & FLAG_C_IMPLIB) || (make->importlib && strarray_exists( &make->imports, make->importlib ))))
3103 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3104 else
3105 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3106 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3107 output( "\t$(CC) -c -o $@ %s", source->filename );
3108 output_filenames( defines );
3109 if (make->module || make->staticlib || make->sharedlib || make->testdll)
3111 output_filenames( dll_flags );
3112 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3114 output_filenames( extra_cflags );
3115 output_filenames( cpp_flags );
3116 output_filename( "$(CFLAGS)" );
3117 output( "\n" );
3119 if (need_cross)
3121 strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
3122 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3123 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
3124 output_filenames( defines );
3125 output_filenames( extra_cross_cflags );
3126 if (source->file->flags & FLAG_C_IMPLIB) output_filename( "-fno-builtin" );
3127 output_filenames( cpp_flags );
3128 output_filename( "$(CROSSCFLAGS)" );
3129 output( "\n" );
3131 if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
3133 strarray_add( &make->c2man_files, source->filename );
3134 if (make->testdll && !is_dll_src)
3136 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3137 output( "%s.ok:\n", obj_dir_path( make, obj ));
3138 output( "\t%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3139 root_src_dir_path( "tools/runtest" ), make->testdll,
3140 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3141 make->is_cross ? "" : dll_ext, obj );
3144 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3145 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3146 output( ":" );
3147 output_filenames( source->dependencies );
3148 output( "\n" );
3152 /* dispatch table to output rules for a single source file */
3153 static const struct
3155 const char *ext;
3156 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3157 } output_source_funcs[] =
3159 { "y", output_source_y },
3160 { "l", output_source_l },
3161 { "h", output_source_h },
3162 { "rh", output_source_h },
3163 { "inl", output_source_h },
3164 { "rc", output_source_rc },
3165 { "mc", output_source_mc },
3166 { "res", output_source_res },
3167 { "idl", output_source_idl },
3168 { "tlb", output_source_tlb },
3169 { "sfd", output_source_sfd },
3170 { "svg", output_source_svg },
3171 { "nls", output_source_nls },
3172 { "desktop", output_source_desktop },
3173 { "po", output_source_po },
3174 { "in", output_source_in },
3175 { "x", output_source_x },
3176 { "spec", output_source_spec },
3177 { NULL, output_source_default }
3181 /*******************************************************************
3182 * has_object_file
3184 static int has_object_file( struct makefile *make )
3186 struct incl_file *source;
3187 int i;
3189 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3191 char *ext = get_extension( source->name );
3193 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3194 ext++;
3196 for (i = 0; output_source_funcs[i].ext; i++)
3197 if (!strcmp( ext, output_source_funcs[i].ext )) break;
3199 if (!output_source_funcs[i].ext) return 1; /* default extension builds to an object file */
3201 return 0;
3205 /*******************************************************************
3206 * output_man_pages
3208 static void output_man_pages( struct makefile *make )
3210 if (make->c2man_files.count)
3212 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3214 output( "manpages::\n" );
3215 output( "\t%s -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3216 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3217 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3218 output_filename( strmake( "-o documentation/man%s", man_ext ));
3219 output_filenames( make->c2man_files );
3220 output( "\n" );
3221 output( "htmlpages::\n" );
3222 output( "\t%s -Th -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3223 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3224 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3225 output_filename( "-o documentation/html" );
3226 output_filenames( make->c2man_files );
3227 output( "\n" );
3228 output( "sgmlpages::\n" );
3229 output( "\t%s -Ts -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3230 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3231 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3232 output_filename( "-o documentation/api-guide" );
3233 output_filenames( make->c2man_files );
3234 output( "\n" );
3235 output( "xmlpages::\n" );
3236 output( "\t%s -Tx -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3237 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3238 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3239 output_filename( "-o documentation/api-guide-xml" );
3240 output_filenames( make->c2man_files );
3241 output( "\n" );
3242 strarray_add( &make->phony_targets, "manpages" );
3243 strarray_add( &make->phony_targets, "htmlpages" );
3244 strarray_add( &make->phony_targets, "sgmlpages" );
3245 strarray_add( &make->phony_targets, "xmlpages" );
3250 /*******************************************************************
3251 * output_module
3253 static void output_module( struct makefile *make )
3255 struct strarray all_libs = empty_strarray;
3256 struct strarray dep_libs = empty_strarray;
3257 char *module_path = obj_dir_path( make, make->module );
3258 const char *debug_file = NULL;
3259 char *spec_file = NULL;
3260 unsigned int i;
3262 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3263 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, 0 ));
3264 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0, 0 ));
3265 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3267 if (make->is_cross)
3269 if (delay_load_flag)
3271 for (i = 0; i < make->delayimports.count; i++)
3272 strarray_add( &all_libs, strmake( "%s%s%s", delay_load_flag, make->delayimports.str[i],
3273 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3275 strarray_add( &make->all_targets, strmake( "%s", make->module ));
3276 add_install_rule( make, make->module, strmake( "%s", make->module ),
3277 strmake( "c$(dlldir)/%s", make->module ));
3278 debug_file = get_debug_file( make, module_path );
3279 output( "%s:", module_path );
3281 else if (*dll_ext)
3283 if (!make->use_msvcrt) strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3284 for (i = 0; i < make->delayimports.count; i++)
3285 strarray_add( &all_libs, strmake( "-Wl,-delayload,%s%s", make->delayimports.str[i],
3286 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3287 strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
3288 strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
3289 add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
3290 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
3291 add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
3292 strmake( "d$(dlldir)/fakedlls/%s", make->module ));
3293 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
3295 else
3297 strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3298 strarray_add( &make->all_targets, make->module );
3299 add_install_rule( make, make->module, make->module,
3300 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
3301 debug_file = get_debug_file( make, module_path );
3302 output( "%s:", module_path );
3305 if (spec_file) output_filename( spec_file );
3306 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3307 output_filenames_obj_dir( make, make->res_files );
3308 output_filenames( dep_libs );
3309 output_filename( tools_path( make, "winebuild" ));
3310 output_filename( tools_path( make, "winegcc" ));
3311 output( "\n" );
3312 output_winegcc_command( make, make->is_cross );
3313 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3314 if (spec_file)
3316 output_filename( "-shared" );
3317 output_filename( spec_file );
3319 output_filenames( make->extradllflags );
3320 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3321 output_filenames_obj_dir( make, make->res_files );
3322 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3323 output_filenames( all_libs );
3324 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3325 output( "\n" );
3327 if (make->unixobj_files.count)
3329 struct strarray unix_imports = empty_strarray;
3330 struct strarray unix_libs = empty_strarray;
3331 struct strarray unix_deps = empty_strarray;
3332 char *ext, *unix_lib = xmalloc( strlen( make->module ) + strlen( dll_ext ) + 1 );
3333 strcpy( unix_lib, make->module );
3334 if ((ext = get_extension( unix_lib ))) *ext = 0;
3335 strcat( unix_lib, dll_ext );
3337 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
3338 strarray_add( &unix_imports, "ntdll" );
3339 strarray_add( &unix_imports, "winecrt0" );
3341 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 1 ));
3342 strarray_addall( &unix_libs, add_unix_libraries( make, &unix_deps ));
3344 strarray_add( &make->all_targets, unix_lib );
3345 add_install_rule( make, make->module, unix_lib, strmake( "p$(dlldir)/%s", unix_lib ));
3346 output( "%s:", obj_dir_path( make, unix_lib ));
3347 if (spec_file) output_filename( spec_file );
3348 output_filenames_obj_dir( make, make->unixobj_files );
3349 output_filenames( unix_deps );
3350 output_filename( tools_path( make, "winebuild" ));
3351 output_filename( tools_path( make, "winegcc" ));
3352 output( "\n" );
3353 output_winegcc_command( make, 0 );
3354 output_filename( "-munix" );
3355 output_filename( "-shared" );
3356 if (spec_file) output_filename( spec_file );
3357 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) output_filename( "-nodefaultlibs" );
3358 output_filenames_obj_dir( make, make->unixobj_files );
3359 output_filenames( unix_libs );
3360 output_filename( "$(LDFLAGS)" );
3361 output( "\n" );
3364 if (spec_file && make->importlib)
3366 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3367 if (*dll_ext && !make->implib_objs.count)
3369 strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
3370 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
3371 output( "\t%s -w --def -o $@", tools_path( make, "winebuild" ) );
3372 output_filenames( target_flags );
3373 if (make->is_win16) output_filename( "-m16" );
3374 output_filename( "--export" );
3375 output_filename( spec_file );
3376 output( "\n" );
3377 add_install_rule( make, make->importlib,
3378 strmake( "lib%s.def", make->importlib ),
3379 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
3381 else
3383 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3384 if (!*dll_ext && needs_delay_lib( make ))
3386 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3387 output( "%s.delay.a ", importlib_path );
3389 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3390 output_filenames_obj_dir( make, make->implib_objs );
3391 output( "\n" );
3392 output( "\t%s -w --implib -o $@", tools_path( make, "winebuild" ) );
3393 output_filenames( target_flags );
3394 if (make->is_win16) output_filename( "-m16" );
3395 output_filename( "--export" );
3396 output_filename( spec_file );
3397 output_filenames_obj_dir( make, make->implib_objs );
3398 output( "\n" );
3399 add_install_rule( make, make->importlib,
3400 strmake( "lib%s.a", make->importlib ),
3401 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
3403 if (crosstarget && (needs_cross_lib( make ) || needs_delay_lib( make )))
3405 struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
3406 if (needs_cross_lib( make ))
3408 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3409 output_filename( strmake( "%s.cross.a", importlib_path ));
3411 if (needs_delay_lib( make ))
3413 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3414 output_filename( strmake( "%s.delay.a", importlib_path ));
3416 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3417 output_filenames_obj_dir( make, cross_files );
3418 output( "\n" );
3419 output( "\t%s -b %s -w --implib -o $@", tools_path( make, "winebuild" ), crosstarget );
3420 if (make->is_win16) output_filename( "-m16" );
3421 output_filename( "--export" );
3422 output_filename( spec_file );
3423 output_filenames_obj_dir( make, cross_files );
3424 output( "\n" );
3426 if (needs_implib_symlink( make ))
3427 strarray_addall( &top_makefile->clean_files, output_importlib_symlinks( make ));
3430 if (spec_file)
3431 output_man_pages( make );
3432 else if (*dll_ext && !make->is_win16 && strendswith( make->module, ".exe" ))
3434 char *binary = replace_extension( make->module, ".exe", "" );
3435 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3440 /*******************************************************************
3441 * output_static_lib
3443 static void output_static_lib( struct makefile *make )
3445 strarray_add( &make->all_targets, make->staticlib );
3446 output( "%s:", obj_dir_path( make, make->staticlib ));
3447 output_filenames_obj_dir( make, make->object_files );
3448 output_filenames_obj_dir( make, make->unixobj_files );
3449 output( "\n\trm -f $@\n" );
3450 output( "\t%s rc $@", ar );
3451 output_filenames_obj_dir( make, make->object_files );
3452 output_filenames_obj_dir( make, make->unixobj_files );
3453 output( "\n\t%s $@\n", ranlib );
3454 add_install_rule( make, make->staticlib, make->staticlib,
3455 strmake( "d$(dlldir)/%s", make->staticlib ));
3456 if (needs_cross_lib( make ))
3458 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3460 strarray_add( &make->all_targets, name );
3461 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3462 output_filenames_obj_dir( make, make->crossobj_files );
3463 output( "\n" );
3464 output( "\t%s -b %s -w --staticlib -o $@", tools_path( make, "winebuild" ), crosstarget );
3465 output_filenames_obj_dir( make, make->crossobj_files );
3466 output( "\n" );
3471 /*******************************************************************
3472 * output_shared_lib
3474 static void output_shared_lib( struct makefile *make )
3476 unsigned int i;
3477 char *basename, *p;
3478 struct strarray names = get_shared_lib_names( make->sharedlib );
3479 struct strarray all_libs = empty_strarray;
3480 struct strarray dep_libs = empty_strarray;
3482 basename = xstrdup( make->sharedlib );
3483 if ((p = strchr( basename, '.' ))) *p = 0;
3485 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3486 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3487 strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3489 output( "%s:", obj_dir_path( make, make->sharedlib ));
3490 output_filenames_obj_dir( make, make->object_files );
3491 output_filenames( dep_libs );
3492 output( "\n" );
3493 output( "\t$(CC) -o $@" );
3494 output_filenames_obj_dir( make, make->object_files );
3495 output_filenames( all_libs );
3496 output_filename( "$(LDFLAGS)" );
3497 output( "\n" );
3498 add_install_rule( make, make->sharedlib, make->sharedlib,
3499 strmake( "p$(libdir)/%s", make->sharedlib ));
3500 for (i = 1; i < names.count; i++)
3502 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3503 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3504 add_install_rule( make, names.str[i], names.str[i-1],
3505 strmake( "y$(libdir)/%s", names.str[i] ));
3507 strarray_addall( &make->all_targets, names );
3511 /*******************************************************************
3512 * output_test_module
3514 static void output_test_module( struct makefile *make )
3516 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3517 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3518 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3519 struct strarray dep_libs = empty_strarray;
3520 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0, 0 );
3521 struct makefile *parent = get_parent_makefile( make );
3522 const char *ext = make->is_cross ? "" : dll_ext;
3523 const char *parent_ext = parent && parent->is_cross ? "" : dll_ext;
3524 const char *debug_file;
3525 char *output_file;
3527 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3528 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3529 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3530 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3531 output( "%s:\n", output_file );
3532 output_winegcc_command( make, make->is_cross );
3533 output_filenames( make->extradllflags );
3534 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3535 output_filenames_obj_dir( make, make->res_files );
3536 if ((debug_file = get_debug_file( make, output_file )))
3537 output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3538 output_filenames( all_libs );
3539 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3540 output( "\n" );
3541 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3542 output_winegcc_command( make, make->is_cross );
3543 output_filename( "-s" );
3544 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3545 output_filenames( make->extradllflags );
3546 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3547 output_filenames_obj_dir( make, make->res_files );
3548 output_filenames( all_libs );
3549 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3550 output( "\n" );
3551 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3552 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3553 output_filenames_obj_dir( make, make->res_files );
3554 output_filenames( dep_libs );
3555 output_filename( tools_path( make, "winebuild" ));
3556 output_filename( tools_path( make, "winegcc" ));
3557 output( "\n" );
3559 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3560 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n",
3561 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3563 output_filenames_obj_dir( make, make->ok_files );
3564 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3565 if (parent) output( " %s%s", obj_dir_path( parent, make->testdll ), parent_ext );
3566 output( "\n" );
3567 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3568 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3569 output( "\n" );
3570 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3571 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3572 output( "%s::\n", obj_dir_path( make, "testclean" ));
3573 output( "\trm -f" );
3574 output_filenames_obj_dir( make, make->ok_files );
3575 output( "\n" );
3576 strarray_addall( &make->clean_files, make->ok_files );
3577 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3581 /*******************************************************************
3582 * output_programs
3584 static void output_programs( struct makefile *make )
3586 unsigned int i, j;
3588 for (i = 0; i < make->programs.count; i++)
3590 char *program_installed = NULL;
3591 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3592 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3593 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3594 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3595 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3597 if (!objs.count) objs = make->object_files;
3598 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3599 strarray_addall( &all_libs, add_unix_libraries( make, &deps ));
3601 output( "%s:", obj_dir_path( make, program ) );
3602 output_filenames_obj_dir( make, objs );
3603 output_filenames( deps );
3604 output( "\n" );
3605 output( "\t$(CC) -o $@" );
3606 output_filenames_obj_dir( make, objs );
3607 output_filenames( all_libs );
3608 output_filename( "$(LDFLAGS)" );
3609 output( "\n" );
3610 strarray_add( &make->all_targets, program );
3612 for (j = 0; j < symlinks.count; j++)
3614 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3615 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3617 strarray_addall( &make->all_targets, symlinks );
3619 add_install_rule( make, program, program_installed ? program_installed : program,
3620 strmake( "p$(bindir)/%s", program ));
3621 for (j = 0; j < symlinks.count; j++)
3622 add_install_rule( make, symlinks.str[j], program,
3623 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3628 /*******************************************************************
3629 * output_subdirs
3631 static void output_subdirs( struct makefile *make )
3633 struct strarray all_targets = empty_strarray;
3634 struct strarray makefile_deps = empty_strarray;
3635 struct strarray clean_files = empty_strarray;
3636 struct strarray testclean_files = empty_strarray;
3637 struct strarray distclean_files = empty_strarray;
3638 struct strarray dependencies = empty_strarray;
3639 struct strarray install_lib_deps = empty_strarray;
3640 struct strarray install_dev_deps = empty_strarray;
3641 struct strarray tooldeps_deps = empty_strarray;
3642 struct strarray buildtest_deps = empty_strarray;
3643 unsigned int i;
3645 strarray_addall( &clean_files, make->clean_files );
3646 strarray_addall( &distclean_files, make->distclean_files );
3647 strarray_addall( &all_targets, make->all_targets );
3648 for (i = 0; i < subdirs.count; i++)
3650 strarray_add( &makefile_deps, src_dir_path( submakes[i], strmake ( "%s.in", output_makefile_name )));
3651 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3652 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3653 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3654 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3655 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3656 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3657 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3659 if (submakes[i]->disabled) continue;
3661 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3662 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3663 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3664 if (submakes[i]->testdll)
3665 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3666 if (submakes[i]->install_rules[INSTALL_LIB].count)
3667 strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
3668 if (submakes[i]->install_rules[INSTALL_DEV].count)
3669 strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
3671 strarray_addall( &dependencies, makefile_deps );
3672 output( "all:" );
3673 output_filenames( all_targets );
3674 output( "\n" );
3675 output( "Makefile:" );
3676 output_filenames( makefile_deps );
3677 output( "\n" );
3678 output_filenames( dependencies );
3679 output( ":\n" );
3680 if (install_lib_deps.count)
3682 output( "install install-lib::" );
3683 output_filenames( install_lib_deps );
3684 output( "\n" );
3685 strarray_add_uniq( &make->phony_targets, "install" );
3686 strarray_add_uniq( &make->phony_targets, "install-lib" );
3688 if (install_dev_deps.count)
3690 output( "install install-dev::" );
3691 output_filenames( install_dev_deps );
3692 output( "\n" );
3693 strarray_add_uniq( &make->phony_targets, "install" );
3694 strarray_add_uniq( &make->phony_targets, "install-dev" );
3696 output_uninstall_rules( make );
3697 if (buildtest_deps.count)
3699 output( "buildtests:" );
3700 output_filenames( buildtest_deps );
3701 output( "\n" );
3702 strarray_add_uniq( &make->phony_targets, "buildtests" );
3704 output( "check test:" );
3705 output_filenames( testclean_files );
3706 output( "\n" );
3707 strarray_add_uniq( &make->phony_targets, "check" );
3708 strarray_add_uniq( &make->phony_targets, "test" );
3710 output( "clean::\n");
3711 output_rm_filenames( clean_files );
3712 output( "testclean::\n");
3713 output_rm_filenames( testclean_files );
3714 output( "distclean::\n");
3715 output_rm_filenames( distclean_files );
3716 strarray_add_uniq( &make->phony_targets, "distclean" );
3717 strarray_add_uniq( &make->phony_targets, "testclean" );
3719 if (tooldeps_deps.count)
3721 output( "__tooldeps__:" );
3722 output_filenames( tooldeps_deps );
3723 output( "\n" );
3724 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3727 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3729 if (make->phony_targets.count)
3731 output( ".PHONY:" );
3732 output_filenames( make->phony_targets );
3733 output( "\n" );
3738 /*******************************************************************
3739 * output_sources
3741 static void output_sources( struct makefile *make )
3743 struct strarray all_targets = empty_strarray;
3744 struct incl_file *source;
3745 unsigned int i, j;
3747 strarray_add_uniq( &make->phony_targets, "all" );
3749 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3751 char *obj = xstrdup( source->name );
3752 char *ext = get_extension( obj );
3754 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3755 *ext++ = 0;
3757 for (j = 0; output_source_funcs[j].ext; j++)
3758 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3760 output_source_funcs[j].fn( make, source, obj );
3761 strarray_addall_uniq( &make->dependencies, source->dependencies );
3764 /* special case for winetest: add resource files from other test dirs */
3765 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3767 struct strarray tests = enable_tests;
3768 if (!tests.count)
3769 for (i = 0; i < subdirs.count; i++)
3770 if (submakes[i]->testdll && !submakes[i]->disabled)
3771 strarray_add( &tests, submakes[i]->testdll );
3772 for (i = 0; i < tests.count; i++)
3773 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3776 if (make->dlldata_files.count)
3778 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3779 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3780 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
3781 output_filenames( make->dlldata_files );
3782 output( "\n" );
3785 if (make->staticlib) output_static_lib( make );
3786 else if (make->module) output_module( make );
3787 else if (make->testdll) output_test_module( make );
3788 else if (make->sharedlib) output_shared_lib( make );
3789 else if (make->programs.count) output_programs( make );
3791 for (i = 0; i < make->scripts.count; i++)
3792 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3793 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3795 for (i = 0; i < make->extra_targets.count; i++)
3796 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3797 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3798 else
3799 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3801 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3802 strarray_add( &make->distclean_files, "Makefile" );
3803 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3805 if (!make->obj_dir)
3806 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3807 else if (!strcmp( make->obj_dir, "po" ))
3808 strarray_add( &make->distclean_files, "LINGUAS" );
3810 strarray_addall( &make->clean_files, make->object_files );
3811 strarray_addall( &make->clean_files, make->crossobj_files );
3812 strarray_addall( &make->clean_files, make->unixobj_files );
3813 strarray_addall( &make->clean_files, make->res_files );
3814 strarray_addall( &make->clean_files, make->pot_files );
3815 strarray_addall( &make->clean_files, make->debug_files );
3816 strarray_addall( &make->clean_files, make->all_targets );
3818 if (make == top_makefile)
3820 output_subdirs( make );
3821 return;
3824 strarray_addall( &all_targets, make->all_targets );
3825 strarray_addall( &all_targets, make->font_files );
3826 if (all_targets.count)
3828 output( "%s:", obj_dir_path( make, "all" ));
3829 output_filenames_obj_dir( make, all_targets );
3830 output( "\n" );
3831 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3833 output_install_rules( make, INSTALL_LIB, "install-lib" );
3834 output_install_rules( make, INSTALL_DEV, "install-dev" );
3836 if (make->clean_files.count)
3838 output( "%s::\n", obj_dir_path( make, "clean" ));
3839 output( "\trm -f" );
3840 output_filenames_obj_dir( make, make->clean_files );
3841 output( "\n" );
3842 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3847 /*******************************************************************
3848 * create_temp_file
3850 static FILE *create_temp_file( const char *orig )
3852 char *name = xmalloc( strlen(orig) + 13 );
3853 unsigned int i, id = getpid();
3854 int fd;
3855 FILE *ret = NULL;
3857 for (i = 0; i < 100; i++)
3859 sprintf( name, "%s.tmp%08x", orig, id );
3860 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3862 ret = fdopen( fd, "w" );
3863 break;
3865 if (errno != EEXIST) break;
3866 id += 7777;
3868 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3869 temp_file_name = name;
3870 return ret;
3874 /*******************************************************************
3875 * rename_temp_file
3877 static void rename_temp_file( const char *dest )
3879 int ret = rename( temp_file_name, dest );
3880 if (ret == -1 && errno == EEXIST)
3882 /* rename doesn't overwrite on windows */
3883 unlink( dest );
3884 ret = rename( temp_file_name, dest );
3886 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3887 temp_file_name = NULL;
3891 /*******************************************************************
3892 * are_files_identical
3894 static int are_files_identical( FILE *file1, FILE *file2 )
3896 for (;;)
3898 char buffer1[8192], buffer2[8192];
3899 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3900 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3901 if (size1 != size2) return 0;
3902 if (!size1) return feof( file1 ) && feof( file2 );
3903 if (memcmp( buffer1, buffer2, size1 )) return 0;
3908 /*******************************************************************
3909 * rename_temp_file_if_changed
3911 static void rename_temp_file_if_changed( const char *dest )
3913 FILE *file1, *file2;
3914 int do_rename = 1;
3916 if ((file1 = fopen( dest, "r" )))
3918 if ((file2 = fopen( temp_file_name, "r" )))
3920 do_rename = !are_files_identical( file1, file2 );
3921 fclose( file2 );
3923 fclose( file1 );
3925 if (!do_rename)
3927 unlink( temp_file_name );
3928 temp_file_name = NULL;
3930 else rename_temp_file( dest );
3934 /*******************************************************************
3935 * output_linguas
3937 static void output_linguas( const struct makefile *make )
3939 const char *dest = obj_dir_path( make, "LINGUAS" );
3940 struct incl_file *source;
3942 output_file = create_temp_file( dest );
3944 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3945 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3946 if (strendswith( source->name, ".po" ))
3947 output( "%s\n", replace_extension( source->name, ".po", "" ));
3949 if (fclose( output_file )) fatal_perror( "write" );
3950 output_file = NULL;
3951 rename_temp_file_if_changed( dest );
3955 /*******************************************************************
3956 * output_testlist
3958 static void output_testlist( const struct makefile *make )
3960 const char *dest = obj_dir_path( make, "testlist.c" );
3961 struct strarray files = empty_strarray;
3962 unsigned int i;
3964 for (i = 0; i < make->ok_files.count; i++)
3965 strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
3967 output_file = create_temp_file( dest );
3969 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3970 output( "#define WIN32_LEAN_AND_MEAN\n" );
3971 output( "#include <windows.h>\n\n" );
3972 output( "#define STANDALONE\n" );
3973 output( "#include \"wine/test.h\"\n\n" );
3975 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
3976 output( "\n" );
3977 output( "const struct test winetest_testlist[] =\n" );
3978 output( "{\n" );
3979 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
3980 output( " { 0, 0 }\n" );
3981 output( "};\n" );
3983 if (fclose( output_file )) fatal_perror( "write" );
3984 output_file = NULL;
3985 rename_temp_file_if_changed( dest );
3989 /*******************************************************************
3990 * output_gitignore
3992 static void output_gitignore( const char *dest, struct strarray files )
3994 int i;
3996 output_file = create_temp_file( dest );
3998 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3999 for (i = 0; i < files.count; i++)
4001 if (!strchr( files.str[i], '/' )) output( "/" );
4002 output( "%s\n", files.str[i] );
4005 if (fclose( output_file )) fatal_perror( "write" );
4006 output_file = NULL;
4007 rename_temp_file( dest );
4011 /*******************************************************************
4012 * output_stub_makefile
4014 static void output_stub_makefile( struct makefile *make )
4016 struct strarray targets = empty_strarray;
4017 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
4019 if (make_var) output( "MAKE = %s\n\n", make_var );
4020 output( "all:\n" );
4022 if (make->all_targets.count) strarray_add( &targets, "all" );
4023 if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
4024 strarray_add( &targets, "install" );
4025 if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
4026 if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
4027 if (make->clean_files.count) strarray_add( &targets, "clean" );
4028 if (make->ok_files.count)
4030 strarray_add( &targets, "check" );
4031 strarray_add( &targets, "test" );
4032 strarray_add( &targets, "testclean" );
4035 output_filenames( targets );
4036 output_filenames( make->clean_files );
4037 output( ":\n" );
4038 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4039 output( ".PHONY:" );
4040 output_filenames( targets );
4041 output( "\n" );
4045 /*******************************************************************
4046 * output_dependencies
4048 static void output_dependencies( struct makefile *make )
4050 struct strarray ignore_files = empty_strarray;
4051 char buffer[1024];
4052 FILE *src_file;
4053 int i, found = 0;
4055 if (make->obj_dir) create_dir( make->obj_dir );
4057 output_file_name = obj_dir_path( make, output_makefile_name );
4058 output_file = create_temp_file( output_file_name );
4060 /* copy the contents of the source makefile */
4061 src_file = open_input_makefile( make );
4062 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4064 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4065 found = !strncmp( buffer, separator, strlen(separator) );
4067 if (fclose( src_file )) fatal_perror( "close" );
4068 input_file_name = NULL;
4070 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4072 if (make == top_makefile)
4074 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4075 output_sources( make );
4077 else output_stub_makefile( make );
4079 fclose( output_file );
4080 output_file = NULL;
4081 rename_temp_file( output_file_name );
4083 strarray_addall( &ignore_files, make->distclean_files );
4084 strarray_addall( &ignore_files, make->clean_files );
4085 if (make->testdll) output_testlist( make );
4086 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4087 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4089 create_file_directories( make, ignore_files );
4091 output_file_name = NULL;
4095 /*******************************************************************
4096 * load_sources
4098 static void load_sources( struct makefile *make )
4100 static const char *source_vars[] =
4102 "SOURCES",
4103 "C_SRCS",
4104 "OBJC_SRCS",
4105 "RC_SRCS",
4106 "MC_SRCS",
4107 "IDL_SRCS",
4108 "BISON_SRCS",
4109 "LEX_SRCS",
4110 "HEADER_SRCS",
4111 "XTEMPLATE_SRCS",
4112 "SVG_SRCS",
4113 "FONT_SRCS",
4114 "IN_SRCS",
4115 "PO_SRCS",
4116 "MANPAGES",
4117 NULL
4119 const char **var;
4120 unsigned int i;
4121 struct strarray value;
4122 struct incl_file *file;
4124 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4125 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4127 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4128 make->module = get_expanded_make_variable( make, "MODULE" );
4129 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4130 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4131 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4132 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4134 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4135 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4136 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4137 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4138 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4139 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4140 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4141 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4143 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
4145 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4146 if ((make->module && make->staticlib) || make->testdll || make->is_win16)
4147 strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4149 strarray_addall( &make->extradllflags, get_expanded_make_var_array( make, "APPMODE" ));
4150 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4151 make->use_msvcrt = strarray_exists( &make->extradllflags, "-mno-cygwin" );
4152 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4153 strarray_exists( &make->extradllflags, "-mwindows" );
4155 if (make->module && !make->install_lib.count && !make->install_dev.count)
4157 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4158 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4159 else strarray_add( &make->install_lib, make->module );
4162 make->include_paths = empty_strarray;
4163 make->include_args = empty_strarray;
4164 make->define_args = empty_strarray;
4165 strarray_add( &make->define_args, "-D__WINESRC__" );
4167 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4168 for (i = 0; i < value.count; i++)
4169 if (!strncmp( value.str[i], "-I", 2 ))
4170 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4171 else
4172 strarray_add_uniq( &make->define_args, value.str[i] );
4173 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4175 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4176 if (make->src_dir)
4177 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4178 if (make->parent_dir)
4179 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4180 strarray_add( &make->include_args, "-Iinclude" );
4181 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4183 list_init( &make->sources );
4184 list_init( &make->includes );
4186 for (var = source_vars; *var; var++)
4188 value = get_expanded_make_var_array( make, *var );
4189 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4192 add_generated_sources( make );
4194 if (!make->use_msvcrt && !has_object_file( make ))
4196 strarray_add( &make->extradllflags, "-mno-cygwin" );
4197 make->use_msvcrt = 1;
4200 if (make->use_msvcrt)
4202 const char *crt_dll = add_crt_import( make, &make->imports );
4204 if (crt_dll && !strncmp( crt_dll, "ucrt", 4 )) strarray_add( &make->define_args, "-D_UCRT" );
4205 else
4207 unsigned int msvcrt_version = 0;
4209 if (crt_dll) sscanf( crt_dll, "msvcr%u", &msvcrt_version );
4210 strarray_add( &make->define_args, strmake( "-D_MSVCR_VER=%u", msvcrt_version ));
4214 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4215 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4217 make->is_cross = crosstarget && make->use_msvcrt;
4219 if (make->is_cross)
4221 strarray_addall_uniq( &cross_import_libs, make->imports );
4222 strarray_addall_uniq( &cross_import_libs, make->extra_imports );
4223 if (make->is_win16) strarray_add_uniq( &cross_import_libs, "kernel" );
4224 strarray_add_uniq( &cross_import_libs, "winecrt0" );
4225 strarray_add_uniq( &cross_import_libs, "kernel32" );
4226 strarray_add_uniq( &cross_import_libs, "ntdll" );
4229 if (!*dll_ext || make->is_cross)
4230 for (i = 0; i < make->delayimports.count; i++)
4231 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4235 /*******************************************************************
4236 * parse_makeflags
4238 static void parse_makeflags( const char *flags )
4240 const char *p = flags;
4241 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4243 while (*p)
4245 while (isspace(*p)) p++;
4246 var = buffer;
4247 while (*p && !isspace(*p))
4249 if (*p == '\\' && p[1]) p++;
4250 *var++ = *p++;
4252 *var = 0;
4253 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4258 /*******************************************************************
4259 * parse_option
4261 static int parse_option( const char *opt )
4263 if (opt[0] != '-')
4265 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4266 return 0;
4268 switch(opt[1])
4270 case 'f':
4271 if (opt[2]) output_makefile_name = opt + 2;
4272 break;
4273 case 'R':
4274 relative_dir_mode = 1;
4275 break;
4276 default:
4277 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4278 exit(1);
4280 return 1;
4284 /*******************************************************************
4285 * main
4287 int main( int argc, char *argv[] )
4289 const char *makeflags = getenv( "MAKEFLAGS" );
4290 int i, j;
4292 if (makeflags) parse_makeflags( makeflags );
4294 i = 1;
4295 while (i < argc)
4297 if (parse_option( argv[i] ))
4299 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4300 argc--;
4302 else i++;
4305 if (relative_dir_mode)
4307 char *relpath;
4309 if (argc != 3)
4311 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4312 exit( 1 );
4314 relpath = get_relative_path( argv[1], argv[2] );
4315 printf( "%s\n", relpath ? relpath : "." );
4316 exit( 0 );
4319 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4321 atexit( cleanup_files );
4322 signal( SIGTERM, exit_on_signal );
4323 signal( SIGINT, exit_on_signal );
4324 #ifdef SIGHUP
4325 signal( SIGHUP, exit_on_signal );
4326 #endif
4328 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4330 top_makefile = parse_makefile( NULL );
4332 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4333 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4334 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4335 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4336 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4337 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4338 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4339 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4340 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4341 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4342 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4343 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4345 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4346 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
4347 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
4348 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4349 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
4350 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4351 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4352 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4353 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4354 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4355 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4356 bison = get_expanded_make_variable( top_makefile, "BISON" );
4357 ar = get_expanded_make_variable( top_makefile, "AR" );
4358 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4359 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4360 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4361 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4362 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4363 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4364 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4366 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4367 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4368 if (!exe_ext) exe_ext = "";
4369 if (!tools_ext) tools_ext = "";
4370 if (!man_ext) man_ext = "3w";
4372 top_makefile->src_dir = root_src_dir;
4373 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4374 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4375 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4377 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4379 load_sources( top_makefile );
4380 for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
4382 output_dependencies( top_makefile );
4383 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4385 return 0;