gdiplus: Free the correct pointer (Coverity).
[wine.git] / tools / makedep.c
blobfba15ed14c976f41cb78ce5980446812f712faa6
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 /* Max first-level includes per file */
39 #define MAX_INCLUDES 200
41 struct incl_file
43 struct list entry;
44 char *name;
45 char *filename;
46 char *sourcename; /* source file name for generated headers */
47 struct incl_file *included_by; /* file that included this one */
48 int included_line; /* line where this file was included */
49 unsigned int flags; /* flags (see below) */
50 struct incl_file *owner;
51 struct incl_file *files[MAX_INCLUDES];
54 #define FLAG_SYSTEM 0x0001 /* is it a system include (#include <name>) */
55 #define FLAG_GENERATED 0x0002 /* generated file */
56 #define FLAG_IDL_PROXY 0x0004 /* generates a proxy (_p.c) file */
57 #define FLAG_IDL_CLIENT 0x0008 /* generates a client (_c.c) file */
58 #define FLAG_IDL_SERVER 0x0010 /* generates a server (_s.c) file */
59 #define FLAG_IDL_IDENT 0x0020 /* generates an ident (_i.c) file */
60 #define FLAG_IDL_REGISTER 0x0040 /* generates a registration (_r.res) file */
61 #define FLAG_IDL_TYPELIB 0x0080 /* generates a typelib (.tlb) file */
62 #define FLAG_IDL_REGTYPELIB 0x0100 /* generates a registered typelib (_t.res) file */
63 #define FLAG_IDL_HEADER 0x0200 /* generates a header (.h) file */
64 #define FLAG_RC_PO 0x0400 /* rc file contains translations */
65 #define FLAG_C_IMPLIB 0x0800 /* file is part of an import library */
67 static const struct
69 unsigned int flag;
70 const char *ext;
71 } idl_outputs[] =
73 { FLAG_IDL_TYPELIB, ".tlb" },
74 { FLAG_IDL_REGTYPELIB, "_t.res" },
75 { FLAG_IDL_CLIENT, "_c.c" },
76 { FLAG_IDL_IDENT, "_i.c" },
77 { FLAG_IDL_PROXY, "_p.c" },
78 { FLAG_IDL_SERVER, "_s.c" },
79 { FLAG_IDL_REGISTER, "_r.res" },
80 { FLAG_IDL_HEADER, ".h" }
83 static struct list sources = LIST_INIT(sources);
84 static struct list includes = LIST_INIT(includes);
86 struct strarray
88 unsigned int count; /* strings in use */
89 unsigned int size; /* total allocated size */
90 const char **str;
93 static const struct strarray empty_strarray;
95 static struct strarray include_args;
96 static struct strarray object_extensions;
97 static struct strarray make_vars;
98 static struct strarray cmdline_vars;
100 static const char *base_dir = ".";
101 static const char *src_dir;
102 static const char *top_src_dir;
103 static const char *top_obj_dir;
104 static const char *parent_dir;
105 static const char *makefile_name = "Makefile";
106 static const char *Separator = "### Dependencies";
107 static const char *input_file_name;
108 static const char *output_file_name;
109 static const char *temp_file_name;
110 static int parse_makefile_mode;
111 static int relative_dir_mode;
112 static int input_line;
113 static int output_column;
114 static FILE *output_file;
116 static const char Usage[] =
117 "Usage: makedep [options] [files]\n"
118 "Options:\n"
119 " -Idir Search for include files in directory 'dir'\n"
120 " -Cdir Search for source files in directory 'dir'\n"
121 " -Sdir Set the top source directory\n"
122 " -Tdir Set the top object directory\n"
123 " -Pdir Set the parent source directory\n"
124 " -M dirs Parse the makefiles from the specified directories\n"
125 " -R from to Compute the relative path between two directories\n"
126 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
127 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
130 #ifndef __GNUC__
131 #define __attribute__(x)
132 #endif
134 static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
135 static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
136 static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
137 static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
139 /*******************************************************************
140 * fatal_error
142 static void fatal_error( const char *msg, ... )
144 va_list valist;
145 va_start( valist, msg );
146 if (input_file_name)
148 fprintf( stderr, "%s:", input_file_name );
149 if (input_line) fprintf( stderr, "%d:", input_line );
150 fprintf( stderr, " error: " );
152 else fprintf( stderr, "makedep: error: " );
153 vfprintf( stderr, msg, valist );
154 va_end( valist );
155 exit(1);
159 /*******************************************************************
160 * fatal_perror
162 static void fatal_perror( const char *msg, ... )
164 va_list valist;
165 va_start( valist, msg );
166 if (input_file_name)
168 fprintf( stderr, "%s:", input_file_name );
169 if (input_line) fprintf( stderr, "%d:", input_line );
170 fprintf( stderr, " error: " );
172 else fprintf( stderr, "makedep: error: " );
173 vfprintf( stderr, msg, valist );
174 perror( " " );
175 va_end( valist );
176 exit(1);
180 /*******************************************************************
181 * cleanup_files
183 static void cleanup_files(void)
185 if (temp_file_name) unlink( temp_file_name );
186 if (output_file_name) unlink( output_file_name );
190 /*******************************************************************
191 * exit_on_signal
193 static void exit_on_signal( int sig )
195 exit( 1 ); /* this will call the atexit functions */
199 /*******************************************************************
200 * xmalloc
202 static void *xmalloc( size_t size )
204 void *res;
205 if (!(res = malloc (size ? size : 1)))
206 fatal_error( "Virtual memory exhausted.\n" );
207 return res;
211 /*******************************************************************
212 * xrealloc
214 static void *xrealloc (void *ptr, size_t size)
216 void *res;
217 assert( size );
218 if (!(res = realloc( ptr, size )))
219 fatal_error( "Virtual memory exhausted.\n" );
220 return res;
223 /*******************************************************************
224 * xstrdup
226 static char *xstrdup( const char *str )
228 char *res = strdup( str );
229 if (!res) fatal_error( "Virtual memory exhausted.\n" );
230 return res;
234 /*******************************************************************
235 * strmake
237 static char *strmake( const char* fmt, ... )
239 int n;
240 size_t size = 100;
241 va_list ap;
243 for (;;)
245 char *p = xmalloc (size);
246 va_start(ap, fmt);
247 n = vsnprintf (p, size, fmt, ap);
248 va_end(ap);
249 if (n == -1) size *= 2;
250 else if ((size_t)n >= size) size = n + 1;
251 else return p;
252 free(p);
257 /*******************************************************************
258 * strendswith
260 static int strendswith( const char* str, const char* end )
262 int l = strlen(str);
263 int m = strlen(end);
265 return l >= m && strcmp(str + l - m, end) == 0;
269 /*******************************************************************
270 * output
272 static void output( const char *format, ... )
274 int ret;
275 va_list valist;
277 va_start( valist, format );
278 ret = vfprintf( output_file, format, valist );
279 va_end( valist );
280 if (ret < 0) fatal_perror( "output" );
281 if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
282 else output_column += ret;
286 /*******************************************************************
287 * strarray_add
289 static void strarray_add( struct strarray *array, const char *str )
291 if (array->count == array->size)
293 if (array->size) array->size *= 2;
294 else array->size = 16;
295 array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
297 array->str[array->count++] = str;
301 /*******************************************************************
302 * strarray_addall
304 static void strarray_addall( struct strarray *array, struct strarray added )
306 unsigned int i;
308 for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
312 /*******************************************************************
313 * strarray_insert
315 static void strarray_insert( struct strarray *array, unsigned int pos, const char *str )
317 unsigned int i;
319 strarray_add( array, NULL );
320 for (i = array->count - 1; i > pos; i--) array->str[i] = array->str[i - 1];
321 array->str[pos] = str;
325 /*******************************************************************
326 * strarray_add_uniq
328 static void strarray_add_uniq( struct strarray *array, const char *str )
330 unsigned int i;
332 for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return;
333 strarray_add( array, str );
337 /*******************************************************************
338 * output_filename
340 static void output_filename( const char *name )
342 if (output_column + strlen(name) + 1 > 100)
344 output( " \\\n" );
345 output( " " );
347 else if (output_column) output( " " );
348 output( "%s", name );
352 /*******************************************************************
353 * output_filenames
355 static void output_filenames( struct strarray array )
357 unsigned int i;
359 for (i = 0; i < array.count; i++) output_filename( array.str[i] );
363 /*******************************************************************
364 * get_extension
366 static char *get_extension( char *filename )
368 char *ext = strrchr( filename, '.' );
369 if (ext && strchr( ext, '/' )) ext = NULL;
370 return ext;
374 /*******************************************************************
375 * replace_extension
377 static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
379 char *ret;
380 int name_len = strlen( name );
381 int ext_len = strlen( old_ext );
383 if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
384 ret = xmalloc( name_len + strlen( new_ext ) + 1 );
385 memcpy( ret, name, name_len );
386 strcpy( ret + name_len, new_ext );
387 return ret;
391 /*******************************************************************
392 * strarray_replace_extension
394 static struct strarray strarray_replace_extension( const struct strarray *array,
395 const char *old_ext, const char *new_ext )
397 unsigned int i;
398 struct strarray ret;
400 ret.count = ret.size = array->count;
401 ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
402 for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
403 return ret;
407 /*******************************************************************
408 * replace_substr
410 static char *replace_substr( const char *str, const char *start, unsigned int len, const char *replace )
412 unsigned int pos = start - str;
413 char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
414 memcpy( ret, str, pos );
415 strcpy( ret + pos, replace );
416 strcat( ret + pos, start + len );
417 return ret;
421 /*******************************************************************
422 * get_relative_path
424 * Determine where the destination path is located relative to the 'from' path.
426 static char *get_relative_path( const char *from, const char *dest )
428 const char *start;
429 char *ret, *p;
430 unsigned int dotdots = 0;
432 /* a path of "." is equivalent to an empty path */
433 if (!strcmp( from, "." )) from = "";
435 for (;;)
437 while (*from == '/') from++;
438 while (*dest == '/') dest++;
439 start = dest; /* save start of next path element */
440 if (!*from) break;
442 while (*from && *from != '/' && *from == *dest) { from++; dest++; }
443 if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
445 /* count remaining elements in 'from' */
448 dotdots++;
449 while (*from && *from != '/') from++;
450 while (*from == '/') from++;
452 while (*from);
453 break;
456 if (!start[0] && !dotdots) return NULL; /* empty path */
458 ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
459 for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
461 if (start[0]) strcpy( p, start );
462 else p[-1] = 0; /* remove trailing slash */
463 return ret;
467 /*******************************************************************
468 * init_paths
470 static void init_paths(void)
472 /* ignore redundant source paths */
473 if (src_dir && !strcmp( src_dir, "." )) src_dir = NULL;
474 if (top_src_dir && top_obj_dir && !strcmp( top_src_dir, top_obj_dir )) top_src_dir = NULL;
476 if (top_src_dir) strarray_insert( &include_args, 0, strmake( "-I%s/include", top_src_dir ));
477 else if (top_obj_dir) strarray_insert( &include_args, 0, strmake( "-I%s/include", top_obj_dir ));
479 /* set the default extension list for object files */
480 if (!object_extensions.count) strarray_add( &object_extensions, "o" );
484 /*******************************************************************
485 * get_line
487 static char *get_line( FILE *file )
489 static char *buffer;
490 static unsigned int size;
492 if (!size)
494 size = 1024;
495 buffer = xmalloc( size );
497 if (!fgets( buffer, size, file )) return NULL;
498 input_line++;
500 for (;;)
502 char *p = buffer + strlen(buffer);
503 /* if line is larger than buffer, resize buffer */
504 while (p == buffer + size - 1 && p[-1] != '\n')
506 buffer = xrealloc( buffer, size * 2 );
507 if (!fgets( buffer + size - 1, size + 1, file )) break;
508 p = buffer + strlen(buffer);
509 size *= 2;
511 if (p > buffer && p[-1] == '\n')
513 *(--p) = 0;
514 if (p > buffer && p[-1] == '\r') *(--p) = 0;
515 if (p > buffer && p[-1] == '\\')
517 *(--p) = 0;
518 /* line ends in backslash, read continuation line */
519 if (!fgets( p, size - (p - buffer), file )) return buffer;
520 input_line++;
521 continue;
524 return buffer;
528 /*******************************************************************
529 * find_src_file
531 static struct incl_file *find_src_file( const char *name )
533 struct incl_file *file;
535 LIST_FOR_EACH_ENTRY( file, &sources, struct incl_file, entry )
536 if (!strcmp( name, file->name )) return file;
537 return NULL;
540 /*******************************************************************
541 * find_include_file
543 static struct incl_file *find_include_file( const char *name )
545 struct incl_file *file;
547 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry )
548 if (!strcmp( name, file->name )) return file;
549 return NULL;
552 /*******************************************************************
553 * add_include
555 * Add an include file if it doesn't already exists.
557 static struct incl_file *add_include( struct incl_file *pFile, const char *name, int system )
559 struct incl_file *include;
560 char *ext;
561 int pos;
563 for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
564 if (pos >= MAX_INCLUDES)
565 fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
567 /* enforce some rules for the Wine tree */
569 if (!memcmp( name, "../", 3 ))
570 fatal_error( "#include directive with relative path not allowed\n" );
572 if (!strcmp( name, "config.h" ))
574 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
575 fatal_error( "config.h must not be included by a header file\n" );
576 if (pos)
577 fatal_error( "config.h must be included before anything else\n" );
579 else if (!strcmp( name, "wine/port.h" ))
581 if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
582 fatal_error( "wine/port.h must not be included by a header file\n" );
583 if (!pos) fatal_error( "config.h must be included before wine/port.h\n" );
584 if (pos > 1)
585 fatal_error( "wine/port.h must be included before everything except config.h\n" );
586 if (strcmp( pFile->files[0]->name, "config.h" ))
587 fatal_error( "config.h must be included before wine/port.h\n" );
590 LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
591 if (!strcmp( name, include->name )) goto found;
593 include = xmalloc( sizeof(*include) );
594 memset( include, 0, sizeof(*include) );
595 include->name = xstrdup(name);
596 include->included_by = pFile;
597 include->included_line = input_line;
598 if (system) include->flags |= FLAG_SYSTEM;
599 list_add_tail( &includes, &include->entry );
600 found:
601 pFile->files[pos] = include;
602 return include;
606 /*******************************************************************
607 * open_file
609 static FILE *open_file( const char *path )
611 FILE *ret;
613 if (path[0] != '/' && strcmp( base_dir, "." ))
615 char *full_path = strmake( "%s/%s", base_dir, path );
616 ret = fopen( full_path, "r" );
617 free( full_path );
619 else ret = fopen( path, "r" );
621 return ret;
624 /*******************************************************************
625 * open_src_file
627 static FILE *open_src_file( struct incl_file *pFile )
629 FILE *file;
631 /* first try name as is */
632 if ((file = open_file( pFile->name )))
634 pFile->filename = xstrdup( pFile->name );
635 return file;
637 /* now try in source dir */
638 if (src_dir)
640 pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
641 file = open_file( pFile->filename );
643 /* now try parent dir */
644 if (!file && parent_dir)
646 if (src_dir)
647 pFile->filename = strmake( "%s/%s/%s", src_dir, parent_dir, pFile->name );
648 else
649 pFile->filename = strmake( "%s/%s", parent_dir, pFile->name );
650 file = open_file( pFile->filename );
652 if (!file) fatal_perror( "open %s", pFile->name );
653 return file;
657 /*******************************************************************
658 * open_include_file
660 static FILE *open_include_file( struct incl_file *pFile )
662 FILE *file = NULL;
663 char *filename, *p;
664 unsigned int i, len;
666 errno = ENOENT;
668 /* check for generated bison header */
670 if (strendswith( pFile->name, ".tab.h" ))
672 filename = replace_extension( pFile->name, ".tab.h", ".y" );
673 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
675 if ((file = open_file( filename )))
677 pFile->sourcename = filename;
678 pFile->filename = xstrdup( pFile->name );
679 /* don't bother to parse it */
680 fclose( file );
681 return NULL;
683 free( filename );
686 /* check for corresponding idl file in source dir */
688 if (strendswith( pFile->name, ".h" ))
690 filename = replace_extension( pFile->name, ".h", ".idl" );
691 if (src_dir) filename = strmake( "%s/%s", src_dir, filename );
693 if ((file = open_file( filename )))
695 pFile->sourcename = filename;
696 pFile->filename = xstrdup( pFile->name );
697 return file;
699 free( filename );
702 /* now try in source dir */
703 if (src_dir)
704 filename = strmake( "%s/%s", src_dir, pFile->name );
705 else
706 filename = xstrdup( pFile->name );
707 if ((file = open_file( filename ))) goto found;
708 free( filename );
710 /* now try in parent source dir */
711 if (parent_dir)
713 if (src_dir)
714 filename = strmake( "%s/%s/%s", src_dir, parent_dir, pFile->name );
715 else
716 filename = strmake( "%s/%s", parent_dir, pFile->name );
717 if ((file = open_file( filename ))) goto found;
718 free( filename );
721 /* check for corresponding idl file in global includes */
723 if (strendswith( pFile->name, ".h" ))
725 filename = replace_extension( pFile->name, ".h", ".idl" );
726 if (top_src_dir)
727 filename = strmake( "%s/include/%s", top_src_dir, filename );
728 else if (top_obj_dir)
729 filename = strmake( "%s/include/%s", top_obj_dir, filename );
730 else
731 filename = NULL;
733 if (filename && (file = open_file( filename )))
735 pFile->sourcename = filename;
736 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
737 return file;
739 free( filename );
742 /* check for corresponding .in file in global includes (for config.h.in) */
744 if (strendswith( pFile->name, ".h" ))
746 filename = replace_extension( pFile->name, ".h", ".h.in" );
747 if (top_src_dir)
748 filename = strmake( "%s/include/%s", top_src_dir, filename );
749 else if (top_obj_dir)
750 filename = strmake( "%s/include/%s", top_obj_dir, filename );
751 else
752 filename = NULL;
754 if (filename && (file = open_file( filename )))
756 pFile->sourcename = filename;
757 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
758 return file;
760 free( filename );
763 /* check for corresponding .x file in global includes */
765 if (strendswith( pFile->name, "tmpl.h" ))
767 filename = replace_extension( pFile->name, ".h", ".x" );
768 if (top_src_dir)
769 filename = strmake( "%s/include/%s", top_src_dir, filename );
770 else if (top_obj_dir)
771 filename = strmake( "%s/include/%s", top_obj_dir, filename );
772 else
773 filename = NULL;
775 if (filename && (file = open_file( filename )))
777 pFile->sourcename = filename;
778 pFile->filename = strmake( "%s/include/%s", top_obj_dir, pFile->name );
779 return file;
781 free( filename );
784 /* now search in include paths */
785 for (i = 0; i < include_args.count; i++)
787 const char *dir = include_args.str[i] + 2; /* skip -I */
788 if (*dir == '/')
790 /* ignore absolute paths that don't point into the source dir */
791 if (!top_src_dir) continue;
792 len = strlen( top_src_dir );
793 if (strncmp( dir, top_src_dir, len )) continue;
794 if (dir[len] && dir[len] != '/') continue;
796 filename = strmake( "%s/%s", dir, pFile->name );
797 if ((file = open_file( filename ))) goto found;
798 free( filename );
800 if (pFile->flags & FLAG_SYSTEM) return NULL; /* ignore system files we cannot find */
802 /* try in src file directory */
803 if ((p = strrchr(pFile->included_by->filename, '/')))
805 int l = p - pFile->included_by->filename + 1;
806 filename = xmalloc(l + strlen(pFile->name) + 1);
807 memcpy( filename, pFile->included_by->filename, l );
808 strcpy( filename + l, pFile->name );
809 if ((file = open_file( filename ))) goto found;
810 free( filename );
813 fprintf( stderr, "%s:%d: error: ", pFile->included_by->filename, pFile->included_line );
814 perror( pFile->name );
815 pFile = pFile->included_by;
816 while (pFile && pFile->included_by)
818 const char *parent = pFile->included_by->sourcename;
819 if (!parent) parent = pFile->included_by->name;
820 fprintf( stderr, "%s:%d: note: %s was first included here\n",
821 parent, pFile->included_line, pFile->name );
822 pFile = pFile->included_by;
824 exit(1);
826 found:
827 pFile->filename = filename;
828 return file;
832 /*******************************************************************
833 * parse_include_directive
835 static void parse_include_directive( struct incl_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_include( source, include, (quote == '>') );
851 /*******************************************************************
852 * parse_pragma_directive
854 static void parse_pragma_directive( struct incl_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_include( source, p, 0 );
868 return;
870 if (strendswith( source->name, ".idl" ))
872 if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
873 else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
874 else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
875 else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
876 else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
877 else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
878 else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
879 else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
881 else if (strendswith( source->name, ".rc" ))
883 if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
885 else if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
890 /*******************************************************************
891 * parse_cpp_directive
893 static void parse_cpp_directive( struct incl_file *source, char *str )
895 while (*str && isspace(*str)) str++;
896 if (*str++ != '#') return;
897 while (*str && isspace(*str)) str++;
899 if (!strncmp( str, "include", 7 ))
900 parse_include_directive( source, str + 7 );
901 else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
902 parse_include_directive( source, str + 6 );
903 else if (!strncmp( str, "pragma", 6 ))
904 parse_pragma_directive( source, str + 6 );
908 /*******************************************************************
909 * parse_idl_file
911 * If for_h_file is non-zero, it means we are not interested in the idl file
912 * itself, but only in the contents of the .h file that will be generated from it.
914 static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file )
916 char *buffer, *include;
918 input_line = 0;
919 if (for_h_file)
921 /* generated .h file always includes these */
922 add_include( pFile, "rpc.h", 1 );
923 add_include( pFile, "rpcndr.h", 1 );
926 while ((buffer = get_line( file )))
928 char quote;
929 char *p = buffer;
930 while (*p && isspace(*p)) p++;
932 if (!strncmp( p, "import", 6 ))
934 p += 6;
935 while (*p && isspace(*p)) p++;
936 if (*p != '"') continue;
937 include = ++p;
938 while (*p && (*p != '"')) p++;
939 if (!*p) fatal_error( "malformed import directive\n" );
940 *p = 0;
941 if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
942 add_include( pFile, include, 0 );
943 continue;
946 if (for_h_file) /* only check for #include inside cpp_quote */
948 if (strncmp( p, "cpp_quote", 9 )) continue;
949 p += 9;
950 while (*p && isspace(*p)) p++;
951 if (*p++ != '(') continue;
952 while (*p && isspace(*p)) p++;
953 if (*p++ != '"') continue;
954 if (*p++ != '#') continue;
955 while (*p && isspace(*p)) p++;
956 if (strncmp( p, "include", 7 )) continue;
957 p += 7;
958 while (*p && isspace(*p)) p++;
959 if (*p == '\\' && p[1] == '"')
961 p += 2;
962 quote = '"';
964 else
966 if (*p++ != '<' ) continue;
967 quote = '>';
969 include = p;
970 while (*p && (*p != quote)) p++;
971 if (!*p || (quote == '"' && p[-1] != '\\'))
972 fatal_error( "malformed #include directive inside cpp_quote\n" );
973 if (quote == '"') p--; /* remove backslash */
974 *p = 0;
975 add_include( pFile, include, (quote == '>') );
976 continue;
979 parse_cpp_directive( pFile, p );
983 /*******************************************************************
984 * parse_c_file
986 static void parse_c_file( struct incl_file *pFile, FILE *file )
988 char *buffer;
990 input_line = 0;
991 while ((buffer = get_line( file )))
993 parse_cpp_directive( pFile, buffer );
998 /*******************************************************************
999 * parse_rc_file
1001 static void parse_rc_file( struct incl_file *pFile, FILE *file )
1003 char *buffer, *include;
1005 input_line = 0;
1006 while ((buffer = get_line( file )))
1008 char quote;
1009 char *p = buffer;
1010 while (*p && isspace(*p)) p++;
1012 if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
1014 p += 2;
1015 while (*p && isspace(*p)) p++;
1016 if (strncmp( p, "@makedep:", 9 )) continue;
1017 p += 9;
1018 while (*p && isspace(*p)) p++;
1019 quote = '"';
1020 if (*p == quote)
1022 include = ++p;
1023 while (*p && *p != quote) p++;
1025 else
1027 include = p;
1028 while (*p && !isspace(*p) && *p != '*') p++;
1030 if (!*p)
1031 fatal_error( "malformed makedep comment\n" );
1032 *p = 0;
1033 add_include( pFile, include, (quote == '>') );
1034 continue;
1037 parse_cpp_directive( pFile, buffer );
1042 /*******************************************************************
1043 * parse_in_file
1045 static void parse_in_file( struct incl_file *source, FILE *file )
1047 char *p, *buffer;
1049 /* make sure it gets rebuilt when the version changes */
1050 add_include( source, "config.h", 1 );
1052 if (!strendswith( source->filename, ".man.in" )) return; /* not a man page */
1054 input_line = 0;
1055 while ((buffer = get_line( file )))
1057 if (strncmp( buffer, ".TH", 3 )) continue;
1058 if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
1059 if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
1060 if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
1061 source->sourcename = xstrdup( p ); /* abuse source name to store section */
1062 return;
1067 /*******************************************************************
1068 * parse_file
1070 static void parse_file( struct incl_file *source, int src )
1072 FILE *file;
1074 /* don't try to open certain types of files */
1075 if (strendswith( source->name, ".tlb" ))
1077 source->filename = xstrdup( source->name );
1078 return;
1081 file = src ? open_src_file( source ) : open_include_file( source );
1082 if (!file) return;
1083 input_file_name = source->filename;
1085 if (source->sourcename && strendswith( source->sourcename, ".idl" ))
1086 parse_idl_file( source, file, 1 );
1087 else if (strendswith( source->filename, ".idl" ))
1088 parse_idl_file( source, file, 0 );
1089 else if (strendswith( source->filename, ".c" ) ||
1090 strendswith( source->filename, ".m" ) ||
1091 strendswith( source->filename, ".h" ) ||
1092 strendswith( source->filename, ".l" ) ||
1093 strendswith( source->filename, ".y" ))
1094 parse_c_file( source, file );
1095 else if (strendswith( source->filename, ".rc" ))
1096 parse_rc_file( source, file );
1097 else if (strendswith( source->filename, ".in" ))
1098 parse_in_file( source, file );
1099 fclose(file);
1100 input_file_name = NULL;
1104 /*******************************************************************
1105 * add_src_file
1107 * Add a source file to the list.
1109 static struct incl_file *add_src_file( const char *name )
1111 struct incl_file *file;
1113 if ((file = find_src_file( name ))) return file; /* we already have it */
1114 file = xmalloc( sizeof(*file) );
1115 memset( file, 0, sizeof(*file) );
1116 file->name = xstrdup(name);
1117 list_add_tail( &sources, &file->entry );
1118 parse_file( file, 1 );
1119 return file;
1123 /*******************************************************************
1124 * get_make_variable
1126 static char *get_make_variable( const char *name )
1128 unsigned int i;
1130 for (i = 0; i < cmdline_vars.count; i += 2)
1131 if (!strcmp( cmdline_vars.str[i], name ))
1132 return xstrdup( cmdline_vars.str[i + 1] );
1134 for (i = 0; i < make_vars.count; i += 2)
1135 if (!strcmp( make_vars.str[i], name ))
1136 return xstrdup( make_vars.str[i + 1] );
1137 return NULL;
1141 /*******************************************************************
1142 * get_expanded_make_variable
1144 static char *get_expanded_make_variable( const char *name )
1146 char *p, *end, *var, *expand, *tmp;
1148 expand = get_make_variable( name );
1149 if (!expand) return NULL;
1151 p = expand;
1152 while ((p = strchr( p, '$' )))
1154 if (p[1] == '(')
1156 if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
1157 *end++ = 0;
1158 if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
1159 var = get_make_variable( p + 2 );
1160 tmp = replace_substr( expand, p, end - p, var ? var : "" );
1161 free( var );
1163 else if (p[1] == '$')
1165 tmp = replace_substr( expand, p, 2, "$" );
1167 else fatal_error( "syntax error in '%s'\n", expand );
1169 /* switch to the new string */
1170 p = tmp + (p - expand);
1171 free( expand );
1172 expand = tmp;
1175 /* consider empty variables undefined */
1176 p = expand;
1177 while (*p && isspace(*p)) p++;
1178 if (*p) return expand;
1179 free( expand );
1180 return NULL;
1184 /*******************************************************************
1185 * get_expanded_make_var_array
1187 static struct strarray get_expanded_make_var_array( const char *name )
1189 struct strarray ret = empty_strarray;
1190 char *value, *token;
1192 if ((value = get_expanded_make_variable( name )))
1193 for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
1194 strarray_add( &ret, token );
1195 return ret;
1199 /*******************************************************************
1200 * set_make_variable
1202 static int set_make_variable( struct strarray *array, const char *assignment )
1204 unsigned int i;
1205 char *p, *name;
1207 p = name = xstrdup( assignment );
1208 while (isalnum(*p) || *p == '_') p++;
1209 if (name == p) return 0; /* not a variable */
1210 if (isspace(*p))
1212 *p++ = 0;
1213 while (isspace(*p)) p++;
1215 if (*p != '=') return 0; /* not an assignment */
1216 *p++ = 0;
1217 while (isspace(*p)) p++;
1219 /* redefining a variable replaces the previous value */
1220 for (i = 0; i < array->count; i += 2)
1222 if (strcmp( array->str[i], name )) continue;
1223 array->str[i + 1] = p;
1224 return 1;
1226 strarray_add( array, name );
1227 strarray_add( array, p );
1228 return 1;
1232 /*******************************************************************
1233 * parse_makefile
1235 static void parse_makefile(void)
1237 char *buffer;
1238 FILE *file;
1240 input_file_name = strmake( "%s/%s", base_dir, makefile_name );
1241 if (!(file = fopen( input_file_name, "r" )))
1243 fatal_perror( "open" );
1244 exit( 1 );
1247 input_line = 0;
1248 while ((buffer = get_line( file )))
1250 if (Separator && !strncmp( buffer, Separator, strlen(Separator) )) break;
1251 if (*buffer == '\t') continue; /* command */
1252 while (isspace( *buffer )) buffer++;
1253 if (*buffer == '#') continue; /* comment */
1254 set_make_variable( &make_vars, buffer );
1256 fclose( file );
1257 input_file_name = NULL;
1261 /*******************************************************************
1262 * add_generated_source
1264 * Add a generated source file to the list.
1266 static struct incl_file *add_generated_source( const char *name, const char *filename )
1268 struct incl_file *file;
1270 if ((file = find_src_file( name ))) return file; /* we already have it */
1271 file = xmalloc( sizeof(*file) );
1272 memset( file, 0, sizeof(*file) );
1273 file->name = xstrdup( name );
1274 file->filename = xstrdup( filename ? filename : name );
1275 file->flags = FLAG_GENERATED;
1276 list_add_tail( &sources, &file->entry );
1277 return file;
1281 /*******************************************************************
1282 * add_generated_sources
1284 static void add_generated_sources(void)
1286 struct incl_file *source, *next, *file;
1288 LIST_FOR_EACH_ENTRY_SAFE( source, next, &sources, struct incl_file, entry )
1290 if (source->flags & FLAG_IDL_CLIENT)
1292 file = add_generated_source( replace_extension( source->name, ".idl", "_c.c" ), NULL );
1293 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1295 if (source->flags & FLAG_IDL_SERVER)
1297 file = add_generated_source( replace_extension( source->name, ".idl", "_s.c" ), NULL );
1298 add_include( file, "wine/exception.h", 0 );
1299 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1301 if (source->flags & FLAG_IDL_IDENT)
1303 file = add_generated_source( replace_extension( source->name, ".idl", "_i.c" ), NULL );
1304 add_include( file, "rpc.h", 0 );
1305 add_include( file, "rpcndr.h", 0 );
1306 add_include( file, "guiddef.h", 0 );
1308 if (source->flags & FLAG_IDL_PROXY)
1310 file = add_generated_source( "dlldata.o", "dlldata.c" );
1311 add_include( file, "objbase.h", 0 );
1312 add_include( file, "rpcproxy.h", 0 );
1313 file = add_generated_source( replace_extension( source->name, ".idl", "_p.c" ), NULL );
1314 add_include( file, "objbase.h", 0 );
1315 add_include( file, "rpcproxy.h", 0 );
1316 add_include( file, "wine/exception.h", 0 );
1317 add_include( file, replace_extension( source->name, ".idl", ".h" ), 0 );
1319 if (source->flags & FLAG_IDL_REGTYPELIB)
1321 add_generated_source( replace_extension( source->name, ".idl", "_t.res" ), NULL );
1323 if (source->flags & FLAG_IDL_REGISTER)
1325 add_generated_source( replace_extension( source->name, ".idl", "_r.res" ), NULL );
1327 if (strendswith( source->name, ".y" ))
1329 file = add_generated_source( replace_extension( source->name, ".y", ".tab.c" ), NULL );
1330 memcpy( file->files, source->files, sizeof(file->files) );
1332 if (strendswith( source->name, ".l" ))
1334 file = add_generated_source( replace_extension( source->name, ".l", ".yy.c" ), NULL );
1335 memcpy( file->files, source->files, sizeof(file->files) );
1338 if (get_make_variable( "TESTDLL" ))
1340 file = add_generated_source( "testlist.o", "testlist.c" );
1341 add_include( file, "wine/test.h", 0 );
1346 /*******************************************************************
1347 * output_include
1349 static void output_include( struct incl_file *pFile, struct incl_file *owner )
1351 int i;
1353 if (pFile->owner == owner) return;
1354 if (!pFile->filename) return;
1355 pFile->owner = owner;
1356 output_filename( pFile->filename );
1357 for (i = 0; i < MAX_INCLUDES; i++)
1358 if (pFile->files[i]) output_include( pFile->files[i], owner );
1362 /*******************************************************************
1363 * output_sources
1365 static struct strarray output_sources(void)
1367 struct incl_file *source;
1368 int i;
1369 const char *dllext = ".so";
1370 struct strarray object_files = empty_strarray;
1371 struct strarray crossobj_files = empty_strarray;
1372 struct strarray res_files = empty_strarray;
1373 struct strarray clean_files = empty_strarray;
1374 struct strarray po_files = empty_strarray;
1375 struct strarray mc_files = empty_strarray;
1376 struct strarray test_files = empty_strarray;
1377 struct strarray dlldata_files = empty_strarray;
1378 struct strarray c2man_files = empty_strarray;
1379 struct strarray implib_objs = empty_strarray;
1380 struct strarray includes = empty_strarray;
1381 struct strarray subdirs = empty_strarray;
1382 struct strarray phony_targets = empty_strarray;
1383 struct strarray imports = get_expanded_make_var_array( "IMPORTS" );
1384 struct strarray all_targets = get_expanded_make_var_array( "PROGRAMS" );
1385 struct strarray delayimports = get_expanded_make_var_array( "DELAYIMPORTS" );
1386 char *module = get_expanded_make_variable( "MODULE" );
1387 char *exeext = get_expanded_make_variable( "EXEEXT" );
1388 char *testdll = get_expanded_make_variable( "TESTDLL" );
1389 char *appmode = get_expanded_make_variable( "APPMODE" );
1390 char *staticlib = get_expanded_make_variable( "STATICLIB" );
1391 char *crosstarget = get_expanded_make_variable( "CROSSTARGET" );
1393 if (exeext && !strcmp( exeext, ".exe" )) dllext = "";
1395 strarray_add( &includes, "-I." );
1396 if (src_dir) strarray_add( &includes, strmake( "-I%s", src_dir ));
1397 if (parent_dir)
1399 if (src_dir) strarray_add( &includes, strmake( "-I%s/%s", src_dir, parent_dir ));
1400 else strarray_add( &includes, strmake( "-I%s", parent_dir ));
1402 if (top_src_dir && top_obj_dir) strarray_add( &includes, strmake( "-I%s/include", top_obj_dir ));
1403 strarray_addall( &includes, include_args );
1405 LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
1407 char *sourcedep;
1408 char *obj = xstrdup( source->name );
1409 char *ext = get_extension( obj );
1411 if (!ext) fatal_error( "unsupported file type %s\n", source->name );
1412 *ext++ = 0;
1414 if (src_dir && strchr( obj, '/' ))
1416 char *dir = xstrdup( obj );
1417 *strrchr( dir, '/' ) = 0;
1418 strarray_add_uniq( &subdirs, dir );
1419 sourcedep = strmake( "%s %s", dir, source->filename );
1421 else sourcedep = xstrdup( source->filename );
1423 if (!strcmp( ext, "y" )) /* yacc file */
1425 /* add source file dependency for parallel makes */
1426 char *header = strmake( "%s.tab.h", obj );
1428 if (find_include_file( header ))
1430 output( "%s.tab.h: %s\n", obj, sourcedep );
1431 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
1432 obj, obj, source->filename );
1433 output( "%s.tab.c: %s %s\n", obj, source->filename, header );
1434 strarray_add( &clean_files, strmake( "%s.tab.h", obj ));
1436 else output( "%s.tab.c: %s\n", obj, sourcedep );
1438 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj, source->filename );
1439 free( header );
1440 continue; /* no dependencies */
1442 else if (!strcmp( ext, "x" )) /* template file */
1444 output( "%s.h: $(MAKEXFTMPL) %s\n", obj, sourcedep );
1445 output( "\t$(MAKEXFTMPL) -H -o $@ %s\n", source->filename );
1446 strarray_add( &clean_files, strmake( "%s.h", obj ));
1447 continue; /* no dependencies */
1449 else if (!strcmp( ext, "l" )) /* lex file */
1451 output( "%s.yy.c: %s\n", obj, sourcedep );
1452 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source->filename );
1453 continue; /* no dependencies */
1455 else if (!strcmp( ext, "rc" )) /* resource file */
1457 if (source->flags & FLAG_RC_PO)
1459 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj, sourcedep );
1460 output( "\t$(WRC) -o $@ %s", source->filename );
1461 output_filenames( includes );
1462 output_filename( "$(RCFLAGS)" );
1463 output( "\n" );
1464 output( "%s.res rsrc.pot:", obj );
1465 strarray_add( &po_files, source->filename );
1467 else
1469 output( "%s.res: $(WRC) %s\n", obj, sourcedep );
1470 output( "\t$(WRC) -o $@ %s", source->filename );
1471 output_filenames( includes );
1472 output_filename( "$(RCFLAGS)" );
1473 output( "\n" );
1474 output( "%s.res:", obj );
1476 strarray_add( &res_files, strmake( "%s.res", obj ));
1478 else if (!strcmp( ext, "mc" )) /* message file */
1480 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj, sourcedep );
1481 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source->filename );
1482 strarray_add( &res_files, strmake( "%s.res", obj ));
1483 strarray_add( &mc_files, source->filename );
1484 output( "msg.pot %s.res:", obj );
1486 else if (!strcmp( ext, "idl" )) /* IDL file */
1488 struct strarray targets = empty_strarray;
1489 unsigned int i;
1490 char *dest;
1492 if (!source->flags || find_include_file( strmake( "%s.h", obj )))
1493 source->flags |= FLAG_IDL_HEADER;
1495 for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
1497 if (!(source->flags & idl_outputs[i].flag)) continue;
1498 dest = strmake( "%s%s", obj, idl_outputs[i].ext );
1499 if (!find_src_file( dest )) strarray_add( &clean_files, dest );
1500 strarray_add( &targets, dest );
1502 if (source->flags & FLAG_IDL_PROXY) strarray_add( &dlldata_files, source->name );
1503 output_filenames( targets );
1504 output( ": $(WIDL)\n" );
1505 output( "\t$(WIDL) -o $@ %s", source->filename );
1506 output_filenames( includes );
1507 output_filename( "$(TARGETFLAGS)" );
1508 output_filename( "$(IDLFLAGS)" );
1509 output( "\n" );
1510 output_filenames( targets );
1511 output( ": %s", sourcedep );
1513 else if (!strcmp( ext, "in" )) /* .in file or man page */
1515 if (strendswith( obj, ".man" ) && source->sourcename)
1517 char *dir, *dest = replace_extension( obj, ".man", "" );
1518 char *lang = strchr( dest, '.' );
1519 if (lang)
1521 *lang++ = 0;
1522 dir = strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang, source->sourcename );
1524 else dir = strmake( "$(DESTDIR)$(mandir)/man%s", source->sourcename );
1525 output( "install-man-pages:: %s\n", obj );
1526 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n",
1527 obj, dir, dest, source->sourcename );
1528 output( "uninstall::\n" );
1529 output( "\t$(RM) %s/%s.%s\n",
1530 dir, dest, source->sourcename );
1531 free( dest );
1532 free( dir );
1533 strarray_add( &all_targets, xstrdup(obj) );
1534 strarray_add_uniq( &phony_targets, "install-man-pages" );
1536 else strarray_add( &clean_files, xstrdup(obj) );
1537 output( "%s: %s\n", obj, sourcedep );
1538 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source->filename );
1539 output( "%s:", obj );
1541 else if (!strcmp( ext, "sfd" )) /* font file */
1543 char *fontforge = get_expanded_make_variable( "FONTFORGE" );
1544 if (fontforge && !src_dir)
1546 output( "%s.ttf: %s\n", obj, source->filename );
1547 output( "\t%s -script %s/fonts/genttf.ff %s $@\n",
1548 fontforge, top_src_dir ? top_src_dir : top_obj_dir, source->filename );
1550 free( fontforge );
1551 continue; /* no dependencies */
1553 else if (!strcmp( ext, "svg" )) /* svg file */
1555 char *convert = get_expanded_make_variable( "CONVERT" );
1556 char *rsvg = get_expanded_make_variable( "RSVG" );
1557 char *icotool = get_expanded_make_variable( "ICOTOOL" );
1558 if (convert && rsvg && icotool && !src_dir)
1560 output( "%s.ico %s.bmp: %s\n", obj, obj, source->filename );
1561 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" $(BUILDIMAGE) %s $@\n",
1562 convert, icotool, rsvg, source->filename );
1564 free( convert );
1565 free( rsvg );
1566 free( icotool );
1567 continue; /* no dependencies */
1569 else if (!strcmp( ext, "res" ))
1571 strarray_add( &res_files, source->name );
1572 continue; /* no dependencies */
1574 else
1576 if (source->flags & FLAG_GENERATED) strarray_add( &clean_files, source->filename );
1577 if (source->flags & FLAG_C_IMPLIB) strarray_add( &implib_objs, strmake( "%s.o", obj ));
1578 for (i = 0; i < object_extensions.count; i++)
1580 output( "%s.%s: %s\n", obj, object_extensions.str[i], sourcedep );
1581 if (strstr( object_extensions.str[i], "cross" ))
1583 strarray_add( &crossobj_files, strmake( "%s.%s", obj, object_extensions.str[i] ));
1584 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1585 output_filenames( includes );
1586 output_filename( "$(ALLCROSSCFLAGS)" );
1587 output( "\n" );
1589 else
1591 strarray_add( &object_files, strmake( "%s.%s", obj, object_extensions.str[i] ));
1592 output( "\t$(CC) -c -o $@ %s", source->filename );
1593 output_filenames( includes );
1594 output_filename( "$(ALLCFLAGS)" );
1595 output( "\n" );
1598 if (crosstarget && (source->flags & FLAG_C_IMPLIB))
1600 strarray_add( &crossobj_files, strmake( "%s.cross.o", obj ));
1601 output( "%s.cross.o: %s\n", obj, sourcedep );
1602 output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
1603 output_filenames( includes );
1604 output_filename( "$(ALLCROSSCFLAGS)" );
1605 output( "\n" );
1607 if (testdll && !strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1609 strarray_add( &test_files, source->name );
1610 output( "%s.ok:\n", obj );
1611 output( "\t$(RUNTEST) $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n", top_obj_dir,
1612 testdll, replace_extension( testdll, ".dll", "_test.exe" ), dllext, obj );
1614 if (!strcmp( ext, "c" ) && !(source->flags & FLAG_GENERATED))
1615 strarray_add( &c2man_files, source->filename );
1616 for (i = 0; i < object_extensions.count; i++)
1617 output( "%s.%s ", obj, object_extensions.str[i] );
1618 if (source->flags & FLAG_C_IMPLIB) output( "%s.cross.o", obj );
1619 output( ":" );
1621 free( obj );
1622 free( sourcedep );
1624 for (i = 0; i < MAX_INCLUDES; i++)
1625 if (source->files[i]) output_include( source->files[i], source );
1626 output( "\n" );
1629 /* rules for files that depend on multiple sources */
1631 if (po_files.count)
1633 output( "rsrc.pot: $(WRC)" );
1634 output_filenames( po_files );
1635 output( "\n" );
1636 output( "\t$(WRC) -O pot -o $@" );
1637 output_filenames( includes );
1638 output_filename( "$(RCFLAGS)" );
1639 output_filenames( po_files );
1640 output( "\n" );
1641 strarray_add( &clean_files, "rsrc.pot" );
1644 if (mc_files.count)
1646 output( "msg.pot: $(WMC)" );
1647 output_filenames( mc_files );
1648 output( "\n" );
1649 output( "\t$(WMC) -O pot -o $@" );
1650 output_filenames( mc_files );
1651 output( "\n" );
1652 strarray_add( &clean_files, "msg.pot" );
1655 if (dlldata_files.count)
1657 output( "dlldata.c: $(WIDL) %s\n", src_dir ? strmake("%s/Makefile.in", src_dir ) : "Makefile.in" );
1658 output( "\t$(WIDL) --dlldata-only -o $@" );
1659 output_filenames( dlldata_files );
1660 output( "\n" );
1663 if (module)
1665 int is_win16 = strendswith( module, "16" );
1666 char *importlib = get_expanded_make_variable( "IMPORTLIB" );
1667 struct strarray all_libs = empty_strarray;
1668 char *spec_file = appmode ? NULL : replace_extension( module, ".dll", ".spec" );
1670 if (spec_file && src_dir) spec_file = strmake( "%s/%s", src_dir, spec_file );
1671 for (i = 0; i < delayimports.count; i++)
1672 strarray_add( &all_libs, strmake( "-l%s", delayimports.str[i] ));
1673 for (i = 0; i < imports.count; i++)
1674 strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1675 for (i = 0; i < delayimports.count; i++)
1676 strarray_add( &all_libs, strmake( "-Wb,-d%s", delayimports.str[i] ));
1677 strarray_add( &all_libs, "-lwine" );
1678 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBPORT" ));
1679 strarray_addall( &all_libs, get_expanded_make_var_array( "EXTRALIBS" ));
1680 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1682 if (*dllext)
1684 strarray_add( &all_targets, strmake( "%s%s", module, dllext ));
1685 strarray_add( &all_targets, strmake( "%s.fake", module ));
1686 output( "%s%s %s.fake:", module, dllext, module );
1688 else
1690 strarray_add( &all_targets, module );
1691 output( "%s:", module );
1693 if (spec_file) output_filename( spec_file );
1694 output_filenames( object_files );
1695 output_filenames( res_files );
1696 output( "\n" );
1697 output( "\t$(WINEGCC) -o $@" );
1698 if (spec_file)
1700 output( " -shared %s", spec_file );
1701 output_filenames( get_expanded_make_var_array( "EXTRADLLFLAGS" ));
1703 else output_filename( appmode );
1704 output_filenames( object_files );
1705 output_filenames( res_files );
1706 output_filenames( all_libs );
1707 output_filename( "$(LDFLAGS)" );
1708 output( "\n" );
1710 if (spec_file && importlib)
1712 if (*dllext)
1714 strarray_add( &clean_files, strmake( "lib%s.def", importlib ));
1715 output( "lib%s.def: %s\n", importlib, spec_file );
1716 output( "\t$(WINEBUILD) -w --def -o $@ --export %s", spec_file );
1717 output_filenames( get_expanded_make_var_array( "TARGETFLAGS" ));
1718 if (is_win16) output_filename( "-m16" );
1719 output( "\n" );
1720 if (implib_objs.count)
1722 strarray_add( &clean_files, strmake( "lib%s.def.a", importlib ));
1723 output( "lib%s.def.a:", importlib );
1724 output_filenames( implib_objs );
1725 output( "\n" );
1726 output( "\t$(RM) $@\n" );
1727 output( "\t$(AR) $(ARFLAGS) $@" );
1728 output_filenames( implib_objs );
1729 output( "\n" );
1730 output( "\t$(RANLIB) $@\n" );
1733 else
1735 strarray_add( &clean_files, strmake( "lib%s.a", importlib ));
1736 output( "lib%s.a: %s", importlib, spec_file );
1737 output_filenames( implib_objs );
1738 output( "\n" );
1739 output( "\t$(WINEBUILD) -w --implib -o $@ --export %s", spec_file );
1740 output_filenames( get_expanded_make_var_array( "TARGETFLAGS" ));
1741 output_filenames( implib_objs );
1742 output( "\n" );
1744 if (crosstarget && !is_win16)
1746 struct strarray cross_files = strarray_replace_extension( &implib_objs, ".o", ".cross.o" );
1747 strarray_add( &clean_files, strmake( "lib%s.cross.a", importlib ));
1748 output( "lib%s.cross.a: %s", importlib, spec_file );
1749 output_filenames( cross_files );
1750 output( "\n" );
1751 output( "\t$(WINEBUILD) -b %s -w --implib -o $@ --export %s", crosstarget, spec_file );
1752 output_filenames( cross_files );
1753 output( "\n" );
1757 if (spec_file)
1759 if (c2man_files.count && top_obj_dir)
1761 char *manext = get_expanded_make_variable( "api_manext" );
1763 output( "manpages::\n" );
1764 output( "\t$(C2MAN) -w %s -R%s", spec_file, top_obj_dir );
1765 output_filename( strmake( "-I%s/include", top_src_dir ? top_src_dir : top_obj_dir ));
1766 output_filename( strmake( "-o %s/documentation/man%s", top_obj_dir, manext ? manext : "3w" ));
1767 output_filenames( c2man_files );
1768 output( "\n" );
1769 output( "htmlpages::\n" );
1770 output( "\t$(C2MAN) -Th -w %s -R%s", spec_file, top_obj_dir );
1771 output_filename( strmake( "-I%s/include", top_src_dir ? top_src_dir : top_obj_dir ));
1772 output_filename( strmake( "-o %s/documentation/html", top_obj_dir ));
1773 output_filenames( c2man_files );
1774 output( "\n" );
1775 output( "sgmlpages::\n" );
1776 output( "\t$(C2MAN) -Ts -w %s -R%s", spec_file, top_obj_dir );
1777 output_filename( strmake( "-I%s/include", top_src_dir ? top_src_dir : top_obj_dir ));
1778 output_filename( strmake( "-o %s/documentation/api-guide", top_obj_dir ));
1779 output_filenames( c2man_files );
1780 output( "\n" );
1781 output( "xmlpages::\n" );
1782 output( "\t$(C2MAN) -Tx -w %s -R%s", spec_file, top_obj_dir );
1783 output_filename( strmake( "-I%s/include", top_src_dir ? top_src_dir : top_obj_dir ));
1784 output_filename( strmake( "-o %s/documentation/api-guide-xml", top_obj_dir ));
1785 output_filenames( c2man_files );
1786 output( "\n" );
1787 strarray_add( &phony_targets, "manpages" );
1788 strarray_add( &phony_targets, "htmlpages" );
1789 strarray_add( &phony_targets, "sgmlpages" );
1790 strarray_add( &phony_targets, "xmlpages" );
1792 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1796 if (staticlib)
1798 strarray_add( &all_targets, staticlib );
1799 output( "%s:", staticlib );
1800 output_filenames( object_files );
1801 output( "\n\t$(RM) $@\n" );
1802 output( "\t$(AR) $(ARFLAGS) $@" );
1803 output_filenames( object_files );
1804 output( "\n\t$(RANLIB) $@\n" );
1805 if (crosstarget && object_extensions.count > 1)
1807 char *name = replace_extension( staticlib, ".a", ".cross.a" );
1809 strarray_add( &all_targets, name );
1810 output( "%s:", name );
1811 output_filenames( crossobj_files );
1812 output( "\n\t$(RM) $@\n" );
1813 output( "\t%s-ar $(ARFLAGS) $@", crosstarget );
1814 output_filenames( crossobj_files );
1815 output( "\n\t%s-ranlib $@\n", crosstarget );
1819 if (testdll)
1821 struct strarray ok_files = strarray_replace_extension( &test_files, ".c", ".ok" );
1822 char *testmodule = replace_extension( testdll, ".dll", "_test.exe" );
1823 char *stripped = replace_extension( testdll, ".dll", "_test-stripped.exe" );
1824 struct strarray all_libs = empty_strarray;
1826 for (i = 0; i < imports.count; i++) strarray_add( &all_libs, strmake( "-l%s", imports.str[i] ));
1827 strarray_addall( &all_libs, get_expanded_make_var_array( "LIBS" ));
1829 strarray_add( &all_targets, strmake( "%s%s", testmodule, dllext ));
1830 strarray_add( &clean_files, strmake( "%s%s", stripped, dllext ));
1831 output( "%s%s:\n", testmodule, dllext );
1832 output( "\t$(WINEGCC) -o $@" );
1833 if (appmode) output_filename( appmode );
1834 output_filenames( object_files );
1835 output_filenames( res_files );
1836 output_filenames( all_libs );
1837 output_filename( "$(LDFLAGS)" );
1838 output( "\n" );
1839 output( "%s%s:\n", stripped, dllext );
1840 output( "\t$(WINEGCC) -s -o $@ -Wb,-F,%s", testmodule );
1841 if (appmode) output_filename( appmode );
1842 output_filenames( object_files );
1843 output_filenames( res_files );
1844 output_filenames( all_libs );
1845 output_filename( "$(LDFLAGS)" );
1846 output( "\n" );
1847 output( "%s%s %s%s:", testmodule, dllext, stripped, dllext );
1848 output_filenames( object_files );
1849 output_filenames( res_files );
1850 output( "\n" );
1852 if (top_obj_dir)
1854 char *testres = replace_extension( testdll, ".dll", "_test.res" );
1855 output( "all: %s/programs/winetest/%s\n", top_obj_dir, testres );
1856 output( "%s/programs/winetest/%s: %s%s\n", top_obj_dir, testres, stripped, dllext );
1857 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | $(WRC) $(RCFLAGS) -o $@\n",
1858 testmodule, stripped, dllext );
1861 if (crosstarget)
1863 char *crosstest = replace_extension( testdll, ".dll", "_crosstest.exe" );
1865 strarray_add( &clean_files, crosstest );
1866 output( "crosstest: %s\n", crosstest );
1867 output( "%s:", crosstest );
1868 output_filenames( crossobj_files );
1869 output_filenames( res_files );
1870 output( "\n" );
1871 output( "\t$(CROSSWINEGCC) -o $@" );
1872 output_filenames( crossobj_files );
1873 output_filenames( res_files );
1874 output_filenames( all_libs );
1875 output_filename( "$(LDFLAGS)" );
1876 output( "\n" );
1877 strarray_add( &phony_targets, "crosstest" );
1880 output( "testlist.c: $(MAKECTESTS) %s\n", src_dir ? strmake("%s/Makefile.in", src_dir ) : "Makefile.in" );
1881 output( "\t$(MAKECTESTS) -o $@" );
1882 output_filenames( test_files );
1883 output( "\n" );
1884 output_filenames( ok_files );
1885 output( ": %s%s ../%s%s\n", testmodule, dllext, testdll, dllext );
1886 output( "check test:" );
1887 output_filenames( ok_files );
1888 output( "\n" );
1889 output( "testclean::\n" );
1890 output( "\t$(RM)" );
1891 output_filenames( ok_files );
1892 output( "\n" );
1893 strarray_addall( &clean_files, ok_files );
1894 strarray_add( &phony_targets, "check" );
1895 strarray_add( &phony_targets, "test" );
1896 strarray_add( &phony_targets, "testclean" );
1899 if (all_targets.count)
1901 output( "all:" );
1902 output_filenames( all_targets );
1903 output( "\n" );
1906 strarray_addall( &clean_files, object_files );
1907 strarray_addall( &clean_files, crossobj_files );
1908 strarray_addall( &clean_files, res_files );
1909 strarray_addall( &clean_files, all_targets );
1910 strarray_addall( &clean_files, get_expanded_make_var_array( "EXTRA_TARGETS" ));
1912 if (clean_files.count)
1914 output( "clean::\n" );
1915 output( "\t$(RM)" );
1916 output_filenames( clean_files );
1917 output( "\n" );
1920 if (subdirs.count)
1922 output_filenames( subdirs );
1923 output( ":\n" );
1924 output( "\t$(MKDIR_P) -m 755 $@\n" );
1927 if (phony_targets.count)
1929 output( ".PHONY:" );
1930 output_filenames( phony_targets );
1931 output( "\n" );
1934 return clean_files;
1938 /*******************************************************************
1939 * create_temp_file
1941 static FILE *create_temp_file( const char *orig )
1943 char *name = xmalloc( strlen(orig) + 13 );
1944 unsigned int i, id = getpid();
1945 int fd;
1946 FILE *ret = NULL;
1948 for (i = 0; i < 100; i++)
1950 sprintf( name, "%s.tmp%08x", orig, id );
1951 if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
1953 ret = fdopen( fd, "w" );
1954 break;
1956 if (errno != EEXIST) break;
1957 id += 7777;
1959 if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
1960 temp_file_name = name;
1961 return ret;
1965 /*******************************************************************
1966 * rename_temp_file
1968 static void rename_temp_file( const char *dest )
1970 int ret = rename( temp_file_name, dest );
1971 if (ret == -1 && errno == EEXIST)
1973 /* rename doesn't overwrite on windows */
1974 unlink( dest );
1975 ret = rename( temp_file_name, dest );
1977 if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
1978 temp_file_name = NULL;
1982 /*******************************************************************
1983 * output_gitignore
1985 static void output_gitignore( const char *dest, const struct strarray *files )
1987 int i;
1989 output_file = create_temp_file( dest );
1991 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
1992 output( "/.gitignore\n" );
1993 output( "/Makefile\n" );
1994 for (i = 0; i < files->count; i++)
1996 if (!strchr( files->str[i], '/' )) output( "/" );
1997 output( "%s\n", files->str[i] );
2000 if (fclose( output_file )) fatal_perror( "write" );
2001 output_file = NULL;
2002 rename_temp_file( dest );
2006 /*******************************************************************
2007 * output_dependencies
2009 static void output_dependencies( const char *path )
2011 struct strarray targets = empty_strarray;
2013 if (Separator && ((output_file = fopen( path, "r" ))))
2015 char buffer[1024];
2016 FILE *tmp_file = create_temp_file( path );
2017 int found = 0;
2019 while (fgets( buffer, sizeof(buffer), output_file ) && !found)
2021 if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer)) fatal_perror( "write" );
2022 found = !strncmp( buffer, Separator, strlen(Separator) );
2024 if (fclose( output_file )) fatal_perror( "write" );
2025 output_file = tmp_file;
2026 if (!found) output( "\n%s\n", Separator );
2028 else
2030 if (!(output_file = fopen( path, Separator ? "a" : "w" )))
2031 fatal_perror( "%s", path );
2034 targets = output_sources();
2036 fclose( output_file );
2037 output_file = NULL;
2038 if (temp_file_name) rename_temp_file( path );
2040 if (!src_dir) output_gitignore( strmake( "%s/.gitignore", base_dir ), &targets );
2044 /*******************************************************************
2045 * update_makefile
2047 static void update_makefile( const char *path )
2049 static const char *source_vars[] =
2051 "C_SRCS",
2052 "OBJC_SRCS",
2053 "RC_SRCS",
2054 "MC_SRCS",
2055 "IDL_SRCS",
2056 "BISON_SRCS",
2057 "LEX_SRCS",
2058 "XTEMPLATE_SRCS",
2059 "SVG_SRCS",
2060 "FONT_SRCS",
2061 "IN_SRCS",
2062 "MANPAGES",
2063 NULL
2065 const char **var;
2066 unsigned int i;
2067 struct strarray value;
2068 struct incl_file *file;
2070 base_dir = path;
2071 output_file_name = strmake( "%s/%s", base_dir, makefile_name );
2072 parse_makefile();
2074 src_dir = get_expanded_make_variable( "srcdir" );
2075 top_src_dir = get_expanded_make_variable( "top_srcdir" );
2076 top_obj_dir = get_expanded_make_variable( "top_builddir" );
2077 parent_dir = get_expanded_make_variable( "PARENTSRC" );
2079 object_extensions = empty_strarray;
2080 value = get_expanded_make_var_array( "MAKEDEPFLAGS" );
2081 for (i = 0; i < value.count; i++)
2082 if (!strncmp( value.str[i], "-x", 2 ))
2083 strarray_add( &object_extensions, value.str[i] + 2 );
2085 include_args = empty_strarray;
2086 value = get_expanded_make_var_array( "EXTRAINCL" );
2087 for (i = 0; i < value.count; i++)
2088 if (!strncmp( value.str[i], "-I", 2 ))
2089 strarray_add_uniq( &include_args, value.str[i] );
2091 init_paths();
2093 list_init( &sources );
2094 list_init( &includes );
2096 for (var = source_vars; *var; var++)
2098 value = get_expanded_make_var_array( *var );
2099 for (i = 0; i < value.count; i++) add_src_file( value.str[i] );
2102 add_generated_sources();
2104 value = get_expanded_make_var_array( "EXTRA_OBJS" );
2105 for (i = 0; i < value.count; i++)
2107 /* default to .c for unknown extra object files */
2108 if (strendswith( value.str[i], ".o" ))
2109 add_generated_source( value.str[i], replace_extension( value.str[i], ".o", ".c" ) );
2110 else
2111 add_generated_source( value.str[i], NULL );
2114 LIST_FOR_EACH_ENTRY( file, &includes, struct incl_file, entry ) parse_file( file, 0 );
2115 output_dependencies( output_file_name );
2116 output_file_name = NULL;
2120 /*******************************************************************
2121 * parse_makeflags
2123 static void parse_makeflags( const char *flags )
2125 const char *p = flags;
2126 char *var, *buffer = xmalloc( strlen(flags) + 1 );
2128 while (*p)
2130 while (isspace(*p)) p++;
2131 var = buffer;
2132 while (*p && !isspace(*p))
2134 if (*p == '\\' && p[1]) p++;
2135 *var++ = *p++;
2137 *var = 0;
2138 if (var > buffer) set_make_variable( &cmdline_vars, buffer );
2143 /*******************************************************************
2144 * parse_option
2146 static int parse_option( const char *opt )
2148 if (opt[0] != '-')
2150 if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
2151 return 0;
2153 switch(opt[1])
2155 case 'I':
2156 if (opt[2]) strarray_add_uniq( &include_args, opt );
2157 break;
2158 case 'C':
2159 src_dir = opt + 2;
2160 break;
2161 case 'S':
2162 top_src_dir = opt + 2;
2163 break;
2164 case 'T':
2165 top_obj_dir = opt + 2;
2166 break;
2167 case 'P':
2168 parent_dir = opt + 2;
2169 break;
2170 case 'f':
2171 if (opt[2]) makefile_name = opt + 2;
2172 break;
2173 case 'M':
2174 parse_makefile_mode = 1;
2175 break;
2176 case 'R':
2177 relative_dir_mode = 1;
2178 break;
2179 case 's':
2180 if (opt[2]) Separator = opt + 2;
2181 else Separator = NULL;
2182 break;
2183 case 'x':
2184 if (opt[2]) strarray_add( &object_extensions, xstrdup( opt + 2 ));
2185 break;
2186 default:
2187 fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
2188 exit(1);
2190 return 1;
2194 /*******************************************************************
2195 * main
2197 int main( int argc, char *argv[] )
2199 const char *makeflags = getenv( "MAKEFLAGS" );
2200 struct incl_file *pFile;
2201 int i, j;
2203 if (makeflags) parse_makeflags( makeflags );
2205 i = 1;
2206 while (i < argc)
2208 if (parse_option( argv[i] ))
2210 for (j = i; j < argc; j++) argv[j] = argv[j+1];
2211 argc--;
2213 else i++;
2216 if (relative_dir_mode)
2218 char *relpath;
2220 if (argc != 3)
2222 fprintf( stderr, "Option -r needs two directories\n%s", Usage );
2223 exit( 1 );
2225 relpath = get_relative_path( argv[1], argv[2] );
2226 printf( "%s\n", relpath ? relpath : "." );
2227 exit( 0 );
2230 atexit( cleanup_files );
2231 signal( SIGTERM, exit_on_signal );
2232 signal( SIGINT, exit_on_signal );
2233 #ifdef SIGHUP
2234 signal( SIGHUP, exit_on_signal );
2235 #endif
2237 if (parse_makefile_mode)
2239 for (i = 1; i < argc; i++) update_makefile( argv[i] );
2240 exit( 0 );
2243 init_paths();
2244 for (i = 1; i < argc; i++) add_src_file( argv[i] );
2245 add_generated_sources();
2247 LIST_FOR_EACH_ENTRY( pFile, &includes, struct incl_file, entry ) parse_file( pFile, 0 );
2248 output_dependencies( makefile_name );
2249 return 0;