Skip to content

Commit 739c14e

Browse files
finagolfinhypcompnerd
authored
[6.1][Android] Get this repo building again (#5151)
* [android] Fix package manifest and one Bionic function's API version * Merge commit '246b3474fd4cb91d13b5ecb8fb5175aa6b50aa1e' into github/main * Fix fts_open now that apinotes are applied * Update Sources/Foundation/FileManager+POSIX.swift * fix Port.swift conditional * Ensure that the Android build on a windows host doesn't turn on WMO yet The windows host builds with the old driver for now, so it doesn't yet support wmo * [android] fix pthread flag inclusion in libxml2 package * review fixes * [android] Use Bionic imports instead where possible --------- Co-authored-by: Alex Lorenz <arphaman@gmail.com> Co-authored-by: Saleem Abdulrasool <compnerd@compnerd.org>
1 parent f9fae48 commit 739c14e

32 files changed

+119
-23
lines changed

CMakeLists.txt

+7-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ if(NOT SWIFT_SYSTEM_NAME)
4242
endif()
4343

4444
# Don't enable WMO on Windows due to linker failures
45-
if(NOT CMAKE_SYSTEM_NAME STREQUAL Windows)
45+
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL Windows)
4646
# Enable whole module optimization for release builds & incremental for debug builds
4747
if(POLICY CMP0157)
4848
set(CMAKE_Swift_COMPILATION_MODE "$<IF:$<CONFIG:Release>,wholemodule,incremental>")
@@ -150,6 +150,12 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL "WASI")
150150
"-I${DISPATCH_INCLUDE_PATH}/Block")
151151
endif()
152152
endif()
153+
if(ANDROID)
154+
# LibXml2 looks for the Threads package, so
155+
# ensure that it doesn't try to use the `-pthread`
156+
# flag on Android.
157+
set(CMAKE_HAVE_LIBC_PTHREAD YES)
158+
endif()
153159
find_package(LibXml2 REQUIRED)
154160
if(FOUNDATION_BUILD_NETWORKING)
155161
find_package(CURL REQUIRED)

Package.swift

