From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from elaine.keithp.com (home.keithp.com [63.227.221.253]) by sourceware.org (Postfix) with ESMTPS id 703BC3884596 for ; Sat, 27 Aug 2022 05:14:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 703BC3884596 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=keithp.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=keithp.com Received: from localhost (localhost [127.0.0.1]) by elaine.keithp.com (Postfix) with ESMTP id 1959A3F30347 for ; Fri, 26 Aug 2022 22:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=keithp.com; s=mail; t=1661577265; bh=LWi8rEBk4TrUoXgtHfyAJhfeyTpCtiFNDBjHnNS0WYQ=; h=From:To:Cc:Subject:Date:From; b=TrLJtpV86v+3DW+80+908OttlLAAjRVkfrkekM5iN3/nrfU8YfDkgj9nHtmu/wXyb +FB6kkeHD46WB8oQlWcsu5KjQGIaunZzDoZW2KX0wpGG8LJbZv0Bq4HvJSELZ6wMWb 8bwpjea+oYaf57HmoM59qhUB5aveOBH0cm4Lb1grLcgzenZEAkIEM/9CDrJfQm84VF XOnZrt2xe8Kuxl0X4scsBL6qh7N9Y26y1OL9Y3d95VRMgLJdKf33xOVMr0/L2utZCk eKgq6AqwYYt4v/19e62CoOwr8V1b9CnDQ1cEweuBVlzx1i/10/Kx2SMOzYTaiyJfFK MOH7qoOR9S8UQ== X-Virus-Scanned: Debian amavisd-new at keithp.com Received: from elaine.keithp.com ([127.0.0.1]) by localhost (elaine.keithp.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id FPJqyxww-gW2; Fri, 26 Aug 2022 22:14:24 -0700 (PDT) Received: from keithp.com (koto.keithp.com [192.168.11.2]) by elaine.keithp.com (Postfix) with ESMTPSA id 7DF7F3F30342; Fri, 26 Aug 2022 22:14:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=keithp.com; s=mail; t=1661577264; bh=LWi8rEBk4TrUoXgtHfyAJhfeyTpCtiFNDBjHnNS0WYQ=; h=From:To:Cc:Subject:Date:From; b=uCoOoT314Bu0wZ+2+B/6y8xBhuyClulWmZiy6jqnxWtouREkrnSGlBZMHUzN9dZDR ArYjsV80uJKP76w9Rc73DDlTRSLVY/+ovGW7Nsagc9aKwvBeZDQ2+MQnOwvgv1Ff7u T7ouuP0SiQ9HCrTlkF1dK/xTO2yr/2kcFNpM7hpkPMXtRcSZv+o+9y5SxXpiohKKRQ 3We/smQehEL71UxwLEl7L6tCdten5zE5QaBSqoPMqgE7rCzaHX+qYHIimE1Z5h/GwH UZ6sM7LHPoBwj+Wc2NZh60jqmzDuMoYlYkrX9W1upUn3TfU5KOJUHPjRY/aLGNrbu7 1WVavjq7uX8PQ== Received: by keithp.com (Postfix, from userid 1000) id 27C861E601CB; Fri, 26 Aug 2022 22:14:24 -0700 (PDT) From: Keith Packard To: gcc-patches@gcc.gnu.org Cc: Keith Packard Subject: [PATCH] driver: Extend 'getenv' function to allow default value Date: Fri, 26 Aug 2022 22:14:22 -0700 Message-Id: <20220827051422.1023580-1-keithp@keithp.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,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 List-Id: Right now, a missing environment variable provided to the 'getenv' function in a .specs file causes a fatal error. That makes writing a spec file that uses the GCC_EXEC_PREFIX value difficult as that variable is only set when the driver has been relocated, but not when run from the defined location. This makes building a relocatable toolchain difficult to extend to other ancilary pieces which use specs files to locate header and library files adjacent to the toolchain. This patch adds an optional third argument to the getenv function that can be used to fall back to the standard installation path when the driver hasn't set GCC_EXEC_PREFIX in the environment. For example, if an alternate C library is installed in ${prefix}/extra, then this change allows the specs file to locate that relative to the gcc directory, if gcc is located in the original installation directory (which would leave GCC_EXEC_PREFIX unset), or if the gcc tree has been moved to a different location (where gcc would set GCC_EXEC_PREFIX itself): *cpp: -isystem %:getenv(GCC_EXEC_PREFIX ../../extra/include ${prefix}/extra/include) I considered changing the behavior of either the %R sequence so that it had a defined behavior when there was no sysroot defined, or making the driver always set the GCC_EXEC_PREFIX environment variable and decided that the approach of adding functionality to getenv where it was previously invalid would cause the least potential for affecting existing usage. Signed-off-by: Keith Packard --- gcc/doc/invoke.texi | 18 +++++++++++------- gcc/gcc.cc | 10 +++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 6131bfa7acf..669c28a609a 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -34223,17 +34223,21 @@ The following built-in spec functions are provided: @table @code @item @code{getenv} -The @code{getenv} spec function takes two arguments: an environment -variable name and a string. If the environment variable is not -defined, a fatal error is issued. Otherwise, the return value is the -value of the environment variable concatenated with the string. For -example, if @env{TOPDIR} is defined as @file{/path/to/top}, then: + +The @code{getenv} spec function takes two or three arguments: an +environment variable name, a string and an optional default value. If +the environment variable is not defined and a default value is +provided, that is used as the return value; otherwise a fatal error is +issued. Otherwise, the return value is the value of the environment +variable concatenated with the string. For example, if @env{TOPDIR} +is defined as @file{/path/to/top}, then: @smallexample -%:getenv(TOPDIR /include) +%:getenv(TOPDIR /include /path/to/default/include) @end smallexample -expands to @file{/path/to/top/include}. +expands to @file{/path/to/top/include}. If @env{TOPDIR} is not +defined, then this expands to @file{/path/to/default/include}. @item @code{if-exists} The @code{if-exists} spec function takes one argument, an absolute diff --git a/gcc/gcc.cc b/gcc/gcc.cc index b6d562a92f0..16295702db7 100644 --- a/gcc/gcc.cc +++ b/gcc/gcc.cc @@ -10169,12 +10169,20 @@ getenv_spec_function (int argc, const char **argv) char *ptr; size_t len; - if (argc != 2) + if (argc != 2 && argc != 3) return NULL; varname = argv[0]; value = env.get (varname); + if (!value && argc == 3) + { + value = argv[2]; + result = XNEWVAR(char, strlen(value) + 1); + strcpy(result, value); + return result; + } + /* If the variable isn't defined and this is allowed, craft our expected return value. Assume variable names used in specs strings don't contain any active spec character so don't need escaping. */ -- 2.36.1 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from elaine.keithp.com (home.keithp.com [63.227.221.253]) by sourceware.org (Postfix) with ESMTPS id 703BC3884596 for ; Sat, 27 Aug 2022 05:14:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 703BC3884596 Received: from localhost (localhost [127.0.0.1]) by elaine.keithp.com (Postfix) with ESMTP id 1959A3F30347 for ; Fri, 26 Aug 2022 22:14:25 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at keithp.com Received: from elaine.keithp.com ([127.0.0.1]) by localhost (elaine.keithp.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id FPJqyxww-gW2; Fri, 26 Aug 2022 22:14:24 -0700 (PDT) Received: from keithp.com (koto.keithp.com [192.168.11.2]) by elaine.keithp.com (Postfix) with ESMTPSA id 7DF7F3F30342; Fri, 26 Aug 2022 22:14:24 -0700 (PDT) Received: by keithp.com (Postfix, from userid 1000) id 27C861E601CB; Fri, 26 Aug 2022 22:14:24 -0700 (PDT) From: Keith Packard To: gcc-patches@gcc.gnu.org Subject: [PATCH] driver: Extend 'getenv' function to allow default value Date: Fri, 26 Aug 2022 22:14:22 -0700 Message-ID: <20220827051422.1023580-1-keithp@keithp.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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: Sat, 27 Aug 2022 05:14:28 -0000 Message-ID: <20220827051422.yPS3fr4cNOisSbRAMVhPd4k9hTSHBuzq5u9qXZAdb8I@z> Right now, a missing environment variable provided to the 'getenv' function in a .specs file causes a fatal error. That makes writing a spec file that uses the GCC_EXEC_PREFIX value difficult as that variable is only set when the driver has been relocated, but not when run from the defined location. This makes building a relocatable toolchain difficult to extend to other ancilary pieces which use specs files to locate header and library files adjacent to the toolchain. This patch adds an optional third argument to the getenv function that can be used to fall back to the standard installation path when the driver hasn't set GCC_EXEC_PREFIX in the environment. For example, if an alternate C library is installed in ${prefix}/extra, then this change allows the specs file to locate that relative to the gcc directory, if gcc is located in the original installation directory (which would leave GCC_EXEC_PREFIX unset), or if the gcc tree has been moved to a different location (where gcc would set GCC_EXEC_PREFIX itself): *cpp: -isystem %:getenv(GCC_EXEC_PREFIX ../../extra/include ${prefix}/extra/include) I considered changing the behavior of either the %R sequence so that it had a defined behavior when there was no sysroot defined, or making the driver always set the GCC_EXEC_PREFIX environment variable and decided that the approach of adding functionality to getenv where it was previously invalid would cause the least potential for affecting existing usage. Signed-off-by: Keith Packard --- gcc/doc/invoke.texi | 18 +++++++++++------- gcc/gcc.cc | 10 +++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 6131bfa7acf..669c28a609a 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -34223,17 +34223,21 @@ The following built-in spec functions are provided: @table @code @item @code{getenv} -The @code{getenv} spec function takes two arguments: an environment -variable name and a string. If the environment variable is not -defined, a fatal error is issued. Otherwise, the return value is the -value of the environment variable concatenated with the string. For -example, if @env{TOPDIR} is defined as @file{/path/to/top}, then: + +The @code{getenv} spec function takes two or three arguments: an +environment variable name, a string and an optional default value. If +the environment variable is not defined and a default value is +provided, that is used as the return value; otherwise a fatal error is +issued. Otherwise, the return value is the value of the environment +variable concatenated with the string. For example, if @env{TOPDIR} +is defined as @file{/path/to/top}, then: @smallexample -%:getenv(TOPDIR /include) +%:getenv(TOPDIR /include /path/to/default/include) @end smallexample -expands to @file{/path/to/top/include}. +expands to @file{/path/to/top/include}. If @env{TOPDIR} is not +defined, then this expands to @file{/path/to/default/include}. @item @code{if-exists} The @code{if-exists} spec function takes one argument, an absolute diff --git a/gcc/gcc.cc b/gcc/gcc.cc index b6d562a92f0..16295702db7 100644 --- a/gcc/gcc.cc +++ b/gcc/gcc.cc @@ -10169,12 +10169,20 @@ getenv_spec_function (int argc, const char **argv) char *ptr; size_t len; - if (argc != 2) + if (argc != 2 && argc != 3) return NULL; varname = argv[0]; value = env.get (varname); + if (!value && argc == 3) + { + value = argv[2]; + result = XNEWVAR(char, strlen(value) + 1); + strcpy(result, value); + return result; + } + /* If the variable isn't defined and this is allowed, craft our expected return value. Assume variable names used in specs strings don't contain any active spec character so don't need escaping. */ -- 2.36.1