From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by sourceware.org (Postfix) with ESMTPS id 5658C3858401 for ; Wed, 10 Aug 2022 06:56:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5658C3858401 Received: by mail-ed1-x52c.google.com with SMTP id w3so17892071edc.2 for ; Tue, 09 Aug 2022 23:56:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc; bh=sEanUuXQGKd5A0VRuXM3NroHT0+UtuDiXzugt3Plm6Y=; b=pbCUE+kajdDDTCUGb60toyepgmRn2wC8Jzl4d3/bg4NtF+rHK5/tr28xoK7HVmd/An 3x0JMUVBltgUhpYKCyMxUv04v1VEBNFvNRIyiufCvCYGL2X6NfSt9a/MUfivAZG7sA1q C5FdHuABTTXzDlasXQ9gAV7de9vtXAPj8yJrUouMX24/xaqxc8qiB58J7DmM1nbQz0MA B0pMWAAOnagoBX4IdMT55gFPoQpkwX4qDNRRlES6/0kTb0iRXwzOEvzTkx21EWEAudYF hdN4OputLj7Yc/LU8hEuvoeZvZyZlww9/rE5XTBxdA/u8czCIbvWa16OEUuTqYQewBzq +e7Q== X-Gm-Message-State: ACgBeo0XOeDA2NKasiHCNIleRkX16p0lNrCqMXa70vrDKAq8ruaNE2SW yrI2K13wVFR+t0XSPJeZsD1HO5tLTYfln0F/X/s= X-Google-Smtp-Source: AA6agR4szr3hgKxjLF5H0L2crIz1f2ABGhWcjZ6A1zyXXtEKP5y5F85fMym1Ra9V/JpNG5pmY6dj6QuvKQDwB3OdpSg= X-Received: by 2002:aa7:d447:0:b0:43f:b049:8e00 with SMTP id q7-20020aa7d447000000b0043fb0498e00mr21789098edr.218.1660114577985; Tue, 09 Aug 2022 23:56:17 -0700 (PDT) MIME-Version: 1.0 References: <7b34d4a1-4f34-cffb-8b5f-eb3424ca1049@suse.cz> In-Reply-To: <7b34d4a1-4f34-cffb-8b5f-eb3424ca1049@suse.cz> From: Richard Biener Date: Wed, 10 Aug 2022 08:56:05 +0200 Message-ID: Subject: Re: [PATCH 1/3] Factor out jobserver_active_p. To: =?UTF-8?Q?Martin_Li=C5=A1ka?= Cc: GCC Patches Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 10 Aug 2022 06:56:21 -0000 On Tue, Aug 9, 2022 at 2:03 PM Martin Li=C5=A1ka wrote: > > Patch can bootstrap on x86_64-linux-gnu and survives regression tests. > > Ready to be installed? > Thanks, > Martin > > gcc/ChangeLog: > > * gcc.cc (driver::detect_jobserver): Remove and move to > jobserver.h. > * lto-wrapper.cc (jobserver_active_p): Likewise. > (run_gcc): Likewise. > * jobserver.h: New file. > --- > gcc/gcc.cc | 36 +++----------------- > gcc/jobserver.h | 85 ++++++++++++++++++++++++++++++++++++++++++++++ > gcc/lto-wrapper.cc | 43 +++++------------------ > 3 files changed, 97 insertions(+), 67 deletions(-) > create mode 100644 gcc/jobserver.h > > diff --git a/gcc/gcc.cc b/gcc/gcc.cc > index 5cbb38560b2..69fbd293eaa 100644 > --- a/gcc/gcc.cc > +++ b/gcc/gcc.cc > @@ -43,6 +43,7 @@ compilation is specified by a string called a "spec". = */ > #include "opts.h" > #include "filenames.h" > #include "spellcheck.h" > +#include "jobserver.h" > > > > @@ -9178,38 +9179,9 @@ driver::final_actions () const > void > driver::detect_jobserver () const > { > - /* Detect jobserver and drop it if it's not working. */ > - const char *makeflags =3D env.get ("MAKEFLAGS"); > - if (makeflags !=3D NULL) > - { > - const char *needle =3D "--jobserver-auth=3D"; > - const char *n =3D strstr (makeflags, needle); > - if (n !=3D NULL) > - { > - int rfd =3D -1; > - int wfd =3D -1; > - > - bool jobserver > - =3D (sscanf (n + strlen (needle), "%d,%d", &rfd, &wfd) =3D=3D= 2 > - && rfd > 0 > - && wfd > 0 > - && is_valid_fd (rfd) > - && is_valid_fd (wfd)); > - > - /* Drop the jobserver if it's not working now. */ > - if (!jobserver) > - { > - unsigned offset =3D n - makeflags; > - char *dup =3D xstrdup (makeflags); > - dup[offset] =3D '\0'; > - > - const char *space =3D strchr (makeflags + offset, ' '); > - if (space !=3D NULL) > - strcpy (dup + offset, space); > - xputenv (concat ("MAKEFLAGS=3D", dup, NULL)); > - } > - } > - } > + jobserver_info jinfo; > + if (!jinfo.is_active && !jinfo.skipped_makeflags.empty ()) > + xputenv (jinfo.skipped_makeflags.c_str ()); > } > > /* Determine what the exit code of the driver should be. */ > diff --git a/gcc/jobserver.h b/gcc/jobserver.h > new file mode 100644 > index 00000000000..85453dd3c79 > --- /dev/null > +++ b/gcc/jobserver.h > @@ -0,0 +1,85 @@ > +/* GNU make's jobserver related functionality. > + Copyright (C) 2022 Free Software Foundation, Inc. > + > +This file is part of GCC. > + > +GCC is free software; you can redistribute it and/or modify it under > +the terms of the GNU General Public License as published by the Free > +Software Foundation; either version 3, or (at your option) any later > +version. > + > +GCC is distributed in the hope that it will be useful, but WITHOUT ANY > +WARRANTY; without even the implied warranty of MERCHANTABILITY or > +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License > +for more details. > + > +You should have received a copy of the GNU General Public License > +along with GCC; see the file COPYING3. If not see > +. > + > +See dbgcnt.def for usage information. */ > + > +#ifndef GCC_JOBSERVER_H > +#define GCC_JOBSERVER_H > + > +#include C++ standard library includes have to go through system.h (#define INCLUDE_STRING). Does the API really have to use std::string? > + > +using namespace std; > + > +struct jobserver_info > +{ > + /* Default constructor. */ > + jobserver_info (); > + > + /* Error message if there is a problem. */ > + string error_msg =3D ""; > + /* Skipped MAKEFLAGS where --jobserver-auth is skipped. */ > + string skipped_makeflags =3D ""; > + /* File descriptor for reading used for jobserver communication. */ > + int rfd =3D -1; > + /* File descriptor for writing used for jobserver communication. */ > + int wfd =3D -1; > + /* Return true if jobserver is active. */ > + bool is_active =3D false; > +}; > + > +jobserver_info::jobserver_info () > +{ > + /* Detect jobserver and drop it if it's not working. */ > + string js_needle =3D "--jobserver-auth=3D"; > + > + const char *envval =3D getenv ("MAKEFLAGS"); > + if (envval !=3D NULL) > + { > + string makeflags =3D envval; > + size_t n =3D makeflags.rfind (js_needle); > + if (n !=3D string::npos) > + { > + if (sscanf (makeflags.c_str () + n + js_needle.size (), > + "%d,%d", &rfd, &wfd) =3D=3D 2 > + && rfd > 0 > + && wfd > 0 > + && is_valid_fd (rfd) > + && is_valid_fd (wfd)) > + is_active =3D true; > + else > + { > + string dup =3D makeflags.substr (0, n); > + size_t pos =3D makeflags.find (' ', n); > + if (pos !=3D string::npos) > + dup +=3D makeflags.substr (pos); > + skipped_makeflags =3D "MAKEFLAGS=3D" + dup; > + error_msg > + =3D "cannot access %<" + js_needle + "%> file descriptors= "; > + } > + } > + error_msg =3D "%<" + js_needle + "%> is not present in %"; > + } > + else > + error_msg =3D "% environment variable is unset"; > + > + if (!error_msg.empty ()) > + error_msg =3D "jobserver is not available: " + error_msg; > +} > + > +#endif /* GCC_JOBSERVER_H */ > diff --git a/gcc/lto-wrapper.cc b/gcc/lto-wrapper.cc > index 795ab74555c..9279958d055 100644 > --- a/gcc/lto-wrapper.cc > +++ b/gcc/lto-wrapper.cc > @@ -49,6 +49,8 @@ along with GCC; see the file COPYING3. If not see > #include "lto-section-names.h" > #include "collect-utils.h" > #include "opts-diagnostic.h" > +#include "opt-suggestions.h" > +#include "jobserver.h" > > /* Environment variable, used for passing the names of offload targets f= rom GCC > driver to lto-wrapper. */ > @@ -1336,35 +1338,6 @@ init_num_threads (void) > #endif > } > > -/* Test and return reason why a jobserver cannot be detected. */ > - > -static const char * > -jobserver_active_p (void) > -{ > - #define JS_PREFIX "jobserver is not available: " > - #define JS_NEEDLE "--jobserver-auth=3D" > - > - const char *makeflags =3D getenv ("MAKEFLAGS"); > - if (makeflags =3D=3D NULL) > - return JS_PREFIX "% environment variable is unset"; > - > - const char *n =3D strstr (makeflags, JS_NEEDLE); > - if (n =3D=3D NULL) > - return JS_PREFIX "%<" JS_NEEDLE "%> is not present in %"= ; > - > - int rfd =3D -1; > - int wfd =3D -1; > - > - if (sscanf (n + strlen (JS_NEEDLE), "%d,%d", &rfd, &wfd) =3D=3D 2 > - && rfd > 0 > - && wfd > 0 > - && is_valid_fd (rfd) > - && is_valid_fd (wfd)) > - return NULL; > - else > - return JS_PREFIX "cannot access %<" JS_NEEDLE "%> file descriptors"; > -} > - > /* Print link to -flto documentation with a hint message. */ > > void > @@ -1422,7 +1395,6 @@ run_gcc (unsigned argc, char *argv[]) > bool jobserver_requested =3D false; > int auto_parallel =3D 0; > bool no_partition =3D false; > - const char *jobserver_error =3D NULL; > bool fdecoded_options_first =3D true; > vec fdecoded_options; > fdecoded_options.create (16); > @@ -1653,14 +1625,14 @@ run_gcc (unsigned argc, char *argv[]) > } > else > { > - jobserver_error =3D jobserver_active_p (); > - if (jobserver && jobserver_error !=3D NULL) > + jobserver_info jinfo; > + if (jobserver && !jinfo.is_active) > { > /* Fall back to auto parallelism. */ > jobserver =3D 0; > auto_parallel =3D 1; > } > - else if (!jobserver && jobserver_error =3D=3D NULL) > + else if (!jobserver && jinfo.is_active) > { > parallel =3D 1; > jobserver =3D 1; > @@ -1971,9 +1943,10 @@ cont: > > if (nr > 1) > { > - if (jobserver_requested && jobserver_error !=3D NULL) > + jobserver_info jinfo; > + if (jobserver_requested && !jinfo.is_active) > { > - warning (0, jobserver_error); > + warning (0, jinfo.error_msg.c_str ()); > print_lto_docs_link (); > } > else if (parallel =3D=3D 0) > -- > 2.37.1 > >