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 *makefile_name
= "Makefile";
108 static const char *Separator
= "### Dependencies";
109 static const char *input_file_name
;
110 static const char *output_file_name
;
111 static const char *temp_file_name
;
112 static int parse_makefile_mode
;
113 static int relative_dir_mode
;
114 static int input_line
;
115 static int output_column
;
116 static FILE *output_file
;
118 static const char Usage
[] =
119 "Usage: makedep [options] [files]\n"
121 " -Idir Search for include files in directory 'dir'\n"
122 " -Cdir Search for source files in directory 'dir'\n"
123 " -Sdir Set the top source directory\n"
124 " -Tdir Set the top object directory\n"
125 " -Pdir Set the parent source directory\n"
126 " -M dirs Parse the makefiles from the specified directories\n"
127 " -R from to Compute the relative path between two directories\n"
128 " -fxxx Store output in file 'xxx' (default: Makefile)\n"
129 " -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
133 #define __attribute__(x)
136 static void fatal_error( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
137 static void fatal_perror( const char *msg
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
138 static void output( const char *format
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
139 static char *strmake( const char* fmt
, ... ) __attribute__ ((__format__ (__printf__
, 1, 2)));
141 /*******************************************************************
144 static void fatal_error( const char *msg
, ... )
147 va_start( valist
, msg
);
150 fprintf( stderr
, "%s:", input_file_name
);
151 if (input_line
) fprintf( stderr
, "%d:", input_line
);
152 fprintf( stderr
, " error: " );
154 else fprintf( stderr
, "makedep: error: " );
155 vfprintf( stderr
, msg
, valist
);
161 /*******************************************************************
164 static void fatal_perror( const char *msg
, ... )
167 va_start( valist
, msg
);
170 fprintf( stderr
, "%s:", input_file_name
);
171 if (input_line
) fprintf( stderr
, "%d:", input_line
);
172 fprintf( stderr
, " error: " );
174 else fprintf( stderr
, "makedep: error: " );
175 vfprintf( stderr
, msg
, valist
);
182 /*******************************************************************
185 static void cleanup_files(void)
187 if (temp_file_name
) unlink( temp_file_name
);
188 if (output_file_name
) unlink( output_file_name
);
192 /*******************************************************************
195 static void exit_on_signal( int sig
)
197 exit( 1 ); /* this will call the atexit functions */
201 /*******************************************************************
204 static void *xmalloc( size_t size
)
207 if (!(res
= malloc (size
? size
: 1)))
208 fatal_error( "Virtual memory exhausted.\n" );
213 /*******************************************************************
216 static void *xrealloc (void *ptr
, size_t size
)
220 if (!(res
= realloc( ptr
, size
)))
221 fatal_error( "Virtual memory exhausted.\n" );
225 /*******************************************************************
228 static char *xstrdup( const char *str
)
230 char *res
= strdup( str
);
231 if (!res
) fatal_error( "Virtual memory exhausted.\n" );
236 /*******************************************************************
239 static char *strmake( const char* fmt
, ... )
247 char *p
= xmalloc (size
);
249 n
= vsnprintf (p
, size
, fmt
, ap
);
251 if (n
== -1) size
*= 2;
252 else if ((size_t)n
>= size
) size
= n
+ 1;
259 /*******************************************************************
262 static int strendswith( const char* str
, const char* end
)
267 return l
>= m
&& strcmp(str
+ l
- m
, end
) == 0;
271 /*******************************************************************
274 static void output( const char *format
, ... )
279 va_start( valist
, format
);
280 ret
= vfprintf( output_file
, format
, valist
);
282 if (ret
< 0) fatal_perror( "output" );
283 if (format
[0] && format
[strlen(format
) - 1] == '\n') output_column
= 0;
284 else output_column
+= ret
;
288 /*******************************************************************
291 static void strarray_add( struct strarray
*array
, const char *str
)
293 if (array
->count
== array
->size
)
295 if (array
->size
) array
->size
*= 2;
296 else array
->size
= 16;
297 array
->str
= xrealloc( array
->str
, sizeof(array
->str
[0]) * array
->size
);
299 array
->str
[array
->count
++] = str
;
303 /*******************************************************************
306 static void strarray_addall( struct strarray
*array
, struct strarray added
)
310 for (i
= 0; i
< added
.count
; i
++) strarray_add( array
, added
.str
[i
] );
314 /*******************************************************************
317 static void strarray_insert( struct strarray
*array
, unsigned int pos
, const char *str
)
321 strarray_add( array
, NULL
);
322 for (i
= array
->count
- 1; i
> pos
; i
--) array
->str
[i
] = array
->str
[i
- 1];
323 array
->str
[pos
] = str
;
327 /*******************************************************************
330 static void strarray_add_uniq( struct strarray
*array
, const char *str
)
334 for (i
= 0; i
< array
->count
; i
++) if (!strcmp( array
->str
[i
], str
)) return;
335 strarray_add( array
, str
);
339 /*******************************************************************
342 static void output_filename( const char *name
)
344 if (output_column
+ strlen(name
) + 1 > 100)
349 else if (output_column
) output( " " );
350 output( "%s", name
);
354 /*******************************************************************
357 static void output_filenames( struct strarray array
)
361 for (i
= 0; i
< array
.count
; i
++) output_filename( array
.str
[i
] );
365 /*******************************************************************
368 static char *get_extension( char *filename
)
370 char *ext
= strrchr( filename
, '.' );
371 if (ext
&& strchr( ext
, '/' )) ext
= NULL
;
376 /*******************************************************************
379 static char *replace_extension( const char *name
, const char *old_ext
, const char *new_ext
)
382 int name_len
= strlen( name
);
383 int ext_len
= strlen( old_ext
);
385 if (name_len
>= ext_len
&& !strcmp( name
+ name_len
- ext_len
, old_ext
)) name_len
-= ext_len
;
386 ret
= xmalloc( name_len
+ strlen( new_ext
) + 1 );
387 memcpy( ret
, name
, name_len
);
388 strcpy( ret
+ name_len
, new_ext
);
393 /*******************************************************************
394 * strarray_replace_extension
396 static struct strarray
strarray_replace_extension( const struct strarray
*array
,
397 const char *old_ext
, const char *new_ext
)
402 ret
.count
= ret
.size
= array
->count
;
403 ret
.str
= xmalloc( sizeof(ret
.str
[0]) * ret
.size
);
404 for (i
= 0; i
< array
->count
; i
++) ret
.str
[i
] = replace_extension( array
->str
[i
], old_ext
, new_ext
);
409 /*******************************************************************
412 static char *replace_substr( const char *str
, const char *start
, unsigned int len
, const char *replace
)
414 unsigned int pos
= start
- str
;
415 char *ret
= xmalloc( pos
+ strlen(replace
) + strlen(start
+ len
) + 1 );
416 memcpy( ret
, str
, pos
);
417 strcpy( ret
+ pos
, replace
);
418 strcat( ret
+ pos
, start
+ len
);
423 /*******************************************************************
426 * Determine where the destination path is located relative to the 'from' path.
428 static char *get_relative_path( const char *from
, const char *dest
)
432 unsigned int dotdots
= 0;
434 /* a path of "." is equivalent to an empty path */
435 if (!strcmp( from
, "." )) from
= "";
439 while (*from
== '/') from
++;
440 while (*dest
== '/') dest
++;
441 start
= dest
; /* save start of next path element */
444 while (*from
&& *from
!= '/' && *from
== *dest
) { from
++; dest
++; }
445 if ((!*from
|| *from
== '/') && (!*dest
|| *dest
== '/')) continue;
447 /* count remaining elements in 'from' */
451 while (*from
&& *from
!= '/') from
++;
452 while (*from
== '/') from
++;
458 if (!start
[0] && !dotdots
) return NULL
; /* empty path */
460 ret
= xmalloc( 3 * dotdots
+ strlen( start
) + 1 );
461 for (p
= ret
; dotdots
; dotdots
--, p
+= 3) memcpy( p
, "../", 3 );
463 if (start
[0]) strcpy( p
, start
);
464 else p
[-1] = 0; /* remove trailing slash */
469 /*******************************************************************
472 static void init_paths(void)
474 /* ignore redundant source paths */
475 if (src_dir
&& !strcmp( src_dir
, "." )) src_dir
= NULL
;
476 if (top_src_dir
&& top_obj_dir
&& !strcmp( top_src_dir
, top_obj_dir
)) top_src_dir
= NULL
;
478 if (top_src_dir
) strarray_insert( &include_args
, 0, strmake( "-I%s/include", top_src_dir
));
479 else if (top_obj_dir
) strarray_insert( &include_args
, 0, strmake( "-I%s/include", top_obj_dir
));
483 /*******************************************************************
486 static char *get_line( FILE *file
)
489 static unsigned int size
;
494 buffer
= xmalloc( size
);
496 if (!fgets( buffer
, size
, file
)) return NULL
;
501 char *p
= buffer
+ strlen(buffer
);
502 /* if line is larger than buffer, resize buffer */
503 while (p
== buffer
+ size
- 1 && p
[-1] != '\n')
505 buffer
= xrealloc( buffer
, size
* 2 );
506 if (!fgets( buffer
+ size
- 1, size
+ 1, file
)) break;
507 p
= buffer
+ strlen(buffer
);
510 if (p
> buffer
&& p
[-1] == '\n')
513 if (p
> buffer
&& p
[-1] == '\r') *(--p
) = 0;
514 if (p
> buffer
&& p
[-1] == '\\')
517 /* line ends in backslash, read continuation line */
518 if (!fgets( p
, size
- (p
- buffer
), file
)) return buffer
;
527 /*******************************************************************
530 static struct incl_file
*find_src_file( const char *name
)
532 struct incl_file
*file
;
534 LIST_FOR_EACH_ENTRY( file
, &sources
, struct incl_file
, entry
)
535 if (!strcmp( name
, file
->name
)) return file
;
539 /*******************************************************************
542 static struct incl_file
*find_include_file( const char *name
)
544 struct incl_file
*file
;
546 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
)
547 if (!strcmp( name
, file
->name
)) return file
;
551 /*******************************************************************
554 * Add an include file if it doesn't already exists.
556 static struct incl_file
*add_include( struct incl_file
*parent
, const char *name
, int system
)
558 struct incl_file
*include
;
561 if (parent
->files_count
>= parent
->files_size
)
563 parent
->files_size
*= 2;
564 if (parent
->files_size
< 16) parent
->files_size
= 16;
565 parent
->files
= xrealloc( parent
->files
, parent
->files_size
* sizeof(*parent
->files
) );
568 /* enforce some rules for the Wine tree */
570 if (!memcmp( name
, "../", 3 ))
571 fatal_error( "#include directive with relative path not allowed\n" );
573 if (!strcmp( name
, "config.h" ))
575 if ((ext
= strrchr( parent
->filename
, '.' )) && !strcmp( ext
, ".h" ))
576 fatal_error( "config.h must not be included by a header file\n" );
577 if (parent
->files_count
)
578 fatal_error( "config.h must be included before anything else\n" );
580 else if (!strcmp( name
, "wine/port.h" ))
582 if ((ext
= strrchr( parent
->filename
, '.' )) && !strcmp( ext
, ".h" ))
583 fatal_error( "wine/port.h must not be included by a header file\n" );
584 if (!parent
->files_count
) fatal_error( "config.h must be included before wine/port.h\n" );
585 if (parent
->files_count
> 1)
586 fatal_error( "wine/port.h must be included before everything except config.h\n" );
587 if (strcmp( parent
->files
[0]->name
, "config.h" ))
588 fatal_error( "config.h must be included before wine/port.h\n" );
591 LIST_FOR_EACH_ENTRY( include
, &includes
, struct incl_file
, entry
)
592 if (!strcmp( name
, include
->name
)) goto found
;
594 include
= xmalloc( sizeof(*include
) );
595 memset( include
, 0, sizeof(*include
) );
596 include
->name
= xstrdup(name
);
597 include
->included_by
= parent
;
598 include
->included_line
= input_line
;
599 if (system
) include
->flags
|= FLAG_SYSTEM
;
600 list_add_tail( &includes
, &include
->entry
);
602 parent
->files
[parent
->files_count
++] = include
;
607 /*******************************************************************
610 static FILE *open_file( const char *path
)
614 if (path
[0] != '/' && strcmp( base_dir
, "." ))
616 char *full_path
= strmake( "%s/%s", base_dir
, path
);
617 ret
= fopen( full_path
, "r" );
620 else ret
= fopen( path
, "r" );
625 /*******************************************************************
628 static FILE *open_src_file( struct incl_file
*pFile
)
632 /* first try name as is */
633 if ((file
= open_file( pFile
->name
)))
635 pFile
->filename
= xstrdup( pFile
->name
);
638 /* now try in source dir */
641 pFile
->filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
642 file
= open_file( pFile
->filename
);
644 /* now try parent dir */
645 if (!file
&& parent_dir
)
648 pFile
->filename
= strmake( "%s/%s/%s", src_dir
, parent_dir
, pFile
->name
);
650 pFile
->filename
= strmake( "%s/%s", parent_dir
, pFile
->name
);
651 file
= open_file( pFile
->filename
);
653 if (!file
) fatal_perror( "open %s", pFile
->name
);
658 /*******************************************************************
661 static FILE *open_include_file( struct incl_file
*pFile
)
669 /* check for generated bison header */
671 if (strendswith( pFile
->name
, ".tab.h" ))
673 filename
= replace_extension( pFile
->name
, ".tab.h", ".y" );
674 if (src_dir
) filename
= strmake( "%s/%s", src_dir
, filename
);
676 if ((file
= open_file( filename
)))
678 pFile
->sourcename
= filename
;
679 pFile
->filename
= xstrdup( pFile
->name
);
680 /* don't bother to parse it */
687 /* check for corresponding idl file in source dir */
689 if (strendswith( pFile
->name
, ".h" ))
691 filename
= replace_extension( pFile
->name
, ".h", ".idl" );
692 if (src_dir
) filename
= strmake( "%s/%s", src_dir
, filename
);
694 if ((file
= open_file( filename
)))
696 pFile
->sourcename
= filename
;
697 pFile
->filename
= xstrdup( pFile
->name
);
703 /* now try in source dir */
705 filename
= strmake( "%s/%s", src_dir
, pFile
->name
);
707 filename
= xstrdup( pFile
->name
);
708 if ((file
= open_file( filename
))) goto found
;
711 /* now try in parent source dir */
715 filename
= strmake( "%s/%s/%s", src_dir
, parent_dir
, pFile
->name
);
717 filename
= strmake( "%s/%s", parent_dir
, pFile
->name
);
718 if ((file
= open_file( filename
))) goto found
;
722 /* check for corresponding idl file in global includes */
724 if (strendswith( pFile
->name
, ".h" ))
726 filename
= replace_extension( pFile
->name
, ".h", ".idl" );
728 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
729 else if (top_obj_dir
)
730 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
734 if (filename
&& (file
= open_file( filename
)))
736 pFile
->sourcename
= filename
;
737 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
743 /* check for corresponding .in file in global includes (for config.h.in) */
745 if (strendswith( pFile
->name
, ".h" ))
747 filename
= replace_extension( pFile
->name
, ".h", ".h.in" );
749 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
750 else if (top_obj_dir
)
751 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
755 if (filename
&& (file
= open_file( filename
)))
757 pFile
->sourcename
= filename
;
758 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
764 /* check for corresponding .x file in global includes */
766 if (strendswith( pFile
->name
, "tmpl.h" ))
768 filename
= replace_extension( pFile
->name
, ".h", ".x" );
770 filename
= strmake( "%s/include/%s", top_src_dir
, filename
);
771 else if (top_obj_dir
)
772 filename
= strmake( "%s/include/%s", top_obj_dir
, filename
);
776 if (filename
&& (file
= open_file( filename
)))
778 pFile
->sourcename
= filename
;
779 pFile
->filename
= strmake( "%s/include/%s", top_obj_dir
, pFile
->name
);
785 /* now search in include paths */
786 for (i
= 0; i
< include_args
.count
; i
++)
788 const char *dir
= include_args
.str
[i
] + 2; /* skip -I */
791 /* ignore absolute paths that don't point into the source dir */
792 if (!top_src_dir
) continue;
793 len
= strlen( top_src_dir
);
794 if (strncmp( dir
, top_src_dir
, len
)) continue;
795 if (dir
[len
] && dir
[len
] != '/') continue;
797 filename
= strmake( "%s/%s", dir
, pFile
->name
);
798 if ((file
= open_file( filename
))) goto found
;
801 if (pFile
->flags
& FLAG_SYSTEM
) return NULL
; /* ignore system files we cannot find */
803 /* try in src file directory */
804 if ((p
= strrchr(pFile
->included_by
->filename
, '/')))
806 int l
= p
- pFile
->included_by
->filename
+ 1;
807 filename
= xmalloc(l
+ strlen(pFile
->name
) + 1);
808 memcpy( filename
, pFile
->included_by
->filename
, l
);
809 strcpy( filename
+ l
, pFile
->name
);
810 if ((file
= open_file( filename
))) goto found
;
814 fprintf( stderr
, "%s:%d: error: ", pFile
->included_by
->filename
, pFile
->included_line
);
815 perror( pFile
->name
);
816 pFile
= pFile
->included_by
;
817 while (pFile
&& pFile
->included_by
)
819 const char *parent
= pFile
->included_by
->sourcename
;
820 if (!parent
) parent
= pFile
->included_by
->name
;
821 fprintf( stderr
, "%s:%d: note: %s was first included here\n",
822 parent
, pFile
->included_line
, pFile
->name
);
823 pFile
= pFile
->included_by
;
828 pFile
->filename
= filename
;
833 /*******************************************************************
834 * parse_include_directive
836 static void parse_include_directive( struct incl_file
*source
, char *str
)
838 char quote
, *include
, *p
= str
;
840 while (*p
&& isspace(*p
)) p
++;
841 if (*p
!= '\"' && *p
!= '<' ) return;
843 if (quote
== '<') quote
= '>';
845 while (*p
&& (*p
!= quote
)) p
++;
846 if (!*p
) fatal_error( "malformed include directive '%s'\n", str
);
848 add_include( source
, include
, (quote
== '>') );
852 /*******************************************************************
853 * parse_pragma_directive
855 static void parse_pragma_directive( struct incl_file
*source
, char *str
)
857 char *flag
, *p
= str
;
859 if (!isspace( *p
)) return;
860 while (*p
&& isspace(*p
)) p
++;
861 p
= strtok( p
, " \t" );
862 if (strcmp( p
, "makedep" )) return;
864 while ((flag
= strtok( NULL
, " \t" )))
866 if (!strcmp( flag
, "depend" ))
868 while ((p
= strtok( NULL
, " \t" ))) add_include( source
, p
, 0 );
871 if (strendswith( source
->name
, ".idl" ))
873 if (!strcmp( flag
, "header" )) source
->flags
|= FLAG_IDL_HEADER
;
874 else if (!strcmp( flag
, "proxy" )) source
->flags
|= FLAG_IDL_PROXY
;
875 else if (!strcmp( flag
, "client" )) source
->flags
|= FLAG_IDL_CLIENT
;
876 else if (!strcmp( flag
, "server" )) source
->flags
|= FLAG_IDL_SERVER
;
877 else if (!strcmp( flag
, "ident" )) source
->flags
|= FLAG_IDL_IDENT
;
878 else if (!strcmp( flag
, "typelib" )) source
->flags
|= FLAG_IDL_TYPELIB
;
879 else if (!strcmp( flag
, "register" )) source
->flags
|= FLAG_IDL_REGISTER
;
880 else if (!strcmp( flag
, "regtypelib" )) source
->flags
|= FLAG_IDL_REGTYPELIB
;
882 else if (strendswith( source
->name
, ".rc" ))
884 if (!strcmp( flag
, "po" )) source
->flags
|= FLAG_RC_PO
;
886 else if (!strcmp( flag
, "implib" )) source
->flags
|= FLAG_C_IMPLIB
;
891 /*******************************************************************
892 * parse_cpp_directive
894 static void parse_cpp_directive( struct incl_file
*source
, char *str
)
896 while (*str
&& isspace(*str
)) str
++;
897 if (*str
++ != '#') return;
898 while (*str
&& isspace(*str
)) str
++;
900 if (!strncmp( str
, "include", 7 ))
901 parse_include_directive( source
, str
+ 7 );
902 else if (!strncmp( str
, "import", 6 ) && strendswith( source
->name
, ".m" ))
903 parse_include_directive( source
, str
+ 6 );
904 else if (!strncmp( str
, "pragma", 6 ))
905 parse_pragma_directive( source
, str
+ 6 );
909 /*******************************************************************
912 * If for_h_file is non-zero, it means we are not interested in the idl file
913 * itself, but only in the contents of the .h file that will be generated from it.
915 static void parse_idl_file( struct incl_file
*pFile
, FILE *file
, int for_h_file
)
917 char *buffer
, *include
;
922 /* generated .h file always includes these */
923 add_include( pFile
, "rpc.h", 1 );
924 add_include( pFile
, "rpcndr.h", 1 );
927 while ((buffer
= get_line( file
)))
931 while (*p
&& isspace(*p
)) p
++;
933 if (!strncmp( p
, "import", 6 ))
936 while (*p
&& isspace(*p
)) p
++;
937 if (*p
!= '"') continue;
939 while (*p
&& (*p
!= '"')) p
++;
940 if (!*p
) fatal_error( "malformed import directive\n" );
942 if (for_h_file
&& strendswith( include
, ".idl" )) strcpy( p
- 4, ".h" );
943 add_include( pFile
, include
, 0 );
947 if (for_h_file
) /* only check for #include inside cpp_quote */
949 if (strncmp( p
, "cpp_quote", 9 )) continue;
951 while (*p
&& isspace(*p
)) p
++;
952 if (*p
++ != '(') continue;
953 while (*p
&& isspace(*p
)) p
++;
954 if (*p
++ != '"') continue;
955 if (*p
++ != '#') continue;
956 while (*p
&& isspace(*p
)) p
++;
957 if (strncmp( p
, "include", 7 )) continue;
959 while (*p
&& isspace(*p
)) p
++;
960 if (*p
== '\\' && p
[1] == '"')
967 if (*p
++ != '<' ) continue;
971 while (*p
&& (*p
!= quote
)) p
++;
972 if (!*p
|| (quote
== '"' && p
[-1] != '\\'))
973 fatal_error( "malformed #include directive inside cpp_quote\n" );
974 if (quote
== '"') p
--; /* remove backslash */
976 add_include( pFile
, include
, (quote
== '>') );
980 parse_cpp_directive( pFile
, p
);
984 /*******************************************************************
987 static void parse_c_file( struct incl_file
*pFile
, FILE *file
)
992 while ((buffer
= get_line( file
)))
994 parse_cpp_directive( pFile
, buffer
);
999 /*******************************************************************
1002 static void parse_rc_file( struct incl_file
*pFile
, FILE *file
)
1004 char *buffer
, *include
;
1007 while ((buffer
= get_line( file
)))
1011 while (*p
&& isspace(*p
)) p
++;
1013 if (p
[0] == '/' && p
[1] == '*') /* check for magic makedep comment */
1016 while (*p
&& isspace(*p
)) p
++;
1017 if (strncmp( p
, "@makedep:", 9 )) continue;
1019 while (*p
&& isspace(*p
)) p
++;
1024 while (*p
&& *p
!= quote
) p
++;
1029 while (*p
&& !isspace(*p
) && *p
!= '*') p
++;
1032 fatal_error( "malformed makedep comment\n" );
1034 add_include( pFile
, include
, (quote
== '>') );
1038 parse_cpp_directive( pFile
, buffer
);
1043 /*******************************************************************
1046 static void parse_in_file( struct incl_file
*source
, FILE *file
)
1050 /* make sure it gets rebuilt when the version changes */
1051 add_include( source
, "config.h", 1 );
1053 if (!strendswith( source
->filename
, ".man.in" )) return; /* not a man page */
1056 while ((buffer
= get_line( file
)))
1058 if (strncmp( buffer
, ".TH", 3 )) continue;
1059 if (!(p
= strtok( buffer
, " \t" ))) continue; /* .TH */
1060 if (!(p
= strtok( NULL
, " \t" ))) continue; /* program name */
1061 if (!(p
= strtok( NULL
, " \t" ))) continue; /* man section */
1062 source
->sourcename
= xstrdup( p
); /* abuse source name to store section */
1068 /*******************************************************************
1071 static void parse_file( struct incl_file
*source
, int src
)
1075 /* don't try to open certain types of files */
1076 if (strendswith( source
->name
, ".tlb" ))
1078 source
->filename
= xstrdup( source
->name
);
1082 file
= src
? open_src_file( source
) : open_include_file( source
);
1084 input_file_name
= source
->filename
;
1086 if (source
->sourcename
&& strendswith( source
->sourcename
, ".idl" ))
1087 parse_idl_file( source
, file
, 1 );
1088 else if (strendswith( source
->filename
, ".idl" ))
1089 parse_idl_file( source
, file
, 0 );
1090 else if (strendswith( source
->filename
, ".c" ) ||
1091 strendswith( source
->filename
, ".m" ) ||
1092 strendswith( source
->filename
, ".h" ) ||
1093 strendswith( source
->filename
, ".l" ) ||
1094 strendswith( source
->filename
, ".y" ))
1095 parse_c_file( source
, file
);
1096 else if (strendswith( source
->filename
, ".rc" ))
1097 parse_rc_file( source
, file
);
1098 else if (strendswith( source
->filename
, ".in" ))
1099 parse_in_file( source
, file
);
1101 input_file_name
= NULL
;
1105 /*******************************************************************
1108 * Add a source file to the list.
1110 static struct incl_file
*add_src_file( const char *name
)
1112 struct incl_file
*file
;
1114 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1115 file
= xmalloc( sizeof(*file
) );
1116 memset( file
, 0, sizeof(*file
) );
1117 file
->name
= xstrdup(name
);
1118 list_add_tail( &sources
, &file
->entry
);
1119 parse_file( file
, 1 );
1124 /*******************************************************************
1127 static char *get_make_variable( const char *name
)
1131 for (i
= 0; i
< cmdline_vars
.count
; i
+= 2)
1132 if (!strcmp( cmdline_vars
.str
[i
], name
))
1133 return xstrdup( cmdline_vars
.str
[i
+ 1] );
1135 for (i
= 0; i
< make_vars
.count
; i
+= 2)
1136 if (!strcmp( make_vars
.str
[i
], name
))
1137 return xstrdup( make_vars
.str
[i
+ 1] );
1142 /*******************************************************************
1143 * get_expanded_make_variable
1145 static char *get_expanded_make_variable( const char *name
)
1147 char *p
, *end
, *var
, *expand
, *tmp
;
1149 expand
= get_make_variable( name
);
1150 if (!expand
) return NULL
;
1153 while ((p
= strchr( p
, '$' )))
1157 if (!(end
= strchr( p
+ 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand
);
1159 if (strchr( p
+ 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p
+ 2 );
1160 var
= get_make_variable( p
+ 2 );
1161 tmp
= replace_substr( expand
, p
, end
- p
, var
? var
: "" );
1164 else if (p
[1] == '{') /* don't expand ${} variables */
1166 if (!(end
= strchr( p
+ 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand
);
1170 else if (p
[1] == '$')
1172 tmp
= replace_substr( expand
, p
, 2, "$" );
1174 else fatal_error( "syntax error in '%s'\n", expand
);
1176 /* switch to the new string */
1177 p
= tmp
+ (p
- expand
);
1182 /* consider empty variables undefined */
1184 while (*p
&& isspace(*p
)) p
++;
1185 if (*p
) return expand
;
1191 /*******************************************************************
1192 * get_expanded_make_var_array
1194 static struct strarray
get_expanded_make_var_array( const char *name
)
1196 struct strarray ret
= empty_strarray
;
1197 char *value
, *token
;
1199 if ((value
= get_expanded_make_variable( name
)))
1200 for (token
= strtok( value
, " \t" ); token
; token
= strtok( NULL
, " \t" ))
1201 strarray_add( &ret
, token
);
1206 /*******************************************************************
1209 static int set_make_variable( struct strarray
*array
, const char *assignment
)
1214 p
= name
= xstrdup( assignment
);
1215 while (isalnum(*p
) || *p
== '_') p
++;
1216 if (name
== p
) return 0; /* not a variable */
1220 while (isspace(*p
)) p
++;
1222 if (*p
!= '=') return 0; /* not an assignment */
1224 while (isspace(*p
)) p
++;
1226 /* redefining a variable replaces the previous value */
1227 for (i
= 0; i
< array
->count
; i
+= 2)
1229 if (strcmp( array
->str
[i
], name
)) continue;
1230 array
->str
[i
+ 1] = p
;
1233 strarray_add( array
, name
);
1234 strarray_add( array
, p
);
1239 /*******************************************************************
1242 static void parse_makefile(void)
1247 input_file_name
= strmake( "%s/%s", base_dir
, makefile_name
);
1248 if (!(file
= fopen( input_file_name
, "r" )))
1250 fatal_perror( "open" );
1255 while ((buffer
= get_line( file
)))
1257 if (Separator
&& !strncmp( buffer
, Separator
, strlen(Separator
) )) break;
1258 if (*buffer
== '\t') continue; /* command */
1259 while (isspace( *buffer
)) buffer
++;
1260 if (*buffer
== '#') continue; /* comment */
1261 set_make_variable( &make_vars
, buffer
);
1264 input_file_name
= NULL
;
1268 /*******************************************************************
1269 * add_generated_source
1271 * Add a generated source file to the list.
1273 static struct incl_file
*add_generated_source( const char *name
, const char *filename
)
1275 struct incl_file
*file
;
1277 if ((file
= find_src_file( name
))) return file
; /* we already have it */
1278 file
= xmalloc( sizeof(*file
) );
1279 memset( file
, 0, sizeof(*file
) );
1280 file
->name
= xstrdup( name
);
1281 file
->filename
= xstrdup( filename
? filename
: name
);
1282 file
->flags
= FLAG_GENERATED
;
1283 list_add_tail( &sources
, &file
->entry
);
1288 /*******************************************************************
1289 * add_generated_sources
1291 static void add_generated_sources(void)
1293 struct incl_file
*source
, *next
, *file
;
1295 LIST_FOR_EACH_ENTRY_SAFE( source
, next
, &sources
, struct incl_file
, entry
)
1297 if (source
->flags
& FLAG_IDL_CLIENT
)
1299 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_c.c" ), NULL
);
1300 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1302 if (source
->flags
& FLAG_IDL_SERVER
)
1304 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_s.c" ), NULL
);
1305 add_include( file
, "wine/exception.h", 0 );
1306 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1308 if (source
->flags
& FLAG_IDL_IDENT
)
1310 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_i.c" ), NULL
);
1311 add_include( file
, "rpc.h", 0 );
1312 add_include( file
, "rpcndr.h", 0 );
1313 add_include( file
, "guiddef.h", 0 );
1315 if (source
->flags
& FLAG_IDL_PROXY
)
1317 file
= add_generated_source( "dlldata.o", "dlldata.c" );
1318 add_include( file
, "objbase.h", 0 );
1319 add_include( file
, "rpcproxy.h", 0 );
1320 file
= add_generated_source( replace_extension( source
->name
, ".idl", "_p.c" ), NULL
);
1321 add_include( file
, "objbase.h", 0 );
1322 add_include( file
, "rpcproxy.h", 0 );
1323 add_include( file
, "wine/exception.h", 0 );
1324 add_include( file
, replace_extension( source
->name
, ".idl", ".h" ), 0 );
1326 if (source
->flags
& FLAG_IDL_REGTYPELIB
)
1328 add_generated_source( replace_extension( source
->name
, ".idl", "_t.res" ), NULL
);
1330 if (source
->flags
& FLAG_IDL_REGISTER
)
1332 add_generated_source( replace_extension( source
->name
, ".idl", "_r.res" ), NULL
);
1334 if (strendswith( source
->name
, ".y" ))
1336 file
= add_generated_source( replace_extension( source
->name
, ".y", ".tab.c" ), NULL
);
1337 /* steal the includes list from the source file */
1338 file
->files_count
= source
->files_count
;
1339 file
->files_size
= source
->files_size
;
1340 file
->files
= source
->files
;
1341 source
->files_count
= source
->files_size
= 0;
1342 source
->files
= NULL
;
1344 if (strendswith( source
->name
, ".l" ))
1346 file
= add_generated_source( replace_extension( source
->name
, ".l", ".yy.c" ), NULL
);
1347 /* steal the includes list from the source file */
1348 file
->files_count
= source
->files_count
;
1349 file
->files_size
= source
->files_size
;
1350 file
->files
= source
->files
;
1351 source
->files_count
= source
->files_size
= 0;
1352 source
->files
= NULL
;
1355 if (get_make_variable( "TESTDLL" ))
1357 file
= add_generated_source( "testlist.o", "testlist.c" );
1358 add_include( file
, "wine/test.h", 0 );
1363 /*******************************************************************
1366 static void output_include( struct incl_file
*pFile
, struct incl_file
*owner
)
1370 if (pFile
->owner
== owner
) return;
1371 if (!pFile
->filename
) return;
1372 pFile
->owner
= owner
;
1373 output_filename( pFile
->filename
);
1374 for (i
= 0; i
< pFile
->files_count
; i
++) output_include( pFile
->files
[i
], owner
);
1378 /*******************************************************************
1381 static struct strarray
output_sources(void)
1383 struct incl_file
*source
;
1384 int i
, is_win16
= 0;
1385 const char *dllext
= ".so";
1386 struct strarray object_files
= empty_strarray
;
1387 struct strarray crossobj_files
= empty_strarray
;
1388 struct strarray res_files
= empty_strarray
;
1389 struct strarray clean_files
= empty_strarray
;
1390 struct strarray po_files
= empty_strarray
;
1391 struct strarray mc_files
= empty_strarray
;
1392 struct strarray test_files
= empty_strarray
;
1393 struct strarray dlldata_files
= empty_strarray
;
1394 struct strarray c2man_files
= empty_strarray
;
1395 struct strarray implib_objs
= empty_strarray
;
1396 struct strarray includes
= empty_strarray
;
1397 struct strarray phony_targets
= empty_strarray
;
1398 struct strarray all_targets
= get_expanded_make_var_array( "PROGRAMS" );
1399 struct strarray targetflags
= get_expanded_make_var_array( "TARGETFLAGS" );
1400 struct strarray delayimports
= get_expanded_make_var_array( "DELAYIMPORTS" );
1401 struct strarray extradllflags
= get_expanded_make_var_array( "EXTRADLLFLAGS" );
1402 char *module
= get_expanded_make_variable( "MODULE" );
1403 char *exeext
= get_expanded_make_variable( "EXEEXT" );
1404 char *testdll
= get_expanded_make_variable( "TESTDLL" );
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
)
1424 struct strarray extradefs
;
1425 char *subdir
= NULL
;
1426 char *obj
= xstrdup( source
->name
);
1427 char *ext
= get_extension( obj
);
1429 if (!ext
) fatal_error( "unsupported file type %s\n", source
->name
);
1432 if (src_dir
&& strchr( obj
, '/' ))
1434 subdir
= xstrdup( obj
);
1435 *strrchr( subdir
, '/' ) = 0;
1438 extradefs
= get_expanded_make_var_array( strmake( "%s_EXTRADEFS", obj
));
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
, source
->filename
);
1448 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1449 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o %s.tab.c -d %s\n",
1450 obj
, obj
, source
->filename
);
1451 output( "%s.tab.c: %s %s\n", obj
, source
->filename
, header
);
1452 strarray_add( &clean_files
, strmake( "%s.tab.h", obj
));
1454 else output( "%s.tab.c: %s\n", obj
, source
->filename
);
1456 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1457 output( "\t$(BISON) $(BISONFLAGS) -p %s_ -o $@ %s\n", obj
, source
->filename
);
1459 continue; /* no dependencies */
1461 else if (!strcmp( ext
, "x" )) /* template file */
1463 output( "%s.h: $(MAKEXFTMPL) %s\n", obj
, source
->filename
);
1464 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1465 output( "\t$(MAKEXFTMPL) -H -o $@ %s\n", source
->filename
);
1466 strarray_add( &clean_files
, strmake( "%s.h", obj
));
1467 continue; /* no dependencies */
1469 else if (!strcmp( ext
, "l" )) /* lex file */
1471 output( "%s.yy.c: %s\n", obj
, source
->filename
);
1472 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1473 output( "\t$(FLEX) $(LEXFLAGS) -o$@ %s\n", source
->filename
);
1474 continue; /* no dependencies */
1476 else if (!strcmp( ext
, "rc" )) /* resource file */
1478 if (source
->flags
& FLAG_RC_PO
)
1480 output( "%s.res: $(WRC) $(ALL_MO_FILES) %s\n", obj
, source
->filename
);
1481 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1482 output( "\t$(WRC) -o $@ %s", source
->filename
);
1483 if (is_win16
) output_filename( "-m16" );
1484 else output_filenames( targetflags
);
1485 output_filenames( includes
);
1486 output_filenames( define_args
);
1487 output_filenames( extradefs
);
1488 output_filename( "$(RCFLAGS)" );
1490 output( "%s.res rsrc.pot:", obj
);
1491 strarray_add( &po_files
, source
->filename
);
1495 output( "%s.res: $(WRC) %s\n", obj
, source
->filename
);
1496 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1497 output( "\t$(WRC) -o $@ %s", source
->filename
);
1498 if (is_win16
) output_filename( "-m16" );
1499 else output_filenames( targetflags
);
1500 output_filenames( includes
);
1501 output_filenames( define_args
);
1502 output_filenames( extradefs
);
1503 output_filename( "$(RCFLAGS)" );
1505 output( "%s.res:", obj
);
1507 strarray_add( &res_files
, strmake( "%s.res", obj
));
1509 else if (!strcmp( ext
, "mc" )) /* message file */
1511 output( "%s.res: $(WMC) $(ALL_MO_FILES) %s\n", obj
, source
->filename
);
1512 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1513 output( "\t$(WMC) -U -O res $(PORCFLAGS) -o $@ %s\n", source
->filename
);
1514 strarray_add( &res_files
, strmake( "%s.res", obj
));
1515 strarray_add( &mc_files
, source
->filename
);
1516 output( "msg.pot %s.res:", obj
);
1518 else if (!strcmp( ext
, "idl" )) /* IDL file */
1520 struct strarray targets
= empty_strarray
;
1524 if (!source
->flags
|| find_include_file( strmake( "%s.h", obj
)))
1525 source
->flags
|= FLAG_IDL_HEADER
;
1527 for (i
= 0; i
< sizeof(idl_outputs
) / sizeof(idl_outputs
[0]); i
++)
1529 if (!(source
->flags
& idl_outputs
[i
].flag
)) continue;
1530 dest
= strmake( "%s%s", obj
, idl_outputs
[i
].ext
);
1531 if (!find_src_file( dest
)) strarray_add( &clean_files
, dest
);
1532 strarray_add( &targets
, dest
);
1534 if (source
->flags
& FLAG_IDL_PROXY
) strarray_add( &dlldata_files
, source
->name
);
1535 output_filenames( targets
);
1536 output( ": $(WIDL)\n" );
1537 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1538 output( "\t$(WIDL) -o $@ %s", source
->filename
);
1539 output_filenames( targetflags
);
1540 output_filenames( includes
);
1541 output_filenames( define_args
);
1542 output_filenames( extradefs
);
1543 output_filename( "$(IDLFLAGS)" );
1545 output_filenames( targets
);
1546 output( ": %s", source
->filename
);
1548 else if (!strcmp( ext
, "in" )) /* .in file or man page */
1550 if (strendswith( obj
, ".man" ) && source
->sourcename
)
1552 char *dir
, *dest
= replace_extension( obj
, ".man", "" );
1553 char *lang
= strchr( dest
, '.' );
1557 dir
= strmake( "$(DESTDIR)$(mandir)/%s/man%s", lang
, source
->sourcename
);
1559 else dir
= strmake( "$(DESTDIR)$(mandir)/man%s", source
->sourcename
);
1560 output( "install-man-pages:: %s\n", obj
);
1561 output( "\t$(INSTALL_DATA) %s %s/%s.%s\n",
1562 obj
, dir
, dest
, source
->sourcename
);
1563 output( "uninstall::\n" );
1564 output( "\t$(RM) %s/%s.%s\n",
1565 dir
, dest
, source
->sourcename
);
1568 strarray_add( &all_targets
, xstrdup(obj
) );
1569 strarray_add_uniq( &phony_targets
, "install-man-pages" );
1571 else strarray_add( &clean_files
, xstrdup(obj
) );
1572 output( "%s: %s\n", obj
, source
->filename
);
1573 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1574 output( "\t$(SED_CMD) %s >$@ || ($(RM) $@ && false)\n", source
->filename
);
1575 output( "%s:", obj
);
1577 else if (!strcmp( ext
, "sfd" )) /* font file */
1579 char *fontforge
= get_expanded_make_variable( "FONTFORGE" );
1580 if (fontforge
&& !src_dir
)
1582 output( "%s.ttf: %s\n", obj
, source
->filename
);
1583 output( "\t%s -script %s/fonts/genttf.ff %s $@\n",
1584 fontforge
, top_src_dir
? top_src_dir
: top_obj_dir
, source
->filename
);
1587 continue; /* no dependencies */
1589 else if (!strcmp( ext
, "svg" )) /* svg file */
1591 char *convert
= get_expanded_make_variable( "CONVERT" );
1592 char *rsvg
= get_expanded_make_variable( "RSVG" );
1593 char *icotool
= get_expanded_make_variable( "ICOTOOL" );
1594 if (convert
&& rsvg
&& icotool
&& !src_dir
)
1596 output( "%s.ico %s.bmp: %s\n", obj
, obj
, source
->filename
);
1597 output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" $(BUILDIMAGE) %s $@\n",
1598 convert
, icotool
, rsvg
, source
->filename
);
1603 continue; /* no dependencies */
1605 else if (!strcmp( ext
, "res" ))
1607 strarray_add( &res_files
, source
->name
);
1608 continue; /* no dependencies */
1612 int need_cross
= testdll
|| (source
->flags
& FLAG_C_IMPLIB
) || (module
&& staticlib
);
1614 if (source
->flags
& FLAG_GENERATED
) strarray_add( &clean_files
, source
->filename
);
1615 if (source
->flags
& FLAG_C_IMPLIB
) strarray_add( &implib_objs
, strmake( "%s.o", obj
));
1616 strarray_add( &object_files
, strmake( "%s.o", obj
));
1617 output( "%s.o: %s\n", obj
, source
->filename
);
1618 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1619 output( "\t$(CC) -c -o $@ %s", source
->filename
);
1620 output_filenames( includes
);
1621 output_filenames( define_args
);
1622 output_filenames( extradefs
);
1623 if (module
|| staticlib
|| testdll
) output_filenames( dllflags
);
1624 output_filename( "$(ALLCFLAGS)" );
1626 if (crosstarget
&& need_cross
)
1628 strarray_add( &crossobj_files
, strmake( "%s.cross.o", obj
));
1629 output( "%s.cross.o: %s\n", obj
, source
->filename
);
1630 if (subdir
) output( "\t$(MKDIR_P) -m 755 %s\n", subdir
);
1631 output( "\t$(CROSSCC) -c -o $@ %s", source
->filename
);
1632 output_filenames( includes
);
1633 output_filenames( define_args
);
1634 output_filenames( extradefs
);
1635 output_filename( "-DWINE_CROSSTEST" );
1636 output_filename( "$(ALLCROSSCFLAGS)" );
1639 if (testdll
&& !strcmp( ext
, "c" ) && !(source
->flags
& FLAG_GENERATED
))
1641 strarray_add( &test_files
, source
->name
);
1642 output( "%s.ok:\n", obj
);
1643 output( "\t$(RUNTEST) $(RUNTESTFLAGS) -T %s -M %s -p %s%s %s && touch $@\n", top_obj_dir
,
1644 testdll
, replace_extension( testdll
, ".dll", "_test.exe" ), dllext
, obj
);
1646 if (!strcmp( ext
, "c" ) && !(source
->flags
& FLAG_GENERATED
))
1647 strarray_add( &c2man_files
, source
->filename
);
1648 output( "%s.o", obj
);
1649 if (crosstarget
&& need_cross
) output( " %s.cross.o", obj
);
1654 for (i
= 0; i
< source
->files_count
; i
++) output_include( source
->files
[i
], source
);
1658 /* rules for files that depend on multiple sources */
1662 output( "rsrc.pot: $(WRC)" );
1663 output_filenames( po_files
);
1665 output( "\t$(WRC) -O pot -o $@" );
1666 if (is_win16
) output_filename( "-m16" );
1667 else output_filenames( targetflags
);
1668 output_filenames( includes
);
1669 output_filenames( define_args
);
1670 output_filename( "$(RCFLAGS)" );
1671 output_filenames( po_files
);
1673 strarray_add( &clean_files
, "rsrc.pot" );
1678 output( "msg.pot: $(WMC)" );
1679 output_filenames( mc_files
);
1681 output( "\t$(WMC) -O pot -o $@" );
1682 output_filenames( mc_files
);
1684 strarray_add( &clean_files
, "msg.pot" );
1687 if (dlldata_files
.count
)
1689 output( "dlldata.c: $(WIDL) %s\n", src_dir
? strmake("%s/Makefile.in", src_dir
) : "Makefile.in" );
1690 output( "\t$(WIDL) --dlldata-only -o $@" );
1691 output_filenames( dlldata_files
);
1695 if (module
&& !staticlib
)
1697 char *importlib
= get_expanded_make_variable( "IMPORTLIB" );
1698 struct strarray all_libs
= empty_strarray
;
1699 char *spec_file
= appmode
.count
? NULL
: replace_extension( module
, ".dll", ".spec" );
1701 if (spec_file
&& src_dir
) spec_file
= strmake( "%s/%s", src_dir
, spec_file
);
1702 for (i
= 0; i
< delayimports
.count
; i
++)
1703 strarray_add( &all_libs
, strmake( "-l%s", delayimports
.str
[i
] ));
1704 for (i
= 0; i
< imports
.count
; i
++)
1705 strarray_add( &all_libs
, strmake( "-l%s", imports
.str
[i
] ));
1706 for (i
= 0; i
< delayimports
.count
; i
++)
1707 strarray_add( &all_libs
, strmake( "-Wb,-d%s", delayimports
.str
[i
] ));
1708 strarray_add( &all_libs
, "-lwine" );
1709 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBPORT" ));
1710 strarray_addall( &all_libs
, get_expanded_make_var_array( "EXTRALIBS" ));
1711 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBS" ));
1715 strarray_add( &all_targets
, strmake( "%s%s", module
, dllext
));
1716 strarray_add( &all_targets
, strmake( "%s.fake", module
));
1717 output( "%s%s %s.fake:", module
, dllext
, module
);
1721 strarray_add( &all_targets
, module
);
1722 output( "%s:", module
);
1724 if (spec_file
) output_filename( spec_file
);
1725 output_filenames( object_files
);
1726 output_filenames( res_files
);
1728 output( "\t$(WINEGCC) -o $@" );
1729 output_filenames( targetflags
);
1732 output( " -shared %s", spec_file
);
1733 output_filenames( extradllflags
);
1735 else output_filenames( appmode
);
1736 output_filenames( object_files
);
1737 output_filenames( res_files
);
1738 output_filenames( all_libs
);
1739 output_filename( "$(LDFLAGS)" );
1742 if (spec_file
&& importlib
)
1746 strarray_add( &clean_files
, strmake( "lib%s.def", importlib
));
1747 output( "lib%s.def: %s\n", importlib
, spec_file
);
1748 output( "\t$(WINEBUILD) -w --def -o $@ --export %s", spec_file
);
1749 output_filenames( targetflags
);
1750 if (is_win16
) output_filename( "-m16" );
1752 if (implib_objs
.count
)
1754 strarray_add( &clean_files
, strmake( "lib%s.def.a", importlib
));
1755 output( "lib%s.def.a:", importlib
);
1756 output_filenames( implib_objs
);
1758 output( "\t$(RM) $@\n" );
1759 output( "\t$(AR) $(ARFLAGS) $@" );
1760 output_filenames( implib_objs
);
1762 output( "\t$(RANLIB) $@\n" );
1767 strarray_add( &clean_files
, strmake( "lib%s.a", importlib
));
1768 output( "lib%s.a: %s", importlib
, spec_file
);
1769 output_filenames( implib_objs
);
1771 output( "\t$(WINEBUILD) -w --implib -o $@ --export %s", spec_file
);
1772 output_filenames( targetflags
);
1773 output_filenames( implib_objs
);
1776 if (crosstarget
&& !is_win16
)
1778 struct strarray cross_files
= strarray_replace_extension( &implib_objs
, ".o", ".cross.o" );
1779 strarray_add( &clean_files
, strmake( "lib%s.cross.a", importlib
));
1780 output( "lib%s.cross.a: %s", importlib
, spec_file
);
1781 output_filenames( cross_files
);
1783 output( "\t$(WINEBUILD) -b %s -w --implib -o $@ --export %s", crosstarget
, spec_file
);
1784 output_filenames( cross_files
);
1791 if (c2man_files
.count
&& top_obj_dir
)
1793 char *manext
= get_expanded_make_variable( "api_manext" );
1795 output( "manpages::\n" );
1796 output( "\t$(C2MAN) -w %s -R%s", spec_file
, top_obj_dir
);
1797 output_filename( strmake( "-I%s/include", top_src_dir
? top_src_dir
: top_obj_dir
));
1798 output_filename( strmake( "-o %s/documentation/man%s", top_obj_dir
, manext
? manext
: "3w" ));
1799 output_filenames( c2man_files
);
1801 output( "htmlpages::\n" );
1802 output( "\t$(C2MAN) -Th -w %s -R%s", spec_file
, top_obj_dir
);
1803 output_filename( strmake( "-I%s/include", top_src_dir
? top_src_dir
: top_obj_dir
));
1804 output_filename( strmake( "-o %s/documentation/html", top_obj_dir
));
1805 output_filenames( c2man_files
);
1807 output( "sgmlpages::\n" );
1808 output( "\t$(C2MAN) -Ts -w %s -R%s", spec_file
, top_obj_dir
);
1809 output_filename( strmake( "-I%s/include", top_src_dir
? top_src_dir
: top_obj_dir
));
1810 output_filename( strmake( "-o %s/documentation/api-guide", top_obj_dir
));
1811 output_filenames( c2man_files
);
1813 output( "xmlpages::\n" );
1814 output( "\t$(C2MAN) -Tx -w %s -R%s", spec_file
, top_obj_dir
);
1815 output_filename( strmake( "-I%s/include", top_src_dir
? top_src_dir
: top_obj_dir
));
1816 output_filename( strmake( "-o %s/documentation/api-guide-xml", top_obj_dir
));
1817 output_filenames( c2man_files
);
1819 strarray_add( &phony_targets
, "manpages" );
1820 strarray_add( &phony_targets
, "htmlpages" );
1821 strarray_add( &phony_targets
, "sgmlpages" );
1822 strarray_add( &phony_targets
, "xmlpages" );
1824 else output( "manpages htmlpages sgmlpages xmlpages::\n" );
1830 strarray_add( &all_targets
, staticlib
);
1831 output( "%s:", staticlib
);
1832 output_filenames( object_files
);
1833 output( "\n\t$(RM) $@\n" );
1834 output( "\t$(AR) $(ARFLAGS) $@" );
1835 output_filenames( object_files
);
1836 output( "\n\t$(RANLIB) $@\n" );
1837 if (crosstarget
&& module
)
1839 char *name
= replace_extension( staticlib
, ".a", ".cross.a" );
1841 strarray_add( &all_targets
, name
);
1842 output( "%s:", name
);
1843 output_filenames( crossobj_files
);
1844 output( "\n\t$(RM) $@\n" );
1845 output( "\t%s-ar $(ARFLAGS) $@", crosstarget
);
1846 output_filenames( crossobj_files
);
1847 output( "\n\t%s-ranlib $@\n", crosstarget
);
1853 struct strarray ok_files
= strarray_replace_extension( &test_files
, ".c", ".ok" );
1854 char *testmodule
= replace_extension( testdll
, ".dll", "_test.exe" );
1855 char *stripped
= replace_extension( testdll
, ".dll", "_test-stripped.exe" );
1856 struct strarray all_libs
= empty_strarray
;
1858 for (i
= 0; i
< imports
.count
; i
++) strarray_add( &all_libs
, strmake( "-l%s", imports
.str
[i
] ));
1859 strarray_addall( &all_libs
, get_expanded_make_var_array( "LIBS" ));
1861 strarray_add( &all_targets
, strmake( "%s%s", testmodule
, dllext
));
1862 strarray_add( &clean_files
, strmake( "%s%s", stripped
, dllext
));
1863 output( "%s%s:\n", testmodule
, dllext
);
1864 output( "\t$(WINEGCC) -o $@" );
1865 output_filenames( targetflags
);
1866 output_filenames( appmode
);
1867 output_filenames( object_files
);
1868 output_filenames( res_files
);
1869 output_filenames( all_libs
);
1870 output_filename( "$(LDFLAGS)" );
1872 output( "%s%s:\n", stripped
, dllext
);
1873 output( "\t$(WINEGCC) -s -o $@" );
1874 output_filenames( targetflags
);
1875 output_filename( strmake( "-Wb,-F,%s", testmodule
));
1876 output_filenames( appmode
);
1877 output_filenames( object_files
);
1878 output_filenames( res_files
);
1879 output_filenames( all_libs
);
1880 output_filename( "$(LDFLAGS)" );
1882 output( "%s%s %s%s:", testmodule
, dllext
, stripped
, dllext
);
1883 output_filenames( object_files
);
1884 output_filenames( res_files
);
1889 char *testres
= replace_extension( testdll
, ".dll", "_test.res" );
1890 output( "all: %s/programs/winetest/%s\n", top_obj_dir
, testres
);
1891 output( "%s/programs/winetest/%s: %s%s\n", top_obj_dir
, testres
, stripped
, dllext
);
1892 output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | $(WRC) -o $@\n",
1893 testmodule
, stripped
, dllext
);
1898 char *crosstest
= replace_extension( testdll
, ".dll", "_crosstest.exe" );
1900 strarray_add( &clean_files
, crosstest
);
1901 output( "crosstest: %s\n", crosstest
);
1902 output( "%s:", crosstest
);
1903 output_filenames( crossobj_files
);
1904 output_filenames( res_files
);
1906 output( "\t$(CROSSWINEGCC) -o $@ -b %s", crosstarget
);
1907 output_filenames( crossobj_files
);
1908 output_filenames( res_files
);
1909 output_filenames( all_libs
);
1910 output_filename( "$(LDFLAGS)" );
1912 strarray_add( &phony_targets
, "crosstest" );
1915 output( "testlist.c: $(MAKECTESTS) %s\n", src_dir
? strmake("%s/Makefile.in", src_dir
) : "Makefile.in" );
1916 output( "\t$(MAKECTESTS) -o $@" );
1917 output_filenames( test_files
);
1919 output_filenames( ok_files
);
1920 output( ": %s%s ../%s%s\n", testmodule
, dllext
, testdll
, dllext
);
1921 output( "check test:" );
1922 output_filenames( ok_files
);
1924 output( "testclean::\n" );
1925 output( "\t$(RM)" );
1926 output_filenames( ok_files
);
1928 strarray_addall( &clean_files
, ok_files
);
1929 strarray_add( &phony_targets
, "check" );
1930 strarray_add( &phony_targets
, "test" );
1931 strarray_add( &phony_targets
, "testclean" );
1934 if (all_targets
.count
)
1937 output_filenames( all_targets
);
1941 strarray_addall( &clean_files
, object_files
);
1942 strarray_addall( &clean_files
, crossobj_files
);
1943 strarray_addall( &clean_files
, res_files
);
1944 strarray_addall( &clean_files
, all_targets
);
1945 strarray_addall( &clean_files
, get_expanded_make_var_array( "EXTRA_TARGETS" ));
1947 if (clean_files
.count
)
1949 output( "clean::\n" );
1950 output( "\t$(RM)" );
1951 output_filenames( clean_files
);
1953 strarray_add( &phony_targets
, "clean" );
1958 output( "depend:\n" );
1959 output( "\t@cd %s && $(MAKE) %s/depend\n", top_obj_dir
, base_dir
);
1960 strarray_add( &phony_targets
, "depend" );
1963 if (phony_targets
.count
)
1965 output( ".PHONY:" );
1966 output_filenames( phony_targets
);
1974 /*******************************************************************
1977 static FILE *create_temp_file( const char *orig
)
1979 char *name
= xmalloc( strlen(orig
) + 13 );
1980 unsigned int i
, id
= getpid();
1984 for (i
= 0; i
< 100; i
++)
1986 sprintf( name
, "%s.tmp%08x", orig
, id
);
1987 if ((fd
= open( name
, O_RDWR
| O_CREAT
| O_EXCL
, 0666 )) != -1)
1989 ret
= fdopen( fd
, "w" );
1992 if (errno
!= EEXIST
) break;
1995 if (!ret
) fatal_error( "failed to create output file for '%s'\n", orig
);
1996 temp_file_name
= name
;
2001 /*******************************************************************
2004 static void rename_temp_file( const char *dest
)
2006 int ret
= rename( temp_file_name
, dest
);
2007 if (ret
== -1 && errno
== EEXIST
)
2009 /* rename doesn't overwrite on windows */
2011 ret
= rename( temp_file_name
, dest
);
2013 if (ret
== -1) fatal_error( "failed to rename output file to '%s'\n", dest
);
2014 temp_file_name
= NULL
;
2018 /*******************************************************************
2021 static void output_gitignore( const char *dest
, const struct strarray
*files
)
2025 output_file
= create_temp_file( dest
);
2027 output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
2028 output( "/.gitignore\n" );
2029 output( "/Makefile\n" );
2030 for (i
= 0; i
< files
->count
; i
++)
2032 if (!strchr( files
->str
[i
], '/' )) output( "/" );
2033 output( "%s\n", files
->str
[i
] );
2036 if (fclose( output_file
)) fatal_perror( "write" );
2038 rename_temp_file( dest
);
2042 /*******************************************************************
2043 * output_dependencies
2045 static void output_dependencies( const char *path
)
2047 struct strarray targets
= empty_strarray
;
2049 if (Separator
&& ((output_file
= fopen( path
, "r" ))))
2052 FILE *tmp_file
= create_temp_file( path
);
2055 while (fgets( buffer
, sizeof(buffer
), output_file
) && !found
)
2057 if (fwrite( buffer
, 1, strlen(buffer
), tmp_file
) != strlen(buffer
)) fatal_perror( "write" );
2058 found
= !strncmp( buffer
, Separator
, strlen(Separator
) );
2060 if (fclose( output_file
)) fatal_perror( "write" );
2061 output_file
= tmp_file
;
2062 if (!found
) output( "\n%s\n", Separator
);
2066 if (!(output_file
= fopen( path
, Separator
? "a" : "w" )))
2067 fatal_perror( "%s", path
);
2070 targets
= output_sources();
2072 fclose( output_file
);
2074 if (temp_file_name
) rename_temp_file( path
);
2076 if (!src_dir
) output_gitignore( strmake( "%s/.gitignore", base_dir
), &targets
);
2080 /*******************************************************************
2083 static void update_makefile( const char *path
)
2085 static const char *source_vars
[] =
2104 struct strarray value
;
2105 struct incl_file
*file
;
2108 output_file_name
= strmake( "%s/%s", base_dir
, makefile_name
);
2111 src_dir
= get_expanded_make_variable( "srcdir" );
2112 top_src_dir
= get_expanded_make_variable( "top_srcdir" );
2113 top_obj_dir
= get_expanded_make_variable( "top_builddir" );
2114 parent_dir
= get_expanded_make_variable( "PARENTSRC" );
2116 appmode
= get_expanded_make_var_array( "APPMODE" );
2117 dllflags
= get_expanded_make_var_array( "DLLFLAGS" );
2118 imports
= get_expanded_make_var_array( "IMPORTS" );
2120 for (i
= 0; i
< appmode
.count
&& !use_msvcrt
; i
++)
2121 use_msvcrt
= !strcmp( appmode
.str
[i
], "-mno-cygwin" );
2122 for (i
= 0; i
< imports
.count
&& !use_msvcrt
; i
++)
2123 use_msvcrt
= !strncmp( imports
.str
[i
], "msvcr", 5 );
2125 include_args
= empty_strarray
;
2126 define_args
= empty_strarray
;
2127 strarray_add( &define_args
, "-D__WINESRC__" );
2129 value
= get_expanded_make_var_array( "EXTRAINCL" );
2130 for (i
= 0; i
< value
.count
; i
++)
2131 if (!strncmp( value
.str
[i
], "-I", 2 ))
2132 strarray_add_uniq( &include_args
, value
.str
[i
] );
2134 strarray_add_uniq( &define_args
, value
.str
[i
] );
2135 strarray_addall( &define_args
, get_expanded_make_var_array( "EXTRADEFS" ));
2141 strarray_add( &dllflags
, get_expanded_make_variable( "MSVCRTFLAGS" ));
2142 strarray_add( &include_args
,
2143 strmake( "-I%s/include/msvcrt", top_src_dir
? top_src_dir
: top_obj_dir
));
2146 list_init( &sources
);
2147 list_init( &includes
);
2149 for (var
= source_vars
; *var
; var
++)
2151 value
= get_expanded_make_var_array( *var
);
2152 for (i
= 0; i
< value
.count
; i
++) add_src_file( value
.str
[i
] );
2155 add_generated_sources();
2157 value
= get_expanded_make_var_array( "EXTRA_OBJS" );
2158 for (i
= 0; i
< value
.count
; i
++)
2160 /* default to .c for unknown extra object files */
2161 if (strendswith( value
.str
[i
], ".o" ))
2162 add_generated_source( value
.str
[i
], replace_extension( value
.str
[i
], ".o", ".c" ) );
2164 add_generated_source( value
.str
[i
], NULL
);
2167 LIST_FOR_EACH_ENTRY( file
, &includes
, struct incl_file
, entry
) parse_file( file
, 0 );
2168 output_dependencies( output_file_name
);
2169 output_file_name
= NULL
;
2173 /*******************************************************************
2176 static void parse_makeflags( const char *flags
)
2178 const char *p
= flags
;
2179 char *var
, *buffer
= xmalloc( strlen(flags
) + 1 );
2183 while (isspace(*p
)) p
++;
2185 while (*p
&& !isspace(*p
))
2187 if (*p
== '\\' && p
[1]) p
++;
2191 if (var
> buffer
) set_make_variable( &cmdline_vars
, buffer
);
2196 /*******************************************************************
2199 static int parse_option( const char *opt
)
2203 if (strchr( opt
, '=' )) return set_make_variable( &cmdline_vars
, opt
);
2209 if (opt
[2]) strarray_add_uniq( &include_args
, opt
);
2215 top_src_dir
= opt
+ 2;
2218 top_obj_dir
= opt
+ 2;
2221 parent_dir
= opt
+ 2;
2224 if (opt
[2]) makefile_name
= opt
+ 2;
2227 parse_makefile_mode
= 1;
2230 relative_dir_mode
= 1;
2233 if (opt
[2]) Separator
= opt
+ 2;
2234 else Separator
= NULL
;
2237 break; /* ignored */
2239 fprintf( stderr
, "Unknown option '%s'\n%s", opt
, Usage
);
2246 /*******************************************************************
2249 int main( int argc
, char *argv
[] )
2251 const char *makeflags
= getenv( "MAKEFLAGS" );
2252 struct incl_file
*pFile
;
2255 if (makeflags
) parse_makeflags( makeflags
);
2260 if (parse_option( argv
[i
] ))
2262 for (j
= i
; j
< argc
; j
++) argv
[j
] = argv
[j
+1];
2268 if (relative_dir_mode
)
2274 fprintf( stderr
, "Option -r needs two directories\n%s", Usage
);
2277 relpath
= get_relative_path( argv
[1], argv
[2] );
2278 printf( "%s\n", relpath
? relpath
: "." );
2282 atexit( cleanup_files
);
2283 signal( SIGTERM
, exit_on_signal
);
2284 signal( SIGINT
, exit_on_signal
);
2286 signal( SIGHUP
, exit_on_signal
);
2289 if (parse_makefile_mode
)
2291 for (i
= 1; i
< argc
; i
++) update_makefile( argv
[i
] );
2296 for (i
= 1; i
< argc
; i
++) add_src_file( argv
[i
] );
2297 add_generated_sources();
2299 LIST_FOR_EACH_ENTRY( pFile
, &includes
, struct incl_file
, entry
) parse_file( pFile
, 0 );
2300 output_dependencies( makefile_name
);