makedep: Pass a makefile pointer to all internal functions.
[wine/multimedia.git] / tools / makedep.c
bloba83320f5fa441273e89b1faeb95e616da16ddd67
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <signal.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "wine/list.h"
38 struct incl_file
40 struct list entry;
41 char *name;
42 char *filename;
43 char *sourcename; /* source file name for generated headers */
44 void *args; /* custom arguments for makefile rule */
45 struct incl_file *included_by; /* file that included this one */
46 int included_line; /* line where this file was included */
47 unsigned int flags; /* flags (see below) */
48 struct incl_file *owner;
49 unsigned int files_count; /* files in use */
50 unsigned int files_size; /* total allocated size */
51 struct incl_file **files;
54 #define FLAG_SYSTEM 0x000001 /* is it a system include (#include <name>) */
55 #define FLAG_GENERATED 0x000002 /* generated file */
56 #define FLAG_INSTALL 0x000004 /* file to install */
57 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
58 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
59 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
60 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
61 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
62 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
63 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
64 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
65 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
66 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
67 #define FLAG_SFD_FONTS 0x040000 /* sfd file generated bitmap fonts */
69 static const struct
71 unsigned int flag;
72 const char *ext;
73 } idl_outputs[] =
75 { FLAG_IDL_TYPELIB, ".tlb" },
76 { FLAG_IDL_REGTYPELIB, "_t.res" },
77 { FLAG_IDL_CLIENT, "_c.c" },
78 { FLAG_IDL_IDENT, "_i.c" },
79 { FLAG_IDL_PROXY, "_p.c" },
80 { FLAG_IDL_SERVER, "_s.c" },
81 { FLAG_IDL_REGISTER, "_r.res" },
82 { FLAG_IDL_HEADER, ".h" }
85 static struct list sources = LIST_INIT(sources);
86 static struct list includes = LIST_INIT(includes);
88 struct strarray
90 unsigned int count; /* strings in use */
91 unsigned int size; /* total allocated size */
92 const char **str;
95 static const struct strarray empty_strarray;
97 /* variables common to all makefiles */
98 static struct strarray linguas;
99 static struct strarray dll_flags;
100 static struct strarray target_flags;
101 static struct strarray msvcrt_flags;
102 static struct strarray extra_cflags;
103 static struct strarray cpp_flags;
104 static struct strarray unwind_flags;
105 static struct strarray libs;
106 static struct strarray cmdline_vars;
107 static const char *root_src_dir;
108 static const char *tools_dir;
109 static const char *tools_ext;
110 static const char *exe_ext;
111 static const char *dll_ext;
112 static const char *man_ext;
113 static const char *dll_prefix;
114 static const char *crosstarget;
115 static const char *fontforge;
116 static const char *convert;
117 static const char *rsvg;
118 static const char *icotool;
120 struct makefile
122 struct strarray vars;
123 struct strarray include_args;
124 struct strarray define_args;
125 struct strarray appmode;
126 struct strarray imports;
127 struct strarray delayimports;
128 struct strarray extradllflags;
129 const char *base_dir;
130 const char *src_dir;
131 const char *obj_dir;
132 const char *top_src_dir;
133 const char *top_obj_dir;
134 const char *parent_dir;
135 const char *module;
136 const char *testdll;
137 const char *staticlib;
138 const char *importlib;
139 int use_msvcrt;
140 int is_win16;
143 static struct makefile *top_makefile;
145 static const char *makefile_name = "Makefile";
146 static const char *Separator = "### Dependencies";
147 static const char *input_file_name;
148 static const char *output_file_name;
149 static const char *temp_file_name;
150 static int relative_dir_mode;
151 static int input_line;
152 static int output_column;
153 static FILE *output_file;
155 static const char Usage[] =
156 "Usage: makedep [options] directories\n"
157 "Options:\n"
158 " -R from to Compute the relative path between two directories\n"
159 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
160 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
163 #ifndef __GNUC__
164 #define __attribute__(x)
165 #endif
167 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
168 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
169 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
170 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
172 /*******************************************************************
173 * fatal_error
175 static void fatal_error( const char *msg, ... )
177 va_list valist;
178 va_start( valist, msg );
179 if (input_file_name)
181 fprintf( stderr, "%s:", input_file_name );
182 if (input_line) fprintf( stderr, "%d:", input_line );
183 fprintf( stderr, " error: " );
185 else fprintf( stderr, "makedep: error: " );
186 vfprintf( stderr, msg, valist );
187 va_end( valist );
188 exit(1);
192 /*******************************************************************
193 * fatal_perror
195 static void fatal_perror( const char *msg, ... )
197 va_list valist;
198 va_start( valist, msg );
199 if (input_file_name)
201 fprintf( stderr, "%s:", input_file_name );
202 if (input_line) fprintf( stderr, "%d:", input_line );
203 fprintf( stderr, " error: " );
205 else fprintf( stderr, "makedep: error: " );
206 vfprintf( stderr, msg, valist );
207 perror( " " );
208 va_end( valist );
209 exit(1);
213 /*******************************************************************
214 * cleanup_files
216 static void cleanup_files(void)
218 if (temp_file_name) unlink( temp_file_name );
219 if (output_file_name) unlink( output_file_name );
223 /*******************************************************************
224 * exit_on_signal
226 static void exit_on_signal( int sig )
228 exit( 1 ); /* this will call the atexit functions */
232 /*******************************************************************
233 * xmalloc
235 static void *xmalloc( size_t size )
237 void *res;
238 if (!(res = malloc (size ? size : 1)))
239 fatal_error( "Virtual memory exhausted.\n" );
240 return res;
244 /*******************************************************************
245 * xrealloc
247 static void *xrealloc (void *ptr, size_t size)
249 void *res;
250 assert( size );
251 if (!(res = realloc( ptr, size )))
252 fatal_error( "Virtual memory exhausted.\n" );
253 return res;
256 /*******************************************************************
257 * xstrdup
259 static char *xstrdup( const char *str )
261 char *res = strdup( str );
262 if (!res) fatal_error( "Virtual memory exhausted.\n" );
263 return res;
267 /*******************************************************************
268 * strmake
270 static char *strmake( const char* fmt, ... )
272 int n;
273 size_t size = 100;
274 va_list ap;
276 for (;;)
278 char *p = xmalloc (size);
279 va_start(ap, fmt);
280 n = vsnprintf (p, size, fmt, ap);
281 va_end(ap);
282 if (n == -1) size *= 2;
283 else if ((size_t)n >= size) size = n + 1;
284 else return p;
285 free(p);
290 /*******************************************************************
291 * strendswith
293 static int strendswith( const char* str, const char* end )
295 int l = strlen(str);
296 int m = strlen(end);
298 return l >= m && strcmp(str + l - m, end) == 0;
302 /*******************************************************************
303 * output
305 static void output( const char *format, ... )
307 int ret;
308 va_list valist;
310 va_start( valist, format );
311 ret = vfprintf( output_file, format, valist );
312 va_end( valist );
313 if (ret < 0) fatal_perror( "output" );
314 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
315 else output_column += ret;
319 /*******************************************************************
320 * strarray_add
322 static void strarray_add( struct strarray *array, const char *str )
324 if (array->count == array->size)
326 if (array->size) array->size *= 2;
327 else array->size = 16;
328 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
330 array->str[array->count++] = str;
334 /*******************************************************************
335 * strarray_addall
337 static void strarray_addall( struct strarray *array, struct strarray added )
339 unsigned int i;
341 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
345 /*******************************************************************
346 * strarray_add_uniq
348 static void strarray_add_uniq( struct strarray *array, const char *str )
350 unsigned int i;
352 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return;
353 strarray_add( array, str );
357 /*******************************************************************
358 * strarray_get_value
360 * Find a value in a name/value pair string array.
362 static char *strarray_get_value( const struct strarray *array, const char *name )
364 unsigned int i;
366 for (i = 0; i < array->count; i += 2)
367 if (!strcmp( array->str[i], name )) return xstrdup( array->str[i + 1] );
368 return NULL;
372 /*******************************************************************
373 * strarray_set_value
375 * Define a value in a name/value pair string array.
377 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
379 unsigned int i;
381 /* redefining a variable replaces the previous value */
382 for (i = 0; i < array->count; i += 2)
384 if (strcmp( array->str[i], name )) continue;
385 array->str[i + 1] = value;
386 return;
388 strarray_add( array, name );
389 strarray_add( array, value );
393 /*******************************************************************
394 * output_filename
396 static void output_filename( const char *name )
398 if (output_column + strlen(name) + 1 > 100)
400 output( " \\\n" );
401 output( " " );
403 else if (output_column) output( " " );
404 output( "%s", name );
408 /*******************************************************************
409 * output_filenames
411 static void output_filenames( struct strarray array )
413 unsigned int i;
415 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
419 /*******************************************************************
420 * get_extension
422 static char *get_extension( char *filename )
424 char *ext = strrchr( filename, '.' );
425 if (ext && strchr( ext, '/' )) ext = NULL;
426 return ext;
430 /*******************************************************************
431 * replace_extension
433 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
435 char *ret;
436 int name_len = strlen( name );
437 int ext_len = strlen( old_ext );
439 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
440 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
441 memcpy( ret, name, name_len );
442 strcpy( ret + name_len, new_ext );
443 return ret;
447 /*******************************************************************
448 * strarray_replace_extension
450 static struct strarray strarray_replace_extension( const struct strarray *array,
451 const char *old_ext, const char *new_ext )
453 unsigned int i;
454 struct strarray ret;
456 ret.count = ret.size = array->count;
457 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
458 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
459 return ret;
463 /*******************************************************************
464 * replace_substr
466 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
468 unsigned int pos = start - str;
469 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
470 memcpy( ret, str, pos );
471 strcpy( ret + pos, replace );
472 strcat( ret + pos, start + len );
473 return ret;
477 /*******************************************************************
478 * get_relative_path
480 * Determine where the destination path is located relative to the 'from' path.
482 static char *get_relative_path( const char *from, const char *dest )
484 const char *start;
485 char *ret, *p;
486 unsigned int dotdots = 0;
488 /* a path of "." is equivalent to an empty path */
489 if (!strcmp( from, "." )) from = "";
491 for (;;)
493 while (*from == '/') from++;
494 while (*dest == '/') dest++;
495 start = dest; /* save start of next path element */
496 if (!*from) break;
498 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
499 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
501 /* count remaining elements in 'from' */
504 dotdots++;
505 while (*from && *from != '/') from++;
506 while (*from == '/') from++;
508 while (*from);
509 break;
512 if (!start[0] && !dotdots) return NULL; /* empty path */
514 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
515 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
517 if (start[0]) strcpy( p, start );
518 else p[-1] = 0; /* remove trailing slash */
519 return ret;
523 /*******************************************************************
524 * concat_paths
526 static char *concat_paths( const char *base, const char *path )
528 if (!base) return xstrdup( path && path[0] ? path : "." );
529 if (!path || !path[0]) return xstrdup( base );
530 if (path[0] == '/') return xstrdup( path );
531 return strmake( "%s/%s", base, path );
535 /*******************************************************************
536 * base_dir_path
538 static char *base_dir_path( struct makefile *make, const char *path )
540 return concat_paths( make->base_dir, path );
544 /*******************************************************************
545 * obj_dir_path
547 static char *obj_dir_path( struct makefile *make, const char *path )
549 return concat_paths( make->obj_dir, path );
553 /*******************************************************************
554 * src_dir_path
556 static char *src_dir_path( struct makefile *make, const char *path )
558 if (make->src_dir) return concat_paths( make->src_dir, path );
559 return obj_dir_path( make, path );
563 /*******************************************************************
564 * top_obj_dir_path
566 static char *top_obj_dir_path( struct makefile *make, const char *path )
568 return concat_paths( make->top_obj_dir, path );
572 /*******************************************************************
573 * top_dir_path
575 static char *top_dir_path( struct makefile *make, const char *path )
577 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
578 return top_obj_dir_path( make, path );
582 /*******************************************************************
583 * tools_dir_path
585 static char *tools_dir_path( struct makefile *make, const char *path )
587 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
588 return top_obj_dir_path( make, strmake( "tools/%s", path ));
592 /*******************************************************************
593 * tools_path
595 static char *tools_path( struct makefile *make, const char *name )
597 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
601 /*******************************************************************
602 * get_line
604 static char *get_line( FILE *file )
606 static char *buffer;
607 static unsigned int size;
609 if (!size)
611 size = 1024;
612 buffer = xmalloc( size );
614 if (!fgets( buffer, size, file )) return NULL;
615 input_line++;
617 for (;;)
619 char *p = buffer + strlen(buffer);
620 /* if line is larger than buffer, resize buffer */
621 while (p == buffer + size - 1 && p[-1] != '\n')
623 buffer = xrealloc( buffer, size * 2 );
624 if (!fgets( buffer + size - 1, size + 1, file )) break;
625 p = buffer + strlen(buffer);
626 size *= 2;
628 if (p > buffer && p[-1] == '\n')
630 *(--p) = 0;
631 if (p > buffer && p[-1] == '\r') *(--p) = 0;
632 if (p > buffer && p[-1] == '\\')
634 *(--p) = 0;
635 /* line ends in backslash, read continuation line */
636 if (!fgets( p, size - (p - buffer), file )) return buffer;
637 input_line++;
638 continue;
641 return buffer;
645 /*******************************************************************
646 * find_src_file
648 static struct incl_file *find_src_file( const char *name )
650 struct incl_file *file;
652 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
653 if (!strcmp( name, file->name )) return file;
654 return NULL;
657 /*******************************************************************
658 * find_include_file
660 static struct incl_file *find_include_file( const char *name )
662 struct incl_file *file;
664 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
665 if (!strcmp( name, file->name )) return file;
666 return NULL;
669 /*******************************************************************
670 * add_include
672 * Add an include file if it doesn't already exists.
674 static struct incl_file *add_include( struct incl_file *parent, const char *name, int system )
676 struct incl_file *include;
677 char *ext;
679 if (parent->files_count >= parent->files_size)
681 parent->files_size *= 2;
682 if (parent->files_size < 16) parent->files_size = 16;
683 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
686 /* enforce some rules for the Wine tree */
688 if (!memcmp( name, "../", 3 ))
689 fatal_error( "#include directive with relative path not allowed\n" );
691 if (!strcmp( name, "config.h" ))
693 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
694 fatal_error( "config.h must not be included by a header file\n" );
695 if (parent->files_count)
696 fatal_error( "config.h must be included before anything else\n" );
698 else if (!strcmp( name, "wine/port.h" ))
700 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
701 fatal_error( "wine/port.h must not be included by a header file\n" );
702 if (!parent->files_count) fatal_error( "config.h must be included before wine/port.h\n" );
703 if (parent->files_count > 1)
704 fatal_error( "wine/port.h must be included before everything except config.h\n" );
705 if (strcmp( parent->files[0]->name, "config.h" ))
706 fatal_error( "config.h must be included before wine/port.h\n" );
709 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
710 if (!strcmp( name, include->name )) goto found;
712 include = xmalloc( sizeof(*include) );
713 memset( include, 0, sizeof(*include) );
714 include->name = xstrdup(name);
715 include->included_by = parent;
716 include->included_line = input_line;
717 if (system) include->flags |= FLAG_SYSTEM;
718 list_add_tail( &includes, &include->entry );
719 found:
720 parent->files[parent->files_count++] = include;
721 return include;
725 /*******************************************************************
726 * add_generated_source
728 * Add a generated source file to the list.
730 static struct incl_file *add_generated_source( struct makefile *make,
731 const char *name, const char *filename )
733 struct incl_file *file;
735 if ((file = find_src_file( name ))) return file; /* we already have it */
736 file = xmalloc( sizeof(*file) );
737 memset( file, 0, sizeof(*file) );
738 file->name = xstrdup( name );
739 file->filename = obj_dir_path( make, filename ? filename : name );
740 file->flags = FLAG_GENERATED;
741 list_add_tail( &sources, &file->entry );
742 return file;
746 /*******************************************************************
747 * open_file
749 static FILE *open_file( struct makefile *make, const char *path )
751 return fopen( base_dir_path( make, path ), "r" );
754 /*******************************************************************
755 * open_src_file
757 static FILE *open_src_file( struct makefile *make, struct incl_file *pFile )
759 FILE *file;
761 /* try in source dir */
762 pFile->filename = src_dir_path( make, pFile->name );
763 file = open_file( make, pFile->filename );
765 /* now try parent dir */
766 if (!file && make->parent_dir)
768 pFile->filename = src_dir_path( make, strmake( "%s/%s", make->parent_dir, pFile->name ));
769 file = open_file( make, pFile->filename );
771 if (!file) fatal_perror( "open %s", pFile->name );
772 return file;
776 /*******************************************************************
777 * open_include_file
779 static FILE *open_include_file( struct makefile *make, struct incl_file *pFile )
781 FILE *file = NULL;
782 char *filename, *p;
783 unsigned int i, len;
785 errno = ENOENT;
787 /* check for generated bison header */
789 if (strendswith( pFile->name, ".tab.h" ))
791 filename = src_dir_path( make, replace_extension( pFile->name, ".tab.h", ".y" ));
792 if ((file = open_file( make, filename )))
794 pFile->sourcename = filename;
795 pFile->filename = obj_dir_path( make, pFile->name );
796 /* don't bother to parse it */
797 fclose( file );
798 return NULL;
800 free( filename );
803 /* check for corresponding idl file in source dir */
805 if (strendswith( pFile->name, ".h" ))
807 filename = src_dir_path( make, replace_extension( pFile->name, ".h", ".idl" ));
808 if ((file = open_file( make, filename )))
810 pFile->sourcename = filename;
811 pFile->filename = obj_dir_path( make, pFile->name );
812 return file;
814 free( filename );
817 /* now try in source dir */
818 filename = src_dir_path( make, pFile->name );
819 if ((file = open_file( make, filename ))) goto found;
820 free( filename );
822 /* now try in parent source dir */
823 if (make->parent_dir)
825 filename = src_dir_path( make, strmake( "%s/%s", make->parent_dir, pFile->name ));
826 if ((file = open_file( make, filename ))) goto found;
827 free( filename );
830 /* check for corresponding idl file in global includes */
832 if (strendswith( pFile->name, ".h" ))
834 filename = top_dir_path( make, strmake( "include/%s",
835 replace_extension( pFile->name, ".h", ".idl" )));
836 if ((file = open_file( make, filename )))
838 pFile->sourcename = filename;
839 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
840 return file;
842 free( filename );
845 /* check for corresponding .in file in global includes (for config.h.in) */
847 if (strendswith( pFile->name, ".h" ))
849 filename = top_dir_path( make, strmake( "include/%s",
850 replace_extension( pFile->name, ".h", ".h.in" )));
851 if ((file = open_file( make, filename )))
853 pFile->sourcename = filename;
854 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
855 return file;
857 free( filename );
860 /* check for corresponding .x file in global includes */
862 if (strendswith( pFile->name, "tmpl.h" ))
864 filename = top_dir_path( make, strmake( "include/%s",
865 replace_extension( pFile->name, ".h", ".x" )));
866 if ((file = open_file( make, filename )))
868 pFile->sourcename = filename;
869 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
870 return file;
872 free( filename );
875 /* check in global includes source dir */
877 filename = top_dir_path( make, strmake( "include/%s", pFile->name ));
878 if ((file = open_file( make, filename ))) goto found;
880 /* check in global msvcrt includes */
881 if (make->use_msvcrt)
883 filename = top_dir_path( make, strmake( "include/msvcrt/%s", pFile->name ));
884 if ((file = open_file( make, filename ))) goto found;
887 /* now search in include paths */
888 for (i = 0; i < make->include_args.count; i++)
890 const char *dir = make->include_args.str[i] + 2; /* skip -I */
891 if (*dir == '/')
893 /* ignore absolute paths that don't point into the source dir */
894 if (!make->top_src_dir) continue;
895 len = strlen( make->top_src_dir );
896 if (strncmp( dir, make->top_src_dir, len )) continue;
897 if (dir[len] && dir[len] != '/') continue;
899 filename = strmake( "%s/%s", dir, pFile->name );
900 if ((file = open_file( make, filename ))) goto found;
901 free( filename );
903 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
905 /* try in src file directory */
906 if ((p = strrchr(pFile->included_by->filename, '/')))
908 int l = p - pFile->included_by->filename + 1;
909 filename = xmalloc(l + strlen(pFile->name) + 1);
910 memcpy( filename, pFile->included_by->filename, l );
911 strcpy( filename + l, pFile->name );
912 if ((file = open_file( make, filename ))) goto found;
913 free( filename );
916 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
917 perror( pFile->name );
918 pFile = pFile->included_by;
919 while (pFile && pFile->included_by)
921 const char *parent = pFile->included_by->sourcename;
922 if (!parent) parent = pFile->included_by->name;
923 fprintf( stderr, "%s:%d: note: %s was first included here\n",
924 parent, pFile->included_line, pFile->name );
925 pFile = pFile->included_by;
927 exit(1);
929 found:
930 pFile->filename = filename;
931 return file;
935 /*******************************************************************
936 * parse_include_directive
938 static void parse_include_directive( struct incl_file *source, char *str )
940 char quote, *include, *p = str;
942 while (*p && isspace(*p)) p++;
943 if (*p != '\"' && *p != '<' ) return;
944 quote = *p++;
945 if (quote == '<') quote = '>';
946 include = p;
947 while (*p && (*p != quote)) p++;
948 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
949 *p = 0;
950 add_include( source, include, (quote == '>') );
954 /*******************************************************************
955 * parse_pragma_directive
957 static void parse_pragma_directive( struct incl_file *source, char *str )
959 char *flag, *p = str;
961 if (!isspace( *p )) return;
962 while (*p && isspace(*p)) p++;
963 p = strtok( p, " \t" );
964 if (strcmp( p, "makedep" )) return;
966 while ((flag = strtok( NULL, " \t" )))
968 if (!strcmp( flag, "depend" ))
970 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
971 return;
973 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
975 if (strendswith( source->name, ".idl" ))
977 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
978 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
979 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
980 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
981 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
982 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
983 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
984 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
986 else if (strendswith( source->name, ".rc" ))
988 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
990 else if (strendswith( source->name, ".sfd" ))
992 if (!strcmp( flag, "font" ))
994 struct strarray *array = source->args;
996 if (!array)
998 source->args = array = xmalloc( sizeof(*array) );
999 *array = empty_strarray;
1000 source->flags |= FLAG_SFD_FONTS;
1002 strarray_add( array, xstrdup( strtok( NULL, "" )));
1003 return;
1006 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
1011 /*******************************************************************
1012 * parse_cpp_directive
1014 static void parse_cpp_directive( struct incl_file *source, char *str )
1016 while (*str && isspace(*str)) str++;
1017 if (*str++ != '#') return;
1018 while (*str && isspace(*str)) str++;
1020 if (!strncmp( str, "include", 7 ))
1021 parse_include_directive( source, str + 7 );
1022 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
1023 parse_include_directive( source, str + 6 );
1024 else if (!strncmp( str, "pragma", 6 ))
1025 parse_pragma_directive( source, str + 6 );
1029 /*******************************************************************
1030 * parse_idl_file
1032 * If for_h_file is non-zero, it means we are not interested in the idl file
1033 * itself, but only in the contents of the .h file that will be generated from it.
1035 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
1037 char *buffer, *include;
1039 input_line = 0;
1040 if (for_h_file)
1042 /* generated .h file always includes these */
1043 add_include( pFile, "rpc.h", 1 );
1044 add_include( pFile, "rpcndr.h", 1 );
1047 while ((buffer = get_line( file )))
1049 char quote;
1050 char *p = buffer;
1051 while (*p && isspace(*p)) p++;
1053 if (!strncmp( p, "import", 6 ))
1055 p += 6;
1056 while (*p && isspace(*p)) p++;
1057 if (*p != '"') continue;
1058 include = ++p;
1059 while (*p && (*p != '"')) p++;
1060 if (!*p) fatal_error( "malformed import directive\n" );
1061 *p = 0;
1062 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
1063 add_include( pFile, include, 0 );
1064 continue;
1067 if (for_h_file) /* only check for #include inside cpp_quote */
1069 if (strncmp( p, "cpp_quote", 9 )) continue;
1070 p += 9;
1071 while (*p && isspace(*p)) p++;
1072 if (*p++ != '(') continue;
1073 while (*p && isspace(*p)) p++;
1074 if (*p++ != '"') continue;
1075 if (*p++ != '#') continue;
1076 while (*p && isspace(*p)) p++;
1077 if (strncmp( p, "include", 7 )) continue;
1078 p += 7;
1079 while (*p && isspace(*p)) p++;
1080 if (*p == '\\' && p[1] == '"')
1082 p += 2;
1083 quote = '"';
1085 else
1087 if (*p++ != '<' ) continue;
1088 quote = '>';
1090 include = p;
1091 while (*p && (*p != quote)) p++;
1092 if (!*p || (quote == '"' && p[-1] != '\\'))
1093 fatal_error( "malformed #include directive inside cpp_quote\n" );
1094 if (quote == '"') p--; /* remove backslash */
1095 *p = 0;
1096 add_include( pFile, include, (quote == '>') );
1097 continue;
1100 parse_cpp_directive( pFile, p );
1104 /*******************************************************************
1105 * parse_c_file
1107 static void parse_c_file( struct incl_file *pFile, FILE *file )
1109 char *buffer;
1111 input_line = 0;
1112 while ((buffer = get_line( file )))
1114 parse_cpp_directive( pFile, buffer );
1119 /*******************************************************************
1120 * parse_rc_file
1122 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1124 char *buffer, *include;
1126 input_line = 0;
1127 while ((buffer = get_line( file )))
1129 char quote;
1130 char *p = buffer;
1131 while (*p && isspace(*p)) p++;
1133 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1135 p += 2;
1136 while (*p && isspace(*p)) p++;
1137 if (strncmp( p, "@makedep:", 9 )) continue;
1138 p += 9;
1139 while (*p && isspace(*p)) p++;
1140 quote = '"';
1141 if (*p == quote)
1143 include = ++p;
1144 while (*p && *p != quote) p++;
1146 else
1148 include = p;
1149 while (*p && !isspace(*p) && *p != '*') p++;
1151 if (!*p)
1152 fatal_error( "malformed makedep comment\n" );
1153 *p = 0;
1154 add_include( pFile, include, (quote == '>') );
1155 continue;
1158 parse_cpp_directive( pFile, buffer );
1163 /*******************************************************************
1164 * parse_in_file
1166 static void parse_in_file( struct incl_file *source, FILE *file )
1168 char *p, *buffer;
1170 /* make sure it gets rebuilt when the version changes */
1171 add_include( source, "config.h", 1 );
1173 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1175 input_line = 0;
1176 while ((buffer = get_line( file )))
1178 if (strncmp( buffer, ".TH", 3 )) continue;
1179 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1180 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1181 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1182 source->args = xstrdup( p );
1183 return;
1188 /*******************************************************************
1189 * parse_sfd_file
1191 static void parse_sfd_file( struct incl_file *source, FILE *file )
1193 char *p, *eol, *buffer;
1195 input_line = 0;
1196 while ((buffer = get_line( file )))
1198 if (strncmp( buffer, "UComments:", 10 )) continue;
1199 p = buffer + 10;
1200 while (*p == ' ') p++;
1201 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1203 p++;
1204 buffer[strlen(buffer) - 1] = 0;
1206 while ((eol = strstr( p, "+AAoA" )))
1208 *eol = 0;
1209 while (*p && isspace(*p)) p++;
1210 if (*p++ == '#')
1212 while (*p && isspace(*p)) p++;
1213 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1215 p = eol + 5;
1217 while (*p && isspace(*p)) p++;
1218 if (*p++ != '#') return;
1219 while (*p && isspace(*p)) p++;
1220 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1221 return;
1226 /*******************************************************************
1227 * parse_file
1229 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1231 FILE *file;
1233 /* don't try to open certain types of files */
1234 if (strendswith( source->name, ".tlb" ))
1236 source->filename = obj_dir_path( make, source->name );
1237 return;
1240 file = src ? open_src_file( make, source ) : open_include_file( make, source );
1241 if (!file) return;
1242 input_file_name = source->filename;
1244 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1245 parse_idl_file( source, file, 1 );
1246 else if (strendswith( source->filename, ".idl" ))
1247 parse_idl_file( source, file, 0 );
1248 else if (strendswith( source->filename, ".c" ) ||
1249 strendswith( source->filename, ".m" ) ||
1250 strendswith( source->filename, ".h" ) ||
1251 strendswith( source->filename, ".l" ) ||
1252 strendswith( source->filename, ".y" ))
1253 parse_c_file( source, file );
1254 else if (strendswith( source->filename, ".rc" ))
1255 parse_rc_file( source, file );
1256 else if (strendswith( source->filename, ".in" ))
1257 parse_in_file( source, file );
1258 else if (strendswith( source->filename, ".sfd" ))
1259 parse_sfd_file( source, file );
1260 fclose(file);
1261 input_file_name = NULL;
1265 /*******************************************************************
1266 * add_src_file
1268 * Add a source file to the list.
1270 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1272 struct incl_file *file;
1274 if ((file = find_src_file( name ))) return file; /* we already have it */
1275 file = xmalloc( sizeof(*file) );
1276 memset( file, 0, sizeof(*file) );
1277 file->name = xstrdup(name);
1278 list_add_tail( &sources, &file->entry );
1279 parse_file( make, file, 1 );
1280 return file;
1284 /*******************************************************************
1285 * get_make_variable
1287 static char *get_make_variable( struct makefile *make, const char *name )
1289 char *ret;
1291 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1292 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1293 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1294 return NULL;
1298 /*******************************************************************
1299 * get_expanded_make_variable
1301 static char *get_expanded_make_variable( struct makefile *make, const char *name )
1303 char *p, *end, *var, *expand, *tmp;
1305 expand = get_make_variable( make, name );
1306 if (!expand) return NULL;
1308 p = expand;
1309 while ((p = strchr( p, '$' )))
1311 if (p[1] == '(')
1313 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1314 *end++ = 0;
1315 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1316 var = get_make_variable( make, p + 2 );
1317 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1318 free( var );
1320 else if (p[1] == '{') /* don't expand ${} variables */
1322 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1323 p = end + 1;
1324 continue;
1326 else if (p[1] == '$')
1328 tmp = replace_substr( expand, p, 2, "$" );
1330 else fatal_error( "syntax error in '%s'\n", expand );
1332 /* switch to the new string */
1333 p = tmp + (p - expand);
1334 free( expand );
1335 expand = tmp;
1338 /* consider empty variables undefined */
1339 p = expand;
1340 while (*p && isspace(*p)) p++;
1341 if (*p) return expand;
1342 free( expand );
1343 return NULL;
1347 /*******************************************************************
1348 * get_expanded_make_var_array
1350 static struct strarray get_expanded_make_var_array( struct makefile *make, const char *name )
1352 struct strarray ret = empty_strarray;
1353 char *value, *token;
1355 if ((value = get_expanded_make_variable( make, name )))
1356 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1357 strarray_add( &ret, token );
1358 return ret;
1362 /*******************************************************************
1363 * set_make_variable
1365 static int set_make_variable( struct strarray *array, const char *assignment )
1367 char *p, *name;
1369 p = name = xstrdup( assignment );
1370 while (isalnum(*p) || *p == '_') p++;
1371 if (name == p) return 0; /* not a variable */
1372 if (isspace(*p))
1374 *p++ = 0;
1375 while (isspace(*p)) p++;
1377 if (*p != '=') return 0; /* not an assignment */
1378 *p++ = 0;
1379 while (isspace(*p)) p++;
1381 strarray_set_value( array, name, p );
1382 return 1;
1386 /*******************************************************************
1387 * parse_makefile
1389 static struct makefile *parse_makefile( const char *path, const char *separator )
1391 char *buffer;
1392 FILE *file;
1393 struct makefile *make = xmalloc( sizeof(*make) );
1395 memset( make, 0, sizeof(*make) );
1396 if (path)
1398 make->top_obj_dir = get_relative_path( path, "" );
1399 make->base_dir = path;
1400 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1403 input_file_name = base_dir_path( make, makefile_name );
1404 if (!(file = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1406 input_line = 0;
1407 while ((buffer = get_line( file )))
1409 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1410 if (*buffer == '\t') continue; /* command */
1411 while (isspace( *buffer )) buffer++;
1412 if (*buffer == '#') continue; /* comment */
1413 set_make_variable( &make->vars, buffer );
1415 fclose( file );
1416 input_file_name = NULL;
1417 return make;
1421 /*******************************************************************
1422 * add_generated_sources
1424 static void add_generated_sources( struct makefile *make )
1426 struct incl_file *source, *next, *file;
1428 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1430 if (source->flags & FLAG_IDL_CLIENT)
1432 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1433 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1435 if (source->flags & FLAG_IDL_SERVER)
1437 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1438 add_include( file, "wine/exception.h", 0 );
1439 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1441 if (source->flags & FLAG_IDL_IDENT)
1443 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1444 add_include( file, "rpc.h", 0 );
1445 add_include( file, "rpcndr.h", 0 );
1446 add_include( file, "guiddef.h", 0 );
1448 if (source->flags & FLAG_IDL_PROXY)
1450 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1451 add_include( file, "objbase.h", 0 );
1452 add_include( file, "rpcproxy.h", 0 );
1453 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1454 add_include( file, "objbase.h", 0 );
1455 add_include( file, "rpcproxy.h", 0 );
1456 add_include( file, "wine/exception.h", 0 );
1457 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1459 if (source->flags & FLAG_IDL_REGTYPELIB)
1461 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1463 if (source->flags & FLAG_IDL_REGISTER)
1465 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1467 if (strendswith( source->name, ".y" ))
1469 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1470 /* steal the includes list from the source file */
1471 file->files_count = source->files_count;
1472 file->files_size = source->files_size;
1473 file->files = source->files;
1474 source->files_count = source->files_size = 0;
1475 source->files = NULL;
1477 if (strendswith( source->name, ".l" ))
1479 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1480 /* steal the includes list from the source file */
1481 file->files_count = source->files_count;
1482 file->files_size = source->files_size;
1483 file->files = source->files;
1484 source->files_count = source->files_size = 0;
1485 source->files = NULL;
1488 if (get_make_variable( make, "TESTDLL" ))
1490 file = add_generated_source( make, "testlist.o", "testlist.c" );
1491 add_include( file, "wine/test.h", 0 );
1496 /*******************************************************************
1497 * create_dir
1499 static void create_dir( const char *dir )
1501 char *p, *path;
1503 p = path = xstrdup( dir );
1504 while ((p = strchr( p, '/' )))
1506 *p = 0;
1507 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1508 *p++ = '/';
1509 while (*p == '/') p++;
1511 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1512 free( path );
1516 /*******************************************************************
1517 * output_filenames_obj_dir
1519 static void output_filenames_obj_dir( struct makefile *make, struct strarray array )
1521 unsigned int i;
1523 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1527 /*******************************************************************
1528 * output_include
1530 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1532 int i;
1534 if (pFile->owner == owner) return;
1535 if (!pFile->filename) return;
1536 pFile->owner = owner;
1537 output_filename( pFile->filename );
1538 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1542 /*******************************************************************
1543 * output_sources
1545 static struct strarray output_sources( struct makefile *make, struct strarray *testlist_files )
1547 struct incl_file *source;
1548 unsigned int i;
1549 struct strarray object_files = empty_strarray;
1550 struct strarray crossobj_files = empty_strarray;
1551 struct strarray res_files = empty_strarray;
1552 struct strarray clean_files = empty_strarray;
1553 struct strarray po_files = empty_strarray;
1554 struct strarray mo_files = empty_strarray;
1555 struct strarray mc_files = empty_strarray;
1556 struct strarray ok_files = empty_strarray;
1557 struct strarray dlldata_files = empty_strarray;
1558 struct strarray c2man_files = empty_strarray;
1559 struct strarray implib_objs = empty_strarray;
1560 struct strarray includes = empty_strarray;
1561 struct strarray subdirs = empty_strarray;
1562 struct strarray phony_targets = empty_strarray;
1563 struct strarray all_targets = get_expanded_make_var_array( make, "PROGRAMS" );
1565 for (i = 0; i < linguas.count; i++)
1566 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
1568 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
1569 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
1570 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
1571 if (make->top_obj_dir) strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
1572 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
1573 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
1574 strarray_addall( &includes, make->include_args );
1576 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1578 struct strarray extradefs;
1579 char *obj = xstrdup( source->name );
1580 char *ext = get_extension( obj );
1582 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1583 *ext++ = 0;
1585 if (make->src_dir && strchr( obj, '/' ))
1587 char *subdir = base_dir_path( make, obj );
1588 *strrchr( subdir, '/' ) = 0;
1589 strarray_add_uniq( &subdirs, subdir );
1592 extradefs = get_expanded_make_var_array( make, strmake( "%s_EXTRADEFS", obj ));
1594 if (!strcmp( ext, "y" )) /* yacc file */
1596 /* add source file dependency for parallel makes */
1597 char *header = strmake( "%s.tab.h", obj );
1599 if (find_include_file( header ))
1601 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
1602 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1603 obj, obj_dir_path( make, obj ), source->filename );
1604 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
1605 source->filename, obj_dir_path( make, header ));
1606 strarray_add( &clean_files, header );
1608 else output( "%s.tab.c: %s\n", obj, source->filename );
1610 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1611 continue; /* no dependencies */
1613 else if (!strcmp( ext, "x" )) /* template file */
1615 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
1616 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1617 output( "\t%s%s -H -o $@ %s\n",
1618 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1619 strarray_add( &clean_files, strmake( "%s.h", obj ));
1620 continue; /* no dependencies */
1622 else if (!strcmp( ext, "l" )) /* lex file */
1624 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
1625 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1626 continue; /* no dependencies */
1628 else if (!strcmp( ext, "rc" )) /* resource file */
1630 strarray_add( &res_files, strmake( "%s.res", obj ));
1631 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1632 tools_path( make, "wrc" ), source->filename );
1633 output( "\t%s -o $@ %s", tools_path( make, "wrc" ), source->filename );
1634 if (make->is_win16) output_filename( "-m16" );
1635 else output_filenames( target_flags );
1636 output_filename( "--nostdinc" );
1637 output_filenames( includes );
1638 output_filenames( make->define_args );
1639 output_filenames( extradefs );
1640 if (mo_files.count && (source->flags & FLAG_RC_PO))
1642 strarray_add( &po_files, source->filename );
1643 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1644 output( "\n" );
1645 output( "%s.res:", obj_dir_path( make, obj ));
1646 output_filenames( mo_files );
1647 output( "\n" );
1648 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
1650 else output( "\n" );
1651 output( "%s.res:", obj_dir_path( make, obj ));
1653 else if (!strcmp( ext, "mc" )) /* message file */
1655 strarray_add( &res_files, strmake( "%s.res", obj ));
1656 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1657 tools_path( make, "wmc" ), source->filename );
1658 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
1659 if (mo_files.count)
1661 strarray_add( &mc_files, source->filename );
1662 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1663 output( "\n" );
1664 output( "%s.res:", obj_dir_path( make, obj ));
1665 output_filenames( mo_files );
1666 output( "\n" );
1667 output( "%s ", obj_dir_path( make, "msg.pot" ));
1669 else output( "\n" );
1670 output( "%s.res:", obj_dir_path( make, obj ));
1672 else if (!strcmp( ext, "idl" )) /* IDL file */
1674 struct strarray targets = empty_strarray;
1675 char *dest;
1677 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1678 source->flags |= FLAG_IDL_HEADER;
1680 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1682 if (!(source->flags & idl_outputs[i].flag)) continue;
1683 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1684 if (!find_src_file( dest )) strarray_add( &clean_files, dest );
1685 strarray_add( &targets, dest );
1687 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1688 output_filenames_obj_dir( make, targets );
1689 output( ": %s\n", tools_path( make, "widl" ));
1690 output( "\t%s -o $@ %s", tools_path( make, "widl" ), source->filename );
1691 output_filenames( target_flags );
1692 output_filenames( includes );
1693 output_filenames( make->define_args );
1694 output_filenames( extradefs );
1695 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
1696 output( "\n" );
1697 output_filenames_obj_dir( make, targets );
1698 output( ": %s", source->filename );
1700 else if (!strcmp( ext, "in" )) /* .in file or man page */
1702 if (strendswith( obj, ".man" ) && source->args)
1704 char *dir, *dest = replace_extension( obj, ".man", "" );
1705 char *lang = strchr( dest, '.' );
1706 char *section = source->args;
1707 if (lang)
1709 *lang++ = 0;
1710 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1712 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1713 output( "install-man-pages:: %s\n", obj_dir_path( make, obj ));
1714 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj_dir_path( make, obj ), dir, dest, section );
1715 output( "uninstall::\n" );
1716 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1717 free( dest );
1718 free( dir );
1719 strarray_add( &all_targets, xstrdup(obj) );
1720 strarray_add_uniq( &phony_targets, "install-man-pages" );
1721 strarray_add_uniq( &phony_targets, "uninstall" );
1723 else strarray_add( &clean_files, xstrdup(obj) );
1724 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
1725 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1726 output( "%s:", obj_dir_path( make, obj ));
1728 else if (!strcmp( ext, "sfd" )) /* font file */
1730 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
1731 if (fontforge && !make->src_dir)
1733 output( "%s: %s\n", ttf_file, source->filename );
1734 output( "\t%s -script %s %s $@\n",
1735 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
1736 if (!(source->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1738 if (source->flags & FLAG_INSTALL)
1740 output( "install install-lib::\n" );
1741 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1742 output( "uninstall::\n" );
1743 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1745 if (source->flags & FLAG_SFD_FONTS)
1747 struct strarray *array = source->args;
1749 for (i = 0; i < array->count; i++)
1751 char *font = strtok( xstrdup(array->str[i]), " \t" );
1752 char *args = strtok( NULL, "" );
1754 strarray_add( &all_targets, xstrdup( font ));
1755 output( "%s: %s %s\n", obj_dir_path( make, font ),
1756 tools_path( make, "sfnt2fon" ), ttf_file );
1757 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
1758 output( "install install-lib:: %s\n", font );
1759 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n",
1760 obj_dir_path( make, font ), font );
1761 output( "uninstall::\n" );
1762 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1765 if (source->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1767 strarray_add_uniq( &phony_targets, "install" );
1768 strarray_add_uniq( &phony_targets, "install-lib" );
1769 strarray_add_uniq( &phony_targets, "uninstall" );
1771 continue; /* no dependencies */
1773 else if (!strcmp( ext, "svg" )) /* svg file */
1775 if (convert && rsvg && icotool && !make->src_dir)
1777 output( "%s.ico %s.bmp: %s\n",
1778 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
1779 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
1780 top_dir_path( make, "tools/buildimage" ), source->filename );
1782 continue; /* no dependencies */
1784 else if (!strcmp( ext, "res" ))
1786 strarray_add( &res_files, source->name );
1787 continue; /* no dependencies */
1789 else
1791 int need_cross = make->testdll ||
1792 (source->flags & FLAG_C_IMPLIB) ||
1793 (make->module && make->staticlib);
1795 if ((source->flags & FLAG_GENERATED) &&
1796 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
1797 strarray_add( &clean_files, source->filename );
1798 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1799 strarray_add( &object_files, strmake( "%s.o", obj ));
1800 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
1801 output( "\t$(CC) -c -o $@ %s", source->filename );
1802 output_filenames( includes );
1803 output_filenames( make->define_args );
1804 output_filenames( extradefs );
1805 if (make->module || make->staticlib || make->testdll)
1807 output_filenames( dll_flags );
1808 if (make->use_msvcrt) output_filenames( msvcrt_flags );
1810 output_filenames( extra_cflags );
1811 output_filenames( cpp_flags );
1812 output_filename( "$(CFLAGS)" );
1813 output( "\n" );
1814 if (crosstarget && need_cross)
1816 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1817 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
1818 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1819 output_filenames( includes );
1820 output_filenames( make->define_args );
1821 output_filenames( extradefs );
1822 output_filename( "-DWINE_CROSSTEST" );
1823 output_filenames( cpp_flags );
1824 output_filename( "$(CFLAGS)" );
1825 output( "\n" );
1827 if (make->testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1829 strarray_add( &ok_files, strmake( "%s.ok", obj ));
1830 output( "%s.ok:\n", obj_dir_path( make, obj ));
1831 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1832 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
1833 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
1835 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1836 strarray_add( &c2man_files, source->filename );
1837 output( "%s.o", obj_dir_path( make, obj ));
1838 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
1839 output( ":" );
1841 free( obj );
1843 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
1844 output( "\n" );
1847 /* rules for files that depend on multiple sources */
1849 if (po_files.count)
1851 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
1852 output_filenames( po_files );
1853 output( "\n" );
1854 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
1855 output_filenames( po_files );
1856 if (make->is_win16) output_filename( "-m16" );
1857 else output_filenames( target_flags );
1858 output_filename( "--nostdinc" );
1859 output_filenames( includes );
1860 output_filenames( make->define_args );
1861 output( "\n" );
1862 strarray_add( &clean_files, "rsrc.pot" );
1865 if (mc_files.count)
1867 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
1868 output_filenames( mc_files );
1869 output( "\n" );
1870 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
1871 output_filenames( mc_files );
1872 output( "\n" );
1873 strarray_add( &clean_files, "msg.pot" );
1876 if (dlldata_files.count)
1878 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
1879 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
1880 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
1881 output_filenames( dlldata_files );
1882 output( "\n" );
1885 if (make->module && !make->staticlib)
1887 struct strarray all_libs = empty_strarray;
1888 char *module_path = obj_dir_path( make, make->module );
1889 char *spec_file = NULL;
1891 if (!make->appmode.count)
1892 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
1893 for (i = 0; i < make->delayimports.count; i++)
1894 strarray_add( &all_libs, strmake( "-l%s", make->delayimports.str[i] ));
1895 for (i = 0; i < make->imports.count; i++)
1896 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
1897 for (i = 0; i < make->delayimports.count; i++)
1898 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
1899 strarray_add( &all_libs, "-lwine" );
1900 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
1901 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
1902 strarray_addall( &all_libs, libs );
1904 if (*dll_ext)
1906 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
1907 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
1908 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
1910 else
1912 strarray_add( &all_targets, make->module );
1913 output( "%s:", module_path );
1915 if (spec_file) output_filename( spec_file );
1916 output_filenames_obj_dir( make, object_files );
1917 output_filenames_obj_dir( make, res_files );
1918 output( "\n" );
1919 output( "\t%s -o $@", tools_path( make, "winegcc" ));
1920 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
1921 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
1922 output_filenames( target_flags );
1923 output_filenames( unwind_flags );
1924 if (spec_file)
1926 output( " -shared %s", spec_file );
1927 output_filenames( make->extradllflags );
1929 else output_filenames( make->appmode );
1930 output_filenames_obj_dir( make, object_files );
1931 output_filenames_obj_dir( make, res_files );
1932 output_filenames( all_libs );
1933 output_filename( "$(LDFLAGS)" );
1934 output( "\n" );
1936 if (spec_file && make->importlib)
1938 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
1939 if (*dll_ext)
1941 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
1942 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
1943 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
1944 output_filenames( target_flags );
1945 if (make->is_win16) output_filename( "-m16" );
1946 output( "\n" );
1947 if (implib_objs.count)
1949 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
1950 output( "%s.def.a:", importlib_path );
1951 output_filenames_obj_dir( make, implib_objs );
1952 output( "\n" );
1953 output( "\t$(RM) $@\n" );
1954 output( "\t$(AR) $(ARFLAGS) $@" );
1955 output_filenames_obj_dir( make, implib_objs );
1956 output( "\n" );
1957 output( "\t$(RANLIB) $@\n" );
1960 else
1962 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
1963 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
1964 output_filenames_obj_dir( make, implib_objs );
1965 output( "\n" );
1966 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
1967 output_filenames( target_flags );
1968 output_filenames_obj_dir( make, implib_objs );
1969 output( "\n" );
1971 if (crosstarget && !make->is_win16)
1973 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
1974 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
1975 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
1976 output_filenames_obj_dir( make, cross_files );
1977 output( "\n" );
1978 output( "\t%s -b %s -w --implib -o $@ --export %s",
1979 tools_path( make, "winebuild" ), crosstarget, spec_file );
1980 output_filenames_obj_dir( make, cross_files );
1981 output( "\n" );
1985 if (spec_file)
1987 if (c2man_files.count)
1989 output( "manpages::\n" );
1990 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
1991 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
1992 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
1993 output_filename( strmake( "-o %s/man%s",
1994 top_obj_dir_path( make, "documentation" ), man_ext ));
1995 output_filenames( c2man_files );
1996 output( "\n" );
1997 output( "htmlpages::\n" );
1998 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
1999 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2000 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2001 output_filename( strmake( "-o %s",
2002 top_obj_dir_path( make, "documentation/html" )));
2003 output_filenames( c2man_files );
2004 output( "\n" );
2005 output( "sgmlpages::\n" );
2006 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2007 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2008 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2009 output_filename( strmake( "-o %s",
2010 top_obj_dir_path( make, "documentation/api-guide" )));
2011 output_filenames( c2man_files );
2012 output( "\n" );
2013 output( "xmlpages::\n" );
2014 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2015 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2016 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2017 output_filename( strmake( "-o %s",
2018 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2019 output_filenames( c2man_files );
2020 output( "\n" );
2021 strarray_add( &phony_targets, "manpages" );
2022 strarray_add( &phony_targets, "htmlpages" );
2023 strarray_add( &phony_targets, "sgmlpages" );
2024 strarray_add( &phony_targets, "xmlpages" );
2026 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2030 if (make->staticlib)
2032 strarray_add( &all_targets, make->staticlib );
2033 output( "%s:", obj_dir_path( make, make->staticlib ));
2034 output_filenames_obj_dir( make, object_files );
2035 output( "\n\t$(RM) $@\n" );
2036 output( "\t$(AR) $(ARFLAGS) $@" );
2037 output_filenames_obj_dir( make, object_files );
2038 output( "\n\t$(RANLIB) $@\n" );
2039 if (crosstarget && make->module)
2041 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2043 strarray_add( &all_targets, name );
2044 output( "%s:", obj_dir_path( make, name ));
2045 output_filenames_obj_dir( make, crossobj_files );
2046 output( "\n\t$(RM) $@\n" );
2047 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2048 output_filenames_obj_dir( make, crossobj_files );
2049 output( "\n\t%s-ranlib $@\n", crosstarget );
2053 if (make->testdll)
2055 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2056 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2057 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2058 struct strarray all_libs = empty_strarray;
2060 for (i = 0; i < make->imports.count; i++)
2061 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2062 strarray_addall( &all_libs, get_expanded_make_var_array( make, "LIBS" ));
2064 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2065 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2066 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2067 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2068 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2069 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2070 output_filenames( target_flags );
2071 output_filenames( unwind_flags );
2072 output_filenames( make->appmode );
2073 output_filenames_obj_dir( make, object_files );
2074 output_filenames_obj_dir( make, res_files );
2075 output_filenames( all_libs );
2076 output_filename( "$(LDFLAGS)" );
2077 output( "\n" );
2078 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2079 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2080 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2081 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2082 output_filenames( target_flags );
2083 output_filenames( unwind_flags );
2084 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2085 output_filenames( make->appmode );
2086 output_filenames_obj_dir( make, object_files );
2087 output_filenames_obj_dir( make, res_files );
2088 output_filenames( all_libs );
2089 output_filename( "$(LDFLAGS)" );
2090 output( "\n" );
2091 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2092 obj_dir_path( make, stripped ), dll_ext );
2093 output_filenames_obj_dir( make, object_files );
2094 output_filenames_obj_dir( make, res_files );
2095 output( "\n" );
2097 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2098 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2099 obj_dir_path( make, stripped ), dll_ext );
2100 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2101 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2103 if (crosstarget)
2105 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2107 strarray_add( &clean_files, crosstest );
2108 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2109 output( "%s:", obj_dir_path( make, crosstest ));
2110 output_filenames_obj_dir( make, crossobj_files );
2111 output_filenames_obj_dir( make, res_files );
2112 output( "\n" );
2113 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2114 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2115 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2116 output_filename( "--lib-suffix=.cross.a" );
2117 output_filenames_obj_dir( make, crossobj_files );
2118 output_filenames_obj_dir( make, res_files );
2119 output_filenames( all_libs );
2120 output_filename( "$(LDFLAGS)" );
2121 output( "\n" );
2122 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2123 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2126 output_filenames_obj_dir( make, ok_files );
2127 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2128 output( "check test:" );
2129 output_filenames_obj_dir( make, ok_files );
2130 output( "\n" );
2131 output( "testclean::\n" );
2132 output( "\t$(RM)" );
2133 output_filenames_obj_dir( make, ok_files );
2134 output( "\n" );
2135 strarray_addall( &clean_files, ok_files );
2136 strarray_add( &phony_targets, "check" );
2137 strarray_add( &phony_targets, "test" );
2138 strarray_add( &phony_targets, "testclean" );
2139 *testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2142 if (all_targets.count)
2144 output( "all:" );
2145 output_filenames_obj_dir( make, all_targets );
2146 output( "\n" );
2149 strarray_addall( &clean_files, object_files );
2150 strarray_addall( &clean_files, crossobj_files );
2151 strarray_addall( &clean_files, res_files );
2152 strarray_addall( &clean_files, all_targets );
2153 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2155 if (clean_files.count)
2157 output( "%s::\n", obj_dir_path( make, "clean" ));
2158 output( "\t$(RM)" );
2159 output_filenames_obj_dir( make, clean_files );
2160 output( "\n" );
2161 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2162 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2165 if (make->top_obj_dir)
2167 output( "depend:\n" );
2168 output( "\t@cd %s && $(MAKE) %s\n", make->top_obj_dir, base_dir_path( make, "depend" ));
2169 strarray_add( &phony_targets, "depend" );
2172 if (phony_targets.count)
2174 output( ".PHONY:" );
2175 output_filenames( phony_targets );
2176 output( "\n" );
2179 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2181 return clean_files;
2185 /*******************************************************************
2186 * create_temp_file
2188 static FILE *create_temp_file( const char *orig )
2190 char *name = xmalloc( strlen(orig) + 13 );
2191 unsigned int i, id = getpid();
2192 int fd;
2193 FILE *ret = NULL;
2195 for (i = 0; i < 100; i++)
2197 sprintf( name, "%s.tmp%08x", orig, id );
2198 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2200 ret = fdopen( fd, "w" );
2201 break;
2203 if (errno != EEXIST) break;
2204 id += 7777;
2206 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2207 temp_file_name = name;
2208 return ret;
2212 /*******************************************************************
2213 * rename_temp_file
2215 static void rename_temp_file( const char *dest )
2217 int ret = rename( temp_file_name, dest );
2218 if (ret == -1 && errno == EEXIST)
2220 /* rename doesn't overwrite on windows */
2221 unlink( dest );
2222 ret = rename( temp_file_name, dest );
2224 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2225 temp_file_name = NULL;
2229 /*******************************************************************
2230 * are_files_identical
2232 static int are_files_identical( FILE *file1, FILE *file2 )
2234 for (;;)
2236 char buffer1[8192], buffer2[8192];
2237 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2238 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2239 if (size1 != size2) return 0;
2240 if (!size1) return feof( file1 ) && feof( file2 );
2241 if (memcmp( buffer1, buffer2, size1 )) return 0;
2246 /*******************************************************************
2247 * rename_temp_file_if_changed
2249 static void rename_temp_file_if_changed( const char *dest )
2251 FILE *file1, *file2;
2252 int do_rename = 1;
2254 if ((file1 = fopen( dest, "r" )))
2256 if ((file2 = fopen( temp_file_name, "r" )))
2258 do_rename = !are_files_identical( file1, file2 );
2259 fclose( file2 );
2261 fclose( file1 );
2263 if (!do_rename)
2265 unlink( temp_file_name );
2266 temp_file_name = NULL;
2268 else rename_temp_file( dest );
2272 /*******************************************************************
2273 * output_testlist
2275 static void output_testlist( const char *dest, struct strarray files )
2277 int i;
2279 output_file = create_temp_file( dest );
2281 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2282 output( "#define WIN32_LEAN_AND_MEAN\n" );
2283 output( "#include <windows.h>\n\n" );
2284 output( "#define STANDALONE\n" );
2285 output( "#include \"wine/test.h\"\n\n" );
2287 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2288 output( "\n" );
2289 output( "const struct test winetest_testlist[] =\n" );
2290 output( "{\n" );
2291 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2292 output( " { 0, 0 }\n" );
2293 output( "};\n" );
2295 if (fclose( output_file )) fatal_perror( "write" );
2296 output_file = NULL;
2297 rename_temp_file_if_changed( dest );
2301 /*******************************************************************
2302 * output_gitignore
2304 static void output_gitignore( const char *dest, struct strarray files )
2306 int i;
2308 output_file = create_temp_file( dest );
2310 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2311 for (i = 0; i < files.count; i++)
2313 if (!strchr( files.str[i], '/' )) output( "/" );
2314 output( "%s\n", files.str[i] );
2317 if (fclose( output_file )) fatal_perror( "write" );
2318 output_file = NULL;
2319 rename_temp_file( dest );
2323 /*******************************************************************
2324 * output_dependencies
2326 static void output_dependencies( struct makefile *make, const char *path )
2328 struct strarray targets, testlist_files = empty_strarray, ignore_files = empty_strarray;
2330 if (Separator && ((output_file = fopen( path, "r" ))))
2332 char buffer[1024];
2333 FILE *tmp_file = create_temp_file( path );
2334 int found = 0;
2336 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2338 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2339 found = !strncmp( buffer, Separator, strlen(Separator) );
2341 if (fclose( output_file )) fatal_perror( "write" );
2342 output_file = tmp_file;
2343 if (!found) output( "\n%s\n", Separator );
2345 else
2347 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2348 fatal_perror( "%s", path );
2351 targets = output_sources( make, &testlist_files );
2353 fclose( output_file );
2354 output_file = NULL;
2355 if (temp_file_name) rename_temp_file( path );
2357 strarray_add( &ignore_files, ".gitignore" );
2358 strarray_add( &ignore_files, "Makefile" );
2359 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2360 strarray_addall( &ignore_files, targets );
2362 if (testlist_files.count)
2363 output_testlist( base_dir_path( make, "testlist.c" ), testlist_files );
2364 if (!make->src_dir && make->base_dir)
2365 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
2369 /*******************************************************************
2370 * update_makefile
2372 static void update_makefile( const char *path )
2374 static const char *source_vars[] =
2376 "C_SRCS",
2377 "OBJC_SRCS",
2378 "RC_SRCS",
2379 "MC_SRCS",
2380 "IDL_SRCS",
2381 "BISON_SRCS",
2382 "LEX_SRCS",
2383 "XTEMPLATE_SRCS",
2384 "SVG_SRCS",
2385 "FONT_SRCS",
2386 "IN_SRCS",
2387 "MANPAGES",
2388 NULL
2390 const char **var;
2391 unsigned int i;
2392 struct strarray value;
2393 struct incl_file *file;
2394 struct makefile *make;
2396 make = parse_makefile( path, Separator );
2398 if (root_src_dir)
2400 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
2401 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
2403 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
2404 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
2405 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
2407 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
2408 make->module = get_expanded_make_variable( make, "MODULE" );
2409 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
2410 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
2411 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
2413 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
2414 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
2415 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
2416 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
2418 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
2420 make->use_msvcrt = 0;
2421 for (i = 0; i < make->appmode.count && !make->use_msvcrt; i++)
2422 make->use_msvcrt = !strcmp( make->appmode.str[i], "-mno-cygwin" );
2423 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
2424 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 );
2426 make->is_win16 = 0;
2427 for (i = 0; i < make->extradllflags.count && !make->is_win16; i++)
2428 if (!strcmp( make->extradllflags.str[i], "-m16" )) make->is_win16 = 1;
2430 make->include_args = empty_strarray;
2431 make->define_args = empty_strarray;
2432 strarray_add( &make->define_args, "-D__WINESRC__" );
2434 value = get_expanded_make_var_array( make, "EXTRAINCL" );
2435 for (i = 0; i < value.count; i++)
2436 if (!strncmp( value.str[i], "-I", 2 ))
2437 strarray_add_uniq( &make->include_args, value.str[i] );
2438 else
2439 strarray_add_uniq( &make->define_args, value.str[i] );
2440 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
2442 list_init( &sources );
2443 list_init( &includes );
2445 for (var = source_vars; *var; var++)
2447 value = get_expanded_make_var_array( make, *var );
2448 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
2451 add_generated_sources( make );
2453 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
2454 for (i = 0; i < value.count; i++)
2456 /* default to .c for unknown extra object files */
2457 if (strendswith( value.str[i], ".o" ))
2458 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2459 else
2460 add_generated_source( make, value.str[i], NULL );
2463 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( make, file, 0 );
2465 output_file_name = base_dir_path( make, makefile_name );
2466 output_dependencies( make, output_file_name );
2467 output_file_name = NULL;
2471 /*******************************************************************
2472 * parse_makeflags
2474 static void parse_makeflags( const char *flags )
2476 const char *p = flags;
2477 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2479 while (*p)
2481 while (isspace(*p)) p++;
2482 var = buffer;
2483 while (*p && !isspace(*p))
2485 if (*p == '\\' && p[1]) p++;
2486 *var++ = *p++;
2488 *var = 0;
2489 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2494 /*******************************************************************
2495 * parse_option
2497 static int parse_option( const char *opt )
2499 if (opt[0] != '-')
2501 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2502 return 0;
2504 switch(opt[1])
2506 case 'f':
2507 if (opt[2]) makefile_name = opt + 2;
2508 break;
2509 case 'R':
2510 relative_dir_mode = 1;
2511 break;
2512 case 's':
2513 if (opt[2]) Separator = opt + 2;
2514 else Separator = NULL;
2515 break;
2516 default:
2517 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2518 exit(1);
2520 return 1;
2524 /*******************************************************************
2525 * main
2527 int main( int argc, char *argv[] )
2529 const char *makeflags = getenv( "MAKEFLAGS" );
2530 int i, j;
2532 if (makeflags) parse_makeflags( makeflags );
2534 i = 1;
2535 while (i < argc)
2537 if (parse_option( argv[i] ))
2539 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2540 argc--;
2542 else i++;
2545 if (relative_dir_mode)
2547 char *relpath;
2549 if (argc != 3)
2551 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2552 exit( 1 );
2554 relpath = get_relative_path( argv[1], argv[2] );
2555 printf( "%s\n", relpath ? relpath : "." );
2556 exit( 0 );
2559 if (argc <= 1)
2561 fprintf( stderr, "%s", Usage );
2562 exit( 1 );
2564 atexit( cleanup_files );
2565 signal( SIGTERM, exit_on_signal );
2566 signal( SIGINT, exit_on_signal );
2567 #ifdef SIGHUP
2568 signal( SIGHUP, exit_on_signal );
2569 #endif
2571 top_makefile = parse_makefile( NULL, "# End of common header" );
2573 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
2574 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
2575 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
2576 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
2577 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
2578 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
2579 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
2580 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
2582 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
2583 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
2584 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
2585 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
2586 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
2587 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2588 dll_prefix = get_expanded_make_variable( top_makefile, "DLLPREFIX" );
2589 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
2590 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
2591 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
2592 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
2593 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
2595 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
2596 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2597 if (!tools_ext) tools_ext = "";
2598 if (!dll_prefix) dll_prefix = "";
2599 if (!man_ext) man_ext = "3w";
2601 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2602 return 0;