winebuild: Unify the strarray implementation with the one from makedep.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Alexandre Julliard 2021-09-23 11:35:48 +02:00
parent 8ec2321328
commit d6d5a21721
6 changed files with 135 additions and 139 deletions

View File

@ -171,9 +171,9 @@ static inline int is_pe(void)
struct strarray
{
unsigned int count; /* strings in use */
unsigned int size; /* total allocated size */
const char **str;
unsigned int count;
unsigned int max;
};
/* entry point flags */
@ -248,9 +248,11 @@ extern char *strupper(char *s);
extern int strendswith(const char* str, const char* end);
extern char *strmake(const char* fmt, ...) __attribute__((__format__ (__printf__, 1, 2 )));
extern struct strarray strarray_fromstring( const char *str, const char *delim );
extern void strarray_add( struct strarray *array, ... );
extern void strarray_addv( struct strarray *array, char * const *argv );
extern void strarray_add( struct strarray *array, const char *str );
extern void strarray_addall( struct strarray *array, struct strarray args );
extern void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) );
extern const char *strarray_bsearch( const struct strarray *array, const char *str,
int (*func)(const char **, const char **) );
extern DECLSPEC_NORETURN void fatal_error( const char *msg, ... )
__attribute__ ((__format__ (__printf__, 1, 2)));
extern DECLSPEC_NORETURN void fatal_perror( const char *msg, ... )

View File

