schannel/tests: Relax GetInfo return value test.
[wine.git] / tools / makedep.c
blob0083661dcc7aef7646162473b76facef95e64c3c
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 struct strarray
40 unsigned int count; /* strings in use */
41 unsigned int size; /* total allocated size */
42 const char **str;
45 enum incl_type
47 INCL_NORMAL, /* #include "foo.h" */
48 INCL_SYSTEM, /* #include <foo.h> */
49 INCL_IMPORT, /* idl import "foo.idl" */
50 INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
51 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
52 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
55 struct dependency
57 int line; /* source line where this header is included */
58 enum incl_type type; /* type of include */
59 char *name; /* header name */
62 struct file
64 struct list entry;
65 char *name; /* full file name relative to cwd */
66 void *args; /* custom arguments for makefile rule */
67 unsigned int flags; /* flags (see below) */
68 unsigned int deps_count; /* files in use */
69 unsigned int deps_size; /* total allocated size */
70 struct dependency *deps; /* all header dependencies */
73 struct incl_file
75 struct list entry;
76 struct file *file;
77 char *name;
78 char *filename;
79 char *sourcename; /* source file name for generated headers */
80 struct incl_file *included_by; /* file that included this one */
81 int included_line; /* line where this file was included */
82 enum incl_type type; /* type of include */
83 struct incl_file *owner;
84 unsigned int files_count; /* files in use */
85 unsigned int files_size; /* total allocated size */
86 struct incl_file **files;
87 struct strarray dependencies; /* file dependencies */
90 #define FLAG_GENERATED 0x000001 /* generated file */
91 #define FLAG_INSTALL 0x000002 /* file to install */
92 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
93 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
94 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
95 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
96 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
97 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
98 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
99 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
100 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
101 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
102 #define FLAG_SFD_FONTS 0x040000 /* sfd file generated bitmap fonts */
104 static const struct
106 unsigned int flag;
107 const char *ext;
108 } idl_outputs[] =
110 { FLAG_IDL_TYPELIB, ".tlb" },
111 { FLAG_IDL_REGTYPELIB, "_t.res" },
112 { FLAG_IDL_CLIENT, "_c.c" },
113 { FLAG_IDL_IDENT, "_i.c" },
114 { FLAG_IDL_PROXY, "_p.c" },
115 { FLAG_IDL_SERVER, "_s.c" },
116 { FLAG_IDL_REGISTER, "_r.res" },
117 { FLAG_IDL_HEADER, ".h" }
120 #define HASH_SIZE 997
122 static struct list files[HASH_SIZE];
124 static const struct strarray empty_strarray;
126 enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
128 /* variables common to all makefiles */
129 static struct strarray linguas;
130 static struct strarray dll_flags;
131 static struct strarray target_flags;
132 static struct strarray msvcrt_flags;
133 static struct strarray extra_cflags;
134 static struct strarray cpp_flags;
135 static struct strarray unwind_flags;
136 static struct strarray libs;
137 static struct strarray cmdline_vars;
138 static struct strarray disabled_dirs;
139 static const char *root_src_dir;
140 static const char *tools_dir;
141 static const char *tools_ext;
142 static const char *exe_ext;
143 static const char *dll_ext;
144 static const char *man_ext;
145 static const char *crosstarget;
146 static const char *fontforge;
147 static const char *convert;
148 static const char *rsvg;
149 static const char *icotool;
150 static const char *dlltool;
151 static const char *msgfmt;
152 static const char *ln_s;
154 struct makefile
156 /* values determined from input makefile */
157 struct strarray vars;
158 struct strarray include_paths;
159 struct strarray include_args;
160 struct strarray define_args;
161 struct strarray programs;
162 struct strarray scripts;
163 struct strarray appmode;
164 struct strarray imports;
165 struct strarray subdirs;
166 struct strarray delayimports;
167 struct strarray extradllflags;
168 struct strarray install_lib;
169 struct strarray install_dev;
170 struct list sources;
171 struct list includes;
172 const char *base_dir;
173 const char *src_dir;
174 const char *obj_dir;
175 const char *top_src_dir;
176 const char *top_obj_dir;
177 const char *parent_dir;
178 const char *module;
179 const char *testdll;
180 const char *sharedlib;
181 const char *staticlib;
182 const char *staticimplib;
183 const char *importlib;
184 int disabled;
185 int use_msvcrt;
186 int is_win16;
187 struct makefile **submakes;
189 /* values generated at output time */
190 struct strarray in_files;
191 struct strarray ok_files;
192 struct strarray clean_files;
193 struct strarray object_files;
194 struct strarray crossobj_files;
195 struct strarray c2man_files;
196 struct strarray dlldata_files;
197 struct strarray implib_objs;
198 struct strarray all_targets;
199 struct strarray phony_targets;
200 struct strarray install_rules[NB_INSTALL_RULES];
203 static struct makefile *top_makefile;
205 static const char separator[] = "### Dependencies";
206 static const char *output_makefile_name = "Makefile";
207 static const char *input_file_name;
208 static const char *output_file_name;
209 static const char *temp_file_name;
210 static int relative_dir_mode;
211 static int input_line;
212 static int output_column;
213 static FILE *output_file;
215 static const char Usage[] =
216 "Usage: makedep [options] [directories]\n"
217 "Options:\n"
218 " -R from to Compute the relative path between two directories\n"
219 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
222 #ifndef __GNUC__
223 #define __attribute__(x)
224 #endif
226 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
227 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
228 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
229 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
231 /*******************************************************************
232 * fatal_error
234 static void fatal_error( const char *msg, ... )
236 va_list valist;
237 va_start( valist, msg );
238 if (input_file_name)
240 fprintf( stderr, "%s:", input_file_name );
241 if (input_line) fprintf( stderr, "%d:", input_line );
242 fprintf( stderr, " error: " );
244 else fprintf( stderr, "makedep: error: " );
245 vfprintf( stderr, msg, valist );
246 va_end( valist );
247 exit(1);
251 /*******************************************************************
252 * fatal_perror
254 static void fatal_perror( const char *msg, ... )
256 va_list valist;
257 va_start( valist, msg );
258 if (input_file_name)
260 fprintf( stderr, "%s:", input_file_name );
261 if (input_line) fprintf( stderr, "%d:", input_line );
262 fprintf( stderr, " error: " );
264 else fprintf( stderr, "makedep: error: " );
265 vfprintf( stderr, msg, valist );
266 perror( " " );
267 va_end( valist );
268 exit(1);
272 /*******************************************************************
273 * cleanup_files
275 static void cleanup_files(void)
277 if (temp_file_name) unlink( temp_file_name );
278 if (output_file_name) unlink( output_file_name );
282 /*******************************************************************
283 * exit_on_signal
285 static void exit_on_signal( int sig )
287 exit( 1 ); /* this will call the atexit functions */
291 /*******************************************************************
292 * xmalloc
294 static void *xmalloc( size_t size )
296 void *res;
297 if (!(res = malloc (size ? size : 1)))
298 fatal_error( "Virtual memory exhausted.\n" );
299 return res;
303 /*******************************************************************
304 * xrealloc
306 static void *xrealloc (void *ptr, size_t size)
308 void *res;
309 assert( size );
310 if (!(res = realloc( ptr, size )))
311 fatal_error( "Virtual memory exhausted.\n" );
312 return res;
315 /*******************************************************************
316 * xstrdup
318 static char *xstrdup( const char *str )
320 char *res = strdup( str );
321 if (!res) fatal_error( "Virtual memory exhausted.\n" );
322 return res;
326 /*******************************************************************
327 * strmake
329 static char *strmake( const char* fmt, ... )
331 int n;
332 size_t size = 100;
333 va_list ap;
335 for (;;)
337 char *p = xmalloc (size);
338 va_start(ap, fmt);
339 n = vsnprintf (p, size, fmt, ap);
340 va_end(ap);
341 if (n == -1) size *= 2;
342 else if ((size_t)n >= size) size = n + 1;
343 else return xrealloc( p, n + 1 );
344 free(p);
349 /*******************************************************************
350 * strendswith
352 static int strendswith( const char* str, const char* end )
354 size_t l = strlen( str );
355 size_t m = strlen( end );
357 return l >= m && strcmp(str + l - m, end) == 0;
361 /*******************************************************************
362 * output
364 static void output( const char *format, ... )
366 int ret;
367 va_list valist;
369 va_start( valist, format );
370 ret = vfprintf( output_file, format, valist );
371 va_end( valist );
372 if (ret < 0) fatal_perror( "output" );
373 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
374 else output_column += ret;
378 /*******************************************************************
379 * strarray_add
381 static void strarray_add( struct strarray *array, const char *str )
383 if (array->count == array->size)
385 if (array->size) array->size *= 2;
386 else array->size = 16;
387 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
389 array->str[array->count++] = str;
393 /*******************************************************************
394 * strarray_addall
396 static void strarray_addall( struct strarray *array, struct strarray added )
398 unsigned int i;
400 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
404 /*******************************************************************
405 * strarray_exists
407 static int strarray_exists( const struct strarray *array, const char *str )
409 unsigned int i;
411 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
412 return 0;
416 /*******************************************************************
417 * strarray_add_uniq
419 static void strarray_add_uniq( struct strarray *array, const char *str )
421 if (!strarray_exists( array, str )) strarray_add( array, str );
425 /*******************************************************************
426 * strarray_get_value
428 * Find a value in a name/value pair string array.
430 static const char *strarray_get_value( const struct strarray *array, const char *name )
432 int pos, res, min = 0, max = array->count / 2 - 1;
434 while (min <= max)
436 pos = (min + max) / 2;
437 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
438 if (res < 0) min = pos + 1;
439 else max = pos - 1;
441 return NULL;
445 /*******************************************************************
446 * strarray_set_value
448 * Define a value in a name/value pair string array.
450 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
452 int i, pos, res, min = 0, max = array->count / 2 - 1;
454 while (min <= max)
456 pos = (min + max) / 2;
457 if (!(res = strcmp( array->str[pos * 2], name )))
459 /* redefining a variable replaces the previous value */
460 array->str[pos * 2 + 1] = value;
461 return;
463 if (res < 0) min = pos + 1;
464 else max = pos - 1;
466 strarray_add( array, NULL );
467 strarray_add( array, NULL );
468 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
469 array->str[min * 2] = name;
470 array->str[min * 2 + 1] = value;
474 /*******************************************************************
475 * output_filename
477 static void output_filename( const char *name )
479 if (output_column + strlen(name) + 1 > 100)
481 output( " \\\n" );
482 output( " " );
484 else if (output_column) output( " " );
485 output( "%s", name );
489 /*******************************************************************
490 * output_filenames
492 static void output_filenames( struct strarray array )
494 unsigned int i;
496 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
500 /*******************************************************************
501 * get_extension
503 static char *get_extension( char *filename )
505 char *ext = strrchr( filename, '.' );
506 if (ext && strchr( ext, '/' )) ext = NULL;
507 return ext;
511 /*******************************************************************
512 * replace_extension
514 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
516 char *ret;
517 size_t name_len = strlen( name );
518 size_t ext_len = strlen( old_ext );
520 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
521 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
522 memcpy( ret, name, name_len );
523 strcpy( ret + name_len, new_ext );
524 return ret;
528 /*******************************************************************
529 * replace_filename
531 static char *replace_filename( const char *path, const char *name )
533 const char *p;
534 char *ret;
535 size_t len;
537 if (!path) return xstrdup( name );
538 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
539 len = p - path + 1;
540 ret = xmalloc( len + strlen( name ) + 1 );
541 memcpy( ret, path, len );
542 strcpy( ret + len, name );
543 return ret;
547 /*******************************************************************
548 * strarray_replace_extension
550 static struct strarray strarray_replace_extension( const struct strarray *array,
551 const char *old_ext, const char *new_ext )
553 unsigned int i;
554 struct strarray ret;
556 ret.count = ret.size = array->count;
557 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
558 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
559 return ret;
563 /*******************************************************************
564 * replace_substr
566 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
568 size_t pos = start - str;
569 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
570 memcpy( ret, str, pos );
571 strcpy( ret + pos, replace );
572 strcat( ret + pos, start + len );
573 return ret;
577 /*******************************************************************
578 * get_relative_path
580 * Determine where the destination path is located relative to the 'from' path.
582 static char *get_relative_path( const char *from, const char *dest )
584 const char *start;
585 char *ret, *p;
586 unsigned int dotdots = 0;
588 /* a path of "." is equivalent to an empty path */
589 if (!strcmp( from, "." )) from = "";
591 for (;;)
593 while (*from == '/') from++;
594 while (*dest == '/') dest++;
595 start = dest; /* save start of next path element */
596 if (!*from) break;
598 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
599 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
601 /* count remaining elements in 'from' */
604 dotdots++;
605 while (*from && *from != '/') from++;
606 while (*from == '/') from++;
608 while (*from);
609 break;
612 if (!start[0] && !dotdots) return NULL; /* empty path */
614 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
615 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
617 if (start[0]) strcpy( p, start );
618 else p[-1] = 0; /* remove trailing slash */
619 return ret;
623 /*******************************************************************
624 * concat_paths
626 static char *concat_paths( const char *base, const char *path )
628 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
629 if (!path || !path[0]) return xstrdup( base );
630 if (path[0] == '/') return xstrdup( path );
631 return strmake( "%s/%s", base, path );
635 /*******************************************************************
636 * base_dir_path
638 static char *base_dir_path( const struct makefile *make, const char *path )
640 return concat_paths( make->base_dir, path );
644 /*******************************************************************
645 * obj_dir_path
647 static char *obj_dir_path( const struct makefile *make, const char *path )
649 return concat_paths( make->obj_dir, path );
653 /*******************************************************************
654 * src_dir_path
656 static char *src_dir_path( const struct makefile *make, const char *path )
658 if (make->src_dir) return concat_paths( make->src_dir, path );
659 return obj_dir_path( make, path );
663 /*******************************************************************
664 * top_obj_dir_path
666 static char *top_obj_dir_path( const struct makefile *make, const char *path )
668 return concat_paths( make->top_obj_dir, path );
672 /*******************************************************************
673 * top_src_dir_path
675 static char *top_src_dir_path( const struct makefile *make, const char *path )
677 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
678 return top_obj_dir_path( make, path );
682 /*******************************************************************
683 * root_dir_path
685 static char *root_dir_path( const char *path )
687 return concat_paths( root_src_dir, path );
691 /*******************************************************************
692 * tools_dir_path
694 static char *tools_dir_path( const struct makefile *make, const char *path )
696 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
697 return top_obj_dir_path( make, strmake( "tools/%s", path ));
701 /*******************************************************************
702 * tools_path
704 static char *tools_path( const struct makefile *make, const char *name )
706 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
710 /*******************************************************************
711 * get_line
713 static char *get_line( FILE *file )
715 static char *buffer;
716 static size_t size;
718 if (!size)
720 size = 1024;
721 buffer = xmalloc( size );
723 if (!fgets( buffer, size, file )) return NULL;
724 input_line++;
726 for (;;)
728 char *p = buffer + strlen(buffer);
729 /* if line is larger than buffer, resize buffer */
730 while (p == buffer + size - 1 && p[-1] != '\n')
732 buffer = xrealloc( buffer, size * 2 );
733 if (!fgets( buffer + size - 1, size + 1, file )) break;
734 p = buffer + strlen(buffer);
735 size *= 2;
737 if (p > buffer && p[-1] == '\n')
739 *(--p) = 0;
740 if (p > buffer && p[-1] == '\r') *(--p) = 0;
741 if (p > buffer && p[-1] == '\\')
743 *(--p) = 0;
744 /* line ends in backslash, read continuation line */
745 if (!fgets( p, size - (p - buffer), file )) return buffer;
746 input_line++;
747 continue;
750 return buffer;
755 /*******************************************************************
756 * hash_filename
758 static unsigned int hash_filename( const char *name )
760 /* FNV-1 hash */
761 unsigned int ret = 2166136261u;
762 while (*name) ret = (ret * 16777619) ^ *name++;
763 return ret % HASH_SIZE;
767 /*******************************************************************
768 * add_file
770 static struct file *add_file( const char *name )
772 struct file *file = xmalloc( sizeof(*file) );
773 memset( file, 0, sizeof(*file) );
774 file->name = xstrdup( name );
775 return file;
779 /*******************************************************************
780 * add_dependency
782 static void add_dependency( struct file *file, const char *name, enum incl_type type )
784 /* enforce some rules for the Wine tree */
786 if (!memcmp( name, "../", 3 ))
787 fatal_error( "#include directive with relative path not allowed\n" );
789 if (!strcmp( name, "config.h" ))
791 if (strendswith( file->name, ".h" ))
792 fatal_error( "config.h must not be included by a header file\n" );
793 if (file->deps_count)
794 fatal_error( "config.h must be included before anything else\n" );
796 else if (!strcmp( name, "wine/port.h" ))
798 if (strendswith( file->name, ".h" ))
799 fatal_error( "wine/port.h must not be included by a header file\n" );
800 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
801 if (file->deps_count > 1)
802 fatal_error( "wine/port.h must be included before everything except config.h\n" );
803 if (strcmp( file->deps[0].name, "config.h" ))
804 fatal_error( "config.h must be included before wine/port.h\n" );
807 if (file->deps_count >= file->deps_size)
809 file->deps_size *= 2;
810 if (file->deps_size < 16) file->deps_size = 16;
811 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
813 file->deps[file->deps_count].line = input_line;
814 file->deps[file->deps_count].type = type;
815 file->deps[file->deps_count].name = xstrdup( name );
816 file->deps_count++;
820 /*******************************************************************
821 * find_src_file
823 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
825 struct incl_file *file;
827 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
828 if (!strcmp( name, file->name )) return file;
829 return NULL;
832 /*******************************************************************
833 * find_include_file
835 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
837 struct incl_file *file;
839 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
840 if (!strcmp( name, file->name )) return file;
841 return NULL;
844 /*******************************************************************
845 * add_include
847 * Add an include file if it doesn't already exists.
849 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
850 const char *name, int line, enum incl_type type )
852 struct incl_file *include;
854 if (parent->files_count >= parent->files_size)
856 parent->files_size *= 2;
857 if (parent->files_size < 16) parent->files_size = 16;
858 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
861 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
862 if (!strcmp( name, include->name )) goto found;
864 include = xmalloc( sizeof(*include) );
865 memset( include, 0, sizeof(*include) );
866 include->name = xstrdup(name);
867 include->included_by = parent;
868 include->included_line = line;
869 include->type = type;
870 list_add_tail( &make->includes, &include->entry );
871 found:
872 parent->files[parent->files_count++] = include;
873 return include;
877 /*******************************************************************
878 * add_generated_source
880 * Add a generated source file to the list.
882 static struct incl_file *add_generated_source( struct makefile *make,
883 const char *name, const char *filename )
885 struct incl_file *file;
887 if ((file = find_src_file( make, name ))) return file; /* we already have it */
888 file = xmalloc( sizeof(*file) );
889 memset( file, 0, sizeof(*file) );
890 file->file = add_file( name );
891 file->name = xstrdup( name );
892 file->filename = obj_dir_path( make, filename ? filename : name );
893 file->file->flags = FLAG_GENERATED;
894 list_add_tail( &make->sources, &file->entry );
895 return file;
899 /*******************************************************************
900 * parse_include_directive
902 static void parse_include_directive( struct file *source, char *str )
904 char quote, *include, *p = str;
906 while (*p && isspace(*p)) p++;
907 if (*p != '\"' && *p != '<' ) return;
908 quote = *p++;
909 if (quote == '<') quote = '>';
910 include = p;
911 while (*p && (*p != quote)) p++;
912 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
913 *p = 0;
914 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
918 /*******************************************************************
919 * parse_pragma_directive
921 static void parse_pragma_directive( struct file *source, char *str )
923 char *flag, *p = str;
925 if (!isspace( *p )) return;
926 while (*p && isspace(*p)) p++;
927 p = strtok( p, " \t" );
928 if (strcmp( p, "makedep" )) return;
930 while ((flag = strtok( NULL, " \t" )))
932 if (!strcmp( flag, "depend" ))
934 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
935 return;
937 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
939 if (strendswith( source->name, ".idl" ))
941 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
942 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
943 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
944 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
945 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
946 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
947 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
948 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
950 else if (strendswith( source->name, ".rc" ))
952 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
954 else if (strendswith( source->name, ".sfd" ))
956 if (!strcmp( flag, "font" ))
958 struct strarray *array = source->args;
960 if (!array)
962 source->args = array = xmalloc( sizeof(*array) );
963 *array = empty_strarray;
964 source->flags |= FLAG_SFD_FONTS;
966 strarray_add( array, xstrdup( strtok( NULL, "" )));
967 return;
970 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
975 /*******************************************************************
976 * parse_cpp_directive
978 static void parse_cpp_directive( struct file *source, char *str )
980 while (*str && isspace(*str)) str++;
981 if (*str++ != '#') return;
982 while (*str && isspace(*str)) str++;
984 if (!strncmp( str, "include", 7 ))
985 parse_include_directive( source, str + 7 );
986 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
987 parse_include_directive( source, str + 6 );
988 else if (!strncmp( str, "pragma", 6 ))
989 parse_pragma_directive( source, str + 6 );
993 /*******************************************************************
994 * parse_idl_file
996 static void parse_idl_file( struct file *source, FILE *file )
998 char *buffer, *include;
1000 input_line = 0;
1002 while ((buffer = get_line( file )))
1004 char quote;
1005 char *p = buffer;
1006 while (*p && isspace(*p)) p++;
1008 if (!strncmp( p, "importlib", 9 ))
1010 p += 9;
1011 while (*p && isspace(*p)) p++;
1012 if (*p++ != '(') continue;
1013 while (*p && isspace(*p)) p++;
1014 if (*p++ != '"') continue;
1015 include = p;
1016 while (*p && (*p != '"')) p++;
1017 if (!*p) fatal_error( "malformed importlib directive\n" );
1018 *p = 0;
1019 add_dependency( source, include, INCL_IMPORTLIB );
1020 continue;
1023 if (!strncmp( p, "import", 6 ))
1025 p += 6;
1026 while (*p && isspace(*p)) p++;
1027 if (*p != '"') continue;
1028 include = ++p;
1029 while (*p && (*p != '"')) p++;
1030 if (!*p) fatal_error( "malformed import directive\n" );
1031 *p = 0;
1032 add_dependency( source, include, INCL_IMPORT );
1033 continue;
1036 /* check for #include inside cpp_quote */
1037 if (!strncmp( p, "cpp_quote", 9 ))
1039 p += 9;
1040 while (*p && isspace(*p)) p++;
1041 if (*p++ != '(') continue;
1042 while (*p && isspace(*p)) p++;
1043 if (*p++ != '"') continue;
1044 if (*p++ != '#') continue;
1045 while (*p && isspace(*p)) p++;
1046 if (strncmp( p, "include", 7 )) continue;
1047 p += 7;
1048 while (*p && isspace(*p)) p++;
1049 if (*p == '\\' && p[1] == '"')
1051 p += 2;
1052 quote = '"';
1054 else
1056 if (*p++ != '<' ) continue;
1057 quote = '>';
1059 include = p;
1060 while (*p && (*p != quote)) p++;
1061 if (!*p || (quote == '"' && p[-1] != '\\'))
1062 fatal_error( "malformed #include directive inside cpp_quote\n" );
1063 if (quote == '"') p--; /* remove backslash */
1064 *p = 0;
1065 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1066 continue;
1069 parse_cpp_directive( source, p );
1073 /*******************************************************************
1074 * parse_c_file
1076 static void parse_c_file( struct file *source, FILE *file )
1078 char *buffer;
1080 input_line = 0;
1081 while ((buffer = get_line( file )))
1083 parse_cpp_directive( source, buffer );
1088 /*******************************************************************
1089 * parse_rc_file
1091 static void parse_rc_file( struct file *source, FILE *file )
1093 char *buffer, *include;
1095 input_line = 0;
1096 while ((buffer = get_line( file )))
1098 char quote;
1099 char *p = buffer;
1100 while (*p && isspace(*p)) p++;
1102 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1104 p += 2;
1105 while (*p && isspace(*p)) p++;
1106 if (strncmp( p, "@makedep:", 9 )) continue;
1107 p += 9;
1108 while (*p && isspace(*p)) p++;
1109 quote = '"';
1110 if (*p == quote)
1112 include = ++p;
1113 while (*p && *p != quote) p++;
1115 else
1117 include = p;
1118 while (*p && !isspace(*p) && *p != '*') p++;
1120 if (!*p)
1121 fatal_error( "malformed makedep comment\n" );
1122 *p = 0;
1123 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1124 continue;
1127 parse_cpp_directive( source, buffer );
1132 /*******************************************************************
1133 * parse_in_file
1135 static void parse_in_file( struct file *source, FILE *file )
1137 char *p, *buffer;
1139 /* make sure it gets rebuilt when the version changes */
1140 add_dependency( source, "config.h", INCL_SYSTEM );
1142 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1144 input_line = 0;
1145 while ((buffer = get_line( file )))
1147 if (strncmp( buffer, ".TH", 3 )) continue;
1148 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1149 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1150 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1151 source->args = xstrdup( p );
1152 return;
1157 /*******************************************************************
1158 * parse_sfd_file
1160 static void parse_sfd_file( struct file *source, FILE *file )
1162 char *p, *eol, *buffer;
1164 input_line = 0;
1165 while ((buffer = get_line( file )))
1167 if (strncmp( buffer, "UComments:", 10 )) continue;
1168 p = buffer + 10;
1169 while (*p == ' ') p++;
1170 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1172 p++;
1173 buffer[strlen(buffer) - 1] = 0;
1175 while ((eol = strstr( p, "+AAoA" )))
1177 *eol = 0;
1178 while (*p && isspace(*p)) p++;
1179 if (*p++ == '#')
1181 while (*p && isspace(*p)) p++;
1182 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1184 p = eol + 5;
1186 while (*p && isspace(*p)) p++;
1187 if (*p++ != '#') return;
1188 while (*p && isspace(*p)) p++;
1189 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1190 return;
1195 static const struct
1197 const char *ext;
1198 void (*parse)( struct file *file, FILE *f );
1199 } parse_functions[] =
1201 { ".c", parse_c_file },
1202 { ".h", parse_c_file },
1203 { ".inl", parse_c_file },
1204 { ".l", parse_c_file },
1205 { ".m", parse_c_file },
1206 { ".rh", parse_c_file },
1207 { ".x", parse_c_file },
1208 { ".y", parse_c_file },
1209 { ".idl", parse_idl_file },
1210 { ".rc", parse_rc_file },
1211 { ".in", parse_in_file },
1212 { ".sfd", parse_sfd_file }
1215 /*******************************************************************
1216 * load_file
1218 static struct file *load_file( const char *name )
1220 struct file *file;
1221 FILE *f;
1222 unsigned int i, hash = hash_filename( name );
1224 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1225 if (!strcmp( name, file->name )) return file;
1227 if (!(f = fopen( name, "r" ))) return NULL;
1229 file = add_file( name );
1230 list_add_tail( &files[hash], &file->entry );
1231 input_file_name = file->name;
1232 input_line = 0;
1234 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1236 if (!strendswith( name, parse_functions[i].ext )) continue;
1237 parse_functions[i].parse( file, f );
1238 break;
1241 fclose( f );
1242 input_file_name = NULL;
1244 return file;
1248 /*******************************************************************
1249 * open_include_path_file
1251 * Open a file from a directory on the include path.
1253 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1254 const char *name, char **filename )
1256 char *src_path = base_dir_path( make, concat_paths( dir, name ));
1257 struct file *ret = load_file( src_path );
1259 if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
1260 return ret;
1264 /*******************************************************************
1265 * open_file_same_dir
1267 * Open a file in the same directory as the parent.
1269 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1271 char *src_path = replace_filename( parent->file->name, name );
1272 struct file *ret = load_file( src_path );
1274 if (ret) *filename = replace_filename( parent->filename, name );
1275 free( src_path );
1276 return ret;
1280 /*******************************************************************
1281 * open_local_file
1283 * Open a file in the source directory of the makefile.
1285 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1287 char *src_path = root_dir_path( base_dir_path( make, path ));
1288 struct file *ret = load_file( src_path );
1290 /* if not found, try parent dir */
1291 if (!ret && make->parent_dir)
1293 free( src_path );
1294 path = strmake( "%s/%s", make->parent_dir, path );
1295 src_path = root_dir_path( base_dir_path( make, path ));
1296 ret = load_file( src_path );
1299 if (ret) *filename = src_dir_path( make, path );
1300 free( src_path );
1301 return ret;
1305 /*******************************************************************
1306 * open_global_file
1308 * Open a file in the top-level source directory.
1310 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1312 char *src_path = root_dir_path( path );
1313 struct file *ret = load_file( src_path );
1315 if (ret) *filename = top_src_dir_path( make, path );
1316 free( src_path );
1317 return ret;
1321 /*******************************************************************
1322 * open_global_header
1324 * Open a file in the global include source directory.
1326 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1328 return open_global_file( make, strmake( "include/%s", path ), filename );
1332 /*******************************************************************
1333 * open_src_file
1335 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1337 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1339 if (!file) fatal_perror( "open %s", pFile->name );
1340 return file;
1344 /*******************************************************************
1345 * open_include_file
1347 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1349 struct file *file = NULL;
1350 char *filename;
1351 unsigned int i, len;
1353 errno = ENOENT;
1355 /* check for generated bison header */
1357 if (strendswith( pFile->name, ".tab.h" ) &&
1358 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1360 pFile->sourcename = filename;
1361 pFile->filename = obj_dir_path( make, pFile->name );
1362 return file;
1365 /* check for corresponding idl file in source dir */
1367 if (strendswith( pFile->name, ".h" ) &&
1368 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1370 pFile->sourcename = filename;
1371 pFile->filename = obj_dir_path( make, pFile->name );
1372 return file;
1375 /* check for corresponding tlb file in source dir */
1377 if (strendswith( pFile->name, ".tlb" ) &&
1378 (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1380 pFile->sourcename = filename;
1381 pFile->filename = obj_dir_path( make, pFile->name );
1382 return file;
1385 /* now try in source dir */
1386 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1388 /* check for corresponding idl file in global includes */
1390 if (strendswith( pFile->name, ".h" ) &&
1391 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1393 pFile->sourcename = filename;
1394 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1395 return file;
1398 /* check for corresponding .in file in global includes (for config.h.in) */
1400 if (strendswith( pFile->name, ".h" ) &&
1401 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1403 pFile->sourcename = filename;
1404 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1405 return file;
1408 /* check for corresponding .x file in global includes */
1410 if (strendswith( pFile->name, "tmpl.h" ) &&
1411 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1413 pFile->sourcename = filename;
1414 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1415 return file;
1418 /* check for corresponding .tlb file in global includes */
1420 if (strendswith( pFile->name, ".tlb" ) &&
1421 (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1423 pFile->sourcename = filename;
1424 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1425 return file;
1428 /* check in global includes source dir */
1430 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1432 /* check in global msvcrt includes */
1433 if (make->use_msvcrt &&
1434 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1435 return file;
1437 /* now search in include paths */
1438 for (i = 0; i < make->include_paths.count; i++)
1440 const char *dir = make->include_paths.str[i];
1441 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1443 if (prefix)
1445 len = strlen( prefix );
1446 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1448 while (dir[len] == '/') len++;
1449 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1450 if (file) return file;
1452 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1454 if (*dir != '/')
1456 if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1457 return file;
1460 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1462 /* try in src file directory */
1463 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1465 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1466 perror( pFile->name );
1467 pFile = pFile->included_by;
1468 while (pFile && pFile->included_by)
1470 const char *parent = pFile->included_by->sourcename;
1471 if (!parent) parent = pFile->included_by->file->name;
1472 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1473 parent, pFile->included_line, pFile->name );
1474 pFile = pFile->included_by;
1476 exit(1);
1480 /*******************************************************************
1481 * add_all_includes
1483 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1485 unsigned int i;
1487 parent->files_count = 0;
1488 parent->files_size = file->deps_count;
1489 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1490 for (i = 0; i < file->deps_count; i++)
1492 switch (file->deps[i].type)
1494 case INCL_NORMAL:
1495 case INCL_IMPORT:
1496 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1497 break;
1498 case INCL_IMPORTLIB:
1499 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1500 break;
1501 case INCL_SYSTEM:
1502 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1503 break;
1504 case INCL_CPP_QUOTE:
1505 case INCL_CPP_QUOTE_SYSTEM:
1506 break;
1512 /*******************************************************************
1513 * parse_file
1515 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1517 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1519 if (!file) return;
1521 source->file = file;
1522 source->files_count = 0;
1523 source->files_size = file->deps_count;
1524 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1526 if (source->sourcename)
1528 if (strendswith( source->sourcename, ".idl" ))
1530 unsigned int i;
1532 if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
1534 /* generated .h file always includes these */
1535 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1536 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1537 for (i = 0; i < file->deps_count; i++)
1539 switch (file->deps[i].type)
1541 case INCL_IMPORT:
1542 if (strendswith( file->deps[i].name, ".idl" ))
1543 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1544 file->deps[i].line, INCL_NORMAL );
1545 else
1546 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1547 break;
1548 case INCL_CPP_QUOTE:
1549 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1550 break;
1551 case INCL_CPP_QUOTE_SYSTEM:
1552 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1553 break;
1554 case INCL_NORMAL:
1555 case INCL_SYSTEM:
1556 case INCL_IMPORTLIB:
1557 break;
1560 return;
1562 if (strendswith( source->sourcename, ".y" ))
1563 return; /* generated .tab.h doesn't include anything */
1566 add_all_includes( make, source, file );
1570 /*******************************************************************
1571 * add_src_file
1573 * Add a source file to the list.
1575 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1577 struct incl_file *file;
1579 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1580 file = xmalloc( sizeof(*file) );
1581 memset( file, 0, sizeof(*file) );
1582 file->name = xstrdup(name);
1583 list_add_tail( &make->sources, &file->entry );
1584 parse_file( make, file, 1 );
1585 return file;
1589 /*******************************************************************
1590 * open_input_makefile
1592 static FILE *open_input_makefile( const struct makefile *make )
1594 FILE *ret;
1596 if (make->base_dir)
1597 input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
1598 else
1599 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1601 input_line = 0;
1602 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1603 return ret;
1607 /*******************************************************************
1608 * get_make_variable
1610 static const char *get_make_variable( const struct makefile *make, const char *name )
1612 const char *ret;
1614 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1615 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1616 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1617 return NULL;
1621 /*******************************************************************
1622 * get_expanded_make_variable
1624 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1626 const char *var;
1627 char *p, *end, *expand, *tmp;
1629 var = get_make_variable( make, name );
1630 if (!var) return NULL;
1632 p = expand = xstrdup( var );
1633 while ((p = strchr( p, '$' )))
1635 if (p[1] == '(')
1637 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1638 *end++ = 0;
1639 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1640 var = get_make_variable( make, p + 2 );
1641 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1642 /* switch to the new string */
1643 p = tmp + (p - expand);
1644 free( expand );
1645 expand = tmp;
1647 else if (p[1] == '{') /* don't expand ${} variables */
1649 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1650 p = end + 1;
1652 else if (p[1] == '$')
1654 p += 2;
1656 else fatal_error( "syntax error in '%s'\n", expand );
1659 /* consider empty variables undefined */
1660 p = expand;
1661 while (*p && isspace(*p)) p++;
1662 if (*p) return expand;
1663 free( expand );
1664 return NULL;
1668 /*******************************************************************
1669 * get_expanded_make_var_array
1671 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1673 struct strarray ret = empty_strarray;
1674 char *value, *token;
1676 if ((value = get_expanded_make_variable( make, name )))
1677 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1678 strarray_add( &ret, token );
1679 return ret;
1683 /*******************************************************************
1684 * get_expanded_file_local_var
1686 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1687 const char *name )
1689 char *p, *var = strmake( "%s_%s", file, name );
1691 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1692 return get_expanded_make_var_array( make, var );
1696 /*******************************************************************
1697 * set_make_variable
1699 static int set_make_variable( struct strarray *array, const char *assignment )
1701 char *p, *name;
1703 p = name = xstrdup( assignment );
1704 while (isalnum(*p) || *p == '_') p++;
1705 if (name == p) return 0; /* not a variable */
1706 if (isspace(*p))
1708 *p++ = 0;
1709 while (isspace(*p)) p++;
1711 if (*p != '=') return 0; /* not an assignment */
1712 *p++ = 0;
1713 while (isspace(*p)) p++;
1715 strarray_set_value( array, name, p );
1716 return 1;
1720 /*******************************************************************
1721 * parse_makefile
1723 static struct makefile *parse_makefile( const char *path )
1725 char *buffer;
1726 FILE *file;
1727 struct makefile *make = xmalloc( sizeof(*make) );
1729 memset( make, 0, sizeof(*make) );
1730 if (path)
1732 make->top_obj_dir = get_relative_path( path, "" );
1733 make->base_dir = path;
1734 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1737 file = open_input_makefile( make );
1738 while ((buffer = get_line( file )))
1740 if (!strncmp( buffer, separator, strlen(separator) )) break;
1741 if (*buffer == '\t') continue; /* command */
1742 while (isspace( *buffer )) buffer++;
1743 if (*buffer == '#') continue; /* comment */
1744 set_make_variable( &make->vars, buffer );
1746 fclose( file );
1747 input_file_name = NULL;
1748 return make;
1752 /*******************************************************************
1753 * add_generated_sources
1755 static void add_generated_sources( struct makefile *make )
1757 struct incl_file *source, *next, *file;
1759 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1761 if (source->file->flags & FLAG_IDL_CLIENT)
1763 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1764 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1765 add_all_includes( make, file, file->file );
1767 if (source->file->flags & FLAG_IDL_SERVER)
1769 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1770 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1771 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1772 add_all_includes( make, file, file->file );
1774 if (source->file->flags & FLAG_IDL_IDENT)
1776 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1777 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1778 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1779 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1780 add_all_includes( make, file, file->file );
1782 if (source->file->flags & FLAG_IDL_PROXY)
1784 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1785 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1786 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1787 add_all_includes( make, file, file->file );
1788 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1789 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1790 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1791 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1792 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1793 add_all_includes( make, file, file->file );
1795 if (source->file->flags & FLAG_IDL_TYPELIB)
1797 add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
1799 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1801 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1803 if (source->file->flags & FLAG_IDL_REGISTER)
1805 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1807 if (source->file->flags & FLAG_IDL_HEADER)
1809 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1811 if (!source->file->flags && strendswith( source->name, ".idl" ))
1813 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1815 if (strendswith( source->name, ".x" ))
1817 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1819 if (strendswith( source->name, ".y" ))
1821 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1822 /* steal the includes list from the source file */
1823 file->files_count = source->files_count;
1824 file->files_size = source->files_size;
1825 file->files = source->files;
1826 source->files_count = source->files_size = 0;
1827 source->files = NULL;
1829 if (strendswith( source->name, ".l" ))
1831 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1832 /* steal the includes list from the source file */
1833 file->files_count = source->files_count;
1834 file->files_size = source->files_size;
1835 file->files = source->files;
1836 source->files_count = source->files_size = 0;
1837 source->files = NULL;
1839 if (source->file->flags & FLAG_C_IMPLIB)
1841 if (!make->staticimplib && make->importlib && *dll_ext)
1842 make->staticimplib = strmake( "lib%s.a", make->importlib );
1844 if (strendswith( source->name, ".po" ))
1846 if (!make->disabled)
1847 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1850 if (make->testdll)
1852 file = add_generated_source( make, "testlist.o", "testlist.c" );
1853 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1854 add_all_includes( make, file, file->file );
1859 /*******************************************************************
1860 * create_dir
1862 static void create_dir( const char *dir )
1864 char *p, *path;
1866 p = path = xstrdup( dir );
1867 while ((p = strchr( p, '/' )))
1869 *p = 0;
1870 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1871 *p++ = '/';
1872 while (*p == '/') p++;
1874 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1875 free( path );
1879 /*******************************************************************
1880 * create_file_directories
1882 * Create the base directories of all the files.
1884 static void create_file_directories( const struct makefile *make, struct strarray files )
1886 struct strarray subdirs = empty_strarray;
1887 unsigned int i;
1888 char *dir;
1890 for (i = 0; i < files.count; i++)
1892 if (!strchr( files.str[i], '/' )) continue;
1893 dir = base_dir_path( make, files.str[i] );
1894 *strrchr( dir, '/' ) = 0;
1895 strarray_add_uniq( &subdirs, dir );
1898 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1902 /*******************************************************************
1903 * output_filenames_obj_dir
1905 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1907 unsigned int i;
1909 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1913 /*******************************************************************
1914 * get_dependencies
1916 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1918 unsigned int i;
1920 if (!file->filename) return;
1922 if (file != source)
1924 if (file->owner == source) return; /* already processed */
1925 if (file->type == INCL_IMPORTLIB &&
1926 !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1927 return; /* library is imported only when building a typelib */
1928 file->owner = source;
1929 strarray_add( &source->dependencies, file->filename );
1931 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
1935 /*******************************************************************
1936 * get_local_dependencies
1938 * Get the local dependencies of a given target.
1940 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1941 struct strarray targets )
1943 unsigned int i;
1944 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
1946 for (i = 0; i < deps.count; i++)
1948 if (strarray_exists( &targets, deps.str[i] ))
1949 deps.str[i] = obj_dir_path( make, deps.str[i] );
1950 else
1951 deps.str[i] = src_dir_path( make, deps.str[i] );
1953 return deps;
1957 /*******************************************************************
1958 * get_static_lib
1960 * Check if makefile builds the named static library and return the full lib path.
1962 static const char *get_static_lib( const struct makefile *make, const char *name )
1964 if (!make->staticlib) return NULL;
1965 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
1966 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
1967 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
1968 return base_dir_path( make, make->staticlib );
1972 /*******************************************************************
1973 * add_default_libraries
1975 static struct strarray add_default_libraries( const struct makefile *make, struct strarray *deps )
1977 struct strarray ret = empty_strarray;
1978 struct strarray all_libs = empty_strarray;
1979 unsigned int i, j;
1981 strarray_add( &all_libs, "-lwine_port" );
1982 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
1983 strarray_addall( &all_libs, libs );
1985 for (i = 0; i < all_libs.count; i++)
1987 const char *lib = NULL;
1989 if (!strncmp( all_libs.str[i], "-l", 2 ))
1991 const char *name = all_libs.str[i] + 2;
1993 for (j = 0; j < top_makefile->subdirs.count; j++)
1995 const struct makefile *submake = top_makefile->submakes[j];
1997 if ((lib = get_static_lib( submake, name ))) break;
2001 if (lib)
2003 lib = top_obj_dir_path( make, lib );
2004 strarray_add( deps, lib );
2005 strarray_add( &ret, lib );
2007 else strarray_add( &ret, all_libs.str[i] );
2009 return ret;
2013 /*******************************************************************
2014 * add_import_libs
2016 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2017 struct strarray imports, int cross )
2019 struct strarray ret = empty_strarray;
2020 unsigned int i, j;
2022 for (i = 0; i < imports.count; i++)
2024 const char *name = imports.str[i];
2025 const char *lib = NULL;
2027 for (j = 0; j < top_makefile->subdirs.count; j++)
2029 const struct makefile *submake = top_makefile->submakes[j];
2031 if (submake->importlib && !strcmp( submake->importlib, name ))
2033 if (cross || !*dll_ext || submake->staticimplib)
2034 lib = base_dir_path( submake, strmake( "lib%s.a", name ));
2035 else
2036 strarray_add( deps, top_obj_dir_path( make,
2037 strmake( "%s/lib%s.def", submake->base_dir, name )));
2038 break;
2041 if ((lib = get_static_lib( submake, name ))) break;
2044 if (lib)
2046 if (cross) lib = replace_extension( lib, ".a", ".cross.a" );
2047 lib = top_obj_dir_path( make, lib );
2048 strarray_add( deps, lib );
2049 strarray_add( &ret, lib );
2051 else strarray_add( &ret, strmake( "-l%s", name ));
2053 return ret;
2057 /*******************************************************************
2058 * get_default_imports
2060 static struct strarray get_default_imports( const struct makefile *make )
2062 struct strarray ret = empty_strarray;
2064 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
2065 if (strarray_exists( &make->appmode, "-mno-cygwin" )) strarray_add( &ret, "msvcrt" );
2066 if (make->is_win16) strarray_add( &ret, "kernel" );
2067 strarray_add( &ret, "kernel32" );
2068 strarray_add( &ret, "ntdll" );
2069 strarray_add( &ret, "winecrt0" );
2070 return ret;
2074 /*******************************************************************
2075 * add_install_rule
2077 static void add_install_rule( struct makefile *make, const char *target,
2078 const char *file, const char *dest )
2080 if (strarray_exists( &make->install_lib, target ))
2082 strarray_add( &make->install_rules[INSTALL_LIB], file );
2083 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2085 else if (strarray_exists( &make->install_dev, target ))
2087 strarray_add( &make->install_rules[INSTALL_DEV], file );
2088 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2093 /*******************************************************************
2094 * get_include_install_path
2096 * Determine the installation path for a given include file.
2098 static const char *get_include_install_path( const char *name )
2100 if (!strncmp( name, "wine/", 5 )) return name + 5;
2101 if (!strncmp( name, "msvcrt/", 7 )) return name;
2102 return strmake( "windows/%s", name );
2106 /*******************************************************************
2107 * get_shared_library_name
2109 * Determine possible names for a shared library with a version number.
2111 static struct strarray get_shared_lib_names( const char *libname )
2113 struct strarray ret = empty_strarray;
2114 const char *ext, *p;
2115 char *name, *first, *second;
2116 size_t len = 0;
2118 strarray_add( &ret, libname );
2120 for (p = libname; (p = strchr( p, '.' )); p++)
2121 if ((len = strspn( p + 1, "0123456789." ))) break;
2123 if (!len) return ret;
2124 ext = p + 1 + len;
2125 if (*ext && ext[-1] == '.') ext--;
2127 /* keep only the first group of digits */
2128 name = xstrdup( libname );
2129 first = name + (p - libname);
2130 if ((second = strchr( first + 1, '.' )))
2132 strcpy( second, ext );
2133 strarray_add( &ret, xstrdup( name ));
2135 /* now remove all digits */
2136 strcpy( first, ext );
2137 strarray_add( &ret, name );
2138 return ret;
2142 /*******************************************************************
2143 * output_symlink_rule
2145 * Output a rule to create a symlink.
2147 static void output_symlink_rule( const char *src_name, const char *link_name )
2149 const char *name;
2151 output( "\trm -f %s && ", link_name );
2153 /* dest path with a directory needs special handling if ln -s isn't supported */
2154 if (strcmp( ln_s, "ln -s" ) && ((name = strrchr( link_name, '/' ))))
2156 char *dir = xstrdup( link_name );
2157 dir[name - link_name] = 0;
2158 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2159 free( dir );
2161 else
2163 output( "%s %s %s\n", ln_s, src_name, link_name );
2168 /*******************************************************************
2169 * output_install_rules
2171 * Rules are stored as a (file,dest) pair of values.
2172 * The first char of dest indicates the type of install.
2174 static struct strarray output_install_rules( struct makefile *make, enum install_rules rules,
2175 const char *target )
2177 unsigned int i;
2178 char *install_sh;
2179 struct strarray files = make->install_rules[rules];
2180 struct strarray uninstall = empty_strarray;
2181 struct strarray targets = empty_strarray;
2183 if (!files.count) return uninstall;
2185 for (i = 0; i < files.count; i += 2)
2187 const char *file = files.str[i];
2188 switch (*files.str[i + 1])
2190 case 'd': /* data file */
2191 case 'p': /* program file */
2192 case 's': /* script */
2193 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2194 break;
2195 case 't': /* script in tools dir */
2196 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2197 break;
2201 output( "install %s::", target );
2202 output_filenames( targets );
2203 output( "\n" );
2205 install_sh = top_src_dir_path( make, "tools/install-sh" );
2206 for (i = 0; i < files.count; i += 2)
2208 const char *file = files.str[i];
2209 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2211 switch (*files.str[i + 1])
2213 case 'd': /* data file */
2214 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2215 install_sh, obj_dir_path( make, file ), dest );
2216 break;
2217 case 'D': /* data file in source dir */
2218 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2219 install_sh, src_dir_path( make, file ), dest );
2220 break;
2221 case 'p': /* program file */
2222 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2223 install_sh, obj_dir_path( make, file ), dest );
2224 break;
2225 case 's': /* script */
2226 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2227 install_sh, obj_dir_path( make, file ), dest );
2228 break;
2229 case 'S': /* script in source dir */
2230 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2231 install_sh, src_dir_path( make, file ), dest );
2232 break;
2233 case 't': /* script in tools dir */
2234 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2235 install_sh, tools_dir_path( make, file ), dest );
2236 break;
2237 case 'y': /* symlink */
2238 output_symlink_rule( file, dest );
2239 break;
2240 default:
2241 assert(0);
2243 strarray_add( &uninstall, dest );
2246 strarray_add_uniq( &make->phony_targets, "install" );
2247 strarray_add_uniq( &make->phony_targets, target );
2248 return uninstall;
2252 /*******************************************************************
2253 * output_importlib_symlinks
2255 static struct strarray output_importlib_symlinks( const struct makefile *parent,
2256 const struct makefile *make )
2258 struct strarray ret = empty_strarray;
2259 const char *lib, *dst;
2261 if (!make->module) return ret;
2262 if (!make->importlib) return ret;
2263 if (make->is_win16 && make->disabled) return ret;
2264 if (strncmp( make->base_dir, "dlls/", 5 )) return ret;
2265 if (!strcmp( make->module, make->importlib )) return ret;
2266 if (!strchr( make->importlib, '.' ) &&
2267 !strncmp( make->module, make->importlib, strlen( make->importlib )) &&
2268 !strcmp( make->module + strlen( make->importlib ), ".dll" ))
2269 return ret;
2271 lib = strmake( "lib%s.%s", make->importlib, *dll_ext ? "def" : "a" );
2272 dst = concat_paths( obj_dir_path( parent, "dlls" ), lib );
2273 output( "%s: %s\n", dst, base_dir_path( make, lib ));
2274 output_symlink_rule( concat_paths( make->base_dir + strlen("dlls/"), lib ), dst );
2275 strarray_add( &ret, dst );
2277 if (crosstarget && !make->is_win16)
2279 lib = strmake( "lib%s.cross.a", make->importlib );
2280 dst = concat_paths( obj_dir_path( parent, "dlls" ), lib );
2281 output( "%s: %s\n", dst, base_dir_path( make, lib ));
2282 output_symlink_rule( concat_paths( make->base_dir + strlen("dlls/"), lib ), dst );
2283 strarray_add( &ret, dst );
2285 return ret;
2289 /*******************************************************************
2290 * output_po_files
2292 static void output_po_files( const struct makefile *make )
2294 const char *po_dir = src_dir_path( make, "po" );
2295 struct strarray pot_files = empty_strarray;
2296 struct incl_file *source;
2297 unsigned int i;
2299 for (i = 0; i < make->subdirs.count; i++)
2301 struct makefile *submake = make->submakes[i];
2303 LIST_FOR_EACH_ENTRY( source, &submake->sources, struct incl_file, entry )
2305 if (strendswith( source->name, ".rc" ) && (source->file->flags & FLAG_RC_PO))
2307 char *pot_file = replace_extension( source->name, ".rc", ".pot" );
2308 char *pot_path = base_dir_path( submake, pot_file );
2309 output( "%s: tools/wrc include dummy\n", pot_path );
2310 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2311 strarray_add( &pot_files, pot_path );
2313 else if (strendswith( source->name, ".mc" ))
2315 char *pot_file = replace_extension( source->name, ".mc", ".pot" );
2316 char *pot_path = base_dir_path( submake, pot_file );
2317 output( "%s: tools/wmc include dummy\n", pot_path );
2318 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2319 strarray_add( &pot_files, pot_path );
2323 if (linguas.count)
2325 for (i = 0; i < linguas.count; i++)
2326 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2327 output( ": %s/wine.pot\n", po_dir );
2328 output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2329 po_dir );
2330 output( "po:" );
2331 for (i = 0; i < linguas.count; i++)
2332 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2333 output( "\n" );
2335 output( "%s/wine.pot:", po_dir );
2336 output_filenames( pot_files );
2337 output( "\n" );
2338 output( "\tmsgcat -o $@" );
2339 output_filenames( pot_files );
2340 output( "\n" );
2344 /*******************************************************************
2345 * output_source_y
2347 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2349 /* add source file dependency for parallel makes */
2350 char *header = strmake( "%s.tab.h", obj );
2352 if (find_include_file( make, header ))
2354 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2355 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
2356 obj, obj_dir_path( make, obj ), source->filename );
2357 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2358 source->filename, obj_dir_path( make, header ));
2359 strarray_add( &make->clean_files, header );
2361 else output( "%s.tab.c: %s\n", obj, source->filename );
2363 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
2367 /*******************************************************************
2368 * output_source_l
2370 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2372 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2373 output( "\t$(FLEX) -o$@ %s\n", source->filename );
2377 /*******************************************************************
2378 * output_source_h
2380 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2382 if (source->file->flags & FLAG_GENERATED)
2384 strarray_add( &make->all_targets, source->name );
2386 else
2388 strarray_add( &make->install_rules[INSTALL_DEV], source->name );
2389 strarray_add( &make->install_rules[INSTALL_DEV],
2390 strmake( "D$(includedir)/%s", get_include_install_path( source->name ) ));
2395 /*******************************************************************
2396 * output_source_rc
2398 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2400 struct strarray extradefs = get_expanded_file_local_var( make, obj, "EXTRADEFS" );
2401 unsigned int i;
2403 strarray_add( &make->object_files, strmake( "%s.res", obj ));
2404 if (crosstarget) strarray_add( &make->crossobj_files, strmake( "%s.res", obj ));
2405 output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
2406 output( "\t%s -o $@", tools_path( make, "wrc" ) );
2407 if (make->is_win16) output_filename( "-m16" );
2408 else output_filenames( target_flags );
2409 output_filename( "--nostdinc" );
2410 output_filenames( make->include_args );
2411 output_filenames( make->define_args );
2412 output_filenames( extradefs );
2413 if (linguas.count && (source->file->flags & FLAG_RC_PO))
2415 char *po_dir = top_obj_dir_path( make, "po" );
2416 output_filename( strmake( "--po-dir=%s", po_dir ));
2417 output_filename( source->filename );
2418 output( "\n" );
2419 output( "%s.res:", obj_dir_path( make, obj ));
2420 for (i = 0; i < linguas.count; i++)
2421 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2422 output( "\n" );
2424 else
2426 output_filename( source->filename );
2427 output( "\n" );
2429 if (source->file->flags & FLAG_RC_PO)
2431 strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
2432 output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
2433 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ) );
2434 if (make->is_win16) output_filename( "-m16" );
2435 else output_filenames( target_flags );
2436 output_filename( "--nostdinc" );
2437 output_filenames( make->include_args );
2438 output_filenames( make->define_args );
2439 output_filenames( extradefs );
2440 output_filename( source->filename );
2441 output( "\n" );
2442 output( "%s.pot ", obj_dir_path( make, obj ));
2444 output( "%s.res:", obj_dir_path( make, obj ));
2445 output_filename( tools_path( make, "wrc" ));
2446 output_filenames( source->dependencies );
2447 output( "\n" );
2451 /*******************************************************************
2452 * output_source_mc
2454 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2456 unsigned int i;
2458 strarray_add( &make->object_files, strmake( "%s.res", obj ));
2459 if (crosstarget) strarray_add( &make->crossobj_files, strmake( "%s.res", obj ));
2460 strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
2461 output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
2462 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
2463 if (linguas.count)
2465 char *po_dir = top_obj_dir_path( make, "po" );
2466 output_filename( strmake( "--po-dir=%s", po_dir ));
2467 output( "\n" );
2468 output( "%s.res:", obj_dir_path( make, obj ));
2469 for (i = 0; i < linguas.count; i++)
2470 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2472 output( "\n" );
2473 output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
2474 output( "\t%s -O pot -o $@ %s", tools_path( make, "wmc" ), source->filename );
2475 output( "\n" );
2476 output( "%s.pot %s.res:", obj_dir_path( make, obj ), obj_dir_path( make, obj ));
2477 output_filename( tools_path( make, "wmc" ));
2478 output_filenames( source->dependencies );
2479 output( "\n" );
2483 /*******************************************************************
2484 * output_source_res
2486 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2488 strarray_add( &make->object_files, source->name );
2489 if (crosstarget) strarray_add( &make->crossobj_files, source->name );
2493 /*******************************************************************
2494 * output_source_idl
2496 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2498 struct strarray extradefs = get_expanded_file_local_var( make, obj, "EXTRADEFS" );
2499 struct strarray targets = empty_strarray;
2500 char *dest;
2501 unsigned int i;
2503 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2504 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2506 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2508 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2509 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2510 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2511 strarray_add( &targets, dest );
2513 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2514 if (source->file->flags & FLAG_INSTALL)
2516 strarray_add( &make->install_rules[INSTALL_DEV], xstrdup( source->name ));
2517 strarray_add( &make->install_rules[INSTALL_DEV],
2518 strmake( "D$(includedir)/%s.idl", get_include_install_path( obj ) ));
2519 if (source->file->flags & FLAG_IDL_HEADER)
2521 strarray_add( &make->install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2522 strarray_add( &make->install_rules[INSTALL_DEV],
2523 strmake( "d$(includedir)/%s.h", get_include_install_path( obj ) ));
2526 if (!targets.count) return;
2528 output_filenames_obj_dir( make, targets );
2529 output( ": %s\n", tools_path( make, "widl" ));
2530 output( "\t%s -o $@", tools_path( make, "widl" ) );
2531 output_filenames( target_flags );
2532 output_filenames( make->include_args );
2533 output_filenames( make->define_args );
2534 output_filenames( extradefs );
2535 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2536 output_filename( source->filename );
2537 output( "\n" );
2538 output_filenames_obj_dir( make, targets );
2539 output( ": %s", source->filename );
2540 output_filenames( source->dependencies );
2541 output( "\n" );
2545 /*******************************************************************
2546 * output_source_tlb
2548 static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
2550 strarray_add( &make->all_targets, source->name );
2554 /*******************************************************************
2555 * output_source_x
2557 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2559 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2560 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2561 output( "\t%s%s -H -o $@ %s\n",
2562 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2563 if (source->file->flags & FLAG_INSTALL)
2565 strarray_add( &make->install_rules[INSTALL_DEV], source->name );
2566 strarray_add( &make->install_rules[INSTALL_DEV],
2567 strmake( "D$(includedir)/%s", get_include_install_path( source->name ) ));
2568 strarray_add( &make->install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2569 strarray_add( &make->install_rules[INSTALL_DEV],
2570 strmake( "d$(includedir)/%s.h", get_include_install_path( obj ) ));
2575 /*******************************************************************
2576 * output_source_sfd
2578 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2580 unsigned int i;
2581 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
2583 if (fontforge && !make->src_dir)
2585 output( "%s: %s\n", ttf_file, source->filename );
2586 output( "\t%s -script %s %s $@\n",
2587 fontforge, top_src_dir_path( make, "fonts/genttf.ff" ), source->filename );
2588 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
2590 if (source->file->flags & FLAG_INSTALL)
2592 strarray_add( &make->install_rules[INSTALL_LIB], strmake( "%s.ttf", obj ));
2593 strarray_add( &make->install_rules[INSTALL_LIB], strmake( "D$(fontdir)/%s.ttf", obj ));
2595 if (source->file->flags & FLAG_SFD_FONTS)
2597 struct strarray *array = source->file->args;
2599 for (i = 0; i < array->count; i++)
2601 char *font = strtok( xstrdup(array->str[i]), " \t" );
2602 char *args = strtok( NULL, "" );
2604 strarray_add( &make->all_targets, xstrdup( font ));
2605 output( "%s: %s %s\n", obj_dir_path( make, font ),
2606 tools_path( make, "sfnt2fon" ), ttf_file );
2607 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2608 strarray_add( &make->install_rules[INSTALL_LIB], xstrdup(font) );
2609 strarray_add( &make->install_rules[INSTALL_LIB], strmake( "d$(fontdir)/%s", font ));
2615 /*******************************************************************
2616 * output_source_svg
2618 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2620 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2621 unsigned int i;
2623 if (convert && rsvg && icotool && !make->src_dir)
2625 for (i = 0; images[i]; i++)
2626 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2628 if (images[i])
2630 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2631 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2632 top_src_dir_path( make, "tools/buildimage" ), source->filename );
2638 /*******************************************************************
2639 * output_source_po
2641 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2643 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2644 output( "\t%s -o $@ %s\n", msgfmt, source->filename );
2645 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2649 /*******************************************************************
2650 * output_source_in
2652 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2654 unsigned int i;
2656 if (strendswith( obj, ".man" ) && source->file->args)
2658 struct strarray symlinks;
2659 char *dir, *dest = replace_extension( obj, ".man", "" );
2660 char *lang = strchr( dest, '.' );
2661 char *section = source->file->args;
2662 if (lang)
2664 *lang++ = 0;
2665 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2667 else dir = strmake( "$(mandir)/man%s", section );
2668 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2669 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2670 for (i = 0; i < symlinks.count; i++)
2671 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2672 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2673 free( dest );
2674 free( dir );
2676 strarray_add( &make->in_files, xstrdup(obj) );
2677 strarray_add( &make->all_targets, xstrdup(obj) );
2678 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2679 output( "\t$(SED_CMD) %s >$@ || (rm -f $@ && false)\n", source->filename );
2680 output( "%s:", obj_dir_path( make, obj ));
2681 output_filenames( source->dependencies );
2682 output( "\n" );
2683 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2687 /*******************************************************************
2688 * output_source_spec
2690 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2692 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2693 struct strarray all_libs, dep_libs = empty_strarray;
2695 if (!imports.count) imports = make->imports;
2696 all_libs = add_import_libs( make, &dep_libs, imports, 0 );
2697 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
2698 strarray_addall( &all_libs, libs );
2700 strarray_add( &make->clean_files, strmake( "%s.dll%s", obj, dll_ext ));
2701 strarray_add( &make->object_files, strmake( "%s.res", obj ));
2702 output( "%s.res: %s.dll%s\n", obj_dir_path( make, obj ), obj_dir_path( make, obj ), dll_ext );
2703 output( "\techo \"%s.dll TESTDLL \\\"%s.dll%s\\\"\" | %s -o $@\n", obj,
2704 obj_dir_path( make, obj ), dll_ext, tools_path( make, "wrc" ));
2706 output( "%s.dll%s:", obj_dir_path( make, obj ), dll_ext );
2707 output_filename( source->filename );
2708 output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
2709 output_filenames( dep_libs );
2710 output_filename( tools_path( make, "winebuild" ));
2711 output_filename( tools_path( make, "winegcc" ));
2712 output( "\n" );
2713 output( "\t%s -s -o $@", tools_path( make, "winegcc" ));
2714 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2715 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2716 output_filenames( target_flags );
2717 output_filenames( unwind_flags );
2718 output_filenames( make->extradllflags );
2719 output_filename( "-shared" );
2720 output_filename( source->filename );
2721 output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
2722 output_filenames( all_libs );
2723 output_filename( "$(LDFLAGS)" );
2724 output( "\n" );
2726 if (crosstarget)
2728 dep_libs = empty_strarray;
2729 all_libs = add_import_libs( make, &dep_libs, imports, 1 );
2730 add_import_libs( make, &dep_libs, get_default_imports( make ), 1 ); /* dependencies only */
2731 strarray_addall( &all_libs, libs );
2733 strarray_add( &make->clean_files, strmake( "%s.dll", obj ));
2734 strarray_add( &make->crossobj_files, strmake( "%s.cross.res", obj ));
2735 output( "%s.cross.res: %s.dll\n", obj_dir_path( make, obj ), obj_dir_path( make, obj ) );
2736 output( "\techo \"%s.dll TESTDLL \\\"%s.dll\\\"\" | %s -o $@\n", obj,
2737 obj_dir_path( make, obj ), tools_path( make, "wrc" ));
2739 output( "%s.dll:", obj_dir_path( make, obj ));
2740 output_filename( source->filename );
2741 output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
2742 output_filenames( dep_libs );
2743 output_filename( tools_path( make, "winebuild" ));
2744 output_filename( tools_path( make, "winegcc" ));
2745 output( "\n" );
2746 output( "\t%s -s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2747 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2748 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2749 output_filename( "--lib-suffix=.cross.a" );
2750 output_filename( "-shared" );
2751 output_filename( source->filename );
2752 output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
2753 output_filenames( all_libs );
2754 output_filename( "$(LDFLAGS)" );
2755 output( "\n" );
2760 /*******************************************************************
2761 * output_source_default
2763 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
2765 struct strarray extradefs = get_expanded_file_local_var( make, obj, "EXTRADEFS" );
2766 int is_dll_src = (make->testdll &&
2767 strendswith( source->name, ".c" ) &&
2768 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
2769 int need_cross = (make->testdll ||
2770 (source->file->flags & FLAG_C_IMPLIB) ||
2771 (make->module && make->staticlib));
2773 if ((source->file->flags & FLAG_GENERATED) &&
2774 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2775 strarray_add( &make->clean_files, source->filename );
2776 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
2777 strarray_add( is_dll_src ? &make->clean_files : &make->object_files, strmake( "%s.o", obj ));
2778 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2779 output( "\t$(CC) -c -o $@ %s", source->filename );
2780 output_filenames( make->include_args );
2781 output_filenames( make->define_args );
2782 output_filenames( extradefs );
2783 if (make->module || make->staticlib || make->sharedlib || make->testdll)
2785 output_filenames( dll_flags );
2786 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2788 output_filenames( extra_cflags );
2789 output_filenames( cpp_flags );
2790 output_filename( "$(CFLAGS)" );
2791 output( "\n" );
2792 if (crosstarget && need_cross)
2794 strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
2795 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2796 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2797 output_filenames( make->include_args );
2798 output_filenames( make->define_args );
2799 output_filenames( extradefs );
2800 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2801 output_filename( "-DWINE_CROSSTEST" );
2802 output_filenames( cpp_flags );
2803 output_filename( "$(CROSSCFLAGS)" );
2804 output( "\n" );
2806 if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
2808 strarray_add( &make->c2man_files, source->filename );
2809 if (make->testdll && !is_dll_src)
2811 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
2812 output( "%s.ok:\n", obj_dir_path( make, obj ));
2813 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2814 top_src_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ),
2815 make->testdll, replace_extension( make->testdll, ".dll", "_test.exe" ),
2816 dll_ext, obj );
2819 output( "%s.o", obj_dir_path( make, obj ));
2820 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2821 output( ":" );
2822 output_filenames( source->dependencies );
2823 output( "\n" );
2827 /* dispatch table to output rules for a single source file */
2828 static const struct
2830 const char *ext;
2831 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
2832 } output_source_funcs[] =
2834 { "y", output_source_y },
2835 { "l", output_source_l },
2836 { "h", output_source_h },
2837 { "rh", output_source_h },
2838 { "inl", output_source_h },
2839 { "rc", output_source_rc },
2840 { "mc", output_source_mc },
2841 { "res", output_source_res },
2842 { "idl", output_source_idl },
2843 { "tlb", output_source_tlb },
2844 { "sfd", output_source_sfd },
2845 { "svg", output_source_svg },
2846 { "po", output_source_po },
2847 { "in", output_source_in },
2848 { "x", output_source_x },
2849 { "spec", output_source_spec },
2850 { NULL, output_source_default }
2854 /*******************************************************************
2855 * output_man_pages
2857 static void output_man_pages( struct makefile *make )
2859 if (make->c2man_files.count)
2861 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2863 output( "manpages::\n" );
2864 output( "\t%s -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
2865 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
2866 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
2867 output_filename( strmake( "-o %s/man%s",
2868 top_obj_dir_path( make, "documentation" ), man_ext ));
2869 output_filenames( make->c2man_files );
2870 output( "\n" );
2871 output( "htmlpages::\n" );
2872 output( "\t%s -Th -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
2873 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
2874 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
2875 output_filename( strmake( "-o %s",
2876 top_obj_dir_path( make, "documentation/html" )));
2877 output_filenames( make->c2man_files );
2878 output( "\n" );
2879 output( "sgmlpages::\n" );
2880 output( "\t%s -Ts -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
2881 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
2882 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
2883 output_filename( strmake( "-o %s",
2884 top_obj_dir_path( make, "documentation/api-guide" )));
2885 output_filenames( make->c2man_files );
2886 output( "\n" );
2887 output( "xmlpages::\n" );
2888 output( "\t%s -Tx -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
2889 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
2890 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
2891 output_filename( strmake( "-o %s",
2892 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2893 output_filenames( make->c2man_files );
2894 output( "\n" );
2895 strarray_add( &make->phony_targets, "manpages" );
2896 strarray_add( &make->phony_targets, "htmlpages" );
2897 strarray_add( &make->phony_targets, "sgmlpages" );
2898 strarray_add( &make->phony_targets, "xmlpages" );
2900 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2904 /*******************************************************************
2905 * output_module
2907 static void output_module( struct makefile *make )
2909 struct strarray all_libs = empty_strarray;
2910 struct strarray dep_libs = empty_strarray;
2911 char *module_path = obj_dir_path( make, make->module );
2912 char *spec_file = NULL;
2913 unsigned int i;
2915 if (!make->appmode.count)
2916 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2917 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 0 ));
2918 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0 ));
2919 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
2920 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
2922 if (*dll_ext)
2924 for (i = 0; i < make->delayimports.count; i++)
2925 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2926 strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
2927 strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
2928 add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
2929 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
2930 add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
2931 strmake( "d$(fakedlldir)/%s", make->module ));
2932 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2934 else
2936 strarray_add( &all_libs, "-lwine" );
2937 strarray_add( &make->all_targets, make->module );
2938 add_install_rule( make, make->module, make->module,
2939 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
2940 output( "%s:", module_path );
2942 if (spec_file) output_filename( spec_file );
2943 output_filenames_obj_dir( make, make->object_files );
2944 output_filenames( dep_libs );
2945 output_filename( tools_path( make, "winebuild" ));
2946 output_filename( tools_path( make, "winegcc" ));
2947 output( "\n" );
2948 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2949 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2950 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2951 output_filenames( target_flags );
2952 output_filenames( unwind_flags );
2953 if (spec_file)
2955 output( " -shared %s", spec_file );
2956 output_filenames( make->extradllflags );
2958 else output_filenames( make->appmode );
2959 output_filenames_obj_dir( make, make->object_files );
2960 output_filenames( all_libs );
2961 output_filename( "$(LDFLAGS)" );
2962 output( "\n" );
2964 if (spec_file && make->importlib)
2966 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2967 if (*dll_ext && !make->implib_objs.count)
2969 strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
2970 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2971 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2972 output_filenames( target_flags );
2973 if (make->is_win16) output_filename( "-m16" );
2974 output( "\n" );
2975 add_install_rule( make, make->importlib,
2976 strmake( "lib%s.def", make->importlib ),
2977 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
2979 else
2981 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
2982 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2983 output_filenames_obj_dir( make, make->implib_objs );
2984 output( "\n" );
2985 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2986 output_filenames( target_flags );
2987 output_filenames_obj_dir( make, make->implib_objs );
2988 output( "\n" );
2989 add_install_rule( make, make->importlib,
2990 strmake( "lib%s.a", make->importlib ),
2991 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
2993 if (crosstarget && !make->is_win16)
2995 struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
2996 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
2997 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2998 output_filenames_obj_dir( make, cross_files );
2999 output( "\n" );
3000 output( "\t%s -b %s -w --implib -o $@ --export %s",
3001 tools_path( make, "winebuild" ), crosstarget, spec_file );
3002 output_filenames_obj_dir( make, cross_files );
3003 output( "\n" );
3007 if (spec_file)
3008 output_man_pages( make );
3009 else if (*dll_ext)
3011 char *binary = replace_extension( make->module, ".exe", "" );
3012 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3017 /*******************************************************************
3018 * output_static_lib
3020 static void output_static_lib( struct makefile *make )
3022 strarray_add( &make->all_targets, make->staticlib );
3023 output( "%s:", obj_dir_path( make, make->staticlib ));
3024 output_filenames_obj_dir( make, make->object_files );
3025 output( "\n\trm -f $@\n" );
3026 output( "\t$(AR) $(ARFLAGS) $@" );
3027 output_filenames_obj_dir( make, make->object_files );
3028 output( "\n\t$(RANLIB) $@\n" );
3029 add_install_rule( make, make->staticlib, make->staticlib,
3030 strmake( "d$(dlldir)/%s", make->staticlib ));
3031 if (crosstarget && make->module)
3033 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3035 strarray_add( &make->all_targets, name );
3036 output( "%s:", obj_dir_path( make, name ));
3037 output_filenames_obj_dir( make, make->crossobj_files );
3038 output( "\n\trm -f $@\n" );
3039 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
3040 output_filenames_obj_dir( make, make->crossobj_files );
3041 output( "\n\t%s-ranlib $@\n", crosstarget );
3046 /*******************************************************************
3047 * output_shared_lib
3049 static void output_shared_lib( struct makefile *make )
3051 unsigned int i;
3052 char *basename, *p;
3053 struct strarray names = get_shared_lib_names( make->sharedlib );
3054 struct strarray all_libs = empty_strarray;
3055 struct strarray dep_libs = empty_strarray;
3057 basename = xstrdup( make->sharedlib );
3058 if ((p = strchr( basename, '.' ))) *p = 0;
3060 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3061 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3062 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
3064 output( "%s:", obj_dir_path( make, make->sharedlib ));
3065 output_filenames_obj_dir( make, make->object_files );
3066 output_filenames( dep_libs );
3067 output( "\n" );
3068 output( "\t$(CC) -o $@" );
3069 output_filenames_obj_dir( make, make->object_files );
3070 output_filenames( all_libs );
3071 output_filename( "$(LDFLAGS)" );
3072 output( "\n" );
3073 add_install_rule( make, make->sharedlib, make->sharedlib,
3074 strmake( "p$(libdir)/%s", make->sharedlib ));
3075 for (i = 1; i < names.count; i++)
3077 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3078 output_symlink_rule( obj_dir_path( make, names.str[i-1] ), obj_dir_path( make, names.str[i] ));
3079 add_install_rule( make, names.str[i], names.str[i-1],
3080 strmake( "y$(libdir)/%s", names.str[i] ));
3082 strarray_addall( &make->all_targets, names );
3086 /*******************************************************************
3087 * output_import_lib
3089 static void output_import_lib( struct makefile *make )
3091 char *def_file = replace_extension( make->importlib, ".a", ".def" );
3093 /* stand-alone import lib (for libwine) */
3094 if (!strncmp( def_file, "lib", 3 )) def_file += 3;
3095 output( "%s: %s\n", obj_dir_path( make, make->importlib ), src_dir_path( make, def_file ));
3096 output( "\t%s -l $@ -d %s\n", dlltool, src_dir_path( make, def_file ));
3097 add_install_rule( make, make->importlib, make->importlib, strmake( "d$(libdir)/%s", make->importlib ));
3098 strarray_add( &make->all_targets, make->importlib );
3102 /*******************************************************************
3103 * output_test_module
3105 static void output_test_module( struct makefile *make )
3107 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3108 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3109 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3110 struct strarray dep_libs = empty_strarray;
3111 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0 );
3113 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
3114 strarray_addall( &all_libs, libs );
3115 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, dll_ext ));
3116 strarray_add( &make->clean_files, strmake( "%s%s", stripped, dll_ext ));
3117 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
3118 output( "\t%s -o $@", tools_path( make, "winegcc" ));
3119 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
3120 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
3121 output_filenames( target_flags );
3122 output_filenames( unwind_flags );
3123 output_filenames( make->appmode );
3124 output_filenames_obj_dir( make, make->object_files );
3125 output_filenames( all_libs );
3126 output_filename( "$(LDFLAGS)" );
3127 output( "\n" );
3128 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
3129 output( "\t%s -s -o $@", tools_path( make, "winegcc" ));
3130 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
3131 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
3132 output_filenames( target_flags );
3133 output_filenames( unwind_flags );
3134 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3135 output_filenames( make->appmode );
3136 output_filenames_obj_dir( make, make->object_files );
3137 output_filenames( all_libs );
3138 output_filename( "$(LDFLAGS)" );
3139 output( "\n" );
3140 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
3141 obj_dir_path( make, stripped ), dll_ext );
3142 output_filenames_obj_dir( make, make->object_files );
3143 output_filenames( dep_libs );
3144 output_filename( tools_path( make, "winebuild" ));
3145 output_filename( tools_path( make, "winegcc" ));
3146 output( "\n" );
3148 if (!make->disabled)
3149 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
3150 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
3151 obj_dir_path( make, stripped ), dll_ext );
3152 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
3153 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
3155 if (crosstarget)
3157 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
3159 dep_libs = empty_strarray;
3160 all_libs = add_import_libs( make, &dep_libs, make->imports, 1 );
3161 add_import_libs( make, &dep_libs, get_default_imports( make ), 1 ); /* dependencies only */
3162 strarray_addall( &all_libs, libs );
3163 strarray_add( &make->clean_files, crosstest );
3164 output( "%s:", obj_dir_path( make, crosstest ));
3165 output_filenames_obj_dir( make, make->crossobj_files );
3166 output_filenames( dep_libs );
3167 output_filename( tools_path( make, "winebuild" ));
3168 output_filename( tools_path( make, "winegcc" ));
3169 output( "\n" );
3170 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
3171 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
3172 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
3173 output_filename( "--lib-suffix=.cross.a" );
3174 output_filenames_obj_dir( make, make->crossobj_files );
3175 output_filenames( all_libs );
3176 output_filename( "$(LDFLAGS)" );
3177 output( "\n" );
3178 if (!make->disabled)
3180 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
3181 strarray_add( &make->phony_targets, obj_dir_path( make, "crosstest" ));
3182 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
3186 output_filenames_obj_dir( make, make->ok_files );
3187 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
3188 if (!make->disabled)
3190 output( "check test:" );
3191 output_filenames_obj_dir( make, make->ok_files );
3192 output( "\n" );
3193 strarray_add( &make->phony_targets, "check" );
3194 strarray_add( &make->phony_targets, "test" );
3196 output( "testclean::\n" );
3197 output( "\trm -f" );
3198 output_filenames_obj_dir( make, make->ok_files );
3199 output( "\n" );
3200 strarray_addall( &make->clean_files, make->ok_files );
3201 strarray_add( &make->phony_targets, "testclean" );
3205 /*******************************************************************
3206 * output_programs
3208 static void output_programs( struct makefile *make )
3210 unsigned int i, j;
3211 char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
3212 char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
3214 for (i = 0; i < make->programs.count; i++)
3216 char *program_installed = NULL;
3217 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3218 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3219 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3220 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3221 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3223 if (!objs.count) objs = make->object_files;
3224 strarray_addall( &all_libs, add_default_libraries( make, &deps ));
3226 output( "%s:", obj_dir_path( make, program ) );
3227 output_filenames_obj_dir( make, objs );
3228 output_filenames( deps );
3229 output( "\n" );
3230 output( "\t$(CC) -o $@" );
3231 output_filenames_obj_dir( make, objs );
3233 if (strarray_exists( &all_libs, "-lwine" ))
3235 strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
3236 if (ldrpath_local && ldrpath_install)
3238 program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
3239 output_filename( ldrpath_local );
3240 output_filenames( all_libs );
3241 output_filename( "$(LDFLAGS)" );
3242 output( "\n" );
3243 output( "%s:", obj_dir_path( make, program_installed ) );
3244 output_filenames_obj_dir( make, objs );
3245 output_filenames( deps );
3246 output( "\n" );
3247 output( "\t$(CC) -o $@" );
3248 output_filenames_obj_dir( make, objs );
3249 output_filename( ldrpath_install );
3250 strarray_add( &make->all_targets, program_installed );
3254 output_filenames( all_libs );
3255 output_filename( "$(LDFLAGS)" );
3256 output( "\n" );
3257 strarray_add( &make->all_targets, program );
3259 for (j = 0; j < symlinks.count; j++)
3261 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3262 output_symlink_rule( obj_dir_path( make, program ), obj_dir_path( make, symlinks.str[j] ));
3264 strarray_addall( &make->all_targets, symlinks );
3266 add_install_rule( make, program, program_installed ? program_installed : program,
3267 strmake( "p$(bindir)/%s", program ));
3268 for (j = 0; j < symlinks.count; j++)
3269 add_install_rule( make, symlinks.str[j], program,
3270 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3275 /*******************************************************************
3276 * output_subdirs
3278 static void output_subdirs( struct makefile *make )
3280 struct strarray build_deps = empty_strarray;
3281 struct strarray makefile_deps = empty_strarray;
3282 struct strarray distclean_files = get_expanded_make_var_array( make, "CONFIGURE_TARGETS" );
3283 unsigned int i;
3285 strarray_add( &distclean_files, obj_dir_path( make, output_makefile_name ));
3286 if (!make->src_dir) strarray_add( &distclean_files, obj_dir_path( make, ".gitignore" ));
3287 for (i = 0; i < make->subdirs.count; i++)
3289 const struct makefile *submake = make->submakes[i];
3291 strarray_add( &makefile_deps, top_src_dir_path( make, base_dir_path( submake,
3292 strmake ( "%s.in", output_makefile_name ))));
3293 strarray_add( &distclean_files, base_dir_path( submake, output_makefile_name ));
3294 if (!make->src_dir) strarray_add( &distclean_files, base_dir_path( submake, ".gitignore" ));
3295 if (submake->testdll) strarray_add( &distclean_files, base_dir_path( submake, "testlist.c" ));
3296 strarray_addall( &build_deps, output_importlib_symlinks( make, submake ));
3298 output( "Makefile:" );
3299 output_filenames( makefile_deps );
3300 output( "\n" );
3301 output_filenames( makefile_deps );
3302 output( ":\n" );
3303 output( "distclean::\n");
3304 output( "\trm -f" );
3305 output_filenames( distclean_files );
3306 output( "\n" );
3307 strarray_add( &make->phony_targets, "distclean" );
3309 if (build_deps.count)
3311 output( "__builddeps__:" );
3312 output_filenames( build_deps );
3313 output( "\n" );
3314 strarray_addall( &make->clean_files, build_deps );
3316 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3320 /*******************************************************************
3321 * output_sources
3323 static struct strarray output_sources( struct makefile *make )
3325 struct incl_file *source;
3326 unsigned int i, j;
3327 struct strarray uninstall_files = empty_strarray;
3329 strarray_add( &make->phony_targets, "all" );
3331 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3333 char *obj = xstrdup( source->name );
3334 char *ext = get_extension( obj );
3336 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3337 *ext++ = 0;
3339 for (j = 0; output_source_funcs[j].ext; j++)
3340 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3342 output_source_funcs[j].fn( make, source, obj );
3343 free( obj );
3346 if (make->dlldata_files.count)
3348 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3349 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3350 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
3351 output_filenames( make->dlldata_files );
3352 output( "\n" );
3355 if (make->staticlib) output_static_lib( make );
3356 else if (make->module) output_module( make );
3357 else if (make->importlib) output_import_lib( make );
3358 else if (make->sharedlib) output_shared_lib( make );
3359 else if (make->testdll) output_test_module( make );
3361 if (make->programs.count) output_programs( make );
3363 for (i = 0; i < make->scripts.count; i++)
3364 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3365 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3367 if (make->subdirs.count) output_subdirs( make );
3369 if (!make->disabled)
3371 if (make->all_targets.count)
3373 output( "all:" );
3374 output_filenames_obj_dir( make, make->all_targets );
3375 output( "\n" );
3377 strarray_addall( &uninstall_files, output_install_rules( make, INSTALL_LIB, "install-lib" ));
3378 strarray_addall( &uninstall_files, output_install_rules( make, INSTALL_DEV, "install-dev" ));
3379 if (uninstall_files.count)
3381 output( "uninstall::\n" );
3382 output( "\trm -f" );
3383 output_filenames( uninstall_files );
3384 output( "\n" );
3385 strarray_add_uniq( &make->phony_targets, "uninstall" );
3389 strarray_addall( &make->clean_files, make->object_files );
3390 for (i = 0; i < make->crossobj_files.count; i++)
3391 strarray_add_uniq( &make->clean_files, make->crossobj_files.str[i] );
3392 strarray_addall( &make->clean_files, make->all_targets );
3393 strarray_addall( &make->clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
3395 if (make->clean_files.count)
3397 output( "%s::\n", obj_dir_path( make, "clean" ));
3398 output( "\trm -f" );
3399 output_filenames_obj_dir( make, make->clean_files );
3400 output( "\n" );
3401 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
3402 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3405 if (make->phony_targets.count)
3407 output( ".PHONY:" );
3408 output_filenames( make->phony_targets );
3409 output( "\n" );
3412 if (!make->base_dir)
3413 strarray_addall( &make->clean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3414 return make->clean_files;
3418 /*******************************************************************
3419 * create_temp_file
3421 static FILE *create_temp_file( const char *orig )
3423 char *name = xmalloc( strlen(orig) + 13 );
3424 unsigned int i, id = getpid();
3425 int fd;
3426 FILE *ret = NULL;
3428 for (i = 0; i < 100; i++)
3430 sprintf( name, "%s.tmp%08x", orig, id );
3431 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3433 ret = fdopen( fd, "w" );
3434 break;
3436 if (errno != EEXIST) break;
3437 id += 7777;
3439 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3440 temp_file_name = name;
3441 return ret;
3445 /*******************************************************************
3446 * rename_temp_file
3448 static void rename_temp_file( const char *dest )
3450 int ret = rename( temp_file_name, dest );
3451 if (ret == -1 && errno == EEXIST)
3453 /* rename doesn't overwrite on windows */
3454 unlink( dest );
3455 ret = rename( temp_file_name, dest );
3457 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3458 temp_file_name = NULL;
3462 /*******************************************************************
3463 * are_files_identical
3465 static int are_files_identical( FILE *file1, FILE *file2 )
3467 for (;;)
3469 char buffer1[8192], buffer2[8192];
3470 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3471 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3472 if (size1 != size2) return 0;
3473 if (!size1) return feof( file1 ) && feof( file2 );
3474 if (memcmp( buffer1, buffer2, size1 )) return 0;
3479 /*******************************************************************
3480 * rename_temp_file_if_changed
3482 static void rename_temp_file_if_changed( const char *dest )
3484 FILE *file1, *file2;
3485 int do_rename = 1;
3487 if ((file1 = fopen( dest, "r" )))
3489 if ((file2 = fopen( temp_file_name, "r" )))
3491 do_rename = !are_files_identical( file1, file2 );
3492 fclose( file2 );
3494 fclose( file1 );
3496 if (!do_rename)
3498 unlink( temp_file_name );
3499 temp_file_name = NULL;
3501 else rename_temp_file( dest );
3505 /*******************************************************************
3506 * output_linguas
3508 static void output_linguas( const struct makefile *make )
3510 const char *dest = base_dir_path( make, "LINGUAS" );
3511 struct incl_file *source;
3513 output_file = create_temp_file( dest );
3515 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3516 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3517 if (strendswith( source->name, ".po" ))
3518 output( "%s\n", replace_extension( source->name, ".po", "" ));
3520 if (fclose( output_file )) fatal_perror( "write" );
3521 output_file = NULL;
3522 rename_temp_file_if_changed( dest );
3526 /*******************************************************************
3527 * output_testlist
3529 static void output_testlist( const struct makefile *make )
3531 const char *dest = base_dir_path( make, "testlist.c" );
3532 struct strarray files = empty_strarray;
3533 unsigned int i;
3535 for (i = 0; i < make->ok_files.count; i++)
3536 strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
3538 output_file = create_temp_file( dest );
3540 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3541 output( "#define WIN32_LEAN_AND_MEAN\n" );
3542 output( "#include <windows.h>\n\n" );
3543 output( "#define STANDALONE\n" );
3544 output( "#include \"wine/test.h\"\n\n" );
3546 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
3547 output( "\n" );
3548 output( "const struct test winetest_testlist[] =\n" );
3549 output( "{\n" );
3550 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
3551 output( " { 0, 0 }\n" );
3552 output( "};\n" );
3554 if (fclose( output_file )) fatal_perror( "write" );
3555 output_file = NULL;
3556 rename_temp_file_if_changed( dest );
3560 /*******************************************************************
3561 * output_gitignore
3563 static void output_gitignore( const char *dest, struct strarray files )
3565 int i;
3567 output_file = create_temp_file( dest );
3569 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3570 for (i = 0; i < files.count; i++)
3572 if (!strchr( files.str[i], '/' )) output( "/" );
3573 output( "%s\n", files.str[i] );
3576 if (fclose( output_file )) fatal_perror( "write" );
3577 output_file = NULL;
3578 rename_temp_file( dest );
3582 /*******************************************************************
3583 * output_top_variables
3585 static void output_top_variables( const struct makefile *make )
3587 unsigned int i;
3588 struct strarray *vars = &top_makefile->vars;
3590 if (!make->base_dir) return; /* don't output variables in the top makefile */
3592 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
3593 output( "all:\n\n" );
3594 for (i = 0; i < vars->count; i += 2)
3596 if (!strcmp( vars->str[i], "SUBDIRS" )) continue; /* not inherited */
3597 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
3599 output( "\n" );
3603 /*******************************************************************
3604 * output_dependencies
3606 static void output_dependencies( struct makefile *make )
3608 struct strarray targets, ignore_files = empty_strarray;
3609 char buffer[1024];
3610 FILE *src_file;
3611 int found = 0;
3613 if (make->base_dir) create_dir( make->base_dir );
3615 output_file_name = base_dir_path( make, output_makefile_name );
3616 output_file = create_temp_file( output_file_name );
3617 output_top_variables( make );
3619 /* copy the contents of the source makefile */
3620 src_file = open_input_makefile( make );
3621 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
3623 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
3624 found = !strncmp( buffer, separator, strlen(separator) );
3626 if (fclose( src_file )) fatal_perror( "close" );
3627 input_file_name = NULL;
3629 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
3630 targets = output_sources( make );
3632 fclose( output_file );
3633 output_file = NULL;
3634 rename_temp_file( output_file_name );
3636 strarray_add( &ignore_files, ".gitignore" );
3637 strarray_add( &ignore_files, "Makefile" );
3638 if (make->testdll)
3640 output_testlist( make );
3641 strarray_add( &ignore_files, "testlist.c" );
3643 if (make->base_dir && !strcmp( make->base_dir, "po" ))
3645 output_linguas( make );
3646 strarray_add( &ignore_files, "LINGUAS" );
3648 strarray_addall( &ignore_files, targets );
3649 if (!make->src_dir) output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
3651 create_file_directories( make, targets );
3653 output_file_name = NULL;
3657 /*******************************************************************
3658 * load_sources
3660 static void load_sources( struct makefile *make )
3662 static const char *source_vars[] =
3664 "SOURCES",
3665 "C_SRCS",
3666 "OBJC_SRCS",
3667 "RC_SRCS",
3668 "MC_SRCS",
3669 "IDL_SRCS",
3670 "BISON_SRCS",
3671 "LEX_SRCS",
3672 "HEADER_SRCS",
3673 "XTEMPLATE_SRCS",
3674 "SVG_SRCS",
3675 "FONT_SRCS",
3676 "IN_SRCS",
3677 "PO_SRCS",
3678 "MANPAGES",
3679 NULL
3681 const char **var;
3682 unsigned int i;
3683 struct strarray value;
3684 struct incl_file *file;
3686 if (root_src_dir)
3688 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
3689 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
3691 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
3692 strarray_set_value( &make->vars, "top_srcdir", top_src_dir_path( make, "" ));
3693 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
3695 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
3696 make->module = get_expanded_make_variable( make, "MODULE" );
3697 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
3698 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
3699 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
3700 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
3702 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
3703 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
3704 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
3705 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
3706 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
3707 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
3708 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
3709 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
3711 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
3713 make->disabled = make->base_dir && strarray_exists( &disabled_dirs, make->base_dir );
3714 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
3715 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
3717 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
3718 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 ) ||
3719 !strcmp( make->imports.str[i], "ucrtbase" );
3721 if (make->module && !make->install_lib.count && !make->install_dev.count)
3723 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
3724 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
3725 else strarray_add( &make->install_lib, make->module );
3728 make->include_paths = empty_strarray;
3729 make->include_args = empty_strarray;
3730 make->define_args = empty_strarray;
3731 strarray_add( &make->define_args, "-D__WINESRC__" );
3733 value = get_expanded_make_var_array( make, "EXTRAINCL" );
3734 for (i = 0; i < value.count; i++)
3735 if (!strncmp( value.str[i], "-I", 2 ))
3736 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
3737 else
3738 strarray_add_uniq( &make->define_args, value.str[i] );
3739 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
3741 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
3742 if (make->src_dir)
3743 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
3744 if (make->parent_dir)
3745 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
3746 strarray_add( &make->include_args, strmake( "-I%s", top_obj_dir_path( make, "include" )));
3747 if (make->top_src_dir)
3748 strarray_add( &make->include_args, strmake( "-I%s", top_src_dir_path( make, "include" )));
3749 if (make->use_msvcrt)
3750 strarray_add( &make->include_args, strmake( "-I%s", top_src_dir_path( make, "include/msvcrt" )));
3751 for (i = 0; i < make->include_paths.count; i++)
3752 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
3754 list_init( &make->sources );
3755 list_init( &make->includes );
3757 for (var = source_vars; *var; var++)
3759 value = get_expanded_make_var_array( make, *var );
3760 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
3763 add_generated_sources( make );
3765 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
3766 for (i = 0; i < value.count; i++)
3768 /* default to .c for unknown extra object files */
3769 if (strendswith( value.str[i], ".o" ))
3770 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
3771 else
3772 add_generated_source( make, value.str[i], NULL );
3775 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
3776 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
3780 /*******************************************************************
3781 * parse_makeflags
3783 static void parse_makeflags( const char *flags )
3785 const char *p = flags;
3786 char *var, *buffer = xmalloc( strlen(flags) + 1 );
3788 while (*p)
3790 while (isspace(*p)) p++;
3791 var = buffer;
3792 while (*p && !isspace(*p))
3794 if (*p == '\\' && p[1]) p++;
3795 *var++ = *p++;
3797 *var = 0;
3798 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
3803 /*******************************************************************
3804 * parse_option
3806 static int parse_option( const char *opt )
3808 if (opt[0] != '-')
3810 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
3811 return 0;
3813 switch(opt[1])
3815 case 'f':
3816 if (opt[2]) output_makefile_name = opt + 2;
3817 break;
3818 case 'R':
3819 relative_dir_mode = 1;
3820 break;
3821 default:
3822 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
3823 exit(1);
3825 return 1;
3829 /*******************************************************************
3830 * main
3832 int main( int argc, char *argv[] )
3834 const char *makeflags = getenv( "MAKEFLAGS" );
3835 int i, j;
3837 if (makeflags) parse_makeflags( makeflags );
3839 i = 1;
3840 while (i < argc)
3842 if (parse_option( argv[i] ))
3844 for (j = i; j < argc; j++) argv[j] = argv[j+1];
3845 argc--;
3847 else i++;
3850 if (relative_dir_mode)
3852 char *relpath;
3854 if (argc != 3)
3856 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
3857 exit( 1 );
3859 relpath = get_relative_path( argv[1], argv[2] );
3860 printf( "%s\n", relpath ? relpath : "." );
3861 exit( 0 );
3864 atexit( cleanup_files );
3865 signal( SIGTERM, exit_on_signal );
3866 signal( SIGINT, exit_on_signal );
3867 #ifdef SIGHUP
3868 signal( SIGHUP, exit_on_signal );
3869 #endif
3871 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
3873 top_makefile = parse_makefile( NULL );
3875 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
3876 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
3877 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
3878 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
3879 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
3880 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
3881 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
3883 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
3884 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
3885 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
3886 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
3887 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
3888 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
3889 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
3890 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
3891 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
3892 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
3893 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
3894 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
3895 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
3896 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
3898 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
3899 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
3900 if (!exe_ext) exe_ext = "";
3901 if (!tools_ext) tools_ext = "";
3902 if (!man_ext) man_ext = "3w";
3904 if (argc == 1)
3906 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
3907 top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
3908 top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );
3910 for (i = 0; i < top_makefile->subdirs.count; i++)
3911 top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i] );
3913 load_sources( top_makefile );
3914 for (i = 0; i < top_makefile->subdirs.count; i++)
3915 load_sources( top_makefile->submakes[i] );
3917 for (i = 0; i < top_makefile->subdirs.count; i++)
3918 output_dependencies( top_makefile->submakes[i] );
3920 output_dependencies( top_makefile );
3921 return 0;
3924 for (i = 1; i < argc; i++)
3926 struct makefile *make = parse_makefile( argv[i] );
3927 load_sources( make );
3928 output_dependencies( make );
3930 return 0;