+6-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if let environmentPath = Context.environment["DISPATCH_INCLUDE_PATH"] {
2626
.unsafeFlags([
2727
"-I/usr/lib/swift",
2828
"-I/usr/lib/swift/Block"
29-
], .when(platforms: [.linux, .android]))
29+
], .when(platforms: [.linux]))
3030
)
3131
if let sdkRoot = Context.environment["SDKROOT"] {
3232
dispatchIncludeFlags.append(.unsafeFlags([
@@ -245,7 +245,8 @@ let package = Package(
245245
"BlockRuntime",
246246
"CMakeLists.txt"
247247
],
248-
cSettings: coreFoundationBuildSettings
248+
cSettings: coreFoundationBuildSettings,
249+
linkerSettings: [.linkedLibrary("log", .when(platforms: [.android]))]
249250
),
250251
.target(
251252
name: "BlocksRuntime",
@@ -262,7 +263,7 @@ let package = Package(
262263
name: "_CFXMLInterface",
263264
dependencies: [
264265
"CoreFoundation",
265-
.target(name: "Clibxml2", condition: .when(platforms: [.linux])),
266+
.target(name: "Clibxml2", condition: .when(platforms: [.linux, .android])),
266267
],
267268
path: "Sources/_CFXMLInterface",
268269
exclude: [
@@ -275,7 +276,7 @@ let package = Package(
275276
name: "_CFURLSessionInterface",
276277
dependencies: [
277278
"CoreFoundation",
278-
.target(name: "Clibcurl", condition: .when(platforms: [.linux])),
279+
.target(name: "Clibcurl", condition: .when(platforms: [.linux, .android])),
279280
],
280281
path: "Sources/_CFURLSessionInterface",
281282
exclude: [
@@ -348,7 +349,7 @@ let package = Package(
348349
"FoundationNetworking",
349350
"XCTest",
350351
"Testing",
351-
.target(name: "xdgTestHelper", condition: .when(platforms: [.linux]))
352+
.target(name: "xdgTestHelper", condition: .when(platforms: [.linux, .android]))
352353
],
353354
resources: [
354355
.copy("Foundation/Resources")

Sources/CoreFoundation/CFPlatform.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2283,7 +2283,7 @@ CF_EXPORT int _CFPosixSpawnFileActionsChdir(_CFPosixSpawnFileActionsRef file_act
22832283
// Glibc versions prior to 2.29 don't support posix_spawn_file_actions_addchdir_np, impacting:
22842284
// - Amazon Linux 2 (EoL mid-2025)
22852285
return ENOSYS;
2286-
#elif defined(__GLIBC__) || TARGET_OS_DARWIN || defined(__FreeBSD__) || defined(__ANDROID__) || defined(__musl__)
2286+
#elif defined(__GLIBC__) || TARGET_OS_DARWIN || defined(__FreeBSD__) || (defined(__ANDROID__) && __ANDROID_API__ >= 34) || defined(__musl__)
22872287
// Pre-standard posix_spawn_file_actions_addchdir_np version available in:
22882288
// - Solaris 11.3 (October 2015)
22892289
// - Glibc 2.29 (February 2019)

Sources/CoreFoundation/include/ForSwiftFoundationOnly.h

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@
6969
#include <sys/stat.h>
7070
#include <sys/syscall.h>
7171
#include <termios.h>
72+
#include <linux/fcntl.h>
73+
#ifdef __swift__
74+
// The linux/stat header is private in the Android modulemap.
75+
#pragma clang module import posix_filesystem.linux_stat
76+
#else
77+
#include <linux/stat.h>
78+
#endif
7279
#elif TARGET_OS_WASI
7380
#include <fcntl.h>
7481
#include <sys/stat.h>

Sources/CoreFoundation/internalInclude/CoreFoundation_Prefix.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ static dispatch_queue_t __ ## PREFIX ## Queue(void) { \
200200
#endif
201201

202202
// We know some things (Darwin, WASI, Glibc >= 2.38) have strlcpy/strlcat
203-
#if TARGET_OS_MAC || TARGET_OS_WASI \
204-
|| (defined(__GLIBC__) && \
205-
((__GLIBC_MAJOR__ == 2 && __GLIBC_MINOR__ >= 38) \
203+
#if TARGET_OS_MAC || TARGET_OS_WASI || TARGET_OS_ANDROID \
204+
|| (defined(__GLIBC__) && \
205+
((__GLIBC_MAJOR__ == 2 && __GLIBC_MINOR__ >= 38) \
206206
|| __GLIBC_MAJOR__ > 2))
207207
#define HAVE_STRLCPY 1
208208
#define HAVE_STRLCAT 1

Sources/Foundation/CGFloat.swift

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if canImport(Android)
11+
import Android
12+
#endif
13+
1014
@frozen
1115
public struct CGFloat: Sendable {
1216
#if arch(i386) || arch(arm) || arch(wasm32)

Sources/Foundation/FileHandle.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ import WASILibc
3434
fileprivate let _read = WASILibc.read(_:_:_:)
3535
fileprivate let _write = WASILibc.write(_:_:_:)
3636
fileprivate let _close = WASILibc.close(_:)
37+
#elseif canImport(Android)
38+
import Android
39+
fileprivate let _read = Android.read(_:_:_:)
40+
fileprivate let _write = Android.write(_:_:_:)
41+
fileprivate let _close = Android.close(_:)
3742
#endif
3843

3944
#if canImport(WinSDK)
@@ -324,7 +329,7 @@ open class FileHandle : NSObject, @unchecked Sendable {
324329
let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)
325330
// Swift does not currently expose MAP_FAILURE
326331
if data != UnsafeMutableRawPointer(bitPattern: -1) {
327-
return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in
332+
return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in
328333
munmap(buffer, length)
329334
}
330335
}

Sources/Foundation/FileManager+POSIX.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
//
88
#if !os(Windows)
99

10+
#if canImport(Android)
11+
import Android
12+
#endif
13+
1014
#if os(Android) && (arch(i386) || arch(arm)) // struct stat.st_mode is UInt32
1115
internal func &(left: UInt32, right: mode_t) -> mode_t {
1216
return mode_t(left) & right
@@ -398,13 +402,13 @@ extension FileManager {
398402

399403
_current = fts_read(stream)
400404
while let current = _current {
401-
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
405+
let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path!, length: Int(current.pointee.fts_pathlen))
402406

403407
switch Int32(current.pointee.fts_info) {
404408
case FTS_D:
405409
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
406410
if skipDescendants {
407-
fts_set(_stream, _current, FTS_SKIP)
411+
fts_set(stream, current, FTS_SKIP)
408412
}
409413
if showFile {
410414
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -578,7 +582,7 @@ extension FileManager {
578582
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
579583
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
580584
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
581-
if rename(newItemFS, originalFS) == 0 {
585+
if rename(newItemFS!, originalFS!) == 0 {
582586
return nil
583587
} else {
584588
return errno

Sources/Foundation/FileManager.swift

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import WinSDK
2121

2222
#if os(WASI)
2323
import WASILibc
24+
#elseif canImport(Bionic)
25+
import Bionic
2426
#endif
2527

2628
#if os(Windows)

Sources/Foundation/Host.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
import WinSDK
1313
#endif
1414

15-
#if os(Android)
16-
// Android Glibc differs a little with respect to the Linux Glibc.
15+
#if canImport(Android)
16+
import Android
17+
// Android Bionic differs a little with respect to the Linux Glibc.
1718

1819
// IFF_LOOPBACK is part of the enumeration net_device_flags, which needs to
1920
// convert to UInt32.
@@ -24,8 +25,8 @@ import WinSDK
2425
}
2526

2627
// getnameinfo uses size_t for its 4th and 6th arguments.
27-
private func getnameinfo(_ addr: UnsafePointer<sockaddr>?, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
28-
return Glibc.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
28+
private func getnameinfo(_ addr: UnsafePointer<sockaddr>, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
29+
return Android.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
2930
}
3031

3132
// getifaddrs and freeifaddrs are not available in Android 6.0 or earlier, so call these functions dynamically.

Sources/Foundation/NSData.swift

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
#if !os(WASI)
1212
import Dispatch
1313
#endif
14+
#if canImport(Android)
15+
import Android
16+
#endif
1417

1518
extension NSData {
1619
public typealias ReadingOptions = Data.ReadingOptions
@@ -469,6 +472,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
469472
let createMode = Int(Musl.S_IRUSR) | Int(Musl.S_IWUSR) | Int(Musl.S_IRGRP) | Int(Musl.S_IWGRP) | Int(Musl.S_IROTH) | Int(Musl.S_IWOTH)
470473
#elseif canImport(WASILibc)
471474
let createMode = Int(WASILibc.S_IRUSR) | Int(WASILibc.S_IWUSR) | Int(WASILibc.S_IRGRP) | Int(WASILibc.S_IWGRP) | Int(WASILibc.S_IROTH) | Int(WASILibc.S_IWOTH)
475+
#elseif canImport(Android)
476+
let createMode = Int(Android.S_IRUSR) | Int(Android.S_IWUSR) | Int(Android.S_IRGRP) | Int(Android.S_IWGRP) | Int(Android.S_IROTH) | Int(Android.S_IWOTH)
472477
#endif
473478
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
474479
throw _NSErrorWithErrno(errno, reading: false, path: path)

Sources/Foundation/NSError.swift

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import Darwin
1616
import Glibc
1717
#elseif canImport(CRT)
1818
import CRT
19+
#elseif canImport(Android)
20+
import Android
1921
#endif
2022

2123
@_implementationOnly import CoreFoundation

Sources/Foundation/NSLock.swift

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#if canImport(Glibc)
1313
import Glibc
14+
#elseif canImport(Bionic)
15+
import Bionic
1416
#endif
1517

1618
#if os(Windows)

Sources/Foundation/NSPathUtilities.swift

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
@_implementationOnly import CoreFoundation
1111
#if os(Windows)
1212
import WinSDK
13+
#elseif canImport(Android)
14+
import Android
1315
#elseif os(WASI)
1416
import WASILibc
1517
#endif

Sources/Foundation/NSPlatform.swift

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#if os(macOS) || os(iOS)
1111
fileprivate let _NSPageSize = Int(vm_page_size)
1212
#elseif os(Linux) || os(Android) || os(OpenBSD)
13+
#if canImport(Android)
14+
import Android
15+
#endif
1316
fileprivate let _NSPageSize = Int(getpagesize())
1417
#elseif os(Windows)
1518
import WinSDK

Sources/Foundation/NSSwiftRuntime.swift

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ internal import Synchronization
1919
@_exported import Glibc
2020
#elseif canImport(Musl)
2121
@_exported import Musl
22+
#elseif canImport(Bionic)
23+
@_exported import Bionic
2224
#elseif os(WASI)
2325
@_exported import WASILibc
2426
#elseif os(Windows)

Sources/Foundation/NSURL.swift

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import Darwin
2222
import Glibc
2323
#elseif canImport(Musl)
2424
import Musl
25+
#elseif canImport(Bionic)
26+
import Bionic
2527
#endif
2628

2729
// NOTE: this represents PLATFORM_PATH_STYLE

Sources/Foundation/Port.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fileprivate let FOUNDATION_SOCK_STREAM = SOCK_STREAM
107107
fileprivate let FOUNDATION_IPPROTO_TCP = IPPROTO_TCP
108108
#endif
109109

110-
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
110+
#if canImport(Glibc) && !os(OpenBSD)
111111
import Glibc
112112
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM.rawValue)
113113
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
@@ -119,14 +119,19 @@ fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
119119
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
120120
#endif
121121

122-
#if canImport(Glibc) && os(Android) || os(OpenBSD)
122+
#if canImport(Glibc) && os(OpenBSD)
123123
import Glibc
124124
fileprivate let FOUNDATION_SOCK_STREAM = Int32(SOCK_STREAM)
125125
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(IPPROTO_TCP)
126126
fileprivate let INADDR_ANY: in_addr_t = 0
127-
#if os(OpenBSD)
128127
fileprivate let INADDR_LOOPBACK = 0x7f000001
129128
#endif
129+
130+
#if canImport(Android)
131+
import Android
132+
fileprivate let FOUNDATION_SOCK_STREAM = Int32(Android.SOCK_STREAM)
133+
fileprivate let FOUNDATION_IPPROTO_TCP = Int32(Android.IPPROTO_TCP)
134+
fileprivate let INADDR_ANY: in_addr_t = 0
130135
#endif
131136

132137

Sources/Foundation/Process.swift

+9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import struct WinSDK.HANDLE
1818

1919
#if canImport(Darwin)
2020
import Darwin
21+
#elseif canImport(Android)
22+
import Android
2123
#endif
2224

2325
internal import Synchronization
@@ -940,6 +942,13 @@ open class Process: NSObject, @unchecked Sendable {
940942
var spawnAttrs: posix_spawnattr_t? = nil
941943
#else
942944
var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t()
945+
#endif
946+
#if os(Android)
947+
guard var spawnAttrs else {
948+
throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno), userInfo: [
949+
NSURLErrorKey:self.executableURL!
950+
])
951+
}
943952
#endif
944953
try _throwIfPosixError(posix_spawnattr_init(&spawnAttrs))
945954
try _throwIfPosixError(posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_SETPGROUP)))

Sources/Foundation/Thread.swift

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import WinSDK
1717
import Glibc
1818
#elseif canImport(Musl)
1919
import Musl
20+
#elseif canImport(Bionic)
21+
import Bionic
2022
#endif
2123

2224
// WORKAROUND_SR9811

Sources/FoundationNetworking/HTTPCookie.swift

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Foundation
1515

1616
#if os(Windows)
1717
import WinSDK
18+
#elseif canImport(Android)
19+
import Android
1820
#endif
1921

2022
public struct HTTPCookiePropertyKey : RawRepresentable, Equatable, Hashable, Sendable {

Sources/Testing/Testing.swift

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import Glibc
1212
#elseif canImport(Musl)
1313
import Musl
14+
#elseif canImport(Bionic)
15+
import Bionic
1416
#elseif os(WASI)
1517
import WASILibc
1618
#elseif canImport(CRT)

Sources/plutil/main.swift

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import Glibc
1515
#elseif canImport(Musl)
1616
import Foundation
1717
import Musl
18+
#elseif canImport(Bionic)
19+
import Foundation
20+
import Bionic
1821
#elseif canImport(CRT)
1922
import Foundation
2023
import CRT

Sources/xdgTestHelper/main.swift

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import FoundationNetworking
1919
#endif
2020
#if os(Windows)
2121
import WinSDK
22+
#elseif canImport(Android)
23+
import Android
2224
#endif
2325

2426
enum HelperCheckStatus : Int32 {

Tests/Foundation/FTPServer.swift

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import Dispatch
1515
import Glibc
1616
#elseif canImport(Darwin)
1717
import Darwin
18+
#elseif canImport(Android)
19+
import Android
1820
#endif
1921

2022
final class ServerSemaphore : Sendable {

Tests/Foundation/HTTPServer.swift

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import Dispatch
2121
import Darwin
2222
#elseif canImport(Glibc)
2323
import Glibc
24+
#elseif canImport(Android)
25+
import Android
2426
#endif
2527

2628
#if !os(Windows)

0 commit comments

Comments
 (0)