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