This fixes a permission error that occurs when cross-compiling with
-save-temps and a relocated toolchain, where the original build path
exists but is inaccessible.

The issue occurs when:
- Building the toolchain at /home/scratch/build/
- Installing it to another location like /home/user/rv64-toolchain/
- The /home/scratch directory exists but has insufficient permissions
  (e.g. drwx------ for /home/scratch/)

Without this fix, cc1 would report errors like:
cc1: error: /home/scratch/build/install/riscv64-unknown-elf/usr/local/include: 
Permission denied

This occurred because the GCC driver did not pass GCC_EXEC_PREFIX and
isysroot to cc1/cc1plus in the compile stage when using -save-temps.
This caused cc1/cc1plus to search for headers from the wrong (original
build) path instead of the relocated installation path.

The fix ensures cc1/cc1plus won't try to collect include paths when
-fpreprocessed is specified.  This prevents the permission error during
cross-compilation with -save-temps, as the preprocessed file already
contains all necessary headers.

gcc/c-family/ChangeLog:

        * c-opts.cc (c_common_post_options): Skip register_include_chains
        when cpp_opts->preprocessed is set.
---
 gcc/c-family/c-opts.cc | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/gcc/c-family/c-opts.cc b/gcc/c-family/c-opts.cc
index cb836b26a5c..1e0f0c59ade 100644
--- a/gcc/c-family/c-opts.cc
+++ b/gcc/c-family/c-opts.cc
@@ -864,8 +864,12 @@ c_common_post_options (const char **pfilename)
 
   sanitize_cpp_opts ();
 
-  register_include_chains (parse_in, sysroot, iprefix, imultilib,
-                          std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
+  /* Don't register include chains if under -fpreprocessed since we might not
+     have correct sysroot this mode, and this may cause file permssion
+     issue.  */
+  if (!cpp_opts->preprocessed)
+    register_include_chains (parse_in, sysroot, iprefix, imultilib,
+                            std_inc, std_cxx_inc && c_dialect_cxx (), verbose);
 
 #ifdef C_COMMON_OVERRIDE_OPTIONS
   /* Some machines may reject certain combinations of C
-- 
2.34.1

Reply via email to