qcap/tests: Test (dis)connecting while not stopped.
[wine.git] / tools / makedep.c
blobc30c21ff27d16c6560b032d3efb43405cc6c604c
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 for (j = 0; j < subdirs.count; j++)
2223 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2225 if (is_cross || !*dll_ext || submakes[j]->staticimplib)
2226 lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
2227 else
2229 strarray_add( deps, strmake( "%s/lib%s.def", submakes[j]->obj_dir, name ));
2230 if (needs_implib_symlink( submakes[j] ))
2231 strarray_add( deps, strmake( "dlls/lib%s.def", name ));
2233 break;
2236 if ((lib = get_static_lib( submakes[j], name ))) break;
2239 if (lib)
2241 const char *ext = NULL;
2243 if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
2244 else if (is_cross) ext = ".cross.a";
2245 if (ext) lib = replace_extension( lib, ".a", ext );
2246 strarray_add( deps, lib );
2247 strarray_add( &ret, lib );
2248 if (needs_implib_symlink( submakes[j] ))
2249 strarray_add( deps, strmake( "dlls/lib%s%s", name, ext ? ext : ".a" ));
2251 else strarray_add( &ret, strmake( "-l%s", name ));
2253 return ret;
2257 /*******************************************************************
2258 * get_default_imports
2260 static struct strarray get_default_imports( const struct makefile *make )
2262 struct strarray ret = empty_strarray;
2264 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
2265 strarray_add( &ret, "winecrt0" );
2266 if (make->is_win16) strarray_add( &ret, "kernel" );
2267 strarray_add( &ret, "kernel32" );
2268 strarray_add( &ret, "ntdll" );
2269 return ret;
2273 /*******************************************************************
2274 * add_crt_import
2276 static void add_crt_import( const struct makefile *make, struct strarray *imports, struct strarray *defs )
2278 unsigned int i;
2279 const char *crt_dll = NULL;
2281 for (i = 0; i < imports->count; i++)
2283 if (strncmp( imports->str[i], "msvcr", 5 ) && strncmp( imports->str[i], "ucrt", 4 )) continue;
2284 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n", crt_dll, imports->str[i] );
2285 crt_dll = imports->str[i];
2287 if (!crt_dll && !strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
2289 if (make->module &&
2290 (!strncmp( make->module, "msvcr", 5 ) ||
2291 !strncmp( make->module, "ucrt", 4 ) ||
2292 !strcmp( make->module, "crtdll.dll" )))
2294 crt_dll = make->module;
2296 else
2298 crt_dll = !make->testdll && !make->staticlib ? "ucrtbase" : "msvcrt";
2299 strarray_add( imports, crt_dll );
2303 if (!defs) return;
2304 if (crt_dll && !strncmp( crt_dll, "ucrt", 4 )) strarray_add( defs, "-D_UCRT" );
2305 else
2307 unsigned int version = 0;
2308 if (crt_dll) sscanf( crt_dll, "msvcr%u", &version );
2309 strarray_add( defs, strmake( "-D_MSVCR_VER=%u", version ));
2314 /*******************************************************************
2315 * add_install_rule
2317 static void add_install_rule( struct makefile *make, const char *target,
2318 const char *file, const char *dest )
2320 if (strarray_exists( &make->install_lib, target ) ||
2321 strarray_exists( &top_install_lib, make->obj_dir ) ||
2322 strarray_exists( &top_install_lib, obj_dir_path( make, target )))
2324 strarray_add( &make->install_rules[INSTALL_LIB], file );
2325 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2327 else if (strarray_exists( &make->install_dev, target ) ||
2328 strarray_exists( &top_install_dev, make->obj_dir ) ||
2329 strarray_exists( &top_install_dev, obj_dir_path( make, target )))
2331 strarray_add( &make->install_rules[INSTALL_DEV], file );
2332 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2337 /*******************************************************************
2338 * get_include_install_path
2340 * Determine the installation path for a given include file.
2342 static const char *get_include_install_path( const char *name )
2344 if (!strncmp( name, "wine/", 5 )) return name + 5;
2345 if (!strncmp( name, "msvcrt/", 7 )) return name;
2346 return strmake( "windows/%s", name );
2350 /*******************************************************************
2351 * get_shared_library_name
2353 * Determine possible names for a shared library with a version number.
2355 static struct strarray get_shared_lib_names( const char *libname )
2357 struct strarray ret = empty_strarray;
2358 const char *ext, *p;
2359 char *name, *first, *second;
2360 size_t len = 0;
2362 strarray_add( &ret, libname );
2364 for (p = libname; (p = strchr( p, '.' )); p++)
2365 if ((len = strspn( p + 1, "0123456789." ))) break;
2367 if (!len) return ret;
2368 ext = p + 1 + len;
2369 if (*ext && ext[-1] == '.') ext--;
2371 /* keep only the first group of digits */
2372 name = xstrdup( libname );
2373 first = name + (p - libname);
2374 if ((second = strchr( first + 1, '.' )))
2376 strcpy( second, ext );
2377 strarray_add( &ret, xstrdup( name ));
2379 /* now remove all digits */
2380 strcpy( first, ext );
2381 strarray_add( &ret, name );
2382 return ret;
2386 /*******************************************************************
2387 * get_source_defines
2389 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2390 const char *obj )
2392 unsigned int i;
2393 struct strarray ret = empty_strarray;
2395 strarray_addall( &ret, make->include_args );
2396 if (source->use_msvcrt)
2397 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2398 for (i = 0; i < make->include_paths.count; i++)
2399 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2400 strarray_addall( &ret, make->define_args );
2401 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2402 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2403 return ret;
2407 /*******************************************************************
2408 * get_debug_file
2410 static const char *get_debug_file( struct makefile *make, const char *name )
2412 const char *debug_file = NULL;
2413 if (!make->is_cross || !crossdebug) return NULL;
2414 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2415 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2416 if (debug_file) strarray_add( &make->debug_files, debug_file );
2417 return debug_file;
2421 /*******************************************************************
2422 * output_winegcc_command
2424 static void output_winegcc_command( struct makefile *make, int is_cross )
2426 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2427 output_filename( "--wine-objdir ." );
2428 if (tools_dir)
2430 output_filename( "--winebuild" );
2431 output_filename( tools_path( make, "winebuild" ));
2433 if (is_cross)
2435 output_filename( "-b" );
2436 output_filename( crosstarget );
2437 output_filename( "--lib-suffix=.cross.a" );
2439 else
2441 output_filenames( target_flags );
2442 output_filenames( lddll_flags );
2447 /*******************************************************************
2448 * output_symlink_rule
2450 * Output a rule to create a symlink.
2452 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2454 const char *name = strrchr( link_name, '/' );
2455 char *dir = NULL;
2457 if (name)
2459 dir = xstrdup( link_name );
2460 dir[name - link_name] = 0;
2463 output( "\t" );
2464 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2465 output( "rm -f %s && ", link_name );
2467 /* dest path with a directory needs special handling if ln -s isn't supported */
2468 if (dir && strcmp( ln_s, "ln -s" ))
2469 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2470 else
2471 output( "%s %s %s\n", ln_s, src_name, link_name );
2473 free( dir );
2477 /*******************************************************************
2478 * output_srcdir_symlink
2480 * Output rule to create a symlink back to the source directory, for source files
2481 * that are needed at run-time.
2483 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2485 char *src_file, *dst_file, *src_name;
2487 if (!make->src_dir) return;
2488 src_file = src_dir_path( make, obj );
2489 dst_file = obj_dir_path( make, obj );
2490 output( "%s: %s\n", dst_file, src_file );
2492 src_name = src_file;
2493 if (src_name[0] != '/' && make->obj_dir)
2494 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2496 output_symlink_rule( src_name, dst_file, 0 );
2497 strarray_add( &make->all_targets, obj );
2501 /*******************************************************************
2502 * output_install_commands
2504 static void output_install_commands( struct makefile *make, struct strarray files )
2506 unsigned int i;
2507 char *install_sh = root_src_dir_path( "tools/install-sh" );
2509 for (i = 0; i < files.count; i += 2)
2511 const char *file = files.str[i];
2512 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2514 switch (*files.str[i + 1])
2516 case 'c': /* cross-compiled program */
2517 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2518 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2519 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2520 break;
2521 case 'd': /* data file */
2522 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2523 install_sh, obj_dir_path( make, file ), dest );
2524 break;
2525 case 'D': /* data file in source dir */
2526 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2527 install_sh, src_dir_path( make, file ), dest );
2528 break;
2529 case 'p': /* program file */
2530 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2531 install_sh, obj_dir_path( make, file ), dest );
2532 break;
2533 case 's': /* script */
2534 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2535 install_sh, obj_dir_path( make, file ), dest );
2536 break;
2537 case 'S': /* script in source dir */
2538 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2539 install_sh, src_dir_path( make, file ), dest );
2540 break;
2541 case 't': /* script in tools dir */
2542 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2543 install_sh, tools_dir_path( make, files.str[i] ), dest );
2544 break;
2545 case 'y': /* symlink */
2546 output_symlink_rule( files.str[i], dest, 1 );
2547 break;
2548 default:
2549 assert(0);
2551 strarray_add( &make->uninstall_files, dest );
2556 /*******************************************************************
2557 * output_install_rules
2559 * Rules are stored as a (file,dest) pair of values.
2560 * The first char of dest indicates the type of install.
2562 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2564 unsigned int i;
2565 struct strarray files = make->install_rules[rules];
2566 struct strarray targets = empty_strarray;
2568 if (!files.count) return;
2570 for (i = 0; i < files.count; i += 2)
2572 const char *file = files.str[i];
2573 switch (*files.str[i + 1])
2575 case 'c': /* cross-compiled program */
2576 case 'd': /* data file */
2577 case 'p': /* program file */
2578 case 's': /* script */
2579 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2580 break;
2581 case 't': /* script in tools dir */
2582 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2583 break;
2587 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
2588 output_filenames( targets );
2589 output( "\n" );
2590 output_install_commands( make, files );
2591 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2592 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
2596 static int cmp_string_length( const char **a, const char **b )
2598 int paths_a = 0, paths_b = 0;
2599 const char *p;
2601 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2602 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2603 if (paths_b != paths_a) return paths_b - paths_a;
2604 return strcmp( *a, *b );
2607 /*******************************************************************
2608 * output_uninstall_rules
2610 static void output_uninstall_rules( struct makefile *make )
2612 static const char *dirs_order[] =
2613 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };
2615 struct strarray uninstall_dirs = empty_strarray;
2616 unsigned int i, j;
2618 if (!make->uninstall_files.count) return;
2619 output( "uninstall::\n" );
2620 output_rm_filenames( make->uninstall_files );
2621 strarray_add_uniq( &make->phony_targets, "uninstall" );
2623 if (!subdirs.count) return;
2624 for (i = 0; i < make->uninstall_files.count; i++)
2626 char *dir = xstrdup( make->uninstall_files.str[i] );
2627 while (strchr( dir, '/' ))
2629 *strrchr( dir, '/' ) = 0;
2630 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2633 strarray_qsort( &uninstall_dirs, cmp_string_length );
2634 output( "\t-rmdir" );
2635 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2637 for (j = 0; j < uninstall_dirs.count; j++)
2639 if (!uninstall_dirs.str[j]) continue;
2640 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2641 continue;
2642 output_filename( uninstall_dirs.str[j] );
2643 uninstall_dirs.str[j] = NULL;
2646 for (j = 0; j < uninstall_dirs.count; j++)
2647 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2648 output( "\n" );
2652 /*******************************************************************
2653 * output_importlib_symlinks
2655 static struct strarray output_importlib_symlinks( const struct makefile *make )
2657 struct strarray ret = empty_strarray;
2658 const char *lib, *dst, *ext[4];
2659 int i, count = 0;
2661 ext[count++] = (*dll_ext && !make->implib_objs.count) ? "def" : "a";
2662 if (needs_delay_lib( make )) ext[count++] = "delay.a";
2663 if (needs_cross_lib( make )) ext[count++] = "cross.a";
2665 for (i = 0; i < count; i++)
2667 lib = strmake( "lib%s.%s", make->importlib, ext[i] );
2668 dst = strmake( "dlls/%s", lib );
2669 output( "%s: %s\n", dst, obj_dir_path( make, lib ));
2670 output_symlink_rule( concat_paths( make->obj_dir + strlen("dlls/"), lib ), dst, 0 );
2671 strarray_add( &ret, dst );
2673 return ret;
2677 /*******************************************************************
2678 * output_po_files
2680 static void output_po_files( const struct makefile *make )
2682 const char *po_dir = src_dir_path( make, "po" );
2683 unsigned int i;
2685 if (linguas.count)
2687 for (i = 0; i < linguas.count; i++)
2688 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2689 output( ": %s/wine.pot\n", po_dir );
2690 output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2691 po_dir );
2692 output( "po:" );
2693 for (i = 0; i < linguas.count; i++)
2694 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2695 output( "\n" );
2697 output( "%s/wine.pot:", po_dir );
2698 output_filenames( make->pot_files );
2699 output( "\n" );
2700 output( "\tmsgcat -o $@" );
2701 output_filenames( make->pot_files );
2702 output( "\n" );
2706 /*******************************************************************
2707 * output_source_y
2709 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2711 /* add source file dependency for parallel makes */
2712 char *header = strmake( "%s.tab.h", obj );
2714 if (find_include_file( make, header ))
2716 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2717 output( "\t%s -p %s_ -o %s.tab.c -d %s\n",
2718 bison, obj, obj_dir_path( make, obj ), source->filename );
2719 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2720 source->filename, obj_dir_path( make, header ));
2721 strarray_add( &make->clean_files, header );
2723 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2725 output( "\t%s -p %s_ -o $@ %s\n", bison, obj, source->filename );
2729 /*******************************************************************
2730 * output_source_l
2732 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2734 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2735 output( "\t%s -o$@ %s\n", flex, source->filename );
2739 /*******************************************************************
2740 * output_source_h
2742 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2744 if (source->file->flags & FLAG_GENERATED)
2745 strarray_add( &make->all_targets, source->name );
2746 else
2747 add_install_rule( make, source->name, source->name,
2748 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2752 /*******************************************************************
2753 * output_source_rc
2755 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2757 struct strarray defines = get_source_defines( make, source, obj );
2758 const char *po_dir = NULL;
2759 unsigned int i;
2761 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2762 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2763 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2764 if (source->file->flags & FLAG_RC_PO)
2766 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2767 output( "%s.pot ", obj_dir_path( make, obj ) );
2769 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2770 output_filename( tools_path( make, "wrc" ));
2771 output_filenames( source->dependencies );
2772 output( "\n" );
2773 output( "\t%s -u -o $@", tools_path( make, "wrc" ) );
2774 if (make->is_win16) output_filename( "-m16" );
2775 else output_filenames( target_flags );
2776 output_filename( "--nostdinc" );
2777 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2778 output_filenames( defines );
2779 output_filename( source->filename );
2780 output( "\n" );
2781 if (po_dir)
2783 output( "%s.res:", obj_dir_path( make, obj ));
2784 for (i = 0; i < linguas.count; i++)
2785 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2786 output( "\n" );
2791 /*******************************************************************
2792 * output_source_mc
2794 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2796 unsigned int i;
2797 char *obj_path = obj_dir_path( make, obj );
2799 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2800 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2801 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2802 output_filename( tools_path( make, "wmc" ));
2803 output_filenames( source->dependencies );
2804 output( "\n" );
2805 output( "\t%s -u -o $@ %s", tools_path( make, "wmc" ), source->filename );
2806 if (linguas.count)
2808 output_filename( "--po-dir=po" );
2809 output( "\n" );
2810 output( "%s.res:", obj_dir_path( make, obj ));
2811 for (i = 0; i < linguas.count; i++)
2812 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2814 output( "\n" );
2818 /*******************************************************************
2819 * output_source_res
2821 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2823 strarray_add( &make->res_files, source->name );
2827 /*******************************************************************
2828 * output_source_idl
2830 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2832 struct strarray defines = get_source_defines( make, source, obj );
2833 struct strarray targets = empty_strarray;
2834 char *dest;
2835 unsigned int i;
2837 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2838 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2840 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2842 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2843 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2844 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2845 strarray_add( &targets, dest );
2847 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2848 if (source->file->flags & FLAG_INSTALL)
2850 add_install_rule( make, source->name, xstrdup( source->name ),
2851 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2852 if (source->file->flags & FLAG_IDL_HEADER)
2853 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2854 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2855 if (source->file->flags & FLAG_IDL_TYPELIB)
2856 add_install_rule( make, source->name, strmake( "%s.tlb", obj ),
2857 strmake( "d$(includedir)/wine/%s.tlb", get_include_install_path( obj ) ));
2859 if (!targets.count) return;
2861 output_filenames_obj_dir( make, targets );
2862 output( ": %s\n", tools_path( make, "widl" ));
2863 output( "\t%s -o $@", tools_path( make, "widl" ) );
2864 output_filenames( target_flags );
2865 output_filename( "--nostdinc" );
2866 output_filenames( defines );
2867 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2868 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2869 output_filename( source->filename );
2870 output( "\n" );
2871 output_filenames_obj_dir( make, targets );
2872 output( ": %s", source->filename );
2873 output_filenames( source->dependencies );
2874 output( "\n" );
2878 /*******************************************************************
2879 * output_source_tlb
2881 static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
2883 strarray_add( &make->all_targets, source->name );
2887 /*******************************************************************
2888 * output_source_x
2890 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2892 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2893 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2894 output( "\t%s%s -H -o $@ %s\n",
2895 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2896 if (source->file->flags & FLAG_INSTALL)
2898 add_install_rule( make, source->name, source->name,
2899 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2900 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2901 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2906 /*******************************************************************
2907 * output_source_sfd
2909 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2911 unsigned int i;
2912 char *ttf_obj = strmake( "%s.ttf", obj );
2913 char *ttf_file = src_dir_path( make, ttf_obj );
2915 if (fontforge && !make->src_dir)
2917 output( "%s: %s\n", ttf_file, source->filename );
2918 output( "\t%s -script %s %s $@\n",
2919 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2920 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2922 if (source->file->flags & FLAG_INSTALL)
2924 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2925 output_srcdir_symlink( make, ttf_obj );
2928 if (source->file->flags & FLAG_SFD_FONTS)
2930 struct strarray *array = source->file->args;
2932 for (i = 0; i < array->count; i++)
2934 char *font = strtok( xstrdup(array->str[i]), " \t" );
2935 char *args = strtok( NULL, "" );
2937 strarray_add( &make->all_targets, xstrdup( font ));
2938 output( "%s: %s %s\n", obj_dir_path( make, font ),
2939 tools_path( make, "sfnt2fon" ), ttf_file );
2940 output( "\t%s -q -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2941 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2947 /*******************************************************************
2948 * output_source_svg
2950 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2952 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2953 unsigned int i;
2955 if (convert && rsvg && icotool && !make->src_dir)
2957 for (i = 0; images[i]; i++)
2958 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2960 if (images[i])
2962 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2963 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2964 root_src_dir_path( "tools/buildimage" ), source->filename );
2970 /*******************************************************************
2971 * output_source_nls
2973 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2975 add_install_rule( make, source->name, source->name,
2976 strmake( "D$(nlsdir)/%s", source->name ));
2977 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2981 /*******************************************************************
2982 * output_source_desktop
2984 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2986 add_install_rule( make, source->name, source->name,
2987 strmake( "D$(datadir)/applications/%s", source->name ));
2991 /*******************************************************************
2992 * output_source_po
2994 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2996 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2997 output( "\t%s -o $@ %s\n", msgfmt, source->filename );
2998 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
3002 /*******************************************************************
3003 * output_source_in
3005 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
3007 unsigned int i;
3009 if (strendswith( obj, ".man" ) && source->file->args)
3011 struct strarray symlinks;
3012 char *dir, *dest = replace_extension( obj, ".man", "" );
3013 char *lang = strchr( dest, '.' );
3014 char *section = source->file->args;
3015 if (lang)
3017 *lang++ = 0;
3018 dir = strmake( "$(mandir)/%s/man%s", lang, section );
3020 else dir = strmake( "$(mandir)/man%s", section );
3021 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
3022 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
3023 for (i = 0; i < symlinks.count; i++)
3024 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
3025 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
3026 free( dest );
3027 free( dir );
3029 strarray_add( &make->in_files, xstrdup(obj) );
3030 strarray_add( &make->all_targets, xstrdup(obj) );
3031 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
3032 output( "\t%s %s >$@ || (rm -f $@ && false)\n", sed_cmd, source->filename );
3033 output( "%s:", obj_dir_path( make, obj ));
3034 output_filenames( source->dependencies );
3035 output( "\n" );
3036 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
3040 /*******************************************************************
3041 * output_source_spec
3043 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
3045 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
3046 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
3047 struct strarray all_libs, dep_libs = empty_strarray;
3048 char *dll_name, *obj_name, *output_file;
3049 const char *debug_file;
3051 if (!imports.count) imports = make->imports;
3052 else if (make->use_msvcrt) add_crt_import( make, &imports, NULL );
3054 if (!dll_flags.count) dll_flags = make->extradllflags;
3055 all_libs = add_import_libs( make, &dep_libs, imports, 0, 0 );
3056 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3057 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
3058 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
3059 output_file = obj_dir_path( make, dll_name );
3061 strarray_add( &make->clean_files, dll_name );
3062 strarray_add( &make->res_files, strmake( "%s.res", obj ));
3063 output( "%s.res: %s\n", obj_dir_path( make, obj ), obj_dir_path( make, dll_name ));
3064 output( "\techo \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", obj, output_file,
3065 tools_path( make, "wrc" ));
3067 output( "%s:", output_file);
3068 output_filename( source->filename );
3069 output_filename( obj_name );
3070 output_filenames( dep_libs );
3071 output_filename( tools_path( make, "winebuild" ));
3072 output_filename( tools_path( make, "winegcc" ));
3073 output( "\n" );
3074 output_winegcc_command( make, make->is_cross );
3075 output_filename( "-s" );
3076 output_filenames( dll_flags );
3077 output_filename( "-shared" );
3078 output_filename( source->filename );
3079 output_filename( obj_name );
3080 if ((debug_file = get_debug_file( make, output_file )))
3081 output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3082 output_filenames( all_libs );
3083 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3084 output( "\n" );
3088 /*******************************************************************
3089 * output_source_default
3091 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3093 struct strarray defines = get_source_defines( make, source, obj );
3094 int is_dll_src = (make->testdll &&
3095 strendswith( source->name, ".c" ) &&
3096 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3097 int need_cross = (crosstarget &&
3098 !(source->file->flags & FLAG_C_UNIX) &&
3099 (make->is_cross ||
3100 ((source->file->flags & FLAG_C_IMPLIB) &&
3101 (needs_cross_lib( make ) || needs_delay_lib( make ))) ||
3102 (make->staticlib && needs_cross_lib( make ))));
3103 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
3104 (!need_cross ||
3105 (source->file->flags & FLAG_C_IMPLIB) ||
3106 (make->module && make->staticlib)));
3108 if ((source->file->flags & FLAG_GENERATED) &&
3109 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3110 strarray_add( &make->clean_files, source->basename );
3111 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
3113 if (need_obj)
3115 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3116 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3117 else if (!is_dll_src && !(source->file->flags & FLAG_C_IMPLIB))
3118 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3119 else
3120 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3121 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3122 output( "\t$(CC) -c -o $@ %s", source->filename );
3123 output_filenames( defines );
3124 if (make->module || make->staticlib || make->sharedlib || make->testdll)
3126 output_filenames( dll_flags );
3127 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3129 output_filenames( extra_cflags );
3130 output_filenames( cpp_flags );
3131 output_filename( "$(CFLAGS)" );
3132 output( "\n" );
3134 if (need_cross)
3136 if (!is_dll_src && !(source->file->flags & FLAG_C_IMPLIB))
3137 strarray_add( &make->crossobj_files, strmake( "%s.cross.o", obj ));
3138 else
3139 strarray_add( &make->clean_files, strmake( "%s.cross.o", obj ));
3140 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3141 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
3142 output_filenames( defines );
3143 output_filenames( extra_cross_cflags );
3144 if (source->file->flags & FLAG_C_IMPLIB) output_filename( "-fno-builtin" );
3145 output_filenames( cpp_flags );
3146 output_filename( "$(CROSSCFLAGS)" );
3147 output( "\n" );
3149 if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
3151 strarray_add( &make->c2man_files, source->filename );
3152 if (make->testdll && !is_dll_src)
3154 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3155 output( "%s.ok:\n", obj_dir_path( make, obj ));
3156 output( "\t%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3157 root_src_dir_path( "tools/runtest" ), make->testdll,
3158 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3159 make->is_cross ? "" : dll_ext, obj );
3162 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3163 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3164 output( ":" );
3165 output_filenames( source->dependencies );
3166 output( "\n" );
3170 /* dispatch table to output rules for a single source file */
3171 static const struct
3173 const char *ext;
3174 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3175 } output_source_funcs[] =
3177 { "y", output_source_y },
3178 { "l", output_source_l },
3179 { "h", output_source_h },
3180 { "rh", output_source_h },
3181 { "inl", output_source_h },
3182 { "rc", output_source_rc },
3183 { "mc", output_source_mc },
3184 { "res", output_source_res },
3185 { "idl", output_source_idl },
3186 { "tlb", output_source_tlb },
3187 { "sfd", output_source_sfd },
3188 { "svg", output_source_svg },
3189 { "nls", output_source_nls },
3190 { "desktop", output_source_desktop },
3191 { "po", output_source_po },
3192 { "in", output_source_in },
3193 { "x", output_source_x },
3194 { "spec", output_source_spec },
3195 { NULL, output_source_default }
3199 /*******************************************************************
3200 * has_object_file
3202 static int has_object_file( struct makefile *make )
3204 struct incl_file *source;
3205 int i;
3207 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3209 char *ext = get_extension( source->name );
3211 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3212 ext++;
3214 for (i = 0; output_source_funcs[i].ext; i++)
3215 if (!strcmp( ext, output_source_funcs[i].ext )) break;
3217 if (!output_source_funcs[i].ext) return 1; /* default extension builds to an object file */
3219 return 0;
3223 /*******************************************************************
3224 * output_man_pages
3226 static void output_man_pages( struct makefile *make )
3228 if (make->c2man_files.count)
3230 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3232 output( "manpages::\n" );
3233 output( "\t%s -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3234 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3235 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3236 output_filename( strmake( "-o documentation/man%s", man_ext ));
3237 output_filenames( make->c2man_files );
3238 output( "\n" );
3239 output( "htmlpages::\n" );
3240 output( "\t%s -Th -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3241 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3242 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3243 output_filename( "-o documentation/html" );
3244 output_filenames( make->c2man_files );
3245 output( "\n" );
3246 output( "sgmlpages::\n" );
3247 output( "\t%s -Ts -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3248 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3249 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3250 output_filename( "-o documentation/api-guide" );
3251 output_filenames( make->c2man_files );
3252 output( "\n" );
3253 output( "xmlpages::\n" );
3254 output( "\t%s -Tx -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
3255 output_filename( strmake( "-R%s", root_src_dir_path( "" )));
3256 output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
3257 output_filename( "-o documentation/api-guide-xml" );
3258 output_filenames( make->c2man_files );
3259 output( "\n" );
3260 strarray_add( &make->phony_targets, "manpages" );
3261 strarray_add( &make->phony_targets, "htmlpages" );
3262 strarray_add( &make->phony_targets, "sgmlpages" );
3263 strarray_add( &make->phony_targets, "xmlpages" );
3268 /*******************************************************************
3269 * output_module
3271 static void output_module( struct makefile *make )
3273 struct strarray all_libs = empty_strarray;
3274 struct strarray dep_libs = empty_strarray;
3275 char *module_path = obj_dir_path( make, make->module );
3276 const char *debug_file = NULL;
3277 char *spec_file = NULL;
3278 unsigned int i;
3280 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3281 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, 0 ));
3282 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0, 0 ));
3283 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3285 if (make->is_cross)
3287 if (delay_load_flag)
3289 for (i = 0; i < make->delayimports.count; i++)
3290 strarray_add( &all_libs, strmake( "%s%s%s", delay_load_flag, make->delayimports.str[i],
3291 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3293 strarray_add( &make->all_targets, strmake( "%s", make->module ));
3294 add_install_rule( make, make->module, strmake( "%s", make->module ),
3295 strmake( "c$(dlldir)/%s", make->module ));
3296 debug_file = get_debug_file( make, module_path );
3297 output( "%s:", module_path );
3299 else if (*dll_ext)
3301 if (!make->use_msvcrt) strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3302 for (i = 0; i < make->delayimports.count; i++)
3303 strarray_add( &all_libs, strmake( "-Wl,-delayload,%s%s", make->delayimports.str[i],
3304 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3305 strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
3306 strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
3307 add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
3308 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
3309 add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
3310 strmake( "d$(dlldir)/fakedlls/%s", make->module ));
3311 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
3313 else
3315 strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3316 strarray_add( &make->all_targets, make->module );
3317 add_install_rule( make, make->module, make->module,
3318 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
3319 debug_file = get_debug_file( make, module_path );
3320 output( "%s:", module_path );
3323 if (spec_file) output_filename( spec_file );
3324 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3325 output_filenames_obj_dir( make, make->res_files );
3326 output_filenames( dep_libs );
3327 output_filename( tools_path( make, "winebuild" ));
3328 output_filename( tools_path( make, "winegcc" ));
3329 output( "\n" );
3330 output_winegcc_command( make, make->is_cross );
3331 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3332 if (spec_file)
3334 output_filename( "-shared" );
3335 output_filename( spec_file );
3337 output_filenames( make->extradllflags );
3338 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3339 output_filenames_obj_dir( make, make->res_files );
3340 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3341 output_filenames( all_libs );
3342 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3343 output( "\n" );
3345 if (make->unixobj_files.count)
3347 struct strarray unix_imports = empty_strarray;
3348 struct strarray unix_libs = empty_strarray;
3349 struct strarray unix_deps = empty_strarray;
3350 char *ext, *unix_lib = xmalloc( strlen( make->module ) + strlen( dll_ext ) + 1 );
3351 strcpy( unix_lib, make->module );
3352 if ((ext = get_extension( unix_lib ))) *ext = 0;
3353 strcat( unix_lib, dll_ext );
3355 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
3356 strarray_add( &unix_imports, "ntdll" );
3357 strarray_add( &unix_imports, "winecrt0" );
3359 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 1 ));
3360 strarray_addall( &unix_libs, add_unix_libraries( make, &unix_deps ));
3362 strarray_add( &make->all_targets, unix_lib );
3363 add_install_rule( make, make->module, unix_lib, strmake( "p$(dlldir)/%s", unix_lib ));
3364 output( "%s:", obj_dir_path( make, unix_lib ));
3365 if (spec_file) output_filename( spec_file );
3366 output_filenames_obj_dir( make, make->unixobj_files );
3367 output_filenames( unix_deps );
3368 output_filename( tools_path( make, "winebuild" ));
3369 output_filename( tools_path( make, "winegcc" ));
3370 output( "\n" );
3371 output_winegcc_command( make, 0 );
3372 output_filename( "-munix" );
3373 output_filename( "-shared" );
3374 if (spec_file) output_filename( spec_file );
3375 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) output_filename( "-nodefaultlibs" );
3376 output_filenames_obj_dir( make, make->unixobj_files );
3377 output_filenames( unix_libs );
3378 output_filename( "$(LDFLAGS)" );
3379 output( "\n" );
3382 if (spec_file && make->importlib)
3384 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3385 if (*dll_ext && !make->implib_objs.count)
3387 strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
3388 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
3389 output( "\t%s -w --def -o $@", tools_path( make, "winebuild" ) );
3390 output_filenames( target_flags );
3391 if (make->is_win16) output_filename( "-m16" );
3392 output_filename( "--export" );
3393 output_filename( spec_file );
3394 output( "\n" );
3395 add_install_rule( make, make->importlib,
3396 strmake( "lib%s.def", make->importlib ),
3397 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
3399 else
3401 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3402 if (!*dll_ext && needs_delay_lib( make ))
3404 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3405 output( "%s.delay.a ", importlib_path );
3407 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3408 output_filenames_obj_dir( make, make->implib_objs );
3409 output( "\n" );
3410 output( "\t%s -w --implib -o $@", tools_path( make, "winebuild" ) );
3411 output_filenames( target_flags );
3412 if (make->is_win16) output_filename( "-m16" );
3413 output_filename( "--export" );
3414 output_filename( spec_file );
3415 output_filenames_obj_dir( make, make->implib_objs );
3416 output( "\n" );
3417 add_install_rule( make, make->importlib,
3418 strmake( "lib%s.a", make->importlib ),
3419 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
3421 if (crosstarget && (needs_cross_lib( make ) || needs_delay_lib( make )))
3423 struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
3424 if (needs_cross_lib( make ))
3426 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3427 output_filename( strmake( "%s.cross.a", importlib_path ));
3429 if (needs_delay_lib( make ))
3431 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3432 output_filename( strmake( "%s.delay.a", importlib_path ));
3434 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3435 output_filenames_obj_dir( make, cross_files );
3436 output( "\n" );
3437 output( "\t%s -b %s -w --implib -o $@", tools_path( make, "winebuild" ), crosstarget );
3438 if (make->is_win16) output_filename( "-m16" );
3439 output_filename( "--export" );
3440 output_filename( spec_file );
3441 output_filenames_obj_dir( make, cross_files );
3442 output( "\n" );
3444 if (needs_implib_symlink( make ))
3445 strarray_addall( &top_makefile->clean_files, output_importlib_symlinks( make ));
3448 if (spec_file)
3449 output_man_pages( make );
3450 else if (*dll_ext && !make->is_win16 && strendswith( make->module, ".exe" ))
3452 char *binary = replace_extension( make->module, ".exe", "" );
3453 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3458 /*******************************************************************
3459 * output_static_lib
3461 static void output_static_lib( struct makefile *make )
3463 strarray_add( &make->all_targets, make->staticlib );
3464 output( "%s:", obj_dir_path( make, make->staticlib ));
3465 output_filenames_obj_dir( make, make->object_files );
3466 output_filenames_obj_dir( make, make->unixobj_files );
3467 output( "\n\trm -f $@\n" );
3468 output( "\t%s rc $@", ar );
3469 output_filenames_obj_dir( make, make->object_files );
3470 output_filenames_obj_dir( make, make->unixobj_files );
3471 output( "\n\t%s $@\n", ranlib );
3472 add_install_rule( make, make->staticlib, make->staticlib,
3473 strmake( "d$(dlldir)/%s", make->staticlib ));
3474 if (needs_cross_lib( make ))
3476 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3478 strarray_add( &make->all_targets, name );
3479 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3480 output_filenames_obj_dir( make, make->crossobj_files );
3481 output( "\n" );
3482 output( "\t%s -b %s -w --staticlib -o $@", tools_path( make, "winebuild" ), crosstarget );
3483 output_filenames_obj_dir( make, make->crossobj_files );
3484 output( "\n" );
3489 /*******************************************************************
3490 * output_shared_lib
3492 static void output_shared_lib( struct makefile *make )
3494 unsigned int i;
3495 char *basename, *p;
3496 struct strarray names = get_shared_lib_names( make->sharedlib );
3497 struct strarray all_libs = empty_strarray;
3498 struct strarray dep_libs = empty_strarray;
3500 basename = xstrdup( make->sharedlib );
3501 if ((p = strchr( basename, '.' ))) *p = 0;
3503 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3504 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3505 strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
3507 output( "%s:", obj_dir_path( make, make->sharedlib ));
3508 output_filenames_obj_dir( make, make->object_files );
3509 output_filenames( dep_libs );
3510 output( "\n" );
3511 output( "\t$(CC) -o $@" );
3512 output_filenames_obj_dir( make, make->object_files );
3513 output_filenames( all_libs );
3514 output_filename( "$(LDFLAGS)" );
3515 output( "\n" );
3516 add_install_rule( make, make->sharedlib, make->sharedlib,
3517 strmake( "p$(libdir)/%s", make->sharedlib ));
3518 for (i = 1; i < names.count; i++)
3520 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3521 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3522 add_install_rule( make, names.str[i], names.str[i-1],
3523 strmake( "y$(libdir)/%s", names.str[i] ));
3525 strarray_addall( &make->all_targets, names );
3529 /*******************************************************************
3530 * output_test_module
3532 static void output_test_module( struct makefile *make )
3534 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3535 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3536 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3537 struct strarray dep_libs = empty_strarray;
3538 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0, 0 );
3539 struct makefile *parent = get_parent_makefile( make );
3540 const char *ext = make->is_cross ? "" : dll_ext;
3541 const char *parent_ext = parent && parent->is_cross ? "" : dll_ext;
3542 const char *debug_file;
3543 char *output_file;
3545 add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
3546 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3547 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3548 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3549 output( "%s:\n", output_file );
3550 output_winegcc_command( make, make->is_cross );
3551 output_filenames( make->extradllflags );
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 if ((debug_file = get_debug_file( make, output_file )))
3555 output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
3556 output_filenames( all_libs );
3557 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3558 output( "\n" );
3559 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3560 output_winegcc_command( make, make->is_cross );
3561 output_filename( "-s" );
3562 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3563 output_filenames( make->extradllflags );
3564 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3565 output_filenames_obj_dir( make, make->res_files );
3566 output_filenames( all_libs );
3567 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3568 output( "\n" );
3569 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3570 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3571 output_filenames_obj_dir( make, make->res_files );
3572 output_filenames( dep_libs );
3573 output_filename( tools_path( make, "winebuild" ));
3574 output_filename( tools_path( make, "winegcc" ));
3575 output( "\n" );
3577 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3578 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n",
3579 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3581 output_filenames_obj_dir( make, make->ok_files );
3582 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3583 if (parent) output( " %s%s", obj_dir_path( parent, make->testdll ), parent_ext );
3584 output( "\n" );
3585 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3586 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3587 output( "\n" );
3588 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3589 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3590 output( "%s::\n", obj_dir_path( make, "testclean" ));
3591 output( "\trm -f" );
3592 output_filenames_obj_dir( make, make->ok_files );
3593 output( "\n" );
3594 strarray_addall( &make->clean_files, make->ok_files );
3595 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3599 /*******************************************************************
3600 * output_programs
3602 static void output_programs( struct makefile *make )
3604 unsigned int i, j;
3606 for (i = 0; i < make->programs.count; i++)
3608 char *program_installed = NULL;
3609 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3610 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3611 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3612 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3613 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3615 if (!objs.count) objs = make->object_files;
3616 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3617 strarray_addall( &all_libs, add_unix_libraries( make, &deps ));
3619 output( "%s:", obj_dir_path( make, program ) );
3620 output_filenames_obj_dir( make, objs );
3621 output_filenames( deps );
3622 output( "\n" );
3623 output( "\t$(CC) -o $@" );
3624 output_filenames_obj_dir( make, objs );
3625 output_filenames( all_libs );
3626 output_filename( "$(LDFLAGS)" );
3627 output( "\n" );
3628 strarray_add( &make->all_targets, program );
3630 for (j = 0; j < symlinks.count; j++)
3632 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3633 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3635 strarray_addall( &make->all_targets, symlinks );
3637 add_install_rule( make, program, program_installed ? program_installed : program,
3638 strmake( "p$(bindir)/%s", program ));
3639 for (j = 0; j < symlinks.count; j++)
3640 add_install_rule( make, symlinks.str[j], program,
3641 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3646 /*******************************************************************
3647 * output_subdirs
3649 static void output_subdirs( struct makefile *make )
3651 struct strarray all_targets = empty_strarray;
3652 struct strarray makefile_deps = empty_strarray;
3653 struct strarray clean_files = empty_strarray;
3654 struct strarray testclean_files = empty_strarray;
3655 struct strarray distclean_files = empty_strarray;
3656 struct strarray dependencies = empty_strarray;
3657 struct strarray install_lib_deps = empty_strarray;
3658 struct strarray install_dev_deps = empty_strarray;
3659 struct strarray tooldeps_deps = empty_strarray;
3660 struct strarray buildtest_deps = empty_strarray;
3661 unsigned int i;
3663 strarray_addall( &clean_files, make->clean_files );
3664 strarray_addall( &distclean_files, make->distclean_files );
3665 strarray_addall( &all_targets, make->all_targets );
3666 for (i = 0; i < subdirs.count; i++)
3668 strarray_add( &makefile_deps, src_dir_path( submakes[i], strmake ( "%s.in", output_makefile_name )));
3669 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3670 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3671 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3672 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3673 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3674 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3675 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3677 if (submakes[i]->disabled) continue;
3679 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3680 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3681 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3682 if (submakes[i]->testdll)
3683 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3684 if (submakes[i]->install_rules[INSTALL_LIB].count)
3685 strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
3686 if (submakes[i]->install_rules[INSTALL_DEV].count)
3687 strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
3689 strarray_addall( &dependencies, makefile_deps );
3690 output( "all:" );
3691 output_filenames( all_targets );
3692 output( "\n" );
3693 output( "Makefile:" );
3694 output_filenames( makefile_deps );
3695 output( "\n" );
3696 output_filenames( dependencies );
3697 output( ":\n" );
3698 if (install_lib_deps.count)
3700 output( "install install-lib::" );
3701 output_filenames( install_lib_deps );
3702 output( "\n" );
3703 strarray_add_uniq( &make->phony_targets, "install" );
3704 strarray_add_uniq( &make->phony_targets, "install-lib" );
3706 if (install_dev_deps.count)
3708 output( "install install-dev::" );
3709 output_filenames( install_dev_deps );
3710 output( "\n" );
3711 strarray_add_uniq( &make->phony_targets, "install" );
3712 strarray_add_uniq( &make->phony_targets, "install-dev" );
3714 output_uninstall_rules( make );
3715 if (buildtest_deps.count)
3717 output( "buildtests:" );
3718 output_filenames( buildtest_deps );
3719 output( "\n" );
3720 strarray_add_uniq( &make->phony_targets, "buildtests" );
3722 output( "check test:" );
3723 output_filenames( testclean_files );
3724 output( "\n" );
3725 strarray_add_uniq( &make->phony_targets, "check" );
3726 strarray_add_uniq( &make->phony_targets, "test" );
3728 output( "clean::\n");
3729 output_rm_filenames( clean_files );
3730 output( "testclean::\n");
3731 output_rm_filenames( testclean_files );
3732 output( "distclean::\n");
3733 output_rm_filenames( distclean_files );
3734 strarray_add_uniq( &make->phony_targets, "distclean" );
3735 strarray_add_uniq( &make->phony_targets, "testclean" );
3737 if (tooldeps_deps.count)
3739 output( "__tooldeps__:" );
3740 output_filenames( tooldeps_deps );
3741 output( "\n" );
3742 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3745 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3747 if (make->phony_targets.count)
3749 output( ".PHONY:" );
3750 output_filenames( make->phony_targets );
3751 output( "\n" );
3756 /*******************************************************************
3757 * output_sources
3759 static void output_sources( struct makefile *make )
3761 struct strarray all_targets = empty_strarray;
3762 struct incl_file *source;
3763 unsigned int i, j;
3765 strarray_add_uniq( &make->phony_targets, "all" );
3767 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3769 char *obj = xstrdup( source->name );
3770 char *ext = get_extension( obj );
3772 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3773 *ext++ = 0;
3775 for (j = 0; output_source_funcs[j].ext; j++)
3776 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3778 output_source_funcs[j].fn( make, source, obj );
3779 strarray_addall_uniq( &make->dependencies, source->dependencies );
3782 /* special case for winetest: add resource files from other test dirs */
3783 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3785 struct strarray tests = enable_tests;
3786 if (!tests.count)
3787 for (i = 0; i < subdirs.count; i++)
3788 if (submakes[i]->testdll && !submakes[i]->disabled)
3789 strarray_add( &tests, submakes[i]->testdll );
3790 for (i = 0; i < tests.count; i++)
3791 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3794 if (make->dlldata_files.count)
3796 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3797 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3798 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
3799 output_filenames( make->dlldata_files );
3800 output( "\n" );
3803 if (make->staticlib) output_static_lib( make );
3804 else if (make->module) output_module( make );
3805 else if (make->testdll) output_test_module( make );
3806 else if (make->sharedlib) output_shared_lib( make );
3807 else if (make->programs.count) output_programs( make );
3809 for (i = 0; i < make->scripts.count; i++)
3810 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3811 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3813 for (i = 0; i < make->extra_targets.count; i++)
3814 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3815 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3816 else
3817 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3819 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3820 strarray_add( &make->distclean_files, "Makefile" );
3821 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3823 if (!make->obj_dir)
3824 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3825 else if (!strcmp( make->obj_dir, "po" ))
3826 strarray_add( &make->distclean_files, "LINGUAS" );
3828 strarray_addall( &make->clean_files, make->object_files );
3829 strarray_addall( &make->clean_files, make->crossobj_files );
3830 strarray_addall( &make->clean_files, make->unixobj_files );
3831 strarray_addall( &make->clean_files, make->res_files );
3832 strarray_addall( &make->clean_files, make->pot_files );
3833 strarray_addall( &make->clean_files, make->debug_files );
3834 strarray_addall( &make->clean_files, make->all_targets );
3836 if (make == top_makefile)
3838 output_subdirs( make );
3839 return;
3842 strarray_addall( &all_targets, make->all_targets );
3843 strarray_addall( &all_targets, make->font_files );
3844 if (all_targets.count)
3846 output( "%s:", obj_dir_path( make, "all" ));
3847 output_filenames_obj_dir( make, all_targets );
3848 output( "\n" );
3849 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3851 output_install_rules( make, INSTALL_LIB, "install-lib" );
3852 output_install_rules( make, INSTALL_DEV, "install-dev" );
3854 if (make->clean_files.count)
3856 output( "%s::\n", obj_dir_path( make, "clean" ));
3857 output( "\trm -f" );
3858 output_filenames_obj_dir( make, make->clean_files );
3859 output( "\n" );
3860 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3865 /*******************************************************************
3866 * create_temp_file
3868 static FILE *create_temp_file( const char *orig )
3870 char *name = xmalloc( strlen(orig) + 13 );
3871 unsigned int i, id = getpid();
3872 int fd;
3873 FILE *ret = NULL;
3875 for (i = 0; i < 100; i++)
3877 sprintf( name, "%s.tmp%08x", orig, id );
3878 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3880 ret = fdopen( fd, "w" );
3881 break;
3883 if (errno != EEXIST) break;
3884 id += 7777;
3886 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3887 temp_file_name = name;
3888 return ret;
3892 /*******************************************************************
3893 * rename_temp_file
3895 static void rename_temp_file( const char *dest )
3897 int ret = rename( temp_file_name, dest );
3898 if (ret == -1 && errno == EEXIST)
3900 /* rename doesn't overwrite on windows */
3901 unlink( dest );
3902 ret = rename( temp_file_name, dest );
3904 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3905 temp_file_name = NULL;
3909 /*******************************************************************
3910 * are_files_identical
3912 static int are_files_identical( FILE *file1, FILE *file2 )
3914 for (;;)
3916 char buffer1[8192], buffer2[8192];
3917 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3918 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3919 if (size1 != size2) return 0;
3920 if (!size1) return feof( file1 ) && feof( file2 );
3921 if (memcmp( buffer1, buffer2, size1 )) return 0;
3926 /*******************************************************************
3927 * rename_temp_file_if_changed
3929 static void rename_temp_file_if_changed( const char *dest )
3931 FILE *file1, *file2;
3932 int do_rename = 1;
3934 if ((file1 = fopen( dest, "r" )))
3936 if ((file2 = fopen( temp_file_name, "r" )))
3938 do_rename = !are_files_identical( file1, file2 );
3939 fclose( file2 );
3941 fclose( file1 );
3943 if (!do_rename)
3945 unlink( temp_file_name );
3946 temp_file_name = NULL;
3948 else rename_temp_file( dest );
3952 /*******************************************************************
3953 * output_linguas
3955 static void output_linguas( const struct makefile *make )
3957 const char *dest = obj_dir_path( make, "LINGUAS" );
3958 struct incl_file *source;
3960 output_file = create_temp_file( dest );
3962 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3963 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3964 if (strendswith( source->name, ".po" ))
3965 output( "%s\n", replace_extension( source->name, ".po", "" ));
3967 if (fclose( output_file )) fatal_perror( "write" );
3968 output_file = NULL;
3969 rename_temp_file_if_changed( dest );
3973 /*******************************************************************
3974 * output_testlist
3976 static void output_testlist( const struct makefile *make )
3978 const char *dest = obj_dir_path( make, "testlist.c" );
3979 struct strarray files = empty_strarray;
3980 unsigned int i;
3982 for (i = 0; i < make->ok_files.count; i++)
3983 strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
3985 output_file = create_temp_file( dest );
3987 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3988 output( "#define WIN32_LEAN_AND_MEAN\n" );
3989 output( "#include <windows.h>\n\n" );
3990 output( "#define STANDALONE\n" );
3991 output( "#include \"wine/test.h\"\n\n" );
3993 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
3994 output( "\n" );
3995 output( "const struct test winetest_testlist[] =\n" );
3996 output( "{\n" );
3997 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
3998 output( " { 0, 0 }\n" );
3999 output( "};\n" );
4001 if (fclose( output_file )) fatal_perror( "write" );
4002 output_file = NULL;
4003 rename_temp_file_if_changed( dest );
4007 /*******************************************************************
4008 * output_gitignore
4010 static void output_gitignore( const char *dest, struct strarray files )
4012 int i;
4014 output_file = create_temp_file( dest );
4016 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
4017 for (i = 0; i < files.count; i++)
4019 if (!strchr( files.str[i], '/' )) output( "/" );
4020 output( "%s\n", files.str[i] );
4023 if (fclose( output_file )) fatal_perror( "write" );
4024 output_file = NULL;
4025 rename_temp_file( dest );
4029 /*******************************************************************
4030 * output_stub_makefile
4032 static void output_stub_makefile( struct makefile *make )
4034 struct strarray targets = empty_strarray;
4035 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
4037 if (make_var) output( "MAKE = %s\n\n", make_var );
4038 output( "all:\n" );
4040 if (make->all_targets.count) strarray_add( &targets, "all" );
4041 if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
4042 strarray_add( &targets, "install" );
4043 if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
4044 if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
4045 if (make->clean_files.count) strarray_add( &targets, "clean" );
4046 if (make->ok_files.count)
4048 strarray_add( &targets, "check" );
4049 strarray_add( &targets, "test" );
4050 strarray_add( &targets, "testclean" );
4053 output_filenames( targets );
4054 output_filenames( make->clean_files );
4055 output( ":\n" );
4056 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4057 output( ".PHONY:" );
4058 output_filenames( targets );
4059 output( "\n" );
4063 /*******************************************************************
4064 * output_dependencies
4066 static void output_dependencies( struct makefile *make )
4068 struct strarray ignore_files = empty_strarray;
4069 char buffer[1024];
4070 FILE *src_file;
4071 int i, found = 0;
4073 if (make->obj_dir) create_dir( make->obj_dir );
4075 output_file_name = obj_dir_path( make, output_makefile_name );
4076 output_file = create_temp_file( output_file_name );
4078 /* copy the contents of the source makefile */
4079 src_file = open_input_makefile( make );
4080 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4082 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4083 found = !strncmp( buffer, separator, strlen(separator) );
4085 if (fclose( src_file )) fatal_perror( "close" );
4086 input_file_name = NULL;
4088 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4090 if (make == top_makefile)
4092 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4093 output_sources( make );
4095 else output_stub_makefile( make );
4097 /* disable implicit rules */
4098 output( ".SUFFIXES:\n" );
4100 fclose( output_file );
4101 output_file = NULL;
4102 rename_temp_file( output_file_name );
4104 strarray_addall( &ignore_files, make->distclean_files );
4105 strarray_addall( &ignore_files, make->clean_files );
4106 if (make->testdll) output_testlist( make );
4107 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4108 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4110 create_file_directories( make, ignore_files );
4112 output_file_name = NULL;
4116 /*******************************************************************
4117 * load_sources
4119 static void load_sources( struct makefile *make )
4121 static const char *source_vars[] =
4123 "SOURCES",
4124 "C_SRCS",
4125 "OBJC_SRCS",
4126 "RC_SRCS",
4127 "MC_SRCS",
4128 "IDL_SRCS",
4129 "BISON_SRCS",
4130 "LEX_SRCS",
4131 "HEADER_SRCS",
4132 "XTEMPLATE_SRCS",
4133 "SVG_SRCS",
4134 "FONT_SRCS",
4135 "IN_SRCS",
4136 "PO_SRCS",
4137 "MANPAGES",
4138 NULL
4140 const char **var;
4141 unsigned int i;
4142 struct strarray value;
4143 struct incl_file *file;
4145 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4146 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4148 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4149 make->module = get_expanded_make_variable( make, "MODULE" );
4150 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4151 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4152 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4153 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4155 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4156 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4157 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4158 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4159 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4160 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4161 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4162 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4164 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
4166 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4167 if ((make->module && make->staticlib) || make->testdll || make->is_win16)
4168 strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4170 strarray_addall( &make->extradllflags, get_expanded_make_var_array( make, "APPMODE" ));
4171 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4172 make->use_msvcrt = strarray_exists( &make->extradllflags, "-mno-cygwin" );
4173 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4174 strarray_exists( &make->extradllflags, "-mwindows" );
4176 if (make->module && !make->install_lib.count && !make->install_dev.count)
4178 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4179 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4180 else strarray_add( &make->install_lib, make->module );
4183 make->include_paths = empty_strarray;
4184 make->include_args = empty_strarray;
4185 make->define_args = empty_strarray;
4186 strarray_add( &make->define_args, "-D__WINESRC__" );
4188 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4189 for (i = 0; i < value.count; i++)
4190 if (!strncmp( value.str[i], "-I", 2 ))
4191 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4192 else
4193 strarray_add_uniq( &make->define_args, value.str[i] );
4194 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4196 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4197 if (make->src_dir)
4198 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4199 if (make->parent_dir)
4200 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4201 strarray_add( &make->include_args, "-Iinclude" );
4202 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4204 list_init( &make->sources );
4205 list_init( &make->includes );
4207 for (var = source_vars; *var; var++)
4209 value = get_expanded_make_var_array( make, *var );
4210 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4213 add_generated_sources( make );
4215 if (!make->use_msvcrt && !has_object_file( make ))
4217 strarray_add( &make->extradllflags, "-mno-cygwin" );
4218 make->use_msvcrt = 1;
4221 if (make->use_msvcrt) add_crt_import( make, &make->imports, &make->define_args );
4223 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4224 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4226 make->is_cross = crosstarget && make->use_msvcrt;
4228 if (make->is_cross)
4230 strarray_addall_uniq( &cross_import_libs, make->imports );
4231 strarray_addall_uniq( &cross_import_libs, make->extra_imports );
4232 if (make->is_win16) strarray_add_uniq( &cross_import_libs, "kernel" );
4233 strarray_add_uniq( &cross_import_libs, "winecrt0" );
4234 strarray_add_uniq( &cross_import_libs, "kernel32" );
4235 strarray_add_uniq( &cross_import_libs, "ntdll" );
4238 if (!*dll_ext || make->is_cross)
4239 for (i = 0; i < make->delayimports.count; i++)
4240 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4244 /*******************************************************************
4245 * parse_makeflags
4247 static void parse_makeflags( const char *flags )
4249 const char *p = flags;
4250 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4252 while (*p)
4254 while (isspace(*p)) p++;
4255 var = buffer;
4256 while (*p && !isspace(*p))
4258 if (*p == '\\' && p[1]) p++;
4259 *var++ = *p++;
4261 *var = 0;
4262 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4267 /*******************************************************************
4268 * parse_option
4270 static int parse_option( const char *opt )
4272 if (opt[0] != '-')
4274 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4275 return 0;
4277 switch(opt[1])
4279 case 'f':
4280 if (opt[2]) output_makefile_name = opt + 2;
4281 break;
4282 case 'R':
4283 relative_dir_mode = 1;
4284 break;
4285 default:
4286 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4287 exit(1);
4289 return 1;
4293 /*******************************************************************
4294 * main
4296 int main( int argc, char *argv[] )
4298 const char *makeflags = getenv( "MAKEFLAGS" );
4299 int i, j;
4301 if (makeflags) parse_makeflags( makeflags );
4303 i = 1;
4304 while (i < argc)
4306 if (parse_option( argv[i] ))
4308 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4309 argc--;
4311 else i++;
4314 if (relative_dir_mode)
4316 char *relpath;
4318 if (argc != 3)
4320 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4321 exit( 1 );
4323 relpath = get_relative_path( argv[1], argv[2] );
4324 printf( "%s\n", relpath ? relpath : "." );
4325 exit( 0 );
4328 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4330 atexit( cleanup_files );
4331 signal( SIGTERM, exit_on_signal );
4332 signal( SIGINT, exit_on_signal );
4333 #ifdef SIGHUP
4334 signal( SIGHUP, exit_on_signal );
4335 #endif
4337 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4339 top_makefile = parse_makefile( NULL );
4341 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4342 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4343 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4344 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4345 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4346 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4347 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4348 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4349 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4350 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4351 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4352 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4354 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4355 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
4356 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
4357 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4358 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
4359 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4360 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4361 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4362 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4363 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4364 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4365 bison = get_expanded_make_variable( top_makefile, "BISON" );
4366 ar = get_expanded_make_variable( top_makefile, "AR" );
4367 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4368 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4369 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4370 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4371 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4372 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4373 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4375 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4376 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4377 if (!exe_ext) exe_ext = "";
4378 if (!tools_ext) tools_ext = "";
4379 if (!man_ext) man_ext = "3w";
4381 top_makefile->src_dir = root_src_dir;
4382 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4383 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4384 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4386 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4388 load_sources( top_makefile );
4389 for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
4391 output_dependencies( top_makefile );
4392 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4394 return 0;