@ -123,7 +123,7 @@ static const char *stdc_names[] =
"wcstoul"
};
static struct strarray stdc_functions = { stdc_names, ARRAY_SIZE(stdc_names), ARRAY_SIZE(stdc_names) };
static const struct strarray stdc_functions = { ARRAY_SIZE(stdc_names), ARRAY_SIZE(stdc_names), stdc_names };
struct import_func
{
@ -172,9 +172,9 @@ static inline const char *ppc_reg( int reg )
}
/* compare function names; helper for resolve_imports */
static int name_cmp( const void *name, const void *entry )
static int name_cmp( const char **name, const char **entry )
{
return strcmp( *(const char* const *)name, *(const char* const *)entry );
return strcmp( *name, *entry );
}
/* compare function names; helper for resolve_imports */
@ -196,18 +196,15 @@ static inline void remove_name( struct strarray *table, unsigned int idx )
}
/* locate a name in a (sorted) list */
static inline const char *find_name( const char *name, const struct strarray *table )
static inline const char *find_name( const char *name, struct strarray table )
{
char **res = NULL;
if (table->count) res = bsearch( &name, table->str, table->count, sizeof(*table->str), name_cmp );
return res ? *res : NULL;
return strarray_bsearch( &table, name, name_cmp );
}
/* sort a name table */
static inline void sort_names( struct strarray *table )
{
if (table->count) qsort( table->str, table->count, sizeof(*table->str), name_cmp );
strarray_qsort( table, name_cmp );
}
/* locate an export in a (sorted) export list */
@ -382,7 +379,7 @@ void add_delayed_import( const char *name )
struct import *imp;
char *fullname = get_dll_name( name, NULL );
strarray_add( &delayed_imports, fullname, NULL );
strarray_add( &delayed_imports, fullname );
if ((imp = find_import_dll( fullname )))
{
list_remove( &imp->entry );
@ -393,7 +390,7 @@ void add_delayed_import( const char *name )
/* add a symbol to the list of extra symbols that ld must resolve */
void add_extra_ld_symbol( const char *name )
{
strarray_add( &extra_ld_symbols, name, NULL );
strarray_add( &extra_ld_symbols, name );
}
/* retrieve an imported dll, adding one if necessary */
@ -546,7 +543,7 @@ static void check_undefined_exports( DLLSPEC *spec )
if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
if (odp->flags & FLAG_FORWARD) continue;
if (odp->flags & FLAG_SYSCALL) continue;
if (find_name( odp->link_name, &undef_symbols ))
if (find_name( odp->link_name, undef_symbols ))
{
switch(odp->type)
{
@ -557,7 +554,7 @@ static void check_undefined_exports( DLLSPEC *spec )
if (link_ext_symbols)
{
odp->flags |= FLAG_EXT_LINK;
strarray_add( &ext_link_imports, odp->link_name, NULL );
strarray_add( &ext_link_imports, odp->link_name );
}
else error( "%s:%d: function '%s' not defined\n",
spec->src_name, odp->lineno, odp->link_name );
@ -611,8 +608,11 @@ static const char *ldcombine_files( DLLSPEC *spec, char **argv )
undef_file = create_undef_symbols_file( spec );
ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
strarray_add( &args, "-r", "-o", ld_tmp_file, undef_file, NULL );
strarray_addv( &args, argv );
strarray_add( &args, "-r" );
strarray_add( &args, "-o" );
strarray_add( &args, ld_tmp_file );
if (undef_file) strarray_add( &args, undef_file );
while (*argv) strarray_add( &args, *argv++ );
spawn( args );
return ld_tmp_file;
}
@ -655,8 +655,8 @@ void read_undef_symbols( DLLSPEC *spec, char **argv )
add_undef_import( p + strlen( import_func_prefix ), 0 );
else if (!strncmp( p, import_ord_prefix, strlen(import_ord_prefix) ))
add_undef_import( p + strlen( import_ord_prefix ), 1 );
else if (use_msvcrt || !find_name( p, &stdc_functions ))
strarray_add( &undef_symbols, xstrdup( p ), NULL );
else if (use_msvcrt || !find_name( p, stdc_functions ))
strarray_add( &undef_symbols, xstrdup( p ));
}
if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
free( cmd );
@ -711,7 +711,7 @@ void resolve_imports( DLLSPEC *spec )
/* check if symbol is still undefined */
int is_undefined( const char *name )
{
return find_name( name, &undef_symbols ) != NULL;
return find_name( name, undef_symbols ) != NULL;
}
/* output the get_pc thunk if needed */
@ -1584,7 +1584,7 @@ static void new_output_as_file(void)
if (output_file) fclose( output_file );
name = open_temp_output_file( ".s" );
strarray_add( &as_files, name, NULL );
strarray_add( &as_files, name );
}
/* assemble all the asm files */
@ -1611,22 +1611,24 @@ static void build_library( const char *output_name, char **argv, int create )
if (!create || target_platform != PLATFORM_WINDOWS)
{
args = find_tool( "ar", NULL );
strarray_add( &args, create ? "rc" : "r", output_name, NULL );
strarray_add( &args, create ? "rc" : "r" );
strarray_add( &args, output_name );
}
else
{
args = find_link_tool();
strarray_add( &args, "/lib", strmake( "-out:%s", output_name ), NULL );
strarray_add( &args, "/lib" );
strarray_add( &args, strmake( "-out:%s", output_name ));
}
strarray_addall( &args, as_files );
strarray_addv( &args, argv );
while (*argv) strarray_add( &args, *argv++ );
if (create) unlink( output_name );
spawn( args );
if (target_platform != PLATFORM_WINDOWS)
{
struct strarray ranlib = find_tool( "ranlib", NULL );
strarray_add( &ranlib, output_name, NULL );
strarray_add( &ranlib, output_name );
spawn( ranlib );
}
}
@ -1636,40 +1638,42 @@ static void build_windows_import_lib( const char *lib_name, DLLSPEC *spec )
{
struct strarray args;
char *def_file;
const char *as_flags, *m_flag;
def_file = open_temp_output_file( ".def" );
output_def_file( spec, 1 );
fclose( output_file );
args = find_tool( "dlltool", NULL );
strarray_add( &args, "-k" );
strarray_add( &args, strendswith( lib_name, ".delay.a" ) ? "-y" : "-l" );
strarray_add( &args, lib_name );
strarray_add( &args, "-d" );
strarray_add( &args, def_file );
switch (target_cpu)
{
case CPU_x86:
m_flag = "i386";
as_flags = "--as-flags=--32";
strarray_add( &args, "-m" );
strarray_add( &args, "i386" );
strarray_add( &args, "--as-flags=--32" );
break;
case CPU_x86_64:
m_flag = "i386:x86-64";
as_flags = "--as-flags=--64";
strarray_add( &args, "-m" );
strarray_add( &args, "i386:x86-64" );
strarray_add( &args, "--as-flags=--64" );
break;
case CPU_ARM:
m_flag = "arm";
as_flags = NULL;
strarray_add( &args, "-m" );
strarray_add( &args, "arm" );
break;
case CPU_ARM64:
m_flag = "arm64";
as_flags = NULL;
strarray_add( &args, "-m" );
strarray_add( &args, "arm64" );
break;
default:
m_flag = NULL;
break;
}
strarray_add( &args, "-k", strendswith( lib_name, ".delay.a" ) ? "-y" : "-l",
lib_name, "-d", def_file, NULL );
if (m_flag)
strarray_add( &args, "-m", m_flag, as_flags, NULL );
spawn( args );
}

