makefiles: Generate the remaining top-level rules from makedep.
[wine.git] / tools / makedep.c
blobbba25e0b10bbf924da736abe071ac21d488e54db
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 distclean_files;
194 struct strarray uninstall_files;
195 struct strarray object_files;
196 struct strarray crossobj_files;
197 struct strarray c2man_files;
198 struct strarray dlldata_files;
199 struct strarray implib_objs;
200 struct strarray all_targets;
201 struct strarray phony_targets;
202 struct strarray install_rules[NB_INSTALL_RULES];
205 static struct makefile *top_makefile;
207 static const char separator[] = "### Dependencies";
208 static const char *output_makefile_name = "Makefile";
209 static const char *input_file_name;
210 static const char *output_file_name;
211 static const char *temp_file_name;
212 static int relative_dir_mode;
213 static int input_line;
214 static int output_column;
215 static FILE *output_file;
217 static const char Usage[] =
218 "Usage: makedep [options] [directories]\n"
219 "Options:\n"
220 " -R from to Compute the relative path between two directories\n"
221 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
224 #ifndef __GNUC__
225 #define __attribute__(x)
226 #endif
228 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
229 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
230 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
231 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
233 /*******************************************************************
234 * fatal_error
236 static void fatal_error( const char *msg, ... )
238 va_list valist;
239 va_start( valist, msg );
240 if (input_file_name)
242 fprintf( stderr, "%s:", input_file_name );
243 if (input_line) fprintf( stderr, "%d:", input_line );
244 fprintf( stderr, " error: " );
246 else fprintf( stderr, "makedep: error: " );
247 vfprintf( stderr, msg, valist );
248 va_end( valist );
249 exit(1);
253 /*******************************************************************
254 * fatal_perror
256 static void fatal_perror( const char *msg, ... )
258 va_list valist;
259 va_start( valist, msg );
260 if (input_file_name)
262 fprintf( stderr, "%s:", input_file_name );
263 if (input_line) fprintf( stderr, "%d:", input_line );
264 fprintf( stderr, " error: " );
266 else fprintf( stderr, "makedep: error: " );
267 vfprintf( stderr, msg, valist );
268 perror( " " );
269 va_end( valist );
270 exit(1);
274 /*******************************************************************
275 * cleanup_files
277 static void cleanup_files(void)
279 if (temp_file_name) unlink( temp_file_name );
280 if (output_file_name) unlink( output_file_name );
284 /*******************************************************************
285 * exit_on_signal
287 static void exit_on_signal( int sig )
289 exit( 1 ); /* this will call the atexit functions */
293 /*******************************************************************
294 * xmalloc
296 static void *xmalloc( size_t size )
298 void *res;
299 if (!(res = malloc (size ? size : 1)))
300 fatal_error( "Virtual memory exhausted.\n" );
301 return res;
305 /*******************************************************************
306 * xrealloc
308 static void *xrealloc (void *ptr, size_t size)
310 void *res;
311 assert( size );
312 if (!(res = realloc( ptr, size )))
313 fatal_error( "Virtual memory exhausted.\n" );
314 return res;
317 /*******************************************************************
318 * xstrdup
320 static char *xstrdup( const char *str )
322 char *res = strdup( str );
323 if (!res) fatal_error( "Virtual memory exhausted.\n" );
324 return res;
328 /*******************************************************************
329 * strmake
331 static char *strmake( const char* fmt, ... )
333 int n;
334 size_t size = 100;
335 va_list ap;
337 for (;;)
339 char *p = xmalloc (size);
340 va_start(ap, fmt);
341 n = vsnprintf (p, size, fmt, ap);
342 va_end(ap);
343 if (n == -1) size *= 2;
344 else if ((size_t)n >= size) size = n + 1;
345 else return xrealloc( p, n + 1 );
346 free(p);
351 /*******************************************************************
352 * strendswith
354 static int strendswith( const char* str, const char* end )
356 size_t l = strlen( str );
357 size_t m = strlen( end );
359 return l >= m && strcmp(str + l - m, end) == 0;
363 /*******************************************************************
364 * output
366 static void output( const char *format, ... )
368 int ret;
369 va_list valist;
371 va_start( valist, format );
372 ret = vfprintf( output_file, format, valist );
373 va_end( valist );
374 if (ret < 0) fatal_perror( "output" );
375 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
376 else output_column += ret;
380 /*******************************************************************
381 * strarray_add
383 static void strarray_add( struct strarray *array, const char *str )
385 if (array->count == array->size)
387 if (array->size) array->size *= 2;
388 else array->size = 16;
389 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
391 array->str[array->count++] = str;
395 /*******************************************************************
396 * strarray_addall
398 static void strarray_addall( struct strarray *array, struct strarray added )
400 unsigned int i;
402 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
406 /*******************************************************************
407 * strarray_exists
409 static int strarray_exists( const struct strarray *array, const char *str )
411 unsigned int i;
413 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
414 return 0;
418 /*******************************************************************
419 * strarray_add_uniq
421 static void strarray_add_uniq( struct strarray *array, const char *str )
423 if (!strarray_exists( array, str )) strarray_add( array, str );
427 /*******************************************************************
428 * strarray_get_value
430 * Find a value in a name/value pair string array.
432 static const char *strarray_get_value( const struct strarray *array, const char *name )
434 int pos, res, min = 0, max = array->count / 2 - 1;
436 while (min <= max)
438 pos = (min + max) / 2;
439 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
440 if (res < 0) min = pos + 1;
441 else max = pos - 1;
443 return NULL;
447 /*******************************************************************
448 * strarray_set_value
450 * Define a value in a name/value pair string array.
452 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
454 int i, pos, res, min = 0, max = array->count / 2 - 1;
456 while (min <= max)
458 pos = (min + max) / 2;
459 if (!(res = strcmp( array->str[pos * 2], name )))
461 /* redefining a variable replaces the previous value */
462 array->str[pos * 2 + 1] = value;
463 return;
465 if (res < 0) min = pos + 1;
466 else max = pos - 1;
468 strarray_add( array, NULL );
469 strarray_add( array, NULL );
470 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
471 array->str[min * 2] = name;
472 array->str[min * 2 + 1] = value;
476 /*******************************************************************
477 * strarray_set_qsort
479 static void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
481 if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
485 /*******************************************************************
486 * output_filename
488 static void output_filename( const char *name )
490 if (output_column + strlen(name) + 1 > 100)
492 output( " \\\n" );
493 output( " " );
495 else if (output_column) output( " " );
496 output( "%s", name );
500 /*******************************************************************
501 * output_filenames
503 static void output_filenames( struct strarray array )
505 unsigned int i;
507 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
511 /*******************************************************************
512 * output_rm_filenames
514 static void output_rm_filenames( struct strarray array )
516 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
517 unsigned int i, len;
519 if (!array.count) return;
520 output( "\trm -f" );
521 for (i = len = 0; i < array.count; i++)
523 if (len > max_cmdline)
525 output( "\n" );
526 output( "\trm -f" );
527 len = 0;
529 output_filename( array.str[i] );
530 len += strlen( array.str[i] ) + 1;
532 output( "\n" );
536 /*******************************************************************
537 * get_extension
539 static char *get_extension( char *filename )
541 char *ext = strrchr( filename, '.' );
542 if (ext && strchr( ext, '/' )) ext = NULL;
543 return ext;
547 /*******************************************************************
548 * replace_extension
550 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
552 char *ret;
553 size_t name_len = strlen( name );
554 size_t ext_len = strlen( old_ext );
556 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
557 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
558 memcpy( ret, name, name_len );
559 strcpy( ret + name_len, new_ext );
560 return ret;
564 /*******************************************************************
565 * replace_filename
567 static char *replace_filename( const char *path, const char *name )
569 const char *p;
570 char *ret;
571 size_t len;
573 if (!path) return xstrdup( name );
574 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
575 len = p - path + 1;
576 ret = xmalloc( len + strlen( name ) + 1 );
577 memcpy( ret, path, len );
578 strcpy( ret + len, name );
579 return ret;
583 /*******************************************************************
584 * strarray_replace_extension
586 static struct strarray strarray_replace_extension( const struct strarray *array,
587 const char *old_ext, const char *new_ext )
589 unsigned int i;
590 struct strarray ret;
592 ret.count = ret.size = array->count;
593 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
594 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
595 return ret;
599 /*******************************************************************
600 * replace_substr
602 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
604 size_t pos = start - str;
605 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
606 memcpy( ret, str, pos );
607 strcpy( ret + pos, replace );
608 strcat( ret + pos, start + len );
609 return ret;
613 /*******************************************************************
614 * get_relative_path
616 * Determine where the destination path is located relative to the 'from' path.
618 static char *get_relative_path( const char *from, const char *dest )
620 const char *start;
621 char *ret, *p;
622 unsigned int dotdots = 0;
624 /* a path of "." is equivalent to an empty path */
625 if (!strcmp( from, "." )) from = "";
627 for (;;)
629 while (*from == '/') from++;
630 while (*dest == '/') dest++;
631 start = dest; /* save start of next path element */
632 if (!*from) break;
634 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
635 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
637 /* count remaining elements in 'from' */
640 dotdots++;
641 while (*from && *from != '/') from++;
642 while (*from == '/') from++;
644 while (*from);
645 break;
648 if (!start[0] && !dotdots) return NULL; /* empty path */
650 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
651 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
653 if (start[0]) strcpy( p, start );
654 else p[-1] = 0; /* remove trailing slash */
655 return ret;
659 /*******************************************************************
660 * concat_paths
662 static char *concat_paths( const char *base, const char *path )
664 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
665 if (!path || !path[0]) return xstrdup( base );
666 if (path[0] == '/') return xstrdup( path );
667 return strmake( "%s/%s", base, path );
671 /*******************************************************************
672 * base_dir_path
674 static char *base_dir_path( const struct makefile *make, const char *path )
676 return concat_paths( make->base_dir, path );
680 /*******************************************************************
681 * obj_dir_path
683 static char *obj_dir_path( const struct makefile *make, const char *path )
685 return concat_paths( make->obj_dir, path );
689 /*******************************************************************
690 * src_dir_path
692 static char *src_dir_path( const struct makefile *make, const char *path )
694 if (make->src_dir) return concat_paths( make->src_dir, path );
695 return obj_dir_path( make, path );
699 /*******************************************************************
700 * top_obj_dir_path
702 static char *top_obj_dir_path( const struct makefile *make, const char *path )
704 return concat_paths( make->top_obj_dir, path );
708 /*******************************************************************
709 * top_src_dir_path
711 static char *top_src_dir_path( const struct makefile *make, const char *path )
713 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
714 return top_obj_dir_path( make, path );
718 /*******************************************************************
719 * root_dir_path
721 static char *root_dir_path( const char *path )
723 return concat_paths( root_src_dir, path );
727 /*******************************************************************
728 * tools_dir_path
730 static char *tools_dir_path( const struct makefile *make, const char *path )
732 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
733 return top_obj_dir_path( make, strmake( "tools/%s", path ));
737 /*******************************************************************
738 * tools_path
740 static char *tools_path( const struct makefile *make, const char *name )
742 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
746 /*******************************************************************
747 * get_line
749 static char *get_line( FILE *file )
751 static char *buffer;
752 static size_t size;
754 if (!size)
756 size = 1024;
757 buffer = xmalloc( size );
759 if (!fgets( buffer, size, file )) return NULL;
760 input_line++;
762 for (;;)
764 char *p = buffer + strlen(buffer);
765 /* if line is larger than buffer, resize buffer */
766 while (p == buffer + size - 1 && p[-1] != '\n')
768 buffer = xrealloc( buffer, size * 2 );
769 if (!fgets( buffer + size - 1, size + 1, file )) break;
770 p = buffer + strlen(buffer);
771 size *= 2;
773 if (p > buffer && p[-1] == '\n')
775 *(--p) = 0;
776 if (p > buffer && p[-1] == '\r') *(--p) = 0;
777 if (p > buffer && p[-1] == '\\')
779 *(--p) = 0;
780 /* line ends in backslash, read continuation line */
781 if (!fgets( p, size - (p - buffer), file )) return buffer;
782 input_line++;
783 continue;
786 return buffer;
791 /*******************************************************************
792 * hash_filename
794 static unsigned int hash_filename( const char *name )
796 /* FNV-1 hash */
797 unsigned int ret = 2166136261u;
798 while (*name) ret = (ret * 16777619) ^ *name++;
799 return ret % HASH_SIZE;
803 /*******************************************************************
804 * add_file
806 static struct file *add_file( const char *name )
808 struct file *file = xmalloc( sizeof(*file) );
809 memset( file, 0, sizeof(*file) );
810 file->name = xstrdup( name );
811 return file;
815 /*******************************************************************
816 * add_dependency
818 static void add_dependency( struct file *file, const char *name, enum incl_type type )
820 /* enforce some rules for the Wine tree */
822 if (!memcmp( name, "../", 3 ))
823 fatal_error( "#include directive with relative path not allowed\n" );
825 if (!strcmp( name, "config.h" ))
827 if (strendswith( file->name, ".h" ))
828 fatal_error( "config.h must not be included by a header file\n" );
829 if (file->deps_count)
830 fatal_error( "config.h must be included before anything else\n" );
832 else if (!strcmp( name, "wine/port.h" ))
834 if (strendswith( file->name, ".h" ))
835 fatal_error( "wine/port.h must not be included by a header file\n" );
836 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
837 if (file->deps_count > 1)
838 fatal_error( "wine/port.h must be included before everything except config.h\n" );
839 if (strcmp( file->deps[0].name, "config.h" ))
840 fatal_error( "config.h must be included before wine/port.h\n" );
843 if (file->deps_count >= file->deps_size)
845 file->deps_size *= 2;
846 if (file->deps_size < 16) file->deps_size = 16;
847 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
849 file->deps[file->deps_count].line = input_line;
850 file->deps[file->deps_count].type = type;
851 file->deps[file->deps_count].name = xstrdup( name );
852 file->deps_count++;
856 /*******************************************************************
857 * find_src_file
859 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
861 struct incl_file *file;
863 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
864 if (!strcmp( name, file->name )) return file;
865 return NULL;
868 /*******************************************************************
869 * find_include_file
871 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
873 struct incl_file *file;
875 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
876 if (!strcmp( name, file->name )) return file;
877 return NULL;
880 /*******************************************************************
881 * add_include
883 * Add an include file if it doesn't already exists.
885 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
886 const char *name, int line, enum incl_type type )
888 struct incl_file *include;
890 if (parent->files_count >= parent->files_size)
892 parent->files_size *= 2;
893 if (parent->files_size < 16) parent->files_size = 16;
894 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
897 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
898 if (!strcmp( name, include->name )) goto found;
900 include = xmalloc( sizeof(*include) );
901 memset( include, 0, sizeof(*include) );
902 include->name = xstrdup(name);
903 include->included_by = parent;
904 include->included_line = line;
905 include->type = type;
906 list_add_tail( &make->includes, &include->entry );
907 found:
908 parent->files[parent->files_count++] = include;
909 return include;
913 /*******************************************************************
914 * add_generated_source
916 * Add a generated source file to the list.
918 static struct incl_file *add_generated_source( struct makefile *make,
919 const char *name, const char *filename )
921 struct incl_file *file;
923 if ((file = find_src_file( make, name ))) return file; /* we already have it */
924 file = xmalloc( sizeof(*file) );
925 memset( file, 0, sizeof(*file) );
926 file->file = add_file( name );
927 file->name = xstrdup( name );
928 file->filename = obj_dir_path( make, filename ? filename : name );
929 file->file->flags = FLAG_GENERATED;
930 list_add_tail( &make->sources, &file->entry );
931 return file;
935 /*******************************************************************
936 * parse_include_directive
938 static void parse_include_directive( struct file *source, char *str )
940 char quote, *include, *p = str;
942 while (*p && isspace(*p)) p++;
943 if (*p != '\"' && *p != '<' ) return;
944 quote = *p++;
945 if (quote == '<') quote = '>';
946 include = p;
947 while (*p && (*p != quote)) p++;
948 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
949 *p = 0;
950 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
954 /*******************************************************************
955 * parse_pragma_directive
957 static void parse_pragma_directive( struct file *source, char *str )
959 char *flag, *p = str;
961 if (!isspace( *p )) return;
962 while (*p && isspace(*p)) p++;
963 p = strtok( p, " \t" );
964 if (strcmp( p, "makedep" )) return;
966 while ((flag = strtok( NULL, " \t" )))
968 if (!strcmp( flag, "depend" ))
970 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
971 return;
973 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
975 if (strendswith( source->name, ".idl" ))
977 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
978 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
979 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
980 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
981 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
982 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
983 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
984 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
986 else if (strendswith( source->name, ".rc" ))
988 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
990 else if (strendswith( source->name, ".sfd" ))
992 if (!strcmp( flag, "font" ))
994 struct strarray *array = source->args;
996 if (!array)
998 source->args = array = xmalloc( sizeof(*array) );
999 *array = empty_strarray;
1000 source->flags |= FLAG_SFD_FONTS;
1002 strarray_add( array, xstrdup( strtok( NULL, "" )));
1003 return;
1006 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
1011 /*******************************************************************
1012 * parse_cpp_directive
1014 static void parse_cpp_directive( struct file *source, char *str )
1016 while (*str && isspace(*str)) str++;
1017 if (*str++ != '#') return;
1018 while (*str && isspace(*str)) str++;
1020 if (!strncmp( str, "include", 7 ))
1021 parse_include_directive( source, str + 7 );
1022 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
1023 parse_include_directive( source, str + 6 );
1024 else if (!strncmp( str, "pragma", 6 ))
1025 parse_pragma_directive( source, str + 6 );
1029 /*******************************************************************
1030 * parse_idl_file
1032 static void parse_idl_file( struct file *source, FILE *file )
1034 char *buffer, *include;
1036 input_line = 0;
1038 while ((buffer = get_line( file )))
1040 char quote;
1041 char *p = buffer;
1042 while (*p && isspace(*p)) p++;
1044 if (!strncmp( p, "importlib", 9 ))
1046 p += 9;
1047 while (*p && isspace(*p)) p++;
1048 if (*p++ != '(') continue;
1049 while (*p && isspace(*p)) p++;
1050 if (*p++ != '"') continue;
1051 include = p;
1052 while (*p && (*p != '"')) p++;
1053 if (!*p) fatal_error( "malformed importlib directive\n" );
1054 *p = 0;
1055 add_dependency( source, include, INCL_IMPORTLIB );
1056 continue;
1059 if (!strncmp( p, "import", 6 ))
1061 p += 6;
1062 while (*p && isspace(*p)) p++;
1063 if (*p != '"') continue;
1064 include = ++p;
1065 while (*p && (*p != '"')) p++;
1066 if (!*p) fatal_error( "malformed import directive\n" );
1067 *p = 0;
1068 add_dependency( source, include, INCL_IMPORT );
1069 continue;
1072 /* check for #include inside cpp_quote */
1073 if (!strncmp( p, "cpp_quote", 9 ))
1075 p += 9;
1076 while (*p && isspace(*p)) p++;
1077 if (*p++ != '(') continue;
1078 while (*p && isspace(*p)) p++;
1079 if (*p++ != '"') continue;
1080 if (*p++ != '#') continue;
1081 while (*p && isspace(*p)) p++;
1082 if (strncmp( p, "include", 7 )) continue;
1083 p += 7;
1084 while (*p && isspace(*p)) p++;
1085 if (*p == '\\' && p[1] == '"')
1087 p += 2;
1088 quote = '"';
1090 else
1092 if (*p++ != '<' ) continue;
1093 quote = '>';
1095 include = p;
1096 while (*p && (*p != quote)) p++;
1097 if (!*p || (quote == '"' && p[-1] != '\\'))
1098 fatal_error( "malformed #include directive inside cpp_quote\n" );
1099 if (quote == '"') p--; /* remove backslash */
1100 *p = 0;
1101 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1102 continue;
1105 parse_cpp_directive( source, p );
1109 /*******************************************************************
1110 * parse_c_file
1112 static void parse_c_file( struct file *source, FILE *file )
1114 char *buffer;
1116 input_line = 0;
1117 while ((buffer = get_line( file )))
1119 parse_cpp_directive( source, buffer );
1124 /*******************************************************************
1125 * parse_rc_file
1127 static void parse_rc_file( struct file *source, FILE *file )
1129 char *buffer, *include;
1131 input_line = 0;
1132 while ((buffer = get_line( file )))
1134 char quote;
1135 char *p = buffer;
1136 while (*p && isspace(*p)) p++;
1138 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1140 p += 2;
1141 while (*p && isspace(*p)) p++;
1142 if (strncmp( p, "@makedep:", 9 )) continue;
1143 p += 9;
1144 while (*p && isspace(*p)) p++;
1145 quote = '"';
1146 if (*p == quote)
1148 include = ++p;
1149 while (*p && *p != quote) p++;
1151 else
1153 include = p;
1154 while (*p && !isspace(*p) && *p != '*') p++;
1156 if (!*p)
1157 fatal_error( "malformed makedep comment\n" );
1158 *p = 0;
1159 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1160 continue;
1163 parse_cpp_directive( source, buffer );
1168 /*******************************************************************
1169 * parse_in_file
1171 static void parse_in_file( struct file *source, FILE *file )
1173 char *p, *buffer;
1175 /* make sure it gets rebuilt when the version changes */
1176 add_dependency( source, "config.h", INCL_SYSTEM );
1178 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1180 input_line = 0;
1181 while ((buffer = get_line( file )))
1183 if (strncmp( buffer, ".TH", 3 )) continue;
1184 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1185 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1186 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1187 source->args = xstrdup( p );
1188 return;
1193 /*******************************************************************
1194 * parse_sfd_file
1196 static void parse_sfd_file( struct file *source, FILE *file )
1198 char *p, *eol, *buffer;
1200 input_line = 0;
1201 while ((buffer = get_line( file )))
1203 if (strncmp( buffer, "UComments:", 10 )) continue;
1204 p = buffer + 10;
1205 while (*p == ' ') p++;
1206 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1208 p++;
1209 buffer[strlen(buffer) - 1] = 0;
1211 while ((eol = strstr( p, "+AAoA" )))
1213 *eol = 0;
1214 while (*p && isspace(*p)) p++;
1215 if (*p++ == '#')
1217 while (*p && isspace(*p)) p++;
1218 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1220 p = eol + 5;
1222 while (*p && isspace(*p)) p++;
1223 if (*p++ != '#') return;
1224 while (*p && isspace(*p)) p++;
1225 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1226 return;
1231 static const struct
1233 const char *ext;
1234 void (*parse)( struct file *file, FILE *f );
1235 } parse_functions[] =
1237 { ".c", parse_c_file },
1238 { ".h", parse_c_file },
1239 { ".inl", parse_c_file },
1240 { ".l", parse_c_file },
1241 { ".m", parse_c_file },
1242 { ".rh", parse_c_file },
1243 { ".x", parse_c_file },
1244 { ".y", parse_c_file },
1245 { ".idl", parse_idl_file },
1246 { ".rc", parse_rc_file },
1247 { ".in", parse_in_file },
1248 { ".sfd", parse_sfd_file }
1251 /*******************************************************************
1252 * load_file
1254 static struct file *load_file( const char *name )
1256 struct file *file;
1257 FILE *f;
1258 unsigned int i, hash = hash_filename( name );
1260 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1261 if (!strcmp( name, file->name )) return file;
1263 if (!(f = fopen( name, "r" ))) return NULL;
1265 file = add_file( name );
1266 list_add_tail( &files[hash], &file->entry );
1267 input_file_name = file->name;
1268 input_line = 0;
1270 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1272 if (!strendswith( name, parse_functions[i].ext )) continue;
1273 parse_functions[i].parse( file, f );
1274 break;
1277 fclose( f );
1278 input_file_name = NULL;
1280 return file;
1284 /*******************************************************************
1285 * open_include_path_file
1287 * Open a file from a directory on the include path.
1289 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1290 const char *name, char **filename )
1292 char *src_path = base_dir_path( make, concat_paths( dir, name ));
1293 struct file *ret = load_file( src_path );
1295 if (ret) *filename = src_dir_path( make, concat_paths( dir, name ));
1296 return ret;
1300 /*******************************************************************
1301 * open_file_same_dir
1303 * Open a file in the same directory as the parent.
1305 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1307 char *src_path = replace_filename( parent->file->name, name );
1308 struct file *ret = load_file( src_path );
1310 if (ret) *filename = replace_filename( parent->filename, name );
1311 free( src_path );
1312 return ret;
1316 /*******************************************************************
1317 * open_local_file
1319 * Open a file in the source directory of the makefile.
1321 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1323 char *src_path = root_dir_path( base_dir_path( make, path ));
1324 struct file *ret = load_file( src_path );
1326 /* if not found, try parent dir */
1327 if (!ret && make->parent_dir)
1329 free( src_path );
1330 path = strmake( "%s/%s", make->parent_dir, path );
1331 src_path = root_dir_path( base_dir_path( make, path ));
1332 ret = load_file( src_path );
1335 if (ret) *filename = src_dir_path( make, path );
1336 free( src_path );
1337 return ret;
1341 /*******************************************************************
1342 * open_global_file
1344 * Open a file in the top-level source directory.
1346 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1348 char *src_path = root_dir_path( path );
1349 struct file *ret = load_file( src_path );
1351 if (ret) *filename = top_src_dir_path( make, path );
1352 free( src_path );
1353 return ret;
1357 /*******************************************************************
1358 * open_global_header
1360 * Open a file in the global include source directory.
1362 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1364 return open_global_file( make, strmake( "include/%s", path ), filename );
1368 /*******************************************************************
1369 * open_src_file
1371 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1373 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1375 if (!file) fatal_perror( "open %s", pFile->name );
1376 return file;
1380 /*******************************************************************
1381 * open_include_file
1383 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1385 struct file *file = NULL;
1386 char *filename;
1387 unsigned int i, len;
1389 errno = ENOENT;
1391 /* check for generated bison header */
1393 if (strendswith( pFile->name, ".tab.h" ) &&
1394 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1396 pFile->sourcename = filename;
1397 pFile->filename = obj_dir_path( make, pFile->name );
1398 return file;
1401 /* check for corresponding idl file in source dir */
1403 if (strendswith( pFile->name, ".h" ) &&
1404 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1406 pFile->sourcename = filename;
1407 pFile->filename = obj_dir_path( make, pFile->name );
1408 return file;
1411 /* check for corresponding tlb file in source dir */
1413 if (strendswith( pFile->name, ".tlb" ) &&
1414 (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1416 pFile->sourcename = filename;
1417 pFile->filename = obj_dir_path( make, pFile->name );
1418 return file;
1421 /* now try in source dir */
1422 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1424 /* check for corresponding idl file in global includes */
1426 if (strendswith( pFile->name, ".h" ) &&
1427 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1429 pFile->sourcename = filename;
1430 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1431 return file;
1434 /* check for corresponding .in file in global includes (for config.h.in) */
1436 if (strendswith( pFile->name, ".h" ) &&
1437 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1439 pFile->sourcename = filename;
1440 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1441 return file;
1444 /* check for corresponding .x file in global includes */
1446 if (strendswith( pFile->name, "tmpl.h" ) &&
1447 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1449 pFile->sourcename = filename;
1450 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1451 return file;
1454 /* check for corresponding .tlb file in global includes */
1456 if (strendswith( pFile->name, ".tlb" ) &&
1457 (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
1459 pFile->sourcename = filename;
1460 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1461 return file;
1464 /* check in global includes source dir */
1466 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1468 /* check in global msvcrt includes */
1469 if (make->use_msvcrt &&
1470 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1471 return file;
1473 /* now search in include paths */
1474 for (i = 0; i < make->include_paths.count; i++)
1476 const char *dir = make->include_paths.str[i];
1477 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1479 if (prefix)
1481 len = strlen( prefix );
1482 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1484 while (dir[len] == '/') len++;
1485 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1486 if (file) return file;
1488 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1490 if (*dir != '/')
1492 if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
1493 return file;
1496 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1498 /* try in src file directory */
1499 if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
1501 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1502 perror( pFile->name );
1503 pFile = pFile->included_by;
1504 while (pFile && pFile->included_by)
1506 const char *parent = pFile->included_by->sourcename;
1507 if (!parent) parent = pFile->included_by->file->name;
1508 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1509 parent, pFile->included_line, pFile->name );
1510 pFile = pFile->included_by;
1512 exit(1);
1516 /*******************************************************************
1517 * add_all_includes
1519 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1521 unsigned int i;
1523 parent->files_count = 0;
1524 parent->files_size = file->deps_count;
1525 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1526 for (i = 0; i < file->deps_count; i++)
1528 switch (file->deps[i].type)
1530 case INCL_NORMAL:
1531 case INCL_IMPORT:
1532 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1533 break;
1534 case INCL_IMPORTLIB:
1535 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1536 break;
1537 case INCL_SYSTEM:
1538 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1539 break;
1540 case INCL_CPP_QUOTE:
1541 case INCL_CPP_QUOTE_SYSTEM:
1542 break;
1548 /*******************************************************************
1549 * parse_file
1551 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1553 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1555 if (!file) return;
1557 source->file = file;
1558 source->files_count = 0;
1559 source->files_size = file->deps_count;
1560 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1562 if (source->sourcename)
1564 if (strendswith( source->sourcename, ".idl" ))
1566 unsigned int i;
1568 if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
1570 /* generated .h file always includes these */
1571 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1572 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1573 for (i = 0; i < file->deps_count; i++)
1575 switch (file->deps[i].type)
1577 case INCL_IMPORT:
1578 if (strendswith( file->deps[i].name, ".idl" ))
1579 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1580 file->deps[i].line, INCL_NORMAL );
1581 else
1582 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1583 break;
1584 case INCL_CPP_QUOTE:
1585 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1586 break;
1587 case INCL_CPP_QUOTE_SYSTEM:
1588 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1589 break;
1590 case INCL_NORMAL:
1591 case INCL_SYSTEM:
1592 case INCL_IMPORTLIB:
1593 break;
1596 return;
1598 if (strendswith( source->sourcename, ".y" ))
1599 return; /* generated .tab.h doesn't include anything */
1602 add_all_includes( make, source, file );
1606 /*******************************************************************
1607 * add_src_file
1609 * Add a source file to the list.
1611 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1613 struct incl_file *file;
1615 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1616 file = xmalloc( sizeof(*file) );
1617 memset( file, 0, sizeof(*file) );
1618 file->name = xstrdup(name);
1619 list_add_tail( &make->sources, &file->entry );
1620 parse_file( make, file, 1 );
1621 return file;
1625 /*******************************************************************
1626 * open_input_makefile
1628 static FILE *open_input_makefile( const struct makefile *make )
1630 FILE *ret;
1632 if (make->base_dir)
1633 input_file_name = root_dir_path( base_dir_path( make, strmake( "%s.in", output_makefile_name )));
1634 else
1635 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1637 input_line = 0;
1638 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1639 return ret;
1643 /*******************************************************************
1644 * get_make_variable
1646 static const char *get_make_variable( const struct makefile *make, const char *name )
1648 const char *ret;
1650 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1651 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1652 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1653 return NULL;
1657 /*******************************************************************
1658 * get_expanded_make_variable
1660 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1662 const char *var;
1663 char *p, *end, *expand, *tmp;
1665 var = get_make_variable( make, name );
1666 if (!var) return NULL;
1668 p = expand = xstrdup( var );
1669 while ((p = strchr( p, '$' )))
1671 if (p[1] == '(')
1673 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1674 *end++ = 0;
1675 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1676 var = get_make_variable( make, p + 2 );
1677 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1678 /* switch to the new string */
1679 p = tmp + (p - expand);
1680 free( expand );
1681 expand = tmp;
1683 else if (p[1] == '{') /* don't expand ${} variables */
1685 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1686 p = end + 1;
1688 else if (p[1] == '$')
1690 p += 2;
1692 else fatal_error( "syntax error in '%s'\n", expand );
1695 /* consider empty variables undefined */
1696 p = expand;
1697 while (*p && isspace(*p)) p++;
1698 if (*p) return expand;
1699 free( expand );
1700 return NULL;
1704 /*******************************************************************
1705 * get_expanded_make_var_array
1707 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1709 struct strarray ret = empty_strarray;
1710 char *value, *token;
1712 if ((value = get_expanded_make_variable( make, name )))
1713 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1714 strarray_add( &ret, token );
1715 return ret;
1719 /*******************************************************************
1720 * get_expanded_file_local_var
1722 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1723 const char *name )
1725 char *p, *var = strmake( "%s_%s", file, name );
1727 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1728 return get_expanded_make_var_array( make, var );
1732 /*******************************************************************
1733 * set_make_variable
1735 static int set_make_variable( struct strarray *array, const char *assignment )
1737 char *p, *name;
1739 p = name = xstrdup( assignment );
1740 while (isalnum(*p) || *p == '_') p++;
1741 if (name == p) return 0; /* not a variable */
1742 if (isspace(*p))
1744 *p++ = 0;
1745 while (isspace(*p)) p++;
1747 if (*p != '=') return 0; /* not an assignment */
1748 *p++ = 0;
1749 while (isspace(*p)) p++;
1751 strarray_set_value( array, name, p );
1752 return 1;
1756 /*******************************************************************
1757 * parse_makefile
1759 static struct makefile *parse_makefile( const char *path )
1761 char *buffer;
1762 FILE *file;
1763 struct makefile *make = xmalloc( sizeof(*make) );
1765 memset( make, 0, sizeof(*make) );
1766 if (path)
1768 make->top_obj_dir = get_relative_path( path, "" );
1769 make->base_dir = path;
1770 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1773 file = open_input_makefile( make );
1774 while ((buffer = get_line( file )))
1776 if (!strncmp( buffer, separator, strlen(separator) )) break;
1777 if (*buffer == '\t') continue; /* command */
1778 while (isspace( *buffer )) buffer++;
1779 if (*buffer == '#') continue; /* comment */
1780 set_make_variable( &make->vars, buffer );
1782 fclose( file );
1783 input_file_name = NULL;
1784 return make;
1788 /*******************************************************************
1789 * add_generated_sources
1791 static void add_generated_sources( struct makefile *make )
1793 struct incl_file *source, *next, *file;
1795 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1797 if (source->file->flags & FLAG_IDL_CLIENT)
1799 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1800 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1801 add_all_includes( make, file, file->file );
1803 if (source->file->flags & FLAG_IDL_SERVER)
1805 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1806 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1807 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1808 add_all_includes( make, file, file->file );
1810 if (source->file->flags & FLAG_IDL_IDENT)
1812 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1813 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1814 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1815 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1816 add_all_includes( make, file, file->file );
1818 if (source->file->flags & FLAG_IDL_PROXY)
1820 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1821 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1822 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1823 add_all_includes( make, file, file->file );
1824 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1825 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1826 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1827 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1828 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1829 add_all_includes( make, file, file->file );
1831 if (source->file->flags & FLAG_IDL_TYPELIB)
1833 add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
1835 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1837 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1839 if (source->file->flags & FLAG_IDL_REGISTER)
1841 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1843 if (source->file->flags & FLAG_IDL_HEADER)
1845 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1847 if (!source->file->flags && strendswith( source->name, ".idl" ))
1849 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1851 if (strendswith( source->name, ".x" ))
1853 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1855 if (strendswith( source->name, ".y" ))
1857 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1858 /* steal the includes list from the source file */
1859 file->files_count = source->files_count;
1860 file->files_size = source->files_size;
1861 file->files = source->files;
1862 source->files_count = source->files_size = 0;
1863 source->files = NULL;
1865 if (strendswith( source->name, ".l" ))
1867 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1868 /* steal the includes list from the source file */
1869 file->files_count = source->files_count;
1870 file->files_size = source->files_size;
1871 file->files = source->files;
1872 source->files_count = source->files_size = 0;
1873 source->files = NULL;
1875 if (source->file->flags & FLAG_C_IMPLIB)
1877 if (!make->staticimplib && make->importlib && *dll_ext)
1878 make->staticimplib = strmake( "lib%s.a", make->importlib );
1880 if (strendswith( source->name, ".po" ))
1882 if (!make->disabled)
1883 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1886 if (make->testdll)
1888 file = add_generated_source( make, "testlist.o", "testlist.c" );
1889 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1890 add_all_includes( make, file, file->file );
1895 /*******************************************************************
1896 * create_dir
1898 static void create_dir( const char *dir )
1900 char *p, *path;
1902 p = path = xstrdup( dir );
1903 while ((p = strchr( p, '/' )))
1905 *p = 0;
1906 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1907 *p++ = '/';
1908 while (*p == '/') p++;
1910 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1911 free( path );
1915 /*******************************************************************
1916 * create_file_directories
1918 * Create the base directories of all the files.
1920 static void create_file_directories( const struct makefile *make, struct strarray files )
1922 struct strarray subdirs = empty_strarray;
1923 unsigned int i;
1924 char *dir;
1926 for (i = 0; i < files.count; i++)
1928 if (!strchr( files.str[i], '/' )) continue;
1929 dir = base_dir_path( make, files.str[i] );
1930 *strrchr( dir, '/' ) = 0;
1931 strarray_add_uniq( &subdirs, dir );
1934 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1938 /*******************************************************************
1939 * output_filenames_obj_dir
1941 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1943 unsigned int i;
1945 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1949 /*******************************************************************
1950 * get_dependencies
1952 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1954 unsigned int i;
1956 if (!file->filename) return;
1958 if (file != source)
1960 if (file->owner == source) return; /* already processed */
1961 if (file->type == INCL_IMPORTLIB &&
1962 !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1963 return; /* library is imported only when building a typelib */
1964 file->owner = source;
1965 strarray_add( &source->dependencies, file->filename );
1967 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
1971 /*******************************************************************
1972 * get_local_dependencies
1974 * Get the local dependencies of a given target.
1976 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1977 struct strarray targets )
1979 unsigned int i;
1980 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
1982 for (i = 0; i < deps.count; i++)
1984 if (strarray_exists( &targets, deps.str[i] ))
1985 deps.str[i] = obj_dir_path( make, deps.str[i] );
1986 else
1987 deps.str[i] = src_dir_path( make, deps.str[i] );
1989 return deps;
1993 /*******************************************************************
1994 * get_static_lib
1996 * Check if makefile builds the named static library and return the full lib path.
1998 static const char *get_static_lib( const struct makefile *make, const char *name )
2000 if (!make->staticlib) return NULL;
2001 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2002 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2003 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2004 return base_dir_path( make, make->staticlib );
2008 /*******************************************************************
2009 * add_default_libraries
2011 static struct strarray add_default_libraries( const struct makefile *make, struct strarray *deps )
2013 struct strarray ret = empty_strarray;
2014 struct strarray all_libs = empty_strarray;
2015 unsigned int i, j;
2017 strarray_add( &all_libs, "-lwine_port" );
2018 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2019 strarray_addall( &all_libs, libs );
2021 for (i = 0; i < all_libs.count; i++)
2023 const char *lib = NULL;
2025 if (!strncmp( all_libs.str[i], "-l", 2 ))
2027 const char *name = all_libs.str[i] + 2;
2029 for (j = 0; j < top_makefile->subdirs.count; j++)
2031 const struct makefile *submake = top_makefile->submakes[j];
2033 if ((lib = get_static_lib( submake, name ))) break;
2037 if (lib)
2039 lib = top_obj_dir_path( make, lib );
2040 strarray_add( deps, lib );
2041 strarray_add( &ret, lib );
2043 else strarray_add( &ret, all_libs.str[i] );
2045 return ret;
2049 /*******************************************************************
2050 * add_import_libs
2052 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2053 struct strarray imports, int cross )
2055 struct strarray ret = empty_strarray;
2056 unsigned int i, j;
2058 for (i = 0; i < imports.count; i++)
2060 const char *name = imports.str[i];
2061 const char *lib = NULL;
2063 for (j = 0; j < top_makefile->subdirs.count; j++)
2065 const struct makefile *submake = top_makefile->submakes[j];
2067 if (submake->importlib && !strcmp( submake->importlib, name ))
2069 if (cross || !*dll_ext || submake->staticimplib)
2070 lib = base_dir_path( submake, strmake( "lib%s.a", name ));
2071 else
2072 strarray_add( deps, top_obj_dir_path( make,
2073 strmake( "%s/lib%s.def", submake->base_dir, name )));
2074 break;
2077 if ((lib = get_static_lib( submake, name ))) break;
2080 if (lib)
2082 if (cross) lib = replace_extension( lib, ".a", ".cross.a" );
2083 lib = top_obj_dir_path( make, lib );
2084 strarray_add( deps, lib );
2085 strarray_add( &ret, lib );
2087 else strarray_add( &ret, strmake( "-l%s", name ));
2089 return ret;
2093 /*******************************************************************
2094 * get_default_imports
2096 static struct strarray get_default_imports( const struct makefile *make )
2098 struct strarray ret = empty_strarray;
2100 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
2101 if (strarray_exists( &make->appmode, "-mno-cygwin" )) strarray_add( &ret, "msvcrt" );
2102 if (make->is_win16) strarray_add( &ret, "kernel" );
2103 strarray_add( &ret, "kernel32" );
2104 strarray_add( &ret, "ntdll" );
2105 strarray_add( &ret, "winecrt0" );
2106 return ret;
2110 /*******************************************************************
2111 * add_install_rule
2113 static void add_install_rule( struct makefile *make, const char *target,
2114 const char *file, const char *dest )
2116 if (strarray_exists( &make->install_lib, target ))
2118 strarray_add( &make->install_rules[INSTALL_LIB], file );
2119 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2121 else if (strarray_exists( &make->install_dev, target ))
2123 strarray_add( &make->install_rules[INSTALL_DEV], file );
2124 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2129 /*******************************************************************
2130 * get_include_install_path
2132 * Determine the installation path for a given include file.
2134 static const char *get_include_install_path( const char *name )
2136 if (!strncmp( name, "wine/", 5 )) return name + 5;
2137 if (!strncmp( name, "msvcrt/", 7 )) return name;
2138 return strmake( "windows/%s", name );
2142 /*******************************************************************
2143 * get_shared_library_name
2145 * Determine possible names for a shared library with a version number.
2147 static struct strarray get_shared_lib_names( const char *libname )
2149 struct strarray ret = empty_strarray;
2150 const char *ext, *p;
2151 char *name, *first, *second;
2152 size_t len = 0;
2154 strarray_add( &ret, libname );
2156 for (p = libname; (p = strchr( p, '.' )); p++)
2157 if ((len = strspn( p + 1, "0123456789." ))) break;
2159 if (!len) return ret;
2160 ext = p + 1 + len;
2161 if (*ext && ext[-1] == '.') ext--;
2163 /* keep only the first group of digits */
2164 name = xstrdup( libname );
2165 first = name + (p - libname);
2166 if ((second = strchr( first + 1, '.' )))
2168 strcpy( second, ext );
2169 strarray_add( &ret, xstrdup( name ));
2171 /* now remove all digits */
2172 strcpy( first, ext );
2173 strarray_add( &ret, name );
2174 return ret;
2178 /*******************************************************************
2179 * output_symlink_rule
2181 * Output a rule to create a symlink.
2183 static void output_symlink_rule( const char *src_name, const char *link_name )
2185 const char *name;
2187 output( "\trm -f %s && ", link_name );
2189 /* dest path with a directory needs special handling if ln -s isn't supported */
2190 if (strcmp( ln_s, "ln -s" ) && ((name = strrchr( link_name, '/' ))))
2192 char *dir = xstrdup( link_name );
2193 dir[name - link_name] = 0;
2194 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2195 free( dir );
2197 else
2199 output( "%s %s %s\n", ln_s, src_name, link_name );
2204 /*******************************************************************
2205 * output_install_commands
2207 static void output_install_commands( struct makefile *make, const struct makefile *submake,
2208 struct strarray files )
2210 unsigned int i;
2211 char *install_sh = top_src_dir_path( make, "tools/install-sh" );
2213 for (i = 0; i < files.count; i += 2)
2215 const char *file = files.str[i];
2216 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2218 if (submake) file = base_dir_path( submake, file );
2219 switch (*files.str[i + 1])
2221 case 'd': /* data file */
2222 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2223 install_sh, obj_dir_path( make, file ), dest );
2224 break;
2225 case 'D': /* data file in source dir */
2226 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2227 install_sh, src_dir_path( make, file ), dest );
2228 break;
2229 case 'p': /* program file */
2230 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2231 install_sh, obj_dir_path( make, file ), dest );
2232 break;
2233 case 's': /* script */
2234 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2235 install_sh, obj_dir_path( make, file ), dest );
2236 break;
2237 case 'S': /* script in source dir */
2238 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2239 install_sh, src_dir_path( make, file ), dest );
2240 break;
2241 case 't': /* script in tools dir */
2242 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2243 install_sh, tools_dir_path( make, files.str[i] ), dest );
2244 break;
2245 case 'y': /* symlink */
2246 output_symlink_rule( files.str[i], dest );
2247 break;
2248 default:
2249 assert(0);
2251 strarray_add( &make->uninstall_files, dest );
2256 /*******************************************************************
2257 * output_install_rules
2259 * Rules are stored as a (file,dest) pair of values.
2260 * The first char of dest indicates the type of install.
2262 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2264 unsigned int i;
2265 struct strarray files = make->install_rules[rules];
2266 struct strarray targets = empty_strarray;
2268 if (!files.count) return;
2270 for (i = 0; i < files.count; i += 2)
2272 const char *file = files.str[i];
2273 switch (*files.str[i + 1])
2275 case 'd': /* data file */
2276 case 'p': /* program file */
2277 case 's': /* script */
2278 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2279 break;
2280 case 't': /* script in tools dir */
2281 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2282 break;
2286 output( "install %s::", target );
2287 output_filenames( targets );
2288 output( "\n" );
2289 output_install_commands( make, NULL, files );
2291 strarray_add_uniq( &make->phony_targets, "install" );
2292 strarray_add_uniq( &make->phony_targets, target );
2296 static int cmp_string_length( const char **a, const char **b )
2298 int paths_a = 0, paths_b = 0;
2299 const char *p;
2301 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2302 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2303 if (paths_b != paths_a) return paths_b - paths_a;
2304 return strcmp( *a, *b );
2307 /*******************************************************************
2308 * output_uninstall_rules
2310 static void output_uninstall_rules( struct makefile *make )
2312 static const char *dirs_order[] =
2313 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };
2315 struct strarray subdirs = empty_strarray;
2316 unsigned int i, j;
2318 if (!make->uninstall_files.count) return;
2319 output( "uninstall::\n" );
2320 output_rm_filenames( make->uninstall_files );
2321 strarray_add_uniq( &make->phony_targets, "uninstall" );
2323 if (!make->subdirs.count) return;
2324 for (i = 0; i < make->uninstall_files.count; i++)
2326 char *dir = xstrdup( make->uninstall_files.str[i] );
2327 while (strchr( dir, '/' ))
2329 *strrchr( dir, '/' ) = 0;
2330 strarray_add_uniq( &subdirs, xstrdup(dir) );
2333 strarray_qsort( &subdirs, cmp_string_length );
2334 output( "\t-rmdir" );
2335 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2337 for (j = 0; j < subdirs.count; j++)
2339 if (!subdirs.str[j]) continue;
2340 if (strncmp( subdirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2341 continue;
2342 output_filename( subdirs.str[j] );
2343 subdirs.str[j] = NULL;
2346 for (j = 0; j < subdirs.count; j++)
2347 if (subdirs.str[j]) output_filename( subdirs.str[j] );
2348 output( "\n" );
2352 /*******************************************************************
2353 * output_importlib_symlinks
2355 static struct strarray output_importlib_symlinks( const struct makefile *parent,
2356 const struct makefile *make )
2358 struct strarray ret = empty_strarray;
2359 const char *lib, *dst;
2361 if (!make->module) return ret;
2362 if (!make->importlib) return ret;
2363 if (make->is_win16 && make->disabled) return ret;
2364 if (strncmp( make->base_dir, "dlls/", 5 )) return ret;
2365 if (!strcmp( make->module, make->importlib )) return ret;
2366 if (!strchr( make->importlib, '.' ) &&
2367 !strncmp( make->module, make->importlib, strlen( make->importlib )) &&
2368 !strcmp( make->module + strlen( make->importlib ), ".dll" ))
2369 return ret;
2371 lib = strmake( "lib%s.%s", make->importlib, *dll_ext ? "def" : "a" );
2372 dst = concat_paths( obj_dir_path( parent, "dlls" ), lib );
2373 output( "%s: %s\n", dst, base_dir_path( make, lib ));
2374 output_symlink_rule( concat_paths( make->base_dir + strlen("dlls/"), lib ), dst );
2375 strarray_add( &ret, dst );
2377 if (crosstarget && !make->is_win16)
2379 lib = strmake( "lib%s.cross.a", make->importlib );
2380 dst = concat_paths( obj_dir_path( parent, "dlls" ), lib );
2381 output( "%s: %s\n", dst, base_dir_path( make, lib ));
2382 output_symlink_rule( concat_paths( make->base_dir + strlen("dlls/"), lib ), dst );
2383 strarray_add( &ret, dst );
2385 return ret;
2389 /*******************************************************************
2390 * output_po_files
2392 static void output_po_files( const struct makefile *make )
2394 const char *po_dir = src_dir_path( make, "po" );
2395 struct strarray pot_files = empty_strarray;
2396 struct incl_file *source;
2397 unsigned int i;
2399 for (i = 0; i < make->subdirs.count; i++)
2401 struct makefile *submake = make->submakes[i];
2403 LIST_FOR_EACH_ENTRY( source, &submake->sources, struct incl_file, entry )
2405 if (strendswith( source->name, ".rc" ) && (source->file->flags & FLAG_RC_PO))
2407 char *pot_file = replace_extension( source->name, ".rc", ".pot" );
2408 char *pot_path = base_dir_path( submake, pot_file );
2409 output( "%s: tools/wrc include dummy\n", pot_path );
2410 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2411 strarray_add( &pot_files, pot_path );
2413 else if (strendswith( source->name, ".mc" ))
2415 char *pot_file = replace_extension( source->name, ".mc", ".pot" );
2416 char *pot_path = base_dir_path( submake, pot_file );
2417 output( "%s: tools/wmc include dummy\n", pot_path );
2418 output( "\t@cd %s && $(MAKE) %s\n", base_dir_path( submake, "" ), pot_file );
2419 strarray_add( &pot_files, pot_path );
2423 if (linguas.count)
2425 for (i = 0; i < linguas.count; i++)
2426 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2427 output( ": %s/wine.pot\n", po_dir );
2428 output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2429 po_dir );
2430 output( "po:" );
2431 for (i = 0; i < linguas.count; i++)
2432 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2433 output( "\n" );
2435 output( "%s/wine.pot:", po_dir );
2436 output_filenames( pot_files );
2437 output( "\n" );
2438 output( "\tmsgcat -o $@" );
2439 output_filenames( pot_files );
2440 output( "\n" );
2444 /*******************************************************************
2445 * output_source_y
2447 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2449 /* add source file dependency for parallel makes */
2450 char *header = strmake( "%s.tab.h", obj );
2452 if (find_include_file( make, header ))
2454 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2455 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
2456 obj, obj_dir_path( make, obj ), source->filename );
2457 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2458 source->filename, obj_dir_path( make, header ));
2459 strarray_add( &make->clean_files, header );
2461 else output( "%s.tab.c: %s\n", obj, source->filename );
2463 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
2467 /*******************************************************************
2468 * output_source_l
2470 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2472 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2473 output( "\t$(FLEX) -o$@ %s\n", source->filename );
2477 /*******************************************************************
2478 * output_source_h
2480 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2482 if (source->file->flags & FLAG_GENERATED)
2484 strarray_add( &make->all_targets, source->name );
2486 else
2488 strarray_add( &make->install_rules[INSTALL_DEV], source->name );
2489 strarray_add( &make->install_rules[INSTALL_DEV],
2490 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2495 /*******************************************************************
2496 * output_source_rc
2498 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2500 struct strarray extradefs = get_expanded_file_local_var( make, obj, "EXTRADEFS" );
2501 unsigned int i;
2503 strarray_add( &make->object_files, strmake( "%s.res", obj ));
2504 if (crosstarget) strarray_add( &make->crossobj_files, strmake( "%s.res", obj ));
2505 output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
2506 output( "\t%s -o $@", tools_path( make, "wrc" ) );
2507 if (make->is_win16) output_filename( "-m16" );
2508 else output_filenames( target_flags );
2509 output_filename( "--nostdinc" );
2510 output_filenames( make->include_args );
2511 output_filenames( make->define_args );
2512 output_filenames( extradefs );
2513 if (linguas.count && (source->file->flags & FLAG_RC_PO))
2515 char *po_dir = top_obj_dir_path( make, "po" );
2516 output_filename( strmake( "--po-dir=%s", po_dir ));
2517 output_filename( source->filename );
2518 output( "\n" );
2519 output( "%s.res:", obj_dir_path( make, obj ));
2520 for (i = 0; i < linguas.count; i++)
2521 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2522 output( "\n" );
2524 else
2526 output_filename( source->filename );
2527 output( "\n" );
2529 if (source->file->flags & FLAG_RC_PO)
2531 strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
2532 output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
2533 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ) );
2534 if (make->is_win16) output_filename( "-m16" );
2535 else output_filenames( target_flags );
2536 output_filename( "--nostdinc" );
2537 output_filenames( make->include_args );
2538 output_filenames( make->define_args );
2539 output_filenames( extradefs );
2540 output_filename( source->filename );
2541 output( "\n" );
2542 output( "%s.pot ", obj_dir_path( make, obj ));
2544 output( "%s.res:", obj_dir_path( make, obj ));
2545 output_filename( tools_path( make, "wrc" ));
2546 output_filenames( source->dependencies );
2547 output( "\n" );
2551 /*******************************************************************
2552 * output_source_mc
2554 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2556 unsigned int i;
2558 strarray_add( &make->object_files, strmake( "%s.res", obj ));
2559 if (crosstarget) strarray_add( &make->crossobj_files, strmake( "%s.res", obj ));
2560 strarray_add( &make->clean_files, strmake( "%s.pot", obj ));
2561 output( "%s.res: %s\n", obj_dir_path( make, obj ), source->filename );
2562 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
2563 if (linguas.count)
2565 char *po_dir = top_obj_dir_path( make, "po" );
2566 output_filename( strmake( "--po-dir=%s", po_dir ));
2567 output( "\n" );
2568 output( "%s.res:", obj_dir_path( make, obj ));
2569 for (i = 0; i < linguas.count; i++)
2570 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2572 output( "\n" );
2573 output( "%s.pot: %s\n", obj_dir_path( make, obj ), source->filename );
2574 output( "\t%s -O pot -o $@ %s", tools_path( make, "wmc" ), source->filename );
2575 output( "\n" );
2576 output( "%s.pot %s.res:", obj_dir_path( make, obj ), obj_dir_path( make, obj ));
2577 output_filename( tools_path( make, "wmc" ));
2578 output_filenames( source->dependencies );
2579 output( "\n" );
2583 /*******************************************************************
2584 * output_source_res
2586 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2588 strarray_add( &make->object_files, source->name );
2589 if (crosstarget) strarray_add( &make->crossobj_files, source->name );
2593 /*******************************************************************
2594 * output_source_idl
2596 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2598 struct strarray extradefs = get_expanded_file_local_var( make, obj, "EXTRADEFS" );
2599 struct strarray targets = empty_strarray;
2600 char *dest;
2601 unsigned int i;
2603 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2604 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2606 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2608 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2609 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2610 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2611 strarray_add( &targets, dest );
2613 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2614 if (source->file->flags & FLAG_INSTALL)
2616 strarray_add( &make->install_rules[INSTALL_DEV], xstrdup( source->name ));
2617 strarray_add( &make->install_rules[INSTALL_DEV],
2618 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2619 if (source->file->flags & FLAG_IDL_HEADER)
2621 strarray_add( &make->install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2622 strarray_add( &make->install_rules[INSTALL_DEV],
2623 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2626 if (!targets.count) return;
2628 output_filenames_obj_dir( make, targets );
2629 output( ": %s\n", tools_path( make, "widl" ));
2630 output( "\t%s -o $@", tools_path( make, "widl" ) );
2631 output_filenames( target_flags );
2632 output_filenames( make->include_args );
2633 output_filenames( make->define_args );
2634 output_filenames( extradefs );
2635 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2636 output_filename( source->filename );
2637 output( "\n" );
2638 output_filenames_obj_dir( make, targets );
2639 output( ": %s", source->filename );
2640 output_filenames( source->dependencies );
2641 output( "\n" );
2645 /*******************************************************************
2646 * output_source_tlb
2648 static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
2650 strarray_add( &make->all_targets, source->name );
2654 /*******************************************************************
2655 * output_source_x
2657 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2659 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2660 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2661 output( "\t%s%s -H -o $@ %s\n",
2662 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2663 if (source->file->flags & FLAG_INSTALL)
2665 strarray_add( &make->install_rules[INSTALL_DEV], source->name );
2666 strarray_add( &make->install_rules[INSTALL_DEV],
2667 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2668 strarray_add( &make->install_rules[INSTALL_DEV], strmake( "%s.h", obj ));
2669 strarray_add( &make->install_rules[INSTALL_DEV],
2670 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2675 /*******************************************************************
2676 * output_source_sfd
2678 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2680 unsigned int i;
2681 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
2683 if (fontforge && !make->src_dir)
2685 output( "%s: %s\n", ttf_file, source->filename );
2686 output( "\t%s -script %s %s $@\n",
2687 fontforge, top_src_dir_path( make, "fonts/genttf.ff" ), source->filename );
2688 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
2690 if (source->file->flags & FLAG_INSTALL)
2692 strarray_add( &make->install_rules[INSTALL_LIB], strmake( "%s.ttf", obj ));
2693 strarray_add( &make->install_rules[INSTALL_LIB], strmake( "D$(fontdir)/%s.ttf", obj ));
2695 if (source->file->flags & FLAG_SFD_FONTS)
2697 struct strarray *array = source->file->args;
2699 for (i = 0; i < array->count; i++)
2701 char *font = strtok( xstrdup(array->str[i]), " \t" );
2702 char *args = strtok( NULL, "" );
2704 strarray_add( &make->all_targets, xstrdup( font ));
2705 output( "%s: %s %s\n", obj_dir_path( make, font ),
2706 tools_path( make, "sfnt2fon" ), ttf_file );
2707 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
2708 strarray_add( &make->install_rules[INSTALL_LIB], xstrdup(font) );
2709 strarray_add( &make->install_rules[INSTALL_LIB], strmake( "d$(fontdir)/%s", font ));
2715 /*******************************************************************
2716 * output_source_svg
2718 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2720 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2721 unsigned int i;
2723 if (convert && rsvg && icotool && !make->src_dir)
2725 for (i = 0; images[i]; i++)
2726 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2728 if (images[i])
2730 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2731 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2732 top_src_dir_path( make, "tools/buildimage" ), source->filename );
2738 /*******************************************************************
2739 * output_source_nls
2741 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2743 add_install_rule( make, source->name, source->name,
2744 strmake( "D$(datadir)/wine/%s", source->name ));
2748 /*******************************************************************
2749 * output_source_desktop
2751 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2753 add_install_rule( make, source->name, source->name,
2754 strmake( "D$(datadir)/applications/%s", source->name ));
2758 /*******************************************************************
2759 * output_source_po
2761 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2763 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2764 output( "\t%s -o $@ %s\n", msgfmt, source->filename );
2765 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2769 /*******************************************************************
2770 * output_source_in
2772 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2774 unsigned int i;
2776 if (strendswith( obj, ".man" ) && source->file->args)
2778 struct strarray symlinks;
2779 char *dir, *dest = replace_extension( obj, ".man", "" );
2780 char *lang = strchr( dest, '.' );
2781 char *section = source->file->args;
2782 if (lang)
2784 *lang++ = 0;
2785 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2787 else dir = strmake( "$(mandir)/man%s", section );
2788 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2789 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2790 for (i = 0; i < symlinks.count; i++)
2791 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2792 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2793 free( dest );
2794 free( dir );
2796 strarray_add( &make->in_files, xstrdup(obj) );
2797 strarray_add( &make->all_targets, xstrdup(obj) );
2798 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2799 output( "\t$(SED_CMD) %s >$@ || (rm -f $@ && false)\n", source->filename );
2800 output( "%s:", obj_dir_path( make, obj ));
2801 output_filenames( source->dependencies );
2802 output( "\n" );
2803 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2807 /*******************************************************************
2808 * output_source_spec
2810 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2812 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2813 struct strarray all_libs, dep_libs = empty_strarray;
2815 if (!imports.count) imports = make->imports;
2816 all_libs = add_import_libs( make, &dep_libs, imports, 0 );
2817 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
2818 strarray_addall( &all_libs, libs );
2820 strarray_add( &make->clean_files, strmake( "%s.dll%s", obj, dll_ext ));
2821 strarray_add( &make->object_files, strmake( "%s.res", obj ));
2822 output( "%s.res: %s.dll%s\n", obj_dir_path( make, obj ), obj_dir_path( make, obj ), dll_ext );
2823 output( "\techo \"%s.dll TESTDLL \\\"%s.dll%s\\\"\" | %s -o $@\n", obj,
2824 obj_dir_path( make, obj ), dll_ext, tools_path( make, "wrc" ));
2826 output( "%s.dll%s:", obj_dir_path( make, obj ), dll_ext );
2827 output_filename( source->filename );
2828 output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
2829 output_filenames( dep_libs );
2830 output_filename( tools_path( make, "winebuild" ));
2831 output_filename( tools_path( make, "winegcc" ));
2832 output( "\n" );
2833 output( "\t%s -s -o $@", tools_path( make, "winegcc" ));
2834 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2835 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2836 output_filenames( target_flags );
2837 output_filenames( unwind_flags );
2838 output_filenames( make->extradllflags );
2839 output_filename( "-shared" );
2840 output_filename( source->filename );
2841 output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
2842 output_filenames( all_libs );
2843 output_filename( "$(LDFLAGS)" );
2844 output( "\n" );
2846 if (crosstarget)
2848 dep_libs = empty_strarray;
2849 all_libs = add_import_libs( make, &dep_libs, imports, 1 );
2850 add_import_libs( make, &dep_libs, get_default_imports( make ), 1 ); /* dependencies only */
2851 strarray_addall( &all_libs, libs );
2853 strarray_add( &make->clean_files, strmake( "%s.dll", obj ));
2854 strarray_add( &make->crossobj_files, strmake( "%s.cross.res", obj ));
2855 output( "%s.cross.res: %s.dll\n", obj_dir_path( make, obj ), obj_dir_path( make, obj ) );
2856 output( "\techo \"%s.dll TESTDLL \\\"%s.dll\\\"\" | %s -o $@\n", obj,
2857 obj_dir_path( make, obj ), tools_path( make, "wrc" ));
2859 output( "%s.dll:", obj_dir_path( make, obj ));
2860 output_filename( source->filename );
2861 output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
2862 output_filenames( dep_libs );
2863 output_filename( tools_path( make, "winebuild" ));
2864 output_filename( tools_path( make, "winegcc" ));
2865 output( "\n" );
2866 output( "\t%s -s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2867 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2868 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2869 output_filename( "--lib-suffix=.cross.a" );
2870 output_filename( "-shared" );
2871 output_filename( source->filename );
2872 output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
2873 output_filenames( all_libs );
2874 output_filename( "$(LDFLAGS)" );
2875 output( "\n" );
2880 /*******************************************************************
2881 * output_source_default
2883 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
2885 struct strarray extradefs = get_expanded_file_local_var( make, obj, "EXTRADEFS" );
2886 int is_dll_src = (make->testdll &&
2887 strendswith( source->name, ".c" ) &&
2888 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
2889 int need_cross = (make->testdll ||
2890 (source->file->flags & FLAG_C_IMPLIB) ||
2891 (make->module && make->staticlib));
2893 if ((source->file->flags & FLAG_GENERATED) &&
2894 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2895 strarray_add( &make->clean_files, source->filename );
2896 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
2897 strarray_add( is_dll_src ? &make->clean_files : &make->object_files, strmake( "%s.o", obj ));
2898 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2899 output( "\t$(CC) -c -o $@ %s", source->filename );
2900 output_filenames( make->include_args );
2901 output_filenames( make->define_args );
2902 output_filenames( extradefs );
2903 if (make->module || make->staticlib || make->sharedlib || make->testdll)
2905 output_filenames( dll_flags );
2906 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2908 output_filenames( extra_cflags );
2909 output_filenames( cpp_flags );
2910 output_filename( "$(CFLAGS)" );
2911 output( "\n" );
2912 if (crosstarget && need_cross)
2914 strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
2915 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2916 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2917 output_filenames( make->include_args );
2918 output_filenames( make->define_args );
2919 output_filenames( extradefs );
2920 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2921 output_filename( "-DWINE_CROSSTEST" );
2922 output_filenames( cpp_flags );
2923 output_filename( "$(CROSSCFLAGS)" );
2924 output( "\n" );
2926 if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
2928 strarray_add( &make->c2man_files, source->filename );
2929 if (make->testdll && !is_dll_src)
2931 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
2932 output( "%s.ok:\n", obj_dir_path( make, obj ));
2933 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2934 top_src_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ),
2935 make->testdll, replace_extension( make->testdll, ".dll", "_test.exe" ),
2936 dll_ext, obj );
2939 output( "%s.o", obj_dir_path( make, obj ));
2940 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2941 output( ":" );
2942 output_filenames( source->dependencies );
2943 output( "\n" );
2947 /* dispatch table to output rules for a single source file */
2948 static const struct
2950 const char *ext;
2951 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
2952 } output_source_funcs[] =
2954 { "y", output_source_y },
2955 { "l", output_source_l },
2956 { "h", output_source_h },
2957 { "rh", output_source_h },
2958 { "inl", output_source_h },
2959 { "rc", output_source_rc },
2960 { "mc", output_source_mc },
2961 { "res", output_source_res },
2962 { "idl", output_source_idl },
2963 { "tlb", output_source_tlb },
2964 { "sfd", output_source_sfd },
2965 { "svg", output_source_svg },
2966 { "nls", output_source_nls },
2967 { "desktop", output_source_desktop },
2968 { "po", output_source_po },
2969 { "in", output_source_in },
2970 { "x", output_source_x },
2971 { "spec", output_source_spec },
2972 { NULL, output_source_default }
2976 /*******************************************************************
2977 * output_man_pages
2979 static void output_man_pages( struct makefile *make )
2981 if (make->c2man_files.count)
2983 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2985 output( "manpages::\n" );
2986 output( "\t%s -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
2987 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
2988 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
2989 output_filename( strmake( "-o %s/man%s",
2990 top_obj_dir_path( make, "documentation" ), man_ext ));
2991 output_filenames( make->c2man_files );
2992 output( "\n" );
2993 output( "htmlpages::\n" );
2994 output( "\t%s -Th -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
2995 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
2996 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
2997 output_filename( strmake( "-o %s",
2998 top_obj_dir_path( make, "documentation/html" )));
2999 output_filenames( make->c2man_files );
3000 output( "\n" );
3001 output( "sgmlpages::\n" );
3002 output( "\t%s -Ts -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3003 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3004 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3005 output_filename( strmake( "-o %s",
3006 top_obj_dir_path( make, "documentation/api-guide" )));
3007 output_filenames( make->c2man_files );
3008 output( "\n" );
3009 output( "xmlpages::\n" );
3010 output( "\t%s -Tx -w %s", top_src_dir_path( make, "tools/c2man.pl" ), spec_file );
3011 output_filename( strmake( "-R%s", top_src_dir_path( make, "" )));
3012 output_filename( strmake( "-I%s", top_src_dir_path( make, "include" )));
3013 output_filename( strmake( "-o %s",
3014 top_obj_dir_path( make, "documentation/api-guide-xml" )));
3015 output_filenames( make->c2man_files );
3016 output( "\n" );
3017 strarray_add( &make->phony_targets, "manpages" );
3018 strarray_add( &make->phony_targets, "htmlpages" );
3019 strarray_add( &make->phony_targets, "sgmlpages" );
3020 strarray_add( &make->phony_targets, "xmlpages" );
3022 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
3026 /*******************************************************************
3027 * output_module
3029 static void output_module( struct makefile *make )
3031 struct strarray all_libs = empty_strarray;
3032 struct strarray dep_libs = empty_strarray;
3033 char *module_path = obj_dir_path( make, make->module );
3034 char *spec_file = NULL;
3035 unsigned int i;
3037 if (!make->appmode.count)
3038 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3039 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 0 ));
3040 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0 ));
3041 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
3042 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
3044 if (*dll_ext)
3046 for (i = 0; i < make->delayimports.count; i++)
3047 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
3048 strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
3049 strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
3050 add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
3051 strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
3052 add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
3053 strmake( "d$(dlldir)/fakedlls/%s", make->module ));
3054 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
3056 else
3058 strarray_add( &all_libs, "-lwine" );
3059 strarray_add( &make->all_targets, make->module );
3060 add_install_rule( make, make->module, make->module,
3061 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
3062 output( "%s:", module_path );
3064 if (spec_file) output_filename( spec_file );
3065 output_filenames_obj_dir( make, make->object_files );
3066 output_filenames( dep_libs );
3067 output_filename( tools_path( make, "winebuild" ));
3068 output_filename( tools_path( make, "winegcc" ));
3069 output( "\n" );
3070 output( "\t%s -o $@", tools_path( make, "winegcc" ));
3071 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
3072 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
3073 output_filenames( target_flags );
3074 output_filenames( unwind_flags );
3075 if (spec_file)
3077 output( " -shared %s", spec_file );
3078 output_filenames( make->extradllflags );
3080 else output_filenames( make->appmode );
3081 output_filenames_obj_dir( make, make->object_files );
3082 output_filenames( all_libs );
3083 output_filename( "$(LDFLAGS)" );
3084 output( "\n" );
3086 if (spec_file && make->importlib)
3088 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3089 if (*dll_ext && !make->implib_objs.count)
3091 strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
3092 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
3093 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
3094 output_filenames( target_flags );
3095 if (make->is_win16) output_filename( "-m16" );
3096 output( "\n" );
3097 add_install_rule( make, make->importlib,
3098 strmake( "lib%s.def", make->importlib ),
3099 strmake( "d$(dlldir)/lib%s.def", make->importlib ));
3101 else
3103 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3104 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3105 output_filenames_obj_dir( make, make->implib_objs );
3106 output( "\n" );
3107 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
3108 output_filenames( target_flags );
3109 output_filenames_obj_dir( make, make->implib_objs );
3110 output( "\n" );
3111 add_install_rule( make, make->importlib,
3112 strmake( "lib%s.a", make->importlib ),
3113 strmake( "d$(dlldir)/lib%s.a", make->importlib ));
3115 if (crosstarget && !make->is_win16)
3117 struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
3118 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3119 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3120 output_filenames_obj_dir( make, cross_files );
3121 output( "\n" );
3122 output( "\t%s -b %s -w --implib -o $@ --export %s",
3123 tools_path( make, "winebuild" ), crosstarget, spec_file );
3124 output_filenames_obj_dir( make, cross_files );
3125 output( "\n" );
3129 if (spec_file)
3130 output_man_pages( make );
3131 else if (*dll_ext && !make->is_win16)
3133 char *binary = replace_extension( make->module, ".exe", "" );
3134 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3139 /*******************************************************************
3140 * output_static_lib
3142 static void output_static_lib( struct makefile *make )
3144 strarray_add( &make->all_targets, make->staticlib );
3145 output( "%s:", obj_dir_path( make, make->staticlib ));
3146 output_filenames_obj_dir( make, make->object_files );
3147 output( "\n\trm -f $@\n" );
3148 output( "\t$(AR) $(ARFLAGS) $@" );
3149 output_filenames_obj_dir( make, make->object_files );
3150 output( "\n\t$(RANLIB) $@\n" );
3151 add_install_rule( make, make->staticlib, make->staticlib,
3152 strmake( "d$(dlldir)/%s", make->staticlib ));
3153 if (crosstarget && make->module)
3155 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3157 strarray_add( &make->all_targets, name );
3158 output( "%s:", obj_dir_path( make, name ));
3159 output_filenames_obj_dir( make, make->crossobj_files );
3160 output( "\n\trm -f $@\n" );
3161 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
3162 output_filenames_obj_dir( make, make->crossobj_files );
3163 output( "\n\t%s-ranlib $@\n", crosstarget );
3168 /*******************************************************************
3169 * output_shared_lib
3171 static void output_shared_lib( struct makefile *make )
3173 unsigned int i;
3174 char *basename, *p;
3175 struct strarray names = get_shared_lib_names( make->sharedlib );
3176 struct strarray all_libs = empty_strarray;
3177 struct strarray dep_libs = empty_strarray;
3179 basename = xstrdup( make->sharedlib );
3180 if ((p = strchr( basename, '.' ))) *p = 0;
3182 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3183 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3184 strarray_addall( &all_libs, add_default_libraries( make, &dep_libs ));
3186 output( "%s:", obj_dir_path( make, make->sharedlib ));
3187 output_filenames_obj_dir( make, make->object_files );
3188 output_filenames( dep_libs );
3189 output( "\n" );
3190 output( "\t$(CC) -o $@" );
3191 output_filenames_obj_dir( make, make->object_files );
3192 output_filenames( all_libs );
3193 output_filename( "$(LDFLAGS)" );
3194 output( "\n" );
3195 add_install_rule( make, make->sharedlib, make->sharedlib,
3196 strmake( "p$(libdir)/%s", make->sharedlib ));
3197 for (i = 1; i < names.count; i++)
3199 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3200 output_symlink_rule( obj_dir_path( make, names.str[i-1] ), obj_dir_path( make, names.str[i] ));
3201 add_install_rule( make, names.str[i], names.str[i-1],
3202 strmake( "y$(libdir)/%s", names.str[i] ));
3204 strarray_addall( &make->all_targets, names );
3208 /*******************************************************************
3209 * output_import_lib
3211 static void output_import_lib( struct makefile *make )
3213 char *def_file = replace_extension( make->importlib, ".a", ".def" );
3215 /* stand-alone import lib (for libwine) */
3216 if (!strncmp( def_file, "lib", 3 )) def_file += 3;
3217 output( "%s: %s\n", obj_dir_path( make, make->importlib ), src_dir_path( make, def_file ));
3218 output( "\t%s -l $@ -d %s\n", dlltool, src_dir_path( make, def_file ));
3219 add_install_rule( make, make->importlib, make->importlib, strmake( "d$(libdir)/%s", make->importlib ));
3220 strarray_add( &make->all_targets, make->importlib );
3224 /*******************************************************************
3225 * output_test_module
3227 static void output_test_module( struct makefile *make )
3229 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3230 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3231 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3232 struct strarray dep_libs = empty_strarray;
3233 struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0 );
3235 add_import_libs( make, &dep_libs, get_default_imports( make ), 0 ); /* dependencies only */
3236 strarray_addall( &all_libs, libs );
3237 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, dll_ext ));
3238 strarray_add( &make->clean_files, strmake( "%s%s", stripped, dll_ext ));
3239 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
3240 output( "\t%s -o $@", tools_path( make, "winegcc" ));
3241 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
3242 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
3243 output_filenames( target_flags );
3244 output_filenames( unwind_flags );
3245 output_filenames( make->appmode );
3246 output_filenames_obj_dir( make, make->object_files );
3247 output_filenames( all_libs );
3248 output_filename( "$(LDFLAGS)" );
3249 output( "\n" );
3250 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
3251 output( "\t%s -s -o $@", tools_path( make, "winegcc" ));
3252 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
3253 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
3254 output_filenames( target_flags );
3255 output_filenames( unwind_flags );
3256 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3257 output_filenames( make->appmode );
3258 output_filenames_obj_dir( make, make->object_files );
3259 output_filenames( all_libs );
3260 output_filename( "$(LDFLAGS)" );
3261 output( "\n" );
3262 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
3263 obj_dir_path( make, stripped ), dll_ext );
3264 output_filenames_obj_dir( make, make->object_files );
3265 output_filenames( dep_libs );
3266 output_filename( tools_path( make, "winebuild" ));
3267 output_filename( tools_path( make, "winegcc" ));
3268 output( "\n" );
3270 if (!make->disabled)
3271 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
3272 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
3273 obj_dir_path( make, stripped ), dll_ext );
3274 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
3275 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
3277 if (crosstarget)
3279 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
3281 dep_libs = empty_strarray;
3282 all_libs = add_import_libs( make, &dep_libs, make->imports, 1 );
3283 add_import_libs( make, &dep_libs, get_default_imports( make ), 1 ); /* dependencies only */
3284 strarray_addall( &all_libs, libs );
3285 strarray_add( &make->clean_files, crosstest );
3286 output( "%s:", obj_dir_path( make, crosstest ));
3287 output_filenames_obj_dir( make, make->crossobj_files );
3288 output_filenames( dep_libs );
3289 output_filename( tools_path( make, "winebuild" ));
3290 output_filename( tools_path( make, "winegcc" ));
3291 output( "\n" );
3292 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
3293 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
3294 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
3295 output_filename( "--lib-suffix=.cross.a" );
3296 output_filenames_obj_dir( make, make->crossobj_files );
3297 output_filenames( all_libs );
3298 output_filename( "$(LDFLAGS)" );
3299 output( "\n" );
3300 if (!make->disabled)
3302 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
3303 strarray_add( &make->phony_targets, obj_dir_path( make, "crosstest" ));
3304 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
3308 output_filenames_obj_dir( make, make->ok_files );
3309 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
3310 if (!make->disabled)
3312 output( "check test:" );
3313 output_filenames_obj_dir( make, make->ok_files );
3314 output( "\n" );
3315 strarray_add( &make->phony_targets, "check" );
3316 strarray_add( &make->phony_targets, "test" );
3318 output( "testclean::\n" );
3319 output( "\trm -f" );
3320 output_filenames_obj_dir( make, make->ok_files );
3321 output( "\n" );
3322 strarray_addall( &make->clean_files, make->ok_files );
3323 strarray_add( &make->phony_targets, "testclean" );
3327 /*******************************************************************
3328 * output_programs
3330 static void output_programs( struct makefile *make )
3332 unsigned int i, j;
3333 char *ldrpath_local = get_expanded_make_variable( make, "LDRPATH_LOCAL" );
3334 char *ldrpath_install = get_expanded_make_variable( make, "LDRPATH_INSTALL" );
3336 for (i = 0; i < make->programs.count; i++)
3338 char *program_installed = NULL;
3339 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3340 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3341 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3342 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3343 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3345 if (!objs.count) objs = make->object_files;
3346 strarray_addall( &all_libs, add_default_libraries( make, &deps ));
3348 output( "%s:", obj_dir_path( make, program ) );
3349 output_filenames_obj_dir( make, objs );
3350 output_filenames( deps );
3351 output( "\n" );
3352 output( "\t$(CC) -o $@" );
3353 output_filenames_obj_dir( make, objs );
3355 if (strarray_exists( &all_libs, "-lwine" ))
3357 strarray_add( &all_libs, strmake( "-L%s", top_obj_dir_path( make, "libs/wine" )));
3358 if (ldrpath_local && ldrpath_install)
3360 program_installed = strmake( "%s-installed%s", make->programs.str[i], exe_ext );
3361 output_filename( ldrpath_local );
3362 output_filenames( all_libs );
3363 output_filename( "$(LDFLAGS)" );
3364 output( "\n" );
3365 output( "%s:", obj_dir_path( make, program_installed ) );
3366 output_filenames_obj_dir( make, objs );
3367 output_filenames( deps );
3368 output( "\n" );
3369 output( "\t$(CC) -o $@" );
3370 output_filenames_obj_dir( make, objs );
3371 output_filename( ldrpath_install );
3372 strarray_add( &make->all_targets, program_installed );
3376 output_filenames( all_libs );
3377 output_filename( "$(LDFLAGS)" );
3378 output( "\n" );
3379 strarray_add( &make->all_targets, program );
3381 for (j = 0; j < symlinks.count; j++)
3383 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3384 output_symlink_rule( obj_dir_path( make, program ), obj_dir_path( make, symlinks.str[j] ));
3386 strarray_addall( &make->all_targets, symlinks );
3388 add_install_rule( make, program, program_installed ? program_installed : program,
3389 strmake( "p$(bindir)/%s", program ));
3390 for (j = 0; j < symlinks.count; j++)
3391 add_install_rule( make, symlinks.str[j], program,
3392 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3397 /*******************************************************************
3398 * output_subdirs
3400 static void output_subdirs( struct makefile *make )
3402 struct strarray symlinks = empty_strarray;
3403 struct strarray all_deps = empty_strarray;
3404 struct strarray build_deps = empty_strarray;
3405 struct strarray builddeps_deps = empty_strarray;
3406 struct strarray makefile_deps = empty_strarray;
3407 struct strarray clean_files = empty_strarray;
3408 struct strarray testclean_files = empty_strarray;
3409 struct strarray distclean_files = empty_strarray;
3410 struct strarray tools_deps = empty_strarray;
3411 struct strarray winetest_deps = empty_strarray;
3412 struct strarray crosstest_deps = empty_strarray;
3413 unsigned int i, j;
3415 strarray_addall( &distclean_files, make->distclean_files );
3416 for (i = 0; i < make->subdirs.count; i++)
3418 const struct makefile *submake = make->submakes[i];
3419 char *subdir = base_dir_path( submake, "" );
3421 strarray_add( &makefile_deps, top_src_dir_path( make, base_dir_path( submake,
3422 strmake ( "%s.in", output_makefile_name ))));
3423 for (j = 0; j < submake->clean_files.count; j++)
3424 strarray_add( &clean_files, base_dir_path( submake, submake->clean_files.str[j] ));
3425 for (j = 0; j < submake->distclean_files.count; j++)
3426 strarray_add( &distclean_files, base_dir_path( submake, submake->distclean_files.str[j] ));
3427 for (j = 0; j < submake->ok_files.count; j++)
3428 strarray_add( &testclean_files, base_dir_path( submake, submake->ok_files.str[j] ));
3430 /* import libs are still created for disabled dirs, except for win16 ones */
3431 if (submake->module && submake->importlib && !(submake->disabled && submake->is_win16))
3433 char *importlib_path = base_dir_path( submake, strmake( "lib%s", submake->importlib ));
3434 if (submake->implib_objs.count)
3436 output( "%s.a: dummy\n", importlib_path );
3437 output( "\t@cd %s && $(MAKE) lib%s.a\n", subdir, submake->importlib );
3438 strarray_add( &tools_deps, strmake( "%s.a", importlib_path ));
3439 if (crosstarget)
3441 output( "%s.cross.a: dummy\n", importlib_path );
3442 output( "\t@cd %s && $(MAKE) lib%s.cross.a\n", subdir, submake->importlib );
3443 strarray_add( &tools_deps, strmake( "%s.cross.a", importlib_path ));
3446 else
3448 const char *libext = *dll_ext ? ".def" : ".a";
3449 char *spec_file = top_src_dir_path( make, base_dir_path( submake,
3450 replace_extension( submake->module, ".dll", ".spec" )));
3451 output( "%s%s: %s", importlib_path, libext, spec_file );
3452 output_filename( tools_path( make, "winebuild" ));
3453 output( "\n" );
3454 output( "\t%s -w -o $@", tools_path( make, "winebuild" ));
3455 output_filename( *dll_ext ? "--def" : "--implib" );
3456 output_filenames( target_flags );
3457 if (submake->is_win16) output_filename( "-m16" );
3458 output_filename( "--export" );
3459 output_filename( spec_file );
3460 output( "\n" );
3461 strarray_add( &build_deps, strmake( "%s%s", importlib_path, libext ));
3462 if (crosstarget && !submake->is_win16)
3464 output( "%s.cross.a: %s", importlib_path, spec_file );
3465 output_filename( tools_path( make, "winebuild" ));
3466 output( "\n" );
3467 output( "\t%s -b %s -w -o $@", tools_path( make, "winebuild" ), crosstarget );
3468 output_filename( "--implib" );
3469 output_filenames( target_flags );
3470 output_filename( "--export" );
3471 output_filename( spec_file );
3472 output( "\n" );
3473 strarray_add( &build_deps, strmake( "%s.cross.a", importlib_path ));
3476 strarray_addall( &symlinks, output_importlib_symlinks( make, submake ));
3479 if (submake->disabled) continue;
3480 strarray_add( &all_deps, subdir );
3482 if (submake->module)
3484 if (!submake->staticlib)
3486 strarray_add( &builddeps_deps, subdir );
3487 if (!make->appmode.count)
3489 output( "manpages htmlpages sgmlpages xmlpages::\n" );
3490 output( "\t@cd %s && $(MAKE) $@\n", subdir );
3493 else strarray_add( &tools_deps, subdir );
3495 else if (submake->testdll)
3497 output( "check test::\n" );
3498 output( "\t@cd %s && $(MAKE) test\n", subdir );
3499 strarray_add( &winetest_deps, subdir );
3500 strarray_add( &builddeps_deps, subdir );
3501 if (crosstarget)
3503 char *target = base_dir_path( submake, "crosstest" );
3504 output( "crosstest: %s\n", target );
3505 output( "%s: dummy\n", target );
3506 output( "\t@cd %s && $(MAKE) crosstest\n", subdir );
3507 strarray_add( &crosstest_deps, target );
3508 strarray_add( &builddeps_deps, target );
3512 if (submake->install_rules[INSTALL_LIB].count)
3514 output( "install install-lib:: %s\n", submake->base_dir );
3515 output_install_commands( make, submake, submake->install_rules[INSTALL_LIB] );
3517 if (submake->install_rules[INSTALL_DEV].count)
3519 output( "install install-dev:: %s\n", submake->base_dir );
3520 output_install_commands( make, submake, submake->install_rules[INSTALL_DEV] );
3523 output( "all:" );
3524 output_filenames( all_deps );
3525 output( "\n" );
3526 output_filenames( all_deps );
3527 output( ": dummy\n" );
3528 output( "\t@cd $@ && $(MAKE)\n" );
3529 output( "Makefile:" );
3530 output_filenames( makefile_deps );
3531 output( "\n" );
3532 output_filenames( makefile_deps );
3533 output( ":\n" );
3534 if (winetest_deps.count)
3536 output( "programs/winetest:" );
3537 output_filenames( winetest_deps );
3538 output( "\n" );
3539 strarray_add( &make->phony_targets, "check" );
3540 strarray_add( &make->phony_targets, "test" );
3542 if (crosstest_deps.count)
3544 output( "crosstest:" );
3545 output_filenames( crosstest_deps );
3546 output( "\n" );
3547 strarray_add( &make->phony_targets, "crosstest" );
3549 output( "clean::\n");
3550 output_rm_filenames( clean_files );
3551 output( "testclean::\n");
3552 output_rm_filenames( testclean_files );
3553 output( "distclean::\n");
3554 output_rm_filenames( distclean_files );
3555 output_filenames( tools_deps );
3556 output( ":" );
3557 output_filename( tools_dir_path( make, "widl" ));
3558 output_filename( tools_dir_path( make, "winebuild" ));
3559 output_filename( tools_dir_path( make, "winegcc" ));
3560 output_filename( obj_dir_path( make, "include" ));
3561 output( "\n" );
3562 output_filenames( builddeps_deps );
3563 output( ": __builddeps__\n" );
3564 strarray_add( &make->phony_targets, "distclean" );
3565 strarray_add( &make->phony_targets, "testclean" );
3566 strarray_addall( &make->phony_targets, all_deps );
3567 strarray_addall( &make->phony_targets, crosstest_deps );
3569 strarray_addall( &make->clean_files, symlinks );
3570 strarray_addall( &build_deps, tools_deps );
3571 strarray_addall( &build_deps, symlinks );
3572 if (build_deps.count)
3574 output( "__builddeps__:" );
3575 output_filenames( build_deps );
3576 output( "\n" );
3577 strarray_add( &make->phony_targets, "__builddeps__" );
3579 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3583 /*******************************************************************
3584 * output_sources
3586 static void output_sources( struct makefile *make )
3588 struct incl_file *source;
3589 unsigned int i, j;
3591 strarray_add( &make->phony_targets, "all" );
3593 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3595 char *obj = xstrdup( source->name );
3596 char *ext = get_extension( obj );
3598 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3599 *ext++ = 0;
3601 for (j = 0; output_source_funcs[j].ext; j++)
3602 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3604 output_source_funcs[j].fn( make, source, obj );
3605 free( obj );
3608 if (make->dlldata_files.count)
3610 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3611 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3612 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
3613 output_filenames( make->dlldata_files );
3614 output( "\n" );
3617 if (make->staticlib) output_static_lib( make );
3618 else if (make->module) output_module( make );
3619 else if (make->importlib) output_import_lib( make );
3620 else if (make->sharedlib) output_shared_lib( make );
3621 else if (make->testdll) output_test_module( make );
3623 if (make->programs.count) output_programs( make );
3625 for (i = 0; i < make->scripts.count; i++)
3626 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3627 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3629 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3630 strarray_add( &make->distclean_files, "Makefile" );
3631 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3633 if (!make->base_dir)
3634 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3635 else if (!strcmp( make->base_dir, "po" ))
3636 strarray_add( &make->distclean_files, "LINGUAS" );
3638 if (make->subdirs.count) output_subdirs( make );
3640 if (!make->disabled)
3642 if (make->all_targets.count)
3644 output( "all:" );
3645 output_filenames_obj_dir( make, make->all_targets );
3646 output( "\n" );
3648 output_install_rules( make, INSTALL_LIB, "install-lib" );
3649 output_install_rules( make, INSTALL_DEV, "install-dev" );
3650 output_uninstall_rules( make );
3653 strarray_addall( &make->clean_files, make->object_files );
3654 for (i = 0; i < make->crossobj_files.count; i++)
3655 strarray_add_uniq( &make->clean_files, make->crossobj_files.str[i] );
3656 strarray_addall( &make->clean_files, make->all_targets );
3657 strarray_addall( &make->clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
3659 if (make->clean_files.count)
3661 output( "%s::\n", obj_dir_path( make, "clean" ));
3662 output( "\trm -f" );
3663 output_filenames_obj_dir( make, make->clean_files );
3664 output( "\n" );
3665 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
3666 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3669 if (make->phony_targets.count)
3671 output( ".PHONY:" );
3672 output_filenames( make->phony_targets );
3673 output( "\n" );
3678 /*******************************************************************
3679 * create_temp_file
3681 static FILE *create_temp_file( const char *orig )
3683 char *name = xmalloc( strlen(orig) + 13 );
3684 unsigned int i, id = getpid();
3685 int fd;
3686 FILE *ret = NULL;
3688 for (i = 0; i < 100; i++)
3690 sprintf( name, "%s.tmp%08x", orig, id );
3691 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3693 ret = fdopen( fd, "w" );
3694 break;
3696 if (errno != EEXIST) break;
3697 id += 7777;
3699 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3700 temp_file_name = name;
3701 return ret;
3705 /*******************************************************************
3706 * rename_temp_file
3708 static void rename_temp_file( const char *dest )
3710 int ret = rename( temp_file_name, dest );
3711 if (ret == -1 && errno == EEXIST)
3713 /* rename doesn't overwrite on windows */
3714 unlink( dest );
3715 ret = rename( temp_file_name, dest );
3717 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3718 temp_file_name = NULL;
3722 /*******************************************************************
3723 * are_files_identical
3725 static int are_files_identical( FILE *file1, FILE *file2 )
3727 for (;;)
3729 char buffer1[8192], buffer2[8192];
3730 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3731 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3732 if (size1 != size2) return 0;
3733 if (!size1) return feof( file1 ) && feof( file2 );
3734 if (memcmp( buffer1, buffer2, size1 )) return 0;
3739 /*******************************************************************
3740 * rename_temp_file_if_changed
3742 static void rename_temp_file_if_changed( const char *dest )
3744 FILE *file1, *file2;
3745 int do_rename = 1;
3747 if ((file1 = fopen( dest, "r" )))
3749 if ((file2 = fopen( temp_file_name, "r" )))
3751 do_rename = !are_files_identical( file1, file2 );
3752 fclose( file2 );
3754 fclose( file1 );
3756 if (!do_rename)
3758 unlink( temp_file_name );
3759 temp_file_name = NULL;
3761 else rename_temp_file( dest );
3765 /*******************************************************************
3766 * output_linguas
3768 static void output_linguas( const struct makefile *make )
3770 const char *dest = base_dir_path( make, "LINGUAS" );
3771 struct incl_file *source;
3773 output_file = create_temp_file( dest );
3775 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3776 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3777 if (strendswith( source->name, ".po" ))
3778 output( "%s\n", replace_extension( source->name, ".po", "" ));
3780 if (fclose( output_file )) fatal_perror( "write" );
3781 output_file = NULL;
3782 rename_temp_file_if_changed( dest );
3786 /*******************************************************************
3787 * output_testlist
3789 static void output_testlist( const struct makefile *make )
3791 const char *dest = base_dir_path( make, "testlist.c" );
3792 struct strarray files = empty_strarray;
3793 unsigned int i;
3795 for (i = 0; i < make->ok_files.count; i++)
3796 strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
3798 output_file = create_temp_file( dest );
3800 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3801 output( "#define WIN32_LEAN_AND_MEAN\n" );
3802 output( "#include <windows.h>\n\n" );
3803 output( "#define STANDALONE\n" );
3804 output( "#include \"wine/test.h\"\n\n" );
3806 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
3807 output( "\n" );
3808 output( "const struct test winetest_testlist[] =\n" );
3809 output( "{\n" );
3810 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
3811 output( " { 0, 0 }\n" );
3812 output( "};\n" );
3814 if (fclose( output_file )) fatal_perror( "write" );
3815 output_file = NULL;
3816 rename_temp_file_if_changed( dest );
3820 /*******************************************************************
3821 * output_gitignore
3823 static void output_gitignore( const char *dest, struct strarray files )
3825 int i;
3827 output_file = create_temp_file( dest );
3829 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3830 for (i = 0; i < files.count; i++)
3832 if (!strchr( files.str[i], '/' )) output( "/" );
3833 output( "%s\n", files.str[i] );
3836 if (fclose( output_file )) fatal_perror( "write" );
3837 output_file = NULL;
3838 rename_temp_file( dest );
3842 /*******************************************************************
3843 * output_top_variables
3845 static void output_top_variables( const struct makefile *make )
3847 unsigned int i;
3848 struct strarray *vars = &top_makefile->vars;
3850 if (!make->base_dir) return; /* don't output variables in the top makefile */
3852 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
3853 output( "all:\n\n" );
3854 for (i = 0; i < vars->count; i += 2)
3856 if (!strcmp( vars->str[i], "SUBDIRS" )) continue; /* not inherited */
3857 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
3859 output( "\n" );
3863 /*******************************************************************
3864 * output_dependencies
3866 static void output_dependencies( struct makefile *make )
3868 struct strarray ignore_files = empty_strarray;
3869 char buffer[1024];
3870 FILE *src_file;
3871 int found = 0;
3873 if (make->base_dir) create_dir( make->base_dir );
3875 output_file_name = base_dir_path( make, output_makefile_name );
3876 output_file = create_temp_file( output_file_name );
3877 output_top_variables( make );
3879 /* copy the contents of the source makefile */
3880 src_file = open_input_makefile( make );
3881 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
3883 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
3884 found = !strncmp( buffer, separator, strlen(separator) );
3886 if (fclose( src_file )) fatal_perror( "close" );
3887 input_file_name = NULL;
3889 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
3890 output_sources( make );
3892 fclose( output_file );
3893 output_file = NULL;
3894 rename_temp_file( output_file_name );
3896 strarray_addall( &ignore_files, make->distclean_files );
3897 strarray_addall( &ignore_files, make->clean_files );
3898 if (make->testdll) output_testlist( make );
3899 if (make->base_dir && !strcmp( make->base_dir, "po" )) output_linguas( make );
3900 if (!make->src_dir) output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
3902 create_file_directories( make, ignore_files );
3904 output_file_name = NULL;
3908 /*******************************************************************
3909 * load_sources
3911 static void load_sources( struct makefile *make )
3913 static const char *source_vars[] =
3915 "SOURCES",
3916 "C_SRCS",
3917 "OBJC_SRCS",
3918 "RC_SRCS",
3919 "MC_SRCS",
3920 "IDL_SRCS",
3921 "BISON_SRCS",
3922 "LEX_SRCS",
3923 "HEADER_SRCS",
3924 "XTEMPLATE_SRCS",
3925 "SVG_SRCS",
3926 "FONT_SRCS",
3927 "IN_SRCS",
3928 "PO_SRCS",
3929 "MANPAGES",
3930 NULL
3932 const char **var;
3933 unsigned int i;
3934 struct strarray value;
3935 struct incl_file *file;
3937 if (root_src_dir)
3939 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
3940 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
3942 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
3943 strarray_set_value( &make->vars, "top_srcdir", top_src_dir_path( make, "" ));
3944 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
3946 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
3947 make->module = get_expanded_make_variable( make, "MODULE" );
3948 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
3949 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
3950 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
3951 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
3953 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
3954 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
3955 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
3956 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
3957 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
3958 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
3959 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
3960 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
3962 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
3964 make->disabled = make->base_dir && strarray_exists( &disabled_dirs, make->base_dir );
3965 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" ) || strarray_exists( &make->appmode, "-m16" );
3966 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
3968 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
3969 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 ) ||
3970 !strcmp( make->imports.str[i], "ucrtbase" );
3972 if (make->module && !make->install_lib.count && !make->install_dev.count)
3974 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
3975 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
3976 else strarray_add( &make->install_lib, make->module );
3979 make->include_paths = empty_strarray;
3980 make->include_args = empty_strarray;
3981 make->define_args = empty_strarray;
3982 strarray_add( &make->define_args, "-D__WINESRC__" );
3984 value = get_expanded_make_var_array( make, "EXTRAINCL" );
3985 for (i = 0; i < value.count; i++)
3986 if (!strncmp( value.str[i], "-I", 2 ))
3987 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
3988 else
3989 strarray_add_uniq( &make->define_args, value.str[i] );
3990 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
3992 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
3993 if (make->src_dir)
3994 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
3995 if (make->parent_dir)
3996 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
3997 strarray_add( &make->include_args, strmake( "-I%s", top_obj_dir_path( make, "include" )));
3998 if (make->top_src_dir)
3999 strarray_add( &make->include_args, strmake( "-I%s", top_src_dir_path( make, "include" )));
4000 if (make->use_msvcrt)
4001 strarray_add( &make->include_args, strmake( "-I%s", top_src_dir_path( make, "include/msvcrt" )));
4002 for (i = 0; i < make->include_paths.count; i++)
4003 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, make->include_paths.str[i] )));
4005 list_init( &make->sources );
4006 list_init( &make->includes );
4008 for (var = source_vars; *var; var++)
4010 value = get_expanded_make_var_array( make, *var );
4011 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4014 add_generated_sources( make );
4016 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
4017 for (i = 0; i < value.count; i++)
4019 /* default to .c for unknown extra object files */
4020 if (strendswith( value.str[i], ".o" ))
4021 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
4022 else
4023 add_generated_source( make, value.str[i], NULL );
4026 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4027 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4031 /*******************************************************************
4032 * parse_makeflags
4034 static void parse_makeflags( const char *flags )
4036 const char *p = flags;
4037 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4039 while (*p)
4041 while (isspace(*p)) p++;
4042 var = buffer;
4043 while (*p && !isspace(*p))
4045 if (*p == '\\' && p[1]) p++;
4046 *var++ = *p++;
4048 *var = 0;
4049 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4054 /*******************************************************************
4055 * parse_option
4057 static int parse_option( const char *opt )
4059 if (opt[0] != '-')
4061 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4062 return 0;
4064 switch(opt[1])
4066 case 'f':
4067 if (opt[2]) output_makefile_name = opt + 2;
4068 break;
4069 case 'R':
4070 relative_dir_mode = 1;
4071 break;
4072 default:
4073 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4074 exit(1);
4076 return 1;
4080 /*******************************************************************
4081 * main
4083 int main( int argc, char *argv[] )
4085 const char *makeflags = getenv( "MAKEFLAGS" );
4086 int i, j;
4088 if (makeflags) parse_makeflags( makeflags );
4090 i = 1;
4091 while (i < argc)
4093 if (parse_option( argv[i] ))
4095 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4096 argc--;
4098 else i++;
4101 if (relative_dir_mode)
4103 char *relpath;
4105 if (argc != 3)
4107 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4108 exit( 1 );
4110 relpath = get_relative_path( argv[1], argv[2] );
4111 printf( "%s\n", relpath ? relpath : "." );
4112 exit( 0 );
4115 atexit( cleanup_files );
4116 signal( SIGTERM, exit_on_signal );
4117 signal( SIGINT, exit_on_signal );
4118 #ifdef SIGHUP
4119 signal( SIGHUP, exit_on_signal );
4120 #endif
4122 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4124 top_makefile = parse_makefile( NULL );
4126 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4127 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4128 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4129 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4130 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4131 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
4132 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4134 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4135 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
4136 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
4137 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4138 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
4139 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4140 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4141 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4142 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4143 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4144 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4145 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4146 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4147 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4149 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4150 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4151 if (!exe_ext) exe_ext = "";
4152 if (!tools_ext) tools_ext = "";
4153 if (!man_ext) man_ext = "3w";
4155 if (argc == 1)
4157 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4158 top_makefile->subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4159 top_makefile->submakes = xmalloc( top_makefile->subdirs.count * sizeof(*top_makefile->submakes) );
4161 for (i = 0; i < top_makefile->subdirs.count; i++)
4162 top_makefile->submakes[i] = parse_makefile( top_makefile->subdirs.str[i] );
4164 load_sources( top_makefile );
4165 for (i = 0; i < top_makefile->subdirs.count; i++)
4166 load_sources( top_makefile->submakes[i] );
4168 for (i = 0; i < top_makefile->subdirs.count; i++)
4169 output_dependencies( top_makefile->submakes[i] );
4171 output_dependencies( top_makefile );
4172 return 0;
4175 for (i = 1; i < argc; i++)
4177 struct makefile *make = parse_makefile( argv[i] );
4178 load_sources( make );
4179 output_dependencies( make );
4181 return 0;