Skip to content

Commit c8bec5b

Browse files
committed
Driver: introduce -sysroot option for non-Darwin targets
This introduces a secondary flag `-sysroot` for the non-Darwin targets, primarily Unicies. The intention here is to support a split `-sdk`, `-sysroot` model where the `-sdk` parameter provides the Swift "SDK" which augments the native platform's C sysroot which is indicated as `-sysroot`. For the case of Android, this would allow us to provide a path to the NDK sysroot and the Swift SDK allowing us to cross-compile Android binaries from Windows.
1 parent 55d4a56 commit c8bec5b

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed

include/swift/AST/SearchPathOptions.h

Lines changed: 7 additions & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 12 additions & 3 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,9 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
21132113
if (const Arg *A = Args.getLastArg(OPT_visualc_tools_version))
21142114
Opts.setVCToolsVersion(A->getValue());
21152115

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

0 commit comments

Comments
 (0)