From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1851) id E6DA63857C6D; Fri, 4 Dec 2020 07:59:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E6DA63857C6D Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Martin Liska To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/users/marxin/heads/record-gcc-switches-factoring-v3)] Change semantics of -frecord-gcc-switches and add -frecord-gcc-switches-format. X-Act-Checkin: gcc X-Git-Author: Martin Liska X-Git-Refname: refs/users/marxin/heads/record-gcc-switches-factoring-v3 X-Git-Oldrev: 23de1dbbd81f662e9eb4a67385b0a2d0773569be X-Git-Newrev: 397e1a28c3e5c0aa9f44a8e19115cc7c50d8b388 Message-Id: <20201204075911.E6DA63857C6D@sourceware.org> Date: Fri, 4 Dec 2020 07:59:11 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Dec 2020 07:59:12 -0000 https://gcc.gnu.org/g:397e1a28c3e5c0aa9f44a8e19115cc7c50d8b388 commit 397e1a28c3e5c0aa9f44a8e19115cc7c50d8b388 Author: Martin Liska Date: Fri Dec 4 08:52:33 2020 +0100 Change semantics of -frecord-gcc-switches and add -frecord-gcc-switches-format. gcc/ChangeLog: * common.opt: Document new options -frecord-gcc-switches-format and -frecord-gcc-switches-file. * doc/invoke.texi: Document the new options. * dwarf2out.c (dwarf2out_early_finish): Respect flag_record_gcc_switches_format. * flag-types.h (enum record_gcc_switches_format): New. * gcc.c (save_driver_cmdline): Save command line to a temporary file. (driver::main): Call set_commandline. (driver::set_commandline): New. * gcc.h (set_commandline): New. * opts.c (get_driver_command_line): New. * opts.h (get_driver_command_line): New. * toplev.c (init_asm_output): Use new function get_producer_string. (process_options): Respect flag_record_gcc_switches_format option. Co-Authored-By: Egeyar Bagcioglu Diff: --- gcc/common.opt | 19 ++++++++++++++++++- gcc/doc/invoke.texi | 23 ++++++++++++++++++++++- gcc/dwarf2out.c | 17 ++++++++++++----- gcc/flag-types.h | 7 +++++++ gcc/gcc.c | 37 ++++++++++++++++++++++++++++++++++++- gcc/gcc.h | 1 + gcc/opts.c | 31 +++++++++++++++++++++++++++++++ gcc/opts.h | 2 ++ gcc/toplev.c | 27 ++++++++++++++++++++------- 9 files changed, 149 insertions(+), 15 deletions(-) diff --git a/gcc/common.opt b/gcc/common.opt index 582e2aa8774..eb112fcdf90 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -2322,9 +2322,26 @@ Common Joined RejectNegative Var(common_deferred_options) Defer ; records information in the assembler output file as comments, so ; they never reach the object file. frecord-gcc-switches -Common Report Var(flag_record_gcc_switches) +Common Driver Report Var(flag_record_gcc_switches) Record gcc command line switches in the object file. +Enum +Name(record_gcc_switches_format) Type(enum record_gcc_switches_format) + +EnumValue +Enum(record_gcc_switches_format) String(processed) Value(RECORD_GCC_SWITCHES_PROCESSED) + +EnumValue +Enum(record_gcc_switches_format) String(driver) Value(RECORD_GCC_SWITCHES_DRIVER) + +frecord-gcc-switches-format= +Common Report Joined RejectNegative Var(flag_record_gcc_switches_format) Enum(record_gcc_switches_format) Init(RECORD_GCC_SWITCHES_PROCESSED) +-frecord-gcc-switches-format=[processed|driver] Format of recorded gcc command line switches. + +frecord-gcc-switches-file= +Common Report Joined RejectNegative Var(flag_record_gcc_switches_file) Undocumented +Path to file that contains driver command line options. + freg-struct-return Common Report Var(flag_pcc_struct_return,0) Optimization Return small aggregates in registers. diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 615eae9a1c5..76f938da5da 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -643,7 +643,7 @@ Objective-C and Objective-C++ Dialects}. -finhibit-size-directive -fcommon -fno-ident @gol -fpcc-struct-return -fpic -fPIC -fpie -fPIE -fno-plt @gol -fno-jump-tables -fno-bit-tests @gol --frecord-gcc-switches @gol +-frecord-gcc-switches -frecord-gcc-switches-format @gol -freg-struct-return -fshort-enums -fshort-wchar @gol -fverbose-asm -fpack-struct[=@var{n}] @gol -fleading-underscore -ftls-model=@var{model} @gol @@ -16206,6 +16206,27 @@ comments, so it never reaches the object file. See also @option{-grecord-gcc-switches} for another way of storing compiler options into the object file. +@item -frecord-gcc-switches-format=@var{format} +@opindex frecord-gcc-switches-format +This switch controls the output format of options that record +the command line. The affected options are @option{-frecord-gcc-switches}, +@option{-grecord-gcc-switches} and @option{-fverbose-asm}. + +The @var{format} argument should be one of the following: + +@table @samp + +@item processed + +Options are printed after processing by the compiler driver. +It is the default option value. + +@item driver + +Options are printed as provided to the compiler driver. + +@end table + @item -fpic @opindex fpic @cindex global offset table diff --git a/gcc/dwarf2out.c b/gcc/dwarf2out.c index 30e7ca68dbc..184f3363008 100644 --- a/gcc/dwarf2out.c +++ b/gcc/dwarf2out.c @@ -32048,13 +32048,20 @@ dwarf2out_early_finish (const char *filename) header compilation, so always fill it with empty string initially and overwrite only here. */ dw_attr_node *producer = get_AT (comp_unit_die (), DW_AT_producer); + producer_string = concat (lang_hooks.name, " ", version_string, NULL); if (dwarf_record_gcc_switches) - producer_string = gen_producer_string (lang_hooks.name, - save_decoded_options, - save_decoded_options_count); - else - producer_string = concat (lang_hooks.name, " ", version_string, NULL); + { + char *producer_string_old = producer_string; + if (flag_record_gcc_switches_format == RECORD_GCC_SWITCHES_DRIVER) + producer_string = concat (producer_string, " ", + get_driver_command_line (), NULL); + else + producer_string = gen_producer_string (lang_hooks.name, + save_decoded_options, + save_decoded_options_count); + free (producer_string_old); + } producer->dw_attr_val.v.val_str->refcount--; producer->dw_attr_val.v.val_str = find_AT_string (producer_string); diff --git a/gcc/flag-types.h b/gcc/flag-types.h index 9342bd87be3..4ded267f197 100644 --- a/gcc/flag-types.h +++ b/gcc/flag-types.h @@ -427,6 +427,13 @@ enum openacc_kernels OPENACC_KERNELS_PARLOOPS }; +/* Record GCC options type. */ +enum record_gcc_switches_format +{ + RECORD_GCC_SWITCHES_PROCESSED = 0, + RECORD_GCC_SWITCHES_DRIVER +}; + #endif #endif /* ! GCC_FLAG_TYPES_H */ diff --git a/gcc/gcc.c b/gcc/gcc.c index 1d32375f648..7ef888c8b18 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -235,6 +235,12 @@ static int verbose_only_flag; static int print_subprocess_help; +/* argc and argv provided to the driver. Necessary to keep for when + -frecord-gcc-switches-format=driver is given. */ + +static unsigned int driver_gcc_argc; +static const char **driver_gcc_argv; + /* Linker suffix passed to -fuse-ld=... */ static const char *use_ld; @@ -433,6 +439,7 @@ static const char *greater_than_spec_func (int, const char **); static const char *debug_level_greater_than_spec_func (int, const char **); static const char *dwarf_version_greater_than_spec_func (int, const char **); static const char *find_fortran_preinclude_file (int, const char **); +static const char *save_driver_cmdline (int, const char **); static char *convert_white_space (char *); static char *quote_spec (char *); static char *quote_spec_arg (char *); @@ -1276,7 +1283,9 @@ static const char *cc1_options = %{coverage:-fprofile-arcs -ftest-coverage}\ %{fprofile-arcs|fprofile-generate*|coverage:\ %{!fprofile-update=single:\ - %{pthread:-fprofile-update=prefer-atomic}}}"; + %{pthread:-fprofile-update=prefer-atomic}}}\ + %{frecord-gcc-switches-format=driver:-frecord-gcc-switches-file=%:save-driver-cmdline(%g.cmdline)\ + %<-frecord-gcc-switches-format=driver}"; static const char *asm_options = "%{-target-help:%:print-asm-header()} " @@ -1768,6 +1777,7 @@ static const struct spec_function static_spec_functions[] = { "debug-level-gt", debug_level_greater_than_spec_func }, { "dwarf-version-gt", dwarf_version_greater_than_spec_func }, { "fortran-preinclude-file", find_fortran_preinclude_file}, + { "save-driver-cmdline", save_driver_cmdline}, #ifdef EXTRA_SPEC_FUNCTIONS EXTRA_SPEC_FUNCTIONS #endif @@ -8012,6 +8022,7 @@ driver::main (int argc, char **argv) set_progname (argv[0]); expand_at_files (&argc, &argv); + set_commandline (argc, const_cast (argv)); decode_argv (argc, const_cast (argv)); global_initializations (); build_multilib_strings (); @@ -8055,6 +8066,15 @@ driver::set_progname (const char *argv0) const xmalloc_set_program_name (progname); } +/* Keep the command line for -frecord-gcc-switches-format=driver. */ + +void +driver::set_commandline (int argc, const char **argv) const +{ + driver_gcc_argc = argc; + driver_gcc_argv = argv; +} + /* Expand any @ files within the command-line args, setting at_file_supplied if any were expanded. */ @@ -10807,6 +10827,21 @@ find_fortran_preinclude_file (int argc, const char **argv) return result; } +/* Save driver options to a temporary file. */ + +static const char * +save_driver_cmdline (int argc ATTRIBUTE_UNUSED, + const char **argv) +{ + FILE *f = fopen (argv[0], "w"); + for (unsigned int i = 0; i < driver_gcc_argc; i++) + fprintf (f, i == 0 ? "%s" : " %s", driver_gcc_argv[i]); + fclose (f); + + return argv[0]; +} + + /* If any character in ORIG fits QUOTE_P (_, P), reallocate the string so as to precede every one of them with a backslash. Return the original string or the reallocated one. */ diff --git a/gcc/gcc.h b/gcc/gcc.h index 70d8f08f059..cde77fbaad7 100644 --- a/gcc/gcc.h +++ b/gcc/gcc.h @@ -37,6 +37,7 @@ class driver private: void set_progname (const char *argv0) const; + void set_commandline (int argc, const char **argv) const; void expand_at_files (int *argc, char ***argv) const; void decode_argv (int argc, const char **argv); void global_initializations (); diff --git a/gcc/opts.c b/gcc/opts.c index efaa4348dde..a760f8d6d21 100644 --- a/gcc/opts.c +++ b/gcc/opts.c @@ -3299,6 +3299,7 @@ char *gen_command_line_string (cl_decoded_option *options, case OPT_SPECIAL_input_file: case OPT_grecord_gcc_switches: case OPT_frecord_gcc_switches: + case OPT_frecord_gcc_switches_format_: case OPT__output_pch_: case OPT_fdiagnostics_show_location_: case OPT_fdiagnostics_show_option: @@ -3384,6 +3385,36 @@ gen_producer_string (const char *language_string, cl_decoded_option *options, gen_command_line_string (options, options_count), NULL); } +/* Return value of env variable GCC_DRIVER_COMMAND_LINE if exists. + Otherwise return empty string. */ + +const char * +get_driver_command_line () +{ + static char *cmdline = NULL; + + if (cmdline != NULL) + return cmdline; + + FILE *f = fopen (flag_record_gcc_switches_file, "r"); + if (f == NULL) + fatal_error (UNKNOWN_LOCATION, "cannot open %s: %m", flag_record_gcc_switches_file); + + fseek(f, 0, SEEK_END); + unsigned int size = ftell (f); + fseek(f, 0, SEEK_SET); + + cmdline = XNEWVEC (char, size + 1); + + unsigned int read; + char *ptr = cmdline; + while ((read = fread (ptr, 4096, 1, f))) + ptr += read; + + cmdline[size] = '\0'; + return cmdline; +} + #if CHECKING_P namespace selftest { diff --git a/gcc/opts.h b/gcc/opts.h index b36c0d18759..a3f1823071b 100644 --- a/gcc/opts.h +++ b/gcc/opts.h @@ -488,6 +488,8 @@ extern char *gen_producer_string (const char *language_string, cl_decoded_option *options, unsigned int options_count); +extern const char *get_driver_command_line (); + /* Set OPTION in OPTS to VALUE if the option is not set in OPTS_SET. */ #define SET_OPTION_IF_UNSET(OPTS, OPTS_SET, OPTION, VALUE) \ diff --git a/gcc/toplev.c b/gcc/toplev.c index 93a41943b8c..6891addba75 100644 --- a/gcc/toplev.c +++ b/gcc/toplev.c @@ -731,11 +731,21 @@ init_asm_output (const char *name) { if (targetm.asm_out.record_gcc_switches) { - const char *str - = gen_producer_string (lang_hooks.name, - save_decoded_options, - save_decoded_options_count); - targetm.asm_out.record_gcc_switches (str); + if (flag_record_gcc_switches_format == RECORD_GCC_SWITCHES_DRIVER) + { + const char *str = concat (version_string, " ", + get_driver_command_line (), NULL); + targetm.asm_out.record_gcc_switches (str); + free (CONST_CAST (char *, str)); + } + else + { + const char *str + = gen_producer_string (lang_hooks.name, + save_decoded_options, + save_decoded_options_count); + targetm.asm_out.record_gcc_switches (str); + } } else inform (UNKNOWN_LOCATION, @@ -1384,8 +1394,11 @@ process_options (void) if (!quiet_flag) { fputs ("options passed: ", stderr); - fputs (gen_command_line_string (save_decoded_options, - save_decoded_options_count), stderr); + if (flag_record_gcc_switches_format == RECORD_GCC_SWITCHES_DRIVER) + fputs (get_driver_command_line (), stderr); + else + fputs (gen_command_line_string (save_decoded_options, + save_decoded_options_count), stderr); fputc ('\n', stderr); } }