From patchwork Wed Aug 17 12:15:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Purdie X-Patchwork-Id: 56812 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A01943858437 for ; Wed, 17 Aug 2022 12:16:08 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A01943858437 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660738568; bh=S/+y2DXLgZ5EOZ4rOe6Ruqr3JnpS+IgHlFEZHIecLCg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=IW2Il1Ri8BTLB9TncVxokLil6FHHiKe5lvLmBnYquX31RebzBAmRCcBOkMmfwpaI8 1jb30rbNFrKFsn8VWVokNHXtuACJ35WPrD+f2OlZTraKufT2HE1bBjplwnlT+FomLa DioTdc0LHPMuhFDkH6WtsHwgiLlnbUv94dylUM4s= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id 6D9BD3858D37 for ; Wed, 17 Aug 2022 12:15:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6D9BD3858D37 Received: by mail-wm1-x32b.google.com with SMTP id v7-20020a1cac07000000b003a6062a4f81so909211wme.1 for ; Wed, 17 Aug 2022 05:15:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc; bh=S/+y2DXLgZ5EOZ4rOe6Ruqr3JnpS+IgHlFEZHIecLCg=; b=FCODqw35iOk4AHiLRBZMbrKnJQnCh+avIKaO9u9tlmLRR54VjldAj6sNy1hPOfYcCE eMgDIhGQ4K8a1lCNoRmoMfLfjbLHBmKmGbeP2xS0HtmBBsz9U1iZ97i+pZe4UOx2Ge9H TR0/v6Wy34ZSD5fvFWxNa++qp55aTIT7AHQ07kUE0BQS7Pm2ms0iAshnH0osm+z3RTJu H0TLpbAUR0n32ZAwg+P365zYtkLOE0O03lGbXz/0nPvixoUbJPK3voFFtL/AQ0IYATUJ 6r2LAA56ma83fnWhik05J6Vx0xQxW30vXWJu0ZoBC28hVytdzA3DocN8/Vo7QbInNqWF UZXQ== X-Gm-Message-State: ACgBeo0HJTvRhaTiEuvAgJij4DizW20oiW38mpLkDSJJ3AfeH36wMPhZ SLhfODqXwN6MOk5LfeDGZwqObeayZgXwYw== X-Google-Smtp-Source: AA6agR6NxJhFt24+2RGDhT0C5Unb6hb1CZ16rL4w0WU7jDtOO8fsm4FQavsxxDELeZIoTflQ4EPZnA== X-Received: by 2002:a05:600c:4e15:b0:3a6:152a:9143 with SMTP id b21-20020a05600c4e1500b003a6152a9143mr1688966wmq.20.1660738536381; Wed, 17 Aug 2022 05:15:36 -0700 (PDT) Received: from max.int.rpsys.net ([2001:8b0:aba:5f3c:3694:2ef4:2932:37d5]) by smtp.gmail.com with ESMTPSA id t126-20020a1c4684000000b003a5fe5ed7edsm2408220wma.0.2022.08.17.05.15.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Aug 2022 05:15:35 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 1/2] gcc/file-prefix-map: Allow remapping of relative paths Date: Wed, 17 Aug 2022 13:15:33 +0100 Message-Id: <20220817121534.1825108-1-richard.purdie@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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-Patchwork-Original-From: Richard Purdie via Gcc-patches From: Richard Purdie Reply-To: Richard Purdie Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Relative paths currently aren't remapped by -ffile-prefix-map and friends. When cross compiling with separate 'source' and 'build' directories, the same relative paths between directories may not be available on target as compared to build time. In order to be able to remap these relative build paths to paths that would work on target, resolve paths within the file-prefix-map function using realpath(). This does cause a change of behaviour if users were previously relying upon symlinks or absolute paths not being resolved. Use basename to ensure plain filenames don't have paths added. gcc/ChangeLog: * file-prefix-map.cc (remap_filename): Allow remapping of relative paths Signed-off-by: Richard Purdie --- gcc/file-prefix-map.cc | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/gcc/file-prefix-map.cc b/gcc/file-prefix-map.cc index 24733f831d6..50d5d724a8f 100644 --- a/gcc/file-prefix-map.cc +++ b/gcc/file-prefix-map.cc @@ -70,19 +70,28 @@ remap_filename (file_prefix_map *maps, const char *filename) file_prefix_map *map; char *s; const char *name; + char *realname; size_t name_len; + if (lbasename (filename) == filename) + return filename; + + realname = lrealpath (filename); + for (map = maps; map; map = map->next) - if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0) + if (filename_ncmp (realname, map->old_prefix, map->old_len) == 0) break; - if (!map) + if (!map) { + free (realname); return filename; - name = filename + map->old_len; + } + name = realname + map->old_len; name_len = strlen (name) + 1; s = (char *) ggc_alloc_atomic (name_len + map->new_len); memcpy (s, map->new_prefix, map->new_len); memcpy (s + map->new_len, name, name_len); + free (realname); return s; } From patchwork Wed Aug 17 12:15:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Purdie X-Patchwork-Id: 56813 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0816238582AF for ; Wed, 17 Aug 2022 12:16:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0816238582AF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1660738569; bh=hhoV687bg+ANc7Vocg6D4RdtpWqDnGnI7atRnNChFJk=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=PEaMU+k69MrDdrYrIPZeRwWR6x2SSp6k77Pmz0VxDTJ1Y1kcpTBrOzD9cbbladPJV yVjVqhz0R/mKnutuc5N3hJNIXLnkTECLdviX3rLYmPBJAReZEIOsU6IJ1/zyO0liZy 4+yry8cGbAfDSrGBLGATiPbBlvwz1wgN/a3cLP/A= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by sourceware.org (Postfix) with ESMTPS id ACE593858D39 for ; Wed, 17 Aug 2022 12:15:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ACE593858D39 Received: by mail-wr1-x42d.google.com with SMTP id v3so16032932wrp.0 for ; Wed, 17 Aug 2022 05:15:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc; bh=hhoV687bg+ANc7Vocg6D4RdtpWqDnGnI7atRnNChFJk=; b=wP2v/QSDpnwOIgmA9AKGoWMHFeJ6x/Xn2SX8omgjdLbv4U+QLJcXb05StBwn3Prkd+ ysS1LnxDYOCLywkQq8PJDOiJ+/ZmEhFhQeWSxYi5s9O4p2ThJqDA267kDpoMXd5ugV9o L3PbjxuRk5B/MLErdQMsS3EZJCt55SwoLUoiNoZeYBEUQ4SwxKbaVQ5Hmid1FrP7tw0H Py6bDauo9n8RxTCj2d5BTbt59g0Czrsgn+tanbxHwOwjU0MRmmrGvoRdQjqo24ht3uUi w3aRFbNHu5UH66SBeW7PixdmStnH0SeJ+wqpYRNj2d0ECdl4xr3QYr8KkGvrqwXXiWrB jE8A== X-Gm-Message-State: ACgBeo1mz2OixoTceqqIik93MBRYR+Rufbmfx7CiB9chRo6zXTofefHB Ek4qMILfkQIQrSx6UbsK48pJdEwr219LEg== X-Google-Smtp-Source: AA6agR7nZAeqQ89aXutJFfCmp7c+EjHMjrLYrdrnAd58g2DC6ljo9/1OeQV8uwC71OBekcjopzlLig== X-Received: by 2002:adf:fa85:0:b0:225:1db8:fe6c with SMTP id h5-20020adffa85000000b002251db8fe6cmr3573480wrr.298.1660738537142; Wed, 17 Aug 2022 05:15:37 -0700 (PDT) Received: from max.int.rpsys.net ([2001:8b0:aba:5f3c:3694:2ef4:2932:37d5]) by smtp.gmail.com with ESMTPSA id t126-20020a1c4684000000b003a5fe5ed7edsm2408220wma.0.2022.08.17.05.15.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Aug 2022 05:15:36 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH 2/2] libcpp: Avoid remapping filenames within directives Date: Wed, 17 Aug 2022 13:15:34 +0100 Message-Id: <20220817121534.1825108-2-richard.purdie@linuxfoundation.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220817121534.1825108-1-richard.purdie@linuxfoundation.org> References: <20220817121534.1825108-1-richard.purdie@linuxfoundation.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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-Patchwork-Original-From: Richard Purdie via Gcc-patches From: Richard Purdie Reply-To: Richard Purdie Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Code such as: can interact poorly with file-prefix-map options when cross compiling. In general you're after to remap filenames for use in target context but the local paths should be used to find include files at compile time. Ingoring filename remapping for directives is one way to avoid such failures. libcpp/ChangeLog: * macro.cc (_cpp_builtin_macro_text): Don't remap filenames within directives Signed-off-by: Richard Purdie --- libcpp/macro.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) I wasn't sure if this should apply to all directives or whether this may need filtering to includes (along the lines of pfile->directive.flags & INCL). That gets a little more complex as the flag isn't available outside of directives.cc and would need a helper function. diff --git a/libcpp/macro.cc b/libcpp/macro.cc index 8ebf360c03c..7d5a0d0fd2e 100644 --- a/libcpp/macro.cc +++ b/libcpp/macro.cc @@ -563,7 +563,7 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node, if (!name) abort (); } - if (pfile->cb.remap_filename) + if (pfile->cb.remap_filename && !pfile->state.in_directive) name = pfile->cb.remap_filename (name); len = strlen (name); buf = _cpp_unaligned_alloc (pfile, len * 2 + 3);