Index: clang/include/clang/Driver/ToolChain.h =================================================================== --- clang/include/clang/Driver/ToolChain.h +++ clang/include/clang/Driver/ToolChain.h @@ -513,6 +513,9 @@ /// compile unit information. virtual bool UseDwarfDebugFlags() const { return false; } + /// Add an additional -fdebug-prefix-map entry. + virtual std::string GetGlobalDebugPathRemapping() const { return {}; } + // Return the DWARF version to emit, in the absence of arguments // to the contrary. virtual unsigned GetDefaultDwarfVersion() const { return 5; } Index: clang/lib/Driver/ToolChains/Clang.cpp =================================================================== --- clang/lib/Driver/ToolChains/Clang.cpp +++ clang/lib/Driver/ToolChains/Clang.cpp @@ -668,7 +668,8 @@ } /// Add a CC1 and CC1AS option to specify the debug file path prefix map. -static void addDebugPrefixMapArg(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs) { +static void addDebugPrefixMapArg(const Driver &D, const ToolChain &TC, + const ArgList &Args, ArgStringList &CmdArgs) { for (const Arg *A : Args.filtered(options::OPT_ffile_prefix_map_EQ, options::OPT_fdebug_prefix_map_EQ)) { StringRef Map = A->getValue(); @@ -679,6 +680,15 @@ CmdArgs.push_back(Args.MakeArgString("-fdebug-prefix-map=" + Map)); A->claim(); } + std::string GlobalRemapEntry = TC.GetGlobalDebugPathRemapping(); + if (GlobalRemapEntry.empty()) + return; + if (!StringRef(GlobalRemapEntry).contains('=')) + D.Diag(diag::err_drv_invalid_argument_to_option) + << GlobalRemapEntry << "environment"; + + CmdArgs.push_back( + Args.MakeArgString("-fdebug-prefix-map=" + GlobalRemapEntry)); } /// Add a CC1 and CC1AS option to specify the macro file path prefix map. @@ -5717,7 +5727,7 @@ const char *DebugCompilationDir = addDebugCompDirArg(Args, CmdArgs, D.getVFS()); - addDebugPrefixMapArg(D, Args, CmdArgs); + addDebugPrefixMapArg(D, TC, Args, CmdArgs); if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_, options::OPT_ftemplate_depth_EQ)) { @@ -7785,7 +7795,8 @@ DebugInfoKind = (WantDebug ? codegenoptions::DebugInfoConstructor : codegenoptions::NoDebugInfo); - addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs); + addDebugPrefixMapArg(getToolChain().getDriver(), getToolChain(), Args, + CmdArgs); // Set the AT_producer to the clang version when using the integrated // assembler on assembly source files. Index: clang/lib/Driver/ToolChains/Darwin.h =================================================================== --- clang/lib/Driver/ToolChains/Darwin.h +++ clang/lib/Driver/ToolChains/Darwin.h @@ -267,6 +267,7 @@ bool SupportsProfiling() const override; bool UseDwarfDebugFlags() const override; + std::string GetGlobalDebugPathRemapping() const override; llvm::ExceptionHandling GetExceptionModel(const llvm::opt::ArgList &Args) const override { Index: clang/lib/Driver/ToolChains/Darwin.cpp =================================================================== --- clang/lib/Driver/ToolChains/Darwin.cpp +++ clang/lib/Driver/ToolChains/Darwin.cpp @@ -2772,6 +2772,12 @@ return false; } +std::string MachO::GetGlobalDebugPathRemapping() const { + if (const char *S = ::getenv("RC_DEBUG_PREFIX_MAP")) + return S; + return {}; +} + llvm::ExceptionHandling Darwin::GetExceptionModel(const ArgList &Args) const { // Darwin uses SjLj exceptions on ARM. if (getTriple().getArch() != llvm::Triple::arm && Index: clang/test/Driver/darwin-debug-prefix-map.c =================================================================== --- /dev/null +++ clang/test/Driver/darwin-debug-prefix-map.c @@ -0,0 +1,6 @@ +// RUN: env RC_DEBUG_PREFIX_MAP=old=new \ +// RUN: %clang -target arm64-apple-darwin -### -c -g %s 2>&1 | FileCheck %s +// RUN: env RC_DEBUG_PREFIX_MAP=illegal \ +// RUN: %clang -target arm64-apple-darwin -### -c -g %s 2>&1 | FileCheck %s --check-prefix=ERR +// CHECK: "-fdebug-prefix-map=old=new" +// ERR: invalid argument 'illegal' Index: clang/test/Driver/darwin-debug-prefix-map.s =================================================================== --- /dev/null +++ clang/test/Driver/darwin-debug-prefix-map.s @@ -0,0 +1,6 @@ +// RUN: env RC_DEBUG_PREFIX_MAP=old=new \ +// RUN: %clang -target arm64-apple-darwin -### -c -g %s 2>&1 | FileCheck %s +// RUN: env RC_DEBUG_PREFIX_MAP=illegal \ +// RUN: %clang -target arm64-apple-darwin -### -c -g %s 2>&1 | FileCheck %s --check-prefix=ERR +// CHECK: "-fdebug-prefix-map=old=new" +// ERR: invalid argument 'illegal'