makedep: Allow the top-level makefile to set default values for make variables.
[wine.git] / tools / makedep.c
blob9ec8aefe75ff5473f7a3975a0926c51e0880f02f
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 incl_file
40 struct list entry;
41 char *name;
42 char *filename;
43 char *sourcename; /* source file name for generated headers */
44 void *args; /* custom arguments for makefile rule */
45 struct incl_file *included_by; /* file that included this one */
46 int included_line; /* line where this file was included */
47 unsigned int flags; /* flags (see below) */
48 struct incl_file *owner;
49 unsigned int files_count; /* files in use */
50 unsigned int files_size; /* total allocated size */
51 struct incl_file **files;
54 #define FLAG_SYSTEM 0x000001 /* is it a system include (#include <name>) */
55 #define FLAG_GENERATED 0x000002 /* generated file */
56 #define FLAG_INSTALL 0x000004 /* file to install */
57 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
58 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
59 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
60 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
61 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
62 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
63 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
64 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
65 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
66 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
67 #define FLAG_SFD_FONTS 0x040000 /* sfd file generated bitmap fonts */
69 static const struct
71 unsigned int flag;
72 const char *ext;
73 } idl_outputs[] =
75 { FLAG_IDL_TYPELIB, ".tlb" },
76 { FLAG_IDL_REGTYPELIB, "_t.res" },
77 { FLAG_IDL_CLIENT, "_c.c" },
78 { FLAG_IDL_IDENT, "_i.c" },
79 { FLAG_IDL_PROXY, "_p.c" },
80 { FLAG_IDL_SERVER, "_s.c" },
81 { FLAG_IDL_REGISTER, "_r.res" },
82 { FLAG_IDL_HEADER, ".h" }
85 static struct list sources = LIST_INIT(sources);
86 static struct list includes = LIST_INIT(includes);
88 struct strarray
90 unsigned int count; /* strings in use */
91 unsigned int size; /* total allocated size */
92 const char **str;
95 static const struct strarray empty_strarray;
97 /* variables common to all makefiles */
98 static struct strarray linguas;
99 static struct strarray dll_flags;
100 static struct strarray target_flags;
101 static struct strarray msvcrt_flags;
102 static struct strarray extra_cflags;
103 static struct strarray cpp_flags;
104 static struct strarray unwind_flags;
105 static struct strarray libs;
106 static struct strarray cmdline_vars;
107 static struct strarray top_make_vars;
108 static const char *root_src_dir;
109 static const char *tools_dir;
110 static const char *tools_ext;
111 static const char *exe_ext;
112 static const char *dll_ext;
113 static const char *man_ext;
114 static const char *dll_prefix;
115 static const char *crosstarget;
116 static const char *fontforge;
117 static const char *convert;
118 static const char *rsvg;
119 static const char *icotool;
121 /* variables that can be set by individual makefiles */
122 static struct strarray include_args;
123 static struct strarray define_args;
124 static struct strarray appmode;
125 static struct strarray imports;
126 static struct strarray make_vars;
127 static struct strarray testlist_files;
128 static const char *base_dir;
129 static const char *src_dir;
130 static const char *top_src_dir;
131 static const char *top_obj_dir;
132 static const char *parent_dir;
134 static const char *makefile_name = "Makefile";
135 static const char *Separator = "### Dependencies";
136 static const char *input_file_name;
137 static const char *output_file_name;
138 static const char *temp_file_name;
139 static int use_msvcrt;
140 static int relative_dir_mode;
141 static int input_line;
142 static int output_column;
143 static FILE *output_file;
145 static const char Usage[] =
146 "Usage: makedep [options] directories\n"
147 "Options:\n"
148 " -R from to Compute the relative path between two directories\n"
149 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
150 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
153 #ifndef __GNUC__
154 #define __attribute__(x)
155 #endif
157 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
158 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
159 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
160 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
162 /*******************************************************************
163 * fatal_error
165 static void fatal_error( const char *msg, ... )
167 va_list valist;
168 va_start( valist, msg );
169 if (input_file_name)
171 fprintf( stderr, "%s:", input_file_name );
172 if (input_line) fprintf( stderr, "%d:", input_line );
173 fprintf( stderr, " error: " );
175 else fprintf( stderr, "makedep: error: " );
176 vfprintf( stderr, msg, valist );
177 va_end( valist );
178 exit(1);
182 /*******************************************************************
183 * fatal_perror
185 static void fatal_perror( const char *msg, ... )
187 va_list valist;
188 va_start( valist, msg );
189 if (input_file_name)
191 fprintf( stderr, "%s:", input_file_name );
192 if (input_line) fprintf( stderr, "%d:", input_line );
193 fprintf( stderr, " error: " );
195 else fprintf( stderr, "makedep: error: " );
196 vfprintf( stderr, msg, valist );
197 perror( " " );
198 va_end( valist );
199 exit(1);
203 /*******************************************************************
204 * cleanup_files
206 static void cleanup_files(void)
208 if (temp_file_name) unlink( temp_file_name );
209 if (output_file_name) unlink( output_file_name );
213 /*******************************************************************
214 * exit_on_signal
216 static void exit_on_signal( int sig )
218 exit( 1 ); /* this will call the atexit functions */
222 /*******************************************************************
223 * xmalloc
225 static void *xmalloc( size_t size )
227 void *res;
228 if (!(res = malloc (size ? size : 1)))
229 fatal_error( "Virtual memory exhausted.\n" );
230 return res;
234 /*******************************************************************
235 * xrealloc
237 static void *xrealloc (void *ptr, size_t size)
239 void *res;
240 assert( size );
241 if (!(res = realloc( ptr, size )))
242 fatal_error( "Virtual memory exhausted.\n" );
243 return res;
246 /*******************************************************************
247 * xstrdup
249 static char *xstrdup( const char *str )
251 char *res = strdup( str );
252 if (!res) fatal_error( "Virtual memory exhausted.\n" );
253 return res;
257 /*******************************************************************
258 * strmake
260 static char *strmake( const char* fmt, ... )
262 int n;
263 size_t size = 100;
264 va_list ap;
266 for (;;)
268 char *p = xmalloc (size);
269 va_start(ap, fmt);
270 n = vsnprintf (p, size, fmt, ap);
271 va_end(ap);
272 if (n == -1) size *= 2;
273 else if ((size_t)n >= size) size = n + 1;
274 else return p;
275 free(p);
280 /*******************************************************************
281 * strendswith
283 static int strendswith( const char* str, const char* end )
285 int l = strlen(str);
286 int m = strlen(end);
288 return l >= m && strcmp(str + l - m, end) == 0;
292 /*******************************************************************
293 * output
295 static void output( const char *format, ... )
297 int ret;
298 va_list valist;
300 va_start( valist, format );
301 ret = vfprintf( output_file, format, valist );
302 va_end( valist );
303 if (ret < 0) fatal_perror( "output" );
304 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
305 else output_column += ret;
309 /*******************************************************************
310 * strarray_add
312 static void strarray_add( struct strarray *array, const char *str )
314 if (array->count == array->size)
316 if (array->size) array->size *= 2;
317 else array->size = 16;
318 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
320 array->str[array->count++] = str;
324 /*******************************************************************
325 * strarray_addall
327 static void strarray_addall( struct strarray *array, struct strarray added )
329 unsigned int i;
331 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
335 /*******************************************************************
336 * strarray_add_uniq
338 static void strarray_add_uniq( struct strarray *array, const char *str )
340 unsigned int i;
342 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return;
343 strarray_add( array, str );
347 /*******************************************************************
348 * output_filename
350 static void output_filename( const char *name )
352 if (output_column + strlen(name) + 1 > 100)
354 output( " \\\n" );
355 output( " " );
357 else if (output_column) output( " " );
358 output( "%s", name );
362 /*******************************************************************
363 * output_filenames
365 static void output_filenames( struct strarray array )
367 unsigned int i;
369 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
373 /*******************************************************************
374 * get_extension
376 static char *get_extension( char *filename )
378 char *ext = strrchr( filename, '.' );
379 if (ext && strchr( ext, '/' )) ext = NULL;
380 return ext;
384 /*******************************************************************
385 * replace_extension
387 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
389 char *ret;
390 int name_len = strlen( name );
391 int ext_len = strlen( old_ext );
393 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
394 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
395 memcpy( ret, name, name_len );
396 strcpy( ret + name_len, new_ext );
397 return ret;
401 /*******************************************************************
402 * strarray_replace_extension
404 static struct strarray strarray_replace_extension( const struct strarray *array,
405 const char *old_ext, const char *new_ext )
407 unsigned int i;
408 struct strarray ret;
410 ret.count = ret.size = array->count;
411 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
412 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
413 return ret;
417 /*******************************************************************
418 * replace_substr
420 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
422 unsigned int pos = start - str;
423 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
424 memcpy( ret, str, pos );
425 strcpy( ret + pos, replace );
426 strcat( ret + pos, start + len );
427 return ret;
431 /*******************************************************************
432 * get_relative_path
434 * Determine where the destination path is located relative to the 'from' path.
436 static char *get_relative_path( const char *from, const char *dest )
438 const char *start;
439 char *ret, *p;
440 unsigned int dotdots = 0;
442 /* a path of "." is equivalent to an empty path */
443 if (!strcmp( from, "." )) from = "";
445 for (;;)
447 while (*from == '/') from++;
448 while (*dest == '/') dest++;
449 start = dest; /* save start of next path element */
450 if (!*from) break;
452 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
453 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
455 /* count remaining elements in 'from' */
458 dotdots++;
459 while (*from && *from != '/') from++;
460 while (*from == '/') from++;
462 while (*from);
463 break;
466 if (!start[0] && !dotdots) return NULL; /* empty path */
468 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
469 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
471 if (start[0]) strcpy( p, start );
472 else p[-1] = 0; /* remove trailing slash */
473 return ret;
477 /*******************************************************************
478 * concat_paths
480 static char *concat_paths( const char *base, const char *path )
482 if (!base) return xstrdup( path[0] ? path : "." );
483 if (path[0] == '/') return xstrdup( path );
484 if (!path[0]) return xstrdup( base );
485 return strmake( "%s/%s", base, path );
489 /*******************************************************************
490 * base_dir_path
492 static char *base_dir_path( const char *path )
494 return concat_paths( base_dir, path );
498 /*******************************************************************
499 * src_dir_path
501 static char *src_dir_path( const char *path )
503 return concat_paths( src_dir, path );
507 /*******************************************************************
508 * top_obj_dir_path
510 static char *top_obj_dir_path( const char *path )
512 return concat_paths( top_obj_dir, path );
516 /*******************************************************************
517 * top_dir_path
519 static char *top_dir_path( const char *path )
521 if (top_src_dir) return concat_paths( top_src_dir, path );
522 return top_obj_dir_path( path );
526 /*******************************************************************
527 * tools_dir_path
529 static char *tools_dir_path( const char *path )
531 if (tools_dir) return top_obj_dir_path( strmake( "%s/tools/%s", tools_dir, path ));
532 return top_obj_dir_path( strmake( "tools/%s", path ));
536 /*******************************************************************
537 * tools_path
539 static char *tools_path( const char *name )
541 return strmake( "%s/%s%s", tools_dir_path( name ), name, tools_ext );
545 /*******************************************************************
546 * get_line
548 static char *get_line( FILE *file )
550 static char *buffer;
551 static unsigned int size;
553 if (!size)
555 size = 1024;
556 buffer = xmalloc( size );
558 if (!fgets( buffer, size, file )) return NULL;
559 input_line++;
561 for (;;)
563 char *p = buffer + strlen(buffer);
564 /* if line is larger than buffer, resize buffer */
565 while (p == buffer + size - 1 && p[-1] != '\n')
567 buffer = xrealloc( buffer, size * 2 );
568 if (!fgets( buffer + size - 1, size + 1, file )) break;
569 p = buffer + strlen(buffer);
570 size *= 2;
572 if (p > buffer && p[-1] == '\n')
574 *(--p) = 0;
575 if (p > buffer && p[-1] == '\r') *(--p) = 0;
576 if (p > buffer && p[-1] == '\\')
578 *(--p) = 0;
579 /* line ends in backslash, read continuation line */
580 if (!fgets( p, size - (p - buffer), file )) return buffer;
581 input_line++;
582 continue;
585 return buffer;
589 /*******************************************************************
590 * find_src_file
592 static struct incl_file *find_src_file( const char *name )
594 struct incl_file *file;
596 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
597 if (!strcmp( name, file->name )) return file;
598 return NULL;
601 /*******************************************************************
602 * find_include_file
604 static struct incl_file *find_include_file( const char *name )
606 struct incl_file *file;
608 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
609 if (!strcmp( name, file->name )) return file;
610 return NULL;
613 /*******************************************************************
614 * add_include
616 * Add an include file if it doesn't already exists.
618 static struct incl_file *add_include( struct incl_file *parent, const char *name, int system )
620 struct incl_file *include;
621 char *ext;
623 if (parent->files_count >= parent->files_size)
625 parent->files_size *= 2;
626 if (parent->files_size < 16) parent->files_size = 16;
627 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
630 /* enforce some rules for the Wine tree */
632 if (!memcmp( name, "../", 3 ))
633 fatal_error( "#include directive with relative path not allowed\n" );
635 if (!strcmp( name, "config.h" ))
637 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
638 fatal_error( "config.h must not be included by a header file\n" );
639 if (parent->files_count)
640 fatal_error( "config.h must be included before anything else\n" );
642 else if (!strcmp( name, "wine/port.h" ))
644 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
645 fatal_error( "wine/port.h must not be included by a header file\n" );
646 if (!parent->files_count) fatal_error( "config.h must be included before wine/port.h\n" );
647 if (parent->files_count > 1)
648 fatal_error( "wine/port.h must be included before everything except config.h\n" );
649 if (strcmp( parent->files[0]->name, "config.h" ))
650 fatal_error( "config.h must be included before wine/port.h\n" );
653 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
654 if (!strcmp( name, include->name )) goto found;
656 include = xmalloc( sizeof(*include) );
657 memset( include, 0, sizeof(*include) );
658 include->name = xstrdup(name);
659 include->included_by = parent;
660 include->included_line = input_line;
661 if (system) include->flags |= FLAG_SYSTEM;
662 list_add_tail( &includes, &include->entry );
663 found:
664 parent->files[parent->files_count++] = include;
665 return include;
669 /*******************************************************************
670 * add_generated_source
672 * Add a generated source file to the list.
674 static struct incl_file *add_generated_source( const char *name, const char *filename )
676 struct incl_file *file;
678 if ((file = find_src_file( name ))) return file; /* we already have it */
679 file = xmalloc( sizeof(*file) );
680 memset( file, 0, sizeof(*file) );
681 file->name = xstrdup( name );
682 file->filename = xstrdup( filename ? filename : name );
683 file->flags = FLAG_GENERATED;
684 list_add_tail( &sources, &file->entry );
685 return file;
689 /*******************************************************************
690 * open_file
692 static FILE *open_file( const char *path )
694 return fopen( base_dir_path( path ), "r" );
697 /*******************************************************************
698 * open_src_file
700 static FILE *open_src_file( struct incl_file *pFile )
702 FILE *file;
704 /* try in source dir */
705 pFile->filename = src_dir_path( pFile->name );
706 file = open_file( pFile->filename );
708 /* now try parent dir */
709 if (!file && parent_dir)
711 pFile->filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
712 file = open_file( pFile->filename );
714 if (!file) fatal_perror( "open %s", pFile->name );
715 return file;
719 /*******************************************************************
720 * open_include_file
722 static FILE *open_include_file( struct incl_file *pFile )
724 FILE *file = NULL;
725 char *filename, *p;
726 unsigned int i, len;
728 errno = ENOENT;
730 /* check for generated bison header */
732 if (strendswith( pFile->name, ".tab.h" ))
734 filename = src_dir_path( replace_extension( pFile->name, ".tab.h", ".y" ));
735 if ((file = open_file( filename )))
737 pFile->sourcename = filename;
738 pFile->filename = xstrdup( pFile->name );
739 /* don't bother to parse it */
740 fclose( file );
741 return NULL;
743 free( filename );
746 /* check for corresponding idl file in source dir */
748 if (strendswith( pFile->name, ".h" ))
750 filename = src_dir_path( replace_extension( pFile->name, ".h", ".idl" ));
751 if ((file = open_file( filename )))
753 pFile->sourcename = filename;
754 pFile->filename = xstrdup( pFile->name );
755 return file;
757 free( filename );
760 /* now try in source dir */
761 filename = src_dir_path( pFile->name );
762 if ((file = open_file( filename ))) goto found;
763 free( filename );
765 /* now try in parent source dir */
766 if (parent_dir)
768 filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
769 if ((file = open_file( filename ))) goto found;
770 free( filename );
773 /* check for corresponding idl file in global includes */
775 if (strendswith( pFile->name, ".h" ))
777 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".idl" )));
778 if ((file = open_file( filename )))
780 pFile->sourcename = filename;
781 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
782 return file;
784 free( filename );
787 /* check for corresponding .in file in global includes (for config.h.in) */
789 if (strendswith( pFile->name, ".h" ))
791 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".h.in" )));
792 if ((file = open_file( filename )))
794 pFile->sourcename = filename;
795 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
796 return file;
798 free( filename );
801 /* check for corresponding .x file in global includes */
803 if (strendswith( pFile->name, "tmpl.h" ))
805 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".x" )));
806 if ((file = open_file( filename )))
808 pFile->sourcename = filename;
809 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
810 return file;
812 free( filename );
815 /* check in global includes source dir */
817 filename = top_dir_path( strmake( "include/%s", pFile->name ));
818 if ((file = open_file( filename ))) goto found;
820 /* check in global msvcrt includes */
821 if (use_msvcrt)
823 filename = top_dir_path( strmake( "include/msvcrt/%s", pFile->name ));
824 if ((file = open_file( filename ))) goto found;
827 /* now search in include paths */
828 for (i = 0; i < include_args.count; i++)
830 const char *dir = include_args.str[i] + 2; /* skip -I */
831 if (*dir == '/')
833 /* ignore absolute paths that don't point into the source dir */
834 if (!top_src_dir) continue;
835 len = strlen( top_src_dir );
836 if (strncmp( dir, top_src_dir, len )) continue;
837 if (dir[len] && dir[len] != '/') continue;
839 filename = strmake( "%s/%s", dir, pFile->name );
840 if ((file = open_file( filename ))) goto found;
841 free( filename );
843 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
845 /* try in src file directory */
846 if ((p = strrchr(pFile->included_by->filename, '/')))
848 int l = p - pFile->included_by->filename + 1;
849 filename = xmalloc(l + strlen(pFile->name) + 1);
850 memcpy( filename, pFile->included_by->filename, l );
851 strcpy( filename + l, pFile->name );
852 if ((file = open_file( filename ))) goto found;
853 free( filename );
856 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
857 perror( pFile->name );
858 pFile = pFile->included_by;
859 while (pFile && pFile->included_by)
861 const char *parent = pFile->included_by->sourcename;
862 if (!parent) parent = pFile->included_by->name;
863 fprintf( stderr, "%s:%d: note: %s was first included here\n",
864 parent, pFile->included_line, pFile->name );
865 pFile = pFile->included_by;
867 exit(1);
869 found:
870 pFile->filename = filename;
871 return file;
875 /*******************************************************************
876 * parse_include_directive
878 static void parse_include_directive( struct incl_file *source, char *str )
880 char quote, *include, *p = str;
882 while (*p && isspace(*p)) p++;
883 if (*p != '\"' && *p != '<' ) return;
884 quote = *p++;
885 if (quote == '<') quote = '>';
886 include = p;
887 while (*p && (*p != quote)) p++;
888 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
889 *p = 0;
890 add_include( source, include, (quote == '>') );
894 /*******************************************************************
895 * parse_pragma_directive
897 static void parse_pragma_directive( struct incl_file *source, char *str )
899 char *flag, *p = str;
901 if (!isspace( *p )) return;
902 while (*p && isspace(*p)) p++;
903 p = strtok( p, " \t" );
904 if (strcmp( p, "makedep" )) return;
906 while ((flag = strtok( NULL, " \t" )))
908 if (!strcmp( flag, "depend" ))
910 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
911 return;
913 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
915 if (strendswith( source->name, ".idl" ))
917 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
918 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
919 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
920 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
921 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
922 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
923 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
924 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
926 else if (strendswith( source->name, ".rc" ))
928 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
930 else if (strendswith( source->name, ".sfd" ))
932 if (!strcmp( flag, "font" ))
934 struct strarray *array = source->args;
936 if (!array)
938 source->args = array = xmalloc( sizeof(*array) );
939 *array = empty_strarray;
940 source->flags |= FLAG_SFD_FONTS;
942 strarray_add( array, xstrdup( strtok( NULL, "" )));
943 return;
946 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
951 /*******************************************************************
952 * parse_cpp_directive
954 static void parse_cpp_directive( struct incl_file *source, char *str )
956 while (*str && isspace(*str)) str++;
957 if (*str++ != '#') return;
958 while (*str && isspace(*str)) str++;
960 if (!strncmp( str, "include", 7 ))
961 parse_include_directive( source, str + 7 );
962 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
963 parse_include_directive( source, str + 6 );
964 else if (!strncmp( str, "pragma", 6 ))
965 parse_pragma_directive( source, str + 6 );
969 /*******************************************************************
970 * parse_idl_file
972 * If for_h_file is non-zero, it means we are not interested in the idl file
973 * itself, but only in the contents of the .h file that will be generated from it.
975 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
977 char *buffer, *include;
979 input_line = 0;
980 if (for_h_file)
982 /* generated .h file always includes these */
983 add_include( pFile, "rpc.h", 1 );
984 add_include( pFile, "rpcndr.h", 1 );
987 while ((buffer = get_line( file )))
989 char quote;
990 char *p = buffer;
991 while (*p && isspace(*p)) p++;
993 if (!strncmp( p, "import", 6 ))
995 p += 6;
996 while (*p && isspace(*p)) p++;
997 if (*p != '"') continue;
998 include = ++p;
999 while (*p && (*p != '"')) p++;
1000 if (!*p) fatal_error( "malformed import directive\n" );
1001 *p = 0;
1002 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
1003 add_include( pFile, include, 0 );
1004 continue;
1007 if (for_h_file) /* only check for #include inside cpp_quote */
1009 if (strncmp( p, "cpp_quote", 9 )) continue;
1010 p += 9;
1011 while (*p && isspace(*p)) p++;
1012 if (*p++ != '(') continue;
1013 while (*p && isspace(*p)) p++;
1014 if (*p++ != '"') continue;
1015 if (*p++ != '#') continue;
1016 while (*p && isspace(*p)) p++;
1017 if (strncmp( p, "include", 7 )) continue;
1018 p += 7;
1019 while (*p && isspace(*p)) p++;
1020 if (*p == '\\' && p[1] == '"')
1022 p += 2;
1023 quote = '"';
1025 else
1027 if (*p++ != '<' ) continue;
1028 quote = '>';
1030 include = p;
1031 while (*p && (*p != quote)) p++;
1032 if (!*p || (quote == '"' && p[-1] != '\\'))
1033 fatal_error( "malformed #include directive inside cpp_quote\n" );
1034 if (quote == '"') p--; /* remove backslash */
1035 *p = 0;
1036 add_include( pFile, include, (quote == '>') );
1037 continue;
1040 parse_cpp_directive( pFile, p );
1044 /*******************************************************************
1045 * parse_c_file
1047 static void parse_c_file( struct incl_file *pFile, FILE *file )
1049 char *buffer;
1051 input_line = 0;
1052 while ((buffer = get_line( file )))
1054 parse_cpp_directive( pFile, buffer );
1059 /*******************************************************************
1060 * parse_rc_file
1062 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1064 char *buffer, *include;
1066 input_line = 0;
1067 while ((buffer = get_line( file )))
1069 char quote;
1070 char *p = buffer;
1071 while (*p && isspace(*p)) p++;
1073 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1075 p += 2;
1076 while (*p && isspace(*p)) p++;
1077 if (strncmp( p, "@makedep:", 9 )) continue;
1078 p += 9;
1079 while (*p && isspace(*p)) p++;
1080 quote = '"';
1081 if (*p == quote)
1083 include = ++p;
1084 while (*p && *p != quote) p++;
1086 else
1088 include = p;
1089 while (*p && !isspace(*p) && *p != '*') p++;
1091 if (!*p)
1092 fatal_error( "malformed makedep comment\n" );
1093 *p = 0;
1094 add_include( pFile, include, (quote == '>') );
1095 continue;
1098 parse_cpp_directive( pFile, buffer );
1103 /*******************************************************************
1104 * parse_in_file
1106 static void parse_in_file( struct incl_file *source, FILE *file )
1108 char *p, *buffer;
1110 /* make sure it gets rebuilt when the version changes */
1111 add_include( source, "config.h", 1 );
1113 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1115 input_line = 0;
1116 while ((buffer = get_line( file )))
1118 if (strncmp( buffer, ".TH", 3 )) continue;
1119 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1120 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1121 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1122 source->args = xstrdup( p );
1123 return;
1128 /*******************************************************************
1129 * parse_sfd_file
1131 static void parse_sfd_file( struct incl_file *source, FILE *file )
1133 char *p, *eol, *buffer;
1135 input_line = 0;
1136 while ((buffer = get_line( file )))
1138 if (strncmp( buffer, "UComments:", 10 )) continue;
1139 p = buffer + 10;
1140 while (*p == ' ') p++;
1141 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1143 p++;
1144 buffer[strlen(buffer) - 1] = 0;
1146 while ((eol = strstr( p, "+AAoA" )))
1148 *eol = 0;
1149 while (*p && isspace(*p)) p++;
1150 if (*p++ == '#')
1152 while (*p && isspace(*p)) p++;
1153 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1155 p = eol + 5;
1157 while (*p && isspace(*p)) p++;
1158 if (*p++ != '#') return;
1159 while (*p && isspace(*p)) p++;
1160 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1161 return;
1166 /*******************************************************************
1167 * parse_file
1169 static void parse_file( struct incl_file *source, int src )
1171 FILE *file;
1173 /* don't try to open certain types of files */
1174 if (strendswith( source->name, ".tlb" ))
1176 source->filename = xstrdup( source->name );
1177 return;
1180 file = src ? open_src_file( source ) : open_include_file( source );
1181 if (!file) return;
1182 input_file_name = source->filename;
1184 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1185 parse_idl_file( source, file, 1 );
1186 else if (strendswith( source->filename, ".idl" ))
1187 parse_idl_file( source, file, 0 );
1188 else if (strendswith( source->filename, ".c" ) ||
1189 strendswith( source->filename, ".m" ) ||
1190 strendswith( source->filename, ".h" ) ||
1191 strendswith( source->filename, ".l" ) ||
1192 strendswith( source->filename, ".y" ))
1193 parse_c_file( source, file );
1194 else if (strendswith( source->filename, ".rc" ))
1195 parse_rc_file( source, file );
1196 else if (strendswith( source->filename, ".in" ))
1197 parse_in_file( source, file );
1198 else if (strendswith( source->filename, ".sfd" ))
1199 parse_sfd_file( source, file );
1200 fclose(file);
1201 input_file_name = NULL;
1205 /*******************************************************************
1206 * add_src_file
1208 * Add a source file to the list.
1210 static struct incl_file *add_src_file( const char *name )
1212 struct incl_file *file;
1214 if ((file = find_src_file( name ))) return file; /* we already have it */
1215 file = xmalloc( sizeof(*file) );
1216 memset( file, 0, sizeof(*file) );
1217 file->name = xstrdup(name);
1218 list_add_tail( &sources, &file->entry );
1219 parse_file( file, 1 );
1220 return file;
1224 /*******************************************************************
1225 * get_make_variable
1227 static char *get_make_variable( const char *name )
1229 unsigned int i;
1231 for (i = 0; i < cmdline_vars.count; i += 2)
1232 if (!strcmp( cmdline_vars.str[i], name ))
1233 return xstrdup( cmdline_vars.str[i + 1] );
1235 for (i = 0; i < make_vars.count; i += 2)
1236 if (!strcmp( make_vars.str[i], name ))
1237 return xstrdup( make_vars.str[i + 1] );
1239 for (i = 0; i < top_make_vars.count; i += 2)
1240 if (!strcmp( top_make_vars.str[i], name ))
1241 return xstrdup( top_make_vars.str[i + 1] );
1242 return NULL;
1246 /*******************************************************************
1247 * get_expanded_make_variable
1249 static char *get_expanded_make_variable( const char *name )
1251 char *p, *end, *var, *expand, *tmp;
1253 expand = get_make_variable( name );
1254 if (!expand) return NULL;
1256 p = expand;
1257 while ((p = strchr( p, '$' )))
1259 if (p[1] == '(')
1261 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1262 *end++ = 0;
1263 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1264 var = get_make_variable( p + 2 );
1265 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1266 free( var );
1268 else if (p[1] == '{') /* don't expand ${} variables */
1270 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1271 p = end + 1;
1272 continue;
1274 else if (p[1] == '$')
1276 tmp = replace_substr( expand, p, 2, "$" );
1278 else fatal_error( "syntax error in '%s'\n", expand );
1280 /* switch to the new string */
1281 p = tmp + (p - expand);
1282 free( expand );
1283 expand = tmp;
1286 /* consider empty variables undefined */
1287 p = expand;
1288 while (*p && isspace(*p)) p++;
1289 if (*p) return expand;
1290 free( expand );
1291 return NULL;
1295 /*******************************************************************
1296 * get_expanded_make_var_array
1298 static struct strarray get_expanded_make_var_array( const char *name )
1300 struct strarray ret = empty_strarray;
1301 char *value, *token;
1303 if ((value = get_expanded_make_variable( name )))
1304 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1305 strarray_add( &ret, token );
1306 return ret;
1310 /*******************************************************************
1311 * set_make_variable
1313 static int set_make_variable( struct strarray *array, const char *assignment )
1315 unsigned int i;
1316 char *p, *name;
1318 p = name = xstrdup( assignment );
1319 while (isalnum(*p) || *p == '_') p++;
1320 if (name == p) return 0; /* not a variable */
1321 if (isspace(*p))
1323 *p++ = 0;
1324 while (isspace(*p)) p++;
1326 if (*p != '=') return 0; /* not an assignment */
1327 *p++ = 0;
1328 while (isspace(*p)) p++;
1330 /* redefining a variable replaces the previous value */
1331 for (i = 0; i < array->count; i += 2)
1333 if (strcmp( array->str[i], name )) continue;
1334 array->str[i + 1] = p;
1335 return 1;
1337 strarray_add( array, name );
1338 strarray_add( array, p );
1339 return 1;
1343 /*******************************************************************
1344 * parse_makefile
1346 static void parse_makefile( const char *name, const char *separator, struct strarray *vars )
1348 char *buffer;
1349 FILE *file;
1351 *vars = empty_strarray;
1352 input_file_name = name;
1353 if (!(file = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1355 input_line = 0;
1356 while ((buffer = get_line( file )))
1358 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1359 if (*buffer == '\t') continue; /* command */
1360 while (isspace( *buffer )) buffer++;
1361 if (*buffer == '#') continue; /* comment */
1362 set_make_variable( vars, buffer );
1364 fclose( file );
1365 input_file_name = NULL;
1369 /*******************************************************************
1370 * add_generated_sources
1372 static void add_generated_sources(void)
1374 struct incl_file *source, *next, *file;
1376 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1378 if (source->flags & FLAG_IDL_CLIENT)
1380 file = add_generated_source( replace_extension( source->name, ".idl", "_c.c" ), NULL );
1381 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1383 if (source->flags & FLAG_IDL_SERVER)
1385 file = add_generated_source( replace_extension( source->name, ".idl", "_s.c" ), NULL );
1386 add_include( file, "wine/exception.h", 0 );
1387 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1389 if (source->flags & FLAG_IDL_IDENT)
1391 file = add_generated_source( replace_extension( source->name, ".idl", "_i.c" ), NULL );
1392 add_include( file, "rpc.h", 0 );
1393 add_include( file, "rpcndr.h", 0 );
1394 add_include( file, "guiddef.h", 0 );
1396 if (source->flags & FLAG_IDL_PROXY)
1398 file = add_generated_source( "dlldata.o", "dlldata.c" );
1399 add_include( file, "objbase.h", 0 );
1400 add_include( file, "rpcproxy.h", 0 );
1401 file = add_generated_source( replace_extension( source->name, ".idl", "_p.c" ), NULL );
1402 add_include( file, "objbase.h", 0 );
1403 add_include( file, "rpcproxy.h", 0 );
1404 add_include( file, "wine/exception.h", 0 );
1405 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1407 if (source->flags & FLAG_IDL_REGTYPELIB)
1409 add_generated_source( replace_extension( source->name, ".idl", "_t.res" ), NULL );
1411 if (source->flags & FLAG_IDL_REGISTER)
1413 add_generated_source( replace_extension( source->name, ".idl", "_r.res" ), NULL );
1415 if (strendswith( source->name, ".y" ))
1417 file = add_generated_source( replace_extension( source->name, ".y", ".tab.c" ), NULL );
1418 /* steal the includes list from the source file */
1419 file->files_count = source->files_count;
1420 file->files_size = source->files_size;
1421 file->files = source->files;
1422 source->files_count = source->files_size = 0;
1423 source->files = NULL;
1425 if (strendswith( source->name, ".l" ))
1427 file = add_generated_source( replace_extension( source->name, ".l", ".yy.c" ), NULL );
1428 /* steal the includes list from the source file */
1429 file->files_count = source->files_count;
1430 file->files_size = source->files_size;
1431 file->files = source->files;
1432 source->files_count = source->files_size = 0;
1433 source->files = NULL;
1436 if (get_make_variable( "TESTDLL" ))
1438 file = add_generated_source( "testlist.o", "testlist.c" );
1439 add_include( file, "wine/test.h", 0 );
1444 /*******************************************************************
1445 * create_dir
1447 static void create_dir( const char *dir )
1449 char *p, *path;
1451 p = path = xstrdup( dir );
1452 while ((p = strchr( p, '/' )))
1454 *p = 0;
1455 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1456 *p++ = '/';
1457 while (*p == '/') p++;
1459 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1460 free( path );
1464 /*******************************************************************
1465 * output_include
1467 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1469 int i;
1471 if (pFile->owner == owner) return;
1472 if (!pFile->filename) return;
1473 pFile->owner = owner;
1474 output_filename( pFile->filename );
1475 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1479 /*******************************************************************
1480 * output_sources
1482 static struct strarray output_sources(void)
1484 struct incl_file *source;
1485 unsigned int i;
1486 int is_win16 = 0;
1487 struct strarray object_files = empty_strarray;
1488 struct strarray crossobj_files = empty_strarray;
1489 struct strarray res_files = empty_strarray;
1490 struct strarray clean_files = empty_strarray;
1491 struct strarray po_files = empty_strarray;
1492 struct strarray mo_files = empty_strarray;
1493 struct strarray mc_files = empty_strarray;
1494 struct strarray ok_files = empty_strarray;
1495 struct strarray dlldata_files = empty_strarray;
1496 struct strarray c2man_files = empty_strarray;
1497 struct strarray implib_objs = empty_strarray;
1498 struct strarray includes = empty_strarray;
1499 struct strarray subdirs = empty_strarray;
1500 struct strarray phony_targets = empty_strarray;
1501 struct strarray all_targets = get_expanded_make_var_array( "PROGRAMS" );
1502 struct strarray delayimports = get_expanded_make_var_array( "DELAYIMPORTS" );
1503 struct strarray extradllflags = get_expanded_make_var_array( "EXTRADLLFLAGS" );
1504 char *module = get_expanded_make_variable( "MODULE" );
1505 char *testdll = get_expanded_make_variable( "TESTDLL" );
1506 char *staticlib = get_expanded_make_variable( "STATICLIB" );
1508 if (module && strendswith( module, ".a" )) staticlib = module;
1509 for (i = 0; i < extradllflags.count; i++) if (!strcmp( extradllflags.str[i], "-m16" )) is_win16 = 1;
1511 for (i = 0; i < linguas.count; i++)
1512 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( "po" ), linguas.str[i] ));
1514 strarray_add( &includes, "-I." );
1515 if (src_dir) strarray_add( &includes, strmake( "-I%s", src_dir ));
1516 if (parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( parent_dir )));
1517 if (top_obj_dir) strarray_add( &includes, strmake( "-I%s/include", top_obj_dir ));
1518 if (top_src_dir) strarray_add( &includes, strmake( "-I%s/include", top_src_dir ));
1519 if (use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( "include/msvcrt" )));
1520 strarray_addall( &includes, include_args );
1522 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1524 struct strarray extradefs;
1525 char *obj = xstrdup( source->name );
1526 char *ext = get_extension( obj );
1528 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1529 *ext++ = 0;
1531 if (src_dir && strchr( obj, '/' ))
1533 char *subdir = base_dir_path( obj );
1534 *strrchr( subdir, '/' ) = 0;
1535 strarray_add_uniq( &subdirs, subdir );
1538 extradefs = get_expanded_make_var_array( strmake( "%s_EXTRADEFS", obj ));
1540 if (!strcmp( ext, "y" )) /* yacc file */
1542 /* add source file dependency for parallel makes */
1543 char *header = strmake( "%s.tab.h", obj );
1545 if (find_include_file( header ))
1547 output( "%s.tab.h: %s\n", obj, source->filename );
1548 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1549 obj, obj, source->filename );
1550 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1551 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1553 else output( "%s.tab.c: %s\n", obj, source->filename );
1555 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1556 free( header );
1557 continue; /* no dependencies */
1559 else if (!strcmp( ext, "x" )) /* template file */
1561 output( "%s.h: %s%s %s\n", obj, tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1562 output( "\t%s%s -H -o $@ %s\n", tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1563 strarray_add( &clean_files, strmake( "%s.h", obj ));
1564 continue; /* no dependencies */
1566 else if (!strcmp( ext, "l" )) /* lex file */
1568 output( "%s.yy.c: %s\n", obj, source->filename );
1569 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1570 continue; /* no dependencies */
1572 else if (!strcmp( ext, "rc" )) /* resource file */
1574 strarray_add( &res_files, strmake( "%s.res", obj ));
1575 output( "%s.res: %s %s\n", obj, tools_path( "wrc" ), source->filename );
1576 output( "\t%s -o $@ %s", tools_path( "wrc" ), source->filename );
1577 if (is_win16) output_filename( "-m16" );
1578 else output_filenames( target_flags );
1579 output_filename( "--nostdinc" );
1580 output_filenames( includes );
1581 output_filenames( define_args );
1582 output_filenames( extradefs );
1583 if (mo_files.count && (source->flags & FLAG_RC_PO))
1585 strarray_add( &po_files, source->filename );
1586 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1587 output( "\n" );
1588 output( "%s.res:", obj );
1589 output_filenames( mo_files );
1590 output( "\n" );
1591 output( "rsrc.pot " );
1593 else output( "\n" );
1594 output( "%s.res:", obj );
1596 else if (!strcmp( ext, "mc" )) /* message file */
1598 strarray_add( &res_files, strmake( "%s.res", obj ));
1599 output( "%s.res: %s %s\n", obj, tools_path( "wmc" ), source->filename );
1600 output( "\t%s -U -O res -o $@ %s", tools_path( "wmc" ), source->filename );
1601 if (mo_files.count)
1603 strarray_add( &mc_files, source->filename );
1604 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1605 output( "\n" );
1606 output( "%s.res:", obj );
1607 output_filenames( mo_files );
1608 output( "\n" );
1609 output( "msg.pot " );
1611 else output( "\n" );
1612 output( "%s.res:", obj );
1614 else if (!strcmp( ext, "idl" )) /* IDL file */
1616 struct strarray targets = empty_strarray;
1617 char *dest;
1619 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1620 source->flags |= FLAG_IDL_HEADER;
1622 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1624 if (!(source->flags & idl_outputs[i].flag)) continue;
1625 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1626 if (!find_src_file( dest )) strarray_add( &clean_files, dest );
1627 strarray_add( &targets, dest );
1629 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1630 output_filenames( targets );
1631 output( ": %s\n", tools_path( "widl" ));
1632 output( "\t%s -o $@ %s", tools_path( "widl" ), source->filename );
1633 output_filenames( target_flags );
1634 output_filenames( includes );
1635 output_filenames( define_args );
1636 output_filenames( extradefs );
1637 output_filenames( get_expanded_make_var_array( "EXTRAIDLFLAGS" ));
1638 output( "\n" );
1639 output_filenames( targets );
1640 output( ": %s", source->filename );
1642 else if (!strcmp( ext, "in" )) /* .in file or man page */
1644 if (strendswith( obj, ".man" ) && source->args)
1646 char *dir, *dest = replace_extension( obj, ".man", "" );
1647 char *lang = strchr( dest, '.' );
1648 char *section = source->args;
1649 if (lang)
1651 *lang++ = 0;
1652 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1654 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1655 output( "install-man-pages:: %s\n", obj );
1656 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj, dir, dest, section );
1657 output( "uninstall::\n" );
1658 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1659 free( dest );
1660 free( dir );
1661 strarray_add( &all_targets, xstrdup(obj) );
1662 strarray_add_uniq( &phony_targets, "install-man-pages" );
1663 strarray_add_uniq( &phony_targets, "uninstall" );
1665 else strarray_add( &clean_files, xstrdup(obj) );
1666 output( "%s: %s\n", obj, source->filename );
1667 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1668 output( "%s:", obj );
1670 else if (!strcmp( ext, "sfd" )) /* font file */
1672 char *ttf_file = src_dir_path( strmake( "%s.ttf", obj ));
1673 if (fontforge && !src_dir)
1675 output( "%s: %s\n", ttf_file, source->filename );
1676 output( "\t%s -script %s %s $@\n",
1677 fontforge, top_dir_path( "fonts/genttf.ff" ), source->filename );
1678 if (!(source->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1680 if (source->flags & FLAG_INSTALL)
1682 output( "install install-lib::\n" );
1683 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1684 output( "uninstall::\n" );
1685 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1687 if (source->flags & FLAG_SFD_FONTS)
1689 struct strarray *array = source->args;
1691 for (i = 0; i < array->count; i++)
1693 char *font = strtok( xstrdup(array->str[i]), " \t" );
1694 char *args = strtok( NULL, "" );
1696 strarray_add( &all_targets, font );
1697 output( "%s: %s %s\n", font, tools_path( "sfnt2fon" ), ttf_file );
1698 output( "\t%s -o $@ %s %s\n", tools_path( "sfnt2fon" ), ttf_file, args );
1699 output( "install install-lib:: %s\n", font );
1700 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n", font, font );
1701 output( "uninstall::\n" );
1702 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1705 if (source->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1707 strarray_add_uniq( &phony_targets, "install" );
1708 strarray_add_uniq( &phony_targets, "install-lib" );
1709 strarray_add_uniq( &phony_targets, "uninstall" );
1711 continue; /* no dependencies */
1713 else if (!strcmp( ext, "svg" )) /* svg file */
1715 if (convert && rsvg && icotool && !src_dir)
1717 output( "%s.ico %s.bmp: %s\n", obj, obj, source->filename );
1718 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
1719 convert, icotool, rsvg, top_dir_path( "tools/buildimage" ), source->filename );
1721 continue; /* no dependencies */
1723 else if (!strcmp( ext, "res" ))
1725 strarray_add( &res_files, source->name );
1726 continue; /* no dependencies */
1728 else
1730 int need_cross = testdll || (source->flags & FLAG_C_IMPLIB) || (module && staticlib);
1732 if ((source->flags & FLAG_GENERATED) && (!testdll || strcmp( source->filename, "testlist.c" )))
1733 strarray_add( &clean_files, source->filename );
1734 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1735 strarray_add( &object_files, strmake( "%s.o", obj ));
1736 output( "%s.o: %s\n", obj, source->filename );
1737 output( "\t$(CC) -c -o $@ %s", source->filename );
1738 output_filenames( includes );
1739 output_filenames( define_args );
1740 output_filenames( extradefs );
1741 if (module || staticlib || testdll)
1743 output_filenames( dll_flags );
1744 if (use_msvcrt) output_filenames( msvcrt_flags );
1746 output_filenames( extra_cflags );
1747 output_filenames( cpp_flags );
1748 output_filename( "$(CFLAGS)" );
1749 output( "\n" );
1750 if (crosstarget && need_cross)
1752 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1753 output( "%s.cross.o: %s\n", obj, source->filename );
1754 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1755 output_filenames( includes );
1756 output_filenames( define_args );
1757 output_filenames( extradefs );
1758 output_filename( "-DWINE_CROSSTEST" );
1759 output_filenames( cpp_flags );
1760 output_filename( "$(CFLAGS)" );
1761 output( "\n" );
1763 if (testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1765 strarray_add( &ok_files, strmake( "%s.ok", obj ));
1766 output( "%s.ok:\n", obj );
1767 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1768 top_dir_path( "tools/runtest" ), top_obj_dir,
1769 testdll, replace_extension( testdll, ".dll", "_test.exe" ), dll_ext, obj );
1771 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1772 strarray_add( &c2man_files, source->filename );
1773 output( "%s.o", obj );
1774 if (crosstarget && need_cross) output( " %s.cross.o", obj );
1775 output( ":" );
1777 free( obj );
1779 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
1780 output( "\n" );
1783 /* rules for files that depend on multiple sources */
1785 if (po_files.count)
1787 output( "rsrc.pot: %s", tools_path( "wrc" ) );
1788 output_filenames( po_files );
1789 output( "\n" );
1790 output( "\t%s -O pot -o $@", tools_path( "wrc" ));
1791 output_filenames( po_files );
1792 if (is_win16) output_filename( "-m16" );
1793 else output_filenames( target_flags );
1794 output_filename( "--nostdinc" );
1795 output_filenames( includes );
1796 output_filenames( define_args );
1797 output( "\n" );
1798 strarray_add( &clean_files, "rsrc.pot" );
1801 if (mc_files.count)
1803 output( "msg.pot: %s", tools_path( "wmc" ));
1804 output_filenames( mc_files );
1805 output( "\n" );
1806 output( "\t%s -O pot -o $@", tools_path( "wmc" ));
1807 output_filenames( mc_files );
1808 output( "\n" );
1809 strarray_add( &clean_files, "msg.pot" );
1812 if (dlldata_files.count)
1814 output( "dlldata.c: %s %s\n", tools_path( "widl" ), src_dir_path( "Makefile.in" ));
1815 output( "\t%s --dlldata-only -o $@", tools_path( "widl" ));
1816 output_filenames( dlldata_files );
1817 output( "\n" );
1820 if (module && !staticlib)
1822 char *importlib = get_expanded_make_variable( "IMPORTLIB" );
1823 struct strarray all_libs = empty_strarray;
1824 char *spec_file = NULL;
1826 if (!appmode.count) spec_file = src_dir_path( replace_extension( module, ".dll", ".spec" ));
1827 for (i = 0; i < delayimports.count; i++)
1828 strarray_add( &all_libs, strmake( "-l%s", delayimports.str[i] ));
1829 for (i = 0; i < imports.count; i++)
1830 strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1831 for (i = 0; i < delayimports.count; i++)
1832 strarray_add( &all_libs, strmake( "-Wb,-d%s", delayimports.str[i] ));
1833 strarray_add( &all_libs, "-lwine" );
1834 strarray_add( &all_libs, top_obj_dir_path( "libs/port/libwine_port.a" ));
1835 strarray_addall( &all_libs, get_expanded_make_var_array( "EXTRALIBS" ));
1836 strarray_addall( &all_libs, libs );
1838 if (*dll_ext)
1840 strarray_add( &all_targets, strmake( "%s%s", module, dll_ext ));
1841 strarray_add( &all_targets, strmake( "%s.fake", module ));
1842 output( "%s%s %s.fake:", module, dll_ext, module );
1844 else
1846 strarray_add( &all_targets, module );
1847 output( "%s:", module );
1849 if (spec_file) output_filename( spec_file );
1850 output_filenames( object_files );
1851 output_filenames( res_files );
1852 output( "\n" );
1853 output( "\t%s -o $@", tools_path( "winegcc" ));
1854 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1855 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1856 output_filenames( target_flags );
1857 output_filenames( unwind_flags );
1858 if (spec_file)
1860 output( " -shared %s", spec_file );
1861 output_filenames( extradllflags );
1863 else output_filenames( appmode );
1864 output_filenames( object_files );
1865 output_filenames( res_files );
1866 output_filenames( all_libs );
1867 output_filename( "$(LDFLAGS)" );
1868 output( "\n" );
1870 if (spec_file && importlib)
1872 if (*dll_ext)
1874 strarray_add( &clean_files, strmake( "lib%s.def", importlib ));
1875 output( "lib%s.def: %s %s\n", importlib, tools_path( "winebuild" ), spec_file );
1876 output( "\t%s -w --def -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1877 output_filenames( target_flags );
1878 if (is_win16) output_filename( "-m16" );
1879 output( "\n" );
1880 if (implib_objs.count)
1882 strarray_add( &clean_files, strmake( "lib%s.def.a", importlib ));
1883 output( "lib%s.def.a:", importlib );
1884 output_filenames( implib_objs );
1885 output( "\n" );
1886 output( "\t$(RM) $@\n" );
1887 output( "\t$(AR) $(ARFLAGS) $@" );
1888 output_filenames( implib_objs );
1889 output( "\n" );
1890 output( "\t$(RANLIB) $@\n" );
1893 else
1895 strarray_add( &clean_files, strmake( "lib%s.a", importlib ));
1896 output( "lib%s.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1897 output_filenames( implib_objs );
1898 output( "\n" );
1899 output( "\t%s -w --implib -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1900 output_filenames( target_flags );
1901 output_filenames( implib_objs );
1902 output( "\n" );
1904 if (crosstarget && !is_win16)
1906 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
1907 strarray_add( &clean_files, strmake( "lib%s.cross.a", importlib ));
1908 output( "lib%s.cross.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1909 output_filenames( cross_files );
1910 output( "\n" );
1911 output( "\t%s -b %s -w --implib -o $@ --export %s",
1912 tools_path( "winebuild" ), crosstarget, spec_file );
1913 output_filenames( cross_files );
1914 output( "\n" );
1918 if (spec_file)
1920 if (c2man_files.count)
1922 output( "manpages::\n" );
1923 output( "\t%s -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1924 output_filename( strmake( "-R%s", top_dir_path( "" )));
1925 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1926 output_filename( strmake( "-o %s/man%s", top_obj_dir_path( "documentation" ), man_ext ));
1927 output_filenames( c2man_files );
1928 output( "\n" );
1929 output( "htmlpages::\n" );
1930 output( "\t%s -Th -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1931 output_filename( strmake( "-R%s", top_dir_path( "" )));
1932 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1933 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/html" )));
1934 output_filenames( c2man_files );
1935 output( "\n" );
1936 output( "sgmlpages::\n" );
1937 output( "\t%s -Ts -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1938 output_filename( strmake( "-R%s", top_dir_path( "" )));
1939 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1940 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/api-guide" )));
1941 output_filenames( c2man_files );
1942 output( "\n" );
1943 output( "xmlpages::\n" );
1944 output( "\t%s -Tx -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1945 output_filename( strmake( "-R%s", top_dir_path( "" )));
1946 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1947 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/api-guide-xml" )));
1948 output_filenames( c2man_files );
1949 output( "\n" );
1950 strarray_add( &phony_targets, "manpages" );
1951 strarray_add( &phony_targets, "htmlpages" );
1952 strarray_add( &phony_targets, "sgmlpages" );
1953 strarray_add( &phony_targets, "xmlpages" );
1955 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1959 if (staticlib)
1961 strarray_add( &all_targets, staticlib );
1962 output( "%s:", staticlib );
1963 output_filenames( object_files );
1964 output( "\n\t$(RM) $@\n" );
1965 output( "\t$(AR) $(ARFLAGS) $@" );
1966 output_filenames( object_files );
1967 output( "\n\t$(RANLIB) $@\n" );
1968 if (crosstarget && module)
1970 char *name = replace_extension( staticlib, ".a", ".cross.a" );
1972 strarray_add( &all_targets, name );
1973 output( "%s:", name );
1974 output_filenames( crossobj_files );
1975 output( "\n\t$(RM) $@\n" );
1976 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
1977 output_filenames( crossobj_files );
1978 output( "\n\t%s-ranlib $@\n", crosstarget );
1982 if (testdll)
1984 char *testmodule = replace_extension( testdll, ".dll", "_test.exe" );
1985 char *stripped = replace_extension( testdll, ".dll", "_test-stripped.exe" );
1986 struct strarray all_libs = empty_strarray;
1988 for (i = 0; i < imports.count; i++) strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1989 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1991 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
1992 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
1993 output( "%s%s:\n", testmodule, dll_ext );
1994 output( "\t%s -o $@", tools_path( "winegcc" ));
1995 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1996 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1997 output_filenames( target_flags );
1998 output_filenames( unwind_flags );
1999 output_filenames( appmode );
2000 output_filenames( object_files );
2001 output_filenames( res_files );
2002 output_filenames( all_libs );
2003 output_filename( "$(LDFLAGS)" );
2004 output( "\n" );
2005 output( "%s%s:\n", stripped, dll_ext );
2006 output( "\t%s -o $@", tools_path( "winegcc" ));
2007 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
2008 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
2009 output_filenames( target_flags );
2010 output_filenames( unwind_flags );
2011 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2012 output_filenames( appmode );
2013 output_filenames( object_files );
2014 output_filenames( res_files );
2015 output_filenames( all_libs );
2016 output_filename( "$(LDFLAGS)" );
2017 output( "\n" );
2018 output( "%s%s %s%s:", testmodule, dll_ext, stripped, dll_ext );
2019 output_filenames( object_files );
2020 output_filenames( res_files );
2021 output( "\n" );
2023 if (top_obj_dir)
2025 char *testres = replace_extension( testdll, ".dll", "_test.res" );
2026 output( "all: %s/%s\n", top_obj_dir_path( "programs/winetest" ), testres );
2027 output( "%s/%s: %s%s\n", top_obj_dir_path( "programs/winetest" ), testres, stripped, dll_ext );
2028 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2029 testmodule, stripped, dll_ext, tools_path( "wrc" ));
2032 if (crosstarget)
2034 char *crosstest = replace_extension( testdll, ".dll", "_crosstest.exe" );
2036 strarray_add( &clean_files, crosstest );
2037 output( "crosstest: %s\n", crosstest );
2038 output( "%s:", crosstest );
2039 output_filenames( crossobj_files );
2040 output_filenames( res_files );
2041 output( "\n" );
2042 output( "\t%s -o $@ -b %s", tools_path( "winegcc" ), crosstarget );
2043 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
2044 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
2045 output_filename( "--lib-suffix=.cross.a" );
2046 output_filenames( crossobj_files );
2047 output_filenames( res_files );
2048 output_filenames( all_libs );
2049 output_filename( "$(LDFLAGS)" );
2050 output( "\n" );
2051 strarray_add( &phony_targets, "crosstest" );
2054 output_filenames( ok_files );
2055 output( ": %s%s ../%s%s\n", testmodule, dll_ext, testdll, dll_ext );
2056 output( "check test:" );
2057 output_filenames( ok_files );
2058 output( "\n" );
2059 output( "testclean::\n" );
2060 output( "\t$(RM)" );
2061 output_filenames( ok_files );
2062 output( "\n" );
2063 strarray_addall( &clean_files, ok_files );
2064 strarray_add( &phony_targets, "check" );
2065 strarray_add( &phony_targets, "test" );
2066 strarray_add( &phony_targets, "testclean" );
2067 testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2070 if (all_targets.count)
2072 output( "all:" );
2073 output_filenames( all_targets );
2074 output( "\n" );
2077 strarray_addall( &clean_files, object_files );
2078 strarray_addall( &clean_files, crossobj_files );
2079 strarray_addall( &clean_files, res_files );
2080 strarray_addall( &clean_files, all_targets );
2081 strarray_addall( &clean_files, get_expanded_make_var_array( "EXTRA_TARGETS" ));
2083 if (clean_files.count)
2085 output( "clean::\n" );
2086 output( "\t$(RM)" );
2087 output_filenames( clean_files );
2088 output( "\n" );
2089 strarray_add( &phony_targets, "clean" );
2092 if (top_obj_dir)
2094 output( "depend:\n" );
2095 output( "\t@cd %s && $(MAKE) %s\n", top_obj_dir, base_dir_path( "depend" ));
2096 strarray_add( &phony_targets, "depend" );
2099 if (phony_targets.count)
2101 output( ".PHONY:" );
2102 output_filenames( phony_targets );
2103 output( "\n" );
2106 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2108 return clean_files;
2112 /*******************************************************************
2113 * create_temp_file
2115 static FILE *create_temp_file( const char *orig )
2117 char *name = xmalloc( strlen(orig) + 13 );
2118 unsigned int i, id = getpid();
2119 int fd;
2120 FILE *ret = NULL;
2122 for (i = 0; i < 100; i++)
2124 sprintf( name, "%s.tmp%08x", orig, id );
2125 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2127 ret = fdopen( fd, "w" );
2128 break;
2130 if (errno != EEXIST) break;
2131 id += 7777;
2133 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2134 temp_file_name = name;
2135 return ret;
2139 /*******************************************************************
2140 * rename_temp_file
2142 static void rename_temp_file( const char *dest )
2144 int ret = rename( temp_file_name, dest );
2145 if (ret == -1 && errno == EEXIST)
2147 /* rename doesn't overwrite on windows */
2148 unlink( dest );
2149 ret = rename( temp_file_name, dest );
2151 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2152 temp_file_name = NULL;
2156 /*******************************************************************
2157 * are_files_identical
2159 static int are_files_identical( FILE *file1, FILE *file2 )
2161 for (;;)
2163 char buffer1[8192], buffer2[8192];
2164 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2165 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2166 if (size1 != size2) return 0;
2167 if (!size1) return feof( file1 ) && feof( file2 );
2168 if (memcmp( buffer1, buffer2, size1 )) return 0;
2173 /*******************************************************************
2174 * rename_temp_file_if_changed
2176 static void rename_temp_file_if_changed( const char *dest )
2178 FILE *file1, *file2;
2179 int do_rename = 1;
2181 if ((file1 = fopen( dest, "r" )))
2183 if ((file2 = fopen( temp_file_name, "r" )))
2185 do_rename = !are_files_identical( file1, file2 );
2186 fclose( file2 );
2188 fclose( file1 );
2190 if (!do_rename)
2192 unlink( temp_file_name );
2193 temp_file_name = NULL;
2195 else rename_temp_file( dest );
2199 /*******************************************************************
2200 * output_testlist
2202 static void output_testlist( const char *dest, struct strarray files )
2204 int i;
2206 output_file = create_temp_file( dest );
2208 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2209 output( "#define WIN32_LEAN_AND_MEAN\n" );
2210 output( "#include <windows.h>\n\n" );
2211 output( "#define STANDALONE\n" );
2212 output( "#include \"wine/test.h\"\n\n" );
2214 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2215 output( "\n" );
2216 output( "const struct test winetest_testlist[] =\n" );
2217 output( "{\n" );
2218 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2219 output( " { 0, 0 }\n" );
2220 output( "};\n" );
2222 if (fclose( output_file )) fatal_perror( "write" );
2223 output_file = NULL;
2224 rename_temp_file_if_changed( dest );
2228 /*******************************************************************
2229 * output_gitignore
2231 static void output_gitignore( const char *dest, struct strarray files )
2233 int i;
2235 output_file = create_temp_file( dest );
2237 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2238 for (i = 0; i < files.count; i++)
2240 if (!strchr( files.str[i], '/' )) output( "/" );
2241 output( "%s\n", files.str[i] );
2244 if (fclose( output_file )) fatal_perror( "write" );
2245 output_file = NULL;
2246 rename_temp_file( dest );
2250 /*******************************************************************
2251 * output_dependencies
2253 static void output_dependencies( const char *path )
2255 struct strarray targets, ignore_files = empty_strarray;
2257 if (Separator && ((output_file = fopen( path, "r" ))))
2259 char buffer[1024];
2260 FILE *tmp_file = create_temp_file( path );
2261 int found = 0;
2263 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2265 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2266 found = !strncmp( buffer, Separator, strlen(Separator) );
2268 if (fclose( output_file )) fatal_perror( "write" );
2269 output_file = tmp_file;
2270 if (!found) output( "\n%s\n", Separator );
2272 else
2274 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2275 fatal_perror( "%s", path );
2278 testlist_files = empty_strarray;
2279 targets = output_sources();
2281 fclose( output_file );
2282 output_file = NULL;
2283 if (temp_file_name) rename_temp_file( path );
2285 strarray_add( &ignore_files, ".gitignore" );
2286 strarray_add( &ignore_files, "Makefile" );
2287 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2288 strarray_addall( &ignore_files, targets );
2290 if (testlist_files.count) output_testlist( base_dir_path( "testlist.c" ), testlist_files );
2291 if (!src_dir && base_dir) output_gitignore( base_dir_path( ".gitignore" ), ignore_files );
2295 /*******************************************************************
2296 * update_makefile
2298 static void update_makefile( const char *path )
2300 static const char *source_vars[] =
2302 "C_SRCS",
2303 "OBJC_SRCS",
2304 "RC_SRCS",
2305 "MC_SRCS",
2306 "IDL_SRCS",
2307 "BISON_SRCS",
2308 "LEX_SRCS",
2309 "XTEMPLATE_SRCS",
2310 "SVG_SRCS",
2311 "FONT_SRCS",
2312 "IN_SRCS",
2313 "MANPAGES",
2314 NULL
2316 const char **var;
2317 unsigned int i;
2318 struct strarray value;
2319 struct incl_file *file;
2321 base_dir = path;
2322 if (!strcmp( base_dir, "." )) base_dir = NULL;
2323 output_file_name = base_dir_path( makefile_name );
2324 parse_makefile( output_file_name, Separator, &make_vars );
2326 src_dir = get_expanded_make_variable( "srcdir" );
2327 top_src_dir = get_expanded_make_variable( "top_srcdir" );
2328 top_obj_dir = get_expanded_make_variable( "top_builddir" );
2329 parent_dir = get_expanded_make_variable( "PARENTSRC" );
2331 /* ignore redundant source paths */
2332 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
2333 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
2334 if (top_obj_dir && !strcmp( top_obj_dir, "." )) top_obj_dir = NULL;
2336 appmode = get_expanded_make_var_array( "APPMODE" );
2337 imports = get_expanded_make_var_array( "IMPORTS" );
2339 use_msvcrt = 0;
2340 for (i = 0; i < appmode.count && !use_msvcrt; i++)
2341 use_msvcrt = !strcmp( appmode.str[i], "-mno-cygwin" );
2342 for (i = 0; i < imports.count && !use_msvcrt; i++)
2343 use_msvcrt = !strncmp( imports.str[i], "msvcr", 5 );
2345 include_args = empty_strarray;
2346 define_args = empty_strarray;
2347 strarray_add( &define_args, "-D__WINESRC__" );
2349 value = get_expanded_make_var_array( "EXTRAINCL" );
2350 for (i = 0; i < value.count; i++)
2351 if (!strncmp( value.str[i], "-I", 2 ))
2352 strarray_add_uniq( &include_args, value.str[i] );
2353 else
2354 strarray_add_uniq( &define_args, value.str[i] );
2355 strarray_addall( &define_args, get_expanded_make_var_array( "EXTRADEFS" ));
2357 list_init( &sources );
2358 list_init( &includes );
2360 for (var = source_vars; *var; var++)
2362 value = get_expanded_make_var_array( *var );
2363 for (i = 0; i < value.count; i++) add_src_file( value.str[i] );
2366 add_generated_sources();
2368 value = get_expanded_make_var_array( "EXTRA_OBJS" );
2369 for (i = 0; i < value.count; i++)
2371 /* default to .c for unknown extra object files */
2372 if (strendswith( value.str[i], ".o" ))
2373 add_generated_source( value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2374 else
2375 add_generated_source( value.str[i], NULL );
2378 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( file, 0 );
2379 output_dependencies( output_file_name );
2380 output_file_name = NULL;
2384 /*******************************************************************
2385 * parse_makeflags
2387 static void parse_makeflags( const char *flags )
2389 const char *p = flags;
2390 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2392 while (*p)
2394 while (isspace(*p)) p++;
2395 var = buffer;
2396 while (*p && !isspace(*p))
2398 if (*p == '\\' && p[1]) p++;
2399 *var++ = *p++;
2401 *var = 0;
2402 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2407 /*******************************************************************
2408 * parse_option
2410 static int parse_option( const char *opt )
2412 if (opt[0] != '-')
2414 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2415 return 0;
2417 switch(opt[1])
2419 case 'f':
2420 if (opt[2]) makefile_name = opt + 2;
2421 break;
2422 case 'R':
2423 relative_dir_mode = 1;
2424 break;
2425 case 's':
2426 if (opt[2]) Separator = opt + 2;
2427 else Separator = NULL;
2428 break;
2429 default:
2430 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2431 exit(1);
2433 return 1;
2437 /*******************************************************************
2438 * main
2440 int main( int argc, char *argv[] )
2442 const char *makeflags = getenv( "MAKEFLAGS" );
2443 int i, j;
2445 if (makeflags) parse_makeflags( makeflags );
2447 i = 1;
2448 while (i < argc)
2450 if (parse_option( argv[i] ))
2452 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2453 argc--;
2455 else i++;
2458 if (relative_dir_mode)
2460 char *relpath;
2462 if (argc != 3)
2464 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2465 exit( 1 );
2467 relpath = get_relative_path( argv[1], argv[2] );
2468 printf( "%s\n", relpath ? relpath : "." );
2469 exit( 0 );
2472 if (argc <= 1)
2474 fprintf( stderr, "%s", Usage );
2475 exit( 1 );
2477 atexit( cleanup_files );
2478 signal( SIGTERM, exit_on_signal );
2479 signal( SIGINT, exit_on_signal );
2480 #ifdef SIGHUP
2481 signal( SIGHUP, exit_on_signal );
2482 #endif
2484 parse_makefile( makefile_name, "# End of common header", &top_make_vars );
2486 linguas = get_expanded_make_var_array( "LINGUAS" );
2487 target_flags = get_expanded_make_var_array( "TARGETFLAGS" );
2488 msvcrt_flags = get_expanded_make_var_array( "MSVCRTFLAGS" );
2489 dll_flags = get_expanded_make_var_array( "DLLFLAGS" );
2490 extra_cflags = get_expanded_make_var_array( "EXTRACFLAGS" );
2491 cpp_flags = get_expanded_make_var_array( "CPPFLAGS" );
2492 unwind_flags = get_expanded_make_var_array( "UNWINDFLAGS" );
2493 libs = get_expanded_make_var_array( "LIBS" );
2495 root_src_dir = get_expanded_make_variable( "srcdir" );
2496 tools_dir = get_expanded_make_variable( "TOOLSDIR" );
2497 tools_ext = get_expanded_make_variable( "TOOLSEXT" );
2498 exe_ext = get_expanded_make_variable( "EXEEXT" );
2499 man_ext = get_expanded_make_variable( "api_manext" );
2500 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2501 dll_prefix = get_expanded_make_variable( "DLLPREFIX" );
2502 crosstarget = get_expanded_make_variable( "CROSSTARGET" );
2503 fontforge = get_expanded_make_variable( "FONTFORGE" );
2504 convert = get_expanded_make_variable( "CONVERT" );
2505 rsvg = get_expanded_make_variable( "RSVG" );
2506 icotool = get_expanded_make_variable( "ICOTOOL" );
2508 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2509 if (!tools_ext) tools_ext = "";
2510 if (!dll_prefix) dll_prefix = "";
2511 if (!man_ext) man_ext = "3w";
2513 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2514 return 0;