comctl32: Introduce _PSP structure to store HPROPSHEETPAGE data.
[wine.git] / tools / makedep.c
blobd7e045bdd6e40540f6cfbca92e528add0e237c85
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013, 2020 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"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <signal.h>
31 #include <string.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
35 #include "tools.h"
36 #include "wine/list.h"
38 enum incl_type
40 INCL_NORMAL, /* #include "foo.h" */
41 INCL_SYSTEM, /* #include <foo.h> */
42 INCL_IMPORT, /* idl import "foo.idl" */
43 INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
44 INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
45 INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
48 struct dependency
50 int line; /* source line where this header is included */
51 enum incl_type type; /* type of include */
52 char *name; /* header name */
55 struct file
57 struct list entry;
58 char *name; /* full file name relative to cwd */
59 void *args; /* custom arguments for makefile rule */
60 unsigned int flags; /* flags (see below) */
61 unsigned int deps_count; /* files in use */
62 unsigned int deps_size; /* total allocated size */
63 struct dependency *deps; /* all header dependencies */
66 struct incl_file
68 struct list entry;
69 struct list hash_entry;
70 struct file *file;
71 char *name;
72 char *filename;
73 char *basename; /* base target name for generated files */
74 char *sourcename; /* source file name for generated headers */
75 struct incl_file *included_by; /* file that included this one */
76 int included_line; /* line where this file was included */
77 enum incl_type type; /* type of include */
78 int use_msvcrt:1; /* put msvcrt headers in the search path? */
79 int is_external:1; /* file from external library? */
80 struct incl_file *owner;
81 unsigned int files_count; /* files in use */
82 unsigned int files_size; /* total allocated size */
83 struct incl_file **files;
84 struct strarray dependencies; /* file dependencies */
85 struct strarray importlibdeps; /* importlib dependencies */
88 #define FLAG_GENERATED 0x000001 /* generated file */
89 #define FLAG_INSTALL 0x000002 /* file to install */
90 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
91 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
92 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
93 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
94 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
95 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (_l.res) file */
96 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
97 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
98 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
99 #define FLAG_RC_HEADER 0x020000 /* rc file is a header */
100 #define FLAG_C_IMPLIB 0x040000 /* file is part of an import library */
101 #define FLAG_C_UNIX 0x080000 /* file is part of a Unix library */
102 #define FLAG_SFD_FONTS 0x100000 /* sfd file generated bitmap fonts */
104 static const struct
106 unsigned int flag;
107 const char *ext;
108 } idl_outputs[] =
110 { FLAG_IDL_TYPELIB, "_l.res" },
111 { FLAG_IDL_REGTYPELIB, "_t.res" },
112 { FLAG_IDL_CLIENT, "_c.c" },
113 { FLAG_IDL_IDENT, "_i.c" },
114 { FLAG_IDL_PROXY, "_p.c" },
115 { FLAG_IDL_SERVER, "_s.c" },
116 { FLAG_IDL_REGISTER, "_r.res" },
117 { FLAG_IDL_HEADER, ".h" }
120 #define HASH_SIZE 197
122 static struct list files[HASH_SIZE];
123 static struct list global_includes[HASH_SIZE];
125 enum install_rules { INSTALL_LIB, INSTALL_DEV, INSTALL_TEST, NB_INSTALL_RULES };
126 static const char *install_targets[NB_INSTALL_RULES] = { "install-lib", "install-dev", "install-test" };
127 static const char *install_variables[NB_INSTALL_RULES] = { "INSTALL_LIB", "INSTALL_DEV", "INSTALL_TEST" };
129 /* variables common to all makefiles */
130 static struct strarray linguas;
131 static struct strarray dll_flags;
132 static struct strarray unix_dllflags;
133 static struct strarray target_flags;
134 static struct strarray msvcrt_flags;
135 static struct strarray extra_cflags;
136 static struct strarray extra_cross_cflags;
137 static struct strarray extra_cflags_extlib;
138 static struct strarray extra_cross_cflags_extlib;
139 static struct strarray cpp_flags;
140 static struct strarray lddll_flags;
141 static struct strarray libs;
142 static struct strarray enable_tests;
143 static struct strarray cmdline_vars;
144 static struct strarray subdirs;
145 static struct strarray disabled_dirs;
146 static struct strarray delay_import_libs;
147 static struct strarray top_install[NB_INSTALL_RULES];
148 static const char *root_src_dir;
149 static const char *tools_dir;
150 static const char *tools_ext;
151 static const char *exe_ext;
152 static const char *dll_ext;
153 static const char *host_cpu;
154 static const char *pe_dir;
155 static const char *so_dir;
156 static const char *crosstarget;
157 static const char *crossdebug;
158 static const char *fontforge;
159 static const char *convert;
160 static const char *flex;
161 static const char *bison;
162 static const char *ar;
163 static const char *ranlib;
164 static const char *rsvg;
165 static const char *icotool;
166 static const char *dlltool;
167 static const char *msgfmt;
168 static const char *ln_s;
169 static const char *sed_cmd;
170 static const char *delay_load_flag;
172 struct makefile
174 /* values determined from input makefile */
175 struct strarray vars;
176 struct strarray include_paths;
177 struct strarray include_args;
178 struct strarray define_args;
179 struct strarray unix_cflags;
180 struct strarray programs;
181 struct strarray scripts;
182 struct strarray imports;
183 struct strarray delayimports;
184 struct strarray extradllflags;
185 struct strarray install[NB_INSTALL_RULES];
186 struct strarray extra_targets;
187 struct strarray extra_imports;
188 struct list sources;
189 struct list includes;
190 const char *src_dir;
191 const char *obj_dir;
192 const char *parent_dir;
193 const char *module;
194 const char *testdll;
195 const char *extlib;
196 const char *sharedlib;
197 const char *staticlib;
198 const char *staticimplib;
199 const char *importlib;
200 const char *unixlib;
201 int native_unix_lib;
202 int disabled;
203 int use_msvcrt;
204 int data_only;
205 int is_cross;
206 int is_win16;
207 int is_exe;
209 /* values generated at output time */
210 struct strarray in_files;
211 struct strarray ok_files;
212 struct strarray pot_files;
213 struct strarray test_files;
214 struct strarray clean_files;
215 struct strarray distclean_files;
216 struct strarray maintainerclean_files;
217 struct strarray uninstall_files;
218 struct strarray object_files;
219 struct strarray crossobj_files;
220 struct strarray unixobj_files;
221 struct strarray res_files;
222 struct strarray font_files;
223 struct strarray debug_files;
224 struct strarray dlldata_files;
225 struct strarray implib_files;
226 struct strarray crossimplib_files;
227 struct strarray all_targets;
228 struct strarray phony_targets;
229 struct strarray dependencies;
230 struct strarray install_rules[NB_INSTALL_RULES];
233 static struct makefile *top_makefile;
234 static struct makefile *include_makefile;
235 static struct makefile **submakes;
237 static const char separator[] = "### Dependencies";
238 static const char *output_makefile_name = "Makefile";
239 static const char *input_file_name;
240 static const char *output_file_name;
241 static const char *temp_file_name;
242 static int relative_dir_mode;
243 static int silent_rules;
244 static int input_line;
245 static int output_column;
246 static FILE *output_file;
248 static const char Usage[] =
249 "Usage: makedep [options] [directories]\n"
250 "Options:\n"
251 " -R from to Compute the relative path between two directories\n"
252 " -S Generate Automake-style silent rules\n"
253 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
256 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
257 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
258 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
259 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
261 /*******************************************************************
262 * fatal_error
264 static void fatal_error( const char *msg, ... )
266 va_list valist;
267 va_start( valist, msg );
268 if (input_file_name)
270 fprintf( stderr, "%s:", input_file_name );
271 if (input_line) fprintf( stderr, "%d:", input_line );
272 fprintf( stderr, " error: " );
274 else fprintf( stderr, "makedep: error: " );
275 vfprintf( stderr, msg, valist );
276 va_end( valist );
277 exit(1);
281 /*******************************************************************
282 * fatal_perror
284 static void fatal_perror( const char *msg, ... )
286 va_list valist;
287 va_start( valist, msg );
288 if (input_file_name)
290 fprintf( stderr, "%s:", input_file_name );
291 if (input_line) fprintf( stderr, "%d:", input_line );
292 fprintf( stderr, " error: " );
294 else fprintf( stderr, "makedep: error: " );
295 vfprintf( stderr, msg, valist );
296 perror( " " );
297 va_end( valist );
298 exit(1);
302 /*******************************************************************
303 * cleanup_files
305 static void cleanup_files(void)
307 if (temp_file_name) unlink( temp_file_name );
308 if (output_file_name) unlink( output_file_name );
312 /*******************************************************************
313 * exit_on_signal
315 static void exit_on_signal( int sig )
317 exit( 1 ); /* this will call the atexit functions */
321 /*******************************************************************
322 * output
324 static void output( const char *format, ... )
326 int ret;
327 va_list valist;
329 va_start( valist, format );
330 ret = vfprintf( output_file, format, valist );
331 va_end( valist );
332 if (ret < 0) fatal_perror( "output" );
333 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
334 else output_column += ret;
338 /*******************************************************************
339 * strarray_get_value
341 * Find a value in a name/value pair string array.
343 static const char *strarray_get_value( const struct strarray *array, const char *name )
345 int pos, res, min = 0, max = array->count / 2 - 1;
347 while (min <= max)
349 pos = (min + max) / 2;
350 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
351 if (res < 0) min = pos + 1;
352 else max = pos - 1;
354 return NULL;
358 /*******************************************************************
359 * strarray_set_value
361 * Define a value in a name/value pair string array.
363 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
365 int i, pos, res, min = 0, max = array->count / 2 - 1;
367 while (min <= max)
369 pos = (min + max) / 2;
370 if (!(res = strcmp( array->str[pos * 2], name )))
372 /* redefining a variable replaces the previous value */
373 array->str[pos * 2 + 1] = value;
374 return;
376 if (res < 0) min = pos + 1;
377 else max = pos - 1;
379 strarray_add( array, NULL );
380 strarray_add( array, NULL );
381 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
382 array->str[min * 2] = name;
383 array->str[min * 2 + 1] = value;
387 /*******************************************************************
388 * normalize_arch
390 static const char *normalize_arch( const char *arch )
392 unsigned int i, j;
394 static const char *map[][8] =
396 /* normalized aliases */
397 { "i386", "i486", "i586", "i686", "ia32" },
398 { "x86_64", "amd64", "x86-64", "x86_amd64", "x64" },
399 { "aarch64", "arm64" },
400 { "arm" },
403 for (i = 0; i < sizeof(map) / sizeof(map[0]); i++)
404 for (j = 0; map[i][j]; j++)
405 if (!strncmp( arch, map[i][j], strlen(map[i][j]) ))
406 return map[i][0];
407 return NULL;
411 /*******************************************************************
412 * output_filename
414 static void output_filename( const char *name )
416 if (output_column + strlen(name) + 1 > 100)
418 output( " \\\n" );
419 output( " " );
421 else if (output_column) output( " " );
422 output( "%s", name );
426 /*******************************************************************
427 * output_filenames
429 static void output_filenames( struct strarray array )
431 unsigned int i;
433 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
437 /*******************************************************************
438 * output_rm_filenames
440 static void output_rm_filenames( struct strarray array )
442 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
443 unsigned int i, len;
445 if (!array.count) return;
446 output( "\trm -f" );
447 for (i = len = 0; i < array.count; i++)
449 if (len > max_cmdline)
451 output( "\n" );
452 output( "\trm -f" );
453 len = 0;
455 output_filename( array.str[i] );
456 len += strlen( array.str[i] ) + 1;
458 output( "\n" );
462 /*******************************************************************
463 * get_extension
465 static char *get_extension( char *filename )
467 char *ext = strrchr( filename, '.' );
468 if (ext && strchr( ext, '/' )) ext = NULL;
469 return ext;
473 /*******************************************************************
474 * get_base_name
476 static const char *get_base_name( const char *name )
478 char *base;
479 if (!strchr( name, '.' )) return name;
480 base = xstrdup( name );
481 *strrchr( base, '.' ) = 0;
482 return base;
486 /*******************************************************************
487 * replace_filename
489 static char *replace_filename( const char *path, const char *name )
491 const char *p;
492 char *ret;
493 size_t len;
495 if (!path) return xstrdup( name );
496 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
497 len = p - path + 1;
498 ret = xmalloc( len + strlen( name ) + 1 );
499 memcpy( ret, path, len );
500 strcpy( ret + len, name );
501 return ret;
505 /*******************************************************************
506 * replace_substr
508 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
510 size_t pos = start - str;
511 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
512 memcpy( ret, str, pos );
513 strcpy( ret + pos, replace );
514 strcat( ret + pos, start + len );
515 return ret;
519 /*******************************************************************
520 * get_relative_path
522 * Determine where the destination path is located relative to the 'from' path.
524 static char *get_relative_path( const char *from, const char *dest )
526 const char *start;
527 char *ret, *p;
528 unsigned int dotdots = 0;
530 /* a path of "." is equivalent to an empty path */
531 if (!strcmp( from, "." )) from = "";
533 for (;;)
535 while (*from == '/') from++;
536 while (*dest == '/') dest++;
537 start = dest; /* save start of next path element */
538 if (!*from) break;
540 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
541 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
543 /* count remaining elements in 'from' */
546 dotdots++;
547 while (*from && *from != '/') from++;
548 while (*from == '/') from++;
550 while (*from);
551 break;
554 if (!start[0] && !dotdots) return NULL; /* empty path */
556 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
557 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
559 if (start[0]) strcpy( p, start );
560 else p[-1] = 0; /* remove trailing slash */
561 return ret;
565 /*******************************************************************
566 * concat_paths
568 static char *concat_paths( const char *base, const char *path )
570 int i, len;
571 char *ret;
573 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
574 if (!path || !path[0]) return xstrdup( base );
575 if (path[0] == '/') return xstrdup( path );
577 len = strlen( base );
578 while (len && base[len - 1] == '/') len--;
579 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
581 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
582 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
583 if (i != len - 1 || base[i] != '.')
585 path += 2;
586 while (*path == '/') path++;
588 /* else ignore "." element */
589 while (i > 0 && base[i - 1] == '/') i--;
590 len = i;
592 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
593 ret = xmalloc( len + strlen( path ) + 2 );
594 memcpy( ret, base, len );
595 ret[len++] = '/';
596 strcpy( ret + len, path );
597 return ret;
601 /*******************************************************************
602 * obj_dir_path
604 static char *obj_dir_path( const struct makefile *make, const char *path )
606 return concat_paths( make->obj_dir, path );
610 /*******************************************************************
611 * src_dir_path
613 static char *src_dir_path( const struct makefile *make, const char *path )
615 if (make->src_dir) return concat_paths( make->src_dir, path );
616 return obj_dir_path( make, path );
620 /*******************************************************************
621 * root_src_dir_path
623 static char *root_src_dir_path( const char *path )
625 return concat_paths( root_src_dir, path );
629 /*******************************************************************
630 * tools_dir_path
632 static char *tools_dir_path( const struct makefile *make, const char *path )
634 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
635 return strmake( "tools/%s", path );
639 /*******************************************************************
640 * tools_path
642 static char *tools_path( const struct makefile *make, const char *name )
644 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
648 /*******************************************************************
649 * strarray_addall_path
651 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
653 unsigned int i;
655 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
659 /*******************************************************************
660 * get_line
662 static char *get_line( FILE *file )
664 static char *buffer;
665 static size_t size;
667 if (!size)
669 size = 1024;
670 buffer = xmalloc( size );
672 if (!fgets( buffer, size, file )) return NULL;
673 input_line++;
675 for (;;)
677 char *p = buffer + strlen(buffer);
678 /* if line is larger than buffer, resize buffer */
679 while (p == buffer + size - 1 && p[-1] != '\n')
681 buffer = xrealloc( buffer, size * 2 );
682 if (!fgets( buffer + size - 1, size + 1, file )) break;
683 p = buffer + strlen(buffer);
684 size *= 2;
686 if (p > buffer && p[-1] == '\n')
688 *(--p) = 0;
689 if (p > buffer && p[-1] == '\r') *(--p) = 0;
690 if (p > buffer && p[-1] == '\\')
692 *(--p) = 0;
693 /* line ends in backslash, read continuation line */
694 if (!fgets( p, size - (p - buffer), file )) return buffer;
695 input_line++;
696 continue;
699 return buffer;
704 /*******************************************************************
705 * hash_filename
707 static unsigned int hash_filename( const char *name )
709 /* FNV-1 hash */
710 unsigned int ret = 2166136261u;
711 while (*name) ret = (ret * 16777619) ^ *name++;
712 return ret % HASH_SIZE;
716 /*******************************************************************
717 * add_file
719 static struct file *add_file( const char *name )
721 struct file *file = xmalloc( sizeof(*file) );
722 memset( file, 0, sizeof(*file) );
723 file->name = xstrdup( name );
724 return file;
728 /*******************************************************************
729 * add_dependency
731 static void add_dependency( struct file *file, const char *name, enum incl_type type )
733 if (file->deps_count >= file->deps_size)
735 file->deps_size *= 2;
736 if (file->deps_size < 16) file->deps_size = 16;
737 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
739 file->deps[file->deps_count].line = input_line;
740 file->deps[file->deps_count].type = type;
741 file->deps[file->deps_count].name = xstrdup( name );
742 file->deps_count++;
746 /*******************************************************************
747 * find_src_file
749 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
751 struct incl_file *file;
753 if (make == include_makefile)
755 unsigned int hash = hash_filename( name );
757 LIST_FOR_EACH_ENTRY( file, &global_includes[hash], struct incl_file, hash_entry )
758 if (!strcmp( name, file->name )) return file;
759 return NULL;
762 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
763 if (!strcmp( name, file->name )) return file;
764 return NULL;
767 /*******************************************************************
768 * find_include_file
770 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
772 struct incl_file *file;
774 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
776 const char *filename = file->filename;
777 if (!filename) continue;
778 if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
780 filename += strlen(make->obj_dir);
781 while (*filename == '/') filename++;
783 if (!strcmp( name, filename )) return file;
785 return NULL;
788 /*******************************************************************
789 * add_include
791 * Add an include file if it doesn't already exists.
793 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
794 const char *name, int line, enum incl_type type )
796 struct incl_file *include;
798 if (parent->files_count >= parent->files_size)
800 parent->files_size *= 2;
801 if (parent->files_size < 16) parent->files_size = 16;
802 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
805 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
806 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
807 goto found;
809 include = xmalloc( sizeof(*include) );
810 memset( include, 0, sizeof(*include) );
811 include->name = xstrdup(name);
812 include->included_by = parent;
813 include->included_line = line;
814 include->type = type;
815 include->use_msvcrt = parent->use_msvcrt;
816 list_add_tail( &make->includes, &include->entry );
817 found:
818 parent->files[parent->files_count++] = include;
819 return include;
823 /*******************************************************************
824 * add_generated_source
826 * Add a generated source file to the list.
828 static struct incl_file *add_generated_source( struct makefile *make,
829 const char *name, const char *filename )
831 struct incl_file *file = xmalloc( sizeof(*file) );
833 memset( file, 0, sizeof(*file) );
834 file->file = add_file( name );
835 file->name = xstrdup( name );
836 file->basename = xstrdup( filename ? filename : name );
837 file->filename = obj_dir_path( make, file->basename );
838 file->file->flags = FLAG_GENERATED;
839 file->use_msvcrt = make->use_msvcrt;
840 list_add_tail( &make->sources, &file->entry );
841 if (make == include_makefile)
843 unsigned int hash = hash_filename( name );
844 list_add_tail( &global_includes[hash], &file->hash_entry );
846 return file;
850 /*******************************************************************
851 * parse_include_directive
853 static void parse_include_directive( struct file *source, char *str )
855 char quote, *include, *p = str;
857 while (*p && isspace(*p)) p++;
858 if (*p != '\"' && *p != '<' ) return;
859 quote = *p++;
860 if (quote == '<') quote = '>';
861 include = p;
862 while (*p && (*p != quote)) p++;
863 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
864 *p = 0;
865 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
869 /*******************************************************************
870 * parse_pragma_directive
872 static void parse_pragma_directive( struct file *source, char *str )
874 char *flag, *p = str;
876 if (!isspace( *p )) return;
877 while (*p && isspace(*p)) p++;
878 p = strtok( p, " \t" );
879 if (strcmp( p, "makedep" )) return;
881 while ((flag = strtok( NULL, " \t" )))
883 if (!strcmp( flag, "depend" ))
885 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
886 return;
888 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
890 if (strendswith( source->name, ".idl" ))
892 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
893 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
894 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
895 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
896 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
897 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
898 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
899 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
901 else if (strendswith( source->name, ".rc" ))
903 if (!strcmp( flag, "header" )) source->flags |= FLAG_RC_HEADER;
904 else if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
906 else if (strendswith( source->name, ".sfd" ))
908 if (!strcmp( flag, "font" ))
910 struct strarray *array = source->args;
912 if (!array)
914 source->args = array = xmalloc( sizeof(*array) );
915 *array = empty_strarray;
916 source->flags |= FLAG_SFD_FONTS;
918 strarray_add( array, xstrdup( strtok( NULL, "" )));
919 return;
922 else
924 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
925 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
931 /*******************************************************************
932 * parse_cpp_directive
934 static void parse_cpp_directive( struct file *source, char *str )
936 while (*str && isspace(*str)) str++;
937 if (*str++ != '#') return;
938 while (*str && isspace(*str)) str++;
940 if (!strncmp( str, "include", 7 ))
941 parse_include_directive( source, str + 7 );
942 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
943 parse_include_directive( source, str + 6 );
944 else if (!strncmp( str, "pragma", 6 ))
945 parse_pragma_directive( source, str + 6 );
949 /*******************************************************************
950 * parse_idl_file
952 static void parse_idl_file( struct file *source, FILE *file )
954 char *buffer, *include;
956 input_line = 0;
958 while ((buffer = get_line( file )))
960 char quote;
961 char *p = buffer;
962 while (*p && isspace(*p)) p++;
964 if (!strncmp( p, "importlib", 9 ))
966 p += 9;
967 while (*p && isspace(*p)) p++;
968 if (*p++ != '(') continue;
969 while (*p && isspace(*p)) p++;
970 if (*p++ != '"') continue;
971 include = p;
972 while (*p && (*p != '"')) p++;
973 if (!*p) fatal_error( "malformed importlib directive\n" );
974 *p = 0;
975 add_dependency( source, include, INCL_IMPORTLIB );
976 continue;
979 if (!strncmp( p, "import", 6 ))
981 p += 6;
982 while (*p && isspace(*p)) p++;
983 if (*p != '"') continue;
984 include = ++p;
985 while (*p && (*p != '"')) p++;
986 if (!*p) fatal_error( "malformed import directive\n" );
987 *p = 0;
988 add_dependency( source, include, INCL_IMPORT );
989 continue;
992 /* check for #include inside cpp_quote */
993 if (!strncmp( p, "cpp_quote", 9 ))
995 p += 9;
996 while (*p && isspace(*p)) p++;
997 if (*p++ != '(') continue;
998 while (*p && isspace(*p)) p++;
999 if (*p++ != '"') continue;
1000 if (*p++ != '#') continue;
1001 while (*p && isspace(*p)) p++;
1002 if (strncmp( p, "include", 7 )) continue;
1003 p += 7;
1004 while (*p && isspace(*p)) p++;
1005 if (*p == '\\' && p[1] == '"')
1007 p += 2;
1008 quote = '"';
1010 else
1012 if (*p++ != '<' ) continue;
1013 quote = '>';
1015 include = p;
1016 while (*p && (*p != quote)) p++;
1017 if (!*p || (quote == '"' && p[-1] != '\\'))
1018 fatal_error( "malformed #include directive inside cpp_quote\n" );
1019 if (quote == '"') p--; /* remove backslash */
1020 *p = 0;
1021 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1022 continue;
1025 parse_cpp_directive( source, p );
1029 /*******************************************************************
1030 * parse_c_file
1032 static void parse_c_file( struct file *source, FILE *file )
1034 char *buffer;
1036 input_line = 0;
1037 while ((buffer = get_line( file )))
1039 parse_cpp_directive( source, buffer );
1044 /*******************************************************************
1045 * parse_rc_file
1047 static void parse_rc_file( struct file *source, FILE *file )
1049 char *buffer, *include;
1051 input_line = 0;
1052 while ((buffer = get_line( file )))
1054 char quote;
1055 char *p = buffer;
1056 while (*p && isspace(*p)) p++;
1058 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1060 p += 2;
1061 while (*p && isspace(*p)) p++;
1062 if (strncmp( p, "@makedep:", 9 )) continue;
1063 p += 9;
1064 while (*p && isspace(*p)) p++;
1065 quote = '"';
1066 if (*p == quote)
1068 include = ++p;
1069 while (*p && *p != quote) p++;
1071 else
1073 include = p;
1074 while (*p && !isspace(*p) && *p != '*') p++;
1076 if (!*p)
1077 fatal_error( "malformed makedep comment\n" );
1078 *p = 0;
1079 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1080 continue;
1083 parse_cpp_directive( source, buffer );
1088 /*******************************************************************
1089 * parse_in_file
1091 static void parse_in_file( struct file *source, FILE *file )
1093 char *p, *buffer;
1095 /* make sure it gets rebuilt when the version changes */
1096 add_dependency( source, "config.h", INCL_SYSTEM );
1098 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1100 input_line = 0;
1101 while ((buffer = get_line( file )))
1103 if (strncmp( buffer, ".TH", 3 )) continue;
1104 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1105 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1106 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1107 source->args = xstrdup( p );
1108 return;
1113 /*******************************************************************
1114 * parse_sfd_file
1116 static void parse_sfd_file( struct file *source, FILE *file )
1118 char *p, *eol, *buffer;
1120 input_line = 0;
1121 while ((buffer = get_line( file )))
1123 if (strncmp( buffer, "UComments:", 10 )) continue;
1124 p = buffer + 10;
1125 while (*p == ' ') p++;
1126 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1128 p++;
1129 buffer[strlen(buffer) - 1] = 0;
1131 while ((eol = strstr( p, "+AAoA" )))
1133 *eol = 0;
1134 while (*p && isspace(*p)) p++;
1135 if (*p++ == '#')
1137 while (*p && isspace(*p)) p++;
1138 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1140 p = eol + 5;
1142 while (*p && isspace(*p)) p++;
1143 if (*p++ != '#') return;
1144 while (*p && isspace(*p)) p++;
1145 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1146 return;
1151 static const struct
1153 const char *ext;
1154 void (*parse)( struct file *file, FILE *f );
1155 } parse_functions[] =
1157 { ".c", parse_c_file },
1158 { ".h", parse_c_file },
1159 { ".inl", parse_c_file },
1160 { ".l", parse_c_file },
1161 { ".m", parse_c_file },
1162 { ".rh", parse_c_file },
1163 { ".x", parse_c_file },
1164 { ".y", parse_c_file },
1165 { ".idl", parse_idl_file },
1166 { ".rc", parse_rc_file },
1167 { ".in", parse_in_file },
1168 { ".sfd", parse_sfd_file }
1171 /*******************************************************************
1172 * load_file
1174 static struct file *load_file( const char *name )
1176 struct file *file;
1177 FILE *f;
1178 unsigned int i, hash = hash_filename( name );
1180 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1181 if (!strcmp( name, file->name )) return file;
1183 if (!(f = fopen( name, "r" ))) return NULL;
1185 file = add_file( name );
1186 list_add_tail( &files[hash], &file->entry );
1187 input_file_name = file->name;
1188 input_line = 0;
1190 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1192 if (!strendswith( name, parse_functions[i].ext )) continue;
1193 parse_functions[i].parse( file, f );
1194 break;
1197 fclose( f );
1198 input_file_name = NULL;
1200 return file;
1204 /*******************************************************************
1205 * open_include_path_file
1207 * Open a file from a directory on the include path.
1209 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1210 const char *name, char **filename )
1212 char *src_path = concat_paths( dir, name );
1213 struct file *ret = load_file( src_path );
1215 if (ret) *filename = src_path;
1216 return ret;
1220 /*******************************************************************
1221 * open_file_same_dir
1223 * Open a file in the same directory as the parent.
1225 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1227 char *src_path = replace_filename( parent->file->name, name );
1228 struct file *ret = load_file( src_path );
1230 if (ret) *filename = replace_filename( parent->filename, name );
1231 return ret;
1235 /*******************************************************************
1236 * open_same_dir_generated_file
1238 * Open a generated_file in the same directory as the parent.
1240 static struct file *open_same_dir_generated_file( const struct makefile *make,
1241 const struct incl_file *parent, struct incl_file *file,
1242 const char *ext, const char *src_ext )
1244 char *filename;
1245 struct file *ret = NULL;
1247 if (strendswith( file->name, ext ) &&
1248 (ret = open_file_same_dir( parent, replace_extension( file->name, ext, src_ext ), &filename )))
1250 file->sourcename = filename;
1251 file->filename = obj_dir_path( make, replace_filename( parent->name, file->name ));
1253 return ret;
1257 /*******************************************************************
1258 * open_local_file
1260 * Open a file in the source directory of the makefile.
1262 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1264 char *src_path = src_dir_path( make, path );
1265 struct file *ret = load_file( src_path );
1267 /* if not found, try parent dir */
1268 if (!ret && make->parent_dir)
1270 free( src_path );
1271 path = strmake( "%s/%s", make->parent_dir, path );
1272 src_path = src_dir_path( make, path );
1273 ret = load_file( src_path );
1276 if (ret) *filename = src_path;
1277 return ret;
1281 /*******************************************************************
1282 * open_local_generated_file
1284 * Open a generated file in the directory of the makefile.
1286 static struct file *open_local_generated_file( const struct makefile *make, struct incl_file *file,
1287 const char *ext, const char *src_ext )
1289 struct incl_file *include;
1291 if (strendswith( file->name, ext ) &&
1292 (include = find_src_file( make, replace_extension( file->name, ext, src_ext ) )))
1294 file->sourcename = include->filename;
1295 file->filename = obj_dir_path( make, file->name );
1296 return include->file;
1298 return NULL;
1302 /*******************************************************************
1303 * open_global_file
1305 * Open a file in the top-level source directory.
1307 static struct file *open_global_file( const char *path, char **filename )
1309 char *src_path = root_src_dir_path( path );
1310 struct file *ret = load_file( src_path );
1312 if (ret) *filename = src_path;
1313 return ret;
1317 /*******************************************************************
1318 * open_global_header
1320 * Open a file in the global include source directory.
1322 static struct file *open_global_header( const char *path, char **filename )
1324 struct incl_file *include = find_src_file( include_makefile, path );
1326 if (!include) return NULL;
1327 *filename = include->filename;
1328 return include->file;
1332 /*******************************************************************
1333 * open_global_generated_file
1335 * Open a generated file in the top-level source directory.
1337 static struct file *open_global_generated_file( const struct makefile *make, struct incl_file *file,
1338 const char *ext, const char *src_ext )
1340 struct incl_file *include;
1342 if (strendswith( file->name, ext ) &&
1343 (include = find_src_file( include_makefile, replace_extension( file->name, ext, src_ext ) )))
1345 file->sourcename = include->filename;
1346 file->filename = strmake( "include/%s", file->name );
1347 return include->file;
1349 return NULL;
1353 /*******************************************************************
1354 * open_src_file
1356 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1358 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1360 if (!file) fatal_perror( "open %s", pFile->name );
1361 return file;
1365 /*******************************************************************
1366 * find_importlib_module
1368 static struct makefile *find_importlib_module( const char *name )
1370 unsigned int i, len;
1372 for (i = 0; i < subdirs.count; i++)
1374 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1375 len = strlen(submakes[i]->obj_dir);
1376 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1377 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1379 return NULL;
1383 /*******************************************************************
1384 * open_include_file
1386 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1388 struct file *file = NULL;
1389 unsigned int i, len;
1391 errno = ENOENT;
1393 /* check for generated files */
1394 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1395 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1396 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1397 if (convert && rsvg && icotool)
1399 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1400 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1401 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1404 /* check for extra targets */
1405 if (strarray_exists( &make->extra_targets, pFile->name ))
1407 pFile->sourcename = src_dir_path( make, pFile->name );
1408 pFile->filename = obj_dir_path( make, pFile->name );
1409 return NULL;
1412 /* now try in source dir */
1413 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1415 /* check for global importlib (module dependency) */
1416 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1418 pFile->filename = pFile->name;
1419 return NULL;
1422 /* check for generated files in global includes */
1423 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1424 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1425 if (strendswith( pFile->name, "tmpl.h" ) &&
1426 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1428 /* check in global includes source dir */
1429 if ((file = open_global_header( pFile->name, &pFile->filename ))) return file;
1431 /* check in global msvcrt includes */
1432 if (pFile->use_msvcrt &&
1433 (file = open_global_header( strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1434 return file;
1436 /* now search in include paths */
1437 for (i = 0; i < make->include_paths.count; i++)
1439 const char *dir = make->include_paths.str[i];
1441 if (root_src_dir)
1443 len = strlen( root_src_dir );
1444 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1446 while (dir[len] == '/') len++;
1447 file = open_global_file( concat_paths( dir + len, pFile->name ), &pFile->filename );
1450 else
1452 if (*dir == '/') continue;
1453 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1455 if (!file) continue;
1456 pFile->is_external = 1;
1457 return file;
1460 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1462 /* try in src file directory */
1463 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1464 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1465 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1467 pFile->is_external = pFile->included_by->is_external;
1468 return file;
1471 if (make->extlib) return NULL; /* ignore missing files in external libs */
1473 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1474 perror( pFile->name );
1475 pFile = pFile->included_by;
1476 while (pFile && pFile->included_by)
1478 const char *parent = pFile->included_by->sourcename;
1479 if (!parent) parent = pFile->included_by->file->name;
1480 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1481 parent, pFile->included_line, pFile->name );
1482 pFile = pFile->included_by;
1484 exit(1);
1488 /*******************************************************************
1489 * add_all_includes
1491 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1493 unsigned int i;
1495 for (i = 0; i < file->deps_count; i++)
1497 switch (file->deps[i].type)
1499 case INCL_NORMAL:
1500 case INCL_IMPORT:
1501 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1502 break;
1503 case INCL_IMPORTLIB:
1504 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1505 break;
1506 case INCL_SYSTEM:
1507 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1508 break;
1509 case INCL_CPP_QUOTE:
1510 case INCL_CPP_QUOTE_SYSTEM:
1511 break;
1517 /*******************************************************************
1518 * parse_file
1520 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1522 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1524 if (!file) return;
1526 source->file = file;
1527 source->files_count = 0;
1528 source->files_size = file->deps_count;
1529 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1531 if (strendswith( file->name, ".m" )) file->flags |= FLAG_C_UNIX;
1532 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1533 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1535 if (source->sourcename)
1537 if (strendswith( source->sourcename, ".idl" ))
1539 unsigned int i;
1541 /* generated .h file always includes these */
1542 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1543 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1544 for (i = 0; i < file->deps_count; i++)
1546 switch (file->deps[i].type)
1548 case INCL_IMPORT:
1549 if (strendswith( file->deps[i].name, ".idl" ))
1550 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1551 file->deps[i].line, INCL_NORMAL );
1552 else
1553 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1554 break;
1555 case INCL_CPP_QUOTE:
1556 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1557 break;
1558 case INCL_CPP_QUOTE_SYSTEM:
1559 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1560 break;
1561 case INCL_NORMAL:
1562 case INCL_SYSTEM:
1563 case INCL_IMPORTLIB:
1564 break;
1567 return;
1569 if (strendswith( source->sourcename, ".y" ))
1570 return; /* generated .tab.h doesn't include anything */
1573 add_all_includes( make, source, file );
1577 /*******************************************************************
1578 * add_src_file
1580 * Add a source file to the list.
1582 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1584 struct incl_file *file = xmalloc( sizeof(*file) );
1586 memset( file, 0, sizeof(*file) );
1587 file->name = xstrdup(name);
1588 file->use_msvcrt = make->use_msvcrt;
1589 file->is_external = !!make->extlib;
1590 list_add_tail( &make->sources, &file->entry );
1591 if (make == include_makefile)
1593 unsigned int hash = hash_filename( name );
1594 list_add_tail( &global_includes[hash], &file->hash_entry );
1596 parse_file( make, file, 1 );
1597 return file;
1601 /*******************************************************************
1602 * open_input_makefile
1604 static FILE *open_input_makefile( const struct makefile *make )
1606 FILE *ret;
1608 if (make->obj_dir)
1609 input_file_name = root_src_dir_path( obj_dir_path( make, "Makefile.in" ));
1610 else
1611 input_file_name = output_makefile_name; /* always use output name for main Makefile */
1613 input_line = 0;
1614 if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
1615 return ret;
1619 /*******************************************************************
1620 * get_make_variable
1622 static const char *get_make_variable( const struct makefile *make, const char *name )
1624 const char *ret;
1626 if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
1627 if ((ret = strarray_get_value( &make->vars, name ))) return ret;
1628 if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
1629 return NULL;
1633 /*******************************************************************
1634 * get_expanded_make_variable
1636 static char *get_expanded_make_variable( const struct makefile *make, const char *name )
1638 const char *var;
1639 char *p, *end, *expand, *tmp;
1641 var = get_make_variable( make, name );
1642 if (!var) return NULL;
1644 p = expand = xstrdup( var );
1645 while ((p = strchr( p, '$' )))
1647 if (p[1] == '(')
1649 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1650 *end++ = 0;
1651 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1652 var = get_make_variable( make, p + 2 );
1653 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1654 /* switch to the new string */
1655 p = tmp + (p - expand);
1656 free( expand );
1657 expand = tmp;
1659 else if (p[1] == '{') /* don't expand ${} variables */
1661 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1662 p = end + 1;
1664 else if (p[1] == '$')
1666 p += 2;
1668 else fatal_error( "syntax error in '%s'\n", expand );
1671 /* consider empty variables undefined */
1672 p = expand;
1673 while (*p && isspace(*p)) p++;
1674 if (*p) return expand;
1675 free( expand );
1676 return NULL;
1680 /*******************************************************************
1681 * get_expanded_make_var_array
1683 static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
1685 struct strarray ret = empty_strarray;
1686 char *value, *token;
1688 if ((value = get_expanded_make_variable( make, name )))
1689 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1690 strarray_add( &ret, token );
1691 return ret;
1695 /*******************************************************************
1696 * get_expanded_file_local_var
1698 static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
1699 const char *name )
1701 char *p, *var = strmake( "%s_%s", file, name );
1703 for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
1704 return get_expanded_make_var_array( make, var );
1708 /*******************************************************************
1709 * set_make_variable
1711 static int set_make_variable( struct strarray *array, const char *assignment )
1713 char *p, *name;
1715 p = name = xstrdup( assignment );
1716 while (isalnum(*p) || *p == '_') p++;
1717 if (name == p) return 0; /* not a variable */
1718 if (isspace(*p))
1720 *p++ = 0;
1721 while (isspace(*p)) p++;
1723 if (*p != '=') return 0; /* not an assignment */
1724 *p++ = 0;
1725 while (isspace(*p)) p++;
1727 strarray_set_value( array, name, p );
1728 return 1;
1732 /*******************************************************************
1733 * parse_makefile
1735 static struct makefile *parse_makefile( const char *path )
1737 char *buffer;
1738 FILE *file;
1739 struct makefile *make = xmalloc( sizeof(*make) );
1741 memset( make, 0, sizeof(*make) );
1742 make->obj_dir = path;
1743 if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
1744 if (path && !strcmp( path, "include" )) include_makefile = make;
1746 file = open_input_makefile( make );
1747 while ((buffer = get_line( file )))
1749 if (!strncmp( buffer, separator, strlen(separator) )) break;
1750 if (*buffer == '\t') continue; /* command */
1751 while (isspace( *buffer )) buffer++;
1752 if (*buffer == '#') continue; /* comment */
1753 set_make_variable( &make->vars, buffer );
1755 fclose( file );
1756 input_file_name = NULL;
1757 return make;
1761 /*******************************************************************
1762 * add_generated_sources
1764 static void add_generated_sources( struct makefile *make )
1766 unsigned int i;
1767 struct incl_file *source, *next, *file, *dlldata = NULL;
1768 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1770 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1772 if (source->file->flags & FLAG_IDL_CLIENT)
1774 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1775 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1776 add_all_includes( make, file, file->file );
1778 if (source->file->flags & FLAG_IDL_SERVER)
1780 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1781 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1782 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1783 add_all_includes( make, file, file->file );
1785 if (source->file->flags & FLAG_IDL_IDENT)
1787 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1788 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1789 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1790 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1791 add_all_includes( make, file, file->file );
1793 if (source->file->flags & FLAG_IDL_PROXY)
1795 if (!dlldata)
1797 dlldata = add_generated_source( make, "dlldata.o", "dlldata.c" );
1798 add_dependency( dlldata->file, "objbase.h", INCL_NORMAL );
1799 add_dependency( dlldata->file, "rpcproxy.h", INCL_NORMAL );
1800 add_all_includes( make, dlldata, dlldata->file );
1802 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1803 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1804 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1805 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1806 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1807 add_all_includes( make, file, file->file );
1809 if (source->file->flags & FLAG_IDL_TYPELIB)
1811 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL );
1813 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1815 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1817 if (source->file->flags & FLAG_IDL_REGISTER)
1819 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1821 if (source->file->flags & FLAG_IDL_HEADER)
1823 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1825 if (!source->file->flags && strendswith( source->name, ".idl" ))
1827 if (!strncmp( source->name, "wine/", 5 )) continue;
1828 source->file->flags = FLAG_IDL_HEADER | FLAG_INSTALL;
1829 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1831 if (strendswith( source->name, ".x" ))
1833 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1835 if (strendswith( source->name, ".y" ))
1837 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1838 /* steal the includes list from the source file */
1839 file->files_count = source->files_count;
1840 file->files_size = source->files_size;
1841 file->files = source->files;
1842 source->files_count = source->files_size = 0;
1843 source->files = NULL;
1845 if (strendswith( source->name, ".l" ))
1847 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1848 /* steal the includes list from the source file */
1849 file->files_count = source->files_count;
1850 file->files_size = source->files_size;
1851 file->files = source->files;
1852 source->files_count = source->files_size = 0;
1853 source->files = NULL;
1855 if (source->file->flags & FLAG_C_IMPLIB)
1857 if (!make->staticimplib && make->importlib && *dll_ext)
1858 make->staticimplib = strmake( "lib%s.a", make->importlib );
1860 if (strendswith( source->name, ".po" ))
1862 if (!make->disabled)
1863 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1865 if (strendswith( source->name, ".spec" ))
1867 char *obj = replace_extension( source->name, ".spec", "" );
1868 strarray_addall_uniq( &make->extra_imports,
1869 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1872 if (make->testdll)
1874 file = add_generated_source( make, "testlist.o", "testlist.c" );
1875 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1876 add_all_includes( make, file, file->file );
1878 for (i = 0; i < objs.count; i++)
1880 /* default to .c for unknown extra object files */
1881 if (strendswith( objs.str[i], ".o" ))
1883 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1884 file->file->flags |= FLAG_C_UNIX;
1885 file->use_msvcrt = 0;
1887 else if (strendswith( objs.str[i], ".res" ))
1888 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1889 else
1890 add_generated_source( make, objs.str[i], NULL );
1895 /*******************************************************************
1896 * create_dir
1898 static void create_dir( const char *dir )
1900 char *p, *path;
1902 p = path = xstrdup( dir );
1903 while ((p = strchr( p, '/' )))
1905 *p = 0;
1906 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1907 *p++ = '/';
1908 while (*p == '/') p++;
1910 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1911 free( path );
1915 /*******************************************************************
1916 * create_file_directories
1918 * Create the base directories of all the files.
1920 static void create_file_directories( const struct makefile *make, struct strarray files )
1922 struct strarray subdirs = empty_strarray;
1923 unsigned int i;
1924 char *dir;
1926 for (i = 0; i < files.count; i++)
1928 if (!strchr( files.str[i], '/' )) continue;
1929 dir = obj_dir_path( make, files.str[i] );
1930 *strrchr( dir, '/' ) = 0;
1931 strarray_add_uniq( &subdirs, dir );
1934 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1938 /*******************************************************************
1939 * output_filenames_obj_dir
1941 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1943 unsigned int i;
1945 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1949 /*******************************************************************
1950 * get_dependencies
1952 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1954 unsigned int i;
1956 if (!file->filename) return;
1958 if (file != source)
1960 if (file->owner == source) return; /* already processed */
1961 if (file->type == INCL_IMPORTLIB)
1963 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1964 return; /* library is imported only when building a typelib */
1965 strarray_add( &source->importlibdeps, file->filename );
1967 else strarray_add( &source->dependencies, file->filename );
1968 file->owner = source;
1970 /* sanity checks */
1971 if (!strcmp( file->filename, "include/config.h" ) &&
1972 file != source->files[0] && !source->is_external)
1974 input_file_name = source->filename;
1975 input_line = 0;
1976 for (i = 0; i < source->file->deps_count; i++)
1978 if (!strcmp( source->file->deps[i].name, file->name ))
1979 input_line = source->file->deps[i].line;
1981 fatal_error( "%s must be included before other headers\n", file->name );
1985 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
1989 /*******************************************************************
1990 * get_local_dependencies
1992 * Get the local dependencies of a given target.
1994 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1995 struct strarray targets )
1997 unsigned int i;
1998 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
2000 for (i = 0; i < deps.count; i++)
2002 if (strarray_exists( &targets, deps.str[i] ))
2003 deps.str[i] = obj_dir_path( make, deps.str[i] );
2004 else
2005 deps.str[i] = src_dir_path( make, deps.str[i] );
2007 return deps;
2011 /*******************************************************************
2012 * get_static_lib
2014 * Check if makefile builds the named static library and return the full lib path.
2016 static const char *get_static_lib( const struct makefile *make, const char *name )
2018 if (!make->staticlib) return NULL;
2019 if (make->disabled) return NULL;
2020 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2021 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2022 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2023 return obj_dir_path( make, make->staticlib );
2027 /*******************************************************************
2028 * get_native_unix_lib
2030 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2032 if (!make->native_unix_lib) return NULL;
2033 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2034 if (make->unixlib[strlen(name)] != '.') return NULL;
2035 return obj_dir_path( make, make->unixlib );
2039 /*******************************************************************
2040 * get_parent_makefile
2042 static struct makefile *get_parent_makefile( struct makefile *make )
2044 char *dir, *p;
2045 unsigned int i;
2047 if (!make->obj_dir) return NULL;
2048 dir = xstrdup( make->obj_dir );
2049 if (!(p = strrchr( dir, '/' ))) return NULL;
2050 *p = 0;
2051 for (i = 0; i < subdirs.count; i++)
2052 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2053 return NULL;
2057 /*******************************************************************
2058 * needs_delay_lib
2060 static int needs_delay_lib( const struct makefile *make )
2062 if (delay_load_flag) return 0;
2063 if (*dll_ext && !crosstarget) return 0;
2064 if (!make->importlib) return 0;
2065 return strarray_exists( &delay_import_libs, make->importlib );
2069 /*******************************************************************
2070 * add_unix_libraries
2072 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2074 struct strarray ret = empty_strarray;
2075 struct strarray all_libs = empty_strarray;
2076 unsigned int i, j;
2078 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2079 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
2081 for (i = 0; i < all_libs.count; i++)
2083 const char *lib = NULL;
2085 if (!strncmp( all_libs.str[i], "-l", 2 ))
2087 for (j = 0; j < subdirs.count; j++)
2089 if (make == submakes[j]) continue;
2090 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2093 if (lib)
2095 strarray_add( deps, lib );
2096 strarray_add( &ret, lib );
2098 else strarray_add( &ret, all_libs.str[i] );
2101 strarray_addall( &ret, libs );
2102 return ret;
2106 /*******************************************************************
2107 * is_crt_module
2109 static int is_crt_module( const char *file )
2111 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2115 /*******************************************************************
2116 * get_default_crt
2118 static const char *get_default_crt( const struct makefile *make )
2120 if (!make->use_msvcrt) return NULL;
2121 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2122 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2126 /*******************************************************************
2127 * get_crt_define
2129 static const char *get_crt_define( const struct makefile *make )
2131 const char *crt_dll = NULL;
2132 unsigned int i, version = 0;
2134 for (i = 0; i < make->imports.count; i++)
2136 if (!is_crt_module( make->imports.str[i] )) continue;
2137 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2138 crt_dll, make->imports.str[i] );
2139 crt_dll = make->imports.str[i];
2142 if (!crt_dll)
2144 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2145 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2147 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2148 sscanf( crt_dll, "msvcr%u", &version );
2149 return strmake( "-D_MSVCR_VER=%u", version );
2153 /*******************************************************************
2154 * get_default_imports
2156 static struct strarray get_default_imports( const struct makefile *make, struct strarray imports )
2158 struct strarray ret = empty_strarray;
2159 const char *crt_dll = get_default_crt( make );
2160 unsigned int i;
2162 for (i = 0; i < imports.count; i++)
2163 if (is_crt_module( imports.str[i] ))
2164 crt_dll = imports.str[i];
2166 strarray_add( &ret, "winecrt0" );
2167 if (crt_dll) strarray_add( &ret, crt_dll );
2169 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2170 strarray_add( &ret, "kernel" );
2172 strarray_add( &ret, "kernel32" );
2173 strarray_add( &ret, "ntdll" );
2174 return ret;
2177 enum import_type
2179 IMPORT_TYPE_DIRECT,
2180 IMPORT_TYPE_DELAYED,
2181 IMPORT_TYPE_DEFAULT,
2184 /*******************************************************************
2185 * add_import_libs
2187 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2188 struct strarray imports, enum import_type type, int is_cross )
2190 struct strarray ret = empty_strarray;
2191 unsigned int i, j;
2193 for (i = 0; i < imports.count; i++)
2195 const char *name = imports.str[i];
2196 const char *lib = NULL;
2198 /* add crt import lib only when adding the default imports libs */
2199 if (is_crt_module( imports.str[i] ) && type != IMPORT_TYPE_DEFAULT) continue;
2201 if (name[0] == '-')
2203 switch (name[1])
2205 case 'L': strarray_add( &ret, name ); continue;
2206 case 'l': name += 2; break;
2207 default: continue;
2210 else name = get_base_name( name );
2212 for (j = 0; j < subdirs.count; j++)
2214 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2215 lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
2216 else
2217 lib = get_static_lib( submakes[j], name );
2218 if (lib) break;
2221 if (lib)
2223 const char *ext = NULL;
2225 if (type == IMPORT_TYPE_DELAYED && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
2226 else if (is_cross) ext = ".cross.a";
2227 if (ext) lib = replace_extension( lib, ".a", ext );
2228 strarray_add_uniq( deps, lib );
2229 strarray_add( &ret, lib );
2231 else strarray_add( &ret, strmake( "-l%s", name ));
2233 return ret;
2237 /*******************************************************************
2238 * add_install_rule
2240 static void add_install_rule( struct makefile *make, const char *target,
2241 const char *file, const char *dest )
2243 unsigned int i;
2245 for (i = 0; i < NB_INSTALL_RULES; i++)
2247 if (strarray_exists( &make->install[i], target ) ||
2248 strarray_exists( &top_install[i], make->obj_dir ) ||
2249 strarray_exists( &top_install[i], obj_dir_path( make, target )))
2251 strarray_add( &make->install_rules[i], file );
2252 strarray_add( &make->install_rules[i], dest );
2253 break;
2259 /*******************************************************************
2260 * get_include_install_path
2262 * Determine the installation path for a given include file.
2264 static const char *get_include_install_path( const char *name )
2266 if (!strncmp( name, "wine/", 5 )) return name + 5;
2267 if (!strncmp( name, "msvcrt/", 7 )) return name;
2268 return strmake( "windows/%s", name );
2272 /*******************************************************************
2273 * get_shared_library_name
2275 * Determine possible names for a shared library with a version number.
2277 static struct strarray get_shared_lib_names( const char *libname )
2279 struct strarray ret = empty_strarray;
2280 const char *ext, *p;
2281 char *name, *first, *second;
2282 size_t len = 0;
2284 strarray_add( &ret, libname );
2286 for (p = libname; (p = strchr( p, '.' )); p++)
2287 if ((len = strspn( p + 1, "0123456789." ))) break;
2289 if (!len) return ret;
2290 ext = p + 1 + len;
2291 if (*ext && ext[-1] == '.') ext--;
2293 /* keep only the first group of digits */
2294 name = xstrdup( libname );
2295 first = name + (p - libname);
2296 if ((second = strchr( first + 1, '.' )))
2298 strcpy( second, ext );
2299 strarray_add( &ret, xstrdup( name ));
2301 return ret;
2305 /*******************************************************************
2306 * get_source_defines
2308 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2309 const char *obj )
2311 unsigned int i;
2312 struct strarray ret = empty_strarray;
2314 strarray_addall( &ret, make->include_args );
2315 if (source->use_msvcrt)
2317 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2318 for (i = 0; i < make->include_paths.count; i++)
2319 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2321 strarray_addall( &ret, make->define_args );
2322 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2323 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2324 return ret;
2328 /*******************************************************************
2329 * remove_warning_flags
2331 static struct strarray remove_warning_flags( struct strarray flags )
2333 unsigned int i;
2334 struct strarray ret = empty_strarray;
2336 for (i = 0; i < flags.count; i++)
2337 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2338 strarray_add( &ret, flags.str[i] );
2339 return ret;
2343 /*******************************************************************
2344 * get_debug_file
2346 static const char *get_debug_file( struct makefile *make, const char *name )
2348 const char *debug_file = NULL;
2349 if (!make->is_cross || !crossdebug) return NULL;
2350 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2351 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2352 if (debug_file) strarray_add( &make->debug_files, debug_file );
2353 return debug_file;
2357 /*******************************************************************
2358 * cmd_prefix
2360 static const char *cmd_prefix( const char *cmd )
2362 if (!silent_rules) return "";
2363 return strmake( "$(quiet_%s)", cmd );
2367 /*******************************************************************
2368 * output_winegcc_command
2370 static void output_winegcc_command( struct makefile *make, int is_cross )
2372 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2373 output_filename( "--wine-objdir ." );
2374 if (tools_dir)
2376 output_filename( "--winebuild" );
2377 output_filename( tools_path( make, "winebuild" ));
2379 if (is_cross)
2381 output_filename( "-b" );
2382 output_filename( crosstarget );
2383 output_filename( "--lib-suffix=.cross.a" );
2385 else
2387 output_filenames( target_flags );
2388 output_filenames( lddll_flags );
2393 /*******************************************************************
2394 * output_symlink_rule
2396 * Output a rule to create a symlink.
2398 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2400 const char *name = strrchr( link_name, '/' );
2401 char *dir = NULL;
2403 if (name)
2405 dir = xstrdup( link_name );
2406 dir[name - link_name] = 0;
2409 output( "\t%s", cmd_prefix( "LN" ));
2410 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2411 output( "rm -f %s && ", link_name );
2413 /* dest path with a directory needs special handling if ln -s isn't supported */
2414 if (dir && strcmp( ln_s, "ln -s" ))
2415 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2416 else
2417 output( "%s %s %s\n", ln_s, src_name, link_name );
2419 free( dir );
2423 /*******************************************************************
2424 * output_srcdir_symlink
2426 * Output rule to create a symlink back to the source directory, for source files
2427 * that are needed at run-time.
2429 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2431 char *src_file, *dst_file, *src_name;
2433 if (!make->src_dir) return;
2434 src_file = src_dir_path( make, obj );
2435 dst_file = obj_dir_path( make, obj );
2436 output( "%s: %s\n", dst_file, src_file );
2438 src_name = src_file;
2439 if (src_name[0] != '/' && make->obj_dir)
2440 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2442 output_symlink_rule( src_name, dst_file, 0 );
2443 strarray_add( &make->all_targets, obj );
2447 /*******************************************************************
2448 * output_install_commands
2450 static void output_install_commands( struct makefile *make, struct strarray files )
2452 unsigned int i;
2453 char *install_sh = root_src_dir_path( "tools/install-sh" );
2455 for (i = 0; i < files.count; i += 2)
2457 const char *file = files.str[i];
2458 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2460 switch (*files.str[i + 1])
2462 case 'c': /* cross-compiled program */
2463 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2464 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2465 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2466 break;
2467 case 'd': /* data file */
2468 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2469 install_sh, obj_dir_path( make, file ), dest );
2470 break;
2471 case 'D': /* data file in source dir */
2472 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2473 install_sh, src_dir_path( make, file ), dest );
2474 break;
2475 case 'p': /* program file */
2476 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2477 install_sh, obj_dir_path( make, file ), dest );
2478 break;
2479 case 's': /* script */
2480 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2481 install_sh, obj_dir_path( make, file ), dest );
2482 break;
2483 case 'S': /* script in source dir */
2484 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2485 install_sh, src_dir_path( make, file ), dest );
2486 break;
2487 case 't': /* script in tools dir */
2488 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2489 install_sh, tools_dir_path( make, files.str[i] ), dest );
2490 break;
2491 case 'y': /* symlink */
2492 output_symlink_rule( files.str[i], dest, 1 );
2493 break;
2494 default:
2495 assert(0);
2497 strarray_add( &make->uninstall_files, dest );
2502 /*******************************************************************
2503 * output_install_rules
2505 * Rules are stored as a (file,dest) pair of values.
2506 * The first char of dest indicates the type of install.
2508 static void output_install_rules( struct makefile *make, enum install_rules rules )
2510 unsigned int i;
2511 struct strarray files = make->install_rules[rules];
2512 struct strarray targets = empty_strarray;
2514 if (!files.count) return;
2516 for (i = 0; i < files.count; i += 2)
2518 const char *file = files.str[i];
2519 switch (*files.str[i + 1])
2521 case 'c': /* cross-compiled program */
2522 case 'd': /* data file */
2523 case 'p': /* program file */
2524 case 's': /* script */
2525 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2526 break;
2527 case 't': /* script in tools dir */
2528 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2529 break;
2533 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, install_targets[rules] ));
2534 output_filenames( targets );
2535 output( "\n" );
2536 output_install_commands( make, files );
2537 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2538 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, install_targets[rules] ));
2542 static int cmp_string_length( const char **a, const char **b )
2544 int paths_a = 0, paths_b = 0;
2545 const char *p;
2547 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2548 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2549 if (paths_b != paths_a) return paths_b - paths_a;
2550 return strcmp( *a, *b );
2553 /*******************************************************************
2554 * output_uninstall_rules
2556 static void output_uninstall_rules( struct makefile *make )
2558 static const char *dirs_order[] =
2559 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2561 struct strarray uninstall_dirs = empty_strarray;
2562 unsigned int i, j;
2564 if (!make->uninstall_files.count) return;
2565 output( "uninstall::\n" );
2566 output_rm_filenames( make->uninstall_files );
2567 strarray_add_uniq( &make->phony_targets, "uninstall" );
2569 if (!subdirs.count) return;
2570 for (i = 0; i < make->uninstall_files.count; i++)
2572 char *dir = xstrdup( make->uninstall_files.str[i] );
2573 while (strchr( dir, '/' ))
2575 *strrchr( dir, '/' ) = 0;
2576 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2579 strarray_qsort( &uninstall_dirs, cmp_string_length );
2580 output( "\t-rmdir" );
2581 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2583 for (j = 0; j < uninstall_dirs.count; j++)
2585 if (!uninstall_dirs.str[j]) continue;
2586 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2587 continue;
2588 output_filename( uninstall_dirs.str[j] );
2589 uninstall_dirs.str[j] = NULL;
2592 for (j = 0; j < uninstall_dirs.count; j++)
2593 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2594 output( "\n" );
2598 /*******************************************************************
2599 * output_po_files
2601 static void output_po_files( struct makefile *make )
2603 const char *po_dir = src_dir_path( make, "po" );
2604 unsigned int i;
2606 if (linguas.count)
2608 for (i = 0; i < linguas.count; i++)
2609 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2610 output( ": %s/wine.pot\n", po_dir );
2611 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2612 cmd_prefix( "MSG" ), po_dir );
2613 output( "po/all:" );
2614 for (i = 0; i < linguas.count; i++)
2615 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2616 output( "\n" );
2618 output( "%s/wine.pot:", po_dir );
2619 output_filenames( make->pot_files );
2620 output( "\n" );
2621 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2622 output_filenames( make->pot_files );
2623 output( "\n" );
2624 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2628 /*******************************************************************
2629 * output_source_y
2631 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2633 /* add source file dependency for parallel makes */
2634 char *header = strmake( "%s.tab.h", obj );
2636 if (find_include_file( make, header ))
2638 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2639 output( "\t%s%s -o %s.tab.c -d %s\n",
2640 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2641 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2642 source->filename, obj_dir_path( make, header ));
2643 strarray_add( &make->clean_files, header );
2645 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2647 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2651 /*******************************************************************
2652 * output_source_l
2654 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2656 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2657 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2661 /*******************************************************************
2662 * output_source_h
2664 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2666 if (source->file->flags & FLAG_GENERATED)
2667 strarray_add( &make->all_targets, source->name );
2668 else if ((source->file->flags & FLAG_INSTALL) || strncmp( source->name, "wine/", 5 ))
2669 add_install_rule( make, source->name, source->name,
2670 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2674 /*******************************************************************
2675 * output_source_rc
2677 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2679 struct strarray defines = get_source_defines( make, source, obj );
2680 const char *po_dir = NULL;
2681 unsigned int i;
2683 if (source->file->flags & FLAG_RC_HEADER) return;
2684 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2685 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2686 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2687 if (source->file->flags & FLAG_RC_PO)
2689 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2690 output( "%s.pot ", obj_dir_path( make, obj ) );
2692 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2693 output_filename( tools_path( make, "wrc" ));
2694 if (make->src_dir) output_filename( "nls/locale.nls" );
2695 output_filenames( source->dependencies );
2696 output( "\n" );
2697 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2698 if (make->is_win16) output_filename( "-m16" );
2699 output_filename( "--nostdinc" );
2700 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2701 output_filenames( defines );
2702 output_filename( source->filename );
2703 output( "\n" );
2704 if (po_dir)
2706 output( "%s.res:", obj_dir_path( make, obj ));
2707 for (i = 0; i < linguas.count; i++)
2708 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2709 output( "\n" );
2714 /*******************************************************************
2715 * output_source_mc
2717 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2719 unsigned int i;
2720 char *obj_path = obj_dir_path( make, obj );
2722 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2723 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2724 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2725 output_filename( tools_path( make, "wmc" ));
2726 output_filenames( source->dependencies );
2727 output( "\n" );
2728 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2729 if (linguas.count)
2731 output_filename( "--po-dir=po" );
2732 output( "\n" );
2733 output( "%s.res:", obj_dir_path( make, obj ));
2734 for (i = 0; i < linguas.count; i++)
2735 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2737 output( "\n" );
2741 /*******************************************************************
2742 * output_source_res
2744 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2746 strarray_add( &make->res_files, source->name );
2750 /*******************************************************************
2751 * output_source_idl
2753 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2755 struct strarray defines = get_source_defines( make, source, obj );
2756 struct strarray targets = empty_strarray;
2757 char *dest;
2758 unsigned int i;
2760 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2761 if (!source->file->flags) return;
2763 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2765 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2766 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2767 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2768 strarray_add( &targets, dest );
2770 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2771 if (source->file->flags & FLAG_INSTALL)
2773 add_install_rule( make, source->name, xstrdup( source->name ),
2774 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2775 if (source->file->flags & FLAG_IDL_HEADER)
2776 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2777 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2779 if (!targets.count) return;
2781 output_filenames_obj_dir( make, targets );
2782 output( ": %s\n", tools_path( make, "widl" ));
2783 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2784 output_filenames( target_flags );
2785 output_filename( "--nostdinc" );
2786 output_filename( "-Ldlls/\\*" );
2787 output_filenames( defines );
2788 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2789 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2790 output_filename( source->filename );
2791 output( "\n" );
2792 output_filenames_obj_dir( make, targets );
2793 output( ": %s", source->filename );
2794 output_filenames( source->dependencies );
2795 for (i = 0; i < source->importlibdeps.count; i++)
2797 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2798 output_filename( obj_dir_path( submake, submake->module ));
2800 output( "\n" );
2804 /*******************************************************************
2805 * output_source_x
2807 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2809 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2810 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2811 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2812 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2813 if (source->file->flags & FLAG_INSTALL)
2815 add_install_rule( make, source->name, source->name,
2816 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2817 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2818 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2823 /*******************************************************************
2824 * output_source_sfd
2826 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2828 unsigned int i;
2829 char *ttf_obj = strmake( "%s.ttf", obj );
2830 char *ttf_file = src_dir_path( make, ttf_obj );
2832 if (fontforge && !make->src_dir)
2834 output( "%s: %s\n", ttf_file, source->filename );
2835 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2836 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2837 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2838 strarray_add( &make->maintainerclean_files, ttf_obj );
2840 if (source->file->flags & FLAG_INSTALL)
2842 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2843 output_srcdir_symlink( make, ttf_obj );
2846 if (source->file->flags & FLAG_SFD_FONTS)
2848 struct strarray *array = source->file->args;
2850 for (i = 0; i < array->count; i++)
2852 char *font = strtok( xstrdup(array->str[i]), " \t" );
2853 char *args = strtok( NULL, "" );
2855 strarray_add( &make->all_targets, xstrdup( font ));
2856 output( "%s: %s %s\n", obj_dir_path( make, font ),
2857 tools_path( make, "sfnt2fon" ), ttf_file );
2858 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2859 tools_path( make, "sfnt2fon" ), ttf_file, args );
2860 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2866 /*******************************************************************
2867 * output_source_svg
2869 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2871 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2872 unsigned int i;
2874 if (convert && rsvg && icotool)
2876 for (i = 0; images[i]; i++)
2877 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2879 if (images[i])
2881 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2882 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2883 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2884 root_src_dir_path( "tools/buildimage" ), source->filename );
2885 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2891 /*******************************************************************
2892 * output_source_nls
2894 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2896 add_install_rule( make, source->name, source->name,
2897 strmake( "D$(nlsdir)/%s", source->name ));
2898 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2902 /*******************************************************************
2903 * output_source_desktop
2905 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2907 add_install_rule( make, source->name, source->name,
2908 strmake( "D$(datadir)/applications/%s", source->name ));
2912 /*******************************************************************
2913 * output_source_po
2915 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2917 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2918 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
2919 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2923 /*******************************************************************
2924 * output_source_in
2926 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2928 unsigned int i;
2930 if (make == include_makefile) return; /* ignore generated includes */
2931 if (strendswith( obj, ".man" ) && source->file->args)
2933 struct strarray symlinks;
2934 char *dir, *dest = replace_extension( obj, ".man", "" );
2935 char *lang = strchr( dest, '.' );
2936 char *section = source->file->args;
2937 if (lang)
2939 *lang++ = 0;
2940 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2942 else dir = strmake( "$(mandir)/man%s", section );
2943 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2944 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2945 for (i = 0; i < symlinks.count; i++)
2946 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2947 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2948 free( dest );
2949 free( dir );
2951 strarray_add( &make->in_files, xstrdup(obj) );
2952 strarray_add( &make->all_targets, xstrdup(obj) );
2953 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2954 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
2955 output( "%s:", obj_dir_path( make, obj ));
2956 output_filenames( source->dependencies );
2957 output( "\n" );
2958 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2962 /*******************************************************************
2963 * output_source_spec
2965 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2967 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2968 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
2969 struct strarray all_libs = empty_strarray, dep_libs = empty_strarray;
2970 struct strarray default_imports = empty_strarray;
2971 char *dll_name, *obj_name, *output_file;
2972 const char *debug_file;
2974 if (!imports.count) imports = make->imports;
2975 if (!dll_flags.count) dll_flags = make->extradllflags;
2976 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
2978 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, make->is_cross ) );
2979 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, make->is_cross ) );
2981 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
2982 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
2983 output_file = obj_dir_path( make, dll_name );
2985 strarray_add( &make->clean_files, dll_name );
2986 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2987 output( "%s.res:", obj_dir_path( make, obj ));
2988 output_filename( obj_dir_path( make, dll_name ));
2989 output_filename( tools_path( make, "wrc" ));
2990 output( "\n" );
2991 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
2992 tools_path( make, "wrc" ));
2994 output( "%s:", output_file);
2995 output_filename( source->filename );
2996 output_filename( obj_name );
2997 output_filenames( dep_libs );
2998 output_filename( tools_path( make, "winebuild" ));
2999 output_filename( tools_path( make, "winegcc" ));
3000 output( "\n" );
3001 output_winegcc_command( make, make->is_cross );
3002 output_filename( "-s" );
3003 output_filenames( dll_flags );
3004 output_filename( "-shared" );
3005 output_filename( source->filename );
3006 output_filename( obj_name );
3007 if ((debug_file = get_debug_file( make, dll_name )))
3008 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3009 output_filenames( all_libs );
3010 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3011 output( "\n" );
3015 /*******************************************************************
3016 * output_source_default
3018 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3020 struct strarray defines = get_source_defines( make, source, obj );
3021 int is_dll_src = (make->testdll &&
3022 strendswith( source->name, ".c" ) &&
3023 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3024 int need_cross = (crosstarget &&
3025 !(source->file->flags & FLAG_C_UNIX) &&
3026 (make->is_cross || make->staticlib ||
3027 (source->file->flags & FLAG_C_IMPLIB)));
3028 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
3029 (!need_cross ||
3030 (source->file->flags & FLAG_C_IMPLIB) ||
3031 (make->staticlib && !make->extlib)));
3033 if ((source->file->flags & FLAG_GENERATED) &&
3034 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3035 strarray_add( &make->clean_files, source->basename );
3037 if (need_obj)
3039 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3040 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3041 else if (source->file->flags & FLAG_C_IMPLIB)
3042 strarray_add( &make->implib_files, strmake( "%s.o", obj ));
3043 else if (!is_dll_src)
3044 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3045 else
3046 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3047 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3048 output( "\t%s$(CC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3049 output_filenames( defines );
3050 if (!source->use_msvcrt) output_filenames( make->unix_cflags );
3051 output_filenames( make->extlib ? extra_cflags_extlib : extra_cflags );
3052 if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
3054 output_filenames( unix_dllflags );
3056 else if (make->module || make->testdll)
3058 output_filenames( dll_flags );
3059 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3060 if (!*dll_ext && make->module && is_crt_module( make->module ))
3061 output_filename( "-fno-builtin" );
3063 output_filenames( cpp_flags );
3064 output_filename( "$(CFLAGS)" );
3065 output( "\n" );
3067 if (need_cross)
3069 if (source->file->flags & FLAG_C_IMPLIB)
3070 strarray_add( &make->crossimplib_files, strmake( "%s.cross.o", obj ));
3071 else if (!is_dll_src)
3072 strarray_add( &make->crossobj_files, strmake( "%s.cross.o", obj ));
3073 else
3074 strarray_add( &make->clean_files, strmake( "%s.cross.o", obj ));
3075 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3076 output( "\t%s$(CROSSCC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3077 output_filenames( defines );
3078 output_filenames( make->extlib ? extra_cross_cflags_extlib : extra_cross_cflags );
3079 if (make->module && is_crt_module( make->module ))
3080 output_filename( "-fno-builtin" );
3081 /* force -Wformat when using 'long' types, until all modules have been converted
3082 * and we can remove -Wno-format */
3083 if (!make->extlib && strarray_exists( &extra_cross_cflags, "-Wno-format" ) &&
3084 !strarray_exists( &defines, "-DWINE_NO_LONG_TYPES" ))
3085 output_filename( "-Wformat" );
3086 output_filenames( cpp_flags );
3087 output_filename( "$(CROSSCFLAGS)" );
3088 output( "\n" );
3090 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3091 !(source->file->flags & FLAG_GENERATED))
3093 strarray_add( &make->test_files, obj );
3094 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3095 output( "%s.ok:\n", obj_dir_path( make, obj ));
3096 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3097 cmd_prefix( "TEST" ),
3098 root_src_dir_path( "tools/runtest" ), make->testdll,
3099 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3100 make->is_cross ? "" : dll_ext, obj );
3102 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3103 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3104 output( ":" );
3105 output_filenames( source->dependencies );
3106 output( "\n" );
3110 /* dispatch table to output rules for a single source file */
3111 static const struct
3113 const char *ext;
3114 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3115 } output_source_funcs[] =
3117 { "y", output_source_y },
3118 { "l", output_source_l },
3119 { "h", output_source_h },
3120 { "rh", output_source_h },
3121 { "inl", output_source_h },
3122 { "rc", output_source_rc },
3123 { "mc", output_source_mc },
3124 { "res", output_source_res },
3125 { "idl", output_source_idl },
3126 { "sfd", output_source_sfd },
3127 { "svg", output_source_svg },
3128 { "nls", output_source_nls },
3129 { "desktop", output_source_desktop },
3130 { "po", output_source_po },
3131 { "in", output_source_in },
3132 { "x", output_source_x },
3133 { "spec", output_source_spec },
3134 { NULL, output_source_default }
3138 /*******************************************************************
3139 * get_unix_lib_name
3141 static char *get_unix_lib_name( struct makefile *make )
3143 struct incl_file *source;
3145 if (!*dll_ext) return NULL;
3146 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3148 if (!(source->file->flags & FLAG_C_UNIX)) continue;
3149 return strmake( "%s%s", get_base_name( make->module ), dll_ext );
3151 return NULL;
3155 /*******************************************************************
3156 * output_module
3158 static void output_module( struct makefile *make )
3160 struct strarray default_imports = empty_strarray;
3161 struct strarray all_libs = empty_strarray;
3162 struct strarray dep_libs = empty_strarray;
3163 struct strarray imports = make->imports;
3164 const char *module_name = make->module;
3165 const char *debug_file;
3166 const char *delay_load = delay_load_flag;
3167 char *spec_file = NULL;
3168 unsigned int i;
3170 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3172 if (!make->data_only)
3174 if (*dll_ext && !make->is_cross && !make->data_only)
3175 module_name = strmake( "%s%s", make->module, dll_ext );
3177 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" )) default_imports = get_default_imports( make, imports );
3179 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, IMPORT_TYPE_DIRECT, make->is_cross ));
3180 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, IMPORT_TYPE_DELAYED, make->is_cross ));
3181 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, make->is_cross ) );
3183 if (!make->use_msvcrt)
3185 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3186 strarray_addall( &all_libs, libs );
3189 if (!make->is_cross && *dll_ext) delay_load = "-Wl,-delayload,";
3190 if (delay_load)
3192 for (i = 0; i < make->delayimports.count; i++)
3193 strarray_add( &all_libs, strmake( "%s%s%s", delay_load, make->delayimports.str[i],
3194 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3197 strarray_add( &make->all_targets, module_name );
3199 if (make->data_only)
3200 add_install_rule( make, make->module, module_name,
3201 strmake( "d%s/%s", *dll_ext ? pe_dir : "$(dlldir)", module_name ));
3202 else if (make->is_cross)
3203 add_install_rule( make, make->module, module_name, strmake( "c%s/%s", pe_dir, module_name ));
3204 else if (*dll_ext)
3205 add_install_rule( make, make->module, module_name, strmake( "p%s/%s", so_dir, module_name ));
3206 else
3207 add_install_rule( make, make->module, module_name,
3208 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", module_name ));
3210 output( "%s:", obj_dir_path( make, module_name ));
3211 if (spec_file) output_filename( spec_file );
3212 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3213 output_filenames_obj_dir( make, make->res_files );
3214 output_filenames( dep_libs );
3215 output_filename( tools_path( make, "winebuild" ));
3216 output_filename( tools_path( make, "winegcc" ));
3217 output( "\n" );
3218 output_winegcc_command( make, make->is_cross );
3219 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3220 if (spec_file)
3222 output_filename( "-shared" );
3223 output_filename( spec_file );
3225 output_filenames( make->extradllflags );
3226 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3227 output_filenames_obj_dir( make, make->res_files );
3228 debug_file = get_debug_file( make, make->module );
3229 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3230 output_filenames( all_libs );
3231 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3232 output( "\n" );
3234 if (*dll_ext && make->is_exe && !make->is_win16 && strendswith( make->module, ".exe" ))
3236 char *binary = replace_extension( make->module, ".exe", "" );
3237 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3242 /*******************************************************************
3243 * output_fake_module
3245 static void output_fake_module( struct makefile *make )
3247 char *spec_file = NULL;
3249 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3251 strarray_add( &make->all_targets, make->module );
3252 add_install_rule( make, make->module, make->module, strmake( "d%s/%s", pe_dir, make->module ));
3254 output( "%s:", obj_dir_path( make, make->module ));
3255 if (spec_file) output_filename( spec_file );
3256 output_filenames_obj_dir( make, make->res_files );
3257 output_filename( tools_path( make, "winebuild" ));
3258 output_filename( tools_path( make, "winegcc" ));
3259 output( "\n" );
3260 output_winegcc_command( make, make->is_cross );
3261 output_filename( "-Wb,--fake-module" );
3262 if (spec_file)
3264 output_filename( "-shared" );
3265 output_filename( spec_file );
3267 output_filenames( make->extradllflags );
3268 output_filenames_obj_dir( make, make->res_files );
3269 output( "\n" );
3273 /*******************************************************************
3274 * output_import_lib
3276 static void output_import_lib( struct makefile *make )
3278 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3279 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3281 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3282 if (!*dll_ext && needs_delay_lib( make ))
3284 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3285 output( "%s.delay.a ", importlib_path );
3287 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3288 output_filenames_obj_dir( make, make->implib_files );
3289 output( "\n" );
3290 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3291 output_filenames( target_flags );
3292 if (make->is_win16) output_filename( "-m16" );
3293 output_filename( "--export" );
3294 output_filename( spec_file );
3295 output_filenames_obj_dir( make, make->implib_files );
3296 output( "\n" );
3297 add_install_rule( make, make->importlib,
3298 strmake( "lib%s.a", make->importlib ),
3299 strmake( "d%s/lib%s.a", so_dir, make->importlib ));
3301 if (crosstarget)
3303 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3304 output_filename( strmake( "%s.cross.a", importlib_path ));
3305 if (needs_delay_lib( make ))
3307 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3308 output_filename( strmake( "%s.delay.a", importlib_path ));
3310 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3311 output_filenames_obj_dir( make, make->crossimplib_files );
3312 output( "\n" );
3313 output( "\t%s%s -b %s -w --implib -o $@", cmd_prefix( "BUILD" ),
3314 tools_path( make, "winebuild" ), crosstarget );
3315 if (make->is_win16) output_filename( "-m16" );
3316 output_filename( "--export" );
3317 output_filename( spec_file );
3318 output_filenames_obj_dir( make, make->crossimplib_files );
3319 output( "\n" );
3320 add_install_rule( make, make->importlib,
3321 strmake( "lib%s.cross.a", make->importlib ),
3322 strmake( "d%s/lib%s.a", pe_dir, make->importlib ));
3327 /*******************************************************************
3328 * output_unix_lib
3330 static void output_unix_lib( struct makefile *make )
3332 struct strarray unix_libs = empty_strarray;
3333 struct strarray unix_deps = empty_strarray;
3334 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3336 if (!make->native_unix_lib)
3338 struct strarray unix_imports = empty_strarray;
3340 strarray_add( &unix_imports, "ntdll" );
3341 strarray_add( &unix_deps, obj_dir_path( top_makefile, "dlls/ntdll/ntdll.so" ));
3342 strarray_add( &unix_imports, "winecrt0" );
3343 if (spec_file) strarray_add( &unix_deps, spec_file );
3345 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, IMPORT_TYPE_DIRECT, 0 ));
3346 strarray_addall( &unix_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3347 strarray_addall( &unix_libs, libs );
3349 else unix_libs = add_unix_libraries( make, &unix_deps );
3351 strarray_add( &make->all_targets, make->unixlib );
3352 add_install_rule( make, make->module, make->unixlib, strmake( "p%s/%s", so_dir, make->unixlib ));
3353 output( "%s:", obj_dir_path( make, make->unixlib ));
3354 output_filenames_obj_dir( make, make->unixobj_files );
3355 output_filenames( unix_deps );
3357 if (make->native_unix_lib)
3359 output( "\n" );
3360 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3361 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3363 else
3365 output_filename( tools_path( make, "winebuild" ));
3366 output_filename( tools_path( make, "winegcc" ));
3367 output( "\n" );
3368 output_winegcc_command( make, 0 );
3369 output_filename( "-munix" );
3370 output_filename( "-shared" );
3371 if (spec_file) output_filename( spec_file );
3373 output_filenames_obj_dir( make, make->unixobj_files );
3374 output_filenames( unix_libs );
3375 output_filename( "$(LDFLAGS)" );
3376 output( "\n" );
3380 /*******************************************************************
3381 * output_static_lib
3383 static void output_static_lib( struct makefile *make )
3385 if (!crosstarget || !make->extlib)
3387 strarray_add( &make->clean_files, make->staticlib );
3388 output( "%s: %s", obj_dir_path( make, make->staticlib ), tools_path( make, "winebuild" ));
3389 output_filenames_obj_dir( make, make->object_files );
3390 output_filenames_obj_dir( make, make->unixobj_files );
3391 output( "\n" );
3392 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3393 output_filenames( target_flags );
3394 output_filenames_obj_dir( make, make->object_files );
3395 output_filenames_obj_dir( make, make->unixobj_files );
3396 output( "\n" );
3397 if (!make->extlib)
3398 add_install_rule( make, make->staticlib, make->staticlib,
3399 strmake( "d%s/%s", so_dir, make->staticlib ));
3401 if (crosstarget)
3403 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3405 strarray_add( &make->clean_files, name );
3406 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3407 output_filenames_obj_dir( make, make->crossobj_files );
3408 output( "\n" );
3409 output( "\t%s%s -b %s -w --staticlib -o $@", cmd_prefix( "BUILD" ),
3410 tools_path( make, "winebuild" ), crosstarget );
3411 output_filenames_obj_dir( make, make->crossobj_files );
3412 output( "\n" );
3413 if (!make->extlib)
3414 add_install_rule( make, make->staticlib, name,
3415 strmake( "d%s/%s", pe_dir, make->staticlib ));
3420 /*******************************************************************
3421 * output_shared_lib
3423 static void output_shared_lib( struct makefile *make )
3425 unsigned int i;
3426 char *basename, *p;
3427 struct strarray names = get_shared_lib_names( make->sharedlib );
3428 struct strarray all_libs = empty_strarray;
3429 struct strarray dep_libs = empty_strarray;
3431 basename = xstrdup( make->sharedlib );
3432 if ((p = strchr( basename, '.' ))) *p = 0;
3434 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3435 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3436 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3437 strarray_addall( &all_libs, libs );
3439 output( "%s:", obj_dir_path( make, make->sharedlib ));
3440 output_filenames_obj_dir( make, make->object_files );
3441 output_filenames( dep_libs );
3442 output( "\n" );
3443 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3444 output_filenames_obj_dir( make, make->object_files );
3445 output_filenames( all_libs );
3446 output_filename( "$(LDFLAGS)" );
3447 output( "\n" );
3448 add_install_rule( make, make->sharedlib, make->sharedlib, strmake( "p%s/%s", so_dir, make->sharedlib ));
3449 for (i = 1; i < names.count; i++)
3451 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3452 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3453 add_install_rule( make, names.str[i], names.str[i-1], strmake( "y%s/%s", so_dir, names.str[i] ));
3455 strarray_addall( &make->all_targets, names );
3459 /*******************************************************************
3460 * output_test_module
3462 static void output_test_module( struct makefile *make )
3464 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3465 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3466 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3467 struct strarray default_imports = get_default_imports( make, make->imports );
3468 struct strarray dep_libs = empty_strarray, all_libs = empty_strarray;
3469 struct makefile *parent = get_parent_makefile( make );
3470 const char *ext = make->is_cross ? "" : dll_ext;
3471 const char *debug_file;
3472 char *output_file;
3474 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, IMPORT_TYPE_DIRECT, make->is_cross ) );
3475 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, default_imports, IMPORT_TYPE_DEFAULT, make->is_cross ) );
3477 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3478 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3479 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3480 output( "%s:\n", output_file );
3481 output_winegcc_command( make, make->is_cross );
3482 output_filenames( make->extradllflags );
3483 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3484 output_filenames_obj_dir( make, make->res_files );
3485 if ((debug_file = get_debug_file( make, testmodule )))
3486 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3487 output_filenames( all_libs );
3488 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3489 output( "\n" );
3490 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3491 output_winegcc_command( make, make->is_cross );
3492 output_filename( "-s" );
3493 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3494 output_filenames( make->extradllflags );
3495 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3496 output_filenames_obj_dir( make, make->res_files );
3497 output_filenames( all_libs );
3498 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3499 output( "\n" );
3500 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3501 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3502 output_filenames_obj_dir( make, make->res_files );
3503 output_filenames( dep_libs );
3504 output_filename( tools_path( make, "winebuild" ));
3505 output_filename( tools_path( make, "winegcc" ));
3506 output( "\n" );
3508 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3509 output( "\t%secho \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3510 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3512 output_filenames_obj_dir( make, make->ok_files );
3513 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3514 if (parent)
3516 output_filename( parent->is_cross ? obj_dir_path( parent, make->testdll )
3517 : strmake( "%s%s", obj_dir_path( parent, make->testdll ), dll_ext ));
3518 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3520 output( "\n" );
3521 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3522 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3523 output( "\n" );
3524 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3525 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3526 output( "%s::\n", obj_dir_path( make, "testclean" ));
3527 output( "\trm -f" );
3528 output_filenames_obj_dir( make, make->ok_files );
3529 output( "\n" );
3530 strarray_addall( &make->clean_files, make->ok_files );
3531 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3535 /*******************************************************************
3536 * output_programs
3538 static void output_programs( struct makefile *make )
3540 unsigned int i, j;
3542 for (i = 0; i < make->programs.count; i++)
3544 char *program_installed = NULL;
3545 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3546 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3547 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3548 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3549 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3551 if (!objs.count) objs = make->object_files;
3552 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3554 strarray_addall( &all_libs, get_expanded_make_var_array( make, "UNIX_LIBS" ));
3555 strarray_addall( &all_libs, libs );
3558 output( "%s:", obj_dir_path( make, program ) );
3559 output_filenames_obj_dir( make, objs );
3560 output_filenames( deps );
3561 output( "\n" );
3562 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3563 output_filenames_obj_dir( make, objs );
3564 output_filenames( all_libs );
3565 output_filename( "$(LDFLAGS)" );
3566 output( "\n" );
3567 strarray_add( &make->all_targets, program );
3569 for (j = 0; j < symlinks.count; j++)
3571 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3572 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3574 strarray_addall( &make->all_targets, symlinks );
3576 add_install_rule( make, program, program_installed ? program_installed : program,
3577 strmake( "p$(bindir)/%s", program ));
3578 for (j = 0; j < symlinks.count; j++)
3579 add_install_rule( make, symlinks.str[j], program,
3580 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3585 /*******************************************************************
3586 * output_subdirs
3588 static void output_subdirs( struct makefile *make )
3590 struct strarray all_targets = empty_strarray;
3591 struct strarray makefile_deps = empty_strarray;
3592 struct strarray clean_files = empty_strarray;
3593 struct strarray testclean_files = empty_strarray;
3594 struct strarray distclean_files = empty_strarray;
3595 struct strarray dependencies = empty_strarray;
3596 struct strarray install_deps[NB_INSTALL_RULES] = { empty_strarray };
3597 struct strarray tooldeps_deps = empty_strarray;
3598 struct strarray buildtest_deps = empty_strarray;
3599 unsigned int i, j;
3601 strarray_addall( &clean_files, make->clean_files );
3602 strarray_addall( &distclean_files, make->distclean_files );
3603 strarray_addall( &all_targets, make->all_targets );
3604 for (i = 0; i < subdirs.count; i++)
3606 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3607 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3608 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3609 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3610 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3611 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3612 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3613 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3614 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3616 if (submakes[i]->disabled) continue;
3618 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3619 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3620 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3621 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3622 if (submakes[i]->testdll)
3623 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3624 for (j = 0; j < NB_INSTALL_RULES; j++)
3625 if (submakes[i]->install_rules[j].count)
3626 strarray_add( &install_deps[j], obj_dir_path( submakes[i], install_targets[j] ));
3628 strarray_addall( &dependencies, makefile_deps );
3629 output( "all:" );
3630 output_filenames( all_targets );
3631 output( "\n" );
3632 output( "Makefile:" );
3633 output_filenames( makefile_deps );
3634 output( "\n" );
3635 output_filenames( dependencies );
3636 output( ":\n" );
3637 for (j = 0; j < NB_INSTALL_RULES; j++)
3639 if (!install_deps[j].count) continue;
3640 if (strcmp( install_targets[j], "install-test" ))
3642 output( "install " );
3643 strarray_add_uniq( &make->phony_targets, "install" );
3645 output( "%s::", install_targets[j] );
3646 output_filenames( install_deps[j] );
3647 output( "\n" );
3648 strarray_add_uniq( &make->phony_targets, install_targets[j] );
3650 output_uninstall_rules( make );
3651 if (buildtest_deps.count)
3653 output( "buildtests:" );
3654 output_filenames( buildtest_deps );
3655 output( "\n" );
3656 strarray_add_uniq( &make->phony_targets, "buildtests" );
3658 output( "check test:" );
3659 output_filenames( testclean_files );
3660 output( "\n" );
3661 strarray_add_uniq( &make->phony_targets, "check" );
3662 strarray_add_uniq( &make->phony_targets, "test" );
3664 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3666 output( "clean::\n");
3667 output_rm_filenames( clean_files );
3668 output( "testclean::\n");
3669 output_rm_filenames( testclean_files );
3670 output( "distclean::\n");
3671 output_rm_filenames( distclean_files );
3672 output( "maintainer-clean::\n");
3673 output_rm_filenames( make->maintainerclean_files );
3674 strarray_add_uniq( &make->phony_targets, "distclean" );
3675 strarray_add_uniq( &make->phony_targets, "testclean" );
3676 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3678 if (tooldeps_deps.count)
3680 output( "__tooldeps__:" );
3681 output_filenames( tooldeps_deps );
3682 output( "\n" );
3683 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3686 if (make->phony_targets.count)
3688 output( ".PHONY:" );
3689 output_filenames( make->phony_targets );
3690 output( "\n" );
3695 /*******************************************************************
3696 * output_sources
3698 static void output_sources( struct makefile *make )
3700 struct strarray all_targets = empty_strarray;
3701 struct incl_file *source;
3702 unsigned int i, j;
3704 strarray_add_uniq( &make->phony_targets, "all" );
3706 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3708 char *obj = xstrdup( source->name );
3709 char *ext = get_extension( obj );
3711 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3712 *ext++ = 0;
3714 for (j = 0; output_source_funcs[j].ext; j++)
3715 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3717 output_source_funcs[j].fn( make, source, obj );
3718 strarray_addall_uniq( &make->dependencies, source->dependencies );
3721 /* special case for winetest: add resource files from other test dirs */
3722 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3724 struct strarray tests = enable_tests;
3725 if (!tests.count)
3726 for (i = 0; i < subdirs.count; i++)
3727 if (submakes[i]->testdll && !submakes[i]->disabled)
3728 strarray_add( &tests, submakes[i]->testdll );
3729 for (i = 0; i < tests.count; i++)
3730 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3733 if (make->dlldata_files.count)
3735 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3736 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3737 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3738 output_filenames( make->dlldata_files );
3739 output( "\n" );
3742 if (make->staticlib) output_static_lib( make );
3743 else if (make->module)
3745 output_module( make );
3746 if (*dll_ext && !make->is_cross && !make->data_only) output_fake_module( make );
3747 if (make->unixlib) output_unix_lib( make );
3748 if (make->importlib) output_import_lib( make );
3750 else if (make->testdll) output_test_module( make );
3751 else if (make->sharedlib) output_shared_lib( make );
3752 else if (make->programs.count) output_programs( make );
3754 for (i = 0; i < make->scripts.count; i++)
3755 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3756 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3758 for (i = 0; i < make->extra_targets.count; i++)
3759 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3760 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3761 else
3762 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3764 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3765 strarray_add( &make->distclean_files, "Makefile" );
3766 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3768 if (!make->obj_dir)
3769 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3770 else if (!strcmp( make->obj_dir, "po" ))
3771 strarray_add( &make->distclean_files, "LINGUAS" );
3773 strarray_addall( &make->clean_files, make->object_files );
3774 strarray_addall( &make->clean_files, make->crossobj_files );
3775 strarray_addall( &make->clean_files, make->implib_files );
3776 strarray_addall( &make->clean_files, make->crossimplib_files );
3777 strarray_addall( &make->clean_files, make->unixobj_files );
3778 strarray_addall( &make->clean_files, make->res_files );
3779 strarray_addall( &make->clean_files, make->pot_files );
3780 strarray_addall( &make->clean_files, make->debug_files );
3781 strarray_addall( &make->clean_files, make->all_targets );
3783 if (make == top_makefile)
3785 output_subdirs( make );
3786 return;
3789 strarray_addall( &all_targets, make->all_targets );
3790 strarray_addall( &all_targets, make->font_files );
3791 if (all_targets.count)
3793 output( "%s:", obj_dir_path( make, "all" ));
3794 output_filenames_obj_dir( make, all_targets );
3795 output( "\n" );
3796 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3798 for (i = 0; i < NB_INSTALL_RULES; i++) output_install_rules( make, i );
3800 if (make->clean_files.count)
3802 output( "%s::\n", obj_dir_path( make, "clean" ));
3803 output( "\trm -f" );
3804 output_filenames_obj_dir( make, make->clean_files );
3805 output( "\n" );
3806 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3811 /*******************************************************************
3812 * create_temp_file
3814 static FILE *create_temp_file( const char *orig )
3816 char *name = xmalloc( strlen(orig) + 13 );
3817 unsigned int i, id = getpid();
3818 int fd;
3819 FILE *ret = NULL;
3821 for (i = 0; i < 100; i++)
3823 sprintf( name, "%s.tmp%08x", orig, id );
3824 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3826 ret = fdopen( fd, "w" );
3827 break;
3829 if (errno != EEXIST) break;
3830 id += 7777;
3832 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3833 temp_file_name = name;
3834 return ret;
3838 /*******************************************************************
3839 * rename_temp_file
3841 static void rename_temp_file( const char *dest )
3843 int ret = rename( temp_file_name, dest );
3844 if (ret == -1 && errno == EEXIST)
3846 /* rename doesn't overwrite on windows */
3847 unlink( dest );
3848 ret = rename( temp_file_name, dest );
3850 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3851 temp_file_name = NULL;
3855 /*******************************************************************
3856 * are_files_identical
3858 static int are_files_identical( FILE *file1, FILE *file2 )
3860 for (;;)
3862 char buffer1[8192], buffer2[8192];
3863 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3864 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3865 if (size1 != size2) return 0;
3866 if (!size1) return feof( file1 ) && feof( file2 );
3867 if (memcmp( buffer1, buffer2, size1 )) return 0;
3872 /*******************************************************************
3873 * rename_temp_file_if_changed
3875 static void rename_temp_file_if_changed( const char *dest )
3877 FILE *file1, *file2;
3878 int do_rename = 1;
3880 if ((file1 = fopen( dest, "r" )))
3882 if ((file2 = fopen( temp_file_name, "r" )))
3884 do_rename = !are_files_identical( file1, file2 );
3885 fclose( file2 );
3887 fclose( file1 );
3889 if (!do_rename)
3891 unlink( temp_file_name );
3892 temp_file_name = NULL;
3894 else rename_temp_file( dest );
3898 /*******************************************************************
3899 * output_linguas
3901 static void output_linguas( const struct makefile *make )
3903 const char *dest = obj_dir_path( make, "LINGUAS" );
3904 struct incl_file *source;
3906 output_file = create_temp_file( dest );
3908 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3909 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3910 if (strendswith( source->name, ".po" ))
3911 output( "%s\n", replace_extension( source->name, ".po", "" ));
3913 if (fclose( output_file )) fatal_perror( "write" );
3914 output_file = NULL;
3915 rename_temp_file_if_changed( dest );
3919 /*******************************************************************
3920 * output_testlist
3922 static void output_testlist( const struct makefile *make )
3924 const char *dest = obj_dir_path( make, "testlist.c" );
3925 unsigned int i;
3927 output_file = create_temp_file( dest );
3929 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3930 output( "#define WIN32_LEAN_AND_MEAN\n" );
3931 output( "#include <windows.h>\n\n" );
3932 output( "#define STANDALONE\n" );
3933 output( "#include \"wine/test.h\"\n\n" );
3935 for (i = 0; i < make->test_files.count; i++)
3936 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3937 output( "\n" );
3938 output( "const struct test winetest_testlist[] =\n" );
3939 output( "{\n" );
3940 for (i = 0; i < make->test_files.count; i++)
3941 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3942 output( " { 0, 0 }\n" );
3943 output( "};\n" );
3945 if (fclose( output_file )) fatal_perror( "write" );
3946 output_file = NULL;
3947 rename_temp_file_if_changed( dest );
3951 /*******************************************************************
3952 * output_gitignore
3954 static void output_gitignore( const char *dest, struct strarray files )
3956 unsigned int i;
3958 output_file = create_temp_file( dest );
3960 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3961 for (i = 0; i < files.count; i++)
3963 if (!strchr( files.str[i], '/' )) output( "/" );
3964 output( "%s\n", files.str[i] );
3967 if (fclose( output_file )) fatal_perror( "write" );
3968 output_file = NULL;
3969 rename_temp_file( dest );
3973 /*******************************************************************
3974 * output_stub_makefile
3976 static void output_stub_makefile( struct makefile *make )
3978 struct strarray targets = empty_strarray;
3979 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
3980 unsigned int i;
3982 if (make->obj_dir) create_dir( make->obj_dir );
3984 output_file_name = obj_dir_path( make, "Makefile" );
3985 output_file = create_temp_file( output_file_name );
3987 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
3989 if (make_var) output( "MAKE = %s\n\n", make_var );
3990 output( "all:\n" );
3992 if (make->all_targets.count) strarray_add( &targets, "all" );
3993 for (i = 0; i < NB_INSTALL_RULES; i++)
3995 if (!make->install_rules[i].count) continue;
3996 strarray_add_uniq( &targets, "install" );
3997 strarray_add( &targets, install_targets[i] );
3999 if (make->clean_files.count) strarray_add( &targets, "clean" );
4000 if (make->test_files.count)
4002 strarray_add( &targets, "check" );
4003 strarray_add( &targets, "test" );
4004 strarray_add( &targets, "testclean" );
4007 output_filenames( targets );
4008 output_filenames( make->clean_files );
4009 output( ":\n" );
4010 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
4011 output( ".PHONY:" );
4012 output_filenames( targets );
4013 output( "\n" );
4015 fclose( output_file );
4016 output_file = NULL;
4017 rename_temp_file( output_file_name );
4021 /*******************************************************************
4022 * output_silent_rules
4024 static void output_silent_rules(void)
4026 static const char *cmds[] =
4028 "BISON",
4029 "BUILD",
4030 "CC",
4031 "CCLD",
4032 "FLEX",
4033 "GEN",
4034 "LN",
4035 "MSG",
4036 "SED",
4037 "TEST",
4038 "WIDL",
4039 "WMC",
4040 "WRC"
4042 unsigned int i;
4044 output( "V = 0\n" );
4045 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++)
4047 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4048 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4049 output( "quiet_%s_1 =\n", cmds[i] );
4054 /*******************************************************************
4055 * output_top_makefile
4057 static void output_top_makefile( struct makefile *make )
4059 char buffer[1024];
4060 FILE *src_file;
4061 unsigned int i;
4062 int found = 0;
4064 output_file_name = obj_dir_path( make, output_makefile_name );
4065 output_file = create_temp_file( output_file_name );
4067 /* copy the contents of the source makefile */
4068 src_file = open_input_makefile( make );
4069 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4071 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4072 found = !strncmp( buffer, separator, strlen(separator) );
4074 if (fclose( src_file )) fatal_perror( "close" );
4075 input_file_name = NULL;
4077 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4079 if (silent_rules) output_silent_rules();
4080 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4081 output_sources( make );
4082 /* disable implicit rules */
4083 output( ".SUFFIXES:\n" );
4085 fclose( output_file );
4086 output_file = NULL;
4087 rename_temp_file( output_file_name );
4091 /*******************************************************************
4092 * output_dependencies
4094 static void output_dependencies( struct makefile *make )
4096 struct strarray ignore_files = empty_strarray;
4098 if (make->obj_dir) create_dir( make->obj_dir );
4100 if (make == top_makefile) output_top_makefile( make );
4101 else output_stub_makefile( make );
4103 strarray_addall( &ignore_files, make->distclean_files );
4104 strarray_addall( &ignore_files, make->clean_files );
4105 if (make->testdll) output_testlist( make );
4106 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4107 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4109 create_file_directories( make, ignore_files );
4111 output_file_name = NULL;
4115 /*******************************************************************
4116 * load_sources
4118 static void load_sources( struct makefile *make )
4120 static const char *source_vars[] =
4122 "SOURCES",
4123 "C_SRCS",
4124 "OBJC_SRCS",
4125 "RC_SRCS",
4126 "MC_SRCS",
4127 "IDL_SRCS",
4128 "BISON_SRCS",
4129 "LEX_SRCS",
4130 "HEADER_SRCS",
4131 "XTEMPLATE_SRCS",
4132 "SVG_SRCS",
4133 "FONT_SRCS",
4134 "IN_SRCS",
4135 "PO_SRCS",
4136 "MANPAGES",
4137 NULL
4139 const char **var;
4140 unsigned int i;
4141 struct strarray value;
4142 struct incl_file *file;
4144 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4145 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4147 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4148 make->module = get_expanded_make_variable( make, "MODULE" );
4149 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4150 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4151 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4152 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4153 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4154 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4156 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4157 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4158 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4159 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4160 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4161 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4162 for (i = 0; i < NB_INSTALL_RULES; i++)
4163 make->install[i] = get_expanded_make_var_array( make, install_variables[i] );
4165 if (make->extlib) make->staticlib = make->extlib;
4166 if (make->staticlib) make->module = make->staticlib;
4168 if (host_cpu)
4170 value = get_expanded_file_local_var( make, host_cpu, "EXTRADLLFLAGS" );
4171 if (value.count) make->extradllflags = value;
4174 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4175 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4176 make->data_only = strarray_exists( &make->extradllflags, "-Wb,--data-only" );
4177 make->use_msvcrt = (make->module || make->testdll || make->is_win16) &&
4178 !strarray_exists( &make->extradllflags, "-mcygwin" );
4179 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4180 strarray_exists( &make->extradllflags, "-mwindows" );
4181 make->native_unix_lib = !!make->unixlib;
4183 if (make->use_msvcrt) strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4185 if (make->module)
4187 /* add default install rules if nothing was specified */
4188 for (i = 0; i < NB_INSTALL_RULES; i++) if (make->install[i].count) break;
4189 if (i == NB_INSTALL_RULES)
4191 if (make->importlib) strarray_add( &make->install[INSTALL_DEV], make->importlib );
4192 if (make->staticlib) strarray_add( &make->install[INSTALL_DEV], make->staticlib );
4193 else strarray_add( &make->install[INSTALL_LIB], make->module );
4197 make->include_paths = empty_strarray;
4198 make->include_args = empty_strarray;
4199 make->define_args = empty_strarray;
4200 make->unix_cflags = empty_strarray;
4201 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4203 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4204 for (i = 0; i < value.count; i++)
4206 if (!strncmp( value.str[i], "-I", 2 ))
4208 const char *dir = value.str[i] + 2;
4209 if (!strncmp( dir, "./", 2 ))
4211 dir += 2;
4212 while (*dir == '/') dir++;
4214 strarray_add_uniq( &make->include_paths, dir );
4216 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4217 strarray_add_uniq( &make->define_args, value.str[i] );
4219 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4220 strarray_addall_uniq( &make->unix_cflags, get_expanded_make_var_array( make, "UNIX_CFLAGS" ));
4222 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4223 if (make->src_dir)
4224 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4225 if (make->parent_dir)
4226 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4227 strarray_add( &make->include_args, "-Iinclude" );
4228 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4230 list_init( &make->sources );
4231 list_init( &make->includes );
4233 for (var = source_vars; *var; var++)
4235 value = get_expanded_make_var_array( make, *var );
4236 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4239 add_generated_sources( make );
4240 if (!make->unixlib) make->unixlib = get_unix_lib_name( make );
4242 if (make->use_msvcrt) strarray_add( &make->define_args, get_crt_define( make ));
4244 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4245 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4247 make->is_cross = crosstarget && make->use_msvcrt;
4249 if (!*dll_ext || make->is_cross)
4250 for (i = 0; i < make->delayimports.count; i++)
4251 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4255 /*******************************************************************
4256 * parse_makeflags
4258 static void parse_makeflags( const char *flags )
4260 const char *p = flags;
4261 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4263 while (*p)
4265 while (isspace(*p)) p++;
4266 var = buffer;
4267 while (*p && !isspace(*p))
4269 if (*p == '\\' && p[1]) p++;
4270 *var++ = *p++;
4272 *var = 0;
4273 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4278 /*******************************************************************
4279 * parse_option
4281 static int parse_option( const char *opt )
4283 if (opt[0] != '-')
4285 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4286 return 0;
4288 switch(opt[1])
4290 case 'f':
4291 if (opt[2]) output_makefile_name = opt + 2;
4292 break;
4293 case 'R':
4294 relative_dir_mode = 1;
4295 break;
4296 case 'S':
4297 silent_rules = 1;
4298 break;
4299 default:
4300 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4301 exit(1);
4303 return 1;
4307 /*******************************************************************
4308 * main
4310 int main( int argc, char *argv[] )
4312 const char *makeflags = getenv( "MAKEFLAGS" );
4313 int i, j;
4315 if (makeflags) parse_makeflags( makeflags );
4317 i = 1;
4318 while (i < argc)
4320 if (parse_option( argv[i] ))
4322 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4323 argc--;
4325 else i++;
4328 if (relative_dir_mode)
4330 char *relpath;
4332 if (argc != 3)
4334 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4335 exit( 1 );
4337 relpath = get_relative_path( argv[1], argv[2] );
4338 printf( "%s\n", relpath ? relpath : "." );
4339 exit( 0 );
4342 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4344 atexit( cleanup_files );
4345 signal( SIGTERM, exit_on_signal );
4346 signal( SIGINT, exit_on_signal );
4347 #ifdef SIGHUP
4348 signal( SIGHUP, exit_on_signal );
4349 #endif
4351 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4352 for (i = 0; i < HASH_SIZE; i++) list_init( &global_includes[i] );
4354 top_makefile = parse_makefile( NULL );
4356 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4357 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4358 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4359 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4360 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4361 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4362 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4363 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4364 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4365 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4366 for (i = 0; i < NB_INSTALL_RULES; i++)
4367 top_install[i] = get_expanded_make_var_array( top_makefile, strmake( "TOP_%s", install_variables[i] ));
4369 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4370 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4371 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4372 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4373 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4374 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4375 host_cpu = get_expanded_make_variable( top_makefile, "host_cpu" );
4376 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4377 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4378 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4379 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4380 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4381 bison = get_expanded_make_variable( top_makefile, "BISON" );
4382 ar = get_expanded_make_variable( top_makefile, "AR" );
4383 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4384 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4385 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4386 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4387 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4388 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4389 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4391 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4392 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4393 if (!exe_ext) exe_ext = "";
4394 if (!tools_ext) tools_ext = "";
4395 if (host_cpu && (host_cpu = normalize_arch( host_cpu )))
4397 so_dir = strmake( "$(dlldir)/%s-unix", host_cpu );
4398 pe_dir = strmake( "$(dlldir)/%s-windows", host_cpu );
4400 else
4401 so_dir = pe_dir = "$(dlldir)";
4403 extra_cflags_extlib = remove_warning_flags( extra_cflags );
4404 extra_cross_cflags_extlib = remove_warning_flags( extra_cross_cflags );
4406 top_makefile->src_dir = root_src_dir;
4407 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4408 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4409 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4411 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4413 load_sources( top_makefile );
4414 load_sources( include_makefile );
4415 for (i = 0; i < subdirs.count; i++)
4416 if (submakes[i] != include_makefile) load_sources( submakes[i] );
4418 output_dependencies( top_makefile );
4419 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4421 return 0;