comctl32: Treeview: Set visible = TRUE when deleting first visible item.
[wine.git] / tools / makedep.c
blob9fd704bd4380b757dabd66665a4757728f0c6e80
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 *staticimplib;
177 const char *importlib;
178 int use_msvcrt;
179 int is_win16;
180 struct makefile **submakes;
183 static struct makefile *top_makefile;
185 static const char *output_makefile_name = "Makefile";
186 static const char *input_file_name;
187 static const char *output_file_name;
188 static const char *temp_file_name;
189 static int relative_dir_mode;
190 static int input_line;
191 static int output_column;
192 static FILE *output_file;
194 static const char Usage[] =
195 "Usage: makedep [options] [directories]\n"
196 "Options:\n"
197 " -R from to Compute the relative path between two directories\n"
198 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
201 #ifndef __GNUC__
202 #define __attribute__(x)
203 #endif
205 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
206 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
207 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
208 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
210 /*******************************************************************
211 * fatal_error
213 static void fatal_error( const char *msg, ... )
215 va_list valist;
216 va_start( valist, msg );
217 if (input_file_name)
219 fprintf( stderr, "%s:", input_file_name );
220 if (input_line) fprintf( stderr, "%d:", input_line );
221 fprintf( stderr, " error: " );
223 else fprintf( stderr, "makedep: error: " );
224 vfprintf( stderr, msg, valist );
225 va_end( valist );
226 exit(1);
230 /*******************************************************************
231 * fatal_perror
233 static void fatal_perror( const char *msg, ... )
235 va_list valist;
236 va_start( valist, msg );
237 if (input_file_name)
239 fprintf( stderr, "%s:", input_file_name );
240 if (input_line) fprintf( stderr, "%d:", input_line );
241 fprintf( stderr, " error: " );
243 else fprintf( stderr, "makedep: error: " );
244 vfprintf( stderr, msg, valist );
245 perror( " " );
246 va_end( valist );
247 exit(1);
251 /*******************************************************************
252 * cleanup_files
254 static void cleanup_files(void)
256 if (temp_file_name) unlink( temp_file_name );
257 if (output_file_name) unlink( output_file_name );
261 /*******************************************************************
262 * exit_on_signal
264 static void exit_on_signal( int sig )
266 exit( 1 ); /* this will call the atexit functions */
270 /*******************************************************************
271 * xmalloc
273 static void *xmalloc( size_t size )
275 void *res;
276 if (!(res = malloc (size ? size : 1)))
277 fatal_error( "Virtual memory exhausted.\n" );
278 return res;
282 /*******************************************************************
283 * xrealloc
285 static void *xrealloc (void *ptr, size_t size)
287 void *res;
288 assert( size );
289 if (!(res = realloc( ptr, size )))
290 fatal_error( "Virtual memory exhausted.\n" );
291 return res;
294 /*******************************************************************
295 * xstrdup
297 static char *xstrdup( const char *str )
299 char *res = strdup( str );
300 if (!res) fatal_error( "Virtual memory exhausted.\n" );
301 return res;
305 /*******************************************************************
306 * strmake
308 static char *strmake( const char* fmt, ... )
310 int n;
311 size_t size = 100;
312 va_list ap;
314 for (;;)
316 char *p = xmalloc (size);
317 va_start(ap, fmt);
318 n = vsnprintf (p, size, fmt, ap);
319 va_end(ap);
320 if (n == -1) size *= 2;
321 else if ((size_t)n >= size) size = n + 1;
322 else return xrealloc( p, n + 1 );
323 free(p);
328 /*******************************************************************
329 * strendswith
331 static int strendswith( const char* str, const char* end )
333 size_t l = strlen( str );
334 size_t m = strlen( end );
336 return l >= m && strcmp(str + l - m, end) == 0;
340 /*******************************************************************
341 * output
343 static void output( const char *format, ... )
345 int ret;
346 va_list valist;
348 va_start( valist, format );
349 ret = vfprintf( output_file, format, valist );
350 va_end( valist );
351 if (ret < 0) fatal_perror( "output" );
352 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
353 else output_column += ret;
357 /*******************************************************************
358 * strarray_add
360 static void strarray_add( struct strarray *array, const char *str )
362 if (array->count == array->size)
364 if (array->size) array->size *= 2;
365 else array->size = 16;
366 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
368 array->str[array->count++] = str;
372 /*******************************************************************
373 * strarray_addall
375 static void strarray_addall( struct strarray *array, struct strarray added )
377 unsigned int i;
379 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
383 /*******************************************************************
384 * strarray_exists
386 static int strarray_exists( const struct strarray *array, const char *str )
388 unsigned int i;
390 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
391 return 0;
395 /*******************************************************************
396 * strarray_add_uniq
398 static void strarray_add_uniq( struct strarray *array, const char *str )
400 if (!strarray_exists( array, str )) strarray_add( array, str );
404 /*******************************************************************
405 * strarray_get_value
407 * Find a value in a name/value pair string array.
409 static const char *strarray_get_value( const struct strarray *array, const char *name )
411 unsigned int i;
413 for (i = 0; i < array->count; i += 2)
414 if (!strcmp( array->str[i], name )) return array->str[i + 1];
415 return NULL;
419 /*******************************************************************
420 * strarray_set_value
422 * Define a value in a name/value pair string array.
424 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
426 unsigned int i;
428 /* redefining a variable replaces the previous value */
429 for (i = 0; i < array->count; i += 2)
431 if (strcmp( array->str[i], name )) continue;
432 array->str[i + 1] = value;
433 return;
435 strarray_add( array, name );
436 strarray_add( array, value );
440 /*******************************************************************
441 * output_filename
443 static void output_filename( const char *name )
445 if (output_column + strlen(name) + 1 > 100)
447 output( " \\\n" );
448 output( " " );
450 else if (output_column) output( " " );
451 output( "%s", name );
455 /*******************************************************************
456 * output_filenames
458 static void output_filenames( struct strarray array )
460 unsigned int i;
462 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
466 /*******************************************************************
467 * get_extension
469 static char *get_extension( char *filename )
471 char *ext = strrchr( filename, '.' );
472 if (ext && strchr( ext, '/' )) ext = NULL;
473 return ext;
477 /*******************************************************************
478 * replace_extension
480 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
482 char *ret;
483 size_t name_len = strlen( name );
484 size_t ext_len = strlen( old_ext );
486 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
487 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
488 memcpy( ret, name, name_len );
489 strcpy( ret + name_len, new_ext );
490 return ret;
494 /*******************************************************************
495 * replace_filename
497 static char *replace_filename( const char *path, const char *name )
499 const char *p;
500 char *ret;
501 size_t len;
503 if (!path) return xstrdup( name );
504 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
505 len = p - path + 1;
506 ret = xmalloc( len + strlen( name ) + 1 );
507 memcpy( ret, path, len );
508 strcpy( ret + len, name );
509 return ret;
513 /*******************************************************************
514 * strarray_replace_extension
516 static struct strarray strarray_replace_extension( const struct strarray *array,
517 const char *old_ext, const char *new_ext )
519 unsigned int i;
520 struct strarray ret;
522 ret.count = ret.size = array->count;
523 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
524 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
525 return ret;
529 /*******************************************************************
530 * replace_substr
532 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
534 size_t pos = start - str;
535 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
536 memcpy( ret, str, pos );
537 strcpy( ret + pos, replace );
538 strcat( ret + pos, start + len );
539 return ret;
543 /*******************************************************************
544 * get_relative_path
546 * Determine where the destination path is located relative to the 'from' path.
548 static char *get_relative_path( const char *from, const char *dest )
550 const char *start;
551 char *ret, *p;
552 unsigned int dotdots = 0;
554 /* a path of "." is equivalent to an empty path */
555 if (!strcmp( from, "." )) from = "";
557 for (;;)
559 while (*from == '/') from++;
560 while (*dest == '/') dest++;
561 start = dest; /* save start of next path element */
562 if (!*from) break;
564 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
565 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
567 /* count remaining elements in 'from' */
570 dotdots++;
571 while (*from && *from != '/') from++;
572 while (*from == '/') from++;
574 while (*from);
575 break;
578 if (!start[0] && !dotdots) return NULL; /* empty path */
580 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
581 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
583 if (start[0]) strcpy( p, start );
584 else p[-1] = 0; /* remove trailing slash */
585 return ret;
589 /*******************************************************************
590 * concat_paths
592 static char *concat_paths( const char *base, const char *path )
594 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
595 if (!path || !path[0]) return xstrdup( base );
596 if (path[0] == '/') return xstrdup( path );
597 return strmake( "%s/%s", base, path );
601 /*******************************************************************
602 * base_dir_path
604 static char *base_dir_path( const struct makefile *make, const char *path )
606 return concat_paths( make->base_dir, path );
610 /*******************************************************************
611 * obj_dir_path
613 static char *obj_dir_path( const struct makefile *make, const char *path )
615 return concat_paths( make->obj_dir, path );
619 /*******************************************************************
620 * src_dir_path
622 static char *src_dir_path( const struct makefile *make, const char *path )
624 if (make->src_dir) return concat_paths( make->src_dir, path );
625 return obj_dir_path( make, path );
629 /*******************************************************************
630 * top_obj_dir_path
632 static char *top_obj_dir_path( const struct makefile *make, const char *path )
634 return concat_paths( make->top_obj_dir, path );
638 /*******************************************************************
639 * top_dir_path
641 static char *top_dir_path( const struct makefile *make, const char *path )
643 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
644 return top_obj_dir_path( make, path );
648 /*******************************************************************
649 * root_dir_path
651 static char *root_dir_path( const char *path )
653 return concat_paths( root_src_dir, path );
657 /*******************************************************************
658 * tools_dir_path
660 static char *tools_dir_path( const struct makefile *make, const char *path )
662 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
663 return top_obj_dir_path( make, strmake( "tools/%s", path ));
667 /*******************************************************************
668 * tools_path
670 static char *tools_path( const struct makefile *make, const char *name )
672 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
676 /*******************************************************************
677 * get_line
679 static char *get_line( FILE *file )
681 static char *buffer;
682 static size_t size;
684 if (!size)
686 size = 1024;
687 buffer = xmalloc( size );
689 if (!fgets( buffer, size, file )) return NULL;
690 input_line++;
692 for (;;)
694 char *p = buffer + strlen(buffer);
695 /* if line is larger than buffer, resize buffer */
696 while (p == buffer + size - 1 && p[-1] != '\n')
698 buffer = xrealloc( buffer, size * 2 );
699 if (!fgets( buffer + size - 1, size + 1, file )) break;
700 p = buffer + strlen(buffer);
701 size *= 2;
703 if (p > buffer && p[-1] == '\n')
705 *(--p) = 0;
706 if (p > buffer && p[-1] == '\r') *(--p) = 0;
707 if (p > buffer && p[-1] == '\\')
709 *(--p) = 0;
710 /* line ends in backslash, read continuation line */
711 if (!fgets( p, size - (p - buffer), file )) return buffer;
712 input_line++;
713 continue;
716 return buffer;
721 /*******************************************************************
722 * hash_filename
724 static unsigned int hash_filename( const char *name )
726 unsigned int ret = 0;
727 while (*name) ret = (ret << 7) + (ret << 3) + *name++;
728 return ret % HASH_SIZE;
732 /*******************************************************************
733 * add_file
735 static struct file *add_file( const char *name )
737 struct file *file = xmalloc( sizeof(*file) );
738 memset( file, 0, sizeof(*file) );
739 file->name = xstrdup( name );
740 list_add_tail( &files[hash_filename( name )], &file->entry );
741 return file;
745 /*******************************************************************
746 * add_dependency
748 static void add_dependency( struct file *file, const char *name, enum incl_type type )
750 /* enforce some rules for the Wine tree */
752 if (!memcmp( name, "../", 3 ))
753 fatal_error( "#include directive with relative path not allowed\n" );
755 if (!strcmp( name, "config.h" ))
757 if (strendswith( file->name, ".h" ))
758 fatal_error( "config.h must not be included by a header file\n" );
759 if (file->deps_count)
760 fatal_error( "config.h must be included before anything else\n" );
762 else if (!strcmp( name, "wine/port.h" ))
764 if (strendswith( file->name, ".h" ))
765 fatal_error( "wine/port.h must not be included by a header file\n" );
766 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
767 if (file->deps_count > 1)
768 fatal_error( "wine/port.h must be included before everything except config.h\n" );
769 if (strcmp( file->deps[0].name, "config.h" ))
770 fatal_error( "config.h must be included before wine/port.h\n" );
773 if (file->deps_count >= file->deps_size)
775 file->deps_size *= 2;
776 if (file->deps_size < 16) file->deps_size = 16;
777 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
779 file->deps[file->deps_count].line = input_line;
780 file->deps[file->deps_count].type = type;
781 file->deps[file->deps_count].name = xstrdup( name );
782 file->deps_count++;
786 /*******************************************************************
787 * find_src_file
789 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
791 struct incl_file *file;
793 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
794 if (!strcmp( name, file->name )) return file;
795 return NULL;
798 /*******************************************************************
799 * find_include_file
801 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
803 struct incl_file *file;
805 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
806 if (!strcmp( name, file->name )) return file;
807 return NULL;
810 /*******************************************************************
811 * add_include
813 * Add an include file if it doesn't already exists.
815 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
816 const char *name, int line, enum incl_type type )
818 struct incl_file *include;
820 if (parent->files_count >= parent->files_size)
822 parent->files_size *= 2;
823 if (parent->files_size < 16) parent->files_size = 16;
824 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
827 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
828 if (!strcmp( name, include->name )) goto found;
830 include = xmalloc( sizeof(*include) );
831 memset( include, 0, sizeof(*include) );
832 include->name = xstrdup(name);
833 include->included_by = parent;
834 include->included_line = line;
835 include->type = type;
836 list_add_tail( &make->includes, &include->entry );
837 found:
838 parent->files[parent->files_count++] = include;
839 return include;
843 /*******************************************************************
844 * add_generated_source
846 * Add a generated source file to the list.
848 static struct incl_file *add_generated_source( struct makefile *make,
849 const char *name, const char *filename )
851 struct incl_file *file;
853 if ((file = find_src_file( make, name ))) return file; /* we already have it */
854 file = xmalloc( sizeof(*file) );
855 memset( file, 0, sizeof(*file) );
856 file->file = add_file( name );
857 file->name = xstrdup( name );
858 file->filename = obj_dir_path( make, filename ? filename : name );
859 file->file->flags = FLAG_GENERATED;
860 list_add_tail( &make->sources, &file->entry );
861 return file;
865 /*******************************************************************
866 * parse_include_directive
868 static void parse_include_directive( struct file *source, char *str )
870 char quote, *include, *p = str;
872 while (*p && isspace(*p)) p++;
873 if (*p != '\"' && *p != '<' ) return;
874 quote = *p++;
875 if (quote == '<') quote = '>';
876 include = p;
877 while (*p && (*p != quote)) p++;
878 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
879 *p = 0;
880 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
884 /*******************************************************************
885 * parse_pragma_directive
887 static void parse_pragma_directive( struct file *source, char *str )
889 char *flag, *p = str;
891 if (!isspace( *p )) return;
892 while (*p && isspace(*p)) p++;
893 p = strtok( p, " \t" );
894 if (strcmp( p, "makedep" )) return;
896 while ((flag = strtok( NULL, " \t" )))
898 if (!strcmp( flag, "depend" ))
900 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
901 return;
903 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
905 if (strendswith( source->name, ".idl" ))
907 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
908 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
909 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
910 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
911 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
912 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
913 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
914 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
916 else if (strendswith( source->name, ".rc" ))
918 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
920 else if (strendswith( source->name, ".sfd" ))
922 if (!strcmp( flag, "font" ))
924 struct strarray *array = source->args;
926 if (!array)
928 source->args = array = xmalloc( sizeof(*array) );
929 *array = empty_strarray;
930 source->flags |= FLAG_SFD_FONTS;
932 strarray_add( array, xstrdup( strtok( NULL, "" )));
933 return;
936 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
941 /*******************************************************************
942 * parse_cpp_directive
944 static void parse_cpp_directive( struct file *source, char *str )
946 while (*str && isspace(*str)) str++;
947 if (*str++ != '#') return;
948 while (*str && isspace(*str)) str++;
950 if (!strncmp( str, "include", 7 ))
951 parse_include_directive( source, str + 7 );
952 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
953 parse_include_directive( source, str + 6 );
954 else if (!strncmp( str, "pragma", 6 ))
955 parse_pragma_directive( source, str + 6 );
959 /*******************************************************************
960 * parse_idl_file
962 static void parse_idl_file( struct file *source, FILE *file )
964 char *buffer, *include;
966 input_line = 0;
968 while ((buffer = get_line( file )))
970 char quote;
971 char *p = buffer;
972 while (*p && isspace(*p)) p++;
974 if (!strncmp( p, "importlib", 9 ))
976 p += 9;
977 while (*p && isspace(*p)) p++;
978 if (*p++ != '(') continue;
979 while (*p && isspace(*p)) p++;
980 if (*p++ != '"') continue;
981 include = p;
982 while (*p && (*p != '"')) p++;
983 if (!*p) fatal_error( "malformed importlib directive\n" );
984 *p = 0;
985 add_dependency( source, include, INCL_IMPORTLIB );
986 continue;
989 if (!strncmp( p, "import", 6 ))
991 p += 6;
992 while (*p && isspace(*p)) p++;
993 if (*p != '"') continue;
994 include = ++p;
995 while (*p && (*p != '"')) p++;
996 if (!*p) fatal_error( "malformed import directive\n" );
997 *p = 0;
998 add_dependency( source, include, INCL_IMPORT );
999 continue;
1002 /* check for #include inside cpp_quote */
1003 if (!strncmp( p, "cpp_quote", 9 ))
1005 p += 9;
1006 while (*p && isspace(*p)) p++;
1007 if (*p++ != '(') continue;
1008 while (*p && isspace(*p)) p++;
1009 if (*p++ != '"') continue;
1010 if (*p++ != '#') continue;
1011 while (*p && isspace(*p)) p++;
1012 if (strncmp( p, "include", 7 )) continue;
1013 p += 7;
1014 while (*p && isspace(*p)) p++;
1015 if (*p == '\\' && p[1] == '"')
1017 p += 2;
1018 quote = '"';
1020 else
1022 if (*p++ != '<' ) continue;
1023 quote = '>';
1025 include = p;
1026 while (*p && (*p != quote)) p++;
1027 if (!*p || (quote == '"' && p[-1] != '\\'))
1028 fatal_error( "malformed #include directive inside cpp_quote\n" );
1029 if (quote == '"') p--; /* remove backslash */
1030 *p = 0;
1031 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1032 continue;
1035 parse_cpp_directive( source, p );
1039 /*******************************************************************
1040 * parse_c_file
1042 static void parse_c_file( struct file *source, FILE *file )
1044 char *buffer;
1046 input_line = 0;
1047 while ((buffer = get_line( file )))
1049 parse_cpp_directive( source, buffer );
1054 /*******************************************************************
1055 * parse_rc_file
1057 static void parse_rc_file( struct file *source, FILE *file )
1059 char *buffer, *include;
1061 input_line = 0;
1062 while ((buffer = get_line( file )))
1064 char quote;
1065 char *p = buffer;
1066 while (*p && isspace(*p)) p++;
1068 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1070 p += 2;
1071 while (*p && isspace(*p)) p++;
1072 if (strncmp( p, "@makedep:", 9 )) continue;
1073 p += 9;
1074 while (*p && isspace(*p)) p++;
1075 quote = '"';
1076 if (*p == quote)
1078 include = ++p;
1079 while (*p && *p != quote) p++;
1081 else
1083 include = p;
1084 while (*p && !isspace(*p) && *p != '*') p++;
1086 if (!*p)
1087 fatal_error( "malformed makedep comment\n" );
1088 *p = 0;
1089 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1090 continue;
1093 parse_cpp_directive( source, buffer );
1098 /*******************************************************************
1099 * parse_in_file
1101 static void parse_in_file( struct file *source, FILE *file )
1103 char *p, *buffer;
1105 /* make sure it gets rebuilt when the version changes */
1106 add_dependency( source, "config.h", INCL_SYSTEM );
1108 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1110 input_line = 0;
1111 while ((buffer = get_line( file )))
1113 if (strncmp( buffer, ".TH", 3 )) continue;
1114 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1115 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1116 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1117 source->args = xstrdup( p );
1118 return;
1123 /*******************************************************************
1124 * parse_sfd_file
1126 static void parse_sfd_file( struct file *source, FILE *file )
1128 char *p, *eol, *buffer;
1130 input_line = 0;
1131 while ((buffer = get_line( file )))
1133 if (strncmp( buffer, "UComments:", 10 )) continue;
1134 p = buffer + 10;
1135 while (*p == ' ') p++;
1136 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1138 p++;
1139 buffer[strlen(buffer) - 1] = 0;
1141 while ((eol = strstr( p, "+AAoA" )))
1143 *eol = 0;
1144 while (*p && isspace(*p)) p++;
1145 if (*p++ == '#')
1147 while (*p && isspace(*p)) p++;
1148 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1150 p = eol + 5;
1152 while (*p && isspace(*p)) p++;
1153 if (*p++ != '#') return;
1154 while (*p && isspace(*p)) p++;
1155 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1156 return;
1161 static const struct
1163 const char *ext;
1164 void (*parse)( struct file *file, FILE *f );
1165 } parse_functions[] =
1167 { ".c", parse_c_file },
1168 { ".h", parse_c_file },
1169 { ".inl", parse_c_file },
1170 { ".l", parse_c_file },
1171 { ".m", parse_c_file },
1172 { ".rh", parse_c_file },
1173 { ".x", parse_c_file },
1174 { ".y", parse_c_file },
1175 { ".idl", parse_idl_file },
1176 { ".rc", parse_rc_file },
1177 { ".in", parse_in_file },
1178 { ".sfd", parse_sfd_file }
1181 /*******************************************************************
1182 * load_file
1184 static struct file *load_file( const char *name )
1186 struct file *file;
1187 FILE *f;
1188 unsigned int i, hash = hash_filename( name );
1190 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1191 if (!strcmp( name, file->name )) return file;
1193 if (!(f = fopen( name, "r" ))) return NULL;
1195 file = add_file( name );
1196 input_file_name = file->name;
1197 input_line = 0;
1199 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1201 if (!strendswith( name, parse_functions[i].ext )) continue;
1202 parse_functions[i].parse( file, f );
1203 break;
1206 fclose( f );
1207 input_file_name = NULL;
1209 return file;
1213 /*******************************************************************
1214 * open_include_path_file
1216 * Open a file from a directory on the include path.
1218 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1219 const char *name, char **filename )
1221 char *src_path = base_dir_path( make, concat_paths( dir, name ));
1222 struct file *ret = load_file( src_path );
1224 if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
1225 return ret;
1229 /*******************************************************************
1230 * open_file_same_dir
1232 * Open a file in the same directory as the parent.
1234 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1236 char *src_path = replace_filename( parent->file->name, name );
1237 struct file *ret = load_file( src_path );
1239 if (ret) *filename = replace_filename( parent->filename, name );
1240 free( src_path );
1241 return ret;
1245 /*******************************************************************
1246 * open_local_file
1248 * Open a file in the source directory of the makefile.
1250 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1252 char *src_path = root_dir_path( base_dir_path( make, path ));
1253 struct file *ret = load_file( src_path );
1255 /* if not found, try parent dir */
1256 if (!ret && make->parent_dir)
1258 free( src_path );
1259 path = strmake( "%s/%s", make->parent_dir, path );
1260 src_path = root_dir_path( base_dir_path( make, path ));
1261 ret = load_file( src_path );
1264 if (ret) *filename = src_dir_path( make, path );
1265 free( src_path );
1266 return ret;
1270 /*******************************************************************
1271 * open_global_file
1273 * Open a file in the top-level source directory.
1275 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1277 char *src_path = root_dir_path( path );
1278 struct file *ret = load_file( src_path );
1280 if (ret) *filename = top_dir_path( make, path );
1281 free( src_path );
1282 return ret;
1286 /*******************************************************************
1287 * open_global_header
1289 * Open a file in the global include source directory.
1291 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1293 return open_global_file( make, strmake( "include/%s", path ), filename );
1297 /*******************************************************************
1298 * open_src_file
1300 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1302 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1304 if (!file) fatal_perror( "open %s", pFile->name );
1305 return file;
1309 /*******************************************************************
1310 * open_include_file
1312 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1314 struct file *file = NULL;
1315 char *filename;
1316 unsigned int i, len;
1318 errno = ENOENT;
1320 /* check for generated bison header */
1322 if (strendswith( pFile->name, ".tab.h" ) &&
1323 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1325 pFile->sourcename = filename;
1326 pFile->filename = obj_dir_path( make, pFile->name );
1327 return file;
1330 /* check for corresponding idl file in source dir */
1332 if (strendswith( pFile->name, ".h" ) &&
1333 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1335 pFile->sourcename = filename;
1336 pFile->filename = obj_dir_path( make, pFile->name );
1337 return file;
1340 /* check for corresponding tlb file in source dir */
1342 if (strendswith( pFile->name, ".tlb" ) &&
1343 (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1345 pFile->sourcename = filename;
1346 pFile->filename = obj_dir_path( make, pFile->name );
1347 return file;
1350 /* now try in source dir */
1351 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1353 /* check for corresponding idl file in global includes */
1355 if (strendswith( pFile->name, ".h" ) &&
1356 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1358 pFile->sourcename = filename;
1359 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1360 return file;
1363 /* check for corresponding .in file in global includes (for config.h.in) */
1365 if (strendswith( pFile->name, ".h" ) &&
1366 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1368 pFile->sourcename = filename;
1369 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1370 return file;
1373 /* check for corresponding .x file in global includes */
1375 if (strendswith( pFile->name, "tmpl.h" ) &&
1376 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1378 pFile->sourcename = filename;
1379 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1380 return file;
1383 /* check for corresponding .tlb file in global includes */
1385 if (strendswith( pFile->name, ".tlb" ) &&
1386 (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1388 pFile->sourcename = filename;
1389 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1390 return file;
1393 /* check in global includes source dir */
1395 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1397 /* check in global msvcrt includes */
1398 if (make->use_msvcrt &&
1399 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1400 return file;
1402 /* now search in include paths */
1403 for (i = 0; i < make->include_paths.count; i++)
1405 const char *dir = make->include_paths.str[i];
1406 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1408 if (prefix)
1410 len = strlen( prefix );
1411 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1413 while (dir[len] == '/') len++;
1414 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1415 if (file) return file;
1417 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1419 if (*dir != '/')
1421 if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1422 return file;
1425 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1427 /* try in src file directory */
1428 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1430 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1431 perror( pFile->name );
1432 pFile = pFile->included_by;
1433 while (pFile && pFile->included_by)
1435 const char *parent = pFile->included_by->sourcename;
1436 if (!parent) parent = pFile->included_by->file->name;
1437 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1438 parent, pFile->included_line, pFile->name );
1439 pFile = pFile->included_by;
1441 exit(1);
1445 /*******************************************************************
1446 * add_all_includes
1448 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1450 unsigned int i;
1452 parent->files_count = 0;
1453 parent->files_size = file->deps_count;
1454 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1455 for (i = 0; i < file->deps_count; i++)
1457 switch (file->deps[i].type)
1459 case INCL_NORMAL:
1460 case INCL_IMPORT:
1461 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1462 break;
1463 case INCL_IMPORTLIB:
1464 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1465 break;
1466 case INCL_SYSTEM:
1467 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1468 break;
1469 case INCL_CPP_QUOTE:
1470 case INCL_CPP_QUOTE_SYSTEM:
1471 break;
1477 /*******************************************************************
1478 * parse_file
1480 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1482 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1484 if (!file) return;
1486 source->file = file;
1487 source->files_count = 0;
1488 source->files_size = file->deps_count;
1489 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1491 if (source->sourcename)
1493 if (strendswith( source->sourcename, ".idl" ))
1495 unsigned int i;
1497 if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
1499 /* generated .h file always includes these */
1500 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1501 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1502 for (i = 0; i < file->deps_count; i++)
1504 switch (file->deps[i].type)
1506 case INCL_IMPORT:
1507 if (strendswith( file->deps[i].name, ".idl" ))
1508 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1509 file->deps[i].line, INCL_NORMAL );
1510 else
1511 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1512 break;
1513 case INCL_CPP_QUOTE:
1514 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1515 break;
1516 case INCL_CPP_QUOTE_SYSTEM:
1517 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1518 break;
1519 case INCL_NORMAL:
1520 case INCL_SYSTEM:
1521 case INCL_IMPORTLIB:
1522 break;
1525 return;
1527 if (strendswith( source->sourcename, ".y" ))
1528 return; /* generated .tab.h doesn't include anything */
1531 add_all_includes( make, source, file );
1535 /*******************************************************************
1536 * add_src_file
1538 * Add a source file to the list.
1540 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1542 struct incl_file *file;
1544 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1545 file = xmalloc( sizeof(*file) );
1546 memset( file, 0, sizeof(*file) );
1547 file->name = xstrdup(name);
1548 list_add_tail( &make->sources, &file->entry );
1549 parse_file( make, file, 1 );
1550 return file;
1554 /*******************************************************************
1555 * open_input_makefile
1557 static FILE *open_input_makefile( const struct makefile *make )
1559 FILE *ret;
1561 if (make->base_dir)
1562 input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
1563 else
1564 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1566 input_line = 0;
1567 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1568 return ret;
1572 /*******************************************************************
1573 * get_make_variable
1575 static const char *get_make_variable( const struct makefile *make, const char *name )
1577 const char *ret;
1579 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1580 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1581 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1582 return NULL;
1586 /*******************************************************************
1587 * get_expanded_make_variable
1589 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1591 const char *var;
1592 char *p, *end, *expand, *tmp;
1594 var = get_make_variable( make, name );
1595 if (!var) return NULL;
1597 p = expand = xstrdup( var );
1598 while ((p = strchr( p, '$' )))
1600 if (p[1] == '(')
1602 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1603 *end++ = 0;
1604 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1605 var = get_make_variable( make, p + 2 );
1606 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1607 /* switch to the new string */
1608 p = tmp + (p - expand);
1609 free( expand );
1610 expand = tmp;
1612 else if (p[1] == '{') /* don't expand ${} variables */
1614 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1615 p = end + 1;
1617 else if (p[1] == '$')
1619 p += 2;
1621 else fatal_error( "syntax error in '%s'\n", expand );
1624 /* consider empty variables undefined */
1625 p = expand;
1626 while (*p && isspace(*p)) p++;
1627 if (*p) return expand;
1628 free( expand );
1629 return NULL;
1633 /*******************************************************************
1634 * get_expanded_make_var_array
1636 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1638 struct strarray ret = empty_strarray;
1639 char *value, *token;
1641 if ((value = get_expanded_make_variable( make, name )))
1642 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1643 strarray_add( &ret, token );
1644 return ret;
1648 /*******************************************************************
1649 * file_local_var
1651 static char *file_local_var( const char *file, const char *name )
1653 char *p, *var;
1655 var = strmake( "%s_%s", file, name );
1656 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1657 return var;
1661 /*******************************************************************
1662 * set_make_variable
1664 static int set_make_variable( struct strarray *array, const char *assignment )
1666 char *p, *name;
1668 p = name = xstrdup( assignment );
1669 while (isalnum(*p) || *p == '_') p++;
1670 if (name == p) return 0; /* not a variable */
1671 if (isspace(*p))
1673 *p++ = 0;
1674 while (isspace(*p)) p++;
1676 if (*p != '=') return 0; /* not an assignment */
1677 *p++ = 0;
1678 while (isspace(*p)) p++;
1680 strarray_set_value( array, name, p );
1681 return 1;
1685 /*******************************************************************
1686 * parse_makefile
1688 static struct makefile *parse_makefile( const char *path, const char *separator )
1690 char *buffer;
1691 FILE *file;
1692 struct makefile *make = xmalloc( sizeof(*make) );
1694 memset( make, 0, sizeof(*make) );
1695 if (path)
1697 make->top_obj_dir = get_relative_path( path, "" );
1698 make->base_dir = path;
1699 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1702 file = open_input_makefile( make );
1703 while ((buffer = get_line( file )))
1705 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1706 if (*buffer == '\t') continue; /* command */
1707 while (isspace( *buffer )) buffer++;
1708 if (*buffer == '#') continue; /* comment */
1709 set_make_variable( &make->vars, buffer );
1711 fclose( file );
1712 input_file_name = NULL;
1713 return make;
1717 /*******************************************************************
1718 * add_generated_sources
1720 static void add_generated_sources( struct makefile *make )
1722 struct incl_file *source, *next, *file;
1724 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1726 if (source->file->flags & FLAG_IDL_CLIENT)
1728 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1729 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1730 add_all_includes( make, file, file->file );
1732 if (source->file->flags & FLAG_IDL_SERVER)
1734 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1735 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1736 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1737 add_all_includes( make, file, file->file );
1739 if (source->file->flags & FLAG_IDL_IDENT)
1741 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1742 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1743 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1744 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1745 add_all_includes( make, file, file->file );
1747 if (source->file->flags & FLAG_IDL_PROXY)
1749 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1750 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1751 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1752 add_all_includes( make, file, file->file );
1753 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1754 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1755 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1756 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1757 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1758 add_all_includes( make, file, file->file );
1760 if (source->file->flags & FLAG_IDL_TYPELIB)
1762 add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
1764 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1766 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1768 if (source->file->flags & FLAG_IDL_REGISTER)
1770 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1772 if (source->file->flags & FLAG_IDL_HEADER)
1774 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1776 if (!source->file->flags && strendswith( source->name, ".idl" ))
1778 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1780 if (strendswith( source->name, ".x" ))
1782 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1784 if (strendswith( source->name, ".y" ))
1786 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1787 /* steal the includes list from the source file */
1788 file->files_count = source->files_count;
1789 file->files_size = source->files_size;
1790 file->files = source->files;
1791 source->files_count = source->files_size = 0;
1792 source->files = NULL;
1794 if (strendswith( source->name, ".l" ))
1796 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1797 /* steal the includes list from the source file */
1798 file->files_count = source->files_count;
1799 file->files_size = source->files_size;
1800 file->files = source->files;
1801 source->files_count = source->files_size = 0;
1802 source->files = NULL;
1804 if (source->file->flags & FLAG_C_IMPLIB)
1806 if (!make->staticimplib && make->importlib && *dll_ext)
1807 make->staticimplib = strmake( "lib%s.def.a", make->importlib );
1810 if (make->testdll)
1812 file = add_generated_source( make, "testlist.o", "testlist.c" );
1813 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1814 add_all_includes( make, file, file->file );
1819 /*******************************************************************
1820 * create_dir
1822 static void create_dir( const char *dir )
1824 char *p, *path;
1826 p = path = xstrdup( dir );
1827 while ((p = strchr( p, '/' )))
1829 *p = 0;
1830 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1831 *p++ = '/';
1832 while (*p == '/') p++;
1834 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1835 free( path );
1839 /*******************************************************************
1840 * output_filenames_obj_dir
1842 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1844 unsigned int i;
1846 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1850 /*******************************************************************
1851 * get_dependencies
1853 static void get_dependencies( struct strarray *deps, struct incl_file *file, struct incl_file *source )
1855 unsigned int i;
1857 if (!file->filename) return;
1859 if (file != source)
1861 if (file->owner == source) return; /* already processed */
1862 if (file->type == INCL_IMPORTLIB &&
1863 !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1864 return; /* library is imported only when building a typelib */
1865 file->owner = source;
1866 strarray_add( deps, file->filename );
1868 for (i = 0; i < file->files_count; i++) get_dependencies( deps, file->files[i], source );
1872 /*******************************************************************
1873 * get_local_dependencies
1875 * Get the local dependencies of a given target.
1877 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1878 struct strarray targets )
1880 unsigned int i;
1881 struct strarray deps = get_expanded_make_var_array( make, file_local_var( name, "DEPS" ));
1883 for (i = 0; i < deps.count; i++)
1885 if (strarray_exists( &targets, deps.str[i] ))
1886 deps.str[i] = obj_dir_path( make, deps.str[i] );
1887 else
1888 deps.str[i] = src_dir_path( make, deps.str[i] );
1890 return deps;
1894 /*******************************************************************
1895 * add_import_libs
1897 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
1898 struct strarray imports, int cross )
1900 struct strarray ret = empty_strarray;
1901 unsigned int i, j;
1903 for (i = 0; i < imports.count; i++)
1905 const char *name = imports.str[i];
1907 for (j = 0; j < top_makefile->subdirs.count; j++)
1909 const struct makefile *submake = top_makefile->submakes[j];
1911 if (submake->importlib && !strcmp( submake->importlib, name ))
1913 const char *dir = top_obj_dir_path( make, submake->base_dir );
1914 const char *ext = cross ? "cross.a" : *dll_ext ? "def" : "a";
1916 strarray_add( deps, strmake( "%s/lib%s.%s", dir, name, ext ));
1917 if (!cross && submake->staticimplib)
1918 strarray_add( deps, strmake( "%s/%s", dir, submake->staticimplib ));
1919 break;
1922 if (submake->staticlib &&
1923 !strncmp( submake->staticlib, "lib", 3 ) &&
1924 !strncmp( submake->staticlib + 3, name, strlen(name) ) &&
1925 !strcmp( submake->staticlib + 3 + strlen(name), ".a" ))
1927 const char *dir = top_obj_dir_path( make, submake->base_dir );
1929 strarray_add( deps, strmake( "%s/lib%s.a", dir, name ));
1930 break;
1933 strarray_add( &ret, strmake( "-l%s", name ));
1935 return ret;
1939 /*******************************************************************
1940 * add_install_rule
1942 static void add_install_rule( const struct makefile *make, struct strarray *install_rules,
1943 const char *target, const char *file, const char *dest )
1945 if (strarray_exists( &make->install_lib, target ))
1947 strarray_add( &install_rules[INSTALL_LIB], file );
1948 strarray_add( &install_rules[INSTALL_LIB], dest );
1950 else if (strarray_exists( &make->install_dev, target ))
1952 strarray_add( &install_rules[INSTALL_DEV], file );
1953 strarray_add( &install_rules[INSTALL_DEV], dest );
1958 /*******************************************************************
1959 * get_include_install_path
1961 * Determine the installation path for a given include file.
1963 static const char *get_include_install_path( const char *name )
1965 if (!strncmp( name, "wine/", 5 )) return name + 5;
1966 if (!strncmp( name, "msvcrt/", 7 )) return name;
1967 return strmake( "windows/%s", name );
1971 /*******************************************************************
1972 * get_shared_library_name
1974 * Determine possible names for a shared library with a version number.
1976 static struct strarray get_shared_lib_names( const char *libname )
1978 struct strarray ret = empty_strarray;
1979 const char *ext, *p;
1980 char *name, *first, *second;
1981 size_t len = 0;
1983 strarray_add( &ret, libname );
1985 for (p = libname; (p = strchr( p, '.' )); p++)
1986 if ((len = strspn( p + 1, "0123456789." ))) break;
1988 if (!len) return ret;
1989 ext = p + 1 + len;
1990 if (*ext && ext[-1] == '.') ext--;
1992 /* keep only the first group of digits */
1993 name = xstrdup( libname );
1994 first = name + (p - libname);
1995 if ((second = strchr( first + 1, '.' )))
1997 strcpy( second, ext );
1998 strarray_add( &ret, xstrdup( name ));
2000 /* now remove all digits */
2001 strcpy( first, ext );
2002 strarray_add( &ret, name );
2003 return ret;
2007 /*******************************************************************
2008 * output_install_rules
2010 * Rules are stored as a (file,dest) pair of values.
2011 * The first char of dest indicates the type of install.
2013 static struct strarray output_install_rules( const struct makefile *make, struct strarray files,
2014 const char *target, struct strarray *phony_targets )
2016 unsigned int i;
2017 char *install_sh;
2018 struct strarray uninstall = empty_strarray;
2019 struct strarray targets = empty_strarray;
2021 if (!files.count) return uninstall;
2023 for (i = 0; i < files.count; i += 2)
2024 if (strchr( "dps", files.str[i + 1][0] )) /* only for files copied from object dir */
2025 strarray_add_uniq( &targets, files.str[i] );
2027 output( "install %s::", target );
2028 output_filenames_obj_dir( make, targets );
2029 output( "\n" );
2031 install_sh = top_dir_path( make, "tools/install-sh" );
2032 for (i = 0; i < files.count; i += 2)
2034 const char *file = files.str[i];
2035 const char *dest = files.str[i + 1];
2037 switch (*dest)
2039 case 'd': /* data file */
2040 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s $(DESTDIR)%s\n",
2041 install_sh, obj_dir_path( make, file ), dest + 1 );
2042 break;
2043 case 'D': /* data file in source dir */
2044 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s $(DESTDIR)%s\n",
2045 install_sh, src_dir_path( make, file ), dest + 1 );
2046 break;
2047 case 'p': /* program file */
2048 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s $(DESTDIR)%s\n",
2049 install_sh, obj_dir_path( make, file ), dest + 1 );
2050 break;
2051 case 's': /* script */
2052 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s $(DESTDIR)%s\n",
2053 install_sh, obj_dir_path( make, file ), dest + 1 );
2054 break;
2055 case 'S': /* script in source dir */
2056 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s $(DESTDIR)%s\n",
2057 install_sh, src_dir_path( make, file ), dest + 1 );
2058 break;
2059 case 'y': /* symlink */
2060 output( "\trm -f $(DESTDIR)%s && $(LN_S) %s $(DESTDIR)%s\n", dest + 1, file, dest + 1 );
2061 break;
2062 default:
2063 assert(0);
2067 for (i = 0; i < files.count; i += 2)
2068 strarray_add( &uninstall, strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 ));
2070 strarray_add_uniq( phony_targets, "install" );
2071 strarray_add_uniq( phony_targets, target );
2072 return uninstall;
2076 /*******************************************************************
2077 * output_sources
2079 static struct strarray output_sources( const struct makefile *make )
2081 struct incl_file *source;
2082 unsigned int i, j;
2083 struct strarray object_files = empty_strarray;
2084 struct strarray crossobj_files = empty_strarray;
2085 struct strarray res_files = empty_strarray;
2086 struct strarray clean_files = empty_strarray;
2087 struct strarray uninstall_files = empty_strarray;
2088 struct strarray po_files = empty_strarray;
2089 struct strarray mo_files = empty_strarray;
2090 struct strarray mc_files = empty_strarray;
2091 struct strarray ok_files = empty_strarray;
2092 struct strarray in_files = empty_strarray;
2093 struct strarray dlldata_files = empty_strarray;
2094 struct strarray c2man_files = empty_strarray;
2095 struct strarray implib_objs = empty_strarray;
2096 struct strarray includes = empty_strarray;
2097 struct strarray subdirs = empty_strarray;
2098 struct strarray phony_targets = empty_strarray;
2099 struct strarray all_targets = empty_strarray;
2100 struct strarray install_rules[NB_INSTALL_RULES];
2101 char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
2102 char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
2104 for (i = 0; i < NB_INSTALL_RULES; i++) install_rules[i] = empty_strarray;
2106 for (i = 0; i < linguas.count; i++)
2107 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
2109 strarray_add( &phony_targets, "all" );
2110 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
2111 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
2112 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
2113 strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
2114 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
2115 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
2116 for (i = 0; i < make->include_paths.count; i++)
2117 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
2119 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
2121 struct strarray dependencies = empty_strarray;
2122 struct strarray extradefs;
2123 char *obj = xstrdup( source->name );
2124 char *ext = get_extension( obj );
2126 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
2127 *ext++ = 0;
2129 if (make->src_dir && strchr( obj, '/' ))
2131 char *subdir = base_dir_path( make, obj );
2132 *strrchr( subdir, '/' ) = 0;
2133 strarray_add_uniq( &subdirs, subdir );
2136 extradefs = get_expanded_make_var_array( make, file_local_var( obj, "EXTRADEFS" ));
2137 get_dependencies( &dependencies, source, source );
2139 if (!strcmp( ext, "y" )) /* yacc file */
2141 /* add source file dependency for parallel makes */
2142 char *header = strmake( "%s.tab.h", obj );
2144 if (find_include_file( make, header ))
2146 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2147 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
2148 obj, obj_dir_path( make, obj ), source->filename );
2149 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2150 source->filename, obj_dir_path( make, header ));
2151 strarray_add( &clean_files, header );
2153 else output( "%s.tab.c: %s\n", obj, source->filename );
2155 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
2157 else if (!strcmp( ext, "x" )) /* template file */
2159 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2160 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2161 output( "\t%s%s -H -o $@ %s\n",
2162 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2163 if (source->file->flags & FLAG_INSTALL)
2165 strarray_add( &install_rules[INSTALL_DEV], source->name );
2166 strarray_add( &install_rules[INSTALL_DEV],
2167 strmake( "D$(includedir)/%s", get_include_install_path( source->name ) ));
2168 strarray_add( &install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2169 strarray_add( &install_rules[INSTALL_DEV],
2170 strmake( "d$(includedir)/%s.h", get_include_install_path( obj ) ));
2173 else if (!strcmp( ext, "l" )) /* lex file */
2175 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2176 output( "\t$(FLEX) -o$@ %s\n", source->filename );
2178 else if (!strcmp( ext, "rc" )) /* resource file */
2180 strarray_add( &res_files, strmake( "%s.res", obj ));
2181 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
2182 tools_path( make, "wrc" ), source->filename );
2183 output( "\t%s -o $@", tools_path( make, "wrc" ) );
2184 if (make->is_win16) output_filename( "-m16" );
2185 else output_filenames( target_flags );
2186 output_filename( "--nostdinc" );
2187 output_filenames( includes );
2188 output_filenames( make->define_args );
2189 output_filenames( extradefs );
2190 if (mo_files.count && (source->file->flags & FLAG_RC_PO))
2192 strarray_add( &po_files, source->filename );
2193 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
2194 output_filename( source->filename );
2195 output( "\n" );
2196 output( "%s.res:", obj_dir_path( make, obj ));
2197 output_filenames( mo_files );
2198 output( "\n" );
2199 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
2201 else
2203 output_filename( source->filename );
2204 output( "\n" );
2206 output( "%s.res:", obj_dir_path( make, obj ));
2207 output_filenames( dependencies );
2208 output( "\n" );
2210 else if (!strcmp( ext, "mc" )) /* message file */
2212 strarray_add( &res_files, strmake( "%s.res", obj ));
2213 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
2214 tools_path( make, "wmc" ), source->filename );
2215 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
2216 if (mo_files.count)
2218 strarray_add( &mc_files, source->filename );
2219 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
2220 output( "\n" );
2221 output( "%s.res:", obj_dir_path( make, obj ));
2222 output_filenames( mo_files );
2223 output( "\n" );
2224 output( "%s ", obj_dir_path( make, "msg.pot" ));
2226 else output( "\n" );
2227 output( "%s.res:", obj_dir_path( make, obj ));
2228 output_filenames( dependencies );
2229 output( "\n" );
2231 else if (!strcmp( ext, "idl" )) /* IDL file */
2233 struct strarray targets = empty_strarray;
2234 char *dest;
2236 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2237 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2239 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2241 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2242 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2243 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
2244 strarray_add( &targets, dest );
2246 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
2247 if (source->file->flags & FLAG_INSTALL)
2249 strarray_add( &install_rules[INSTALL_DEV], xstrdup( source->name ));
2250 strarray_add( &install_rules[INSTALL_DEV],
2251 strmake( "D$(includedir)/%s.idl", get_include_install_path( obj ) ));
2252 if (source->file->flags & FLAG_IDL_HEADER)
2254 strarray_add( &install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2255 strarray_add( &install_rules[INSTALL_DEV],
2256 strmake( "d$(includedir)/%s.h", get_include_install_path( obj ) ));
2259 if (!targets.count) continue;
2260 output_filenames_obj_dir( make, targets );
2261 output( ": %s\n", tools_path( make, "widl" ));
2262 output( "\t%s -o $@", tools_path( make, "widl" ) );
2263 output_filenames( target_flags );
2264 output_filenames( includes );
2265 output_filenames( make->define_args );
2266 output_filenames( extradefs );
2267 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2268 output_filename( source->filename );
2269 output( "\n" );
2270 output_filenames_obj_dir( make, targets );
2271 output( ": %s", source->filename );
2272 output_filenames( dependencies );
2273 output( "\n" );
2275 else if (!strcmp( ext, "in" )) /* .in file or man page */
2277 if (strendswith( obj, ".man" ) && source->file->args)
2279 struct strarray symlinks;
2280 char *dir, *dest = replace_extension( obj, ".man", "" );
2281 char *lang = strchr( dest, '.' );
2282 char *section = source->file->args;
2283 if (lang)
2285 *lang++ = 0;
2286 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2288 else dir = strmake( "$(mandir)/man%s", section );
2289 add_install_rule( make, install_rules, dest, xstrdup(obj),
2290 strmake( "d%s/%s.%s", dir, dest, section ));
2291 symlinks = get_expanded_make_var_array( make, file_local_var( dest, "SYMLINKS" ));
2292 for (i = 0; i < symlinks.count; i++)
2293 add_install_rule( make, install_rules, symlinks.str[i],
2294 strmake( "%s.%s", dest, section ),
2295 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2296 free( dest );
2297 free( dir );
2299 strarray_add( &in_files, xstrdup(obj) );
2300 strarray_add( &all_targets, xstrdup(obj) );
2301 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2302 output( "\t$(SED_CMD) %s >$@ || (rm -f $@ && false)\n", source->filename );
2303 output( "%s:", obj_dir_path( make, obj ));
2304 output_filenames( dependencies );
2305 output( "\n" );
2306 add_install_rule( make, install_rules, obj, xstrdup( obj ),
2307 strmake( "d$(datadir)/wine/%s", obj ));
2309 else if (!strcmp( ext, "sfd" )) /* font file */
2311 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
2312 if (fontforge && !make->src_dir)
2314 output( "%s: %s\n", ttf_file, source->filename );
2315 output( "\t%s -script %s %s $@\n",
2316 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
2317 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
2319 if (source->file->flags & FLAG_INSTALL)
2321 strarray_add( &install_rules[INSTALL_LIB], strmake( "%s.ttf", obj ));
2322 strarray_add( &install_rules[INSTALL_LIB], strmake( "D$(fontdir)/%s.ttf", obj ));
2324 if (source->file->flags & FLAG_SFD_FONTS)
2326 struct strarray *array = source->file->args;
2328 for (i = 0; i < array->count; i++)
2330 char *font = strtok( xstrdup(array->str[i]), " \t" );
2331 char *args = strtok( NULL, "" );
2333 strarray_add( &all_targets, xstrdup( font ));
2334 output( "%s: %s %s\n", obj_dir_path( make, font ),
2335 tools_path( make, "sfnt2fon" ), ttf_file );
2336 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2337 strarray_add( &install_rules[INSTALL_LIB], xstrdup(font) );
2338 strarray_add( &install_rules[INSTALL_LIB], strmake( "d$(fontdir)/%s", font ));
2342 else if (!strcmp( ext, "svg" )) /* svg file */
2344 if (convert && rsvg && icotool && !make->src_dir)
2346 output( "%s.ico %s.bmp: %s\n",
2347 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
2348 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2349 top_dir_path( make, "tools/buildimage" ), source->filename );
2352 else if (!strcmp( ext, "res" ))
2354 strarray_add( &res_files, source->name );
2356 else if (!strcmp( ext, "tlb" ))
2358 strarray_add( &all_targets, source->name );
2360 else if (!strcmp( ext, "h" ) || !strcmp( ext, "rh" ) || !strcmp( ext, "inl" )) /* header file */
2362 if (source->file->flags & FLAG_GENERATED)
2364 strarray_add( &all_targets, source->name );
2366 else
2368 strarray_add( &install_rules[INSTALL_DEV], source->name );
2369 strarray_add( &install_rules[INSTALL_DEV],
2370 strmake( "D$(includedir)/%s", get_include_install_path( source->name ) ));
2373 else
2375 int need_cross = make->testdll ||
2376 (source->file->flags & FLAG_C_IMPLIB) ||
2377 (make->module && make->staticlib);
2379 if ((source->file->flags & FLAG_GENERATED) &&
2380 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2381 strarray_add( &clean_files, source->filename );
2382 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
2383 strarray_add( &object_files, strmake( "%s.o", obj ));
2384 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2385 output( "\t$(CC) -c -o $@ %s", source->filename );
2386 output_filenames( includes );
2387 output_filenames( make->define_args );
2388 output_filenames( extradefs );
2389 if (make->module || make->staticlib || make->testdll)
2391 output_filenames( dll_flags );
2392 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2394 output_filenames( extra_cflags );
2395 output_filenames( cpp_flags );
2396 output_filename( "$(CFLAGS)" );
2397 output( "\n" );
2398 if (crosstarget && need_cross)
2400 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
2401 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2402 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2403 output_filenames( includes );
2404 output_filenames( make->define_args );
2405 output_filenames( extradefs );
2406 output_filename( "-DWINE_CROSSTEST" );
2407 output_filenames( cpp_flags );
2408 output_filename( "$(CFLAGS)" );
2409 output( "\n" );
2411 if (make->testdll && !strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2413 strarray_add( &ok_files, strmake( "%s.ok", obj ));
2414 output( "%s.ok:\n", obj_dir_path( make, obj ));
2415 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2416 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
2417 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
2419 if (!strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2420 strarray_add( &c2man_files, source->filename );
2421 output( "%s.o", obj_dir_path( make, obj ));
2422 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2423 output( ":" );
2424 output_filenames( dependencies );
2425 output( "\n" );
2427 free( obj );
2430 /* rules for files that depend on multiple sources */
2432 if (po_files.count)
2434 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
2435 output_filenames( po_files );
2436 output( "\n" );
2437 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
2438 if (make->is_win16) output_filename( "-m16" );
2439 else output_filenames( target_flags );
2440 output_filename( "--nostdinc" );
2441 output_filenames( includes );
2442 output_filenames( make->define_args );
2443 output_filenames( po_files );
2444 output( "\n" );
2445 strarray_add( &clean_files, "rsrc.pot" );
2448 if (mc_files.count)
2450 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
2451 output_filenames( mc_files );
2452 output( "\n" );
2453 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
2454 output_filenames( mc_files );
2455 output( "\n" );
2456 strarray_add( &clean_files, "msg.pot" );
2459 if (dlldata_files.count)
2461 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
2462 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
2463 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
2464 output_filenames( dlldata_files );
2465 output( "\n" );
2468 if (make->module && !make->staticlib)
2470 struct strarray all_libs = empty_strarray;
2471 struct strarray dep_libs = empty_strarray;
2472 char *module_path = obj_dir_path( make, make->module );
2473 char *spec_file = NULL;
2475 if (!make->appmode.count)
2476 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2477 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 0 ));
2478 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0 ));
2479 for (i = 0; i < make->delayimports.count; i++)
2480 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2481 strarray_add( &all_libs, "-lwine" );
2482 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2483 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2484 strarray_addall( &all_libs, libs );
2486 if (*dll_ext)
2488 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
2489 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
2490 add_install_rule( make, install_rules, make->module, strmake( "%s%s", make->module, dll_ext ),
2491 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
2492 add_install_rule( make, install_rules, make->module, strmake( "%s.fake", make->module ),
2493 strmake( "d$(fakedlldir)/%s", make->module ));
2494 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2496 else
2498 strarray_add( &all_targets, make->module );
2499 add_install_rule( make, install_rules, make->module, make->module,
2500 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
2501 output( "%s:", module_path );
2503 if (spec_file) output_filename( spec_file );
2504 output_filenames_obj_dir( make, object_files );
2505 output_filenames_obj_dir( make, res_files );
2506 output_filenames( dep_libs );
2507 output( "\n" );
2508 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2509 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2510 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2511 output_filenames( target_flags );
2512 output_filenames( unwind_flags );
2513 if (spec_file)
2515 output( " -shared %s", spec_file );
2516 output_filenames( make->extradllflags );
2518 else output_filenames( make->appmode );
2519 output_filenames_obj_dir( make, object_files );
2520 output_filenames_obj_dir( make, res_files );
2521 output_filenames( all_libs );
2522 output_filename( "$(LDFLAGS)" );
2523 output( "\n" );
2525 if (spec_file && make->importlib)
2527 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2528 if (*dll_ext)
2530 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
2531 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2532 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2533 output_filenames( target_flags );
2534 if (make->is_win16) output_filename( "-m16" );
2535 output( "\n" );
2536 add_install_rule( make, install_rules, make->importlib,
2537 strmake( "lib%s.def", make->importlib ),
2538 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
2539 if (implib_objs.count)
2541 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
2542 output( "%s.def.a:", importlib_path );
2543 output_filenames_obj_dir( make, implib_objs );
2544 output( "\n" );
2545 output( "\trm -f $@\n" );
2546 output( "\t$(AR) $(ARFLAGS) $@" );
2547 output_filenames_obj_dir( make, implib_objs );
2548 output( "\n" );
2549 output( "\t$(RANLIB) $@\n" );
2550 add_install_rule( make, install_rules, make->importlib,
2551 strmake( "lib%s.def.a", make->importlib ),
2552 strmake( "d$(dlldir)/lib%s.def.a", make->importlib ));
2555 else
2557 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
2558 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2559 output_filenames_obj_dir( make, implib_objs );
2560 output( "\n" );
2561 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2562 output_filenames( target_flags );
2563 output_filenames_obj_dir( make, implib_objs );
2564 output( "\n" );
2565 add_install_rule( make, install_rules, make->importlib,
2566 strmake( "lib%s.a", make->importlib ),
2567 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
2569 if (crosstarget && !make->is_win16)
2571 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2572 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2573 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2574 output_filenames_obj_dir( make, cross_files );
2575 output( "\n" );
2576 output( "\t%s -b %s -w --implib -o $@ --export %s",
2577 tools_path( make, "winebuild" ), crosstarget, spec_file );
2578 output_filenames_obj_dir( make, cross_files );
2579 output( "\n" );
2583 if (spec_file)
2585 if (c2man_files.count)
2587 output( "manpages::\n" );
2588 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2589 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2590 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2591 output_filename( strmake( "-o %s/man%s",
2592 top_obj_dir_path( make, "documentation" ), man_ext ));
2593 output_filenames( c2man_files );
2594 output( "\n" );
2595 output( "htmlpages::\n" );
2596 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2597 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2598 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2599 output_filename( strmake( "-o %s",
2600 top_obj_dir_path( make, "documentation/html" )));
2601 output_filenames( c2man_files );
2602 output( "\n" );
2603 output( "sgmlpages::\n" );
2604 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2605 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2606 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2607 output_filename( strmake( "-o %s",
2608 top_obj_dir_path( make, "documentation/api-guide" )));
2609 output_filenames( c2man_files );
2610 output( "\n" );
2611 output( "xmlpages::\n" );
2612 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2613 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2614 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2615 output_filename( strmake( "-o %s",
2616 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2617 output_filenames( c2man_files );
2618 output( "\n" );
2619 strarray_add( &phony_targets, "manpages" );
2620 strarray_add( &phony_targets, "htmlpages" );
2621 strarray_add( &phony_targets, "sgmlpages" );
2622 strarray_add( &phony_targets, "xmlpages" );
2624 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2626 else if (*dll_ext)
2628 char *binary = replace_extension( make->module, ".exe", "" );
2629 add_install_rule( make, install_rules, binary, tools_dir_path( make, "wineapploader" ),
2630 strmake( "s$(bindir)/%s", binary ));
2634 if (make->staticlib)
2636 strarray_add( &all_targets, make->staticlib );
2637 output( "%s:", obj_dir_path( make, make->staticlib ));
2638 output_filenames_obj_dir( make, object_files );
2639 output( "\n\trm -f $@\n" );
2640 output( "\t$(AR) $(ARFLAGS) $@" );
2641 output_filenames_obj_dir( make, object_files );
2642 output( "\n\t$(RANLIB) $@\n" );
2643 if (crosstarget && make->module)
2645 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2647 strarray_add( &all_targets, name );
2648 output( "%s:", obj_dir_path( make, name ));
2649 output_filenames_obj_dir( make, crossobj_files );
2650 output( "\n\trm -f $@\n" );
2651 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2652 output_filenames_obj_dir( make, crossobj_files );
2653 output( "\n\t%s-ranlib $@\n", crosstarget );
2657 if (make->sharedlib)
2659 char *basename, *p;
2660 struct strarray names = get_shared_lib_names( make->sharedlib );
2661 struct strarray all_libs = empty_strarray;
2663 basename = xstrdup( make->sharedlib );
2664 if ((p = strchr( basename, '.' ))) *p = 0;
2666 strarray_addall( &all_libs, get_expanded_make_var_array( make,
2667 file_local_var( basename, "LDFLAGS" )));
2668 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2669 strarray_addall( &all_libs, libs );
2671 output( "%s:", obj_dir_path( make, make->sharedlib ));
2672 output_filenames_obj_dir( make, object_files );
2673 output_filenames( get_local_dependencies( make, basename, in_files ));
2674 output( "\n" );
2675 output( "\t$(CC) -o $@" );
2676 output_filenames_obj_dir( make, object_files );
2677 output_filenames( all_libs );
2678 output_filename( "$(LDFLAGS)" );
2679 output( "\n" );
2680 add_install_rule( make, install_rules, make->sharedlib, make->sharedlib,
2681 strmake( "p$(libdir)/%s", make->sharedlib ));
2682 for (i = 1; i < names.count; i++)
2684 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
2685 output( "\trm -f $@ && $(LN_S) %s $@\n", names.str[i-1] );
2686 add_install_rule( make, install_rules, names.str[i], names.str[i-1],
2687 strmake( "y$(libdir)/%s", names.str[i] ));
2689 strarray_addall( &all_targets, names );
2692 if (make->importlib && !make->module) /* stand-alone import lib (for libwine) */
2694 char *def_file = replace_extension( make->importlib, ".a", ".def" );
2696 if (!strncmp( def_file, "lib", 3 )) def_file += 3;
2697 output( "%s: %s\n", obj_dir_path( make, make->importlib ), src_dir_path( make, def_file ));
2698 output( "\t%s -l $@ -d %s\n", dlltool, src_dir_path( make, def_file ));
2699 add_install_rule( make, install_rules, make->importlib, make->importlib,
2700 strmake( "d$(libdir)/%s", make->importlib ));
2701 strarray_add( &all_targets, make->importlib );
2704 if (make->testdll)
2706 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2707 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2708 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2709 struct strarray dep_libs = empty_strarray;
2710 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0 );
2712 strarray_addall( &all_libs, libs );
2713 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2714 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2715 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2716 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2717 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2718 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2719 output_filenames( target_flags );
2720 output_filenames( unwind_flags );
2721 output_filenames( make->appmode );
2722 output_filenames_obj_dir( make, object_files );
2723 output_filenames_obj_dir( make, res_files );
2724 output_filenames( all_libs );
2725 output_filename( "$(LDFLAGS)" );
2726 output( "\n" );
2727 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2728 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2729 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2730 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2731 output_filenames( target_flags );
2732 output_filenames( unwind_flags );
2733 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2734 output_filenames( make->appmode );
2735 output_filenames_obj_dir( make, object_files );
2736 output_filenames_obj_dir( make, res_files );
2737 output_filenames( all_libs );
2738 output_filename( "$(LDFLAGS)" );
2739 output( "\n" );
2740 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2741 obj_dir_path( make, stripped ), dll_ext );
2742 output_filenames_obj_dir( make, object_files );
2743 output_filenames_obj_dir( make, res_files );
2744 output_filenames( dep_libs );
2745 output( "\n" );
2747 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2748 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2749 obj_dir_path( make, stripped ), dll_ext );
2750 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2751 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2753 if (crosstarget)
2755 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2757 dep_libs = empty_strarray;
2758 all_libs = add_import_libs( make, &dep_libs, make->imports, 1 );
2759 strarray_addall( &all_libs, libs );
2760 strarray_add( &clean_files, crosstest );
2761 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2762 output( "%s:", obj_dir_path( make, crosstest ));
2763 output_filenames_obj_dir( make, crossobj_files );
2764 output_filenames_obj_dir( make, res_files );
2765 output_filenames( dep_libs );
2766 output( "\n" );
2767 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2768 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2769 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2770 output_filename( "--lib-suffix=.cross.a" );
2771 output_filenames_obj_dir( make, crossobj_files );
2772 output_filenames_obj_dir( make, res_files );
2773 output_filenames( all_libs );
2774 output_filename( "$(LDFLAGS)" );
2775 output( "\n" );
2776 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2777 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2780 output_filenames_obj_dir( make, ok_files );
2781 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2782 output( "check test:" );
2783 output_filenames_obj_dir( make, ok_files );
2784 output( "\n" );
2785 output( "testclean::\n" );
2786 output( "\trm -f" );
2787 output_filenames_obj_dir( make, ok_files );
2788 output( "\n" );
2789 strarray_addall( &clean_files, ok_files );
2790 strarray_add( &phony_targets, "check" );
2791 strarray_add( &phony_targets, "test" );
2792 strarray_add( &phony_targets, "testclean" );
2795 for (i = 0; i < make->programs.count; i++)
2797 char *program_installed = NULL;
2798 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
2799 struct strarray all_libs = empty_strarray;
2800 struct strarray deps = get_local_dependencies( make, make->programs.str[i], in_files );
2801 struct strarray objs = get_expanded_make_var_array( make,
2802 file_local_var( make->programs.str[i], "OBJS" ));
2803 struct strarray symlinks = get_expanded_make_var_array( make,
2804 file_local_var( make->programs.str[i], "SYMLINKS" ));
2806 if (!objs.count) objs = object_files;
2807 output( "%s:", obj_dir_path( make, program ) );
2808 output_filenames_obj_dir( make, objs );
2809 output_filenames( deps );
2810 output( "\n" );
2811 output( "\t$(CC) -o $@" );
2812 output_filenames_obj_dir( make, objs );
2813 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2814 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2815 strarray_addall( &all_libs, libs );
2816 strarray_addall( &all_libs, get_expanded_make_var_array( make,
2817 file_local_var( make->programs.str[i], "LDFLAGS" )));
2819 if (strarray_exists( &all_libs, "-lwine" ))
2821 strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
2822 if (ldrpath_local && ldrpath_install)
2824 program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
2825 output_filename( ldrpath_local );
2826 output_filenames( all_libs );
2827 output_filename( "$(LDFLAGS)" );
2828 output( "\n" );
2829 output( "%s:", obj_dir_path( make, program_installed ) );
2830 output_filenames_obj_dir( make, objs );
2831 output_filenames( deps );
2832 output( "\n" );
2833 output( "\t$(CC) -o $@" );
2834 output_filenames_obj_dir( make, objs );
2835 output_filename( ldrpath_install );
2836 strarray_add( &all_targets, program_installed );
2840 output_filenames( all_libs );
2841 output_filename( "$(LDFLAGS)" );
2842 output( "\n" );
2843 strarray_add( &all_targets, program );
2845 if (symlinks.count)
2847 output_filenames_obj_dir( make, symlinks );
2848 output( ": %s\n", obj_dir_path( make, program ));
2849 output( "\trm -f $@ && $(LN_S) %s $@\n", obj_dir_path( make, program ));
2850 strarray_addall( &all_targets, symlinks );
2853 add_install_rule( make, install_rules, program, program_installed ? program_installed : program,
2854 strmake( "p$(bindir)/%s", program ));
2855 for (j = 0; j < symlinks.count; j++)
2856 add_install_rule( make, install_rules, symlinks.str[j], program,
2857 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
2860 for (i = 0; i < make->scripts.count; i++)
2861 add_install_rule( make, install_rules, make->scripts.str[i], make->scripts.str[i],
2862 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
2864 if (all_targets.count)
2866 output( "all:" );
2867 output_filenames_obj_dir( make, all_targets );
2868 output( "\n" );
2871 strarray_addall( &uninstall_files, output_install_rules( make, install_rules[INSTALL_LIB],
2872 "install-lib", &phony_targets ));
2873 strarray_addall( &uninstall_files, output_install_rules( make, install_rules[INSTALL_DEV],
2874 "install-dev", &phony_targets ));
2875 if (uninstall_files.count)
2877 output( "uninstall::\n" );
2878 output( "\trm -f" );
2879 output_filenames( uninstall_files );
2880 output( "\n" );
2881 strarray_add_uniq( &phony_targets, "uninstall" );
2884 strarray_addall( &clean_files, object_files );
2885 strarray_addall( &clean_files, crossobj_files );
2886 strarray_addall( &clean_files, res_files );
2887 strarray_addall( &clean_files, all_targets );
2888 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2890 if (clean_files.count)
2892 output( "%s::\n", obj_dir_path( make, "clean" ));
2893 output( "\trm -f" );
2894 output_filenames_obj_dir( make, clean_files );
2895 output( "\n" );
2896 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2897 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2900 if (make->subdirs.count)
2902 struct strarray makefile_deps = empty_strarray;
2903 struct strarray distclean_files = empty_strarray;
2905 for (i = 0; i < make->subdirs.count; i++)
2907 strarray_add( &makefile_deps, top_dir_path( make, base_dir_path( make->submakes[i],
2908 strmake ( "%s.in", output_makefile_name ))));
2909 strarray_add( &distclean_files, base_dir_path( make->submakes[i], output_makefile_name ));
2910 if (!make->src_dir)
2911 strarray_add( &distclean_files, base_dir_path( make->submakes[i], ".gitignore" ));
2912 if (make->submakes[i]->testdll)
2913 strarray_add( &distclean_files, base_dir_path( make->submakes[i], "testlist.c" ));
2915 output( "Makefile:" );
2916 output_filenames( makefile_deps );
2917 output( "\n" );
2918 output( "distclean::\n");
2919 output( "\trm -f" );
2920 output_filenames( distclean_files );
2921 output( "\n" );
2922 strarray_add( &phony_targets, "distclean" );
2925 if (phony_targets.count)
2927 output( ".PHONY:" );
2928 output_filenames( phony_targets );
2929 output( "\n" );
2932 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2934 return clean_files;
2938 /*******************************************************************
2939 * create_temp_file
2941 static FILE *create_temp_file( const char *orig )
2943 char *name = xmalloc( strlen(orig) + 13 );
2944 unsigned int i, id = getpid();
2945 int fd;
2946 FILE *ret = NULL;
2948 for (i = 0; i < 100; i++)
2950 sprintf( name, "%s.tmp%08x", orig, id );
2951 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2953 ret = fdopen( fd, "w" );
2954 break;
2956 if (errno != EEXIST) break;
2957 id += 7777;
2959 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2960 temp_file_name = name;
2961 return ret;
2965 /*******************************************************************
2966 * rename_temp_file
2968 static void rename_temp_file( const char *dest )
2970 int ret = rename( temp_file_name, dest );
2971 if (ret == -1 && errno == EEXIST)
2973 /* rename doesn't overwrite on windows */
2974 unlink( dest );
2975 ret = rename( temp_file_name, dest );
2977 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2978 temp_file_name = NULL;
2982 /*******************************************************************
2983 * are_files_identical
2985 static int are_files_identical( FILE *file1, FILE *file2 )
2987 for (;;)
2989 char buffer1[8192], buffer2[8192];
2990 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2991 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2992 if (size1 != size2) return 0;
2993 if (!size1) return feof( file1 ) && feof( file2 );
2994 if (memcmp( buffer1, buffer2, size1 )) return 0;
2999 /*******************************************************************
3000 * rename_temp_file_if_changed
3002 static void rename_temp_file_if_changed( const char *dest )
3004 FILE *file1, *file2;
3005 int do_rename = 1;
3007 if ((file1 = fopen( dest, "r" )))
3009 if ((file2 = fopen( temp_file_name, "r" )))
3011 do_rename = !are_files_identical( file1, file2 );
3012 fclose( file2 );
3014 fclose( file1 );
3016 if (!do_rename)
3018 unlink( temp_file_name );
3019 temp_file_name = NULL;
3021 else rename_temp_file( dest );
3025 /*******************************************************************
3026 * output_testlist
3028 static void output_testlist( const struct makefile *make )
3030 const char *dest = base_dir_path( make, "testlist.c" );
3031 struct strarray files = empty_strarray;
3032 struct incl_file *source;
3033 unsigned int i;
3035 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3037 if (source->file->flags & FLAG_GENERATED) continue;
3038 if (!strendswith( source->name, ".c" )) continue;
3039 strarray_add( &files, replace_extension( source->name, ".c", "" ));
3042 output_file = create_temp_file( dest );
3044 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3045 output( "#define WIN32_LEAN_AND_MEAN\n" );
3046 output( "#include <windows.h>\n\n" );
3047 output( "#define STANDALONE\n" );
3048 output( "#include \"wine/test.h\"\n\n" );
3050 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
3051 output( "\n" );
3052 output( "const struct test winetest_testlist[] =\n" );
3053 output( "{\n" );
3054 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
3055 output( " { 0, 0 }\n" );
3056 output( "};\n" );
3058 if (fclose( output_file )) fatal_perror( "write" );
3059 output_file = NULL;
3060 rename_temp_file_if_changed( dest );
3064 /*******************************************************************
3065 * output_gitignore
3067 static void output_gitignore( const char *dest, struct strarray files )
3069 int i;
3071 output_file = create_temp_file( dest );
3073 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3074 for (i = 0; i < files.count; i++)
3076 if (!strchr( files.str[i], '/' )) output( "/" );
3077 output( "%s\n", files.str[i] );
3080 if (fclose( output_file )) fatal_perror( "write" );
3081 output_file = NULL;
3082 rename_temp_file( dest );
3086 /*******************************************************************
3087 * output_top_variables
3089 static void output_top_variables( const struct makefile *make )
3091 unsigned int i;
3092 struct strarray *vars = &top_makefile->vars;
3094 if (!make->base_dir) return; /* don't output variables in the top makefile */
3096 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
3097 output( "all:\n\n" );
3098 for (i = 0; i < vars->count; i += 2)
3100 if (!strcmp( vars->str[i], "SUBDIRS" )) continue; /* not inherited */
3101 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
3103 output( "\n" );
3107 /*******************************************************************
3108 * output_dependencies
3110 static void output_dependencies( const struct makefile *make )
3112 static const char separator[] = "### Dependencies";
3113 struct strarray targets, ignore_files = empty_strarray;
3114 char buffer[1024];
3115 FILE *src_file;
3116 int found = 0;
3118 output_file_name = base_dir_path( make, output_makefile_name );
3119 output_file = create_temp_file( output_file_name );
3120 output_top_variables( make );
3122 /* copy the contents of the source makefile */
3123 src_file = open_input_makefile( make );
3124 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
3126 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
3127 found = !strncmp( buffer, separator, strlen(separator) );
3129 if (fclose( src_file )) fatal_perror( "close" );
3130 input_file_name = NULL;
3132 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
3133 targets = output_sources( make );
3135 fclose( output_file );
3136 output_file = NULL;
3137 rename_temp_file( output_file_name );
3139 strarray_add( &ignore_files, ".gitignore" );
3140 strarray_add( &ignore_files, "Makefile" );
3141 if (make->testdll)
3143 output_testlist( make );
3144 strarray_add( &ignore_files, "testlist.c" );
3146 strarray_addall( &ignore_files, targets );
3147 if (!make->src_dir && make->base_dir)
3148 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
3150 output_file_name = NULL;
3154 /*******************************************************************
3155 * load_sources
3157 static void load_sources( struct makefile *make )
3159 static const char *source_vars[] =
3161 "C_SRCS",
3162 "OBJC_SRCS",
3163 "RC_SRCS",
3164 "MC_SRCS",
3165 "IDL_SRCS",
3166 "BISON_SRCS",
3167 "LEX_SRCS",
3168 "HEADER_SRCS",
3169 "XTEMPLATE_SRCS",
3170 "SVG_SRCS",
3171 "FONT_SRCS",
3172 "IN_SRCS",
3173 "MANPAGES",
3174 NULL
3176 const char **var;
3177 unsigned int i;
3178 struct strarray value;
3179 struct incl_file *file;
3181 if (root_src_dir)
3183 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
3184 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
3186 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
3187 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
3188 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
3190 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
3191 make->module = get_expanded_make_variable( make, "MODULE" );
3192 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
3193 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
3194 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
3195 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
3197 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
3198 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
3199 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
3200 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
3201 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
3202 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
3203 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
3204 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
3206 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
3208 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
3209 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
3211 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
3212 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 ) ||
3213 !strcmp( make->imports.str[i], "ucrtbase" );
3215 if (make->module && !make->install_lib.count) strarray_add( &make->install_lib, make->module );
3217 make->include_paths = empty_strarray;
3218 make->define_args = empty_strarray;
3219 strarray_add( &make->define_args, "-D__WINESRC__" );
3221 value = get_expanded_make_var_array( make, "EXTRAINCL" );
3222 for (i = 0; i < value.count; i++)
3223 if (!strncmp( value.str[i], "-I", 2 ))
3224 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
3225 else
3226 strarray_add_uniq( &make->define_args, value.str[i] );
3227 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
3229 list_init( &make->sources );
3230 list_init( &make->includes );
3232 /* FIXME: target dir has to exist to allow locating srcdir-relative include files */
3233 if (make->base_dir) create_dir( make->base_dir );
3235 for (var = source_vars; *var; var++)
3237 value = get_expanded_make_var_array( make, *var );
3238 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
3241 add_generated_sources( make );
3243 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
3244 for (i = 0; i < value.count; i++)
3246 /* default to .c for unknown extra object files */
3247 if (strendswith( value.str[i], ".o" ))
3248 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
3249 else
3250 add_generated_source( make, value.str[i], NULL );
3253 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
3257 /*******************************************************************
3258 * parse_makeflags
3260 static void parse_makeflags( const char *flags )
3262 const char *p = flags;
3263 char *var, *buffer = xmalloc( strlen(flags) + 1 );
3265 while (*p)
3267 while (isspace(*p)) p++;
3268 var = buffer;
3269 while (*p && !isspace(*p))
3271 if (*p == '\\' && p[1]) p++;
3272 *var++ = *p++;
3274 *var = 0;
3275 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
3280 /*******************************************************************
3281 * parse_option
3283 static int parse_option( const char *opt )
3285 if (opt[0] != '-')
3287 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
3288 return 0;
3290 switch(opt[1])
3292 case 'f':
3293 if (opt[2]) output_makefile_name = opt + 2;
3294 break;
3295 case 'R':
3296 relative_dir_mode = 1;
3297 break;
3298 default:
3299 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
3300 exit(1);
3302 return 1;
3306 /*******************************************************************
3307 * main
3309 int main( int argc, char *argv[] )
3311 const char *makeflags = getenv( "MAKEFLAGS" );
3312 int i, j;
3314 if (makeflags) parse_makeflags( makeflags );
3316 i = 1;
3317 while (i < argc)
3319 if (parse_option( argv[i] ))
3321 for (j = i; j < argc; j++) argv[j] = argv[j+1];
3322 argc--;
3324 else i++;
3327 if (relative_dir_mode)
3329 char *relpath;
3331 if (argc != 3)
3333 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
3334 exit( 1 );
3336 relpath = get_relative_path( argv[1], argv[2] );
3337 printf( "%s\n", relpath ? relpath : "." );
3338 exit( 0 );
3341 atexit( cleanup_files );
3342 signal( SIGTERM, exit_on_signal );
3343 signal( SIGINT, exit_on_signal );
3344 #ifdef SIGHUP
3345 signal( SIGHUP, exit_on_signal );
3346 #endif
3348 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
3350 top_makefile = parse_makefile( NULL, "# End of common header" );
3352 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
3353 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
3354 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
3355 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
3356 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
3357 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
3358 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
3359 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
3361 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
3362 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
3363 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
3364 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
3365 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
3366 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
3367 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
3368 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
3369 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
3370 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
3371 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
3372 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
3374 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
3375 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
3376 if (!exe_ext) exe_ext = "";
3377 if (!tools_ext) tools_ext = "";
3378 if (!man_ext) man_ext = "3w";
3380 if (argc == 1)
3382 top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
3383 top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );
3385 for (i = 0; i < top_makefile->subdirs.count; i++)
3386 top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i], NULL );
3388 load_sources( top_makefile );
3389 for (i = 0; i < top_makefile->subdirs.count; i++)
3390 load_sources( top_makefile->submakes[i] );
3392 for (i = 0; i < top_makefile->subdirs.count; i++)
3393 output_dependencies( top_makefile->submakes[i] );
3395 output_dependencies( top_makefile );
3396 return 0;
3399 for (i = 1; i < argc; i++)
3401 struct makefile *make = parse_makefile( argv[i], NULL );
3402 load_sources( make );
3403 output_dependencies( make );
3405 return 0;