Skip to content

Commit b5430a9

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 fb0a1b9 commit b5430a9

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
@@ -264,6 +264,13 @@ def visualc_tools_version : Separate<["-"], "visualc-tools-version">,
264264
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
265265
HelpText<"VisualC++ ToolSet Version">, MetaVarName<"<version>">;
266266

267+
// Android Options
268+
269+
def sysroot : Separate<["-"], "sysroot">,
270+
Flags<[ArgumentIsPath, FrontendOption, SwiftAPIExtractOption,
271+
SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>,
272+
HelpText<"Native Platform sysroot">, MetaVarName<"<sysroot>">;
273+
267274
// Standard Options
268275
def _DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>,
269276
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>;

lib/ClangImporter/ClangImporter.cpp

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

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
@@ -2047,6 +2047,9 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
20472047
if (const Arg *A = Args.getLastArg(OPT_visualc_tools_version))
20482048
Opts.setVCToolsVersion(A->getValue());
20492049

2050+
if (const Arg *A = Args.getLastArg(OPT_sysroot))
2051+
Opts.setSysRoot(A->getValue());
2052+
20502053
if (const Arg *A = Args.getLastArg(OPT_resource_dir))
20512054
Opts.RuntimeResourcePath = A->getValue();
20522055

0 commit comments

Comments
 (0)