[Driver] Fix -working-directory issues
Currently the `-working-directory` option does not actually impact the working directory for all of the clang driver, it only impacts how files are looked up to make sure they exist. This means that that clang passes the wrong paths to -fdebug-compilation-dir and -coverage-notes-file. This patch fixes that by changing all the places in the driver where we convert to absolute paths to use the VFS, and then calling setCurrentWorkingDirectory on the VFS. This also changes the default VFS for `Driver` to use a virtualized working directory, instead of changing the process's working directory. Differential Revision: https://reviews.llvm.org/D62271 llvm-svn: 361885
This commit is contained in:
parent
5514658591
commit
7e48b406ef
|
@ -91,6 +91,8 @@ def err_no_external_assembler : Error<
|
||||||
"there is no external assembler that can be used on this platform">;
|
"there is no external assembler that can be used on this platform">;
|
||||||
def err_drv_unable_to_remove_file : Error<
|
def err_drv_unable_to_remove_file : Error<
|
||||||
"unable to remove file: %0">;
|
"unable to remove file: %0">;
|
||||||
|
def err_drv_unable_to_set_working_directory : Error <
|
||||||
|
"unable to set working directory: %0">;
|
||||||
def err_drv_command_failure : Error<
|
def err_drv_command_failure : Error<
|
||||||
"unable to execute command: %0">;
|
"unable to execute command: %0">;
|
||||||
def err_drv_invalid_darwin_version : Error<
|
def err_drv_invalid_darwin_version : Error<
|
||||||
|
|
|
@ -133,7 +133,7 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
|
||||||
|
|
||||||
// Provide a sane fallback if no VFS is specified.
|
// Provide a sane fallback if no VFS is specified.
|
||||||
if (!this->VFS)
|
if (!this->VFS)
|
||||||
this->VFS = llvm::vfs::getRealFileSystem();
|
this->VFS = llvm::vfs::createPhysicalFileSystem().release();
|
||||||
|
|
||||||
Name = llvm::sys::path::filename(ClangExecutable);
|
Name = llvm::sys::path::filename(ClangExecutable);
|
||||||
Dir = llvm::sys::path::parent_path(ClangExecutable);
|
Dir = llvm::sys::path::parent_path(ClangExecutable);
|
||||||
|
@ -1005,6 +1005,11 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check for working directory option before accessing any files
|
||||||
|
if (Arg *WD = Args.getLastArg(options::OPT_working_directory))
|
||||||
|
if (std::error_code EC = VFS->setCurrentWorkingDirectory(WD->getValue()))
|
||||||
|
Diag(diag::err_drv_unable_to_set_working_directory) << WD->getValue();
|
||||||
|
|
||||||
// FIXME: This stuff needs to go into the Compilation, not the driver.
|
// FIXME: This stuff needs to go into the Compilation, not the driver.
|
||||||
bool CCCPrintPhases;
|
bool CCCPrintPhases;
|
||||||
|
|
||||||
|
@ -1984,20 +1989,11 @@ bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
|
||||||
if (Value == "-")
|
if (Value == "-")
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
SmallString<64> Path(Value);
|
if (getVFS().exists(Value))
|
||||||
if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) {
|
|
||||||
if (!llvm::sys::path::is_absolute(Path)) {
|
|
||||||
SmallString<64> Directory(WorkDir->getValue());
|
|
||||||
llvm::sys::path::append(Directory, Value);
|
|
||||||
Path.assign(Directory);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (getVFS().exists(Path))
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (IsCLMode()) {
|
if (IsCLMode()) {
|
||||||
if (!llvm::sys::path::is_absolute(Twine(Path)) &&
|
if (!llvm::sys::path::is_absolute(Twine(Value)) &&
|
||||||
llvm::sys::Process::FindInEnvPath("LIB", Value))
|
llvm::sys::Process::FindInEnvPath("LIB", Value))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
@ -2023,12 +2019,12 @@ bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value,
|
||||||
if (getOpts().findNearest(Value, Nearest, IncludedFlagsBitmask,
|
if (getOpts().findNearest(Value, Nearest, IncludedFlagsBitmask,
|
||||||
ExcludedFlagsBitmask) <= 1) {
|
ExcludedFlagsBitmask) <= 1) {
|
||||||
Diag(clang::diag::err_drv_no_such_file_with_suggestion)
|
Diag(clang::diag::err_drv_no_such_file_with_suggestion)
|
||||||
<< Path << Nearest;
|
<< Value << Nearest;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Diag(clang::diag::err_drv_no_such_file) << Path;
|
Diag(clang::diag::err_drv_no_such_file) << Value;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -616,11 +616,11 @@ static bool shouldUseLeafFramePointer(const ArgList &Args,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a CC1 option to specify the debug compilation directory.
|
/// Add a CC1 option to specify the debug compilation directory.
|
||||||
static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) {
|
static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs,
|
||||||
SmallString<128> cwd;
|
const llvm::vfs::FileSystem &VFS) {
|
||||||
if (!llvm::sys::fs::current_path(cwd)) {
|
if (llvm::ErrorOr<std::string> CWD = VFS.getCurrentWorkingDirectory()) {
|
||||||
CmdArgs.push_back("-fdebug-compilation-dir");
|
CmdArgs.push_back("-fdebug-compilation-dir");
|
||||||
CmdArgs.push_back(Args.MakeArgString(cwd));
|
CmdArgs.push_back(Args.MakeArgString(*CWD));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -885,13 +885,8 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
|
||||||
else
|
else
|
||||||
OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
|
OutputFilename = llvm::sys::path::filename(Output.getBaseInput());
|
||||||
SmallString<128> CoverageFilename = OutputFilename;
|
SmallString<128> CoverageFilename = OutputFilename;
|
||||||
if (llvm::sys::path::is_relative(CoverageFilename)) {
|
if (llvm::sys::path::is_relative(CoverageFilename))
|
||||||
SmallString<128> Pwd;
|
(void)D.getVFS().makeAbsolute(CoverageFilename);
|
||||||
if (!llvm::sys::fs::current_path(Pwd)) {
|
|
||||||
llvm::sys::path::append(Pwd, CoverageFilename);
|
|
||||||
CoverageFilename.swap(Pwd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
llvm::sys::path::replace_extension(CoverageFilename, "gcno");
|
llvm::sys::path::replace_extension(CoverageFilename, "gcno");
|
||||||
CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
|
CmdArgs.push_back(Args.MakeArgString(CoverageFilename));
|
||||||
|
|
||||||
|
@ -4354,7 +4349,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
|
||||||
CmdArgs.push_back("-fno-autolink");
|
CmdArgs.push_back("-fno-autolink");
|
||||||
|
|
||||||
// Add in -fdebug-compilation-dir if necessary.
|
// Add in -fdebug-compilation-dir if necessary.
|
||||||
addDebugCompDirArg(Args, CmdArgs);
|
addDebugCompDirArg(Args, CmdArgs, D.getVFS());
|
||||||
|
|
||||||
addDebugPrefixMapArg(D, Args, CmdArgs);
|
addDebugPrefixMapArg(D, Args, CmdArgs);
|
||||||
|
|
||||||
|
@ -6065,7 +6060,7 @@ void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
|
||||||
DebugInfoKind = (WantDebug ? codegenoptions::LimitedDebugInfo
|
DebugInfoKind = (WantDebug ? codegenoptions::LimitedDebugInfo
|
||||||
: codegenoptions::NoDebugInfo);
|
: codegenoptions::NoDebugInfo);
|
||||||
// Add the -fdebug-compilation-dir flag if needed.
|
// Add the -fdebug-compilation-dir flag if needed.
|
||||||
addDebugCompDirArg(Args, CmdArgs);
|
addDebugCompDirArg(Args, CmdArgs, C.getDriver().getVFS());
|
||||||
|
|
||||||
addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
|
addDebugPrefixMapArg(getToolChain().getDriver(), Args, CmdArgs);
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,11 @@
|
||||||
// RUN: %clang -### -working-directory /no/such/dir/ input 2>&1 | FileCheck %s
|
// RUN: %clang -### -working-directory /no/such/dir/ input 2>&1 | FileCheck %s
|
||||||
|
// RUN: %clang -### -working-directory %p/Inputs no_such_file.cpp -c 2>&1 | FileCheck %s --check-prefix=CHECK_NO_FILE
|
||||||
|
// RUN: %clang -### -working-directory %p/Inputs pchfile.cpp -c 2>&1 | FileCheck %s --check-prefix=CHECK_WORKS
|
||||||
|
|
||||||
//CHECK: no such file or directory: '/no/such/dir/input'
|
// CHECK: unable to set working directory: /no/such/dir/
|
||||||
|
|
||||||
|
// CHECK_NO_FILE: no such file or directory: 'no_such_file.cpp'
|
||||||
|
|
||||||
|
// CHECK_WORKS: "-coverage-notes-file" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs{{/|\\\\}}pchfile.gcno"
|
||||||
|
// CHECK_WORKS: "-working-directory" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"
|
||||||
|
// CHECK_WORKS: "-fdebug-compilation-dir" "{{[^"]+}}test{{/|\\\\}}Driver{{/|\\\\}}Inputs"
|
||||||
|
|
Loading…
Reference in New Issue