dplayx: Use BOOL type where appropriate.
[wine/multimedia.git] / tools / makedep.c
blobfe8a614c0ae71a8c92fe7d99c12ea863b2c60d22
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 static struct strarray include_args;
98 static struct strarray define_args;
99 static struct strarray appmode;
100 static struct strarray dllflags;
101 static struct strarray imports;
102 static struct strarray make_vars;
103 static struct strarray cmdline_vars;
104 static struct strarray testlist_files;
106 static const char *base_dir;
107 static const char *src_dir;
108 static const char *top_src_dir;
109 static const char *top_obj_dir;
110 static const char *parent_dir;
111 static const char *tools_dir;
112 static const char *tools_ext;
113 static const char *makefile_name = "Makefile";
114 static const char *Separator = "### Dependencies";
115 static const char *input_file_name;
116 static const char *output_file_name;
117 static const char *temp_file_name;
118 static int use_msvcrt;
119 static int relative_dir_mode;
120 static int input_line;
121 static int output_column;
122 static FILE *output_file;
124 static const char Usage[] =
125 "Usage: makedep [options] directories\n"
126 "Options:\n"
127 " -R from to Compute the relative path between two directories\n"
128 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
129 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
132 #ifndef __GNUC__
133 #define __attribute__(x)
134 #endif
136 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
137 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
138 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
139 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
141 /*******************************************************************
142 * fatal_error
144 static void fatal_error( const char *msg, ... )
146 va_list valist;
147 va_start( valist, msg );
148 if (input_file_name)
150 fprintf( stderr, "%s:", input_file_name );
151 if (input_line) fprintf( stderr, "%d:", input_line );
152 fprintf( stderr, " error: " );
154 else fprintf( stderr, "makedep: error: " );
155 vfprintf( stderr, msg, valist );
156 va_end( valist );
157 exit(1);
161 /*******************************************************************
162 * fatal_perror
164 static void fatal_perror( const char *msg, ... )
166 va_list valist;
167 va_start( valist, msg );
168 if (input_file_name)
170 fprintf( stderr, "%s:", input_file_name );
171 if (input_line) fprintf( stderr, "%d:", input_line );
172 fprintf( stderr, " error: " );
174 else fprintf( stderr, "makedep: error: " );
175 vfprintf( stderr, msg, valist );
176 perror( " " );
177 va_end( valist );
178 exit(1);
182 /*******************************************************************
183 * cleanup_files
185 static void cleanup_files(void)
187 if (temp_file_name) unlink( temp_file_name );
188 if (output_file_name) unlink( output_file_name );
192 /*******************************************************************
193 * exit_on_signal
195 static void exit_on_signal( int sig )
197 exit( 1 ); /* this will call the atexit functions */
201 /*******************************************************************
202 * xmalloc
204 static void *xmalloc( size_t size )
206 void *res;
207 if (!(res = malloc (size ? size : 1)))
208 fatal_error( "Virtual memory exhausted.\n" );
209 return res;
213 /*******************************************************************
214 * xrealloc
216 static void *xrealloc (void *ptr, size_t size)
218 void *res;
219 assert( size );
220 if (!(res = realloc( ptr, size )))
221 fatal_error( "Virtual memory exhausted.\n" );
222 return res;
225 /*******************************************************************
226 * xstrdup
228 static char *xstrdup( const char *str )
230 char *res = strdup( str );
231 if (!res) fatal_error( "Virtual memory exhausted.\n" );
232 return res;
236 /*******************************************************************
237 * strmake
239 static char *strmake( const char* fmt, ... )
241 int n;
242 size_t size = 100;
243 va_list ap;
245 for (;;)
247 char *p = xmalloc (size);
248 va_start(ap, fmt);
249 n = vsnprintf (p, size, fmt, ap);
250 va_end(ap);
251 if (n == -1) size *= 2;
252 else if ((size_t)n >= size) size = n + 1;
253 else return p;
254 free(p);
259 /*******************************************************************
260 * strendswith
262 static int strendswith( const char* str, const char* end )
264 int l = strlen(str);
265 int m = strlen(end);
267 return l >= m && strcmp(str + l - m, end) == 0;
271 /*******************************************************************
272 * output
274 static void output( const char *format, ... )
276 int ret;
277 va_list valist;
279 va_start( valist, format );
280 ret = vfprintf( output_file, format, valist );
281 va_end( valist );
282 if (ret < 0) fatal_perror( "output" );
283 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
284 else output_column += ret;
288 /*******************************************************************
289 * strarray_add
291 static void strarray_add( struct strarray *array, const char *str )
293 if (array->count == array->size)
295 if (array->size) array->size *= 2;
296 else array->size = 16;
297 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
299 array->str[array->count++] = str;
303 /*******************************************************************
304 * strarray_addall
306 static void strarray_addall( struct strarray *array, struct strarray added )
308 unsigned int i;
310 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
314 /*******************************************************************
315 * strarray_add_uniq
317 static void strarray_add_uniq( struct strarray *array, const char *str )
319 unsigned int i;
321 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return;
322 strarray_add( array, str );
326 /*******************************************************************
327 * output_filename
329 static void output_filename( const char *name )
331 if (output_column + strlen(name) + 1 > 100)
333 output( " \\\n" );
334 output( " " );
336 else if (output_column) output( " " );
337 output( "%s", name );
341 /*******************************************************************
342 * output_filenames
344 static void output_filenames( struct strarray array )
346 unsigned int i;
348 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
352 /*******************************************************************
353 * get_extension
355 static char *get_extension( char *filename )
357 char *ext = strrchr( filename, '.' );
358 if (ext && strchr( ext, '/' )) ext = NULL;
359 return ext;
363 /*******************************************************************
364 * replace_extension
366 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
368 char *ret;
369 int name_len = strlen( name );
370 int ext_len = strlen( old_ext );
372 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
373 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
374 memcpy( ret, name, name_len );
375 strcpy( ret + name_len, new_ext );
376 return ret;
380 /*******************************************************************
381 * strarray_replace_extension
383 static struct strarray strarray_replace_extension( const struct strarray *array,
384 const char *old_ext, const char *new_ext )
386 unsigned int i;
387 struct strarray ret;
389 ret.count = ret.size = array->count;
390 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
391 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
392 return ret;
396 /*******************************************************************
397 * replace_substr
399 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
401 unsigned int pos = start - str;
402 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
403 memcpy( ret, str, pos );
404 strcpy( ret + pos, replace );
405 strcat( ret + pos, start + len );
406 return ret;
410 /*******************************************************************
411 * get_relative_path
413 * Determine where the destination path is located relative to the 'from' path.
415 static char *get_relative_path( const char *from, const char *dest )
417 const char *start;
418 char *ret, *p;
419 unsigned int dotdots = 0;
421 /* a path of "." is equivalent to an empty path */
422 if (!strcmp( from, "." )) from = "";
424 for (;;)
426 while (*from == '/') from++;
427 while (*dest == '/') dest++;
428 start = dest; /* save start of next path element */
429 if (!*from) break;
431 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
432 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
434 /* count remaining elements in 'from' */
437 dotdots++;
438 while (*from && *from != '/') from++;
439 while (*from == '/') from++;
441 while (*from);
442 break;
445 if (!start[0] && !dotdots) return NULL; /* empty path */
447 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
448 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
450 if (start[0]) strcpy( p, start );
451 else p[-1] = 0; /* remove trailing slash */
452 return ret;
456 /*******************************************************************
457 * base_dir_path
459 static char *base_dir_path( const char *path )
461 if (base_dir && path[0] != '/') return strmake( "%s/%s", base_dir, path );
462 return xstrdup( path );
466 /*******************************************************************
467 * src_dir_path
469 static char *src_dir_path( const char *path )
471 if (src_dir) return strmake( "%s/%s", src_dir, path );
472 return xstrdup( path );
476 /*******************************************************************
477 * top_obj_dir_path
479 static char *top_obj_dir_path( const char *path )
481 if (top_obj_dir) return strmake( "%s/%s", top_obj_dir, path );
482 return xstrdup( path );
486 /*******************************************************************
487 * top_dir_path
489 static char *top_dir_path( const char *path )
491 if (top_src_dir) return strmake( "%s/%s", top_src_dir, path );
492 return top_obj_dir_path( path );
496 /*******************************************************************
497 * tools_dir_path
499 static char *tools_dir_path( const char *path )
501 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
502 if (top_obj_dir) return strmake( "%s/tools/%s", top_obj_dir, path );
503 return strmake( "tools/%s", path );
507 /*******************************************************************
508 * tools_path
510 static char *tools_path( const char *name )
512 if (tools_dir) return strmake( "%s/tools/%s/%s%s", tools_dir, name, name, tools_ext );
513 if (top_obj_dir) return strmake( "%s/tools/%s/%s%s", top_obj_dir, name, name, tools_ext );
514 return strmake( "tools/%s/%s%s", name, name, tools_ext );
518 /*******************************************************************
519 * get_line
521 static char *get_line( FILE *file )
523 static char *buffer;
524 static unsigned int size;
526 if (!size)
528 size = 1024;
529 buffer = xmalloc( size );
531 if (!fgets( buffer, size, file )) return NULL;
532 input_line++;
534 for (;;)
536 char *p = buffer + strlen(buffer);
537 /* if line is larger than buffer, resize buffer */
538 while (p == buffer + size - 1 && p[-1] != '\n')
540 buffer = xrealloc( buffer, size * 2 );
541 if (!fgets( buffer + size - 1, size + 1, file )) break;
542 p = buffer + strlen(buffer);
543 size *= 2;
545 if (p > buffer && p[-1] == '\n')
547 *(--p) = 0;
548 if (p > buffer && p[-1] == '\r') *(--p) = 0;
549 if (p > buffer && p[-1] == '\\')
551 *(--p) = 0;
552 /* line ends in backslash, read continuation line */
553 if (!fgets( p, size - (p - buffer), file )) return buffer;
554 input_line++;
555 continue;
558 return buffer;
562 /*******************************************************************
563 * find_src_file
565 static struct incl_file *find_src_file( const char *name )
567 struct incl_file *file;
569 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
570 if (!strcmp( name, file->name )) return file;
571 return NULL;
574 /*******************************************************************
575 * find_include_file
577 static struct incl_file *find_include_file( const char *name )
579 struct incl_file *file;
581 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
582 if (!strcmp( name, file->name )) return file;
583 return NULL;
586 /*******************************************************************
587 * add_include
589 * Add an include file if it doesn't already exists.
591 static struct incl_file *add_include( struct incl_file *parent, const char *name, int system )
593 struct incl_file *include;
594 char *ext;
596 if (parent->files_count >= parent->files_size)
598 parent->files_size *= 2;
599 if (parent->files_size < 16) parent->files_size = 16;
600 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
603 /* enforce some rules for the Wine tree */
605 if (!memcmp( name, "../", 3 ))
606 fatal_error( "#include directive with relative path not allowed\n" );
608 if (!strcmp( name, "config.h" ))
610 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
611 fatal_error( "config.h must not be included by a header file\n" );
612 if (parent->files_count)
613 fatal_error( "config.h must be included before anything else\n" );
615 else if (!strcmp( name, "wine/port.h" ))
617 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
618 fatal_error( "wine/port.h must not be included by a header file\n" );
619 if (!parent->files_count) fatal_error( "config.h must be included before wine/port.h\n" );
620 if (parent->files_count > 1)
621 fatal_error( "wine/port.h must be included before everything except config.h\n" );
622 if (strcmp( parent->files[0]->name, "config.h" ))
623 fatal_error( "config.h must be included before wine/port.h\n" );
626 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
627 if (!strcmp( name, include->name )) goto found;
629 include = xmalloc( sizeof(*include) );
630 memset( include, 0, sizeof(*include) );
631 include->name = xstrdup(name);
632 include->included_by = parent;
633 include->included_line = input_line;
634 if (system) include->flags |= FLAG_SYSTEM;
635 list_add_tail( &includes, &include->entry );
636 found:
637 parent->files[parent->files_count++] = include;
638 return include;
642 /*******************************************************************
643 * add_generated_source
645 * Add a generated source file to the list.
647 static struct incl_file *add_generated_source( const char *name, const char *filename )
649 struct incl_file *file;
651 if ((file = find_src_file( name ))) return file; /* we already have it */
652 file = xmalloc( sizeof(*file) );
653 memset( file, 0, sizeof(*file) );
654 file->name = xstrdup( name );
655 file->filename = xstrdup( filename ? filename : name );
656 file->flags = FLAG_GENERATED;
657 list_add_tail( &sources, &file->entry );
658 return file;
662 /*******************************************************************
663 * open_file
665 static FILE *open_file( const char *path )
667 return fopen( base_dir_path( path ), "r" );
670 /*******************************************************************
671 * open_src_file
673 static FILE *open_src_file( struct incl_file *pFile )
675 FILE *file;
677 /* try in source dir */
678 pFile->filename = src_dir_path( pFile->name );
679 file = open_file( pFile->filename );
681 /* now try parent dir */
682 if (!file && parent_dir)
684 pFile->filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
685 file = open_file( pFile->filename );
687 if (!file) fatal_perror( "open %s", pFile->name );
688 return file;
692 /*******************************************************************
693 * open_include_file
695 static FILE *open_include_file( struct incl_file *pFile )
697 FILE *file = NULL;
698 char *filename, *p;
699 unsigned int i, len;
701 errno = ENOENT;
703 /* check for generated bison header */
705 if (strendswith( pFile->name, ".tab.h" ))
707 filename = src_dir_path( replace_extension( pFile->name, ".tab.h", ".y" ));
708 if ((file = open_file( filename )))
710 pFile->sourcename = filename;
711 pFile->filename = xstrdup( pFile->name );
712 /* don't bother to parse it */
713 fclose( file );
714 return NULL;
716 free( filename );
719 /* check for corresponding idl file in source dir */
721 if (strendswith( pFile->name, ".h" ))
723 filename = src_dir_path( replace_extension( pFile->name, ".h", ".idl" ));
724 if ((file = open_file( filename )))
726 pFile->sourcename = filename;
727 pFile->filename = xstrdup( pFile->name );
728 return file;
730 free( filename );
733 /* now try in source dir */
734 filename = src_dir_path( pFile->name );
735 if ((file = open_file( filename ))) goto found;
736 free( filename );
738 /* now try in parent source dir */
739 if (parent_dir)
741 filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
742 if ((file = open_file( filename ))) goto found;
743 free( filename );
746 /* check for corresponding idl file in global includes */
748 if (strendswith( pFile->name, ".h" ))
750 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".idl" )));
751 if ((file = open_file( filename )))
753 pFile->sourcename = filename;
754 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
755 return file;
757 free( filename );
760 /* check for corresponding .in file in global includes (for config.h.in) */
762 if (strendswith( pFile->name, ".h" ))
764 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".h.in" )));
765 if ((file = open_file( filename )))
767 pFile->sourcename = filename;
768 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
769 return file;
771 free( filename );
774 /* check for corresponding .x file in global includes */
776 if (strendswith( pFile->name, "tmpl.h" ))
778 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".x" )));
779 if ((file = open_file( filename )))
781 pFile->sourcename = filename;
782 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
783 return file;
785 free( filename );
788 /* check in global includes source dir */
790 filename = top_dir_path( strmake( "include/%s", pFile->name ));
791 if ((file = open_file( filename ))) goto found;
793 /* check in global msvcrt includes */
794 if (use_msvcrt)
796 filename = top_dir_path( strmake( "include/msvcrt/%s", pFile->name ));
797 if ((file = open_file( filename ))) goto found;
800 /* now search in include paths */
801 for (i = 0; i < include_args.count; i++)
803 const char *dir = include_args.str[i] + 2; /* skip -I */
804 if (*dir == '/')
806 /* ignore absolute paths that don't point into the source dir */
807 if (!top_src_dir) continue;
808 len = strlen( top_src_dir );
809 if (strncmp( dir, top_src_dir, len )) continue;
810 if (dir[len] && dir[len] != '/') continue;
812 filename = strmake( "%s/%s", dir, pFile->name );
813 if ((file = open_file( filename ))) goto found;
814 free( filename );
816 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
818 /* try in src file directory */
819 if ((p = strrchr(pFile->included_by->filename, '/')))
821 int l = p - pFile->included_by->filename + 1;
822 filename = xmalloc(l + strlen(pFile->name) + 1);
823 memcpy( filename, pFile->included_by->filename, l );
824 strcpy( filename + l, pFile->name );
825 if ((file = open_file( filename ))) goto found;
826 free( filename );
829 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
830 perror( pFile->name );
831 pFile = pFile->included_by;
832 while (pFile && pFile->included_by)
834 const char *parent = pFile->included_by->sourcename;
835 if (!parent) parent = pFile->included_by->name;
836 fprintf( stderr, "%s:%d: note: %s was first included here\n",
837 parent, pFile->included_line, pFile->name );
838 pFile = pFile->included_by;
840 exit(1);
842 found:
843 pFile->filename = filename;
844 return file;
848 /*******************************************************************
849 * parse_include_directive
851 static void parse_include_directive( struct incl_file *source, char *str )
853 char quote, *include, *p = str;
855 while (*p && isspace(*p)) p++;
856 if (*p != '\"' && *p != '<' ) return;
857 quote = *p++;
858 if (quote == '<') quote = '>';
859 include = p;
860 while (*p && (*p != quote)) p++;
861 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
862 *p = 0;
863 add_include( source, include, (quote == '>') );
867 /*******************************************************************
868 * parse_pragma_directive
870 static void parse_pragma_directive( struct incl_file *source, char *str )
872 char *flag, *p = str;
874 if (!isspace( *p )) return;
875 while (*p && isspace(*p)) p++;
876 p = strtok( p, " \t" );
877 if (strcmp( p, "makedep" )) return;
879 while ((flag = strtok( NULL, " \t" )))
881 if (!strcmp( flag, "depend" ))
883 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
884 return;
886 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
888 if (strendswith( source->name, ".idl" ))
890 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
891 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
892 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
893 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
894 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
895 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
896 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
897 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
899 else if (strendswith( source->name, ".rc" ))
901 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
903 else if (strendswith( source->name, ".sfd" ))
905 if (!strcmp( flag, "font" ))
907 struct strarray *array = source->args;
909 if (!array)
911 source->args = array = xmalloc( sizeof(*array) );
912 *array = empty_strarray;
913 source->flags |= FLAG_SFD_FONTS;
915 strarray_add( array, xstrdup( strtok( NULL, "" )));
916 return;
919 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
924 /*******************************************************************
925 * parse_cpp_directive
927 static void parse_cpp_directive( struct incl_file *source, char *str )
929 while (*str && isspace(*str)) str++;
930 if (*str++ != '#') return;
931 while (*str && isspace(*str)) str++;
933 if (!strncmp( str, "include", 7 ))
934 parse_include_directive( source, str + 7 );
935 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
936 parse_include_directive( source, str + 6 );
937 else if (!strncmp( str, "pragma", 6 ))
938 parse_pragma_directive( source, str + 6 );
942 /*******************************************************************
943 * parse_idl_file
945 * If for_h_file is non-zero, it means we are not interested in the idl file
946 * itself, but only in the contents of the .h file that will be generated from it.
948 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
950 char *buffer, *include;
952 input_line = 0;
953 if (for_h_file)
955 /* generated .h file always includes these */
956 add_include( pFile, "rpc.h", 1 );
957 add_include( pFile, "rpcndr.h", 1 );
960 while ((buffer = get_line( file )))
962 char quote;
963 char *p = buffer;
964 while (*p && isspace(*p)) p++;
966 if (!strncmp( p, "import", 6 ))
968 p += 6;
969 while (*p && isspace(*p)) p++;
970 if (*p != '"') continue;
971 include = ++p;
972 while (*p && (*p != '"')) p++;
973 if (!*p) fatal_error( "malformed import directive\n" );
974 *p = 0;
975 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
976 add_include( pFile, include, 0 );
977 continue;
980 if (for_h_file) /* only check for #include inside cpp_quote */
982 if (strncmp( p, "cpp_quote", 9 )) continue;
983 p += 9;
984 while (*p && isspace(*p)) p++;
985 if (*p++ != '(') continue;
986 while (*p && isspace(*p)) p++;
987 if (*p++ != '"') continue;
988 if (*p++ != '#') continue;
989 while (*p && isspace(*p)) p++;
990 if (strncmp( p, "include", 7 )) continue;
991 p += 7;
992 while (*p && isspace(*p)) p++;
993 if (*p == '\\' && p[1] == '"')
995 p += 2;
996 quote = '"';
998 else
1000 if (*p++ != '<' ) continue;
1001 quote = '>';
1003 include = p;
1004 while (*p && (*p != quote)) p++;
1005 if (!*p || (quote == '"' && p[-1] != '\\'))
1006 fatal_error( "malformed #include directive inside cpp_quote\n" );
1007 if (quote == '"') p--; /* remove backslash */
1008 *p = 0;
1009 add_include( pFile, include, (quote == '>') );
1010 continue;
1013 parse_cpp_directive( pFile, p );
1017 /*******************************************************************
1018 * parse_c_file
1020 static void parse_c_file( struct incl_file *pFile, FILE *file )
1022 char *buffer;
1024 input_line = 0;
1025 while ((buffer = get_line( file )))
1027 parse_cpp_directive( pFile, buffer );
1032 /*******************************************************************
1033 * parse_rc_file
1035 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1037 char *buffer, *include;
1039 input_line = 0;
1040 while ((buffer = get_line( file )))
1042 char quote;
1043 char *p = buffer;
1044 while (*p && isspace(*p)) p++;
1046 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1048 p += 2;
1049 while (*p && isspace(*p)) p++;
1050 if (strncmp( p, "@makedep:", 9 )) continue;
1051 p += 9;
1052 while (*p && isspace(*p)) p++;
1053 quote = '"';
1054 if (*p == quote)
1056 include = ++p;
1057 while (*p && *p != quote) p++;
1059 else
1061 include = p;
1062 while (*p && !isspace(*p) && *p != '*') p++;
1064 if (!*p)
1065 fatal_error( "malformed makedep comment\n" );
1066 *p = 0;
1067 add_include( pFile, include, (quote == '>') );
1068 continue;
1071 parse_cpp_directive( pFile, buffer );
1076 /*******************************************************************
1077 * parse_in_file
1079 static void parse_in_file( struct incl_file *source, FILE *file )
1081 char *p, *buffer;
1083 /* make sure it gets rebuilt when the version changes */
1084 add_include( source, "config.h", 1 );
1086 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1088 input_line = 0;
1089 while ((buffer = get_line( file )))
1091 if (strncmp( buffer, ".TH", 3 )) continue;
1092 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1093 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1094 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1095 source->args = xstrdup( p );
1096 return;
1101 /*******************************************************************
1102 * parse_sfd_file
1104 static void parse_sfd_file( struct incl_file *source, FILE *file )
1106 char *p, *eol, *buffer;
1108 input_line = 0;
1109 while ((buffer = get_line( file )))
1111 if (strncmp( buffer, "UComments:", 10 )) continue;
1112 p = buffer + 10;
1113 while (*p == ' ') p++;
1114 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1116 p++;
1117 buffer[strlen(buffer) - 1] = 0;
1119 while ((eol = strstr( p, "+AAoA" )))
1121 *eol = 0;
1122 while (*p && isspace(*p)) p++;
1123 if (*p++ == '#')
1125 while (*p && isspace(*p)) p++;
1126 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1128 p = eol + 5;
1130 while (*p && isspace(*p)) p++;
1131 if (*p++ != '#') return;
1132 while (*p && isspace(*p)) p++;
1133 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1134 return;
1139 /*******************************************************************
1140 * parse_file
1142 static void parse_file( struct incl_file *source, int src )
1144 FILE *file;
1146 /* don't try to open certain types of files */
1147 if (strendswith( source->name, ".tlb" ))
1149 source->filename = xstrdup( source->name );
1150 return;
1153 file = src ? open_src_file( source ) : open_include_file( source );
1154 if (!file) return;
1155 input_file_name = source->filename;
1157 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1158 parse_idl_file( source, file, 1 );
1159 else if (strendswith( source->filename, ".idl" ))
1160 parse_idl_file( source, file, 0 );
1161 else if (strendswith( source->filename, ".c" ) ||
1162 strendswith( source->filename, ".m" ) ||
1163 strendswith( source->filename, ".h" ) ||
1164 strendswith( source->filename, ".l" ) ||
1165 strendswith( source->filename, ".y" ))
1166 parse_c_file( source, file );
1167 else if (strendswith( source->filename, ".rc" ))
1168 parse_rc_file( source, file );
1169 else if (strendswith( source->filename, ".in" ))
1170 parse_in_file( source, file );
1171 else if (strendswith( source->filename, ".sfd" ))
1172 parse_sfd_file( source, file );
1173 fclose(file);
1174 input_file_name = NULL;
1178 /*******************************************************************
1179 * add_src_file
1181 * Add a source file to the list.
1183 static struct incl_file *add_src_file( const char *name )
1185 struct incl_file *file;
1187 if ((file = find_src_file( name ))) return file; /* we already have it */
1188 file = xmalloc( sizeof(*file) );
1189 memset( file, 0, sizeof(*file) );
1190 file->name = xstrdup(name);
1191 list_add_tail( &sources, &file->entry );
1192 parse_file( file, 1 );
1193 return file;
1197 /*******************************************************************
1198 * get_make_variable
1200 static char *get_make_variable( const char *name )
1202 unsigned int i;
1204 for (i = 0; i < cmdline_vars.count; i += 2)
1205 if (!strcmp( cmdline_vars.str[i], name ))
1206 return xstrdup( cmdline_vars.str[i + 1] );
1208 for (i = 0; i < make_vars.count; i += 2)
1209 if (!strcmp( make_vars.str[i], name ))
1210 return xstrdup( make_vars.str[i + 1] );
1211 return NULL;
1215 /*******************************************************************
1216 * get_expanded_make_variable
1218 static char *get_expanded_make_variable( const char *name )
1220 char *p, *end, *var, *expand, *tmp;
1222 expand = get_make_variable( name );
1223 if (!expand) return NULL;
1225 p = expand;
1226 while ((p = strchr( p, '$' )))
1228 if (p[1] == '(')
1230 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1231 *end++ = 0;
1232 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1233 var = get_make_variable( p + 2 );
1234 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1235 free( var );
1237 else if (p[1] == '{') /* don't expand ${} variables */
1239 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1240 p = end + 1;
1241 continue;
1243 else if (p[1] == '$')
1245 tmp = replace_substr( expand, p, 2, "$" );
1247 else fatal_error( "syntax error in '%s'\n", expand );
1249 /* switch to the new string */
1250 p = tmp + (p - expand);
1251 free( expand );
1252 expand = tmp;
1255 /* consider empty variables undefined */
1256 p = expand;
1257 while (*p && isspace(*p)) p++;
1258 if (*p) return expand;
1259 free( expand );
1260 return NULL;
1264 /*******************************************************************
1265 * get_expanded_make_var_array
1267 static struct strarray get_expanded_make_var_array( const char *name )
1269 struct strarray ret = empty_strarray;
1270 char *value, *token;
1272 if ((value = get_expanded_make_variable( name )))
1273 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1274 strarray_add( &ret, token );
1275 return ret;
1279 /*******************************************************************
1280 * set_make_variable
1282 static int set_make_variable( struct strarray *array, const char *assignment )
1284 unsigned int i;
1285 char *p, *name;
1287 p = name = xstrdup( assignment );
1288 while (isalnum(*p) || *p == '_') p++;
1289 if (name == p) return 0; /* not a variable */
1290 if (isspace(*p))
1292 *p++ = 0;
1293 while (isspace(*p)) p++;
1295 if (*p != '=') return 0; /* not an assignment */
1296 *p++ = 0;
1297 while (isspace(*p)) p++;
1299 /* redefining a variable replaces the previous value */
1300 for (i = 0; i < array->count; i += 2)
1302 if (strcmp( array->str[i], name )) continue;
1303 array->str[i + 1] = p;
1304 return 1;
1306 strarray_add( array, name );
1307 strarray_add( array, p );
1308 return 1;
1312 /*******************************************************************
1313 * parse_makefile
1315 static void parse_makefile(void)
1317 char *buffer;
1318 FILE *file;
1320 input_file_name = base_dir_path( makefile_name );
1321 if (!(file = fopen( input_file_name, "r" )))
1323 fatal_perror( "open" );
1324 exit( 1 );
1327 input_line = 0;
1328 while ((buffer = get_line( file )))
1330 if (Separator && !strncmp( buffer, Separator, strlen(Separator) )) break;
1331 if (*buffer == '\t') continue; /* command */
1332 while (isspace( *buffer )) buffer++;
1333 if (*buffer == '#') continue; /* comment */
1334 set_make_variable( &make_vars, buffer );
1336 fclose( file );
1337 input_file_name = NULL;
1341 /*******************************************************************
1342 * add_generated_sources
1344 static void add_generated_sources(void)
1346 struct incl_file *source, *next, *file;
1348 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1350 if (source->flags & FLAG_IDL_CLIENT)
1352 file = add_generated_source( replace_extension( source->name, ".idl", "_c.c" ), NULL );
1353 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1355 if (source->flags & FLAG_IDL_SERVER)
1357 file = add_generated_source( replace_extension( source->name, ".idl", "_s.c" ), NULL );
1358 add_include( file, "wine/exception.h", 0 );
1359 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1361 if (source->flags & FLAG_IDL_IDENT)
1363 file = add_generated_source( replace_extension( source->name, ".idl", "_i.c" ), NULL );
1364 add_include( file, "rpc.h", 0 );
1365 add_include( file, "rpcndr.h", 0 );
1366 add_include( file, "guiddef.h", 0 );
1368 if (source->flags & FLAG_IDL_PROXY)
1370 file = add_generated_source( "dlldata.o", "dlldata.c" );
1371 add_include( file, "objbase.h", 0 );
1372 add_include( file, "rpcproxy.h", 0 );
1373 file = add_generated_source( replace_extension( source->name, ".idl", "_p.c" ), NULL );
1374 add_include( file, "objbase.h", 0 );
1375 add_include( file, "rpcproxy.h", 0 );
1376 add_include( file, "wine/exception.h", 0 );
1377 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1379 if (source->flags & FLAG_IDL_REGTYPELIB)
1381 add_generated_source( replace_extension( source->name, ".idl", "_t.res" ), NULL );
1383 if (source->flags & FLAG_IDL_REGISTER)
1385 add_generated_source( replace_extension( source->name, ".idl", "_r.res" ), NULL );
1387 if (strendswith( source->name, ".y" ))
1389 file = add_generated_source( replace_extension( source->name, ".y", ".tab.c" ), NULL );
1390 /* steal the includes list from the source file */
1391 file->files_count = source->files_count;
1392 file->files_size = source->files_size;
1393 file->files = source->files;
1394 source->files_count = source->files_size = 0;
1395 source->files = NULL;
1397 if (strendswith( source->name, ".l" ))
1399 file = add_generated_source( replace_extension( source->name, ".l", ".yy.c" ), NULL );
1400 /* steal the includes list from the source file */
1401 file->files_count = source->files_count;
1402 file->files_size = source->files_size;
1403 file->files = source->files;
1404 source->files_count = source->files_size = 0;
1405 source->files = NULL;
1408 if (get_make_variable( "TESTDLL" ))
1410 file = add_generated_source( "testlist.o", "testlist.c" );
1411 add_include( file, "wine/test.h", 0 );
1416 /*******************************************************************
1417 * create_dir
1419 static void create_dir( const char *dir )
1421 char *p, *path;
1423 p = path = xstrdup( dir );
1424 while ((p = strchr( p, '/' )))
1426 *p = 0;
1427 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1428 *p++ = '/';
1429 while (*p == '/') p++;
1431 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1432 free( path );
1436 /*******************************************************************
1437 * output_include
1439 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1441 int i;
1443 if (pFile->owner == owner) return;
1444 if (!pFile->filename) return;
1445 pFile->owner = owner;
1446 output_filename( pFile->filename );
1447 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1451 /*******************************************************************
1452 * output_sources
1454 static struct strarray output_sources(void)
1456 struct incl_file *source;
1457 unsigned int i;
1458 int is_win16 = 0;
1459 const char *dllext = ".so";
1460 struct strarray object_files = empty_strarray;
1461 struct strarray crossobj_files = empty_strarray;
1462 struct strarray res_files = empty_strarray;
1463 struct strarray clean_files = empty_strarray;
1464 struct strarray po_files = empty_strarray;
1465 struct strarray mo_files = empty_strarray;
1466 struct strarray mc_files = empty_strarray;
1467 struct strarray ok_files = empty_strarray;
1468 struct strarray dlldata_files = empty_strarray;
1469 struct strarray c2man_files = empty_strarray;
1470 struct strarray implib_objs = empty_strarray;
1471 struct strarray includes = empty_strarray;
1472 struct strarray subdirs = empty_strarray;
1473 struct strarray phony_targets = empty_strarray;
1474 struct strarray linguas = get_expanded_make_var_array( "LINGUAS" );
1475 struct strarray all_targets = get_expanded_make_var_array( "PROGRAMS" );
1476 struct strarray targetflags = get_expanded_make_var_array( "TARGETFLAGS" );
1477 struct strarray delayimports = get_expanded_make_var_array( "DELAYIMPORTS" );
1478 struct strarray extradllflags = get_expanded_make_var_array( "EXTRADLLFLAGS" );
1479 char *module = get_expanded_make_variable( "MODULE" );
1480 char *exeext = get_expanded_make_variable( "EXEEXT" );
1481 char *testdll = get_expanded_make_variable( "TESTDLL" );
1482 char *staticlib = get_expanded_make_variable( "STATICLIB" );
1483 char *crosstarget = get_expanded_make_variable( "CROSSTARGET" );
1485 if (exeext && !strcmp( exeext, ".exe" )) dllext = "";
1486 if (module && strendswith( module, ".a" )) staticlib = module;
1487 for (i = 0; i < extradllflags.count; i++) if (!strcmp( extradllflags.str[i], "-m16" )) is_win16 = 1;
1489 for (i = 0; i < linguas.count; i++)
1490 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( "po" ), linguas.str[i] ));
1492 strarray_add( &includes, "-I." );
1493 if (src_dir) strarray_add( &includes, strmake( "-I%s", src_dir ));
1494 if (parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( parent_dir )));
1495 if (top_obj_dir) strarray_add( &includes, strmake( "-I%s/include", top_obj_dir ));
1496 if (top_src_dir) strarray_add( &includes, strmake( "-I%s/include", top_src_dir ));
1497 if (use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( "include/msvcrt" )));
1498 strarray_addall( &includes, include_args );
1500 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1502 struct strarray extradefs;
1503 char *obj = xstrdup( source->name );
1504 char *ext = get_extension( obj );
1506 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1507 *ext++ = 0;
1509 if (src_dir && strchr( obj, '/' ))
1511 char *subdir = base_dir_path( obj );
1512 *strrchr( subdir, '/' ) = 0;
1513 strarray_add_uniq( &subdirs, subdir );
1516 extradefs = get_expanded_make_var_array( strmake( "%s_EXTRADEFS", obj ));
1518 if (!strcmp( ext, "y" )) /* yacc file */
1520 /* add source file dependency for parallel makes */
1521 char *header = strmake( "%s.tab.h", obj );
1523 if (find_include_file( header ))
1525 output( "%s.tab.h: %s\n", obj, source->filename );
1526 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1527 obj, obj, source->filename );
1528 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1529 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1531 else output( "%s.tab.c: %s\n", obj, source->filename );
1533 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1534 free( header );
1535 continue; /* no dependencies */
1537 else if (!strcmp( ext, "x" )) /* template file */
1539 output( "%s.h: %s%s %s\n", obj, tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1540 output( "\t%s%s -H -o $@ %s\n", tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1541 strarray_add( &clean_files, strmake( "%s.h", obj ));
1542 continue; /* no dependencies */
1544 else if (!strcmp( ext, "l" )) /* lex file */
1546 output( "%s.yy.c: %s\n", obj, source->filename );
1547 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1548 continue; /* no dependencies */
1550 else if (!strcmp( ext, "rc" )) /* resource file */
1552 strarray_add( &res_files, strmake( "%s.res", obj ));
1553 output( "%s.res: %s %s\n", obj, tools_path( "wrc" ), source->filename );
1554 output( "\t%s -o $@ %s", tools_path( "wrc" ), source->filename );
1555 if (is_win16) output_filename( "-m16" );
1556 else output_filenames( targetflags );
1557 output_filename( "--nostdinc" );
1558 output_filenames( includes );
1559 output_filenames( define_args );
1560 output_filenames( extradefs );
1561 if (mo_files.count && (source->flags & FLAG_RC_PO))
1563 strarray_add( &po_files, source->filename );
1564 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1565 output( "\n" );
1566 output( "%s.res:", obj );
1567 output_filenames( mo_files );
1568 output( "\n" );
1569 output( "rsrc.pot " );
1571 else output( "\n" );
1572 output( "%s.res:", obj );
1574 else if (!strcmp( ext, "mc" )) /* message file */
1576 strarray_add( &res_files, strmake( "%s.res", obj ));
1577 output( "%s.res: %s %s\n", obj, tools_path( "wmc" ), source->filename );
1578 output( "\t%s -U -O res -o $@ %s", tools_path( "wmc" ), source->filename );
1579 if (mo_files.count)
1581 strarray_add( &mc_files, source->filename );
1582 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1583 output( "\n" );
1584 output( "%s.res:", obj );
1585 output_filenames( mo_files );
1586 output( "\n" );
1587 output( "msg.pot " );
1589 else output( "\n" );
1590 output( "%s.res:", obj );
1592 else if (!strcmp( ext, "idl" )) /* IDL file */
1594 struct strarray targets = empty_strarray;
1595 char *dest;
1597 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1598 source->flags |= FLAG_IDL_HEADER;
1600 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1602 if (!(source->flags & idl_outputs[i].flag)) continue;
1603 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1604 if (!find_src_file( dest )) strarray_add( &clean_files, dest );
1605 strarray_add( &targets, dest );
1607 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1608 output_filenames( targets );
1609 output( ": %s\n", tools_path( "widl" ));
1610 output( "\t%s -o $@ %s", tools_path( "widl" ), source->filename );
1611 output_filenames( targetflags );
1612 output_filenames( includes );
1613 output_filenames( define_args );
1614 output_filenames( extradefs );
1615 output_filenames( get_expanded_make_var_array( "EXTRAIDLFLAGS" ));
1616 output( "\n" );
1617 output_filenames( targets );
1618 output( ": %s", source->filename );
1620 else if (!strcmp( ext, "in" )) /* .in file or man page */
1622 if (strendswith( obj, ".man" ) && source->args)
1624 char *dir, *dest = replace_extension( obj, ".man", "" );
1625 char *lang = strchr( dest, '.' );
1626 char *section = source->args;
1627 if (lang)
1629 *lang++ = 0;
1630 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1632 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1633 output( "install-man-pages:: %s\n", obj );
1634 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj, dir, dest, section );
1635 output( "uninstall::\n" );
1636 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1637 free( dest );
1638 free( dir );
1639 strarray_add( &all_targets, xstrdup(obj) );
1640 strarray_add_uniq( &phony_targets, "install-man-pages" );
1641 strarray_add_uniq( &phony_targets, "uninstall" );
1643 else strarray_add( &clean_files, xstrdup(obj) );
1644 output( "%s: %s\n", obj, source->filename );
1645 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1646 output( "%s:", obj );
1648 else if (!strcmp( ext, "sfd" )) /* font file */
1650 char *ttf_file = src_dir_path( strmake( "%s.ttf", obj ));
1651 char *fontforge = get_expanded_make_variable( "FONTFORGE" );
1652 if (fontforge && !src_dir)
1654 output( "%s: %s\n", ttf_file, source->filename );
1655 output( "\t%s -script %s %s $@\n",
1656 fontforge, top_dir_path( "fonts/genttf.ff" ), source->filename );
1658 if (source->flags & FLAG_INSTALL)
1660 output( "install install-lib::\n" );
1661 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1662 output( "uninstall::\n" );
1663 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1665 if (source->flags & FLAG_SFD_FONTS)
1667 struct strarray *array = source->args;
1669 for (i = 0; i < array->count; i++)
1671 char *font = strtok( xstrdup(array->str[i]), " \t" );
1672 char *args = strtok( NULL, "" );
1674 strarray_add( &all_targets, font );
1675 output( "%s: %s %s\n", font, tools_path( "sfnt2fon" ), ttf_file );
1676 output( "\t%s -o $@ %s %s\n", tools_path( "sfnt2fon" ), ttf_file, args );
1677 output( "install install-lib:: %s\n", font );
1678 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n", font, font );
1679 output( "uninstall::\n" );
1680 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1681 strarray_add_uniq( &phony_targets, "install" );
1682 strarray_add_uniq( &phony_targets, "install-lib" );
1683 strarray_add_uniq( &phony_targets, "uninstall" );
1686 continue; /* no dependencies */
1688 else if (!strcmp( ext, "svg" )) /* svg file */
1690 char *convert = get_expanded_make_variable( "CONVERT" );
1691 char *rsvg = get_expanded_make_variable( "RSVG" );
1692 char *icotool = get_expanded_make_variable( "ICOTOOL" );
1693 if (convert && rsvg && icotool && !src_dir)
1695 output( "%s.ico %s.bmp: %s\n", obj, obj, source->filename );
1696 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
1697 convert, icotool, rsvg, top_dir_path( "tools/buildimage" ), source->filename );
1699 free( convert );
1700 free( rsvg );
1701 free( icotool );
1702 continue; /* no dependencies */
1704 else if (!strcmp( ext, "res" ))
1706 strarray_add( &res_files, source->name );
1707 continue; /* no dependencies */
1709 else
1711 int need_cross = testdll || (source->flags & FLAG_C_IMPLIB) || (module && staticlib);
1713 if ((source->flags & FLAG_GENERATED) && (!testdll || strcmp( source->filename, "testlist.c" )))
1714 strarray_add( &clean_files, source->filename );
1715 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1716 strarray_add( &object_files, strmake( "%s.o", obj ));
1717 output( "%s.o: %s\n", obj, source->filename );
1718 output( "\t$(CC) -c -o $@ %s", source->filename );
1719 output_filenames( includes );
1720 output_filenames( define_args );
1721 output_filenames( extradefs );
1722 if (module || staticlib || testdll) output_filenames( dllflags );
1723 output_filenames( get_expanded_make_var_array( "EXTRACFLAGS" ));
1724 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1725 output_filename( "$(CFLAGS)" );
1726 output( "\n" );
1727 if (crosstarget && need_cross)
1729 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1730 output( "%s.cross.o: %s\n", obj, source->filename );
1731 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1732 output_filenames( includes );
1733 output_filenames( define_args );
1734 output_filenames( extradefs );
1735 output_filename( "-DWINE_CROSSTEST" );
1736 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1737 output_filename( "$(CFLAGS)" );
1738 output( "\n" );
1740 if (testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1742 strarray_add( &ok_files, strmake( "%s.ok", obj ));
1743 output( "%s.ok:\n", obj );
1744 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1745 top_dir_path( "tools/runtest" ), top_obj_dir,
1746 testdll, replace_extension( testdll, ".dll", "_test.exe" ), dllext, obj );
1748 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1749 strarray_add( &c2man_files, source->filename );
1750 output( "%s.o", obj );
1751 if (crosstarget && need_cross) output( " %s.cross.o", obj );
1752 output( ":" );
1754 free( obj );
1756 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
1757 output( "\n" );
1760 /* rules for files that depend on multiple sources */
1762 if (po_files.count)
1764 output( "rsrc.pot: %s", tools_path( "wrc" ) );
1765 output_filenames( po_files );
1766 output( "\n" );
1767 output( "\t%s -O pot -o $@", tools_path( "wrc" ));
1768 output_filenames( po_files );
1769 if (is_win16) output_filename( "-m16" );
1770 else output_filenames( targetflags );
1771 output_filename( "--nostdinc" );
1772 output_filenames( includes );
1773 output_filenames( define_args );
1774 output( "\n" );
1775 strarray_add( &clean_files, "rsrc.pot" );
1778 if (mc_files.count)
1780 output( "msg.pot: %s", tools_path( "wmc" ));
1781 output_filenames( mc_files );
1782 output( "\n" );
1783 output( "\t%s -O pot -o $@", tools_path( "wmc" ));
1784 output_filenames( mc_files );
1785 output( "\n" );
1786 strarray_add( &clean_files, "msg.pot" );
1789 if (dlldata_files.count)
1791 output( "dlldata.c: %s %s\n", tools_path( "widl" ), src_dir_path( "Makefile.in" ));
1792 output( "\t%s --dlldata-only -o $@", tools_path( "widl" ));
1793 output_filenames( dlldata_files );
1794 output( "\n" );
1797 if (module && !staticlib)
1799 char *importlib = get_expanded_make_variable( "IMPORTLIB" );
1800 struct strarray all_libs = empty_strarray;
1801 char *spec_file = NULL;
1803 if (!appmode.count) spec_file = src_dir_path( replace_extension( module, ".dll", ".spec" ));
1804 for (i = 0; i < delayimports.count; i++)
1805 strarray_add( &all_libs, strmake( "-l%s", delayimports.str[i] ));
1806 for (i = 0; i < imports.count; i++)
1807 strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1808 for (i = 0; i < delayimports.count; i++)
1809 strarray_add( &all_libs, strmake( "-Wb,-d%s", delayimports.str[i] ));
1810 strarray_add( &all_libs, "-lwine" );
1811 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBPORT" ));
1812 strarray_addall( &all_libs, get_expanded_make_var_array( "EXTRALIBS" ));
1813 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1815 if (*dllext)
1817 strarray_add( &all_targets, strmake( "%s%s", module, dllext ));
1818 strarray_add( &all_targets, strmake( "%s.fake", module ));
1819 output( "%s%s %s.fake:", module, dllext, module );
1821 else
1823 strarray_add( &all_targets, module );
1824 output( "%s:", module );
1826 if (spec_file) output_filename( spec_file );
1827 output_filenames( object_files );
1828 output_filenames( res_files );
1829 output( "\n" );
1830 output( "\t%s -o $@", tools_path( "winegcc" ));
1831 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1832 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1833 output_filenames( targetflags );
1834 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1835 if (spec_file)
1837 output( " -shared %s", spec_file );
1838 output_filenames( extradllflags );
1840 else output_filenames( appmode );
1841 output_filenames( object_files );
1842 output_filenames( res_files );
1843 output_filenames( all_libs );
1844 output_filename( "$(LDFLAGS)" );
1845 output( "\n" );
1847 if (spec_file && importlib)
1849 if (*dllext)
1851 strarray_add( &clean_files, strmake( "lib%s.def", importlib ));
1852 output( "lib%s.def: %s %s\n", importlib, tools_path( "winebuild" ), spec_file );
1853 output( "\t%s -w --def -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1854 output_filenames( targetflags );
1855 if (is_win16) output_filename( "-m16" );
1856 output( "\n" );
1857 if (implib_objs.count)
1859 strarray_add( &clean_files, strmake( "lib%s.def.a", importlib ));
1860 output( "lib%s.def.a:", importlib );
1861 output_filenames( implib_objs );
1862 output( "\n" );
1863 output( "\t$(RM) $@\n" );
1864 output( "\t$(AR) $(ARFLAGS) $@" );
1865 output_filenames( implib_objs );
1866 output( "\n" );
1867 output( "\t$(RANLIB) $@\n" );
1870 else
1872 strarray_add( &clean_files, strmake( "lib%s.a", importlib ));
1873 output( "lib%s.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1874 output_filenames( implib_objs );
1875 output( "\n" );
1876 output( "\t%s -w --implib -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1877 output_filenames( targetflags );
1878 output_filenames( implib_objs );
1879 output( "\n" );
1881 if (crosstarget && !is_win16)
1883 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
1884 strarray_add( &clean_files, strmake( "lib%s.cross.a", importlib ));
1885 output( "lib%s.cross.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1886 output_filenames( cross_files );
1887 output( "\n" );
1888 output( "\t%s -b %s -w --implib -o $@ --export %s",
1889 tools_path( "winebuild" ), crosstarget, spec_file );
1890 output_filenames( cross_files );
1891 output( "\n" );
1895 if (spec_file)
1897 if (c2man_files.count && top_obj_dir)
1899 char *manext = get_expanded_make_variable( "api_manext" );
1901 output( "manpages::\n" );
1902 output( "\t%s -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file, top_obj_dir );
1903 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1904 output_filename( strmake( "-o %s/documentation/man%s", top_obj_dir, manext ? manext : "3w" ));
1905 output_filenames( c2man_files );
1906 output( "\n" );
1907 output( "htmlpages::\n" );
1908 output( "\t%s -Th -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file, top_obj_dir );
1909 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1910 output_filename( strmake( "-o %s/documentation/html", top_obj_dir ));
1911 output_filenames( c2man_files );
1912 output( "\n" );
1913 output( "sgmlpages::\n" );
1914 output( "\t%s -Ts -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file, top_obj_dir );
1915 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1916 output_filename( strmake( "-o %s/documentation/api-guide", top_obj_dir ));
1917 output_filenames( c2man_files );
1918 output( "\n" );
1919 output( "xmlpages::\n" );
1920 output( "\t%s -Tx -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file, top_obj_dir );
1921 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1922 output_filename( strmake( "-o %s/documentation/api-guide-xml", top_obj_dir ));
1923 output_filenames( c2man_files );
1924 output( "\n" );
1925 strarray_add( &phony_targets, "manpages" );
1926 strarray_add( &phony_targets, "htmlpages" );
1927 strarray_add( &phony_targets, "sgmlpages" );
1928 strarray_add( &phony_targets, "xmlpages" );
1930 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1934 if (staticlib)
1936 strarray_add( &all_targets, staticlib );
1937 output( "%s:", staticlib );
1938 output_filenames( object_files );
1939 output( "\n\t$(RM) $@\n" );
1940 output( "\t$(AR) $(ARFLAGS) $@" );
1941 output_filenames( object_files );
1942 output( "\n\t$(RANLIB) $@\n" );
1943 if (crosstarget && module)
1945 char *name = replace_extension( staticlib, ".a", ".cross.a" );
1947 strarray_add( &all_targets, name );
1948 output( "%s:", name );
1949 output_filenames( crossobj_files );
1950 output( "\n\t$(RM) $@\n" );
1951 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
1952 output_filenames( crossobj_files );
1953 output( "\n\t%s-ranlib $@\n", crosstarget );
1957 if (testdll)
1959 char *testmodule = replace_extension( testdll, ".dll", "_test.exe" );
1960 char *stripped = replace_extension( testdll, ".dll", "_test-stripped.exe" );
1961 struct strarray all_libs = empty_strarray;
1963 for (i = 0; i < imports.count; i++) strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1964 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1966 strarray_add( &all_targets, strmake( "%s%s", testmodule, dllext ));
1967 strarray_add( &clean_files, strmake( "%s%s", stripped, dllext ));
1968 output( "%s%s:\n", testmodule, dllext );
1969 output( "\t%s -o $@", tools_path( "winegcc" ));
1970 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1971 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1972 output_filenames( targetflags );
1973 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1974 output_filenames( appmode );
1975 output_filenames( object_files );
1976 output_filenames( res_files );
1977 output_filenames( all_libs );
1978 output_filename( "$(LDFLAGS)" );
1979 output( "\n" );
1980 output( "%s%s:\n", stripped, dllext );
1981 output( "\t%s -o $@", tools_path( "winegcc" ));
1982 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1983 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1984 output_filenames( targetflags );
1985 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1986 output_filename( strmake( "-Wb,-F,%s", testmodule ));
1987 output_filenames( appmode );
1988 output_filenames( object_files );
1989 output_filenames( res_files );
1990 output_filenames( all_libs );
1991 output_filename( "$(LDFLAGS)" );
1992 output( "\n" );
1993 output( "%s%s %s%s:", testmodule, dllext, stripped, dllext );
1994 output_filenames( object_files );
1995 output_filenames( res_files );
1996 output( "\n" );
1998 if (top_obj_dir)
2000 char *testres = replace_extension( testdll, ".dll", "_test.res" );
2001 output( "all: %s/%s\n", top_obj_dir_path( "programs/winetest" ), testres );
2002 output( "%s/%s: %s%s\n", top_obj_dir_path( "programs/winetest" ), testres, stripped, dllext );
2003 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2004 testmodule, stripped, dllext, tools_path( "wrc" ));
2007 if (crosstarget)
2009 char *crosstest = replace_extension( testdll, ".dll", "_crosstest.exe" );
2011 strarray_add( &clean_files, crosstest );
2012 output( "crosstest: %s\n", crosstest );
2013 output( "%s:", crosstest );
2014 output_filenames( crossobj_files );
2015 output_filenames( res_files );
2016 output( "\n" );
2017 output( "\t%s -o $@ -b %s", tools_path( "winegcc" ), crosstarget );
2018 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
2019 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
2020 output_filename( "--lib-suffix=.cross.a" );
2021 output_filenames( crossobj_files );
2022 output_filenames( res_files );
2023 output_filenames( all_libs );
2024 output_filename( "$(LDFLAGS)" );
2025 output( "\n" );
2026 strarray_add( &phony_targets, "crosstest" );
2029 output_filenames( ok_files );
2030 output( ": %s%s ../%s%s\n", testmodule, dllext, testdll, dllext );
2031 output( "check test:" );
2032 output_filenames( ok_files );
2033 output( "\n" );
2034 output( "testclean::\n" );
2035 output( "\t$(RM)" );
2036 output_filenames( ok_files );
2037 output( "\n" );
2038 strarray_addall( &clean_files, ok_files );
2039 strarray_add( &phony_targets, "check" );
2040 strarray_add( &phony_targets, "test" );
2041 strarray_add( &phony_targets, "testclean" );
2042 testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2045 if (all_targets.count)
2047 output( "all:" );
2048 output_filenames( all_targets );
2049 output( "\n" );
2052 strarray_addall( &clean_files, object_files );
2053 strarray_addall( &clean_files, crossobj_files );
2054 strarray_addall( &clean_files, res_files );
2055 strarray_addall( &clean_files, all_targets );
2056 strarray_addall( &clean_files, get_expanded_make_var_array( "EXTRA_TARGETS" ));
2058 if (clean_files.count)
2060 output( "clean::\n" );
2061 output( "\t$(RM)" );
2062 output_filenames( clean_files );
2063 output( "\n" );
2064 strarray_add( &phony_targets, "clean" );
2067 if (top_obj_dir)
2069 output( "depend:\n" );
2070 output( "\t@cd %s && $(MAKE) %s\n", top_obj_dir, base_dir_path( "depend" ));
2071 strarray_add( &phony_targets, "depend" );
2074 if (phony_targets.count)
2076 output( ".PHONY:" );
2077 output_filenames( phony_targets );
2078 output( "\n" );
2081 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2083 return clean_files;
2087 /*******************************************************************
2088 * create_temp_file
2090 static FILE *create_temp_file( const char *orig )
2092 char *name = xmalloc( strlen(orig) + 13 );
2093 unsigned int i, id = getpid();
2094 int fd;
2095 FILE *ret = NULL;
2097 for (i = 0; i < 100; i++)
2099 sprintf( name, "%s.tmp%08x", orig, id );
2100 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2102 ret = fdopen( fd, "w" );
2103 break;
2105 if (errno != EEXIST) break;
2106 id += 7777;
2108 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2109 temp_file_name = name;
2110 return ret;
2114 /*******************************************************************
2115 * rename_temp_file
2117 static void rename_temp_file( const char *dest )
2119 int ret = rename( temp_file_name, dest );
2120 if (ret == -1 && errno == EEXIST)
2122 /* rename doesn't overwrite on windows */
2123 unlink( dest );
2124 ret = rename( temp_file_name, dest );
2126 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2127 temp_file_name = NULL;
2131 /*******************************************************************
2132 * are_files_identical
2134 static int are_files_identical( FILE *file1, FILE *file2 )
2136 for (;;)
2138 char buffer1[8192], buffer2[8192];
2139 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2140 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2141 if (size1 != size2) return 0;
2142 if (!size1) return feof( file1 ) && feof( file2 );
2143 if (memcmp( buffer1, buffer2, size1 )) return 0;
2148 /*******************************************************************
2149 * rename_temp_file_if_changed
2151 static void rename_temp_file_if_changed( const char *dest )
2153 FILE *file1, *file2;
2154 int do_rename = 1;
2156 if ((file1 = fopen( dest, "r" )))
2158 if ((file2 = fopen( temp_file_name, "r" )))
2160 do_rename = !are_files_identical( file1, file2 );
2161 fclose( file2 );
2163 fclose( file1 );
2165 if (!do_rename)
2167 unlink( temp_file_name );
2168 temp_file_name = NULL;
2170 else rename_temp_file( dest );
2174 /*******************************************************************
2175 * output_testlist
2177 static void output_testlist( const char *dest, struct strarray files )
2179 int i;
2181 output_file = create_temp_file( dest );
2183 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2184 output( "#define WIN32_LEAN_AND_MEAN\n" );
2185 output( "#include <windows.h>\n\n" );
2186 output( "#define STANDALONE\n" );
2187 output( "#include \"wine/test.h\"\n\n" );
2189 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2190 output( "\n" );
2191 output( "const struct test winetest_testlist[] =\n" );
2192 output( "{\n" );
2193 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2194 output( " { 0, 0 }\n" );
2195 output( "};\n" );
2197 if (fclose( output_file )) fatal_perror( "write" );
2198 output_file = NULL;
2199 rename_temp_file_if_changed( dest );
2203 /*******************************************************************
2204 * output_gitignore
2206 static void output_gitignore( const char *dest, struct strarray files )
2208 int i;
2210 output_file = create_temp_file( dest );
2212 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2213 for (i = 0; i < files.count; i++)
2215 if (!strchr( files.str[i], '/' )) output( "/" );
2216 output( "%s\n", files.str[i] );
2219 if (fclose( output_file )) fatal_perror( "write" );
2220 output_file = NULL;
2221 rename_temp_file( dest );
2225 /*******************************************************************
2226 * output_dependencies
2228 static void output_dependencies( const char *path )
2230 struct strarray targets, ignore_files = empty_strarray;
2232 if (Separator && ((output_file = fopen( path, "r" ))))
2234 char buffer[1024];
2235 FILE *tmp_file = create_temp_file( path );
2236 int found = 0;
2238 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2240 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2241 found = !strncmp( buffer, Separator, strlen(Separator) );
2243 if (fclose( output_file )) fatal_perror( "write" );
2244 output_file = tmp_file;
2245 if (!found) output( "\n%s\n", Separator );
2247 else
2249 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2250 fatal_perror( "%s", path );
2253 testlist_files = empty_strarray;
2254 targets = output_sources();
2256 fclose( output_file );
2257 output_file = NULL;
2258 if (temp_file_name) rename_temp_file( path );
2260 strarray_add( &ignore_files, ".gitignore" );
2261 strarray_add( &ignore_files, "Makefile" );
2262 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2263 strarray_addall( &ignore_files, targets );
2265 if (testlist_files.count) output_testlist( base_dir_path( "testlist.c" ), testlist_files );
2266 if (!src_dir && base_dir) output_gitignore( base_dir_path( ".gitignore" ), ignore_files );
2270 /*******************************************************************
2271 * update_makefile
2273 static void update_makefile( const char *path )
2275 static const char *source_vars[] =
2277 "C_SRCS",
2278 "OBJC_SRCS",
2279 "RC_SRCS",
2280 "MC_SRCS",
2281 "IDL_SRCS",
2282 "BISON_SRCS",
2283 "LEX_SRCS",
2284 "XTEMPLATE_SRCS",
2285 "SVG_SRCS",
2286 "FONT_SRCS",
2287 "IN_SRCS",
2288 "MANPAGES",
2289 NULL
2291 const char **var;
2292 unsigned int i;
2293 struct strarray value;
2294 struct incl_file *file;
2296 base_dir = path;
2297 if (!strcmp( base_dir, "." )) base_dir = NULL;
2298 output_file_name = base_dir_path( makefile_name );
2299 parse_makefile();
2301 src_dir = get_expanded_make_variable( "srcdir" );
2302 top_src_dir = get_expanded_make_variable( "top_srcdir" );
2303 top_obj_dir = get_expanded_make_variable( "top_builddir" );
2304 parent_dir = get_expanded_make_variable( "PARENTSRC" );
2305 tools_dir = get_expanded_make_variable( "TOOLSDIR" );
2306 tools_ext = get_expanded_make_variable( "TOOLSEXT" );
2308 /* ignore redundant source paths */
2309 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
2310 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
2311 if (tools_dir && top_obj_dir && !strcmp( tools_dir, top_obj_dir )) tools_dir = NULL;
2312 if (top_obj_dir && !strcmp( top_obj_dir, "." )) top_obj_dir = NULL;
2314 appmode = get_expanded_make_var_array( "APPMODE" );
2315 dllflags = get_expanded_make_var_array( "DLLFLAGS" );
2316 imports = get_expanded_make_var_array( "IMPORTS" );
2318 use_msvcrt = 0;
2319 for (i = 0; i < appmode.count && !use_msvcrt; i++)
2320 use_msvcrt = !strcmp( appmode.str[i], "-mno-cygwin" );
2321 for (i = 0; i < imports.count && !use_msvcrt; i++)
2322 use_msvcrt = !strncmp( imports.str[i], "msvcr", 5 );
2324 include_args = empty_strarray;
2325 define_args = empty_strarray;
2326 strarray_add( &define_args, "-D__WINESRC__" );
2328 if (!tools_ext) tools_ext = "";
2330 value = get_expanded_make_var_array( "EXTRAINCL" );
2331 for (i = 0; i < value.count; i++)
2332 if (!strncmp( value.str[i], "-I", 2 ))
2333 strarray_add_uniq( &include_args, value.str[i] );
2334 else
2335 strarray_add_uniq( &define_args, value.str[i] );
2336 strarray_addall( &define_args, get_expanded_make_var_array( "EXTRADEFS" ));
2338 if (use_msvcrt) strarray_add( &dllflags, get_expanded_make_variable( "MSVCRTFLAGS" ));
2340 list_init( &sources );
2341 list_init( &includes );
2343 for (var = source_vars; *var; var++)
2345 value = get_expanded_make_var_array( *var );
2346 for (i = 0; i < value.count; i++) add_src_file( value.str[i] );
2349 add_generated_sources();
2351 value = get_expanded_make_var_array( "EXTRA_OBJS" );
2352 for (i = 0; i < value.count; i++)
2354 /* default to .c for unknown extra object files */
2355 if (strendswith( value.str[i], ".o" ))
2356 add_generated_source( value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2357 else
2358 add_generated_source( value.str[i], NULL );
2361 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( file, 0 );
2362 output_dependencies( output_file_name );
2363 output_file_name = NULL;
2367 /*******************************************************************
2368 * parse_makeflags
2370 static void parse_makeflags( const char *flags )
2372 const char *p = flags;
2373 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2375 while (*p)
2377 while (isspace(*p)) p++;
2378 var = buffer;
2379 while (*p && !isspace(*p))
2381 if (*p == '\\' && p[1]) p++;
2382 *var++ = *p++;
2384 *var = 0;
2385 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2390 /*******************************************************************
2391 * parse_option
2393 static int parse_option( const char *opt )
2395 if (opt[0] != '-')
2397 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2398 return 0;
2400 switch(opt[1])
2402 case 'f':
2403 if (opt[2]) makefile_name = opt + 2;
2404 break;
2405 case 'R':
2406 relative_dir_mode = 1;
2407 break;
2408 case 's':
2409 if (opt[2]) Separator = opt + 2;
2410 else Separator = NULL;
2411 break;
2412 default:
2413 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2414 exit(1);
2416 return 1;
2420 /*******************************************************************
2421 * main
2423 int main( int argc, char *argv[] )
2425 const char *makeflags = getenv( "MAKEFLAGS" );
2426 int i, j;
2428 if (makeflags) parse_makeflags( makeflags );
2430 i = 1;
2431 while (i < argc)
2433 if (parse_option( argv[i] ))
2435 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2436 argc--;
2438 else i++;
2441 if (relative_dir_mode)
2443 char *relpath;
2445 if (argc != 3)
2447 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2448 exit( 1 );
2450 relpath = get_relative_path( argv[1], argv[2] );
2451 printf( "%s\n", relpath ? relpath : "." );
2452 exit( 0 );
2455 if (argc <= 1)
2457 fprintf( stderr, "%s", Usage );
2458 exit( 1 );
2460 atexit( cleanup_files );
2461 signal( SIGTERM, exit_on_signal );
2462 signal( SIGINT, exit_on_signal );
2463 #ifdef SIGHUP
2464 signal( SIGHUP, exit_on_signal );
2465 #endif
2467 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2468 return 0;