From c8bec5b12f36e9e46aa2c84d7f75bac2018148a2 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 15 Mar 2024 09:07:09 -0700 Subject: [PATCH] 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. --- include/swift/AST/SearchPathOptions.h | 7 +++++++ include/swift/Option/Options.td | 7 +++++++ lib/ClangImporter/ClangImporter.cpp | 15 ++++++++++++--- lib/ClangImporter/ClangIncludePaths.cpp | 2 ++ lib/Driver/ToolChains.cpp | 4 ++++ lib/Frontend/CompilerInvocation.cpp | 3 +++ 6 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/swift/AST/SearchPathOptions.h b/include/swift/AST/SearchPathOptions.h index 5d21ca6f29217..62ae837cecb7c 100644 --- a/include/swift/AST/SearchPathOptions.h +++ b/include/swift/AST/SearchPathOptions.h @@ -378,6 +378,8 @@ class SearchPathOptions { std::optional VCToolsRoot = std::nullopt; std::optional VCToolsVersion = std::nullopt; + std::optional SysRoot = std::nullopt; + public: StringRef getSDKPath() const { return SDKPath; } @@ -416,6 +418,11 @@ class SearchPathOptions { VCToolsVersion = version; } + std::optional getSysRoot() const { return SysRoot; } + void setSysRoot(StringRef sysroot) { + SysRoot = sysroot; + } + ArrayRef getImportSearchPaths() const { return ImportSearchPaths; } diff --git a/include/swift/Option/Options.td b/include/swift/Option/Options.td index 48566a6bb9c23..f4cb701b8bbcb 100644 --- a/include/swift/Option/Options.td +++ b/include/swift/Option/Options.td @@ -255,6 +255,13 @@ def visualc_tools_version : Separate<["-"], "visualc-tools-version">, SwiftSymbolGraphExtractOption, SwiftAPIDigesterOption]>, HelpText<"VisualC++ ToolSet Version">, MetaVarName<"">; +// Android Options + +def sysroot : Separate<["-"], "sysroot">, + Flags<[ArgumentIsPath, FrontendOption, SwiftSymbolGraphExtractOption, + SwiftAPIDigesterOption]>, + HelpText<"Native Platform sysroot">, MetaVarName<"">; + // Standard Options def _DASH_DASH : Option<["--"], "", KIND_REMAINING_ARGS>, Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>; diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index d9ab9214ab475..7d848a208267a 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -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()); + } + } } } diff --git a/lib/ClangImporter/ClangIncludePaths.cpp b/lib/ClangImporter/ClangIncludePaths.cpp index 20933c2331e92..172cc5d952fda 100644 --- a/lib/ClangImporter/ClangIncludePaths.cpp +++ b/lib/ClangImporter/ClangIncludePaths.cpp @@ -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; } diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index 65f6a31193693..179ae3d466422 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -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"); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index 3403c9db39c4d..63ea0931f91e0 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -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();