Add back --sysroot support for darwin header search.

Before e97b5f5cf3 ([clang][Darwin] Refactor header search path logic
into the driver), both --sysroot and -isysroot worked to specify where
to look for system and C++ headers on Darwin. However, that change
caused clang to start ignoring --sysroot.

This fixes the regression, and adds tests.

(I also note that on all other platforms, clang seems to almost
completely ignore -isysroot, but that's another issue...)

Differential Revision: https://reviews.llvm.org/D62268

llvm-svn: 361429
This commit is contained in:
James Y Knight 2019-05-22 20:39:51 +00:00
parent 275a55cb5a
commit b2ece169ed
4 changed files with 59 additions and 10 deletions

View File

@ -1805,13 +1805,21 @@ void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
}
}
// Returns the effective header sysroot path to use. This comes either from
// -isysroot or --sysroot.
llvm::StringRef DarwinClang::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
if(DriverArgs.hasArg(options::OPT_isysroot))
return DriverArgs.getLastArgValue(options::OPT_isysroot);
if (!getDriver().SysRoot.empty())
return getDriver().SysRoot;
return "/";
}
void DarwinClang::AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
llvm::opt::ArgStringList &CC1Args) const {
const Driver &D = getDriver();
llvm::StringRef Sysroot = "/";
if (const Arg *A = DriverArgs.getLastArg(options::OPT_isysroot))
Sysroot = A->getValue();
llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
bool NoStdInc = DriverArgs.hasArg(options::OPT_nostdinc);
bool NoStdlibInc = DriverArgs.hasArg(options::OPT_nostdlibinc);
@ -1897,12 +1905,7 @@ void DarwinClang::AddClangCXXStdlibIncludeArgs(
DriverArgs.hasArg(options::OPT_nostdincxx))
return;
llvm::SmallString<128> Sysroot;
if (const Arg *A = DriverArgs.getLastArg(options::OPT_isysroot)) {
Sysroot = A->getValue();
} else {
Sysroot = "/";
}
llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
switch (GetCXXStdlibType(DriverArgs)) {
case ToolChain::CST_Libcxx: {

View File

@ -539,6 +539,8 @@ private:
llvm::StringRef Version,
llvm::StringRef ArchDir,
llvm::StringRef BitDir) const;
llvm::StringRef GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const;
};
} // end namespace toolchains

View File

@ -36,6 +36,7 @@
// Check with both headers in the sysroot and headers alongside the installation
// (the headers in <sysroot> should be added after the toolchain headers).
// Ensure that both -isysroot and --sysroot work, and that isysroot has precedence.
//
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
// RUN: -target x86_64-apple-darwin \
@ -46,6 +47,28 @@
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
//
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
// RUN: -target x86_64-apple-darwin \
// RUN: -stdlib=libc++ \
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
// RUN: -resource-dir=%S/Inputs/resource_dir \
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_usr \
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
//
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
// RUN: -target x86_64-apple-darwin \
// RUN: -stdlib=libc++ \
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain/usr/bin \
// RUN: -resource-dir=%S/Inputs/resource_dir \
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr \
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_no_libcxx \
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr \
// RUN: -DTOOLCHAIN=%S/Inputs/basic_darwin_toolchain \
// RUN: --check-prefix=CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1 %s
//
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[TOOLCHAIN]]/usr/bin/../include/c++/v1"
// CHECK-LIBCXX-SYSROOT_AND_TOOLCHAIN-1: "-internal-isystem" "[[SYSROOT]]/usr/include/c++/v1"

View File

@ -3,7 +3,8 @@
// General tests that the system header search paths detected by the driver
// and passed to CC1 are correct on Darwin platforms.
// Check system headers (everything below <sysroot> and <resource-dir>)
// Check system headers (everything below <sysroot> and <resource-dir>). Ensure
// that both sysroot and isysroot are checked, and that isysroot has precedence.
//
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
// RUN: -target x86_64-apple-darwin \
@ -13,6 +14,26 @@
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_and_usr_local \
// RUN: -DRESOURCE=%S/Inputs/resource_dir \
// RUN: --check-prefix=CHECK-SYSTEM %s
//
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
// RUN: -target x86_64-apple-darwin \
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
// RUN: -resource-dir=%S/Inputs/resource_dir \
// RUN: --sysroot %S/Inputs/basic_darwin_sdk_usr_and_usr_local \
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_and_usr_local \
// RUN: -DRESOURCE=%S/Inputs/resource_dir \
// RUN: --check-prefix=CHECK-SYSTEM %s
//
// RUN: %clang -no-canonical-prefixes %s -### -fsyntax-only 2>&1 \
// RUN: -target x86_64-apple-darwin \
// RUN: -ccc-install-dir %S/Inputs/basic_darwin_toolchain_no_libcxx/usr/bin \
// RUN: -resource-dir=%S/Inputs/resource_dir \
// RUN: -isysroot %S/Inputs/basic_darwin_sdk_usr_and_usr_local \
// RUN: --sysroot / \
// RUN: | FileCheck -DSYSROOT=%S/Inputs/basic_darwin_sdk_usr_and_usr_local \
// RUN: -DRESOURCE=%S/Inputs/resource_dir \
// RUN: --check-prefix=CHECK-SYSTEM %s
//
// CHECK-SYSTEM: "{{[^"]*}}clang{{[^"]*}}" "-cc1"
// CHECK-SYSTEM: "-internal-isystem" "[[SYSROOT]]/usr/local/include"
// CHECK-SYSTEM: "-internal-isystem" "[[RESOURCE]]/include"