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 struct incl_file
*included_by
; /* file that included this one */
45 int included_line
; /* line where this file was included */
46 unsigned int flags
; /* flags (see below) */
47 struct incl_file
*owner
;
48 unsigned int files_count
; /* files in use */
49 unsigned int files_size
; /* total allocated size */
50 struct incl_file
**files
;
53 #define FLAG_SYSTEM 0x0001 /* is it a system include (#include <name>) */
54 #define FLAG_GENERATED 0x0002 /* generated file */
55 #define FLAG_IDL_PROXY 0x0004 /* generates a proxy (_p.c) file */
56 #define FLAG_IDL_CLIENT 0x0008 /* generates a client (_c.c) file */
57 #define FLAG_IDL_SERVER 0x0010 /* generates a server (_s.c) file */
58 #define FLAG_IDL_IDENT 0x0020 /* generates an ident (_i.c) file */
59 #define FLAG_IDL_REGISTER 0x0040 /* generates a registration (_r.res) file */
60 #define FLAG_IDL_TYPELIB 0x0080 /* generates a typelib (.tlb) file */
61 #define FLAG_IDL_REGTYPELIB 0x0100 /* generates a registered typelib (_t.res) file */
62 #define FLAG_IDL_HEADER 0x0200 /* generates a header (.h) file */
63 #define FLAG_RC_PO 0x0400 /* rc file contains translations */
64 #define FLAG_C_IMPLIB 0x0800 /* file is part of an import library */
72 { FLAG_IDL_TYPELIB
, ".tlb" },
73 { FLAG_IDL_REGTYPELIB
, "_t.res" },
74 { FLAG_IDL_CLIENT
, "_c.c" },
75 { FLAG_IDL_IDENT
, "_i.c" },
76 { FLAG_IDL_PROXY
, "_p.c" },
77 { FLAG_IDL_SERVER
, "_s.c" },
78 { FLAG_IDL_REGISTER
, "_r.res" },
79 { FLAG_IDL_HEADER
, ".h" }
82 static struct list sources
= LIST_INIT(sources
);
83 static struct list includes
= LIST_INIT(includes
);
87 unsigned int count
; /* strings in use */
88 unsigned int size
; /* total allocated size */
92 static const struct strarray empty_strarray
;
94 static struct strarray include_args
;
95 static struct strarray define_args
;
96 static struct strarray appmode
;
97 static struct strarray dllflags
;
98 static struct strarray imports
;
99 static struct strarray make_vars
;
100 static struct strarray cmdline_vars
;
102 static const char *base_dir
= ".";
103 static const char *src_dir
;
104 static const char *top_src_dir
;
105 static const char *top_obj_dir
;
106 static const char *parent_dir
;
107 static const char *tools_dir
;
108 static const char *tools_ext
;
109 static const char *makefile_name
= "Makefile";
110 static const char *Separator
= "### Dependencies";
111 static const char *input_file_name
;
112 static const char *output_file_name
;
113 static const char *temp_file_name
;
114 static int relative_dir_mode
;
115 static int input_line
;
116 static int output_column
;
117 static FILE *output_file
;
119 static const char Usage
[] =
120 "Usage: makedep [options] directories\n"
122 " -R from to Compute the relative path between two directories\n"
123 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
124 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
128 #define __attribute__(x)
131 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
132 static void fatal_perror( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
133 static void output( const char *format
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
134 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
136 /*******************************************************************
139 static void fatal_error( const char *msg
, ... )
142 va_start( valist
, msg
);
145 fprintf( stderr
, "%s:", input_file_name
);
146 if (input_line
) fprintf( stderr
, "%d:", input_line
);
147 fprintf( stderr
, " error: " );
149 else fprintf( stderr
, "makedep: error: " );
150 vfprintf( stderr
, msg
, valist
);
156 /*******************************************************************
159 static void fatal_perror( const char *msg
, ... )
162 va_start( valist
, msg
);
165 fprintf( stderr
, "%s:", input_file_name
);
166 if (input_line
) fprintf( stderr
, "%d:", input_line
);
167 fprintf( stderr
, " error: " );
169 else fprintf( stderr
, "makedep: error: " );
170 vfprintf( stderr
, msg
, valist
);
177 /*******************************************************************
180 static void cleanup_files(void)
182 if (temp_file_name
) unlink( temp_file_name
);
183 if (output_file_name
) unlink( output_file_name
);
187 /*******************************************************************
190 static void exit_on_signal( int sig
)
192 exit( 1 ); /* this will call the atexit functions */
196 /*******************************************************************
199 static void *xmalloc( size_t size
)
202 if (!(res
= malloc (size
? size
: 1)))
203 fatal_error( "Virtual memory exhausted.\n" );
208 /*******************************************************************
211 static void *xrealloc (void *ptr
, size_t size
)
215 if (!(res
= realloc( ptr
, size
)))
216 fatal_error( "Virtual memory exhausted.\n" );
220 /*******************************************************************
223 static char *xstrdup( const char *str
)
225 char *res
= strdup( str
);
226 if (!res
) fatal_error( "Virtual memory exhausted.\n" );
231 /*******************************************************************
234 static char *strmake( const char* fmt
, ... )
242 char *p
= xmalloc (size
);
244 n
= vsnprintf (p
, size
, fmt
, ap
);
246 if (n
== -1) size
*= 2;
247 else if ((size_t)n
>= size
) size
= n
+ 1;
254 /*******************************************************************
257 static int strendswith( const char* str
, const char* end
)
262 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
266 /*******************************************************************
269 static void output( const char *format
, ... )
274 va_start( valist
, format
);
275 ret
= vfprintf( output_file
, format
, valist
);
277 if (ret
< 0) fatal_perror( "output" );
278 if (format
[0] && format
[strlen(format
) - 1] == '\n') output_column
= 0;
279 else output_column
+= ret
;
283 /*******************************************************************
286 static void strarray_add( struct strarray
*array
, const char *str
)
288 if (array
->count
== array
->size
)
290 if (array
->size
) array
->size
*= 2;
291 else array
->size
= 16;
292 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
294 array
->str
[array
->count
++] = str
;
298 /*******************************************************************
301 static void strarray_addall( struct strarray
*array
, struct strarray added
)
305 for (i
= 0; i
< added
.count
; i
++) strarray_add( array
, added
.str
[i
] );
309 /*******************************************************************
312 static void strarray_add_uniq( struct strarray
*array
, const char *str
)
316 for (i
= 0; i
< array
->count
; i
++) if (!strcmp( array
->str
[i
], str
)) return;
317 strarray_add( array
, str
);
321 /*******************************************************************
324 static void output_filename( const char *name
)
326 if (output_column
+ strlen(name
) + 1 > 100)
331 else if (output_column
) output( " " );
332 output( "%s", name
);
336 /*******************************************************************
339 static void output_filenames( struct strarray array
)
343 for (i
= 0; i
< array
.count
; i
++) output_filename( array
.str
[i
] );
347 /*******************************************************************
350 static char *get_extension( char *filename
)
352 char *ext
= strrchr( filename
, '.' );
353 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
358 /*******************************************************************
361 static char *replace_extension( const char *name
, const char *old_ext
, const char *new_ext
)
364 int name_len
= strlen( name
);
365 int ext_len
= strlen( old_ext
);
367 if (name_len
>= ext_len
&& !strcmp( name
+ name_len
- ext_len
, old_ext
)) name_len
-= ext_len
;
368 ret
= xmalloc( name_len
+ strlen( new_ext
) + 1 );
369 memcpy( ret
, name
, name_len
);
370 strcpy( ret
+ name_len
, new_ext
);
375 /*******************************************************************
376 * strarray_replace_extension
378 static struct strarray
strarray_replace_extension( const struct strarray
*array
,
379 const char *old_ext
, const char *new_ext
)
384 ret
.count
= ret
.size
= array
->count
;
385 ret
.str
= xmalloc( sizeof(ret
.str
[0]) * ret
.size
);
386 for (i
= 0; i
< array
->count
; i
++) ret
.str
[i
] = replace_extension( array
->str
[i
], old_ext
, new_ext
);
391 /*******************************************************************
394 static char *replace_substr( const char *str
, const char *start
, unsigned int len
, const char *replace
)
396 unsigned int pos
= start
- str
;
397 char *ret
= xmalloc( pos
+ strlen(replace
) + strlen(start
+ len
) + 1 );
398 memcpy( ret
, str
, pos
);
399 strcpy( ret
+ pos
, replace
);
400 strcat( ret
+ pos
, start
+ len
);
405 /*******************************************************************
408 * Determine where the destination path is located relative to the 'from' path.
410 static char *get_relative_path( const char *from
, const char *dest
)
414 unsigned int dotdots
= 0;
416 /* a path of "." is equivalent to an empty path */
417 if (!strcmp( from
, "." )) from
= "";
421 while (*from
== '/') from
++;
422 while (*dest
== '/') dest
++;
423 start
= dest
; /* save start of next path element */
426 while (*from
&& *from
!= '/' && *from
== *dest
) { from
++; dest
++; }
427 if ((!*from
|| *from
== '/') && (!*dest
|| *dest
== '/')) continue;
429 /* count remaining elements in 'from' */
433 while (*from
&& *from
!= '/') from
++;
434 while (*from
== '/') from
++;
440 if (!start
[0] && !dotdots
) return NULL
; /* empty path */
442 ret
= xmalloc( 3 * dotdots
+ strlen( start
) + 1 );
443 for (p
= ret
; dotdots
; dotdots
--, p
+= 3) memcpy( p
, "../", 3 );
445 if (start
[0]) strcpy( p
, start
);
446 else p
[-1] = 0; /* remove trailing slash */
451 /*******************************************************************
454 static char *src_dir_path( const char *path
)
456 if (src_dir
) return strmake( "%s/%s", src_dir
, path
);
457 return xstrdup( path
);
461 /*******************************************************************
464 static char *top_obj_dir_path( const char *path
)
466 if (top_obj_dir
) return strmake( "%s/%s", top_obj_dir
, path
);
467 return xstrdup( path
);
471 /*******************************************************************
474 static char *top_dir_path( const char *path
)
476 if (top_src_dir
) return strmake( "%s/%s", top_src_dir
, path
);
477 return top_obj_dir_path( path
);
481 /*******************************************************************
484 static char *tools_dir_path( const char *path
)
486 if (tools_dir
) return strmake( "%s/tools/%s", tools_dir
, path
);
487 if (top_obj_dir
) return strmake( "%s/tools/%s", top_obj_dir
, path
);
488 return strmake( "tools/%s", path
);
492 /*******************************************************************
495 static char *tools_path( const char *name
)
497 if (tools_dir
) return strmake( "%s/tools/%s/%s%s", tools_dir
, name
, name
, tools_ext
);
498 if (top_obj_dir
) return strmake( "%s/tools/%s/%s%s", top_obj_dir
, name
, name
, tools_ext
);
499 return strmake( "tools/%s/%s%s", name
, name
, tools_ext
);
503 /*******************************************************************
506 static char *get_line( FILE *file
)
509 static unsigned int size
;
514 buffer
= xmalloc( size
);
516 if (!fgets( buffer
, size
, file
)) return NULL
;
521 char *p
= buffer
+ strlen(buffer
);
522 /* if line is larger than buffer, resize buffer */
523 while (p
== buffer
+ size
- 1 && p
[-1] != '\n')
525 buffer
= xrealloc( buffer
, size
* 2 );
526 if (!fgets( buffer
+ size
- 1, size
+ 1, file
)) break;
527 p
= buffer
+ strlen(buffer
);
530 if (p
> buffer
&& p
[-1] == '\n')
533 if (p
> buffer
&& p
[-1] == '\r') *(--p
) = 0;
534 if (p
> buffer
&& p
[-1] == '\\')
537 /* line ends in backslash, read continuation line */
538 if (!fgets( p
, size
- (p
- buffer
), file
)) return buffer
;
547 /*******************************************************************
550 static struct incl_file
*find_src_file( const char *name
)
552 struct incl_file
*file
;
554 LIST_FOR_EACH_ENTRY( file
, &sources
, struct incl_file
, entry
)
555 if (!strcmp( name
, file
->name
)) return file
;
559 /*******************************************************************
562 static struct incl_file
*find_include_file( const char *name
)
564 struct incl_file
*file
;
566 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
)
567 if (!strcmp( name
, file
->name
)) return file
;
571 /*******************************************************************
574 * Add an include file if it doesn't already exists.
576 static struct incl_file
*add_include( struct incl_file
*parent
, const char *name
, int system
)
578 struct incl_file
*include
;
581 if (parent
->files_count
>= parent
->files_size
)
583 parent
->files_size
*= 2;
584 if (parent
->files_size
< 16) parent
->files_size
= 16;
585 parent
->files
= xrealloc( parent
->files
, parent
->files_size
* sizeof(*parent
->files
) );
588 /* enforce some rules for the Wine tree */
590 if (!memcmp( name
, "../", 3 ))
591 fatal_error( "#include directive with relative path not allowed\n" );
593 if (!strcmp( name
, "config.h" ))
595 if ((ext
= strrchr( parent
->filename
, '.' )) && !strcmp( ext
, ".h" ))
596 fatal_error( "config.h must not be included by a header file\n" );
597 if (parent
->files_count
)
598 fatal_error( "config.h must be included before anything else\n" );
600 else if (!strcmp( name
, "wine/port.h" ))
602 if ((ext
= strrchr( parent
->filename
, '.' )) && !strcmp( ext
, ".h" ))
603 fatal_error( "wine/port.h must not be included by a header file\n" );
604 if (!parent
->files_count
) fatal_error( "config.h must be included before wine/port.h\n" );
605 if (parent
->files_count
> 1)
606 fatal_error( "wine/port.h must be included before everything except config.h\n" );
607 if (strcmp( parent
->files
[0]->name
, "config.h" ))
608 fatal_error( "config.h must be included before wine/port.h\n" );
611 LIST_FOR_EACH_ENTRY( include
, &includes
, struct incl_file
, entry
)
612 if (!strcmp( name
, include
->name
)) goto found
;
614 include
= xmalloc( sizeof(*include
) );
615 memset( include
, 0, sizeof(*include
) );
616 include
->name
= xstrdup(name
);
617 include
->included_by
= parent
;
618 include
->included_line
= input_line
;
619 if (system
) include
->flags
|= FLAG_SYSTEM
;
620 list_add_tail( &includes
, &include
->entry
);
622 parent
->files
[parent
->files_count
++] = include
;
627 /*******************************************************************
630 static FILE *open_file( const char *path
)
634 if (path
[0] != '/' && strcmp( base_dir
, "." ))
636 char *full_path
= strmake( "%s/%s", base_dir
, path
);
637 ret
= fopen( full_path
, "r" );
640 else ret
= fopen( path
, "r" );
645 /*******************************************************************
648 static FILE *open_src_file( struct incl_file
*pFile
)
652 /* first try name as is */
653 if ((file
= open_file( pFile
->name
)))
655 pFile
->filename
= xstrdup( pFile
->name
);
658 /* now try in source dir */
661 pFile
->filename
= src_dir_path( pFile
->name
);
662 file
= open_file( pFile
->filename
);
664 /* now try parent dir */
665 if (!file
&& parent_dir
)
667 pFile
->filename
= src_dir_path( strmake( "%s/%s", parent_dir
, pFile
->name
));
668 file
= open_file( pFile
->filename
);
670 if (!file
) fatal_perror( "open %s", pFile
->name
);
675 /*******************************************************************
678 static FILE *open_include_file( struct incl_file
*pFile
)
686 /* check for generated bison header */
688 if (strendswith( pFile
->name
, ".tab.h" ))
690 filename
= src_dir_path( replace_extension( pFile
->name
, ".tab.h", ".y" ));
691 if ((file
= open_file( filename
)))
693 pFile
->sourcename
= filename
;
694 pFile
->filename
= xstrdup( pFile
->name
);
695 /* don't bother to parse it */
702 /* check for corresponding idl file in source dir */
704 if (strendswith( pFile
->name
, ".h" ))
706 filename
= src_dir_path( replace_extension( pFile
->name
, ".h", ".idl" ));
707 if ((file
= open_file( filename
)))
709 pFile
->sourcename
= filename
;
710 pFile
->filename
= xstrdup( pFile
->name
);
716 /* now try in source dir */
717 filename
= src_dir_path( pFile
->name
);
718 if ((file
= open_file( filename
))) goto found
;
721 /* now try in parent source dir */
724 filename
= src_dir_path( strmake( "%s/%s", parent_dir
, pFile
->name
));
725 if ((file
= open_file( filename
))) goto found
;
729 /* check for corresponding idl file in global includes */
731 if (strendswith( pFile
->name
, ".h" ))
733 filename
= top_dir_path( strmake( "include/%s", replace_extension( pFile
->name
, ".h", ".idl" )));
734 if ((file
= open_file( filename
)))
736 pFile
->sourcename
= filename
;
737 pFile
->filename
= top_obj_dir_path( strmake( "include/%s", pFile
->name
));
743 /* check for corresponding .in file in global includes (for config.h.in) */
745 if (strendswith( pFile
->name
, ".h" ))
747 filename
= top_dir_path( strmake( "include/%s", replace_extension( pFile
->name
, ".h", ".h.in" )));
748 if ((file
= open_file( filename
)))
750 pFile
->sourcename
= filename
;
751 pFile
->filename
= top_obj_dir_path( strmake( "include/%s", pFile
->name
));
757 /* check for corresponding .x file in global includes */
759 if (strendswith( pFile
->name
, "tmpl.h" ))
761 filename
= top_dir_path( strmake( "include/%s", replace_extension( pFile
->name
, ".h", ".x" )));
762 if ((file
= open_file( filename
)))
764 pFile
->sourcename
= filename
;
765 pFile
->filename
= top_obj_dir_path( strmake( "include/%s", pFile
->name
));
771 /* now search in include paths */
772 for (i
= 0; i
< include_args
.count
; i
++)
774 const char *dir
= include_args
.str
[i
] + 2; /* skip -I */
777 /* ignore absolute paths that don't point into the source dir */
778 if (!top_src_dir
) continue;
779 len
= strlen( top_src_dir
);
780 if (strncmp( dir
, top_src_dir
, len
)) continue;
781 if (dir
[len
] && dir
[len
] != '/') continue;
783 filename
= strmake( "%s/%s", dir
, pFile
->name
);
784 if ((file
= open_file( filename
))) goto found
;
787 if (pFile
->flags
& FLAG_SYSTEM
) return NULL
; /* ignore system files we cannot find */
789 /* try in src file directory */
790 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
792 int l
= p
- pFile
->included_by
->filename
+ 1;
793 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
794 memcpy( filename
, pFile
->included_by
->filename
, l
);
795 strcpy( filename
+ l
, pFile
->name
);
796 if ((file
= open_file( filename
))) goto found
;
800 fprintf( stderr
, "%s:%d: error: ", pFile
->included_by
->filename
, pFile
->included_line
);
801 perror( pFile
->name
);
802 pFile
= pFile
->included_by
;
803 while (pFile
&& pFile
->included_by
)
805 const char *parent
= pFile
->included_by
->sourcename
;
806 if (!parent
) parent
= pFile
->included_by
->name
;
807 fprintf( stderr
, "%s:%d: note: %s was first included here\n",
808 parent
, pFile
->included_line
, pFile
->name
);
809 pFile
= pFile
->included_by
;
814 pFile
->filename
= filename
;
819 /*******************************************************************
820 * parse_include_directive
822 static void parse_include_directive( struct incl_file
*source
, char *str
)
824 char quote
, *include
, *p
= str
;
826 while (*p
&& isspace(*p
)) p
++;
827 if (*p
!= '\"' && *p
!= '<' ) return;
829 if (quote
== '<') quote
= '>';
831 while (*p
&& (*p
!= quote
)) p
++;
832 if (!*p
) fatal_error( "malformed include directive '%s'\n", str
);
834 add_include( source
, include
, (quote
== '>') );
838 /*******************************************************************
839 * parse_pragma_directive
841 static void parse_pragma_directive( struct incl_file
*source
, char *str
)
843 char *flag
, *p
= str
;
845 if (!isspace( *p
)) return;
846 while (*p
&& isspace(*p
)) p
++;
847 p
= strtok( p
, " \t" );
848 if (strcmp( p
, "makedep" )) return;
850 while ((flag
= strtok( NULL
, " \t" )))
852 if (!strcmp( flag
, "depend" ))
854 while ((p
= strtok( NULL
, " \t" ))) add_include( source
, p
, 0 );
857 if (strendswith( source
->name
, ".idl" ))
859 if (!strcmp( flag
, "header" )) source
->flags
|= FLAG_IDL_HEADER
;
860 else if (!strcmp( flag
, "proxy" )) source
->flags
|= FLAG_IDL_PROXY
;
861 else if (!strcmp( flag
, "client" )) source
->flags
|= FLAG_IDL_CLIENT
;
862 else if (!strcmp( flag
, "server" )) source
->flags
|= FLAG_IDL_SERVER
;
863 else if (!strcmp( flag
, "ident" )) source
->flags
|= FLAG_IDL_IDENT
;
864 else if (!strcmp( flag
, "typelib" )) source
->flags
|= FLAG_IDL_TYPELIB
;
865 else if (!strcmp( flag
, "register" )) source
->flags
|= FLAG_IDL_REGISTER
;
866 else if (!strcmp( flag
, "regtypelib" )) source
->flags
|= FLAG_IDL_REGTYPELIB
;
868 else if (strendswith( source
->name
, ".rc" ))
870 if (!strcmp( flag
, "po" )) source
->flags
|= FLAG_RC_PO
;
872 else if (!strcmp( flag
, "implib" )) source
->flags
|= FLAG_C_IMPLIB
;
877 /*******************************************************************
878 * parse_cpp_directive
880 static void parse_cpp_directive( struct incl_file
*source
, char *str
)
882 while (*str
&& isspace(*str
)) str
++;
883 if (*str
++ != '#') return;
884 while (*str
&& isspace(*str
)) str
++;
886 if (!strncmp( str
, "include", 7 ))
887 parse_include_directive( source
, str
+ 7 );
888 else if (!strncmp( str
, "import", 6 ) && strendswith( source
->name
, ".m" ))
889 parse_include_directive( source
, str
+ 6 );
890 else if (!strncmp( str
, "pragma", 6 ))
891 parse_pragma_directive( source
, str
+ 6 );
895 /*******************************************************************
898 * If for_h_file is non-zero, it means we are not interested in the idl file
899 * itself, but only in the contents of the .h file that will be generated from it.
901 static void parse_idl_file( struct incl_file
*pFile
, FILE *file
, int for_h_file
)
903 char *buffer
, *include
;
908 /* generated .h file always includes these */
909 add_include( pFile
, "rpc.h", 1 );
910 add_include( pFile
, "rpcndr.h", 1 );
913 while ((buffer
= get_line( file
)))
917 while (*p
&& isspace(*p
)) p
++;
919 if (!strncmp( p
, "import", 6 ))
922 while (*p
&& isspace(*p
)) p
++;
923 if (*p
!= '"') continue;
925 while (*p
&& (*p
!= '"')) p
++;
926 if (!*p
) fatal_error( "malformed import directive\n" );
928 if (for_h_file
&& strendswith( include
, ".idl" )) strcpy( p
- 4, ".h" );
929 add_include( pFile
, include
, 0 );
933 if (for_h_file
) /* only check for #include inside cpp_quote */
935 if (strncmp( p
, "cpp_quote", 9 )) continue;
937 while (*p
&& isspace(*p
)) p
++;
938 if (*p
++ != '(') continue;
939 while (*p
&& isspace(*p
)) p
++;
940 if (*p
++ != '"') continue;
941 if (*p
++ != '#') continue;
942 while (*p
&& isspace(*p
)) p
++;
943 if (strncmp( p
, "include", 7 )) continue;
945 while (*p
&& isspace(*p
)) p
++;
946 if (*p
== '\\' && p
[1] == '"')
953 if (*p
++ != '<' ) continue;
957 while (*p
&& (*p
!= quote
)) p
++;
958 if (!*p
|| (quote
== '"' && p
[-1] != '\\'))
959 fatal_error( "malformed #include directive inside cpp_quote\n" );
960 if (quote
== '"') p
--; /* remove backslash */
962 add_include( pFile
, include
, (quote
== '>') );
966 parse_cpp_directive( pFile
, p
);
970 /*******************************************************************
973 static void parse_c_file( struct incl_file
*pFile
, FILE *file
)
978 while ((buffer
= get_line( file
)))
980 parse_cpp_directive( pFile
, buffer
);
985 /*******************************************************************
988 static void parse_rc_file( struct incl_file
*pFile
, FILE *file
)
990 char *buffer
, *include
;
993 while ((buffer
= get_line( file
)))
997 while (*p
&& isspace(*p
)) p
++;
999 if (p
[0] == '/' && p
[1] == '*') /* check for magic makedep comment */
1002 while (*p
&& isspace(*p
)) p
++;
1003 if (strncmp( p
, "@makedep:", 9 )) continue;
1005 while (*p
&& isspace(*p
)) p
++;
1010 while (*p
&& *p
!= quote
) p
++;
1015 while (*p
&& !isspace(*p
) && *p
!= '*') p
++;
1018 fatal_error( "malformed makedep comment\n" );
1020 add_include( pFile
, include
, (quote
== '>') );
1024 parse_cpp_directive( pFile
, buffer
);
1029 /*******************************************************************
1032 static void parse_in_file( struct incl_file
*source
, FILE *file
)
1036 /* make sure it gets rebuilt when the version changes */
1037 add_include( source
, "config.h", 1 );
1039 if (!strendswith( source
->filename
, ".man.in" )) return; /* not a man page */
1042 while ((buffer
= get_line( file
)))
1044 if (strncmp( buffer
, ".TH", 3 )) continue;
1045 if (!(p
= strtok( buffer
, " \t" ))) continue; /* .TH */
1046 if (!(p
= strtok( NULL
, " \t" ))) continue; /* program name */
1047 if (!(p
= strtok( NULL
, " \t" ))) continue; /* man section */
1048 source
->sourcename
= xstrdup( p
); /* abuse source name to store section */
1054 /*******************************************************************
1057 static void parse_file( struct incl_file
*source
, int src
)
1061 /* don't try to open certain types of files */
1062 if (strendswith( source
->name
, ".tlb" ))
1064 source
->filename
= xstrdup( source
->name
);
1068 file
= src
? open_src_file( source
) : open_include_file( source
);
1070 input_file_name
= source
->filename
;
1072 if (source
->sourcename
&& strendswith( source
->sourcename
, ".idl" ))
1073 parse_idl_file( source
, file
, 1 );
1074 else if (strendswith( source
->filename
, ".idl" ))
1075 parse_idl_file( source
, file
, 0 );
1076 else if (strendswith( source
->filename
, ".c" ) ||
1077 strendswith( source
->filename
, ".m" ) ||
1078 strendswith( source
->filename
, ".h" ) ||
1079 strendswith( source
->filename
, ".l" ) ||
1080 strendswith( source
->filename
, ".y" ))
1081 parse_c_file( source
, file
);
1082 else if (strendswith( source
->filename
, ".rc" ))
1083 parse_rc_file( source
, file
);
1084 else if (strendswith( source
->filename
, ".in" ))
1085 parse_in_file( source
, file
);
1087 input_file_name
= NULL
;
1091 /*******************************************************************
1094 * Add a source file to the list.
1096 static struct incl_file
*add_src_file( const char *name
)
1098 struct incl_file
*file
;
1100 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1101 file
= xmalloc( sizeof(*file
) );
1102 memset( file
, 0, sizeof(*file
) );
1103 file
->name
= xstrdup(name
);
1104 list_add_tail( &sources
, &file
->entry
);
1105 parse_file( file
, 1 );
1110 /*******************************************************************
1113 static char *get_make_variable( const char *name
)
1117 for (i
= 0; i
< cmdline_vars
.count
; i
+= 2)
1118 if (!strcmp( cmdline_vars
.str
[i
], name
))
1119 return xstrdup( cmdline_vars
.str
[i
+ 1] );
1121 for (i
= 0; i
< make_vars
.count
; i
+= 2)
1122 if (!strcmp( make_vars
.str
[i
], name
))
1123 return xstrdup( make_vars
.str
[i
+ 1] );
1128 /*******************************************************************
1129 * get_expanded_make_variable
1131 static char *get_expanded_make_variable( const char *name
)
1133 char *p
, *end
, *var
, *expand
, *tmp
;
1135 expand
= get_make_variable( name
);
1136 if (!expand
) return NULL
;
1139 while ((p
= strchr( p
, '$' )))
1143 if (!(end
= strchr( p
+ 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand
);
1145 if (strchr( p
+ 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p
+ 2 );
1146 var
= get_make_variable( p
+ 2 );
1147 tmp
= replace_substr( expand
, p
, end
- p
, var
? var
: "" );
1150 else if (p
[1] == '{') /* don't expand ${} variables */
1152 if (!(end
= strchr( p
+ 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand
);
1156 else if (p
[1] == '$')
1158 tmp
= replace_substr( expand
, p
, 2, "$" );
1160 else fatal_error( "syntax error in '%s'\n", expand
);
1162 /* switch to the new string */
1163 p
= tmp
+ (p
- expand
);
1168 /* consider empty variables undefined */
1170 while (*p
&& isspace(*p
)) p
++;
1171 if (*p
) return expand
;
1177 /*******************************************************************
1178 * get_expanded_make_var_array
1180 static struct strarray
get_expanded_make_var_array( const char *name
)
1182 struct strarray ret
= empty_strarray
;
1183 char *value
, *token
;
1185 if ((value
= get_expanded_make_variable( name
)))
1186 for (token
= strtok( value
, " \t" ); token
; token
= strtok( NULL
, " \t" ))
1187 strarray_add( &ret
, token
);
1192 /*******************************************************************
1195 static int set_make_variable( struct strarray
*array
, const char *assignment
)
1200 p
= name
= xstrdup( assignment
);
1201 while (isalnum(*p
) || *p
== '_') p
++;
1202 if (name
== p
) return 0; /* not a variable */
1206 while (isspace(*p
)) p
++;
1208 if (*p
!= '=') return 0; /* not an assignment */
1210 while (isspace(*p
)) p
++;
1212 /* redefining a variable replaces the previous value */
1213 for (i
= 0; i
< array
->count
; i
+= 2)
1215 if (strcmp( array
->str
[i
], name
)) continue;
1216 array
->str
[i
+ 1] = p
;
1219 strarray_add( array
, name
);
1220 strarray_add( array
, p
);
1225 /*******************************************************************
1228 static void parse_makefile(void)
1233 input_file_name
= strmake( "%s/%s", base_dir
, makefile_name
);
1234 if (!(file
= fopen( input_file_name
, "r" )))
1236 fatal_perror( "open" );
1241 while ((buffer
= get_line( file
)))
1243 if (Separator
&& !strncmp( buffer
, Separator
, strlen(Separator
) )) break;
1244 if (*buffer
== '\t') continue; /* command */
1245 while (isspace( *buffer
)) buffer
++;
1246 if (*buffer
== '#') continue; /* comment */
1247 set_make_variable( &make_vars
, buffer
);
1250 input_file_name
= NULL
;
1254 /*******************************************************************
1255 * add_generated_source
1257 * Add a generated source file to the list.
1259 static struct incl_file
*add_generated_source( const char *name
, const char *filename
)
1261 struct incl_file
*file
;
1263 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1264 file
= xmalloc( sizeof(*file
) );
1265 memset( file
, 0, sizeof(*file
) );
1266 file
->name
= xstrdup( name
);
1267 file
->filename
= xstrdup( filename
? filename
: name
);
1268 file
->flags
= FLAG_GENERATED
;
1269 list_add_tail( &sources
, &file
->entry
);
1274 /*******************************************************************
1275 * add_generated_sources
1277 static void add_generated_sources(void)
1279 struct incl_file
*source
, *next
, *file
;
1281 LIST_FOR_EACH_ENTRY_SAFE( source
, next
, &sources
, struct incl_file
, entry
)
1283 if (source
->flags
& FLAG_IDL_CLIENT
)
1285 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_c.c" ), NULL
);
1286 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1288 if (source
->flags
& FLAG_IDL_SERVER
)
1290 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_s.c" ), NULL
);
1291 add_include( file
, "wine/exception.h", 0 );
1292 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1294 if (source
->flags
& FLAG_IDL_IDENT
)
1296 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_i.c" ), NULL
);
1297 add_include( file
, "rpc.h", 0 );
1298 add_include( file
, "rpcndr.h", 0 );
1299 add_include( file
, "guiddef.h", 0 );
1301 if (source
->flags
& FLAG_IDL_PROXY
)
1303 file
= add_generated_source( "dlldata.o", "dlldata.c" );
1304 add_include( file
, "objbase.h", 0 );
1305 add_include( file
, "rpcproxy.h", 0 );
1306 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_p.c" ), NULL
);
1307 add_include( file
, "objbase.h", 0 );
1308 add_include( file
, "rpcproxy.h", 0 );
1309 add_include( file
, "wine/exception.h", 0 );
1310 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1312 if (source
->flags
& FLAG_IDL_REGTYPELIB
)
1314 add_generated_source( replace_extension( source
->name
, ".idl", "_t.res" ), NULL
);
1316 if (source
->flags
& FLAG_IDL_REGISTER
)
1318 add_generated_source( replace_extension( source
->name
, ".idl", "_r.res" ), NULL
);
1320 if (strendswith( source
->name
, ".y" ))
1322 file
= add_generated_source( replace_extension( source
->name
, ".y", ".tab.c" ), NULL
);
1323 /* steal the includes list from the source file */
1324 file
->files_count
= source
->files_count
;
1325 file
->files_size
= source
->files_size
;
1326 file
->files
= source
->files
;
1327 source
->files_count
= source
->files_size
= 0;
1328 source
->files
= NULL
;
1330 if (strendswith( source
->name
, ".l" ))
1332 file
= add_generated_source( replace_extension( source
->name
, ".l", ".yy.c" ), NULL
);
1333 /* steal the includes list from the source file */
1334 file
->files_count
= source
->files_count
;
1335 file
->files_size
= source
->files_size
;
1336 file
->files
= source
->files
;
1337 source
->files_count
= source
->files_size
= 0;
1338 source
->files
= NULL
;
1341 if (get_make_variable( "TESTDLL" ))
1343 file
= add_generated_source( "testlist.o", "testlist.c" );
1344 add_include( file
, "wine/test.h", 0 );
1349 /*******************************************************************
1352 static void create_dir( const char *dir
)
1356 p
= path
= xstrdup( dir
);
1357 while ((p
= strchr( p
, '/' )))
1360 if (mkdir( path
, 0755 ) == -1 && errno
!= EEXIST
) fatal_perror( "mkdir %s", path
);
1362 while (*p
== '/') p
++;
1364 if (mkdir( path
, 0755 ) == -1 && errno
!= EEXIST
) fatal_perror( "mkdir %s", path
);
1369 /*******************************************************************
1372 static void output_include( struct incl_file
*pFile
, struct incl_file
*owner
)
1376 if (pFile
->owner
== owner
) return;
1377 if (!pFile
->filename
) return;
1378 pFile
->owner
= owner
;
1379 output_filename( pFile
->filename
);
1380 for (i
= 0; i
< pFile
->files_count
; i
++) output_include( pFile
->files
[i
], owner
);
1384 /*******************************************************************
1387 static struct strarray
output_sources(void)
1389 struct incl_file
*source
;
1390 int i
, is_win16
= 0;
1391 const char *dllext
= ".so";
1392 struct strarray object_files
= empty_strarray
;
1393 struct strarray crossobj_files
= empty_strarray
;
1394 struct strarray res_files
= empty_strarray
;
1395 struct strarray clean_files
= empty_strarray
;
1396 struct strarray po_files
= empty_strarray
;
1397 struct strarray mo_files
= empty_strarray
;
1398 struct strarray mc_files
= empty_strarray
;
1399 struct strarray test_files
= empty_strarray
;
1400 struct strarray dlldata_files
= empty_strarray
;
1401 struct strarray c2man_files
= empty_strarray
;
1402 struct strarray implib_objs
= empty_strarray
;
1403 struct strarray includes
= empty_strarray
;
1404 struct strarray subdirs
= empty_strarray
;
1405 struct strarray phony_targets
= empty_strarray
;
1406 struct strarray linguas
= get_expanded_make_var_array( "LINGUAS" );
1407 struct strarray all_targets
= get_expanded_make_var_array( "PROGRAMS" );
1408 struct strarray targetflags
= get_expanded_make_var_array( "TARGETFLAGS" );
1409 struct strarray delayimports
= get_expanded_make_var_array( "DELAYIMPORTS" );
1410 struct strarray extradllflags
= get_expanded_make_var_array( "EXTRADLLFLAGS" );
1411 char *module
= get_expanded_make_variable( "MODULE" );
1412 char *exeext
= get_expanded_make_variable( "EXEEXT" );
1413 char *testdll
= get_expanded_make_variable( "TESTDLL" );
1414 char *staticlib
= get_expanded_make_variable( "STATICLIB" );
1415 char *crosstarget
= get_expanded_make_variable( "CROSSTARGET" );
1417 if (exeext
&& !strcmp( exeext
, ".exe" )) dllext
= "";
1418 if (module
&& strendswith( module
, ".a" )) staticlib
= module
;
1419 for (i
= 0; i
< extradllflags
.count
; i
++) if (!strcmp( extradllflags
.str
[i
], "-m16" )) is_win16
= 1;
1421 for (i
= 0; i
< linguas
.count
; i
++)
1422 strarray_add( &mo_files
, strmake( "%s/po/%s.mo", top_obj_dir
, linguas
.str
[i
] ));
1424 strarray_add( &includes
, "-I." );
1425 if (src_dir
) strarray_add( &includes
, strmake( "-I%s", src_dir
));
1426 if (parent_dir
) strarray_add( &includes
, strmake( "-I%s", src_dir_path( parent_dir
)));
1427 if (top_src_dir
&& top_obj_dir
) strarray_add( &includes
, strmake( "-I%s/include", top_obj_dir
));
1428 strarray_addall( &includes
, include_args
);
1430 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1432 struct strarray extradefs
;
1433 char *obj
= xstrdup( source
->name
);
1434 char *ext
= get_extension( obj
);
1436 if (!ext
) fatal_error( "unsupported file type %s\n", source
->name
);
1439 if (src_dir
&& strchr( obj
, '/' ))
1441 char *subdir
= strmake( "%s/%s", base_dir
, obj
);
1442 *strrchr( subdir
, '/' ) = 0;
1443 strarray_add_uniq( &subdirs
, subdir
);
1446 extradefs
= get_expanded_make_var_array( strmake( "%s_EXTRADEFS", obj
));
1448 if (!strcmp( ext
, "y" )) /* yacc file */
1450 /* add source file dependency for parallel makes */
1451 char *header
= strmake( "%s.tab.h", obj
);
1453 if (find_include_file( header
))
1455 output( "%s.tab.h: %s\n", obj
, source
->filename
);
1456 output( "\t$(BISON) -p %s_ -o %s.tab.c -d %s\n",
1457 obj
, obj
, source
->filename
);
1458 output( "%s.tab.c: %s %s\n", obj
, source
->filename
, header
);
1459 strarray_add( &clean_files
, strmake( "%s.tab.h", obj
));
1461 else output( "%s.tab.c: %s\n", obj
, source
->filename
);
1463 output( "\t$(BISON) -p %s_ -o $@ %s\n", obj
, source
->filename
);
1465 continue; /* no dependencies */
1467 else if (!strcmp( ext
, "x" )) /* template file */
1469 output( "%s.h: %s%s %s\n", obj
, tools_dir_path( "make_xftmpl" ), tools_ext
, source
->filename
);
1470 output( "\t%s%s -H -o $@ %s\n", tools_dir_path( "make_xftmpl" ), tools_ext
, source
->filename
);
1471 strarray_add( &clean_files
, strmake( "%s.h", obj
));
1472 continue; /* no dependencies */
1474 else if (!strcmp( ext
, "l" )) /* lex file */
1476 output( "%s.yy.c: %s\n", obj
, source
->filename
);
1477 output( "\t$(FLEX) -o$@ %s\n", source
->filename
);
1478 continue; /* no dependencies */
1480 else if (!strcmp( ext
, "rc" )) /* resource file */
1482 strarray_add( &res_files
, strmake( "%s.res", obj
));
1483 output( "%s.res: %s %s\n", obj
, tools_path( "wrc" ), source
->filename
);
1484 output( "\t%s -o $@ %s", tools_path( "wrc" ), source
->filename
);
1485 if (is_win16
) output_filename( "-m16" );
1486 else output_filenames( targetflags
);
1487 output_filename( "--nostdinc" );
1488 output_filenames( includes
);
1489 output_filenames( define_args
);
1490 output_filenames( extradefs
);
1491 if (mo_files
.count
&& (source
->flags
& FLAG_RC_PO
))
1493 strarray_add( &po_files
, source
->filename
);
1494 output_filename( strmake( "--po-dir=%s/po", top_obj_dir
));
1496 output( "%s.res:", obj
);
1497 output_filenames( mo_files
);
1499 output( "rsrc.pot " );
1501 else output( "\n" );
1502 output( "%s.res:", obj
);
1504 else if (!strcmp( ext
, "mc" )) /* message file */
1506 strarray_add( &res_files
, strmake( "%s.res", obj
));
1507 output( "%s.res: %s %s\n", obj
, tools_path( "wmc" ), source
->filename
);
1508 output( "\t%s -U -O res -o $@ %s", tools_path( "wmc" ), source
->filename
);
1511 strarray_add( &mc_files
, source
->filename
);
1512 output_filename( strmake( "--po-dir=%s/po", top_obj_dir
));
1514 output( "%s.res:", obj
);
1515 output_filenames( mo_files
);
1517 output( "msg.pot " );
1519 else output( "\n" );
1520 output( "%s.res:", obj
);
1522 else if (!strcmp( ext
, "idl" )) /* IDL file */
1524 struct strarray targets
= empty_strarray
;
1528 if (!source
->flags
|| find_include_file( strmake( "%s.h", obj
)))
1529 source
->flags
|= FLAG_IDL_HEADER
;
1531 for (i
= 0; i
< sizeof(idl_outputs
) / sizeof(idl_outputs
[0]); i
++)
1533 if (!(source
->flags
& idl_outputs
[i
].flag
)) continue;
1534 dest
= strmake( "%s%s", obj
, idl_outputs
[i
].ext
);
1535 if (!find_src_file( dest
)) strarray_add( &clean_files
, dest
);
1536 strarray_add( &targets
, dest
);
1538 if (source
->flags
& FLAG_IDL_PROXY
) strarray_add( &dlldata_files
, source
->name
);
1539 output_filenames( targets
);
1540 output( ": %s\n", tools_path( "widl" ));
1541 output( "\t%s -o $@ %s", tools_path( "widl" ), source
->filename
);
1542 output_filenames( targetflags
);
1543 output_filenames( includes
);
1544 output_filenames( define_args
);
1545 output_filenames( extradefs
);
1546 output_filenames( get_expanded_make_var_array( "EXTRAIDLFLAGS" ));
1548 output_filenames( targets
);
1549 output( ": %s", source
->filename
);
1551 else if (!strcmp( ext
, "in" )) /* .in file or man page */
1553 if (strendswith( obj
, ".man" ) && source
->sourcename
)
1555 char *dir
, *dest
= replace_extension( obj
, ".man", "" );
1556 char *lang
= strchr( dest
, '.' );
1560 dir
= strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang
, source
->sourcename
);
1562 else dir
= strmake( "$(DESTDIR)$(mandir)/man%s", source
->sourcename
);
1563 output( "install-man-pages:: %s\n", obj
);
1564 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n",
1565 obj
, dir
, dest
, source
->sourcename
);
1566 output( "uninstall::\n" );
1567 output( "\t$(RM) %s/%s.%s\n",
1568 dir
, dest
, source
->sourcename
);
1571 strarray_add( &all_targets
, xstrdup(obj
) );
1572 strarray_add_uniq( &phony_targets
, "install-man-pages" );
1574 else strarray_add( &clean_files
, xstrdup(obj
) );
1575 output( "%s: %s\n", obj
, source
->filename
);
1576 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source
->filename
);
1577 output( "%s:", obj
);
1579 else if (!strcmp( ext
, "sfd" )) /* font file */
1581 char *fontforge
= get_expanded_make_variable( "FONTFORGE" );
1582 if (fontforge
&& !src_dir
)
1584 output( "%s.ttf: %s\n", obj
, source
->filename
);
1585 output( "\t%s -script %s %s $@\n",
1586 fontforge
, top_dir_path( "fonts/genttf.ff" ), source
->filename
);
1589 continue; /* no dependencies */
1591 else if (!strcmp( ext
, "svg" )) /* svg file */
1593 char *convert
= get_expanded_make_variable( "CONVERT" );
1594 char *rsvg
= get_expanded_make_variable( "RSVG" );
1595 char *icotool
= get_expanded_make_variable( "ICOTOOL" );
1596 if (convert
&& rsvg
&& icotool
&& !src_dir
)
1598 output( "%s.ico %s.bmp: %s\n", obj
, obj
, source
->filename
);
1599 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n",
1600 convert
, icotool
, rsvg
, top_dir_path( "tools/buildimage" ), source
->filename
);
1605 continue; /* no dependencies */
1607 else if (!strcmp( ext
, "res" ))
1609 strarray_add( &res_files
, source
->name
);
1610 continue; /* no dependencies */
1614 int need_cross
= testdll
|| (source
->flags
& FLAG_C_IMPLIB
) || (module
&& staticlib
);
1616 if (source
->flags
& FLAG_GENERATED
) strarray_add( &clean_files
, source
->filename
);
1617 if (source
->flags
& FLAG_C_IMPLIB
) strarray_add( &implib_objs
, strmake( "%s.o", obj
));
1618 strarray_add( &object_files
, strmake( "%s.o", obj
));
1619 output( "%s.o: %s\n", obj
, source
->filename
);
1620 output( "\t$(CC) -c -o $@ %s", source
->filename
);
1621 output_filenames( includes
);
1622 output_filenames( define_args
);
1623 output_filenames( extradefs
);
1624 if (module
|| staticlib
|| testdll
) output_filenames( dllflags
);
1625 output_filenames( get_expanded_make_var_array( "EXTRACFLAGS" ));
1626 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1627 output_filename( "$(CFLAGS)" );
1629 if (crosstarget
&& need_cross
)
1631 strarray_add( &crossobj_files
, strmake( "%s.cross.o", obj
));
1632 output( "%s.cross.o: %s\n", obj
, source
->filename
);
1633 output( "\t$(CROSSCC) -c -o $@ %s", source
->filename
);
1634 output_filenames( includes
);
1635 output_filenames( define_args
);
1636 output_filenames( extradefs
);
1637 output_filename( "-DWINE_CROSSTEST" );
1638 output_filenames( get_expanded_make_var_array( "CPPFLAGS" ));
1639 output_filename( "$(CFLAGS)" );
1642 if (testdll
&& !strcmp( ext
, "c" ) && !(source
->flags
& FLAG_GENERATED
))
1644 strarray_add( &test_files
, source
->name
);
1645 output( "%s.ok:\n", obj
);
1646 output( "\t%s $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n",
1647 top_dir_path( "tools/runtest" ), top_obj_dir
,
1648 testdll
, replace_extension( testdll
, ".dll", "_test.exe" ), dllext
, obj
);
1650 if (!strcmp( ext
, "c" ) && !(source
->flags
& FLAG_GENERATED
))
1651 strarray_add( &c2man_files
, source
->filename
);
1652 output( "%s.o", obj
);
1653 if (crosstarget
&& need_cross
) output( " %s.cross.o", obj
);
1658 for (i
= 0; i
< source
->files_count
; i
++) output_include( source
->files
[i
], source
);
1662 /* rules for files that depend on multiple sources */
1666 output( "rsrc.pot: %s", tools_path( "wrc" ) );
1667 output_filenames( po_files
);
1669 output( "\t%s -O pot -o $@", tools_path( "wrc" ));
1670 output_filenames( po_files
);
1671 if (is_win16
) output_filename( "-m16" );
1672 else output_filenames( targetflags
);
1673 output_filename( "--nostdinc" );
1674 output_filenames( includes
);
1675 output_filenames( define_args
);
1677 strarray_add( &clean_files
, "rsrc.pot" );
1682 output( "msg.pot: %s", tools_path( "wmc" ));
1683 output_filenames( mc_files
);
1685 output( "\t%s -O pot -o $@", tools_path( "wmc" ));
1686 output_filenames( mc_files
);
1688 strarray_add( &clean_files
, "msg.pot" );
1691 if (dlldata_files
.count
)
1693 output( "dlldata.c: %s %s\n", tools_path( "widl" ), src_dir_path( "Makefile.in" ));
1694 output( "\t%s --dlldata-only -o $@", tools_path( "widl" ));
1695 output_filenames( dlldata_files
);
1699 if (module
&& !staticlib
)
1701 char *importlib
= get_expanded_make_variable( "IMPORTLIB" );
1702 struct strarray all_libs
= empty_strarray
;
1703 char *spec_file
= NULL
;
1705 if (!appmode
.count
) spec_file
= src_dir_path( replace_extension( module
, ".dll", ".spec" ));
1706 for (i
= 0; i
< delayimports
.count
; i
++)
1707 strarray_add( &all_libs
, strmake( "-l%s", delayimports
.str
[i
] ));
1708 for (i
= 0; i
< imports
.count
; i
++)
1709 strarray_add( &all_libs
, strmake( "-l%s", imports
.str
[i
] ));
1710 for (i
= 0; i
< delayimports
.count
; i
++)
1711 strarray_add( &all_libs
, strmake( "-Wb,-d%s", delayimports
.str
[i
] ));
1712 strarray_add( &all_libs
, "-lwine" );
1713 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBPORT" ));
1714 strarray_addall( &all_libs
, get_expanded_make_var_array( "EXTRALIBS" ));
1715 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBS" ));
1719 strarray_add( &all_targets
, strmake( "%s%s", module
, dllext
));
1720 strarray_add( &all_targets
, strmake( "%s.fake", module
));
1721 output( "%s%s %s.fake:", module
, dllext
, module
);
1725 strarray_add( &all_targets
, module
);
1726 output( "%s:", module
);
1728 if (spec_file
) output_filename( spec_file
);
1729 output_filenames( object_files
);
1730 output_filenames( res_files
);
1732 output( "\t%s -o $@", tools_path( "winegcc" ));
1733 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1734 if (tools_dir
) output_filename( strmake( "--sysroot=%s", top_obj_dir
));
1735 output_filenames( targetflags
);
1736 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1739 output( " -shared %s", spec_file
);
1740 output_filenames( extradllflags
);
1742 else output_filenames( appmode
);
1743 output_filenames( object_files
);
1744 output_filenames( res_files
);
1745 output_filenames( all_libs
);
1746 output_filename( "$(LDFLAGS)" );
1749 if (spec_file
&& importlib
)
1753 strarray_add( &clean_files
, strmake( "lib%s.def", importlib
));
1754 output( "lib%s.def: %s %s\n", importlib
, tools_path( "winebuild" ), spec_file
);
1755 output( "\t%s -w --def -o $@ --export %s", tools_path( "winebuild" ), spec_file
);
1756 output_filenames( targetflags
);
1757 if (is_win16
) output_filename( "-m16" );
1759 if (implib_objs
.count
)
1761 strarray_add( &clean_files
, strmake( "lib%s.def.a", importlib
));
1762 output( "lib%s.def.a:", importlib
);
1763 output_filenames( implib_objs
);
1765 output( "\t$(RM) $@\n" );
1766 output( "\t$(AR) $(ARFLAGS) $@" );
1767 output_filenames( implib_objs
);
1769 output( "\t$(RANLIB) $@\n" );
1774 strarray_add( &clean_files
, strmake( "lib%s.a", importlib
));
1775 output( "lib%s.a: %s %s", importlib
, tools_path( "winebuild" ), spec_file
);
1776 output_filenames( implib_objs
);
1778 output( "\t%s -w --implib -o $@ --export %s", tools_path( "winebuild" ), spec_file
);
1779 output_filenames( targetflags
);
1780 output_filenames( implib_objs
);
1783 if (crosstarget
&& !is_win16
)
1785 struct strarray cross_files
= strarray_replace_extension( &implib_objs
, ".o", ".cross.o" );
1786 strarray_add( &clean_files
, strmake( "lib%s.cross.a", importlib
));
1787 output( "lib%s.cross.a: %s %s", importlib
, tools_path( "winebuild" ), spec_file
);
1788 output_filenames( cross_files
);
1790 output( "\t%s -b %s -w --implib -o $@ --export %s",
1791 tools_path( "winebuild" ), crosstarget
, spec_file
);
1792 output_filenames( cross_files
);
1799 if (c2man_files
.count
&& top_obj_dir
)
1801 char *manext
= get_expanded_make_variable( "api_manext" );
1803 output( "manpages::\n" );
1804 output( "\t%s -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file
, top_obj_dir
);
1805 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1806 output_filename( strmake( "-o %s/documentation/man%s", top_obj_dir
, manext
? manext
: "3w" ));
1807 output_filenames( c2man_files
);
1809 output( "htmlpages::\n" );
1810 output( "\t%s -Th -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file
, top_obj_dir
);
1811 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1812 output_filename( strmake( "-o %s/documentation/html", top_obj_dir
));
1813 output_filenames( c2man_files
);
1815 output( "sgmlpages::\n" );
1816 output( "\t%s -Ts -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file
, top_obj_dir
);
1817 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1818 output_filename( strmake( "-o %s/documentation/api-guide", top_obj_dir
));
1819 output_filenames( c2man_files
);
1821 output( "xmlpages::\n" );
1822 output( "\t%s -Tx -w %s -R%s", top_dir_path( "tools/c2man.pl" ), spec_file
, top_obj_dir
);
1823 output_filename( strmake( "-I%s", top_dir_path( "include" )));
1824 output_filename( strmake( "-o %s/documentation/api-guide-xml", top_obj_dir
));
1825 output_filenames( c2man_files
);
1827 strarray_add( &phony_targets
, "manpages" );
1828 strarray_add( &phony_targets
, "htmlpages" );
1829 strarray_add( &phony_targets
, "sgmlpages" );
1830 strarray_add( &phony_targets
, "xmlpages" );
1832 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1838 strarray_add( &all_targets
, staticlib
);
1839 output( "%s:", staticlib
);
1840 output_filenames( object_files
);
1841 output( "\n\t$(RM) $@\n" );
1842 output( "\t$(AR) $(ARFLAGS) $@" );
1843 output_filenames( object_files
);
1844 output( "\n\t$(RANLIB) $@\n" );
1845 if (crosstarget
&& module
)
1847 char *name
= replace_extension( staticlib
, ".a", ".cross.a" );
1849 strarray_add( &all_targets
, name
);
1850 output( "%s:", name
);
1851 output_filenames( crossobj_files
);
1852 output( "\n\t$(RM) $@\n" );
1853 output( "\t%s-ar $(ARFLAGS) $@", crosstarget
);
1854 output_filenames( crossobj_files
);
1855 output( "\n\t%s-ranlib $@\n", crosstarget
);
1861 struct strarray ok_files
= strarray_replace_extension( &test_files
, ".c", ".ok" );
1862 char *testmodule
= replace_extension( testdll
, ".dll", "_test.exe" );
1863 char *stripped
= replace_extension( testdll
, ".dll", "_test-stripped.exe" );
1864 struct strarray all_libs
= empty_strarray
;
1866 for (i
= 0; i
< imports
.count
; i
++) strarray_add( &all_libs
, strmake( "-l%s", imports
.str
[i
] ));
1867 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBS" ));
1869 strarray_add( &all_targets
, strmake( "%s%s", testmodule
, dllext
));
1870 strarray_add( &clean_files
, strmake( "%s%s", stripped
, dllext
));
1871 output( "%s%s:\n", testmodule
, dllext
);
1872 output( "\t%s -o $@", tools_path( "winegcc" ));
1873 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1874 if (tools_dir
) output_filename( strmake( "--sysroot=%s", top_obj_dir
));
1875 output_filenames( targetflags
);
1876 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1877 output_filenames( appmode
);
1878 output_filenames( object_files
);
1879 output_filenames( res_files
);
1880 output_filenames( all_libs
);
1881 output_filename( "$(LDFLAGS)" );
1883 output( "%s%s:\n", stripped
, dllext
);
1884 output( "\t%s -o $@", tools_path( "winegcc" ));
1885 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1886 if (tools_dir
) output_filename( strmake( "--sysroot=%s", top_obj_dir
));
1887 output_filenames( targetflags
);
1888 output_filenames( get_expanded_make_var_array( "UNWINDFLAGS" ));
1889 output_filename( strmake( "-Wb,-F,%s", testmodule
));
1890 output_filenames( appmode
);
1891 output_filenames( object_files
);
1892 output_filenames( res_files
);
1893 output_filenames( all_libs
);
1894 output_filename( "$(LDFLAGS)" );
1896 output( "%s%s %s%s:", testmodule
, dllext
, stripped
, dllext
);
1897 output_filenames( object_files
);
1898 output_filenames( res_files
);
1903 char *testres
= replace_extension( testdll
, ".dll", "_test.res" );
1904 output( "all: %s/%s\n", top_obj_dir_path( "programs/winetest" ), testres
);
1905 output( "%s/%s: %s%s\n", top_obj_dir_path( "programs/winetest" ), testres
, stripped
, dllext
);
1906 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -o $@\n",
1907 testmodule
, stripped
, dllext
, tools_path( "wrc" ));
1912 char *crosstest
= replace_extension( testdll
, ".dll", "_crosstest.exe" );
1914 strarray_add( &clean_files
, crosstest
);
1915 output( "crosstest: %s\n", crosstest
);
1916 output( "%s:", crosstest
);
1917 output_filenames( crossobj_files
);
1918 output_filenames( res_files
);
1920 output( "\t%s -o $@ -b %s", tools_path( "winegcc" ), crosstarget
);
1921 output_filename( strmake( "-B%s", tools_dir_path( "winebuild" )));
1922 if (tools_dir
) output_filename( strmake( "--sysroot=%s", top_obj_dir
));
1923 output_filename( "--lib-suffix=.cross.a" );
1924 output_filenames( crossobj_files
);
1925 output_filenames( res_files
);
1926 output_filenames( all_libs
);
1927 output_filename( "$(LDFLAGS)" );
1929 strarray_add( &phony_targets
, "crosstest" );
1932 output( "testlist.c: %s%s %s\n",
1933 tools_dir_path( "make_ctests" ), tools_ext
, src_dir_path( "Makefile.in" ));
1934 output( "\t%s%s -o $@", tools_dir_path( "make_ctests" ), tools_ext
);
1935 output_filenames( test_files
);
1937 output_filenames( ok_files
);
1938 output( ": %s%s ../%s%s\n", testmodule
, dllext
, testdll
, dllext
);
1939 output( "check test:" );
1940 output_filenames( ok_files
);
1942 output( "testclean::\n" );
1943 output( "\t$(RM)" );
1944 output_filenames( ok_files
);
1946 strarray_addall( &clean_files
, ok_files
);
1947 strarray_add( &phony_targets
, "check" );
1948 strarray_add( &phony_targets
, "test" );
1949 strarray_add( &phony_targets
, "testclean" );
1952 if (all_targets
.count
)
1955 output_filenames( all_targets
);
1959 strarray_addall( &clean_files
, object_files
);
1960 strarray_addall( &clean_files
, crossobj_files
);
1961 strarray_addall( &clean_files
, res_files
);
1962 strarray_addall( &clean_files
, all_targets
);
1963 strarray_addall( &clean_files
, get_expanded_make_var_array( "EXTRA_TARGETS" ));
1965 if (clean_files
.count
)
1967 output( "clean::\n" );
1968 output( "\t$(RM)" );
1969 output_filenames( clean_files
);
1971 strarray_add( &phony_targets
, "clean" );
1976 output( "depend:\n" );
1977 output( "\t@cd %s && $(MAKE) %s/depend\n", top_obj_dir
, base_dir
);
1978 strarray_add( &phony_targets
, "depend" );
1981 if (phony_targets
.count
)
1983 output( ".PHONY:" );
1984 output_filenames( phony_targets
);
1988 for (i
= 0; i
< subdirs
.count
; i
++) create_dir( subdirs
.str
[i
] );
1994 /*******************************************************************
1997 static FILE *create_temp_file( const char *orig
)
1999 char *name
= xmalloc( strlen(orig
) + 13 );
2000 unsigned int i
, id
= getpid();
2004 for (i
= 0; i
< 100; i
++)
2006 sprintf( name
, "%s.tmp%08x", orig
, id
);
2007 if ((fd
= open( name
, O_RDWR
| O_CREAT
| O_EXCL
, 0666 )) != -1)
2009 ret
= fdopen( fd
, "w" );
2012 if (errno
!= EEXIST
) break;
2015 if (!ret
) fatal_error( "failed to create output file for '%s'\n", orig
);
2016 temp_file_name
= name
;
2021 /*******************************************************************
2024 static void rename_temp_file( const char *dest
)
2026 int ret
= rename( temp_file_name
, dest
);
2027 if (ret
== -1 && errno
== EEXIST
)
2029 /* rename doesn't overwrite on windows */
2031 ret
= rename( temp_file_name
, dest
);
2033 if (ret
== -1) fatal_error( "failed to rename output file to '%s'\n", dest
);
2034 temp_file_name
= NULL
;
2038 /*******************************************************************
2041 static void output_gitignore( const char *dest
, const struct strarray
*files
)
2045 output_file
= create_temp_file( dest
);
2047 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2048 output( "/.gitignore\n" );
2049 output( "/Makefile\n" );
2050 for (i
= 0; i
< files
->count
; i
++)
2052 if (!strchr( files
->str
[i
], '/' )) output( "/" );
2053 output( "%s\n", files
->str
[i
] );
2056 if (fclose( output_file
)) fatal_perror( "write" );
2058 rename_temp_file( dest
);
2062 /*******************************************************************
2063 * output_dependencies
2065 static void output_dependencies( const char *path
)
2067 struct strarray targets
= empty_strarray
;
2069 if (Separator
&& ((output_file
= fopen( path
, "r" ))))
2072 FILE *tmp_file
= create_temp_file( path
);
2075 while (fgets( buffer
, sizeof(buffer
), output_file
) && !found
)
2077 if (fwrite( buffer
, 1, strlen(buffer
), tmp_file
) != strlen(buffer
)) fatal_perror( "write" );
2078 found
= !strncmp( buffer
, Separator
, strlen(Separator
) );
2080 if (fclose( output_file
)) fatal_perror( "write" );
2081 output_file
= tmp_file
;
2082 if (!found
) output( "\n%s\n", Separator
);
2086 if (!(output_file
= fopen( path
, Separator
? "a" : "w" )))
2087 fatal_perror( "%s", path
);
2090 targets
= output_sources();
2092 fclose( output_file
);
2094 if (temp_file_name
) rename_temp_file( path
);
2096 if (!src_dir
) output_gitignore( strmake( "%s/.gitignore", base_dir
), &targets
);
2100 /*******************************************************************
2103 static void update_makefile( const char *path
)
2105 static const char *source_vars
[] =
2124 struct strarray value
;
2125 struct incl_file
*file
;
2128 output_file_name
= strmake( "%s/%s", base_dir
, makefile_name
);
2131 src_dir
= get_expanded_make_variable( "srcdir" );
2132 top_src_dir
= get_expanded_make_variable( "top_srcdir" );
2133 top_obj_dir
= get_expanded_make_variable( "top_builddir" );
2134 parent_dir
= get_expanded_make_variable( "PARENTSRC" );
2135 tools_dir
= get_expanded_make_variable( "TOOLSDIR" );
2136 tools_ext
= get_expanded_make_variable( "TOOLSEXT" );
2138 /* ignore redundant source paths */
2139 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
2140 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
2141 if (tools_dir
&& top_obj_dir
&& !strcmp( tools_dir
, top_obj_dir
)) tools_dir
= NULL
;
2143 appmode
= get_expanded_make_var_array( "APPMODE" );
2144 dllflags
= get_expanded_make_var_array( "DLLFLAGS" );
2145 imports
= get_expanded_make_var_array( "IMPORTS" );
2147 for (i
= 0; i
< appmode
.count
&& !use_msvcrt
; i
++)
2148 use_msvcrt
= !strcmp( appmode
.str
[i
], "-mno-cygwin" );
2149 for (i
= 0; i
< imports
.count
&& !use_msvcrt
; i
++)
2150 use_msvcrt
= !strncmp( imports
.str
[i
], "msvcr", 5 );
2152 include_args
= empty_strarray
;
2153 define_args
= empty_strarray
;
2154 strarray_add( &define_args
, "-D__WINESRC__" );
2155 strarray_add( &include_args
, strmake( "-I%s", top_dir_path( "include" )));
2157 if (!tools_ext
) tools_ext
= "";
2159 value
= get_expanded_make_var_array( "EXTRAINCL" );
2160 for (i
= 0; i
< value
.count
; i
++)
2161 if (!strncmp( value
.str
[i
], "-I", 2 ))
2162 strarray_add_uniq( &include_args
, value
.str
[i
] );
2164 strarray_add_uniq( &define_args
, value
.str
[i
] );
2165 strarray_addall( &define_args
, get_expanded_make_var_array( "EXTRADEFS" ));
2169 strarray_add( &dllflags
, get_expanded_make_variable( "MSVCRTFLAGS" ));
2170 strarray_add( &include_args
, strmake( "-I%s", top_dir_path( "include/msvcrt" )));
2173 list_init( &sources
);
2174 list_init( &includes
);
2176 for (var
= source_vars
; *var
; var
++)
2178 value
= get_expanded_make_var_array( *var
);
2179 for (i
= 0; i
< value
.count
; i
++) add_src_file( value
.str
[i
] );
2182 add_generated_sources();
2184 value
= get_expanded_make_var_array( "EXTRA_OBJS" );
2185 for (i
= 0; i
< value
.count
; i
++)
2187 /* default to .c for unknown extra object files */
2188 if (strendswith( value
.str
[i
], ".o" ))
2189 add_generated_source( value
.str
[i
], replace_extension( value
.str
[i
], ".o", ".c" ) );
2191 add_generated_source( value
.str
[i
], NULL
);
2194 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
) parse_file( file
, 0 );
2195 output_dependencies( output_file_name
);
2196 output_file_name
= NULL
;
2200 /*******************************************************************
2203 static void parse_makeflags( const char *flags
)
2205 const char *p
= flags
;
2206 char *var
, *buffer
= xmalloc( strlen(flags
) + 1 );
2210 while (isspace(*p
)) p
++;
2212 while (*p
&& !isspace(*p
))
2214 if (*p
== '\\' && p
[1]) p
++;
2218 if (var
> buffer
) set_make_variable( &cmdline_vars
, buffer
);
2223 /*******************************************************************
2226 static int parse_option( const char *opt
)
2230 if (strchr( opt
, '=' )) return set_make_variable( &cmdline_vars
, opt
);
2236 if (opt
[2]) makefile_name
= opt
+ 2;
2239 relative_dir_mode
= 1;
2242 if (opt
[2]) Separator
= opt
+ 2;
2243 else Separator
= NULL
;
2246 fprintf( stderr
, "Unknown option '%s'\n%s", opt
, Usage
);
2253 /*******************************************************************
2256 int main( int argc
, char *argv
[] )
2258 const char *makeflags
= getenv( "MAKEFLAGS" );
2261 if (makeflags
) parse_makeflags( makeflags
);
2266 if (parse_option( argv
[i
] ))
2268 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
2274 if (relative_dir_mode
)
2280 fprintf( stderr
, "Option -r needs two directories\n%s", Usage
);
2283 relpath
= get_relative_path( argv
[1], argv
[2] );
2284 printf( "%s\n", relpath
? relpath
: "." );
2290 fprintf( stderr
, "%s", Usage
);
2293 atexit( cleanup_files
);
2294 signal( SIGTERM
, exit_on_signal
);
2295 signal( SIGINT
, exit_on_signal
);
2297 signal( SIGHUP
, exit_on_signal
);
2300 for (i
= 1; i
< argc
; i
++) update_makefile( argv
[i
] );