On 8/1/19 4:41 PM, Jakub Jelinek wrote:
> On Thu, Aug 01, 2019 at 04:34:09PM +0200, Martin Liška wrote:
>> Ok, after deeper discussion with Honza, I would like to suggest the original
>> patch that was about proper detection of jobserver.
>>
>> Can you please Jakub test the patch in your environment?
> 
> Isn't this done too late (as in, doesn't the driver at that moment already
> have some files newly openend, like e.g. the @ option files?

You are right, I've reworked that. Good observation.

> 
>> +            = ((sscanf (n, "--jobserver-auth=%d,%d", &rfd, &wfd) == 2)
> 
> No need to wrap sscanf (...) == 2 into ()s.  Also, you've already done
> a strstr, what is the point in verifying it once again that it starts with
> --jobserver-auth= string?
> And in the lto-writer.c code there is no space between sscanf and (.

Yep, I simplified that.

Martin

> 
>       Jakub
> 

>From 9f96e16a7798fd3b60fa9ec5b7a66748212146c4 Mon Sep 17 00:00:00 2001
From: Martin Liska <mli...@suse.cz>
Date: Thu, 1 Aug 2019 16:30:01 +0200
Subject: [PATCH] Properly detect working jobserver in gcc driver.

gcc/ChangeLog:

2019-08-02  Martin Liska  <mli...@suse.cz>

	* gcc.c (driver::main): Call detect_jobserver right
	at the very start of the function.
	(driver::detect_jobserver): Test whether jobserver
	is active from GCC driver. That will prevent situation where
	GCC is invoked from a LD plugin and the linker already uses
	file descriptors suggested by make.  That leads to a wrong
	detection.
	* gcc.h (driver): Add detect_jobserver.
	* lto-wrapper.c (jobserver_active_p): Simplify sscanf by
	not scanning for --jobserver-auth prefix.
---
 gcc/gcc.c         | 41 +++++++++++++++++++++++++++++++++++++++++
 gcc/gcc.h         |  1 +
 gcc/lto-wrapper.c |  2 +-
 3 files changed, 43 insertions(+), 1 deletion(-)

diff --git a/gcc/gcc.c b/gcc/gcc.c
index a4323eb146e..c8753b9671f 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -7354,6 +7354,7 @@ driver::main (int argc, char **argv)
 {
   bool early_exit;
 
+  detect_jobserver ();
   set_progname (argv[0]);
   expand_at_files (&argc, &argv);
   decode_argv (argc, const_cast <const char **> (argv));
@@ -8357,6 +8358,46 @@ driver::final_actions () const
     }
 }
 
+/* Detect whether jobserver is active and working.  If not drop
+   --jobserver-auth from MAKEFLAGS.  */
+
+void
+driver::detect_jobserver () const
+{
+  /* Detect jobserver and drop it if it's not working.  */
+  const char *makeflags = env.get ("MAKEFLAGS");
+  if (makeflags != NULL)
+    {
+      const char *needle = "--jobserver-auth=";
+      const char *n = strstr (makeflags, needle);
+      if (n != NULL)
+	{
+	  int rfd = -1;
+	  int wfd = -1;
+
+	  bool jobserver
+	    = (sscanf (n + strlen (needle), "%d,%d", &rfd, &wfd) == 2
+	       && rfd > 0
+	       && wfd > 0
+	       && fcntl (rfd, F_GETFD) >= 0
+	       && fcntl (wfd, F_GETFD) >= 0);
+
+	  /* Drop the jobserver if it's not working now.  */
+	  if (!jobserver)
+	    {
+	      unsigned offset = n - makeflags;
+	      char *dup = xstrdup (makeflags);
+	      dup[offset] = '\0';
+
+	      const char *space = strchr (makeflags + offset, ' ');
+	      if (space != NULL)
+		strcpy (dup + offset, space);
+	      xputenv (concat ("MAKEFLAGS=", dup, NULL));
+	    }
+	}
+    }
+}
+
 /* Determine what the exit code of the driver should be.  */
 
 int
diff --git a/gcc/gcc.h b/gcc/gcc.h
index a0a1d94c6e6..dc77dba67fb 100644
--- a/gcc/gcc.h
+++ b/gcc/gcc.h
@@ -51,6 +51,7 @@ class driver
   void do_spec_on_infiles () const;
   void maybe_run_linker (const char *argv0) const;
   void final_actions () const;
+  void detect_jobserver () const;
   int get_exit_code () const;
 
  private:
diff --git a/gcc/lto-wrapper.c b/gcc/lto-wrapper.c
index 353187c6043..3414adedd26 100644
--- a/gcc/lto-wrapper.c
+++ b/gcc/lto-wrapper.c
@@ -1234,7 +1234,7 @@ jobserver_active_p (void)
   int rfd = -1;
   int wfd = -1;
 
-  return ((sscanf(n, "--jobserver-auth=%d,%d", &rfd, &wfd) == 2)
+  return (sscanf (n + strlen (needle), "%d,%d", &rfd, &wfd) == 2
 	  && rfd > 0
 	  && wfd > 0
 	  && fcntl (rfd, F_GETFD) >= 0
-- 
2.22.0

Reply via email to