wbemprox: Implement more properties of Win32_OperatingSystem.
[wine/wine-gecko.git] / tools / makedep.c
blob73bf3f04d2f79d3016bfa35791961e74df09cdd4
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 * strarray_get_value
350 * Find a value in a name/value pair string array.
352 static char *strarray_get_value( const struct strarray *array, const char *name )
354 unsigned int i;
356 for (i = 0; i < array->count; i += 2)
357 if (!strcmp( array->str[i], name )) return xstrdup( array->str[i + 1] );
358 return NULL;
362 /*******************************************************************
363 * strarray_set_value
365 * Define a value in a name/value pair string array.
367 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
369 unsigned int i;
371 /* redefining a variable replaces the previous value */
372 for (i = 0; i < array->count; i += 2)
374 if (strcmp( array->str[i], name )) continue;
375 array->str[i + 1] = value;
376 return;
378 strarray_add( array, name );
379 strarray_add( array, value );
383 /*******************************************************************
384 * output_filename
386 static void output_filename( const char *name )
388 if (output_column + strlen(name) + 1 > 100)
390 output( " \\\n" );
391 output( " " );
393 else if (output_column) output( " " );
394 output( "%s", name );
398 /*******************************************************************
399 * output_filenames
401 static void output_filenames( struct strarray array )
403 unsigned int i;
405 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
409 /*******************************************************************
410 * get_extension
412 static char *get_extension( char *filename )
414 char *ext = strrchr( filename, '.' );
415 if (ext && strchr( ext, '/' )) ext = NULL;
416 return ext;
420 /*******************************************************************
421 * replace_extension
423 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
425 char *ret;
426 int name_len = strlen( name );
427 int ext_len = strlen( old_ext );
429 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
430 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
431 memcpy( ret, name, name_len );
432 strcpy( ret + name_len, new_ext );
433 return ret;
437 /*******************************************************************
438 * strarray_replace_extension
440 static struct strarray strarray_replace_extension( const struct strarray *array,
441 const char *old_ext, const char *new_ext )
443 unsigned int i;
444 struct strarray ret;
446 ret.count = ret.size = array->count;
447 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
448 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
449 return ret;
453 /*******************************************************************
454 * replace_substr
456 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
458 unsigned int pos = start - str;
459 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
460 memcpy( ret, str, pos );
461 strcpy( ret + pos, replace );
462 strcat( ret + pos, start + len );
463 return ret;
467 /*******************************************************************
468 * get_relative_path
470 * Determine where the destination path is located relative to the 'from' path.
472 static char *get_relative_path( const char *from, const char *dest )
474 const char *start;
475 char *ret, *p;
476 unsigned int dotdots = 0;
478 /* a path of "." is equivalent to an empty path */
479 if (!strcmp( from, "." )) from = "";
481 for (;;)
483 while (*from == '/') from++;
484 while (*dest == '/') dest++;
485 start = dest; /* save start of next path element */
486 if (!*from) break;
488 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
489 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
491 /* count remaining elements in 'from' */
494 dotdots++;
495 while (*from && *from != '/') from++;
496 while (*from == '/') from++;
498 while (*from);
499 break;
502 if (!start[0] && !dotdots) return NULL; /* empty path */
504 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
505 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
507 if (start[0]) strcpy( p, start );
508 else p[-1] = 0; /* remove trailing slash */
509 return ret;
513 /*******************************************************************
514 * concat_paths
516 static char *concat_paths( const char *base, const char *path )
518 if (!base) return xstrdup( path[0] ? path : "." );
519 if (path[0] == '/') return xstrdup( path );
520 if (!path[0]) return xstrdup( base );
521 return strmake( "%s/%s", base, path );
525 /*******************************************************************
526 * base_dir_path
528 static char *base_dir_path( const char *path )
530 return concat_paths( base_dir, path );
534 /*******************************************************************
535 * src_dir_path
537 static char *src_dir_path( const char *path )
539 return concat_paths( src_dir, path );
543 /*******************************************************************
544 * top_obj_dir_path
546 static char *top_obj_dir_path( const char *path )
548 return concat_paths( top_obj_dir, path );
552 /*******************************************************************
553 * top_dir_path
555 static char *top_dir_path( const char *path )
557 if (top_src_dir) return concat_paths( top_src_dir, path );
558 return top_obj_dir_path( path );
562 /*******************************************************************
563 * tools_dir_path
565 static char *tools_dir_path( const char *path )
567 if (tools_dir) return top_obj_dir_path( strmake( "%s/tools/%s", tools_dir, path ));
568 return top_obj_dir_path( strmake( "tools/%s", path ));
572 /*******************************************************************
573 * tools_path
575 static char *tools_path( const char *name )
577 return strmake( "%s/%s%s", tools_dir_path( name ), name, tools_ext );
581 /*******************************************************************
582 * get_line
584 static char *get_line( FILE *file )
586 static char *buffer;
587 static unsigned int size;
589 if (!size)
591 size = 1024;
592 buffer = xmalloc( size );
594 if (!fgets( buffer, size, file )) return NULL;
595 input_line++;
597 for (;;)
599 char *p = buffer + strlen(buffer);
600 /* if line is larger than buffer, resize buffer */
601 while (p == buffer + size - 1 && p[-1] != '\n')
603 buffer = xrealloc( buffer, size * 2 );
604 if (!fgets( buffer + size - 1, size + 1, file )) break;
605 p = buffer + strlen(buffer);
606 size *= 2;
608 if (p > buffer && p[-1] == '\n')
610 *(--p) = 0;
611 if (p > buffer && p[-1] == '\r') *(--p) = 0;
612 if (p > buffer && p[-1] == '\\')
614 *(--p) = 0;
615 /* line ends in backslash, read continuation line */
616 if (!fgets( p, size - (p - buffer), file )) return buffer;
617 input_line++;
618 continue;
621 return buffer;
625 /*******************************************************************
626 * find_src_file
628 static struct incl_file *find_src_file( const char *name )
630 struct incl_file *file;
632 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
633 if (!strcmp( name, file->name )) return file;
634 return NULL;
637 /*******************************************************************
638 * find_include_file
640 static struct incl_file *find_include_file( const char *name )
642 struct incl_file *file;
644 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
645 if (!strcmp( name, file->name )) return file;
646 return NULL;
649 /*******************************************************************
650 * add_include
652 * Add an include file if it doesn't already exists.
654 static struct incl_file *add_include( struct incl_file *parent, const char *name, int system )
656 struct incl_file *include;
657 char *ext;
659 if (parent->files_count >= parent->files_size)
661 parent->files_size *= 2;
662 if (parent->files_size < 16) parent->files_size = 16;
663 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
666 /* enforce some rules for the Wine tree */
668 if (!memcmp( name, "../", 3 ))
669 fatal_error( "#include directive with relative path not allowed\n" );
671 if (!strcmp( name, "config.h" ))
673 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
674 fatal_error( "config.h must not be included by a header file\n" );
675 if (parent->files_count)
676 fatal_error( "config.h must be included before anything else\n" );
678 else if (!strcmp( name, "wine/port.h" ))
680 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
681 fatal_error( "wine/port.h must not be included by a header file\n" );
682 if (!parent->files_count) fatal_error( "config.h must be included before wine/port.h\n" );
683 if (parent->files_count > 1)
684 fatal_error( "wine/port.h must be included before everything except config.h\n" );
685 if (strcmp( parent->files[0]->name, "config.h" ))
686 fatal_error( "config.h must be included before wine/port.h\n" );
689 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
690 if (!strcmp( name, include->name )) goto found;
692 include = xmalloc( sizeof(*include) );
693 memset( include, 0, sizeof(*include) );
694 include->name = xstrdup(name);
695 include->included_by = parent;
696 include->included_line = input_line;
697 if (system) include->flags |= FLAG_SYSTEM;
698 list_add_tail( &includes, &include->entry );
699 found:
700 parent->files[parent->files_count++] = include;
701 return include;
705 /*******************************************************************
706 * add_generated_source
708 * Add a generated source file to the list.
710 static struct incl_file *add_generated_source( const char *name, const char *filename )
712 struct incl_file *file;
714 if ((file = find_src_file( name ))) return file; /* we already have it */
715 file = xmalloc( sizeof(*file) );
716 memset( file, 0, sizeof(*file) );
717 file->name = xstrdup( name );
718 file->filename = xstrdup( filename ? filename : name );
719 file->flags = FLAG_GENERATED;
720 list_add_tail( &sources, &file->entry );
721 return file;
725 /*******************************************************************
726 * open_file
728 static FILE *open_file( const char *path )
730 return fopen( base_dir_path( path ), "r" );
733 /*******************************************************************
734 * open_src_file
736 static FILE *open_src_file( struct incl_file *pFile )
738 FILE *file;
740 /* try in source dir */
741 pFile->filename = src_dir_path( pFile->name );
742 file = open_file( pFile->filename );
744 /* now try parent dir */
745 if (!file && parent_dir)
747 pFile->filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
748 file = open_file( pFile->filename );
750 if (!file) fatal_perror( "open %s", pFile->name );
751 return file;
755 /*******************************************************************
756 * open_include_file
758 static FILE *open_include_file( struct incl_file *pFile )
760 FILE *file = NULL;
761 char *filename, *p;
762 unsigned int i, len;
764 errno = ENOENT;
766 /* check for generated bison header */
768 if (strendswith( pFile->name, ".tab.h" ))
770 filename = src_dir_path( replace_extension( pFile->name, ".tab.h", ".y" ));
771 if ((file = open_file( filename )))
773 pFile->sourcename = filename;
774 pFile->filename = xstrdup( pFile->name );
775 /* don't bother to parse it */
776 fclose( file );
777 return NULL;
779 free( filename );
782 /* check for corresponding idl file in source dir */
784 if (strendswith( pFile->name, ".h" ))
786 filename = src_dir_path( replace_extension( pFile->name, ".h", ".idl" ));
787 if ((file = open_file( filename )))
789 pFile->sourcename = filename;
790 pFile->filename = xstrdup( pFile->name );
791 return file;
793 free( filename );
796 /* now try in source dir */
797 filename = src_dir_path( pFile->name );
798 if ((file = open_file( filename ))) goto found;
799 free( filename );
801 /* now try in parent source dir */
802 if (parent_dir)
804 filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
805 if ((file = open_file( filename ))) goto found;
806 free( filename );
809 /* check for corresponding idl file in global includes */
811 if (strendswith( pFile->name, ".h" ))
813 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".idl" )));
814 if ((file = open_file( filename )))
816 pFile->sourcename = filename;
817 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
818 return file;
820 free( filename );
823 /* check for corresponding .in file in global includes (for config.h.in) */
825 if (strendswith( pFile->name, ".h" ))
827 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".h.in" )));
828 if ((file = open_file( filename )))
830 pFile->sourcename = filename;
831 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
832 return file;
834 free( filename );
837 /* check for corresponding .x file in global includes */
839 if (strendswith( pFile->name, "tmpl.h" ))
841 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".x" )));
842 if ((file = open_file( filename )))
844 pFile->sourcename = filename;
845 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
846 return file;
848 free( filename );
851 /* check in global includes source dir */
853 filename = top_dir_path( strmake( "include/%s", pFile->name ));
854 if ((file = open_file( filename ))) goto found;
856 /* check in global msvcrt includes */
857 if (use_msvcrt)
859 filename = top_dir_path( strmake( "include/msvcrt/%s", pFile->name ));
860 if ((file = open_file( filename ))) goto found;
863 /* now search in include paths */
864 for (i = 0; i < include_args.count; i++)
866 const char *dir = include_args.str[i] + 2; /* skip -I */
867 if (*dir == '/')
869 /* ignore absolute paths that don't point into the source dir */
870 if (!top_src_dir) continue;
871 len = strlen( top_src_dir );
872 if (strncmp( dir, top_src_dir, len )) continue;
873 if (dir[len] && dir[len] != '/') continue;
875 filename = strmake( "%s/%s", dir, pFile->name );
876 if ((file = open_file( filename ))) goto found;
877 free( filename );
879 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
881 /* try in src file directory */
882 if ((p = strrchr(pFile->included_by->filename, '/')))
884 int l = p - pFile->included_by->filename + 1;
885 filename = xmalloc(l + strlen(pFile->name) + 1);
886 memcpy( filename, pFile->included_by->filename, l );
887 strcpy( filename + l, pFile->name );
888 if ((file = open_file( filename ))) goto found;
889 free( filename );
892 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
893 perror( pFile->name );
894 pFile = pFile->included_by;
895 while (pFile && pFile->included_by)
897 const char *parent = pFile->included_by->sourcename;
898 if (!parent) parent = pFile->included_by->name;
899 fprintf( stderr, "%s:%d: note: %s was first included here\n",
900 parent, pFile->included_line, pFile->name );
901 pFile = pFile->included_by;
903 exit(1);
905 found:
906 pFile->filename = filename;
907 return file;
911 /*******************************************************************
912 * parse_include_directive
914 static void parse_include_directive( struct incl_file *source, char *str )
916 char quote, *include, *p = str;
918 while (*p && isspace(*p)) p++;
919 if (*p != '\"' && *p != '<' ) return;
920 quote = *p++;
921 if (quote == '<') quote = '>';
922 include = p;
923 while (*p && (*p != quote)) p++;
924 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
925 *p = 0;
926 add_include( source, include, (quote == '>') );
930 /*******************************************************************
931 * parse_pragma_directive
933 static void parse_pragma_directive( struct incl_file *source, char *str )
935 char *flag, *p = str;
937 if (!isspace( *p )) return;
938 while (*p && isspace(*p)) p++;
939 p = strtok( p, " \t" );
940 if (strcmp( p, "makedep" )) return;
942 while ((flag = strtok( NULL, " \t" )))
944 if (!strcmp( flag, "depend" ))
946 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
947 return;
949 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
951 if (strendswith( source->name, ".idl" ))
953 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
954 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
955 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
956 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
957 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
958 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
959 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
960 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
962 else if (strendswith( source->name, ".rc" ))
964 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
966 else if (strendswith( source->name, ".sfd" ))
968 if (!strcmp( flag, "font" ))
970 struct strarray *array = source->args;
972 if (!array)
974 source->args = array = xmalloc( sizeof(*array) );
975 *array = empty_strarray;
976 source->flags |= FLAG_SFD_FONTS;
978 strarray_add( array, xstrdup( strtok( NULL, "" )));
979 return;
982 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
987 /*******************************************************************
988 * parse_cpp_directive
990 static void parse_cpp_directive( struct incl_file *source, char *str )
992 while (*str && isspace(*str)) str++;
993 if (*str++ != '#') return;
994 while (*str && isspace(*str)) str++;
996 if (!strncmp( str, "include", 7 ))
997 parse_include_directive( source, str + 7 );
998 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
999 parse_include_directive( source, str + 6 );
1000 else if (!strncmp( str, "pragma", 6 ))
1001 parse_pragma_directive( source, str + 6 );
1005 /*******************************************************************
1006 * parse_idl_file
1008 * If for_h_file is non-zero, it means we are not interested in the idl file
1009 * itself, but only in the contents of the .h file that will be generated from it.
1011 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
1013 char *buffer, *include;
1015 input_line = 0;
1016 if (for_h_file)
1018 /* generated .h file always includes these */
1019 add_include( pFile, "rpc.h", 1 );
1020 add_include( pFile, "rpcndr.h", 1 );
1023 while ((buffer = get_line( file )))
1025 char quote;
1026 char *p = buffer;
1027 while (*p && isspace(*p)) p++;
1029 if (!strncmp( p, "import", 6 ))
1031 p += 6;
1032 while (*p && isspace(*p)) p++;
1033 if (*p != '"') continue;
1034 include = ++p;
1035 while (*p && (*p != '"')) p++;
1036 if (!*p) fatal_error( "malformed import directive\n" );
1037 *p = 0;
1038 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
1039 add_include( pFile, include, 0 );
1040 continue;
1043 if (for_h_file) /* only check for #include inside cpp_quote */
1045 if (strncmp( p, "cpp_quote", 9 )) continue;
1046 p += 9;
1047 while (*p && isspace(*p)) p++;
1048 if (*p++ != '(') continue;
1049 while (*p && isspace(*p)) p++;
1050 if (*p++ != '"') continue;
1051 if (*p++ != '#') continue;
1052 while (*p && isspace(*p)) p++;
1053 if (strncmp( p, "include", 7 )) continue;
1054 p += 7;
1055 while (*p && isspace(*p)) p++;
1056 if (*p == '\\' && p[1] == '"')
1058 p += 2;
1059 quote = '"';
1061 else
1063 if (*p++ != '<' ) continue;
1064 quote = '>';
1066 include = p;
1067 while (*p && (*p != quote)) p++;
1068 if (!*p || (quote == '"' && p[-1] != '\\'))
1069 fatal_error( "malformed #include directive inside cpp_quote\n" );
1070 if (quote == '"') p--; /* remove backslash */
1071 *p = 0;
1072 add_include( pFile, include, (quote == '>') );
1073 continue;
1076 parse_cpp_directive( pFile, p );
1080 /*******************************************************************
1081 * parse_c_file
1083 static void parse_c_file( struct incl_file *pFile, FILE *file )
1085 char *buffer;
1087 input_line = 0;
1088 while ((buffer = get_line( file )))
1090 parse_cpp_directive( pFile, buffer );
1095 /*******************************************************************
1096 * parse_rc_file
1098 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1100 char *buffer, *include;
1102 input_line = 0;
1103 while ((buffer = get_line( file )))
1105 char quote;
1106 char *p = buffer;
1107 while (*p && isspace(*p)) p++;
1109 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1111 p += 2;
1112 while (*p && isspace(*p)) p++;
1113 if (strncmp( p, "@makedep:", 9 )) continue;
1114 p += 9;
1115 while (*p && isspace(*p)) p++;
1116 quote = '"';
1117 if (*p == quote)
1119 include = ++p;
1120 while (*p && *p != quote) p++;
1122 else
1124 include = p;
1125 while (*p && !isspace(*p) && *p != '*') p++;
1127 if (!*p)
1128 fatal_error( "malformed makedep comment\n" );
1129 *p = 0;
1130 add_include( pFile, include, (quote == '>') );
1131 continue;
1134 parse_cpp_directive( pFile, buffer );
1139 /*******************************************************************
1140 * parse_in_file
1142 static void parse_in_file( struct incl_file *source, FILE *file )
1144 char *p, *buffer;
1146 /* make sure it gets rebuilt when the version changes */
1147 add_include( source, "config.h", 1 );
1149 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1151 input_line = 0;
1152 while ((buffer = get_line( file )))
1154 if (strncmp( buffer, ".TH", 3 )) continue;
1155 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1156 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1157 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1158 source->args = xstrdup( p );
1159 return;
1164 /*******************************************************************
1165 * parse_sfd_file
1167 static void parse_sfd_file( struct incl_file *source, FILE *file )
1169 char *p, *eol, *buffer;
1171 input_line = 0;
1172 while ((buffer = get_line( file )))
1174 if (strncmp( buffer, "UComments:", 10 )) continue;
1175 p = buffer + 10;
1176 while (*p == ' ') p++;
1177 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1179 p++;
1180 buffer[strlen(buffer) - 1] = 0;
1182 while ((eol = strstr( p, "+AAoA" )))
1184 *eol = 0;
1185 while (*p && isspace(*p)) p++;
1186 if (*p++ == '#')
1188 while (*p && isspace(*p)) p++;
1189 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1191 p = eol + 5;
1193 while (*p && isspace(*p)) p++;
1194 if (*p++ != '#') return;
1195 while (*p && isspace(*p)) p++;
1196 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1197 return;
1202 /*******************************************************************
1203 * parse_file
1205 static void parse_file( struct incl_file *source, int src )
1207 FILE *file;
1209 /* don't try to open certain types of files */
1210 if (strendswith( source->name, ".tlb" ))
1212 source->filename = xstrdup( source->name );
1213 return;
1216 file = src ? open_src_file( source ) : open_include_file( source );
1217 if (!file) return;
1218 input_file_name = source->filename;
1220 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1221 parse_idl_file( source, file, 1 );
1222 else if (strendswith( source->filename, ".idl" ))
1223 parse_idl_file( source, file, 0 );
1224 else if (strendswith( source->filename, ".c" ) ||
1225 strendswith( source->filename, ".m" ) ||
1226 strendswith( source->filename, ".h" ) ||
1227 strendswith( source->filename, ".l" ) ||
1228 strendswith( source->filename, ".y" ))
1229 parse_c_file( source, file );
1230 else if (strendswith( source->filename, ".rc" ))
1231 parse_rc_file( source, file );
1232 else if (strendswith( source->filename, ".in" ))
1233 parse_in_file( source, file );
1234 else if (strendswith( source->filename, ".sfd" ))
1235 parse_sfd_file( source, file );
1236 fclose(file);
1237 input_file_name = NULL;
1241 /*******************************************************************
1242 * add_src_file
1244 * Add a source file to the list.
1246 static struct incl_file *add_src_file( const char *name )
1248 struct incl_file *file;
1250 if ((file = find_src_file( name ))) return file; /* we already have it */
1251 file = xmalloc( sizeof(*file) );
1252 memset( file, 0, sizeof(*file) );
1253 file->name = xstrdup(name);
1254 list_add_tail( &sources, &file->entry );
1255 parse_file( file, 1 );
1256 return file;
1260 /*******************************************************************
1261 * get_make_variable
1263 static char *get_make_variable( const char *name )
1265 char *ret;
1267 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1268 if ((ret = strarray_get_value( &make_vars, name ))) return ret;
1269 if ((ret = strarray_get_value( &top_make_vars, name ))) return ret;
1270 return NULL;
1274 /*******************************************************************
1275 * get_expanded_make_variable
1277 static char *get_expanded_make_variable( const char *name )
1279 char *p, *end, *var, *expand, *tmp;
1281 expand = get_make_variable( name );
1282 if (!expand) return NULL;
1284 p = expand;
1285 while ((p = strchr( p, '$' )))
1287 if (p[1] == '(')
1289 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1290 *end++ = 0;
1291 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1292 var = get_make_variable( p + 2 );
1293 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1294 free( var );
1296 else if (p[1] == '{') /* don't expand ${} variables */
1298 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1299 p = end + 1;
1300 continue;
1302 else if (p[1] == '$')
1304 tmp = replace_substr( expand, p, 2, "$" );
1306 else fatal_error( "syntax error in '%s'\n", expand );
1308 /* switch to the new string */
1309 p = tmp + (p - expand);
1310 free( expand );
1311 expand = tmp;
1314 /* consider empty variables undefined */
1315 p = expand;
1316 while (*p && isspace(*p)) p++;
1317 if (*p) return expand;
1318 free( expand );
1319 return NULL;
1323 /*******************************************************************
1324 * get_expanded_make_var_array
1326 static struct strarray get_expanded_make_var_array( const char *name )
1328 struct strarray ret = empty_strarray;
1329 char *value, *token;
1331 if ((value = get_expanded_make_variable( name )))
1332 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1333 strarray_add( &ret, token );
1334 return ret;
1338 /*******************************************************************
1339 * set_make_variable
1341 static int set_make_variable( struct strarray *array, const char *assignment )
1343 char *p, *name;
1345 p = name = xstrdup( assignment );
1346 while (isalnum(*p) || *p == '_') p++;
1347 if (name == p) return 0; /* not a variable */
1348 if (isspace(*p))
1350 *p++ = 0;
1351 while (isspace(*p)) p++;
1353 if (*p != '=') return 0; /* not an assignment */
1354 *p++ = 0;
1355 while (isspace(*p)) p++;
1357 strarray_set_value( array, name, p );
1358 return 1;
1362 /*******************************************************************
1363 * parse_makefile
1365 static void parse_makefile( const char *name, const char *separator, struct strarray *vars )
1367 char *buffer;
1368 FILE *file;
1370 *vars = empty_strarray;
1371 input_file_name = name;
1372 if (!(file = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1374 input_line = 0;
1375 while ((buffer = get_line( file )))
1377 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1378 if (*buffer == '\t') continue; /* command */
1379 while (isspace( *buffer )) buffer++;
1380 if (*buffer == '#') continue; /* comment */
1381 set_make_variable( vars, buffer );
1383 fclose( file );
1384 input_file_name = NULL;
1388 /*******************************************************************
1389 * add_generated_sources
1391 static void add_generated_sources(void)
1393 struct incl_file *source, *next, *file;
1395 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1397 if (source->flags & FLAG_IDL_CLIENT)
1399 file = add_generated_source( replace_extension( source->name, ".idl", "_c.c" ), NULL );
1400 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1402 if (source->flags & FLAG_IDL_SERVER)
1404 file = add_generated_source( replace_extension( source->name, ".idl", "_s.c" ), NULL );
1405 add_include( file, "wine/exception.h", 0 );
1406 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1408 if (source->flags & FLAG_IDL_IDENT)
1410 file = add_generated_source( replace_extension( source->name, ".idl", "_i.c" ), NULL );
1411 add_include( file, "rpc.h", 0 );
1412 add_include( file, "rpcndr.h", 0 );
1413 add_include( file, "guiddef.h", 0 );
1415 if (source->flags & FLAG_IDL_PROXY)
1417 file = add_generated_source( "dlldata.o", "dlldata.c" );
1418 add_include( file, "objbase.h", 0 );
1419 add_include( file, "rpcproxy.h", 0 );
1420 file = add_generated_source( replace_extension( source->name, ".idl", "_p.c" ), NULL );
1421 add_include( file, "objbase.h", 0 );
1422 add_include( file, "rpcproxy.h", 0 );
1423 add_include( file, "wine/exception.h", 0 );
1424 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1426 if (source->flags & FLAG_IDL_REGTYPELIB)
1428 add_generated_source( replace_extension( source->name, ".idl", "_t.res" ), NULL );
1430 if (source->flags & FLAG_IDL_REGISTER)
1432 add_generated_source( replace_extension( source->name, ".idl", "_r.res" ), NULL );
1434 if (strendswith( source->name, ".y" ))
1436 file = add_generated_source( replace_extension( source->name, ".y", ".tab.c" ), NULL );
1437 /* steal the includes list from the source file */
1438 file->files_count = source->files_count;
1439 file->files_size = source->files_size;
1440 file->files = source->files;
1441 source->files_count = source->files_size = 0;
1442 source->files = NULL;
1444 if (strendswith( source->name, ".l" ))
1446 file = add_generated_source( replace_extension( source->name, ".l", ".yy.c" ), NULL );
1447 /* steal the includes list from the source file */
1448 file->files_count = source->files_count;
1449 file->files_size = source->files_size;
1450 file->files = source->files;
1451 source->files_count = source->files_size = 0;
1452 source->files = NULL;
1455 if (get_make_variable( "TESTDLL" ))
1457 file = add_generated_source( "testlist.o", "testlist.c" );
1458 add_include( file, "wine/test.h", 0 );
1463 /*******************************************************************
1464 * create_dir
1466 static void create_dir( const char *dir )
1468 char *p, *path;
1470 p = path = xstrdup( dir );
1471 while ((p = strchr( p, '/' )))
1473 *p = 0;
1474 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1475 *p++ = '/';
1476 while (*p == '/') p++;
1478 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1479 free( path );
1483 /*******************************************************************
1484 * output_include
1486 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1488 int i;
1490 if (pFile->owner == owner) return;
1491 if (!pFile->filename) return;
1492 pFile->owner = owner;
1493 output_filename( pFile->filename );
1494 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1498 /*******************************************************************
1499 * output_sources
1501 static struct strarray output_sources(void)
1503 struct incl_file *source;
1504 unsigned int i;
1505 int is_win16 = 0;
1506 struct strarray object_files = empty_strarray;
1507 struct strarray crossobj_files = empty_strarray;
1508 struct strarray res_files = empty_strarray;
1509 struct strarray clean_files = empty_strarray;
1510 struct strarray po_files = empty_strarray;
1511 struct strarray mo_files = empty_strarray;
1512 struct strarray mc_files = empty_strarray;
1513 struct strarray ok_files = empty_strarray;
1514 struct strarray dlldata_files = empty_strarray;
1515 struct strarray c2man_files = empty_strarray;
1516 struct strarray implib_objs = empty_strarray;
1517 struct strarray includes = empty_strarray;
1518 struct strarray subdirs = empty_strarray;
1519 struct strarray phony_targets = empty_strarray;
1520 struct strarray all_targets = get_expanded_make_var_array( "PROGRAMS" );
1521 struct strarray delayimports = get_expanded_make_var_array( "DELAYIMPORTS" );
1522 struct strarray extradllflags = get_expanded_make_var_array( "EXTRADLLFLAGS" );
1523 char *module = get_expanded_make_variable( "MODULE" );
1524 char *testdll = get_expanded_make_variable( "TESTDLL" );
1525 char *staticlib = get_expanded_make_variable( "STATICLIB" );
1527 if (module && strendswith( module, ".a" )) staticlib = module;
1528 for (i = 0; i < extradllflags.count; i++) if (!strcmp( extradllflags.str[i], "-m16" )) is_win16 = 1;
1530 for (i = 0; i < linguas.count; i++)
1531 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( "po" ), linguas.str[i] ));
1533 strarray_add( &includes, "-I." );
1534 if (src_dir) strarray_add( &includes, strmake( "-I%s", src_dir ));
1535 if (parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( parent_dir )));
1536 if (top_obj_dir) strarray_add( &includes, strmake( "-I%s/include", top_obj_dir ));
1537 if (top_src_dir) strarray_add( &includes, strmake( "-I%s/include", top_src_dir ));
1538 if (use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( "include/msvcrt" )));
1539 strarray_addall( &includes, include_args );
1541 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1543 struct strarray extradefs;
1544 char *obj = xstrdup( source->name );
1545 char *ext = get_extension( obj );
1547 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1548 *ext++ = 0;
1550 if (src_dir && strchr( obj, '/' ))
1552 char *subdir = base_dir_path( obj );
1553 *strrchr( subdir, '/' ) = 0;
1554 strarray_add_uniq( &subdirs, subdir );
1557 extradefs = get_expanded_make_var_array( strmake( "%s_EXTRADEFS", obj ));
1559 if (!strcmp( ext, "y" )) /* yacc file */
1561 /* add source file dependency for parallel makes */
1562 char *header = strmake( "%s.tab.h", obj );
1564 if (find_include_file( header ))
1566 output( "%s.tab.h: %s\n", obj, source->filename );
1567 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1568 obj, obj, source->filename );
1569 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1570 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1572 else output( "%s.tab.c: %s\n", obj, source->filename );
1574 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1575 free( header );
1576 continue; /* no dependencies */
1578 else if (!strcmp( ext, "x" )) /* template file */
1580 output( "%s.h: %s%s %s\n", obj, tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1581 output( "\t%s%s -H -o $@ %s\n", tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1582 strarray_add( &clean_files, strmake( "%s.h", obj ));
1583 continue; /* no dependencies */
1585 else if (!strcmp( ext, "l" )) /* lex file */
1587 output( "%s.yy.c: %s\n", obj, source->filename );
1588 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1589 continue; /* no dependencies */
1591 else if (!strcmp( ext, "rc" )) /* resource file */
1593 strarray_add( &res_files, strmake( "%s.res", obj ));
1594 output( "%s.res: %s %s\n", obj, tools_path( "wrc" ), source->filename );
1595 output( "\t%s -o $@ %s", tools_path( "wrc" ), source->filename );
1596 if (is_win16) output_filename( "-m16" );
1597 else output_filenames( target_flags );
1598 output_filename( "--nostdinc" );
1599 output_filenames( includes );
1600 output_filenames( define_args );
1601 output_filenames( extradefs );
1602 if (mo_files.count && (source->flags & FLAG_RC_PO))
1604 strarray_add( &po_files, source->filename );
1605 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1606 output( "\n" );
1607 output( "%s.res:", obj );
1608 output_filenames( mo_files );
1609 output( "\n" );
1610 output( "rsrc.pot " );
1612 else output( "\n" );
1613 output( "%s.res:", obj );
1615 else if (!strcmp( ext, "mc" )) /* message file */
1617 strarray_add( &res_files, strmake( "%s.res", obj ));
1618 output( "%s.res: %s %s\n", obj, tools_path( "wmc" ), source->filename );
1619 output( "\t%s -U -O res -o $@ %s", tools_path( "wmc" ), source->filename );
1620 if (mo_files.count)
1622 strarray_add( &mc_files, source->filename );
1623 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1624 output( "\n" );
1625 output( "%s.res:", obj );
1626 output_filenames( mo_files );
1627 output( "\n" );
1628 output( "msg.pot " );
1630 else output( "\n" );
1631 output( "%s.res:", obj );
1633 else if (!strcmp( ext, "idl" )) /* IDL file */
1635 struct strarray targets = empty_strarray;
1636 char *dest;
1638 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1639 source->flags |= FLAG_IDL_HEADER;
1641 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1643 if (!(source->flags & idl_outputs[i].flag)) continue;
1644 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1645 if (!find_src_file( dest )) strarray_add( &clean_files, dest );
1646 strarray_add( &targets, dest );
1648 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1649 output_filenames( targets );
1650 output( ": %s\n", tools_path( "widl" ));
1651 output( "\t%s -o $@ %s", tools_path( "widl" ), source->filename );
1652 output_filenames( target_flags );
1653 output_filenames( includes );
1654 output_filenames( define_args );
1655 output_filenames( extradefs );
1656 output_filenames( get_expanded_make_var_array( "EXTRAIDLFLAGS" ));
1657 output( "\n" );
1658 output_filenames( targets );
1659 output( ": %s", source->filename );
1661 else if (!strcmp( ext, "in" )) /* .in file or man page */
1663 if (strendswith( obj, ".man" ) && source->args)
1665 char *dir, *dest = replace_extension( obj, ".man", "" );
1666 char *lang = strchr( dest, '.' );
1667 char *section = source->args;
1668 if (lang)
1670 *lang++ = 0;
1671 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1673 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1674 output( "install-man-pages:: %s\n", obj );
1675 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj, dir, dest, section );
1676 output( "uninstall::\n" );
1677 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1678 free( dest );
1679 free( dir );
1680 strarray_add( &all_targets, xstrdup(obj) );
1681 strarray_add_uniq( &phony_targets, "install-man-pages" );
1682 strarray_add_uniq( &phony_targets, "uninstall" );
1684 else strarray_add( &clean_files, xstrdup(obj) );
1685 output( "%s: %s\n", obj, source->filename );
1686 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1687 output( "%s:", obj );
1689 else if (!strcmp( ext, "sfd" )) /* font file */
1691 char *ttf_file = src_dir_path( strmake( "%s.ttf", obj ));
1692 if (fontforge && !src_dir)
1694 output( "%s: %s\n", ttf_file, source->filename );
1695 output( "\t%s -script %s %s $@\n",
1696 fontforge, top_dir_path( "fonts/genttf.ff" ), source->filename );
1697 if (!(source->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1699 if (source->flags & FLAG_INSTALL)
1701 output( "install install-lib::\n" );
1702 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1703 output( "uninstall::\n" );
1704 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1706 if (source->flags & FLAG_SFD_FONTS)
1708 struct strarray *array = source->args;
1710 for (i = 0; i < array->count; i++)
1712 char *font = strtok( xstrdup(array->str[i]), " \t" );
1713 char *args = strtok( NULL, "" );
1715 strarray_add( &all_targets, font );
1716 output( "%s: %s %s\n", font, tools_path( "sfnt2fon" ), ttf_file );
1717 output( "\t%s -o $@ %s %s\n", tools_path( "sfnt2fon" ), ttf_file, args );
1718 output( "install install-lib:: %s\n", font );
1719 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n", font, font );
1720 output( "uninstall::\n" );
1721 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1724 if (source->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1726 strarray_add_uniq( &phony_targets, "install" );
1727 strarray_add_uniq( &phony_targets, "install-lib" );
1728 strarray_add_uniq( &phony_targets, "uninstall" );
1730 continue; /* no dependencies */
1732 else if (!strcmp( ext, "svg" )) /* svg file */
1734 if (convert && rsvg && icotool && !src_dir)
1736 output( "%s.ico %s.bmp: %s\n", obj, obj, source->filename );
1737 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
1738 convert, icotool, rsvg, top_dir_path( "tools/buildimage" ), source->filename );
1740 continue; /* no dependencies */
1742 else if (!strcmp( ext, "res" ))
1744 strarray_add( &res_files, source->name );
1745 continue; /* no dependencies */
1747 else
1749 int need_cross = testdll || (source->flags & FLAG_C_IMPLIB) || (module && staticlib);
1751 if ((source->flags & FLAG_GENERATED) && (!testdll || strcmp( source->filename, "testlist.c" )))
1752 strarray_add( &clean_files, source->filename );
1753 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1754 strarray_add( &object_files, strmake( "%s.o", obj ));
1755 output( "%s.o: %s\n", obj, source->filename );
1756 output( "\t$(CC) -c -o $@ %s", source->filename );
1757 output_filenames( includes );
1758 output_filenames( define_args );
1759 output_filenames( extradefs );
1760 if (module || staticlib || testdll)
1762 output_filenames( dll_flags );
1763 if (use_msvcrt) output_filenames( msvcrt_flags );
1765 output_filenames( extra_cflags );
1766 output_filenames( cpp_flags );
1767 output_filename( "$(CFLAGS)" );
1768 output( "\n" );
1769 if (crosstarget && need_cross)
1771 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1772 output( "%s.cross.o: %s\n", obj, source->filename );
1773 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1774 output_filenames( includes );
1775 output_filenames( define_args );
1776 output_filenames( extradefs );
1777 output_filename( "-DWINE_CROSSTEST" );
1778 output_filenames( cpp_flags );
1779 output_filename( "$(CFLAGS)" );
1780 output( "\n" );
1782 if (testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1784 strarray_add( &ok_files, strmake( "%s.ok", obj ));
1785 output( "%s.ok:\n", obj );
1786 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1787 top_dir_path( "tools/runtest" ), top_obj_dir,
1788 testdll, replace_extension( testdll, ".dll", "_test.exe" ), dll_ext, obj );
1790 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1791 strarray_add( &c2man_files, source->filename );
1792 output( "%s.o", obj );
1793 if (crosstarget && need_cross) output( " %s.cross.o", obj );
1794 output( ":" );
1796 free( obj );
1798 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
1799 output( "\n" );
1802 /* rules for files that depend on multiple sources */
1804 if (po_files.count)
1806 output( "rsrc.pot: %s", tools_path( "wrc" ) );
1807 output_filenames( po_files );
1808 output( "\n" );
1809 output( "\t%s -O pot -o $@", tools_path( "wrc" ));
1810 output_filenames( po_files );
1811 if (is_win16) output_filename( "-m16" );
1812 else output_filenames( target_flags );
1813 output_filename( "--nostdinc" );
1814 output_filenames( includes );
1815 output_filenames( define_args );
1816 output( "\n" );
1817 strarray_add( &clean_files, "rsrc.pot" );
1820 if (mc_files.count)
1822 output( "msg.pot: %s", tools_path( "wmc" ));
1823 output_filenames( mc_files );
1824 output( "\n" );
1825 output( "\t%s -O pot -o $@", tools_path( "wmc" ));
1826 output_filenames( mc_files );
1827 output( "\n" );
1828 strarray_add( &clean_files, "msg.pot" );
1831 if (dlldata_files.count)
1833 output( "dlldata.c: %s %s\n", tools_path( "widl" ), src_dir_path( "Makefile.in" ));
1834 output( "\t%s --dlldata-only -o $@", tools_path( "widl" ));
1835 output_filenames( dlldata_files );
1836 output( "\n" );
1839 if (module && !staticlib)
1841 char *importlib = get_expanded_make_variable( "IMPORTLIB" );
1842 struct strarray all_libs = empty_strarray;
1843 char *spec_file = NULL;
1845 if (!appmode.count) spec_file = src_dir_path( replace_extension( module, ".dll", ".spec" ));
1846 for (i = 0; i < delayimports.count; i++)
1847 strarray_add( &all_libs, strmake( "-l%s", delayimports.str[i] ));
1848 for (i = 0; i < imports.count; i++)
1849 strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1850 for (i = 0; i < delayimports.count; i++)
1851 strarray_add( &all_libs, strmake( "-Wb,-d%s", delayimports.str[i] ));
1852 strarray_add( &all_libs, "-lwine" );
1853 strarray_add( &all_libs, top_obj_dir_path( "libs/port/libwine_port.a" ));
1854 strarray_addall( &all_libs, get_expanded_make_var_array( "EXTRALIBS" ));
1855 strarray_addall( &all_libs, libs );
1857 if (*dll_ext)
1859 strarray_add( &all_targets, strmake( "%s%s", module, dll_ext ));
1860 strarray_add( &all_targets, strmake( "%s.fake", module ));
1861 output( "%s%s %s.fake:", module, dll_ext, module );
1863 else
1865 strarray_add( &all_targets, module );
1866 output( "%s:", module );
1868 if (spec_file) output_filename( spec_file );
1869 output_filenames( object_files );
1870 output_filenames( res_files );
1871 output( "\n" );
1872 output( "\t%s -o $@", tools_path( "winegcc" ));
1873 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1874 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1875 output_filenames( target_flags );
1876 output_filenames( unwind_flags );
1877 if (spec_file)
1879 output( " -shared %s", spec_file );
1880 output_filenames( extradllflags );
1882 else output_filenames( appmode );
1883 output_filenames( object_files );
1884 output_filenames( res_files );
1885 output_filenames( all_libs );
1886 output_filename( "$(LDFLAGS)" );
1887 output( "\n" );
1889 if (spec_file && importlib)
1891 if (*dll_ext)
1893 strarray_add( &clean_files, strmake( "lib%s.def", importlib ));
1894 output( "lib%s.def: %s %s\n", importlib, tools_path( "winebuild" ), spec_file );
1895 output( "\t%s -w --def -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1896 output_filenames( target_flags );
1897 if (is_win16) output_filename( "-m16" );
1898 output( "\n" );
1899 if (implib_objs.count)
1901 strarray_add( &clean_files, strmake( "lib%s.def.a", importlib ));
1902 output( "lib%s.def.a:", importlib );
1903 output_filenames( implib_objs );
1904 output( "\n" );
1905 output( "\t$(RM) $@\n" );
1906 output( "\t$(AR) $(ARFLAGS) $@" );
1907 output_filenames( implib_objs );
1908 output( "\n" );
1909 output( "\t$(RANLIB) $@\n" );
1912 else
1914 strarray_add( &clean_files, strmake( "lib%s.a", importlib ));
1915 output( "lib%s.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1916 output_filenames( implib_objs );
1917 output( "\n" );
1918 output( "\t%s -w --implib -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1919 output_filenames( target_flags );
1920 output_filenames( implib_objs );
1921 output( "\n" );
1923 if (crosstarget && !is_win16)
1925 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
1926 strarray_add( &clean_files, strmake( "lib%s.cross.a", importlib ));
1927 output( "lib%s.cross.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1928 output_filenames( cross_files );
1929 output( "\n" );
1930 output( "\t%s -b %s -w --implib -o $@ --export %s",
1931 tools_path( "winebuild" ), crosstarget, spec_file );
1932 output_filenames( cross_files );
1933 output( "\n" );
1937 if (spec_file)
1939 if (c2man_files.count)
1941 output( "manpages::\n" );
1942 output( "\t%s -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1943 output_filename( strmake( "-R%s", top_dir_path( "" )));
1944 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1945 output_filename( strmake( "-o %s/man%s", top_obj_dir_path( "documentation" ), man_ext ));
1946 output_filenames( c2man_files );
1947 output( "\n" );
1948 output( "htmlpages::\n" );
1949 output( "\t%s -Th -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1950 output_filename( strmake( "-R%s", top_dir_path( "" )));
1951 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1952 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/html" )));
1953 output_filenames( c2man_files );
1954 output( "\n" );
1955 output( "sgmlpages::\n" );
1956 output( "\t%s -Ts -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1957 output_filename( strmake( "-R%s", top_dir_path( "" )));
1958 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1959 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/api-guide" )));
1960 output_filenames( c2man_files );
1961 output( "\n" );
1962 output( "xmlpages::\n" );
1963 output( "\t%s -Tx -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1964 output_filename( strmake( "-R%s", top_dir_path( "" )));
1965 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1966 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/api-guide-xml" )));
1967 output_filenames( c2man_files );
1968 output( "\n" );
1969 strarray_add( &phony_targets, "manpages" );
1970 strarray_add( &phony_targets, "htmlpages" );
1971 strarray_add( &phony_targets, "sgmlpages" );
1972 strarray_add( &phony_targets, "xmlpages" );
1974 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1978 if (staticlib)
1980 strarray_add( &all_targets, staticlib );
1981 output( "%s:", staticlib );
1982 output_filenames( object_files );
1983 output( "\n\t$(RM) $@\n" );
1984 output( "\t$(AR) $(ARFLAGS) $@" );
1985 output_filenames( object_files );
1986 output( "\n\t$(RANLIB) $@\n" );
1987 if (crosstarget && module)
1989 char *name = replace_extension( staticlib, ".a", ".cross.a" );
1991 strarray_add( &all_targets, name );
1992 output( "%s:", name );
1993 output_filenames( crossobj_files );
1994 output( "\n\t$(RM) $@\n" );
1995 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
1996 output_filenames( crossobj_files );
1997 output( "\n\t%s-ranlib $@\n", crosstarget );
2001 if (testdll)
2003 char *testmodule = replace_extension( testdll, ".dll", "_test.exe" );
2004 char *stripped = replace_extension( testdll, ".dll", "_test-stripped.exe" );
2005 struct strarray all_libs = empty_strarray;
2007 for (i = 0; i < imports.count; i++) strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
2008 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
2010 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2011 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2012 output( "%s%s:\n", testmodule, dll_ext );
2013 output( "\t%s -o $@", tools_path( "winegcc" ));
2014 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
2015 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
2016 output_filenames( target_flags );
2017 output_filenames( unwind_flags );
2018 output_filenames( appmode );
2019 output_filenames( object_files );
2020 output_filenames( res_files );
2021 output_filenames( all_libs );
2022 output_filename( "$(LDFLAGS)" );
2023 output( "\n" );
2024 output( "%s%s:\n", stripped, dll_ext );
2025 output( "\t%s -o $@", tools_path( "winegcc" ));
2026 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
2027 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
2028 output_filenames( target_flags );
2029 output_filenames( unwind_flags );
2030 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2031 output_filenames( appmode );
2032 output_filenames( object_files );
2033 output_filenames( res_files );
2034 output_filenames( all_libs );
2035 output_filename( "$(LDFLAGS)" );
2036 output( "\n" );
2037 output( "%s%s %s%s:", testmodule, dll_ext, stripped, dll_ext );
2038 output_filenames( object_files );
2039 output_filenames( res_files );
2040 output( "\n" );
2042 if (top_obj_dir)
2044 char *testres = replace_extension( testdll, ".dll", "_test.res" );
2045 output( "all: %s/%s\n", top_obj_dir_path( "programs/winetest" ), testres );
2046 output( "%s/%s: %s%s\n", top_obj_dir_path( "programs/winetest" ), testres, stripped, dll_ext );
2047 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2048 testmodule, stripped, dll_ext, tools_path( "wrc" ));
2051 if (crosstarget)
2053 char *crosstest = replace_extension( testdll, ".dll", "_crosstest.exe" );
2055 strarray_add( &clean_files, crosstest );
2056 output( "crosstest: %s\n", crosstest );
2057 output( "%s:", crosstest );
2058 output_filenames( crossobj_files );
2059 output_filenames( res_files );
2060 output( "\n" );
2061 output( "\t%s -o $@ -b %s", tools_path( "winegcc" ), crosstarget );
2062 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
2063 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
2064 output_filename( "--lib-suffix=.cross.a" );
2065 output_filenames( crossobj_files );
2066 output_filenames( res_files );
2067 output_filenames( all_libs );
2068 output_filename( "$(LDFLAGS)" );
2069 output( "\n" );
2070 strarray_add( &phony_targets, "crosstest" );
2073 output_filenames( ok_files );
2074 output( ": %s%s ../%s%s\n", testmodule, dll_ext, testdll, dll_ext );
2075 output( "check test:" );
2076 output_filenames( ok_files );
2077 output( "\n" );
2078 output( "testclean::\n" );
2079 output( "\t$(RM)" );
2080 output_filenames( ok_files );
2081 output( "\n" );
2082 strarray_addall( &clean_files, ok_files );
2083 strarray_add( &phony_targets, "check" );
2084 strarray_add( &phony_targets, "test" );
2085 strarray_add( &phony_targets, "testclean" );
2086 testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2089 if (all_targets.count)
2091 output( "all:" );
2092 output_filenames( all_targets );
2093 output( "\n" );
2096 strarray_addall( &clean_files, object_files );
2097 strarray_addall( &clean_files, crossobj_files );
2098 strarray_addall( &clean_files, res_files );
2099 strarray_addall( &clean_files, all_targets );
2100 strarray_addall( &clean_files, get_expanded_make_var_array( "EXTRA_TARGETS" ));
2102 if (clean_files.count)
2104 output( "clean::\n" );
2105 output( "\t$(RM)" );
2106 output_filenames( clean_files );
2107 output( "\n" );
2108 strarray_add( &phony_targets, "clean" );
2111 if (top_obj_dir)
2113 output( "depend:\n" );
2114 output( "\t@cd %s && $(MAKE) %s\n", top_obj_dir, base_dir_path( "depend" ));
2115 strarray_add( &phony_targets, "depend" );
2118 if (phony_targets.count)
2120 output( ".PHONY:" );
2121 output_filenames( phony_targets );
2122 output( "\n" );
2125 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2127 return clean_files;
2131 /*******************************************************************
2132 * create_temp_file
2134 static FILE *create_temp_file( const char *orig )
2136 char *name = xmalloc( strlen(orig) + 13 );
2137 unsigned int i, id = getpid();
2138 int fd;
2139 FILE *ret = NULL;
2141 for (i = 0; i < 100; i++)
2143 sprintf( name, "%s.tmp%08x", orig, id );
2144 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2146 ret = fdopen( fd, "w" );
2147 break;
2149 if (errno != EEXIST) break;
2150 id += 7777;
2152 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2153 temp_file_name = name;
2154 return ret;
2158 /*******************************************************************
2159 * rename_temp_file
2161 static void rename_temp_file( const char *dest )
2163 int ret = rename( temp_file_name, dest );
2164 if (ret == -1 && errno == EEXIST)
2166 /* rename doesn't overwrite on windows */
2167 unlink( dest );
2168 ret = rename( temp_file_name, dest );
2170 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2171 temp_file_name = NULL;
2175 /*******************************************************************
2176 * are_files_identical
2178 static int are_files_identical( FILE *file1, FILE *file2 )
2180 for (;;)
2182 char buffer1[8192], buffer2[8192];
2183 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2184 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2185 if (size1 != size2) return 0;
2186 if (!size1) return feof( file1 ) && feof( file2 );
2187 if (memcmp( buffer1, buffer2, size1 )) return 0;
2192 /*******************************************************************
2193 * rename_temp_file_if_changed
2195 static void rename_temp_file_if_changed( const char *dest )
2197 FILE *file1, *file2;
2198 int do_rename = 1;
2200 if ((file1 = fopen( dest, "r" )))
2202 if ((file2 = fopen( temp_file_name, "r" )))
2204 do_rename = !are_files_identical( file1, file2 );
2205 fclose( file2 );
2207 fclose( file1 );
2209 if (!do_rename)
2211 unlink( temp_file_name );
2212 temp_file_name = NULL;
2214 else rename_temp_file( dest );
2218 /*******************************************************************
2219 * output_testlist
2221 static void output_testlist( const char *dest, struct strarray files )
2223 int i;
2225 output_file = create_temp_file( dest );
2227 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2228 output( "#define WIN32_LEAN_AND_MEAN\n" );
2229 output( "#include <windows.h>\n\n" );
2230 output( "#define STANDALONE\n" );
2231 output( "#include \"wine/test.h\"\n\n" );
2233 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2234 output( "\n" );
2235 output( "const struct test winetest_testlist[] =\n" );
2236 output( "{\n" );
2237 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2238 output( " { 0, 0 }\n" );
2239 output( "};\n" );
2241 if (fclose( output_file )) fatal_perror( "write" );
2242 output_file = NULL;
2243 rename_temp_file_if_changed( dest );
2247 /*******************************************************************
2248 * output_gitignore
2250 static void output_gitignore( const char *dest, struct strarray files )
2252 int i;
2254 output_file = create_temp_file( dest );
2256 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2257 for (i = 0; i < files.count; i++)
2259 if (!strchr( files.str[i], '/' )) output( "/" );
2260 output( "%s\n", files.str[i] );
2263 if (fclose( output_file )) fatal_perror( "write" );
2264 output_file = NULL;
2265 rename_temp_file( dest );
2269 /*******************************************************************
2270 * output_dependencies
2272 static void output_dependencies( const char *path )
2274 struct strarray targets, ignore_files = empty_strarray;
2276 if (Separator && ((output_file = fopen( path, "r" ))))
2278 char buffer[1024];
2279 FILE *tmp_file = create_temp_file( path );
2280 int found = 0;
2282 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2284 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2285 found = !strncmp( buffer, Separator, strlen(Separator) );
2287 if (fclose( output_file )) fatal_perror( "write" );
2288 output_file = tmp_file;
2289 if (!found) output( "\n%s\n", Separator );
2291 else
2293 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2294 fatal_perror( "%s", path );
2297 testlist_files = empty_strarray;
2298 targets = output_sources();
2300 fclose( output_file );
2301 output_file = NULL;
2302 if (temp_file_name) rename_temp_file( path );
2304 strarray_add( &ignore_files, ".gitignore" );
2305 strarray_add( &ignore_files, "Makefile" );
2306 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2307 strarray_addall( &ignore_files, targets );
2309 if (testlist_files.count) output_testlist( base_dir_path( "testlist.c" ), testlist_files );
2310 if (!src_dir && base_dir) output_gitignore( base_dir_path( ".gitignore" ), ignore_files );
2314 /*******************************************************************
2315 * update_makefile
2317 static void update_makefile( const char *path )
2319 static const char *source_vars[] =
2321 "C_SRCS",
2322 "OBJC_SRCS",
2323 "RC_SRCS",
2324 "MC_SRCS",
2325 "IDL_SRCS",
2326 "BISON_SRCS",
2327 "LEX_SRCS",
2328 "XTEMPLATE_SRCS",
2329 "SVG_SRCS",
2330 "FONT_SRCS",
2331 "IN_SRCS",
2332 "MANPAGES",
2333 NULL
2335 const char **var;
2336 unsigned int i;
2337 struct strarray value;
2338 struct incl_file *file;
2340 base_dir = path;
2341 if (!strcmp( base_dir, "." )) base_dir = NULL;
2342 output_file_name = base_dir_path( makefile_name );
2343 parse_makefile( output_file_name, Separator, &make_vars );
2345 src_dir = get_expanded_make_variable( "srcdir" );
2346 top_src_dir = get_expanded_make_variable( "top_srcdir" );
2347 top_obj_dir = get_expanded_make_variable( "top_builddir" );
2348 parent_dir = get_expanded_make_variable( "PARENTSRC" );
2350 /* ignore redundant source paths */
2351 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
2352 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
2353 if (top_obj_dir && !strcmp( top_obj_dir, "." )) top_obj_dir = NULL;
2355 appmode = get_expanded_make_var_array( "APPMODE" );
2356 imports = get_expanded_make_var_array( "IMPORTS" );
2358 use_msvcrt = 0;
2359 for (i = 0; i < appmode.count && !use_msvcrt; i++)
2360 use_msvcrt = !strcmp( appmode.str[i], "-mno-cygwin" );
2361 for (i = 0; i < imports.count && !use_msvcrt; i++)
2362 use_msvcrt = !strncmp( imports.str[i], "msvcr", 5 );
2364 include_args = empty_strarray;
2365 define_args = empty_strarray;
2366 strarray_add( &define_args, "-D__WINESRC__" );
2368 value = get_expanded_make_var_array( "EXTRAINCL" );
2369 for (i = 0; i < value.count; i++)
2370 if (!strncmp( value.str[i], "-I", 2 ))
2371 strarray_add_uniq( &include_args, value.str[i] );
2372 else
2373 strarray_add_uniq( &define_args, value.str[i] );
2374 strarray_addall( &define_args, get_expanded_make_var_array( "EXTRADEFS" ));
2376 list_init( &sources );
2377 list_init( &includes );
2379 for (var = source_vars; *var; var++)
2381 value = get_expanded_make_var_array( *var );
2382 for (i = 0; i < value.count; i++) add_src_file( value.str[i] );
2385 add_generated_sources();
2387 value = get_expanded_make_var_array( "EXTRA_OBJS" );
2388 for (i = 0; i < value.count; i++)
2390 /* default to .c for unknown extra object files */
2391 if (strendswith( value.str[i], ".o" ))
2392 add_generated_source( value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2393 else
2394 add_generated_source( value.str[i], NULL );
2397 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( file, 0 );
2398 output_dependencies( output_file_name );
2399 output_file_name = NULL;
2403 /*******************************************************************
2404 * parse_makeflags
2406 static void parse_makeflags( const char *flags )
2408 const char *p = flags;
2409 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2411 while (*p)
2413 while (isspace(*p)) p++;
2414 var = buffer;
2415 while (*p && !isspace(*p))
2417 if (*p == '\\' && p[1]) p++;
2418 *var++ = *p++;
2420 *var = 0;
2421 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2426 /*******************************************************************
2427 * parse_option
2429 static int parse_option( const char *opt )
2431 if (opt[0] != '-')
2433 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2434 return 0;
2436 switch(opt[1])
2438 case 'f':
2439 if (opt[2]) makefile_name = opt + 2;
2440 break;
2441 case 'R':
2442 relative_dir_mode = 1;
2443 break;
2444 case 's':
2445 if (opt[2]) Separator = opt + 2;
2446 else Separator = NULL;
2447 break;
2448 default:
2449 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2450 exit(1);
2452 return 1;
2456 /*******************************************************************
2457 * main
2459 int main( int argc, char *argv[] )
2461 const char *makeflags = getenv( "MAKEFLAGS" );
2462 int i, j;
2464 if (makeflags) parse_makeflags( makeflags );
2466 i = 1;
2467 while (i < argc)
2469 if (parse_option( argv[i] ))
2471 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2472 argc--;
2474 else i++;
2477 if (relative_dir_mode)
2479 char *relpath;
2481 if (argc != 3)
2483 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2484 exit( 1 );
2486 relpath = get_relative_path( argv[1], argv[2] );
2487 printf( "%s\n", relpath ? relpath : "." );
2488 exit( 0 );
2491 if (argc <= 1)
2493 fprintf( stderr, "%s", Usage );
2494 exit( 1 );
2496 atexit( cleanup_files );
2497 signal( SIGTERM, exit_on_signal );
2498 signal( SIGINT, exit_on_signal );
2499 #ifdef SIGHUP
2500 signal( SIGHUP, exit_on_signal );
2501 #endif
2503 parse_makefile( makefile_name, "# End of common header", &top_make_vars );
2505 linguas = get_expanded_make_var_array( "LINGUAS" );
2506 target_flags = get_expanded_make_var_array( "TARGETFLAGS" );
2507 msvcrt_flags = get_expanded_make_var_array( "MSVCRTFLAGS" );
2508 dll_flags = get_expanded_make_var_array( "DLLFLAGS" );
2509 extra_cflags = get_expanded_make_var_array( "EXTRACFLAGS" );
2510 cpp_flags = get_expanded_make_var_array( "CPPFLAGS" );
2511 unwind_flags = get_expanded_make_var_array( "UNWINDFLAGS" );
2512 libs = get_expanded_make_var_array( "LIBS" );
2514 root_src_dir = get_expanded_make_variable( "srcdir" );
2515 tools_dir = get_expanded_make_variable( "TOOLSDIR" );
2516 tools_ext = get_expanded_make_variable( "TOOLSEXT" );
2517 exe_ext = get_expanded_make_variable( "EXEEXT" );
2518 man_ext = get_expanded_make_variable( "api_manext" );
2519 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2520 dll_prefix = get_expanded_make_variable( "DLLPREFIX" );
2521 crosstarget = get_expanded_make_variable( "CROSSTARGET" );
2522 fontforge = get_expanded_make_variable( "FONTFORGE" );
2523 convert = get_expanded_make_variable( "CONVERT" );
2524 rsvg = get_expanded_make_variable( "RSVG" );
2525 icotool = get_expanded_make_variable( "ICOTOOL" );
2527 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2528 if (!tools_ext) tools_ext = "";
2529 if (!dll_prefix) dll_prefix = "";
2530 if (!man_ext) man_ext = "3w";
2532 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2533 return 0;