From: Konstantin Demin rockdrilla@gmail.com
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=41712 Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=51051
Signed-off-by: Konstantin Demin rockdrilla@gmail.com --- configure.ac | 7 ++ tools/makedep.c | 139 +++++++++++++++++++++++++++++++++++++++ tools/tools.h | 72 ++++++++++++++++++++ tools/winebuild/build.h | 1 + tools/winebuild/import.c | 39 ++++++++++- tools/winebuild/main.c | 6 +- tools/winebuild/utils.c | 22 ++++++- tools/winegcc/winegcc.c | 17 +++++ 8 files changed, 297 insertions(+), 6 deletions(-)
diff --git a/configure.ac b/configure.ac index b999e3506e0..3f9c895c6b9 100644 --- a/configure.ac +++ b/configure.ac @@ -24,6 +24,7 @@ AC_ARG_ENABLE(maintainer-mode, AS_HELP_STRING([--enable-maintainer-mode],[enable AC_ARG_ENABLE(sast, AS_HELP_STRING([--enable-sast],[enable static application security testing using Clang])) AC_ARG_ENABLE(silent-rules, AS_HELP_STRING([--enable-silent-rules],[use silent build rules (override: "make V=1")])) AC_ARG_ENABLE(werror, AS_HELP_STRING([--enable-werror],[treat compilation warnings as errors])) +AC_ARG_ENABLE(lto, AS_HELP_STRING([--enable-lto],[build with LTO (EXPERIMENTAL)]))
AC_ARG_WITH(alsa, AS_HELP_STRING([--without-alsa],[do not use the Alsa sound support])) AC_ARG_WITH(capi, AS_HELP_STRING([--without-capi],[do not use CAPI (ISDN support)])) @@ -837,6 +838,11 @@ do AS_VAR_COPY([LDFLAGS],[${wine_arch}_LDFLAGS]) AS_VAR_SET([${wine_arch}_EXTRACFLAGS],["-D__WINE_PE_BUILD -Wall"])
+ if test "x$enable_lto" = xyes + then + AS_VAR_APPEND([${wine_arch}_EXTRACFLAGS],[" -D__WINE_LTO_BUILD"]) + fi + target="" set x $CC shift @@ -2377,6 +2383,7 @@ AS_ECHO_N("creating Makefile rules...") >&AS_MESSAGE_FD
makedep_flags=" -C" test "x$enable_silent_rules" = xyes && makedep_flags="$makedep_flags -S" +test "x$enable_lto" = xyes && makedep_flags="$makedep_flags -L"
wine_srcdir= test "$srcdir" = . || wine_srcdir="$srcdir/" diff --git a/tools/makedep.c b/tools/makedep.c index c1937363c39..7c58e8b1c43 100644 --- a/tools/makedep.c +++ b/tools/makedep.c @@ -175,6 +175,7 @@ static struct strarray disabled_dirs[MAX_ARCHS]; static unsigned int native_archs[MAX_ARCHS]; static unsigned int hybrid_archs[MAX_ARCHS]; static struct strarray hybrid_target_flags[MAX_ARCHS]; +static struct strarray global_lto_skip[MAX_ARCHS];
struct makefile { @@ -229,6 +230,8 @@ struct makefile struct strarray res_files[MAX_ARCHS]; struct strarray all_targets[MAX_ARCHS]; struct strarray install_rules[NB_INSTALL_RULES]; + int lto_skip[MAX_ARCHS]; + struct strarray lto_skip_src[MAX_ARCHS]; };
static struct makefile *top_makefile; @@ -242,6 +245,7 @@ static const char *output_file_name; static const char *temp_file_name; static int compile_commands_mode; static int silent_rules; +static int with_lto = 0; static int input_line; static int output_column; static FILE *output_file; @@ -270,6 +274,13 @@ static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__p static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2))); static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
+static void parse_global_lto_skip_lists( void ); +static int is_lto_enabled_source( struct makefile *make, struct incl_file *source, unsigned int arch ); +static int is_lto_enabled_target( struct makefile *make, unsigned int arch ); +static void output_lto_flags( int use_lto, int use_env ); +static void output_lto_flags_source( struct makefile *make, struct incl_file *source, unsigned int arch, int use_env ); +static void output_lto_flags_target( struct makefile *make, unsigned int arch, int use_env ); + /******************************************************************* * fatal_error */ @@ -2439,6 +2450,7 @@ static const char *cmd_prefix( const char *cmd ) static void output_winegcc_command( struct makefile *make, unsigned int arch ) { output( "\t%s%s -o $@", cmd_prefix( "CCLD" ), tools_path( make, "winegcc" )); + output_lto_flags_target( make, arch, 0 /* use_env = FALSE */ ); output_filename( "--wine-objdir ." ); if (tools_dir) { @@ -3271,6 +3283,7 @@ static void output_source_one_arch( struct makefile *make, struct incl_file *sou output_filenames( defines ); output_filenames( cflags ); output_filename( var_cflags ); + output_lto_flags_source( make, source, arch, 1 /* use_env = TRUE */ ); output( "\n" );
if (make->testdll && strendswith( source->name, ".c" ) && @@ -3545,6 +3558,7 @@ static void output_import_lib( struct makefile *make, unsigned int arch ) if (hybrid_arch) output_filenames_obj_dir( make, make->implib_files[hybrid_arch] ); output( "\n" ); output( "\t%s%s -w --implib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" ) ); + output_lto_flags_target( make, arch, 0 /* use_env = FALSE */ ); if (!delay_load_flags[arch]) output_filename( "--without-dlltool" ); output_filenames( target_flags[hybrid_arch ? hybrid_arch : arch] ); if (make->is_win16) output_filename( "-m16" ); @@ -3579,6 +3593,7 @@ static void output_unix_lib( struct makefile *make ) output_filenames( unix_deps ); output( "\n" ); output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" )); + output_lto_flags_target( make, arch, 1 /* use_env = TRUE */ ); output_filenames( get_expanded_make_var_array( make, "UNIXLDFLAGS" )); output_filenames_obj_dir( make, make->unixobj_files ); output_filenames( unix_libs ); @@ -3604,6 +3619,7 @@ static void output_static_lib( struct makefile *make, unsigned int arch ) if (!arch) output_filenames_obj_dir( make, make->unixobj_files ); output( "\n" ); output( "\t%s%s -w --staticlib -o $@", cmd_prefix( "BUILD" ), tools_path( make, "winebuild" )); + output_lto_flags_target( make, arch, 0 /* use_env = FALSE */ ); output_filenames( target_flags[hybrid_arch ? hybrid_arch : arch] ); output_filenames_obj_dir( make, make->object_files[arch] ); if (hybrid_arch) output_filenames_obj_dir( make, make->object_files[hybrid_arch] ); @@ -3730,6 +3746,7 @@ static void output_programs( struct makefile *make ) output_filenames( deps ); output( "\n" ); output( "\t%s$(CC) -o $@", cmd_prefix( "CCLD" )); + output_lto_flags_target( make, arch, 1 /* use_env = TRUE */ ); output_filenames_obj_dir( make, objs ); output_filenames( all_libs ); output_filename( "$(LDFLAGS)" ); @@ -4490,6 +4507,22 @@ static void load_sources( struct makefile *make )
for (i = 0; i < make->delayimports.count; i++) strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] )); + + if (with_lto) + { + for (arch = 0; arch < archs.count; arch++) + { + const char * lto_skip; + lto_skip = get_expanded_arch_var( make, "LTO_SKIP", arch ); + + make->lto_skip[arch] = var_to_bool(lto_skip); + + memset( &make->lto_skip_src[arch], 0, sizeof(struct strarray) ); + value = get_expanded_arch_var_array( make, "LTO_SKIP_SRC", arch ); + for (i = 0; i < value.count; i++) + strarray_add( &make->lto_skip_src[arch], xstrdup( value.str[i] )); + } + } }
@@ -4537,6 +4570,9 @@ static int parse_option( const char *opt ) case 'S': silent_rules = 1; break; + case 'L': + with_lto = 1; + break; default: fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage ); exit(1); @@ -4679,6 +4715,7 @@ int main( int argc, char *argv[] )
for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
+ parse_global_lto_skip_lists(); load_sources( top_makefile ); load_sources( include_makefile ); for (i = 0; i < subdirs.count; i++) @@ -4691,3 +4728,105 @@ int main( int argc, char *argv[] )
return 0; } + + +static void parse_global_lto_skip_lists( void ) +{ + unsigned int arch; + char *buffer; + FILE *file; + + if (!with_lto) return; + + for (arch = 0; arch < archs.count; arch++) + { + memset( &global_lto_skip[arch], 0, sizeof(struct strarray) ); + + input_file_name = root_src_dir_path( arch ? strmake( "tools/makedep.lto-skip.%s.list", archs.str[arch] ) : "tools/makedep.lto-skip.any.list" ); + input_line = 0; + if (!(file = fopen( input_file_name, "r" ))) + { + /* skip nonexistent files */ + input_file_name = NULL; + continue; + } + + while ((buffer = get_line( file ))) + { + if (*buffer == '#') continue; /* comment */ + buffer = skip_spaces( buffer ); + strarray_add( &global_lto_skip[arch], xstrdup( buffer )); + } + fclose( file ); + input_file_name = NULL; + } +} + +static int is_lto_enabled_source( struct makefile *make, struct incl_file *source, unsigned int arch ) +{ + if (!with_lto) return 0; + + /* target config */ + if (strarray_exists( &make->lto_skip_src[0], source->name )) + return 0; + + if (arch) + { + /* target config */ + if (strarray_exists( &make->lto_skip_src[arch], source->name )) + return 0; + + /* global config */ + if (strarray_exists( &global_lto_skip[arch], source->filename )) + return 0; + } + + /* global config */ + if (strarray_exists( &global_lto_skip[0], source->filename )) + return 0; + + return 1; +} + + +static int is_lto_enabled_target( struct makefile *make, unsigned int arch ) +{ + if (!with_lto) return 0; + + if (make->lto_skip[0]) return 0; + + if (arch && make->lto_skip[arch]) + return 0; + + return 1; +} + + +static void output_lto_flags( int use_lto, int use_env ) +{ + struct strarray lto_flags = empty_strarray; + append_lto_flags( <o_flags, use_lto, use_env ); + output_filenames( lto_flags ); +} + + +static void output_lto_flags_source( struct makefile *make, struct incl_file *source, unsigned int arch, int use_env ) +{ + int use_lto = 0; + + if (!with_lto) return; + + use_lto = is_lto_enabled_target( make, arch ) && is_lto_enabled_source( make, source, arch ); + output_lto_flags( use_lto, use_env ); +} + + +static void output_lto_flags_target( struct makefile *make, unsigned int arch, int use_env ) +{ + int use_lto = 0; + + if (!with_lto) return; + + use_lto = is_lto_enabled_target( make, arch ); + output_lto_flags( use_lto, use_env ); +} diff --git a/tools/tools.h b/tools/tools.h index b840a85b2e5..7c1f4a22a39 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -949,4 +949,76 @@ static inline struct strarray parse_options( int argc, char **argv, const char * #undef OPT_ERR }
+static int var_to_bool( const char *var ) +{ + if (!var) return 0; + if (!( var[0] )) return 0; + + switch (var[0]) + { + /* "1" */ + case '1': + if (!var[1]) return 1; + break; + + /* "[Tt]", "[Tt][Rr][Uu][Ee]" */ + case 'T': /* -fallthrough */ + case 't': + if (!var[1]) return 1; + + if (strlen( var ) != 4) break; + if ((var[1] != 'R') && (var[1] != 'r')) break; + if ((var[2] != 'U') && (var[2] != 'u')) break; + if ((var[3] != 'E') && (var[3] != 'e')) break; + return 1; + + break; + + /* "[Yy]", "[Yy][Ee][Ss]" */ + case 'Y': /* -fallthrough */ + case 'y': + if (!var[1]) return 1; + + if (strlen( var ) != 3) break; + if ((var[1] != 'E') && (var[1] != 'e')) break; + if ((var[2] != 'S') && (var[2] != 's')) break; + return 1; + + break; + } + + return 0; +} + +static int adjust_verbose_lto( int with_lto, int verbose ) +{ + const char* lto_debug; + + if (with_lto < 1) return verbose; + if (verbose) return verbose; + + lto_debug = getenv("WINE_LTO_DEBUG"); + if (!lto_debug) return verbose; + if (!strlen( lto_debug )) return verbose; + + return var_to_bool(lto_debug); +} + +static void append_lto_flags( struct strarray *args, int with_lto, int use_env ) +{ + if (with_lto < 0) return; + + while (use_env > 0) + { + const char *flags = getenv( (with_lto) ? "WINE_LTO_FLAGS" : "WINE_LTO_SKIP_FLAGS" ); + if (!flags) break; + if (!strlen( flags )) break; + + strarray_addall( args, strarray_fromstring( flags, " " ) ); + return; + } + + strarray_add( args, with_lto ? "-flto" : "-fno-lto" ); +} + #endif /* __WINE_TOOLS_H */ diff --git a/tools/winebuild/build.h b/tools/winebuild/build.h index e5ede93425b..f6a57db1a2e 100644 --- a/tools/winebuild/build.h +++ b/tools/winebuild/build.h @@ -363,6 +363,7 @@ extern void put_pword( unsigned int val );
extern int current_line; extern int UsePIC; +extern int UseLTO; extern int nb_errors; extern int display_warnings; extern int kill_at; diff --git a/tools/winebuild/import.c b/tools/winebuild/import.c index 6aee0fa98f8..3223795e430 100644 --- a/tools/winebuild/import.c +++ b/tools/winebuild/import.c @@ -1241,11 +1241,27 @@ static const char *get_target_machine(void) /* build a library from the current asm files and any additional object files in argv */ void output_static_lib( const char *output_name, struct strarray files, int create ) { - struct strarray args; + struct strarray args = empty_strarray;
if (!create || target.platform != PLATFORM_WINDOWS) { - args = find_tool( "ar", NULL ); + while (UseLTO) + { + static const char * _cmd[2] = { 0 }; + if (!_cmd[0]) + { + const char *_ar = getenv( "WINE_LTO_AR" ); + if (!_ar) break; + if (!strlen( _ar )) break; + _cmd[0] = _ar; + } + args = find_tool( "ar", _cmd ); + break; + } + if (!args.count) + { + args = find_tool( "ar", NULL ); + } strarray_add( &args, create ? "rc" : "r" ); strarray_add( &args, output_name ); } @@ -1263,7 +1279,24 @@ void output_static_lib( const char *output_name, struct strarray files, int crea
if (target.platform != PLATFORM_WINDOWS) { - struct strarray ranlib = find_tool( "ranlib", NULL ); + struct strarray ranlib = empty_strarray; + while (UseLTO) + { + static const char * _cmd[2] = { 0 }; + if (!_cmd[0]) + { + const char *_ranlib = getenv( "WINE_LTO_RANLIB" ); + if (!_ranlib) break; + if (!strlen( _ranlib )) break; + _cmd[0] = _ranlib; + } + ranlib = find_tool( "ranlib", _cmd ); + break; + } + if (!ranlib.count) + { + ranlib = find_tool( "ranlib", NULL ); + } strarray_add( &ranlib, output_name ); spawn( ranlib ); } diff --git a/tools/winebuild/main.c b/tools/winebuild/main.c index 63d65e7bcd0..49331544822 100644 --- a/tools/winebuild/main.c +++ b/tools/winebuild/main.c @@ -34,6 +34,7 @@ #include "build.h"
int UsePIC = 0; +int UseLTO = -1; int nb_errors = 0; int display_warnings = 0; int native_arch = -1; @@ -185,7 +186,7 @@ static const char usage_str[] = " -e, --entry=FUNC Set the DLL entry point function (default: DllMain)\n" " -E, --export=FILE Export the symbols defined in the .spec or .def file\n" " --external-symbols Allow linking to external symbols\n" -" -f FLAGS Compiler flags (-fPIC and -fasynchronous-unwind-tables are supported)\n" +" -f FLAGS Compiler flags (-flto, -fPIC and -fasynchronous-unwind-tables are supported)\n" " -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n" " --fake-module Create a fake binary module\n" " -h, --help Display this help message\n" @@ -405,6 +406,8 @@ static void option_callback( int optc, char *optarg ) if (!strcmp( optarg, "PIC") || !strcmp( optarg, "pic")) UsePIC = 1; else if (!strcmp( optarg, "asynchronous-unwind-tables")) unwind_tables = 1; else if (!strcmp( optarg, "no-asynchronous-unwind-tables")) unwind_tables = 0; + else if (!strcmp( optarg, "lto" )) UseLTO = 1; + else if (!strcmp( optarg, "no-lto" )) UseLTO = 0; /* ignore all other flags */ break; case 'h': @@ -569,6 +572,7 @@ int main(int argc, char **argv) if (is_pe()) unwind_tables = 1;
files = parse_options( argc, argv, short_options, long_options, 0, option_callback ); + verbose = adjust_verbose_lto( UseLTO, verbose );
atexit( cleanup ); /* make sure we remove the output file on exit */
diff --git a/tools/winebuild/utils.c b/tools/winebuild/utils.c index 05595f86d37..bd016107232 100644 --- a/tools/winebuild/utils.c +++ b/tools/winebuild/utils.c @@ -303,6 +303,7 @@ struct strarray get_as_command(void)
if (using_cc) { + append_lto_flags( &args, (UseLTO == -1) ? -1 : 0, 1 /* use_env = TRUE */ ); strarray_add( &args, "-xassembler" ); strarray_add( &args, "-c" ); if (force_pointer_size) @@ -345,6 +346,7 @@ struct strarray get_ld_command(void) }
strarray_addall( &args, ld_command ); + append_lto_flags( &args, UseLTO, 1 /* use_env = TRUE */ );
if (force_pointer_size) { @@ -380,8 +382,24 @@ const char *get_nm_command(void) { if (!nm_command.count) { - static const char * const commands[] = { "nm", "gnm", NULL }; - nm_command = find_tool( "nm", commands ); + while (UseLTO) + { + static const char * _cmd[2] = { 0 }; + if (!_cmd[0]) + { + const char *_nm = getenv( "WINE_LTO_NM" ); + if (!_nm) break; + if (!strlen( _nm )) break; + _cmd[0] = _nm; + } + nm_command = find_tool( "nm", _cmd ); + break; + } + if (!nm_command.count) + { + static const char * const commands[] = { "nm", "gnm", NULL }; + nm_command = find_tool( "nm", commands ); + } } if (nm_command.count > 1) fatal_error( "multiple arguments in nm command not supported yet\n" ); diff --git a/tools/winegcc/winegcc.c b/tools/winegcc/winegcc.c index 5b60c57daf7..2108d0e9374 100644 --- a/tools/winegcc/winegcc.c +++ b/tools/winegcc/winegcc.c @@ -177,6 +177,7 @@ struct options int unwind_tables; int strip; int pic; + int lto; int no_default_config; int build_id; const char* wine_objdir; @@ -364,6 +365,7 @@ static struct strarray get_link_args( struct options *opts, const char *output_n struct strarray flags = empty_strarray;
strarray_addall( &link_args, opts->linker_args ); + append_lto_flags( &link_args, opts->lto, 1 /* use_env = TRUE */ );
if (verbose > 1) strarray_add( &flags, "-v" );
@@ -653,6 +655,14 @@ static void compile(struct options* opts, const char* lang) break; }
+ switch(opts->processor) + { + case proc_cc: /* -fallthrough */ + case proc_cxx: + append_lto_flags( &comp_args, opts->lto, 1 /* use_env = TRUE */ ); + break; + } + if (opts->target.platform == PLATFORM_WINDOWS || opts->target.platform == PLATFORM_CYGWIN || opts->target.platform == PLATFORM_MINGW) @@ -835,6 +845,7 @@ static struct strarray get_winebuild_args( struct options *opts, const char *tar strarray_add( &spec_args, binary ); if (verbose) strarray_add( &spec_args, "-v" ); if (keep_generated) strarray_add( &spec_args, "--save-temps" ); + append_lto_flags( &spec_args, opts->lto, 0 /* use_env = FALSE */ ); if (target) { strarray_add( &spec_args, "--target" ); @@ -1502,6 +1513,7 @@ int main(int argc, char **argv) memset(&opts, 0, sizeof(opts)); opts.target = init_argv0_target( argv[0] ); opts.pic = 1; + opts.lto = -1;
/* determine the processor type */ if (strendswith(argv[0], "winecpp")) opts.processor = proc_cpp; @@ -1653,6 +1665,10 @@ int main(int argc, char **argv) opts.pic = 1; else if (!strcmp("-fno-PIC", opts.args.str[i]) || !strcmp("-fno-pic", opts.args.str[i])) opts.pic = 0; + else if (!strcmp( "-flto", opts.args.str[i] )) + opts.lto = 1; + else if (!strcmp( "-fno-lto", opts.args.str[i] )) + opts.lto = 0; break; case 'i': if (!strcmp( "-isysroot", opts.args.str[i] )) opts.isysroot = opts.args.str[i + 1]; @@ -1942,6 +1958,7 @@ int main(int argc, char **argv) strarray_add( &opts.files, opts.args.str[i] ); } } + verbose = adjust_verbose_lto( opts.lto, verbose );
if (opts.force_pointer_size) set_target_ptr_size( &opts.target, opts.force_pointer_size ); if (opts.processor == proc_cpp) linking = 0;