tools: Upgrade the config.guess/config.sub scripts.
[wine.git] / tools / makedep.c
blob8c5abde29402607c3406947465d91f5288130232
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <signal.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "wine/list.h"
38 enum incl_type
40 INCL_NORMAL, /* #include "foo.h" */
41 INCL_SYSTEM, /* #include <foo.h> */
42 INCL_IMPORT, /* idl import "foo.idl" */
43 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
44 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
47 struct dependency
49 int line; /* source line where this header is included */
50 enum incl_type type; /* type of include */
51 char *name; /* header name */
54 struct file
56 struct list entry;
57 char *name; /* full file name relative to cwd */
58 void *args; /* custom arguments for makefile rule */
59 unsigned int flags; /* flags (see below) */
60 unsigned int deps_count; /* files in use */
61 unsigned int deps_size; /* total allocated size */
62 struct dependency *deps; /* all header dependencies */
65 struct incl_file
67 struct list entry;
68 struct file *file;
69 char *name;
70 char *filename;
71 char *sourcename; /* source file name for generated headers */
72 struct incl_file *included_by; /* file that included this one */
73 int included_line; /* line where this file was included */
74 int system; /* is it a system include (#include <name>) */
75 struct incl_file *owner;
76 unsigned int files_count; /* files in use */
77 unsigned int files_size; /* total allocated size */
78 struct incl_file **files;
81 #define FLAG_GENERATED 0x000001 /* generated file */
82 #define FLAG_INSTALL 0x000002 /* file to install */
83 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
84 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
85 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
86 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
87 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
88 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
89 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
90 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
91 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
92 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
93 #define FLAG_SFD_FONTS 0x040000 /* sfd file generated bitmap fonts */
95 static const struct
97 unsigned int flag;
98 const char *ext;
99 } idl_outputs[] =
101 { FLAG_IDL_TYPELIB, ".tlb" },
102 { FLAG_IDL_REGTYPELIB, "_t.res" },
103 { FLAG_IDL_CLIENT, "_c.c" },
104 { FLAG_IDL_IDENT, "_i.c" },
105 { FLAG_IDL_PROXY, "_p.c" },
106 { FLAG_IDL_SERVER, "_s.c" },
107 { FLAG_IDL_REGISTER, "_r.res" },
108 { FLAG_IDL_HEADER, ".h" }
111 #define HASH_SIZE 137
113 static struct list files[HASH_SIZE];
115 struct strarray
117 unsigned int count; /* strings in use */
118 unsigned int size; /* total allocated size */
119 const char **str;
122 static const struct strarray empty_strarray;
124 /* variables common to all makefiles */
125 static struct strarray linguas;
126 static struct strarray dll_flags;
127 static struct strarray target_flags;
128 static struct strarray msvcrt_flags;
129 static struct strarray extra_cflags;
130 static struct strarray cpp_flags;
131 static struct strarray unwind_flags;
132 static struct strarray libs;
133 static struct strarray cmdline_vars;
134 static const char *root_src_dir;
135 static const char *tools_dir;
136 static const char *tools_ext;
137 static const char *exe_ext;
138 static const char *dll_ext;
139 static const char *man_ext;
140 static const char *dll_prefix;
141 static const char *crosstarget;
142 static const char *fontforge;
143 static const char *convert;
144 static const char *rsvg;
145 static const char *icotool;
147 struct makefile
149 struct strarray vars;
150 struct strarray include_args;
151 struct strarray define_args;
152 struct strarray programs;
153 struct strarray appmode;
154 struct strarray imports;
155 struct strarray delayimports;
156 struct strarray extradllflags;
157 struct list sources;
158 struct list includes;
159 const char *base_dir;
160 const char *src_dir;
161 const char *obj_dir;
162 const char *top_src_dir;
163 const char *top_obj_dir;
164 const char *parent_dir;
165 const char *module;
166 const char *testdll;
167 const char *staticlib;
168 const char *importlib;
169 int use_msvcrt;
170 int is_win16;
173 static struct makefile *top_makefile;
175 static const char *output_makefile_name = "Makefile";
176 static const char *input_makefile_name;
177 static const char *input_file_name;
178 static const char *output_file_name;
179 static const char *temp_file_name;
180 static int relative_dir_mode;
181 static int input_line;
182 static int output_column;
183 static FILE *output_file;
185 static const char Usage[] =
186 "Usage: makedep [options] directories\n"
187 "Options:\n"
188 " -R from to Compute the relative path between two directories\n"
189 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
190 " -ixxx Read input from file 'xxx' (default: Makefile.in)\n";
193 #ifndef __GNUC__
194 #define __attribute__(x)
195 #endif
197 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
198 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
199 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
200 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
202 /*******************************************************************
203 * fatal_error
205 static void fatal_error( const char *msg, ... )
207 va_list valist;
208 va_start( valist, msg );
209 if (input_file_name)
211 fprintf( stderr, "%s:", input_file_name );
212 if (input_line) fprintf( stderr, "%d:", input_line );
213 fprintf( stderr, " error: " );
215 else fprintf( stderr, "makedep: error: " );
216 vfprintf( stderr, msg, valist );
217 va_end( valist );
218 exit(1);
222 /*******************************************************************
223 * fatal_perror
225 static void fatal_perror( const char *msg, ... )
227 va_list valist;
228 va_start( valist, msg );
229 if (input_file_name)
231 fprintf( stderr, "%s:", input_file_name );
232 if (input_line) fprintf( stderr, "%d:", input_line );
233 fprintf( stderr, " error: " );
235 else fprintf( stderr, "makedep: error: " );
236 vfprintf( stderr, msg, valist );
237 perror( " " );
238 va_end( valist );
239 exit(1);
243 /*******************************************************************
244 * cleanup_files
246 static void cleanup_files(void)
248 if (temp_file_name) unlink( temp_file_name );
249 if (output_file_name) unlink( output_file_name );
253 /*******************************************************************
254 * exit_on_signal
256 static void exit_on_signal( int sig )
258 exit( 1 ); /* this will call the atexit functions */
262 /*******************************************************************
263 * xmalloc
265 static void *xmalloc( size_t size )
267 void *res;
268 if (!(res = malloc (size ? size : 1)))
269 fatal_error( "Virtual memory exhausted.\n" );
270 return res;
274 /*******************************************************************
275 * xrealloc
277 static void *xrealloc (void *ptr, size_t size)
279 void *res;
280 assert( size );
281 if (!(res = realloc( ptr, size )))
282 fatal_error( "Virtual memory exhausted.\n" );
283 return res;
286 /*******************************************************************
287 * xstrdup
289 static char *xstrdup( const char *str )
291 char *res = strdup( str );
292 if (!res) fatal_error( "Virtual memory exhausted.\n" );
293 return res;
297 /*******************************************************************
298 * strmake
300 static char *strmake( const char* fmt, ... )
302 int n;
303 size_t size = 100;
304 va_list ap;
306 for (;;)
308 char *p = xmalloc (size);
309 va_start(ap, fmt);
310 n = vsnprintf (p, size, fmt, ap);
311 va_end(ap);
312 if (n == -1) size *= 2;
313 else if ((size_t)n >= size) size = n + 1;
314 else return p;
315 free(p);
320 /*******************************************************************
321 * strendswith
323 static int strendswith( const char* str, const char* end )
325 int l = strlen(str);
326 int m = strlen(end);
328 return l >= m && strcmp(str + l - m, end) == 0;
332 /*******************************************************************
333 * output
335 static void output( const char *format, ... )
337 int ret;
338 va_list valist;
340 va_start( valist, format );
341 ret = vfprintf( output_file, format, valist );
342 va_end( valist );
343 if (ret < 0) fatal_perror( "output" );
344 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
345 else output_column += ret;
349 /*******************************************************************
350 * strarray_add
352 static void strarray_add( struct strarray *array, const char *str )
354 if (array->count == array->size)
356 if (array->size) array->size *= 2;
357 else array->size = 16;
358 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
360 array->str[array->count++] = str;
364 /*******************************************************************
365 * strarray_addall
367 static void strarray_addall( struct strarray *array, struct strarray added )
369 unsigned int i;
371 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
375 /*******************************************************************
376 * strarray_exists
378 static int strarray_exists( struct strarray *array, const char *str )
380 unsigned int i;
382 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
383 return 0;
387 /*******************************************************************
388 * strarray_add_uniq
390 static void strarray_add_uniq( struct strarray *array, const char *str )
392 if (!strarray_exists( array, str )) strarray_add( array, str );
396 /*******************************************************************
397 * strarray_get_value
399 * Find a value in a name/value pair string array.
401 static char *strarray_get_value( const struct strarray *array, const char *name )
403 unsigned int i;
405 for (i = 0; i < array->count; i += 2)
406 if (!strcmp( array->str[i], name )) return xstrdup( array->str[i + 1] );
407 return NULL;
411 /*******************************************************************
412 * strarray_set_value
414 * Define a value in a name/value pair string array.
416 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
418 unsigned int i;
420 /* redefining a variable replaces the previous value */
421 for (i = 0; i < array->count; i += 2)
423 if (strcmp( array->str[i], name )) continue;
424 array->str[i + 1] = value;
425 return;
427 strarray_add( array, name );
428 strarray_add( array, value );
432 /*******************************************************************
433 * output_filename
435 static void output_filename( const char *name )
437 if (output_column + strlen(name) + 1 > 100)
439 output( " \\\n" );
440 output( " " );
442 else if (output_column) output( " " );
443 output( "%s", name );
447 /*******************************************************************
448 * output_filenames
450 static void output_filenames( struct strarray array )
452 unsigned int i;
454 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
458 /*******************************************************************
459 * get_extension
461 static char *get_extension( char *filename )
463 char *ext = strrchr( filename, '.' );
464 if (ext && strchr( ext, '/' )) ext = NULL;
465 return ext;
469 /*******************************************************************
470 * replace_extension
472 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
474 char *ret;
475 int name_len = strlen( name );
476 int ext_len = strlen( old_ext );
478 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
479 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
480 memcpy( ret, name, name_len );
481 strcpy( ret + name_len, new_ext );
482 return ret;
486 /*******************************************************************
487 * strarray_replace_extension
489 static struct strarray strarray_replace_extension( const struct strarray *array,
490 const char *old_ext, const char *new_ext )
492 unsigned int i;
493 struct strarray ret;
495 ret.count = ret.size = array->count;
496 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
497 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
498 return ret;
502 /*******************************************************************
503 * replace_substr
505 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
507 unsigned int pos = start - str;
508 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
509 memcpy( ret, str, pos );
510 strcpy( ret + pos, replace );
511 strcat( ret + pos, start + len );
512 return ret;
516 /*******************************************************************
517 * get_relative_path
519 * Determine where the destination path is located relative to the 'from' path.
521 static char *get_relative_path( const char *from, const char *dest )
523 const char *start;
524 char *ret, *p;
525 unsigned int dotdots = 0;
527 /* a path of "." is equivalent to an empty path */
528 if (!strcmp( from, "." )) from = "";
530 for (;;)
532 while (*from == '/') from++;
533 while (*dest == '/') dest++;
534 start = dest; /* save start of next path element */
535 if (!*from) break;
537 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
538 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
540 /* count remaining elements in 'from' */
543 dotdots++;
544 while (*from && *from != '/') from++;
545 while (*from == '/') from++;
547 while (*from);
548 break;
551 if (!start[0] && !dotdots) return NULL; /* empty path */
553 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
554 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
556 if (start[0]) strcpy( p, start );
557 else p[-1] = 0; /* remove trailing slash */
558 return ret;
562 /*******************************************************************
563 * concat_paths
565 static char *concat_paths( const char *base, const char *path )
567 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
568 if (!path || !path[0]) return xstrdup( base );
569 if (path[0] == '/') return xstrdup( path );
570 return strmake( "%s/%s", base, path );
574 /*******************************************************************
575 * base_dir_path
577 static char *base_dir_path( struct makefile *make, const char *path )
579 return concat_paths( make->base_dir, path );
583 /*******************************************************************
584 * obj_dir_path
586 static char *obj_dir_path( struct makefile *make, const char *path )
588 return concat_paths( make->obj_dir, path );
592 /*******************************************************************
593 * src_dir_path
595 static char *src_dir_path( struct makefile *make, const char *path )
597 if (make->src_dir) return concat_paths( make->src_dir, path );
598 return obj_dir_path( make, path );
602 /*******************************************************************
603 * top_obj_dir_path
605 static char *top_obj_dir_path( struct makefile *make, const char *path )
607 return concat_paths( make->top_obj_dir, path );
611 /*******************************************************************
612 * top_dir_path
614 static char *top_dir_path( struct makefile *make, const char *path )
616 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
617 return top_obj_dir_path( make, path );
621 /*******************************************************************
622 * root_dir_path
624 static char *root_dir_path( const char *path )
626 return concat_paths( root_src_dir, path );
630 /*******************************************************************
631 * tools_dir_path
633 static char *tools_dir_path( struct makefile *make, const char *path )
635 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
636 return top_obj_dir_path( make, strmake( "tools/%s", path ));
640 /*******************************************************************
641 * tools_path
643 static char *tools_path( struct makefile *make, const char *name )
645 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
649 /*******************************************************************
650 * get_line
652 static char *get_line( FILE *file )
654 static char *buffer;
655 static unsigned int size;
657 if (!size)
659 size = 1024;
660 buffer = xmalloc( size );
662 if (!fgets( buffer, size, file )) return NULL;
663 input_line++;
665 for (;;)
667 char *p = buffer + strlen(buffer);
668 /* if line is larger than buffer, resize buffer */
669 while (p == buffer + size - 1 && p[-1] != '\n')
671 buffer = xrealloc( buffer, size * 2 );
672 if (!fgets( buffer + size - 1, size + 1, file )) break;
673 p = buffer + strlen(buffer);
674 size *= 2;
676 if (p > buffer && p[-1] == '\n')
678 *(--p) = 0;
679 if (p > buffer && p[-1] == '\r') *(--p) = 0;
680 if (p > buffer && p[-1] == '\\')
682 *(--p) = 0;
683 /* line ends in backslash, read continuation line */
684 if (!fgets( p, size - (p - buffer), file )) return buffer;
685 input_line++;
686 continue;
689 return buffer;
694 /*******************************************************************
695 * hash_filename
697 static unsigned int hash_filename( const char *name )
699 unsigned int ret = 0;
700 while (*name) ret = (ret << 7) + (ret << 3) + *name++;
701 return ret % HASH_SIZE;
705 /*******************************************************************
706 * add_file
708 static struct file *add_file( const char *name )
710 struct file *file = xmalloc( sizeof(*file) );
711 memset( file, 0, sizeof(*file) );
712 file->name = xstrdup( name );
713 list_add_tail( &files[hash_filename( name )], &file->entry );
714 return file;
718 /*******************************************************************
719 * add_dependency
721 static void add_dependency( struct file *file, const char *name, enum incl_type type )
723 /* enforce some rules for the Wine tree */
725 if (!memcmp( name, "../", 3 ))
726 fatal_error( "#include directive with relative path not allowed\n" );
728 if (!strcmp( name, "config.h" ))
730 if (strendswith( file->name, ".h" ))
731 fatal_error( "config.h must not be included by a header file\n" );
732 if (file->deps_count)
733 fatal_error( "config.h must be included before anything else\n" );
735 else if (!strcmp( name, "wine/port.h" ))
737 if (strendswith( file->name, ".h" ))
738 fatal_error( "wine/port.h must not be included by a header file\n" );
739 if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
740 if (file->deps_count > 1)
741 fatal_error( "wine/port.h must be included before everything except config.h\n" );
742 if (strcmp( file->deps[0].name, "config.h" ))
743 fatal_error( "config.h must be included before wine/port.h\n" );
746 if (file->deps_count >= file->deps_size)
748 file->deps_size *= 2;
749 if (file->deps_size < 16) file->deps_size = 16;
750 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
752 file->deps[file->deps_count].line = input_line;
753 file->deps[file->deps_count].type = type;
754 file->deps[file->deps_count].name = xstrdup( name );
755 file->deps_count++;
759 /*******************************************************************
760 * find_src_file
762 static struct incl_file *find_src_file( struct makefile *make, const char *name )
764 struct incl_file *file;
766 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
767 if (!strcmp( name, file->name )) return file;
768 return NULL;
771 /*******************************************************************
772 * find_include_file
774 static struct incl_file *find_include_file( struct makefile *make, const char *name )
776 struct incl_file *file;
778 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
779 if (!strcmp( name, file->name )) return file;
780 return NULL;
783 /*******************************************************************
784 * add_include
786 * Add an include file if it doesn't already exists.
788 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
789 const char *name, int line, int system )
791 struct incl_file *include;
793 if (parent->files_count >= parent->files_size)
795 parent->files_size *= 2;
796 if (parent->files_size < 16) parent->files_size = 16;
797 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
800 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
801 if (!strcmp( name, include->name )) goto found;
803 include = xmalloc( sizeof(*include) );
804 memset( include, 0, sizeof(*include) );
805 include->name = xstrdup(name);
806 include->included_by = parent;
807 include->included_line = line;
808 include->system = system;
809 list_add_tail( &make->includes, &include->entry );
810 found:
811 parent->files[parent->files_count++] = include;
812 return include;
816 /*******************************************************************
817 * add_generated_source
819 * Add a generated source file to the list.
821 static struct incl_file *add_generated_source( struct makefile *make,
822 const char *name, const char *filename )
824 struct incl_file *file;
826 if ((file = find_src_file( make, name ))) return file; /* we already have it */
827 file = xmalloc( sizeof(*file) );
828 memset( file, 0, sizeof(*file) );
829 file->file = add_file( name );
830 file->name = xstrdup( name );
831 file->filename = obj_dir_path( make, filename ? filename : name );
832 file->file->flags = FLAG_GENERATED;
833 list_add_tail( &make->sources, &file->entry );
834 return file;
838 /*******************************************************************
839 * parse_include_directive
841 static void parse_include_directive( struct file *source, char *str )
843 char quote, *include, *p = str;
845 while (*p && isspace(*p)) p++;
846 if (*p != '\"' && *p != '<' ) return;
847 quote = *p++;
848 if (quote == '<') quote = '>';
849 include = p;
850 while (*p && (*p != quote)) p++;
851 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
852 *p = 0;
853 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
857 /*******************************************************************
858 * parse_pragma_directive
860 static void parse_pragma_directive( struct file *source, char *str )
862 char *flag, *p = str;
864 if (!isspace( *p )) return;
865 while (*p && isspace(*p)) p++;
866 p = strtok( p, " \t" );
867 if (strcmp( p, "makedep" )) return;
869 while ((flag = strtok( NULL, " \t" )))
871 if (!strcmp( flag, "depend" ))
873 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
874 return;
876 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
878 if (strendswith( source->name, ".idl" ))
880 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
881 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
882 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
883 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
884 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
885 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
886 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
887 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
889 else if (strendswith( source->name, ".rc" ))
891 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
893 else if (strendswith( source->name, ".sfd" ))
895 if (!strcmp( flag, "font" ))
897 struct strarray *array = source->args;
899 if (!array)
901 source->args = array = xmalloc( sizeof(*array) );
902 *array = empty_strarray;
903 source->flags |= FLAG_SFD_FONTS;
905 strarray_add( array, xstrdup( strtok( NULL, "" )));
906 return;
909 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
914 /*******************************************************************
915 * parse_cpp_directive
917 static void parse_cpp_directive( struct file *source, char *str )
919 while (*str && isspace(*str)) str++;
920 if (*str++ != '#') return;
921 while (*str && isspace(*str)) str++;
923 if (!strncmp( str, "include", 7 ))
924 parse_include_directive( source, str + 7 );
925 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
926 parse_include_directive( source, str + 6 );
927 else if (!strncmp( str, "pragma", 6 ))
928 parse_pragma_directive( source, str + 6 );
932 /*******************************************************************
933 * parse_idl_file
935 static void parse_idl_file( struct file *source, FILE *file )
937 char *buffer, *include;
939 input_line = 0;
941 while ((buffer = get_line( file )))
943 char quote;
944 char *p = buffer;
945 while (*p && isspace(*p)) p++;
947 if (!strncmp( p, "import", 6 ))
949 p += 6;
950 while (*p && isspace(*p)) p++;
951 if (*p != '"') continue;
952 include = ++p;
953 while (*p && (*p != '"')) p++;
954 if (!*p) fatal_error( "malformed import directive\n" );
955 *p = 0;
956 add_dependency( source, include, INCL_IMPORT );
957 continue;
960 /* check for #include inside cpp_quote */
961 if (!strncmp( p, "cpp_quote", 9 ))
963 p += 9;
964 while (*p && isspace(*p)) p++;
965 if (*p++ != '(') continue;
966 while (*p && isspace(*p)) p++;
967 if (*p++ != '"') continue;
968 if (*p++ != '#') continue;
969 while (*p && isspace(*p)) p++;
970 if (strncmp( p, "include", 7 )) continue;
971 p += 7;
972 while (*p && isspace(*p)) p++;
973 if (*p == '\\' && p[1] == '"')
975 p += 2;
976 quote = '"';
978 else
980 if (*p++ != '<' ) continue;
981 quote = '>';
983 include = p;
984 while (*p && (*p != quote)) p++;
985 if (!*p || (quote == '"' && p[-1] != '\\'))
986 fatal_error( "malformed #include directive inside cpp_quote\n" );
987 if (quote == '"') p--; /* remove backslash */
988 *p = 0;
989 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
990 continue;
993 parse_cpp_directive( source, p );
997 /*******************************************************************
998 * parse_c_file
1000 static void parse_c_file( struct file *source, FILE *file )
1002 char *buffer;
1004 input_line = 0;
1005 while ((buffer = get_line( file )))
1007 parse_cpp_directive( source, buffer );
1012 /*******************************************************************
1013 * parse_rc_file
1015 static void parse_rc_file( struct file *source, FILE *file )
1017 char *buffer, *include;
1019 input_line = 0;
1020 while ((buffer = get_line( file )))
1022 char quote;
1023 char *p = buffer;
1024 while (*p && isspace(*p)) p++;
1026 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1028 p += 2;
1029 while (*p && isspace(*p)) p++;
1030 if (strncmp( p, "@makedep:", 9 )) continue;
1031 p += 9;
1032 while (*p && isspace(*p)) p++;
1033 quote = '"';
1034 if (*p == quote)
1036 include = ++p;
1037 while (*p && *p != quote) p++;
1039 else
1041 include = p;
1042 while (*p && !isspace(*p) && *p != '*') p++;
1044 if (!*p)
1045 fatal_error( "malformed makedep comment\n" );
1046 *p = 0;
1047 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1048 continue;
1051 parse_cpp_directive( source, buffer );
1056 /*******************************************************************
1057 * parse_in_file
1059 static void parse_in_file( struct file *source, FILE *file )
1061 char *p, *buffer;
1063 /* make sure it gets rebuilt when the version changes */
1064 add_dependency( source, "config.h", INCL_SYSTEM );
1066 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1068 input_line = 0;
1069 while ((buffer = get_line( file )))
1071 if (strncmp( buffer, ".TH", 3 )) continue;
1072 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1073 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1074 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1075 source->args = xstrdup( p );
1076 return;
1081 /*******************************************************************
1082 * parse_sfd_file
1084 static void parse_sfd_file( struct file *source, FILE *file )
1086 char *p, *eol, *buffer;
1088 input_line = 0;
1089 while ((buffer = get_line( file )))
1091 if (strncmp( buffer, "UComments:", 10 )) continue;
1092 p = buffer + 10;
1093 while (*p == ' ') p++;
1094 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1096 p++;
1097 buffer[strlen(buffer) - 1] = 0;
1099 while ((eol = strstr( p, "+AAoA" )))
1101 *eol = 0;
1102 while (*p && isspace(*p)) p++;
1103 if (*p++ == '#')
1105 while (*p && isspace(*p)) p++;
1106 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1108 p = eol + 5;
1110 while (*p && isspace(*p)) p++;
1111 if (*p++ != '#') return;
1112 while (*p && isspace(*p)) p++;
1113 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1114 return;
1119 /*******************************************************************
1120 * load_file
1122 static struct file *load_file( const char *name )
1124 struct file *file;
1125 FILE *f;
1126 unsigned int hash = hash_filename( name );
1128 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1129 if (!strcmp( name, file->name )) return file;
1131 if (!(f = fopen( name, "r" ))) return NULL;
1133 file = add_file( name );
1134 input_file_name = file->name;
1135 input_line = 0;
1137 if (strendswith( name, ".idl" )) parse_idl_file( file, f );
1138 else if (strendswith( name, ".rc" )) parse_rc_file( file, f );
1139 else if (strendswith( name, ".in" )) parse_in_file( file, f );
1140 else if (strendswith( name, ".sfd" )) parse_sfd_file( file, f );
1141 else if (strendswith( name, ".c" ) ||
1142 strendswith( name, ".m" ) ||
1143 strendswith( name, ".h" ) ||
1144 strendswith( name, ".l" ) ||
1145 strendswith( name, ".y" )) parse_c_file( file, f );
1147 fclose( f );
1148 input_file_name = NULL;
1150 return file;
1154 /*******************************************************************
1155 * open_file
1157 static struct file *open_file( struct makefile *make, const char *path, char **filename )
1159 struct file *ret = load_file( base_dir_path( make, path ));
1161 if (ret) *filename = xstrdup( path );
1162 return ret;
1166 /*******************************************************************
1167 * open_local_file
1169 * Open a file in the source directory of the makefile.
1171 static struct file *open_local_file( struct makefile *make, const char *path, char **filename )
1173 char *src_path = root_dir_path( base_dir_path( make, path ));
1174 struct file *ret = load_file( src_path );
1176 /* if not found, try parent dir */
1177 if (!ret && make->parent_dir)
1179 free( src_path );
1180 path = strmake( "%s/%s", make->parent_dir, path );
1181 src_path = root_dir_path( base_dir_path( make, path ));
1182 ret = load_file( src_path );
1185 if (ret) *filename = src_dir_path( make, path );
1186 free( src_path );
1187 return ret;
1191 /*******************************************************************
1192 * open_global_file
1194 * Open a file in the top-level source directory.
1196 static struct file *open_global_file( struct makefile *make, const char *path, char **filename )
1198 char *src_path = root_dir_path( path );
1199 struct file *ret = load_file( src_path );
1201 if (ret) *filename = top_dir_path( make, path );
1202 free( src_path );
1203 return ret;
1207 /*******************************************************************
1208 * open_global_header
1210 * Open a file in the global include source directory.
1212 static struct file *open_global_header( struct makefile *make, const char *path, char **filename )
1214 return open_global_file( make, strmake( "include/%s", path ), filename );
1218 /*******************************************************************
1219 * open_src_file
1221 static struct file *open_src_file( struct makefile *make, struct incl_file *pFile )
1223 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1225 if (!file) fatal_perror( "open %s", pFile->name );
1226 return file;
1230 /*******************************************************************
1231 * open_include_file
1233 static struct file *open_include_file( struct makefile *make, struct incl_file *pFile )
1235 struct file *file = NULL;
1236 char *filename, *p;
1237 unsigned int i, len;
1239 errno = ENOENT;
1241 /* check for generated bison header */
1243 if (strendswith( pFile->name, ".tab.h" ) &&
1244 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
1246 pFile->sourcename = filename;
1247 pFile->filename = obj_dir_path( make, pFile->name );
1248 return file;
1251 /* check for corresponding idl file in source dir */
1253 if (strendswith( pFile->name, ".h" ) &&
1254 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1256 pFile->sourcename = filename;
1257 pFile->filename = obj_dir_path( make, pFile->name );
1258 return file;
1261 /* now try in source dir */
1262 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1264 /* check for corresponding idl file in global includes */
1266 if (strendswith( pFile->name, ".h" ) &&
1267 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
1269 pFile->sourcename = filename;
1270 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1271 return file;
1274 /* check for corresponding .in file in global includes (for config.h.in) */
1276 if (strendswith( pFile->name, ".h" ) &&
1277 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
1279 pFile->sourcename = filename;
1280 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1281 return file;
1284 /* check for corresponding .x file in global includes */
1286 if (strendswith( pFile->name, "tmpl.h" ) &&
1287 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
1289 pFile->sourcename = filename;
1290 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
1291 return file;
1294 /* check in global includes source dir */
1296 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1298 /* check in global msvcrt includes */
1299 if (make->use_msvcrt &&
1300 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1301 return file;
1303 /* now search in include paths */
1304 for (i = 0; i < make->include_args.count; i++)
1306 const char *dir = make->include_args.str[i] + 2; /* skip -I */
1307 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
1309 if (prefix)
1311 len = strlen( prefix );
1312 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
1314 while (dir[len] == '/') len++;
1315 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1316 if (file) return file;
1318 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
1320 if (*dir != '/')
1322 if ((file = open_file( make, concat_paths( dir, pFile->name ), &pFile->filename )))
1323 return file;
1326 if (pFile->system) return NULL; /* ignore system files we cannot find */
1328 /* try in src file directory */
1329 if ((p = strrchr(pFile->included_by->filename, '/')))
1331 int l = p - pFile->included_by->filename + 1;
1332 filename = xmalloc(l + strlen(pFile->name) + 1);
1333 memcpy( filename, pFile->included_by->filename, l );
1334 strcpy( filename + l, pFile->name );
1335 if ((file = open_file( make, filename, &pFile->filename ))) return file;
1336 free( filename );
1339 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1340 perror( pFile->name );
1341 pFile = pFile->included_by;
1342 while (pFile && pFile->included_by)
1344 const char *parent = pFile->included_by->sourcename;
1345 if (!parent) parent = pFile->included_by->file->name;
1346 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1347 parent, pFile->included_line, pFile->name );
1348 pFile = pFile->included_by;
1350 exit(1);
1354 /*******************************************************************
1355 * add_all_includes
1357 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1359 unsigned int i;
1361 parent->files_count = 0;
1362 parent->files_size = file->deps_count;
1363 parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
1364 for (i = 0; i < file->deps_count; i++)
1366 switch (file->deps[i].type)
1368 case INCL_NORMAL:
1369 case INCL_IMPORT:
1370 add_include( make, parent, file->deps[i].name, file->deps[i].line, 0 );
1371 break;
1372 case INCL_SYSTEM:
1373 add_include( make, parent, file->deps[i].name, file->deps[i].line, 1 );
1374 break;
1375 case INCL_CPP_QUOTE:
1376 case INCL_CPP_QUOTE_SYSTEM:
1377 break;
1383 /*******************************************************************
1384 * parse_file
1386 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1388 struct file *file;
1390 /* don't try to open certain types of files */
1391 if (strendswith( source->name, ".tlb" ))
1393 source->filename = obj_dir_path( make, source->name );
1394 return;
1397 file = src ? open_src_file( make, source ) : open_include_file( make, source );
1398 if (!file) return;
1400 source->file = file;
1401 source->files_count = 0;
1402 source->files_size = file->deps_count;
1403 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1405 if (source->sourcename)
1407 if (strendswith( source->sourcename, ".idl" ))
1409 unsigned int i;
1411 /* generated .h file always includes these */
1412 add_include( make, source, "rpc.h", 0, 1 );
1413 add_include( make, source, "rpcndr.h", 0, 1 );
1414 for (i = 0; i < file->deps_count; i++)
1416 switch (file->deps[i].type)
1418 case INCL_IMPORT:
1419 if (strendswith( file->deps[i].name, ".idl" ))
1420 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1421 file->deps[i].line, 0 );
1422 else
1423 add_include( make, source, file->deps[i].name, file->deps[i].line, 0 );
1424 break;
1425 case INCL_CPP_QUOTE:
1426 add_include( make, source, file->deps[i].name, file->deps[i].line, 0 );
1427 break;
1428 case INCL_CPP_QUOTE_SYSTEM:
1429 add_include( make, source, file->deps[i].name, file->deps[i].line, 1 );
1430 break;
1431 case INCL_NORMAL:
1432 case INCL_SYSTEM:
1433 break;
1436 return;
1438 if (strendswith( source->sourcename, ".y" ))
1439 return; /* generated .tab.h doesn't include anything */
1442 add_all_includes( make, source, file );
1446 /*******************************************************************
1447 * add_src_file
1449 * Add a source file to the list.
1451 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1453 struct incl_file *file;
1455 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1456 file = xmalloc( sizeof(*file) );
1457 memset( file, 0, sizeof(*file) );
1458 file->name = xstrdup(name);
1459 list_add_tail( &make->sources, &file->entry );
1460 parse_file( make, file, 1 );
1461 return file;
1465 /*******************************************************************
1466 * open_input_makefile
1468 static FILE *open_input_makefile( struct makefile *make )
1470 FILE *ret;
1472 if (make->base_dir)
1473 input_file_name = base_dir_path( make, input_makefile_name );
1474 else
1475 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1477 input_line = 0;
1478 if (!(ret = fopen( input_file_name, "r" )))
1480 input_file_name = root_dir_path( input_file_name );
1481 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1483 return ret;
1487 /*******************************************************************
1488 * get_make_variable
1490 static char *get_make_variable( struct makefile *make, const char *name )
1492 char *ret;
1494 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1495 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1496 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1497 return NULL;
1501 /*******************************************************************
1502 * get_expanded_make_variable
1504 static char *get_expanded_make_variable( struct makefile *make, const char *name )
1506 char *p, *end, *var, *expand, *tmp;
1508 expand = get_make_variable( make, name );
1509 if (!expand) return NULL;
1511 p = expand;
1512 while ((p = strchr( p, '$' )))
1514 if (p[1] == '(')
1516 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1517 *end++ = 0;
1518 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1519 var = get_make_variable( make, p + 2 );
1520 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1521 free( var );
1522 /* switch to the new string */
1523 p = tmp + (p - expand);
1524 free( expand );
1525 expand = tmp;
1527 else if (p[1] == '{') /* don't expand ${} variables */
1529 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1530 p = end + 1;
1532 else if (p[1] == '$')
1534 p += 2;
1536 else fatal_error( "syntax error in '%s'\n", expand );
1539 /* consider empty variables undefined */
1540 p = expand;
1541 while (*p && isspace(*p)) p++;
1542 if (*p) return expand;
1543 free( expand );
1544 return NULL;
1548 /*******************************************************************
1549 * get_expanded_make_var_array
1551 static struct strarray get_expanded_make_var_array( struct makefile *make, const char *name )
1553 struct strarray ret = empty_strarray;
1554 char *value, *token;
1556 if ((value = get_expanded_make_variable( make, name )))
1557 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1558 strarray_add( &ret, token );
1559 return ret;
1563 /*******************************************************************
1564 * file_local_var
1566 static char *file_local_var( const char *file, const char *name )
1568 char *p, *var;
1570 var = strmake( "%s_%s", file, name );
1571 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1572 return var;
1576 /*******************************************************************
1577 * set_make_variable
1579 static int set_make_variable( struct strarray *array, const char *assignment )
1581 char *p, *name;
1583 p = name = xstrdup( assignment );
1584 while (isalnum(*p) || *p == '_') p++;
1585 if (name == p) return 0; /* not a variable */
1586 if (isspace(*p))
1588 *p++ = 0;
1589 while (isspace(*p)) p++;
1591 if (*p != '=') return 0; /* not an assignment */
1592 *p++ = 0;
1593 while (isspace(*p)) p++;
1595 strarray_set_value( array, name, p );
1596 return 1;
1600 /*******************************************************************
1601 * parse_makefile
1603 static struct makefile *parse_makefile( const char *path, const char *separator )
1605 char *buffer;
1606 FILE *file;
1607 struct makefile *make = xmalloc( sizeof(*make) );
1609 memset( make, 0, sizeof(*make) );
1610 if (path)
1612 make->top_obj_dir = get_relative_path( path, "" );
1613 make->base_dir = path;
1614 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1617 file = open_input_makefile( make );
1618 while ((buffer = get_line( file )))
1620 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1621 if (*buffer == '\t') continue; /* command */
1622 while (isspace( *buffer )) buffer++;
1623 if (*buffer == '#') continue; /* comment */
1624 set_make_variable( &make->vars, buffer );
1626 fclose( file );
1627 input_file_name = NULL;
1628 return make;
1632 /*******************************************************************
1633 * add_generated_sources
1635 static void add_generated_sources( struct makefile *make )
1637 struct incl_file *source, *next, *file;
1639 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1641 if (source->file->flags & FLAG_IDL_CLIENT)
1643 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1644 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1645 add_all_includes( make, file, file->file );
1647 if (source->file->flags & FLAG_IDL_SERVER)
1649 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1650 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1651 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1652 add_all_includes( make, file, file->file );
1654 if (source->file->flags & FLAG_IDL_IDENT)
1656 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1657 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1658 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1659 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1660 add_all_includes( make, file, file->file );
1662 if (source->file->flags & FLAG_IDL_PROXY)
1664 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1665 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1666 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1667 add_all_includes( make, file, file->file );
1668 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1669 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1670 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1671 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1672 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1673 add_all_includes( make, file, file->file );
1675 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1677 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1679 if (source->file->flags & FLAG_IDL_REGISTER)
1681 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1683 if (strendswith( source->name, ".y" ))
1685 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1686 /* steal the includes list from the source file */
1687 file->files_count = source->files_count;
1688 file->files_size = source->files_size;
1689 file->files = source->files;
1690 source->files_count = source->files_size = 0;
1691 source->files = NULL;
1693 if (strendswith( source->name, ".l" ))
1695 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1696 /* steal the includes list from the source file */
1697 file->files_count = source->files_count;
1698 file->files_size = source->files_size;
1699 file->files = source->files;
1700 source->files_count = source->files_size = 0;
1701 source->files = NULL;
1704 if (get_make_variable( make, "TESTDLL" ))
1706 file = add_generated_source( make, "testlist.o", "testlist.c" );
1707 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1708 add_all_includes( make, file, file->file );
1713 /*******************************************************************
1714 * create_dir
1716 static void create_dir( const char *dir )
1718 char *p, *path;
1720 p = path = xstrdup( dir );
1721 while ((p = strchr( p, '/' )))
1723 *p = 0;
1724 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1725 *p++ = '/';
1726 while (*p == '/') p++;
1728 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1729 free( path );
1733 /*******************************************************************
1734 * output_filenames_obj_dir
1736 static void output_filenames_obj_dir( struct makefile *make, struct strarray array )
1738 unsigned int i;
1740 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1744 /*******************************************************************
1745 * output_include
1747 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1749 int i;
1751 if (pFile->owner == owner) return;
1752 if (!pFile->filename) return;
1753 pFile->owner = owner;
1754 output_filename( pFile->filename );
1755 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1759 /*******************************************************************
1760 * output_sources
1762 static struct strarray output_sources( struct makefile *make, struct strarray *testlist_files )
1764 struct incl_file *source;
1765 unsigned int i;
1766 struct strarray object_files = empty_strarray;
1767 struct strarray crossobj_files = empty_strarray;
1768 struct strarray res_files = empty_strarray;
1769 struct strarray clean_files = empty_strarray;
1770 struct strarray po_files = empty_strarray;
1771 struct strarray mo_files = empty_strarray;
1772 struct strarray mc_files = empty_strarray;
1773 struct strarray ok_files = empty_strarray;
1774 struct strarray dlldata_files = empty_strarray;
1775 struct strarray c2man_files = empty_strarray;
1776 struct strarray implib_objs = empty_strarray;
1777 struct strarray includes = empty_strarray;
1778 struct strarray subdirs = empty_strarray;
1779 struct strarray phony_targets = empty_strarray;
1780 struct strarray all_targets = empty_strarray;
1782 for (i = 0; i < linguas.count; i++)
1783 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
1785 strarray_add( &phony_targets, "all" );
1786 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
1787 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
1788 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
1789 if (make->top_obj_dir) strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
1790 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
1791 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
1792 strarray_addall( &includes, make->include_args );
1794 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
1796 struct strarray extradefs;
1797 char *obj = xstrdup( source->name );
1798 char *ext = get_extension( obj );
1800 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1801 *ext++ = 0;
1803 if (make->src_dir && strchr( obj, '/' ))
1805 char *subdir = base_dir_path( make, obj );
1806 *strrchr( subdir, '/' ) = 0;
1807 strarray_add_uniq( &subdirs, subdir );
1810 extradefs = get_expanded_make_var_array( make, file_local_var( obj, "EXTRADEFS" ));
1812 if (!strcmp( ext, "y" )) /* yacc file */
1814 /* add source file dependency for parallel makes */
1815 char *header = strmake( "%s.tab.h", obj );
1817 if (find_include_file( make, header ))
1819 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
1820 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1821 obj, obj_dir_path( make, obj ), source->filename );
1822 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
1823 source->filename, obj_dir_path( make, header ));
1824 strarray_add( &clean_files, header );
1826 else output( "%s.tab.c: %s\n", obj, source->filename );
1828 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1829 continue; /* no dependencies */
1831 else if (!strcmp( ext, "x" )) /* template file */
1833 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
1834 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1835 output( "\t%s%s -H -o $@ %s\n",
1836 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1837 strarray_add( &clean_files, strmake( "%s.h", obj ));
1838 continue; /* no dependencies */
1840 else if (!strcmp( ext, "l" )) /* lex file */
1842 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
1843 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1844 continue; /* no dependencies */
1846 else if (!strcmp( ext, "rc" )) /* resource file */
1848 strarray_add( &res_files, strmake( "%s.res", obj ));
1849 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1850 tools_path( make, "wrc" ), source->filename );
1851 output( "\t%s -o $@", tools_path( make, "wrc" ) );
1852 if (make->is_win16) output_filename( "-m16" );
1853 else output_filenames( target_flags );
1854 output_filename( "--nostdinc" );
1855 output_filenames( includes );
1856 output_filenames( make->define_args );
1857 output_filenames( extradefs );
1858 if (mo_files.count && (source->file->flags & FLAG_RC_PO))
1860 strarray_add( &po_files, source->filename );
1861 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1862 output_filename( source->filename );
1863 output( "\n" );
1864 output( "%s.res:", obj_dir_path( make, obj ));
1865 output_filenames( mo_files );
1866 output( "\n" );
1867 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
1869 else
1871 output_filename( source->filename );
1872 output( "\n" );
1874 output( "%s.res:", obj_dir_path( make, obj ));
1876 else if (!strcmp( ext, "mc" )) /* message file */
1878 strarray_add( &res_files, strmake( "%s.res", obj ));
1879 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1880 tools_path( make, "wmc" ), source->filename );
1881 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
1882 if (mo_files.count)
1884 strarray_add( &mc_files, source->filename );
1885 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1886 output( "\n" );
1887 output( "%s.res:", obj_dir_path( make, obj ));
1888 output_filenames( mo_files );
1889 output( "\n" );
1890 output( "%s ", obj_dir_path( make, "msg.pot" ));
1892 else output( "\n" );
1893 output( "%s.res:", obj_dir_path( make, obj ));
1895 else if (!strcmp( ext, "idl" )) /* IDL file */
1897 struct strarray targets = empty_strarray;
1898 char *dest;
1900 if (!source->file->flags || find_include_file( make, strmake( "%s.h", obj )))
1901 source->file->flags |= FLAG_IDL_HEADER;
1903 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1905 if (!(source->file->flags & idl_outputs[i].flag)) continue;
1906 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1907 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
1908 strarray_add( &targets, dest );
1910 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1911 output_filenames_obj_dir( make, targets );
1912 output( ": %s\n", tools_path( make, "widl" ));
1913 output( "\t%s -o $@", tools_path( make, "widl" ) );
1914 output_filenames( target_flags );
1915 output_filenames( includes );
1916 output_filenames( make->define_args );
1917 output_filenames( extradefs );
1918 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
1919 output_filename( source->filename );
1920 output( "\n" );
1921 output_filenames_obj_dir( make, targets );
1922 output( ": %s", source->filename );
1924 else if (!strcmp( ext, "in" )) /* .in file or man page */
1926 if (strendswith( obj, ".man" ) && source->file->args)
1928 char *dir, *dest = replace_extension( obj, ".man", "" );
1929 char *lang = strchr( dest, '.' );
1930 char *section = source->file->args;
1931 if (lang)
1933 *lang++ = 0;
1934 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1936 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1937 output( "install-man-pages:: %s\n", obj_dir_path( make, obj ));
1938 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj_dir_path( make, obj ), dir, dest, section );
1939 output( "uninstall::\n" );
1940 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1941 free( dest );
1942 free( dir );
1943 strarray_add( &all_targets, xstrdup(obj) );
1944 strarray_add_uniq( &phony_targets, "install-man-pages" );
1945 strarray_add_uniq( &phony_targets, "uninstall" );
1947 else strarray_add( &clean_files, xstrdup(obj) );
1948 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
1949 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1950 output( "%s:", obj_dir_path( make, obj ));
1952 else if (!strcmp( ext, "sfd" )) /* font file */
1954 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
1955 if (fontforge && !make->src_dir)
1957 output( "%s: %s\n", ttf_file, source->filename );
1958 output( "\t%s -script %s %s $@\n",
1959 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
1960 if (!(source->file->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1962 if (source->file->flags & FLAG_INSTALL)
1964 output( "install install-lib::\n" );
1965 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1966 output( "uninstall::\n" );
1967 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1969 if (source->file->flags & FLAG_SFD_FONTS)
1971 struct strarray *array = source->file->args;
1973 for (i = 0; i < array->count; i++)
1975 char *font = strtok( xstrdup(array->str[i]), " \t" );
1976 char *args = strtok( NULL, "" );
1978 strarray_add( &all_targets, xstrdup( font ));
1979 output( "%s: %s %s\n", obj_dir_path( make, font ),
1980 tools_path( make, "sfnt2fon" ), ttf_file );
1981 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
1982 output( "install install-lib:: %s\n", font );
1983 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n",
1984 obj_dir_path( make, font ), font );
1985 output( "uninstall::\n" );
1986 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1989 if (source->file->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1991 strarray_add_uniq( &phony_targets, "install" );
1992 strarray_add_uniq( &phony_targets, "install-lib" );
1993 strarray_add_uniq( &phony_targets, "uninstall" );
1995 continue; /* no dependencies */
1997 else if (!strcmp( ext, "svg" )) /* svg file */
1999 if (convert && rsvg && icotool && !make->src_dir)
2001 output( "%s.ico %s.bmp: %s\n",
2002 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
2003 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
2004 top_dir_path( make, "tools/buildimage" ), source->filename );
2006 continue; /* no dependencies */
2008 else if (!strcmp( ext, "res" ))
2010 strarray_add( &res_files, source->name );
2011 continue; /* no dependencies */
2013 else
2015 int need_cross = make->testdll ||
2016 (source->file->flags & FLAG_C_IMPLIB) ||
2017 (make->module && make->staticlib);
2019 if ((source->file->flags & FLAG_GENERATED) &&
2020 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
2021 strarray_add( &clean_files, source->filename );
2022 if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
2023 strarray_add( &object_files, strmake( "%s.o", obj ));
2024 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
2025 output( "\t$(CC) -c -o $@ %s", source->filename );
2026 output_filenames( includes );
2027 output_filenames( make->define_args );
2028 output_filenames( extradefs );
2029 if (make->module || make->staticlib || make->testdll)
2031 output_filenames( dll_flags );
2032 if (make->use_msvcrt) output_filenames( msvcrt_flags );
2034 output_filenames( extra_cflags );
2035 output_filenames( cpp_flags );
2036 output_filename( "$(CFLAGS)" );
2037 output( "\n" );
2038 if (crosstarget && need_cross)
2040 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
2041 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
2042 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
2043 output_filenames( includes );
2044 output_filenames( make->define_args );
2045 output_filenames( extradefs );
2046 output_filename( "-DWINE_CROSSTEST" );
2047 output_filenames( cpp_flags );
2048 output_filename( "$(CFLAGS)" );
2049 output( "\n" );
2051 if (make->testdll && !strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2053 strarray_add( &ok_files, strmake( "%s.ok", obj ));
2054 output( "%s.ok:\n", obj_dir_path( make, obj ));
2055 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
2056 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
2057 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
2059 if (!strcmp( ext, "c" ) && !(source->file->flags & FLAG_GENERATED))
2060 strarray_add( &c2man_files, source->filename );
2061 output( "%s.o", obj_dir_path( make, obj ));
2062 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
2063 output( ":" );
2065 free( obj );
2067 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
2068 output( "\n" );
2071 /* rules for files that depend on multiple sources */
2073 if (po_files.count)
2075 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
2076 output_filenames( po_files );
2077 output( "\n" );
2078 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
2079 if (make->is_win16) output_filename( "-m16" );
2080 else output_filenames( target_flags );
2081 output_filename( "--nostdinc" );
2082 output_filenames( includes );
2083 output_filenames( make->define_args );
2084 output_filenames( po_files );
2085 output( "\n" );
2086 strarray_add( &clean_files, "rsrc.pot" );
2089 if (mc_files.count)
2091 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
2092 output_filenames( mc_files );
2093 output( "\n" );
2094 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
2095 output_filenames( mc_files );
2096 output( "\n" );
2097 strarray_add( &clean_files, "msg.pot" );
2100 if (dlldata_files.count)
2102 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
2103 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
2104 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
2105 output_filenames( dlldata_files );
2106 output( "\n" );
2109 if (make->module && !make->staticlib)
2111 struct strarray all_libs = empty_strarray;
2112 char *module_path = obj_dir_path( make, make->module );
2113 char *spec_file = NULL;
2115 if (!make->appmode.count)
2116 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
2117 for (i = 0; i < make->delayimports.count; i++)
2118 strarray_add( &all_libs, strmake( "-l%s", make->delayimports.str[i] ));
2119 for (i = 0; i < make->imports.count; i++)
2120 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2121 for (i = 0; i < make->delayimports.count; i++)
2122 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
2123 strarray_add( &all_libs, "-lwine" );
2124 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2125 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2126 strarray_addall( &all_libs, libs );
2128 if (*dll_ext)
2130 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
2131 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
2132 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
2134 else
2136 strarray_add( &all_targets, make->module );
2137 output( "%s:", module_path );
2139 if (spec_file) output_filename( spec_file );
2140 output_filenames_obj_dir( make, object_files );
2141 output_filenames_obj_dir( make, res_files );
2142 output( "\n" );
2143 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2144 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2145 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2146 output_filenames( target_flags );
2147 output_filenames( unwind_flags );
2148 if (spec_file)
2150 output( " -shared %s", spec_file );
2151 output_filenames( make->extradllflags );
2153 else output_filenames( make->appmode );
2154 output_filenames_obj_dir( make, object_files );
2155 output_filenames_obj_dir( make, res_files );
2156 output_filenames( all_libs );
2157 output_filename( "$(LDFLAGS)" );
2158 output( "\n" );
2160 if (spec_file && make->importlib)
2162 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
2163 if (*dll_ext)
2165 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
2166 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
2167 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2168 output_filenames( target_flags );
2169 if (make->is_win16) output_filename( "-m16" );
2170 output( "\n" );
2171 if (implib_objs.count)
2173 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
2174 output( "%s.def.a:", importlib_path );
2175 output_filenames_obj_dir( make, implib_objs );
2176 output( "\n" );
2177 output( "\t$(RM) $@\n" );
2178 output( "\t$(AR) $(ARFLAGS) $@" );
2179 output_filenames_obj_dir( make, implib_objs );
2180 output( "\n" );
2181 output( "\t$(RANLIB) $@\n" );
2184 else
2186 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
2187 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2188 output_filenames_obj_dir( make, implib_objs );
2189 output( "\n" );
2190 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
2191 output_filenames( target_flags );
2192 output_filenames_obj_dir( make, implib_objs );
2193 output( "\n" );
2195 if (crosstarget && !make->is_win16)
2197 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2198 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2199 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2200 output_filenames_obj_dir( make, cross_files );
2201 output( "\n" );
2202 output( "\t%s -b %s -w --implib -o $@ --export %s",
2203 tools_path( make, "winebuild" ), crosstarget, spec_file );
2204 output_filenames_obj_dir( make, cross_files );
2205 output( "\n" );
2209 if (spec_file)
2211 if (c2man_files.count)
2213 output( "manpages::\n" );
2214 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2215 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2216 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2217 output_filename( strmake( "-o %s/man%s",
2218 top_obj_dir_path( make, "documentation" ), man_ext ));
2219 output_filenames( c2man_files );
2220 output( "\n" );
2221 output( "htmlpages::\n" );
2222 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2223 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2224 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2225 output_filename( strmake( "-o %s",
2226 top_obj_dir_path( make, "documentation/html" )));
2227 output_filenames( c2man_files );
2228 output( "\n" );
2229 output( "sgmlpages::\n" );
2230 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2231 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2232 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2233 output_filename( strmake( "-o %s",
2234 top_obj_dir_path( make, "documentation/api-guide" )));
2235 output_filenames( c2man_files );
2236 output( "\n" );
2237 output( "xmlpages::\n" );
2238 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2239 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2240 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2241 output_filename( strmake( "-o %s",
2242 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2243 output_filenames( c2man_files );
2244 output( "\n" );
2245 strarray_add( &phony_targets, "manpages" );
2246 strarray_add( &phony_targets, "htmlpages" );
2247 strarray_add( &phony_targets, "sgmlpages" );
2248 strarray_add( &phony_targets, "xmlpages" );
2250 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2254 if (make->staticlib)
2256 strarray_add( &all_targets, make->staticlib );
2257 output( "%s:", obj_dir_path( make, make->staticlib ));
2258 output_filenames_obj_dir( make, object_files );
2259 output( "\n\t$(RM) $@\n" );
2260 output( "\t$(AR) $(ARFLAGS) $@" );
2261 output_filenames_obj_dir( make, object_files );
2262 output( "\n\t$(RANLIB) $@\n" );
2263 if (crosstarget && make->module)
2265 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2267 strarray_add( &all_targets, name );
2268 output( "%s:", obj_dir_path( make, name ));
2269 output_filenames_obj_dir( make, crossobj_files );
2270 output( "\n\t$(RM) $@\n" );
2271 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2272 output_filenames_obj_dir( make, crossobj_files );
2273 output( "\n\t%s-ranlib $@\n", crosstarget );
2277 if (make->testdll)
2279 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2280 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2281 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2282 struct strarray all_libs = empty_strarray;
2284 for (i = 0; i < make->imports.count; i++)
2285 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2286 strarray_addall( &all_libs, libs );
2288 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2289 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2290 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2291 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2292 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2293 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2294 output_filenames( target_flags );
2295 output_filenames( unwind_flags );
2296 output_filenames( make->appmode );
2297 output_filenames_obj_dir( make, object_files );
2298 output_filenames_obj_dir( make, res_files );
2299 output_filenames( all_libs );
2300 output_filename( "$(LDFLAGS)" );
2301 output( "\n" );
2302 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2303 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2304 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2305 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2306 output_filenames( target_flags );
2307 output_filenames( unwind_flags );
2308 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2309 output_filenames( make->appmode );
2310 output_filenames_obj_dir( make, object_files );
2311 output_filenames_obj_dir( make, res_files );
2312 output_filenames( all_libs );
2313 output_filename( "$(LDFLAGS)" );
2314 output( "\n" );
2315 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2316 obj_dir_path( make, stripped ), dll_ext );
2317 output_filenames_obj_dir( make, object_files );
2318 output_filenames_obj_dir( make, res_files );
2319 output( "\n" );
2321 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2322 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2323 obj_dir_path( make, stripped ), dll_ext );
2324 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2325 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2327 if (crosstarget)
2329 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2331 strarray_add( &clean_files, crosstest );
2332 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2333 output( "%s:", obj_dir_path( make, crosstest ));
2334 output_filenames_obj_dir( make, crossobj_files );
2335 output_filenames_obj_dir( make, res_files );
2336 output( "\n" );
2337 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2338 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2339 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2340 output_filename( "--lib-suffix=.cross.a" );
2341 output_filenames_obj_dir( make, crossobj_files );
2342 output_filenames_obj_dir( make, res_files );
2343 output_filenames( all_libs );
2344 output_filename( "$(LDFLAGS)" );
2345 output( "\n" );
2346 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2347 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2350 output_filenames_obj_dir( make, ok_files );
2351 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2352 output( "check test:" );
2353 output_filenames_obj_dir( make, ok_files );
2354 output( "\n" );
2355 output( "testclean::\n" );
2356 output( "\t$(RM)" );
2357 output_filenames_obj_dir( make, ok_files );
2358 output( "\n" );
2359 strarray_addall( &clean_files, ok_files );
2360 strarray_add( &phony_targets, "check" );
2361 strarray_add( &phony_targets, "test" );
2362 strarray_add( &phony_targets, "testclean" );
2363 *testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2366 for (i = 0; i < make->programs.count; i++)
2368 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
2369 struct strarray all_libs = empty_strarray;
2370 struct strarray objs = get_expanded_make_var_array( make,
2371 file_local_var( make->programs.str[i], "OBJS" ));
2373 if (!objs.count) objs = object_files;
2374 output( "%s:", obj_dir_path( make, program ) );
2375 output_filenames_obj_dir( make, objs );
2376 output( "\n" );
2377 output( "\t$(CC) -o $@" );
2378 output_filenames_obj_dir( make, objs );
2379 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
2380 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2381 strarray_addall( &all_libs, libs );
2382 strarray_addall( &all_libs, get_expanded_make_var_array( make,
2383 file_local_var( make->programs.str[i], "LDFLAGS" )));
2384 output_filenames( all_libs );
2385 output_filename( "$(LDFLAGS)" );
2386 output( "\n" );
2387 strarray_add( &all_targets, program );
2390 if (all_targets.count)
2392 output( "all:" );
2393 output_filenames_obj_dir( make, all_targets );
2394 output( "\n" );
2397 strarray_addall( &clean_files, object_files );
2398 strarray_addall( &clean_files, crossobj_files );
2399 strarray_addall( &clean_files, res_files );
2400 strarray_addall( &clean_files, all_targets );
2401 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2403 if (clean_files.count)
2405 output( "%s::\n", obj_dir_path( make, "clean" ));
2406 output( "\t$(RM)" );
2407 output_filenames_obj_dir( make, clean_files );
2408 output( "\n" );
2409 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2410 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2413 if (make->top_obj_dir)
2415 output( "depend:\n" );
2416 output( "\t@cd %s && $(MAKE) %s\n", make->top_obj_dir, base_dir_path( make, "depend" ));
2417 strarray_add( &phony_targets, "depend" );
2420 if (phony_targets.count)
2422 output( ".PHONY:" );
2423 output_filenames( phony_targets );
2424 output( "\n" );
2427 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2429 return clean_files;
2433 /*******************************************************************
2434 * create_temp_file
2436 static FILE *create_temp_file( const char *orig )
2438 char *name = xmalloc( strlen(orig) + 13 );
2439 unsigned int i, id = getpid();
2440 int fd;
2441 FILE *ret = NULL;
2443 for (i = 0; i < 100; i++)
2445 sprintf( name, "%s.tmp%08x", orig, id );
2446 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2448 ret = fdopen( fd, "w" );
2449 break;
2451 if (errno != EEXIST) break;
2452 id += 7777;
2454 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2455 temp_file_name = name;
2456 return ret;
2460 /*******************************************************************
2461 * rename_temp_file
2463 static void rename_temp_file( const char *dest )
2465 int ret = rename( temp_file_name, dest );
2466 if (ret == -1 && errno == EEXIST)
2468 /* rename doesn't overwrite on windows */
2469 unlink( dest );
2470 ret = rename( temp_file_name, dest );
2472 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2473 temp_file_name = NULL;
2477 /*******************************************************************
2478 * are_files_identical
2480 static int are_files_identical( FILE *file1, FILE *file2 )
2482 for (;;)
2484 char buffer1[8192], buffer2[8192];
2485 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2486 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2487 if (size1 != size2) return 0;
2488 if (!size1) return feof( file1 ) && feof( file2 );
2489 if (memcmp( buffer1, buffer2, size1 )) return 0;
2494 /*******************************************************************
2495 * rename_temp_file_if_changed
2497 static void rename_temp_file_if_changed( const char *dest )
2499 FILE *file1, *file2;
2500 int do_rename = 1;
2502 if ((file1 = fopen( dest, "r" )))
2504 if ((file2 = fopen( temp_file_name, "r" )))
2506 do_rename = !are_files_identical( file1, file2 );
2507 fclose( file2 );
2509 fclose( file1 );
2511 if (!do_rename)
2513 unlink( temp_file_name );
2514 temp_file_name = NULL;
2516 else rename_temp_file( dest );
2520 /*******************************************************************
2521 * output_testlist
2523 static void output_testlist( const char *dest, struct strarray files )
2525 int i;
2527 output_file = create_temp_file( dest );
2529 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2530 output( "#define WIN32_LEAN_AND_MEAN\n" );
2531 output( "#include <windows.h>\n\n" );
2532 output( "#define STANDALONE\n" );
2533 output( "#include \"wine/test.h\"\n\n" );
2535 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2536 output( "\n" );
2537 output( "const struct test winetest_testlist[] =\n" );
2538 output( "{\n" );
2539 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2540 output( " { 0, 0 }\n" );
2541 output( "};\n" );
2543 if (fclose( output_file )) fatal_perror( "write" );
2544 output_file = NULL;
2545 rename_temp_file_if_changed( dest );
2549 /*******************************************************************
2550 * output_gitignore
2552 static void output_gitignore( const char *dest, struct strarray files )
2554 int i;
2556 output_file = create_temp_file( dest );
2558 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2559 for (i = 0; i < files.count; i++)
2561 if (!strchr( files.str[i], '/' )) output( "/" );
2562 output( "%s\n", files.str[i] );
2565 if (fclose( output_file )) fatal_perror( "write" );
2566 output_file = NULL;
2567 rename_temp_file( dest );
2571 /*******************************************************************
2572 * output_top_variables
2574 static void output_top_variables( struct makefile *make )
2576 unsigned int i;
2577 struct strarray *vars = &top_makefile->vars;
2579 if (!make->base_dir) return; /* don't output variables in the top makefile */
2581 output( "# Automatically generated by make depend; DO NOT EDIT!!\n\n" );
2582 output( "all:\n\n" );
2583 for (i = 0; i < vars->count; i += 2)
2584 output( "%s = %s\n", vars->str[i], get_make_variable( make, vars->str[i] ));
2585 output( "\n" );
2589 /*******************************************************************
2590 * output_dependencies
2592 static void output_dependencies( struct makefile *make )
2594 struct strarray targets, testlist_files = empty_strarray, ignore_files = empty_strarray;
2595 char buffer[1024];
2596 FILE *src_file;
2598 output_file_name = base_dir_path( make, output_makefile_name );
2599 output_file = create_temp_file( output_file_name );
2600 output_top_variables( make );
2602 /* copy the contents of the source makefile */
2603 src_file = open_input_makefile( make );
2604 while (fgets( buffer, sizeof(buffer), src_file ))
2605 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
2606 if (fclose( src_file )) fatal_perror( "close" );
2607 input_file_name = NULL;
2609 targets = output_sources( make, &testlist_files );
2611 fclose( output_file );
2612 output_file = NULL;
2613 rename_temp_file( output_file_name );
2615 strarray_add( &ignore_files, ".gitignore" );
2616 strarray_add( &ignore_files, "Makefile" );
2617 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2618 strarray_addall( &ignore_files, targets );
2620 if (testlist_files.count)
2621 output_testlist( base_dir_path( make, "testlist.c" ), testlist_files );
2622 if (!make->src_dir && make->base_dir)
2623 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
2625 output_file_name = NULL;
2629 /*******************************************************************
2630 * update_makefile
2632 static void update_makefile( const char *path )
2634 static const char *source_vars[] =
2636 "C_SRCS",
2637 "OBJC_SRCS",
2638 "RC_SRCS",
2639 "MC_SRCS",
2640 "IDL_SRCS",
2641 "BISON_SRCS",
2642 "LEX_SRCS",
2643 "XTEMPLATE_SRCS",
2644 "SVG_SRCS",
2645 "FONT_SRCS",
2646 "IN_SRCS",
2647 "MANPAGES",
2648 NULL
2650 const char **var;
2651 unsigned int i;
2652 struct strarray value;
2653 struct incl_file *file;
2654 struct makefile *make;
2656 make = parse_makefile( path, NULL );
2658 if (root_src_dir)
2660 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
2661 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
2663 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
2664 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
2665 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
2667 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
2668 make->module = get_expanded_make_variable( make, "MODULE" );
2669 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
2670 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
2671 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
2673 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
2674 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
2675 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
2676 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
2677 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
2679 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
2681 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
2682 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
2684 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
2685 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 );
2687 make->include_args = empty_strarray;
2688 make->define_args = empty_strarray;
2689 strarray_add( &make->define_args, "-D__WINESRC__" );
2691 value = get_expanded_make_var_array( make, "EXTRAINCL" );
2692 for (i = 0; i < value.count; i++)
2693 if (!strncmp( value.str[i], "-I", 2 ))
2694 strarray_add_uniq( &make->include_args, value.str[i] );
2695 else
2696 strarray_add_uniq( &make->define_args, value.str[i] );
2697 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
2699 list_init( &make->sources );
2700 list_init( &make->includes );
2702 /* FIXME: target dir has to exist to allow locating srcdir-relative include files */
2703 if (make->base_dir) create_dir( make->base_dir );
2705 for (var = source_vars; *var; var++)
2707 value = get_expanded_make_var_array( make, *var );
2708 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
2711 add_generated_sources( make );
2713 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
2714 for (i = 0; i < value.count; i++)
2716 /* default to .c for unknown extra object files */
2717 if (strendswith( value.str[i], ".o" ))
2718 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2719 else
2720 add_generated_source( make, value.str[i], NULL );
2723 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
2725 output_dependencies( make );
2729 /*******************************************************************
2730 * parse_makeflags
2732 static void parse_makeflags( const char *flags )
2734 const char *p = flags;
2735 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2737 while (*p)
2739 while (isspace(*p)) p++;
2740 var = buffer;
2741 while (*p && !isspace(*p))
2743 if (*p == '\\' && p[1]) p++;
2744 *var++ = *p++;
2746 *var = 0;
2747 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2752 /*******************************************************************
2753 * parse_option
2755 static int parse_option( const char *opt )
2757 if (opt[0] != '-')
2759 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2760 return 0;
2762 switch(opt[1])
2764 case 'f':
2765 if (opt[2]) output_makefile_name = opt + 2;
2766 break;
2767 case 'i':
2768 if (opt[2]) input_makefile_name = opt + 2;
2769 break;
2770 case 'R':
2771 relative_dir_mode = 1;
2772 break;
2773 default:
2774 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2775 exit(1);
2777 return 1;
2781 /*******************************************************************
2782 * main
2784 int main( int argc, char *argv[] )
2786 const char *makeflags = getenv( "MAKEFLAGS" );
2787 int i, j;
2789 if (makeflags) parse_makeflags( makeflags );
2791 i = 1;
2792 while (i < argc)
2794 if (parse_option( argv[i] ))
2796 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2797 argc--;
2799 else i++;
2802 if (relative_dir_mode)
2804 char *relpath;
2806 if (argc != 3)
2808 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2809 exit( 1 );
2811 relpath = get_relative_path( argv[1], argv[2] );
2812 printf( "%s\n", relpath ? relpath : "." );
2813 exit( 0 );
2816 if (argc <= 1)
2818 fprintf( stderr, "%s", Usage );
2819 exit( 1 );
2821 atexit( cleanup_files );
2822 signal( SIGTERM, exit_on_signal );
2823 signal( SIGINT, exit_on_signal );
2824 #ifdef SIGHUP
2825 signal( SIGHUP, exit_on_signal );
2826 #endif
2828 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
2830 if (!input_makefile_name) input_makefile_name = strmake( "%s.in", output_makefile_name );
2832 top_makefile = parse_makefile( NULL, "# End of common header" );
2834 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
2835 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
2836 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
2837 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
2838 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
2839 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
2840 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
2841 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
2843 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
2844 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
2845 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
2846 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
2847 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
2848 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2849 dll_prefix = get_expanded_make_variable( top_makefile, "DLLPREFIX" );
2850 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
2851 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
2852 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
2853 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
2854 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
2856 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
2857 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2858 if (!exe_ext) exe_ext = "";
2859 if (!tools_ext) tools_ext = "";
2860 if (!dll_prefix) dll_prefix = "";
2861 if (!man_ext) man_ext = "3w";
2863 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2864 return 0;