makedep: Add more helpers for file output and error handling.

This commit is contained in:
Alexandre Julliard 2013-10-14 11:11:07 +02:00
parent cf34a967cc
commit c3aa495295
1 changed files with 161 additions and 125 deletions

View File

@ -74,11 +74,12 @@ static const char *top_src_dir;
static const char *top_obj_dir;
static const char *OutputFileName = "Makefile";
static const char *Separator = "### Dependencies";
static const char *ProgramName;
static const char *input_file_name;
static int input_line;
static FILE *output_file;
static const char Usage[] =
"Usage: %s [options] [files]\n"
"Usage: makedep [options] [files]\n"
"Options:\n"
" -Idir Search for include files in directory 'dir'\n"
" -Cdir Search for source files in directory 'dir'\n"
@ -88,6 +89,14 @@ static const char Usage[] =
" -sxxx Use 'xxx' as separator (default: \"### Dependencies\")\n";
#ifndef __GNUC__
#define __attribute__(x)
#endif
static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
static int output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
/*******************************************************************
* fatal_error
*/
@ -95,12 +104,39 @@ static void fatal_error( const char *msg, ... )
{
va_list valist;
va_start( valist, msg );
if (input_file_name)
{
fprintf( stderr, "%s:", input_file_name );
if (input_line) fprintf( stderr, "%d:", input_line );
fprintf( stderr, " error: " );
}
else fprintf( stderr, "makedep: error: " );
vfprintf( stderr, msg, valist );
va_end( valist );
exit(1);
}
/*******************************************************************
* fatal_perror
*/
static void fatal_perror( const char *msg, ... )
{
va_list valist;
va_start( valist, msg );
if (input_file_name)
{
fprintf( stderr, "%s:", input_file_name );
if (input_line) fprintf( stderr, "%d:", input_line );
fprintf( stderr, " error:" );
}
else fprintf( stderr, "makedep: error:" );
perror( " " );
va_end( valist );
exit(1);
}
/*******************************************************************
* xmalloc
*/
@ -108,7 +144,7 @@ static void *xmalloc( size_t size )
{
void *res;
if (!(res = malloc (size ? size : 1)))
fatal_error( "%s: error: Virtual memory exhausted.\n", ProgramName );
fatal_error( "Virtual memory exhausted.\n" );
return res;
}
@ -121,7 +157,7 @@ static void *xrealloc (void *ptr, size_t size)
void *res;
assert( size );
if (!(res = realloc( ptr, size )))
fatal_error( "%s: error: Virtual memory exhausted.\n", ProgramName );
fatal_error( "Virtual memory exhausted.\n" );
return res;
}
@ -131,7 +167,7 @@ static void *xrealloc (void *ptr, size_t size)
static char *xstrdup( const char *str )
{
char *res = strdup( str );
if (!res) fatal_error( "%s: error: Virtual memory exhausted.\n", ProgramName );
if (!res) fatal_error( "Virtual memory exhausted.\n" );
return res;
}
@ -170,6 +206,37 @@ static int strendswith( const char* str, const char* end )
return l >= m && strcmp(str + l - m, end) == 0;
}
/*******************************************************************
* output
*/
static int output( const char *format, ... )
{
int ret;
va_list valist;
va_start( valist, format );
ret = vfprintf( output_file, format, valist );
va_end( valist );
if (ret < 0) fatal_perror( "output" );
return ret;
}
/*******************************************************************
* output_filename
*/
static void output_filename( const char *name, int *column )
{
if (*column + strlen(name) + 1 > 100)
{
output( " \\\n" );
*column = 0;
}
*column += output( " %s", name );
}
/*******************************************************************
* get_extension
*/
@ -278,7 +345,7 @@ static struct incl_file *find_include_file( const char *name )
*
* Add an include file if it doesn't already exists.
*/
static struct incl_file *add_include( struct incl_file *pFile, const char *name, int line, int system )
static struct incl_file *add_include( struct incl_file *pFile, const char *name, int system )
{
struct incl_file *include;
char *ext;
@ -286,37 +353,29 @@ static struct incl_file *add_include( struct incl_file *pFile, const char *name,
for (pos = 0; pos < MAX_INCLUDES; pos++) if (!pFile->files[pos]) break;
if (pos >= MAX_INCLUDES)
fatal_error( "%s: %s: error: too many included files, please fix MAX_INCLUDES\n",
ProgramName, pFile->name );
fatal_error( "too many included files, please fix MAX_INCLUDES\n" );
/* enforce some rules for the Wine tree */
if (!memcmp( name, "../", 3 ))
fatal_error( "%s:%d: error: #include directive with relative path not allowed\n",
pFile->filename, line );
fatal_error( "#include directive with relative path not allowed\n" );
if (!strcmp( name, "config.h" ))
{
if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
fatal_error( "%s:%d: error: config.h must not be included by a header file\n",
pFile->filename, line );
fatal_error( "config.h must not be included by a header file\n" );
if (pos)
fatal_error( "%s:%d: error: config.h must be included before anything else\n",
pFile->filename, line );
fatal_error( "config.h must be included before anything else\n" );
}
else if (!strcmp( name, "wine/port.h" ))
{
if ((ext = strrchr( pFile->filename, '.' )) && !strcmp( ext, ".h" ))
fatal_error( "%s:%d: error: wine/port.h must not be included by a header file\n",
pFile->filename, line );
if (!pos) fatal_error( "%s:%d: error: config.h must be included before wine/port.h\n",
pFile->filename, line );
fatal_error( "wine/port.h must not be included by a header file\n" );
if (!pos) fatal_error( "config.h must be included before wine/port.h\n" );
if (pos > 1)
fatal_error( "%s:%d: error: wine/port.h must be included before everything except config.h\n",
pFile->filename, line );
fatal_error( "wine/port.h must be included before everything except config.h\n" );
if (strcmp( pFile->files[0]->name, "config.h" ))
fatal_error( "%s:%d: error: config.h must be included before wine/port.h\n",
pFile->filename, line );
fatal_error( "config.h must be included before wine/port.h\n" );
}
LIST_FOR_EACH_ENTRY( include, &includes, struct incl_file, entry )
@ -326,7 +385,7 @@ static struct incl_file *add_include( struct incl_file *pFile, const char *name,
memset( include, 0, sizeof(*include) );
include->name = xstrdup(name);
include->included_by = pFile;
include->included_line = line;
include->included_line = input_line;
include->system = system;
list_add_tail( &includes, &include->entry );
found:
@ -354,12 +413,7 @@ static FILE *open_src_file( struct incl_file *pFile )
pFile->filename = strmake( "%s/%s", src_dir, pFile->name );
file = fopen( pFile->filename, "r" );
}
if (!file)
{
fprintf( stderr, "%s: error: ", ProgramName );
perror( pFile->name );
exit(1);
}
if (!file) fatal_perror( "open %s", pFile->name );
return file;
}
@ -557,14 +611,14 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file
{
char *buffer, *include;
input_line = 0;
if (for_h_file)
{
/* generated .h file always includes these */
add_include( pFile, "rpc.h", 0, 1 );
add_include( pFile, "rpcndr.h", 0, 1 );
add_include( pFile, "rpc.h", 1 );
add_include( pFile, "rpcndr.h", 1 );
}
input_line = 0;
while ((buffer = get_line( file )))
{
char quote;
@ -578,11 +632,10 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file
if (*p != '"') continue;
include = ++p;
while (*p && (*p != '"')) p++;
if (!*p) fatal_error( "%s:%d: error: Malformed import directive\n",
pFile->filename, input_line );
if (!*p) fatal_error( "malformed import directive\n" );
*p = 0;
if (for_h_file && strendswith( include, ".idl" )) strcpy( p - 4, ".h" );
add_include( pFile, include, input_line, 0 );
add_include( pFile, include, 0 );
continue;
}
@ -612,11 +665,10 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file
include = p;
while (*p && (*p != quote)) p++;
if (!*p || (quote == '"' && p[-1] != '\\'))
fatal_error( "%s:%d: error: Malformed #include directive inside cpp_quote\n",
pFile->filename, input_line );
fatal_error( "malformed #include directive inside cpp_quote\n" );
if (quote == '"') p--; /* remove backslash */
*p = 0;
add_include( pFile, include, input_line, (quote == '>') );
add_include( pFile, include, (quote == '>') );
continue;
}
@ -631,10 +683,9 @@ static void parse_idl_file( struct incl_file *pFile, FILE *file, int for_h_file
if (quote == '<') quote = '>';
include = p;
while (*p && (*p != quote)) p++;
if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n",
pFile->filename, input_line );
if (!*p) fatal_error( "malformed #include directive\n" );
*p = 0;
add_include( pFile, include, input_line, (quote == '>') );
add_include( pFile, include, (quote == '>') );
}
}
@ -662,10 +713,9 @@ static void parse_c_file( struct incl_file *pFile, FILE *file )
if (quote == '<') quote = '>';
include = p;
while (*p && (*p != quote)) p++;
if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n",
pFile->filename, input_line );
if (!*p) fatal_error( "malformed #include directive\n" );
*p = 0;
add_include( pFile, include, input_line, (quote == '>') );
add_include( pFile, include, (quote == '>') );
}
}
@ -703,7 +753,7 @@ static void parse_rc_file( struct incl_file *pFile, FILE *file )
while (*p && !isspace(*p) && *p != '*') p++;
}
if (!*p)
fatal_error( "%s:%d: error: Malformed makedep comment\n", pFile->filename, input_line );
fatal_error( "malformed makedep comment\n" );
*p = 0;
}
else /* check for #include */
@ -718,11 +768,10 @@ static void parse_rc_file( struct incl_file *pFile, FILE *file )
if (quote == '<') quote = '>';
include = p;
while (*p && (*p != quote)) p++;
if (!*p) fatal_error( "%s:%d: error: Malformed #include directive\n",
pFile->filename, input_line );
if (!*p) fatal_error( "malformed #include directive\n" );
*p = 0;
}
add_include( pFile, include, input_line, (quote == '>') );
add_include( pFile, include, (quote == '>') );
}
}
@ -741,30 +790,30 @@ static void parse_generated_idl( struct incl_file *source )
if (strendswith( source->name, "_c.c" ))
{
add_include( source, header, 0, 0 );
add_include( source, header, 0 );
}
else if (strendswith( source->name, "_i.c" ))
{
add_include( source, "rpc.h", 0, 1 );
add_include( source, "rpcndr.h", 0, 1 );
add_include( source, "guiddef.h", 0, 1 );
add_include( source, "rpc.h", 1 );
add_include( source, "rpcndr.h", 1 );
add_include( source, "guiddef.h", 1 );
}
else if (strendswith( source->name, "_p.c" ))
{
add_include( source, "objbase.h", 0, 1 );
add_include( source, "rpcproxy.h", 0, 1 );
add_include( source, "wine/exception.h", 0, 1 );
add_include( source, header, 0, 0 );
add_include( source, "objbase.h", 1 );
add_include( source, "rpcproxy.h", 1 );
add_include( source, "wine/exception.h", 1 );
add_include( source, header, 0 );
}
else if (strendswith( source->name, "_s.c" ))
{
add_include( source, "wine/exception.h", 0, 1 );
add_include( source, header, 0, 0 );
add_include( source, "wine/exception.h", 1 );
add_include( source, header, 0 );
}
else if (!strcmp( source->name, "dlldata.c" ))
{
add_include( source, "objbase.h", 0, 1 );
add_include( source, "rpcproxy.h", 0, 1 );
add_include( source, "objbase.h", 1 );
add_include( source, "rpcproxy.h", 1 );
}
free( header );
@ -800,6 +849,7 @@ static void parse_file( struct incl_file *pFile, int src )
file = src ? open_src_file( pFile ) : open_include_file( pFile );
if (!file) return;
input_file_name = pFile->filename;
if (pFile->sourcename && strendswith( pFile->sourcename, ".idl" ))
parse_idl_file( pFile, file, 1 );
@ -814,6 +864,7 @@ static void parse_file( struct incl_file *pFile, int src )
else if (strendswith( pFile->filename, ".rc" ))
parse_rc_file( pFile, file );
fclose(file);
input_file_name = NULL;
}
@ -839,56 +890,55 @@ static struct incl_file *add_src_file( const char *name )
/*******************************************************************
* output_include
*/
static void output_include( FILE *file, struct incl_file *pFile,
struct incl_file *owner, int *column )
static void output_include( struct incl_file *pFile, struct incl_file *owner, int *column )
{
int i;
if (pFile->owner == owner) return;
if (!pFile->filename) return;
pFile->owner = owner;
if (*column + strlen(pFile->filename) + 1 > 70)
{
fprintf( file, " \\\n" );
*column = 0;
}
fprintf( file, " %s", pFile->filename );
*column += strlen(pFile->filename) + 1;
output_filename( pFile->filename, column );
for (i = 0; i < MAX_INCLUDES; i++)
if (pFile->files[i]) output_include( file, pFile->files[i],
owner, column );
if (pFile->files[i]) output_include( pFile->files[i], owner, column );
}
/*******************************************************************
* output_src
* output_sources
*/
static int output_src( FILE *file, struct incl_file *pFile, int *column )
static void output_sources(void)
{
char *obj = xstrdup( pFile->name );
char *ext = get_extension( obj );
if (ext)
struct incl_file *source;
int i, column;
LIST_FOR_EACH_ENTRY( source, &sources, struct incl_file, entry )
{
char *obj = xstrdup( source->name );
char *ext = get_extension( obj );
if (!ext) fatal_error( "unsupported file type %s\n", source->name );
*ext++ = 0;
column = 0;
if (!strcmp( ext, "y" )) /* yacc file */
{
/* add source file dependency for parallel makes */
char *header = strmake( "%s.tab.h", obj );
if (find_include_file( header )) fprintf( file, "%s.tab.c: %s\n", obj, header );
if (find_include_file( header )) output( "%s.tab.c: %s\n", obj, header );
free( header );
*column += fprintf( file, "%s.tab.o: %s.tab.c", obj, obj );
column += output( "%s.tab.o: %s.tab.c", obj, obj );
}
else if (!strcmp( ext, "l" )) /* lex file */
{
*column += fprintf( file, "%s.yy.o: %s.yy.c", obj, obj );
column += output( "%s.yy.o: %s.yy.c", obj, obj );
}
else if (!strcmp( ext, "rc" )) /* resource file */
{
*column += fprintf( file, "rsrc.pot %s.res: %s", obj, pFile->filename );
column += output( "rsrc.pot %s.res: %s", obj, source->filename );
}
else if (!strcmp( ext, "mc" )) /* message file */
{
*column += fprintf( file, "msg.pot %s.res: %s", obj, pFile->filename );
column += output( "msg.pot %s.res: %s", obj, source->filename );
}
else if (!strcmp( ext, "idl" )) /* IDL file */
{
@ -897,11 +947,11 @@ static int output_src( FILE *file, struct incl_file *pFile, int *column )
const char *suffix = "cips";
name = strmake( "%s.tlb", obj );
if (find_src_file( name )) *column += fprintf( file, "%s %s_t.res", name, obj );
if (find_src_file( name )) column += output( "%s %s_t.res", name, obj );
else
{
got_header = 1;
*column += fprintf( file, "%s.h", obj );
column += output( "%s.h", obj );
}
free( name );
@ -910,33 +960,36 @@ static int output_src( FILE *file, struct incl_file *pFile, int *column )
name = strmake( "%s_%c.c", obj, *suffix );
if (find_src_file( name ))
{
if (!got_header++) *column += fprintf( file, " %s.h", obj );
*column += fprintf( file, " %s", name );
if (!got_header++) column += output( " %s.h", obj );
column += output( " %s", name );
}
free( name );
suffix++;
}
name = strmake( "%s_r.res", obj );
if (find_src_file( name )) *column += fprintf( file, " %s", name );
if (find_src_file( name )) column += output( " %s", name );
free( name );
*column += fprintf( file, ": %s", pFile->filename );
column += output( ": %s", source->filename );
}
else if (!strcmp( ext, "tlb" ) || !strcmp( ext, "res" ))
{
return 0; /* nothing to do for typelib files */
continue; /* nothing to do for typelib files */
}
else
{
struct object_extension *ext;
LIST_FOR_EACH_ENTRY( ext, &object_extensions, struct object_extension, entry )
*column += fprintf( file, "%s.%s ", obj, ext->extension );
*column += fprintf( file, ": %s", pFile->filename );
column += output( "%s.%s ", obj, ext->extension );
column += output( ": %s", source->filename );
}
free( obj );
for (i = 0; i < MAX_INCLUDES; i++)
if (source->files[i]) output_include( source->files[i], source, &column );
output( "\n" );
}
free( obj );
return 1;
}
@ -961,8 +1014,7 @@ static FILE *create_temp_file( char **tmp_name )
if (errno != EEXIST) break;
id += 7777;
}
if (!ret) fatal_error( "%s: error: failed to create output file for '%s'\n",
ProgramName, OutputFileName );
if (!ret) fatal_error( "failed to create output file for '%s'\n", OutputFileName );
*tmp_name = name;
return ret;
}
@ -973,45 +1025,34 @@ static FILE *create_temp_file( char **tmp_name )
*/
static void output_dependencies(void)
{
struct incl_file *pFile;
int i, column;
FILE *file = NULL;
char *tmp_name = NULL;
if (Separator && ((file = fopen( OutputFileName, "r" ))))
if (Separator && ((output_file = fopen( OutputFileName, "r" ))))
{
char buffer[1024];
FILE *tmp_file = create_temp_file( &tmp_name );
int found = 0;
while (fgets( buffer, sizeof(buffer), file ) && !found)
while (fgets( buffer, sizeof(buffer), output_file ) && !found)
{
if (fwrite( buffer, 1, strlen(buffer), tmp_file ) != strlen(buffer))
fatal_error( "%s: error: failed to write to %s\n", ProgramName, tmp_name );
fatal_error( "failed to write to %s\n", tmp_name );
found = !strncmp( buffer, Separator, strlen(Separator) );
}
fclose( file );
file = tmp_file;
if (!found && list_head(&sources)) fprintf( file, "\n%s\n", Separator );
fclose( output_file );
output_file = tmp_file;
if (!found && list_head(&sources)) output( "\n%s\n", Separator );
}
else
{
if (!(file = fopen( OutputFileName, Separator ? "a" : "w" )))
{
perror( OutputFileName );
exit(1);
}
if (!(output_file = fopen( OutputFileName, Separator ? "a" : "w" )))
fatal_perror( "%s", OutputFileName );
}
LIST_FOR_EACH_ENTRY( pFile, &sources, struct incl_file, entry )
{
column = 0;
if (!output_src( file, pFile, &column )) continue;
for (i = 0; i < MAX_INCLUDES; i++)
if (pFile->files[i]) output_include( file, pFile->files[i],
pFile, &column );
fprintf( file, "\n" );
}
fclose( file );
output_sources();
fclose( output_file );
output_file = NULL;
if (tmp_name)
{
@ -1025,8 +1066,7 @@ static void output_dependencies(void)
if (ret == -1)
{
unlink( tmp_name );
fatal_error( "%s: error: failed to rename output file to '%s'\n",
ProgramName, OutputFileName );
fatal_error( "failed to rename output file to '%s'\n", OutputFileName );
}
free( tmp_name );
}
@ -1063,8 +1103,7 @@ static void parse_option( const char *opt )
if (opt[2]) add_object_extension( opt + 2 );
break;
default:
fprintf( stderr, "Unknown option '%s'\n", opt );
fprintf( stderr, Usage, ProgramName );
fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
exit(1);
}
}
@ -1079,9 +1118,6 @@ int main( int argc, char *argv[] )
struct incl_path *path, *next;
int i, j;
if ((ProgramName = strrchr( argv[0], '/' ))) ProgramName++;
else ProgramName = argv[0];
i = 1;
while (i < argc)
{