Module: wine Branch: master Commit: 65880e0c8c8f5cd699af6d80bf890d23f6c5d856 URL: https://gitlab.winehq.org/wine/wine/-/commit/65880e0c8c8f5cd699af6d80bf890d2...
Author: Alexandre Julliard julliard@winehq.org Date: Wed Jun 26 10:37:46 2024 +0200
makedep: Generate a compile_commands.json file.
---
configure | 3 +- configure.ac | 3 +- tools/makedep.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 118 insertions(+), 19 deletions(-)
diff --git a/configure b/configure index e27f98a8a67..9ef4dfef5a4 100755 --- a/configure +++ b/configure @@ -21924,7 +21924,7 @@ ac_config_commands="$ac_config_commands include/stamp-h"
printf %s "creating Makefile rules..." >&6
-makedep_flags="" +makedep_flags=" -C" test "x$enable_silent_rules" = xyes && makedep_flags="$makedep_flags -S"
wine_srcdir= @@ -23094,6 +23094,7 @@ test "x$enable_tools" = xno || wine_fn_config_makefile tools/wrc enable_wrc as_fn_append CONFIGURE_TARGETS " TAGS" as_fn_append CONFIGURE_TARGETS " tags" as_fn_append CONFIGURE_TARGETS " autom4te.cache" +as_fn_append CONFIGURE_TARGETS " compile_commands.json" as_fn_append CONFIGURE_TARGETS " config.log" as_fn_append CONFIGURE_TARGETS " config.status" as_fn_append CONFIGURE_TARGETS " include/config.h" diff --git a/configure.ac b/configure.ac index 798ed738d97..6f48132b22a 100644 --- a/configure.ac +++ b/configure.ac @@ -2410,7 +2410,7 @@ AH_BOTTOM([#endif /* __WINE_CONFIG_H */]) AC_CONFIG_COMMANDS([include/stamp-h], [echo timestamp > include/stamp-h]) AS_ECHO_N("creating Makefile rules...") >&AS_MESSAGE_FD
-makedep_flags="" +makedep_flags=" -C" test "x$enable_silent_rules" = xyes && makedep_flags="$makedep_flags -S"
wine_srcdir= @@ -3578,6 +3578,7 @@ dnl Main ignore file WINE_IGNORE_FILE(TAGS) WINE_IGNORE_FILE(tags) WINE_IGNORE_FILE(autom4te.cache) +WINE_IGNORE_FILE(compile_commands.json) WINE_IGNORE_FILE(config.log) WINE_IGNORE_FILE(config.status) WINE_IGNORE_FILE(include/config.h) diff --git a/tools/makedep.c b/tools/makedep.c index 25ddb92f7fa..c185030ea66 100644 --- a/tools/makedep.c +++ b/tools/makedep.c @@ -240,14 +240,27 @@ static const char *output_makefile_name = "Makefile"; static const char *input_file_name; static const char *output_file_name; static const char *temp_file_name; +static int compile_commands_mode; static int silent_rules; static int input_line; static int output_column; static FILE *output_file;
+struct compile_command +{ + struct list entry; + const char *cmd; + const char *source; + const char *obj; + struct strarray args; +}; + +static struct list compile_commands = LIST_INIT( compile_commands ); + static const char Usage[] = "Usage: makedep [options]\n" "Options:\n" + " -C Generate compile_commands.json along with the makefile\n" " -S Generate Automake-style silent rules\n" " -fxxx Store output in file 'xxx' (default: Makefile)\n";
@@ -552,6 +565,26 @@ static char *concat_paths( const char *base, const char *path ) }
+/******************************************************************* + * escape_cstring + */ +static const char *escape_cstring( const char *str ) +{ + char *ret; + unsigned int i = 0, j = 0; + + if (!strpbrk( str, "\"" )) return str; + ret = xmalloc( 2 * strlen(str) + 1 ); + while (str[i]) + { + if (str[i] == '\' || str[i] == '"') ret[j++] = '\'; + ret[j++] = str[i++]; + } + ret[j] = 0; + return ret; +} + + /******************************************************************* * is_native_arch_disabled * @@ -3166,7 +3199,7 @@ static void output_source_one_arch( struct makefile *make, struct incl_file *sou unsigned int arch ) { const char *obj_name, *var_cc, *var_cflags; - struct strarray arch_cflags = empty_strarray; + struct strarray cflags = empty_strarray;
if (make->disabled[arch] && !(source->file->flags & FLAG_C_IMPLIB)) return;
@@ -3197,46 +3230,46 @@ static void output_source_one_arch( struct makefile *make, struct incl_file *sou else strarray_add( &make->clean_files, obj_name );
+ if (!source->use_msvcrt) strarray_addall( &cflags, make->unix_cflags ); if ((source->file->flags & FLAG_ARM64EC_X64) && !strcmp( archs.str[arch], "arm64ec" )) { var_cc = "$(x86_64_CC)"; var_cflags = "$(x86_64_CFLAGS)"; - strarray_add( &arch_cflags, "-D__arm64ec_x64__" ); - strarray_addall( &arch_cflags, get_expanded_make_var_array( top_makefile, "x86_64_EXTRACFLAGS" )); + strarray_add( &cflags, "-D__arm64ec_x64__" ); + strarray_addall( &cflags, get_expanded_make_var_array( top_makefile, "x86_64_EXTRACFLAGS" )); } else { var_cc = arch_make_variable( "CC", arch ); var_cflags = arch_make_variable( "CFLAGS", arch ); - strarray_addall( &arch_cflags, make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] ); + strarray_addall( &cflags, make->extlib ? extra_cflags_extlib[arch] : extra_cflags[arch] ); }
- output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename ); - output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), var_cc, source->filename ); - output_filenames( defines ); - if (!source->use_msvcrt) output_filenames( make->unix_cflags ); - output_filenames( arch_cflags ); - if (!arch) { if (source->file->flags & FLAG_C_UNIX) { - output_filenames( unix_dllflags ); + strarray_addall( &cflags, unix_dllflags ); } else if (make->module || make->testdll) { - output_filenames( dll_flags ); - if (source->use_msvcrt) output_filenames( msvcrt_flags ); + strarray_addall( &cflags, dll_flags ); + if (source->use_msvcrt) strarray_addall( &cflags, msvcrt_flags ); if (!unix_lib_supported && make->module && is_crt_module( make->module )) - output_filename( "-fno-builtin" ); + strarray_add( &cflags, "-fno-builtin" ); } } else { - if (make->module && is_crt_module( make->module )) output_filename( "-fno-builtin" ); + if (make->module && is_crt_module( make->module )) strarray_add( &cflags, "-fno-builtin" ); }
- output_filenames( cpp_flags ); + strarray_addall( &cflags, cpp_flags ); + + output( "%s: %s\n", obj_dir_path( make, obj_name ), source->filename ); + output( "\t%s%s -c -o $@ %s", cmd_prefix( "CC" ), var_cc, source->filename ); + output_filenames( defines ); + output_filenames( cflags ); output_filename( var_cflags ); output( "\n" );
@@ -3254,6 +3287,29 @@ static void output_source_one_arch( struct makefile *make, struct incl_file *sou root_src_dir_path( "tools/runtest" ), make->testdll, obj_dir_path( make, arch_module_name( test_exe, arch )), obj ); } + + if (!(source->file->flags & FLAG_GENERATED)) + { + struct compile_command *cmd = malloc( sizeof(*cmd) ); + + cmd->source = source->filename; + cmd->obj = obj_dir_path( make, obj_name ); + cmd->args = empty_strarray; + strarray_addall( &cmd->args, defines ); + strarray_addall( &cmd->args, cflags ); + + if ((source->file->flags & FLAG_ARM64EC_X64) && !strcmp( archs.str[arch], "arm64ec" )) + { + cmd->cmd = get_expanded_make_variable( make, "x86_64_CC" ); + strarray_add( &cmd->args, get_expanded_make_variable( make, "x86_64_CFLAGS" )); + } + else + { + cmd->cmd = get_expanded_arch_var( make, "CC", arch ); + strarray_add( &cmd->args, get_expanded_arch_var( make, "CFLAGS", arch )); + } + list_add_tail( &compile_commands, &cmd->entry ); + } }
@@ -4043,6 +4099,41 @@ static void output_linguas( const struct makefile *make ) }
+/******************************************************************* + * output_compile_commands + */ +static void output_compile_commands( const char *dest ) +{ + struct compile_command *cmd; + unsigned int i; + const char *dir; + char buffer[PATH_MAX]; + + output_file = create_temp_file( dest ); + + getcwd( buffer, sizeof(buffer) ); + dir = escape_cstring( buffer ); + + output( "[\n" ); + LIST_FOR_EACH_ENTRY( cmd, &compile_commands, struct compile_command, entry ) + { + output( " {\n" ); + output( " "command": "%s -c -o %s %s", cmd->cmd, cmd->obj, cmd->source ); + for (i = 0; i < cmd->args.count; i++) output( " %s", escape_cstring( cmd->args.str[i] )); + output( "",\n" ); + output( " "file": "%s",\n", cmd->source ); + output( " "output": "%s",\n", cmd->obj ); + output( " "directory": "%s"\n", dir ); + output( " }%s\n", list_next( &compile_commands, &cmd->entry ) ? "," : "" ); + } + output( "]\n" ); + + if (fclose( output_file )) fatal_perror( "write" ); + output_file = NULL; + rename_temp_file( dest ); +} + + /******************************************************************* * output_testlist */ @@ -4216,7 +4307,8 @@ static void output_top_makefile( struct makefile *make ) makedep = strmake( "%s%s",tools_dir_path( make, "makedep" ), tools_ext ); output( "Makefile: %s\n", makedep ); output( "depend: %s\n", makedep ); - output( "\t%s%s\n", makedep, + output( "\t%s%s%s\n", makedep, + compile_commands_mode ? " -C" : "", silent_rules ? " -S" : "" ); strarray_add( &make->phony_targets, "depend" );
@@ -4396,6 +4488,9 @@ static int parse_option( const char *opt ) case 'f': if (opt[2]) output_makefile_name = opt + 2; break; + case 'C': + compile_commands_mode = 1; + break; case 'S': silent_rules = 1; break; @@ -4550,5 +4645,7 @@ int main( int argc, char *argv[] ) output_dependencies( top_makefile ); for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
+ if (compile_commands_mode) output_compile_commands( "compile_commands.json" ); + return 0; }