From patchwork Sat Aug 27 05:14:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keith Packard X-Patchwork-Id: 798 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:ecc5:0:0:0:0:0 with SMTP id s5csp516666wro; Fri, 26 Aug 2022 22:15:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR69Zzwxx+LjQhmhZlcEFnRcz/1T0TqOnwpCsSb31ME3IgUyRIJNdqrX3mC14mRBeiHdhd+E X-Received: by 2002:a17:907:284a:b0:730:a1f1:38b5 with SMTP id el10-20020a170907284a00b00730a1f138b5mr6913452ejc.732.1661577311378; Fri, 26 Aug 2022 22:15:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661577311; cv=none; d=google.com; s=arc-20160816; b=RzNaRQ/1niSCX0ZlT+rzg9hCxrv3v0Z32bYeGNuHS6KEnIZaqa3vZAMuCWrzs/FlvE MYSpk/0B768QKZdWqKySHR+mFhBuTQ3/5lIPY0xhpJX3irqad7r9B2+yFvB7tMDwn8KT 9eI4UIOp6jv5hcyY5x5vbngalhRz4db1PXoEPFB7HBWqkiScYilmFL8gs29dtr0OO49i 8YSy4wssa8ME5hsavU5w5ntJ/04T5dPY+8RMwkwf4BjlRV2y64X9InnmJGFZmp3vZyra 6iVaP+VbeDQEcV6DzQhhiPNT7D0NNKIjLX03cC1hHfn6NPebUGVIu3cGl7aBFGG4ccDQ 72XQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=QZU+GCPaAA6I1K7yLJhxXwg0VwMSRrTwi961acAJ9GY=; b=xrtqjq3bGZAy/HbdN8zEf4toJBskc6FrNLoICEyc+sCAfq1cylNhh9V91BRK11H8+n /oIqtktHaR9FGg3zDrDyGHnBFn4njyFHlsB9IhbSPsg8uN7MADiE3b8ZnVrKyXKpDk2a FeL4UDUIkHclhcFvVXn/KlM7BUgPdaymC3rGmFMnGJ819A7nD1RmXBqx0RHei0UPpDsp WFcEErw2qDyYDxSlONisRU2Rn2Bks04sXlxN1qDAi4vS/JdGOA67G5rlMyb1TkGeY+5i RMdxkqcZgag+3BZVrfJ/hgaDrct7F6H56wFM69pUcT4sGAw1XfgeWgbR+O9lomsSwac2 sutg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="gD/dEgmM"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id h8-20020a05640250c800b00447519b1b0bsi2953022edb.261.2022.08.26.22.15.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Aug 2022 22:15:11 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="gD/dEgmM"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 36007382CDE2 for ; Sat, 27 Aug 2022 05:15:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 36007382CDE2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661577310; bh=QZU+GCPaAA6I1K7yLJhxXwg0VwMSRrTwi961acAJ9GY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=gD/dEgmMWFBNDbd8kmgcc/C4+RVvsMG+jmmNUc1BA5czHToVFWqueI7/ml8Jl68Re Ke1qSxx1nbW2BPHVcPMfauQjCgP6mJ+PwUzXHytRB5CVqHFwAwIUsOo3mvrJyxP06N n2GSqcjFtX0Hh4QYeuVDp70Y+76Pq2WfkR7GBD60= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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) 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 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-Patchwork-Original-From: Keith Packard via Gcc-patches From: Keith Packard Reply-To: Keith Packard Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1742290090849190142?= X-GMAIL-MSGID: =?utf-8?q?1742290090849190142?= 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. */