]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gcc/file-prefix-map: Allow remapping of relative paths
authorRichard Purdie <richard.purdie@linuxfoundation.org>
Tue, 1 Nov 2022 19:45:08 +0000 (13:45 -0600)
committerJeff Law <jlaw@ventanamicro.com>
Tue, 1 Nov 2022 19:45:51 +0000 (13:45 -0600)
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.

gcc/file-prefix-map.cc

index 24733f831d66531174c5d1b3ab03047d5c0eebbc..439586bd2b5527a6ab368b7d3d50d3cb3e5483ee 100644 (file)
@@ -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;
 }