Hey.

It's quite common case that a build system does not a -flto=foo option
as part of linker command. We should merge -flto options from the corresponding
object files.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

gcc/ChangeLog:

        * lto-wrapper.c (merge_and_complain): Merge -flto=arg options.
        (run_gcc): Use -flto argument detection for merged
        fdecoded_options.
---
 gcc/lto-wrapper.c | 78 +++++++++++++++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 23 deletions(-)

diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c
index 5ccf729b249..a4d499d5ad8 100644
--- a/gcc/lto-wrapper.c
+++ b/gcc/lto-wrapper.c
@@ -420,6 +420,30 @@ merge_and_complain (vec<cl_decoded_option> decoded_options,
        case OPT_foffload_:
          decoded_options.safe_push (*foption);
          break;
+
+       case OPT_flto_:
+         if (existing_opt == NULL)
+           decoded_options.safe_push (*foption);
+         else
+           {
+             if (strcmp (foption->arg, existing_opt->arg) != 0)
+               {
+                 /* -flto=auto is preferred.  */
+                 if (strcmp (existing_opt->arg, "auto") == 0)
+                   ;
+                 else if (strcmp (foption->arg, "auto") == 0
+                          || strcmp (foption->arg, "jobserver") == 0)
+                   existing_opt->arg = foption->arg;
+                 else if (strcmp (existing_opt->arg, "jobserver") != 0)
+                   {
+                     int n = atoi (foption->arg);
+                     int original_n = atoi (existing_opt->arg);
+                     if (n > original_n)
+                       existing_opt->arg = foption->arg;
+                   }
+               }
+           }
+         break;
        }
     }
 
@@ -1441,6 +1465,37 @@ run_gcc (unsigned argc, char *argv[])
   append_compiler_options (&argv_obstack, fdecoded_options);
   append_linker_options (&argv_obstack, decoded_options);
 
+  /* Process LTO-related options on merged options.  */
+  for (j = 1; j < fdecoded_options.length (); ++j)
+    {
+      cl_decoded_option *option = &fdecoded_options[j];
+      switch (option->opt_index)
+       {
+       case OPT_flto_:
+         if (strcmp (option->arg, "jobserver") == 0)
+           {
+             parallel = 1;
+             jobserver = 1;
+           }
+         else if (strcmp (option->arg, "auto") == 0)
+           {
+             parallel = 1;
+             auto_parallel = 1;
+           }
+         else
+           {
+             parallel = atoi (option->arg);
+             if (parallel <= 1)
+               parallel = 0;
+           }
+         /* Fallthru.  */
+
+       case OPT_flto:
+         lto_mode = LTO_MODE_WHOPR;
+         break;
+       }
+    }
+
   /* Scan linker driver arguments for things that are of relevance to us.  */
   for (j = 1; j < decoded_options.length (); ++j)
     {
@@ -1468,29 +1523,6 @@ run_gcc (unsigned argc, char *argv[])
            no_partition = true;
          break;
 
-       case OPT_flto_:
-         if (strcmp (option->arg, "jobserver") == 0)
-           {
-             parallel = 1;
-             jobserver = 1;
-           }
-         else if (strcmp (option->arg, "auto") == 0)
-           {
-             parallel = 1;
-             auto_parallel = 1;
-           }
-         else
-           {
-             parallel = atoi (option->arg);
-             if (parallel <= 1)
-               parallel = 0;
-           }
-         /* Fallthru.  */
-
-       case OPT_flto:
-         lto_mode = LTO_MODE_WHOPR;
-         break;
-
        case OPT_flinker_output_:
          linker_output_rel = !strcmp (option->arg, "rel");
          break;
-- 
2.31.1

Reply via email to