dwrite/tests: Avoid "misleading indentation" warnings.
[wine.git] / tools / makedep.c
blob589ea7a7514c84eb436c4d255ac526eba2e54e26
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 file *file;
70 char *name;
71 char *filename;
72 char *basename; /* base target name for generated files */
73 char *sourcename; /* source file name for generated headers */
74 struct incl_file *included_by; /* file that included this one */
75 int included_line; /* line where this file was included */
76 enum incl_type type; /* type of include */
77 int use_msvcrt:1; /* put msvcrt headers in the search path? */
78 int is_external:1; /* file from external library? */
79 struct incl_file *owner;
80 unsigned int files_count; /* files in use */
81 unsigned int files_size; /* total allocated size */
82 struct incl_file **files;
83 struct strarray dependencies; /* file dependencies */
84 struct strarray importlibdeps; /* importlib dependencies */
87 #define FLAG_GENERATED 0x000001 /* generated file */
88 #define FLAG_INSTALL 0x000002 /* file to install */
89 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
90 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
91 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
92 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
93 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
94 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (_l.res) file */
95 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
96 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
97 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
98 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
99 #define FLAG_C_UNIX 0x040000 /* file is part of a Unix library */
100 #define FLAG_SFD_FONTS 0x080000 /* sfd file generated bitmap fonts */
102 static const struct
104 unsigned int flag;
105 const char *ext;
106 } idl_outputs[] =
108 { FLAG_IDL_TYPELIB, "_l.res" },
109 { FLAG_IDL_REGTYPELIB, "_t.res" },
110 { FLAG_IDL_CLIENT, "_c.c" },
111 { FLAG_IDL_IDENT, "_i.c" },
112 { FLAG_IDL_PROXY, "_p.c" },
113 { FLAG_IDL_SERVER, "_s.c" },
114 { FLAG_IDL_REGISTER, "_r.res" },
115 { FLAG_IDL_HEADER, ".h" }
118 #define HASH_SIZE 997
120 static struct list files[HASH_SIZE];
122 enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
124 /* variables common to all makefiles */
125 static struct strarray linguas;
126 static struct strarray dll_flags;
127 static struct strarray unix_dllflags;
128 static struct strarray target_flags;
129 static struct strarray msvcrt_flags;
130 static struct strarray extra_cflags;
131 static struct strarray extra_cross_cflags;
132 static struct strarray extra_cflags_extlib;
133 static struct strarray extra_cross_cflags_extlib;
134 static struct strarray cpp_flags;
135 static struct strarray lddll_flags;
136 static struct strarray libs;
137 static struct strarray enable_tests;
138 static struct strarray cmdline_vars;
139 static struct strarray subdirs;
140 static struct strarray disabled_dirs;
141 static struct strarray delay_import_libs;
142 static struct strarray top_install_lib;
143 static struct strarray top_install_dev;
144 static const char *root_src_dir;
145 static const char *tools_dir;
146 static const char *tools_ext;
147 static const char *exe_ext;
148 static const char *dll_ext;
149 static const char *host_cpu;
150 static const char *pe_dir;
151 static const char *so_dir;
152 static const char *crosstarget;
153 static const char *crossdebug;
154 static const char *fontforge;
155 static const char *convert;
156 static const char *flex;
157 static const char *bison;
158 static const char *ar;
159 static const char *ranlib;
160 static const char *rsvg;
161 static const char *icotool;
162 static const char *dlltool;
163 static const char *msgfmt;
164 static const char *ln_s;
165 static const char *sed_cmd;
166 static const char *delay_load_flag;
168 struct makefile
170 /* values determined from input makefile */
171 struct strarray vars;
172 struct strarray include_paths;
173 struct strarray include_args;
174 struct strarray define_args;
175 struct strarray programs;
176 struct strarray scripts;
177 struct strarray imports;
178 struct strarray delayimports;
179 struct strarray extradllflags;
180 struct strarray install_lib;
181 struct strarray install_dev;
182 struct strarray extra_targets;
183 struct strarray extra_imports;
184 struct list sources;
185 struct list includes;
186 const char *src_dir;
187 const char *obj_dir;
188 const char *parent_dir;
189 const char *module;
190 const char *testdll;
191 const char *extlib;
192 const char *sharedlib;
193 const char *staticlib;
194 const char *staticimplib;
195 const char *importlib;
196 const char *unixlib;
197 int native_unix_lib;
198 int disabled;
199 int use_msvcrt;
200 int is_cross;
201 int is_win16;
202 int is_exe;
204 /* values generated at output time */
205 struct strarray in_files;
206 struct strarray ok_files;
207 struct strarray pot_files;
208 struct strarray test_files;
209 struct strarray clean_files;
210 struct strarray distclean_files;
211 struct strarray maintainerclean_files;
212 struct strarray uninstall_files;
213 struct strarray object_files;
214 struct strarray crossobj_files;
215 struct strarray unixobj_files;
216 struct strarray res_files;
217 struct strarray font_files;
218 struct strarray debug_files;
219 struct strarray dlldata_files;
220 struct strarray implib_files;
221 struct strarray crossimplib_files;
222 struct strarray all_targets;
223 struct strarray phony_targets;
224 struct strarray dependencies;
225 struct strarray install_rules[NB_INSTALL_RULES];
228 static struct makefile *top_makefile;
229 static struct makefile **submakes;
231 static const char separator[] = "### Dependencies";
232 static const char *output_makefile_name = "Makefile";
233 static const char *input_file_name;
234 static const char *output_file_name;
235 static const char *temp_file_name;
236 static int relative_dir_mode;
237 static int silent_rules;
238 static int input_line;
239 static int output_column;
240 static FILE *output_file;
242 static const char Usage[] =
243 "Usage: makedep [options] [directories]\n"
244 "Options:\n"
245 " -R from to Compute the relative path between two directories\n"
246 " -S Generate Automake-style silent rules\n"
247 " -fxxx Store output in file 'xxx' (default: Makefile)\n";
250 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
251 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
252 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
253 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
255 /*******************************************************************
256 * fatal_error
258 static void fatal_error( const char *msg, ... )
260 va_list valist;
261 va_start( valist, msg );
262 if (input_file_name)
264 fprintf( stderr, "%s:", input_file_name );
265 if (input_line) fprintf( stderr, "%d:", input_line );
266 fprintf( stderr, " error: " );
268 else fprintf( stderr, "makedep: error: " );
269 vfprintf( stderr, msg, valist );
270 va_end( valist );
271 exit(1);
275 /*******************************************************************
276 * fatal_perror
278 static void fatal_perror( const char *msg, ... )
280 va_list valist;
281 va_start( valist, msg );
282 if (input_file_name)
284 fprintf( stderr, "%s:", input_file_name );
285 if (input_line) fprintf( stderr, "%d:", input_line );
286 fprintf( stderr, " error: " );
288 else fprintf( stderr, "makedep: error: " );
289 vfprintf( stderr, msg, valist );
290 perror( " " );
291 va_end( valist );
292 exit(1);
296 /*******************************************************************
297 * cleanup_files
299 static void cleanup_files(void)
301 if (temp_file_name) unlink( temp_file_name );
302 if (output_file_name) unlink( output_file_name );
306 /*******************************************************************
307 * exit_on_signal
309 static void exit_on_signal( int sig )
311 exit( 1 ); /* this will call the atexit functions */
315 /*******************************************************************
316 * output
318 static void output( const char *format, ... )
320 int ret;
321 va_list valist;
323 va_start( valist, format );
324 ret = vfprintf( output_file, format, valist );
325 va_end( valist );
326 if (ret < 0) fatal_perror( "output" );
327 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
328 else output_column += ret;
332 /*******************************************************************
333 * strarray_get_value
335 * Find a value in a name/value pair string array.
337 static const char *strarray_get_value( const struct strarray *array, const char *name )
339 int pos, res, min = 0, max = array->count / 2 - 1;
341 while (min <= max)
343 pos = (min + max) / 2;
344 if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
345 if (res < 0) min = pos + 1;
346 else max = pos - 1;
348 return NULL;
352 /*******************************************************************
353 * strarray_set_value
355 * Define a value in a name/value pair string array.
357 static void strarray_set_value( struct strarray *array, const char *name, const char *value )
359 int i, pos, res, min = 0, max = array->count / 2 - 1;
361 while (min <= max)
363 pos = (min + max) / 2;
364 if (!(res = strcmp( array->str[pos * 2], name )))
366 /* redefining a variable replaces the previous value */
367 array->str[pos * 2 + 1] = value;
368 return;
370 if (res < 0) min = pos + 1;
371 else max = pos - 1;
373 strarray_add( array, NULL );
374 strarray_add( array, NULL );
375 for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
376 array->str[min * 2] = name;
377 array->str[min * 2 + 1] = value;
381 /*******************************************************************
382 * normalize_arch
384 static const char *normalize_arch( const char *arch )
386 unsigned int i, j;
388 static const char *map[][8] =
390 /* normalized aliases */
391 { "i386", "i486", "i586", "i686", "ia32" },
392 { "x86_64", "amd64", "x86-64", "x86_amd64", "x64" },
393 { "aarch64", "arm64" },
394 { "arm" },
397 for (i = 0; i < sizeof(map) / sizeof(map[0]); i++)
398 for (j = 0; map[i][j]; j++)
399 if (!strncmp( arch, map[i][j], strlen(map[i][j]) ))
400 return map[i][0];
401 return NULL;
405 /*******************************************************************
406 * output_filename
408 static void output_filename( const char *name )
410 if (output_column + strlen(name) + 1 > 100)
412 output( " \\\n" );
413 output( " " );
415 else if (output_column) output( " " );
416 output( "%s", name );
420 /*******************************************************************
421 * output_filenames
423 static void output_filenames( struct strarray array )
425 unsigned int i;
427 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
431 /*******************************************************************
432 * output_rm_filenames
434 static void output_rm_filenames( struct strarray array )
436 static const unsigned int max_cmdline = 30000; /* to be on the safe side */
437 unsigned int i, len;
439 if (!array.count) return;
440 output( "\trm -f" );
441 for (i = len = 0; i < array.count; i++)
443 if (len > max_cmdline)
445 output( "\n" );
446 output( "\trm -f" );
447 len = 0;
449 output_filename( array.str[i] );
450 len += strlen( array.str[i] ) + 1;
452 output( "\n" );
456 /*******************************************************************
457 * get_extension
459 static char *get_extension( char *filename )
461 char *ext = strrchr( filename, '.' );
462 if (ext && strchr( ext, '/' )) ext = NULL;
463 return ext;
467 /*******************************************************************
468 * get_base_name
470 static const char *get_base_name( const char *name )
472 char *base;
473 if (!strchr( name, '.' )) return name;
474 base = xstrdup( name );
475 *strrchr( base, '.' ) = 0;
476 return base;
480 /*******************************************************************
481 * replace_filename
483 static char *replace_filename( const char *path, const char *name )
485 const char *p;
486 char *ret;
487 size_t len;
489 if (!path) return xstrdup( name );
490 if (!(p = strrchr( path, '/' ))) return xstrdup( name );
491 len = p - path + 1;
492 ret = xmalloc( len + strlen( name ) + 1 );
493 memcpy( ret, path, len );
494 strcpy( ret + len, name );
495 return ret;
499 /*******************************************************************
500 * replace_substr
502 static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
504 size_t pos = start - str;
505 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
506 memcpy( ret, str, pos );
507 strcpy( ret + pos, replace );
508 strcat( ret + pos, start + len );
509 return ret;
513 /*******************************************************************
514 * get_relative_path
516 * Determine where the destination path is located relative to the 'from' path.
518 static char *get_relative_path( const char *from, const char *dest )
520 const char *start;
521 char *ret, *p;
522 unsigned int dotdots = 0;
524 /* a path of "." is equivalent to an empty path */
525 if (!strcmp( from, "." )) from = "";
527 for (;;)
529 while (*from == '/') from++;
530 while (*dest == '/') dest++;
531 start = dest; /* save start of next path element */
532 if (!*from) break;
534 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
535 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
537 /* count remaining elements in 'from' */
540 dotdots++;
541 while (*from && *from != '/') from++;
542 while (*from == '/') from++;
544 while (*from);
545 break;
548 if (!start[0] && !dotdots) return NULL; /* empty path */
550 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
551 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
553 if (start[0]) strcpy( p, start );
554 else p[-1] = 0; /* remove trailing slash */
555 return ret;
559 /*******************************************************************
560 * concat_paths
562 static char *concat_paths( const char *base, const char *path )
564 int i, len;
565 char *ret;
567 if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
568 if (!path || !path[0]) return xstrdup( base );
569 if (path[0] == '/') return xstrdup( path );
571 len = strlen( base );
572 while (len && base[len - 1] == '/') len--;
573 while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
575 for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
576 if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
577 if (i != len - 1 || base[i] != '.')
579 path += 2;
580 while (*path == '/') path++;
582 /* else ignore "." element */
583 while (i > 0 && base[i - 1] == '/') i--;
584 len = i;
586 if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
587 ret = xmalloc( len + strlen( path ) + 2 );
588 memcpy( ret, base, len );
589 ret[len++] = '/';
590 strcpy( ret + len, path );
591 return ret;
595 /*******************************************************************
596 * obj_dir_path
598 static char *obj_dir_path( const struct makefile *make, const char *path )
600 return concat_paths( make->obj_dir, path );
604 /*******************************************************************
605 * src_dir_path
607 static char *src_dir_path( const struct makefile *make, const char *path )
609 if (make->src_dir) return concat_paths( make->src_dir, path );
610 return obj_dir_path( make, path );
614 /*******************************************************************
615 * root_src_dir_path
617 static char *root_src_dir_path( const char *path )
619 return concat_paths( root_src_dir, path );
623 /*******************************************************************
624 * tools_dir_path
626 static char *tools_dir_path( const struct makefile *make, const char *path )
628 if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
629 return strmake( "tools/%s", path );
633 /*******************************************************************
634 * tools_path
636 static char *tools_path( const struct makefile *make, const char *name )
638 return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
642 /*******************************************************************
643 * strarray_addall_path
645 static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
647 unsigned int i;
649 for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
653 /*******************************************************************
654 * get_line
656 static char *get_line( FILE *file )
658 static char *buffer;
659 static size_t size;
661 if (!size)
663 size = 1024;
664 buffer = xmalloc( size );
666 if (!fgets( buffer, size, file )) return NULL;
667 input_line++;
669 for (;;)
671 char *p = buffer + strlen(buffer);
672 /* if line is larger than buffer, resize buffer */
673 while (p == buffer + size - 1 && p[-1] != '\n')
675 buffer = xrealloc( buffer, size * 2 );
676 if (!fgets( buffer + size - 1, size + 1, file )) break;
677 p = buffer + strlen(buffer);
678 size *= 2;
680 if (p > buffer && p[-1] == '\n')
682 *(--p) = 0;
683 if (p > buffer && p[-1] == '\r') *(--p) = 0;
684 if (p > buffer && p[-1] == '\\')
686 *(--p) = 0;
687 /* line ends in backslash, read continuation line */
688 if (!fgets( p, size - (p - buffer), file )) return buffer;
689 input_line++;
690 continue;
693 return buffer;
698 /*******************************************************************
699 * hash_filename
701 static unsigned int hash_filename( const char *name )
703 /* FNV-1 hash */
704 unsigned int ret = 2166136261u;
705 while (*name) ret = (ret * 16777619) ^ *name++;
706 return ret % HASH_SIZE;
710 /*******************************************************************
711 * add_file
713 static struct file *add_file( const char *name )
715 struct file *file = xmalloc( sizeof(*file) );
716 memset( file, 0, sizeof(*file) );
717 file->name = xstrdup( name );
718 return file;
722 /*******************************************************************
723 * add_dependency
725 static void add_dependency( struct file *file, const char *name, enum incl_type type )
727 if (file->deps_count >= file->deps_size)
729 file->deps_size *= 2;
730 if (file->deps_size < 16) file->deps_size = 16;
731 file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
733 file->deps[file->deps_count].line = input_line;
734 file->deps[file->deps_count].type = type;
735 file->deps[file->deps_count].name = xstrdup( name );
736 file->deps_count++;
740 /*******************************************************************
741 * find_src_file
743 static struct incl_file *find_src_file( const struct makefile *make, const char *name )
745 struct incl_file *file;
747 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
748 if (!strcmp( name, file->name )) return file;
749 return NULL;
752 /*******************************************************************
753 * find_include_file
755 static struct incl_file *find_include_file( const struct makefile *make, const char *name )
757 struct incl_file *file;
759 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
761 const char *filename = file->filename;
762 if (!filename) continue;
763 if (make->obj_dir && strlen(make->obj_dir) < strlen(filename))
765 filename += strlen(make->obj_dir);
766 while (*filename == '/') filename++;
768 if (!strcmp( name, filename )) return file;
770 return NULL;
773 /*******************************************************************
774 * add_include
776 * Add an include file if it doesn't already exists.
778 static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
779 const char *name, int line, enum incl_type type )
781 struct incl_file *include;
783 if (parent->files_count >= parent->files_size)
785 parent->files_size *= 2;
786 if (parent->files_size < 16) parent->files_size = 16;
787 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
790 LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
791 if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
792 goto found;
794 include = xmalloc( sizeof(*include) );
795 memset( include, 0, sizeof(*include) );
796 include->name = xstrdup(name);
797 include->included_by = parent;
798 include->included_line = line;
799 include->type = type;
800 include->use_msvcrt = parent->use_msvcrt;
801 list_add_tail( &make->includes, &include->entry );
802 found:
803 parent->files[parent->files_count++] = include;
804 return include;
808 /*******************************************************************
809 * add_generated_source
811 * Add a generated source file to the list.
813 static struct incl_file *add_generated_source( struct makefile *make,
814 const char *name, const char *filename )
816 struct incl_file *file;
818 if ((file = find_src_file( make, name ))) return file; /* we already have it */
819 file = xmalloc( sizeof(*file) );
820 memset( file, 0, sizeof(*file) );
821 file->file = add_file( name );
822 file->name = xstrdup( name );
823 file->basename = xstrdup( filename ? filename : name );
824 file->filename = obj_dir_path( make, file->basename );
825 file->file->flags = FLAG_GENERATED;
826 file->use_msvcrt = make->use_msvcrt;
827 list_add_tail( &make->sources, &file->entry );
828 return file;
832 /*******************************************************************
833 * parse_include_directive
835 static void parse_include_directive( struct file *source, char *str )
837 char quote, *include, *p = str;
839 while (*p && isspace(*p)) p++;
840 if (*p != '\"' && *p != '<' ) return;
841 quote = *p++;
842 if (quote == '<') quote = '>';
843 include = p;
844 while (*p && (*p != quote)) p++;
845 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
846 *p = 0;
847 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
851 /*******************************************************************
852 * parse_pragma_directive
854 static void parse_pragma_directive( struct file *source, char *str )
856 char *flag, *p = str;
858 if (!isspace( *p )) return;
859 while (*p && isspace(*p)) p++;
860 p = strtok( p, " \t" );
861 if (strcmp( p, "makedep" )) return;
863 while ((flag = strtok( NULL, " \t" )))
865 if (!strcmp( flag, "depend" ))
867 while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
868 return;
870 else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
872 if (strendswith( source->name, ".idl" ))
874 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
875 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
876 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
877 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
878 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
879 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
880 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
881 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
883 else if (strendswith( source->name, ".rc" ))
885 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
887 else if (strendswith( source->name, ".sfd" ))
889 if (!strcmp( flag, "font" ))
891 struct strarray *array = source->args;
893 if (!array)
895 source->args = array = xmalloc( sizeof(*array) );
896 *array = empty_strarray;
897 source->flags |= FLAG_SFD_FONTS;
899 strarray_add( array, xstrdup( strtok( NULL, "" )));
900 return;
903 else
905 if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
906 if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
912 /*******************************************************************
913 * parse_cpp_directive
915 static void parse_cpp_directive( struct file *source, char *str )
917 while (*str && isspace(*str)) str++;
918 if (*str++ != '#') return;
919 while (*str && isspace(*str)) str++;
921 if (!strncmp( str, "include", 7 ))
922 parse_include_directive( source, str + 7 );
923 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
924 parse_include_directive( source, str + 6 );
925 else if (!strncmp( str, "pragma", 6 ))
926 parse_pragma_directive( source, str + 6 );
930 /*******************************************************************
931 * parse_idl_file
933 static void parse_idl_file( struct file *source, FILE *file )
935 char *buffer, *include;
937 input_line = 0;
939 while ((buffer = get_line( file )))
941 char quote;
942 char *p = buffer;
943 while (*p && isspace(*p)) p++;
945 if (!strncmp( p, "importlib", 9 ))
947 p += 9;
948 while (*p && isspace(*p)) p++;
949 if (*p++ != '(') continue;
950 while (*p && isspace(*p)) p++;
951 if (*p++ != '"') continue;
952 include = p;
953 while (*p && (*p != '"')) p++;
954 if (!*p) fatal_error( "malformed importlib directive\n" );
955 *p = 0;
956 add_dependency( source, include, INCL_IMPORTLIB );
957 continue;
960 if (!strncmp( p, "import", 6 ))
962 p += 6;
963 while (*p && isspace(*p)) p++;
964 if (*p != '"') continue;
965 include = ++p;
966 while (*p && (*p != '"')) p++;
967 if (!*p) fatal_error( "malformed import directive\n" );
968 *p = 0;
969 add_dependency( source, include, INCL_IMPORT );
970 continue;
973 /* check for #include inside cpp_quote */
974 if (!strncmp( p, "cpp_quote", 9 ))
976 p += 9;
977 while (*p && isspace(*p)) p++;
978 if (*p++ != '(') continue;
979 while (*p && isspace(*p)) p++;
980 if (*p++ != '"') continue;
981 if (*p++ != '#') continue;
982 while (*p && isspace(*p)) p++;
983 if (strncmp( p, "include", 7 )) continue;
984 p += 7;
985 while (*p && isspace(*p)) p++;
986 if (*p == '\\' && p[1] == '"')
988 p += 2;
989 quote = '"';
991 else
993 if (*p++ != '<' ) continue;
994 quote = '>';
996 include = p;
997 while (*p && (*p != quote)) p++;
998 if (!*p || (quote == '"' && p[-1] != '\\'))
999 fatal_error( "malformed #include directive inside cpp_quote\n" );
1000 if (quote == '"') p--; /* remove backslash */
1001 *p = 0;
1002 add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
1003 continue;
1006 parse_cpp_directive( source, p );
1010 /*******************************************************************
1011 * parse_c_file
1013 static void parse_c_file( struct file *source, FILE *file )
1015 char *buffer;
1017 input_line = 0;
1018 while ((buffer = get_line( file )))
1020 parse_cpp_directive( source, buffer );
1025 /*******************************************************************
1026 * parse_rc_file
1028 static void parse_rc_file( struct file *source, FILE *file )
1030 char *buffer, *include;
1032 input_line = 0;
1033 while ((buffer = get_line( file )))
1035 char quote;
1036 char *p = buffer;
1037 while (*p && isspace(*p)) p++;
1039 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1041 p += 2;
1042 while (*p && isspace(*p)) p++;
1043 if (strncmp( p, "@makedep:", 9 )) continue;
1044 p += 9;
1045 while (*p && isspace(*p)) p++;
1046 quote = '"';
1047 if (*p == quote)
1049 include = ++p;
1050 while (*p && *p != quote) p++;
1052 else
1054 include = p;
1055 while (*p && !isspace(*p) && *p != '*') p++;
1057 if (!*p)
1058 fatal_error( "malformed makedep comment\n" );
1059 *p = 0;
1060 add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
1061 continue;
1064 parse_cpp_directive( source, buffer );
1069 /*******************************************************************
1070 * parse_in_file
1072 static void parse_in_file( struct file *source, FILE *file )
1074 char *p, *buffer;
1076 /* make sure it gets rebuilt when the version changes */
1077 add_dependency( source, "config.h", INCL_SYSTEM );
1079 if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
1081 input_line = 0;
1082 while ((buffer = get_line( file )))
1084 if (strncmp( buffer, ".TH", 3 )) continue;
1085 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1086 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1087 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1088 source->args = xstrdup( p );
1089 return;
1094 /*******************************************************************
1095 * parse_sfd_file
1097 static void parse_sfd_file( struct file *source, FILE *file )
1099 char *p, *eol, *buffer;
1101 input_line = 0;
1102 while ((buffer = get_line( file )))
1104 if (strncmp( buffer, "UComments:", 10 )) continue;
1105 p = buffer + 10;
1106 while (*p == ' ') p++;
1107 if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
1109 p++;
1110 buffer[strlen(buffer) - 1] = 0;
1112 while ((eol = strstr( p, "+AAoA" )))
1114 *eol = 0;
1115 while (*p && isspace(*p)) p++;
1116 if (*p++ == '#')
1118 while (*p && isspace(*p)) p++;
1119 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1121 p = eol + 5;
1123 while (*p && isspace(*p)) p++;
1124 if (*p++ != '#') return;
1125 while (*p && isspace(*p)) p++;
1126 if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
1127 return;
1132 static const struct
1134 const char *ext;
1135 void (*parse)( struct file *file, FILE *f );
1136 } parse_functions[] =
1138 { ".c", parse_c_file },
1139 { ".h", parse_c_file },
1140 { ".inl", parse_c_file },
1141 { ".l", parse_c_file },
1142 { ".m", parse_c_file },
1143 { ".rh", parse_c_file },
1144 { ".x", parse_c_file },
1145 { ".y", parse_c_file },
1146 { ".idl", parse_idl_file },
1147 { ".rc", parse_rc_file },
1148 { ".in", parse_in_file },
1149 { ".sfd", parse_sfd_file }
1152 /*******************************************************************
1153 * load_file
1155 static struct file *load_file( const char *name )
1157 struct file *file;
1158 FILE *f;
1159 unsigned int i, hash = hash_filename( name );
1161 LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
1162 if (!strcmp( name, file->name )) return file;
1164 if (!(f = fopen( name, "r" ))) return NULL;
1166 file = add_file( name );
1167 list_add_tail( &files[hash], &file->entry );
1168 input_file_name = file->name;
1169 input_line = 0;
1171 for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
1173 if (!strendswith( name, parse_functions[i].ext )) continue;
1174 parse_functions[i].parse( file, f );
1175 break;
1178 fclose( f );
1179 input_file_name = NULL;
1181 return file;
1185 /*******************************************************************
1186 * open_include_path_file
1188 * Open a file from a directory on the include path.
1190 static struct file *open_include_path_file( const struct makefile *make, const char *dir,
1191 const char *name, char **filename )
1193 char *src_path = concat_paths( dir, name );
1194 struct file *ret = load_file( src_path );
1196 if (ret) *filename = src_path;
1197 return ret;
1201 /*******************************************************************
1202 * open_file_same_dir
1204 * Open a file in the same directory as the parent.
1206 static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
1208 char *src_path = replace_filename( parent->file->name, name );
1209 struct file *ret = load_file( src_path );
1211 if (ret) *filename = replace_filename( parent->filename, name );
1212 return ret;
1216 /*******************************************************************
1217 * open_same_dir_generated_file
1219 * Open a generated_file in the same directory as the parent.
1221 static struct file *open_same_dir_generated_file( const struct makefile *make,
1222 const struct incl_file *parent, struct incl_file *file,
1223 const char *ext, const char *src_ext )
1225 char *filename;
1226 struct file *ret = NULL;
1228 if (strendswith( file->name, ext ) &&
1229 (ret = open_file_same_dir( parent, replace_extension( file->name, ext, src_ext ), &filename )))
1231 file->sourcename = filename;
1232 file->filename = obj_dir_path( make, replace_filename( parent->name, file->name ));
1234 return ret;
1238 /*******************************************************************
1239 * open_local_file
1241 * Open a file in the source directory of the makefile.
1243 static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
1245 char *src_path = src_dir_path( make, path );
1246 struct file *ret = load_file( src_path );
1248 /* if not found, try parent dir */
1249 if (!ret && make->parent_dir)
1251 free( src_path );
1252 path = strmake( "%s/%s", make->parent_dir, path );
1253 src_path = src_dir_path( make, path );
1254 ret = load_file( src_path );
1257 if (ret) *filename = src_path;
1258 return ret;
1262 /*******************************************************************
1263 * open_local_generated_file
1265 * Open a generated file in the directory of the makefile.
1267 static struct file *open_local_generated_file( const struct makefile *make, struct incl_file *file,
1268 const char *ext, const char *src_ext )
1270 char *filename;
1271 struct file *ret = NULL;
1273 if (strendswith( file->name, ext ) &&
1274 (ret = open_local_file( make, replace_extension( file->name, ext, src_ext ), &filename )))
1276 file->sourcename = filename;
1277 file->filename = obj_dir_path( make, file->name );
1279 return ret;
1283 /*******************************************************************
1284 * open_global_file
1286 * Open a file in the top-level source directory.
1288 static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
1290 char *src_path = root_src_dir_path( path );
1291 struct file *ret = load_file( src_path );
1293 if (ret) *filename = src_path;
1294 return ret;
1298 /*******************************************************************
1299 * open_global_header
1301 * Open a file in the global include source directory.
1303 static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
1305 if (!strncmp( path, "../", 3 )) return NULL;
1306 return open_global_file( make, strmake( "include/%s", path ), filename );
1310 /*******************************************************************
1311 * open_global_generated_file
1313 * Open a generated file in the top-level source directory.
1315 static struct file *open_global_generated_file( const struct makefile *make, struct incl_file *file,
1316 const char *ext, const char *src_ext )
1318 char *filename;
1319 struct file *ret = NULL;
1321 if (strendswith( file->name, ext ) &&
1322 (ret = open_global_header( make, replace_extension( file->name, ext, src_ext ), &filename )))
1324 file->sourcename = filename;
1325 file->filename = strmake( "include/%s", file->name );
1327 return ret;
1331 /*******************************************************************
1332 * open_src_file
1334 static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
1336 struct file *file = open_local_file( make, pFile->name, &pFile->filename );
1338 if (!file) fatal_perror( "open %s", pFile->name );
1339 return file;
1343 /*******************************************************************
1344 * find_importlib_module
1346 static struct makefile *find_importlib_module( const char *name )
1348 unsigned int i, len;
1350 for (i = 0; i < subdirs.count; i++)
1352 if (strncmp( submakes[i]->obj_dir, "dlls/", 5 )) continue;
1353 len = strlen(submakes[i]->obj_dir);
1354 if (strncmp( submakes[i]->obj_dir + 5, name, len - 5 )) continue;
1355 if (!name[len - 5] || !strcmp( name + len - 5, ".dll" )) return submakes[i];
1357 return NULL;
1361 /*******************************************************************
1362 * has_external_import
1364 static int has_external_import( const struct makefile *make )
1366 unsigned int i;
1368 for (i = 0; i < make->imports.count; i++)
1370 if (!strncmp( make->imports.str[i], "-l", 2 ))
1371 return 1;
1373 return 0;
1377 /*******************************************************************
1378 * open_include_file
1380 static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
1382 struct file *file = NULL;
1383 unsigned int i, len;
1385 errno = ENOENT;
1387 /* check for generated files */
1388 if ((file = open_local_generated_file( make, pFile, ".tab.h", ".y" ))) return file;
1389 if ((file = open_local_generated_file( make, pFile, ".h", ".idl" ))) return file;
1390 if (fontforge && (file = open_local_generated_file( make, pFile, ".ttf", ".sfd" ))) return file;
1391 if (convert && rsvg && icotool)
1393 if ((file = open_local_generated_file( make, pFile, ".bmp", ".svg" ))) return file;
1394 if ((file = open_local_generated_file( make, pFile, ".cur", ".svg" ))) return file;
1395 if ((file = open_local_generated_file( make, pFile, ".ico", ".svg" ))) return file;
1398 /* check for extra targets */
1399 if (strarray_exists( &make->extra_targets, pFile->name ))
1401 pFile->sourcename = src_dir_path( make, pFile->name );
1402 pFile->filename = obj_dir_path( make, pFile->name );
1403 return NULL;
1406 /* now try in source dir */
1407 if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
1409 /* check for global importlib (module dependency) */
1410 if (pFile->type == INCL_IMPORTLIB && find_importlib_module( pFile->name ))
1412 pFile->filename = pFile->name;
1413 return NULL;
1416 /* check for generated files in global includes */
1417 if ((file = open_global_generated_file( make, pFile, ".h", ".idl" ))) return file;
1418 if ((file = open_global_generated_file( make, pFile, ".h", ".h.in" ))) return file;
1419 if (strendswith( pFile->name, "tmpl.h" ) &&
1420 (file = open_global_generated_file( make, pFile, ".h", ".x" ))) return file;
1422 /* check in global includes source dir */
1423 if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
1425 /* check in global msvcrt includes */
1426 if (pFile->use_msvcrt &&
1427 (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
1428 return file;
1430 /* now search in include paths */
1431 for (i = 0; i < make->include_paths.count; i++)
1433 const char *dir = make->include_paths.str[i];
1435 if (root_src_dir)
1437 len = strlen( root_src_dir );
1438 if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
1440 while (dir[len] == '/') len++;
1441 file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
1444 else
1446 if (*dir == '/') continue;
1447 file = open_include_path_file( make, dir, pFile->name, &pFile->filename );
1449 if (!file) continue;
1450 pFile->is_external = 1;
1451 return file;
1454 if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt &&
1455 !make->extlib && !pFile->included_by->is_external)
1457 if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
1458 if (!strcmp( pFile->name, "x86intrin.h" )) return NULL;
1459 if (has_external_import( make )) return NULL;
1460 fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
1461 pFile->included_by->file->name, pFile->included_line, pFile->name );
1462 exit(1);
1465 if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
1467 /* try in src file directory */
1468 if ((file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".tab.h", ".y" )) ||
1469 (file = open_same_dir_generated_file( make, pFile->included_by, pFile, ".h", ".idl" )) ||
1470 (file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename )))
1472 pFile->is_external = pFile->included_by->is_external;
1473 return file;
1476 if (make->extlib) return NULL; /* ignore missing files in external libs */
1478 fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
1479 perror( pFile->name );
1480 pFile = pFile->included_by;
1481 while (pFile && pFile->included_by)
1483 const char *parent = pFile->included_by->sourcename;
1484 if (!parent) parent = pFile->included_by->file->name;
1485 fprintf( stderr, "%s:%d: note: %s was first included here\n",
1486 parent, pFile->included_line, pFile->name );
1487 pFile = pFile->included_by;
1489 exit(1);
1493 /*******************************************************************
1494 * add_all_includes
1496 static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
1498 unsigned int i;
1500 for (i = 0; i < file->deps_count; i++)
1502 switch (file->deps[i].type)
1504 case INCL_NORMAL:
1505 case INCL_IMPORT:
1506 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1507 break;
1508 case INCL_IMPORTLIB:
1509 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
1510 break;
1511 case INCL_SYSTEM:
1512 add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1513 break;
1514 case INCL_CPP_QUOTE:
1515 case INCL_CPP_QUOTE_SYSTEM:
1516 break;
1522 /*******************************************************************
1523 * parse_file
1525 static void parse_file( struct makefile *make, struct incl_file *source, int src )
1527 struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
1529 if (!file) return;
1531 source->file = file;
1532 source->files_count = 0;
1533 source->files_size = file->deps_count;
1534 source->files = xmalloc( source->files_size * sizeof(*source->files) );
1535 if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
1536 else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
1538 if (source->sourcename)
1540 if (strendswith( source->sourcename, ".idl" ))
1542 unsigned int i;
1544 /* generated .h file always includes these */
1545 add_include( make, source, "rpc.h", 0, INCL_NORMAL );
1546 add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
1547 for (i = 0; i < file->deps_count; i++)
1549 switch (file->deps[i].type)
1551 case INCL_IMPORT:
1552 if (strendswith( file->deps[i].name, ".idl" ))
1553 add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
1554 file->deps[i].line, INCL_NORMAL );
1555 else
1556 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1557 break;
1558 case INCL_CPP_QUOTE:
1559 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
1560 break;
1561 case INCL_CPP_QUOTE_SYSTEM:
1562 add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
1563 break;
1564 case INCL_NORMAL:
1565 case INCL_SYSTEM:
1566 case INCL_IMPORTLIB:
1567 break;
1570 return;
1572 if (strendswith( source->sourcename, ".y" ))
1573 return; /* generated .tab.h doesn't include anything */
1576 add_all_includes( make, source, file );
1580 /*******************************************************************
1581 * add_src_file
1583 * Add a source file to the list.
1585 static struct incl_file *add_src_file( struct makefile *make, const char *name )
1587 struct incl_file *file;
1589 if ((file = find_src_file( make, name ))) return file; /* we already have it */
1590 file = xmalloc( sizeof(*file) );
1591 memset( file, 0, sizeof(*file) );
1592 file->name = xstrdup(name);
1593 file->use_msvcrt = make->use_msvcrt;
1594 file->is_external = !!make->extlib;
1595 list_add_tail( &make->sources, &file->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 );
1745 file = open_input_makefile( make );
1746 while ((buffer = get_line( file )))
1748 if (!strncmp( buffer, separator, strlen(separator) )) break;
1749 if (*buffer == '\t') continue; /* command */
1750 while (isspace( *buffer )) buffer++;
1751 if (*buffer == '#') continue; /* comment */
1752 set_make_variable( &make->vars, buffer );
1754 fclose( file );
1755 input_file_name = NULL;
1756 return make;
1760 /*******************************************************************
1761 * add_generated_sources
1763 static void add_generated_sources( struct makefile *make )
1765 unsigned int i;
1766 struct incl_file *source, *next, *file;
1767 struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
1769 LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
1771 if (source->file->flags & FLAG_IDL_CLIENT)
1773 file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
1774 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1775 add_all_includes( make, file, file->file );
1777 if (source->file->flags & FLAG_IDL_SERVER)
1779 file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
1780 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1781 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1782 add_all_includes( make, file, file->file );
1784 if (source->file->flags & FLAG_IDL_IDENT)
1786 file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
1787 add_dependency( file->file, "rpc.h", INCL_NORMAL );
1788 add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
1789 add_dependency( file->file, "guiddef.h", INCL_NORMAL );
1790 add_all_includes( make, file, file->file );
1792 if (source->file->flags & FLAG_IDL_PROXY)
1794 file = add_generated_source( make, "dlldata.o", "dlldata.c" );
1795 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1796 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1797 add_all_includes( make, file, file->file );
1798 file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
1799 add_dependency( file->file, "objbase.h", INCL_NORMAL );
1800 add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
1801 add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
1802 add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
1803 add_all_includes( make, file, file->file );
1805 if (source->file->flags & FLAG_IDL_TYPELIB)
1807 add_generated_source( make, replace_extension( source->name, ".idl", "_l.res" ), NULL );
1809 if (source->file->flags & FLAG_IDL_REGTYPELIB)
1811 add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
1813 if (source->file->flags & FLAG_IDL_REGISTER)
1815 add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
1817 if (source->file->flags & FLAG_IDL_HEADER)
1819 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1821 if (!source->file->flags && strendswith( source->name, ".idl" ))
1823 add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
1825 if (strendswith( source->name, ".x" ))
1827 add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
1829 if (strendswith( source->name, ".y" ))
1831 file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
1832 /* steal the includes list from the source file */
1833 file->files_count = source->files_count;
1834 file->files_size = source->files_size;
1835 file->files = source->files;
1836 source->files_count = source->files_size = 0;
1837 source->files = NULL;
1839 if (strendswith( source->name, ".l" ))
1841 file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
1842 /* steal the includes list from the source file */
1843 file->files_count = source->files_count;
1844 file->files_size = source->files_size;
1845 file->files = source->files;
1846 source->files_count = source->files_size = 0;
1847 source->files = NULL;
1849 if (source->file->flags & FLAG_C_IMPLIB)
1851 if (!make->staticimplib && make->importlib && *dll_ext)
1852 make->staticimplib = strmake( "lib%s.a", make->importlib );
1854 if (strendswith( source->name, ".po" ))
1856 if (!make->disabled)
1857 strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
1859 if (strendswith( source->name, ".spec" ))
1861 char *obj = replace_extension( source->name, ".spec", "" );
1862 strarray_addall_uniq( &make->extra_imports,
1863 get_expanded_file_local_var( make, obj, "IMPORTS" ));
1866 if (make->testdll)
1868 file = add_generated_source( make, "testlist.o", "testlist.c" );
1869 add_dependency( file->file, "wine/test.h", INCL_NORMAL );
1870 add_all_includes( make, file, file->file );
1872 for (i = 0; i < objs.count; i++)
1874 /* default to .c for unknown extra object files */
1875 if (strendswith( objs.str[i], ".o" ))
1877 file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
1878 file->file->flags |= FLAG_C_UNIX;
1879 file->use_msvcrt = 0;
1881 else if (strendswith( objs.str[i], ".res" ))
1882 add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
1883 else
1884 add_generated_source( make, objs.str[i], NULL );
1889 /*******************************************************************
1890 * create_dir
1892 static void create_dir( const char *dir )
1894 char *p, *path;
1896 p = path = xstrdup( dir );
1897 while ((p = strchr( p, '/' )))
1899 *p = 0;
1900 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1901 *p++ = '/';
1902 while (*p == '/') p++;
1904 if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
1905 free( path );
1909 /*******************************************************************
1910 * create_file_directories
1912 * Create the base directories of all the files.
1914 static void create_file_directories( const struct makefile *make, struct strarray files )
1916 struct strarray subdirs = empty_strarray;
1917 unsigned int i;
1918 char *dir;
1920 for (i = 0; i < files.count; i++)
1922 if (!strchr( files.str[i], '/' )) continue;
1923 dir = obj_dir_path( make, files.str[i] );
1924 *strrchr( dir, '/' ) = 0;
1925 strarray_add_uniq( &subdirs, dir );
1928 for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
1932 /*******************************************************************
1933 * output_filenames_obj_dir
1935 static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
1937 unsigned int i;
1939 for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
1943 /*******************************************************************
1944 * get_dependencies
1946 static void get_dependencies( struct incl_file *file, struct incl_file *source )
1948 unsigned int i;
1950 if (!file->filename) return;
1952 if (file != source)
1954 if (file->owner == source) return; /* already processed */
1955 if (file->type == INCL_IMPORTLIB)
1957 if (!(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
1958 return; /* library is imported only when building a typelib */
1959 strarray_add( &source->importlibdeps, file->filename );
1961 else strarray_add( &source->dependencies, file->filename );
1962 file->owner = source;
1964 /* sanity checks */
1965 if (!strcmp( file->filename, "include/config.h" ) &&
1966 file != source->files[0] && !source->is_external)
1968 input_file_name = source->filename;
1969 input_line = 0;
1970 for (i = 0; i < source->file->deps_count; i++)
1972 if (!strcmp( source->file->deps[i].name, file->name ))
1973 input_line = source->file->deps[i].line;
1975 fatal_error( "%s must be included before other headers\n", file->name );
1979 for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
1983 /*******************************************************************
1984 * get_local_dependencies
1986 * Get the local dependencies of a given target.
1988 static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
1989 struct strarray targets )
1991 unsigned int i;
1992 struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
1994 for (i = 0; i < deps.count; i++)
1996 if (strarray_exists( &targets, deps.str[i] ))
1997 deps.str[i] = obj_dir_path( make, deps.str[i] );
1998 else
1999 deps.str[i] = src_dir_path( make, deps.str[i] );
2001 return deps;
2005 /*******************************************************************
2006 * get_static_lib
2008 * Check if makefile builds the named static library and return the full lib path.
2010 static const char *get_static_lib( const struct makefile *make, const char *name )
2012 if (!make->staticlib) return NULL;
2013 if (make->disabled) return NULL;
2014 if (strncmp( make->staticlib, "lib", 3 )) return NULL;
2015 if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
2016 if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
2017 return obj_dir_path( make, make->staticlib );
2021 /*******************************************************************
2022 * get_native_unix_lib
2024 static const char *get_native_unix_lib( const struct makefile *make, const char *name )
2026 if (!make->native_unix_lib) return NULL;
2027 if (strncmp( make->unixlib, name, strlen(name) )) return NULL;
2028 if (make->unixlib[strlen(name)] != '.') return NULL;
2029 return obj_dir_path( make, make->unixlib );
2033 /*******************************************************************
2034 * get_parent_makefile
2036 static struct makefile *get_parent_makefile( struct makefile *make )
2038 char *dir, *p;
2039 int i;
2041 if (!make->obj_dir) return NULL;
2042 dir = xstrdup( make->obj_dir );
2043 if (!(p = strrchr( dir, '/' ))) return NULL;
2044 *p = 0;
2045 for (i = 0; i < subdirs.count; i++)
2046 if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
2047 return NULL;
2051 /*******************************************************************
2052 * needs_delay_lib
2054 static int needs_delay_lib( const struct makefile *make )
2056 if (delay_load_flag) return 0;
2057 if (*dll_ext && !crosstarget) return 0;
2058 if (!make->importlib) return 0;
2059 return strarray_exists( &delay_import_libs, make->importlib );
2063 /*******************************************************************
2064 * add_unix_libraries
2066 static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
2068 struct strarray ret = empty_strarray;
2069 struct strarray all_libs = empty_strarray;
2070 unsigned int i, j;
2072 if (strcmp( make->unixlib, "ntdll.so" )) strarray_add( &all_libs, "-lntdll" );
2073 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
2075 for (i = 0; i < all_libs.count; i++)
2077 const char *lib = NULL;
2079 if (!strncmp( all_libs.str[i], "-l", 2 ))
2081 for (j = 0; j < subdirs.count; j++)
2083 if (make == submakes[j]) continue;
2084 if ((lib = get_native_unix_lib( submakes[j], all_libs.str[i] + 2 ))) break;
2087 if (lib)
2089 strarray_add( deps, lib );
2090 strarray_add( &ret, lib );
2092 else strarray_add( &ret, all_libs.str[i] );
2095 strarray_addall( &ret, libs );
2096 return ret;
2100 /*******************************************************************
2101 * is_crt_module
2103 static int is_crt_module( const char *file )
2105 return !strncmp( file, "msvcr", 5 ) || !strncmp( file, "ucrt", 4 ) || !strcmp( file, "crtdll.dll" );
2109 /*******************************************************************
2110 * get_default_crt
2112 static const char *get_default_crt( const struct makefile *make )
2114 if (!make->use_msvcrt) return NULL;
2115 if (make->module && is_crt_module( make->module )) return NULL; /* don't add crt import to crt dlls */
2116 return !make->testdll && (!make->staticlib || make->extlib) ? "ucrtbase" : "msvcrt";
2120 /*******************************************************************
2121 * get_crt_define
2123 static const char *get_crt_define( const struct makefile *make )
2125 const char *crt_dll = NULL;
2126 unsigned int i, version = 0;
2128 for (i = 0; i < make->imports.count; i++)
2130 if (!is_crt_module( make->imports.str[i] )) continue;
2131 if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n",
2132 crt_dll, make->imports.str[i] );
2133 crt_dll = make->imports.str[i];
2136 if (!crt_dll)
2138 if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return "-D_MSVCR_VER=0";
2139 if (!(crt_dll = get_default_crt( make ))) crt_dll = make->module;
2141 if (!strncmp( crt_dll, "ucrt", 4 )) return "-D_UCRT";
2142 sscanf( crt_dll, "msvcr%u", &version );
2143 return strmake( "-D_MSVCR_VER=%u", version );
2147 /*******************************************************************
2148 * add_default_imports
2150 static struct strarray add_default_imports( const struct makefile *make, struct strarray imports )
2152 struct strarray ret = empty_strarray;
2153 const char *crt_dll = get_default_crt( make );
2154 unsigned int i;
2156 for (i = 0; i < imports.count; i++)
2158 if (is_crt_module( imports.str[i] )) crt_dll = imports.str[i];
2159 else strarray_add( &ret, imports.str[i] );
2162 strarray_add( &ret, "winecrt0" );
2163 if (crt_dll) strarray_add( &ret, crt_dll );
2165 if (make->is_win16 && (!make->importlib || strcmp( make->importlib, "kernel" )))
2166 strarray_add( &ret, "kernel" );
2168 strarray_add( &ret, "kernel32" );
2169 strarray_add( &ret, "ntdll" );
2170 return ret;
2174 /*******************************************************************
2175 * add_import_libs
2177 static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
2178 struct strarray imports, int delay, int is_cross )
2180 struct strarray ret = empty_strarray;
2181 unsigned int i, j;
2183 for (i = 0; i < imports.count; i++)
2185 const char *name = imports.str[i];
2186 const char *lib = NULL;
2188 if (name[0] == '-')
2190 switch (name[1])
2192 case 'L': strarray_add( &ret, name ); continue;
2193 case 'l': name += 2; break;
2194 default: continue;
2197 else name = get_base_name( name );
2199 for (j = 0; j < subdirs.count; j++)
2201 if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
2202 lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
2203 else
2204 lib = get_static_lib( submakes[j], name );
2205 if (lib) break;
2208 if (lib)
2210 const char *ext = NULL;
2212 if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
2213 else if (is_cross) ext = ".cross.a";
2214 if (ext) lib = replace_extension( lib, ".a", ext );
2215 strarray_add_uniq( deps, lib );
2216 strarray_add( &ret, lib );
2218 else strarray_add( &ret, strmake( "-l%s", name ));
2220 return ret;
2224 /*******************************************************************
2225 * add_install_rule
2227 static void add_install_rule( struct makefile *make, const char *target,
2228 const char *file, const char *dest )
2230 if (strarray_exists( &make->install_lib, target ) ||
2231 strarray_exists( &top_install_lib, make->obj_dir ) ||
2232 strarray_exists( &top_install_lib, obj_dir_path( make, target )))
2234 strarray_add( &make->install_rules[INSTALL_LIB], file );
2235 strarray_add( &make->install_rules[INSTALL_LIB], dest );
2237 else if (strarray_exists( &make->install_dev, target ) ||
2238 strarray_exists( &top_install_dev, make->obj_dir ) ||
2239 strarray_exists( &top_install_dev, obj_dir_path( make, target )))
2241 strarray_add( &make->install_rules[INSTALL_DEV], file );
2242 strarray_add( &make->install_rules[INSTALL_DEV], dest );
2247 /*******************************************************************
2248 * get_include_install_path
2250 * Determine the installation path for a given include file.
2252 static const char *get_include_install_path( const char *name )
2254 if (!strncmp( name, "wine/", 5 )) return name + 5;
2255 if (!strncmp( name, "msvcrt/", 7 )) return name;
2256 return strmake( "windows/%s", name );
2260 /*******************************************************************
2261 * get_shared_library_name
2263 * Determine possible names for a shared library with a version number.
2265 static struct strarray get_shared_lib_names( const char *libname )
2267 struct strarray ret = empty_strarray;
2268 const char *ext, *p;
2269 char *name, *first, *second;
2270 size_t len = 0;
2272 strarray_add( &ret, libname );
2274 for (p = libname; (p = strchr( p, '.' )); p++)
2275 if ((len = strspn( p + 1, "0123456789." ))) break;
2277 if (!len) return ret;
2278 ext = p + 1 + len;
2279 if (*ext && ext[-1] == '.') ext--;
2281 /* keep only the first group of digits */
2282 name = xstrdup( libname );
2283 first = name + (p - libname);
2284 if ((second = strchr( first + 1, '.' )))
2286 strcpy( second, ext );
2287 strarray_add( &ret, xstrdup( name ));
2289 return ret;
2293 /*******************************************************************
2294 * get_source_defines
2296 static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
2297 const char *obj )
2299 unsigned int i;
2300 struct strarray ret = empty_strarray;
2302 strarray_addall( &ret, make->include_args );
2303 if (source->use_msvcrt)
2304 strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
2305 for (i = 0; i < make->include_paths.count; i++)
2306 strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
2307 strarray_addall( &ret, make->define_args );
2308 strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
2309 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
2310 return ret;
2314 /*******************************************************************
2315 * remove_warning_flags
2317 static struct strarray remove_warning_flags( struct strarray flags )
2319 unsigned int i;
2320 struct strarray ret = empty_strarray;
2322 for (i = 0; i < flags.count; i++)
2323 if (strncmp( flags.str[i], "-W", 2 ) || !strncmp( flags.str[i], "-Wno-", 5 ))
2324 strarray_add( &ret, flags.str[i] );
2325 return ret;
2329 /*******************************************************************
2330 * get_debug_file
2332 static const char *get_debug_file( struct makefile *make, const char *name )
2334 const char *debug_file = NULL;
2335 if (!make->is_cross || !crossdebug) return NULL;
2336 if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
2337 else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
2338 if (debug_file) strarray_add( &make->debug_files, debug_file );
2339 return debug_file;
2343 /*******************************************************************
2344 * cmd_prefix
2346 static const char *cmd_prefix( const char *cmd )
2348 if (!silent_rules) return "";
2349 return strmake( "$(quiet_%s)", cmd );
2353 /*******************************************************************
2354 * output_winegcc_command
2356 static void output_winegcc_command( struct makefile *make, int is_cross )
2358 output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" ));
2359 output_filename( "--wine-objdir ." );
2360 if (tools_dir)
2362 output_filename( "--winebuild" );
2363 output_filename( tools_path( make, "winebuild" ));
2365 if (is_cross)
2367 output_filename( "-b" );
2368 output_filename( crosstarget );
2369 output_filename( "--lib-suffix=.cross.a" );
2371 else
2373 output_filenames( target_flags );
2374 output_filenames( lddll_flags );
2379 /*******************************************************************
2380 * output_symlink_rule
2382 * Output a rule to create a symlink.
2384 static void output_symlink_rule( const char *src_name, const char *link_name, int create_dir )
2386 const char *name = strrchr( link_name, '/' );
2387 char *dir = NULL;
2389 if (name)
2391 dir = xstrdup( link_name );
2392 dir[name - link_name] = 0;
2395 output( "\t%s", cmd_prefix( "LN" ));
2396 if (create_dir && dir && *dir) output( "%s -d %s && ", root_src_dir_path( "tools/install-sh" ), dir );
2397 output( "rm -f %s && ", link_name );
2399 /* dest path with a directory needs special handling if ln -s isn't supported */
2400 if (dir && strcmp( ln_s, "ln -s" ))
2401 output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
2402 else
2403 output( "%s %s %s\n", ln_s, src_name, link_name );
2405 free( dir );
2409 /*******************************************************************
2410 * output_srcdir_symlink
2412 * Output rule to create a symlink back to the source directory, for source files
2413 * that are needed at run-time.
2415 static void output_srcdir_symlink( struct makefile *make, const char *obj )
2417 char *src_file, *dst_file, *src_name;
2419 if (!make->src_dir) return;
2420 src_file = src_dir_path( make, obj );
2421 dst_file = obj_dir_path( make, obj );
2422 output( "%s: %s\n", dst_file, src_file );
2424 src_name = src_file;
2425 if (src_name[0] != '/' && make->obj_dir)
2426 src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
2428 output_symlink_rule( src_name, dst_file, 0 );
2429 strarray_add( &make->all_targets, obj );
2433 /*******************************************************************
2434 * output_install_commands
2436 static void output_install_commands( struct makefile *make, struct strarray files )
2438 unsigned int i;
2439 char *install_sh = root_src_dir_path( "tools/install-sh" );
2441 for (i = 0; i < files.count; i += 2)
2443 const char *file = files.str[i];
2444 const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
2446 switch (*files.str[i + 1])
2448 case 'c': /* cross-compiled program */
2449 output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2450 crosstarget, install_sh, obj_dir_path( make, file ), dest );
2451 output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
2452 break;
2453 case 'd': /* data file */
2454 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2455 install_sh, obj_dir_path( make, file ), dest );
2456 break;
2457 case 'D': /* data file in source dir */
2458 output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
2459 install_sh, src_dir_path( make, file ), dest );
2460 break;
2461 case 'p': /* program file */
2462 output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
2463 install_sh, obj_dir_path( make, file ), dest );
2464 break;
2465 case 's': /* script */
2466 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2467 install_sh, obj_dir_path( make, file ), dest );
2468 break;
2469 case 'S': /* script in source dir */
2470 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2471 install_sh, src_dir_path( make, file ), dest );
2472 break;
2473 case 't': /* script in tools dir */
2474 output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
2475 install_sh, tools_dir_path( make, files.str[i] ), dest );
2476 break;
2477 case 'y': /* symlink */
2478 output_symlink_rule( files.str[i], dest, 1 );
2479 break;
2480 default:
2481 assert(0);
2483 strarray_add( &make->uninstall_files, dest );
2488 /*******************************************************************
2489 * output_install_rules
2491 * Rules are stored as a (file,dest) pair of values.
2492 * The first char of dest indicates the type of install.
2494 static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
2496 unsigned int i;
2497 struct strarray files = make->install_rules[rules];
2498 struct strarray targets = empty_strarray;
2500 if (!files.count) return;
2502 for (i = 0; i < files.count; i += 2)
2504 const char *file = files.str[i];
2505 switch (*files.str[i + 1])
2507 case 'c': /* cross-compiled program */
2508 case 'd': /* data file */
2509 case 'p': /* program file */
2510 case 's': /* script */
2511 strarray_add_uniq( &targets, obj_dir_path( make, file ));
2512 break;
2513 case 't': /* script in tools dir */
2514 strarray_add_uniq( &targets, tools_dir_path( make, file ));
2515 break;
2519 output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
2520 output_filenames( targets );
2521 output( "\n" );
2522 output_install_commands( make, files );
2523 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
2524 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
2528 static int cmp_string_length( const char **a, const char **b )
2530 int paths_a = 0, paths_b = 0;
2531 const char *p;
2533 for (p = *a; *p; p++) if (*p == '/') paths_a++;
2534 for (p = *b; *p; p++) if (*p == '/') paths_b++;
2535 if (paths_b != paths_a) return paths_b - paths_a;
2536 return strcmp( *a, *b );
2539 /*******************************************************************
2540 * output_uninstall_rules
2542 static void output_uninstall_rules( struct makefile *make )
2544 static const char *dirs_order[] =
2545 { "$(includedir)", "$(mandir)", "$(fontdir)", "$(nlsdir)", "$(datadir)", "$(dlldir)" };
2547 struct strarray uninstall_dirs = empty_strarray;
2548 unsigned int i, j;
2550 if (!make->uninstall_files.count) return;
2551 output( "uninstall::\n" );
2552 output_rm_filenames( make->uninstall_files );
2553 strarray_add_uniq( &make->phony_targets, "uninstall" );
2555 if (!subdirs.count) return;
2556 for (i = 0; i < make->uninstall_files.count; i++)
2558 char *dir = xstrdup( make->uninstall_files.str[i] );
2559 while (strchr( dir, '/' ))
2561 *strrchr( dir, '/' ) = 0;
2562 strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
2565 strarray_qsort( &uninstall_dirs, cmp_string_length );
2566 output( "\t-rmdir" );
2567 for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
2569 for (j = 0; j < uninstall_dirs.count; j++)
2571 if (!uninstall_dirs.str[j]) continue;
2572 if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
2573 continue;
2574 output_filename( uninstall_dirs.str[j] );
2575 uninstall_dirs.str[j] = NULL;
2578 for (j = 0; j < uninstall_dirs.count; j++)
2579 if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
2580 output( "\n" );
2584 /*******************************************************************
2585 * output_po_files
2587 static void output_po_files( struct makefile *make )
2589 const char *po_dir = src_dir_path( make, "po" );
2590 unsigned int i;
2592 if (linguas.count)
2594 for (i = 0; i < linguas.count; i++)
2595 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2596 output( ": %s/wine.pot\n", po_dir );
2597 output( "\t%smsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
2598 cmd_prefix( "MSG" ), po_dir );
2599 output( "po/all:" );
2600 for (i = 0; i < linguas.count; i++)
2601 output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
2602 output( "\n" );
2604 output( "%s/wine.pot:", po_dir );
2605 output_filenames( make->pot_files );
2606 output( "\n" );
2607 output( "\t%smsgcat -o $@", cmd_prefix( "MSG" ));
2608 output_filenames( make->pot_files );
2609 output( "\n" );
2610 strarray_add( &make->maintainerclean_files, strmake( "%s/wine.pot", po_dir ));
2614 /*******************************************************************
2615 * output_source_y
2617 static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
2619 /* add source file dependency for parallel makes */
2620 char *header = strmake( "%s.tab.h", obj );
2622 if (find_include_file( make, header ))
2624 output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
2625 output( "\t%s%s -o %s.tab.c -d %s\n",
2626 cmd_prefix( "BISON" ), bison, obj_dir_path( make, obj ), source->filename );
2627 output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
2628 source->filename, obj_dir_path( make, header ));
2629 strarray_add( &make->clean_files, header );
2631 else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
2633 output( "\t%s%s -o $@ %s\n", cmd_prefix( "BISON" ), bison, source->filename );
2637 /*******************************************************************
2638 * output_source_l
2640 static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
2642 output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
2643 output( "\t%s%s -o$@ %s\n", cmd_prefix( "FLEX" ), flex, source->filename );
2647 /*******************************************************************
2648 * output_source_h
2650 static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
2652 if (source->file->flags & FLAG_GENERATED)
2653 strarray_add( &make->all_targets, source->name );
2654 else
2655 add_install_rule( make, source->name, source->name,
2656 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2660 /*******************************************************************
2661 * output_source_rc
2663 static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
2665 struct strarray defines = get_source_defines( make, source, obj );
2666 const char *po_dir = NULL;
2667 unsigned int i;
2669 if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
2670 if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
2671 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2672 if (source->file->flags & FLAG_RC_PO)
2674 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2675 output( "%s.pot ", obj_dir_path( make, obj ) );
2677 output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
2678 output_filename( tools_path( make, "wrc" ));
2679 output_filenames( source->dependencies );
2680 output( "\n" );
2681 output( "\t%s%s -u -o $@", cmd_prefix( "WRC" ), tools_path( make, "wrc" ) );
2682 if (make->is_win16) output_filename( "-m16" );
2683 output_filename( "--nostdinc" );
2684 if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
2685 output_filenames( defines );
2686 output_filename( source->filename );
2687 output( "\n" );
2688 if (po_dir)
2690 output( "%s.res:", obj_dir_path( make, obj ));
2691 for (i = 0; i < linguas.count; i++)
2692 output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
2693 output( "\n" );
2698 /*******************************************************************
2699 * output_source_mc
2701 static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
2703 unsigned int i;
2704 char *obj_path = obj_dir_path( make, obj );
2706 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2707 strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
2708 output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
2709 output_filename( tools_path( make, "wmc" ));
2710 output_filenames( source->dependencies );
2711 output( "\n" );
2712 output( "\t%s%s -u -o $@ %s", cmd_prefix( "WMC" ), tools_path( make, "wmc" ), source->filename );
2713 if (linguas.count)
2715 output_filename( "--po-dir=po" );
2716 output( "\n" );
2717 output( "%s.res:", obj_dir_path( make, obj ));
2718 for (i = 0; i < linguas.count; i++)
2719 output_filename( strmake( "po/%s.mo", linguas.str[i] ));
2721 output( "\n" );
2725 /*******************************************************************
2726 * output_source_res
2728 static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
2730 strarray_add( &make->res_files, source->name );
2734 /*******************************************************************
2735 * output_source_idl
2737 static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
2739 struct strarray defines = get_source_defines( make, source, obj );
2740 struct strarray targets = empty_strarray;
2741 char *dest;
2742 unsigned int i;
2744 if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
2745 if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
2747 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
2749 if (!(source->file->flags & idl_outputs[i].flag)) continue;
2750 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
2751 if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
2752 strarray_add( &targets, dest );
2754 if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
2755 if (source->file->flags & FLAG_INSTALL)
2757 add_install_rule( make, source->name, xstrdup( source->name ),
2758 strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
2759 if (source->file->flags & FLAG_IDL_HEADER)
2760 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2761 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2763 if (!targets.count) return;
2765 output_filenames_obj_dir( make, targets );
2766 output( ": %s\n", tools_path( make, "widl" ));
2767 output( "\t%s%s -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ) );
2768 output_filenames( target_flags );
2769 output_filename( "--nostdinc" );
2770 output_filename( "-Ldlls/\\*" );
2771 output_filenames( defines );
2772 output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
2773 output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
2774 output_filename( source->filename );
2775 output( "\n" );
2776 output_filenames_obj_dir( make, targets );
2777 output( ": %s", source->filename );
2778 output_filenames( source->dependencies );
2779 for (i = 0; i < source->importlibdeps.count; i++)
2781 struct makefile *submake = find_importlib_module( source->importlibdeps.str[i] );
2782 output_filename( obj_dir_path( submake, submake->module ));
2784 output( "\n" );
2788 /*******************************************************************
2789 * output_source_x
2791 static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
2793 output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
2794 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2795 output( "\t%s%s%s -H -o $@ %s\n", cmd_prefix( "GEN" ),
2796 tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
2797 if (source->file->flags & FLAG_INSTALL)
2799 add_install_rule( make, source->name, source->name,
2800 strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
2801 add_install_rule( make, source->name, strmake( "%s.h", obj ),
2802 strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
2807 /*******************************************************************
2808 * output_source_sfd
2810 static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
2812 unsigned int i;
2813 char *ttf_obj = strmake( "%s.ttf", obj );
2814 char *ttf_file = src_dir_path( make, ttf_obj );
2816 if (fontforge && !make->src_dir)
2818 output( "%s: %s\n", ttf_file, source->filename );
2819 output( "\t%s%s -script %s %s $@\n", cmd_prefix( "GEN" ),
2820 fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
2821 if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
2822 strarray_add( &make->maintainerclean_files, ttf_obj );
2824 if (source->file->flags & FLAG_INSTALL)
2826 add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
2827 output_srcdir_symlink( make, ttf_obj );
2830 if (source->file->flags & FLAG_SFD_FONTS)
2832 struct strarray *array = source->file->args;
2834 for (i = 0; i < array->count; i++)
2836 char *font = strtok( xstrdup(array->str[i]), " \t" );
2837 char *args = strtok( NULL, "" );
2839 strarray_add( &make->all_targets, xstrdup( font ));
2840 output( "%s: %s %s\n", obj_dir_path( make, font ),
2841 tools_path( make, "sfnt2fon" ), ttf_file );
2842 output( "\t%s%s -q -o $@ %s %s\n", cmd_prefix( "GEN" ),
2843 tools_path( make, "sfnt2fon" ), ttf_file, args );
2844 add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
2850 /*******************************************************************
2851 * output_source_svg
2853 static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
2855 static const char * const images[] = { "bmp", "cur", "ico", NULL };
2856 unsigned int i;
2858 if (convert && rsvg && icotool)
2860 for (i = 0; images[i]; i++)
2861 if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
2863 if (images[i])
2865 output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
2866 output( "\t%sCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
2867 cmd_prefix( "GEN" ), convert, icotool, rsvg,
2868 root_src_dir_path( "tools/buildimage" ), source->filename );
2869 strarray_add( &make->maintainerclean_files, strmake( "%s.%s", obj, images[i] ));
2875 /*******************************************************************
2876 * output_source_nls
2878 static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
2880 add_install_rule( make, source->name, source->name,
2881 strmake( "D$(nlsdir)/%s", source->name ));
2882 output_srcdir_symlink( make, strmake( "%s.nls", obj ));
2886 /*******************************************************************
2887 * output_source_desktop
2889 static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
2891 add_install_rule( make, source->name, source->name,
2892 strmake( "D$(datadir)/applications/%s", source->name ));
2896 /*******************************************************************
2897 * output_source_po
2899 static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
2901 output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
2902 output( "\t%s%s -o $@ %s\n", cmd_prefix( "MSG" ), msgfmt, source->filename );
2903 strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
2907 /*******************************************************************
2908 * output_source_in
2910 static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
2912 unsigned int i;
2914 if (strendswith( obj, ".man" ) && source->file->args)
2916 struct strarray symlinks;
2917 char *dir, *dest = replace_extension( obj, ".man", "" );
2918 char *lang = strchr( dest, '.' );
2919 char *section = source->file->args;
2920 if (lang)
2922 *lang++ = 0;
2923 dir = strmake( "$(mandir)/%s/man%s", lang, section );
2925 else dir = strmake( "$(mandir)/man%s", section );
2926 add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
2927 symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
2928 for (i = 0; i < symlinks.count; i++)
2929 add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
2930 strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
2931 free( dest );
2932 free( dir );
2934 strarray_add( &make->in_files, xstrdup(obj) );
2935 strarray_add( &make->all_targets, xstrdup(obj) );
2936 output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
2937 output( "\t%s%s %s >$@ || (rm -f $@ && false)\n", cmd_prefix( "SED" ), sed_cmd, source->filename );
2938 output( "%s:", obj_dir_path( make, obj ));
2939 output_filenames( source->dependencies );
2940 output( "\n" );
2941 add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
2945 /*******************************************************************
2946 * output_source_spec
2948 static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
2950 struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
2951 struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
2952 struct strarray all_libs, dep_libs = empty_strarray;
2953 char *dll_name, *obj_name, *output_file;
2954 const char *debug_file;
2956 if (!imports.count) imports = make->imports;
2957 if (!dll_flags.count) dll_flags = make->extradllflags;
2958 if (!strarray_exists( &dll_flags, "-nodefaultlibs" )) imports = add_default_imports( make, imports );
2960 all_libs = add_import_libs( make, &dep_libs, imports, 0, make->is_cross );
2961 dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
2962 obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
2963 output_file = obj_dir_path( make, dll_name );
2965 strarray_add( &make->clean_files, dll_name );
2966 strarray_add( &make->res_files, strmake( "%s.res", obj ));
2967 output( "%s.res:", obj_dir_path( make, obj ));
2968 output_filename( obj_dir_path( make, dll_name ));
2969 output_filename( tools_path( make, "wrc" ));
2970 output( "\n" );
2971 output( "\t%secho \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ), obj, output_file,
2972 tools_path( make, "wrc" ));
2974 output( "%s:", output_file);
2975 output_filename( source->filename );
2976 output_filename( obj_name );
2977 output_filenames( dep_libs );
2978 output_filename( tools_path( make, "winebuild" ));
2979 output_filename( tools_path( make, "winegcc" ));
2980 output( "\n" );
2981 output_winegcc_command( make, make->is_cross );
2982 output_filename( "-s" );
2983 output_filenames( dll_flags );
2984 output_filename( "-shared" );
2985 output_filename( source->filename );
2986 output_filename( obj_name );
2987 if ((debug_file = get_debug_file( make, dll_name )))
2988 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
2989 output_filenames( all_libs );
2990 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
2991 output( "\n" );
2995 /*******************************************************************
2996 * output_source_default
2998 static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
3000 struct strarray defines = get_source_defines( make, source, obj );
3001 int is_dll_src = (make->testdll &&
3002 strendswith( source->name, ".c" ) &&
3003 find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
3004 int need_cross = (crosstarget &&
3005 !(source->file->flags & FLAG_C_UNIX) &&
3006 (make->is_cross || make->staticlib ||
3007 (source->file->flags & FLAG_C_IMPLIB)));
3008 int need_obj = ((*dll_ext || !(source->file->flags & FLAG_C_UNIX)) &&
3009 (!need_cross ||
3010 (source->file->flags & FLAG_C_IMPLIB) ||
3011 (make->staticlib && !make->extlib)));
3013 if ((source->file->flags & FLAG_GENERATED) &&
3014 (!make->testdll || !strendswith( source->filename, "testlist.c" )))
3015 strarray_add( &make->clean_files, source->basename );
3017 if (need_obj)
3019 if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
3020 strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
3021 else if (source->file->flags & FLAG_C_IMPLIB)
3022 strarray_add( &make->implib_files, strmake( "%s.o", obj ));
3023 else if (!is_dll_src)
3024 strarray_add( &make->object_files, strmake( "%s.o", obj ));
3025 else
3026 strarray_add( &make->clean_files, strmake( "%s.o", obj ));
3027 output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
3028 output( "\t%s$(CC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3029 output_filenames( defines );
3030 if (make->sharedlib || (source->file->flags & FLAG_C_UNIX))
3032 output_filenames( unix_dllflags );
3034 else if (make->module || make->testdll)
3036 output_filenames( dll_flags );
3037 if (source->use_msvcrt) output_filenames( msvcrt_flags );
3038 if (!*dll_ext && make->module && is_crt_module( make->module ))
3039 output_filename( "-fno-builtin" );
3041 output_filenames( make->extlib ? extra_cflags_extlib : extra_cflags );
3042 output_filenames( cpp_flags );
3043 output_filename( "$(CFLAGS)" );
3044 output( "\n" );
3046 if (need_cross)
3048 if (source->file->flags & FLAG_C_IMPLIB)
3049 strarray_add( &make->crossimplib_files, strmake( "%s.cross.o", obj ));
3050 else if (!is_dll_src)
3051 strarray_add( &make->crossobj_files, strmake( "%s.cross.o", obj ));
3052 else
3053 strarray_add( &make->clean_files, strmake( "%s.cross.o", obj ));
3054 output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
3055 output( "\t%s$(CROSSCC) -c -o $@ %s", cmd_prefix( "CC" ), source->filename );
3056 output_filenames( defines );
3057 output_filenames( make->extlib ? extra_cross_cflags_extlib : extra_cross_cflags );
3058 if (make->module && is_crt_module( make->module ))
3059 output_filename( "-fno-builtin" );
3060 output_filenames( cpp_flags );
3061 output_filename( "$(CROSSCFLAGS)" );
3062 output( "\n" );
3064 if (make->testdll && !is_dll_src && strendswith( source->name, ".c" ) &&
3065 !(source->file->flags & FLAG_GENERATED))
3067 strarray_add( &make->test_files, obj );
3068 strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
3069 output( "%s.ok:\n", obj_dir_path( make, obj ));
3070 output( "\t%s%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
3071 cmd_prefix( "TEST" ),
3072 root_src_dir_path( "tools/runtest" ), make->testdll,
3073 obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
3074 make->is_cross ? "" : dll_ext, obj );
3076 if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
3077 if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
3078 output( ":" );
3079 output_filenames( source->dependencies );
3080 output( "\n" );
3084 /* dispatch table to output rules for a single source file */
3085 static const struct
3087 const char *ext;
3088 void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
3089 } output_source_funcs[] =
3091 { "y", output_source_y },
3092 { "l", output_source_l },
3093 { "h", output_source_h },
3094 { "rh", output_source_h },
3095 { "inl", output_source_h },
3096 { "rc", output_source_rc },
3097 { "mc", output_source_mc },
3098 { "res", output_source_res },
3099 { "idl", output_source_idl },
3100 { "sfd", output_source_sfd },
3101 { "svg", output_source_svg },
3102 { "nls", output_source_nls },
3103 { "desktop", output_source_desktop },
3104 { "po", output_source_po },
3105 { "in", output_source_in },
3106 { "x", output_source_x },
3107 { "spec", output_source_spec },
3108 { NULL, output_source_default }
3112 /*******************************************************************
3113 * get_unix_lib_name
3115 static char *get_unix_lib_name( struct makefile *make )
3117 struct incl_file *source;
3119 if (!*dll_ext) return NULL;
3120 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3122 if (!(source->file->flags & FLAG_C_UNIX)) continue;
3123 return strmake( "%s%s", get_base_name( make->module ), dll_ext );
3125 return NULL;
3129 /*******************************************************************
3130 * output_module
3132 static void output_module( struct makefile *make )
3134 struct strarray all_libs = empty_strarray;
3135 struct strarray dep_libs = empty_strarray;
3136 struct strarray imports = make->imports;
3137 char *module_name = strmake( "%s%s", make->module, make->is_cross ? "" : dll_ext );
3138 const char *debug_file;
3139 const char *delay_load = delay_load_flag;
3140 char *spec_file = NULL;
3141 unsigned int i;
3143 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3144 if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
3145 imports = add_default_imports( make, imports );
3147 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, make->is_cross ));
3148 strarray_addall( &all_libs, add_import_libs( make, &dep_libs, imports, 0, make->is_cross ));
3150 if (!make->use_msvcrt)
3152 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3153 strarray_addall( &all_libs, libs );
3156 if (!make->is_cross && *dll_ext) delay_load = "-Wl,-delayload,";
3157 if (delay_load)
3159 for (i = 0; i < make->delayimports.count; i++)
3160 strarray_add( &all_libs, strmake( "%s%s%s", delay_load, make->delayimports.str[i],
3161 strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
3163 strarray_add( &make->all_targets, module_name );
3165 if (make->is_cross)
3166 add_install_rule( make, make->module, module_name, strmake( "c%s/%s", pe_dir, module_name ));
3167 else if (*dll_ext)
3168 add_install_rule( make, make->module, module_name, strmake( "p%s/%s", so_dir, module_name ));
3169 else
3170 add_install_rule( make, make->module, module_name,
3171 strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", module_name ));
3173 output( "%s:", obj_dir_path( make, module_name ));
3174 if (spec_file) output_filename( spec_file );
3175 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3176 output_filenames_obj_dir( make, make->res_files );
3177 output_filenames( dep_libs );
3178 output_filename( tools_path( make, "winebuild" ));
3179 output_filename( tools_path( make, "winegcc" ));
3180 output( "\n" );
3181 output_winegcc_command( make, make->is_cross );
3182 if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
3183 if (spec_file)
3185 output_filename( "-shared" );
3186 output_filename( spec_file );
3188 output_filenames( make->extradllflags );
3189 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3190 output_filenames_obj_dir( make, make->res_files );
3191 debug_file = get_debug_file( make, make->module );
3192 if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3193 output_filenames( all_libs );
3194 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3195 output( "\n" );
3197 if (*dll_ext && make->is_exe && !make->is_win16 && strendswith( make->module, ".exe" ))
3199 char *binary = replace_extension( make->module, ".exe", "" );
3200 add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
3205 /*******************************************************************
3206 * output_fake_module
3208 static void output_fake_module( struct makefile *make )
3210 char *spec_file = NULL;
3212 if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3214 strarray_add( &make->all_targets, make->module );
3215 add_install_rule( make, make->module, make->module, strmake( "d%s/%s", pe_dir, make->module ));
3217 output( "%s:", obj_dir_path( make, make->module ));
3218 if (spec_file) output_filename( spec_file );
3219 output_filenames_obj_dir( make, make->res_files );
3220 output_filename( tools_path( make, "winebuild" ));
3221 output_filename( tools_path( make, "winegcc" ));
3222 output( "\n" );
3223 output_winegcc_command( make, make->is_cross );
3224 output_filename( "-Wb,--fake-module" );
3225 if (spec_file)
3227 output_filename( "-shared" );
3228 output_filename( spec_file );
3230 output_filenames( make->extradllflags );
3231 output_filenames_obj_dir( make, make->res_files );
3232 output( "\n" );
3236 /*******************************************************************
3237 * output_import_lib
3239 static void output_import_lib( struct makefile *make )
3241 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3242 char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
3244 strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
3245 if (!*dll_ext && needs_delay_lib( make ))
3247 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3248 output( "%s.delay.a ", importlib_path );
3250 output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
3251 output_filenames_obj_dir( make, make->implib_files );
3252 output( "\n" );
3253 output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) );
3254 output_filenames( target_flags );
3255 if (make->is_win16) output_filename( "-m16" );
3256 output_filename( "--export" );
3257 output_filename( spec_file );
3258 output_filenames_obj_dir( make, make->implib_files );
3259 output( "\n" );
3260 add_install_rule( make, make->importlib,
3261 strmake( "lib%s.a", make->importlib ),
3262 strmake( "d%s/lib%s.a", so_dir, make->importlib ));
3264 if (crosstarget)
3266 strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
3267 output_filename( strmake( "%s.cross.a", importlib_path ));
3268 if (needs_delay_lib( make ))
3270 strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
3271 output_filename( strmake( "%s.delay.a", importlib_path ));
3273 output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
3274 output_filenames_obj_dir( make, make->crossimplib_files );
3275 output( "\n" );
3276 output( "\t%s%s -b %s -w --implib -o $@", cmd_prefix( "BUILD" ),
3277 tools_path( make, "winebuild" ), crosstarget );
3278 if (make->is_win16) output_filename( "-m16" );
3279 output_filename( "--export" );
3280 output_filename( spec_file );
3281 output_filenames_obj_dir( make, make->crossimplib_files );
3282 output( "\n" );
3283 add_install_rule( make, make->importlib,
3284 strmake( "lib%s.cross.a", make->importlib ),
3285 strmake( "d%s/lib%s.a", pe_dir, make->importlib ));
3290 /*******************************************************************
3291 * output_unix_lib
3293 static void output_unix_lib( struct makefile *make )
3295 struct strarray unix_libs = empty_strarray;
3296 struct strarray unix_deps = empty_strarray;
3297 char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
3299 if (!make->native_unix_lib)
3301 struct strarray unix_imports = empty_strarray;
3303 strarray_add( &unix_imports, "ntdll" );
3304 strarray_add( &unix_deps, obj_dir_path( top_makefile, "dlls/ntdll/ntdll.so" ));
3305 strarray_add( &unix_imports, "winecrt0" );
3306 if (spec_file) strarray_add( &unix_deps, spec_file );
3308 strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 0 ));
3309 strarray_addall( &unix_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3310 strarray_addall( &unix_libs, libs );
3312 else unix_libs = add_unix_libraries( make, &unix_deps );
3314 strarray_add( &make->all_targets, make->unixlib );
3315 add_install_rule( make, make->module, make->unixlib, strmake( "p%s/%s", so_dir, make->unixlib ));
3316 output( "%s:", obj_dir_path( make, make->unixlib ));
3317 output_filenames_obj_dir( make, make->unixobj_files );
3318 output_filenames( unix_deps );
3320 if (make->native_unix_lib)
3322 output( "\n" );
3323 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3324 output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" ));
3326 else
3328 output_filename( tools_path( make, "winebuild" ));
3329 output_filename( tools_path( make, "winegcc" ));
3330 output( "\n" );
3331 output_winegcc_command( make, 0 );
3332 output_filename( "-munix" );
3333 output_filename( "-shared" );
3334 if (spec_file) output_filename( spec_file );
3336 output_filenames_obj_dir( make, make->unixobj_files );
3337 output_filenames( unix_libs );
3338 output_filename( "$(LDFLAGS)" );
3339 output( "\n" );
3343 /*******************************************************************
3344 * output_static_lib
3346 static void output_static_lib( struct makefile *make )
3348 if (!crosstarget || !make->extlib)
3350 strarray_add( &make->clean_files, make->staticlib );
3351 output( "%s: %s", obj_dir_path( make, make->staticlib ), tools_path( make, "winebuild" ));
3352 output_filenames_obj_dir( make, make->object_files );
3353 output_filenames_obj_dir( make, make->unixobj_files );
3354 output( "\n" );
3355 output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ));
3356 output_filenames( target_flags );
3357 output_filenames_obj_dir( make, make->object_files );
3358 output_filenames_obj_dir( make, make->unixobj_files );
3359 output( "\n" );
3360 add_install_rule( make, make->staticlib, make->staticlib,
3361 strmake( "d%s/%s", so_dir, make->staticlib ));
3363 if (crosstarget)
3365 char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
3367 strarray_add( &make->clean_files, name );
3368 output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
3369 output_filenames_obj_dir( make, make->crossobj_files );
3370 output( "\n" );
3371 output( "\t%s%s -b %s -w --staticlib -o $@", cmd_prefix( "BUILD" ),
3372 tools_path( make, "winebuild" ), crosstarget );
3373 output_filenames_obj_dir( make, make->crossobj_files );
3374 output( "\n" );
3375 if (!make->extlib)
3376 add_install_rule( make, make->staticlib, name,
3377 strmake( "d%s/%s", pe_dir, make->staticlib ));
3382 /*******************************************************************
3383 * output_shared_lib
3385 static void output_shared_lib( struct makefile *make )
3387 unsigned int i;
3388 char *basename, *p;
3389 struct strarray names = get_shared_lib_names( make->sharedlib );
3390 struct strarray all_libs = empty_strarray;
3391 struct strarray dep_libs = empty_strarray;
3393 basename = xstrdup( make->sharedlib );
3394 if ((p = strchr( basename, '.' ))) *p = 0;
3396 strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
3397 strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
3398 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3399 strarray_addall( &all_libs, libs );
3401 output( "%s:", obj_dir_path( make, make->sharedlib ));
3402 output_filenames_obj_dir( make, make->object_files );
3403 output_filenames( dep_libs );
3404 output( "\n" );
3405 output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" ));
3406 output_filenames_obj_dir( make, make->object_files );
3407 output_filenames( all_libs );
3408 output_filename( "$(LDFLAGS)" );
3409 output( "\n" );
3410 add_install_rule( make, make->sharedlib, make->sharedlib, strmake( "p%s/%s", so_dir, make->sharedlib ));
3411 for (i = 1; i < names.count; i++)
3413 output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
3414 output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ), 0 );
3415 add_install_rule( make, names.str[i], names.str[i-1], strmake( "y%s/%s", so_dir, names.str[i] ));
3417 strarray_addall( &make->all_targets, names );
3421 /*******************************************************************
3422 * output_test_module
3424 static void output_test_module( struct makefile *make )
3426 char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
3427 char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
3428 char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
3429 struct strarray dep_libs = empty_strarray;
3430 struct strarray all_libs = add_import_libs( make, &dep_libs, add_default_imports( make, make->imports ),
3431 0, make->is_cross );
3432 struct makefile *parent = get_parent_makefile( make );
3433 const char *ext = make->is_cross ? "" : dll_ext;
3434 const char *debug_file;
3435 char *output_file;
3437 strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
3438 strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
3439 output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
3440 output( "%s:\n", output_file );
3441 output_winegcc_command( make, make->is_cross );
3442 output_filenames( make->extradllflags );
3443 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3444 output_filenames_obj_dir( make, make->res_files );
3445 if ((debug_file = get_debug_file( make, testmodule )))
3446 output_filename( strmake( "-Wl,--debug-file,%s", obj_dir_path( make, debug_file )));
3447 output_filenames( all_libs );
3448 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3449 output( "\n" );
3450 output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
3451 output_winegcc_command( make, make->is_cross );
3452 output_filename( "-s" );
3453 output_filename( strmake( "-Wb,-F,%s", testmodule ));
3454 output_filenames( make->extradllflags );
3455 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3456 output_filenames_obj_dir( make, make->res_files );
3457 output_filenames( all_libs );
3458 output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
3459 output( "\n" );
3460 output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
3461 output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
3462 output_filenames_obj_dir( make, make->res_files );
3463 output_filenames( dep_libs );
3464 output_filename( tools_path( make, "winebuild" ));
3465 output_filename( tools_path( make, "winegcc" ));
3466 output( "\n" );
3468 output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
3469 output( "\t%secho \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n", cmd_prefix( "WRC" ),
3470 testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
3472 output_filenames_obj_dir( make, make->ok_files );
3473 output( ": %s%s", obj_dir_path( make, testmodule ), ext );
3474 if (parent)
3476 output_filename( parent->is_cross ? obj_dir_path( parent, make->testdll )
3477 : strmake( "%s%s", obj_dir_path( parent, make->testdll ), dll_ext ));
3478 if (parent->unixlib) output_filename( obj_dir_path( parent, parent->unixlib ));
3480 output( "\n" );
3481 output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
3482 if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
3483 output( "\n" );
3484 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
3485 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
3486 output( "%s::\n", obj_dir_path( make, "testclean" ));
3487 output( "\trm -f" );
3488 output_filenames_obj_dir( make, make->ok_files );
3489 output( "\n" );
3490 strarray_addall( &make->clean_files, make->ok_files );
3491 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
3495 /*******************************************************************
3496 * output_programs
3498 static void output_programs( struct makefile *make )
3500 unsigned int i, j;
3502 for (i = 0; i < make->programs.count; i++)
3504 char *program_installed = NULL;
3505 char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
3506 struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
3507 struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
3508 struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
3509 struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
3511 if (!objs.count) objs = make->object_files;
3512 if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
3514 strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
3515 strarray_addall( &all_libs, libs );
3518 output( "%s:", obj_dir_path( make, program ) );
3519 output_filenames_obj_dir( make, objs );
3520 output_filenames( deps );
3521 output( "\n" );
3522 output( "\t%s$(CC) -o $@", cmd_prefix( "CC" ));
3523 output_filenames_obj_dir( make, objs );
3524 output_filenames( all_libs );
3525 output_filename( "$(LDFLAGS)" );
3526 output( "\n" );
3527 strarray_add( &make->all_targets, program );
3529 for (j = 0; j < symlinks.count; j++)
3531 output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
3532 output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ), 0 );
3534 strarray_addall( &make->all_targets, symlinks );
3536 add_install_rule( make, program, program_installed ? program_installed : program,
3537 strmake( "p$(bindir)/%s", program ));
3538 for (j = 0; j < symlinks.count; j++)
3539 add_install_rule( make, symlinks.str[j], program,
3540 strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
3545 /*******************************************************************
3546 * output_subdirs
3548 static void output_subdirs( struct makefile *make )
3550 struct strarray all_targets = empty_strarray;
3551 struct strarray makefile_deps = empty_strarray;
3552 struct strarray clean_files = empty_strarray;
3553 struct strarray testclean_files = empty_strarray;
3554 struct strarray distclean_files = empty_strarray;
3555 struct strarray dependencies = empty_strarray;
3556 struct strarray install_lib_deps = empty_strarray;
3557 struct strarray install_dev_deps = empty_strarray;
3558 struct strarray tooldeps_deps = empty_strarray;
3559 struct strarray buildtest_deps = empty_strarray;
3560 unsigned int i;
3562 strarray_addall( &clean_files, make->clean_files );
3563 strarray_addall( &distclean_files, make->distclean_files );
3564 strarray_addall( &all_targets, make->all_targets );
3565 for (i = 0; i < subdirs.count; i++)
3567 strarray_add( &makefile_deps, src_dir_path( submakes[i], "Makefile.in" ));
3568 strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
3569 strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
3570 strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
3571 strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
3572 strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
3573 strarray_addall_path( &make->maintainerclean_files, submakes[i]->obj_dir, submakes[i]->maintainerclean_files );
3574 strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
3575 strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
3577 if (submakes[i]->disabled) continue;
3579 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
3580 strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->font_files );
3581 if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
3582 strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
3583 if (submakes[i]->testdll)
3584 strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
3585 if (submakes[i]->install_rules[INSTALL_LIB].count)
3586 strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
3587 if (submakes[i]->install_rules[INSTALL_DEV].count)
3588 strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
3590 strarray_addall( &dependencies, makefile_deps );
3591 output( "all:" );
3592 output_filenames( all_targets );
3593 output( "\n" );
3594 output( "Makefile:" );
3595 output_filenames( makefile_deps );
3596 output( "\n" );
3597 output_filenames( dependencies );
3598 output( ":\n" );
3599 if (install_lib_deps.count)
3601 output( "install install-lib::" );
3602 output_filenames( install_lib_deps );
3603 output( "\n" );
3604 strarray_add_uniq( &make->phony_targets, "install" );
3605 strarray_add_uniq( &make->phony_targets, "install-lib" );
3607 if (install_dev_deps.count)
3609 output( "install install-dev::" );
3610 output_filenames( install_dev_deps );
3611 output( "\n" );
3612 strarray_add_uniq( &make->phony_targets, "install" );
3613 strarray_add_uniq( &make->phony_targets, "install-dev" );
3615 output_uninstall_rules( make );
3616 if (buildtest_deps.count)
3618 output( "buildtests:" );
3619 output_filenames( buildtest_deps );
3620 output( "\n" );
3621 strarray_add_uniq( &make->phony_targets, "buildtests" );
3623 output( "check test:" );
3624 output_filenames( testclean_files );
3625 output( "\n" );
3626 strarray_add_uniq( &make->phony_targets, "check" );
3627 strarray_add_uniq( &make->phony_targets, "test" );
3629 if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
3631 output( "clean::\n");
3632 output_rm_filenames( clean_files );
3633 output( "testclean::\n");
3634 output_rm_filenames( testclean_files );
3635 output( "distclean::\n");
3636 output_rm_filenames( distclean_files );
3637 output( "maintainer-clean::\n");
3638 output_rm_filenames( make->maintainerclean_files );
3639 strarray_add_uniq( &make->phony_targets, "distclean" );
3640 strarray_add_uniq( &make->phony_targets, "testclean" );
3641 strarray_add_uniq( &make->phony_targets, "maintainer-clean" );
3643 if (tooldeps_deps.count)
3645 output( "__tooldeps__:" );
3646 output_filenames( tooldeps_deps );
3647 output( "\n" );
3648 strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
3651 if (make->phony_targets.count)
3653 output( ".PHONY:" );
3654 output_filenames( make->phony_targets );
3655 output( "\n" );
3660 /*******************************************************************
3661 * output_sources
3663 static void output_sources( struct makefile *make )
3665 struct strarray all_targets = empty_strarray;
3666 struct incl_file *source;
3667 unsigned int i, j;
3669 strarray_add_uniq( &make->phony_targets, "all" );
3671 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3673 char *obj = xstrdup( source->name );
3674 char *ext = get_extension( obj );
3676 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
3677 *ext++ = 0;
3679 for (j = 0; output_source_funcs[j].ext; j++)
3680 if (!strcmp( ext, output_source_funcs[j].ext )) break;
3682 output_source_funcs[j].fn( make, source, obj );
3683 strarray_addall_uniq( &make->dependencies, source->dependencies );
3686 /* special case for winetest: add resource files from other test dirs */
3687 if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
3689 struct strarray tests = enable_tests;
3690 if (!tests.count)
3691 for (i = 0; i < subdirs.count; i++)
3692 if (submakes[i]->testdll && !submakes[i]->disabled)
3693 strarray_add( &tests, submakes[i]->testdll );
3694 for (i = 0; i < tests.count; i++)
3695 strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
3698 if (make->dlldata_files.count)
3700 output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
3701 tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
3702 output( "\t%s%s --dlldata-only -o $@", cmd_prefix( "WIDL" ), tools_path( make, "widl" ));
3703 output_filenames( make->dlldata_files );
3704 output( "\n" );
3707 if (make->staticlib) output_static_lib( make );
3708 else if (make->module)
3710 output_module( make );
3711 if (*dll_ext && !make->is_cross) output_fake_module( make );
3712 if (make->unixlib) output_unix_lib( make );
3713 if (make->importlib) output_import_lib( make );
3715 else if (make->testdll) output_test_module( make );
3716 else if (make->sharedlib) output_shared_lib( make );
3717 else if (make->programs.count) output_programs( make );
3719 for (i = 0; i < make->scripts.count; i++)
3720 add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
3721 strmake( "S$(bindir)/%s", make->scripts.str[i] ));
3723 for (i = 0; i < make->extra_targets.count; i++)
3724 if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
3725 strarray_add( &make->clean_files, make->extra_targets.str[i] );
3726 else
3727 strarray_add( &make->all_targets, make->extra_targets.str[i] );
3729 if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
3730 strarray_add( &make->distclean_files, "Makefile" );
3731 if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
3733 if (!make->obj_dir)
3734 strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
3735 else if (!strcmp( make->obj_dir, "po" ))
3736 strarray_add( &make->distclean_files, "LINGUAS" );
3738 strarray_addall( &make->clean_files, make->object_files );
3739 strarray_addall( &make->clean_files, make->crossobj_files );
3740 strarray_addall( &make->clean_files, make->implib_files );
3741 strarray_addall( &make->clean_files, make->crossimplib_files );
3742 strarray_addall( &make->clean_files, make->unixobj_files );
3743 strarray_addall( &make->clean_files, make->res_files );
3744 strarray_addall( &make->clean_files, make->pot_files );
3745 strarray_addall( &make->clean_files, make->debug_files );
3746 strarray_addall( &make->clean_files, make->all_targets );
3748 if (make == top_makefile)
3750 output_subdirs( make );
3751 return;
3754 strarray_addall( &all_targets, make->all_targets );
3755 strarray_addall( &all_targets, make->font_files );
3756 if (all_targets.count)
3758 output( "%s:", obj_dir_path( make, "all" ));
3759 output_filenames_obj_dir( make, all_targets );
3760 output( "\n" );
3761 strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
3763 output_install_rules( make, INSTALL_LIB, "install-lib" );
3764 output_install_rules( make, INSTALL_DEV, "install-dev" );
3766 if (make->clean_files.count)
3768 output( "%s::\n", obj_dir_path( make, "clean" ));
3769 output( "\trm -f" );
3770 output_filenames_obj_dir( make, make->clean_files );
3771 output( "\n" );
3772 strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
3777 /*******************************************************************
3778 * create_temp_file
3780 static FILE *create_temp_file( const char *orig )
3782 char *name = xmalloc( strlen(orig) + 13 );
3783 unsigned int i, id = getpid();
3784 int fd;
3785 FILE *ret = NULL;
3787 for (i = 0; i < 100; i++)
3789 sprintf( name, "%s.tmp%08x", orig, id );
3790 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
3792 ret = fdopen( fd, "w" );
3793 break;
3795 if (errno != EEXIST) break;
3796 id += 7777;
3798 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
3799 temp_file_name = name;
3800 return ret;
3804 /*******************************************************************
3805 * rename_temp_file
3807 static void rename_temp_file( const char *dest )
3809 int ret = rename( temp_file_name, dest );
3810 if (ret == -1 && errno == EEXIST)
3812 /* rename doesn't overwrite on windows */
3813 unlink( dest );
3814 ret = rename( temp_file_name, dest );
3816 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
3817 temp_file_name = NULL;
3821 /*******************************************************************
3822 * are_files_identical
3824 static int are_files_identical( FILE *file1, FILE *file2 )
3826 for (;;)
3828 char buffer1[8192], buffer2[8192];
3829 int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
3830 int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
3831 if (size1 != size2) return 0;
3832 if (!size1) return feof( file1 ) && feof( file2 );
3833 if (memcmp( buffer1, buffer2, size1 )) return 0;
3838 /*******************************************************************
3839 * rename_temp_file_if_changed
3841 static void rename_temp_file_if_changed( const char *dest )
3843 FILE *file1, *file2;
3844 int do_rename = 1;
3846 if ((file1 = fopen( dest, "r" )))
3848 if ((file2 = fopen( temp_file_name, "r" )))
3850 do_rename = !are_files_identical( file1, file2 );
3851 fclose( file2 );
3853 fclose( file1 );
3855 if (!do_rename)
3857 unlink( temp_file_name );
3858 temp_file_name = NULL;
3860 else rename_temp_file( dest );
3864 /*******************************************************************
3865 * output_linguas
3867 static void output_linguas( const struct makefile *make )
3869 const char *dest = obj_dir_path( make, "LINGUAS" );
3870 struct incl_file *source;
3872 output_file = create_temp_file( dest );
3874 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3875 LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
3876 if (strendswith( source->name, ".po" ))
3877 output( "%s\n", replace_extension( source->name, ".po", "" ));
3879 if (fclose( output_file )) fatal_perror( "write" );
3880 output_file = NULL;
3881 rename_temp_file_if_changed( dest );
3885 /*******************************************************************
3886 * output_testlist
3888 static void output_testlist( const struct makefile *make )
3890 const char *dest = obj_dir_path( make, "testlist.c" );
3891 unsigned int i;
3893 output_file = create_temp_file( dest );
3895 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
3896 output( "#define WIN32_LEAN_AND_MEAN\n" );
3897 output( "#include <windows.h>\n\n" );
3898 output( "#define STANDALONE\n" );
3899 output( "#include \"wine/test.h\"\n\n" );
3901 for (i = 0; i < make->test_files.count; i++)
3902 output( "extern void func_%s(void);\n", make->test_files.str[i] );
3903 output( "\n" );
3904 output( "const struct test winetest_testlist[] =\n" );
3905 output( "{\n" );
3906 for (i = 0; i < make->test_files.count; i++)
3907 output( " { \"%s\", func_%s },\n", make->test_files.str[i], make->test_files.str[i] );
3908 output( " { 0, 0 }\n" );
3909 output( "};\n" );
3911 if (fclose( output_file )) fatal_perror( "write" );
3912 output_file = NULL;
3913 rename_temp_file_if_changed( dest );
3917 /*******************************************************************
3918 * output_gitignore
3920 static void output_gitignore( const char *dest, struct strarray files )
3922 int i;
3924 output_file = create_temp_file( dest );
3926 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
3927 for (i = 0; i < files.count; i++)
3929 if (!strchr( files.str[i], '/' )) output( "/" );
3930 output( "%s\n", files.str[i] );
3933 if (fclose( output_file )) fatal_perror( "write" );
3934 output_file = NULL;
3935 rename_temp_file( dest );
3939 /*******************************************************************
3940 * output_stub_makefile
3942 static void output_stub_makefile( struct makefile *make )
3944 struct strarray targets = empty_strarray;
3945 const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
3947 if (make->obj_dir) create_dir( make->obj_dir );
3949 output_file_name = obj_dir_path( make, "Makefile" );
3950 output_file = create_temp_file( output_file_name );
3952 output( "# Auto-generated stub makefile; all rules forward to the top-level makefile\n\n" );
3954 if (make_var) output( "MAKE = %s\n\n", make_var );
3955 output( "all:\n" );
3957 if (make->all_targets.count) strarray_add( &targets, "all" );
3958 if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
3959 strarray_add( &targets, "install" );
3960 if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
3961 if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
3962 if (make->clean_files.count) strarray_add( &targets, "clean" );
3963 if (make->test_files.count)
3965 strarray_add( &targets, "check" );
3966 strarray_add( &targets, "test" );
3967 strarray_add( &targets, "testclean" );
3970 output_filenames( targets );
3971 output_filenames( make->clean_files );
3972 output( ":\n" );
3973 output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
3974 output( ".PHONY:" );
3975 output_filenames( targets );
3976 output( "\n" );
3978 fclose( output_file );
3979 output_file = NULL;
3980 rename_temp_file( output_file_name );
3984 /*******************************************************************
3985 * output_silent_rules
3987 static void output_silent_rules(void)
3989 static const char *cmds[] =
3991 "BISON",
3992 "BUILD",
3993 "CC",
3994 "CCLD",
3995 "FLEX",
3996 "GEN",
3997 "LN",
3998 "MSG",
3999 "SED",
4000 "TEST",
4001 "WIDL",
4002 "WMC",
4003 "WRC"
4005 unsigned int i;
4007 output( "V = 0\n" );
4008 for (i = 0; i < sizeof(cmds) / sizeof(cmds[0]); i++)
4010 output( "quiet_%s = $(quiet_%s_$(V))\n", cmds[i], cmds[i] );
4011 output( "quiet_%s_0 = @echo \" %-5s \" $@;\n", cmds[i], cmds[i] );
4012 output( "quiet_%s_1 =\n", cmds[i] );
4017 /*******************************************************************
4018 * output_top_makefile
4020 static void output_top_makefile( struct makefile *make )
4022 char buffer[1024];
4023 FILE *src_file;
4024 int i, found = 0;
4026 output_file_name = obj_dir_path( make, output_makefile_name );
4027 output_file = create_temp_file( output_file_name );
4029 /* copy the contents of the source makefile */
4030 src_file = open_input_makefile( make );
4031 while (fgets( buffer, sizeof(buffer), src_file ) && !found)
4033 if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
4034 found = !strncmp( buffer, separator, strlen(separator) );
4036 if (fclose( src_file )) fatal_perror( "close" );
4037 input_file_name = NULL;
4039 if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
4041 if (silent_rules) output_silent_rules();
4042 for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
4043 output_sources( make );
4044 /* disable implicit rules */
4045 output( ".SUFFIXES:\n" );
4047 fclose( output_file );
4048 output_file = NULL;
4049 rename_temp_file( output_file_name );
4053 /*******************************************************************
4054 * output_dependencies
4056 static void output_dependencies( struct makefile *make )
4058 struct strarray ignore_files = empty_strarray;
4060 if (make->obj_dir) create_dir( make->obj_dir );
4062 if (make == top_makefile) output_top_makefile( make );
4063 else output_stub_makefile( make );
4065 strarray_addall( &ignore_files, make->distclean_files );
4066 strarray_addall( &ignore_files, make->clean_files );
4067 if (make->testdll) output_testlist( make );
4068 if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
4069 if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
4071 create_file_directories( make, ignore_files );
4073 output_file_name = NULL;
4077 /*******************************************************************
4078 * load_sources
4080 static void load_sources( struct makefile *make )
4082 static const char *source_vars[] =
4084 "SOURCES",
4085 "C_SRCS",
4086 "OBJC_SRCS",
4087 "RC_SRCS",
4088 "MC_SRCS",
4089 "IDL_SRCS",
4090 "BISON_SRCS",
4091 "LEX_SRCS",
4092 "HEADER_SRCS",
4093 "XTEMPLATE_SRCS",
4094 "SVG_SRCS",
4095 "FONT_SRCS",
4096 "IN_SRCS",
4097 "PO_SRCS",
4098 "MANPAGES",
4099 NULL
4101 const char **var;
4102 unsigned int i;
4103 struct strarray value;
4104 struct incl_file *file;
4106 strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
4107 strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
4109 make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
4110 make->module = get_expanded_make_variable( make, "MODULE" );
4111 make->testdll = get_expanded_make_variable( make, "TESTDLL" );
4112 make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
4113 make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
4114 make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
4115 make->extlib = get_expanded_make_variable( make, "EXTLIB" );
4116 if (*dll_ext) make->unixlib = get_expanded_make_variable( make, "UNIXLIB" );
4118 make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
4119 make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
4120 make->imports = get_expanded_make_var_array( make, "IMPORTS" );
4121 make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
4122 make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
4123 make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
4124 make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
4125 make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
4127 if (make->extlib) make->staticlib = make->extlib;
4128 if (make->staticlib) make->module = make->staticlib;
4130 if (host_cpu)
4132 value = get_expanded_file_local_var( make, host_cpu, "EXTRADLLFLAGS" );
4133 if (value.count) make->extradllflags = value;
4136 make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
4137 make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
4138 make->use_msvcrt = (make->module || make->testdll || make->is_win16) &&
4139 !strarray_exists( &make->extradllflags, "-mcygwin" );
4140 make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
4141 strarray_exists( &make->extradllflags, "-mwindows" );
4142 make->native_unix_lib = !!make->unixlib;
4144 if (make->use_msvcrt) strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
4146 if (make->module && !make->install_lib.count && !make->install_dev.count)
4148 if (make->importlib) strarray_add( &make->install_dev, make->importlib );
4149 if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
4150 else strarray_add( &make->install_lib, make->module );
4153 make->include_paths = empty_strarray;
4154 make->include_args = empty_strarray;
4155 make->define_args = empty_strarray;
4156 if (!make->extlib) strarray_add( &make->define_args, "-D__WINESRC__" );
4158 value = get_expanded_make_var_array( make, "EXTRAINCL" );
4159 for (i = 0; i < value.count; i++)
4160 if (!strncmp( value.str[i], "-I", 2 ))
4161 strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
4162 else if (!strncmp( value.str[i], "-D", 2 ) || !strncmp( value.str[i], "-U", 2 ))
4163 strarray_add_uniq( &make->define_args, value.str[i] );
4164 strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
4166 strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
4167 if (make->src_dir)
4168 strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
4169 if (make->parent_dir)
4170 strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
4171 strarray_add( &make->include_args, "-Iinclude" );
4172 if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
4174 list_init( &make->sources );
4175 list_init( &make->includes );
4177 for (var = source_vars; *var; var++)
4179 value = get_expanded_make_var_array( make, *var );
4180 for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
4183 add_generated_sources( make );
4184 if (!make->unixlib) make->unixlib = get_unix_lib_name( make );
4186 if (make->use_msvcrt) strarray_add( &make->define_args, get_crt_define( make ));
4188 LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
4189 LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
4191 make->is_cross = crosstarget && make->use_msvcrt;
4193 if (!*dll_ext || make->is_cross)
4194 for (i = 0; i < make->delayimports.count; i++)
4195 strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
4199 /*******************************************************************
4200 * parse_makeflags
4202 static void parse_makeflags( const char *flags )
4204 const char *p = flags;
4205 char *var, *buffer = xmalloc( strlen(flags) + 1 );
4207 while (*p)
4209 while (isspace(*p)) p++;
4210 var = buffer;
4211 while (*p && !isspace(*p))
4213 if (*p == '\\' && p[1]) p++;
4214 *var++ = *p++;
4216 *var = 0;
4217 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
4222 /*******************************************************************
4223 * parse_option
4225 static int parse_option( const char *opt )
4227 if (opt[0] != '-')
4229 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
4230 return 0;
4232 switch(opt[1])
4234 case 'f':
4235 if (opt[2]) output_makefile_name = opt + 2;
4236 break;
4237 case 'R':
4238 relative_dir_mode = 1;
4239 break;
4240 case 'S':
4241 silent_rules = 1;
4242 break;
4243 default:
4244 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
4245 exit(1);
4247 return 1;
4251 /*******************************************************************
4252 * main
4254 int main( int argc, char *argv[] )
4256 const char *makeflags = getenv( "MAKEFLAGS" );
4257 int i, j;
4259 if (makeflags) parse_makeflags( makeflags );
4261 i = 1;
4262 while (i < argc)
4264 if (parse_option( argv[i] ))
4266 for (j = i; j < argc; j++) argv[j] = argv[j+1];
4267 argc--;
4269 else i++;
4272 if (relative_dir_mode)
4274 char *relpath;
4276 if (argc != 3)
4278 fprintf( stderr, "Option -R needs two directories\n%s", Usage );
4279 exit( 1 );
4281 relpath = get_relative_path( argv[1], argv[2] );
4282 printf( "%s\n", relpath ? relpath : "." );
4283 exit( 0 );
4286 if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
4288 atexit( cleanup_files );
4289 signal( SIGTERM, exit_on_signal );
4290 signal( SIGINT, exit_on_signal );
4291 #ifdef SIGHUP
4292 signal( SIGHUP, exit_on_signal );
4293 #endif
4295 for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
4297 top_makefile = parse_makefile( NULL );
4299 target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
4300 msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
4301 dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
4302 extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
4303 extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
4304 unix_dllflags = get_expanded_make_var_array( top_makefile, "UNIXDLLFLAGS" );
4305 cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
4306 lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
4307 libs = get_expanded_make_var_array( top_makefile, "LIBS" );
4308 enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
4309 top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
4310 top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
4312 delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
4313 root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
4314 tools_dir = get_expanded_make_variable( top_makefile, "toolsdir" );
4315 tools_ext = get_expanded_make_variable( top_makefile, "toolsext" );
4316 exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
4317 dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
4318 host_cpu = get_expanded_make_variable( top_makefile, "host_cpu" );
4319 crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
4320 crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
4321 fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
4322 convert = get_expanded_make_variable( top_makefile, "CONVERT" );
4323 flex = get_expanded_make_variable( top_makefile, "FLEX" );
4324 bison = get_expanded_make_variable( top_makefile, "BISON" );
4325 ar = get_expanded_make_variable( top_makefile, "AR" );
4326 ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
4327 rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
4328 icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
4329 dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
4330 msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
4331 sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
4332 ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
4334 if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
4335 if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
4336 if (!exe_ext) exe_ext = "";
4337 if (!tools_ext) tools_ext = "";
4338 if (host_cpu && (host_cpu = normalize_arch( host_cpu )))
4340 so_dir = strmake( "$(dlldir)/%s-unix", host_cpu );
4341 pe_dir = strmake( "$(dlldir)/%s-windows", host_cpu );
4343 else
4344 so_dir = pe_dir = "$(dlldir)";
4346 extra_cflags_extlib = remove_warning_flags( extra_cflags );
4347 extra_cross_cflags_extlib = remove_warning_flags( extra_cross_cflags );
4349 top_makefile->src_dir = root_src_dir;
4350 subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
4351 disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
4352 submakes = xmalloc( subdirs.count * sizeof(*submakes) );
4354 for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
4356 load_sources( top_makefile );
4357 for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
4359 output_dependencies( top_makefile );
4360 for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
4362 return 0;