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
22 #define NO_LIBWINE_PORT
23 #include "wine/port.h"
36 #include "wine/list.h"
43 char *sourcename
; /* source file name for generated headers */
44 void *args
; /* custom arguments for makefile rule */
45 struct incl_file
*included_by
; /* file that included this one */
46 int included_line
; /* line where this file was included */
47 unsigned int flags
; /* flags (see below) */
48 struct incl_file
*owner
;
49 unsigned int files_count
; /* files in use */
50 unsigned int files_size
; /* total allocated size */
51 struct incl_file
**files
;
54 #define FLAG_SYSTEM 0x000001 /* is it a system include (#include <name>) */
55 #define FLAG_GENERATED 0x000002 /* generated file */
56 #define FLAG_INSTALL 0x000004 /* file to install */
57 #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
58 #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
59 #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
60 #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
61 #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
62 #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
63 #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
64 #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
65 #define FLAG_RC_PO 0x010000 /* rc file contains translations */
66 #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
67 #define FLAG_SFD_FONTS 0x040000 /* sfd file generated bitmap fonts */
75 { FLAG_IDL_TYPELIB
, ".tlb" },
76 { FLAG_IDL_REGTYPELIB
, "_t.res" },
77 { FLAG_IDL_CLIENT
, "_c.c" },
78 { FLAG_IDL_IDENT
, "_i.c" },
79 { FLAG_IDL_PROXY
, "_p.c" },
80 { FLAG_IDL_SERVER
, "_s.c" },
81 { FLAG_IDL_REGISTER
, "_r.res" },
82 { FLAG_IDL_HEADER
, ".h" }
85 static struct list sources
= LIST_INIT(sources
);
86 static struct list includes
= LIST_INIT(includes
);
90 unsigned int count
; /* strings in use */
91 unsigned int size
; /* total allocated size */
95 static const struct strarray empty_strarray
;
97 static struct strarray include_args
;
98 static struct strarray define_args
;
99 static struct strarray appmode
;
100 static struct strarray dllflags
;
101 static struct strarray imports
;
102 static struct strarray make_vars
;
103 static struct strarray cmdline_vars
;
104 static struct strarray testlist_files
;
106 static const char *base_dir
;
107 static const char *src_dir
;
108 static const char *top_src_dir
;
109 static const char *top_obj_dir
;
110 static const char *parent_dir
;
111 static const char *tools_dir
;
112 static const char *tools_ext
;
113 static const char *makefile_name
= "Makefile";
114 static const char *Separator
= "### Dependencies";
115 static const char *input_file_name
;
116 static const char *output_file_name
;
117 static const char *temp_file_name
;
118 static int use_msvcrt
;
119 static int relative_dir_mode
;
120 static int input_line
;
121 static int output_column
;
122 static FILE *output_file
;
124 static const char Usage
[] =
125 "Usage: makedep [options] directories\n"
127 " -R from to Compute the relative path between two directories\n"
128 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
129 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
133 #define __attribute__(x)
136 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
137 static void fatal_perror( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
138 static void output( const char *format
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
139 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
141 /*******************************************************************
144 static void fatal_error( const char *msg
, ... )
147 va_start( valist
, msg
);
150 fprintf( stderr
, "%s:", input_file_name
);
151 if (input_line
) fprintf( stderr
, "%d:", input_line
);
152 fprintf( stderr
, " error: " );
154 else fprintf( stderr
, "makedep: error: " );
155 vfprintf( stderr
, msg
, valist
);
161 /*******************************************************************
164 static void fatal_perror( const char *msg
, ... )
167 va_start( valist
, msg
);
170 fprintf( stderr
, "%s:", input_file_name
);
171 if (input_line
) fprintf( stderr
, "%d:", input_line
);
172 fprintf( stderr
, " error: " );
174 else fprintf( stderr
, "makedep: error: " );
175 vfprintf( stderr
, msg
, valist
);
182 /*******************************************************************
185 static void cleanup_files(void)
187 if (temp_file_name
) unlink( temp_file_name
);
188 if (output_file_name
) unlink( output_file_name
);
192 /*******************************************************************
195 static void exit_on_signal( int sig
)
197 exit( 1 ); /* this will call the atexit functions */
201 /*******************************************************************
204 static void *xmalloc( size_t size
)
207 if (!(res
= malloc (size
? size
: 1)))
208 fatal_error( "Virtual memory exhausted.\n" );
213 /*******************************************************************
216 static void *xrealloc (void *ptr
, size_t size
)
220 if (!(res
= realloc( ptr
, size
)))
221 fatal_error( "Virtual memory exhausted.\n" );
225 /*******************************************************************
228 static char *xstrdup( const char *str
)
230 char *res
= strdup( str
);
231 if (!res
) fatal_error( "Virtual memory exhausted.\n" );
236 /*******************************************************************
239 static char *strmake( const char* fmt
, ... )
247 char *p
= xmalloc (size
);
249 n
= vsnprintf (p
, size
, fmt
, ap
);
251 if (n
== -1) size
*= 2;
252 else if ((size_t)n
>= size
) size
= n
+ 1;
259 /*******************************************************************
262 static int strendswith( const char* str
, const char* end
)
267 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
271 /*******************************************************************
274 static void output( const char *format
, ... )
279 va_start( valist
, format
);
280 ret
= vfprintf( output_file
, format
, valist
);
282 if (ret
< 0) fatal_perror( "output" );
283 if (format
[0] && format
[strlen(format
) - 1] == '\n') output_column
= 0;
284 else output_column
+= ret
;
288 /*******************************************************************
291 static void strarray_add( struct strarray
*array
, const char *str
)
293 if (array
->count
== array
->size
)
295 if (array
->size
) array
->size
*= 2;
296 else array
->size
= 16;
297 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
299 array
->str
[array
->count
++] = str
;
303 /*******************************************************************
306 static void strarray_addall( struct strarray
*array
, struct strarray added
)
310 for (i
= 0; i
< added
.count
; i
++) strarray_add( array
, added
.str
[i
] );
314 /*******************************************************************
317 static void strarray_add_uniq( struct strarray
*array
, const char *str
)
321 for (i
= 0; i
< array
->count
; i
++) if (!strcmp( array
->str
[i
], str
)) return;
322 strarray_add( array
, str
);
326 /*******************************************************************
329 static void output_filename( const char *name
)
331 if (output_column
+ strlen(name
) + 1 > 100)
336 else if (output_column
) output( " " );
337 output( "%s", name
);
341 /*******************************************************************
344 static void output_filenames( struct strarray array
)
348 for (i
= 0; i
< array
.count
; i
++) output_filename( array
.str
[i
] );
352 /*******************************************************************
355 static char *get_extension( char *filename
)
357 char *ext
= strrchr( filename
, '.' );
358 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
363 /*******************************************************************
366 static char *replace_extension( const char *name
, const char *old_ext
, const char *new_ext
)
369 int name_len
= strlen( name
);
370 int ext_len
= strlen( old_ext
);
372 if (name_len
>= ext_len
&& !strcmp( name
+ name_len
- ext_len
, old_ext
)) name_len
-= ext_len
;
373 ret
= xmalloc( name_len
+ strlen( new_ext
) + 1 );
374 memcpy( ret
, name
, name_len
);
375 strcpy( ret
+ name_len
, new_ext
);
380 /*******************************************************************
381 * strarray_replace_extension
383 static struct strarray
strarray_replace_extension( const struct strarray
*array
,
384 const char *old_ext
, const char *new_ext
)
389 ret
.count
= ret
.size
= array
->count
;
390 ret
.str
= xmalloc( sizeof(ret
.str
[0]) * ret
.size
);
391 for (i
= 0; i
< array
->count
; i
++) ret
.str
[i
] = replace_extension( array
->str
[i
], old_ext
, new_ext
);
396 /*******************************************************************
399 static char *replace_substr( const char *str
, const char *start
, unsigned int len
, const char *replace
)
401 unsigned int pos
= start
- str
;
402 char *ret
= xmalloc( pos
+ strlen(replace
) + strlen(start
+ len
) + 1 );
403 memcpy( ret
, str
, pos
);
404 strcpy( ret
+ pos
, replace
);
405 strcat( ret
+ pos
, start
+ len
);
410 /*******************************************************************
413 * Determine where the destination path is located relative to the 'from' path.
415 static char *get_relative_path( const char *from
, const char *dest
)
419 unsigned int dotdots
= 0;
421 /* a path of "." is equivalent to an empty path */
422 if (!strcmp( from
, "." )) from
= "";
426 while (*from
== '/') from
++;
427 while (*dest
== '/') dest
++;
428 start
= dest
; /* save start of next path element */
431 while (*from
&& *from
!= '/' && *from
== *dest
) { from
++; dest
++; }
432 if ((!*from
|| *from
== '/') && (!*dest
|| *dest
== '/')) continue;
434 /* count remaining elements in 'from' */
438 while (*from
&& *from
!= '/') from
++;
439 while (*from
== '/') from
++;
445 if (!start
[0] && !dotdots
) return NULL
; /* empty path */
447 ret
= xmalloc( 3 * dotdots
+ strlen( start
) + 1 );
448 for (p
= ret
; dotdots
; dotdots
--, p
+= 3) memcpy( p
, "../", 3 );
450 if (start
[0]) strcpy( p
, start
);
451 else p
[-1] = 0; /* remove trailing slash */
456 /*******************************************************************
459 static char *concat_paths( const char *base
, const char *path
)
461 if (!base
) return xstrdup( path
[0] ? path
: "." );
462 if (path
[0] == '/') return xstrdup( path
);
463 if (!path
[0]) return xstrdup( base
);
464 return strmake( "%s/%s", base
, path
);
468 /*******************************************************************
471 static char *base_dir_path( const char *path
)
473 return concat_paths( base_dir
, path
);
477 /*******************************************************************
480 static char *src_dir_path( const char *path
)
482 return concat_paths( src_dir
, path
);
486 /*******************************************************************
489 static char *top_obj_dir_path( const char *path
)
491 return concat_paths( top_obj_dir
, path
);
495 /*******************************************************************
498 static char *top_dir_path( const char *path
)
500 if (top_src_dir
) return concat_paths( top_src_dir
, path
);
501 return top_obj_dir_path( path
);
505 /*******************************************************************
508 static char *tools_dir_path( const char *path
)
510 if (tools_dir
) return strmake( "%s/tools/%s", tools_dir
, path
);
511 if (top_obj_dir
) return strmake( "%s/tools/%s", top_obj_dir
, path
);
512 return strmake( "tools/%s", path
);
516 /*******************************************************************
519 static char *tools_path( const char *name
)
521 if (tools_dir
) return strmake( "%s/tools/%s/%s%s", tools_dir
, name
, name
, tools_ext
);
522 if (top_obj_dir
) return strmake( "%s/tools/%s/%s%s", top_obj_dir
, name
, name
, tools_ext
);
523 return strmake( "tools/%s/%s%s", name
, name
, tools_ext
);
527 /*******************************************************************
530 static char *get_line( FILE *file
)
533 static unsigned int size
;
538 buffer
= xmalloc( size
);
540 if (!fgets( buffer
, size
, file
)) return NULL
;
545 char *p
= buffer
+ strlen(buffer
);
546 /* if line is larger than buffer, resize buffer */
547 while (p
== buffer
+ size
- 1 && p
[-1] != '\n')
549 buffer
= xrealloc( buffer
, size
* 2 );
550 if (!fgets( buffer
+ size
- 1, size
+ 1, file
)) break;
551 p
= buffer
+ strlen(buffer
);
554 if (p
> buffer
&& p
[-1] == '\n')
557 if (p
> buffer
&& p
[-1] == '\r') *(--p
) = 0;
558 if (p
> buffer
&& p
[-1] == '\\')
561 /* line ends in backslash, read continuation line */
562 if (!fgets( p
, size
- (p
- buffer
), file
)) return buffer
;
571 /*******************************************************************
574 static struct incl_file
*find_src_file( const char *name
)
576 struct incl_file
*file
;
578 LIST_FOR_EACH_ENTRY( file
, &sources
, struct incl_file
, entry
)
579 if (!strcmp( name
, file
->name
)) return file
;
583 /*******************************************************************
586 static struct incl_file
*find_include_file( const char *name
)
588 struct incl_file
*file
;
590 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
)
591 if (!strcmp( name
, file
->name
)) return file
;
595 /*******************************************************************
598 * Add an include file if it doesn't already exists.
600 static struct incl_file
*add_include( struct incl_file
*parent
, const char *name
, int system
)
602 struct incl_file
*include
;
605 if (parent
->files_count
>= parent
->files_size
)
607 parent
->files_size
*= 2;
608 if (parent
->files_size
< 16) parent
->files_size
= 16;
609 parent
->files
= xrealloc( parent
->files
, parent
->files_size
* sizeof(*parent
->files
) );
612 /* enforce some rules for the Wine tree */
614 if (!memcmp( name
, "../", 3 ))
615 fatal_error( "#include directive with relative path not allowed\n" );
617 if (!strcmp( name
, "config.h" ))
619 if ((ext
= strrchr( parent
->filename
, '.' )) && !strcmp( ext
, ".h" ))
620 fatal_error( "config.h must not be included by a header file\n" );
621 if (parent
->files_count
)
622 fatal_error( "config.h must be included before anything else\n" );
624 else if (!strcmp( name
, "wine/port.h" ))
626 if ((ext
= strrchr( parent
->filename
, '.' )) && !strcmp( ext
, ".h" ))
627 fatal_error( "wine/port.h must not be included by a header file\n" );
628 if (!parent
->files_count
) fatal_error( "config.h must be included before wine/port.h\n" );
629 if (parent
->files_count
> 1)
630 fatal_error( "wine/port.h must be included before everything except config.h\n" );
631 if (strcmp( parent
->files
[0]->name
, "config.h" ))
632 fatal_error( "config.h must be included before wine/port.h\n" );
635 LIST_FOR_EACH_ENTRY( include
, &includes
, struct incl_file
, entry
)
636 if (!strcmp( name
, include
->name
)) goto found
;
638 include
= xmalloc( sizeof(*include
) );
639 memset( include
, 0, sizeof(*include
) );
640 include
->name
= xstrdup(name
);
641 include
->included_by
= parent
;
642 include
->included_line
= input_line
;
643 if (system
) include
->flags
|= FLAG_SYSTEM
;
644 list_add_tail( &includes
, &include
->entry
);
646 parent
->files
[parent
->files_count
++] = include
;
651 /*******************************************************************
652 * add_generated_source
654 * Add a generated source file to the list.
656 static struct incl_file
*add_generated_source( const char *name
, const char *filename
)
658 struct incl_file
*file
;
660 if ((file
= find_src_file( name
))) return file
; /* we already have it */
661 file
= xmalloc( sizeof(*file
) );
662 memset( file
, 0, sizeof(*file
) );
663 file
->name
= xstrdup( name
);
664 file
->filename
= xstrdup( filename
? filename
: name
);
665 file
->flags
= FLAG_GENERATED
;
666 list_add_tail( &sources
, &file
->entry
);
671 /*******************************************************************
674 static FILE *open_file( const char *path
)
676 return fopen( base_dir_path( path
), "r" );
679 /*******************************************************************
682 static FILE *open_src_file( struct incl_file
*pFile
)
686 /* try in source dir */
687 pFile
->filename
= src_dir_path( pFile
->name
);
688 file
= open_file( pFile
->filename
);
690 /* now try parent dir */
691 if (!file
&& parent_dir
)
693 pFile
->filename
= src_dir_path( strmake( "%s/%s", parent_dir
, pFile
->name
));
694 file
= open_file( pFile
->filename
);
696 if (!file
) fatal_perror( "open %s", pFile
->name
);
701 /*******************************************************************
704 static FILE *open_include_file( struct incl_file
*pFile
)
712 /* check for generated bison header */
714 if (strendswith( pFile
->name
, ".tab.h" ))
716 filename
= src_dir_path( replace_extension( pFile
->name
, ".tab.h", ".y" ));
717 if ((file
= open_file( filename
)))
719 pFile
->sourcename
= filename
;
720 pFile
->filename
= xstrdup( pFile
->name
);
721 /* don't bother to parse it */
728 /* check for corresponding idl file in source dir */
730 if (strendswith( pFile
->name
, ".h" ))
732 filename
= src_dir_path( replace_extension( pFile
->name
, ".h", ".idl" ));
733 if ((file
= open_file( filename
)))
735 pFile
->sourcename
= filename
;
736 pFile
->filename
= xstrdup( pFile
->name
);
742 /* now try in source dir */
743 filename
= src_dir_path( pFile
->name
);
744 if ((file
= open_file( filename
))) goto found
;
747 /* now try in parent source dir */
750 filename
= src_dir_path( strmake( "%s/%s", parent_dir
, pFile
->name
));
751 if ((file
= open_file( filename
))) goto found
;
755 /* check for corresponding idl file in global includes */
757 if (strendswith( pFile
->name
, ".h" ))
759 filename
= top_dir_path( strmake( "include/%s", replace_extension( pFile
->name
, ".h", ".idl" )));
760 if ((file
= open_file( filename
)))
762 pFile
->sourcename
= filename
;
763 pFile
->filename
= top_obj_dir_path( strmake( "include/%s", pFile
->name
));
769 /* check for corresponding .in file in global includes (for config.h.in) */
771 if (strendswith( pFile
->name
, ".h" ))
773 filename
= top_dir_path( strmake( "include/%s", replace_extension( pFile
->name
, ".h", ".h.in" )));
774 if ((file
= open_file( filename
)))
776 pFile
->sourcename
= filename
;
777 pFile
->filename
= top_obj_dir_path( strmake( "include/%s", pFile
->name
));
783 /* check for corresponding .x file in global includes */
785 if (strendswith( pFile
->name
, "tmpl.h" ))
787 filename
= top_dir_path( strmake( "include/%s", replace_extension( pFile
->name
, ".h", ".x" )));
788 if ((file
= open_file( filename
)))
790 pFile
->sourcename
= filename
;
791 pFile
->filename
= top_obj_dir_path( strmake( "include/%s", pFile
->name
));
797 /* check in global includes source dir */
799 filename
= top_dir_path( strmake( "include/%s", pFile
->name
));
800 if ((file
= open_file( filename
))) goto found
;
802 /* check in global msvcrt includes */
805 filename
= top_dir_path( strmake( "include/msvcrt/%s", pFile
->name
));
806 if ((file
= open_file( filename
))) goto found
;
809 /* now search in include paths */
810 for (i
= 0; i
< include_args
.count
; i
++)
812 const char *dir
= include_args
.str
[i
] + 2; /* skip -I */
815 /* ignore absolute paths that don't point into the source dir */
816 if (!top_src_dir
) continue;
817 len
= strlen( top_src_dir
);
818 if (strncmp( dir
, top_src_dir
, len
)) continue;
819 if (dir
[len
] && dir
[len
] != '/') continue;
821 filename
= strmake( "%s/%s", dir
, pFile
->name
);
822 if ((file
= open_file( filename
))) goto found
;
825 if (pFile
->flags
& FLAG_SYSTEM
) return NULL
; /* ignore system files we cannot find */
827 /* try in src file directory */
828 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
830 int l
= p
- pFile
->included_by
->filename
+ 1;
831 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
832 memcpy( filename
, pFile
->included_by
->filename
, l
);
833 strcpy( filename
+ l
, pFile
->name
);
834 if ((file
= open_file( filename
))) goto found
;
838 fprintf( stderr
, "%s:%d: error: ", pFile
->included_by
->filename
, pFile
->included_line
);
839 perror( pFile
->name
);
840 pFile
= pFile
->included_by
;
841 while (pFile
&& pFile
->included_by
)
843 const char *parent
= pFile
->included_by
->sourcename
;
844 if (!parent
) parent
= pFile
->included_by
->name
;
845 fprintf( stderr
, "%s:%d: note: %s was first included here\n",
846 parent
, pFile
->included_line
, pFile
->name
);
847 pFile
= pFile
->included_by
;
852 pFile
->filename
= filename
;
857 /*******************************************************************
858 * parse_include_directive
860 static void parse_include_directive( struct incl_file
*source
, char *str
)
862 char quote
, *include
, *p
= str
;
864 while (*p
&& isspace(*p
)) p
++;
865 if (*p
!= '\"' && *p
!= '<' ) return;
867 if (quote
== '<') quote
= '>';
869 while (*p
&& (*p
!= quote
)) p
++;
870 if (!*p
) fatal_error( "malformed include directive '%s'\n", str
);
872 add_include( source
, include
, (quote
== '>') );
876 /*******************************************************************
877 * parse_pragma_directive
879 static void parse_pragma_directive( struct incl_file
*source
, char *str
)
881 char *flag
, *p
= str
;
883 if (!isspace( *p
)) return;
884 while (*p
&& isspace(*p
)) p
++;
885 p
= strtok( p
, " \t" );
886 if (strcmp( p
, "makedep" )) return;
888 while ((flag
= strtok( NULL
, " \t" )))
890 if (!strcmp( flag
, "depend" ))
892 while ((p
= strtok( NULL
, " \t" ))) add_include( source
, p
, 0 );
895 else if (!strcmp( flag
, "install" )) source
->flags
|= FLAG_INSTALL
;
897 if (strendswith( source
->name
, ".idl" ))
899 if (!strcmp( flag
, "header" )) source
->flags
|= FLAG_IDL_HEADER
;
900 else if (!strcmp( flag
, "proxy" )) source
->flags
|= FLAG_IDL_PROXY
;
901 else if (!strcmp( flag
, "client" )) source
->flags
|= FLAG_IDL_CLIENT
;
902 else if (!strcmp( flag
, "server" )) source
->flags
|= FLAG_IDL_SERVER
;
903 else if (!strcmp( flag
, "ident" )) source
->flags
|= FLAG_IDL_IDENT
;
904 else if (!strcmp( flag
, "typelib" )) source
->flags
|= FLAG_IDL_TYPELIB
;
905 else if (!strcmp( flag
, "register" )) source
->flags
|= FLAG_IDL_REGISTER
;
906 else if (!strcmp( flag
, "regtypelib" )) source
->flags
|= FLAG_IDL_REGTYPELIB
;
908 else if (strendswith( source
->name
, ".rc" ))
910 if (!strcmp( flag
, "po" )) source
->flags
|= FLAG_RC_PO
;
912 else if (strendswith( source
->name
, ".sfd" ))
914 if (!strcmp( flag
, "font" ))
916 struct strarray
*array
= source
->args
;
920 source
->args
= array
= xmalloc( sizeof(*array
) );
921 *array
= empty_strarray
;
922 source
->flags
|= FLAG_SFD_FONTS
;
924 strarray_add( array
, xstrdup( strtok( NULL
, "" )));
928 else if (!strcmp( flag
, "implib" )) source
->flags
|= FLAG_C_IMPLIB
;
933 /*******************************************************************
934 * parse_cpp_directive
936 static void parse_cpp_directive( struct incl_file
*source
, char *str
)
938 while (*str
&& isspace(*str
)) str
++;
939 if (*str
++ != '#') return;
940 while (*str
&& isspace(*str
)) str
++;
942 if (!strncmp( str
, "include", 7 ))
943 parse_include_directive( source
, str
+ 7 );
944 else if (!strncmp( str
, "import", 6 ) && strendswith( source
->name
, ".m" ))
945 parse_include_directive( source
, str
+ 6 );
946 else if (!strncmp( str
, "pragma", 6 ))
947 parse_pragma_directive( source
, str
+ 6 );
951 /*******************************************************************
954 * If for_h_file is non-zero, it means we are not interested in the idl file
955 * itself, but only in the contents of the .h file that will be generated from it.
957 static void parse_idl_file( struct incl_file
*pFile
, FILE *file
, int for_h_file
)
959 char *buffer
, *include
;
964 /* generated .h file always includes these */
965 add_include( pFile
, "rpc.h", 1 );
966 add_include( pFile
, "rpcndr.h", 1 );
969 while ((buffer
= get_line( file
)))
973 while (*p
&& isspace(*p
)) p
++;
975 if (!strncmp( p
, "import", 6 ))
978 while (*p
&& isspace(*p
)) p
++;
979 if (*p
!= '"') continue;
981 while (*p
&& (*p
!= '"')) p
++;
982 if (!*p
) fatal_error( "malformed import directive\n" );
984 if (for_h_file
&& strendswith( include
, ".idl" )) strcpy( p
- 4, ".h" );
985 add_include( pFile
, include
, 0 );
989 if (for_h_file
) /* only check for #include inside cpp_quote */
991 if (strncmp( p
, "cpp_quote", 9 )) continue;
993 while (*p
&& isspace(*p
)) p
++;
994 if (*p
++ != '(') continue;
995 while (*p
&& isspace(*p
)) p
++;
996 if (*p
++ != '"') continue;
997 if (*p
++ != '#') continue;
998 while (*p
&& isspace(*p
)) p
++;
999 if (strncmp( p
, "include", 7 )) continue;
1001 while (*p
&& isspace(*p
)) p
++;
1002 if (*p
== '\\' && p
[1] == '"')
1009 if (*p
++ != '<' ) continue;
1013 while (*p
&& (*p
!= quote
)) p
++;
1014 if (!*p
|| (quote
== '"' && p
[-1] != '\\'))
1015 fatal_error( "malformed #include directive inside cpp_quote\n" );
1016 if (quote
== '"') p
--; /* remove backslash */
1018 add_include( pFile
, include
, (quote
== '>') );
1022 parse_cpp_directive( pFile
, p
);
1026 /*******************************************************************
1029 static void parse_c_file( struct incl_file
*pFile
, FILE *file
)
1034 while ((buffer
= get_line( file
)))
1036 parse_cpp_directive( pFile
, buffer
);
1041 /*******************************************************************
1044 static void parse_rc_file( struct incl_file
*pFile
, FILE *file
)
1046 char *buffer
, *include
;
1049 while ((buffer
= get_line( file
)))
1053 while (*p
&& isspace(*p
)) p
++;
1055 if (p
[0] == '/' && p
[1] == '*') /* check for magic makedep comment */
1058 while (*p
&& isspace(*p
)) p
++;
1059 if (strncmp( p
, "@makedep:", 9 )) continue;
1061 while (*p
&& isspace(*p
)) p
++;
1066 while (*p
&& *p
!= quote
) p
++;
1071 while (*p
&& !isspace(*p
) && *p
!= '*') p
++;
1074 fatal_error( "malformed makedep comment\n" );
1076 add_include( pFile
, include
, (quote
== '>') );
1080 parse_cpp_directive( pFile
, buffer
);
1085 /*******************************************************************
1088 static void parse_in_file( struct incl_file
*source
, FILE *file
)
1092 /* make sure it gets rebuilt when the version changes */
1093 add_include( source
, "config.h", 1 );
1095 if (!strendswith( source
->filename
, ".man.in" )) return; /* not a man page */
1098 while ((buffer
= get_line( file
)))
1100 if (strncmp( buffer
, ".TH", 3 )) continue;
1101 if (!(p
= strtok( buffer
, " \t" ))) continue; /* .TH */
1102 if (!(p
= strtok( NULL
, " \t" ))) continue; /* program name */
1103 if (!(p
= strtok( NULL
, " \t" ))) continue; /* man section */
1104 source
->args
= xstrdup( p
);
1110 /*******************************************************************
1113 static void parse_sfd_file( struct incl_file
*source
, FILE *file
)
1115 char *p
, *eol
, *buffer
;
1118 while ((buffer
= get_line( file
)))
1120 if (strncmp( buffer
, "UComments:", 10 )) continue;
1122 while (*p
== ' ') p
++;
1123 if (p
[0] == '"' && p
[1] && buffer
[strlen(buffer
) - 1] == '"')
1126 buffer
[strlen(buffer
) - 1] = 0;
1128 while ((eol
= strstr( p
, "+AAoA" )))
1131 while (*p
&& isspace(*p
)) p
++;
1134 while (*p
&& isspace(*p
)) p
++;
1135 if (!strncmp( p
, "pragma", 6 )) parse_pragma_directive( source
, p
+ 6 );
1139 while (*p
&& isspace(*p
)) p
++;
1140 if (*p
++ != '#') return;
1141 while (*p
&& isspace(*p
)) p
++;
1142 if (!strncmp( p
, "pragma", 6 )) parse_pragma_directive( source
, p
+ 6 );
1148 /*******************************************************************
1151 static void parse_file( struct incl_file
*source
, int src
)
1155 /* don't try to open certain types of files */
1156 if (strendswith( source
->name
, ".tlb" ))
1158 source
->filename
= xstrdup( source
->name
);
1162 file
= src
? open_src_file( source
) : open_include_file( source
);
1164 input_file_name
= source
->filename
;
1166 if (source
->sourcename
&& strendswith( source
->sourcename
, ".idl" ))
1167 parse_idl_file( source
, file
, 1 );
1168 else if (strendswith( source
->filename
, ".idl" ))
1169 parse_idl_file( source
, file
, 0 );
1170 else if (strendswith( source
->filename
, ".c" ) ||
1171 strendswith( source
->filename
, ".m" ) ||
1172 strendswith( source
->filename
, ".h" ) ||
1173 strendswith( source
->filename
, ".l" ) ||
1174 strendswith( source
->filename
, ".y" ))
1175 parse_c_file( source
, file
);
1176 else if (strendswith( source
->filename
, ".rc" ))
1177 parse_rc_file( source
, file
);
1178 else if (strendswith( source
->filename
, ".in" ))
1179 parse_in_file( source
, file
);
1180 else if (strendswith( source
->filename
, ".sfd" ))
1181 parse_sfd_file( source
, file
);
1183 input_file_name
= NULL
;
1187 /*******************************************************************
1190 * Add a source file to the list.
1192 static struct incl_file
*add_src_file( const char *name
)
1194 struct incl_file
*file
;
1196 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1197 file
= xmalloc( sizeof(*file
) );
1198 memset( file
, 0, sizeof(*file
) );
1199 file
->name
= xstrdup(name
);
1200 list_add_tail( &sources
, &file
->entry
);
1201 parse_file( file
, 1 );
1206 /*******************************************************************
1209 static char *get_make_variable( const char *name
)
1213 for (i
= 0; i
< cmdline_vars
.count
; i
+= 2)
1214 if (!strcmp( cmdline_vars
.str
[i
], name
))
1215 return xstrdup( cmdline_vars
.str
[i
+ 1] );
1217 for (i
= 0; i
< make_vars
.count
; i
+= 2)
1218 if (!strcmp( make_vars
.str
[i
], name
))
1219 return xstrdup( make_vars
.str
[i
+ 1] );
1224 /*******************************************************************
1225 * get_expanded_make_variable
1227 static char *get_expanded_make_variable( const char *name
)
1229 char *p
, *end
, *var
, *expand
, *tmp
;
1231 expand
= get_make_variable( name
);
1232 if (!expand
) return NULL
;
1235 while ((p
= strchr( p
, '$' )))
1239 if (!(end
= strchr( p
+ 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand
);
1241 if (strchr( p
+ 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p
+ 2 );
1242 var
= get_make_variable( p
+ 2 );
1243 tmp
= replace_substr( expand
, p
, end
- p
, var
? var
: "" );
1246 else if (p
[1] == '{') /* don't expand ${} variables */
1248 if (!(end
= strchr( p
+ 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand
);
1252 else if (p
[1] == '$')
1254 tmp
= replace_substr( expand
, p
, 2, "$" );
1256 else fatal_error( "syntax error in '%s'\n", expand
);
1258 /* switch to the new string */
1259 p
= tmp
+ (p
- expand
);
1264 /* consider empty variables undefined */
1266 while (*p
&& isspace(*p
)) p
++;
1267 if (*p
) return expand
;
1273 /*******************************************************************
1274 * get_expanded_make_var_array
1276 static struct strarray
get_expanded_make_var_array( const char *name
)
1278 struct strarray ret
= empty_strarray
;
1279 char *value
, *token
;
1281 if ((value
= get_expanded_make_variable( name
)))
1282 for (token
= strtok( value
, " \t" ); token
; token
= strtok( NULL
, " \t" ))
1283 strarray_add( &ret
, token
);
1288 /*******************************************************************
1291 static int set_make_variable( struct strarray
*array
, const char *assignment
)
1296 p
= name
= xstrdup( assignment
);
1297 while (isalnum(*p
) || *p
== '_') p
++;
1298 if (name
== p
) return 0; /* not a variable */
1302 while (isspace(*p
)) p
++;
1304 if (*p
!= '=') return 0; /* not an assignment */
1306 while (isspace(*p
)) p
++;
1308 /* redefining a variable replaces the previous value */
1309 for (i
= 0; i
< array
->count
; i
+= 2)
1311 if (strcmp( array
->str
[i
], name
)) continue;
1312 array
->str
[i
+ 1] = p
;
1315 strarray_add( array
, name
);
1316 strarray_add( array
, p
);
1321 /*******************************************************************
1324 static void parse_makefile(void)
1329 input_file_name
= base_dir_path( makefile_name
);
1330 if (!(file
= fopen( input_file_name
, "r" )))
1332 fatal_perror( "open" );
1337 while ((buffer
= get_line( file
)))
1339 if (Separator
&& !strncmp( buffer
, Separator
, strlen(Separator
) )) break;
1340 if (*buffer
== '\t') continue; /* command */
1341 while (isspace( *buffer
)) buffer
++;
1342 if (*buffer
== '#') continue; /* comment */
1343 set_make_variable( &make_vars
, buffer
);
1346 input_file_name
= NULL
;
1350 /*******************************************************************
1351 * add_generated_sources
1353 static void add_generated_sources(void)
1355 struct incl_file
*source
, *next
, *file
;
1357 LIST_FOR_EACH_ENTRY_SAFE( source
, next
, &sources
, struct incl_file
, entry
)
1359 if (source
->flags
& FLAG_IDL_CLIENT
)
1361 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_c.c" ), NULL
);
1362 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1364 if (source
->flags
& FLAG_IDL_SERVER
)
1366 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_s.c" ), NULL
);
1367 add_include( file
, "wine/exception.h", 0 );
1368 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1370 if (source
->flags
& FLAG_IDL_IDENT
)
1372 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_i.c" ), NULL
);
1373 add_include( file
, "rpc.h", 0 );
1374 add_include( file
, "rpcndr.h", 0 );
1375 add_include( file
, "guiddef.h", 0 );
1377 if (source
->flags
& FLAG_IDL_PROXY
)
1379 file
= add_generated_source( "dlldata.o", "dlldata.c" );
1380 add_include( file
, "objbase.h", 0 );
1381 add_include( file
, "rpcproxy.h", 0 );
1382 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_p.c" ), NULL
);
1383 add_include( file
, "objbase.h", 0 );
1384 add_include( file
, "rpcproxy.h", 0 );
1385 add_include( file
, "wine/exception.h", 0 );
1386 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1388 if (source
->flags
& FLAG_IDL_REGTYPELIB
)
1390 add_generated_source( replace_extension( source
->name
, ".idl", "_t.res" ), NULL
);
1392 if (source
->flags
& FLAG_IDL_REGISTER
)
1394 add_generated_source( replace_extension( source
->name
, ".idl", "_r.res" ), NULL
);
1396 if (strendswith( source
->name
, ".y" ))
1398 file
= add_generated_source( replace_extension( source
->name
, ".y", ".tab.c" ), NULL
);
1399 /* steal the includes list from the source file */
1400 file
->files_count
= source
->files_count
;
1401 file
->files_size
= source
->files_size
;
1402 file
->files
= source
->files
;
1403 source
->files_count
= source
->files_size
= 0;
1404 source
->files
= NULL
;
1406 if (strendswith( source
->name
, ".l" ))
1408 file
= add_generated_source( replace_extension( source
->name
, ".l", ".yy.c" ), NULL
);
1409 /* steal the includes list from the source file */
1410 file
->files_count
= source
->files_count
;
1411 file
->files_size
= source
->files_size
;
1412 file
->files
= source
->files
;
1413 source
->files_count
= source
->files_size
= 0;
1414 source
->files
= NULL
;
1417 if (get_make_variable( "TESTDLL" ))
1419 file
= add_generated_source( "testlist.o", "testlist.c" );
1420 add_include( file
, "wine/test.h", 0 );
1425 /*******************************************************************
1428 static void create_dir( const char *dir
)
1432 p
= path
= xstrdup( dir
);
1433 while ((p
= strchr( p
, '/' )))
1436 if (mkdir( path
, 0755 ) == -1 && errno
!= EEXIST
) fatal_perror( "mkdir %s", path
);
1438 while (*p
== '/') p
++;
1440 if (mkdir( path
, 0755 ) == -1 && errno
!= EEXIST
) fatal_perror( "mkdir %s", path
);
1445 /*******************************************************************
1448 static void output_include( struct incl_file
*pFile
, struct incl_file
*owner
)
1452 if (pFile
->owner
== owner
) return;
1453 if (!pFile
->filename
) return;
1454 pFile
->owner
= owner
;
1455 output_filename( pFile
->filename
);
1456 for (i
= 0; i
< pFile
->files_count
; i
++) output_include( pFile
->files
[i
], owner
);
1460 /*******************************************************************
1463 static struct strarray
output_sources(void)
1465 struct incl_file
*source
;
1468 const char *dllext
= ".so";
1469 struct strarray object_files
= empty_strarray
;
1470 struct strarray crossobj_files
= empty_strarray
;
1471 struct strarray res_files
= empty_strarray
;
1472 struct strarray clean_files
= empty_strarray
;
1473 struct strarray po_files
= empty_strarray
;
1474 struct strarray mo_files
= empty_strarray
;
1475 struct strarray mc_files
= empty_strarray
;
1476 struct strarray ok_files
= empty_strarray
;
1477 struct strarray dlldata_files
= empty_strarray
;
1478 struct strarray c2man_files
= empty_strarray
;
1479 struct strarray implib_objs
= empty_strarray
;
1480 struct strarray includes
= empty_strarray
;
1481 struct strarray subdirs
= empty_strarray
;
1482 struct strarray phony_targets
= empty_strarray
;
1483 struct strarray linguas
= get_expanded_make_var_array( "LINGUAS" );
1484 struct strarray all_targets
= get_expanded_make_var_array( "PROGRAMS" );
1485 struct strarray targetflags
= get_expanded_make_var_array( "TARGETFLAGS" );
1486 struct strarray delayimports
= get_expanded_make_var_array( "DELAYIMPORTS" );
1487 struct strarray extradllflags
= get_expanded_make_var_array( "EXTRADLLFLAGS" );
1488 char *module
= get_expanded_make_variable( "MODULE" );
1489 char *exeext
= get_expanded_make_variable( "EXEEXT" );
1490 char *testdll
= get_expanded_make_variable( "TESTDLL" );
1491 char *staticlib
= get_expanded_make_variable( "STATICLIB" );
1492 char *crosstarget
= get_expanded_make_variable( "CROSSTARGET" );
1494 if (exeext
&& !strcmp( exeext
, ".exe" )) dllext
= "";
1495 if (module
&& strendswith( module
, ".a" )) staticlib
= module
;
1496 for (i
= 0; i
< extradllflags
.count
; i
++) if (!strcmp( extradllflags
.str
[i
], "-m16" )) is_win16
= 1;
1498 for (i
= 0; i
< linguas
.count
; i
++)
1499 strarray_add( &mo_files
, strmake( "%s/%s.mo", top_obj_dir_path( "po" ), linguas
.str
[i
] ));
1501 strarray_add( &includes
, "-I." );
1502 if (src_dir
) strarray_add( &includes
, strmake( "-I%s", src_dir
));
1503 if (parent_dir
) strarray_add( &includes
, strmake( "-I%s", src_dir_path( parent_dir
)));
1504 if (top_obj_dir
) strarray_add( &includes
, strmake( "-I%s/include", top_obj_dir
));
1505 if (top_src_dir
) strarray_add( &includes
, strmake( "-I%s/include", top_src_dir
));
1506 if (use_msvcrt
) strarray_add( &includes
, strmake( "-I%s", top_dir_path( "include/msvcrt" )));
1507 strarray_addall( &includes
, include_args
);
1509 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1511 struct strarray extradefs
;
1512 char *obj
= xstrdup( source
->name
);
1513 char *ext
= get_extension( obj
);
1515 if (!ext
) fatal_error( "unsupported file type %s\n", source
->name
);
1518 if (src_dir
&& strchr( obj
, '/' ))
1520 char *subdir
= base_dir_path( obj
);
1521 *strrchr( subdir
, '/' ) = 0;
1522 strarray_add_uniq( &subdirs
, subdir
);
1525 extradefs
= get_expanded_make_var_array( strmake( "%s_EXTRADEFS", obj
));
1527 if (!strcmp( ext
, "y" )) /* yacc file */
1529 /* add source file dependency for parallel makes */
1530 char *header
= strmake( "%s.tab.h", obj
);
1532 if (find_include_file( header
))
1534 output( "%s.tab.h: %s\n", obj
, source
->filename
);
1535 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1536 obj
, obj
, source
->filename
);
1537 output( "%s.tab.c: %s %s\n", obj
, source
->filename
, header
);
1538 strarray_add( &clean_files
, strmake( "%s.tab.h", obj
));
1540 else output( "%s.tab.c: %s\n", obj
, source
->filename
);
1542 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj
, source
->filename
);
1544 continue; /* no dependencies */
1546 else if (!strcmp( ext
, "x" )) /* template file */
1548 output( "%s.h: %s%s %s\n", obj
, tools_dir_path( "make_xftmpl" ), tools_ext
, source
->filename
);
1549 output( "\t%s%s -H -o $@ %s\n", tools_dir_path( "make_xftmpl" ), tools_ext
, source
->filename
);
1550 strarray_add( &clean_files
, strmake( "%s.h", obj
));
1551 continue; /* no dependencies */
1553 else if (!strcmp( ext
, "l" )) /* lex file */
1555 output( "%s.yy.c: %s\n", obj
, source
->filename
);
1556 output( "\t$(FLEX) -o$@ %s\n", source
->filename
);
1557 continue; /* no dependencies */
1559 else if (!strcmp( ext
, "rc" )) /* resource file */
1561 strarray_add( &res_files
, strmake( "%s.res", obj
));
1562 output( "%s.res: %s %s\n", obj
, tools_path( "wrc" ), source
->filename
);
1563 output( "\t%s -o $@ %s", tools_path( "wrc" ), source
->filename
);
1564 if (is_win16
) output_filename( "-m16" );
1565 else output_filenames( targetflags
);
1566 output_filename( "--nostdinc" );
1567 output_filenames( includes
);
1568 output_filenames( define_args
);
1569 output_filenames( extradefs
);
1570 if (mo_files
.count
&& (source
->flags
& FLAG_RC_PO
))
1572 strarray_add( &po_files
, source
->filename
);
1573 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1575 output( "%s.res:", obj
);
1576 output_filenames( mo_files
);
1578 output( "rsrc.pot " );
1580 else output( "\n" );
1581 output( "%s.res:", obj
);
1583 else if (!strcmp( ext
, "mc" )) /* message file */
1585 strarray_add( &res_files
, strmake( "%s.res", obj
));
1586 output( "%s.res: %s %s\n", obj
, tools_path( "wmc" ), source
->filename
);
1587 output( "\t%s -U -O res -o $@ %s", tools_path( "wmc" ), source
->filename
);
1590 strarray_add( &mc_files
, source
->filename
);
1591 output_filename( strmake( "--po-dir=%s", top_obj_dir_path( "po" )));
1593 output( "%s.res:", obj
);
1594 output_filenames( mo_files
);
1596 output( "msg.pot " );
1598 else output( "\n" );
1599 output( "%s.res:", obj
);
1601 else if (!strcmp( ext
, "idl" )) /* IDL file */
1603 struct strarray targets
= empty_strarray
;
1606 if (!source
->flags
|| find_include_file( strmake( "%s.h", obj
)))
1607 source
->flags
|= FLAG_IDL_HEADER
;
1609 for (i
= 0; i
< sizeof(idl_outputs
) / sizeof(idl_outputs
[0]); i
++)
1611 if (!(source
->flags
& idl_outputs
[i
].flag
)) continue;
1612 dest
= strmake( "%s%s", obj
, idl_outputs
[i
].ext
);
1613 if (!find_src_file( dest
)) strarray_add( &clean_files
, dest
);
1614 strarray_add( &targets
, dest
);
1616 if (source
->flags
& FLAG_IDL_PROXY
) strarray_add( &dlldata_files
, source
->name
);
1617 output_filenames( targets
);
1618 output( ": %s\n", tools_path( "widl" ));
1619 output( "\t%s -o $@ %s", tools_path( "widl" ), source
->filename
);
1620 output_filenames( targetflags
);
1621 output_filenames( includes
);
1622 output_filenames( define_args
);
1623 output_filenames( extradefs
);
1624 output_filenames( get_expanded_make_var_array( "EXTRAIDLFLAGS" ));
1626 output_filenames( targets
);
1627 output( ": %s", source
->filename
);
1629 else if (!strcmp( ext
, "in" )) /* .in file or man page */
1631 if (strendswith( obj
, ".man" ) && source
->args
)
1633 char *dir
, *dest
= replace_extension( obj
, ".man", "" );
1634 char *lang
= strchr( dest
, '.' );
1635 char *section
= source
->args
;
1639 dir
= strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang
, section
);
1641 else dir
= strmake( "$(DESTDIR)$(mandir)/man%s", section
);
1642 output( "install-man-pages:: %s\n", obj
);
1643 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n", obj
, dir
, dest
, section
);
1644 output( "uninstall::\n" );
1645 output( "\t$(RM) %s/%s.%s\n", dir
, dest
, section
);
1648 strarray_add( &all_targets
, xstrdup(obj
) );
1649 strarray_add_uniq( &phony_targets
, "install-man-pages" );
1650 strarray_add_uniq( &phony_targets
, "uninstall" );
1652 else strarray_add( &clean_files
, xstrdup(obj
) );
1653 output( "%s: %s\n", obj
, source
->filename
);
1654 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source
->filename
);
1655 output( "%s:", obj
);
1657 else if (!strcmp( ext
, "sfd" )) /* font file */
1659 char *ttf_file
= src_dir_path( strmake( "%s.ttf", obj
));
1660 char *fontforge
= get_expanded_make_variable( "FONTFORGE" );
1661 if (fontforge
&& !src_dir
)
1663 output( "%s: %s\n", ttf_file
, source
->filename
);
1664 output( "\t%s -script %s %s $@\n",
1665 fontforge
, top_dir_path( "fonts/genttf.ff" ), source
->filename
);
1667 if (source
->flags
& FLAG_INSTALL
)
1669 output( "install install-lib::\n" );
1670 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s.ttf\n", ttf_file
, obj
);
1671 output( "uninstall::\n" );
1672 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s.ttf\n", obj
);
1674 if (source
->flags
& FLAG_SFD_FONTS
)
1676 struct strarray
*array
= source
->args
;
1678 for (i
= 0; i
< array
->count
; i
++)
1680 char *font
= strtok( xstrdup(array
->str
[i
]), " \t" );
1681 char *args
= strtok( NULL
, "" );
1683 strarray_add( &all_targets
, font
);
1684 output( "%s: %s %s\n", font
, tools_path( "sfnt2fon" ), ttf_file
);
1685 output( "\t%s -o $@ %s %s\n", tools_path( "sfnt2fon" ), ttf_file
, args
);
1686 output( "install install-lib:: %s\n", font
);
1687 output( "\t$(INSTALL_DATA) %s $(DESTDIR)$(fontdir)/%s\n", font
, font
);
1688 output( "uninstall::\n" );
1689 output( "\t$(RM) $(DESTDIR)$(fontdir)/%s\n", font
);
1690 strarray_add_uniq( &phony_targets
, "install" );
1691 strarray_add_uniq( &phony_targets
, "install-lib" );
1692 strarray_add_uniq( &phony_targets
, "uninstall" );
1695 else output( "all: %s\n", ttf_file
);
1697 continue; /* no dependencies */
1699 else if (!strcmp( ext
, "svg" )) /* svg file */
1701 char *convert
= get_expanded_make_variable( "CONVERT" );
1702 char *rsvg
= get_expanded_make_variable( "RSVG" );
1703 char *icotool
= get_expanded_make_variable( "ICOTOOL" );
1704 if (convert
&& rsvg
&& icotool
&& !src_dir
)
1706 output( "%s.ico %s.bmp: %s\n", obj
, obj
, source
->filename
);
1707 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
1708 convert
, icotool
, rsvg
, top_dir_path( "tools/buildimage" ), source
->filename
);
1713 continue; /* no dependencies */
1715 else if (!strcmp( ext
, "res" ))
1717 strarray_add( &res_files
, source
->name
);
1718 continue; /* no dependencies */
1722 int need_cross
= testdll
|| (source
->flags
& FLAG_C_IMPLIB
) || (module
&& staticlib
);
1724 if ((source
->flags
& FLAG_GENERATED
) && (!testdll
|| strcmp( source
->filename
, "testlist.c" )))
1725 strarray_add( &clean_files
, source
->filename
);
1726 if (source
->flags
& FLAG_C_IMPLIB
) strarray_add( &implib_objs
, strmake( "%s.o", obj
));
1727 strarray_add( &object_files
, strmake( "%s.o", obj
));
1728 output( "%s.o: %s\n", obj
, source
->filename
);
1729 output( "\t$(CC) -c -o $@ %s", source
->filename
);
1730 output_filenames( includes
);
1731 output_filenames( define_args
);
1732 output_filenames( extradefs
);
1733 if (module
|| staticlib
|| testdll
) output_filenames( dllflags
);
1734 output_filenames( get_expanded_make_var_array( "EXTRACFLAGS" ));
1735 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1736 output_filename( "$(CFLAGS)" );
1738 if (crosstarget
&& need_cross
)
1740 strarray_add( &crossobj_files
, strmake( "%s.cross.o", obj
));
1741 output( "%s.cross.o: %s\n", obj
, source
->filename
);
1742 output( "\t$(CROSSCC) -c -o $@ %s", source
->filename
);
1743 output_filenames( includes
);
1744 output_filenames( define_args
);
1745 output_filenames( extradefs
);
1746 output_filename( "-DWINE_CROSSTEST" );
1747 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1748 output_filename( "$(CFLAGS)" );
1751 if (testdll
&& !strcmp( ext
, "c" ) && !(source
->flags
& FLAG_GENERATED
))
1753 strarray_add( &ok_files
, strmake( "%s.ok", obj
));
1754 output( "%s.ok:\n", obj
);
1755 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1756 top_dir_path( "tools/runtest" ), top_obj_dir
,
1757 testdll
, replace_extension( testdll
, ".dll", "_test.exe" ), dllext
, obj
);
1759 if (!strcmp( ext
, "c" ) && !(source
->flags
& FLAG_GENERATED
))
1760 strarray_add( &c2man_files
, source
->filename
);
1761 output( "%s.o", obj
);
1762 if (crosstarget
&& need_cross
) output( " %s.cross.o", obj
);
1767 for (i
= 0; i
< source
->files_count
; i
++) output_include( source
->files
[i
], source
);
1771 /* rules for files that depend on multiple sources */
1775 output( "rsrc.pot: %s", tools_path( "wrc" ) );
1776 output_filenames( po_files
);
1778 output( "\t%s -O pot -o $@", tools_path( "wrc" ));
1779 output_filenames( po_files
);
1780 if (is_win16
) output_filename( "-m16" );
1781 else output_filenames( targetflags
);
1782 output_filename( "--nostdinc" );
1783 output_filenames( includes
);
1784 output_filenames( define_args
);
1786 strarray_add( &clean_files
, "rsrc.pot" );
1791 output( "msg.pot: %s", tools_path( "wmc" ));
1792 output_filenames( mc_files
);
1794 output( "\t%s -O pot -o $@", tools_path( "wmc" ));
1795 output_filenames( mc_files
);
1797 strarray_add( &clean_files
, "msg.pot" );
1800 if (dlldata_files
.count
)
1802 output( "dlldata.c: %s %s\n", tools_path( "widl" ), src_dir_path( "Makefile.in" ));
1803 output( "\t%s --dlldata-only -o $@", tools_path( "widl" ));
1804 output_filenames( dlldata_files
);
1808 if (module
&& !staticlib
)
1810 char *importlib
= get_expanded_make_variable( "IMPORTLIB" );
1811 struct strarray all_libs
= empty_strarray
;
1812 char *spec_file
= NULL
;
1814 if (!appmode
.count
) spec_file
= src_dir_path( replace_extension( module
, ".dll", ".spec" ));
1815 for (i
= 0; i
< delayimports
.count
; i
++)
1816 strarray_add( &all_libs
, strmake( "-l%s", delayimports
.str
[i
] ));
1817 for (i
= 0; i
< imports
.count
; i
++)
1818 strarray_add( &all_libs
, strmake( "-l%s", imports
.str
[i
] ));
1819 for (i
= 0; i
< delayimports
.count
; i
++)
1820 strarray_add( &all_libs
, strmake( "-Wb,-d%s", delayimports
.str
[i
] ));
1821 strarray_add( &all_libs
, "-lwine" );
1822 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBPORT" ));
1823 strarray_addall( &all_libs
, get_expanded_make_var_array( "EXTRALIBS" ));
1824 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBS" ));
1828 strarray_add( &all_targets
, strmake( "%s%s", module
, dllext
));
1829 strarray_add( &all_targets
, strmake( "%s.fake", module
));
1830 output( "%s%s %s.fake:", module
, dllext
, module
);
1834 strarray_add( &all_targets
, module
);
1835 output( "%s:", module
);
1837 if (spec_file
) output_filename( spec_file
);
1838 output_filenames( object_files
);
1839 output_filenames( res_files
);
1841 output( "\t%s -o $@", tools_path( "winegcc" ));
1842 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1843 if (tools_dir
) output_filename( strmake( "--sysroot=%s", top_obj_dir
));
1844 output_filenames( targetflags
);
1845 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1848 output( " -shared %s", spec_file
);
1849 output_filenames( extradllflags
);
1851 else output_filenames( appmode
);
1852 output_filenames( object_files
);
1853 output_filenames( res_files
);
1854 output_filenames( all_libs
);
1855 output_filename( "$(LDFLAGS)" );
1858 if (spec_file
&& importlib
)
1862 strarray_add( &clean_files
, strmake( "lib%s.def", importlib
));
1863 output( "lib%s.def: %s %s\n", importlib
, tools_path( "winebuild" ), spec_file
);
1864 output( "\t%s -w --def -o $@ --export %s", tools_path( "winebuild" ), spec_file
);
1865 output_filenames( targetflags
);
1866 if (is_win16
) output_filename( "-m16" );
1868 if (implib_objs
.count
)
1870 strarray_add( &clean_files
, strmake( "lib%s.def.a", importlib
));
1871 output( "lib%s.def.a:", importlib
);
1872 output_filenames( implib_objs
);
1874 output( "\t$(RM) $@\n" );
1875 output( "\t$(AR) $(ARFLAGS) $@" );
1876 output_filenames( implib_objs
);
1878 output( "\t$(RANLIB) $@\n" );
1883 strarray_add( &clean_files
, strmake( "lib%s.a", importlib
));
1884 output( "lib%s.a: %s %s", importlib
, tools_path( "winebuild" ), spec_file
);
1885 output_filenames( implib_objs
);
1887 output( "\t%s -w --implib -o $@ --export %s", tools_path( "winebuild" ), spec_file
);
1888 output_filenames( targetflags
);
1889 output_filenames( implib_objs
);
1892 if (crosstarget
&& !is_win16
)
1894 struct strarray cross_files
= strarray_replace_extension( &implib_objs
, ".o", ".cross.o" );
1895 strarray_add( &clean_files
, strmake( "lib%s.cross.a", importlib
));
1896 output( "lib%s.cross.a: %s %s", importlib
, tools_path( "winebuild" ), spec_file
);
1897 output_filenames( cross_files
);
1899 output( "\t%s -b %s -w --implib -o $@ --export %s",
1900 tools_path( "winebuild" ), crosstarget
, spec_file
);
1901 output_filenames( cross_files
);
1908 if (c2man_files
.count
)
1910 char *manext
= get_expanded_make_variable( "api_manext" );
1912 output( "manpages::\n" );
1913 output( "\t%s -w %s", top_dir_path( "tools/c2man.pl" ), spec_file
);
1914 output_filename( strmake( "-R%s", top_dir_path( "" )));
1915 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1916 output_filename( strmake( "-o %s/man%s", top_obj_dir_path( "documentation" ), manext
? manext
: "3w" ));
1917 output_filenames( c2man_files
);
1919 output( "htmlpages::\n" );
1920 output( "\t%s -Th -w %s", top_dir_path( "tools/c2man.pl" ), spec_file
);
1921 output_filename( strmake( "-R%s", top_dir_path( "" )));
1922 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1923 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/html" )));
1924 output_filenames( c2man_files
);
1926 output( "sgmlpages::\n" );
1927 output( "\t%s -Ts -w %s", top_dir_path( "tools/c2man.pl" ), spec_file
);
1928 output_filename( strmake( "-R%s", top_dir_path( "" )));
1929 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1930 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/api-guide" )));
1931 output_filenames( c2man_files
);
1933 output( "xmlpages::\n" );
1934 output( "\t%s -Tx -w %s", top_dir_path( "tools/c2man.pl" ), spec_file
);
1935 output_filename( strmake( "-R%s", top_dir_path( "" )));
1936 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1937 output_filename( strmake( "-o %s", top_obj_dir_path( "documentation/api-guide-xml" )));
1938 output_filenames( c2man_files
);
1940 strarray_add( &phony_targets
, "manpages" );
1941 strarray_add( &phony_targets
, "htmlpages" );
1942 strarray_add( &phony_targets
, "sgmlpages" );
1943 strarray_add( &phony_targets
, "xmlpages" );
1945 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1951 strarray_add( &all_targets
, staticlib
);
1952 output( "%s:", staticlib
);
1953 output_filenames( object_files
);
1954 output( "\n\t$(RM) $@\n" );
1955 output( "\t$(AR) $(ARFLAGS) $@" );
1956 output_filenames( object_files
);
1957 output( "\n\t$(RANLIB) $@\n" );
1958 if (crosstarget
&& module
)
1960 char *name
= replace_extension( staticlib
, ".a", ".cross.a" );
1962 strarray_add( &all_targets
, name
);
1963 output( "%s:", name
);
1964 output_filenames( crossobj_files
);
1965 output( "\n\t$(RM) $@\n" );
1966 output( "\t%s-ar $(ARFLAGS) $@", crosstarget
);
1967 output_filenames( crossobj_files
);
1968 output( "\n\t%s-ranlib $@\n", crosstarget
);
1974 char *testmodule
= replace_extension( testdll
, ".dll", "_test.exe" );
1975 char *stripped
= replace_extension( testdll
, ".dll", "_test-stripped.exe" );
1976 struct strarray all_libs
= empty_strarray
;
1978 for (i
= 0; i
< imports
.count
; i
++) strarray_add( &all_libs
, strmake( "-l%s", imports
.str
[i
] ));
1979 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBS" ));
1981 strarray_add( &all_targets
, strmake( "%s%s", testmodule
, dllext
));
1982 strarray_add( &clean_files
, strmake( "%s%s", stripped
, dllext
));
1983 output( "%s%s:\n", testmodule
, dllext
);
1984 output( "\t%s -o $@", tools_path( "winegcc" ));
1985 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1986 if (tools_dir
) output_filename( strmake( "--sysroot=%s", top_obj_dir
));
1987 output_filenames( targetflags
);
1988 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1989 output_filenames( appmode
);
1990 output_filenames( object_files
);
1991 output_filenames( res_files
);
1992 output_filenames( all_libs
);
1993 output_filename( "$(LDFLAGS)" );
1995 output( "%s%s:\n", stripped
, dllext
);
1996 output( "\t%s -o $@", tools_path( "winegcc" ));
1997 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1998 if (tools_dir
) output_filename( strmake( "--sysroot=%s", top_obj_dir
));
1999 output_filenames( targetflags
);
2000 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
2001 output_filename( strmake( "-Wb,-F,%s", testmodule
));
2002 output_filenames( appmode
);
2003 output_filenames( object_files
);
2004 output_filenames( res_files
);
2005 output_filenames( all_libs
);
2006 output_filename( "$(LDFLAGS)" );
2008 output( "%s%s %s%s:", testmodule
, dllext
, stripped
, dllext
);
2009 output_filenames( object_files
);
2010 output_filenames( res_files
);
2015 char *testres
= replace_extension( testdll
, ".dll", "_test.res" );
2016 output( "all: %s/%s\n", top_obj_dir_path( "programs/winetest" ), testres
);
2017 output( "%s/%s: %s%s\n", top_obj_dir_path( "programs/winetest" ), testres
, stripped
, dllext
);
2018 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
2019 testmodule
, stripped
, dllext
, tools_path( "wrc" ));
2024 char *crosstest
= replace_extension( testdll
, ".dll", "_crosstest.exe" );
2026 strarray_add( &clean_files
, crosstest
);
2027 output( "crosstest: %s\n", crosstest
);
2028 output( "%s:", crosstest
);
2029 output_filenames( crossobj_files
);
2030 output_filenames( res_files
);
2032 output( "\t%s -o $@ -b %s", tools_path( "winegcc" ), crosstarget
);
2033 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
2034 if (tools_dir
) output_filename( strmake( "--sysroot=%s", top_obj_dir
));
2035 output_filename( "--lib-suffix=.cross.a" );
2036 output_filenames( crossobj_files
);
2037 output_filenames( res_files
);
2038 output_filenames( all_libs
);
2039 output_filename( "$(LDFLAGS)" );
2041 strarray_add( &phony_targets
, "crosstest" );
2044 output_filenames( ok_files
);
2045 output( ": %s%s ../%s%s\n", testmodule
, dllext
, testdll
, dllext
);
2046 output( "check test:" );
2047 output_filenames( ok_files
);
2049 output( "testclean::\n" );
2050 output( "\t$(RM)" );
2051 output_filenames( ok_files
);
2053 strarray_addall( &clean_files
, ok_files
);
2054 strarray_add( &phony_targets
, "check" );
2055 strarray_add( &phony_targets
, "test" );
2056 strarray_add( &phony_targets
, "testclean" );
2057 testlist_files
= strarray_replace_extension( &ok_files
, ".ok", "" );
2060 if (all_targets
.count
)
2063 output_filenames( all_targets
);
2067 strarray_addall( &clean_files
, object_files
);
2068 strarray_addall( &clean_files
, crossobj_files
);
2069 strarray_addall( &clean_files
, res_files
);
2070 strarray_addall( &clean_files
, all_targets
);
2071 strarray_addall( &clean_files
, get_expanded_make_var_array( "EXTRA_TARGETS" ));
2073 if (clean_files
.count
)
2075 output( "clean::\n" );
2076 output( "\t$(RM)" );
2077 output_filenames( clean_files
);
2079 strarray_add( &phony_targets
, "clean" );
2084 output( "depend:\n" );
2085 output( "\t@cd %s && $(MAKE) %s\n", top_obj_dir
, base_dir_path( "depend" ));
2086 strarray_add( &phony_targets
, "depend" );
2089 if (phony_targets
.count
)
2091 output( ".PHONY:" );
2092 output_filenames( phony_targets
);
2096 for (i
= 0; i
< subdirs
.count
; i
++) create_dir( subdirs
.str
[i
] );
2102 /*******************************************************************
2105 static FILE *create_temp_file( const char *orig
)
2107 char *name
= xmalloc( strlen(orig
) + 13 );
2108 unsigned int i
, id
= getpid();
2112 for (i
= 0; i
< 100; i
++)
2114 sprintf( name
, "%s.tmp%08x", orig
, id
);
2115 if ((fd
= open( name
, O_RDWR
| O_CREAT
| O_EXCL
, 0666 )) != -1)
2117 ret
= fdopen( fd
, "w" );
2120 if (errno
!= EEXIST
) break;
2123 if (!ret
) fatal_error( "failed to create output file for '%s'\n", orig
);
2124 temp_file_name
= name
;
2129 /*******************************************************************
2132 static void rename_temp_file( const char *dest
)
2134 int ret
= rename( temp_file_name
, dest
);
2135 if (ret
== -1 && errno
== EEXIST
)
2137 /* rename doesn't overwrite on windows */
2139 ret
= rename( temp_file_name
, dest
);
2141 if (ret
== -1) fatal_error( "failed to rename output file to '%s'\n", dest
);
2142 temp_file_name
= NULL
;
2146 /*******************************************************************
2147 * are_files_identical
2149 static int are_files_identical( FILE *file1
, FILE *file2
)
2153 char buffer1
[8192], buffer2
[8192];
2154 int size1
= fread( buffer1
, 1, sizeof(buffer1
), file1
);
2155 int size2
= fread( buffer2
, 1, sizeof(buffer2
), file2
);
2156 if (size1
!= size2
) return 0;
2157 if (!size1
) return feof( file1
) && feof( file2
);
2158 if (memcmp( buffer1
, buffer2
, size1
)) return 0;
2163 /*******************************************************************
2164 * rename_temp_file_if_changed
2166 static void rename_temp_file_if_changed( const char *dest
)
2168 FILE *file1
, *file2
;
2171 if ((file1
= fopen( dest
, "r" )))
2173 if ((file2
= fopen( temp_file_name
, "r" )))
2175 do_rename
= !are_files_identical( file1
, file2
);
2182 unlink( temp_file_name
);
2183 temp_file_name
= NULL
;
2185 else rename_temp_file( dest
);
2189 /*******************************************************************
2192 static void output_testlist( const char *dest
, struct strarray files
)
2196 output_file
= create_temp_file( dest
);
2198 output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
2199 output( "#define WIN32_LEAN_AND_MEAN\n" );
2200 output( "#include <windows.h>\n\n" );
2201 output( "#define STANDALONE\n" );
2202 output( "#include \"wine/test.h\"\n\n" );
2204 for (i
= 0; i
< files
.count
; i
++) output( "extern void func_%s(void);\n", files
.str
[i
] );
2206 output( "const struct test winetest_testlist[] =\n" );
2208 for (i
= 0; i
< files
.count
; i
++) output( " { \"%s\", func_%s },\n", files
.str
[i
], files
.str
[i
] );
2209 output( " { 0, 0 }\n" );
2212 if (fclose( output_file
)) fatal_perror( "write" );
2214 rename_temp_file_if_changed( dest
);
2218 /*******************************************************************
2221 static void output_gitignore( const char *dest
, struct strarray files
)
2225 output_file
= create_temp_file( dest
);
2227 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2228 for (i
= 0; i
< files
.count
; i
++)
2230 if (!strchr( files
.str
[i
], '/' )) output( "/" );
2231 output( "%s\n", files
.str
[i
] );
2234 if (fclose( output_file
)) fatal_perror( "write" );
2236 rename_temp_file( dest
);
2240 /*******************************************************************
2241 * output_dependencies
2243 static void output_dependencies( const char *path
)
2245 struct strarray targets
, ignore_files
= empty_strarray
;
2247 if (Separator
&& ((output_file
= fopen( path
, "r" ))))
2250 FILE *tmp_file
= create_temp_file( path
);
2253 while (fgets( buffer
, sizeof(buffer
), output_file
) && !found
)
2255 if (fwrite( buffer
, 1, strlen(buffer
), tmp_file
) != strlen(buffer
)) fatal_perror( "write" );
2256 found
= !strncmp( buffer
, Separator
, strlen(Separator
) );
2258 if (fclose( output_file
)) fatal_perror( "write" );
2259 output_file
= tmp_file
;
2260 if (!found
) output( "\n%s\n", Separator
);
2264 if (!(output_file
= fopen( path
, Separator
? "a" : "w" )))
2265 fatal_perror( "%s", path
);
2268 testlist_files
= empty_strarray
;
2269 targets
= output_sources();
2271 fclose( output_file
);
2273 if (temp_file_name
) rename_temp_file( path
);
2275 strarray_add( &ignore_files
, ".gitignore" );
2276 strarray_add( &ignore_files
, "Makefile" );
2277 if (testlist_files
.count
) strarray_add( &ignore_files
, "testlist.c" );
2278 strarray_addall( &ignore_files
, targets
);
2280 if (testlist_files
.count
) output_testlist( base_dir_path( "testlist.c" ), testlist_files
);
2281 if (!src_dir
&& base_dir
) output_gitignore( base_dir_path( ".gitignore" ), ignore_files
);
2285 /*******************************************************************
2288 static void update_makefile( const char *path
)
2290 static const char *source_vars
[] =
2308 struct strarray value
;
2309 struct incl_file
*file
;
2312 if (!strcmp( base_dir
, "." )) base_dir
= NULL
;
2313 output_file_name
= base_dir_path( makefile_name
);
2316 src_dir
= get_expanded_make_variable( "srcdir" );
2317 top_src_dir
= get_expanded_make_variable( "top_srcdir" );
2318 top_obj_dir
= get_expanded_make_variable( "top_builddir" );
2319 parent_dir
= get_expanded_make_variable( "PARENTSRC" );
2320 tools_dir
= get_expanded_make_variable( "TOOLSDIR" );
2321 tools_ext
= get_expanded_make_variable( "TOOLSEXT" );
2323 /* ignore redundant source paths */
2324 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
2325 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
2326 if (tools_dir
&& top_obj_dir
&& !strcmp( tools_dir
, top_obj_dir
)) tools_dir
= NULL
;
2327 if (top_obj_dir
&& !strcmp( top_obj_dir
, "." )) top_obj_dir
= NULL
;
2329 appmode
= get_expanded_make_var_array( "APPMODE" );
2330 dllflags
= get_expanded_make_var_array( "DLLFLAGS" );
2331 imports
= get_expanded_make_var_array( "IMPORTS" );
2334 for (i
= 0; i
< appmode
.count
&& !use_msvcrt
; i
++)
2335 use_msvcrt
= !strcmp( appmode
.str
[i
], "-mno-cygwin" );
2336 for (i
= 0; i
< imports
.count
&& !use_msvcrt
; i
++)
2337 use_msvcrt
= !strncmp( imports
.str
[i
], "msvcr", 5 );
2339 include_args
= empty_strarray
;
2340 define_args
= empty_strarray
;
2341 strarray_add( &define_args
, "-D__WINESRC__" );
2343 if (!tools_ext
) tools_ext
= "";
2345 value
= get_expanded_make_var_array( "EXTRAINCL" );
2346 for (i
= 0; i
< value
.count
; i
++)
2347 if (!strncmp( value
.str
[i
], "-I", 2 ))
2348 strarray_add_uniq( &include_args
, value
.str
[i
] );
2350 strarray_add_uniq( &define_args
, value
.str
[i
] );
2351 strarray_addall( &define_args
, get_expanded_make_var_array( "EXTRADEFS" ));
2353 if (use_msvcrt
) strarray_add( &dllflags
, get_expanded_make_variable( "MSVCRTFLAGS" ));
2355 list_init( &sources
);
2356 list_init( &includes
);
2358 for (var
= source_vars
; *var
; var
++)
2360 value
= get_expanded_make_var_array( *var
);
2361 for (i
= 0; i
< value
.count
; i
++) add_src_file( value
.str
[i
] );
2364 add_generated_sources();
2366 value
= get_expanded_make_var_array( "EXTRA_OBJS" );
2367 for (i
= 0; i
< value
.count
; i
++)
2369 /* default to .c for unknown extra object files */
2370 if (strendswith( value
.str
[i
], ".o" ))
2371 add_generated_source( value
.str
[i
], replace_extension( value
.str
[i
], ".o", ".c" ) );
2373 add_generated_source( value
.str
[i
], NULL
);
2376 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
) parse_file( file
, 0 );
2377 output_dependencies( output_file_name
);
2378 output_file_name
= NULL
;
2382 /*******************************************************************
2385 static void parse_makeflags( const char *flags
)
2387 const char *p
= flags
;
2388 char *var
, *buffer
= xmalloc( strlen(flags
) + 1 );
2392 while (isspace(*p
)) p
++;
2394 while (*p
&& !isspace(*p
))
2396 if (*p
== '\\' && p
[1]) p
++;
2400 if (var
> buffer
) set_make_variable( &cmdline_vars
, buffer
);
2405 /*******************************************************************
2408 static int parse_option( const char *opt
)
2412 if (strchr( opt
, '=' )) return set_make_variable( &cmdline_vars
, opt
);
2418 if (opt
[2]) makefile_name
= opt
+ 2;
2421 relative_dir_mode
= 1;
2424 if (opt
[2]) Separator
= opt
+ 2;
2425 else Separator
= NULL
;
2428 fprintf( stderr
, "Unknown option '%s'\n%s", opt
, Usage
);
2435 /*******************************************************************
2438 int main( int argc
, char *argv
[] )
2440 const char *makeflags
= getenv( "MAKEFLAGS" );
2443 if (makeflags
) parse_makeflags( makeflags
);
2448 if (parse_option( argv
[i
] ))
2450 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
2456 if (relative_dir_mode
)
2462 fprintf( stderr
, "Option -r needs two directories\n%s", Usage
);
2465 relpath
= get_relative_path( argv
[1], argv
[2] );
2466 printf( "%s\n", relpath
? relpath
: "." );
2472 fprintf( stderr
, "%s", Usage
);
2475 atexit( cleanup_files
);
2476 signal( SIGTERM
, exit_on_signal
);
2477 signal( SIGINT
, exit_on_signal
);
2479 signal( SIGHUP
, exit_on_signal
);
2482 for (i
= 1; i
< argc
; i
++) update_makefile( argv
[i
] );