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 make_vars
;
96 static struct strarray cmdline_vars
;
98 static const char *base_dir
= ".";
99 static const char *src_dir
;
100 static const char *top_src_dir
;
101 static const char *top_obj_dir
;
102 static const char *parent_dir
;
103 static const char *makefile_name
= "Makefile";
104 static const char *Separator
= "### Dependencies";
105 static const char *input_file_name
;
106 static const char *output_file_name
;
107 static const char *temp_file_name
;
108 static int parse_makefile_mode
;
109 static int relative_dir_mode
;
110 static int input_line
;
111 static int output_column
;
112 static FILE *output_file
;
114 static const char Usage
[] =
115 "Usage: makedep [options] [files]\n"
117 " -Idir Search for include files in directory 'dir'\n"
118 " -Cdir Search for source files in directory 'dir'\n"
119 " -Sdir Set the top source directory\n"
120 " -Tdir Set the top object directory\n"
121 " -Pdir Set the parent source directory\n"
122 " -M dirs Parse the makefiles from the specified directories\n"
123 " -R from to Compute the relative path between two directories\n"
124 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
125 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
129 #define __attribute__(x)
132 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
133 static void fatal_perror( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
134 static void output( const char *format
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
135 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
137 /*******************************************************************
140 static void fatal_error( const char *msg
, ... )
143 va_start( valist
, msg
);
146 fprintf( stderr
, "%s:", input_file_name
);
147 if (input_line
) fprintf( stderr
, "%d:", input_line
);
148 fprintf( stderr
, " error: " );
150 else fprintf( stderr
, "makedep: error: " );
151 vfprintf( stderr
, msg
, valist
);
157 /*******************************************************************
160 static void fatal_perror( const char *msg
, ... )
163 va_start( valist
, msg
);
166 fprintf( stderr
, "%s:", input_file_name
);
167 if (input_line
) fprintf( stderr
, "%d:", input_line
);
168 fprintf( stderr
, " error: " );
170 else fprintf( stderr
, "makedep: error: " );
171 vfprintf( stderr
, msg
, valist
);
178 /*******************************************************************
181 static void cleanup_files(void)
183 if (temp_file_name
) unlink( temp_file_name
);
184 if (output_file_name
) unlink( output_file_name
);
188 /*******************************************************************
191 static void exit_on_signal( int sig
)
193 exit( 1 ); /* this will call the atexit functions */
197 /*******************************************************************
200 static void *xmalloc( size_t size
)
203 if (!(res
= malloc (size
? size
: 1)))
204 fatal_error( "Virtual memory exhausted.\n" );
209 /*******************************************************************
212 static void *xrealloc (void *ptr
, size_t size
)
216 if (!(res
= realloc( ptr
, size
)))
217 fatal_error( "Virtual memory exhausted.\n" );
221 /*******************************************************************
224 static char *xstrdup( const char *str
)
226 char *res
= strdup( str
);
227 if (!res
) fatal_error( "Virtual memory exhausted.\n" );
232 /*******************************************************************
235 static char *strmake( const char* fmt
, ... )
243 char *p
= xmalloc (size
);
245 n
= vsnprintf (p
, size
, fmt
, ap
);
247 if (n
== -1) size
*= 2;
248 else if ((size_t)n
>= size
) size
= n
+ 1;
255 /*******************************************************************
258 static int strendswith( const char* str
, const char* end
)
263 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
267 /*******************************************************************
270 static void output( const char *format
, ... )
275 va_start( valist
, format
);
276 ret
= vfprintf( output_file
, format
, valist
);
278 if (ret
< 0) fatal_perror( "output" );
279 if (format
[0] && format
[strlen(format
) - 1] == '\n') output_column
= 0;
280 else output_column
+= ret
;
284 /*******************************************************************
287 static void strarray_add( struct strarray
*array
, const char *str
)
289 if (array
->count
== array
->size
)
291 if (array
->size
) array
->size
*= 2;
292 else array
->size
= 16;
293 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
295 array
->str
[array
->count
++] = str
;
299 /*******************************************************************
302 static void strarray_addall( struct strarray
*array
, struct strarray added
)
306 for (i
= 0; i
< added
.count
; i
++) strarray_add( array
, added
.str
[i
] );
310 /*******************************************************************
313 static void strarray_insert( struct strarray
*array
, unsigned int pos
, const char *str
)
317 strarray_add( array
, NULL
);
318 for (i
= array
->count
- 1; i
> pos
; i
--) array
->str
[i
] = array
->str
[i
- 1];
319 array
->str
[pos
] = str
;
323 /*******************************************************************
326 static void strarray_add_uniq( struct strarray
*array
, const char *str
)
330 for (i
= 0; i
< array
->count
; i
++) if (!strcmp( array
->str
[i
], str
)) return;
331 strarray_add( array
, str
);
335 /*******************************************************************
338 static void output_filename( const char *name
)
340 if (output_column
+ strlen(name
) + 1 > 100)
345 else if (output_column
) output( " " );
346 output( "%s", name
);
350 /*******************************************************************
353 static void output_filenames( struct strarray array
)
357 for (i
= 0; i
< array
.count
; i
++) output_filename( array
.str
[i
] );
361 /*******************************************************************
364 static char *get_extension( char *filename
)
366 char *ext
= strrchr( filename
, '.' );
367 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
372 /*******************************************************************
375 static char *replace_extension( const char *name
, const char *old_ext
, const char *new_ext
)
378 int name_len
= strlen( name
);
379 int ext_len
= strlen( old_ext
);
381 if (name_len
>= ext_len
&& !strcmp( name
+ name_len
- ext_len
, old_ext
)) name_len
-= ext_len
;
382 ret
= xmalloc( name_len
+ strlen( new_ext
) + 1 );
383 memcpy( ret
, name
, name_len
);
384 strcpy( ret
+ name_len
, new_ext
);
389 /*******************************************************************
390 * strarray_replace_extension
392 static struct strarray
strarray_replace_extension( const struct strarray
*array
,
393 const char *old_ext
, const char *new_ext
)
398 ret
.count
= ret
.size
= array
->count
;
399 ret
.str
= xmalloc( sizeof(ret
.str
[0]) * ret
.size
);
400 for (i
= 0; i
< array
->count
; i
++) ret
.str
[i
] = replace_extension( array
->str
[i
], old_ext
, new_ext
);
405 /*******************************************************************
408 static char *replace_substr( const char *str
, const char *start
, unsigned int len
, const char *replace
)
410 unsigned int pos
= start
- str
;
411 char *ret
= xmalloc( pos
+ strlen(replace
) + strlen(start
+ len
) + 1 );
412 memcpy( ret
, str
, pos
);
413 strcpy( ret
+ pos
, replace
);
414 strcat( ret
+ pos
, start
+ len
);
419 /*******************************************************************
422 * Determine where the destination path is located relative to the 'from' path.
424 static char *get_relative_path( const char *from
, const char *dest
)
428 unsigned int dotdots
= 0;
430 /* a path of "." is equivalent to an empty path */
431 if (!strcmp( from
, "." )) from
= "";
435 while (*from
== '/') from
++;
436 while (*dest
== '/') dest
++;
437 start
= dest
; /* save start of next path element */
440 while (*from
&& *from
!= '/' && *from
== *dest
) { from
++; dest
++; }
441 if ((!*from
|| *from
== '/') && (!*dest
|| *dest
== '/')) continue;
443 /* count remaining elements in 'from' */
447 while (*from
&& *from
!= '/') from
++;
448 while (*from
== '/') from
++;
454 if (!start
[0] && !dotdots
) return NULL
; /* empty path */
456 ret
= xmalloc( 3 * dotdots
+ strlen( start
) + 1 );
457 for (p
= ret
; dotdots
; dotdots
--, p
+= 3) memcpy( p
, "../", 3 );
459 if (start
[0]) strcpy( p
, start
);
460 else p
[-1] = 0; /* remove trailing slash */
465 /*******************************************************************
468 static void init_paths(void)
470 /* ignore redundant source paths */
471 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
472 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
474 if (top_src_dir
) strarray_insert( &include_args
, 0, strmake( "-I%s/include", top_src_dir
));
475 else if (top_obj_dir
) strarray_insert( &include_args
, 0, strmake( "-I%s/include", top_obj_dir
));
479 /*******************************************************************
482 static char *get_line( FILE *file
)
485 static unsigned int size
;
490 buffer
= xmalloc( size
);
492 if (!fgets( buffer
, size
, file
)) return NULL
;
497 char *p
= buffer
+ strlen(buffer
);
498 /* if line is larger than buffer, resize buffer */
499 while (p
== buffer
+ size
- 1 && p
[-1] != '\n')
501 buffer
= xrealloc( buffer
, size
* 2 );
502 if (!fgets( buffer
+ size
- 1, size
+ 1, file
)) break;
503 p
= buffer
+ strlen(buffer
);
506 if (p
> buffer
&& p
[-1] == '\n')
509 if (p
> buffer
&& p
[-1] == '\r') *(--p
) = 0;
510 if (p
> buffer
&& p
[-1] == '\\')
513 /* line ends in backslash, read continuation line */
514 if (!fgets( p
, size
- (p
- buffer
), file
)) return buffer
;
523 /*******************************************************************
526 static struct incl_file
*find_src_file( const char *name
)
528 struct incl_file
*file
;
530 LIST_FOR_EACH_ENTRY( file
, &sources
, struct incl_file
, entry
)
531 if (!strcmp( name
, file
->name
)) return file
;
535 /*******************************************************************
538 static struct incl_file
*find_include_file( const char *name
)
540 struct incl_file
*file
;
542 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
)
543 if (!strcmp( name
, file
->name
)) return file
;
547 /*******************************************************************
550 * Add an include file if it doesn't already exists.
552 static struct incl_file
*add_include( struct incl_file
*parent
, const char *name
, int system
)
554 struct incl_file
*include
;
557 if (parent
->files_count
>= parent
->files_size
)
559 parent
->files_size
*= 2;
560 if (parent
->files_size
< 16) parent
->files_size
= 16;
561 parent
->files
= xrealloc( parent
->files
, parent
->files_size
* sizeof(*parent
->files
) );
564 /* enforce some rules for the Wine tree */
566 if (!memcmp( name
, "../", 3 ))
567 fatal_error( "#include directive with relative path not allowed\n" );
569 if (!strcmp( name
, "config.h" ))
571 if ((ext
= strrchr( parent
->filename
, '.' )) && !strcmp( ext
, ".h" ))
572 fatal_error( "config.h must not be included by a header file\n" );
573 if (parent
->files_count
)
574 fatal_error( "config.h must be included before anything else\n" );
576 else if (!strcmp( name
, "wine/port.h" ))
578 if ((ext
= strrchr( parent
->filename
, '.' )) && !strcmp( ext
, ".h" ))
579 fatal_error( "wine/port.h must not be included by a header file\n" );
580 if (!parent
->files_count
) fatal_error( "config.h must be included before wine/port.h\n" );
581 if (parent
->files_count
> 1)
582 fatal_error( "wine/port.h must be included before everything except config.h\n" );
583 if (strcmp( parent
->files
[0]->name
, "config.h" ))
584 fatal_error( "config.h must be included before wine/port.h\n" );
587 LIST_FOR_EACH_ENTRY( include
, &includes
, struct incl_file
, entry
)
588 if (!strcmp( name
, include
->name
)) goto found
;
590 include
= xmalloc( sizeof(*include
) );
591 memset( include
, 0, sizeof(*include
) );
592 include
->name
= xstrdup(name
);
593 include
->included_by
= parent
;
594 include
->included_line
= input_line
;
595 if (system
) include
->flags
|= FLAG_SYSTEM
;
596 list_add_tail( &includes
, &include
->entry
);
598 parent
->files
[parent
->files_count
++] = include
;
603 /*******************************************************************
606 static FILE *open_file( const char *path
)
610 if (path
[0] != '/' && strcmp( base_dir
, "." ))
612 char *full_path
= strmake( "%s/%s", base_dir
, path
);
613 ret
= fopen( full_path
, "r" );
616 else ret
= fopen( path
, "r" );
621 /*******************************************************************
624 static FILE *open_src_file( struct incl_file
*pFile
)
628 /* first try name as is */
629 if ((file
= open_file( pFile
->name
)))
631 pFile
->filename
= xstrdup( pFile
->name
);
634 /* now try in source dir */
637 pFile
->filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
638 file
= open_file( pFile
->filename
);
640 /* now try parent dir */
641 if (!file
&& parent_dir
)
644 pFile
->filename
= strmake( "%s/%s/%s", src_dir
, parent_dir
, pFile
->name
);
646 pFile
->filename
= strmake( "%s/%s", parent_dir
, pFile
->name
);
647 file
= open_file( pFile
->filename
);
649 if (!file
) fatal_perror( "open %s", pFile
->name
);
654 /*******************************************************************
657 static FILE *open_include_file( struct incl_file
*pFile
)
665 /* check for generated bison header */
667 if (strendswith( pFile
->name
, ".tab.h" ))
669 filename
= replace_extension( pFile
->name
, ".tab.h", ".y" );
670 if (src_dir
) filename
= strmake( "%s/%s", src_dir
, filename
);
672 if ((file
= open_file( filename
)))
674 pFile
->sourcename
= filename
;
675 pFile
->filename
= xstrdup( pFile
->name
);
676 /* don't bother to parse it */
683 /* check for corresponding idl file in source dir */
685 if (strendswith( pFile
->name
, ".h" ))
687 filename
= replace_extension( pFile
->name
, ".h", ".idl" );
688 if (src_dir
) filename
= strmake( "%s/%s", src_dir
, filename
);
690 if ((file
= open_file( filename
)))
692 pFile
->sourcename
= filename
;
693 pFile
->filename
= xstrdup( pFile
->name
);
699 /* now try in source dir */
701 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
703 filename
= xstrdup( pFile
->name
);
704 if ((file
= open_file( filename
))) goto found
;
707 /* now try in parent source dir */
711 filename
= strmake( "%s/%s/%s", src_dir
, parent_dir
, pFile
->name
);
713 filename
= strmake( "%s/%s", parent_dir
, pFile
->name
);
714 if ((file
= open_file( filename
))) goto found
;
718 /* check for corresponding idl file in global includes */
720 if (strendswith( pFile
->name
, ".h" ))
722 filename
= replace_extension( pFile
->name
, ".h", ".idl" );
724 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
725 else if (top_obj_dir
)
726 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
730 if (filename
&& (file
= open_file( filename
)))
732 pFile
->sourcename
= filename
;
733 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
739 /* check for corresponding .in file in global includes (for config.h.in) */
741 if (strendswith( pFile
->name
, ".h" ))
743 filename
= replace_extension( pFile
->name
, ".h", ".h.in" );
745 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
746 else if (top_obj_dir
)
747 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
751 if (filename
&& (file
= open_file( filename
)))
753 pFile
->sourcename
= filename
;
754 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
760 /* check for corresponding .x file in global includes */
762 if (strendswith( pFile
->name
, "tmpl.h" ))
764 filename
= replace_extension( pFile
->name
, ".h", ".x" );
766 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
767 else if (top_obj_dir
)
768 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
772 if (filename
&& (file
= open_file( filename
)))
774 pFile
->sourcename
= filename
;
775 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
781 /* now search in include paths */
782 for (i
= 0; i
< include_args
.count
; i
++)
784 const char *dir
= include_args
.str
[i
] + 2; /* skip -I */
787 /* ignore absolute paths that don't point into the source dir */
788 if (!top_src_dir
) continue;
789 len
= strlen( top_src_dir
);
790 if (strncmp( dir
, top_src_dir
, len
)) continue;
791 if (dir
[len
] && dir
[len
] != '/') continue;
793 filename
= strmake( "%s/%s", dir
, pFile
->name
);
794 if ((file
= open_file( filename
))) goto found
;
797 if (pFile
->flags
& FLAG_SYSTEM
) return NULL
; /* ignore system files we cannot find */
799 /* try in src file directory */
800 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
802 int l
= p
- pFile
->included_by
->filename
+ 1;
803 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
804 memcpy( filename
, pFile
->included_by
->filename
, l
);
805 strcpy( filename
+ l
, pFile
->name
);
806 if ((file
= open_file( filename
))) goto found
;
810 fprintf( stderr
, "%s:%d: error: ", pFile
->included_by
->filename
, pFile
->included_line
);
811 perror( pFile
->name
);
812 pFile
= pFile
->included_by
;
813 while (pFile
&& pFile
->included_by
)
815 const char *parent
= pFile
->included_by
->sourcename
;
816 if (!parent
) parent
= pFile
->included_by
->name
;
817 fprintf( stderr
, "%s:%d: note: %s was first included here\n",
818 parent
, pFile
->included_line
, pFile
->name
);
819 pFile
= pFile
->included_by
;
824 pFile
->filename
= filename
;
829 /*******************************************************************
830 * parse_include_directive
832 static void parse_include_directive( struct incl_file
*source
, char *str
)
834 char quote
, *include
, *p
= str
;
836 while (*p
&& isspace(*p
)) p
++;
837 if (*p
!= '\"' && *p
!= '<' ) return;
839 if (quote
== '<') quote
= '>';
841 while (*p
&& (*p
!= quote
)) p
++;
842 if (!*p
) fatal_error( "malformed include directive '%s'\n", str
);
844 add_include( source
, include
, (quote
== '>') );
848 /*******************************************************************
849 * parse_pragma_directive
851 static void parse_pragma_directive( struct incl_file
*source
, char *str
)
853 char *flag
, *p
= str
;
855 if (!isspace( *p
)) return;
856 while (*p
&& isspace(*p
)) p
++;
857 p
= strtok( p
, " \t" );
858 if (strcmp( p
, "makedep" )) return;
860 while ((flag
= strtok( NULL
, " \t" )))
862 if (!strcmp( flag
, "depend" ))
864 while ((p
= strtok( NULL
, " \t" ))) add_include( source
, p
, 0 );
867 if (strendswith( source
->name
, ".idl" ))
869 if (!strcmp( flag
, "header" )) source
->flags
|= FLAG_IDL_HEADER
;
870 else if (!strcmp( flag
, "proxy" )) source
->flags
|= FLAG_IDL_PROXY
;
871 else if (!strcmp( flag
, "client" )) source
->flags
|= FLAG_IDL_CLIENT
;
872 else if (!strcmp( flag
, "server" )) source
->flags
|= FLAG_IDL_SERVER
;
873 else if (!strcmp( flag
, "ident" )) source
->flags
|= FLAG_IDL_IDENT
;
874 else if (!strcmp( flag
, "typelib" )) source
->flags
|= FLAG_IDL_TYPELIB
;
875 else if (!strcmp( flag
, "register" )) source
->flags
|= FLAG_IDL_REGISTER
;
876 else if (!strcmp( flag
, "regtypelib" )) source
->flags
|= FLAG_IDL_REGTYPELIB
;
878 else if (strendswith( source
->name
, ".rc" ))
880 if (!strcmp( flag
, "po" )) source
->flags
|= FLAG_RC_PO
;
882 else if (!strcmp( flag
, "implib" )) source
->flags
|= FLAG_C_IMPLIB
;
887 /*******************************************************************
888 * parse_cpp_directive
890 static void parse_cpp_directive( struct incl_file
*source
, char *str
)
892 while (*str
&& isspace(*str
)) str
++;
893 if (*str
++ != '#') return;
894 while (*str
&& isspace(*str
)) str
++;
896 if (!strncmp( str
, "include", 7 ))
897 parse_include_directive( source
, str
+ 7 );
898 else if (!strncmp( str
, "import", 6 ) && strendswith( source
->name
, ".m" ))
899 parse_include_directive( source
, str
+ 6 );
900 else if (!strncmp( str
, "pragma", 6 ))
901 parse_pragma_directive( source
, str
+ 6 );
905 /*******************************************************************
908 * If for_h_file is non-zero, it means we are not interested in the idl file
909 * itself, but only in the contents of the .h file that will be generated from it.
911 static void parse_idl_file( struct incl_file
*pFile
, FILE *file
, int for_h_file
)
913 char *buffer
, *include
;
918 /* generated .h file always includes these */
919 add_include( pFile
, "rpc.h", 1 );
920 add_include( pFile
, "rpcndr.h", 1 );
923 while ((buffer
= get_line( file
)))
927 while (*p
&& isspace(*p
)) p
++;
929 if (!strncmp( p
, "import", 6 ))
932 while (*p
&& isspace(*p
)) p
++;
933 if (*p
!= '"') continue;
935 while (*p
&& (*p
!= '"')) p
++;
936 if (!*p
) fatal_error( "malformed import directive\n" );
938 if (for_h_file
&& strendswith( include
, ".idl" )) strcpy( p
- 4, ".h" );
939 add_include( pFile
, include
, 0 );
943 if (for_h_file
) /* only check for #include inside cpp_quote */
945 if (strncmp( p
, "cpp_quote", 9 )) continue;
947 while (*p
&& isspace(*p
)) p
++;
948 if (*p
++ != '(') continue;
949 while (*p
&& isspace(*p
)) p
++;
950 if (*p
++ != '"') continue;
951 if (*p
++ != '#') continue;
952 while (*p
&& isspace(*p
)) p
++;
953 if (strncmp( p
, "include", 7 )) continue;
955 while (*p
&& isspace(*p
)) p
++;
956 if (*p
== '\\' && p
[1] == '"')
963 if (*p
++ != '<' ) continue;
967 while (*p
&& (*p
!= quote
)) p
++;
968 if (!*p
|| (quote
== '"' && p
[-1] != '\\'))
969 fatal_error( "malformed #include directive inside cpp_quote\n" );
970 if (quote
== '"') p
--; /* remove backslash */
972 add_include( pFile
, include
, (quote
== '>') );
976 parse_cpp_directive( pFile
, p
);
980 /*******************************************************************
983 static void parse_c_file( struct incl_file
*pFile
, FILE *file
)
988 while ((buffer
= get_line( file
)))
990 parse_cpp_directive( pFile
, buffer
);
995 /*******************************************************************
998 static void parse_rc_file( struct incl_file
*pFile
, FILE *file
)
1000 char *buffer
, *include
;
1003 while ((buffer
= get_line( file
)))
1007 while (*p
&& isspace(*p
)) p
++;
1009 if (p
[0] == '/' && p
[1] == '*') /* check for magic makedep comment */
1012 while (*p
&& isspace(*p
)) p
++;
1013 if (strncmp( p
, "@makedep:", 9 )) continue;
1015 while (*p
&& isspace(*p
)) p
++;
1020 while (*p
&& *p
!= quote
) p
++;
1025 while (*p
&& !isspace(*p
) && *p
!= '*') p
++;
1028 fatal_error( "malformed makedep comment\n" );
1030 add_include( pFile
, include
, (quote
== '>') );
1034 parse_cpp_directive( pFile
, buffer
);
1039 /*******************************************************************
1042 static void parse_in_file( struct incl_file
*source
, FILE *file
)
1046 /* make sure it gets rebuilt when the version changes */
1047 add_include( source
, "config.h", 1 );
1049 if (!strendswith( source
->filename
, ".man.in" )) return; /* not a man page */
1052 while ((buffer
= get_line( file
)))
1054 if (strncmp( buffer
, ".TH", 3 )) continue;
1055 if (!(p
= strtok( buffer
, " \t" ))) continue; /* .TH */
1056 if (!(p
= strtok( NULL
, " \t" ))) continue; /* program name */
1057 if (!(p
= strtok( NULL
, " \t" ))) continue; /* man section */
1058 source
->sourcename
= xstrdup( p
); /* abuse source name to store section */
1064 /*******************************************************************
1067 static void parse_file( struct incl_file
*source
, int src
)
1071 /* don't try to open certain types of files */
1072 if (strendswith( source
->name
, ".tlb" ))
1074 source
->filename
= xstrdup( source
->name
);
1078 file
= src
? open_src_file( source
) : open_include_file( source
);
1080 input_file_name
= source
->filename
;
1082 if (source
->sourcename
&& strendswith( source
->sourcename
, ".idl" ))
1083 parse_idl_file( source
, file
, 1 );
1084 else if (strendswith( source
->filename
, ".idl" ))
1085 parse_idl_file( source
, file
, 0 );
1086 else if (strendswith( source
->filename
, ".c" ) ||
1087 strendswith( source
->filename
, ".m" ) ||
1088 strendswith( source
->filename
, ".h" ) ||
1089 strendswith( source
->filename
, ".l" ) ||
1090 strendswith( source
->filename
, ".y" ))
1091 parse_c_file( source
, file
);
1092 else if (strendswith( source
->filename
, ".rc" ))
1093 parse_rc_file( source
, file
);
1094 else if (strendswith( source
->filename
, ".in" ))
1095 parse_in_file( source
, file
);
1097 input_file_name
= NULL
;
1101 /*******************************************************************
1104 * Add a source file to the list.
1106 static struct incl_file
*add_src_file( const char *name
)
1108 struct incl_file
*file
;
1110 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1111 file
= xmalloc( sizeof(*file
) );
1112 memset( file
, 0, sizeof(*file
) );
1113 file
->name
= xstrdup(name
);
1114 list_add_tail( &sources
, &file
->entry
);
1115 parse_file( file
, 1 );
1120 /*******************************************************************
1123 static char *get_make_variable( const char *name
)
1127 for (i
= 0; i
< cmdline_vars
.count
; i
+= 2)
1128 if (!strcmp( cmdline_vars
.str
[i
], name
))
1129 return xstrdup( cmdline_vars
.str
[i
+ 1] );
1131 for (i
= 0; i
< make_vars
.count
; i
+= 2)
1132 if (!strcmp( make_vars
.str
[i
], name
))
1133 return xstrdup( make_vars
.str
[i
+ 1] );
1138 /*******************************************************************
1139 * get_expanded_make_variable
1141 static char *get_expanded_make_variable( const char *name
)
1143 char *p
, *end
, *var
, *expand
, *tmp
;
1145 expand
= get_make_variable( name
);
1146 if (!expand
) return NULL
;
1149 while ((p
= strchr( p
, '$' )))
1153 if (!(end
= strchr( p
+ 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand
);
1155 if (strchr( p
+ 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p
+ 2 );
1156 var
= get_make_variable( p
+ 2 );
1157 tmp
= replace_substr( expand
, p
, end
- p
, var
? var
: "" );
1160 else if (p
[1] == '{') /* don't expand ${} variables */
1162 if (!(end
= strchr( p
+ 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand
);
1166 else if (p
[1] == '$')
1168 tmp
= replace_substr( expand
, p
, 2, "$" );
1170 else fatal_error( "syntax error in '%s'\n", expand
);
1172 /* switch to the new string */
1173 p
= tmp
+ (p
- expand
);
1178 /* consider empty variables undefined */
1180 while (*p
&& isspace(*p
)) p
++;
1181 if (*p
) return expand
;
1187 /*******************************************************************
1188 * get_expanded_make_var_array
1190 static struct strarray
get_expanded_make_var_array( const char *name
)
1192 struct strarray ret
= empty_strarray
;
1193 char *value
, *token
;
1195 if ((value
= get_expanded_make_variable( name
)))
1196 for (token
= strtok( value
, " \t" ); token
; token
= strtok( NULL
, " \t" ))
1197 strarray_add( &ret
, token
);
1202 /*******************************************************************
1205 static int set_make_variable( struct strarray
*array
, const char *assignment
)
1210 p
= name
= xstrdup( assignment
);
1211 while (isalnum(*p
) || *p
== '_') p
++;
1212 if (name
== p
) return 0; /* not a variable */
1216 while (isspace(*p
)) p
++;
1218 if (*p
!= '=') return 0; /* not an assignment */
1220 while (isspace(*p
)) p
++;
1222 /* redefining a variable replaces the previous value */
1223 for (i
= 0; i
< array
->count
; i
+= 2)
1225 if (strcmp( array
->str
[i
], name
)) continue;
1226 array
->str
[i
+ 1] = p
;
1229 strarray_add( array
, name
);
1230 strarray_add( array
, p
);
1235 /*******************************************************************
1238 static void parse_makefile(void)
1243 input_file_name
= strmake( "%s/%s", base_dir
, makefile_name
);
1244 if (!(file
= fopen( input_file_name
, "r" )))
1246 fatal_perror( "open" );
1251 while ((buffer
= get_line( file
)))
1253 if (Separator
&& !strncmp( buffer
, Separator
, strlen(Separator
) )) break;
1254 if (*buffer
== '\t') continue; /* command */
1255 while (isspace( *buffer
)) buffer
++;
1256 if (*buffer
== '#') continue; /* comment */
1257 set_make_variable( &make_vars
, buffer
);
1260 input_file_name
= NULL
;
1264 /*******************************************************************
1265 * add_generated_source
1267 * Add a generated source file to the list.
1269 static struct incl_file
*add_generated_source( const char *name
, const char *filename
)
1271 struct incl_file
*file
;
1273 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1274 file
= xmalloc( sizeof(*file
) );
1275 memset( file
, 0, sizeof(*file
) );
1276 file
->name
= xstrdup( name
);
1277 file
->filename
= xstrdup( filename
? filename
: name
);
1278 file
->flags
= FLAG_GENERATED
;
1279 list_add_tail( &sources
, &file
->entry
);
1284 /*******************************************************************
1285 * add_generated_sources
1287 static void add_generated_sources(void)
1289 struct incl_file
*source
, *next
, *file
;
1291 LIST_FOR_EACH_ENTRY_SAFE( source
, next
, &sources
, struct incl_file
, entry
)
1293 if (source
->flags
& FLAG_IDL_CLIENT
)
1295 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_c.c" ), NULL
);
1296 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1298 if (source
->flags
& FLAG_IDL_SERVER
)
1300 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_s.c" ), NULL
);
1301 add_include( file
, "wine/exception.h", 0 );
1302 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1304 if (source
->flags
& FLAG_IDL_IDENT
)
1306 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_i.c" ), NULL
);
1307 add_include( file
, "rpc.h", 0 );
1308 add_include( file
, "rpcndr.h", 0 );
1309 add_include( file
, "guiddef.h", 0 );
1311 if (source
->flags
& FLAG_IDL_PROXY
)
1313 file
= add_generated_source( "dlldata.o", "dlldata.c" );
1314 add_include( file
, "objbase.h", 0 );
1315 add_include( file
, "rpcproxy.h", 0 );
1316 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_p.c" ), NULL
);
1317 add_include( file
, "objbase.h", 0 );
1318 add_include( file
, "rpcproxy.h", 0 );
1319 add_include( file
, "wine/exception.h", 0 );
1320 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1322 if (source
->flags
& FLAG_IDL_REGTYPELIB
)
1324 add_generated_source( replace_extension( source
->name
, ".idl", "_t.res" ), NULL
);
1326 if (source
->flags
& FLAG_IDL_REGISTER
)
1328 add_generated_source( replace_extension( source
->name
, ".idl", "_r.res" ), NULL
);
1330 if (strendswith( source
->name
, ".y" ))
1332 file
= add_generated_source( replace_extension( source
->name
, ".y", ".tab.c" ), NULL
);
1333 /* steal the includes list from the source file */
1334 file
->files_count
= source
->files_count
;
1335 file
->files_size
= source
->files_size
;
1336 file
->files
= source
->files
;
1337 source
->files_count
= source
->files_size
= 0;
1338 source
->files
= NULL
;
1340 if (strendswith( source
->name
, ".l" ))
1342 file
= add_generated_source( replace_extension( source
->name
, ".l", ".yy.c" ), NULL
);
1343 /* steal the includes list from the source file */
1344 file
->files_count
= source
->files_count
;
1345 file
->files_size
= source
->files_size
;
1346 file
->files
= source
->files
;
1347 source
->files_count
= source
->files_size
= 0;
1348 source
->files
= NULL
;
1351 if (get_make_variable( "TESTDLL" ))
1353 file
= add_generated_source( "testlist.o", "testlist.c" );
1354 add_include( file
, "wine/test.h", 0 );
1359 /*******************************************************************
1362 static void output_include( struct incl_file
*pFile
, struct incl_file
*owner
)
1366 if (pFile
->owner
== owner
) return;
1367 if (!pFile
->filename
) return;
1368 pFile
->owner
= owner
;
1369 output_filename( pFile
->filename
);
1370 for (i
= 0; i
< pFile
->files_count
; i
++) output_include( pFile
->files
[i
], owner
);
1374 /*******************************************************************
1377 static struct strarray
output_sources(void)
1379 struct incl_file
*source
;
1380 int i
, is_win16
= 0;
1381 const char *dllext
= ".so";
1382 struct strarray object_files
= empty_strarray
;
1383 struct strarray crossobj_files
= empty_strarray
;
1384 struct strarray res_files
= empty_strarray
;
1385 struct strarray clean_files
= empty_strarray
;
1386 struct strarray po_files
= empty_strarray
;
1387 struct strarray mc_files
= empty_strarray
;
1388 struct strarray test_files
= empty_strarray
;
1389 struct strarray dlldata_files
= empty_strarray
;
1390 struct strarray c2man_files
= empty_strarray
;
1391 struct strarray implib_objs
= empty_strarray
;
1392 struct strarray includes
= empty_strarray
;
1393 struct strarray subdirs
= empty_strarray
;
1394 struct strarray phony_targets
= empty_strarray
;
1395 struct strarray dllflags
= get_expanded_make_var_array( "DLLFLAGS" );
1396 struct strarray imports
= get_expanded_make_var_array( "IMPORTS" );
1397 struct strarray all_targets
= get_expanded_make_var_array( "PROGRAMS" );
1398 struct strarray targetflags
= get_expanded_make_var_array( "TARGETFLAGS" );
1399 struct strarray delayimports
= get_expanded_make_var_array( "DELAYIMPORTS" );
1400 struct strarray extradllflags
= get_expanded_make_var_array( "EXTRADLLFLAGS" );
1401 char *module
= get_expanded_make_variable( "MODULE" );
1402 char *exeext
= get_expanded_make_variable( "EXEEXT" );
1403 char *testdll
= get_expanded_make_variable( "TESTDLL" );
1404 char *appmode
= get_expanded_make_variable( "APPMODE" );
1405 char *staticlib
= get_expanded_make_variable( "STATICLIB" );
1406 char *crosstarget
= get_expanded_make_variable( "CROSSTARGET" );
1408 if (exeext
&& !strcmp( exeext
, ".exe" )) dllext
= "";
1409 if (module
&& strendswith( module
, ".a" )) staticlib
= module
;
1410 for (i
= 0; i
< extradllflags
.count
; i
++) if (!strcmp( extradllflags
.str
[i
], "-m16" )) is_win16
= 1;
1412 strarray_add( &includes
, "-I." );
1413 if (src_dir
) strarray_add( &includes
, strmake( "-I%s", src_dir
));
1416 if (src_dir
) strarray_add( &includes
, strmake( "-I%s/%s", src_dir
, parent_dir
));
1417 else strarray_add( &includes
, strmake( "-I%s", parent_dir
));
1419 if (top_src_dir
&& top_obj_dir
) strarray_add( &includes
, strmake( "-I%s/include", top_obj_dir
));
1420 strarray_addall( &includes
, include_args
);
1422 LIST_FOR_EACH_ENTRY( source
, &sources
, struct incl_file
, entry
)
1425 char *obj
= xstrdup( source
->name
);
1426 char *ext
= get_extension( obj
);
1428 if (!ext
) fatal_error( "unsupported file type %s\n", source
->name
);
1431 if (src_dir
&& strchr( obj
, '/' ))
1433 char *dir
= xstrdup( obj
);
1434 *strrchr( dir
, '/' ) = 0;
1435 strarray_add_uniq( &subdirs
, dir
);
1436 sourcedep
= strmake( "%s %s", dir
, source
->filename
);
1438 else sourcedep
= xstrdup( source
->filename
);
1440 if (!strcmp( ext
, "y" )) /* yacc file */
1442 /* add source file dependency for parallel makes */
1443 char *header
= strmake( "%s.tab.h", obj
);
1445 if (find_include_file( header
))
1447 output( "%s.tab.h: %s\n", obj
, sourcedep
);
1448 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
1449 obj
, obj
, source
->filename
);
1450 output( "%s.tab.c: %s %s\n", obj
, source
->filename
, header
);
1451 strarray_add( &clean_files
, strmake( "%s.tab.h", obj
));
1453 else output( "%s.tab.c: %s\n", obj
, sourcedep
);
1455 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj
, source
->filename
);
1457 continue; /* no dependencies */
1459 else if (!strcmp( ext
, "x" )) /* template file */
1461 output( "%s.h: $(MAKEXFTMPL) %s\n", obj
, sourcedep
);
1462 output( "\t$(MAKEXFTMPL) -H -o $@ %s\n", source
->filename
);
1463 strarray_add( &clean_files
, strmake( "%s.h", obj
));
1464 continue; /* no dependencies */
1466 else if (!strcmp( ext
, "l" )) /* lex file */
1468 output( "%s.yy.c: %s\n", obj
, sourcedep
);
1469 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source
->filename
);
1470 continue; /* no dependencies */
1472 else if (!strcmp( ext
, "rc" )) /* resource file */
1474 if (source
->flags
& FLAG_RC_PO
)
1476 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj
, sourcedep
);
1477 output( "\t$(WRC) -o $@ %s", source
->filename
);
1478 output_filenames( includes
);
1479 if (is_win16
) output_filename( "-m16" );
1480 else output_filenames( targetflags
);
1481 output_filename( "$(RCFLAGS)" );
1483 output( "%s.res rsrc.pot:", obj
);
1484 strarray_add( &po_files
, source
->filename
);
1488 output( "%s.res: $(WRC) %s\n", obj
, sourcedep
);
1489 output( "\t$(WRC) -o $@ %s", source
->filename
);
1490 output_filenames( includes
);
1491 if (is_win16
) output_filename( "-m16" );
1492 else output_filenames( targetflags
);
1493 output_filename( "$(RCFLAGS)" );
1495 output( "%s.res:", obj
);
1497 strarray_add( &res_files
, strmake( "%s.res", obj
));
1499 else if (!strcmp( ext
, "mc" )) /* message file */
1501 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj
, sourcedep
);
1502 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source
->filename
);
1503 strarray_add( &res_files
, strmake( "%s.res", obj
));
1504 strarray_add( &mc_files
, source
->filename
);
1505 output( "msg.pot %s.res:", obj
);
1507 else if (!strcmp( ext
, "idl" )) /* IDL file */
1509 struct strarray targets
= empty_strarray
;
1513 if (!source
->flags
|| find_include_file( strmake( "%s.h", obj
)))
1514 source
->flags
|= FLAG_IDL_HEADER
;
1516 for (i
= 0; i
< sizeof(idl_outputs
) / sizeof(idl_outputs
[0]); i
++)
1518 if (!(source
->flags
& idl_outputs
[i
].flag
)) continue;
1519 dest
= strmake( "%s%s", obj
, idl_outputs
[i
].ext
);
1520 if (!find_src_file( dest
)) strarray_add( &clean_files
, dest
);
1521 strarray_add( &targets
, dest
);
1523 if (source
->flags
& FLAG_IDL_PROXY
) strarray_add( &dlldata_files
, source
->name
);
1524 output_filenames( targets
);
1525 output( ": $(WIDL)\n" );
1526 output( "\t$(WIDL) -o $@ %s", source
->filename
);
1527 output_filenames( includes
);
1528 output_filenames( targetflags
);
1529 output_filename( "$(IDLFLAGS)" );
1531 output_filenames( targets
);
1532 output( ": %s", sourcedep
);
1534 else if (!strcmp( ext
, "in" )) /* .in file or man page */
1536 if (strendswith( obj
, ".man" ) && source
->sourcename
)
1538 char *dir
, *dest
= replace_extension( obj
, ".man", "" );
1539 char *lang
= strchr( dest
, '.' );
1543 dir
= strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang
, source
->sourcename
);
1545 else dir
= strmake( "$(DESTDIR)$(mandir)/man%s", source
->sourcename
);
1546 output( "install-man-pages:: %s\n", obj
);
1547 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n",
1548 obj
, dir
, dest
, source
->sourcename
);
1549 output( "uninstall::\n" );
1550 output( "\t$(RM) %s/%s.%s\n",
1551 dir
, dest
, source
->sourcename
);
1554 strarray_add( &all_targets
, xstrdup(obj
) );
1555 strarray_add_uniq( &phony_targets
, "install-man-pages" );
1557 else strarray_add( &clean_files
, xstrdup(obj
) );
1558 output( "%s: %s\n", obj
, sourcedep
);
1559 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source
->filename
);
1560 output( "%s:", obj
);
1562 else if (!strcmp( ext
, "sfd" )) /* font file */
1564 char *fontforge
= get_expanded_make_variable( "FONTFORGE" );
1565 if (fontforge
&& !src_dir
)
1567 output( "%s.ttf: %s\n", obj
, source
->filename
);
1568 output( "\t%s -script %s/fonts/genttf.ff %s $@\n",
1569 fontforge
, top_src_dir
? top_src_dir
: top_obj_dir
, source
->filename
);
1572 continue; /* no dependencies */
1574 else if (!strcmp( ext
, "svg" )) /* svg file */
1576 char *convert
= get_expanded_make_variable( "CONVERT" );
1577 char *rsvg
= get_expanded_make_variable( "RSVG" );
1578 char *icotool
= get_expanded_make_variable( "ICOTOOL" );
1579 if (convert
&& rsvg
&& icotool
&& !src_dir
)
1581 output( "%s.ico %s.bmp: %s\n", obj
, obj
, source
->filename
);
1582 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" $(BUILDIMAGE) %s $@\n",
1583 convert
, icotool
, rsvg
, source
->filename
);
1588 continue; /* no dependencies */
1590 else if (!strcmp( ext
, "res" ))
1592 strarray_add( &res_files
, source
->name
);
1593 continue; /* no dependencies */
1597 int need_cross
= testdll
|| (source
->flags
& FLAG_C_IMPLIB
) || (module
&& staticlib
);
1599 if (source
->flags
& FLAG_GENERATED
) strarray_add( &clean_files
, source
->filename
);
1600 if (source
->flags
& FLAG_C_IMPLIB
) strarray_add( &implib_objs
, strmake( "%s.o", obj
));
1601 strarray_add( &object_files
, strmake( "%s.o", obj
));
1602 output( "%s.o: %s\n", obj
, sourcedep
);
1603 output( "\t$(CC) -c -o $@ %s", source
->filename
);
1604 output_filenames( includes
);
1605 if (module
|| staticlib
|| testdll
) output_filenames( dllflags
);
1606 output_filename( "$(ALLCFLAGS)" );
1608 if (crosstarget
&& need_cross
)
1610 strarray_add( &crossobj_files
, strmake( "%s.cross.o", obj
));
1611 output( "%s.cross.o: %s\n", obj
, sourcedep
);
1612 output( "\t$(CROSSCC) -c -o $@ %s", source
->filename
);
1613 output_filenames( includes
);
1614 output_filename( "$(ALLCROSSCFLAGS)" );
1617 if (testdll
&& !strcmp( ext
, "c" ) && !(source
->flags
& FLAG_GENERATED
))
1619 strarray_add( &test_files
, source
->name
);
1620 output( "%s.ok:\n", obj
);
1621 output( "\t$(RUNTEST) $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n", top_obj_dir
,
1622 testdll
, replace_extension( testdll
, ".dll", "_test.exe" ), dllext
, obj
);
1624 if (!strcmp( ext
, "c" ) && !(source
->flags
& FLAG_GENERATED
))
1625 strarray_add( &c2man_files
, source
->filename
);
1626 output( "%s.o", obj
);
1627 if (crosstarget
&& need_cross
) output( " %s.cross.o", obj
);
1633 for (i
= 0; i
< source
->files_count
; i
++) output_include( source
->files
[i
], source
);
1637 /* rules for files that depend on multiple sources */
1641 output( "rsrc.pot: $(WRC)" );
1642 output_filenames( po_files
);
1644 output( "\t$(WRC) -O pot -o $@" );
1645 output_filenames( includes
);
1646 if (is_win16
) output_filename( "-m16" );
1647 else output_filenames( targetflags
);
1648 output_filename( "$(RCFLAGS)" );
1649 output_filenames( po_files
);
1651 strarray_add( &clean_files
, "rsrc.pot" );
1656 output( "msg.pot: $(WMC)" );
1657 output_filenames( mc_files
);
1659 output( "\t$(WMC) -O pot -o $@" );
1660 output_filenames( mc_files
);
1662 strarray_add( &clean_files
, "msg.pot" );
1665 if (dlldata_files
.count
)
1667 output( "dlldata.c: $(WIDL) %s\n", src_dir
? strmake("%s/Makefile.in", src_dir
) : "Makefile.in" );
1668 output( "\t$(WIDL) --dlldata-only -o $@" );
1669 output_filenames( dlldata_files
);
1673 if (module
&& !staticlib
)
1675 char *importlib
= get_expanded_make_variable( "IMPORTLIB" );
1676 struct strarray all_libs
= empty_strarray
;
1677 char *spec_file
= appmode
? NULL
: replace_extension( module
, ".dll", ".spec" );
1679 if (spec_file
&& src_dir
) spec_file
= strmake( "%s/%s", src_dir
, spec_file
);
1680 for (i
= 0; i
< delayimports
.count
; i
++)
1681 strarray_add( &all_libs
, strmake( "-l%s", delayimports
.str
[i
] ));
1682 for (i
= 0; i
< imports
.count
; i
++)
1683 strarray_add( &all_libs
, strmake( "-l%s", imports
.str
[i
] ));
1684 for (i
= 0; i
< delayimports
.count
; i
++)
1685 strarray_add( &all_libs
, strmake( "-Wb,-d%s", delayimports
.str
[i
] ));
1686 strarray_add( &all_libs
, "-lwine" );
1687 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBPORT" ));
1688 strarray_addall( &all_libs
, get_expanded_make_var_array( "EXTRALIBS" ));
1689 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBS" ));
1693 strarray_add( &all_targets
, strmake( "%s%s", module
, dllext
));
1694 strarray_add( &all_targets
, strmake( "%s.fake", module
));
1695 output( "%s%s %s.fake:", module
, dllext
, module
);
1699 strarray_add( &all_targets
, module
);
1700 output( "%s:", module
);
1702 if (spec_file
) output_filename( spec_file
);
1703 output_filenames( object_files
);
1704 output_filenames( res_files
);
1706 output( "\t$(WINEGCC) -o $@" );
1707 output_filenames( targetflags
);
1710 output( " -shared %s", spec_file
);
1711 output_filenames( extradllflags
);
1713 else output_filename( appmode
);
1714 output_filenames( object_files
);
1715 output_filenames( res_files
);
1716 output_filenames( all_libs
);
1717 output_filename( "$(LDFLAGS)" );
1720 if (spec_file
&& importlib
)
1724 strarray_add( &clean_files
, strmake( "lib%s.def", importlib
));
1725 output( "lib%s.def: %s\n", importlib
, spec_file
);
1726 output( "\t$(WINEBUILD) -w --def -o $@ --export %s", spec_file
);
1727 output_filenames( targetflags
);
1728 if (is_win16
) output_filename( "-m16" );
1730 if (implib_objs
.count
)
1732 strarray_add( &clean_files
, strmake( "lib%s.def.a", importlib
));
1733 output( "lib%s.def.a:", importlib
);
1734 output_filenames( implib_objs
);
1736 output( "\t$(RM) $@\n" );
1737 output( "\t$(AR) $(ARFLAGS) $@" );
1738 output_filenames( implib_objs
);
1740 output( "\t$(RANLIB) $@\n" );
1745 strarray_add( &clean_files
, strmake( "lib%s.a", importlib
));
1746 output( "lib%s.a: %s", importlib
, spec_file
);
1747 output_filenames( implib_objs
);
1749 output( "\t$(WINEBUILD) -w --implib -o $@ --export %s", spec_file
);
1750 output_filenames( targetflags
);
1751 output_filenames( implib_objs
);
1754 if (crosstarget
&& !is_win16
)
1756 struct strarray cross_files
= strarray_replace_extension( &implib_objs
, ".o", ".cross.o" );
1757 strarray_add( &clean_files
, strmake( "lib%s.cross.a", importlib
));
1758 output( "lib%s.cross.a: %s", importlib
, spec_file
);
1759 output_filenames( cross_files
);
1761 output( "\t$(WINEBUILD) -b %s -w --implib -o $@ --export %s", crosstarget
, spec_file
);
1762 output_filenames( cross_files
);
1769 if (c2man_files
.count
&& top_obj_dir
)
1771 char *manext
= get_expanded_make_variable( "api_manext" );
1773 output( "manpages::\n" );
1774 output( "\t$(C2MAN) -w %s -R%s", spec_file
, top_obj_dir
);
1775 output_filename( strmake( "-I%s/include", top_src_dir
? top_src_dir
: top_obj_dir
));
1776 output_filename( strmake( "-o %s/documentation/man%s", top_obj_dir
, manext
? manext
: "3w" ));
1777 output_filenames( c2man_files
);
1779 output( "htmlpages::\n" );
1780 output( "\t$(C2MAN) -Th -w %s -R%s", spec_file
, top_obj_dir
);
1781 output_filename( strmake( "-I%s/include", top_src_dir
? top_src_dir
: top_obj_dir
));
1782 output_filename( strmake( "-o %s/documentation/html", top_obj_dir
));
1783 output_filenames( c2man_files
);
1785 output( "sgmlpages::\n" );
1786 output( "\t$(C2MAN) -Ts -w %s -R%s", spec_file
, top_obj_dir
);
1787 output_filename( strmake( "-I%s/include", top_src_dir
? top_src_dir
: top_obj_dir
));
1788 output_filename( strmake( "-o %s/documentation/api-guide", top_obj_dir
));
1789 output_filenames( c2man_files
);
1791 output( "xmlpages::\n" );
1792 output( "\t$(C2MAN) -Tx -w %s -R%s", spec_file
, top_obj_dir
);
1793 output_filename( strmake( "-I%s/include", top_src_dir
? top_src_dir
: top_obj_dir
));
1794 output_filename( strmake( "-o %s/documentation/api-guide-xml", top_obj_dir
));
1795 output_filenames( c2man_files
);
1797 strarray_add( &phony_targets
, "manpages" );
1798 strarray_add( &phony_targets
, "htmlpages" );
1799 strarray_add( &phony_targets
, "sgmlpages" );
1800 strarray_add( &phony_targets
, "xmlpages" );
1802 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1808 strarray_add( &all_targets
, staticlib
);
1809 output( "%s:", staticlib
);
1810 output_filenames( object_files
);
1811 output( "\n\t$(RM) $@\n" );
1812 output( "\t$(AR) $(ARFLAGS) $@" );
1813 output_filenames( object_files
);
1814 output( "\n\t$(RANLIB) $@\n" );
1815 if (crosstarget
&& module
)
1817 char *name
= replace_extension( staticlib
, ".a", ".cross.a" );
1819 strarray_add( &all_targets
, name
);
1820 output( "%s:", name
);
1821 output_filenames( crossobj_files
);
1822 output( "\n\t$(RM) $@\n" );
1823 output( "\t%s-ar $(ARFLAGS) $@", crosstarget
);
1824 output_filenames( crossobj_files
);
1825 output( "\n\t%s-ranlib $@\n", crosstarget
);
1831 struct strarray ok_files
= strarray_replace_extension( &test_files
, ".c", ".ok" );
1832 char *testmodule
= replace_extension( testdll
, ".dll", "_test.exe" );
1833 char *stripped
= replace_extension( testdll
, ".dll", "_test-stripped.exe" );
1834 struct strarray all_libs
= empty_strarray
;
1836 for (i
= 0; i
< imports
.count
; i
++) strarray_add( &all_libs
, strmake( "-l%s", imports
.str
[i
] ));
1837 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBS" ));
1839 strarray_add( &all_targets
, strmake( "%s%s", testmodule
, dllext
));
1840 strarray_add( &clean_files
, strmake( "%s%s", stripped
, dllext
));
1841 output( "%s%s:\n", testmodule
, dllext
);
1842 output( "\t$(WINEGCC) -o $@" );
1843 output_filenames( targetflags
);
1844 if (appmode
) output_filename( appmode
);
1845 output_filenames( object_files
);
1846 output_filenames( res_files
);
1847 output_filenames( all_libs
);
1848 output_filename( "$(LDFLAGS)" );
1850 output( "%s%s:\n", stripped
, dllext
);
1851 output( "\t$(WINEGCC) -s -o $@" );
1852 output_filenames( targetflags
);
1853 output_filename( strmake( "-Wb,-F,%s", testmodule
));
1854 if (appmode
) output_filename( appmode
);
1855 output_filenames( object_files
);
1856 output_filenames( res_files
);
1857 output_filenames( all_libs
);
1858 output_filename( "$(LDFLAGS)" );
1860 output( "%s%s %s%s:", testmodule
, dllext
, stripped
, dllext
);
1861 output_filenames( object_files
);
1862 output_filenames( res_files
);
1867 char *testres
= replace_extension( testdll
, ".dll", "_test.res" );
1868 output( "all: %s/programs/winetest/%s\n", top_obj_dir
, testres
);
1869 output( "%s/programs/winetest/%s: %s%s\n", top_obj_dir
, testres
, stripped
, dllext
);
1870 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | $(WRC) -o $@\n",
1871 testmodule
, stripped
, dllext
);
1876 char *crosstest
= replace_extension( testdll
, ".dll", "_crosstest.exe" );
1878 strarray_add( &clean_files
, crosstest
);
1879 output( "crosstest: %s\n", crosstest
);
1880 output( "%s:", crosstest
);
1881 output_filenames( crossobj_files
);
1882 output_filenames( res_files
);
1884 output( "\t$(CROSSWINEGCC) -o $@ -b %s", crosstarget
);
1885 output_filenames( crossobj_files
);
1886 output_filenames( res_files
);
1887 output_filenames( all_libs
);
1888 output_filename( "$(LDFLAGS)" );
1890 strarray_add( &phony_targets
, "crosstest" );
1893 output( "testlist.c: $(MAKECTESTS) %s\n", src_dir
? strmake("%s/Makefile.in", src_dir
) : "Makefile.in" );
1894 output( "\t$(MAKECTESTS) -o $@" );
1895 output_filenames( test_files
);
1897 output_filenames( ok_files
);
1898 output( ": %s%s ../%s%s\n", testmodule
, dllext
, testdll
, dllext
);
1899 output( "check test:" );
1900 output_filenames( ok_files
);
1902 output( "testclean::\n" );
1903 output( "\t$(RM)" );
1904 output_filenames( ok_files
);
1906 strarray_addall( &clean_files
, ok_files
);
1907 strarray_add( &phony_targets
, "check" );
1908 strarray_add( &phony_targets
, "test" );
1909 strarray_add( &phony_targets
, "testclean" );
1912 if (all_targets
.count
)
1915 output_filenames( all_targets
);
1919 strarray_addall( &clean_files
, object_files
);
1920 strarray_addall( &clean_files
, crossobj_files
);
1921 strarray_addall( &clean_files
, res_files
);
1922 strarray_addall( &clean_files
, all_targets
);
1923 strarray_addall( &clean_files
, get_expanded_make_var_array( "EXTRA_TARGETS" ));
1925 if (clean_files
.count
)
1927 output( "clean::\n" );
1928 output( "\t$(RM)" );
1929 output_filenames( clean_files
);
1935 output_filenames( subdirs
);
1937 output( "\t$(MKDIR_P) -m 755 $@\n" );
1940 if (phony_targets
.count
)
1942 output( ".PHONY:" );
1943 output_filenames( phony_targets
);
1951 /*******************************************************************
1954 static FILE *create_temp_file( const char *orig
)
1956 char *name
= xmalloc( strlen(orig
) + 13 );
1957 unsigned int i
, id
= getpid();
1961 for (i
= 0; i
< 100; i
++)
1963 sprintf( name
, "%s.tmp%08x", orig
, id
);
1964 if ((fd
= open( name
, O_RDWR
| O_CREAT
| O_EXCL
, 0666 )) != -1)
1966 ret
= fdopen( fd
, "w" );
1969 if (errno
!= EEXIST
) break;
1972 if (!ret
) fatal_error( "failed to create output file for '%s'\n", orig
);
1973 temp_file_name
= name
;
1978 /*******************************************************************
1981 static void rename_temp_file( const char *dest
)
1983 int ret
= rename( temp_file_name
, dest
);
1984 if (ret
== -1 && errno
== EEXIST
)
1986 /* rename doesn't overwrite on windows */
1988 ret
= rename( temp_file_name
, dest
);
1990 if (ret
== -1) fatal_error( "failed to rename output file to '%s'\n", dest
);
1991 temp_file_name
= NULL
;
1995 /*******************************************************************
1998 static void output_gitignore( const char *dest
, const struct strarray
*files
)
2002 output_file
= create_temp_file( dest
);
2004 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2005 output( "/.gitignore\n" );
2006 output( "/Makefile\n" );
2007 for (i
= 0; i
< files
->count
; i
++)
2009 if (!strchr( files
->str
[i
], '/' )) output( "/" );
2010 output( "%s\n", files
->str
[i
] );
2013 if (fclose( output_file
)) fatal_perror( "write" );
2015 rename_temp_file( dest
);
2019 /*******************************************************************
2020 * output_dependencies
2022 static void output_dependencies( const char *path
)
2024 struct strarray targets
= empty_strarray
;
2026 if (Separator
&& ((output_file
= fopen( path
, "r" ))))
2029 FILE *tmp_file
= create_temp_file( path
);
2032 while (fgets( buffer
, sizeof(buffer
), output_file
) && !found
)
2034 if (fwrite( buffer
, 1, strlen(buffer
), tmp_file
) != strlen(buffer
)) fatal_perror( "write" );
2035 found
= !strncmp( buffer
, Separator
, strlen(Separator
) );
2037 if (fclose( output_file
)) fatal_perror( "write" );
2038 output_file
= tmp_file
;
2039 if (!found
) output( "\n%s\n", Separator
);
2043 if (!(output_file
= fopen( path
, Separator
? "a" : "w" )))
2044 fatal_perror( "%s", path
);
2047 targets
= output_sources();
2049 fclose( output_file
);
2051 if (temp_file_name
) rename_temp_file( path
);
2053 if (!src_dir
) output_gitignore( strmake( "%s/.gitignore", base_dir
), &targets
);
2057 /*******************************************************************
2060 static void update_makefile( const char *path
)
2062 static const char *source_vars
[] =
2080 struct strarray value
;
2081 struct incl_file
*file
;
2084 output_file_name
= strmake( "%s/%s", base_dir
, makefile_name
);
2087 src_dir
= get_expanded_make_variable( "srcdir" );
2088 top_src_dir
= get_expanded_make_variable( "top_srcdir" );
2089 top_obj_dir
= get_expanded_make_variable( "top_builddir" );
2090 parent_dir
= get_expanded_make_variable( "PARENTSRC" );
2092 include_args
= empty_strarray
;
2093 value
= get_expanded_make_var_array( "EXTRAINCL" );
2094 for (i
= 0; i
< value
.count
; i
++)
2095 if (!strncmp( value
.str
[i
], "-I", 2 ))
2096 strarray_add_uniq( &include_args
, value
.str
[i
] );
2100 list_init( &sources
);
2101 list_init( &includes
);
2103 for (var
= source_vars
; *var
; var
++)
2105 value
= get_expanded_make_var_array( *var
);
2106 for (i
= 0; i
< value
.count
; i
++) add_src_file( value
.str
[i
] );
2109 add_generated_sources();
2111 value
= get_expanded_make_var_array( "EXTRA_OBJS" );
2112 for (i
= 0; i
< value
.count
; i
++)
2114 /* default to .c for unknown extra object files */
2115 if (strendswith( value
.str
[i
], ".o" ))
2116 add_generated_source( value
.str
[i
], replace_extension( value
.str
[i
], ".o", ".c" ) );
2118 add_generated_source( value
.str
[i
], NULL
);
2121 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
) parse_file( file
, 0 );
2122 output_dependencies( output_file_name
);
2123 output_file_name
= NULL
;
2127 /*******************************************************************
2130 static void parse_makeflags( const char *flags
)
2132 const char *p
= flags
;
2133 char *var
, *buffer
= xmalloc( strlen(flags
) + 1 );
2137 while (isspace(*p
)) p
++;
2139 while (*p
&& !isspace(*p
))
2141 if (*p
== '\\' && p
[1]) p
++;
2145 if (var
> buffer
) set_make_variable( &cmdline_vars
, buffer
);
2150 /*******************************************************************
2153 static int parse_option( const char *opt
)
2157 if (strchr( opt
, '=' )) return set_make_variable( &cmdline_vars
, opt
);
2163 if (opt
[2]) strarray_add_uniq( &include_args
, opt
);
2169 top_src_dir
= opt
+ 2;
2172 top_obj_dir
= opt
+ 2;
2175 parent_dir
= opt
+ 2;
2178 if (opt
[2]) makefile_name
= opt
+ 2;
2181 parse_makefile_mode
= 1;
2184 relative_dir_mode
= 1;
2187 if (opt
[2]) Separator
= opt
+ 2;
2188 else Separator
= NULL
;
2191 break; /* ignored */
2193 fprintf( stderr
, "Unknown option '%s'\n%s", opt
, Usage
);
2200 /*******************************************************************
2203 int main( int argc
, char *argv
[] )
2205 const char *makeflags
= getenv( "MAKEFLAGS" );
2206 struct incl_file
*pFile
;
2209 if (makeflags
) parse_makeflags( makeflags
);
2214 if (parse_option( argv
[i
] ))
2216 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
2222 if (relative_dir_mode
)
2228 fprintf( stderr
, "Option -r needs two directories\n%s", Usage
);
2231 relpath
= get_relative_path( argv
[1], argv
[2] );
2232 printf( "%s\n", relpath
? relpath
: "." );
2236 atexit( cleanup_files
);
2237 signal( SIGTERM
, exit_on_signal
);
2238 signal( SIGINT
, exit_on_signal
);
2240 signal( SIGHUP
, exit_on_signal
);
2243 if (parse_makefile_mode
)
2245 for (i
= 1; i
< argc
; i
++) update_makefile( argv
[i
] );
2250 for (i
= 1; i
< argc
; i
++) add_src_file( argv
[i
] );
2251 add_generated_sources();
2253 LIST_FOR_EACH_ENTRY( pFile
, &includes
, struct incl_file
, entry
) parse_file( pFile
, 0 );
2254 output_dependencies( makefile_name
);