makedep: Open files from the top-level directory as far as possible.
[wine.git] / tools / makedep.c
blob85683519fa12239cb2f7659d1949fb831937406d
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 includes = LIST_INIT(includes);
87 struct strarray
89 unsigned int count; /* strings in use */
90 unsigned int size; /* total allocated size */
91 const char **str;
94 static const struct strarray empty_strarray;
96 /* variables common to all makefiles */
97 static struct strarray linguas;
98 static struct strarray dll_flags;
99 static struct strarray target_flags;
100 static struct strarray msvcrt_flags;
101 static struct strarray extra_cflags;
102 static struct strarray cpp_flags;
103 static struct strarray unwind_flags;
104 static struct strarray libs;
105 static struct strarray cmdline_vars;
106 static const char *root_src_dir;
107 static const char *tools_dir;
108 static const char *tools_ext;
109 static const char *exe_ext;
110 static const char *dll_ext;
111 static const char *man_ext;
112 static const char *dll_prefix;
113 static const char *crosstarget;
114 static const char *fontforge;
115 static const char *convert;
116 static const char *rsvg;
117 static const char *icotool;
119 struct makefile
121 struct strarray vars;
122 struct strarray include_args;
123 struct strarray define_args;
124 struct strarray appmode;
125 struct strarray imports;
126 struct strarray delayimports;
127 struct strarray extradllflags;
128 struct list sources;
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_exists
348 static int strarray_exists( 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 1;
353 return 0;
357 /*******************************************************************
358 * strarray_add_uniq
360 static void strarray_add_uniq( struct strarray *array, const char *str )
362 if (!strarray_exists( array, str )) strarray_add( array, str );
366 /*******************************************************************
367 * strarray_get_value
369 * Find a value in a name/value pair string array.
371 static char *strarray_get_value( const struct strarray *array, const char *name )
373 unsigned int i;
375 for (i = 0; i < array->count; i += 2)
376 if (!strcmp( array->str[i], name )) return xstrdup( array->str[i + 1] );
377 return NULL;
381 /*******************************************************************
382 * strarray_set_value
384 * Define a value in a name/value pair string array.
386 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
388 unsigned int i;
390 /* redefining a variable replaces the previous value */
391 for (i = 0; i < array->count; i += 2)
393 if (strcmp( array->str[i], name )) continue;
394 array->str[i + 1] = value;
395 return;
397 strarray_add( array, name );
398 strarray_add( array, value );
402 /*******************************************************************
403 * output_filename
405 static void output_filename( const char *name )
407 if (output_column + strlen(name) + 1 > 100)
409 output( " \\\n" );
410 output( " " );
412 else if (output_column) output( " " );
413 output( "%s", name );
417 /*******************************************************************
418 * output_filenames
420 static void output_filenames( struct strarray array )
422 unsigned int i;
424 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
428 /*******************************************************************
429 * get_extension
431 static char *get_extension( char *filename )
433 char *ext = strrchr( filename, '.' );
434 if (ext && strchr( ext, '/' )) ext = NULL;
435 return ext;
439 /*******************************************************************
440 * replace_extension
442 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
444 char *ret;
445 int name_len = strlen( name );
446 int ext_len = strlen( old_ext );
448 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
449 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
450 memcpy( ret, name, name_len );
451 strcpy( ret + name_len, new_ext );
452 return ret;
456 /*******************************************************************
457 * strarray_replace_extension
459 static struct strarray strarray_replace_extension( const struct strarray *array,
460 const char *old_ext, const char *new_ext )
462 unsigned int i;
463 struct strarray ret;
465 ret.count = ret.size = array->count;
466 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
467 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
468 return ret;
472 /*******************************************************************
473 * replace_substr
475 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
477 unsigned int pos = start - str;
478 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
479 memcpy( ret, str, pos );
480 strcpy( ret + pos, replace );
481 strcat( ret + pos, start + len );
482 return ret;
486 /*******************************************************************
487 * get_relative_path
489 * Determine where the destination path is located relative to the 'from' path.
491 static char *get_relative_path( const char *from, const char *dest )
493 const char *start;
494 char *ret, *p;
495 unsigned int dotdots = 0;
497 /* a path of "." is equivalent to an empty path */
498 if (!strcmp( from, "." )) from = "";
500 for (;;)
502 while (*from == '/') from++;
503 while (*dest == '/') dest++;
504 start = dest; /* save start of next path element */
505 if (!*from) break;
507 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
508 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
510 /* count remaining elements in 'from' */
513 dotdots++;
514 while (*from && *from != '/') from++;
515 while (*from == '/') from++;
517 while (*from);
518 break;
521 if (!start[0] && !dotdots) return NULL; /* empty path */
523 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
524 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
526 if (start[0]) strcpy( p, start );
527 else p[-1] = 0; /* remove trailing slash */
528 return ret;
532 /*******************************************************************
533 * concat_paths
535 static char *concat_paths( const char *base, const char *path )
537 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
538 if (!path || !path[0]) return xstrdup( base );
539 if (path[0] == '/') return xstrdup( path );
540 return strmake( "%s/%s", base, path );
544 /*******************************************************************
545 * base_dir_path
547 static char *base_dir_path( struct makefile *make, const char *path )
549 return concat_paths( make->base_dir, path );
553 /*******************************************************************
554 * obj_dir_path
556 static char *obj_dir_path( struct makefile *make, const char *path )
558 return concat_paths( make->obj_dir, path );
562 /*******************************************************************
563 * src_dir_path
565 static char *src_dir_path( struct makefile *make, const char *path )
567 if (make->src_dir) return concat_paths( make->src_dir, path );
568 return obj_dir_path( make, path );
572 /*******************************************************************
573 * top_obj_dir_path
575 static char *top_obj_dir_path( struct makefile *make, const char *path )
577 return concat_paths( make->top_obj_dir, path );
581 /*******************************************************************
582 * top_dir_path
584 static char *top_dir_path( struct makefile *make, const char *path )
586 if (make->top_src_dir) return concat_paths( make->top_src_dir, path );
587 return top_obj_dir_path( make, path );
591 /*******************************************************************
592 * root_dir_path
594 static char *root_dir_path( const char *path )
596 return concat_paths( root_src_dir, path );
600 /*******************************************************************
601 * tools_dir_path
603 static char *tools_dir_path( struct makefile *make, const char *path )
605 if (tools_dir) return top_obj_dir_path( make, strmake( "%s/tools/%s", tools_dir, path ));
606 return top_obj_dir_path( make, strmake( "tools/%s", path ));
610 /*******************************************************************
611 * tools_path
613 static char *tools_path( struct makefile *make, const char *name )
615 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
619 /*******************************************************************
620 * get_line
622 static char *get_line( FILE *file )
624 static char *buffer;
625 static unsigned int size;
627 if (!size)
629 size = 1024;
630 buffer = xmalloc( size );
632 if (!fgets( buffer, size, file )) return NULL;
633 input_line++;
635 for (;;)
637 char *p = buffer + strlen(buffer);
638 /* if line is larger than buffer, resize buffer */
639 while (p == buffer + size - 1 && p[-1] != '\n')
641 buffer = xrealloc( buffer, size * 2 );
642 if (!fgets( buffer + size - 1, size + 1, file )) break;
643 p = buffer + strlen(buffer);
644 size *= 2;
646 if (p > buffer && p[-1] == '\n')
648 *(--p) = 0;
649 if (p > buffer && p[-1] == '\r') *(--p) = 0;
650 if (p > buffer && p[-1] == '\\')
652 *(--p) = 0;
653 /* line ends in backslash, read continuation line */
654 if (!fgets( p, size - (p - buffer), file )) return buffer;
655 input_line++;
656 continue;
659 return buffer;
663 /*******************************************************************
664 * find_src_file
666 static struct incl_file *find_src_file( struct makefile *make, const char *name )
668 struct incl_file *file;
670 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
671 if (!strcmp( name, file->name )) return file;
672 return NULL;
675 /*******************************************************************
676 * find_include_file
678 static struct incl_file *find_include_file( const char *name )
680 struct incl_file *file;
682 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
683 if (!strcmp( name, file->name )) return file;
684 return NULL;
687 /*******************************************************************
688 * add_include
690 * Add an include file if it doesn't already exists.
692 static struct incl_file *add_include( struct incl_file *parent, const char *name, int system )
694 struct incl_file *include;
695 char *ext;
697 if (parent->files_count >= parent->files_size)
699 parent->files_size *= 2;
700 if (parent->files_size < 16) parent->files_size = 16;
701 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
704 /* enforce some rules for the Wine tree */
706 if (!memcmp( name, "../", 3 ))
707 fatal_error( "#include directive with relative path not allowed\n" );
709 if (!strcmp( name, "config.h" ))
711 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
712 fatal_error( "config.h must not be included by a header file\n" );
713 if (parent->files_count)
714 fatal_error( "config.h must be included before anything else\n" );
716 else if (!strcmp( name, "wine/port.h" ))
718 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
719 fatal_error( "wine/port.h must not be included by a header file\n" );
720 if (!parent->files_count) fatal_error( "config.h must be included before wine/port.h\n" );
721 if (parent->files_count > 1)
722 fatal_error( "wine/port.h must be included before everything except config.h\n" );
723 if (strcmp( parent->files[0]->name, "config.h" ))
724 fatal_error( "config.h must be included before wine/port.h\n" );
727 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
728 if (!strcmp( name, include->name )) goto found;
730 include = xmalloc( sizeof(*include) );
731 memset( include, 0, sizeof(*include) );
732 include->name = xstrdup(name);
733 include->included_by = parent;
734 include->included_line = input_line;
735 if (system) include->flags |= FLAG_SYSTEM;
736 list_add_tail( &includes, &include->entry );
737 found:
738 parent->files[parent->files_count++] = include;
739 return include;
743 /*******************************************************************
744 * add_generated_source
746 * Add a generated source file to the list.
748 static struct incl_file *add_generated_source( struct makefile *make,
749 const char *name, const char *filename )
751 struct incl_file *file;
753 if ((file = find_src_file( make, name ))) return file; /* we already have it */
754 file = xmalloc( sizeof(*file) );
755 memset( file, 0, sizeof(*file) );
756 file->name = xstrdup( name );
757 file->filename = obj_dir_path( make, filename ? filename : name );
758 file->flags = FLAG_GENERATED;
759 list_add_tail( &make->sources, &file->entry );
760 return file;
764 /*******************************************************************
765 * open_file
767 static FILE *open_file( struct makefile *make, const char *path, char **filename )
769 FILE *ret = fopen( base_dir_path( make, path ), "r" );
771 if (ret) *filename = xstrdup( path );
772 return ret;
776 /*******************************************************************
777 * open_local_file
779 * Open a file in the source directory of the makefile.
781 static FILE *open_local_file( struct makefile *make, const char *path, char **filename )
783 char *src_path = root_dir_path( base_dir_path( make, path ));
784 FILE *ret = fopen( src_path, "r" );
786 /* if not found, try parent dir */
787 if (!ret && make->parent_dir)
789 free( src_path );
790 path = strmake( "%s/%s", make->parent_dir, path );
791 src_path = root_dir_path( base_dir_path( make, path ));
792 ret = fopen( src_path, "r" );
795 if (ret) *filename = src_dir_path( make, path );
796 free( src_path );
797 return ret;
801 /*******************************************************************
802 * open_global_file
804 * Open a file in the top-level source directory.
806 static FILE *open_global_file( struct makefile *make, const char *path, char **filename )
808 char *src_path = root_dir_path( path );
809 FILE *ret = fopen( src_path, "r" );
811 if (ret) *filename = top_dir_path( make, path );
812 free( src_path );
813 return ret;
817 /*******************************************************************
818 * open_global_header
820 * Open a file in the global include source directory.
822 static FILE *open_global_header( struct makefile *make, const char *path, char **filename )
824 return open_global_file( make, strmake( "include/%s", path ), filename );
828 /*******************************************************************
829 * open_src_file
831 static FILE *open_src_file( struct makefile *make, struct incl_file *pFile )
833 FILE *file = open_local_file( make, pFile->name, &pFile->filename );
835 if (!file) fatal_perror( "open %s", pFile->name );
836 return file;
840 /*******************************************************************
841 * open_include_file
843 static FILE *open_include_file( struct makefile *make, struct incl_file *pFile )
845 FILE *file = NULL;
846 char *filename, *p;
847 unsigned int i, len;
849 errno = ENOENT;
851 /* check for generated bison header */
853 if (strendswith( pFile->name, ".tab.h" ) &&
854 (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
856 pFile->sourcename = filename;
857 pFile->filename = obj_dir_path( make, pFile->name );
858 /* don't bother to parse it */
859 fclose( file );
860 return NULL;
863 /* check for corresponding idl file in source dir */
865 if (strendswith( pFile->name, ".h" ) &&
866 (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
868 pFile->sourcename = filename;
869 pFile->filename = obj_dir_path( make, pFile->name );
870 return file;
873 /* now try in source dir */
874 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
876 /* check for corresponding idl file in global includes */
878 if (strendswith( pFile->name, ".h" ) &&
879 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
881 pFile->sourcename = filename;
882 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
883 return file;
886 /* check for corresponding .in file in global includes (for config.h.in) */
888 if (strendswith( pFile->name, ".h" ) &&
889 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
891 pFile->sourcename = filename;
892 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
893 return file;
896 /* check for corresponding .x file in global includes */
898 if (strendswith( pFile->name, "tmpl.h" ) &&
899 (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
901 pFile->sourcename = filename;
902 pFile->filename = top_obj_dir_path( make, strmake( "include/%s", pFile->name ));
903 return file;
906 /* check in global includes source dir */
908 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
910 /* check in global msvcrt includes */
911 if (make->use_msvcrt &&
912 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
913 return file;
915 /* now search in include paths */
916 for (i = 0; i < make->include_args.count; i++)
918 const char *dir = make->include_args.str[i] + 2; /* skip -I */
919 const char *prefix = make->top_src_dir ? make->top_src_dir : make->top_obj_dir;
921 if (prefix)
923 len = strlen( prefix );
924 if (!strncmp( dir, prefix, len ) && (!dir[len] || dir[len] == '/'))
926 while (dir[len] == '/') len++;
927 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
928 if (file) return file;
930 if (make->top_src_dir) continue; /* ignore paths that don't point to the top source dir */
932 if (*dir != '/')
934 if ((file = open_file( make, concat_paths( dir, pFile->name ), &pFile->filename )))
935 return file;
938 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
940 /* try in src file directory */
941 if ((p = strrchr(pFile->included_by->filename, '/')))
943 int l = p - pFile->included_by->filename + 1;
944 filename = xmalloc(l + strlen(pFile->name) + 1);
945 memcpy( filename, pFile->included_by->filename, l );
946 strcpy( filename + l, pFile->name );
947 if ((file = open_file( make, filename, &pFile->filename ))) return file;
948 free( filename );
951 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
952 perror( pFile->name );
953 pFile = pFile->included_by;
954 while (pFile && pFile->included_by)
956 const char *parent = pFile->included_by->sourcename;
957 if (!parent) parent = pFile->included_by->name;
958 fprintf( stderr, "%s:%d: note: %s was first included here\n",
959 parent, pFile->included_line, pFile->name );
960 pFile = pFile->included_by;
962 exit(1);
966 /*******************************************************************
967 * parse_include_directive
969 static void parse_include_directive( struct incl_file *source, char *str )
971 char quote, *include, *p = str;
973 while (*p && isspace(*p)) p++;
974 if (*p != '\"' && *p != '<' ) return;
975 quote = *p++;
976 if (quote == '<') quote = '>';
977 include = p;
978 while (*p && (*p != quote)) p++;
979 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
980 *p = 0;
981 add_include( source, include, (quote == '>') );
985 /*******************************************************************
986 * parse_pragma_directive
988 static void parse_pragma_directive( struct incl_file *source, char *str )
990 char *flag, *p = str;
992 if (!isspace( *p )) return;
993 while (*p && isspace(*p)) p++;
994 p = strtok( p, " \t" );
995 if (strcmp( p, "makedep" )) return;
997 while ((flag = strtok( NULL, " \t" )))
999 if (!strcmp( flag, "depend" ))
1001 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
1002 return;
1004 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
1006 if (strendswith( source->name, ".idl" ))
1008 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
1009 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
1010 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
1011 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
1012 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
1013 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
1014 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
1015 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
1017 else if (strendswith( source->name, ".rc" ))
1019 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
1021 else if (strendswith( source->name, ".sfd" ))
1023 if (!strcmp( flag, "font" ))
1025 struct strarray *array = source->args;
1027 if (!array)
1029 source->args = array = xmalloc( sizeof(*array) );
1030 *array = empty_strarray;
1031 source->flags |= FLAG_SFD_FONTS;
1033 strarray_add( array, xstrdup( strtok( NULL, "" )));
1034 return;
1037 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
1042 /*******************************************************************
1043 * parse_cpp_directive
1045 static void parse_cpp_directive( struct incl_file *source, char *str )
1047 while (*str && isspace(*str)) str++;
1048 if (*str++ != '#') return;
1049 while (*str && isspace(*str)) str++;
1051 if (!strncmp( str, "include", 7 ))
1052 parse_include_directive( source, str + 7 );
1053 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
1054 parse_include_directive( source, str + 6 );
1055 else if (!strncmp( str, "pragma", 6 ))
1056 parse_pragma_directive( source, str + 6 );
1060 /*******************************************************************
1061 * parse_idl_file
1063 * If for_h_file is non-zero, it means we are not interested in the idl file
1064 * itself, but only in the contents of the .h file that will be generated from it.
1066 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
1068 char *buffer, *include;
1070 input_line = 0;
1071 if (for_h_file)
1073 /* generated .h file always includes these */
1074 add_include( pFile, "rpc.h", 1 );
1075 add_include( pFile, "rpcndr.h", 1 );
1078 while ((buffer = get_line( file )))
1080 char quote;
1081 char *p = buffer;
1082 while (*p && isspace(*p)) p++;
1084 if (!strncmp( p, "import", 6 ))
1086 p += 6;
1087 while (*p && isspace(*p)) p++;
1088 if (*p != '"') continue;
1089 include = ++p;
1090 while (*p && (*p != '"')) p++;
1091 if (!*p) fatal_error( "malformed import directive\n" );
1092 *p = 0;
1093 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
1094 add_include( pFile, include, 0 );
1095 continue;
1098 if (for_h_file) /* only check for #include inside cpp_quote */
1100 if (strncmp( p, "cpp_quote", 9 )) continue;
1101 p += 9;
1102 while (*p && isspace(*p)) p++;
1103 if (*p++ != '(') continue;
1104 while (*p && isspace(*p)) p++;
1105 if (*p++ != '"') continue;
1106 if (*p++ != '#') continue;
1107 while (*p && isspace(*p)) p++;
1108 if (strncmp( p, "include", 7 )) continue;
1109 p += 7;
1110 while (*p && isspace(*p)) p++;
1111 if (*p == '\\' && p[1] == '"')
1113 p += 2;
1114 quote = '"';
1116 else
1118 if (*p++ != '<' ) continue;
1119 quote = '>';
1121 include = p;
1122 while (*p && (*p != quote)) p++;
1123 if (!*p || (quote == '"' && p[-1] != '\\'))
1124 fatal_error( "malformed #include directive inside cpp_quote\n" );
1125 if (quote == '"') p--; /* remove backslash */
1126 *p = 0;
1127 add_include( pFile, include, (quote == '>') );
1128 continue;
1131 parse_cpp_directive( pFile, p );
1135 /*******************************************************************
1136 * parse_c_file
1138 static void parse_c_file( struct incl_file *pFile, FILE *file )
1140 char *buffer;
1142 input_line = 0;
1143 while ((buffer = get_line( file )))
1145 parse_cpp_directive( pFile, buffer );
1150 /*******************************************************************
1151 * parse_rc_file
1153 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1155 char *buffer, *include;
1157 input_line = 0;
1158 while ((buffer = get_line( file )))
1160 char quote;
1161 char *p = buffer;
1162 while (*p && isspace(*p)) p++;
1164 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1166 p += 2;
1167 while (*p && isspace(*p)) p++;
1168 if (strncmp( p, "@makedep:", 9 )) continue;
1169 p += 9;
1170 while (*p && isspace(*p)) p++;
1171 quote = '"';
1172 if (*p == quote)
1174 include = ++p;
1175 while (*p && *p != quote) p++;
1177 else
1179 include = p;
1180 while (*p && !isspace(*p) && *p != '*') p++;
1182 if (!*p)
1183 fatal_error( "malformed makedep comment\n" );
1184 *p = 0;
1185 add_include( pFile, include, (quote == '>') );
1186 continue;
1189 parse_cpp_directive( pFile, buffer );
1194 /*******************************************************************
1195 * parse_in_file
1197 static void parse_in_file( struct incl_file *source, FILE *file )
1199 char *p, *buffer;
1201 /* make sure it gets rebuilt when the version changes */
1202 add_include( source, "config.h", 1 );
1204 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1206 input_line = 0;
1207 while ((buffer = get_line( file )))
1209 if (strncmp( buffer, ".TH", 3 )) continue;
1210 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1211 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1212 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1213 source->args = xstrdup( p );
1214 return;
1219 /*******************************************************************
1220 * parse_sfd_file
1222 static void parse_sfd_file( struct incl_file *source, FILE *file )
1224 char *p, *eol, *buffer;
1226 input_line = 0;
1227 while ((buffer = get_line( file )))
1229 if (strncmp( buffer, "UComments:", 10 )) continue;
1230 p = buffer + 10;
1231 while (*p == ' ') p++;
1232 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1234 p++;
1235 buffer[strlen(buffer) - 1] = 0;
1237 while ((eol = strstr( p, "+AAoA" )))
1239 *eol = 0;
1240 while (*p && isspace(*p)) p++;
1241 if (*p++ == '#')
1243 while (*p && isspace(*p)) p++;
1244 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1246 p = eol + 5;
1248 while (*p && isspace(*p)) p++;
1249 if (*p++ != '#') return;
1250 while (*p && isspace(*p)) p++;
1251 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1252 return;
1257 /*******************************************************************
1258 * parse_file
1260 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1262 FILE *file;
1264 /* don't try to open certain types of files */
1265 if (strendswith( source->name, ".tlb" ))
1267 source->filename = obj_dir_path( make, source->name );
1268 return;
1271 file = src ? open_src_file( make, source ) : open_include_file( make, source );
1272 if (!file) return;
1273 input_file_name = source->filename;
1275 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1276 parse_idl_file( source, file, 1 );
1277 else if (strendswith( source->filename, ".idl" ))
1278 parse_idl_file( source, file, 0 );
1279 else if (strendswith( source->filename, ".c" ) ||
1280 strendswith( source->filename, ".m" ) ||
1281 strendswith( source->filename, ".h" ) ||
1282 strendswith( source->filename, ".l" ) ||
1283 strendswith( source->filename, ".y" ))
1284 parse_c_file( source, file );
1285 else if (strendswith( source->filename, ".rc" ))
1286 parse_rc_file( source, file );
1287 else if (strendswith( source->filename, ".in" ))
1288 parse_in_file( source, file );
1289 else if (strendswith( source->filename, ".sfd" ))
1290 parse_sfd_file( source, file );
1291 fclose(file);
1292 input_file_name = NULL;
1296 /*******************************************************************
1297 * add_src_file
1299 * Add a source file to the list.
1301 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1303 struct incl_file *file;
1305 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1306 file = xmalloc( sizeof(*file) );
1307 memset( file, 0, sizeof(*file) );
1308 file->name = xstrdup(name);
1309 list_add_tail( &make->sources, &file->entry );
1310 parse_file( make, file, 1 );
1311 return file;
1315 /*******************************************************************
1316 * get_make_variable
1318 static char *get_make_variable( struct makefile *make, const char *name )
1320 char *ret;
1322 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1323 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1324 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1325 return NULL;
1329 /*******************************************************************
1330 * get_expanded_make_variable
1332 static char *get_expanded_make_variable( struct makefile *make, const char *name )
1334 char *p, *end, *var, *expand, *tmp;
1336 expand = get_make_variable( make, name );
1337 if (!expand) return NULL;
1339 p = expand;
1340 while ((p = strchr( p, '$' )))
1342 if (p[1] == '(')
1344 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1345 *end++ = 0;
1346 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1347 var = get_make_variable( make, p + 2 );
1348 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1349 free( var );
1351 else if (p[1] == '{') /* don't expand ${} variables */
1353 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1354 p = end + 1;
1355 continue;
1357 else if (p[1] == '$')
1359 tmp = replace_substr( expand, p, 2, "$" );
1361 else fatal_error( "syntax error in '%s'\n", expand );
1363 /* switch to the new string */
1364 p = tmp + (p - expand);
1365 free( expand );
1366 expand = tmp;
1369 /* consider empty variables undefined */
1370 p = expand;
1371 while (*p && isspace(*p)) p++;
1372 if (*p) return expand;
1373 free( expand );
1374 return NULL;
1378 /*******************************************************************
1379 * get_expanded_make_var_array
1381 static struct strarray get_expanded_make_var_array( struct makefile *make, const char *name )
1383 struct strarray ret = empty_strarray;
1384 char *value, *token;
1386 if ((value = get_expanded_make_variable( make, name )))
1387 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1388 strarray_add( &ret, token );
1389 return ret;
1393 /*******************************************************************
1394 * set_make_variable
1396 static int set_make_variable( struct strarray *array, const char *assignment )
1398 char *p, *name;
1400 p = name = xstrdup( assignment );
1401 while (isalnum(*p) || *p == '_') p++;
1402 if (name == p) return 0; /* not a variable */
1403 if (isspace(*p))
1405 *p++ = 0;
1406 while (isspace(*p)) p++;
1408 if (*p != '=') return 0; /* not an assignment */
1409 *p++ = 0;
1410 while (isspace(*p)) p++;
1412 strarray_set_value( array, name, p );
1413 return 1;
1417 /*******************************************************************
1418 * parse_makefile
1420 static struct makefile *parse_makefile( const char *path, const char *separator )
1422 char *buffer;
1423 FILE *file;
1424 struct makefile *make = xmalloc( sizeof(*make) );
1426 memset( make, 0, sizeof(*make) );
1427 if (path)
1429 make->top_obj_dir = get_relative_path( path, "" );
1430 make->base_dir = path;
1431 if (!strcmp( make->base_dir, "." )) make->base_dir = NULL;
1434 input_file_name = base_dir_path( make, makefile_name );
1435 if (!(file = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1437 input_line = 0;
1438 while ((buffer = get_line( file )))
1440 if (separator && !strncmp( buffer, separator, strlen(separator) )) break;
1441 if (*buffer == '\t') continue; /* command */
1442 while (isspace( *buffer )) buffer++;
1443 if (*buffer == '#') continue; /* comment */
1444 set_make_variable( &make->vars, buffer );
1446 fclose( file );
1447 input_file_name = NULL;
1448 return make;
1452 /*******************************************************************
1453 * add_generated_sources
1455 static void add_generated_sources( struct makefile *make )
1457 struct incl_file *source, *next, *file;
1459 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1461 if (source->flags & FLAG_IDL_CLIENT)
1463 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1464 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1466 if (source->flags & FLAG_IDL_SERVER)
1468 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1469 add_include( file, "wine/exception.h", 0 );
1470 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1472 if (source->flags & FLAG_IDL_IDENT)
1474 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1475 add_include( file, "rpc.h", 0 );
1476 add_include( file, "rpcndr.h", 0 );
1477 add_include( file, "guiddef.h", 0 );
1479 if (source->flags & FLAG_IDL_PROXY)
1481 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1482 add_include( file, "objbase.h", 0 );
1483 add_include( file, "rpcproxy.h", 0 );
1484 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1485 add_include( file, "objbase.h", 0 );
1486 add_include( file, "rpcproxy.h", 0 );
1487 add_include( file, "wine/exception.h", 0 );
1488 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1490 if (source->flags & FLAG_IDL_REGTYPELIB)
1492 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1494 if (source->flags & FLAG_IDL_REGISTER)
1496 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1498 if (strendswith( source->name, ".y" ))
1500 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1501 /* steal the includes list from the source file */
1502 file->files_count = source->files_count;
1503 file->files_size = source->files_size;
1504 file->files = source->files;
1505 source->files_count = source->files_size = 0;
1506 source->files = NULL;
1508 if (strendswith( source->name, ".l" ))
1510 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1511 /* steal the includes list from the source file */
1512 file->files_count = source->files_count;
1513 file->files_size = source->files_size;
1514 file->files = source->files;
1515 source->files_count = source->files_size = 0;
1516 source->files = NULL;
1519 if (get_make_variable( make, "TESTDLL" ))
1521 file = add_generated_source( make, "testlist.o", "testlist.c" );
1522 add_include( file, "wine/test.h", 0 );
1527 /*******************************************************************
1528 * create_dir
1530 static void create_dir( const char *dir )
1532 char *p, *path;
1534 p = path = xstrdup( dir );
1535 while ((p = strchr( p, '/' )))
1537 *p = 0;
1538 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1539 *p++ = '/';
1540 while (*p == '/') p++;
1542 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1543 free( path );
1547 /*******************************************************************
1548 * output_filenames_obj_dir
1550 static void output_filenames_obj_dir( struct makefile *make, struct strarray array )
1552 unsigned int i;
1554 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1558 /*******************************************************************
1559 * output_include
1561 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1563 int i;
1565 if (pFile->owner == owner) return;
1566 if (!pFile->filename) return;
1567 pFile->owner = owner;
1568 output_filename( pFile->filename );
1569 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1573 /*******************************************************************
1574 * output_sources
1576 static struct strarray output_sources( struct makefile *make, struct strarray *testlist_files )
1578 struct incl_file *source;
1579 unsigned int i;
1580 struct strarray object_files = empty_strarray;
1581 struct strarray crossobj_files = empty_strarray;
1582 struct strarray res_files = empty_strarray;
1583 struct strarray clean_files = empty_strarray;
1584 struct strarray po_files = empty_strarray;
1585 struct strarray mo_files = empty_strarray;
1586 struct strarray mc_files = empty_strarray;
1587 struct strarray ok_files = empty_strarray;
1588 struct strarray dlldata_files = empty_strarray;
1589 struct strarray c2man_files = empty_strarray;
1590 struct strarray implib_objs = empty_strarray;
1591 struct strarray includes = empty_strarray;
1592 struct strarray subdirs = empty_strarray;
1593 struct strarray phony_targets = empty_strarray;
1594 struct strarray all_targets = get_expanded_make_var_array( make, "PROGRAMS" );
1596 for (i = 0; i < linguas.count; i++)
1597 strarray_add( &mo_files, strmake( "%s/%s.mo", top_obj_dir_path( make, "po" ), linguas.str[i] ));
1599 strarray_add( &includes, strmake( "-I%s", obj_dir_path( make, "" )));
1600 if (make->src_dir) strarray_add( &includes, strmake( "-I%s", make->src_dir ));
1601 if (make->parent_dir) strarray_add( &includes, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
1602 if (make->top_obj_dir) strarray_add( &includes, strmake( "-I%s", top_obj_dir_path( make, "include" )));
1603 if (make->top_src_dir) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include" )));
1604 if (make->use_msvcrt) strarray_add( &includes, strmake( "-I%s", top_dir_path( make, "include/msvcrt" )));
1605 strarray_addall( &includes, make->include_args );
1607 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
1609 struct strarray extradefs;
1610 char *obj = xstrdup( source->name );
1611 char *ext = get_extension( obj );
1613 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1614 *ext++ = 0;
1616 if (make->src_dir && strchr( obj, '/' ))
1618 char *subdir = base_dir_path( make, obj );
1619 *strrchr( subdir, '/' ) = 0;
1620 strarray_add_uniq( &subdirs, subdir );
1623 extradefs = get_expanded_make_var_array( make, strmake( "%s_EXTRADEFS", obj ));
1625 if (!strcmp( ext, "y" )) /* yacc file */
1627 /* add source file dependency for parallel makes */
1628 char *header = strmake( "%s.tab.h", obj );
1630 if (find_include_file( header ))
1632 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
1633 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1634 obj, obj_dir_path( make, obj ), source->filename );
1635 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
1636 source->filename, obj_dir_path( make, header ));
1637 strarray_add( &clean_files, header );
1639 else output( "%s.tab.c: %s\n", obj, source->filename );
1641 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj, source->filename );
1642 continue; /* no dependencies */
1644 else if (!strcmp( ext, "x" )) /* template file */
1646 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
1647 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1648 output( "\t%s%s -H -o $@ %s\n",
1649 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
1650 strarray_add( &clean_files, strmake( "%s.h", obj ));
1651 continue; /* no dependencies */
1653 else if (!strcmp( ext, "l" )) /* lex file */
1655 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
1656 output( "\t$(FLEX) -o$@ %s\n", source->filename );
1657 continue; /* no dependencies */
1659 else if (!strcmp( ext, "rc" )) /* resource file */
1661 strarray_add( &res_files, strmake( "%s.res", obj ));
1662 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1663 tools_path( make, "wrc" ), source->filename );
1664 output( "\t%s -o $@ %s", tools_path( make, "wrc" ), source->filename );
1665 if (make->is_win16) output_filename( "-m16" );
1666 else output_filenames( target_flags );
1667 output_filename( "--nostdinc" );
1668 output_filenames( includes );
1669 output_filenames( make->define_args );
1670 output_filenames( extradefs );
1671 if (mo_files.count && (source->flags & FLAG_RC_PO))
1673 strarray_add( &po_files, source->filename );
1674 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1675 output( "\n" );
1676 output( "%s.res:", obj_dir_path( make, obj ));
1677 output_filenames( mo_files );
1678 output( "\n" );
1679 output( "%s ", obj_dir_path( make, "rsrc.pot" ));
1681 else output( "\n" );
1682 output( "%s.res:", obj_dir_path( make, obj ));
1684 else if (!strcmp( ext, "mc" )) /* message file */
1686 strarray_add( &res_files, strmake( "%s.res", obj ));
1687 output( "%s.res: %s %s\n", obj_dir_path( make, obj ),
1688 tools_path( make, "wmc" ), source->filename );
1689 output( "\t%s -U -O res -o $@ %s", tools_path( make, "wmc" ), source->filename );
1690 if (mo_files.count)
1692 strarray_add( &mc_files, source->filename );
1693 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( make, "po" )));
1694 output( "\n" );
1695 output( "%s.res:", obj_dir_path( make, obj ));
1696 output_filenames( mo_files );
1697 output( "\n" );
1698 output( "%s ", obj_dir_path( make, "msg.pot" ));
1700 else output( "\n" );
1701 output( "%s.res:", obj_dir_path( make, obj ));
1703 else if (!strcmp( ext, "idl" )) /* IDL file */
1705 struct strarray targets = empty_strarray;
1706 char *dest;
1708 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1709 source->flags |= FLAG_IDL_HEADER;
1711 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1713 if (!(source->flags & idl_outputs[i].flag)) continue;
1714 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1715 if (!find_src_file( make, dest )) strarray_add( &clean_files, dest );
1716 strarray_add( &targets, dest );
1718 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1719 output_filenames_obj_dir( make, targets );
1720 output( ": %s\n", tools_path( make, "widl" ));
1721 output( "\t%s -o $@ %s", tools_path( make, "widl" ), source->filename );
1722 output_filenames( target_flags );
1723 output_filenames( includes );
1724 output_filenames( make->define_args );
1725 output_filenames( extradefs );
1726 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
1727 output( "\n" );
1728 output_filenames_obj_dir( make, targets );
1729 output( ": %s", source->filename );
1731 else if (!strcmp( ext, "in" )) /* .in file or man page */
1733 if (strendswith( obj, ".man" ) && source->args)
1735 char *dir, *dest = replace_extension( obj, ".man", "" );
1736 char *lang = strchr( dest, '.' );
1737 char *section = source->args;
1738 if (lang)
1740 *lang++ = 0;
1741 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, section );
1743 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", section );
1744 output( "install-man-pages:: %s\n", obj_dir_path( make, obj ));
1745 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj_dir_path( make, obj ), dir, dest, section );
1746 output( "uninstall::\n" );
1747 output( "\t$(RM) %s/%s.%s\n", dir, dest, section );
1748 free( dest );
1749 free( dir );
1750 strarray_add( &all_targets, xstrdup(obj) );
1751 strarray_add_uniq( &phony_targets, "install-man-pages" );
1752 strarray_add_uniq( &phony_targets, "uninstall" );
1754 else strarray_add( &clean_files, xstrdup(obj) );
1755 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
1756 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1757 output( "%s:", obj_dir_path( make, obj ));
1759 else if (!strcmp( ext, "sfd" )) /* font file */
1761 char *ttf_file = src_dir_path( make, strmake( "%s.ttf", obj ));
1762 if (fontforge && !make->src_dir)
1764 output( "%s: %s\n", ttf_file, source->filename );
1765 output( "\t%s -script %s %s $@\n",
1766 fontforge, top_dir_path( make, "fonts/genttf.ff" ), source->filename );
1767 if (!(source->flags & FLAG_SFD_FONTS)) output( "all: %s\n", ttf_file );
1769 if (source->flags & FLAG_INSTALL)
1771 output( "install install-lib::\n" );
1772 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file, obj );
1773 output( "uninstall::\n" );
1774 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj );
1776 if (source->flags & FLAG_SFD_FONTS)
1778 struct strarray *array = source->args;
1780 for (i = 0; i < array->count; i++)
1782 char *font = strtok( xstrdup(array->str[i]), " \t" );
1783 char *args = strtok( NULL, "" );
1785 strarray_add( &all_targets, xstrdup( font ));
1786 output( "%s: %s %s\n", obj_dir_path( make, font ),
1787 tools_path( make, "sfnt2fon" ), ttf_file );
1788 output( "\t%s -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
1789 output( "install install-lib:: %s\n", font );
1790 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n",
1791 obj_dir_path( make, font ), font );
1792 output( "uninstall::\n" );
1793 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font );
1796 if (source->flags & (FLAG_INSTALL | FLAG_SFD_FONTS))
1798 strarray_add_uniq( &phony_targets, "install" );
1799 strarray_add_uniq( &phony_targets, "install-lib" );
1800 strarray_add_uniq( &phony_targets, "uninstall" );
1802 continue; /* no dependencies */
1804 else if (!strcmp( ext, "svg" )) /* svg file */
1806 if (convert && rsvg && icotool && !make->src_dir)
1808 output( "%s.ico %s.bmp: %s\n",
1809 src_dir_path( make, obj ), src_dir_path( make, obj ), source->filename );
1810 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
1811 top_dir_path( make, "tools/buildimage" ), source->filename );
1813 continue; /* no dependencies */
1815 else if (!strcmp( ext, "res" ))
1817 strarray_add( &res_files, source->name );
1818 continue; /* no dependencies */
1820 else
1822 int need_cross = make->testdll ||
1823 (source->flags & FLAG_C_IMPLIB) ||
1824 (make->module && make->staticlib);
1826 if ((source->flags & FLAG_GENERATED) &&
1827 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
1828 strarray_add( &clean_files, source->filename );
1829 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1830 strarray_add( &object_files, strmake( "%s.o", obj ));
1831 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
1832 output( "\t$(CC) -c -o $@ %s", source->filename );
1833 output_filenames( includes );
1834 output_filenames( make->define_args );
1835 output_filenames( extradefs );
1836 if (make->module || make->staticlib || make->testdll)
1838 output_filenames( dll_flags );
1839 if (make->use_msvcrt) output_filenames( msvcrt_flags );
1841 output_filenames( extra_cflags );
1842 output_filenames( cpp_flags );
1843 output_filename( "$(CFLAGS)" );
1844 output( "\n" );
1845 if (crosstarget && need_cross)
1847 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1848 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
1849 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1850 output_filenames( includes );
1851 output_filenames( make->define_args );
1852 output_filenames( extradefs );
1853 output_filename( "-DWINE_CROSSTEST" );
1854 output_filenames( cpp_flags );
1855 output_filename( "$(CFLAGS)" );
1856 output( "\n" );
1858 if (make->testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1860 strarray_add( &ok_files, strmake( "%s.ok", obj ));
1861 output( "%s.ok:\n", obj_dir_path( make, obj ));
1862 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1863 top_dir_path( make, "tools/runtest" ), top_obj_dir_path( make, "" ), make->testdll,
1864 replace_extension( make->testdll, ".dll", "_test.exe" ), dll_ext, obj );
1866 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1867 strarray_add( &c2man_files, source->filename );
1868 output( "%s.o", obj_dir_path( make, obj ));
1869 if (crosstarget && need_cross) output( " %s.cross.o", obj_dir_path( make, obj ));
1870 output( ":" );
1872 free( obj );
1874 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
1875 output( "\n" );
1878 /* rules for files that depend on multiple sources */
1880 if (po_files.count)
1882 output( "%s: %s", obj_dir_path( make, "rsrc.pot" ), tools_path( make, "wrc" ) );
1883 output_filenames( po_files );
1884 output( "\n" );
1885 output( "\t%s -O pot -o $@", tools_path( make, "wrc" ));
1886 output_filenames( po_files );
1887 if (make->is_win16) output_filename( "-m16" );
1888 else output_filenames( target_flags );
1889 output_filename( "--nostdinc" );
1890 output_filenames( includes );
1891 output_filenames( make->define_args );
1892 output( "\n" );
1893 strarray_add( &clean_files, "rsrc.pot" );
1896 if (mc_files.count)
1898 output( "%s: %s", obj_dir_path( make, "msg.pot" ), tools_path( make, "wmc" ));
1899 output_filenames( mc_files );
1900 output( "\n" );
1901 output( "\t%s -O pot -o $@", tools_path( make, "wmc" ));
1902 output_filenames( mc_files );
1903 output( "\n" );
1904 strarray_add( &clean_files, "msg.pot" );
1907 if (dlldata_files.count)
1909 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
1910 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
1911 output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
1912 output_filenames( dlldata_files );
1913 output( "\n" );
1916 if (make->module && !make->staticlib)
1918 struct strarray all_libs = empty_strarray;
1919 char *module_path = obj_dir_path( make, make->module );
1920 char *spec_file = NULL;
1922 if (!make->appmode.count)
1923 spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
1924 for (i = 0; i < make->delayimports.count; i++)
1925 strarray_add( &all_libs, strmake( "-l%s", make->delayimports.str[i] ));
1926 for (i = 0; i < make->imports.count; i++)
1927 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
1928 for (i = 0; i < make->delayimports.count; i++)
1929 strarray_add( &all_libs, strmake( "-Wb,-d%s", make->delayimports.str[i] ));
1930 strarray_add( &all_libs, "-lwine" );
1931 strarray_add( &all_libs, top_obj_dir_path( make, "libs/port/libwine_port.a" ));
1932 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
1933 strarray_addall( &all_libs, libs );
1935 if (*dll_ext)
1937 strarray_add( &all_targets, strmake( "%s%s", make->module, dll_ext ));
1938 strarray_add( &all_targets, strmake( "%s.fake", make->module ));
1939 output( "%s%s %s.fake:", module_path, dll_ext, module_path );
1941 else
1943 strarray_add( &all_targets, make->module );
1944 output( "%s:", module_path );
1946 if (spec_file) output_filename( spec_file );
1947 output_filenames_obj_dir( make, object_files );
1948 output_filenames_obj_dir( make, res_files );
1949 output( "\n" );
1950 output( "\t%s -o $@", tools_path( make, "winegcc" ));
1951 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
1952 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
1953 output_filenames( target_flags );
1954 output_filenames( unwind_flags );
1955 if (spec_file)
1957 output( " -shared %s", spec_file );
1958 output_filenames( make->extradllflags );
1960 else output_filenames( make->appmode );
1961 output_filenames_obj_dir( make, object_files );
1962 output_filenames_obj_dir( make, res_files );
1963 output_filenames( all_libs );
1964 output_filename( "$(LDFLAGS)" );
1965 output( "\n" );
1967 if (spec_file && make->importlib)
1969 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
1970 if (*dll_ext)
1972 strarray_add( &clean_files, strmake( "lib%s.def", make->importlib ));
1973 output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
1974 output( "\t%s -w --def -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
1975 output_filenames( target_flags );
1976 if (make->is_win16) output_filename( "-m16" );
1977 output( "\n" );
1978 if (implib_objs.count)
1980 strarray_add( &clean_files, strmake( "lib%s.def.a", make->importlib ));
1981 output( "%s.def.a:", importlib_path );
1982 output_filenames_obj_dir( make, implib_objs );
1983 output( "\n" );
1984 output( "\t$(RM) $@\n" );
1985 output( "\t$(AR) $(ARFLAGS) $@" );
1986 output_filenames_obj_dir( make, implib_objs );
1987 output( "\n" );
1988 output( "\t$(RANLIB) $@\n" );
1991 else
1993 strarray_add( &clean_files, strmake( "lib%s.a", make->importlib ));
1994 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
1995 output_filenames_obj_dir( make, implib_objs );
1996 output( "\n" );
1997 output( "\t%s -w --implib -o $@ --export %s", tools_path( make, "winebuild" ), spec_file );
1998 output_filenames( target_flags );
1999 output_filenames_obj_dir( make, implib_objs );
2000 output( "\n" );
2002 if (crosstarget && !make->is_win16)
2004 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
2005 strarray_add( &clean_files, strmake( "lib%s.cross.a", make->importlib ));
2006 output( "%s.cross.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
2007 output_filenames_obj_dir( make, cross_files );
2008 output( "\n" );
2009 output( "\t%s -b %s -w --implib -o $@ --export %s",
2010 tools_path( make, "winebuild" ), crosstarget, spec_file );
2011 output_filenames_obj_dir( make, cross_files );
2012 output( "\n" );
2016 if (spec_file)
2018 if (c2man_files.count)
2020 output( "manpages::\n" );
2021 output( "\t%s -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2022 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2023 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2024 output_filename( strmake( "-o %s/man%s",
2025 top_obj_dir_path( make, "documentation" ), man_ext ));
2026 output_filenames( c2man_files );
2027 output( "\n" );
2028 output( "htmlpages::\n" );
2029 output( "\t%s -Th -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2030 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2031 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2032 output_filename( strmake( "-o %s",
2033 top_obj_dir_path( make, "documentation/html" )));
2034 output_filenames( c2man_files );
2035 output( "\n" );
2036 output( "sgmlpages::\n" );
2037 output( "\t%s -Ts -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2038 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2039 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2040 output_filename( strmake( "-o %s",
2041 top_obj_dir_path( make, "documentation/api-guide" )));
2042 output_filenames( c2man_files );
2043 output( "\n" );
2044 output( "xmlpages::\n" );
2045 output( "\t%s -Tx -w %s", top_dir_path( make, "tools/c2man.pl" ), spec_file );
2046 output_filename( strmake( "-R%s", top_dir_path( make, "" )));
2047 output_filename( strmake( "-I%s", top_dir_path( make, "include" )));
2048 output_filename( strmake( "-o %s",
2049 top_obj_dir_path( make, "documentation/api-guide-xml" )));
2050 output_filenames( c2man_files );
2051 output( "\n" );
2052 strarray_add( &phony_targets, "manpages" );
2053 strarray_add( &phony_targets, "htmlpages" );
2054 strarray_add( &phony_targets, "sgmlpages" );
2055 strarray_add( &phony_targets, "xmlpages" );
2057 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
2061 if (make->staticlib)
2063 strarray_add( &all_targets, make->staticlib );
2064 output( "%s:", obj_dir_path( make, make->staticlib ));
2065 output_filenames_obj_dir( make, object_files );
2066 output( "\n\t$(RM) $@\n" );
2067 output( "\t$(AR) $(ARFLAGS) $@" );
2068 output_filenames_obj_dir( make, object_files );
2069 output( "\n\t$(RANLIB) $@\n" );
2070 if (crosstarget && make->module)
2072 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
2074 strarray_add( &all_targets, name );
2075 output( "%s:", obj_dir_path( make, name ));
2076 output_filenames_obj_dir( make, crossobj_files );
2077 output( "\n\t$(RM) $@\n" );
2078 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
2079 output_filenames_obj_dir( make, crossobj_files );
2080 output( "\n\t%s-ranlib $@\n", crosstarget );
2084 if (make->testdll)
2086 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
2087 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
2088 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
2089 struct strarray all_libs = empty_strarray;
2091 for (i = 0; i < make->imports.count; i++)
2092 strarray_add( &all_libs, strmake( "-l%s", make->imports.str[i] ));
2093 strarray_addall( &all_libs, get_expanded_make_var_array( make, "LIBS" ));
2095 strarray_add( &all_targets, strmake( "%s%s", testmodule, dll_ext ));
2096 strarray_add( &clean_files, strmake( "%s%s", stripped, dll_ext ));
2097 output( "%s%s:\n", obj_dir_path( make, testmodule ), dll_ext );
2098 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2099 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2100 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2101 output_filenames( target_flags );
2102 output_filenames( unwind_flags );
2103 output_filenames( make->appmode );
2104 output_filenames_obj_dir( make, object_files );
2105 output_filenames_obj_dir( make, res_files );
2106 output_filenames( all_libs );
2107 output_filename( "$(LDFLAGS)" );
2108 output( "\n" );
2109 output( "%s%s:\n", obj_dir_path( make, stripped ), dll_ext );
2110 output( "\t%s -o $@", tools_path( make, "winegcc" ));
2111 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2112 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2113 output_filenames( target_flags );
2114 output_filenames( unwind_flags );
2115 output_filename( strmake( "-Wb,-F,%s", testmodule ));
2116 output_filenames( make->appmode );
2117 output_filenames_obj_dir( make, object_files );
2118 output_filenames_obj_dir( make, res_files );
2119 output_filenames( all_libs );
2120 output_filename( "$(LDFLAGS)" );
2121 output( "\n" );
2122 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), dll_ext,
2123 obj_dir_path( make, stripped ), dll_ext );
2124 output_filenames_obj_dir( make, object_files );
2125 output_filenames_obj_dir( make, res_files );
2126 output( "\n" );
2128 output( "all: %s/%s\n", top_obj_dir_path( make, "programs/winetest" ), testres );
2129 output( "%s/%s: %s%s\n", top_obj_dir_path( make, "programs/winetest" ), testres,
2130 obj_dir_path( make, stripped ), dll_ext );
2131 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2132 testmodule, obj_dir_path( make, stripped ), dll_ext, tools_path( make, "wrc" ));
2134 if (crosstarget)
2136 char *crosstest = replace_extension( make->testdll, ".dll", "_crosstest.exe" );
2138 strarray_add( &clean_files, crosstest );
2139 output( "%s: %s\n", obj_dir_path( make, "crosstest" ), obj_dir_path( make, crosstest ));
2140 output( "%s:", obj_dir_path( make, crosstest ));
2141 output_filenames_obj_dir( make, crossobj_files );
2142 output_filenames_obj_dir( make, res_files );
2143 output( "\n" );
2144 output( "\t%s -o $@ -b %s", tools_path( make, "winegcc" ), crosstarget );
2145 output_filename( strmake( "-B%s", tools_dir_path( make, "winebuild" )));
2146 if (tools_dir) output_filename( strmake( "--sysroot=%s", top_obj_dir_path( make, "" )));
2147 output_filename( "--lib-suffix=.cross.a" );
2148 output_filenames_obj_dir( make, crossobj_files );
2149 output_filenames_obj_dir( make, res_files );
2150 output_filenames( all_libs );
2151 output_filename( "$(LDFLAGS)" );
2152 output( "\n" );
2153 strarray_add( &phony_targets, obj_dir_path( make, "crosstest" ));
2154 if (make->obj_dir) output( "crosstest: %s\n", obj_dir_path( make, "crosstest" ));
2157 output_filenames_obj_dir( make, ok_files );
2158 output( ": %s%s ../%s%s\n", testmodule, dll_ext, make->testdll, dll_ext );
2159 output( "check test:" );
2160 output_filenames_obj_dir( make, ok_files );
2161 output( "\n" );
2162 output( "testclean::\n" );
2163 output( "\t$(RM)" );
2164 output_filenames_obj_dir( make, ok_files );
2165 output( "\n" );
2166 strarray_addall( &clean_files, ok_files );
2167 strarray_add( &phony_targets, "check" );
2168 strarray_add( &phony_targets, "test" );
2169 strarray_add( &phony_targets, "testclean" );
2170 *testlist_files = strarray_replace_extension( &ok_files, ".ok", "" );
2173 if (all_targets.count)
2175 output( "all:" );
2176 output_filenames_obj_dir( make, all_targets );
2177 output( "\n" );
2180 strarray_addall( &clean_files, object_files );
2181 strarray_addall( &clean_files, crossobj_files );
2182 strarray_addall( &clean_files, res_files );
2183 strarray_addall( &clean_files, all_targets );
2184 strarray_addall( &clean_files, get_expanded_make_var_array( make, "EXTRA_TARGETS" ));
2186 if (clean_files.count)
2188 output( "%s::\n", obj_dir_path( make, "clean" ));
2189 output( "\t$(RM)" );
2190 output_filenames_obj_dir( make, clean_files );
2191 output( "\n" );
2192 if (make->obj_dir) output( "__clean__: %s\n", obj_dir_path( make, "clean" ));
2193 strarray_add( &phony_targets, obj_dir_path( make, "clean" ));
2196 if (make->top_obj_dir)
2198 output( "depend:\n" );
2199 output( "\t@cd %s && $(MAKE) %s\n", make->top_obj_dir, base_dir_path( make, "depend" ));
2200 strarray_add( &phony_targets, "depend" );
2203 if (phony_targets.count)
2205 output( ".PHONY:" );
2206 output_filenames( phony_targets );
2207 output( "\n" );
2210 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
2212 return clean_files;
2216 /*******************************************************************
2217 * create_temp_file
2219 static FILE *create_temp_file( const char *orig )
2221 char *name = xmalloc( strlen(orig) + 13 );
2222 unsigned int i, id = getpid();
2223 int fd;
2224 FILE *ret = NULL;
2226 for (i = 0; i < 100; i++)
2228 sprintf( name, "%s.tmp%08x", orig, id );
2229 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
2231 ret = fdopen( fd, "w" );
2232 break;
2234 if (errno != EEXIST) break;
2235 id += 7777;
2237 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
2238 temp_file_name = name;
2239 return ret;
2243 /*******************************************************************
2244 * rename_temp_file
2246 static void rename_temp_file( const char *dest )
2248 int ret = rename( temp_file_name, dest );
2249 if (ret == -1 && errno == EEXIST)
2251 /* rename doesn't overwrite on windows */
2252 unlink( dest );
2253 ret = rename( temp_file_name, dest );
2255 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
2256 temp_file_name = NULL;
2260 /*******************************************************************
2261 * are_files_identical
2263 static int are_files_identical( FILE *file1, FILE *file2 )
2265 for (;;)
2267 char buffer1[8192], buffer2[8192];
2268 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
2269 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
2270 if (size1 != size2) return 0;
2271 if (!size1) return feof( file1 ) && feof( file2 );
2272 if (memcmp( buffer1, buffer2, size1 )) return 0;
2277 /*******************************************************************
2278 * rename_temp_file_if_changed
2280 static void rename_temp_file_if_changed( const char *dest )
2282 FILE *file1, *file2;
2283 int do_rename = 1;
2285 if ((file1 = fopen( dest, "r" )))
2287 if ((file2 = fopen( temp_file_name, "r" )))
2289 do_rename = !are_files_identical( file1, file2 );
2290 fclose( file2 );
2292 fclose( file1 );
2294 if (!do_rename)
2296 unlink( temp_file_name );
2297 temp_file_name = NULL;
2299 else rename_temp_file( dest );
2303 /*******************************************************************
2304 * output_testlist
2306 static void output_testlist( const char *dest, struct strarray files )
2308 int i;
2310 output_file = create_temp_file( dest );
2312 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2313 output( "#define WIN32_LEAN_AND_MEAN\n" );
2314 output( "#include <windows.h>\n\n" );
2315 output( "#define STANDALONE\n" );
2316 output( "#include \"wine/test.h\"\n\n" );
2318 for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
2319 output( "\n" );
2320 output( "const struct test winetest_testlist[] =\n" );
2321 output( "{\n" );
2322 for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
2323 output( " { 0, 0 }\n" );
2324 output( "};\n" );
2326 if (fclose( output_file )) fatal_perror( "write" );
2327 output_file = NULL;
2328 rename_temp_file_if_changed( dest );
2332 /*******************************************************************
2333 * output_gitignore
2335 static void output_gitignore( const char *dest, struct strarray files )
2337 int i;
2339 output_file = create_temp_file( dest );
2341 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2342 for (i = 0; i < files.count; i++)
2344 if (!strchr( files.str[i], '/' )) output( "/" );
2345 output( "%s\n", files.str[i] );
2348 if (fclose( output_file )) fatal_perror( "write" );
2349 output_file = NULL;
2350 rename_temp_file( dest );
2354 /*******************************************************************
2355 * output_dependencies
2357 static void output_dependencies( struct makefile *make, const char *path )
2359 struct strarray targets, testlist_files = empty_strarray, ignore_files = empty_strarray;
2361 if (Separator && ((output_file = fopen( path, "r" ))))
2363 char buffer[1024];
2364 FILE *tmp_file = create_temp_file( path );
2365 int found = 0;
2367 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2369 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2370 found = !strncmp( buffer, Separator, strlen(Separator) );
2372 if (fclose( output_file )) fatal_perror( "write" );
2373 output_file = tmp_file;
2374 if (!found) output( "\n%s\n", Separator );
2376 else
2378 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2379 fatal_perror( "%s", path );
2382 targets = output_sources( make, &testlist_files );
2384 fclose( output_file );
2385 output_file = NULL;
2386 if (temp_file_name) rename_temp_file( path );
2388 strarray_add( &ignore_files, ".gitignore" );
2389 strarray_add( &ignore_files, "Makefile" );
2390 if (testlist_files.count) strarray_add( &ignore_files, "testlist.c" );
2391 strarray_addall( &ignore_files, targets );
2393 if (testlist_files.count)
2394 output_testlist( base_dir_path( make, "testlist.c" ), testlist_files );
2395 if (!make->src_dir && make->base_dir)
2396 output_gitignore( base_dir_path( make, ".gitignore" ), ignore_files );
2400 /*******************************************************************
2401 * update_makefile
2403 static void update_makefile( const char *path )
2405 static const char *source_vars[] =
2407 "C_SRCS",
2408 "OBJC_SRCS",
2409 "RC_SRCS",
2410 "MC_SRCS",
2411 "IDL_SRCS",
2412 "BISON_SRCS",
2413 "LEX_SRCS",
2414 "XTEMPLATE_SRCS",
2415 "SVG_SRCS",
2416 "FONT_SRCS",
2417 "IN_SRCS",
2418 "MANPAGES",
2419 NULL
2421 const char **var;
2422 unsigned int i;
2423 struct strarray value;
2424 struct incl_file *file;
2425 struct makefile *make;
2427 make = parse_makefile( path, Separator );
2429 if (root_src_dir)
2431 make->top_src_dir = concat_paths( make->top_obj_dir, root_src_dir );
2432 make->src_dir = concat_paths( make->top_src_dir, make->base_dir );
2434 strarray_set_value( &make->vars, "top_builddir", top_obj_dir_path( make, "" ));
2435 strarray_set_value( &make->vars, "top_srcdir", top_dir_path( make, "" ));
2436 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
2438 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
2439 make->module = get_expanded_make_variable( make, "MODULE" );
2440 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
2441 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
2442 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
2444 make->appmode = get_expanded_make_var_array( make, "APPMODE" );
2445 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
2446 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
2447 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
2449 if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
2451 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
2452 make->use_msvcrt = strarray_exists( &make->appmode, "-mno-cygwin" );
2454 for (i = 0; i < make->imports.count && !make->use_msvcrt; i++)
2455 make->use_msvcrt = !strncmp( make->imports.str[i], "msvcr", 5 );
2457 make->include_args = empty_strarray;
2458 make->define_args = empty_strarray;
2459 strarray_add( &make->define_args, "-D__WINESRC__" );
2461 value = get_expanded_make_var_array( make, "EXTRAINCL" );
2462 for (i = 0; i < value.count; i++)
2463 if (!strncmp( value.str[i], "-I", 2 ))
2464 strarray_add_uniq( &make->include_args, value.str[i] );
2465 else
2466 strarray_add_uniq( &make->define_args, value.str[i] );
2467 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
2469 list_init( &make->sources );
2470 list_init( &includes );
2472 for (var = source_vars; *var; var++)
2474 value = get_expanded_make_var_array( make, *var );
2475 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
2478 add_generated_sources( make );
2480 value = get_expanded_make_var_array( make, "EXTRA_OBJS" );
2481 for (i = 0; i < value.count; i++)
2483 /* default to .c for unknown extra object files */
2484 if (strendswith( value.str[i], ".o" ))
2485 add_generated_source( make, value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2486 else
2487 add_generated_source( make, value.str[i], NULL );
2490 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( make, file, 0 );
2492 output_file_name = base_dir_path( make, makefile_name );
2493 output_dependencies( make, output_file_name );
2494 output_file_name = NULL;
2498 /*******************************************************************
2499 * parse_makeflags
2501 static void parse_makeflags( const char *flags )
2503 const char *p = flags;
2504 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2506 while (*p)
2508 while (isspace(*p)) p++;
2509 var = buffer;
2510 while (*p && !isspace(*p))
2512 if (*p == '\\' && p[1]) p++;
2513 *var++ = *p++;
2515 *var = 0;
2516 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2521 /*******************************************************************
2522 * parse_option
2524 static int parse_option( const char *opt )
2526 if (opt[0] != '-')
2528 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2529 return 0;
2531 switch(opt[1])
2533 case 'f':
2534 if (opt[2]) makefile_name = opt + 2;
2535 break;
2536 case 'R':
2537 relative_dir_mode = 1;
2538 break;
2539 case 's':
2540 if (opt[2]) Separator = opt + 2;
2541 else Separator = NULL;
2542 break;
2543 default:
2544 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2545 exit(1);
2547 return 1;
2551 /*******************************************************************
2552 * main
2554 int main( int argc, char *argv[] )
2556 const char *makeflags = getenv( "MAKEFLAGS" );
2557 int i, j;
2559 if (makeflags) parse_makeflags( makeflags );
2561 i = 1;
2562 while (i < argc)
2564 if (parse_option( argv[i] ))
2566 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2567 argc--;
2569 else i++;
2572 if (relative_dir_mode)
2574 char *relpath;
2576 if (argc != 3)
2578 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2579 exit( 1 );
2581 relpath = get_relative_path( argv[1], argv[2] );
2582 printf( "%s\n", relpath ? relpath : "." );
2583 exit( 0 );
2586 if (argc <= 1)
2588 fprintf( stderr, "%s", Usage );
2589 exit( 1 );
2591 atexit( cleanup_files );
2592 signal( SIGTERM, exit_on_signal );
2593 signal( SIGINT, exit_on_signal );
2594 #ifdef SIGHUP
2595 signal( SIGHUP, exit_on_signal );
2596 #endif
2598 top_makefile = parse_makefile( NULL, "# End of common header" );
2600 linguas = get_expanded_make_var_array( top_makefile, "LINGUAS" );
2601 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
2602 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
2603 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
2604 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
2605 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
2606 unwind_flags = get_expanded_make_var_array( top_makefile, "UNWINDFLAGS" );
2607 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
2609 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
2610 tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
2611 tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
2612 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
2613 man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
2614 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
2615 dll_prefix = get_expanded_make_variable( top_makefile, "DLLPREFIX" );
2616 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
2617 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
2618 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
2619 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
2620 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
2622 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
2623 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
2624 if (!tools_ext) tools_ext = "";
2625 if (!dll_prefix) dll_prefix = "";
2626 if (!man_ext) man_ext = "3w";
2628 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2629 return 0;