scrrun: Implement Next() for file collection.
[wine/multimedia.git] / tools / makedep.c
blob28f60fd3b7a31155d23c30f4a61722a1e6b41326
1 /*
2 * Generate include file dependencies
4 * Copyright 1996, 2013 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <signal.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include "wine/list.h"
38 struct incl_file
40 struct list entry;
41 char *name;
42 char *filename;
43 char *sourcename; /* source file name for generated headers */
44 struct incl_file *included_by; /* file that included this one */
45 int included_line; /* line where this file was included */
46 unsigned int flags; /* flags (see below) */
47 struct incl_file *owner;
48 unsigned int files_count; /* files in use */
49 unsigned int files_size; /* total allocated size */
50 struct incl_file **files;
53 #define FLAG_SYSTEM 0x0001 /* is it a system include (#include <name>) */
54 #define FLAG_GENERATED 0x0002 /* generated file */
55 #define FLAG_IDL_PROXY 0x0004 /* generates a proxy (_p.c) file */
56 #define FLAG_IDL_CLIENT 0x0008 /* generates a client (_c.c) file */
57 #define FLAG_IDL_SERVER 0x0010 /* generates a server (_s.c) file */
58 #define FLAG_IDL_IDENT 0x0020 /* generates an ident (_i.c) file */
59 #define FLAG_IDL_REGISTER 0x0040 /* generates a registration (_r.res) file */
60 #define FLAG_IDL_TYPELIB 0x0080 /* generates a typelib (.tlb) file */
61 #define FLAG_IDL_REGTYPELIB 0x0100 /* generates a registered typelib (_t.res) file */
62 #define FLAG_IDL_HEADER 0x0200 /* generates a header (.h) file */
63 #define FLAG_RC_PO 0x0400 /* rc file contains translations */
64 #define FLAG_C_IMPLIB 0x0800 /* file is part of an import library */
66 static const struct
68 unsigned int flag;
69 const char *ext;
70 } idl_outputs[] =
72 { FLAG_IDL_TYPELIB, ".tlb" },
73 { FLAG_IDL_REGTYPELIB, "_t.res" },
74 { FLAG_IDL_CLIENT, "_c.c" },
75 { FLAG_IDL_IDENT, "_i.c" },
76 { FLAG_IDL_PROXY, "_p.c" },
77 { FLAG_IDL_SERVER, "_s.c" },
78 { FLAG_IDL_REGISTER, "_r.res" },
79 { FLAG_IDL_HEADER, ".h" }
82 static struct list sources = LIST_INIT(sources);
83 static struct list includes = LIST_INIT(includes);
85 struct strarray
87 unsigned int count; /* strings in use */
88 unsigned int size; /* total allocated size */
89 const char **str;
92 static const struct strarray empty_strarray;
94 static struct strarray include_args;
95 static struct strarray make_vars;
96 static struct strarray cmdline_vars;
98 static const char *base_dir = ".";
99 static const char *src_dir;
100 static const char *top_src_dir;
101 static const char *top_obj_dir;
102 static const char *parent_dir;
103 static const char *makefile_name = "Makefile";
104 static const char *Separator = "### Dependencies";
105 static const char *input_file_name;
106 static const char *output_file_name;
107 static const char *temp_file_name;
108 static int parse_makefile_mode;
109 static int relative_dir_mode;
110 static int input_line;
111 static int output_column;
112 static FILE *output_file;
114 static const char Usage[] =
115 "Usage: makedep [options] [files]\n"
116 "Options:\n"
117 " -Idir Search for include files in directory 'dir'\n"
118 " -Cdir Search for source files in directory 'dir'\n"
119 " -Sdir Set the top source directory\n"
120 " -Tdir Set the top object directory\n"
121 " -Pdir Set the parent source directory\n"
122 " -M dirs Parse the makefiles from the specified directories\n"
123 " -R from to Compute the relative path between two directories\n"
124 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
125 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
128 #ifndef __GNUC__
129 #define __attribute__(x)
130 #endif
132 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
133 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
134 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
135 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
137 /*******************************************************************
138 * fatal_error
140 static void fatal_error( const char *msg, ... )
142 va_list valist;
143 va_start( valist, msg );
144 if (input_file_name)
146 fprintf( stderr, "%s:", input_file_name );
147 if (input_line) fprintf( stderr, "%d:", input_line );
148 fprintf( stderr, " error: " );
150 else fprintf( stderr, "makedep: error: " );
151 vfprintf( stderr, msg, valist );
152 va_end( valist );
153 exit(1);
157 /*******************************************************************
158 * fatal_perror
160 static void fatal_perror( const char *msg, ... )
162 va_list valist;
163 va_start( valist, msg );
164 if (input_file_name)
166 fprintf( stderr, "%s:", input_file_name );
167 if (input_line) fprintf( stderr, "%d:", input_line );
168 fprintf( stderr, " error: " );
170 else fprintf( stderr, "makedep: error: " );
171 vfprintf( stderr, msg, valist );
172 perror( " " );
173 va_end( valist );
174 exit(1);
178 /*******************************************************************
179 * cleanup_files
181 static void cleanup_files(void)
183 if (temp_file_name) unlink( temp_file_name );
184 if (output_file_name) unlink( output_file_name );
188 /*******************************************************************
189 * exit_on_signal
191 static void exit_on_signal( int sig )
193 exit( 1 ); /* this will call the atexit functions */
197 /*******************************************************************
198 * xmalloc
200 static void *xmalloc( size_t size )
202 void *res;
203 if (!(res = malloc (size ? size : 1)))
204 fatal_error( "Virtual memory exhausted.\n" );
205 return res;
209 /*******************************************************************
210 * xrealloc
212 static void *xrealloc (void *ptr, size_t size)
214 void *res;
215 assert( size );
216 if (!(res = realloc( ptr, size )))
217 fatal_error( "Virtual memory exhausted.\n" );
218 return res;
221 /*******************************************************************
222 * xstrdup
224 static char *xstrdup( const char *str )
226 char *res = strdup( str );
227 if (!res) fatal_error( "Virtual memory exhausted.\n" );
228 return res;
232 /*******************************************************************
233 * strmake
235 static char *strmake( const char* fmt, ... )
237 int n;
238 size_t size = 100;
239 va_list ap;
241 for (;;)
243 char *p = xmalloc (size);
244 va_start(ap, fmt);
245 n = vsnprintf (p, size, fmt, ap);
246 va_end(ap);
247 if (n == -1) size *= 2;
248 else if ((size_t)n >= size) size = n + 1;
249 else return p;
250 free(p);
255 /*******************************************************************
256 * strendswith
258 static int strendswith( const char* str, const char* end )
260 int l = strlen(str);
261 int m = strlen(end);
263 return l >= m && strcmp(str + l - m, end) == 0;
267 /*******************************************************************
268 * output
270 static void output( const char *format, ... )
272 int ret;
273 va_list valist;
275 va_start( valist, format );
276 ret = vfprintf( output_file, format, valist );
277 va_end( valist );
278 if (ret < 0) fatal_perror( "output" );
279 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
280 else output_column += ret;
284 /*******************************************************************
285 * strarray_add
287 static void strarray_add( struct strarray *array, const char *str )
289 if (array->count == array->size)
291 if (array->size) array->size *= 2;
292 else array->size = 16;
293 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
295 array->str[array->count++] = str;
299 /*******************************************************************
300 * strarray_addall
302 static void strarray_addall( struct strarray *array, struct strarray added )
304 unsigned int i;
306 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
310 /*******************************************************************
311 * strarray_insert
313 static void strarray_insert( struct strarray *array, unsigned int pos, const char *str )
315 unsigned int i;
317 strarray_add( array, NULL );
318 for (i = array->count - 1; i > pos; i--) array->str[i] = array->str[i - 1];
319 array->str[pos] = str;
323 /*******************************************************************
324 * strarray_add_uniq
326 static void strarray_add_uniq( struct strarray *array, const char *str )
328 unsigned int i;
330 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return;
331 strarray_add( array, str );
335 /*******************************************************************
336 * output_filename
338 static void output_filename( const char *name )
340 if (output_column + strlen(name) + 1 > 100)
342 output( " \\\n" );
343 output( " " );
345 else if (output_column) output( " " );
346 output( "%s", name );
350 /*******************************************************************
351 * output_filenames
353 static void output_filenames( struct strarray array )
355 unsigned int i;
357 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
361 /*******************************************************************
362 * get_extension
364 static char *get_extension( char *filename )
366 char *ext = strrchr( filename, '.' );
367 if (ext && strchr( ext, '/' )) ext = NULL;
368 return ext;
372 /*******************************************************************
373 * replace_extension
375 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
377 char *ret;
378 int name_len = strlen( name );
379 int ext_len = strlen( old_ext );
381 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
382 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
383 memcpy( ret, name, name_len );
384 strcpy( ret + name_len, new_ext );
385 return ret;
389 /*******************************************************************
390 * strarray_replace_extension
392 static struct strarray strarray_replace_extension( const struct strarray *array,
393 const char *old_ext, const char *new_ext )
395 unsigned int i;
396 struct strarray ret;
398 ret.count = ret.size = array->count;
399 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
400 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
401 return ret;
405 /*******************************************************************
406 * replace_substr
408 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
410 unsigned int pos = start - str;
411 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
412 memcpy( ret, str, pos );
413 strcpy( ret + pos, replace );
414 strcat( ret + pos, start + len );
415 return ret;
419 /*******************************************************************
420 * get_relative_path
422 * Determine where the destination path is located relative to the 'from' path.
424 static char *get_relative_path( const char *from, const char *dest )
426 const char *start;
427 char *ret, *p;
428 unsigned int dotdots = 0;
430 /* a path of "." is equivalent to an empty path */
431 if (!strcmp( from, "." )) from = "";
433 for (;;)
435 while (*from == '/') from++;
436 while (*dest == '/') dest++;
437 start = dest; /* save start of next path element */
438 if (!*from) break;
440 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
441 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
443 /* count remaining elements in 'from' */
446 dotdots++;
447 while (*from && *from != '/') from++;
448 while (*from == '/') from++;
450 while (*from);
451 break;
454 if (!start[0] && !dotdots) return NULL; /* empty path */
456 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
457 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
459 if (start[0]) strcpy( p, start );
460 else p[-1] = 0; /* remove trailing slash */
461 return ret;
465 /*******************************************************************
466 * init_paths
468 static void init_paths(void)
470 /* ignore redundant source paths */
471 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
472 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
474 if (top_src_dir) strarray_insert( &include_args, 0, strmake( "-I%s/include", top_src_dir ));
475 else if (top_obj_dir) strarray_insert( &include_args, 0, strmake( "-I%s/include", top_obj_dir ));
479 /*******************************************************************
480 * get_line
482 static char *get_line( FILE *file )
484 static char *buffer;
485 static unsigned int size;
487 if (!size)
489 size = 1024;
490 buffer = xmalloc( size );
492 if (!fgets( buffer, size, file )) return NULL;
493 input_line++;
495 for (;;)
497 char *p = buffer + strlen(buffer);
498 /* if line is larger than buffer, resize buffer */
499 while (p == buffer + size - 1 && p[-1] != '\n')
501 buffer = xrealloc( buffer, size * 2 );
502 if (!fgets( buffer + size - 1, size + 1, file )) break;
503 p = buffer + strlen(buffer);
504 size *= 2;
506 if (p > buffer && p[-1] == '\n')
508 *(--p) = 0;
509 if (p > buffer && p[-1] == '\r') *(--p) = 0;
510 if (p > buffer && p[-1] == '\\')
512 *(--p) = 0;
513 /* line ends in backslash, read continuation line */
514 if (!fgets( p, size - (p - buffer), file )) return buffer;
515 input_line++;
516 continue;
519 return buffer;
523 /*******************************************************************
524 * find_src_file
526 static struct incl_file *find_src_file( const char *name )
528 struct incl_file *file;
530 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
531 if (!strcmp( name, file->name )) return file;
532 return NULL;
535 /*******************************************************************
536 * find_include_file
538 static struct incl_file *find_include_file( const char *name )
540 struct incl_file *file;
542 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
543 if (!strcmp( name, file->name )) return file;
544 return NULL;
547 /*******************************************************************
548 * add_include
550 * Add an include file if it doesn't already exists.
552 static struct incl_file *add_include( struct incl_file *parent, const char *name, int system )
554 struct incl_file *include;
555 char *ext;
557 if (parent->files_count >= parent->files_size)
559 parent->files_size *= 2;
560 if (parent->files_size < 16) parent->files_size = 16;
561 parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
564 /* enforce some rules for the Wine tree */
566 if (!memcmp( name, "../", 3 ))
567 fatal_error( "#include directive with relative path not allowed\n" );
569 if (!strcmp( name, "config.h" ))
571 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
572 fatal_error( "config.h must not be included by a header file\n" );
573 if (parent->files_count)
574 fatal_error( "config.h must be included before anything else\n" );
576 else if (!strcmp( name, "wine/port.h" ))
578 if ((ext = strrchr( parent->filename, '.' )) && !strcmp( ext, ".h" ))
579 fatal_error( "wine/port.h must not be included by a header file\n" );
580 if (!parent->files_count) fatal_error( "config.h must be included before wine/port.h\n" );
581 if (parent->files_count > 1)
582 fatal_error( "wine/port.h must be included before everything except config.h\n" );
583 if (strcmp( parent->files[0]->name, "config.h" ))
584 fatal_error( "config.h must be included before wine/port.h\n" );
587 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
588 if (!strcmp( name, include->name )) goto found;
590 include = xmalloc( sizeof(*include) );
591 memset( include, 0, sizeof(*include) );
592 include->name = xstrdup(name);
593 include->included_by = parent;
594 include->included_line = input_line;
595 if (system) include->flags |= FLAG_SYSTEM;
596 list_add_tail( &includes, &include->entry );
597 found:
598 parent->files[parent->files_count++] = include;
599 return include;
603 /*******************************************************************
604 * open_file
606 static FILE *open_file( const char *path )
608 FILE *ret;
610 if (path[0] != '/' && strcmp( base_dir, "." ))
612 char *full_path = strmake( "%s/%s", base_dir, path );
613 ret = fopen( full_path, "r" );
614 free( full_path );
616 else ret = fopen( path, "r" );
618 return ret;
621 /*******************************************************************
622 * open_src_file
624 static FILE *open_src_file( struct incl_file *pFile )
626 FILE *file;
628 /* first try name as is */
629 if ((file = open_file( pFile->name )))
631 pFile->filename = xstrdup( pFile->name );
632 return file;
634 /* now try in source dir */
635 if (src_dir)
637 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
638 file = open_file( pFile->filename );
640 /* now try parent dir */
641 if (!file && parent_dir)
643 if (src_dir)
644 pFile->filename = strmake( "%s/%s/%s", src_dir, parent_dir, pFile->name );
645 else
646 pFile->filename = strmake( "%s/%s", parent_dir, pFile->name );
647 file = open_file( pFile->filename );
649 if (!file) fatal_perror( "open %s", pFile->name );
650 return file;
654 /*******************************************************************
655 * open_include_file
657 static FILE *open_include_file( struct incl_file *pFile )
659 FILE *file = NULL;
660 char *filename, *p;
661 unsigned int i, len;
663 errno = ENOENT;
665 /* check for generated bison header */
667 if (strendswith( pFile->name, ".tab.h" ))
669 filename = replace_extension( pFile->name, ".tab.h", ".y" );
670 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
672 if ((file = open_file( filename )))
674 pFile->sourcename = filename;
675 pFile->filename = xstrdup( pFile->name );
676 /* don't bother to parse it */
677 fclose( file );
678 return NULL;
680 free( filename );
683 /* check for corresponding idl file in source dir */
685 if (strendswith( pFile->name, ".h" ))
687 filename = replace_extension( pFile->name, ".h", ".idl" );
688 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
690 if ((file = open_file( filename )))
692 pFile->sourcename = filename;
693 pFile->filename = xstrdup( pFile->name );
694 return file;
696 free( filename );
699 /* now try in source dir */
700 if (src_dir)
701 filename = strmake( "%s/%s", src_dir, pFile->name );
702 else
703 filename = xstrdup( pFile->name );
704 if ((file = open_file( filename ))) goto found;
705 free( filename );
707 /* now try in parent source dir */
708 if (parent_dir)
710 if (src_dir)
711 filename = strmake( "%s/%s/%s", src_dir, parent_dir, pFile->name );
712 else
713 filename = strmake( "%s/%s", parent_dir, pFile->name );
714 if ((file = open_file( filename ))) goto found;
715 free( filename );
718 /* check for corresponding idl file in global includes */
720 if (strendswith( pFile->name, ".h" ))
722 filename = replace_extension( pFile->name, ".h", ".idl" );
723 if (top_src_dir)
724 filename = strmake( "%s/include/%s", top_src_dir, filename );
725 else if (top_obj_dir)
726 filename = strmake( "%s/include/%s", top_obj_dir, filename );
727 else
728 filename = NULL;
730 if (filename && (file = open_file( filename )))
732 pFile->sourcename = filename;
733 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
734 return file;
736 free( filename );
739 /* check for corresponding .in file in global includes (for config.h.in) */
741 if (strendswith( pFile->name, ".h" ))
743 filename = replace_extension( pFile->name, ".h", ".h.in" );
744 if (top_src_dir)
745 filename = strmake( "%s/include/%s", top_src_dir, filename );
746 else if (top_obj_dir)
747 filename = strmake( "%s/include/%s", top_obj_dir, filename );
748 else
749 filename = NULL;
751 if (filename && (file = open_file( filename )))
753 pFile->sourcename = filename;
754 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
755 return file;
757 free( filename );
760 /* check for corresponding .x file in global includes */
762 if (strendswith( pFile->name, "tmpl.h" ))
764 filename = replace_extension( pFile->name, ".h", ".x" );
765 if (top_src_dir)
766 filename = strmake( "%s/include/%s", top_src_dir, filename );
767 else if (top_obj_dir)
768 filename = strmake( "%s/include/%s", top_obj_dir, filename );
769 else
770 filename = NULL;
772 if (filename && (file = open_file( filename )))
774 pFile->sourcename = filename;
775 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
776 return file;
778 free( filename );
781 /* now search in include paths */
782 for (i = 0; i < include_args.count; i++)
784 const char *dir = include_args.str[i] + 2; /* skip -I */
785 if (*dir == '/')
787 /* ignore absolute paths that don't point into the source dir */
788 if (!top_src_dir) continue;
789 len = strlen( top_src_dir );
790 if (strncmp( dir, top_src_dir, len )) continue;
791 if (dir[len] && dir[len] != '/') continue;
793 filename = strmake( "%s/%s", dir, pFile->name );
794 if ((file = open_file( filename ))) goto found;
795 free( filename );
797 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
799 /* try in src file directory */
800 if ((p = strrchr(pFile->included_by->filename, '/')))
802 int l = p - pFile->included_by->filename + 1;
803 filename = xmalloc(l + strlen(pFile->name) + 1);
804 memcpy( filename, pFile->included_by->filename, l );
805 strcpy( filename + l, pFile->name );
806 if ((file = open_file( filename ))) goto found;
807 free( filename );
810 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
811 perror( pFile->name );
812 pFile = pFile->included_by;
813 while (pFile && pFile->included_by)
815 const char *parent = pFile->included_by->sourcename;
816 if (!parent) parent = pFile->included_by->name;
817 fprintf( stderr, "%s:%d: note: %s was first included here\n",
818 parent, pFile->included_line, pFile->name );
819 pFile = pFile->included_by;
821 exit(1);
823 found:
824 pFile->filename = filename;
825 return file;
829 /*******************************************************************
830 * parse_include_directive
832 static void parse_include_directive( struct incl_file *source, char *str )
834 char quote, *include, *p = str;
836 while (*p && isspace(*p)) p++;
837 if (*p != '\"' && *p != '<' ) return;
838 quote = *p++;
839 if (quote == '<') quote = '>';
840 include = p;
841 while (*p && (*p != quote)) p++;
842 if (!*p) fatal_error( "malformed include directive '%s'\n", str );
843 *p = 0;
844 add_include( source, include, (quote == '>') );
848 /*******************************************************************
849 * parse_pragma_directive
851 static void parse_pragma_directive( struct incl_file *source, char *str )
853 char *flag, *p = str;
855 if (!isspace( *p )) return;
856 while (*p && isspace(*p)) p++;
857 p = strtok( p, " \t" );
858 if (strcmp( p, "makedep" )) return;
860 while ((flag = strtok( NULL, " \t" )))
862 if (!strcmp( flag, "depend" ))
864 while ((p = strtok( NULL, " \t" ))) add_include( source, p, 0 );
865 return;
867 if (strendswith( source->name, ".idl" ))
869 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
870 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
871 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
872 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
873 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
874 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
875 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
876 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
878 else if (strendswith( source->name, ".rc" ))
880 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
882 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
887 /*******************************************************************
888 * parse_cpp_directive
890 static void parse_cpp_directive( struct incl_file *source, char *str )
892 while (*str && isspace(*str)) str++;
893 if (*str++ != '#') return;
894 while (*str && isspace(*str)) str++;
896 if (!strncmp( str, "include", 7 ))
897 parse_include_directive( source, str + 7 );
898 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
899 parse_include_directive( source, str + 6 );
900 else if (!strncmp( str, "pragma", 6 ))
901 parse_pragma_directive( source, str + 6 );
905 /*******************************************************************
906 * parse_idl_file
908 * If for_h_file is non-zero, it means we are not interested in the idl file
909 * itself, but only in the contents of the .h file that will be generated from it.
911 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
913 char *buffer, *include;
915 input_line = 0;
916 if (for_h_file)
918 /* generated .h file always includes these */
919 add_include( pFile, "rpc.h", 1 );
920 add_include( pFile, "rpcndr.h", 1 );
923 while ((buffer = get_line( file )))
925 char quote;
926 char *p = buffer;
927 while (*p && isspace(*p)) p++;
929 if (!strncmp( p, "import", 6 ))
931 p += 6;
932 while (*p && isspace(*p)) p++;
933 if (*p != '"') continue;
934 include = ++p;
935 while (*p && (*p != '"')) p++;
936 if (!*p) fatal_error( "malformed import directive\n" );
937 *p = 0;
938 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
939 add_include( pFile, include, 0 );
940 continue;
943 if (for_h_file) /* only check for #include inside cpp_quote */
945 if (strncmp( p, "cpp_quote", 9 )) continue;
946 p += 9;
947 while (*p && isspace(*p)) p++;
948 if (*p++ != '(') continue;
949 while (*p && isspace(*p)) p++;
950 if (*p++ != '"') continue;
951 if (*p++ != '#') continue;
952 while (*p && isspace(*p)) p++;
953 if (strncmp( p, "include", 7 )) continue;
954 p += 7;
955 while (*p && isspace(*p)) p++;
956 if (*p == '\\' && p[1] == '"')
958 p += 2;
959 quote = '"';
961 else
963 if (*p++ != '<' ) continue;
964 quote = '>';
966 include = p;
967 while (*p && (*p != quote)) p++;
968 if (!*p || (quote == '"' && p[-1] != '\\'))
969 fatal_error( "malformed #include directive inside cpp_quote\n" );
970 if (quote == '"') p--; /* remove backslash */
971 *p = 0;
972 add_include( pFile, include, (quote == '>') );
973 continue;
976 parse_cpp_directive( pFile, p );
980 /*******************************************************************
981 * parse_c_file
983 static void parse_c_file( struct incl_file *pFile, FILE *file )
985 char *buffer;
987 input_line = 0;
988 while ((buffer = get_line( file )))
990 parse_cpp_directive( pFile, buffer );
995 /*******************************************************************
996 * parse_rc_file
998 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1000 char *buffer, *include;
1002 input_line = 0;
1003 while ((buffer = get_line( file )))
1005 char quote;
1006 char *p = buffer;
1007 while (*p && isspace(*p)) p++;
1009 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1011 p += 2;
1012 while (*p && isspace(*p)) p++;
1013 if (strncmp( p, "@makedep:", 9 )) continue;
1014 p += 9;
1015 while (*p && isspace(*p)) p++;
1016 quote = '"';
1017 if (*p == quote)
1019 include = ++p;
1020 while (*p && *p != quote) p++;
1022 else
1024 include = p;
1025 while (*p && !isspace(*p) && *p != '*') p++;
1027 if (!*p)
1028 fatal_error( "malformed makedep comment\n" );
1029 *p = 0;
1030 add_include( pFile, include, (quote == '>') );
1031 continue;
1034 parse_cpp_directive( pFile, buffer );
1039 /*******************************************************************
1040 * parse_in_file
1042 static void parse_in_file( struct incl_file *source, FILE *file )
1044 char *p, *buffer;
1046 /* make sure it gets rebuilt when the version changes */
1047 add_include( source, "config.h", 1 );
1049 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1051 input_line = 0;
1052 while ((buffer = get_line( file )))
1054 if (strncmp( buffer, ".TH", 3 )) continue;
1055 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1056 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1057 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1058 source->sourcename = xstrdup( p ); /* abuse source name to store section */
1059 return;
1064 /*******************************************************************
1065 * parse_file
1067 static void parse_file( struct incl_file *source, int src )
1069 FILE *file;
1071 /* don't try to open certain types of files */
1072 if (strendswith( source->name, ".tlb" ))
1074 source->filename = xstrdup( source->name );
1075 return;
1078 file = src ? open_src_file( source ) : open_include_file( source );
1079 if (!file) return;
1080 input_file_name = source->filename;
1082 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1083 parse_idl_file( source, file, 1 );
1084 else if (strendswith( source->filename, ".idl" ))
1085 parse_idl_file( source, file, 0 );
1086 else if (strendswith( source->filename, ".c" ) ||
1087 strendswith( source->filename, ".m" ) ||
1088 strendswith( source->filename, ".h" ) ||
1089 strendswith( source->filename, ".l" ) ||
1090 strendswith( source->filename, ".y" ))
1091 parse_c_file( source, file );
1092 else if (strendswith( source->filename, ".rc" ))
1093 parse_rc_file( source, file );
1094 else if (strendswith( source->filename, ".in" ))
1095 parse_in_file( source, file );
1096 fclose(file);
1097 input_file_name = NULL;
1101 /*******************************************************************
1102 * add_src_file
1104 * Add a source file to the list.
1106 static struct incl_file *add_src_file( const char *name )
1108 struct incl_file *file;
1110 if ((file = find_src_file( name ))) return file; /* we already have it */
1111 file = xmalloc( sizeof(*file) );
1112 memset( file, 0, sizeof(*file) );
1113 file->name = xstrdup(name);
1114 list_add_tail( &sources, &file->entry );
1115 parse_file( file, 1 );
1116 return file;
1120 /*******************************************************************
1121 * get_make_variable
1123 static char *get_make_variable( const char *name )
1125 unsigned int i;
1127 for (i = 0; i < cmdline_vars.count; i += 2)
1128 if (!strcmp( cmdline_vars.str[i], name ))
1129 return xstrdup( cmdline_vars.str[i + 1] );
1131 for (i = 0; i < make_vars.count; i += 2)
1132 if (!strcmp( make_vars.str[i], name ))
1133 return xstrdup( make_vars.str[i + 1] );
1134 return NULL;
1138 /*******************************************************************
1139 * get_expanded_make_variable
1141 static char *get_expanded_make_variable( const char *name )
1143 char *p, *end, *var, *expand, *tmp;
1145 expand = get_make_variable( name );
1146 if (!expand) return NULL;
1148 p = expand;
1149 while ((p = strchr( p, '$' )))
1151 if (p[1] == '(')
1153 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1154 *end++ = 0;
1155 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1156 var = get_make_variable( p + 2 );
1157 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1158 free( var );
1160 else if (p[1] == '{') /* don't expand ${} variables */
1162 if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
1163 p = end + 1;
1164 continue;
1166 else if (p[1] == '$')
1168 tmp = replace_substr( expand, p, 2, "$" );
1170 else fatal_error( "syntax error in '%s'\n", expand );
1172 /* switch to the new string */
1173 p = tmp + (p - expand);
1174 free( expand );
1175 expand = tmp;
1178 /* consider empty variables undefined */
1179 p = expand;
1180 while (*p && isspace(*p)) p++;
1181 if (*p) return expand;
1182 free( expand );
1183 return NULL;
1187 /*******************************************************************
1188 * get_expanded_make_var_array
1190 static struct strarray get_expanded_make_var_array( const char *name )
1192 struct strarray ret = empty_strarray;
1193 char *value, *token;
1195 if ((value = get_expanded_make_variable( name )))
1196 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1197 strarray_add( &ret, token );
1198 return ret;
1202 /*******************************************************************
1203 * set_make_variable
1205 static int set_make_variable( struct strarray *array, const char *assignment )
1207 unsigned int i;
1208 char *p, *name;
1210 p = name = xstrdup( assignment );
1211 while (isalnum(*p) || *p == '_') p++;
1212 if (name == p) return 0; /* not a variable */
1213 if (isspace(*p))
1215 *p++ = 0;
1216 while (isspace(*p)) p++;
1218 if (*p != '=') return 0; /* not an assignment */
1219 *p++ = 0;
1220 while (isspace(*p)) p++;
1222 /* redefining a variable replaces the previous value */
1223 for (i = 0; i < array->count; i += 2)
1225 if (strcmp( array->str[i], name )) continue;
1226 array->str[i + 1] = p;
1227 return 1;
1229 strarray_add( array, name );
1230 strarray_add( array, p );
1231 return 1;
1235 /*******************************************************************
1236 * parse_makefile
1238 static void parse_makefile(void)
1240 char *buffer;
1241 FILE *file;
1243 input_file_name = strmake( "%s/%s", base_dir, makefile_name );
1244 if (!(file = fopen( input_file_name, "r" )))
1246 fatal_perror( "open" );
1247 exit( 1 );
1250 input_line = 0;
1251 while ((buffer = get_line( file )))
1253 if (Separator && !strncmp( buffer, Separator, strlen(Separator) )) break;
1254 if (*buffer == '\t') continue; /* command */
1255 while (isspace( *buffer )) buffer++;
1256 if (*buffer == '#') continue; /* comment */
1257 set_make_variable( &make_vars, buffer );
1259 fclose( file );
1260 input_file_name = NULL;
1264 /*******************************************************************
1265 * add_generated_source
1267 * Add a generated source file to the list.
1269 static struct incl_file *add_generated_source( const char *name, const char *filename )
1271 struct incl_file *file;
1273 if ((file = find_src_file( name ))) return file; /* we already have it */
1274 file = xmalloc( sizeof(*file) );
1275 memset( file, 0, sizeof(*file) );
1276 file->name = xstrdup( name );
1277 file->filename = xstrdup( filename ? filename : name );
1278 file->flags = FLAG_GENERATED;
1279 list_add_tail( &sources, &file->entry );
1280 return file;
1284 /*******************************************************************
1285 * add_generated_sources
1287 static void add_generated_sources(void)
1289 struct incl_file *source, *next, *file;
1291 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1293 if (source->flags & FLAG_IDL_CLIENT)
1295 file = add_generated_source( replace_extension( source->name, ".idl", "_c.c" ), NULL );
1296 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1298 if (source->flags & FLAG_IDL_SERVER)
1300 file = add_generated_source( replace_extension( source->name, ".idl", "_s.c" ), NULL );
1301 add_include( file, "wine/exception.h", 0 );
1302 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1304 if (source->flags & FLAG_IDL_IDENT)
1306 file = add_generated_source( replace_extension( source->name, ".idl", "_i.c" ), NULL );
1307 add_include( file, "rpc.h", 0 );
1308 add_include( file, "rpcndr.h", 0 );
1309 add_include( file, "guiddef.h", 0 );
1311 if (source->flags & FLAG_IDL_PROXY)
1313 file = add_generated_source( "dlldata.o", "dlldata.c" );
1314 add_include( file, "objbase.h", 0 );
1315 add_include( file, "rpcproxy.h", 0 );
1316 file = add_generated_source( replace_extension( source->name, ".idl", "_p.c" ), NULL );
1317 add_include( file, "objbase.h", 0 );
1318 add_include( file, "rpcproxy.h", 0 );
1319 add_include( file, "wine/exception.h", 0 );
1320 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1322 if (source->flags & FLAG_IDL_REGTYPELIB)
1324 add_generated_source( replace_extension( source->name, ".idl", "_t.res" ), NULL );
1326 if (source->flags & FLAG_IDL_REGISTER)
1328 add_generated_source( replace_extension( source->name, ".idl", "_r.res" ), NULL );
1330 if (strendswith( source->name, ".y" ))
1332 file = add_generated_source( replace_extension( source->name, ".y", ".tab.c" ), NULL );
1333 /* steal the includes list from the source file */
1334 file->files_count = source->files_count;
1335 file->files_size = source->files_size;
1336 file->files = source->files;
1337 source->files_count = source->files_size = 0;
1338 source->files = NULL;
1340 if (strendswith( source->name, ".l" ))
1342 file = add_generated_source( replace_extension( source->name, ".l", ".yy.c" ), NULL );
1343 /* steal the includes list from the source file */
1344 file->files_count = source->files_count;
1345 file->files_size = source->files_size;
1346 file->files = source->files;
1347 source->files_count = source->files_size = 0;
1348 source->files = NULL;
1351 if (get_make_variable( "TESTDLL" ))
1353 file = add_generated_source( "testlist.o", "testlist.c" );
1354 add_include( file, "wine/test.h", 0 );
1359 /*******************************************************************
1360 * output_include
1362 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1364 int i;
1366 if (pFile->owner == owner) return;
1367 if (!pFile->filename) return;
1368 pFile->owner = owner;
1369 output_filename( pFile->filename );
1370 for (i = 0; i < pFile->files_count; i++) output_include( pFile->files[i], owner );
1374 /*******************************************************************
1375 * output_sources
1377 static struct strarray output_sources(void)
1379 struct incl_file *source;
1380 int i, is_win16 = 0;
1381 const char *dllext = ".so";
1382 struct strarray object_files = empty_strarray;
1383 struct strarray crossobj_files = empty_strarray;
1384 struct strarray res_files = empty_strarray;
1385 struct strarray clean_files = empty_strarray;
1386 struct strarray po_files = empty_strarray;
1387 struct strarray mc_files = empty_strarray;
1388 struct strarray test_files = empty_strarray;
1389 struct strarray dlldata_files = empty_strarray;
1390 struct strarray c2man_files = empty_strarray;
1391 struct strarray implib_objs = empty_strarray;
1392 struct strarray includes = empty_strarray;
1393 struct strarray subdirs = empty_strarray;
1394 struct strarray phony_targets = empty_strarray;
1395 struct strarray dllflags = get_expanded_make_var_array( "DLLFLAGS" );
1396 struct strarray imports = get_expanded_make_var_array( "IMPORTS" );
1397 struct strarray all_targets = get_expanded_make_var_array( "PROGRAMS" );
1398 struct strarray targetflags = get_expanded_make_var_array( "TARGETFLAGS" );
1399 struct strarray delayimports = get_expanded_make_var_array( "DELAYIMPORTS" );
1400 struct strarray extradllflags = get_expanded_make_var_array( "EXTRADLLFLAGS" );
1401 char *module = get_expanded_make_variable( "MODULE" );
1402 char *exeext = get_expanded_make_variable( "EXEEXT" );
1403 char *testdll = get_expanded_make_variable( "TESTDLL" );
1404 char *appmode = get_expanded_make_variable( "APPMODE" );
1405 char *staticlib = get_expanded_make_variable( "STATICLIB" );
1406 char *crosstarget = get_expanded_make_variable( "CROSSTARGET" );
1408 if (exeext && !strcmp( exeext, ".exe" )) dllext = "";
1409 if (module && strendswith( module, ".a" )) staticlib = module;
1410 for (i = 0; i < extradllflags.count; i++) if (!strcmp( extradllflags.str[i], "-m16" )) is_win16 = 1;
1412 strarray_add( &includes, "-I." );
1413 if (src_dir) strarray_add( &includes, strmake( "-I%s", src_dir ));
1414 if (parent_dir)
1416 if (src_dir) strarray_add( &includes, strmake( "-I%s/%s", src_dir, parent_dir ));
1417 else strarray_add( &includes, strmake( "-I%s", parent_dir ));
1419 if (top_src_dir && top_obj_dir) strarray_add( &includes, strmake( "-I%s/include", top_obj_dir ));
1420 strarray_addall( &includes, include_args );
1422 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1424 char *sourcedep;
1425 char *obj = xstrdup( source->name );
1426 char *ext = get_extension( obj );
1428 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1429 *ext++ = 0;
1431 if (src_dir && strchr( obj, '/' ))
1433 char *dir = xstrdup( obj );
1434 *strrchr( dir, '/' ) = 0;
1435 strarray_add_uniq( &subdirs, dir );
1436 sourcedep = strmake( "%s %s", dir, source->filename );
1438 else sourcedep = xstrdup( source->filename );
1440 if (!strcmp( ext, "y" )) /* yacc file */
1442 /* add source file dependency for parallel makes */
1443 char *header = strmake( "%s.tab.h", obj );
1445 if (find_include_file( header ))
1447 output( "%s.tab.h: %s\n", obj, sourcedep );
1448 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
1449 obj, obj, source->filename );
1450 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1451 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1453 else output( "%s.tab.c: %s\n", obj, sourcedep );
1455 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj, source->filename );
1456 free( header );
1457 continue; /* no dependencies */
1459 else if (!strcmp( ext, "x" )) /* template file */
1461 output( "%s.h: $(MAKEXFTMPL) %s\n", obj, sourcedep );
1462 output( "\t$(MAKEXFTMPL) -H -o $@ %s\n", source->filename );
1463 strarray_add( &clean_files, strmake( "%s.h", obj ));
1464 continue; /* no dependencies */
1466 else if (!strcmp( ext, "l" )) /* lex file */
1468 output( "%s.yy.c: %s\n", obj, sourcedep );
1469 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source->filename );
1470 continue; /* no dependencies */
1472 else if (!strcmp( ext, "rc" )) /* resource file */
1474 if (source->flags & FLAG_RC_PO)
1476 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj, sourcedep );
1477 output( "\t$(WRC) -o $@ %s", source->filename );
1478 output_filenames( includes );
1479 if (is_win16) output_filename( "-m16" );
1480 else output_filenames( targetflags );
1481 output_filename( "$(RCFLAGS)" );
1482 output( "\n" );
1483 output( "%s.res rsrc.pot:", obj );
1484 strarray_add( &po_files, source->filename );
1486 else
1488 output( "%s.res: $(WRC) %s\n", obj, sourcedep );
1489 output( "\t$(WRC) -o $@ %s", source->filename );
1490 output_filenames( includes );
1491 if (is_win16) output_filename( "-m16" );
1492 else output_filenames( targetflags );
1493 output_filename( "$(RCFLAGS)" );
1494 output( "\n" );
1495 output( "%s.res:", obj );
1497 strarray_add( &res_files, strmake( "%s.res", obj ));
1499 else if (!strcmp( ext, "mc" )) /* message file */
1501 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj, sourcedep );
1502 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source->filename );
1503 strarray_add( &res_files, strmake( "%s.res", obj ));
1504 strarray_add( &mc_files, source->filename );
1505 output( "msg.pot %s.res:", obj );
1507 else if (!strcmp( ext, "idl" )) /* IDL file */
1509 struct strarray targets = empty_strarray;
1510 unsigned int i;
1511 char *dest;
1513 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1514 source->flags |= FLAG_IDL_HEADER;
1516 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1518 if (!(source->flags & idl_outputs[i].flag)) continue;
1519 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1520 if (!find_src_file( dest )) strarray_add( &clean_files, dest );
1521 strarray_add( &targets, dest );
1523 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1524 output_filenames( targets );
1525 output( ": $(WIDL)\n" );
1526 output( "\t$(WIDL) -o $@ %s", source->filename );
1527 output_filenames( includes );
1528 output_filenames( targetflags );
1529 output_filename( "$(IDLFLAGS)" );
1530 output( "\n" );
1531 output_filenames( targets );
1532 output( ": %s", sourcedep );
1534 else if (!strcmp( ext, "in" )) /* .in file or man page */
1536 if (strendswith( obj, ".man" ) && source->sourcename)
1538 char *dir, *dest = replace_extension( obj, ".man", "" );
1539 char *lang = strchr( dest, '.' );
1540 if (lang)
1542 *lang++ = 0;
1543 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, source->sourcename );
1545 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", source->sourcename );
1546 output( "install-man-pages:: %s\n", obj );
1547 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n",
1548 obj, dir, dest, source->sourcename );
1549 output( "uninstall::\n" );
1550 output( "\t$(RM) %s/%s.%s\n",
1551 dir, dest, source->sourcename );
1552 free( dest );
1553 free( dir );
1554 strarray_add( &all_targets, xstrdup(obj) );
1555 strarray_add_uniq( &phony_targets, "install-man-pages" );
1557 else strarray_add( &clean_files, xstrdup(obj) );
1558 output( "%s: %s\n", obj, sourcedep );
1559 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1560 output( "%s:", obj );
1562 else if (!strcmp( ext, "sfd" )) /* font file */
1564 char *fontforge = get_expanded_make_variable( "FONTFORGE" );
1565 if (fontforge && !src_dir)
1567 output( "%s.ttf: %s\n", obj, source->filename );
1568 output( "\t%s -script %s/fonts/genttf.ff %s $@\n",
1569 fontforge, top_src_dir ? top_src_dir : top_obj_dir, source->filename );
1571 free( fontforge );
1572 continue; /* no dependencies */
1574 else if (!strcmp( ext, "svg" )) /* svg file */
1576 char *convert = get_expanded_make_variable( "CONVERT" );
1577 char *rsvg = get_expanded_make_variable( "RSVG" );
1578 char *icotool = get_expanded_make_variable( "ICOTOOL" );
1579 if (convert && rsvg && icotool && !src_dir)
1581 output( "%s.ico %s.bmp: %s\n", obj, obj, source->filename );
1582 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" $(BUILDIMAGE) %s $@\n",
1583 convert, icotool, rsvg, source->filename );
1585 free( convert );
1586 free( rsvg );
1587 free( icotool );
1588 continue; /* no dependencies */
1590 else if (!strcmp( ext, "res" ))
1592 strarray_add( &res_files, source->name );
1593 continue; /* no dependencies */
1595 else
1597 int need_cross = testdll || (source->flags & FLAG_C_IMPLIB) || (module && staticlib);
1599 if (source->flags & FLAG_GENERATED) strarray_add( &clean_files, source->filename );
1600 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1601 strarray_add( &object_files, strmake( "%s.o", obj ));
1602 output( "%s.o: %s\n", obj, sourcedep );
1603 output( "\t$(CC) -c -o $@ %s", source->filename );
1604 output_filenames( includes );
1605 if (module || staticlib || testdll) output_filenames( dllflags );
1606 output_filename( "$(ALLCFLAGS)" );
1607 output( "\n" );
1608 if (crosstarget && need_cross)
1610 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1611 output( "%s.cross.o: %s\n", obj, sourcedep );
1612 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1613 output_filenames( includes );
1614 output_filename( "$(ALLCROSSCFLAGS)" );
1615 output( "\n" );
1617 if (testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1619 strarray_add( &test_files, source->name );
1620 output( "%s.ok:\n", obj );
1621 output( "\t$(RUNTEST) $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n", top_obj_dir,
1622 testdll, replace_extension( testdll, ".dll", "_test.exe" ), dllext, obj );
1624 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1625 strarray_add( &c2man_files, source->filename );
1626 output( "%s.o", obj );
1627 if (crosstarget && need_cross) output( " %s.cross.o", obj );
1628 output( ":" );
1630 free( obj );
1631 free( sourcedep );
1633 for (i = 0; i < source->files_count; i++) output_include( source->files[i], source );
1634 output( "\n" );
1637 /* rules for files that depend on multiple sources */
1639 if (po_files.count)
1641 output( "rsrc.pot: $(WRC)" );
1642 output_filenames( po_files );
1643 output( "\n" );
1644 output( "\t$(WRC) -O pot -o $@" );
1645 output_filenames( includes );
1646 if (is_win16) output_filename( "-m16" );
1647 else output_filenames( targetflags );
1648 output_filename( "$(RCFLAGS)" );
1649 output_filenames( po_files );
1650 output( "\n" );
1651 strarray_add( &clean_files, "rsrc.pot" );
1654 if (mc_files.count)
1656 output( "msg.pot: $(WMC)" );
1657 output_filenames( mc_files );
1658 output( "\n" );
1659 output( "\t$(WMC) -O pot -o $@" );
1660 output_filenames( mc_files );
1661 output( "\n" );
1662 strarray_add( &clean_files, "msg.pot" );
1665 if (dlldata_files.count)
1667 output( "dlldata.c: $(WIDL) %s\n", src_dir ? strmake("%s/Makefile.in", src_dir ) : "Makefile.in" );
1668 output( "\t$(WIDL) --dlldata-only -o $@" );
1669 output_filenames( dlldata_files );
1670 output( "\n" );
1673 if (module && !staticlib)
1675 char *importlib = get_expanded_make_variable( "IMPORTLIB" );
1676 struct strarray all_libs = empty_strarray;
1677 char *spec_file = appmode ? NULL : replace_extension( module, ".dll", ".spec" );
1679 if (spec_file && src_dir) spec_file = strmake( "%s/%s", src_dir, spec_file );
1680 for (i = 0; i < delayimports.count; i++)
1681 strarray_add( &all_libs, strmake( "-l%s", delayimports.str[i] ));
1682 for (i = 0; i < imports.count; i++)
1683 strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1684 for (i = 0; i < delayimports.count; i++)
1685 strarray_add( &all_libs, strmake( "-Wb,-d%s", delayimports.str[i] ));
1686 strarray_add( &all_libs, "-lwine" );
1687 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBPORT" ));
1688 strarray_addall( &all_libs, get_expanded_make_var_array( "EXTRALIBS" ));
1689 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1691 if (*dllext)
1693 strarray_add( &all_targets, strmake( "%s%s", module, dllext ));
1694 strarray_add( &all_targets, strmake( "%s.fake", module ));
1695 output( "%s%s %s.fake:", module, dllext, module );
1697 else
1699 strarray_add( &all_targets, module );
1700 output( "%s:", module );
1702 if (spec_file) output_filename( spec_file );
1703 output_filenames( object_files );
1704 output_filenames( res_files );
1705 output( "\n" );
1706 output( "\t$(WINEGCC) -o $@" );
1707 output_filenames( targetflags );
1708 if (spec_file)
1710 output( " -shared %s", spec_file );
1711 output_filenames( extradllflags );
1713 else output_filename( appmode );
1714 output_filenames( object_files );
1715 output_filenames( res_files );
1716 output_filenames( all_libs );
1717 output_filename( "$(LDFLAGS)" );
1718 output( "\n" );
1720 if (spec_file && importlib)
1722 if (*dllext)
1724 strarray_add( &clean_files, strmake( "lib%s.def", importlib ));
1725 output( "lib%s.def: %s\n", importlib, spec_file );
1726 output( "\t$(WINEBUILD) -w --def -o $@ --export %s", spec_file );
1727 output_filenames( targetflags );
1728 if (is_win16) output_filename( "-m16" );
1729 output( "\n" );
1730 if (implib_objs.count)
1732 strarray_add( &clean_files, strmake( "lib%s.def.a", importlib ));
1733 output( "lib%s.def.a:", importlib );
1734 output_filenames( implib_objs );
1735 output( "\n" );
1736 output( "\t$(RM) $@\n" );
1737 output( "\t$(AR) $(ARFLAGS) $@" );
1738 output_filenames( implib_objs );
1739 output( "\n" );
1740 output( "\t$(RANLIB) $@\n" );
1743 else
1745 strarray_add( &clean_files, strmake( "lib%s.a", importlib ));
1746 output( "lib%s.a: %s", importlib, spec_file );
1747 output_filenames( implib_objs );
1748 output( "\n" );
1749 output( "\t$(WINEBUILD) -w --implib -o $@ --export %s", spec_file );
1750 output_filenames( targetflags );
1751 output_filenames( implib_objs );
1752 output( "\n" );
1754 if (crosstarget && !is_win16)
1756 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
1757 strarray_add( &clean_files, strmake( "lib%s.cross.a", importlib ));
1758 output( "lib%s.cross.a: %s", importlib, spec_file );
1759 output_filenames( cross_files );
1760 output( "\n" );
1761 output( "\t$(WINEBUILD) -b %s -w --implib -o $@ --export %s", crosstarget, spec_file );
1762 output_filenames( cross_files );
1763 output( "\n" );
1767 if (spec_file)
1769 if (c2man_files.count && top_obj_dir)
1771 char *manext = get_expanded_make_variable( "api_manext" );
1773 output( "manpages::\n" );
1774 output( "\t$(C2MAN) -w %s -R%s", spec_file, top_obj_dir );
1775 output_filename( strmake( "-I%s/include", top_src_dir ? top_src_dir : top_obj_dir ));
1776 output_filename( strmake( "-o %s/documentation/man%s", top_obj_dir, manext ? manext : "3w" ));
1777 output_filenames( c2man_files );
1778 output( "\n" );
1779 output( "htmlpages::\n" );
1780 output( "\t$(C2MAN) -Th -w %s -R%s", spec_file, top_obj_dir );
1781 output_filename( strmake( "-I%s/include", top_src_dir ? top_src_dir : top_obj_dir ));
1782 output_filename( strmake( "-o %s/documentation/html", top_obj_dir ));
1783 output_filenames( c2man_files );
1784 output( "\n" );
1785 output( "sgmlpages::\n" );
1786 output( "\t$(C2MAN) -Ts -w %s -R%s", spec_file, top_obj_dir );
1787 output_filename( strmake( "-I%s/include", top_src_dir ? top_src_dir : top_obj_dir ));
1788 output_filename( strmake( "-o %s/documentation/api-guide", top_obj_dir ));
1789 output_filenames( c2man_files );
1790 output( "\n" );
1791 output( "xmlpages::\n" );
1792 output( "\t$(C2MAN) -Tx -w %s -R%s", spec_file, top_obj_dir );
1793 output_filename( strmake( "-I%s/include", top_src_dir ? top_src_dir : top_obj_dir ));
1794 output_filename( strmake( "-o %s/documentation/api-guide-xml", top_obj_dir ));
1795 output_filenames( c2man_files );
1796 output( "\n" );
1797 strarray_add( &phony_targets, "manpages" );
1798 strarray_add( &phony_targets, "htmlpages" );
1799 strarray_add( &phony_targets, "sgmlpages" );
1800 strarray_add( &phony_targets, "xmlpages" );
1802 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1806 if (staticlib)
1808 strarray_add( &all_targets, staticlib );
1809 output( "%s:", staticlib );
1810 output_filenames( object_files );
1811 output( "\n\t$(RM) $@\n" );
1812 output( "\t$(AR) $(ARFLAGS) $@" );
1813 output_filenames( object_files );
1814 output( "\n\t$(RANLIB) $@\n" );
1815 if (crosstarget && module)
1817 char *name = replace_extension( staticlib, ".a", ".cross.a" );
1819 strarray_add( &all_targets, name );
1820 output( "%s:", name );
1821 output_filenames( crossobj_files );
1822 output( "\n\t$(RM) $@\n" );
1823 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
1824 output_filenames( crossobj_files );
1825 output( "\n\t%s-ranlib $@\n", crosstarget );
1829 if (testdll)
1831 struct strarray ok_files = strarray_replace_extension( &test_files, ".c", ".ok" );
1832 char *testmodule = replace_extension( testdll, ".dll", "_test.exe" );
1833 char *stripped = replace_extension( testdll, ".dll", "_test-stripped.exe" );
1834 struct strarray all_libs = empty_strarray;
1836 for (i = 0; i < imports.count; i++) strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1837 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1839 strarray_add( &all_targets, strmake( "%s%s", testmodule, dllext ));
1840 strarray_add( &clean_files, strmake( "%s%s", stripped, dllext ));
1841 output( "%s%s:\n", testmodule, dllext );
1842 output( "\t$(WINEGCC) -o $@" );
1843 output_filenames( targetflags );
1844 if (appmode) output_filename( appmode );
1845 output_filenames( object_files );
1846 output_filenames( res_files );
1847 output_filenames( all_libs );
1848 output_filename( "$(LDFLAGS)" );
1849 output( "\n" );
1850 output( "%s%s:\n", stripped, dllext );
1851 output( "\t$(WINEGCC) -s -o $@" );
1852 output_filenames( targetflags );
1853 output_filename( strmake( "-Wb,-F,%s", testmodule ));
1854 if (appmode) output_filename( appmode );
1855 output_filenames( object_files );
1856 output_filenames( res_files );
1857 output_filenames( all_libs );
1858 output_filename( "$(LDFLAGS)" );
1859 output( "\n" );
1860 output( "%s%s %s%s:", testmodule, dllext, stripped, dllext );
1861 output_filenames( object_files );
1862 output_filenames( res_files );
1863 output( "\n" );
1865 if (top_obj_dir)
1867 char *testres = replace_extension( testdll, ".dll", "_test.res" );
1868 output( "all: %s/programs/winetest/%s\n", top_obj_dir, testres );
1869 output( "%s/programs/winetest/%s: %s%s\n", top_obj_dir, testres, stripped, dllext );
1870 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | $(WRC) -o $@\n",
1871 testmodule, stripped, dllext );
1874 if (crosstarget)
1876 char *crosstest = replace_extension( testdll, ".dll", "_crosstest.exe" );
1878 strarray_add( &clean_files, crosstest );
1879 output( "crosstest: %s\n", crosstest );
1880 output( "%s:", crosstest );
1881 output_filenames( crossobj_files );
1882 output_filenames( res_files );
1883 output( "\n" );
1884 output( "\t$(CROSSWINEGCC) -o $@ -b %s", crosstarget );
1885 output_filenames( crossobj_files );
1886 output_filenames( res_files );
1887 output_filenames( all_libs );
1888 output_filename( "$(LDFLAGS)" );
1889 output( "\n" );
1890 strarray_add( &phony_targets, "crosstest" );
1893 output( "testlist.c: $(MAKECTESTS) %s\n", src_dir ? strmake("%s/Makefile.in", src_dir ) : "Makefile.in" );
1894 output( "\t$(MAKECTESTS) -o $@" );
1895 output_filenames( test_files );
1896 output( "\n" );
1897 output_filenames( ok_files );
1898 output( ": %s%s ../%s%s\n", testmodule, dllext, testdll, dllext );
1899 output( "check test:" );
1900 output_filenames( ok_files );
1901 output( "\n" );
1902 output( "testclean::\n" );
1903 output( "\t$(RM)" );
1904 output_filenames( ok_files );
1905 output( "\n" );
1906 strarray_addall( &clean_files, ok_files );
1907 strarray_add( &phony_targets, "check" );
1908 strarray_add( &phony_targets, "test" );
1909 strarray_add( &phony_targets, "testclean" );
1912 if (all_targets.count)
1914 output( "all:" );
1915 output_filenames( all_targets );
1916 output( "\n" );
1919 strarray_addall( &clean_files, object_files );
1920 strarray_addall( &clean_files, crossobj_files );
1921 strarray_addall( &clean_files, res_files );
1922 strarray_addall( &clean_files, all_targets );
1923 strarray_addall( &clean_files, get_expanded_make_var_array( "EXTRA_TARGETS" ));
1925 if (clean_files.count)
1927 output( "clean::\n" );
1928 output( "\t$(RM)" );
1929 output_filenames( clean_files );
1930 output( "\n" );
1933 if (subdirs.count)
1935 output_filenames( subdirs );
1936 output( ":\n" );
1937 output( "\t$(MKDIR_P) -m 755 $@\n" );
1940 if (phony_targets.count)
1942 output( ".PHONY:" );
1943 output_filenames( phony_targets );
1944 output( "\n" );
1947 return clean_files;
1951 /*******************************************************************
1952 * create_temp_file
1954 static FILE *create_temp_file( const char *orig )
1956 char *name = xmalloc( strlen(orig) + 13 );
1957 unsigned int i, id = getpid();
1958 int fd;
1959 FILE *ret = NULL;
1961 for (i = 0; i < 100; i++)
1963 sprintf( name, "%s.tmp%08x", orig, id );
1964 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
1966 ret = fdopen( fd, "w" );
1967 break;
1969 if (errno != EEXIST) break;
1970 id += 7777;
1972 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
1973 temp_file_name = name;
1974 return ret;
1978 /*******************************************************************
1979 * rename_temp_file
1981 static void rename_temp_file( const char *dest )
1983 int ret = rename( temp_file_name, dest );
1984 if (ret == -1 && errno == EEXIST)
1986 /* rename doesn't overwrite on windows */
1987 unlink( dest );
1988 ret = rename( temp_file_name, dest );
1990 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
1991 temp_file_name = NULL;
1995 /*******************************************************************
1996 * output_gitignore
1998 static void output_gitignore( const char *dest, const struct strarray *files )
2000 int i;
2002 output_file = create_temp_file( dest );
2004 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2005 output( "/.gitignore\n" );
2006 output( "/Makefile\n" );
2007 for (i = 0; i < files->count; i++)
2009 if (!strchr( files->str[i], '/' )) output( "/" );
2010 output( "%s\n", files->str[i] );
2013 if (fclose( output_file )) fatal_perror( "write" );
2014 output_file = NULL;
2015 rename_temp_file( dest );
2019 /*******************************************************************
2020 * output_dependencies
2022 static void output_dependencies( const char *path )
2024 struct strarray targets = empty_strarray;
2026 if (Separator && ((output_file = fopen( path, "r" ))))
2028 char buffer[1024];
2029 FILE *tmp_file = create_temp_file( path );
2030 int found = 0;
2032 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2034 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2035 found = !strncmp( buffer, Separator, strlen(Separator) );
2037 if (fclose( output_file )) fatal_perror( "write" );
2038 output_file = tmp_file;
2039 if (!found) output( "\n%s\n", Separator );
2041 else
2043 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2044 fatal_perror( "%s", path );
2047 targets = output_sources();
2049 fclose( output_file );
2050 output_file = NULL;
2051 if (temp_file_name) rename_temp_file( path );
2053 if (!src_dir) output_gitignore( strmake( "%s/.gitignore", base_dir ), &targets );
2057 /*******************************************************************
2058 * update_makefile
2060 static void update_makefile( const char *path )
2062 static const char *source_vars[] =
2064 "C_SRCS",
2065 "OBJC_SRCS",
2066 "RC_SRCS",
2067 "MC_SRCS",
2068 "IDL_SRCS",
2069 "BISON_SRCS",
2070 "LEX_SRCS",
2071 "XTEMPLATE_SRCS",
2072 "SVG_SRCS",
2073 "FONT_SRCS",
2074 "IN_SRCS",
2075 "MANPAGES",
2076 NULL
2078 const char **var;
2079 unsigned int i;
2080 struct strarray value;
2081 struct incl_file *file;
2083 base_dir = path;
2084 output_file_name = strmake( "%s/%s", base_dir, makefile_name );
2085 parse_makefile();
2087 src_dir = get_expanded_make_variable( "srcdir" );
2088 top_src_dir = get_expanded_make_variable( "top_srcdir" );
2089 top_obj_dir = get_expanded_make_variable( "top_builddir" );
2090 parent_dir = get_expanded_make_variable( "PARENTSRC" );
2092 include_args = empty_strarray;
2093 value = get_expanded_make_var_array( "EXTRAINCL" );
2094 for (i = 0; i < value.count; i++)
2095 if (!strncmp( value.str[i], "-I", 2 ))
2096 strarray_add_uniq( &include_args, value.str[i] );
2098 init_paths();
2100 list_init( &sources );
2101 list_init( &includes );
2103 for (var = source_vars; *var; var++)
2105 value = get_expanded_make_var_array( *var );
2106 for (i = 0; i < value.count; i++) add_src_file( value.str[i] );
2109 add_generated_sources();
2111 value = get_expanded_make_var_array( "EXTRA_OBJS" );
2112 for (i = 0; i < value.count; i++)
2114 /* default to .c for unknown extra object files */
2115 if (strendswith( value.str[i], ".o" ))
2116 add_generated_source( value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2117 else
2118 add_generated_source( value.str[i], NULL );
2121 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( file, 0 );
2122 output_dependencies( output_file_name );
2123 output_file_name = NULL;
2127 /*******************************************************************
2128 * parse_makeflags
2130 static void parse_makeflags( const char *flags )
2132 const char *p = flags;
2133 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2135 while (*p)
2137 while (isspace(*p)) p++;
2138 var = buffer;
2139 while (*p && !isspace(*p))
2141 if (*p == '\\' && p[1]) p++;
2142 *var++ = *p++;
2144 *var = 0;
2145 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2150 /*******************************************************************
2151 * parse_option
2153 static int parse_option( const char *opt )
2155 if (opt[0] != '-')
2157 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2158 return 0;
2160 switch(opt[1])
2162 case 'I':
2163 if (opt[2]) strarray_add_uniq( &include_args, opt );
2164 break;
2165 case 'C':
2166 src_dir = opt + 2;
2167 break;
2168 case 'S':
2169 top_src_dir = opt + 2;
2170 break;
2171 case 'T':
2172 top_obj_dir = opt + 2;
2173 break;
2174 case 'P':
2175 parent_dir = opt + 2;
2176 break;
2177 case 'f':
2178 if (opt[2]) makefile_name = opt + 2;
2179 break;
2180 case 'M':
2181 parse_makefile_mode = 1;
2182 break;
2183 case 'R':
2184 relative_dir_mode = 1;
2185 break;
2186 case 's':
2187 if (opt[2]) Separator = opt + 2;
2188 else Separator = NULL;
2189 break;
2190 case 'x':
2191 break; /* ignored */
2192 default:
2193 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2194 exit(1);
2196 return 1;
2200 /*******************************************************************
2201 * main
2203 int main( int argc, char *argv[] )
2205 const char *makeflags = getenv( "MAKEFLAGS" );
2206 struct incl_file *pFile;
2207 int i, j;
2209 if (makeflags) parse_makeflags( makeflags );
2211 i = 1;
2212 while (i < argc)
2214 if (parse_option( argv[i] ))
2216 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2217 argc--;
2219 else i++;
2222 if (relative_dir_mode)
2224 char *relpath;
2226 if (argc != 3)
2228 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2229 exit( 1 );
2231 relpath = get_relative_path( argv[1], argv[2] );
2232 printf( "%s\n", relpath ? relpath : "." );
2233 exit( 0 );
2236 atexit( cleanup_files );
2237 signal( SIGTERM, exit_on_signal );
2238 signal( SIGINT, exit_on_signal );
2239 #ifdef SIGHUP
2240 signal( SIGHUP, exit_on_signal );
2241 #endif
2243 if (parse_makefile_mode)
2245 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2246 exit( 0 );
2249 init_paths();
2250 for (i = 1; i < argc; i++) add_src_file( argv[i] );
2251 add_generated_sources();
2253 LIST_FOR_EACH_ENTRY( pFile, &includes, struct incl_file, entry ) parse_file( pFile, 0 );
2254 output_dependencies( makefile_name );
2255 return 0;