Skip to content

Driver: introduce -sysroot option for non-Darwin targets #72352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/swift/AST/SearchPathOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ class SearchPathOptions {
std::optional<std::string> VCToolsRoot = std::nullopt;
std::optional<std::string> VCToolsVersion = std::nullopt;

std::optional<StringRef> SysRoot = std::nullopt;

public:
StringRef getSDKPath() const { return SDKPath; }

Expand Down Expand Up @@ -416,6 +418,11 @@ class SearchPathOptions {
VCToolsVersion = version;
}

std::optional<StringRef> getSysRoot() const { return SysRoot; }
void setSysRoot(StringRef sysroot) {
SysRoot = sysroot;
}

ArrayRef<std::string> getImportSearchPaths() const {
return ImportSearchPaths;
}
Expand Down
7 changes: 7 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ def visualc_tools_version : Separate<["-"], "visualc-tools-version">,
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
HelpText<"VisualC++ ToolSet Version">, MetaVarName<"<version>">;

// Android Options

def sysroot : Separate<["-"], "sysroot">,
Flags<[ArgumentIsPath, FrontendOption, SwiftSymbolGraphExtractOption,
SwiftAPIDigesterOption]>,
HelpText<"Native Platform sysroot">, MetaVarName<"<sysroot>">;

// Standard Options
def _DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>,
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>;
Expand Down
15 changes: 12 additions & 3 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,9 +696,18 @@ void importer::getNormalInvocationArguments(
} else {
// On Darwin, Clang uses -isysroot to specify the include
// system root. On other targets, it seems to use --sysroot.
invocationArgStrs.push_back(triple.isOSDarwin() ? "-isysroot"
: "--sysroot");
invocationArgStrs.push_back(searchPathOpts.getSDKPath().str());
if (triple.isOSDarwin()) {
invocationArgStrs.push_back("-isysroot");
invocationArgStrs.push_back(searchPathOpts.getSDKPath().str());
} else {
if (auto sysroot = searchPathOpts.getSysRoot()) {
invocationArgStrs.push_back("--sysroot");
invocationArgStrs.push_back(sysroot->str());
} else {
invocationArgStrs.push_back("--sysroot");
invocationArgStrs.push_back(searchPathOpts.getSDKPath().str());
}
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/ClangImporter/ClangIncludePaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ createClangArgs(const ASTContext &ctx, clang::driver::Driver &clangDriver) {
auto sdkPath = ctx.SearchPathOpts.getSDKPath();
if (!sdkPath.empty())
clangDriver.SysRoot = sdkPath.str();
if (auto sysroot = ctx.SearchPathOpts.getSysRoot())
clangDriver.SysRoot = sysroot->str();
return clangDriverArgs;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
arguments.push_back(inputArgs.MakeArgString(A->getValue()));
}

if (const Arg *A = inputArgs.getLastArg(options::OPT_sysroot)) {
arguments.push_back("-sysroot");
arguments.push_back(inputArgs.MakeArgString(A->getValue()));
}

if (llvm::sys::Process::StandardErrHasColors()) {
arguments.push_back("-color-diagnostics");
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2113,6 +2113,9 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
if (const Arg *A = Args.getLastArg(OPT_visualc_tools_version))
Opts.setVCToolsVersion(A->getValue());

if (const Arg *A = Args.getLastArg(OPT_sysroot))
Opts.setSysRoot(A->getValue());

if (const Arg *A = Args.getLastArg(OPT_resource_dir))
Opts.RuntimeResourcePath = A->getValue();

Expand Down