Skip to content

Commit 7e83253

Browse files
committed
review fixes
1 parent 112ae4a commit 7e83253

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

-20
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,6 @@ extension Driver {
182182
try commandLine.appendAll(.F, .Fsystem, from: &parsedOptions)
183183
try commandLine.appendAll(.vfsoverlay, from: &parsedOptions)
184184

185-
if targetTriple.environment == .android {
186-
if let ndk = env["ANDROID_NDK_ROOT"] {
187-
var sysroot: AbsolutePath =
188-
try AbsolutePath(validating: ndk)
189-
.appending(components: "toolchains", "llvm", "prebuilt")
190-
#if arch(x86_64)
191-
#if os(Windows)
192-
.appending(component: "windows-x86_64")
193-
#elseif os(Linux)
194-
.appending(component: "linux-x86_64")
195-
#else
196-
.appending(component: "darwin-x86_64")
197-
#endif
198-
#endif
199-
.appending(component: "sysroot")
200-
commandLine.appendFlag("-sysroot")
201-
commandLine.appendFlag(sysroot.pathString)
202-
}
203-
}
204-
205185
if let gccToolchain = parsedOptions.getLastArgument(.gccToolchain) {
206186
appendXccFlag("--gcc-toolchain=\(gccToolchain.asSingle)")
207187
}

Sources/SwiftDriver/Jobs/GenericUnixToolchain+LinkerSupport.swift

+5-19
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extension GenericUnixToolchain {
147147
}
148148
}
149149

150-
if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
150+
if targetInfo.sdkPath != nil {
151151
for libpath in targetInfo.runtimeLibraryImportPaths {
152152
commandLine.appendFlag(.L)
153153
commandLine.appendPath(VirtualPath.lookup(libpath.path))
@@ -156,10 +156,9 @@ extension GenericUnixToolchain {
156156

157157
if !isEmbeddedEnabled && !parsedOptions.hasArgument(.nostartfiles) {
158158
let rsrc: VirtualPath
159-
if let sdk = parsedOptions.getLastArgument(.sdk)?.asSingle ?? env["SDKROOT"], !sdk.isEmpty {
160-
rsrc = try VirtualPath(path: AbsolutePath(validating: sdk)
161-
.appending(components: "usr", "lib", "swift")
162-
.pathString)
159+
// Prefer the swiftrt.o runtime file from the SDK if it's specified.
160+
if let sdk = targetInfo.sdkPath.flatMap({ VirtualPath.lookup($0.path) }) {
161+
rsrc = sdk.appending(components: "usr", "lib", "swift")
163162
} else {
164163
rsrc = VirtualPath.lookup(targetInfo.runtimeResourcePath.path)
165164
}
@@ -206,20 +205,7 @@ extension GenericUnixToolchain {
206205
}
207206

208207
if targetTriple.environment == .android {
209-
if let ndk = env["ANDROID_NDK_ROOT"] {
210-
var sysroot: AbsolutePath =
211-
try AbsolutePath(validating: ndk)
212-
.appending(components: "toolchains", "llvm", "prebuilt")
213-
#if arch(x86_64)
214-
#if os(Windows)
215-
.appending(component: "windows-x86_64")
216-
#elseif os(Linux)
217-
.appending(component: "linux-x86_64")
218-
#else
219-
.appending(component: "darwin-x86_64")
220-
#endif
221-
#endif
222-
.appending(component: "sysroot")
208+
if let sysroot = try getAndroidNDKSysrootPath() {
223209
commandLine.appendFlag("--sysroot")
224210
commandLine.appendPath(sysroot)
225211
}

Sources/SwiftDriver/Toolchains/GenericUnixToolchain.swift

+46
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,50 @@ public final class GenericUnixToolchain: Toolchain {
117117
let environment = (targetTriple.environment == .android) ? "-android" : ""
118118
return "libclang_rt.\(sanitizer.libraryName)-\(targetTriple.archName)\(environment).a"
119119
}
120+
121+
private func getAndroidNDKHostOSSuffix() -> String? {
122+
#if os(Windows)
123+
"windows"
124+
#elseif os(Linux)
125+
"linux"
126+
#elseif os(macOS)
127+
"darwin"
128+
#else
129+
// The NDK is only available on macOS, linux and windows hosts.
130+
nil
131+
#endif
132+
}
133+
134+
func getAndroidNDKSysrootPath() throws -> AbsolutePath? {
135+
#if arch(x86_64)
136+
// The NDK's sysroot should be specified in the environment.
137+
guard let ndk = env["ANDROID_NDK_ROOT"],
138+
let osSuffix = getAndroidNDKHostOSSuffix() else {
139+
return nil
140+
}
141+
var sysroot: AbsolutePath =
142+
try AbsolutePath(validating: ndk)
143+
.appending(components: "toolchains", "llvm", "prebuilt")
144+
.appending(component: "\(osSuffix)-x86_64")
145+
.appending(component: "sysroot")
146+
return sysroot
147+
#else
148+
// The NDK is only available on an x86_64 host.
149+
return nil
150+
#endif
151+
}
152+
153+
public func addPlatformSpecificCommonFrontendOptions(
154+
commandLine: inout [Job.ArgTemplate],
155+
inputs: inout [TypedVirtualPath],
156+
frontendTargetInfo: FrontendTargetInfo,
157+
driver: inout Driver
158+
) throws {
159+
if driver.targetTriple.environment == .android {
160+
if let sysroot = try getAndroidNDKSysrootPath() {
161+
commandLine.appendFlag("-sysroot")
162+
commandLine.appendFlag(sysroot.pathString)
163+
}
164+
}
165+
}
120166
}

0 commit comments

Comments
 (0)