From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2119) id 5B8093858D37; Tue, 1 Nov 2022 19:46:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B8093858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667331967; bh=NiZvt5IAnouyW6rM7+gUuakyKNOmKjM+9bk7U8FaJ8E=; h=From:To:Subject:Date:From; b=PG02b+DK9hBT1IN8UBdZr4Y5s/Pl2F+nTJofgo0oQ2xJVAVy627tJg/3h4G0ni4PL Ad+1WhUsuxg2bOgfcgfuVwgl1mWYWKgc2kI7kBFK5pWVlLDpS4vdV/EGIRYc10uIUw JWO1s+Oz3T5E8yRkazEwprRrKh1pQrux8h9UNqes= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jeff Law To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3599] gcc/file-prefix-map: Allow remapping of relative paths X-Act-Checkin: gcc X-Git-Author: Richard Purdie X-Git-Refname: refs/heads/master X-Git-Oldrev: bdf0018519c39931fdcc7aeffe9e87ba756894d7 X-Git-Newrev: e5c15eb183f17e806ad6b58c9497321ded87866f Message-Id: <20221101194607.5B8093858D37@sourceware.org> Date: Tue, 1 Nov 2022 19:46:07 +0000 (GMT) List-Id: https://gcc.gnu.org/g:e5c15eb183f17e806ad6b58c9497321ded87866f commit r13-3599-ge5c15eb183f17e806ad6b58c9497321ded87866f Author: Richard Purdie Date: Tue Nov 1 13:45:08 2022 -0600 gcc/file-prefix-map: Allow remapping of relative paths 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. Diff: --- gcc/file-prefix-map.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/gcc/file-prefix-map.cc b/gcc/file-prefix-map.cc index 24733f831d6..439586bd2b5 100644 --- a/gcc/file-prefix-map.cc +++ b/gcc/file-prefix-map.cc @@ -70,19 +70,29 @@ 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) - return filename; - name = filename + map->old_len; + { + free (realname); + return filename; + } + 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; }