View File

@ -453,7 +453,7 @@ static char **parse_options( int argc, char **argv, DLLSPEC *spec )
switch(optc)
{
case 'B':
strarray_add( &tools_path, xstrdup( optarg ), NULL );
strarray_add( &tools_path, xstrdup( optarg ));
break;
case 'D':
/* ignored */
@ -479,7 +479,7 @@ static char **parse_options( int argc, char **argv, DLLSPEC *spec )
/* ignored, because cc generates correct code. */
break;
case 'L':
strarray_add( &lib_path, xstrdup( optarg ), NULL );
strarray_add( &lib_path, xstrdup( optarg ));
break;
case 'm':
if (!strcmp( optarg, "16" )) spec->type = SPEC_WIN16;
@ -533,7 +533,7 @@ static char **parse_options( int argc, char **argv, DLLSPEC *spec )
output_file_name = xstrdup( optarg );
break;
case 'r':
strarray_add( &res_files, xstrdup( optarg ), NULL );
strarray_add( &res_files, xstrdup( optarg ));
break;
case 'u':
add_extra_ld_symbol( optarg );

View File

@ -628,7 +628,6 @@ void output_res_o_file( DLLSPEC *spec )
{
unsigned int i;
char *res_file = NULL;
const char *format;
int fd;
struct strarray args;
@ -687,20 +686,22 @@ void output_res_o_file( DLLSPEC *spec )
free( output_buffer );
args = find_tool( "windres", NULL );
strarray_add( &args, "-i" );
strarray_add( &args, res_file );
strarray_add( &args, "-o" );
strarray_add( &args, output_file_name );
switch (target_cpu)
{
case CPU_x86:
format = "pe-i386";
strarray_add( &args, "-F" );
strarray_add( &args, "pe-i386" );
break;
case CPU_x86_64:
format = "pe-x86-64";
strarray_add( &args, "-F" );
strarray_add( &args, "pe-x86-64" );
break;
default:
format = NULL;
break;
}
strarray_add( &args, "-i", res_file, "-o", output_file_name, NULL );
if (format)
strarray_add( &args, "-F", format, NULL );
spawn( args );
}

View File

@ -55,7 +55,7 @@ static struct strarray spec_extra_ld_symbols = { 0 }; /* list of extra symbols t
/* add a symbol to the list of extra symbols that ld must resolve */
void add_spec_extra_ld_symbol( const char *name )
{
strarray_add( &spec_extra_ld_symbols, name, NULL );
strarray_add( &spec_extra_ld_symbols, name );
}
static unsigned int hash_filename( const char *name )

View File

@ -36,12 +36,6 @@
#include "build.h"
#if defined(_WIN32) && !defined(__CYGWIN__)
# define PATH_SEPARATOR ';'
#else
# define PATH_SEPARATOR ':'
#endif
static struct strarray tmp_files;
static struct strarray empty_strarray;
static const char *output_file_source_name;
@ -145,47 +139,22 @@ char *strmake( const char* fmt, ... )
}
}
static struct strarray strarray_copy( struct strarray src )
void strarray_add( struct strarray *array, const char *str )
{
struct strarray array;
array.count = src.count;
array.max = src.max;
array.str = xmalloc( array.max * sizeof(*array.str) );
memcpy( array.str, src.str, array.count * sizeof(*array.str) );
return array;
}
static void strarray_add_one( struct strarray *array, const char *str )
{
if (array->count == array->max)
if (array->count == array->size)
{
array->max *= 2;
if (array->max < 16) array->max = 16;
array->str = xrealloc( array->str, array->max * sizeof(*array->str) );
if (array->size) array->size *= 2;
else array->size = 16;
array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
}
array->str[array->count++] = str;
}
void strarray_add( struct strarray *array, ... )
{
va_list valist;
const char *str;
va_start( valist, array );
while ((str = va_arg( valist, const char *))) strarray_add_one( array, str );
va_end( valist );
}
void strarray_addv( struct strarray *array, char * const *argv )
{
while (*argv) strarray_add_one( array, *argv++ );
}
void strarray_addall( struct strarray *array, struct strarray args )
{
unsigned int i;
for (i = 0; i < args.count; i++) strarray_add_one( array, args.str[i] );
for (i = 0; i < args.count; i++) strarray_add( array, args.str[i] );
}
struct strarray strarray_fromstring( const char *str, const char *delim )
@ -195,12 +164,35 @@ struct strarray strarray_fromstring( const char *str, const char *delim )
char *buf = xstrdup( str );
for (tok = strtok( buf, delim ); tok; tok = strtok( NULL, delim ))
strarray_add_one( &array, strdup( tok ));
strarray_add( &array, xstrdup( tok ));
free( buf );
return array;
}
static struct strarray strarray_frompath( const char *path )
{
if (!path) return empty_strarray;
#if defined(_WIN32) && !defined(__CYGWIN__)
return strarray_fromstring( path, ";" );
#else
return strarray_fromstring( path, ":" );
#endif
}
void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
{
if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
}
const char *strarray_bsearch( const struct strarray *array, const char *str,
int (*func)(const char **, const char **) )
{
char **res = NULL;
if (array->count) res = bsearch( &str, array->str, array->count, sizeof(*array->str), (void *)func );
return res ? *res : NULL;
}
void fatal_error( const char *msg, ... )
{
va_list valist;
@ -288,20 +280,8 @@ static struct strarray get_tools_path(void)
if (!done)
{
dirs = strarray_copy( tools_path );
/* then append the PATH directories */
if (getenv( "PATH" ))
{
char *p = xstrdup( getenv( "PATH" ));
while (*p)
{
strarray_add_one( &dirs, p );
while (*p && *p != PATH_SEPARATOR) p++;
if (!*p) break;
*p++ = 0;
}
}
strarray_addall( &dirs, tools_path );
strarray_addall( &dirs, strarray_frompath( getenv( "PATH" )));
done = 1;
}
return dirs;
@ -347,7 +327,7 @@ void spawn( struct strarray args )
const char *argv0 = find_binary( NULL, args.str[0] );
if (argv0) args.str[0] = argv0;
strarray_add_one( &args, NULL );
strarray_add( &args, NULL );
if (verbose)
for (i = 0; args.str[i]; i++)
fprintf( stderr, "%s%c", args.str[i], args.str[i+1] ? ' ' : '\n' );
@ -360,18 +340,18 @@ void spawn( struct strarray args )
}
}
static const char *find_clang_tool( const struct strarray clang, const char *tool )
static const char *find_clang_tool( struct strarray clang, const char *tool )
{
const char *out = get_temp_file_name( "print_tool", ".out" );
struct strarray args;
struct strarray args = empty_strarray;
int sout = -1;
char *path, *p;
struct stat st;
size_t cnt;
args = strarray_copy( clang );
strarray_add_one( &args, strmake( "-print-prog-name=%s", tool ) );
if (verbose) strarray_add_one( &args, "-v" );
strarray_addall( &args, clang );
strarray_add( &args, strmake( "-print-prog-name=%s", tool ));
if (verbose) strarray_add( &args, "-v" );
sout = dup( fileno(stdout) );
freopen( out, "w", stdout );
@ -426,13 +406,13 @@ struct strarray find_tool( const char *name, const char * const *names )
if (!file && !(file = find_binary( "llvm", name )))
{
struct strarray clang = empty_strarray;
strarray_add_one( &clang, "clang" );
strarray_add( &clang, "clang" );
file = find_clang_tool( clang, strmake( "llvm-%s", name ));
}
}
if (!file) fatal_error( "cannot find the '%s' tool\n", name );
strarray_add_one( &ret, file );
strarray_add( &ret, file );
return ret;
}
@ -447,31 +427,32 @@ struct strarray find_link_tool(void)
if (!file)
{
struct strarray clang = empty_strarray;
strarray_add_one( &clang, "clang" );
strarray_add( &clang, "clang" );
file = find_clang_tool( clang, "lld-link" );
}
if (!file) fatal_error( "cannot find the 'lld-link' tool\n" );
strarray_add_one( &ret, file );
strarray_add( &ret, file );
return ret;
}
struct strarray get_as_command(void)
{
struct strarray args;
struct strarray args = empty_strarray;
unsigned int i;
if (cc_command.count)
{
args = strarray_copy( cc_command );
strarray_add( &args, "-xassembler", "-c", NULL );
strarray_addall( &args, cc_command );
strarray_add( &args, "-xassembler" );
strarray_add( &args, "-c" );
if (force_pointer_size)
strarray_add_one( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
if (arch_option) strarray_add_one( &args, strmake("-march=%s", arch_option) );
strarray_add( &args, (force_pointer_size == 8) ? "-m64" : "-m32" );
if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
if (arch_option) strarray_add( &args, strmake("-march=%s", arch_option) );
for (i = 0; i < tools_path.count; i++)
strarray_add_one( &args, strmake("-B%s", tools_path.str[i] ));
strarray_add( &args, strmake("-B%s", tools_path.str[i] ));
return args;
}
@ -481,37 +462,38 @@ struct strarray get_as_command(void)
as_command = find_tool( "as", commands );
}
args = strarray_copy( as_command );
strarray_addall( &args, as_command );
if (force_pointer_size)
{
switch (target_platform)
{
case PLATFORM_APPLE:
strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
strarray_add( &args, "-arch" );
strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
break;
default:
switch(target_cpu)
{
case CPU_POWERPC:
strarray_add_one( &args, (force_pointer_size == 8) ? "-a64" : "-a32" );
strarray_add( &args, (force_pointer_size == 8) ? "-a64" : "-a32" );
break;
default:
strarray_add_one( &args, (force_pointer_size == 8) ? "--64" : "--32" );
strarray_add( &args, (force_pointer_size == 8) ? "--64" : "--32" );
break;
}
break;
}
}
if (cpu_option) strarray_add_one( &args, strmake("-mcpu=%s", cpu_option) );
if (fpu_option) strarray_add_one( &args, strmake("-mfpu=%s", fpu_option) );
if (cpu_option) strarray_add( &args, strmake("-mcpu=%s", cpu_option) );
if (fpu_option) strarray_add( &args, strmake("-mfpu=%s", fpu_option) );
return args;
}
struct strarray get_ld_command(void)
{
struct strarray args;
struct strarray args = empty_strarray;
if (!ld_command.count)
{
@ -519,30 +501,35 @@ struct strarray get_ld_command(void)
ld_command = find_tool( "ld", commands );
}
args = strarray_copy( ld_command );
strarray_addall( &args, ld_command );
if (force_pointer_size)
{
switch (target_platform)
{
case PLATFORM_APPLE:
strarray_add( &args, "-arch", (force_pointer_size == 8) ? "x86_64" : "i386", NULL );
strarray_add( &args, "-arch" );
strarray_add( &args, (force_pointer_size == 8) ? "x86_64" : "i386" );
break;
case PLATFORM_FREEBSD:
strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd", NULL );
strarray_add( &args, "-m" );
strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64_fbsd" : "elf_i386_fbsd" );
break;
case PLATFORM_MINGW:
case PLATFORM_WINDOWS:
strarray_add( &args, "-m", (force_pointer_size == 8) ? "i386pep" : "i386pe", NULL );
strarray_add( &args, "-m" );
strarray_add( &args, (force_pointer_size == 8) ? "i386pep" : "i386pe" );
break;
default:
switch(target_cpu)
{
case CPU_POWERPC:
strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc", NULL );
strarray_add( &args, "-m" );
strarray_add( &args, (force_pointer_size == 8) ? "elf64ppc" : "elf32ppc" );
break;
default:
strarray_add( &args, "-m", (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386", NULL );
strarray_add( &args, "-m" );
strarray_add( &args, (force_pointer_size == 8) ? "elf_x86_64" : "elf_i386" );
break;
}
break;
@ -550,7 +537,7 @@ struct strarray get_ld_command(void)
}
if (target_cpu == CPU_ARM && !is_pe())
strarray_add( &args, "--no-wchar-size-warning", NULL );
strarray_add( &args, "--no-wchar-size-warning" );
return args;
}
@ -595,7 +582,7 @@ char *get_temp_file_name( const char *prefix, const char *suffix )
}
close( fd );
strarray_add_one( &tmp_files, name );
strarray_add( &tmp_files, name );
return name;
}
@ -889,7 +876,9 @@ int remove_stdcall_decoration( char *name )
void assemble_file( const char *src_file, const char *obj_file )
{
struct strarray args = get_as_command();
strarray_add( &args, "-o", obj_file, src_file, NULL );
strarray_add( &args, "-o" );
strarray_add( &args, obj_file );
strarray_add( &args, src_file );
spawn( args );
}