Skip to content

Commit 7cc7e2b

Browse files
authored
Merge pull request #72352 from compnerd/sysroot
Driver: introduce `-sysroot` option for non-Darwin targets
2 parents 9d63e0f + c8bec5b commit 7cc7e2b

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed

include/swift/AST/SearchPathOptions.h

+7
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ class SearchPathOptions {
378378
std::optional<std::string> VCToolsRoot = std::nullopt;
379379
std::optional<std::string> VCToolsVersion = std::nullopt;
380380

381+
std::optional<StringRef> SysRoot = std::nullopt;
382+
381383
public:
382384
StringRef getSDKPath() const { return SDKPath; }
383385

@@ -416,6 +418,11 @@ class SearchPathOptions {
416418
VCToolsVersion = version;
417419
}
418420

421+
std::optional<StringRef> getSysRoot() const { return SysRoot; }
422+
void setSysRoot(StringRef sysroot) {
423+
SysRoot = sysroot;
424+
}
425+
419426
ArrayRef<std::string> getImportSearchPaths() const {
420427
return ImportSearchPaths;
421428
}

include/swift/Option/Options.td

+7
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,13 @@ def visualc_tools_version : Separate<["-"], "visualc-tools-version">,
255255
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
256256
HelpText<"VisualC++ ToolSet Version">, MetaVarName<"<version>">;
257257

258+
// Android Options
259+
260+
def sysroot : Separate<["-"], "sysroot">,
261+
Flags<[ArgumentIsPath, FrontendOption, SwiftSymbolGraphExtractOption,
262+
SwiftAPIDigesterOption]>,
263+
HelpText<"Native Platform sysroot">, MetaVarName<"<sysroot>">;
264+
258265
// Standard Options
259266
def _DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>,
260267
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>;

lib/ClangImporter/ClangImporter.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,18 @@ void importer::getNormalInvocationArguments(
696696
} else {
697697
// On Darwin, Clang uses -isysroot to specify the include
698698
// system root. On other targets, it seems to use --sysroot.
699-
invocationArgStrs.push_back(triple.isOSDarwin() ? "-isysroot"
700-
: "--sysroot");
701-
invocationArgStrs.push_back(searchPathOpts.getSDKPath().str());
699+
if (triple.isOSDarwin()) {
700+
invocationArgStrs.push_back("-isysroot");
701+
invocationArgStrs.push_back(searchPathOpts.getSDKPath().str());
702+
} else {
703+
if (auto sysroot = searchPathOpts.getSysRoot()) {
704+
invocationArgStrs.push_back("--sysroot");
705+
invocationArgStrs.push_back(sysroot->str());
706+
} else {
707+
invocationArgStrs.push_back("--sysroot");
708+
invocationArgStrs.push_back(searchPathOpts.getSDKPath().str());
709+
}
710+
}
702711
}
703712
}
704713

lib/ClangImporter/ClangIncludePaths.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ createClangArgs(const ASTContext &ctx, clang::driver::Driver &clangDriver) {
176176
auto sdkPath = ctx.SearchPathOpts.getSDKPath();
177177
if (!sdkPath.empty())
178178
clangDriver.SysRoot = sdkPath.str();
179+
if (auto sysroot = ctx.SearchPathOpts.getSysRoot())
180+
clangDriver.SysRoot = sysroot->str();
179181
return clangDriverArgs;
180182
}
181183

lib/Driver/ToolChains.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
244244
arguments.push_back(inputArgs.MakeArgString(A->getValue()));
245245
}
246246

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

248252
if (llvm::sys::Process::StandardErrHasColors()) {
249253
arguments.push_back("-color-diagnostics");

lib/Frontend/CompilerInvocation.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,9 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
21122112
if (const Arg *A = Args.getLastArg(OPT_visualc_tools_version))
21132113
Opts.setVCToolsVersion(A->getValue());
21142114

2115+
if (const Arg *A = Args.getLastArg(OPT_sysroot))
2116+
Opts.setSysRoot(A->getValue());
2117+
21152118
if (const Arg *A = Args.getLastArg(OPT_resource_dir))
21162119
Opts.RuntimeResourcePath = A->getValue();
21172120

0 commit comments

Comments
 (0)