ntdll/tests: Add some tests for registry path names.
[wine.git] / tools / makedep.c
blobe514cb3a17ec4bb2277bba13720da604d86e9054
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;
150 struct makefile
152 struct strarray vars;
153 struct strarray include_paths;
154 struct strarray define_args;
155 struct strarray programs;
156 struct strarray scripts;
157 struct strarray appmode;
158 struct strarray imports;
159 struct strarray subdirs;
160 struct strarray delayimports;
161 struct strarray extradllflags;
162 struct strarray install_lib;
163 struct strarray install_dev;
164 struct list sources;
165 struct list includes;
166 const char *base_dir;
167 const char *src_dir;
168 const char *obj_dir;
169 const char *top_src_dir;
170 const char *top_obj_dir;
171 const char *parent_dir;
172 const char *module;
173 const char *testdll;
174 const char *sharedlib;
175 const char *staticlib;
176 const char *importlib;
177 int use_msvcrt;
178 int is_win16;
179 struct makefile **submakes;
182 static struct makefile *top_makefile;
184 static const char *output_makefile_name = "Makefile";
185 static const char *input_file_name;
186 static const char *output_file_name;
187 static const char *temp_file_name;
188 static int relative_dir_mode;
189 static int input_line;
190 static int output_column;
191 static FILE *output_file;
193 static const char Usage[] =
194 "Usage: makedep [options] [directories]\n"
195 "Options:\n"
196 " -R from to Compute the relative path between two directories\n"
197 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
200 #ifndef __GNUC__
201 #define __attribute__(x)
202 #endif
204 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
205 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
206 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
207 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
209 /*******************************************************************
210 * fatal_error
212 static void fatal_error( const char *msg, ... )
214 va_list valist;
215 va_start( valist, msg );
216 if (input_file_name)
218 fprintf( stderr, "%s:", input_file_name );
219 if (input_line) fprintf( stderr, "%d:", input_line );
220 fprintf( stderr, " error: " );
222 else fprintf( stderr, "makedep: error: " );
223 vfprintf( stderr, msg, valist );
224 va_end( valist );
225 exit(1);
229 /*******************************************************************
230 * fatal_perror
232 static void fatal_perror( const char *msg, ... )
234 va_list valist;
235 va_start( valist, msg );
236 if (input_file_name)
238 fprintf( stderr, "%s:", input_file_name );
239 if (input_line) fprintf( stderr, "%d:", input_line );
240 fprintf( stderr, " error: " );
242 else fprintf( stderr, "makedep: error: " );
243 vfprintf( stderr, msg, valist );
244 perror( " " );
245 va_end( valist );
246 exit(1);
250 /*******************************************************************
251 * cleanup_files
253 static void cleanup_files(void)
255 if (temp_file_name) unlink( temp_file_name );
256 if (output_file_name) unlink( output_file_name );
260 /*******************************************************************
261 * exit_on_signal
263 static void exit_on_signal( int sig )
265 exit( 1 ); /* this will call the atexit functions */
269 /*******************************************************************
270 * xmalloc
272 static void *xmalloc( size_t size )
274 void *res;
275 if (!(res = malloc (size ? size : 1)))
276 fatal_error( "Virtual memory exhausted.\n" );
277 return res;
281 /*******************************************************************
282 * xrealloc
284 static void *xrealloc (void *ptr, size_t size)
286 void *res;
287 assert( size );
288 if (!(res = realloc( ptr, size )))
289 fatal_error( "Virtual memory exhausted.\n" );
290 return res;
293 /*******************************************************************
294 * xstrdup
296 static char *xstrdup( const char *str )
298 char *res = strdup( str );
299 if (!res) fatal_error( "Virtual memory exhausted.\n" );
300 return res;
304 /*******************************************************************
305 * strmake
307 static char *strmake( const char* fmt, ... )
309 int n;
310 size_t size = 100;
311 va_list ap;
313 for (;;)
315 char *p = xmalloc (size);
316 va_start(ap, fmt);
317 n = vsnprintf (p, size, fmt, ap);
318 va_end(ap);
319 if (n == -1) size *= 2;
320 else if ((size_t)n >= size) size = n + 1;
321 else return xrealloc( p, n + 1 );
322 free(p);
327 /*******************************************************************
328 * strendswith
330 static int strendswith( const char* str, const char* end )
332 size_t l = strlen( str );
333 size_t m = strlen( end );
335 return l >= m && strcmp(str + l - m, end) == 0;
339 /*******************************************************************
340 * output
342 static void output( const char *format, ... )
344 int ret;
345 va_list valist;
347 va_start( valist, format );
348 ret = vfprintf( output_file, format, valist );
349 va_end( valist );
350 if (ret < 0) fatal_perror( "output" );
351 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
352 else output_column += ret;
356 /*******************************************************************
357 * strarray_add
359 static void strarray_add( struct strarray *array, const char *str )
361 if (array->count == array->size)
363 if (array->size) array->size *= 2;
364 else array->size = 16;
365 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
367 array->str[array->count++] = str;
371 /*******************************************************************
372 * strarray_addall
374 static void strarray_addall( struct strarray *array, struct strarray added )
376 unsigned int i;
378 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
382 /*******************************************************************
383 * strarray_exists
385 static int strarray_exists( const struct strarray *array, const char *str )
387 unsigned int i;
389 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
390 return 0;
394 /*******************************************************************
395 * strarray_add_uniq
397 static void strarray_add_uniq( struct strarray *array, const char *str )
399 if (!strarray_exists( array, str )) strarray_add( array, str );
403 /*******************************************************************
404 * strarray_get_value
406 * Find a value in a name/value pair string array.
408 static const char *strarray_get_value( const struct strarray *array, const char *name )
410 unsigned int i;
412 for (i = 0; i < array->count; i += 2)
413 if (!strcmp( array->str[i], name )) return array->str[i + 1];
414 return NULL;
418 /*******************************************************************
419 * strarray_set_value
421 * Define a value in a name/value pair string array.
423 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
425 unsigned int i;
427 /* redefining a variable replaces the previous value */
428 for (i = 0; i < array->count; i += 2)
430 if (strcmp( array->str[i], name )) continue;
431 array->str[i + 1] = value;
432 return;
434 strarray_add( array, name );
435 strarray_add( array, value );
439 /*******************************************************************
440 * output_filename
442 static void output_filename( const char *name )
444 if (output_column + strlen(name) + 1 > 100)
446 output( " \\\n" );
447 output( " " );
449 else if (output_column) output( " " );
450 output( "%s", name );
454 /*******************************************************************
455 * output_filenames
457 static void output_filenames( struct strarray array )
459 unsigned int i;
461 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
465 /*******************************************************************
466 * get_extension
468 static char *get_extension( char *filename )
470 char *ext = strrchr( filename, '.' );
471 if (ext && strchr( ext, '/' )) ext = NULL;
472 return ext;
476 /*******************************************************************
477 * replace_extension
479 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
481 char *ret;
482 size_t name_len = strlen( name );
483 size_t ext_len = strlen( old_ext );
485 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
486 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
487 memcpy( ret, name, name_len );
488 strcpy( ret + name_len, new_ext );
489 return ret;
493 /*******************************************************************
494 * replace_filename
496 static char *replace_filename( const char *path, const char *name )
498 const char *p;
499 char *ret;
500 size_t len;
502 if (!path) return xstrdup( name );
503 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
504 len = p - path + 1;
505 ret = xmalloc( len + strlen( name ) + 1 );
506 memcpy( ret, path, len );
507 strcpy( ret + len, name );
508 return ret;
512 /*******************************************************************
513 * strarray_replace_extension
515 static struct strarray strarray_replace_extension( const struct strarray *array,
516 const char *old_ext, const char *new_ext )
518 unsigned int i;
519 struct strarray ret;
521 ret.count = ret.size = array->count;
522 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
523 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
524 return ret;
528 /*******************************************************************
529 * replace_substr
531 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
533 size_t pos = start - str;
534 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
535 memcpy( ret, str, pos );
536 strcpy( ret + pos, replace );
537 strcat( ret + pos, start + len );
538 return ret;
542 /*******************************************************************
543 * get_relative_path
545 * Determine where the destination path is located relative to the 'from' path.
547 static char *get_relative_path( const char *from, const char *dest )
549 const char *start;
550 char *ret, *p;
551 unsigned int dotdots = 0;
553 /* a path of "." is equivalent to an empty path */
554 if (!strcmp( from, "." )) from = "";
556 for (;;)
558 while (*from == '/') from++;
559 while (*dest == '/') dest++;
560 start = dest; /* save start of next path element */
561 if (!*from) break;
563 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
564 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
566 /* count remaining elements in 'from' */
569 dotdots++;
570 while (*from && *from != '/') from++;
571 while (*from == '/') from++;
573 while (*from);
574 break;
577 if (!start[0] && !dotdots) return NULL; /* empty path */
579 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
580 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
582 if (start[0]) strcpy( p, start );
583 else p[-1] = 0; /* remove trailing slash */
584 return ret;
588 /*******************************************************************
589 * concat_paths
591 static char *concat_paths( const char *base, const char *path )
593 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
594 if (!path || !path[0]) return xstrdup( base );
595 if (path[0] == '/') return xstrdup( path );
596 return strmake( "%s/%s", base, path );
600 /*******************************************************************
601 * base_dir_path
603 static char *base_dir_path( const struct makefile *make, const char *path )
605 return concat_paths( make->base_dir, path );
609 /*******************************************************************
610 * obj_dir_path
612 static char *obj_dir_path( const struct makefile *make, const char *path )
614 return concat_paths( make->obj_dir, path );
618 /*******************************************************************
619 * src_dir_path
621 static char *src_dir_path( const struct makefile *make, const char *path )
623 if (make->src_dir) return concat_paths( make->src_dir, path );
624 return obj_dir_path( make, path );
628 /*******************************************************************
629 * top_obj_dir_path
631 static char *top_obj_dir_path( const struct makefile *make, const char *path )
633 return concat_paths( make->top_obj_dir, path );
637 /*******************************************************************
638 * top_dir_path
640 static char *top_dir_path( const struct makefile *make, const char *path )
642 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
643 return top_obj_dir_path( make, path );
647 /*******************************************************************
648 * root_dir_path
650 static char *root_dir_path( const char *path )
652 return concat_paths( root_src_dir, path );
656 /*******************************************************************
657 * tools_dir_path
659 static char *tools_dir_path( const struct makefile *make, const char *path )
661 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
662 return top_obj_dir_path( make, strmake( "tools/%s", path ));
666 /*******************************************************************
667 * tools_path
669 static char *tools_path( const struct makefile *make, const char *name )
671 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
675 /*******************************************************************
676 * get_line
678 static char *get_line( FILE *file )
680 static char *buffer;
681 static size_t size;
683 if (!size)
685 size = 1024;
686 buffer = xmalloc( size );
688 if (!fgets( buffer, size, file )) return NULL;
689 input_line++;
691 for (;;)
693 char *p = buffer + strlen(buffer);
694 /* if line is larger than buffer, resize buffer */
695 while (p == buffer + size - 1 && p[-1] != '\n')
697 buffer = xrealloc( buffer, size * 2 );
698 if (!fgets( buffer + size - 1, size + 1, file )) break;
699 p = buffer + strlen(buffer);
700 size *= 2;
702 if (p > buffer && p[-1] == '\n')
704 *(--p) = 0;
705 if (p > buffer && p[-1] == '\r') *(--p) = 0;
706 if (p > buffer && p[-1] == '\\')
708 *(--p) = 0;
709 /* line ends in backslash, read continuation line */
710 if (!fgets( p, size - (p - buffer), file )) return buffer;
711 input_line++;
712 continue;
715 return buffer;
720 /*******************************************************************
721 * hash_filename
723 static unsigned int hash_filename( const char *name )
725 unsigned int ret = 0;
726 while (*name) ret = (ret << 7) + (ret << 3) + *name++;
727 return ret % HASH_SIZE;
731 /*******************************************************************
732 * add_file
734 static struct file *add_file( const char *name )
736 struct file *file = xmalloc( sizeof(*file) );
737 memset( file, 0, sizeof(*file) );
738 file->name = xstrdup( name );
739 list_add_tail( &files[hash_filename( name )], &file->entry );
740 return file;
744 /*******************************************************************
745 * add_dependency
747 static void add_dependency( struct file *file, const char *name, enum incl_type type )
749 /* enforce some rules for the Wine tree */
751 if (!memcmp( name, "../", 3 ))
752 fatal_error( "#include directive with relative path not allowed\n" );
754 if (!strcmp( name, "config.h" ))
756 if (strendswith( file->name, ".h" ))
757 fatal_error( "config.h must not be included by a header file\n" );
758 if (file->deps_count)
759 fatal_error( "config.h must be included before anything else\n" );
761 else if (!strcmp( name, "wine/port.h" ))
763 if (strendswith( file->name, ".h" ))
764 fatal_error( "wine/port.h must not be included by a header file\n" );
765 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
766 if (file->deps_count > 1)
767 fatal_error( "wine/port.h must be included before everything except config.h\n" );
768 if (strcmp( file->deps[0].name, "config.h" ))
769 fatal_error( "config.h must be included before wine/port.h\n" );
772 if (file->deps_count >= file->deps_size)
774 file->deps_size *= 2;
775 if (file->deps_size < 16) file->deps_size = 16;
776 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
778 file->deps[file->deps_count].line = input_line;
779 file->deps[file->deps_count].type = type;
780 file->deps[file->deps_count].name = xstrdup( name );
781 file->deps_count++;
785 /*******************************************************************
786 * find_src_file
788 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
790 struct incl_file *file;
792 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
793 if (!strcmp( name, file->name )) return file;
794 return NULL;
797 /*******************************************************************
798 * find_include_file
800 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
802 struct incl_file *file;
804 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
805 if (!strcmp( name, file->name )) return file;
806 return NULL;
809 /*******************************************************************
810 * add_include
812 * Add an include file if it doesn't already exists.
814 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
815 const char *name, int line, enum incl_type type )
817 struct incl_file *include;
819 if (parent->files_count >= parent->files_size)
821 parent->files_size *= 2;
822 if (parent->files_size < 16) parent->files_size = 16;
823 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
826 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
827 if (!strcmp( name, include->name )) goto found;
829 include = xmalloc( sizeof(*include) );
830 memset( include, 0, sizeof(*include) );
831 include->name = xstrdup(name);
832 include->included_by = parent;
833 include->included_line = line;
834 include->type = type;
835 list_add_tail( &make->includes, &include->entry );
836 found:
837 parent->files[parent->files_count++] = include;
838 return include;
842 /*******************************************************************
843 * add_generated_source
845 * Add a generated source file to the list.
847 static struct incl_file *add_generated_source( struct makefile *make,
848 const char *name, const char *filename )
850 struct incl_file *file;
852 if ((file = find_src_file( make, name ))) return file; /* we already have it */
853 file = xmalloc( sizeof(*file) );
854 memset( file, 0, sizeof(*file) );
855 file->file = add_file( name );
856 file->name = xstrdup( name );
857 file->filename = obj_dir_path( make, filename ? filename : name );
858 file->file->flags = FLAG_GENERATED;
859 list_add_tail( &make->sources, &file->entry );
860 return file;
864 /*******************************************************************
865 * parse_include_directive
867 static void parse_include_directive( struct file *source, char *str )
869 char quote, *include, *p = str;
871 while (*p && isspace(*p)) p++;
872 if (*p != '\"' && *p != '<' ) return;
873 quote = *p++;
874 if (quote == '<') quote = '>';
875 include = p;
876 while (*p && (*p != quote)) p++;
877 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
878 *p = 0;
879 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
883 /*******************************************************************
884 * parse_pragma_directive
886 static void parse_pragma_directive( struct file *source, char *str )
888 char *flag, *p = str;
890 if (!isspace( *p )) return;
891 while (*p && isspace(*p)) p++;
892 p = strtok( p, " \t" );
893 if (strcmp( p, "makedep" )) return;
895 while ((flag = strtok( NULL, " \t" )))
897 if (!strcmp( flag, "depend" ))
899 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
900 return;
902 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
904 if (strendswith( source->name, ".idl" ))
906 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
907 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
908 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
909 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
910 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
911 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
912 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
913 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
915 else if (strendswith( source->name, ".rc" ))
917 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
919 else if (strendswith( source->name, ".sfd" ))
921 if (!strcmp( flag, "font" ))
923 struct strarray *array = source->args;
925 if (!array)
927 source->args = array = xmalloc( sizeof(*array) );
928 *array = empty_strarray;
929 source->flags |= FLAG_SFD_FONTS;
931 strarray_add( array, xstrdup( strtok( NULL, "" )));
932 return;
935 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
940 /*******************************************************************
941 * parse_cpp_directive
943 static void parse_cpp_directive( struct file *source, char *str )
945 while (*str && isspace(*str)) str++;
946 if (*str++ != '#') return;
947 while (*str && isspace(*str)) str++;
949 if (!strncmp( str, "include", 7 ))
950 parse_include_directive( source, str + 7 );
951 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
952 parse_include_directive( source, str + 6 );
953 else if (!strncmp( str, "pragma", 6 ))
954 parse_pragma_directive( source, str + 6 );
958 /*******************************************************************
959 * parse_idl_file
961 static void parse_idl_file( struct file *source, FILE *file )
963 char *buffer, *include;
965 input_line = 0;
967 while ((buffer = get_line( file )))
969 char quote;
970 char *p = buffer;
971 while (*p && isspace(*p)) p++;
973 if (!strncmp( p, "importlib", 9 ))
975 p += 9;
976 while (*p && isspace(*p)) p++;
977 if (*p++ != '(') continue;
978 while (*p && isspace(*p)) p++;
979 if (*p++ != '"') continue;
980 include = p;
981 while (*p && (*p != '"')) p++;
982 if (!*p) fatal_error( "malformed importlib directive\n" );
983 *p = 0;
984 add_dependency( source, include, INCL_IMPORTLIB );
985 continue;
988 if (!strncmp( p, "import", 6 ))
990 p += 6;
991 while (*p && isspace(*p)) p++;
992 if (*p != '"') continue;
993 include = ++p;
994 while (*p && (*p != '"')) p++;
995 if (!*p) fatal_error( "malformed import directive\n" );
996 *p = 0;
997 add_dependency( source, include, INCL_IMPORT );
998 continue;
1001 /* check for #include inside cpp_quote */
1002 if (!strncmp( p, "cpp_quote", 9 ))
1004 p += 9;
1005 while (*p && isspace(*p)) p++;
1006 if (*p++ != '(') continue;
1007 while (*p && isspace(*p)) p++;
1008 if (*p++ != '"') continue;
1009 if (*p++ != '#') continue;
1010 while (*p && isspace(*p)) p++;
1011 if (strncmp( p, "include", 7 )) continue;
1012 p += 7;
1013 while (*p && isspace(*p)) p++;
1014 if (*p == '\\' && p[1] == '"')
1016 p += 2;
1017 quote = '"';
1019 else
1021 if (*p++ != '<' ) continue;
1022 quote = '>';
1024 include = p;
1025 while (*p && (*p != quote)) p++;
1026 if (!*p || (quote == '"' && p[-1] != '\\'))
1027 fatal_error( "malformed #include directive inside cpp_quote\n" );
1028 if (quote == '"') p--; /* remove backslash */
1029 *p = 0;
1030 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1031 continue;
1034 parse_cpp_directive( source, p );
1038 /*******************************************************************
1039 * parse_c_file
1041 static void parse_c_file( struct file *source, FILE *file )
1043 char *buffer;
1045 input_line = 0;
1046 while ((buffer = get_line( file )))
1048 parse_cpp_directive( source, buffer );
1053 /*******************************************************************
1054 * parse_rc_file
1056 static void parse_rc_file( struct file *source, FILE *file )
1058 char *buffer, *include;
1060 input_line = 0;
1061 while ((buffer = get_line( file )))
1063 char quote;
1064 char *p = buffer;
1065 while (*p && isspace(*p)) p++;
1067 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1069 p += 2;
1070 while (*p && isspace(*p)) p++;
1071 if (strncmp( p, "@makedep:", 9 )) continue;
1072 p += 9;
1073 while (*p && isspace(*p)) p++;
1074 quote = '"';
1075 if (*p == quote)
1077 include = ++p;
1078 while (*p && *p != quote) p++;
1080 else
1082 include = p;
1083 while (*p && !isspace(*p) && *p != '*') p++;
1085 if (!*p)
1086 fatal_error( "malformed makedep comment\n" );
1087 *p = 0;
1088 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1089 continue;
1092 parse_cpp_directive( source, buffer );
1097 /*******************************************************************
1098 * parse_in_file
1100 static void parse_in_file( struct file *source, FILE *file )
1102 char *p, *buffer;
1104 /* make sure it gets rebuilt when the version changes */
1105 add_dependency( source, "config.h", INCL_SYSTEM );
1107 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1109 input_line = 0;
1110 while ((buffer = get_line( file )))
1112 if (strncmp( buffer, ".TH", 3 )) continue;
1113 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1114 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1115 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1116 source->args = xstrdup( p );
1117 return;
1122 /*******************************************************************
1123 * parse_sfd_file
1125 static void parse_sfd_file( struct file *source, FILE *file )
1127 char *p, *eol, *buffer;
1129 input_line = 0;
1130 while ((buffer = get_line( file )))
1132 if (strncmp( buffer, "UComments:", 10 )) continue;
1133 p = buffer + 10;
1134 while (*p == ' ') p++;
1135 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1137 p++;
1138 buffer[strlen(buffer) - 1] = 0;
1140 while ((eol = strstr( p, "+AAoA" )))
1142 *eol = 0;
1143 while (*p && isspace(*p)) p++;
1144 if (*p++ == '#')
1146 while (*p && isspace(*p)) p++;
1147 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1149 p = eol + 5;
1151 while (*p && isspace(*p)) p++;
1152 if (*p++ != '#') return;
1153 while (*p && isspace(*p)) p++;
1154 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1155 return;
1160 static const struct
1162 const char *ext;
1163 void (*parse)( struct file *file, FILE *f );
1164 } parse_functions[] =
1166 { ".c", parse_c_file },
1167 { ".h", parse_c_file },
1168 { ".inl", parse_c_file },
1169 { ".l", parse_c_file },
1170 { ".m", parse_c_file },
1171 { ".rh", parse_c_file },
1172 { ".x", parse_c_file },
1173 { ".y", parse_c_file },
1174 { ".idl", parse_idl_file },
1175 { ".rc", parse_rc_file },
1176 { ".in", parse_in_file },
1177 { ".sfd", parse_sfd_file }
1180 /*******************************************************************
1181 * load_file
1183 static struct file *load_file( const char *name )
1185 struct file *file;
1186 FILE *f;
1187 unsigned int i, hash = hash_filename( name );
1189 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1190 if (!strcmp( name, file->name )) return file;
1192 if (!(f = fopen( name, "r" ))) return NULL;
1194 file = add_file( name );
1195 input_file_name = file->name;
1196 input_line = 0;
1198 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1200 if (!strendswith( name, parse_functions[i].ext )) continue;
1201 parse_functions[i].parse( file, f );
1202 break;
1205 fclose( f );
1206 input_file_name = NULL;
1208 return file;
1212 /*******************************************************************
1213 * open_include_path_file
1215 * Open a file from a directory on the include path.
1217 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1218 const char *name, char **filename )
1220 char *src_path = base_dir_path( make, concat_paths( dir, name ));
1221 struct file *ret = load_file( src_path );
1223 if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
1224 return ret;
1228 /*******************************************************************
1229 * open_file_same_dir
1231 * Open a file in the same directory as the parent.
1233 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1235 char *src_path = replace_filename( parent->file->name, name );
1236 struct file *ret = load_file( src_path );
1238 if (ret) *filename = replace_filename( parent->filename, name );
1239 free( src_path );
1240 return ret;
1244 /*******************************************************************
1245 * open_local_file
1247 * Open a file in the source directory of the makefile.
1249 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1251 char *src_path = root_dir_path( base_dir_path( make, path ));
1252 struct file *ret = load_file( src_path );
1254 /* if not found, try parent dir */
1255 if (!ret && make->parent_dir)
1257 free( src_path );
1258 path = strmake( "%s/%s", make->parent_dir, path );
1259 src_path = root_dir_path( base_dir_path( make, path ));
1260 ret = load_file( src_path );
1263 if (ret) *filename = src_dir_path( make, path );
1264 free( src_path );
1265 return ret;
1269 /*******************************************************************
1270 * open_global_file
1272 * Open a file in the top-level source directory.
1274 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1276 char *src_path = root_dir_path( path );
1277 struct file *ret = load_file( src_path );
1279 if (ret) *filename = top_dir_path( make, path );
1280 free( src_path );
1281 return ret;
1285 /*******************************************************************
1286 * open_global_header
1288 * Open a file in the global include source directory.
1290 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1292 return open_global_file( make, strmake( "include/%s", path ), filename );
1296 /*******************************************************************
1297 * open_src_file
1299 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1301 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1303 if (!file) fatal_perror( "open %s", pFile->name );
1304 return file;
1308 /*******************************************************************
1309 * open_include_file
1311 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1313 struct file *file = NULL;
1314 char *filename;
1315 unsigned int i, len;
1317 errno = ENOENT;
1319 /* check for generated bison header */
1321 if (strendswith( pFile->name, ".tab.h" ) &&
1322 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1324 pFile->sourcename = filename;
1325 pFile->filename = obj_dir_path( make, pFile->name );
1326 return file;
1329 /* check for corresponding idl file in source dir */
1331 if (strendswith( pFile->name, ".h" ) &&
1332 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1334 pFile->sourcename = filename;
1335 pFile->filename = obj_dir_path( make, pFile->name );
1336 return file;
1339 /* check for corresponding tlb file in source dir */
1341 if (strendswith( pFile->name, ".tlb" ) &&
1342 (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1344 pFile->sourcename = filename;
1345 pFile->filename = obj_dir_path( make, pFile->name );
1346 return file;
1349 /* now try in source dir */
1350 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1352 /* check for corresponding idl file in global includes */
1354 if (strendswith( pFile->name, ".h" ) &&
1355 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1357 pFile->sourcename = filename;
1358 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1359 return file;
1362 /* check for corresponding .in file in global includes (for config.h.in) */
1364 if (strendswith( pFile->name, ".h" ) &&
1365 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1367 pFile->sourcename = filename;
1368 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1369 return file;
1372 /* check for corresponding .x file in global includes */
1374 if (strendswith( pFile->name, "tmpl.h" ) &&
1375 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1377 pFile->sourcename = filename;
1378 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1379 return file;
1382 /* check for corresponding .tlb file in global includes */
1384 if (strendswith( pFile->name, ".tlb" ) &&
1385 (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1387 pFile->sourcename = filename;
1388 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1389 return file;
1392 /* check in global includes source dir */
1394 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1396 /* check in global msvcrt includes */
1397 if (make->use_msvcrt &&
1398 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1399 return file;
1401 /* now search in include paths */
1402 for (i = 0; i < make->include_paths.count; i++)
1404 const char *dir = make->include_paths.str[i];
1405 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1407 if (prefix)
1409 len = strlen( prefix );
1410 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1412 while (dir[len] == '/') len++;
1413 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1414 if (file) return file;
1416 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1418 if (*dir != '/')
1420 if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1421 return file;
1424 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1426 /* try in src file directory */
1427 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1429 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1430 perror( pFile->name );
1431 pFile = pFile->included_by;
1432 while (pFile && pFile->included_by)
1434 const char *parent = pFile->included_by->sourcename;
1435 if (!parent) parent = pFile->included_by->file->name;
1436 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1437 parent, pFile->included_line, pFile->name );
1438 pFile = pFile->included_by;
1440 exit(1);
1444 /*******************************************************************
1445 * add_all_includes
1447 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1449 unsigned int i;
1451 parent->files_count = 0;
1452 parent->files_size = file->deps_count;
1453 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1454 for (i = 0; i < file->deps_count; i++)
1456 switch (file->deps[i].type)
1458 case INCL_NORMAL:
1459 case INCL_IMPORT:
1460 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1461 break;
1462 case INCL_IMPORTLIB:
1463 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1464 break;
1465 case INCL_SYSTEM:
1466 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1467 break;
1468 case INCL_CPP_QUOTE:
1469 case INCL_CPP_QUOTE_SYSTEM:
1470 break;
1476 /*******************************************************************
1477 * parse_file
1479 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1481 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1483 if (!file) return;
1485 source->file = file;
1486 source->files_count = 0;
1487 source->files_size = file->deps_count;
1488 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1490 if (source->sourcename)
1492 if (strendswith( source->sourcename, ".idl" ))
1494 unsigned int i;
1496 if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
1498 /* generated .h file always includes these */
1499 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1500 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1501 for (i = 0; i < file->deps_count; i++)
1503 switch (file->deps[i].type)
1505 case INCL_IMPORT:
1506 if (strendswith( file->deps[i].name, ".idl" ))
1507 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1508 file->deps[i].line, INCL_NORMAL );
1509 else
1510 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1511 break;
1512 case INCL_CPP_QUOTE:
1513 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1514 break;
1515 case INCL_CPP_QUOTE_SYSTEM:
1516 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1517 break;
1518 case INCL_NORMAL:
1519 case INCL_SYSTEM:
1520 case INCL_IMPORTLIB:
1521 break;
1524 return;
1526 if (strendswith( source->sourcename, ".y" ))
1527 return; /* generated .tab.h doesn't include anything */
1530 add_all_includes( make, source, file );
1534 /*******************************************************************
1535 * add_src_file
1537 * Add a source file to the list.
1539 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1541 struct incl_file *file;
1543 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1544 file = xmalloc( sizeof(*file) );
1545 memset( file, 0, sizeof(*file) );
1546 file->name = xstrdup(name);
1547 list_add_tail( &make->sources, &file->entry );
1548 parse_file( make, file, 1 );
1549 return file;
1553 /*******************************************************************
1554 * open_input_makefile
1556 static FILE *open_input_makefile( const struct makefile *make )
1558 FILE *ret;
1560 if (make->base_dir)
1561 input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
1562 else
1563 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1565 input_line = 0;
1566 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1567 return ret;
1571 /*******************************************************************
1572 * get_make_variable
1574 static const char *get_make_variable( const struct makefile *make, const char *name )
1576 const char *ret;
1578 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1579 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1580 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1581 return NULL;
1585 /*******************************************************************
1586 * get_expanded_make_variable
1588 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1590 const char *var;
1591 char *p, *end, *expand, *tmp;
1593 var = get_make_variable( make, name );
1594 if (!var) return NULL;
1596 p = expand = xstrdup( var );
1597 while ((p = strchr( p, '$' )))
1599 if (p[1] == '(')
1601 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1602 *end++ = 0;
1603 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1604 var = get_make_variable( make, p + 2 );
1605 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1606 /* switch to the new string */
1607 p = tmp + (p - expand);
1608 free( expand );
1609 expand = tmp;
1611 else if (p[1] == '{') /* don't expand ${} variables */
1613 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1614 p = end + 1;
1616 else if (p[1] == '$')
1618 p += 2;
1620 else fatal_error( "syntax error in '%s'\n", expand );
1623 /* consider empty variables undefined */
1624 p = expand;
1625 while (*p && isspace(*p)) p++;
1626 if (*p) return expand;
1627 free( expand );
1628 return NULL;
1632 /*******************************************************************
1633 * get_expanded_make_var_array
1635 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1637 struct strarray ret = empty_strarray;
1638 char *value, *token;
1640 if ((value = get_expanded_make_variable( make, name )))
1641 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1642 strarray_add( &ret, token );
1643 return ret;
1647 /*******************************************************************
1648 * file_local_var
1650 static char *file_local_var( const char *file, const char *name )
1652 char *p, *var;
1654 var = strmake( "%s_%s", file, name );
1655 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1656 return var;
1660 /*******************************************************************
1661 * set_make_variable
1663 static int set_make_variable( struct strarray *array, const char *assignment )
1665 char *p, *name;
1667 p = name = xstrdup( assignment );
1668 while (isalnum(*p) || *p == '_') p++;
1669 if (name == p) return 0; /* not a variable */
1670 if (isspace(*p))
1672 *p++ = 0;
1673 while (isspace(*p)) p++;
1675 if (*p != '=') return 0; /* not an assignment */
1676 *p++ = 0;
1677 while (isspace(*p)) p++;
1679 strarray_set_value( array, name, p );
1680 return 1;
1684 /*******************************************************************
1685 * parse_makefile
1687 static struct makefile *parse_makefile( const char *path, const char *separator )
1689 char *buffer;
1690 FILE *file;
1691 struct makefile *make = xmalloc( sizeof(*make) );
1693 memset( make, 0, sizeof(*make) );
1694 if (path)
1696 make->top_obj_dir = get_relative_path( path, "" );
1697 make->base_dir = path;
1698 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1701 file = open_input_makefile( make );
1702 while ((buffer = get_line( file )))
1704 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1705 if (*buffer == '\t') continue; /* command */
1706 while (isspace( *buffer )) buffer++;
1707 if (*buffer == '#') continue; /* comment */
1708 set_make_variable( &make->vars, buffer );
1710 fclose( file );
1711 input_file_name = NULL;
1712 return make;
1716 /*******************************************************************
1717 * add_generated_sources
1719 static void add_generated_sources( struct makefile *make )
1721 struct incl_file *source, *next, *file;
1723 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1725 if (source->file->flags & FLAG_IDL_CLIENT)
1727 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1728 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1729 add_all_includes( make, file, file->file );
1731 if (source->file->flags & FLAG_IDL_SERVER)
1733 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1734 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1735 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1736 add_all_includes( make, file, file->file );
1738 if (source->file->flags & FLAG_IDL_IDENT)
1740 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1741 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1742 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1743 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1744 add_all_includes( make, file, file->file );
1746 if (source->file->flags & FLAG_IDL_PROXY)
1748 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1749 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1750 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1751 add_all_includes( make, file, file->file );
1752 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1753 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1754 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1755 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1756 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1757 add_all_includes( make, file, file->file );
1759 if (source->file->flags & FLAG_IDL_TYPELIB)
1761 add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
1763 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1765 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1767 if (source->file->flags & FLAG_IDL_REGISTER)
1769 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1771 if (source->file->flags & FLAG_IDL_HEADER)
1773 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1775 if (!source->file->flags && strendswith( source->name, ".idl" ))
1777 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1779 if (strendswith( source->name, ".x" ))
1781 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1783 if (strendswith( source->name, ".y" ))
1785 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1786 /* steal the includes list from the source file */
1787 file->files_count = source->files_count;
1788 file->files_size = source->files_size;
1789 file->files = source->files;
1790 source->files_count = source->files_size = 0;
1791 source->files = NULL;
1793 if (strendswith( source->name, ".l" ))
1795 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1796 /* steal the includes list from the source file */
1797 file->files_count = source->files_count;
1798 file->files_size = source->files_size;
1799 file->files = source->files;
1800 source->files_count = source->files_size = 0;
1801 source->files = NULL;
1804 if (make->testdll)
1806 file = add_generated_source( make, "testlist.o", "testlist.c" );
1807 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1808 add_all_includes( make, file, file->file );
1813 /*******************************************************************
1814 * create_dir
1816 static void create_dir( const char *dir )
1818 char *p, *path;
1820 p = path = xstrdup( dir );
1821 while ((p = strchr( p, '/' )))
1823 *p = 0;
1824 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1825 *p++ = '/';
1826 while (*p == '/') p++;
1828 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1829 free( path );
1833 /*******************************************************************
1834 * output_filenames_obj_dir
1836 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1838 unsigned int i;
1840 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1844 /*******************************************************************
1845 * get_dependencies
1847 static void get_dependencies( struct strarray *deps, struct incl_file *file, struct incl_file *source )
1849 unsigned int i;
1851 if (!file->filename) return;
1853 if (file != source)
1855 if (file->owner == source) return; /* already processed */
1856 if (file->type == INCL_IMPORTLIB &&
1857 !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1858 return; /* library is imported only when building a typelib */
1859 file->owner = source;
1860 strarray_add( deps, file->filename );
1862 for (i = 0; i < file->files_count; i++) get_dependencies( deps, file->files[i], source );
1866 /*******************************************************************
1867 * get_local_dependencies
1869 * Get the local dependencies of a given target.
1871 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1872 struct strarray targets )
1874 unsigned int i;
1875 struct strarray deps = get_expanded_make_var_array( make, file_local_var( name, "DEPS" ));
1877 for (i = 0; i < deps.count; i++)
1879 if (strarray_exists( &targets, deps.str[i] ))
1880 deps.str[i] = obj_dir_path( make, deps.str[i] );
1881 else
1882 deps.str[i] = src_dir_path( make, deps.str[i] );
1884 return deps;
1888 /*******************************************************************
1889 * add_install_rule
1891 static void add_install_rule( const struct makefile *make, struct strarray *install_rules,
1892 const char *target, const char *file, const char *dest )
1894 if (strarray_exists( &make->install_lib, target ))
1896 strarray_add( &install_rules[INSTALL_LIB], file );
1897 strarray_add( &install_rules[INSTALL_LIB], dest );
1899 else if (strarray_exists( &make->install_dev, target ))
1901 strarray_add( &install_rules[INSTALL_DEV], file );
1902 strarray_add( &install_rules[INSTALL_DEV], dest );
1907 /*******************************************************************
1908 * get_include_install_path
1910 * Determine the installation path for a given include file.
1912 static const char *get_include_install_path( const char *name )
1914 if (!strncmp( name, "wine/", 5 )) return name + 5;
1915 if (!strncmp( name, "msvcrt/", 7 )) return name;
1916 return strmake( "windows/%s", name );
1920 /*******************************************************************
1921 * get_shared_library_name
1923 * Determine possible names for a shared library with a version number.
1925 static struct strarray get_shared_lib_names( const char *libname )
1927 struct strarray ret = empty_strarray;
1928 const char *ext, *p;
1929 char *name, *first, *second;
1930 size_t len = 0;
1932 strarray_add( &ret, libname );
1934 for (p = libname; (p = strchr( p, '.' )); p++)
1935 if ((len = strspn( p + 1, "0123456789." ))) break;
1937 if (!len) return ret;
1938 ext = p + 1 + len;
1939 if (*ext && ext[-1] == '.') ext--;
1941 /* keep only the first group of digits */
1942 name = xstrdup( libname );
1943 first = name + (p - libname);
1944 if ((second = strchr( first + 1, '.' )))
1946 strcpy( second, ext );
1947 strarray_add( &ret, xstrdup( name ));
1949 /* now remove all digits */
1950 strcpy( first, ext );
1951 strarray_add( &ret, name );
1952 return ret;
1956 /*******************************************************************
1957 * output_install_rules
1959 * Rules are stored as a (file,dest) pair of values.
1960 * The first char of dest indicates the type of install.
1962 static struct strarray output_install_rules( const struct makefile *make, struct strarray files,
1963 const char *target, struct strarray *phony_targets )
1965 unsigned int i;
1966 char *install_sh;
1967 struct strarray uninstall = empty_strarray;
1968 struct strarray targets = empty_strarray;
1970 if (!files.count) return uninstall;
1972 for (i = 0; i < files.count; i += 2)
1973 if (strchr( "dps", files.str[i + 1][0] )) /* only for files copied from object dir */
1974 strarray_add_uniq( &targets, files.str[i] );
1976 output( "install %s::", target );
1977 output_filenames_obj_dir( make, targets );
1978 output( "\n" );
1980 install_sh = top_dir_path( make, "tools/install-sh" );
1981 for (i = 0; i < files.count; i += 2)
1983 const char *file = files.str[i];
1984 const char *dest = files.str[i + 1];
1986 switch (*dest)
1988 case 'd': /* data file */
1989 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s $(DESTDIR)%s\n",
1990 install_sh, obj_dir_path( make, file ), dest + 1 );
1991 break;
1992 case 'D': /* data file in source dir */
1993 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s $(DESTDIR)%s\n",
1994 install_sh, src_dir_path( make, file ), dest + 1 );
1995 break;
1996 case 'p': /* program file */
1997 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s $(DESTDIR)%s\n",
1998 install_sh, obj_dir_path( make, file ), dest + 1 );
1999 break;
2000 case 's': /* script */
2001 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s $(DESTDIR)%s\n",
2002 install_sh, obj_dir_path( make, file ), dest + 1 );
2003 break;
2004 case 'S': /* script in source dir */
2005 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s $(DESTDIR)%s\n",
2006 install_sh, src_dir_path( make, file ), dest + 1 );
2007 break;
2008 case 'y': /* symlink */
2009 output( "\trm -f $(DESTDIR)%s && $(LN_S) %s $(DESTDIR)%s\n", dest + 1, file, dest + 1 );
2010 break;
2011 default:
2012 assert(0);
2016 for (i = 0; i < files.count; i += 2)
2017 strarray_add( &uninstall, strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 ));
2019 strarray_add_uniq( phony_targets, "install" );
2020 strarray_add_uniq( phony_targets, target );
2021 return uninstall;
2025 /*******************************************************************
2026 * output_sources
2028 static struct strarray output_sources( const struct makefile *make )
2030 struct incl_file *source;
2031 unsigned int i, j;
2032 struct strarray object_files = empty_strarray;
2033 struct strarray crossobj_files = empty_strarray;
2034 struct strarray res_files = empty_strarray;
2035 struct strarray clean_files = empty_strarray;
2036 struct strarray uninstall_files = empty_strarray;
2037 struct strarray po_files = empty_strarray;
2038 struct strarray mo_files = empty_strarray;
2039 struct strarray mc_files = empty_strarray;
2040 struct strarray ok_files = empty_strarray;
2041 struct strarray in_files = empty_strarray;
2042 struct strarray dlldata_files = empty_strarray;
2043 struct strarray c2man_files = empty_strarray;
2044 struct strarray implib_objs = empty_strarray;
2045 struct strarray includes = empty_strarray;
2046 struct strarray subdirs = empty_strarray;
2047 struct strarray phony_targets = empty_strarray;
2048 struct strarray all_targets = empty_strarray;
2049 struct strarray install_rules[NB_INSTALL_RULES];
2050 char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
2051 char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
2053 for (i = 0; i < NB_INSTALL_RULES; i++) install_rules[i] = empty_strarray;
2055 for (i = 0; i < linguas.count; i++)
2056 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
2058 strarray_add( &phony_targets, "all" );
2059 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
2060 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
2061 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
2062 strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
2063 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
2064 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
2065 for (i = 0; i < make->include_paths.count; i++)
2066 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
2068 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
2070 struct strarray dependencies = empty_strarray;
2071 struct strarray extradefs;
2072 char *obj = xstrdup( source->name );
2073 char *ext = get_extension( obj );
2075 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
2076 *ext++ = 0;
2078 if (make->src_dir && strchr( obj, '/' ))
2080 char *subdir = base_dir_path( make, obj );
2081 *strrchr( subdir, '/' ) = 0;
2082 strarray_add_uniq( &subdirs, subdir );
2085 extradefs = get_expanded_make_var_array( make, file_local_var( obj, "EXTRADEFS" ));
2086 get_dependencies( &dependencies, source, source );
2088 if (!strcmp( ext, "y" )) /* yacc file */
2090 /* add source file dependency for parallel makes */
2091 char *header = strmake( "%s.tab.h", obj );
2093 if (find_include_file( make, header ))
2095 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2096 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
2097 obj, obj_dir_path( make, obj ), source->filename );
2098 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2099 source->filename, obj_dir_path( make, header ));
2100 strarray_add( &clean_files, header );
2102 else output( "%s.tab.c: %s\n", obj, source->filename );
2104 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
2106 else if (!strcmp( ext, "x" )) /* template file */
2108 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2109 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2110 output( "\t%s%s -H -o $@ %s\n",
2111 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2112 if (source->file->flags & FLAG_INSTALL)
2114 strarray_add( &install_rules[INSTALL_DEV], source->name );
2115 strarray_add( &install_rules[INSTALL_DEV],
2116 strmake( "D$(includedir)/%s", get_include_install_path( source->name ) ));
2117 strarray_add( &install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2118 strarray_add( &install_rules[INSTALL_DEV],
2119 strmake( "d$(includedir)/%s.h", get_include_install_path( obj ) ));
2122 else if (!strcmp( ext, "l" )) /* lex file */
2124 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2125 output( "\t$(FLEX) -o$@ %s\n", source->filename );
2127 else if (!strcmp( ext, "rc" )) /* resource file */
2129 strarray_add( &res_files, strmake( "%s.res", obj ));
2130 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
2131 tools_path( make, "wrc" ), source->filename );
2132 output( "\t%s -o $@", tools_path( make, "wrc" ) );
2133 if (make->is_win16) output_filename( "-m16" );
2134 else output_filenames( target_flags );
2135 output_filename( "--nostdinc" );
2136 output_filenames( includes );
2137 output_filenames( make->define_args );
2138 output_filenames( extradefs );
2139 if (mo_files.count && (source->file->flags & FLAG_RC_PO))
2141 strarray_add( &po_files, source->filename );
2142 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
2143 output_filename( source->filename );
2144 output( "\n" );
2145 output( "%s.res:", obj_dir_path( make, obj ));
2146 output_filenames( mo_files );
2147 output( "\n" );
2148 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
2150 else
2152 output_filename( source->filename );
2153 output( "\n" );
2155 output( "%s.res:", obj_dir_path( make, obj ));
2156 output_filenames( dependencies );
2157 output( "\n" );
2159 else if (!strcmp( ext, "mc" )) /* message file */
2161 strarray_add( &res_files, strmake( "%s.res", obj ));
2162 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
2163 tools_path( make, "wmc" ), source->filename );
2164 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
2165 if (mo_files.count)
2167 strarray_add( &mc_files, source->filename );
2168 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
2169 output( "\n" );
2170 output( "%s.res:", obj_dir_path( make, obj ));
2171 output_filenames( mo_files );
2172 output( "\n" );
2173 output( "%s ", obj_dir_path( make, "msg.pot" ));
2175 else output( "\n" );
2176 output( "%s.res:", obj_dir_path( make, obj ));
2177 output_filenames( dependencies );
2178 output( "\n" );
2180 else if (!strcmp( ext, "idl" )) /* IDL file */
2182 struct strarray targets = empty_strarray;
2183 char *dest;
2185 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2186 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2188 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2190 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2191 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2192 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
2193 strarray_add( &targets, dest );
2195 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
2196 if (source->file->flags & FLAG_INSTALL)
2198 strarray_add( &install_rules[INSTALL_DEV], xstrdup( source->name ));
2199 strarray_add( &install_rules[INSTALL_DEV],
2200 strmake( "D$(includedir)/%s.idl", get_include_install_path( obj ) ));
2201 if (source->file->flags & FLAG_IDL_HEADER)
2203 strarray_add( &install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2204 strarray_add( &install_rules[INSTALL_DEV],
2205 strmake( "d$(includedir)/%s.h", get_include_install_path( obj ) ));
2208 if (!targets.count) continue;
2209 output_filenames_obj_dir( make, targets );
2210 output( ": %s\n", tools_path( make, "widl" ));
2211 output( "\t%s -o $@", tools_path( make, "widl" ) );
2212 output_filenames( target_flags );
2213 output_filenames( includes );
2214 output_filenames( make->define_args );
2215 output_filenames( extradefs );
2216 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2217 output_filename( source->filename );
2218 output( "\n" );
2219 output_filenames_obj_dir( make, targets );
2220 output( ": %s", source->filename );
2221 output_filenames( dependencies );
2222 output( "\n" );
2224 else if (!strcmp( ext, "in" )) /* .in file or man page */
2226 if (strendswith( obj, ".man" ) && source->file->args)
2228 struct strarray symlinks;
2229 char *dir, *dest = replace_extension( obj, ".man", "" );
2230 char *lang = strchr( dest, '.' );
2231 char *section = source->file->args;
2232 if (lang)
2234 *lang++ = 0;
2235 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2237 else dir = strmake( "$(mandir)/man%s", section );
2238 add_install_rule( make, install_rules, dest, xstrdup(obj),
2239 strmake( "d%s/%s.%s", dir, dest, section ));
2240 symlinks = get_expanded_make_var_array( make, file_local_var( dest, "SYMLINKS" ));
2241 for (i = 0; i < symlinks.count; i++)
2242 add_install_rule( make, install_rules, symlinks.str[i],
2243 strmake( "%s.%s", dest, section ),
2244 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2245 free( dest );
2246 free( dir );
2248 strarray_add( &in_files, xstrdup(obj) );
2249 strarray_add( &all_targets, xstrdup(obj) );
2250 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2251 output( "\t$(SED_CMD) %s >$@ || (rm -f $@ && false)\n", source->filename );
2252 output( "%s:", obj_dir_path( make, obj ));
2253 output_filenames( dependencies );
2254 output( "\n" );
2255 add_install_rule( make, install_rules, obj, xstrdup( obj ),
2256 strmake( "d$(datadir)/wine/%s", obj ));
2258 else if (!strcmp( ext, "sfd" )) /* font file */
2260 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
2261 if (fontforge && !make->src_dir)
2263 output( "%s: %s\n", ttf_file, source->filename );
2264 output( "\t%s -script %s %s $@\n",
2265 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
2266 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
2268 if (source->file->flags & FLAG_INSTALL)
2270 strarray_add( &install_rules[INSTALL_LIB], strmake( "%s.ttf", obj ));
2271 strarray_add( &install_rules[INSTALL_LIB], strmake( "D$(fontdir)/%s.ttf", obj ));
2273 if (source->file->flags & FLAG_SFD_FONTS)
2275 struct strarray *array = source->file->args;
2277 for (i = 0; i < array->count; i++)
2279 char *font = strtok( xstrdup(array->str[i]), " \t" );
2280 char *args = strtok( NULL, "" );
2282 strarray_add( &all_targets, xstrdup( font ));
2283 output( "%s: %s %s\n", obj_dir_path( make, font ),
2284 tools_path( make, "sfnt2fon" ), ttf_file );
2285 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2286 strarray_add( &install_rules[INSTALL_LIB], xstrdup(font) );
2287 strarray_add( &install_rules[INSTALL_LIB], strmake( "d$(fontdir)/%s", font ));
2291 else if (!strcmp( ext, "svg" )) /* svg file */
2293 if (convert && rsvg && icotool && !make->src_dir)
2295 output( "%s.ico %s.bmp: %s\n",
2296 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
2297 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2298 top_dir_path( make, "tools/buildimage" ), source->filename );
2301 else if (!strcmp( ext, "res" ))
2303 strarray_add( &res_files, source->name );
2305 else if (!strcmp( ext, "tlb" ))
2307 strarray_add( &all_targets, source->name );
2309 else if (!strcmp( ext, "h" ) || !strcmp( ext, "rh" ) || !strcmp( ext, "inl" )) /* header file */
2311 if (source->file->flags & FLAG_GENERATED)
2313 strarray_add( &all_targets, source->name );
2315 else
2317 strarray_add( &install_rules[INSTALL_DEV], source->name );
2318 strarray_add( &install_rules[INSTALL_DEV],
2319 strmake( "D$(includedir)/%s", get_include_install_path( source->name ) ));
2322 else
2324 int need_cross = make->testdll ||
2325 (source->file->flags & FLAG_C_IMPLIB) ||
2326 (make->module && make->staticlib);
2328 if ((source->file->flags & FLAG_GENERATED) &&
2329 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2330 strarray_add( &clean_files, source->filename );
2331 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
2332 strarray_add( &object_files, strmake( "%s.o", obj ));
2333 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2334 output( "\t$(CC) -c -o $@ %s", source->filename );
2335 output_filenames( includes );
2336 output_filenames( make->define_args );
2337 output_filenames( extradefs );
2338 if (make->module || make->staticlib || make->testdll)
2340 output_filenames( dll_flags );
2341 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2343 output_filenames( extra_cflags );
2344 output_filenames( cpp_flags );
2345 output_filename( "$(CFLAGS)" );
2346 output( "\n" );
2347 if (crosstarget && need_cross)
2349 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
2350 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2351 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2352 output_filenames( includes );
2353 output_filenames( make->define_args );
2354 output_filenames( extradefs );
2355 output_filename( "-DWINE_CROSSTEST" );
2356 output_filenames( cpp_flags );
2357 output_filename( "$(CFLAGS)" );
2358 output( "\n" );
2360 if (make->testdll && !strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2362 strarray_add( &ok_files, strmake( "%s.ok", obj ));
2363 output( "%s.ok:\n", obj_dir_path( make, obj ));
2364 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2365 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
2366 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
2368 if (!strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2369 strarray_add( &c2man_files, source->filename );
2370 output( "%s.o", obj_dir_path( make, obj ));
2371 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2372 output( ":" );
2373 output_filenames( dependencies );
2374 output( "\n" );
2376 free( obj );
2379 /* rules for files that depend on multiple sources */
2381 if (po_files.count)
2383 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
2384 output_filenames( po_files );
2385 output( "\n" );
2386 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
2387 if (make->is_win16) output_filename( "-m16" );
2388 else output_filenames( target_flags );
2389 output_filename( "--nostdinc" );
2390 output_filenames( includes );
2391 output_filenames( make->define_args );
2392 output_filenames( po_files );
2393 output( "\n" );
2394 strarray_add( &clean_files, "rsrc.pot" );
2397 if (mc_files.count)
2399 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
2400 output_filenames( mc_files );
2401 output( "\n" );
2402 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
2403 output_filenames( mc_files );
2404 output( "\n" );
2405 strarray_add( &clean_files, "msg.pot" );
2408 if (dlldata_files.count)
2410 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
2411 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
2412 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
2413 output_filenames( dlldata_files );
2414 output( "\n" );
2417 if (make->module && !make->staticlib)
2419 struct strarray all_libs = empty_strarray;
2420 char *module_path = obj_dir_path( make, make->module );
2421 char *spec_file = NULL;
2423 if (!make->appmode.count)
2424 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2425 for (i = 0; i < make->delayimports.count; i++)
2426 strarray_add( &all_libs, strmake( "-l%s", make->delayimports.str[i] ));
2427 for (i = 0; i < make->imports.count; i++)
2428 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2429 for (i = 0; i < make->delayimports.count; i++)
2430 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2431 strarray_add( &all_libs, "-lwine" );
2432 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2433 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2434 strarray_addall( &all_libs, libs );
2436 if (*dll_ext)
2438 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
2439 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
2440 add_install_rule( make, install_rules, make->module, strmake( "%s%s", make->module, dll_ext ),
2441 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
2442 add_install_rule( make, install_rules, make->module, strmake( "%s.fake", make->module ),
2443 strmake( "d$(fakedlldir)/%s", make->module ));
2444 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2446 else
2448 strarray_add( &all_targets, make->module );
2449 add_install_rule( make, install_rules, make->module, make->module,
2450 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
2451 output( "%s:", module_path );
2453 if (spec_file) output_filename( spec_file );
2454 output_filenames_obj_dir( make, object_files );
2455 output_filenames_obj_dir( make, res_files );
2456 output( "\n" );
2457 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2458 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2459 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2460 output_filenames( target_flags );
2461 output_filenames( unwind_flags );
2462 if (spec_file)
2464 output( " -shared %s", spec_file );
2465 output_filenames( make->extradllflags );
2467 else output_filenames( make->appmode );
2468 output_filenames_obj_dir( make, object_files );
2469 output_filenames_obj_dir( make, res_files );
2470 output_filenames( all_libs );
2471 output_filename( "$(LDFLAGS)" );
2472 output( "\n" );
2474 if (spec_file && make->importlib)
2476 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2477 if (*dll_ext)
2479 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
2480 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2481 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2482 output_filenames( target_flags );
2483 if (make->is_win16) output_filename( "-m16" );
2484 output( "\n" );
2485 add_install_rule( make, install_rules, make->importlib,
2486 strmake( "lib%s.def", make->importlib ),
2487 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
2488 if (implib_objs.count)
2490 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
2491 output( "%s.def.a:", importlib_path );
2492 output_filenames_obj_dir( make, implib_objs );
2493 output( "\n" );
2494 output( "\trm -f $@\n" );
2495 output( "\t$(AR) $(ARFLAGS) $@" );
2496 output_filenames_obj_dir( make, implib_objs );
2497 output( "\n" );
2498 output( "\t$(RANLIB) $@\n" );
2499 add_install_rule( make, install_rules, make->importlib,
2500 strmake( "lib%s.def.a", make->importlib ),
2501 strmake( "d$(dlldir)/lib%s.def.a", make->importlib ));
2504 else
2506 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
2507 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2508 output_filenames_obj_dir( make, implib_objs );
2509 output( "\n" );
2510 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2511 output_filenames( target_flags );
2512 output_filenames_obj_dir( make, implib_objs );
2513 output( "\n" );
2514 add_install_rule( make, install_rules, make->importlib,
2515 strmake( "lib%s.a", make->importlib ),
2516 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
2518 if (crosstarget && !make->is_win16)
2520 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2521 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2522 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2523 output_filenames_obj_dir( make, cross_files );
2524 output( "\n" );
2525 output( "\t%s -b %s -w --implib -o $@ --export %s",
2526 tools_path( make, "winebuild" ), crosstarget, spec_file );
2527 output_filenames_obj_dir( make, cross_files );
2528 output( "\n" );
2532 if (spec_file)
2534 if (c2man_files.count)
2536 output( "manpages::\n" );
2537 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2538 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2539 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2540 output_filename( strmake( "-o %s/man%s",
2541 top_obj_dir_path( make, "documentation" ), man_ext ));
2542 output_filenames( c2man_files );
2543 output( "\n" );
2544 output( "htmlpages::\n" );
2545 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2546 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2547 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2548 output_filename( strmake( "-o %s",
2549 top_obj_dir_path( make, "documentation/html" )));
2550 output_filenames( c2man_files );
2551 output( "\n" );
2552 output( "sgmlpages::\n" );
2553 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2554 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2555 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2556 output_filename( strmake( "-o %s",
2557 top_obj_dir_path( make, "documentation/api-guide" )));
2558 output_filenames( c2man_files );
2559 output( "\n" );
2560 output( "xmlpages::\n" );
2561 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2562 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2563 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2564 output_filename( strmake( "-o %s",
2565 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2566 output_filenames( c2man_files );
2567 output( "\n" );
2568 strarray_add( &phony_targets, "manpages" );
2569 strarray_add( &phony_targets, "htmlpages" );
2570 strarray_add( &phony_targets, "sgmlpages" );
2571 strarray_add( &phony_targets, "xmlpages" );
2573 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2575 else if (*dll_ext)
2577 char *binary = replace_extension( make->module, ".exe", "" );
2578 add_install_rule( make, install_rules, binary, tools_dir_path( make, "wineapploader" ),
2579 strmake( "s$(bindir)/%s", binary ));
2583 if (make->staticlib)
2585 strarray_add( &all_targets, make->staticlib );
2586 output( "%s:", obj_dir_path( make, make->staticlib ));
2587 output_filenames_obj_dir( make, object_files );
2588 output( "\n\trm -f $@\n" );
2589 output( "\t$(AR) $(ARFLAGS) $@" );
2590 output_filenames_obj_dir( make, object_files );
2591 output( "\n\t$(RANLIB) $@\n" );
2592 if (crosstarget && make->module)
2594 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2596 strarray_add( &all_targets, name );
2597 output( "%s:", obj_dir_path( make, name ));
2598 output_filenames_obj_dir( make, crossobj_files );
2599 output( "\n\trm -f $@\n" );
2600 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2601 output_filenames_obj_dir( make, crossobj_files );
2602 output( "\n\t%s-ranlib $@\n", crosstarget );
2606 if (make->sharedlib)
2608 char *basename, *p;
2609 struct strarray names = get_shared_lib_names( make->sharedlib );
2610 struct strarray all_libs = empty_strarray;
2612 basename = xstrdup( make->sharedlib );
2613 if ((p = strchr( basename, '.' ))) *p = 0;
2615 strarray_addall( &all_libs, get_expanded_make_var_array( make,
2616 file_local_var( basename, "LDFLAGS" )));
2617 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2618 strarray_addall( &all_libs, libs );
2620 output( "%s:", obj_dir_path( make, make->sharedlib ));
2621 output_filenames_obj_dir( make, object_files );
2622 output_filenames( get_local_dependencies( make, basename, in_files ));
2623 output( "\n" );
2624 output( "\t$(CC) -o $@" );
2625 output_filenames_obj_dir( make, object_files );
2626 output_filenames( all_libs );
2627 output_filename( "$(LDFLAGS)" );
2628 output( "\n" );
2629 add_install_rule( make, install_rules, make->sharedlib, make->sharedlib,
2630 strmake( "p$(libdir)/%s", make->sharedlib ));
2631 for (i = 1; i < names.count; i++)
2633 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
2634 output( "\trm -f $@ && $(LN_S) %s $@\n", names.str[i-1] );
2635 add_install_rule( make, install_rules, names.str[i], names.str[i-1],
2636 strmake( "y$(libdir)/%s", names.str[i] ));
2638 strarray_addall( &all_targets, names );
2641 if (make->importlib && !make->module) /* stand-alone import lib (for libwine) */
2643 char *def_file = replace_extension( make->importlib, ".a", ".def" );
2645 if (!strncmp( def_file, "lib", 3 )) def_file += 3;
2646 output( "%s: %s\n", obj_dir_path( make, make->importlib ), src_dir_path( make, def_file ));
2647 output( "\t%s -l $@ -d %s\n", dlltool, src_dir_path( make, def_file ));
2648 add_install_rule( make, install_rules, make->importlib, make->importlib,
2649 strmake( "d$(libdir)/%s", make->importlib ));
2650 strarray_add( &all_targets, make->importlib );
2653 if (make->testdll)
2655 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2656 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2657 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2658 struct strarray all_libs = empty_strarray;
2660 for (i = 0; i < make->imports.count; i++)
2661 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2662 strarray_addall( &all_libs, libs );
2664 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2665 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2666 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2667 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2668 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2669 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2670 output_filenames( target_flags );
2671 output_filenames( unwind_flags );
2672 output_filenames( make->appmode );
2673 output_filenames_obj_dir( make, object_files );
2674 output_filenames_obj_dir( make, res_files );
2675 output_filenames( all_libs );
2676 output_filename( "$(LDFLAGS)" );
2677 output( "\n" );
2678 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2679 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2680 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2681 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2682 output_filenames( target_flags );
2683 output_filenames( unwind_flags );
2684 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2685 output_filenames( make->appmode );
2686 output_filenames_obj_dir( make, object_files );
2687 output_filenames_obj_dir( make, res_files );
2688 output_filenames( all_libs );
2689 output_filename( "$(LDFLAGS)" );
2690 output( "\n" );
2691 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2692 obj_dir_path( make, stripped ), dll_ext );
2693 output_filenames_obj_dir( make, object_files );
2694 output_filenames_obj_dir( make, res_files );
2695 output( "\n" );
2697 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2698 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2699 obj_dir_path( make, stripped ), dll_ext );
2700 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2701 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2703 if (crosstarget)
2705 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2707 strarray_add( &clean_files, crosstest );
2708 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2709 output( "%s:", obj_dir_path( make, crosstest ));
2710 output_filenames_obj_dir( make, crossobj_files );
2711 output_filenames_obj_dir( make, res_files );
2712 output( "\n" );
2713 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2714 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2715 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2716 output_filename( "--lib-suffix=.cross.a" );
2717 output_filenames_obj_dir( make, crossobj_files );
2718 output_filenames_obj_dir( make, res_files );
2719 output_filenames( all_libs );
2720 output_filename( "$(LDFLAGS)" );
2721 output( "\n" );
2722 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2723 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2726 output_filenames_obj_dir( make, ok_files );
2727 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2728 output( "check test:" );
2729 output_filenames_obj_dir( make, ok_files );
2730 output( "\n" );
2731 output( "testclean::\n" );
2732 output( "\trm -f" );
2733 output_filenames_obj_dir( make, ok_files );
2734 output( "\n" );
2735 strarray_addall( &clean_files, ok_files );
2736 strarray_add( &phony_targets, "check" );
2737 strarray_add( &phony_targets, "test" );
2738 strarray_add( &phony_targets, "testclean" );
2741 for (i = 0; i < make->programs.count; i++)
2743 char *program_installed = NULL;
2744 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
2745 struct strarray all_libs = empty_strarray;
2746 struct strarray deps = get_local_dependencies( make, make->programs.str[i], in_files );
2747 struct strarray objs = get_expanded_make_var_array( make,
2748 file_local_var( make->programs.str[i], "OBJS" ));
2749 struct strarray symlinks = get_expanded_make_var_array( make,
2750 file_local_var( make->programs.str[i], "SYMLINKS" ));
2752 if (!objs.count) objs = object_files;
2753 output( "%s:", obj_dir_path( make, program ) );
2754 output_filenames_obj_dir( make, objs );
2755 output_filenames( deps );
2756 output( "\n" );
2757 output( "\t$(CC) -o $@" );
2758 output_filenames_obj_dir( make, objs );
2759 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2760 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2761 strarray_addall( &all_libs, libs );
2762 strarray_addall( &all_libs, get_expanded_make_var_array( make,
2763 file_local_var( make->programs.str[i], "LDFLAGS" )));
2765 if (strarray_exists( &all_libs, "-lwine" ))
2767 strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
2768 if (ldrpath_local && ldrpath_install)
2770 program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
2771 output_filename( ldrpath_local );
2772 output_filenames( all_libs );
2773 output_filename( "$(LDFLAGS)" );
2774 output( "\n" );
2775 output( "%s:", obj_dir_path( make, program_installed ) );
2776 output_filenames_obj_dir( make, objs );
2777 output_filenames( deps );
2778 output( "\n" );
2779 output( "\t$(CC) -o $@" );
2780 output_filenames_obj_dir( make, objs );
2781 output_filename( ldrpath_install );
2782 strarray_add( &all_targets, program_installed );
2786 output_filenames( all_libs );
2787 output_filename( "$(LDFLAGS)" );
2788 output( "\n" );
2789 strarray_add( &all_targets, program );
2791 if (symlinks.count)
2793 output_filenames_obj_dir( make, symlinks );
2794 output( ": %s\n", obj_dir_path( make, program ));
2795 output( "\trm -f $@ && $(LN_S) %s $@\n", obj_dir_path( make, program ));
2796 strarray_addall( &all_targets, symlinks );
2799 add_install_rule( make, install_rules, program, program_installed ? program_installed : program,
2800 strmake( "p$(bindir)/%s", program ));
2801 for (j = 0; j < symlinks.count; j++)
2802 add_install_rule( make, install_rules, symlinks.str[j], program,
2803 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
2806 for (i = 0; i < make->scripts.count; i++)
2807 add_install_rule( make, install_rules, make->scripts.str[i], make->scripts.str[i],
2808 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
2810 if (all_targets.count)
2812 output( "all:" );
2813 output_filenames_obj_dir( make, all_targets );
2814 output( "\n" );
2817 strarray_addall( &uninstall_files, output_install_rules( make, install_rules[INSTALL_LIB],
2818 "install-lib", &phony_targets ));
2819 strarray_addall( &uninstall_files, output_install_rules( make, install_rules[INSTALL_DEV],
2820 "install-dev", &phony_targets ));
2821 if (uninstall_files.count)
2823 output( "uninstall::\n" );
2824 output( "\trm -f" );
2825 output_filenames( uninstall_files );
2826 output( "\n" );
2827 strarray_add_uniq( &phony_targets, "uninstall" );
2830 strarray_addall( &clean_files, object_files );
2831 strarray_addall( &clean_files, crossobj_files );
2832 strarray_addall( &clean_files, res_files );
2833 strarray_addall( &clean_files, all_targets );
2834 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2836 if (clean_files.count)
2838 output( "%s::\n", obj_dir_path( make, "clean" ));
2839 output( "\trm -f" );
2840 output_filenames_obj_dir( make, clean_files );
2841 output( "\n" );
2842 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2843 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2846 if (make->subdirs.count)
2848 struct strarray makefile_deps = empty_strarray;
2849 struct strarray distclean_files = empty_strarray;
2851 for (i = 0; i < make->subdirs.count; i++)
2853 strarray_add( &makefile_deps, top_dir_path( make, base_dir_path( make->submakes[i],
2854 strmake ( "%s.in", output_makefile_name ))));
2855 strarray_add( &distclean_files, base_dir_path( make->submakes[i], output_makefile_name ));
2856 if (!make->src_dir)
2857 strarray_add( &distclean_files, base_dir_path( make->submakes[i], ".gitignore" ));
2858 if (make->submakes[i]->testdll)
2859 strarray_add( &distclean_files, base_dir_path( make->submakes[i], "testlist.c" ));
2861 output( "Makefile:" );
2862 output_filenames( makefile_deps );
2863 output( "\n" );
2864 output( "distclean::\n");
2865 output( "\trm -f" );
2866 output_filenames( distclean_files );
2867 output( "\n" );
2868 strarray_add( &phony_targets, "distclean" );
2871 if (phony_targets.count)
2873 output( ".PHONY:" );
2874 output_filenames( phony_targets );
2875 output( "\n" );
2878 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2880 return clean_files;
2884 /*******************************************************************
2885 * create_temp_file
2887 static FILE *create_temp_file( const char *orig )
2889 char *name = xmalloc( strlen(orig) + 13 );
2890 unsigned int i, id = getpid();
2891 int fd;
2892 FILE *ret = NULL;
2894 for (i = 0; i < 100; i++)
2896 sprintf( name, "%s.tmp%08x", orig, id );
2897 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2899 ret = fdopen( fd, "w" );
2900 break;
2902 if (errno != EEXIST) break;
2903 id += 7777;
2905 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2906 temp_file_name = name;
2907 return ret;
2911 /*******************************************************************
2912 * rename_temp_file
2914 static void rename_temp_file( const char *dest )
2916 int ret = rename( temp_file_name, dest );
2917 if (ret == -1 && errno == EEXIST)
2919 /* rename doesn't overwrite on windows */
2920 unlink( dest );
2921 ret = rename( temp_file_name, dest );
2923 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2924 temp_file_name = NULL;
2928 /*******************************************************************
2929 * are_files_identical
2931 static int are_files_identical( FILE *file1, FILE *file2 )
2933 for (;;)
2935 char buffer1[8192], buffer2[8192];
2936 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2937 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2938 if (size1 != size2) return 0;
2939 if (!size1) return feof( file1 ) && feof( file2 );
2940 if (memcmp( buffer1, buffer2, size1 )) return 0;
2945 /*******************************************************************
2946 * rename_temp_file_if_changed
2948 static void rename_temp_file_if_changed( const char *dest )
2950 FILE *file1, *file2;
2951 int do_rename = 1;
2953 if ((file1 = fopen( dest, "r" )))
2955 if ((file2 = fopen( temp_file_name, "r" )))
2957 do_rename = !are_files_identical( file1, file2 );
2958 fclose( file2 );
2960 fclose( file1 );
2962 if (!do_rename)
2964 unlink( temp_file_name );
2965 temp_file_name = NULL;
2967 else rename_temp_file( dest );
2971 /*******************************************************************
2972 * output_testlist
2974 static void output_testlist( const struct makefile *make )
2976 const char *dest = base_dir_path( make, "testlist.c" );
2977 struct strarray files = empty_strarray;
2978 struct incl_file *source;
2979 unsigned int i;
2981 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
2983 if (source->file->flags & FLAG_GENERATED) continue;
2984 if (!strendswith( source->name, ".c" )) continue;
2985 strarray_add( &files, replace_extension( source->name, ".c", "" ));
2988 output_file = create_temp_file( dest );
2990 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2991 output( "#define WIN32_LEAN_AND_MEAN\n" );
2992 output( "#include <windows.h>\n\n" );
2993 output( "#define STANDALONE\n" );
2994 output( "#include \"wine/test.h\"\n\n" );
2996 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2997 output( "\n" );
2998 output( "const struct test winetest_testlist[] =\n" );
2999 output( "{\n" );
3000 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
3001 output( " { 0, 0 }\n" );
3002 output( "};\n" );
3004 if (fclose( output_file )) fatal_perror( "write" );
3005 output_file = NULL;
3006 rename_temp_file_if_changed( dest );
3010 /*******************************************************************
3011 * output_gitignore
3013 static void output_gitignore( const char *dest, struct strarray files )
3015 int i;
3017 output_file = create_temp_file( dest );
3019 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3020 for (i = 0; i < files.count; i++)
3022 if (!strchr( files.str[i], '/' )) output( "/" );
3023 output( "%s\n", files.str[i] );
3026 if (fclose( output_file )) fatal_perror( "write" );
3027 output_file = NULL;
3028 rename_temp_file( dest );
3032 /*******************************************************************
3033 * output_top_variables
3035 static void output_top_variables( const struct makefile *make )
3037 unsigned int i;
3038 struct strarray *vars = &top_makefile->vars;
3040 if (!make->base_dir) return; /* don't output variables in the top makefile */
3042 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
3043 output( "all:\n\n" );
3044 for (i = 0; i < vars->count; i += 2)
3046 if (!strcmp( vars->str[i], "SUBDIRS" )) continue; /* not inherited */
3047 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
3049 output( "\n" );
3053 /*******************************************************************
3054 * output_dependencies
3056 static void output_dependencies( const struct makefile *make )
3058 static const char separator[] = "### Dependencies";
3059 struct strarray targets, ignore_files = empty_strarray;
3060 char buffer[1024];
3061 FILE *src_file;
3062 int found = 0;
3064 output_file_name = base_dir_path( make, output_makefile_name );
3065 output_file = create_temp_file( output_file_name );
3066 output_top_variables( make );
3068 /* copy the contents of the source makefile */
3069 src_file = open_input_makefile( make );
3070 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
3072 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
3073 found = !strncmp( buffer, separator, strlen(separator) );
3075 if (fclose( src_file )) fatal_perror( "close" );
3076 input_file_name = NULL;
3078 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
3079 targets = output_sources( make );
3081 fclose( output_file );
3082 output_file = NULL;
3083 rename_temp_file( output_file_name );
3085 strarray_add( &ignore_files, ".gitignore" );
3086 strarray_add( &ignore_files, "Makefile" );
3087 if (make->testdll)
3089 output_testlist( make );
3090 strarray_add( &ignore_files, "testlist.c" );
3092 strarray_addall( &ignore_files, targets );
3093 if (!make->src_dir && make->base_dir)
3094 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
3096 output_file_name = NULL;
3100 /*******************************************************************
3101 * load_sources
3103 static void load_sources( struct makefile *make )
3105 static const char *source_vars[] =
3107 "C_SRCS",
3108 "OBJC_SRCS",
3109 "RC_SRCS",
3110 "MC_SRCS",
3111 "IDL_SRCS",
3112 "BISON_SRCS",
3113 "LEX_SRCS",
3114 "HEADER_SRCS",
3115 "XTEMPLATE_SRCS",
3116 "SVG_SRCS",
3117 "FONT_SRCS",
3118 "IN_SRCS",
3119 "MANPAGES",
3120 NULL
3122 const char **var;
3123 unsigned int i;
3124 struct strarray value;
3125 struct incl_file *file;
3127 if (root_src_dir)
3129 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
3130 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
3132 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
3133 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
3134 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
3136 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
3137 make->module = get_expanded_make_variable( make, "MODULE" );
3138 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
3139 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
3140 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
3141 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
3143 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
3144 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
3145 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
3146 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
3147 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
3148 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
3149 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
3150 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
3152 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
3154 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
3155 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
3157 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
3158 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 ) ||
3159 !strcmp( make->imports.str[i], "ucrtbase" );
3161 if (make->module && !make->install_lib.count) strarray_add( &make->install_lib, make->module );
3163 make->include_paths = empty_strarray;
3164 make->define_args = empty_strarray;
3165 strarray_add( &make->define_args, "-D__WINESRC__" );
3167 value = get_expanded_make_var_array( make, "EXTRAINCL" );
3168 for (i = 0; i < value.count; i++)
3169 if (!strncmp( value.str[i], "-I", 2 ))
3170 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
3171 else
3172 strarray_add_uniq( &make->define_args, value.str[i] );
3173 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
3175 list_init( &make->sources );
3176 list_init( &make->includes );
3178 /* FIXME: target dir has to exist to allow locating srcdir-relative include files */
3179 if (make->base_dir) create_dir( make->base_dir );
3181 for (var = source_vars; *var; var++)
3183 value = get_expanded_make_var_array( make, *var );
3184 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
3187 add_generated_sources( make );
3189 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
3190 for (i = 0; i < value.count; i++)
3192 /* default to .c for unknown extra object files */
3193 if (strendswith( value.str[i], ".o" ))
3194 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
3195 else
3196 add_generated_source( make, value.str[i], NULL );
3199 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
3203 /*******************************************************************
3204 * parse_makeflags
3206 static void parse_makeflags( const char *flags )
3208 const char *p = flags;
3209 char *var, *buffer = xmalloc( strlen(flags) + 1 );
3211 while (*p)
3213 while (isspace(*p)) p++;
3214 var = buffer;
3215 while (*p && !isspace(*p))
3217 if (*p == '\\' && p[1]) p++;
3218 *var++ = *p++;
3220 *var = 0;
3221 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
3226 /*******************************************************************
3227 * parse_option
3229 static int parse_option( const char *opt )
3231 if (opt[0] != '-')
3233 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
3234 return 0;
3236 switch(opt[1])
3238 case 'f':
3239 if (opt[2]) output_makefile_name = opt + 2;
3240 break;
3241 case 'R':
3242 relative_dir_mode = 1;
3243 break;
3244 default:
3245 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
3246 exit(1);
3248 return 1;
3252 /*******************************************************************
3253 * main
3255 int main( int argc, char *argv[] )
3257 const char *makeflags = getenv( "MAKEFLAGS" );
3258 int i, j;
3260 if (makeflags) parse_makeflags( makeflags );
3262 i = 1;
3263 while (i < argc)
3265 if (parse_option( argv[i] ))
3267 for (j = i; j < argc; j++) argv[j] = argv[j+1];
3268 argc--;
3270 else i++;
3273 if (relative_dir_mode)
3275 char *relpath;
3277 if (argc != 3)
3279 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
3280 exit( 1 );
3282 relpath = get_relative_path( argv[1], argv[2] );
3283 printf( "%s\n", relpath ? relpath : "." );
3284 exit( 0 );
3287 atexit( cleanup_files );
3288 signal( SIGTERM, exit_on_signal );
3289 signal( SIGINT, exit_on_signal );
3290 #ifdef SIGHUP
3291 signal( SIGHUP, exit_on_signal );
3292 #endif
3294 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
3296 top_makefile = parse_makefile( NULL, "# End of common header" );
3298 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
3299 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
3300 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
3301 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
3302 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
3303 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
3304 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
3305 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
3307 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
3308 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
3309 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
3310 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
3311 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
3312 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
3313 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
3314 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
3315 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
3316 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
3317 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
3318 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
3320 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
3321 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
3322 if (!exe_ext) exe_ext = "";
3323 if (!tools_ext) tools_ext = "";
3324 if (!man_ext) man_ext = "3w";
3326 if (argc == 1)
3328 top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
3329 top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );
3331 for (i = 0; i < top_makefile->subdirs.count; i++)
3332 top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i], NULL );
3334 load_sources( top_makefile );
3335 for (i = 0; i < top_makefile->subdirs.count; i++)
3336 load_sources( top_makefile->submakes[i] );
3338 for (i = 0; i < top_makefile->subdirs.count; i++)
3339 output_dependencies( top_makefile->submakes[i] );
3341 output_dependencies( top_makefile );
3342 return 0;
3345 for (i = 1; i < argc; i++)
3347 struct makefile *make = parse_makefile( argv[i], NULL );
3348 load_sources( make );
3349 output_dependencies( make );
3351 return 0;