makefiles: Generate top-level rules for building mo files.
[wine.git] / tools / makedep.c
blob79ad71da5cb87ee3abf5d7cc5e8cf4f9bc5b4d04
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <signal.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "wine/list.h"
38 enum incl_type
40 INCL_NORMAL, /* #include "foo.h" */
41 INCL_SYSTEM, /* #include <foo.h> */
42 INCL_IMPORT, /* idl import "foo.idl" */
43 INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
44 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
45 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
48 struct dependency
50 int line; /* source line where this header is included */
51 enum incl_type type; /* type of include */
52 char *name; /* header name */
55 struct file
57 struct list entry;
58 char *name; /* full file name relative to cwd */
59 void *args; /* custom arguments for makefile rule */
60 unsigned int flags; /* flags (see below) */
61 unsigned int deps_count; /* files in use */
62 unsigned int deps_size; /* total allocated size */
63 struct dependency *deps; /* all header dependencies */
66 struct incl_file
68 struct list entry;
69 struct file *file;
70 char *name;
71 char *filename;
72 char *sourcename; /* source file name for generated headers */
73 struct incl_file *included_by; /* file that included this one */
74 int included_line; /* line where this file was included */
75 enum incl_type type; /* type of include */
76 struct incl_file *owner;
77 unsigned int files_count; /* files in use */
78 unsigned int files_size; /* total allocated size */
79 struct incl_file **files;
82 #define FLAG_GENERATED 0x000001 /* generated file */
83 #define FLAG_INSTALL 0x000002 /* file to install */
84 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
85 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
86 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
87 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
88 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
89 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
90 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
91 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
92 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
93 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
94 #define FLAG_SFD_FONTS 0x040000 /* sfd file generated bitmap fonts */
96 static const struct
98 unsigned int flag;
99 const char *ext;
100 } idl_outputs[] =
102 { FLAG_IDL_TYPELIB, ".tlb" },
103 { FLAG_IDL_REGTYPELIB, "_t.res" },
104 { FLAG_IDL_CLIENT, "_c.c" },
105 { FLAG_IDL_IDENT, "_i.c" },
106 { FLAG_IDL_PROXY, "_p.c" },
107 { FLAG_IDL_SERVER, "_s.c" },
108 { FLAG_IDL_REGISTER, "_r.res" },
109 { FLAG_IDL_HEADER, ".h" }
112 #define HASH_SIZE 137
114 static struct list files[HASH_SIZE];
116 struct strarray
118 unsigned int count; /* strings in use */
119 unsigned int size; /* total allocated size */
120 const char **str;
123 static const struct strarray empty_strarray;
125 enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
127 /* variables common to all makefiles */
128 static struct strarray linguas;
129 static struct strarray dll_flags;
130 static struct strarray target_flags;
131 static struct strarray msvcrt_flags;
132 static struct strarray extra_cflags;
133 static struct strarray cpp_flags;
134 static struct strarray unwind_flags;
135 static struct strarray libs;
136 static struct strarray cmdline_vars;
137 static const char *root_src_dir;
138 static const char *tools_dir;
139 static const char *tools_ext;
140 static const char *exe_ext;
141 static const char *dll_ext;
142 static const char *man_ext;
143 static const char *crosstarget;
144 static const char *fontforge;
145 static const char *convert;
146 static const char *rsvg;
147 static const char *icotool;
148 static const char *dlltool;
149 static const char *msgfmt;
151 struct makefile
153 struct strarray vars;
154 struct strarray include_paths;
155 struct strarray define_args;
156 struct strarray programs;
157 struct strarray scripts;
158 struct strarray appmode;
159 struct strarray imports;
160 struct strarray subdirs;
161 struct strarray delayimports;
162 struct strarray extradllflags;
163 struct strarray install_lib;
164 struct strarray install_dev;
165 struct list sources;
166 struct list includes;
167 const char *base_dir;
168 const char *src_dir;
169 const char *obj_dir;
170 const char *top_src_dir;
171 const char *top_obj_dir;
172 const char *parent_dir;
173 const char *module;
174 const char *testdll;
175 const char *sharedlib;
176 const char *staticlib;
177 const char *staticimplib;
178 const char *importlib;
179 int use_msvcrt;
180 int is_win16;
181 struct makefile **submakes;
184 static struct makefile *top_makefile;
186 static const char separator[] = "### Dependencies";
187 static const char *output_makefile_name = "Makefile";
188 static const char *input_file_name;
189 static const char *output_file_name;
190 static const char *temp_file_name;
191 static int relative_dir_mode;
192 static int input_line;
193 static int output_column;
194 static FILE *output_file;
196 static const char Usage[] =
197 "Usage: makedep [options] [directories]\n"
198 "Options:\n"
199 " -R from to Compute the relative path between two directories\n"
200 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
203 #ifndef __GNUC__
204 #define __attribute__(x)
205 #endif
207 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
208 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
209 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
210 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
212 /*******************************************************************
213 * fatal_error
215 static void fatal_error( const char *msg, ... )
217 va_list valist;
218 va_start( valist, msg );
219 if (input_file_name)
221 fprintf( stderr, "%s:", input_file_name );
222 if (input_line) fprintf( stderr, "%d:", input_line );
223 fprintf( stderr, " error: " );
225 else fprintf( stderr, "makedep: error: " );
226 vfprintf( stderr, msg, valist );
227 va_end( valist );
228 exit(1);
232 /*******************************************************************
233 * fatal_perror
235 static void fatal_perror( const char *msg, ... )
237 va_list valist;
238 va_start( valist, msg );
239 if (input_file_name)
241 fprintf( stderr, "%s:", input_file_name );
242 if (input_line) fprintf( stderr, "%d:", input_line );
243 fprintf( stderr, " error: " );
245 else fprintf( stderr, "makedep: error: " );
246 vfprintf( stderr, msg, valist );
247 perror( " " );
248 va_end( valist );
249 exit(1);
253 /*******************************************************************
254 * cleanup_files
256 static void cleanup_files(void)
258 if (temp_file_name) unlink( temp_file_name );
259 if (output_file_name) unlink( output_file_name );
263 /*******************************************************************
264 * exit_on_signal
266 static void exit_on_signal( int sig )
268 exit( 1 ); /* this will call the atexit functions */
272 /*******************************************************************
273 * xmalloc
275 static void *xmalloc( size_t size )
277 void *res;
278 if (!(res = malloc (size ? size : 1)))
279 fatal_error( "Virtual memory exhausted.\n" );
280 return res;
284 /*******************************************************************
285 * xrealloc
287 static void *xrealloc (void *ptr, size_t size)
289 void *res;
290 assert( size );
291 if (!(res = realloc( ptr, size )))
292 fatal_error( "Virtual memory exhausted.\n" );
293 return res;
296 /*******************************************************************
297 * xstrdup
299 static char *xstrdup( const char *str )
301 char *res = strdup( str );
302 if (!res) fatal_error( "Virtual memory exhausted.\n" );
303 return res;
307 /*******************************************************************
308 * strmake
310 static char *strmake( const char* fmt, ... )
312 int n;
313 size_t size = 100;
314 va_list ap;
316 for (;;)
318 char *p = xmalloc (size);
319 va_start(ap, fmt);
320 n = vsnprintf (p, size, fmt, ap);
321 va_end(ap);
322 if (n == -1) size *= 2;
323 else if ((size_t)n >= size) size = n + 1;
324 else return xrealloc( p, n + 1 );
325 free(p);
330 /*******************************************************************
331 * strendswith
333 static int strendswith( const char* str, const char* end )
335 size_t l = strlen( str );
336 size_t m = strlen( end );
338 return l >= m && strcmp(str + l - m, end) == 0;
342 /*******************************************************************
343 * output
345 static void output( const char *format, ... )
347 int ret;
348 va_list valist;
350 va_start( valist, format );
351 ret = vfprintf( output_file, format, valist );
352 va_end( valist );
353 if (ret < 0) fatal_perror( "output" );
354 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
355 else output_column += ret;
359 /*******************************************************************
360 * strarray_add
362 static void strarray_add( struct strarray *array, const char *str )
364 if (array->count == array->size)
366 if (array->size) array->size *= 2;
367 else array->size = 16;
368 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
370 array->str[array->count++] = str;
374 /*******************************************************************
375 * strarray_addall
377 static void strarray_addall( struct strarray *array, struct strarray added )
379 unsigned int i;
381 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
385 /*******************************************************************
386 * strarray_exists
388 static int strarray_exists( const struct strarray *array, const char *str )
390 unsigned int i;
392 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
393 return 0;
397 /*******************************************************************
398 * strarray_add_uniq
400 static void strarray_add_uniq( struct strarray *array, const char *str )
402 if (!strarray_exists( array, str )) strarray_add( array, str );
406 /*******************************************************************
407 * strarray_get_value
409 * Find a value in a name/value pair string array.
411 static const char *strarray_get_value( const struct strarray *array, const char *name )
413 unsigned int i;
415 for (i = 0; i < array->count; i += 2)
416 if (!strcmp( array->str[i], name )) return array->str[i + 1];
417 return NULL;
421 /*******************************************************************
422 * strarray_set_value
424 * Define a value in a name/value pair string array.
426 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
428 unsigned int i;
430 /* redefining a variable replaces the previous value */
431 for (i = 0; i < array->count; i += 2)
433 if (strcmp( array->str[i], name )) continue;
434 array->str[i + 1] = value;
435 return;
437 strarray_add( array, name );
438 strarray_add( array, value );
442 /*******************************************************************
443 * output_filename
445 static void output_filename( const char *name )
447 if (output_column + strlen(name) + 1 > 100)
449 output( " \\\n" );
450 output( " " );
452 else if (output_column) output( " " );
453 output( "%s", name );
457 /*******************************************************************
458 * output_filenames
460 static void output_filenames( struct strarray array )
462 unsigned int i;
464 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
468 /*******************************************************************
469 * get_extension
471 static char *get_extension( char *filename )
473 char *ext = strrchr( filename, '.' );
474 if (ext && strchr( ext, '/' )) ext = NULL;
475 return ext;
479 /*******************************************************************
480 * replace_extension
482 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
484 char *ret;
485 size_t name_len = strlen( name );
486 size_t ext_len = strlen( old_ext );
488 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
489 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
490 memcpy( ret, name, name_len );
491 strcpy( ret + name_len, new_ext );
492 return ret;
496 /*******************************************************************
497 * replace_filename
499 static char *replace_filename( const char *path, const char *name )
501 const char *p;
502 char *ret;
503 size_t len;
505 if (!path) return xstrdup( name );
506 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
507 len = p - path + 1;
508 ret = xmalloc( len + strlen( name ) + 1 );
509 memcpy( ret, path, len );
510 strcpy( ret + len, name );
511 return ret;
515 /*******************************************************************
516 * strarray_replace_extension
518 static struct strarray strarray_replace_extension( const struct strarray *array,
519 const char *old_ext, const char *new_ext )
521 unsigned int i;
522 struct strarray ret;
524 ret.count = ret.size = array->count;
525 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
526 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
527 return ret;
531 /*******************************************************************
532 * replace_substr
534 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
536 size_t pos = start - str;
537 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
538 memcpy( ret, str, pos );
539 strcpy( ret + pos, replace );
540 strcat( ret + pos, start + len );
541 return ret;
545 /*******************************************************************
546 * get_relative_path
548 * Determine where the destination path is located relative to the 'from' path.
550 static char *get_relative_path( const char *from, const char *dest )
552 const char *start;
553 char *ret, *p;
554 unsigned int dotdots = 0;
556 /* a path of "." is equivalent to an empty path */
557 if (!strcmp( from, "." )) from = "";
559 for (;;)
561 while (*from == '/') from++;
562 while (*dest == '/') dest++;
563 start = dest; /* save start of next path element */
564 if (!*from) break;
566 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
567 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
569 /* count remaining elements in 'from' */
572 dotdots++;
573 while (*from && *from != '/') from++;
574 while (*from == '/') from++;
576 while (*from);
577 break;
580 if (!start[0] && !dotdots) return NULL; /* empty path */
582 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
583 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
585 if (start[0]) strcpy( p, start );
586 else p[-1] = 0; /* remove trailing slash */
587 return ret;
591 /*******************************************************************
592 * concat_paths
594 static char *concat_paths( const char *base, const char *path )
596 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
597 if (!path || !path[0]) return xstrdup( base );
598 if (path[0] == '/') return xstrdup( path );
599 return strmake( "%s/%s", base, path );
603 /*******************************************************************
604 * base_dir_path
606 static char *base_dir_path( const struct makefile *make, const char *path )
608 return concat_paths( make->base_dir, path );
612 /*******************************************************************
613 * obj_dir_path
615 static char *obj_dir_path( const struct makefile *make, const char *path )
617 return concat_paths( make->obj_dir, path );
621 /*******************************************************************
622 * src_dir_path
624 static char *src_dir_path( const struct makefile *make, const char *path )
626 if (make->src_dir) return concat_paths( make->src_dir, path );
627 return obj_dir_path( make, path );
631 /*******************************************************************
632 * top_obj_dir_path
634 static char *top_obj_dir_path( const struct makefile *make, const char *path )
636 return concat_paths( make->top_obj_dir, path );
640 /*******************************************************************
641 * top_dir_path
643 static char *top_dir_path( const struct makefile *make, const char *path )
645 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
646 return top_obj_dir_path( make, path );
650 /*******************************************************************
651 * root_dir_path
653 static char *root_dir_path( const char *path )
655 return concat_paths( root_src_dir, path );
659 /*******************************************************************
660 * tools_dir_path
662 static char *tools_dir_path( const struct makefile *make, const char *path )
664 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
665 return top_obj_dir_path( make, strmake( "tools/%s", path ));
669 /*******************************************************************
670 * tools_path
672 static char *tools_path( const struct makefile *make, const char *name )
674 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
678 /*******************************************************************
679 * get_line
681 static char *get_line( FILE *file )
683 static char *buffer;
684 static size_t size;
686 if (!size)
688 size = 1024;
689 buffer = xmalloc( size );
691 if (!fgets( buffer, size, file )) return NULL;
692 input_line++;
694 for (;;)
696 char *p = buffer + strlen(buffer);
697 /* if line is larger than buffer, resize buffer */
698 while (p == buffer + size - 1 && p[-1] != '\n')
700 buffer = xrealloc( buffer, size * 2 );
701 if (!fgets( buffer + size - 1, size + 1, file )) break;
702 p = buffer + strlen(buffer);
703 size *= 2;
705 if (p > buffer && p[-1] == '\n')
707 *(--p) = 0;
708 if (p > buffer && p[-1] == '\r') *(--p) = 0;
709 if (p > buffer && p[-1] == '\\')
711 *(--p) = 0;
712 /* line ends in backslash, read continuation line */
713 if (!fgets( p, size - (p - buffer), file )) return buffer;
714 input_line++;
715 continue;
718 return buffer;
723 /*******************************************************************
724 * hash_filename
726 static unsigned int hash_filename( const char *name )
728 unsigned int ret = 0;
729 while (*name) ret = (ret << 7) + (ret << 3) + *name++;
730 return ret % HASH_SIZE;
734 /*******************************************************************
735 * add_file
737 static struct file *add_file( const char *name )
739 struct file *file = xmalloc( sizeof(*file) );
740 memset( file, 0, sizeof(*file) );
741 file->name = xstrdup( name );
742 list_add_tail( &files[hash_filename( name )], &file->entry );
743 return file;
747 /*******************************************************************
748 * add_dependency
750 static void add_dependency( struct file *file, const char *name, enum incl_type type )
752 /* enforce some rules for the Wine tree */
754 if (!memcmp( name, "../", 3 ))
755 fatal_error( "#include directive with relative path not allowed\n" );
757 if (!strcmp( name, "config.h" ))
759 if (strendswith( file->name, ".h" ))
760 fatal_error( "config.h must not be included by a header file\n" );
761 if (file->deps_count)
762 fatal_error( "config.h must be included before anything else\n" );
764 else if (!strcmp( name, "wine/port.h" ))
766 if (strendswith( file->name, ".h" ))
767 fatal_error( "wine/port.h must not be included by a header file\n" );
768 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
769 if (file->deps_count > 1)
770 fatal_error( "wine/port.h must be included before everything except config.h\n" );
771 if (strcmp( file->deps[0].name, "config.h" ))
772 fatal_error( "config.h must be included before wine/port.h\n" );
775 if (file->deps_count >= file->deps_size)
777 file->deps_size *= 2;
778 if (file->deps_size < 16) file->deps_size = 16;
779 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
781 file->deps[file->deps_count].line = input_line;
782 file->deps[file->deps_count].type = type;
783 file->deps[file->deps_count].name = xstrdup( name );
784 file->deps_count++;
788 /*******************************************************************
789 * find_src_file
791 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
793 struct incl_file *file;
795 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
796 if (!strcmp( name, file->name )) return file;
797 return NULL;
800 /*******************************************************************
801 * find_include_file
803 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
805 struct incl_file *file;
807 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
808 if (!strcmp( name, file->name )) return file;
809 return NULL;
812 /*******************************************************************
813 * add_include
815 * Add an include file if it doesn't already exists.
817 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
818 const char *name, int line, enum incl_type type )
820 struct incl_file *include;
822 if (parent->files_count >= parent->files_size)
824 parent->files_size *= 2;
825 if (parent->files_size < 16) parent->files_size = 16;
826 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
829 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
830 if (!strcmp( name, include->name )) goto found;
832 include = xmalloc( sizeof(*include) );
833 memset( include, 0, sizeof(*include) );
834 include->name = xstrdup(name);
835 include->included_by = parent;
836 include->included_line = line;
837 include->type = type;
838 list_add_tail( &make->includes, &include->entry );
839 found:
840 parent->files[parent->files_count++] = include;
841 return include;
845 /*******************************************************************
846 * add_generated_source
848 * Add a generated source file to the list.
850 static struct incl_file *add_generated_source( struct makefile *make,
851 const char *name, const char *filename )
853 struct incl_file *file;
855 if ((file = find_src_file( make, name ))) return file; /* we already have it */
856 file = xmalloc( sizeof(*file) );
857 memset( file, 0, sizeof(*file) );
858 file->file = add_file( name );
859 file->name = xstrdup( name );
860 file->filename = obj_dir_path( make, filename ? filename : name );
861 file->file->flags = FLAG_GENERATED;
862 list_add_tail( &make->sources, &file->entry );
863 return file;
867 /*******************************************************************
868 * parse_include_directive
870 static void parse_include_directive( struct file *source, char *str )
872 char quote, *include, *p = str;
874 while (*p && isspace(*p)) p++;
875 if (*p != '\"' && *p != '<' ) return;
876 quote = *p++;
877 if (quote == '<') quote = '>';
878 include = p;
879 while (*p && (*p != quote)) p++;
880 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
881 *p = 0;
882 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
886 /*******************************************************************
887 * parse_pragma_directive
889 static void parse_pragma_directive( struct file *source, char *str )
891 char *flag, *p = str;
893 if (!isspace( *p )) return;
894 while (*p && isspace(*p)) p++;
895 p = strtok( p, " \t" );
896 if (strcmp( p, "makedep" )) return;
898 while ((flag = strtok( NULL, " \t" )))
900 if (!strcmp( flag, "depend" ))
902 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
903 return;
905 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
907 if (strendswith( source->name, ".idl" ))
909 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
910 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
911 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
912 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
913 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
914 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
915 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
916 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
918 else if (strendswith( source->name, ".rc" ))
920 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
922 else if (strendswith( source->name, ".sfd" ))
924 if (!strcmp( flag, "font" ))
926 struct strarray *array = source->args;
928 if (!array)
930 source->args = array = xmalloc( sizeof(*array) );
931 *array = empty_strarray;
932 source->flags |= FLAG_SFD_FONTS;
934 strarray_add( array, xstrdup( strtok( NULL, "" )));
935 return;
938 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
943 /*******************************************************************
944 * parse_cpp_directive
946 static void parse_cpp_directive( struct file *source, char *str )
948 while (*str && isspace(*str)) str++;
949 if (*str++ != '#') return;
950 while (*str && isspace(*str)) str++;
952 if (!strncmp( str, "include", 7 ))
953 parse_include_directive( source, str + 7 );
954 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
955 parse_include_directive( source, str + 6 );
956 else if (!strncmp( str, "pragma", 6 ))
957 parse_pragma_directive( source, str + 6 );
961 /*******************************************************************
962 * parse_idl_file
964 static void parse_idl_file( struct file *source, FILE *file )
966 char *buffer, *include;
968 input_line = 0;
970 while ((buffer = get_line( file )))
972 char quote;
973 char *p = buffer;
974 while (*p && isspace(*p)) p++;
976 if (!strncmp( p, "importlib", 9 ))
978 p += 9;
979 while (*p && isspace(*p)) p++;
980 if (*p++ != '(') continue;
981 while (*p && isspace(*p)) p++;
982 if (*p++ != '"') continue;
983 include = p;
984 while (*p && (*p != '"')) p++;
985 if (!*p) fatal_error( "malformed importlib directive\n" );
986 *p = 0;
987 add_dependency( source, include, INCL_IMPORTLIB );
988 continue;
991 if (!strncmp( p, "import", 6 ))
993 p += 6;
994 while (*p && isspace(*p)) p++;
995 if (*p != '"') continue;
996 include = ++p;
997 while (*p && (*p != '"')) p++;
998 if (!*p) fatal_error( "malformed import directive\n" );
999 *p = 0;
1000 add_dependency( source, include, INCL_IMPORT );
1001 continue;
1004 /* check for #include inside cpp_quote */
1005 if (!strncmp( p, "cpp_quote", 9 ))
1007 p += 9;
1008 while (*p && isspace(*p)) p++;
1009 if (*p++ != '(') continue;
1010 while (*p && isspace(*p)) p++;
1011 if (*p++ != '"') continue;
1012 if (*p++ != '#') continue;
1013 while (*p && isspace(*p)) p++;
1014 if (strncmp( p, "include", 7 )) continue;
1015 p += 7;
1016 while (*p && isspace(*p)) p++;
1017 if (*p == '\\' && p[1] == '"')
1019 p += 2;
1020 quote = '"';
1022 else
1024 if (*p++ != '<' ) continue;
1025 quote = '>';
1027 include = p;
1028 while (*p && (*p != quote)) p++;
1029 if (!*p || (quote == '"' && p[-1] != '\\'))
1030 fatal_error( "malformed #include directive inside cpp_quote\n" );
1031 if (quote == '"') p--; /* remove backslash */
1032 *p = 0;
1033 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1034 continue;
1037 parse_cpp_directive( source, p );
1041 /*******************************************************************
1042 * parse_c_file
1044 static void parse_c_file( struct file *source, FILE *file )
1046 char *buffer;
1048 input_line = 0;
1049 while ((buffer = get_line( file )))
1051 parse_cpp_directive( source, buffer );
1056 /*******************************************************************
1057 * parse_rc_file
1059 static void parse_rc_file( struct file *source, FILE *file )
1061 char *buffer, *include;
1063 input_line = 0;
1064 while ((buffer = get_line( file )))
1066 char quote;
1067 char *p = buffer;
1068 while (*p && isspace(*p)) p++;
1070 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1072 p += 2;
1073 while (*p && isspace(*p)) p++;
1074 if (strncmp( p, "@makedep:", 9 )) continue;
1075 p += 9;
1076 while (*p && isspace(*p)) p++;
1077 quote = '"';
1078 if (*p == quote)
1080 include = ++p;
1081 while (*p && *p != quote) p++;
1083 else
1085 include = p;
1086 while (*p && !isspace(*p) && *p != '*') p++;
1088 if (!*p)
1089 fatal_error( "malformed makedep comment\n" );
1090 *p = 0;
1091 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1092 continue;
1095 parse_cpp_directive( source, buffer );
1100 /*******************************************************************
1101 * parse_in_file
1103 static void parse_in_file( struct file *source, FILE *file )
1105 char *p, *buffer;
1107 /* make sure it gets rebuilt when the version changes */
1108 add_dependency( source, "config.h", INCL_SYSTEM );
1110 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1112 input_line = 0;
1113 while ((buffer = get_line( file )))
1115 if (strncmp( buffer, ".TH", 3 )) continue;
1116 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1117 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1118 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1119 source->args = xstrdup( p );
1120 return;
1125 /*******************************************************************
1126 * parse_sfd_file
1128 static void parse_sfd_file( struct file *source, FILE *file )
1130 char *p, *eol, *buffer;
1132 input_line = 0;
1133 while ((buffer = get_line( file )))
1135 if (strncmp( buffer, "UComments:", 10 )) continue;
1136 p = buffer + 10;
1137 while (*p == ' ') p++;
1138 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1140 p++;
1141 buffer[strlen(buffer) - 1] = 0;
1143 while ((eol = strstr( p, "+AAoA" )))
1145 *eol = 0;
1146 while (*p && isspace(*p)) p++;
1147 if (*p++ == '#')
1149 while (*p && isspace(*p)) p++;
1150 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1152 p = eol + 5;
1154 while (*p && isspace(*p)) p++;
1155 if (*p++ != '#') return;
1156 while (*p && isspace(*p)) p++;
1157 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1158 return;
1163 static const struct
1165 const char *ext;
1166 void (*parse)( struct file *file, FILE *f );
1167 } parse_functions[] =
1169 { ".c", parse_c_file },
1170 { ".h", parse_c_file },
1171 { ".inl", parse_c_file },
1172 { ".l", parse_c_file },
1173 { ".m", parse_c_file },
1174 { ".rh", parse_c_file },
1175 { ".x", parse_c_file },
1176 { ".y", parse_c_file },
1177 { ".idl", parse_idl_file },
1178 { ".rc", parse_rc_file },
1179 { ".in", parse_in_file },
1180 { ".sfd", parse_sfd_file }
1183 /*******************************************************************
1184 * load_file
1186 static struct file *load_file( const char *name )
1188 struct file *file;
1189 FILE *f;
1190 unsigned int i, hash = hash_filename( name );
1192 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1193 if (!strcmp( name, file->name )) return file;
1195 if (!(f = fopen( name, "r" ))) return NULL;
1197 file = add_file( name );
1198 input_file_name = file->name;
1199 input_line = 0;
1201 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1203 if (!strendswith( name, parse_functions[i].ext )) continue;
1204 parse_functions[i].parse( file, f );
1205 break;
1208 fclose( f );
1209 input_file_name = NULL;
1211 return file;
1215 /*******************************************************************
1216 * open_include_path_file
1218 * Open a file from a directory on the include path.
1220 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1221 const char *name, char **filename )
1223 char *src_path = base_dir_path( make, concat_paths( dir, name ));
1224 struct file *ret = load_file( src_path );
1226 if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
1227 return ret;
1231 /*******************************************************************
1232 * open_file_same_dir
1234 * Open a file in the same directory as the parent.
1236 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1238 char *src_path = replace_filename( parent->file->name, name );
1239 struct file *ret = load_file( src_path );
1241 if (ret) *filename = replace_filename( parent->filename, name );
1242 free( src_path );
1243 return ret;
1247 /*******************************************************************
1248 * open_local_file
1250 * Open a file in the source directory of the makefile.
1252 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1254 char *src_path = root_dir_path( base_dir_path( make, path ));
1255 struct file *ret = load_file( src_path );
1257 /* if not found, try parent dir */
1258 if (!ret && make->parent_dir)
1260 free( src_path );
1261 path = strmake( "%s/%s", make->parent_dir, path );
1262 src_path = root_dir_path( base_dir_path( make, path ));
1263 ret = load_file( src_path );
1266 if (ret) *filename = src_dir_path( make, path );
1267 free( src_path );
1268 return ret;
1272 /*******************************************************************
1273 * open_global_file
1275 * Open a file in the top-level source directory.
1277 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1279 char *src_path = root_dir_path( path );
1280 struct file *ret = load_file( src_path );
1282 if (ret) *filename = top_dir_path( make, path );
1283 free( src_path );
1284 return ret;
1288 /*******************************************************************
1289 * open_global_header
1291 * Open a file in the global include source directory.
1293 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1295 return open_global_file( make, strmake( "include/%s", path ), filename );
1299 /*******************************************************************
1300 * open_src_file
1302 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1304 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1306 if (!file) fatal_perror( "open %s", pFile->name );
1307 return file;
1311 /*******************************************************************
1312 * open_include_file
1314 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1316 struct file *file = NULL;
1317 char *filename;
1318 unsigned int i, len;
1320 errno = ENOENT;
1322 /* check for generated bison header */
1324 if (strendswith( pFile->name, ".tab.h" ) &&
1325 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1327 pFile->sourcename = filename;
1328 pFile->filename = obj_dir_path( make, pFile->name );
1329 return file;
1332 /* check for corresponding idl file in source dir */
1334 if (strendswith( pFile->name, ".h" ) &&
1335 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1337 pFile->sourcename = filename;
1338 pFile->filename = obj_dir_path( make, pFile->name );
1339 return file;
1342 /* check for corresponding tlb file in source dir */
1344 if (strendswith( pFile->name, ".tlb" ) &&
1345 (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1347 pFile->sourcename = filename;
1348 pFile->filename = obj_dir_path( make, pFile->name );
1349 return file;
1352 /* now try in source dir */
1353 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1355 /* check for corresponding idl file in global includes */
1357 if (strendswith( pFile->name, ".h" ) &&
1358 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1360 pFile->sourcename = filename;
1361 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1362 return file;
1365 /* check for corresponding .in file in global includes (for config.h.in) */
1367 if (strendswith( pFile->name, ".h" ) &&
1368 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1370 pFile->sourcename = filename;
1371 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1372 return file;
1375 /* check for corresponding .x file in global includes */
1377 if (strendswith( pFile->name, "tmpl.h" ) &&
1378 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1380 pFile->sourcename = filename;
1381 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1382 return file;
1385 /* check for corresponding .tlb file in global includes */
1387 if (strendswith( pFile->name, ".tlb" ) &&
1388 (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1390 pFile->sourcename = filename;
1391 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1392 return file;
1395 /* check in global includes source dir */
1397 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1399 /* check in global msvcrt includes */
1400 if (make->use_msvcrt &&
1401 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1402 return file;
1404 /* now search in include paths */
1405 for (i = 0; i < make->include_paths.count; i++)
1407 const char *dir = make->include_paths.str[i];
1408 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1410 if (prefix)
1412 len = strlen( prefix );
1413 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1415 while (dir[len] == '/') len++;
1416 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1417 if (file) return file;
1419 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1421 if (*dir != '/')
1423 if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1424 return file;
1427 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1429 /* try in src file directory */
1430 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1432 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1433 perror( pFile->name );
1434 pFile = pFile->included_by;
1435 while (pFile && pFile->included_by)
1437 const char *parent = pFile->included_by->sourcename;
1438 if (!parent) parent = pFile->included_by->file->name;
1439 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1440 parent, pFile->included_line, pFile->name );
1441 pFile = pFile->included_by;
1443 exit(1);
1447 /*******************************************************************
1448 * add_all_includes
1450 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1452 unsigned int i;
1454 parent->files_count = 0;
1455 parent->files_size = file->deps_count;
1456 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1457 for (i = 0; i < file->deps_count; i++)
1459 switch (file->deps[i].type)
1461 case INCL_NORMAL:
1462 case INCL_IMPORT:
1463 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1464 break;
1465 case INCL_IMPORTLIB:
1466 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1467 break;
1468 case INCL_SYSTEM:
1469 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1470 break;
1471 case INCL_CPP_QUOTE:
1472 case INCL_CPP_QUOTE_SYSTEM:
1473 break;
1479 /*******************************************************************
1480 * parse_file
1482 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1484 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1486 if (!file) return;
1488 source->file = file;
1489 source->files_count = 0;
1490 source->files_size = file->deps_count;
1491 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1493 if (source->sourcename)
1495 if (strendswith( source->sourcename, ".idl" ))
1497 unsigned int i;
1499 if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
1501 /* generated .h file always includes these */
1502 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1503 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1504 for (i = 0; i < file->deps_count; i++)
1506 switch (file->deps[i].type)
1508 case INCL_IMPORT:
1509 if (strendswith( file->deps[i].name, ".idl" ))
1510 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1511 file->deps[i].line, INCL_NORMAL );
1512 else
1513 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1514 break;
1515 case INCL_CPP_QUOTE:
1516 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1517 break;
1518 case INCL_CPP_QUOTE_SYSTEM:
1519 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1520 break;
1521 case INCL_NORMAL:
1522 case INCL_SYSTEM:
1523 case INCL_IMPORTLIB:
1524 break;
1527 return;
1529 if (strendswith( source->sourcename, ".y" ))
1530 return; /* generated .tab.h doesn't include anything */
1533 add_all_includes( make, source, file );
1537 /*******************************************************************
1538 * add_src_file
1540 * Add a source file to the list.
1542 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1544 struct incl_file *file;
1546 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1547 file = xmalloc( sizeof(*file) );
1548 memset( file, 0, sizeof(*file) );
1549 file->name = xstrdup(name);
1550 list_add_tail( &make->sources, &file->entry );
1551 parse_file( make, file, 1 );
1552 return file;
1556 /*******************************************************************
1557 * open_input_makefile
1559 static FILE *open_input_makefile( const struct makefile *make )
1561 FILE *ret;
1563 if (make->base_dir)
1564 input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
1565 else
1566 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1568 input_line = 0;
1569 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1570 return ret;
1574 /*******************************************************************
1575 * get_make_variable
1577 static const char *get_make_variable( const struct makefile *make, const char *name )
1579 const char *ret;
1581 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1582 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1583 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1584 return NULL;
1588 /*******************************************************************
1589 * get_expanded_make_variable
1591 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1593 const char *var;
1594 char *p, *end, *expand, *tmp;
1596 var = get_make_variable( make, name );
1597 if (!var) return NULL;
1599 p = expand = xstrdup( var );
1600 while ((p = strchr( p, '$' )))
1602 if (p[1] == '(')
1604 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1605 *end++ = 0;
1606 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1607 var = get_make_variable( make, p + 2 );
1608 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1609 /* switch to the new string */
1610 p = tmp + (p - expand);
1611 free( expand );
1612 expand = tmp;
1614 else if (p[1] == '{') /* don't expand ${} variables */
1616 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1617 p = end + 1;
1619 else if (p[1] == '$')
1621 p += 2;
1623 else fatal_error( "syntax error in '%s'\n", expand );
1626 /* consider empty variables undefined */
1627 p = expand;
1628 while (*p && isspace(*p)) p++;
1629 if (*p) return expand;
1630 free( expand );
1631 return NULL;
1635 /*******************************************************************
1636 * get_expanded_make_var_array
1638 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1640 struct strarray ret = empty_strarray;
1641 char *value, *token;
1643 if ((value = get_expanded_make_variable( make, name )))
1644 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1645 strarray_add( &ret, token );
1646 return ret;
1650 /*******************************************************************
1651 * get_expanded_file_local_var
1653 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1654 const char *name )
1656 char *p, *var = strmake( "%s_%s", file, name );
1658 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1659 return get_expanded_make_var_array( make, var );
1663 /*******************************************************************
1664 * set_make_variable
1666 static int set_make_variable( struct strarray *array, const char *assignment )
1668 char *p, *name;
1670 p = name = xstrdup( assignment );
1671 while (isalnum(*p) || *p == '_') p++;
1672 if (name == p) return 0; /* not a variable */
1673 if (isspace(*p))
1675 *p++ = 0;
1676 while (isspace(*p)) p++;
1678 if (*p != '=') return 0; /* not an assignment */
1679 *p++ = 0;
1680 while (isspace(*p)) p++;
1682 strarray_set_value( array, name, p );
1683 return 1;
1687 /*******************************************************************
1688 * parse_makefile
1690 static struct makefile *parse_makefile( const char *path )
1692 char *buffer;
1693 FILE *file;
1694 struct makefile *make = xmalloc( sizeof(*make) );
1696 memset( make, 0, sizeof(*make) );
1697 if (path)
1699 make->top_obj_dir = get_relative_path( path, "" );
1700 make->base_dir = path;
1701 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1704 file = open_input_makefile( make );
1705 while ((buffer = get_line( file )))
1707 if (!strncmp( buffer, separator, strlen(separator) )) break;
1708 if (*buffer == '\t') continue; /* command */
1709 while (isspace( *buffer )) buffer++;
1710 if (*buffer == '#') continue; /* comment */
1711 set_make_variable( &make->vars, buffer );
1713 fclose( file );
1714 input_file_name = NULL;
1715 return make;
1719 /*******************************************************************
1720 * add_generated_sources
1722 static void add_generated_sources( struct makefile *make )
1724 struct incl_file *source, *next, *file;
1726 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1728 if (source->file->flags & FLAG_IDL_CLIENT)
1730 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1731 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1732 add_all_includes( make, file, file->file );
1734 if (source->file->flags & FLAG_IDL_SERVER)
1736 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1737 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1738 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1739 add_all_includes( make, file, file->file );
1741 if (source->file->flags & FLAG_IDL_IDENT)
1743 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1744 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1745 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1746 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1747 add_all_includes( make, file, file->file );
1749 if (source->file->flags & FLAG_IDL_PROXY)
1751 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1752 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1753 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1754 add_all_includes( make, file, file->file );
1755 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1756 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1757 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1758 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1759 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1760 add_all_includes( make, file, file->file );
1762 if (source->file->flags & FLAG_IDL_TYPELIB)
1764 add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
1766 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1768 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1770 if (source->file->flags & FLAG_IDL_REGISTER)
1772 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1774 if (source->file->flags & FLAG_IDL_HEADER)
1776 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1778 if (!source->file->flags && strendswith( source->name, ".idl" ))
1780 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1782 if (strendswith( source->name, ".x" ))
1784 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1786 if (strendswith( source->name, ".y" ))
1788 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1789 /* steal the includes list from the source file */
1790 file->files_count = source->files_count;
1791 file->files_size = source->files_size;
1792 file->files = source->files;
1793 source->files_count = source->files_size = 0;
1794 source->files = NULL;
1796 if (strendswith( source->name, ".l" ))
1798 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1799 /* steal the includes list from the source file */
1800 file->files_count = source->files_count;
1801 file->files_size = source->files_size;
1802 file->files = source->files;
1803 source->files_count = source->files_size = 0;
1804 source->files = NULL;
1806 if (source->file->flags & FLAG_C_IMPLIB)
1808 if (!make->staticimplib && make->importlib && *dll_ext)
1809 make->staticimplib = strmake( "lib%s.def.a", make->importlib );
1812 if (make->testdll)
1814 file = add_generated_source( make, "testlist.o", "testlist.c" );
1815 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1816 add_all_includes( make, file, file->file );
1821 /*******************************************************************
1822 * create_dir
1824 static void create_dir( const char *dir )
1826 char *p, *path;
1828 p = path = xstrdup( dir );
1829 while ((p = strchr( p, '/' )))
1831 *p = 0;
1832 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1833 *p++ = '/';
1834 while (*p == '/') p++;
1836 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1837 free( path );
1841 /*******************************************************************
1842 * output_filenames_obj_dir
1844 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1846 unsigned int i;
1848 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1852 /*******************************************************************
1853 * get_dependencies
1855 static void get_dependencies( struct strarray *deps, struct incl_file *file, struct incl_file *source )
1857 unsigned int i;
1859 if (!file->filename) return;
1861 if (file != source)
1863 if (file->owner == source) return; /* already processed */
1864 if (file->type == INCL_IMPORTLIB &&
1865 !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1866 return; /* library is imported only when building a typelib */
1867 file->owner = source;
1868 strarray_add( deps, file->filename );
1870 for (i = 0; i < file->files_count; i++) get_dependencies( deps, file->files[i], source );
1874 /*******************************************************************
1875 * get_local_dependencies
1877 * Get the local dependencies of a given target.
1879 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1880 struct strarray targets )
1882 unsigned int i;
1883 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
1885 for (i = 0; i < deps.count; i++)
1887 if (strarray_exists( &targets, deps.str[i] ))
1888 deps.str[i] = obj_dir_path( make, deps.str[i] );
1889 else
1890 deps.str[i] = src_dir_path( make, deps.str[i] );
1892 return deps;
1896 /*******************************************************************
1897 * has_static_lib
1899 * Check if makefile builds the named static library.
1901 static int has_static_lib( const struct makefile *make, const char *name )
1903 if (!make->staticlib) return 0;
1904 if (strncmp( make->staticlib, "lib", 3 )) return 0;
1905 if (strncmp( make->staticlib + 3, name, strlen(name) )) return 0;
1906 return !strcmp( make->staticlib + 3 + strlen(name), ".a" );
1910 /*******************************************************************
1911 * add_default_libraries
1913 static struct strarray add_default_libraries( const struct makefile *make, struct strarray *deps )
1915 struct strarray ret = empty_strarray;
1916 struct strarray all_libs = empty_strarray;
1917 unsigned int i, j;
1919 strarray_add( &all_libs, "-lwine_port" );
1920 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
1921 strarray_addall( &all_libs, libs );
1923 for (i = 0; i < all_libs.count; i++)
1925 int found = 0;
1926 if (!strncmp( all_libs.str[i], "-l", 2 ))
1928 const char *name = all_libs.str[i] + 2;
1930 for (j = 0; j < top_makefile->subdirs.count; j++)
1932 const struct makefile *submake = top_makefile->submakes[j];
1934 if ((found = has_static_lib( submake, name )))
1936 const char *lib = strmake( "%s/lib%s.a",
1937 top_obj_dir_path( make, submake->base_dir ), name );
1938 strarray_add( deps, lib );
1939 strarray_add( &ret, lib );
1940 break;
1944 if (!found) strarray_add( &ret, all_libs.str[i] );
1946 return ret;
1950 /*******************************************************************
1951 * add_import_libs
1953 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
1954 struct strarray imports, int cross )
1956 struct strarray ret = empty_strarray;
1957 unsigned int i, j;
1959 for (i = 0; i < imports.count; i++)
1961 const char *name = imports.str[i];
1963 for (j = 0; j < top_makefile->subdirs.count; j++)
1965 const struct makefile *submake = top_makefile->submakes[j];
1967 if (submake->importlib && !strcmp( submake->importlib, name ))
1969 const char *dir = top_obj_dir_path( make, submake->base_dir );
1970 const char *ext = cross ? "cross.a" : *dll_ext ? "def" : "a";
1972 strarray_add( deps, strmake( "%s/lib%s.%s", dir, name, ext ));
1973 if (!cross && submake->staticimplib)
1974 strarray_add( deps, strmake( "%s/%s", dir, submake->staticimplib ));
1975 break;
1978 if (has_static_lib( submake, name ))
1980 const char *dir = top_obj_dir_path( make, submake->base_dir );
1982 strarray_add( deps, strmake( "%s/lib%s.a", dir, name ));
1983 break;
1986 strarray_add( &ret, strmake( "-l%s", name ));
1988 return ret;
1992 /*******************************************************************
1993 * get_default_imports
1995 static struct strarray get_default_imports( const struct makefile *make )
1997 struct strarray ret = empty_strarray;
1999 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
2000 if (strarray_exists( &make->appmode, "-mno-cygwin" )) strarray_add( &ret, "msvcrt" );
2001 if (make->is_win16) strarray_add( &ret, "kernel" );
2002 strarray_add( &ret, "kernel32" );
2003 strarray_add( &ret, "ntdll" );
2004 strarray_add( &ret, "winecrt0" );
2005 return ret;
2009 /*******************************************************************
2010 * add_install_rule
2012 static void add_install_rule( const struct makefile *make, struct strarray *install_rules,
2013 const char *target, const char *file, const char *dest )
2015 if (strarray_exists( &make->install_lib, target ))
2017 strarray_add( &install_rules[INSTALL_LIB], file );
2018 strarray_add( &install_rules[INSTALL_LIB], dest );
2020 else if (strarray_exists( &make->install_dev, target ))
2022 strarray_add( &install_rules[INSTALL_DEV], file );
2023 strarray_add( &install_rules[INSTALL_DEV], dest );
2028 /*******************************************************************
2029 * get_include_install_path
2031 * Determine the installation path for a given include file.
2033 static const char *get_include_install_path( const char *name )
2035 if (!strncmp( name, "wine/", 5 )) return name + 5;
2036 if (!strncmp( name, "msvcrt/", 7 )) return name;
2037 return strmake( "windows/%s", name );
2041 /*******************************************************************
2042 * get_shared_library_name
2044 * Determine possible names for a shared library with a version number.
2046 static struct strarray get_shared_lib_names( const char *libname )
2048 struct strarray ret = empty_strarray;
2049 const char *ext, *p;
2050 char *name, *first, *second;
2051 size_t len = 0;
2053 strarray_add( &ret, libname );
2055 for (p = libname; (p = strchr( p, '.' )); p++)
2056 if ((len = strspn( p + 1, "0123456789." ))) break;
2058 if (!len) return ret;
2059 ext = p + 1 + len;
2060 if (*ext && ext[-1] == '.') ext--;
2062 /* keep only the first group of digits */
2063 name = xstrdup( libname );
2064 first = name + (p - libname);
2065 if ((second = strchr( first + 1, '.' )))
2067 strcpy( second, ext );
2068 strarray_add( &ret, xstrdup( name ));
2070 /* now remove all digits */
2071 strcpy( first, ext );
2072 strarray_add( &ret, name );
2073 return ret;
2077 /*******************************************************************
2078 * output_install_rules
2080 * Rules are stored as a (file,dest) pair of values.
2081 * The first char of dest indicates the type of install.
2083 static struct strarray output_install_rules( const struct makefile *make, struct strarray files,
2084 const char *target, struct strarray *phony_targets )
2086 unsigned int i;
2087 char *install_sh;
2088 struct strarray uninstall = empty_strarray;
2089 struct strarray targets = empty_strarray;
2091 if (!files.count) return uninstall;
2093 for (i = 0; i < files.count; i += 2)
2094 if (strchr( "dps", files.str[i + 1][0] )) /* only for files copied from object dir */
2095 strarray_add_uniq( &targets, files.str[i] );
2097 output( "install %s::", target );
2098 output_filenames_obj_dir( make, targets );
2099 output( "\n" );
2101 install_sh = top_dir_path( make, "tools/install-sh" );
2102 for (i = 0; i < files.count; i += 2)
2104 const char *file = files.str[i];
2105 const char *dest = files.str[i + 1];
2107 switch (*dest)
2109 case 'd': /* data file */
2110 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s $(DESTDIR)%s\n",
2111 install_sh, obj_dir_path( make, file ), dest + 1 );
2112 break;
2113 case 'D': /* data file in source dir */
2114 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s $(DESTDIR)%s\n",
2115 install_sh, src_dir_path( make, file ), dest + 1 );
2116 break;
2117 case 'p': /* program file */
2118 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s $(DESTDIR)%s\n",
2119 install_sh, obj_dir_path( make, file ), dest + 1 );
2120 break;
2121 case 's': /* script */
2122 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s $(DESTDIR)%s\n",
2123 install_sh, obj_dir_path( make, file ), dest + 1 );
2124 break;
2125 case 'S': /* script in source dir */
2126 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s $(DESTDIR)%s\n",
2127 install_sh, src_dir_path( make, file ), dest + 1 );
2128 break;
2129 case 'y': /* symlink */
2130 output( "\trm -f $(DESTDIR)%s && $(LN_S) %s $(DESTDIR)%s\n", dest + 1, file, dest + 1 );
2131 break;
2132 default:
2133 assert(0);
2137 for (i = 0; i < files.count; i += 2)
2138 strarray_add( &uninstall, strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 ));
2140 strarray_add_uniq( phony_targets, "install" );
2141 strarray_add_uniq( phony_targets, target );
2142 return uninstall;
2146 /*******************************************************************
2147 * output_po_files
2149 static void output_po_files( const struct makefile *make )
2151 const char *po_dir = src_dir_path( make, "po" );
2152 struct strarray pot_files = empty_strarray;
2153 struct incl_file *source;
2154 unsigned int i;
2156 for (i = 0; i < make->subdirs.count; i++)
2158 struct makefile *submake = make->submakes[i];
2160 LIST_FOR_EACH_ENTRY( source, &submake->sources, struct incl_file, entry )
2162 if (strendswith( source->name, ".rc" ) && (source->file->flags & FLAG_RC_PO))
2164 char *pot_file = replace_extension( source->name, ".rc", ".pot" );
2165 char *pot_path = base_dir_path( submake, pot_file );
2166 output( "%s: tools/wrc include dummy\n", pot_path );
2167 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2168 strarray_add( &pot_files, pot_path );
2170 else if (strendswith( source->name, ".mc" ))
2172 char *pot_file = replace_extension( source->name, ".mc", ".pot" );
2173 char *pot_path = base_dir_path( submake, pot_file );
2174 output( "%s: tools/wmc include dummy\n", pot_path );
2175 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2176 strarray_add( &pot_files, pot_path );
2180 if (linguas.count)
2182 for (i = 0; i < linguas.count; i++)
2183 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2184 output( ": %s/wine.pot\n", po_dir );
2185 output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2186 po_dir );
2188 output( "%s/wine.pot:", po_dir );
2189 output_filenames( pot_files );
2190 output( "\n" );
2191 output( "\tmsgcat -o $@" );
2192 output_filenames( pot_files );
2193 output( "\n" );
2197 /*******************************************************************
2198 * output_sources
2200 static struct strarray output_sources( const struct makefile *make )
2202 struct incl_file *source;
2203 unsigned int i, j;
2204 struct strarray object_files = empty_strarray;
2205 struct strarray crossobj_files = empty_strarray;
2206 struct strarray res_files = empty_strarray;
2207 struct strarray clean_files = empty_strarray;
2208 struct strarray uninstall_files = empty_strarray;
2209 struct strarray mo_files = empty_strarray;
2210 struct strarray ok_files = empty_strarray;
2211 struct strarray in_files = empty_strarray;
2212 struct strarray dlldata_files = empty_strarray;
2213 struct strarray c2man_files = empty_strarray;
2214 struct strarray implib_objs = empty_strarray;
2215 struct strarray includes = empty_strarray;
2216 struct strarray subdirs = empty_strarray;
2217 struct strarray phony_targets = empty_strarray;
2218 struct strarray all_targets = empty_strarray;
2219 struct strarray install_rules[NB_INSTALL_RULES];
2220 char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
2221 char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
2223 for (i = 0; i < NB_INSTALL_RULES; i++) install_rules[i] = empty_strarray;
2225 for (i = 0; i < linguas.count; i++)
2226 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
2228 strarray_add( &phony_targets, "all" );
2229 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
2230 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
2231 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
2232 strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
2233 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
2234 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
2235 for (i = 0; i < make->include_paths.count; i++)
2236 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
2238 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
2240 struct strarray dependencies = empty_strarray;
2241 struct strarray extradefs;
2242 char *obj = xstrdup( source->name );
2243 char *ext = get_extension( obj );
2245 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
2246 *ext++ = 0;
2248 if (make->src_dir && strchr( obj, '/' ))
2250 char *subdir = base_dir_path( make, obj );
2251 *strrchr( subdir, '/' ) = 0;
2252 strarray_add_uniq( &subdirs, subdir );
2255 extradefs = get_expanded_file_local_var( make, obj, "EXTRADEFS" );
2256 get_dependencies( &dependencies, source, source );
2258 if (!strcmp( ext, "y" )) /* yacc file */
2260 /* add source file dependency for parallel makes */
2261 char *header = strmake( "%s.tab.h", obj );
2263 if (find_include_file( make, header ))
2265 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2266 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
2267 obj, obj_dir_path( make, obj ), source->filename );
2268 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2269 source->filename, obj_dir_path( make, header ));
2270 strarray_add( &clean_files, header );
2272 else output( "%s.tab.c: %s\n", obj, source->filename );
2274 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
2276 else if (!strcmp( ext, "x" )) /* template file */
2278 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2279 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2280 output( "\t%s%s -H -o $@ %s\n",
2281 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2282 if (source->file->flags & FLAG_INSTALL)
2284 strarray_add( &install_rules[INSTALL_DEV], source->name );
2285 strarray_add( &install_rules[INSTALL_DEV],
2286 strmake( "D$(includedir)/%s", get_include_install_path( source->name ) ));
2287 strarray_add( &install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2288 strarray_add( &install_rules[INSTALL_DEV],
2289 strmake( "d$(includedir)/%s.h", get_include_install_path( obj ) ));
2292 else if (!strcmp( ext, "l" )) /* lex file */
2294 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2295 output( "\t$(FLEX) -o$@ %s\n", source->filename );
2297 else if (!strcmp( ext, "rc" )) /* resource file */
2299 strarray_add( &res_files, strmake( "%s.res", obj ));
2300 output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
2301 output( "\t%s -o $@", tools_path( make, "wrc" ) );
2302 if (make->is_win16) output_filename( "-m16" );
2303 else output_filenames( target_flags );
2304 output_filename( "--nostdinc" );
2305 output_filenames( includes );
2306 output_filenames( make->define_args );
2307 output_filenames( extradefs );
2308 if (mo_files.count && (source->file->flags & FLAG_RC_PO))
2310 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
2311 output_filename( source->filename );
2312 output( "\n" );
2313 output( "%s.res:", obj_dir_path( make, obj ));
2314 output_filenames( mo_files );
2315 output( "\n" );
2317 else
2319 output_filename( source->filename );
2320 output( "\n" );
2322 if (source->file->flags & FLAG_RC_PO)
2324 strarray_add( &clean_files, strmake( "%s.pot", obj ));
2325 output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
2326 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ) );
2327 if (make->is_win16) output_filename( "-m16" );
2328 else output_filenames( target_flags );
2329 output_filename( "--nostdinc" );
2330 output_filenames( includes );
2331 output_filenames( make->define_args );
2332 output_filenames( extradefs );
2333 output_filename( source->filename );
2334 output( "\n" );
2335 output( "%s.pot ", obj_dir_path( make, obj ));
2337 output( "%s.res:", obj_dir_path( make, obj ));
2338 output_filename( tools_path( make, "wrc" ));
2339 output_filenames( dependencies );
2340 output( "\n" );
2342 else if (!strcmp( ext, "mc" )) /* message file */
2344 strarray_add( &res_files, strmake( "%s.res", obj ));
2345 strarray_add( &clean_files, strmake( "%s.pot", obj ));
2346 output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
2347 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
2348 if (mo_files.count)
2350 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
2351 output( "\n" );
2352 output( "%s.res:", obj_dir_path( make, obj ));
2353 output_filenames( mo_files );
2355 output( "\n" );
2356 output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
2357 output( "\t%s -O pot -o $@ %s", tools_path( make, "wmc" ), source->filename );
2358 output( "\n" );
2359 output( "%s.pot %s.res:", obj_dir_path( make, obj ), obj_dir_path( make, obj ));
2360 output_filename( tools_path( make, "wmc" ));
2361 output_filenames( dependencies );
2362 output( "\n" );
2364 else if (!strcmp( ext, "idl" )) /* IDL file */
2366 struct strarray targets = empty_strarray;
2367 char *dest;
2369 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2370 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2372 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2374 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2375 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2376 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
2377 strarray_add( &targets, dest );
2379 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
2380 if (source->file->flags & FLAG_INSTALL)
2382 strarray_add( &install_rules[INSTALL_DEV], xstrdup( source->name ));
2383 strarray_add( &install_rules[INSTALL_DEV],
2384 strmake( "D$(includedir)/%s.idl", get_include_install_path( obj ) ));
2385 if (source->file->flags & FLAG_IDL_HEADER)
2387 strarray_add( &install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2388 strarray_add( &install_rules[INSTALL_DEV],
2389 strmake( "d$(includedir)/%s.h", get_include_install_path( obj ) ));
2392 if (!targets.count) continue;
2393 output_filenames_obj_dir( make, targets );
2394 output( ": %s\n", tools_path( make, "widl" ));
2395 output( "\t%s -o $@", tools_path( make, "widl" ) );
2396 output_filenames( target_flags );
2397 output_filenames( includes );
2398 output_filenames( make->define_args );
2399 output_filenames( extradefs );
2400 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2401 output_filename( source->filename );
2402 output( "\n" );
2403 output_filenames_obj_dir( make, targets );
2404 output( ": %s", source->filename );
2405 output_filenames( dependencies );
2406 output( "\n" );
2408 else if (!strcmp( ext, "in" )) /* .in file or man page */
2410 if (strendswith( obj, ".man" ) && source->file->args)
2412 struct strarray symlinks;
2413 char *dir, *dest = replace_extension( obj, ".man", "" );
2414 char *lang = strchr( dest, '.' );
2415 char *section = source->file->args;
2416 if (lang)
2418 *lang++ = 0;
2419 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2421 else dir = strmake( "$(mandir)/man%s", section );
2422 add_install_rule( make, install_rules, dest, xstrdup(obj),
2423 strmake( "d%s/%s.%s", dir, dest, section ));
2424 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2425 for (i = 0; i < symlinks.count; i++)
2426 add_install_rule( make, install_rules, symlinks.str[i],
2427 strmake( "%s.%s", dest, section ),
2428 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2429 free( dest );
2430 free( dir );
2432 strarray_add( &in_files, xstrdup(obj) );
2433 strarray_add( &all_targets, xstrdup(obj) );
2434 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2435 output( "\t$(SED_CMD) %s >$@ || (rm -f $@ && false)\n", source->filename );
2436 output( "%s:", obj_dir_path( make, obj ));
2437 output_filenames( dependencies );
2438 output( "\n" );
2439 add_install_rule( make, install_rules, obj, xstrdup( obj ),
2440 strmake( "d$(datadir)/wine/%s", obj ));
2442 else if (!strcmp( ext, "sfd" )) /* font file */
2444 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
2445 if (fontforge && !make->src_dir)
2447 output( "%s: %s\n", ttf_file, source->filename );
2448 output( "\t%s -script %s %s $@\n",
2449 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
2450 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
2452 if (source->file->flags & FLAG_INSTALL)
2454 strarray_add( &install_rules[INSTALL_LIB], strmake( "%s.ttf", obj ));
2455 strarray_add( &install_rules[INSTALL_LIB], strmake( "D$(fontdir)/%s.ttf", obj ));
2457 if (source->file->flags & FLAG_SFD_FONTS)
2459 struct strarray *array = source->file->args;
2461 for (i = 0; i < array->count; i++)
2463 char *font = strtok( xstrdup(array->str[i]), " \t" );
2464 char *args = strtok( NULL, "" );
2466 strarray_add( &all_targets, xstrdup( font ));
2467 output( "%s: %s %s\n", obj_dir_path( make, font ),
2468 tools_path( make, "sfnt2fon" ), ttf_file );
2469 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2470 strarray_add( &install_rules[INSTALL_LIB], xstrdup(font) );
2471 strarray_add( &install_rules[INSTALL_LIB], strmake( "d$(fontdir)/%s", font ));
2475 else if (!strcmp( ext, "svg" )) /* svg file */
2477 if (convert && rsvg && icotool && !make->src_dir)
2479 output( "%s.ico %s.bmp: %s\n",
2480 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
2481 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2482 top_dir_path( make, "tools/buildimage" ), source->filename );
2485 else if (!strcmp( ext, "res" ))
2487 strarray_add( &res_files, source->name );
2489 else if (!strcmp( ext, "tlb" ))
2491 strarray_add( &all_targets, source->name );
2493 else if (!strcmp( ext, "h" ) || !strcmp( ext, "rh" ) || !strcmp( ext, "inl" )) /* header file */
2495 if (source->file->flags & FLAG_GENERATED)
2497 strarray_add( &all_targets, source->name );
2499 else
2501 strarray_add( &install_rules[INSTALL_DEV], source->name );
2502 strarray_add( &install_rules[INSTALL_DEV],
2503 strmake( "D$(includedir)/%s", get_include_install_path( source->name ) ));
2506 else
2508 int need_cross = make->testdll ||
2509 (source->file->flags & FLAG_C_IMPLIB) ||
2510 (make->module && make->staticlib);
2512 if ((source->file->flags & FLAG_GENERATED) &&
2513 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2514 strarray_add( &clean_files, source->filename );
2515 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
2516 strarray_add( &object_files, strmake( "%s.o", obj ));
2517 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2518 output( "\t$(CC) -c -o $@ %s", source->filename );
2519 output_filenames( includes );
2520 output_filenames( make->define_args );
2521 output_filenames( extradefs );
2522 if (make->module || make->staticlib || make->sharedlib || make->testdll)
2524 output_filenames( dll_flags );
2525 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2527 output_filenames( extra_cflags );
2528 output_filenames( cpp_flags );
2529 output_filename( "$(CFLAGS)" );
2530 output( "\n" );
2531 if (crosstarget && need_cross)
2533 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
2534 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2535 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2536 output_filenames( includes );
2537 output_filenames( make->define_args );
2538 output_filenames( extradefs );
2539 output_filename( "-DWINE_CROSSTEST" );
2540 output_filenames( cpp_flags );
2541 output_filename( "$(CFLAGS)" );
2542 output( "\n" );
2544 if (make->testdll && !strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2546 strarray_add( &ok_files, strmake( "%s.ok", obj ));
2547 output( "%s.ok:\n", obj_dir_path( make, obj ));
2548 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2549 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
2550 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
2552 if (!strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2553 strarray_add( &c2man_files, source->filename );
2554 output( "%s.o", obj_dir_path( make, obj ));
2555 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2556 output( ":" );
2557 output_filenames( dependencies );
2558 output( "\n" );
2560 free( obj );
2563 /* rules for files that depend on multiple sources */
2565 if (dlldata_files.count)
2567 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
2568 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
2569 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
2570 output_filenames( dlldata_files );
2571 output( "\n" );
2574 if (make->module && !make->staticlib)
2576 struct strarray all_libs = empty_strarray;
2577 struct strarray dep_libs = empty_strarray;
2578 char *module_path = obj_dir_path( make, make->module );
2579 char *spec_file = NULL;
2581 if (!make->appmode.count)
2582 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2583 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 0 ));
2584 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0 ));
2585 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
2586 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
2588 if (*dll_ext)
2590 for (i = 0; i < make->delayimports.count; i++)
2591 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2592 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
2593 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
2594 add_install_rule( make, install_rules, make->module, strmake( "%s%s", make->module, dll_ext ),
2595 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
2596 add_install_rule( make, install_rules, make->module, strmake( "%s.fake", make->module ),
2597 strmake( "d$(fakedlldir)/%s", make->module ));
2598 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2600 else
2602 strarray_add( &all_libs, "-lwine" );
2603 strarray_add( &all_targets, make->module );
2604 add_install_rule( make, install_rules, make->module, make->module,
2605 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
2606 output( "%s:", module_path );
2608 if (spec_file) output_filename( spec_file );
2609 output_filenames_obj_dir( make, object_files );
2610 output_filenames_obj_dir( make, res_files );
2611 output_filenames( dep_libs );
2612 output( "\n" );
2613 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2614 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2615 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2616 output_filenames( target_flags );
2617 output_filenames( unwind_flags );
2618 if (spec_file)
2620 output( " -shared %s", spec_file );
2621 output_filenames( make->extradllflags );
2623 else output_filenames( make->appmode );
2624 output_filenames_obj_dir( make, object_files );
2625 output_filenames_obj_dir( make, res_files );
2626 output_filenames( all_libs );
2627 output_filename( "$(LDFLAGS)" );
2628 output( "\n" );
2630 if (spec_file && make->importlib)
2632 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2633 if (*dll_ext)
2635 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
2636 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2637 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2638 output_filenames( target_flags );
2639 if (make->is_win16) output_filename( "-m16" );
2640 output( "\n" );
2641 add_install_rule( make, install_rules, make->importlib,
2642 strmake( "lib%s.def", make->importlib ),
2643 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
2644 if (implib_objs.count)
2646 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
2647 output( "%s.def.a:", importlib_path );
2648 output_filenames_obj_dir( make, implib_objs );
2649 output( "\n" );
2650 output( "\trm -f $@\n" );
2651 output( "\t$(AR) $(ARFLAGS) $@" );
2652 output_filenames_obj_dir( make, implib_objs );
2653 output( "\n" );
2654 output( "\t$(RANLIB) $@\n" );
2655 add_install_rule( make, install_rules, make->importlib,
2656 strmake( "lib%s.def.a", make->importlib ),
2657 strmake( "d$(dlldir)/lib%s.def.a", make->importlib ));
2660 else
2662 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
2663 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2664 output_filenames_obj_dir( make, implib_objs );
2665 output( "\n" );
2666 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2667 output_filenames( target_flags );
2668 output_filenames_obj_dir( make, implib_objs );
2669 output( "\n" );
2670 add_install_rule( make, install_rules, make->importlib,
2671 strmake( "lib%s.a", make->importlib ),
2672 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
2674 if (crosstarget && !make->is_win16)
2676 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2677 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2678 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2679 output_filenames_obj_dir( make, cross_files );
2680 output( "\n" );
2681 output( "\t%s -b %s -w --implib -o $@ --export %s",
2682 tools_path( make, "winebuild" ), crosstarget, spec_file );
2683 output_filenames_obj_dir( make, cross_files );
2684 output( "\n" );
2688 if (spec_file)
2690 if (c2man_files.count)
2692 output( "manpages::\n" );
2693 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2694 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2695 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2696 output_filename( strmake( "-o %s/man%s",
2697 top_obj_dir_path( make, "documentation" ), man_ext ));
2698 output_filenames( c2man_files );
2699 output( "\n" );
2700 output( "htmlpages::\n" );
2701 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2702 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2703 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2704 output_filename( strmake( "-o %s",
2705 top_obj_dir_path( make, "documentation/html" )));
2706 output_filenames( c2man_files );
2707 output( "\n" );
2708 output( "sgmlpages::\n" );
2709 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2710 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2711 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2712 output_filename( strmake( "-o %s",
2713 top_obj_dir_path( make, "documentation/api-guide" )));
2714 output_filenames( c2man_files );
2715 output( "\n" );
2716 output( "xmlpages::\n" );
2717 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2718 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2719 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2720 output_filename( strmake( "-o %s",
2721 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2722 output_filenames( c2man_files );
2723 output( "\n" );
2724 strarray_add( &phony_targets, "manpages" );
2725 strarray_add( &phony_targets, "htmlpages" );
2726 strarray_add( &phony_targets, "sgmlpages" );
2727 strarray_add( &phony_targets, "xmlpages" );
2729 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2731 else if (*dll_ext)
2733 char *binary = replace_extension( make->module, ".exe", "" );
2734 add_install_rule( make, install_rules, binary, tools_dir_path( make, "wineapploader" ),
2735 strmake( "s$(bindir)/%s", binary ));
2739 if (make->staticlib)
2741 strarray_add( &all_targets, make->staticlib );
2742 output( "%s:", obj_dir_path( make, make->staticlib ));
2743 output_filenames_obj_dir( make, object_files );
2744 output( "\n\trm -f $@\n" );
2745 output( "\t$(AR) $(ARFLAGS) $@" );
2746 output_filenames_obj_dir( make, object_files );
2747 output( "\n\t$(RANLIB) $@\n" );
2748 if (crosstarget && make->module)
2750 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2752 strarray_add( &all_targets, name );
2753 output( "%s:", obj_dir_path( make, name ));
2754 output_filenames_obj_dir( make, crossobj_files );
2755 output( "\n\trm -f $@\n" );
2756 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2757 output_filenames_obj_dir( make, crossobj_files );
2758 output( "\n\t%s-ranlib $@\n", crosstarget );
2762 if (make->sharedlib)
2764 char *basename, *p;
2765 struct strarray names = get_shared_lib_names( make->sharedlib );
2766 struct strarray all_libs = empty_strarray;
2767 struct strarray dep_libs = empty_strarray;
2769 basename = xstrdup( make->sharedlib );
2770 if ((p = strchr( basename, '.' ))) *p = 0;
2772 strarray_addall( &dep_libs, get_local_dependencies( make, basename, in_files ));
2773 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
2774 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
2776 output( "%s:", obj_dir_path( make, make->sharedlib ));
2777 output_filenames_obj_dir( make, object_files );
2778 output_filenames( dep_libs );
2779 output( "\n" );
2780 output( "\t$(CC) -o $@" );
2781 output_filenames_obj_dir( make, object_files );
2782 output_filenames( all_libs );
2783 output_filename( "$(LDFLAGS)" );
2784 output( "\n" );
2785 add_install_rule( make, install_rules, make->sharedlib, make->sharedlib,
2786 strmake( "p$(libdir)/%s", make->sharedlib ));
2787 for (i = 1; i < names.count; i++)
2789 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
2790 output( "\trm -f $@ && $(LN_S) %s $@\n", names.str[i-1] );
2791 add_install_rule( make, install_rules, names.str[i], names.str[i-1],
2792 strmake( "y$(libdir)/%s", names.str[i] ));
2794 strarray_addall( &all_targets, names );
2797 if (make->importlib && !make->module) /* stand-alone import lib (for libwine) */
2799 char *def_file = replace_extension( make->importlib, ".a", ".def" );
2801 if (!strncmp( def_file, "lib", 3 )) def_file += 3;
2802 output( "%s: %s\n", obj_dir_path( make, make->importlib ), src_dir_path( make, def_file ));
2803 output( "\t%s -l $@ -d %s\n", dlltool, src_dir_path( make, def_file ));
2804 add_install_rule( make, install_rules, make->importlib, make->importlib,
2805 strmake( "d$(libdir)/%s", make->importlib ));
2806 strarray_add( &all_targets, make->importlib );
2809 if (make->testdll)
2811 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2812 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2813 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2814 struct strarray dep_libs = empty_strarray;
2815 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0 );
2817 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
2818 strarray_addall( &all_libs, libs );
2819 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2820 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2821 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2822 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2823 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2824 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2825 output_filenames( target_flags );
2826 output_filenames( unwind_flags );
2827 output_filenames( make->appmode );
2828 output_filenames_obj_dir( make, object_files );
2829 output_filenames_obj_dir( make, res_files );
2830 output_filenames( all_libs );
2831 output_filename( "$(LDFLAGS)" );
2832 output( "\n" );
2833 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2834 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2835 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2836 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2837 output_filenames( target_flags );
2838 output_filenames( unwind_flags );
2839 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2840 output_filenames( make->appmode );
2841 output_filenames_obj_dir( make, object_files );
2842 output_filenames_obj_dir( make, res_files );
2843 output_filenames( all_libs );
2844 output_filename( "$(LDFLAGS)" );
2845 output( "\n" );
2846 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2847 obj_dir_path( make, stripped ), dll_ext );
2848 output_filenames_obj_dir( make, object_files );
2849 output_filenames_obj_dir( make, res_files );
2850 output_filenames( dep_libs );
2851 output( "\n" );
2853 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2854 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2855 obj_dir_path( make, stripped ), dll_ext );
2856 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2857 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2859 if (crosstarget)
2861 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2863 dep_libs = empty_strarray;
2864 all_libs = add_import_libs( make, &dep_libs, make->imports, 1 );
2865 add_import_libs( make, &dep_libs, get_default_imports( make ), 1 ); /* dependencies only */
2866 strarray_addall( &all_libs, libs );
2867 strarray_add( &clean_files, crosstest );
2868 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2869 output( "%s:", obj_dir_path( make, crosstest ));
2870 output_filenames_obj_dir( make, crossobj_files );
2871 output_filenames_obj_dir( make, res_files );
2872 output_filenames( dep_libs );
2873 output( "\n" );
2874 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2875 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2876 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2877 output_filename( "--lib-suffix=.cross.a" );
2878 output_filenames_obj_dir( make, crossobj_files );
2879 output_filenames_obj_dir( make, res_files );
2880 output_filenames( all_libs );
2881 output_filename( "$(LDFLAGS)" );
2882 output( "\n" );
2883 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2884 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2887 output_filenames_obj_dir( make, ok_files );
2888 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2889 output( "check test:" );
2890 output_filenames_obj_dir( make, ok_files );
2891 output( "\n" );
2892 output( "testclean::\n" );
2893 output( "\trm -f" );
2894 output_filenames_obj_dir( make, ok_files );
2895 output( "\n" );
2896 strarray_addall( &clean_files, ok_files );
2897 strarray_add( &phony_targets, "check" );
2898 strarray_add( &phony_targets, "test" );
2899 strarray_add( &phony_targets, "testclean" );
2902 for (i = 0; i < make->programs.count; i++)
2904 char *program_installed = NULL;
2905 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
2906 struct strarray deps = get_local_dependencies( make, make->programs.str[i], in_files );
2907 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
2908 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
2909 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
2911 if (!objs.count) objs = object_files;
2912 strarray_addall( &all_libs, add_default_libraries( make, &deps ));
2914 output( "%s:", obj_dir_path( make, program ) );
2915 output_filenames_obj_dir( make, objs );
2916 output_filenames( deps );
2917 output( "\n" );
2918 output( "\t$(CC) -o $@" );
2919 output_filenames_obj_dir( make, objs );
2921 if (strarray_exists( &all_libs, "-lwine" ))
2923 strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
2924 if (ldrpath_local && ldrpath_install)
2926 program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
2927 output_filename( ldrpath_local );
2928 output_filenames( all_libs );
2929 output_filename( "$(LDFLAGS)" );
2930 output( "\n" );
2931 output( "%s:", obj_dir_path( make, program_installed ) );
2932 output_filenames_obj_dir( make, objs );
2933 output_filenames( deps );
2934 output( "\n" );
2935 output( "\t$(CC) -o $@" );
2936 output_filenames_obj_dir( make, objs );
2937 output_filename( ldrpath_install );
2938 strarray_add( &all_targets, program_installed );
2942 output_filenames( all_libs );
2943 output_filename( "$(LDFLAGS)" );
2944 output( "\n" );
2945 strarray_add( &all_targets, program );
2947 if (symlinks.count)
2949 output_filenames_obj_dir( make, symlinks );
2950 output( ": %s\n", obj_dir_path( make, program ));
2951 output( "\trm -f $@ && $(LN_S) %s $@\n", obj_dir_path( make, program ));
2952 strarray_addall( &all_targets, symlinks );
2955 add_install_rule( make, install_rules, program, program_installed ? program_installed : program,
2956 strmake( "p$(bindir)/%s", program ));
2957 for (j = 0; j < symlinks.count; j++)
2958 add_install_rule( make, install_rules, symlinks.str[j], program,
2959 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
2962 for (i = 0; i < make->scripts.count; i++)
2963 add_install_rule( make, install_rules, make->scripts.str[i], make->scripts.str[i],
2964 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
2966 if (all_targets.count)
2968 output( "all:" );
2969 output_filenames_obj_dir( make, all_targets );
2970 output( "\n" );
2973 strarray_addall( &uninstall_files, output_install_rules( make, install_rules[INSTALL_LIB],
2974 "install-lib", &phony_targets ));
2975 strarray_addall( &uninstall_files, output_install_rules( make, install_rules[INSTALL_DEV],
2976 "install-dev", &phony_targets ));
2977 if (uninstall_files.count)
2979 output( "uninstall::\n" );
2980 output( "\trm -f" );
2981 output_filenames( uninstall_files );
2982 output( "\n" );
2983 strarray_add_uniq( &phony_targets, "uninstall" );
2986 strarray_addall( &clean_files, object_files );
2987 strarray_addall( &clean_files, crossobj_files );
2988 strarray_addall( &clean_files, res_files );
2989 strarray_addall( &clean_files, all_targets );
2990 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2992 if (make->subdirs.count)
2994 struct strarray makefile_deps = empty_strarray;
2995 struct strarray distclean_files = empty_strarray;
2997 for (i = 0; i < make->subdirs.count; i++)
2999 strarray_add( &makefile_deps, top_dir_path( make, base_dir_path( make->submakes[i],
3000 strmake ( "%s.in", output_makefile_name ))));
3001 strarray_add( &distclean_files, base_dir_path( make->submakes[i], output_makefile_name ));
3002 if (!make->src_dir)
3003 strarray_add( &distclean_files, base_dir_path( make->submakes[i], ".gitignore" ));
3004 if (make->submakes[i]->testdll)
3005 strarray_add( &distclean_files, base_dir_path( make->submakes[i], "testlist.c" ));
3007 output( "Makefile:" );
3008 output_filenames( makefile_deps );
3009 output( "\n" );
3010 output( "distclean::\n");
3011 output( "\trm -f" );
3012 output_filenames( distclean_files );
3013 output( "\n" );
3014 strarray_add( &phony_targets, "distclean" );
3016 if (msgfmt && strcmp( msgfmt, "false" ))
3018 strarray_addall( &clean_files, mo_files );
3019 output( "__builddeps__:" );
3020 output_filenames( mo_files );
3021 output( "\n" );
3022 for (i = 0; i < linguas.count; i++)
3024 output( "%s/%s.mo:", obj_dir_path( make, "po" ), linguas.str[i] );
3025 output( " %s/%s.po\n", src_dir_path( make, "po" ), linguas.str[i] );
3026 output( "\t%s -o $@ %s/%s.po\n", msgfmt, src_dir_path( make, "po" ), linguas.str[i] );
3029 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3032 if (clean_files.count)
3034 output( "%s::\n", obj_dir_path( make, "clean" ));
3035 output( "\trm -f" );
3036 output_filenames_obj_dir( make, clean_files );
3037 output( "\n" );
3038 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
3039 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
3042 if (phony_targets.count)
3044 output( ".PHONY:" );
3045 output_filenames( phony_targets );
3046 output( "\n" );
3049 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
3051 return clean_files;
3055 /*******************************************************************
3056 * create_temp_file
3058 static FILE *create_temp_file( const char *orig )
3060 char *name = xmalloc( strlen(orig) + 13 );
3061 unsigned int i, id = getpid();
3062 int fd;
3063 FILE *ret = NULL;
3065 for (i = 0; i < 100; i++)
3067 sprintf( name, "%s.tmp%08x", orig, id );
3068 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3070 ret = fdopen( fd, "w" );
3071 break;
3073 if (errno != EEXIST) break;
3074 id += 7777;
3076 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3077 temp_file_name = name;
3078 return ret;
3082 /*******************************************************************
3083 * rename_temp_file
3085 static void rename_temp_file( const char *dest )
3087 int ret = rename( temp_file_name, dest );
3088 if (ret == -1 && errno == EEXIST)
3090 /* rename doesn't overwrite on windows */
3091 unlink( dest );
3092 ret = rename( temp_file_name, dest );
3094 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3095 temp_file_name = NULL;
3099 /*******************************************************************
3100 * are_files_identical
3102 static int are_files_identical( FILE *file1, FILE *file2 )
3104 for (;;)
3106 char buffer1[8192], buffer2[8192];
3107 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3108 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3109 if (size1 != size2) return 0;
3110 if (!size1) return feof( file1 ) && feof( file2 );
3111 if (memcmp( buffer1, buffer2, size1 )) return 0;
3116 /*******************************************************************
3117 * rename_temp_file_if_changed
3119 static void rename_temp_file_if_changed( const char *dest )
3121 FILE *file1, *file2;
3122 int do_rename = 1;
3124 if ((file1 = fopen( dest, "r" )))
3126 if ((file2 = fopen( temp_file_name, "r" )))
3128 do_rename = !are_files_identical( file1, file2 );
3129 fclose( file2 );
3131 fclose( file1 );
3133 if (!do_rename)
3135 unlink( temp_file_name );
3136 temp_file_name = NULL;
3138 else rename_temp_file( dest );
3142 /*******************************************************************
3143 * output_testlist
3145 static void output_testlist( const struct makefile *make )
3147 const char *dest = base_dir_path( make, "testlist.c" );
3148 struct strarray files = empty_strarray;
3149 struct incl_file *source;
3150 unsigned int i;
3152 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3154 if (source->file->flags & FLAG_GENERATED) continue;
3155 if (!strendswith( source->name, ".c" )) continue;
3156 strarray_add( &files, replace_extension( source->name, ".c", "" ));
3159 output_file = create_temp_file( dest );
3161 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3162 output( "#define WIN32_LEAN_AND_MEAN\n" );
3163 output( "#include <windows.h>\n\n" );
3164 output( "#define STANDALONE\n" );
3165 output( "#include \"wine/test.h\"\n\n" );
3167 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
3168 output( "\n" );
3169 output( "const struct test winetest_testlist[] =\n" );
3170 output( "{\n" );
3171 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
3172 output( " { 0, 0 }\n" );
3173 output( "};\n" );
3175 if (fclose( output_file )) fatal_perror( "write" );
3176 output_file = NULL;
3177 rename_temp_file_if_changed( dest );
3181 /*******************************************************************
3182 * output_gitignore
3184 static void output_gitignore( const char *dest, struct strarray files )
3186 int i;
3188 output_file = create_temp_file( dest );
3190 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3191 for (i = 0; i < files.count; i++)
3193 if (!strchr( files.str[i], '/' )) output( "/" );
3194 output( "%s\n", files.str[i] );
3197 if (fclose( output_file )) fatal_perror( "write" );
3198 output_file = NULL;
3199 rename_temp_file( dest );
3203 /*******************************************************************
3204 * output_top_variables
3206 static void output_top_variables( const struct makefile *make )
3208 unsigned int i;
3209 struct strarray *vars = &top_makefile->vars;
3211 if (!make->base_dir) return; /* don't output variables in the top makefile */
3213 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
3214 output( "all:\n\n" );
3215 for (i = 0; i < vars->count; i += 2)
3217 if (!strcmp( vars->str[i], "SUBDIRS" )) continue; /* not inherited */
3218 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
3220 output( "\n" );
3224 /*******************************************************************
3225 * output_dependencies
3227 static void output_dependencies( const struct makefile *make )
3229 struct strarray targets, ignore_files = empty_strarray;
3230 char buffer[1024];
3231 FILE *src_file;
3232 int found = 0;
3234 output_file_name = base_dir_path( make, output_makefile_name );
3235 output_file = create_temp_file( output_file_name );
3236 output_top_variables( make );
3238 /* copy the contents of the source makefile */
3239 src_file = open_input_makefile( make );
3240 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
3242 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
3243 found = !strncmp( buffer, separator, strlen(separator) );
3245 if (fclose( src_file )) fatal_perror( "close" );
3246 input_file_name = NULL;
3248 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
3249 targets = output_sources( make );
3251 fclose( output_file );
3252 output_file = NULL;
3253 rename_temp_file( output_file_name );
3255 strarray_add( &ignore_files, ".gitignore" );
3256 strarray_add( &ignore_files, "Makefile" );
3257 if (make->testdll)
3259 output_testlist( make );
3260 strarray_add( &ignore_files, "testlist.c" );
3262 strarray_addall( &ignore_files, targets );
3263 if (!make->src_dir && make->base_dir)
3264 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
3266 output_file_name = NULL;
3270 /*******************************************************************
3271 * load_sources
3273 static void load_sources( struct makefile *make )
3275 static const char *source_vars[] =
3277 "C_SRCS",
3278 "OBJC_SRCS",
3279 "RC_SRCS",
3280 "MC_SRCS",
3281 "IDL_SRCS",
3282 "BISON_SRCS",
3283 "LEX_SRCS",
3284 "HEADER_SRCS",
3285 "XTEMPLATE_SRCS",
3286 "SVG_SRCS",
3287 "FONT_SRCS",
3288 "IN_SRCS",
3289 "MANPAGES",
3290 NULL
3292 const char **var;
3293 unsigned int i;
3294 struct strarray value;
3295 struct incl_file *file;
3297 if (root_src_dir)
3299 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
3300 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
3302 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
3303 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
3304 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
3306 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
3307 make->module = get_expanded_make_variable( make, "MODULE" );
3308 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
3309 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
3310 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
3311 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
3313 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
3314 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
3315 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
3316 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
3317 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
3318 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
3319 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
3320 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
3322 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
3324 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
3325 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
3327 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
3328 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 ) ||
3329 !strcmp( make->imports.str[i], "ucrtbase" );
3331 if (make->module && !make->install_lib.count) strarray_add( &make->install_lib, make->module );
3333 make->include_paths = empty_strarray;
3334 make->define_args = empty_strarray;
3335 strarray_add( &make->define_args, "-D__WINESRC__" );
3337 value = get_expanded_make_var_array( make, "EXTRAINCL" );
3338 for (i = 0; i < value.count; i++)
3339 if (!strncmp( value.str[i], "-I", 2 ))
3340 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
3341 else
3342 strarray_add_uniq( &make->define_args, value.str[i] );
3343 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
3345 list_init( &make->sources );
3346 list_init( &make->includes );
3348 /* FIXME: target dir has to exist to allow locating srcdir-relative include files */
3349 if (make->base_dir) create_dir( make->base_dir );
3351 for (var = source_vars; *var; var++)
3353 value = get_expanded_make_var_array( make, *var );
3354 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
3357 add_generated_sources( make );
3359 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
3360 for (i = 0; i < value.count; i++)
3362 /* default to .c for unknown extra object files */
3363 if (strendswith( value.str[i], ".o" ))
3364 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
3365 else
3366 add_generated_source( make, value.str[i], NULL );
3369 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
3373 /*******************************************************************
3374 * parse_makeflags
3376 static void parse_makeflags( const char *flags )
3378 const char *p = flags;
3379 char *var, *buffer = xmalloc( strlen(flags) + 1 );
3381 while (*p)
3383 while (isspace(*p)) p++;
3384 var = buffer;
3385 while (*p && !isspace(*p))
3387 if (*p == '\\' && p[1]) p++;
3388 *var++ = *p++;
3390 *var = 0;
3391 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
3396 /*******************************************************************
3397 * parse_option
3399 static int parse_option( const char *opt )
3401 if (opt[0] != '-')
3403 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
3404 return 0;
3406 switch(opt[1])
3408 case 'f':
3409 if (opt[2]) output_makefile_name = opt + 2;
3410 break;
3411 case 'R':
3412 relative_dir_mode = 1;
3413 break;
3414 default:
3415 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
3416 exit(1);
3418 return 1;
3422 /*******************************************************************
3423 * main
3425 int main( int argc, char *argv[] )
3427 const char *makeflags = getenv( "MAKEFLAGS" );
3428 int i, j;
3430 if (makeflags) parse_makeflags( makeflags );
3432 i = 1;
3433 while (i < argc)
3435 if (parse_option( argv[i] ))
3437 for (j = i; j < argc; j++) argv[j] = argv[j+1];
3438 argc--;
3440 else i++;
3443 if (relative_dir_mode)
3445 char *relpath;
3447 if (argc != 3)
3449 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
3450 exit( 1 );
3452 relpath = get_relative_path( argv[1], argv[2] );
3453 printf( "%s\n", relpath ? relpath : "." );
3454 exit( 0 );
3457 atexit( cleanup_files );
3458 signal( SIGTERM, exit_on_signal );
3459 signal( SIGINT, exit_on_signal );
3460 #ifdef SIGHUP
3461 signal( SIGHUP, exit_on_signal );
3462 #endif
3464 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
3466 top_makefile = parse_makefile( NULL );
3468 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
3469 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
3470 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
3471 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
3472 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
3473 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
3474 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
3475 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
3477 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
3478 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
3479 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
3480 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
3481 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
3482 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
3483 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
3484 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
3485 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
3486 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
3487 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
3488 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
3489 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
3491 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
3492 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
3493 if (!exe_ext) exe_ext = "";
3494 if (!tools_ext) tools_ext = "";
3495 if (!man_ext) man_ext = "3w";
3497 if (argc == 1)
3499 top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
3500 top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );
3502 for (i = 0; i < top_makefile->subdirs.count; i++)
3503 top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i] );
3505 load_sources( top_makefile );
3506 for (i = 0; i < top_makefile->subdirs.count; i++)
3507 load_sources( top_makefile->submakes[i] );
3509 for (i = 0; i < top_makefile->subdirs.count; i++)
3510 output_dependencies( top_makefile->submakes[i] );
3512 output_dependencies( top_makefile );
3513 return 0;
3516 for (i = 1; i < argc; i++)
3518 struct makefile *make = parse_makefile( argv[i] );
3519 load_sources( make );
3520 output_dependencies( make );
3522 return 0;