shell32/tests: Mark some test results as broken.
[wine/wine-gecko.git] / tools / makedep.c
blobd8788692ac70d29c5cb135a1433c5561c7407fee
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 * concat_paths
459 static char *concat_paths( const char *base, const char *path )
461 if (!base) return xstrdup( path[0] ? path : "." );
462 if (path[0] == '/') return xstrdup( path );
463 if (!path[0]) return xstrdup( base );
464 return strmake( "%s/%s", base, path );
468 /*******************************************************************
469 * base_dir_path
471 static char *base_dir_path( const char *path )
473 return concat_paths( base_dir, path );
477 /*******************************************************************
478 * src_dir_path
480 static char *src_dir_path( const char *path )
482 return concat_paths( src_dir, path );
486 /*******************************************************************
487 * top_obj_dir_path
489 static char *top_obj_dir_path( const char *path )
491 return concat_paths( top_obj_dir, path );
495 /*******************************************************************
496 * top_dir_path
498 static char *top_dir_path( const char *path )
500 if (top_src_dir) return concat_paths( top_src_dir, path );
501 return top_obj_dir_path( path );
505 /*******************************************************************
506 * tools_dir_path
508 static char *tools_dir_path( const char *path )
510 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
511 if (top_obj_dir) return strmake( "%s/tools/%s", top_obj_dir, path );
512 return strmake( "tools/%s", path );
516 /*******************************************************************
517 * tools_path
519 static char *tools_path( const char *name )
521 if (tools_dir) return strmake( "%s/tools/%s/%s%s", tools_dir, name, name, tools_ext );
522 if (top_obj_dir) return strmake( "%s/tools/%s/%s%s", top_obj_dir, name, name, tools_ext );
523 return strmake( "tools/%s/%s%s", name, name, tools_ext );
527 /*******************************************************************
528 * get_line
530 static char *get_line( FILE *file )
532 static char *buffer;
533 static unsigned int size;
535 if (!size)
537 size = 1024;
538 buffer = xmalloc( size );
540 if (!fgets( buffer, size, file )) return NULL;
541 input_line++;
543 for (;;)
545 char *p = buffer + strlen(buffer);
546 /* if line is larger than buffer, resize buffer */
547 while (p == buffer + size - 1 && p[-1] != '\n')
549 buffer = xrealloc( buffer, size * 2 );
550 if (!fgets( buffer + size - 1, size + 1, file )) break;
551 p = buffer + strlen(buffer);
552 size *= 2;
554 if (p > buffer && p[-1] == '\n')
556 *(--p) = 0;
557 if (p > buffer && p[-1] == '\r') *(--p) = 0;
558 if (p > buffer && p[-1] == '\\')
560 *(--p) = 0;
561 /* line ends in backslash, read continuation line */
562 if (!fgets( p, size - (p - buffer), file )) return buffer;
563 input_line++;
564 continue;
567 return buffer;
571 /*******************************************************************
572 * find_src_file
574 static struct incl_file *find_src_file( const char *name )
576 struct incl_file *file;
578 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
579 if (!strcmp( name, file->name )) return file;
580 return NULL;
583 /*******************************************************************
584 * find_include_file
586 static struct incl_file *find_include_file( const char *name )
588 struct incl_file *file;
590 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
591 if (!strcmp( name, file->name )) return file;
592 return NULL;
595 /*******************************************************************
596 * add_include
598 * Add an include file if it doesn't already exists.
600 static struct incl_file *add_include( struct incl_file *parent, const char *name, int system )
602 struct incl_file *include;
603 char *ext;
605 if (parent->files_count >= parent->files_size)
607 parent->files_size *= 2;
608 if (parent->files_size < 16) parent->files_size = 16;
609 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
612 /* enforce some rules for the Wine tree */
614 if (!memcmp( name, "../", 3 ))
615 fatal_error( "#include directive with relative path not allowed\n" );
617 if (!strcmp( name, "config.h" ))
619 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
620 fatal_error( "config.h must not be included by a header file\n" );
621 if (parent->files_count)
622 fatal_error( "config.h must be included before anything else\n" );
624 else if (!strcmp( name, "wine/port.h" ))
626 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
627 fatal_error( "wine/port.h must not be included by a header file\n" );
628 if (!parent->files_count) fatal_error( "config.h must be included before wine/port.h\n" );
629 if (parent->files_count > 1)
630 fatal_error( "wine/port.h must be included before everything except config.h\n" );
631 if (strcmp( parent->files[0]->name, "config.h" ))
632 fatal_error( "config.h must be included before wine/port.h\n" );
635 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
636 if (!strcmp( name, include->name )) goto found;
638 include = xmalloc( sizeof(*include) );
639 memset( include, 0, sizeof(*include) );
640 include->name = xstrdup(name);
641 include->included_by = parent;
642 include->included_line = input_line;
643 if (system) include->flags |= FLAG_SYSTEM;
644 list_add_tail( &includes, &include->entry );
645 found:
646 parent->files[parent->files_count++] = include;
647 return include;
651 /*******************************************************************
652 * add_generated_source
654 * Add a generated source file to the list.
656 static struct incl_file *add_generated_source( const char *name, const char *filename )
658 struct incl_file *file;
660 if ((file = find_src_file( name ))) return file; /* we already have it */
661 file = xmalloc( sizeof(*file) );
662 memset( file, 0, sizeof(*file) );
663 file->name = xstrdup( name );
664 file->filename = xstrdup( filename ? filename : name );
665 file->flags = FLAG_GENERATED;
666 list_add_tail( &sources, &file->entry );
667 return file;
671 /*******************************************************************
672 * open_file
674 static FILE *open_file( const char *path )
676 return fopen( base_dir_path( path ), "r" );
679 /*******************************************************************
680 * open_src_file
682 static FILE *open_src_file( struct incl_file *pFile )
684 FILE *file;
686 /* try in source dir */
687 pFile->filename = src_dir_path( pFile->name );
688 file = open_file( pFile->filename );
690 /* now try parent dir */
691 if (!file && parent_dir)
693 pFile->filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
694 file = open_file( pFile->filename );
696 if (!file) fatal_perror( "open %s", pFile->name );
697 return file;
701 /*******************************************************************
702 * open_include_file
704 static FILE *open_include_file( struct incl_file *pFile )
706 FILE *file = NULL;
707 char *filename, *p;
708 unsigned int i, len;
710 errno = ENOENT;
712 /* check for generated bison header */
714 if (strendswith( pFile->name, ".tab.h" ))
716 filename = src_dir_path( replace_extension( pFile->name, ".tab.h", ".y" ));
717 if ((file = open_file( filename )))
719 pFile->sourcename = filename;
720 pFile->filename = xstrdup( pFile->name );
721 /* don't bother to parse it */
722 fclose( file );
723 return NULL;
725 free( filename );
728 /* check for corresponding idl file in source dir */
730 if (strendswith( pFile->name, ".h" ))
732 filename = src_dir_path( replace_extension( pFile->name, ".h", ".idl" ));
733 if ((file = open_file( filename )))
735 pFile->sourcename = filename;
736 pFile->filename = xstrdup( pFile->name );
737 return file;
739 free( filename );
742 /* now try in source dir */
743 filename = src_dir_path( pFile->name );
744 if ((file = open_file( filename ))) goto found;
745 free( filename );
747 /* now try in parent source dir */
748 if (parent_dir)
750 filename = src_dir_path( strmake( "%s/%s", parent_dir, pFile->name ));
751 if ((file = open_file( filename ))) goto found;
752 free( filename );
755 /* check for corresponding idl file in global includes */
757 if (strendswith( pFile->name, ".h" ))
759 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".idl" )));
760 if ((file = open_file( filename )))
762 pFile->sourcename = filename;
763 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
764 return file;
766 free( filename );
769 /* check for corresponding .in file in global includes (for config.h.in) */
771 if (strendswith( pFile->name, ".h" ))
773 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".h.in" )));
774 if ((file = open_file( filename )))
776 pFile->sourcename = filename;
777 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
778 return file;
780 free( filename );
783 /* check for corresponding .x file in global includes */
785 if (strendswith( pFile->name, "tmpl.h" ))
787 filename = top_dir_path( strmake( "include/%s", replace_extension( pFile->name, ".h", ".x" )));
788 if ((file = open_file( filename )))
790 pFile->sourcename = filename;
791 pFile->filename = top_obj_dir_path( strmake( "include/%s", pFile->name ));
792 return file;
794 free( filename );
797 /* check in global includes source dir */
799 filename = top_dir_path( strmake( "include/%s", pFile->name ));
800 if ((file = open_file( filename ))) goto found;
802 /* check in global msvcrt includes */
803 if (use_msvcrt)
805 filename = top_dir_path( strmake( "include/msvcrt/%s", pFile->name ));
806 if ((file = open_file( filename ))) goto found;
809 /* now search in include paths */
810 for (i = 0; i < include_args.count; i++)
812 const char *dir = include_args.str[i] + 2; /* skip -I */
813 if (*dir == '/')
815 /* ignore absolute paths that don't point into the source dir */
816 if (!top_src_dir) continue;
817 len = strlen( top_src_dir );
818 if (strncmp( dir, top_src_dir, len )) continue;
819 if (dir[len] && dir[len] != '/') continue;
821 filename = strmake( "%s/%s", dir, pFile->name );
822 if ((file = open_file( filename ))) goto found;
823 free( filename );
825 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
827 /* try in src file directory */
828 if ((p = strrchr(pFile->included_by->filename, '/')))
830 int l = p - pFile->included_by->filename + 1;
831 filename = xmalloc(l + strlen(pFile->name) + 1);
832 memcpy( filename, pFile->included_by->filename, l );
833 strcpy( filename + l, pFile->name );
834 if ((file = open_file( filename ))) goto found;
835 free( filename );
838 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
839 perror( pFile->name );
840 pFile = pFile->included_by;
841 while (pFile && pFile->included_by)
843 const char *parent = pFile->included_by->sourcename;
844 if (!parent) parent = pFile->included_by->name;
845 fprintf( stderr, "%s:%d: note: %s was first included here\n",
846 parent, pFile->included_line, pFile->name );
847 pFile = pFile->included_by;
849 exit(1);
851 found:
852 pFile->filename = filename;
853 return file;
857 /*******************************************************************
858 * parse_include_directive
860 static void parse_include_directive( struct incl_file *source, char *str )
862 char quote, *include, *p = str;
864 while (*p && isspace(*p)) p++;
865 if (*p != '\"' && *p != '<' ) return;
866 quote = *p++;
867 if (quote == '<') quote = '>';
868 include = p;
869 while (*p && (*p != quote)) p++;
870 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
871 *p = 0;
872 add_include( source, include, (quote == '>') );
876 /*******************************************************************
877 * parse_pragma_directive
879 static void parse_pragma_directive( struct incl_file *source, char *str )
881 char *flag, *p = str;
883 if (!isspace( *p )) return;
884 while (*p && isspace(*p)) p++;
885 p = strtok( p, " \t" );
886 if (strcmp( p, "makedep" )) return;
888 while ((flag = strtok( NULL, " \t" )))
890 if (!strcmp( flag, "depend" ))
892 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
893 return;
895 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
897 if (strendswith( source->name, ".idl" ))
899 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
900 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
901 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
902 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
903 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
904 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
905 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
906 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
908 else if (strendswith( source->name, ".rc" ))
910 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
912 else if (strendswith( source->name, ".sfd" ))
914 if (!strcmp( flag, "font" ))
916 struct strarray *array = source->args;
918 if (!array)
920 source->args = array = xmalloc( sizeof(*array) );
921 *array = empty_strarray;
922 source->flags |= FLAG_SFD_FONTS;
924 strarray_add( array, xstrdup( strtok( NULL, "" )));
925 return;
928 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
933 /*******************************************************************
934 * parse_cpp_directive
936 static void parse_cpp_directive( struct incl_file *source, char *str )
938 while (*str && isspace(*str)) str++;
939 if (*str++ != '#') return;
940 while (*str && isspace(*str)) str++;
942 if (!strncmp( str, "include", 7 ))
943 parse_include_directive( source, str + 7 );
944 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
945 parse_include_directive( source, str + 6 );
946 else if (!strncmp( str, "pragma", 6 ))
947 parse_pragma_directive( source, str + 6 );
951 /*******************************************************************
952 * parse_idl_file
954 * If for_h_file is non-zero, it means we are not interested in the idl file
955 * itself, but only in the contents of the .h file that will be generated from it.
957 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
959 char *buffer, *include;
961 input_line = 0;
962 if (for_h_file)
964 /* generated .h file always includes these */
965 add_include( pFile, "rpc.h", 1 );
966 add_include( pFile, "rpcndr.h", 1 );
969 while ((buffer = get_line( file )))
971 char quote;
972 char *p = buffer;
973 while (*p && isspace(*p)) p++;
975 if (!strncmp( p, "import", 6 ))
977 p += 6;
978 while (*p && isspace(*p)) p++;
979 if (*p != '"') continue;
980 include = ++p;
981 while (*p && (*p != '"')) p++;
982 if (!*p) fatal_error( "malformed import directive\n" );
983 *p = 0;
984 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
985 add_include( pFile, include, 0 );
986 continue;
989 if (for_h_file) /* only check for #include inside cpp_quote */
991 if (strncmp( p, "cpp_quote", 9 )) continue;
992 p += 9;
993 while (*p && isspace(*p)) p++;
994 if (*p++ != '(') continue;
995 while (*p && isspace(*p)) p++;
996 if (*p++ != '"') continue;
997 if (*p++ != '#') continue;
998 while (*p && isspace(*p)) p++;
999 if (strncmp( p, "include", 7 )) continue;
1000 p += 7;
1001 while (*p && isspace(*p)) p++;
1002 if (*p == '\\' && p[1] == '"')
1004 p += 2;
1005 quote = '"';
1007 else
1009 if (*p++ != '<' ) continue;
1010 quote = '>';
1012 include = p;
1013 while (*p && (*p != quote)) p++;
1014 if (!*p || (quote == '"' && p[-1] != '\\'))
1015 fatal_error( "malformed #include directive inside cpp_quote\n" );
1016 if (quote == '"') p--; /* remove backslash */
1017 *p = 0;
1018 add_include( pFile, include, (quote == '>') );
1019 continue;
1022 parse_cpp_directive( pFile, p );
1026 /*******************************************************************
1027 * parse_c_file
1029 static void parse_c_file( struct incl_file *pFile, FILE *file )
1031 char *buffer;
1033 input_line = 0;
1034 while ((buffer = get_line( file )))
1036 parse_cpp_directive( pFile, buffer );
1041 /*******************************************************************
1042 * parse_rc_file
1044 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1046 char *buffer, *include;
1048 input_line = 0;
1049 while ((buffer = get_line( file )))
1051 char quote;
1052 char *p = buffer;
1053 while (*p && isspace(*p)) p++;
1055 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1057 p += 2;
1058 while (*p && isspace(*p)) p++;
1059 if (strncmp( p, "@makedep:", 9 )) continue;
1060 p += 9;
1061 while (*p && isspace(*p)) p++;
1062 quote = '"';
1063 if (*p == quote)
1065 include = ++p;
1066 while (*p && *p != quote) p++;
1068 else
1070 include = p;
1071 while (*p && !isspace(*p) && *p != '*') p++;
1073 if (!*p)
1074 fatal_error( "malformed makedep comment\n" );
1075 *p = 0;
1076 add_include( pFile, include, (quote == '>') );
1077 continue;
1080 parse_cpp_directive( pFile, buffer );
1085 /*******************************************************************
1086 * parse_in_file
1088 static void parse_in_file( struct incl_file *source, FILE *file )
1090 char *p, *buffer;
1092 /* make sure it gets rebuilt when the version changes */
1093 add_include( source, "config.h", 1 );
1095 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1097 input_line = 0;
1098 while ((buffer = get_line( file )))
1100 if (strncmp( buffer, ".TH", 3 )) continue;
1101 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1102 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1103 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1104 source->args = xstrdup( p );
1105 return;
1110 /*******************************************************************
1111 * parse_sfd_file
1113 static void parse_sfd_file( struct incl_file *source, FILE *file )
1115 char *p, *eol, *buffer;
1117 input_line = 0;
1118 while ((buffer = get_line( file )))
1120 if (strncmp( buffer, "UComments:", 10 )) continue;
1121 p = buffer + 10;
1122 while (*p == ' ') p++;
1123 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1125 p++;
1126 buffer[strlen(buffer) - 1] = 0;
1128 while ((eol = strstr( p, "+AAoA" )))
1130 *eol = 0;
1131 while (*p && isspace(*p)) p++;
1132 if (*p++ == '#')
1134 while (*p && isspace(*p)) p++;
1135 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1137 p = eol + 5;
1139 while (*p && isspace(*p)) p++;
1140 if (*p++ != '#') return;
1141 while (*p && isspace(*p)) p++;
1142 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1143 return;
1148 /*******************************************************************
1149 * parse_file
1151 static void parse_file( struct incl_file *source, int src )
1153 FILE *file;
1155 /* don't try to open certain types of files */
1156 if (strendswith( source->name, ".tlb" ))
1158 source->filename = xstrdup( source->name );
1159 return;
1162 file = src ? open_src_file( source ) : open_include_file( source );
1163 if (!file) return;
1164 input_file_name = source->filename;
1166 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1167 parse_idl_file( source, file, 1 );
1168 else if (strendswith( source->filename, ".idl" ))
1169 parse_idl_file( source, file, 0 );
1170 else if (strendswith( source->filename, ".c" ) ||
1171 strendswith( source->filename, ".m" ) ||
1172 strendswith( source->filename, ".h" ) ||
1173 strendswith( source->filename, ".l" ) ||
1174 strendswith( source->filename, ".y" ))
1175 parse_c_file( source, file );
1176 else if (strendswith( source->filename, ".rc" ))
1177 parse_rc_file( source, file );
1178 else if (strendswith( source->filename, ".in" ))
1179 parse_in_file( source, file );
1180 else if (strendswith( source->filename, ".sfd" ))
1181 parse_sfd_file( source, file );
1182 fclose(file);
1183 input_file_name = NULL;
1187 /*******************************************************************
1188 * add_src_file
1190 * Add a source file to the list.
1192 static struct incl_file *add_src_file( const char *name )
1194 struct incl_file *file;
1196 if ((file = find_src_file( name ))) return file; /* we already have it */
1197 file = xmalloc( sizeof(*file) );
1198 memset( file, 0, sizeof(*file) );
1199 file->name = xstrdup(name);
1200 list_add_tail( &sources, &file->entry );
1201 parse_file( file, 1 );
1202 return file;
1206 /*******************************************************************
1207 * get_make_variable
1209 static char *get_make_variable( const char *name )
1211 unsigned int i;
1213 for (i = 0; i < cmdline_vars.count; i += 2)
1214 if (!strcmp( cmdline_vars.str[i], name ))
1215 return xstrdup( cmdline_vars.str[i + 1] );
1217 for (i = 0; i < make_vars.count; i += 2)
1218 if (!strcmp( make_vars.str[i], name ))
1219 return xstrdup( make_vars.str[i + 1] );
1220 return NULL;
1224 /*******************************************************************
1225 * get_expanded_make_variable
1227 static char *get_expanded_make_variable( const char *name )
1229 char *p, *end, *var, *expand, *tmp;
1231 expand = get_make_variable( name );
1232 if (!expand) return NULL;
1234 p = expand;
1235 while ((p = strchr( p, '$' )))
1237 if (p[1] == '(')
1239 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1240 *end++ = 0;
1241 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1242 var = get_make_variable( p + 2 );
1243 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1244 free( var );
1246 else if (p[1] == '{') /* don't expand ${} variables */
1248 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1249 p = end + 1;
1250 continue;
1252 else if (p[1] == '$')
1254 tmp = replace_substr( expand, p, 2, "$" );
1256 else fatal_error( "syntax error in '%s'\n", expand );
1258 /* switch to the new string */
1259 p = tmp + (p - expand);
1260 free( expand );
1261 expand = tmp;
1264 /* consider empty variables undefined */
1265 p = expand;
1266 while (*p && isspace(*p)) p++;
1267 if (*p) return expand;
1268 free( expand );
1269 return NULL;
1273 /*******************************************************************
1274 * get_expanded_make_var_array
1276 static struct strarray get_expanded_make_var_array( const char *name )
1278 struct strarray ret = empty_strarray;
1279 char *value, *token;
1281 if ((value = get_expanded_make_variable( name )))
1282 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1283 strarray_add( &ret, token );
1284 return ret;
1288 /*******************************************************************
1289 * set_make_variable
1291 static int set_make_variable( struct strarray *array, const char *assignment )
1293 unsigned int i;
1294 char *p, *name;
1296 p = name = xstrdup( assignment );
1297 while (isalnum(*p) || *p == '_') p++;
1298 if (name == p) return 0; /* not a variable */
1299 if (isspace(*p))
1301 *p++ = 0;
1302 while (isspace(*p)) p++;
1304 if (*p != '=') return 0; /* not an assignment */
1305 *p++ = 0;
1306 while (isspace(*p)) p++;
1308 /* redefining a variable replaces the previous value */
1309 for (i = 0; i < array->count; i += 2)
1311 if (strcmp( array->str[i], name )) continue;
1312 array->str[i + 1] = p;
1313 return 1;
1315 strarray_add( array, name );
1316 strarray_add( array, p );
1317 return 1;
1321 /*******************************************************************
1322 * parse_makefile
1324 static void parse_makefile(void)
1326 char *buffer;
1327 FILE *file;
1329 input_file_name = base_dir_path( makefile_name );
1330 if (!(file = fopen( input_file_name, "r" )))
1332 fatal_perror( "open" );
1333 exit( 1 );
1336 input_line = 0;
1337 while ((buffer = get_line( file )))
1339 if (Separator && !strncmp( buffer, Separator, strlen(Separator) )) break;
1340 if (*buffer == '\t') continue; /* command */
1341 while (isspace( *buffer )) buffer++;
1342 if (*buffer == '#') continue; /* comment */
1343 set_make_variable( &make_vars, buffer );
1345 fclose( file );
1346 input_file_name = NULL;
1350 /*******************************************************************
1351 * add_generated_sources
1353 static void add_generated_sources(void)
1355 struct incl_file *source, *next, *file;
1357 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1359 if (source->flags & FLAG_IDL_CLIENT)
1361 file = add_generated_source( replace_extension( source->name, ".idl", "_c.c" ), NULL );
1362 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1364 if (source->flags & FLAG_IDL_SERVER)
1366 file = add_generated_source( replace_extension( source->name, ".idl", "_s.c" ), NULL );
1367 add_include( file, "wine/exception.h", 0 );
1368 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1370 if (source->flags & FLAG_IDL_IDENT)
1372 file = add_generated_source( replace_extension( source->name, ".idl", "_i.c" ), NULL );
1373 add_include( file, "rpc.h", 0 );
1374 add_include( file, "rpcndr.h", 0 );
1375 add_include( file, "guiddef.h", 0 );
1377 if (source->flags & FLAG_IDL_PROXY)
1379 file = add_generated_source( "dlldata.o", "dlldata.c" );
1380 add_include( file, "objbase.h", 0 );
1381 add_include( file, "rpcproxy.h", 0 );
1382 file = add_generated_source( replace_extension( source->name, ".idl", "_p.c" ), NULL );
1383 add_include( file, "objbase.h", 0 );
1384 add_include( file, "rpcproxy.h", 0 );
1385 add_include( file, "wine/exception.h", 0 );
1386 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1388 if (source->flags & FLAG_IDL_REGTYPELIB)
1390 add_generated_source( replace_extension( source->name, ".idl", "_t.res" ), NULL );
1392 if (source->flags & FLAG_IDL_REGISTER)
1394 add_generated_source( replace_extension( source->name, ".idl", "_r.res" ), NULL );
1396 if (strendswith( source->name, ".y" ))
1398 file = add_generated_source( replace_extension( source->name, ".y", ".tab.c" ), NULL );
1399 /* steal the includes list from the source file */
1400 file->files_count = source->files_count;
1401 file->files_size = source->files_size;
1402 file->files = source->files;
1403 source->files_count = source->files_size = 0;
1404 source->files = NULL;
1406 if (strendswith( source->name, ".l" ))
1408 file = add_generated_source( replace_extension( source->name, ".l", ".yy.c" ), NULL );
1409 /* steal the includes list from the source file */
1410 file->files_count = source->files_count;
1411 file->files_size = source->files_size;
1412 file->files = source->files;
1413 source->files_count = source->files_size = 0;
1414 source->files = NULL;
1417 if (get_make_variable( "TESTDLL" ))
1419 file = add_generated_source( "testlist.o", "testlist.c" );
1420 add_include( file, "wine/test.h", 0 );
1425 /*******************************************************************
1426 * create_dir
1428 static void create_dir( const char *dir )
1430 char *p, *path;
1432 p = path = xstrdup( dir );
1433 while ((p = strchr( p, '/' )))
1435 *p = 0;
1436 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1437 *p++ = '/';
1438 while (*p == '/') p++;
1440 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1441 free( path );
1445 /*******************************************************************
1446 * output_include
1448 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1450 int i;
1452 if (pFile->owner == owner) return;
1453 if (!pFile->filename) return;
1454 pFile->owner = owner;
1455 output_filename( pFile->filename );
1456 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1460 /*******************************************************************
1461 * output_sources
1463 static struct strarray output_sources(void)
1465 struct incl_file *source;
1466 unsigned int i;
1467 int is_win16 = 0;
1468 const char *dllext = ".so";
1469 struct strarray object_files = empty_strarray;
1470 struct strarray crossobj_files = empty_strarray;
1471 struct strarray res_files = empty_strarray;
1472 struct strarray clean_files = empty_strarray;
1473 struct strarray po_files = empty_strarray;
1474 struct strarray mo_files = empty_strarray;
1475 struct strarray mc_files = empty_strarray;
1476 struct strarray ok_files = empty_strarray;
1477 struct strarray dlldata_files = empty_strarray;
1478 struct strarray c2man_files = empty_strarray;
1479 struct strarray implib_objs = empty_strarray;
1480 struct strarray includes = empty_strarray;
1481 struct strarray subdirs = empty_strarray;
1482 struct strarray phony_targets = empty_strarray;
1483 struct strarray linguas = get_expanded_make_var_array( "LINGUAS" );
1484 struct strarray all_targets = get_expanded_make_var_array( "PROGRAMS" );
1485 struct strarray targetflags = get_expanded_make_var_array( "TARGETFLAGS" );
1486 struct strarray delayimports = get_expanded_make_var_array( "DELAYIMPORTS" );
1487 struct strarray extradllflags = get_expanded_make_var_array( "EXTRADLLFLAGS" );
1488 char *module = get_expanded_make_variable( "MODULE" );
1489 char *exeext = get_expanded_make_variable( "EXEEXT" );
1490 char *testdll = get_expanded_make_variable( "TESTDLL" );
1491 char *staticlib = get_expanded_make_variable( "STATICLIB" );
1492 char *crosstarget = get_expanded_make_variable( "CROSSTARGET" );
1494 if (exeext && !strcmp( exeext, ".exe" )) dllext = "";
1495 if (module && strendswith( module, ".a" )) staticlib = module;
1496 for (i = 0; i < extradllflags.count; i++) if (!strcmp( extradllflags.str[i], "-m16" )) is_win16 = 1;
1498 for (i = 0; i < linguas.count; i++)
1499 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( "po" ), linguas.str[i] ));
1501 strarray_add( &includes, "-I." );
1502 if (src_dir) strarray_add( &includes, strmake( "-I%s", src_dir ));
1503 if (parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( parent_dir )));
1504 if (top_obj_dir) strarray_add( &includes, strmake( "-I%s/include", top_obj_dir ));
1505 if (top_src_dir) strarray_add( &includes, strmake( "-I%s/include", top_src_dir ));
1506 if (use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( "include/msvcrt" )));
1507 strarray_addall( &includes, include_args );
1509 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1511 struct strarray extradefs;
1512 char *obj = xstrdup( source->name );
1513 char *ext = get_extension( obj );
1515 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1516 *ext++ = 0;
1518 if (src_dir && strchr( obj, '/' ))
1520 char *subdir = base_dir_path( obj );
1521 *strrchr( subdir, '/' ) = 0;
1522 strarray_add_uniq( &subdirs, subdir );
1525 extradefs = get_expanded_make_var_array( strmake( "%s_EXTRADEFS", obj ));
1527 if (!strcmp( ext, "y" )) /* yacc file */
1529 /* add source file dependency for parallel makes */
1530 char *header = strmake( "%s.tab.h", obj );
1532 if (find_include_file( header ))
1534 output( "%s.tab.h: %s\n", obj, source->filename );
1535 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1536 obj, obj, source->filename );
1537 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1538 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1540 else output( "%s.tab.c: %s\n", obj, source->filename );
1542 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1543 free( header );
1544 continue; /* no dependencies */
1546 else if (!strcmp( ext, "x" )) /* template file */
1548 output( "%s.h: %s%s %s\n", obj, tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1549 output( "\t%s%s -H -o $@ %s\n", tools_dir_path( "make_xftmpl" ), tools_ext, source->filename );
1550 strarray_add( &clean_files, strmake( "%s.h", obj ));
1551 continue; /* no dependencies */
1553 else if (!strcmp( ext, "l" )) /* lex file */
1555 output( "%s.yy.c: %s\n", obj, source->filename );
1556 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1557 continue; /* no dependencies */
1559 else if (!strcmp( ext, "rc" )) /* resource file */
1561 strarray_add( &res_files, strmake( "%s.res", obj ));
1562 output( "%s.res: %s %s\n", obj, tools_path( "wrc" ), source->filename );
1563 output( "\t%s -o $@ %s", tools_path( "wrc" ), source->filename );
1564 if (is_win16) output_filename( "-m16" );
1565 else output_filenames( targetflags );
1566 output_filename( "--nostdinc" );
1567 output_filenames( includes );
1568 output_filenames( define_args );
1569 output_filenames( extradefs );
1570 if (mo_files.count && (source->flags & FLAG_RC_PO))
1572 strarray_add( &po_files, source->filename );
1573 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1574 output( "\n" );
1575 output( "%s.res:", obj );
1576 output_filenames( mo_files );
1577 output( "\n" );
1578 output( "rsrc.pot " );
1580 else output( "\n" );
1581 output( "%s.res:", obj );
1583 else if (!strcmp( ext, "mc" )) /* message file */
1585 strarray_add( &res_files, strmake( "%s.res", obj ));
1586 output( "%s.res: %s %s\n", obj, tools_path( "wmc" ), source->filename );
1587 output( "\t%s -U -O res -o $@ %s", tools_path( "wmc" ), source->filename );
1588 if (mo_files.count)
1590 strarray_add( &mc_files, source->filename );
1591 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1592 output( "\n" );
1593 output( "%s.res:", obj );
1594 output_filenames( mo_files );
1595 output( "\n" );
1596 output( "msg.pot " );
1598 else output( "\n" );
1599 output( "%s.res:", obj );
1601 else if (!strcmp( ext, "idl" )) /* IDL file */
1603 struct strarray targets = empty_strarray;
1604 char *dest;
1606 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1607 source->flags |= FLAG_IDL_HEADER;
1609 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1611 if (!(source->flags & idl_outputs[i].flag)) continue;
1612 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1613 if (!find_src_file( dest )) strarray_add( &clean_files, dest );
1614 strarray_add( &targets, dest );
1616 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1617 output_filenames( targets );
1618 output( ": %s\n", tools_path( "widl" ));
1619 output( "\t%s -o $@ %s", tools_path( "widl" ), source->filename );
1620 output_filenames( targetflags );
1621 output_filenames( includes );
1622 output_filenames( define_args );
1623 output_filenames( extradefs );
1624 output_filenames( get_expanded_make_var_array( "EXTRAIDLFLAGS" ));
1625 output( "\n" );
1626 output_filenames( targets );
1627 output( ": %s", source->filename );
1629 else if (!strcmp( ext, "in" )) /* .in file or man page */
1631 if (strendswith( obj, ".man" ) && source->args)
1633 char *dir, *dest = replace_extension( obj, ".man", "" );
1634 char *lang = strchr( dest, '.' );
1635 char *section = source->args;
1636 if (lang)
1638 *lang++ = 0;
1639 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1641 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1642 output( "install-man-pages:: %s\n", obj );
1643 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj, dir, dest, section );
1644 output( "uninstall::\n" );
1645 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1646 free( dest );
1647 free( dir );
1648 strarray_add( &all_targets, xstrdup(obj) );
1649 strarray_add_uniq( &phony_targets, "install-man-pages" );
1650 strarray_add_uniq( &phony_targets, "uninstall" );
1652 else strarray_add( &clean_files, xstrdup(obj) );
1653 output( "%s: %s\n", obj, source->filename );
1654 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1655 output( "%s:", obj );
1657 else if (!strcmp( ext, "sfd" )) /* font file */
1659 char *ttf_file = src_dir_path( strmake( "%s.ttf", obj ));
1660 char *fontforge = get_expanded_make_variable( "FONTFORGE" );
1661 if (fontforge && !src_dir)
1663 output( "%s: %s\n", ttf_file, source->filename );
1664 output( "\t%s -script %s %s $@\n",
1665 fontforge, top_dir_path( "fonts/genttf.ff" ), source->filename );
1667 if (source->flags & FLAG_INSTALL)
1669 output( "install install-lib::\n" );
1670 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1671 output( "uninstall::\n" );
1672 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1674 if (source->flags & FLAG_SFD_FONTS)
1676 struct strarray *array = source->args;
1678 for (i = 0; i < array->count; i++)
1680 char *font = strtok( xstrdup(array->str[i]), " \t" );
1681 char *args = strtok( NULL, "" );
1683 strarray_add( &all_targets, font );
1684 output( "%s: %s %s\n", font, tools_path( "sfnt2fon" ), ttf_file );
1685 output( "\t%s -o $@ %s %s\n", tools_path( "sfnt2fon" ), ttf_file, args );
1686 output( "install install-lib:: %s\n", font );
1687 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n", font, font );
1688 output( "uninstall::\n" );
1689 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1690 strarray_add_uniq( &phony_targets, "install" );
1691 strarray_add_uniq( &phony_targets, "install-lib" );
1692 strarray_add_uniq( &phony_targets, "uninstall" );
1695 else output( "all: %s\n", ttf_file );
1697 continue; /* no dependencies */
1699 else if (!strcmp( ext, "svg" )) /* svg file */
1701 char *convert = get_expanded_make_variable( "CONVERT" );
1702 char *rsvg = get_expanded_make_variable( "RSVG" );
1703 char *icotool = get_expanded_make_variable( "ICOTOOL" );
1704 if (convert && rsvg && icotool && !src_dir)
1706 output( "%s.ico %s.bmp: %s\n", obj, obj, source->filename );
1707 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
1708 convert, icotool, rsvg, top_dir_path( "tools/buildimage" ), source->filename );
1710 free( convert );
1711 free( rsvg );
1712 free( icotool );
1713 continue; /* no dependencies */
1715 else if (!strcmp( ext, "res" ))
1717 strarray_add( &res_files, source->name );
1718 continue; /* no dependencies */
1720 else
1722 int need_cross = testdll || (source->flags & FLAG_C_IMPLIB) || (module && staticlib);
1724 if ((source->flags & FLAG_GENERATED) && (!testdll || strcmp( source->filename, "testlist.c" )))
1725 strarray_add( &clean_files, source->filename );
1726 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1727 strarray_add( &object_files, strmake( "%s.o", obj ));
1728 output( "%s.o: %s\n", obj, source->filename );
1729 output( "\t$(CC) -c -o $@ %s", source->filename );
1730 output_filenames( includes );
1731 output_filenames( define_args );
1732 output_filenames( extradefs );
1733 if (module || staticlib || testdll) output_filenames( dllflags );
1734 output_filenames( get_expanded_make_var_array( "EXTRACFLAGS" ));
1735 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1736 output_filename( "$(CFLAGS)" );
1737 output( "\n" );
1738 if (crosstarget && need_cross)
1740 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1741 output( "%s.cross.o: %s\n", obj, source->filename );
1742 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1743 output_filenames( includes );
1744 output_filenames( define_args );
1745 output_filenames( extradefs );
1746 output_filename( "-DWINE_CROSSTEST" );
1747 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1748 output_filename( "$(CFLAGS)" );
1749 output( "\n" );
1751 if (testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1753 strarray_add( &ok_files, strmake( "%s.ok", obj ));
1754 output( "%s.ok:\n", obj );
1755 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1756 top_dir_path( "tools/runtest" ), top_obj_dir,
1757 testdll, replace_extension( testdll, ".dll", "_test.exe" ), dllext, obj );
1759 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1760 strarray_add( &c2man_files, source->filename );
1761 output( "%s.o", obj );
1762 if (crosstarget && need_cross) output( " %s.cross.o", obj );
1763 output( ":" );
1765 free( obj );
1767 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
1768 output( "\n" );
1771 /* rules for files that depend on multiple sources */
1773 if (po_files.count)
1775 output( "rsrc.pot: %s", tools_path( "wrc" ) );
1776 output_filenames( po_files );
1777 output( "\n" );
1778 output( "\t%s -O pot -o $@", tools_path( "wrc" ));
1779 output_filenames( po_files );
1780 if (is_win16) output_filename( "-m16" );
1781 else output_filenames( targetflags );
1782 output_filename( "--nostdinc" );
1783 output_filenames( includes );
1784 output_filenames( define_args );
1785 output( "\n" );
1786 strarray_add( &clean_files, "rsrc.pot" );
1789 if (mc_files.count)
1791 output( "msg.pot: %s", tools_path( "wmc" ));
1792 output_filenames( mc_files );
1793 output( "\n" );
1794 output( "\t%s -O pot -o $@", tools_path( "wmc" ));
1795 output_filenames( mc_files );
1796 output( "\n" );
1797 strarray_add( &clean_files, "msg.pot" );
1800 if (dlldata_files.count)
1802 output( "dlldata.c: %s %s\n", tools_path( "widl" ), src_dir_path( "Makefile.in" ));
1803 output( "\t%s --dlldata-only -o $@", tools_path( "widl" ));
1804 output_filenames( dlldata_files );
1805 output( "\n" );
1808 if (module && !staticlib)
1810 char *importlib = get_expanded_make_variable( "IMPORTLIB" );
1811 struct strarray all_libs = empty_strarray;
1812 char *spec_file = NULL;
1814 if (!appmode.count) spec_file = src_dir_path( replace_extension( module, ".dll", ".spec" ));
1815 for (i = 0; i < delayimports.count; i++)
1816 strarray_add( &all_libs, strmake( "-l%s", delayimports.str[i] ));
1817 for (i = 0; i < imports.count; i++)
1818 strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1819 for (i = 0; i < delayimports.count; i++)
1820 strarray_add( &all_libs, strmake( "-Wb,-d%s", delayimports.str[i] ));
1821 strarray_add( &all_libs, "-lwine" );
1822 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBPORT" ));
1823 strarray_addall( &all_libs, get_expanded_make_var_array( "EXTRALIBS" ));
1824 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1826 if (*dllext)
1828 strarray_add( &all_targets, strmake( "%s%s", module, dllext ));
1829 strarray_add( &all_targets, strmake( "%s.fake", module ));
1830 output( "%s%s %s.fake:", module, dllext, module );
1832 else
1834 strarray_add( &all_targets, module );
1835 output( "%s:", module );
1837 if (spec_file) output_filename( spec_file );
1838 output_filenames( object_files );
1839 output_filenames( res_files );
1840 output( "\n" );
1841 output( "\t%s -o $@", tools_path( "winegcc" ));
1842 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1843 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1844 output_filenames( targetflags );
1845 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1846 if (spec_file)
1848 output( " -shared %s", spec_file );
1849 output_filenames( extradllflags );
1851 else output_filenames( appmode );
1852 output_filenames( object_files );
1853 output_filenames( res_files );
1854 output_filenames( all_libs );
1855 output_filename( "$(LDFLAGS)" );
1856 output( "\n" );
1858 if (spec_file && importlib)
1860 if (*dllext)
1862 strarray_add( &clean_files, strmake( "lib%s.def", importlib ));
1863 output( "lib%s.def: %s %s\n", importlib, tools_path( "winebuild" ), spec_file );
1864 output( "\t%s -w --def -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1865 output_filenames( targetflags );
1866 if (is_win16) output_filename( "-m16" );
1867 output( "\n" );
1868 if (implib_objs.count)
1870 strarray_add( &clean_files, strmake( "lib%s.def.a", importlib ));
1871 output( "lib%s.def.a:", importlib );
1872 output_filenames( implib_objs );
1873 output( "\n" );
1874 output( "\t$(RM) $@\n" );
1875 output( "\t$(AR) $(ARFLAGS) $@" );
1876 output_filenames( implib_objs );
1877 output( "\n" );
1878 output( "\t$(RANLIB) $@\n" );
1881 else
1883 strarray_add( &clean_files, strmake( "lib%s.a", importlib ));
1884 output( "lib%s.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1885 output_filenames( implib_objs );
1886 output( "\n" );
1887 output( "\t%s -w --implib -o $@ --export %s", tools_path( "winebuild" ), spec_file );
1888 output_filenames( targetflags );
1889 output_filenames( implib_objs );
1890 output( "\n" );
1892 if (crosstarget && !is_win16)
1894 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
1895 strarray_add( &clean_files, strmake( "lib%s.cross.a", importlib ));
1896 output( "lib%s.cross.a: %s %s", importlib, tools_path( "winebuild" ), spec_file );
1897 output_filenames( cross_files );
1898 output( "\n" );
1899 output( "\t%s -b %s -w --implib -o $@ --export %s",
1900 tools_path( "winebuild" ), crosstarget, spec_file );
1901 output_filenames( cross_files );
1902 output( "\n" );
1906 if (spec_file)
1908 if (c2man_files.count)
1910 char *manext = get_expanded_make_variable( "api_manext" );
1912 output( "manpages::\n" );
1913 output( "\t%s -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1914 output_filename( strmake( "-R%s", top_dir_path( "" )));
1915 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1916 output_filename( strmake( "-o %s/man%s", top_obj_dir_path( "documentation" ), manext ? manext : "3w" ));
1917 output_filenames( c2man_files );
1918 output( "\n" );
1919 output( "htmlpages::\n" );
1920 output( "\t%s -Th -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1921 output_filename( strmake( "-R%s", top_dir_path( "" )));
1922 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1923 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/html" )));
1924 output_filenames( c2man_files );
1925 output( "\n" );
1926 output( "sgmlpages::\n" );
1927 output( "\t%s -Ts -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1928 output_filename( strmake( "-R%s", top_dir_path( "" )));
1929 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1930 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/api-guide" )));
1931 output_filenames( c2man_files );
1932 output( "\n" );
1933 output( "xmlpages::\n" );
1934 output( "\t%s -Tx -w %s", top_dir_path( "tools/c2man.pl" ), spec_file );
1935 output_filename( strmake( "-R%s", top_dir_path( "" )));
1936 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1937 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/api-guide-xml" )));
1938 output_filenames( c2man_files );
1939 output( "\n" );
1940 strarray_add( &phony_targets, "manpages" );
1941 strarray_add( &phony_targets, "htmlpages" );
1942 strarray_add( &phony_targets, "sgmlpages" );
1943 strarray_add( &phony_targets, "xmlpages" );
1945 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1949 if (staticlib)
1951 strarray_add( &all_targets, staticlib );
1952 output( "%s:", staticlib );
1953 output_filenames( object_files );
1954 output( "\n\t$(RM) $@\n" );
1955 output( "\t$(AR) $(ARFLAGS) $@" );
1956 output_filenames( object_files );
1957 output( "\n\t$(RANLIB) $@\n" );
1958 if (crosstarget && module)
1960 char *name = replace_extension( staticlib, ".a", ".cross.a" );
1962 strarray_add( &all_targets, name );
1963 output( "%s:", name );
1964 output_filenames( crossobj_files );
1965 output( "\n\t$(RM) $@\n" );
1966 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
1967 output_filenames( crossobj_files );
1968 output( "\n\t%s-ranlib $@\n", crosstarget );
1972 if (testdll)
1974 char *testmodule = replace_extension( testdll, ".dll", "_test.exe" );
1975 char *stripped = replace_extension( testdll, ".dll", "_test-stripped.exe" );
1976 struct strarray all_libs = empty_strarray;
1978 for (i = 0; i < imports.count; i++) strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1979 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1981 strarray_add( &all_targets, strmake( "%s%s", testmodule, dllext ));
1982 strarray_add( &clean_files, strmake( "%s%s", stripped, dllext ));
1983 output( "%s%s:\n", testmodule, dllext );
1984 output( "\t%s -o $@", tools_path( "winegcc" ));
1985 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1986 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1987 output_filenames( targetflags );
1988 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1989 output_filenames( appmode );
1990 output_filenames( object_files );
1991 output_filenames( res_files );
1992 output_filenames( all_libs );
1993 output_filename( "$(LDFLAGS)" );
1994 output( "\n" );
1995 output( "%s%s:\n", stripped, dllext );
1996 output( "\t%s -o $@", tools_path( "winegcc" ));
1997 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1998 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
1999 output_filenames( targetflags );
2000 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
2001 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2002 output_filenames( appmode );
2003 output_filenames( object_files );
2004 output_filenames( res_files );
2005 output_filenames( all_libs );
2006 output_filename( "$(LDFLAGS)" );
2007 output( "\n" );
2008 output( "%s%s %s%s:", testmodule, dllext, stripped, dllext );
2009 output_filenames( object_files );
2010 output_filenames( res_files );
2011 output( "\n" );
2013 if (top_obj_dir)
2015 char *testres = replace_extension( testdll, ".dll", "_test.res" );
2016 output( "all: %s/%s\n", top_obj_dir_path( "programs/winetest" ), testres );
2017 output( "%s/%s: %s%s\n", top_obj_dir_path( "programs/winetest" ), testres, stripped, dllext );
2018 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2019 testmodule, stripped, dllext, tools_path( "wrc" ));
2022 if (crosstarget)
2024 char *crosstest = replace_extension( testdll, ".dll", "_crosstest.exe" );
2026 strarray_add( &clean_files, crosstest );
2027 output( "crosstest: %s\n", crosstest );
2028 output( "%s:", crosstest );
2029 output_filenames( crossobj_files );
2030 output_filenames( res_files );
2031 output( "\n" );
2032 output( "\t%s -o $@ -b %s", tools_path( "winegcc" ), crosstarget );
2033 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
2034 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir ));
2035 output_filename( "--lib-suffix=.cross.a" );
2036 output_filenames( crossobj_files );
2037 output_filenames( res_files );
2038 output_filenames( all_libs );
2039 output_filename( "$(LDFLAGS)" );
2040 output( "\n" );
2041 strarray_add( &phony_targets, "crosstest" );
2044 output_filenames( ok_files );
2045 output( ": %s%s ../%s%s\n", testmodule, dllext, testdll, dllext );
2046 output( "check test:" );
2047 output_filenames( ok_files );
2048 output( "\n" );
2049 output( "testclean::\n" );
2050 output( "\t$(RM)" );
2051 output_filenames( ok_files );
2052 output( "\n" );
2053 strarray_addall( &clean_files, ok_files );
2054 strarray_add( &phony_targets, "check" );
2055 strarray_add( &phony_targets, "test" );
2056 strarray_add( &phony_targets, "testclean" );
2057 testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2060 if (all_targets.count)
2062 output( "all:" );
2063 output_filenames( all_targets );
2064 output( "\n" );
2067 strarray_addall( &clean_files, object_files );
2068 strarray_addall( &clean_files, crossobj_files );
2069 strarray_addall( &clean_files, res_files );
2070 strarray_addall( &clean_files, all_targets );
2071 strarray_addall( &clean_files, get_expanded_make_var_array( "EXTRA_TARGETS" ));
2073 if (clean_files.count)
2075 output( "clean::\n" );
2076 output( "\t$(RM)" );
2077 output_filenames( clean_files );
2078 output( "\n" );
2079 strarray_add( &phony_targets, "clean" );
2082 if (top_obj_dir)
2084 output( "depend:\n" );
2085 output( "\t@cd %s && $(MAKE) %s\n", top_obj_dir, base_dir_path( "depend" ));
2086 strarray_add( &phony_targets, "depend" );
2089 if (phony_targets.count)
2091 output( ".PHONY:" );
2092 output_filenames( phony_targets );
2093 output( "\n" );
2096 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2098 return clean_files;
2102 /*******************************************************************
2103 * create_temp_file
2105 static FILE *create_temp_file( const char *orig )
2107 char *name = xmalloc( strlen(orig) + 13 );
2108 unsigned int i, id = getpid();
2109 int fd;
2110 FILE *ret = NULL;
2112 for (i = 0; i < 100; i++)
2114 sprintf( name, "%s.tmp%08x", orig, id );
2115 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2117 ret = fdopen( fd, "w" );
2118 break;
2120 if (errno != EEXIST) break;
2121 id += 7777;
2123 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2124 temp_file_name = name;
2125 return ret;
2129 /*******************************************************************
2130 * rename_temp_file
2132 static void rename_temp_file( const char *dest )
2134 int ret = rename( temp_file_name, dest );
2135 if (ret == -1 && errno == EEXIST)
2137 /* rename doesn't overwrite on windows */
2138 unlink( dest );
2139 ret = rename( temp_file_name, dest );
2141 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2142 temp_file_name = NULL;
2146 /*******************************************************************
2147 * are_files_identical
2149 static int are_files_identical( FILE *file1, FILE *file2 )
2151 for (;;)
2153 char buffer1[8192], buffer2[8192];
2154 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2155 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2156 if (size1 != size2) return 0;
2157 if (!size1) return feof( file1 ) && feof( file2 );
2158 if (memcmp( buffer1, buffer2, size1 )) return 0;
2163 /*******************************************************************
2164 * rename_temp_file_if_changed
2166 static void rename_temp_file_if_changed( const char *dest )
2168 FILE *file1, *file2;
2169 int do_rename = 1;
2171 if ((file1 = fopen( dest, "r" )))
2173 if ((file2 = fopen( temp_file_name, "r" )))
2175 do_rename = !are_files_identical( file1, file2 );
2176 fclose( file2 );
2178 fclose( file1 );
2180 if (!do_rename)
2182 unlink( temp_file_name );
2183 temp_file_name = NULL;
2185 else rename_temp_file( dest );
2189 /*******************************************************************
2190 * output_testlist
2192 static void output_testlist( const char *dest, struct strarray files )
2194 int i;
2196 output_file = create_temp_file( dest );
2198 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2199 output( "#define WIN32_LEAN_AND_MEAN\n" );
2200 output( "#include <windows.h>\n\n" );
2201 output( "#define STANDALONE\n" );
2202 output( "#include \"wine/test.h\"\n\n" );
2204 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2205 output( "\n" );
2206 output( "const struct test winetest_testlist[] =\n" );
2207 output( "{\n" );
2208 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2209 output( " { 0, 0 }\n" );
2210 output( "};\n" );
2212 if (fclose( output_file )) fatal_perror( "write" );
2213 output_file = NULL;
2214 rename_temp_file_if_changed( dest );
2218 /*******************************************************************
2219 * output_gitignore
2221 static void output_gitignore( 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" );
2228 for (i = 0; i < files.count; i++)
2230 if (!strchr( files.str[i], '/' )) output( "/" );
2231 output( "%s\n", files.str[i] );
2234 if (fclose( output_file )) fatal_perror( "write" );
2235 output_file = NULL;
2236 rename_temp_file( dest );
2240 /*******************************************************************
2241 * output_dependencies
2243 static void output_dependencies( const char *path )
2245 struct strarray targets, ignore_files = empty_strarray;
2247 if (Separator && ((output_file = fopen( path, "r" ))))
2249 char buffer[1024];
2250 FILE *tmp_file = create_temp_file( path );
2251 int found = 0;
2253 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2255 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2256 found = !strncmp( buffer, Separator, strlen(Separator) );
2258 if (fclose( output_file )) fatal_perror( "write" );
2259 output_file = tmp_file;
2260 if (!found) output( "\n%s\n", Separator );
2262 else
2264 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2265 fatal_perror( "%s", path );
2268 testlist_files = empty_strarray;
2269 targets = output_sources();
2271 fclose( output_file );
2272 output_file = NULL;
2273 if (temp_file_name) rename_temp_file( path );
2275 strarray_add( &ignore_files, ".gitignore" );
2276 strarray_add( &ignore_files, "Makefile" );
2277 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2278 strarray_addall( &ignore_files, targets );
2280 if (testlist_files.count) output_testlist( base_dir_path( "testlist.c" ), testlist_files );
2281 if (!src_dir && base_dir) output_gitignore( base_dir_path( ".gitignore" ), ignore_files );
2285 /*******************************************************************
2286 * update_makefile
2288 static void update_makefile( const char *path )
2290 static const char *source_vars[] =
2292 "C_SRCS",
2293 "OBJC_SRCS",
2294 "RC_SRCS",
2295 "MC_SRCS",
2296 "IDL_SRCS",
2297 "BISON_SRCS",
2298 "LEX_SRCS",
2299 "XTEMPLATE_SRCS",
2300 "SVG_SRCS",
2301 "FONT_SRCS",
2302 "IN_SRCS",
2303 "MANPAGES",
2304 NULL
2306 const char **var;
2307 unsigned int i;
2308 struct strarray value;
2309 struct incl_file *file;
2311 base_dir = path;
2312 if (!strcmp( base_dir, "." )) base_dir = NULL;
2313 output_file_name = base_dir_path( makefile_name );
2314 parse_makefile();
2316 src_dir = get_expanded_make_variable( "srcdir" );
2317 top_src_dir = get_expanded_make_variable( "top_srcdir" );
2318 top_obj_dir = get_expanded_make_variable( "top_builddir" );
2319 parent_dir = get_expanded_make_variable( "PARENTSRC" );
2320 tools_dir = get_expanded_make_variable( "TOOLSDIR" );
2321 tools_ext = get_expanded_make_variable( "TOOLSEXT" );
2323 /* ignore redundant source paths */
2324 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
2325 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
2326 if (tools_dir && top_obj_dir && !strcmp( tools_dir, top_obj_dir )) tools_dir = NULL;
2327 if (top_obj_dir && !strcmp( top_obj_dir, "." )) top_obj_dir = NULL;
2329 appmode = get_expanded_make_var_array( "APPMODE" );
2330 dllflags = get_expanded_make_var_array( "DLLFLAGS" );
2331 imports = get_expanded_make_var_array( "IMPORTS" );
2333 use_msvcrt = 0;
2334 for (i = 0; i < appmode.count && !use_msvcrt; i++)
2335 use_msvcrt = !strcmp( appmode.str[i], "-mno-cygwin" );
2336 for (i = 0; i < imports.count && !use_msvcrt; i++)
2337 use_msvcrt = !strncmp( imports.str[i], "msvcr", 5 );
2339 include_args = empty_strarray;
2340 define_args = empty_strarray;
2341 strarray_add( &define_args, "-D__WINESRC__" );
2343 if (!tools_ext) tools_ext = "";
2345 value = get_expanded_make_var_array( "EXTRAINCL" );
2346 for (i = 0; i < value.count; i++)
2347 if (!strncmp( value.str[i], "-I", 2 ))
2348 strarray_add_uniq( &include_args, value.str[i] );
2349 else
2350 strarray_add_uniq( &define_args, value.str[i] );
2351 strarray_addall( &define_args, get_expanded_make_var_array( "EXTRADEFS" ));
2353 if (use_msvcrt) strarray_add( &dllflags, get_expanded_make_variable( "MSVCRTFLAGS" ));
2355 list_init( &sources );
2356 list_init( &includes );
2358 for (var = source_vars; *var; var++)
2360 value = get_expanded_make_var_array( *var );
2361 for (i = 0; i < value.count; i++) add_src_file( value.str[i] );
2364 add_generated_sources();
2366 value = get_expanded_make_var_array( "EXTRA_OBJS" );
2367 for (i = 0; i < value.count; i++)
2369 /* default to .c for unknown extra object files */
2370 if (strendswith( value.str[i], ".o" ))
2371 add_generated_source( value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2372 else
2373 add_generated_source( value.str[i], NULL );
2376 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( file, 0 );
2377 output_dependencies( output_file_name );
2378 output_file_name = NULL;
2382 /*******************************************************************
2383 * parse_makeflags
2385 static void parse_makeflags( const char *flags )
2387 const char *p = flags;
2388 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2390 while (*p)
2392 while (isspace(*p)) p++;
2393 var = buffer;
2394 while (*p && !isspace(*p))
2396 if (*p == '\\' && p[1]) p++;
2397 *var++ = *p++;
2399 *var = 0;
2400 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2405 /*******************************************************************
2406 * parse_option
2408 static int parse_option( const char *opt )
2410 if (opt[0] != '-')
2412 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2413 return 0;
2415 switch(opt[1])
2417 case 'f':
2418 if (opt[2]) makefile_name = opt + 2;
2419 break;
2420 case 'R':
2421 relative_dir_mode = 1;
2422 break;
2423 case 's':
2424 if (opt[2]) Separator = opt + 2;
2425 else Separator = NULL;
2426 break;
2427 default:
2428 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2429 exit(1);
2431 return 1;
2435 /*******************************************************************
2436 * main
2438 int main( int argc, char *argv[] )
2440 const char *makeflags = getenv( "MAKEFLAGS" );
2441 int i, j;
2443 if (makeflags) parse_makeflags( makeflags );
2445 i = 1;
2446 while (i < argc)
2448 if (parse_option( argv[i] ))
2450 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2451 argc--;
2453 else i++;
2456 if (relative_dir_mode)
2458 char *relpath;
2460 if (argc != 3)
2462 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2463 exit( 1 );
2465 relpath = get_relative_path( argv[1], argv[2] );
2466 printf( "%s\n", relpath ? relpath : "." );
2467 exit( 0 );
2470 if (argc <= 1)
2472 fprintf( stderr, "%s", Usage );
2473 exit( 1 );
2475 atexit( cleanup_files );
2476 signal( SIGTERM, exit_on_signal );
2477 signal( SIGINT, exit_on_signal );
2478 #ifdef SIGHUP
2479 signal( SIGHUP, exit_on_signal );
2480 #endif
2482 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2483 return 0;