Skip to content

Commit 4c3228c

Browse files
authored
Merge pull request swiftlang#5024 from hyp/eng/recore-android-build
Add android support, that uses new swift-foundation for Android
2 parents b81f510 + 1da58eb commit 4c3228c

29 files changed

+110
-17
lines changed

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
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)

Sources/CoreFoundation/include/ForSwiftFoundationOnly.h

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

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

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

Lines changed: 6 additions & 1 deletion
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

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

Lines changed: 2 additions & 0 deletions
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(Android)
25+
import Android
2426
#endif
2527

2628
#if os(Windows)

Sources/Foundation/Host.swift

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

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

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

0 commit comments

Comments
 (0)