Skip to content

Commit 44773d2

Browse files
committed
[android] android build fixes
1 parent 66ede86 commit 44773d2

15 files changed

+63
-13
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ if(HAS_LIBDISPATCH_API)
7676
find_package(Threads REQUIRED)
7777
endif()
7878

79+
# CMake's Threads adds '-pthread' flag to the interface link
80+
# libraries, which isn't supported by Swift. This is not enabled
81+
# when building with MSVC, but it trips up the Android build, so
82+
# we need to clear out the threads INTERFACE_LINK_LIBRARIES.
83+
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
84+
set_property(TARGET Threads::Threads PROPERTY INTERFACE_LINK_LIBRARIES "")
85+
endif()
86+
7987
set(SAVED_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
8088
set(BUILD_SHARED_LIBS NO)
8189
add_subdirectory(CoreFoundation EXCLUDE_FROM_ALL)

Sources/Foundation/Data.swift

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
@usableFromInline let memset = WASILibc.memset
4343
@usableFromInline let memcpy = WASILibc.memcpy
4444
@usableFromInline let memcmp = WASILibc.memcmp
45+
#elseif canImport(Android)
46+
@usableFromInline let calloc = Android.calloc
47+
@usableFromInline let malloc = Android.malloc
48+
@usableFromInline let free = Android.free
49+
@usableFromInline let memset = Android.memset
50+
@usableFromInline let memcpy = Android.memcpy
51+
@usableFromInline let memcmp = Android.memcmp
4552
#endif
4653

4754
#if !canImport(Darwin)

Sources/Foundation/FileHandle.swift

+5
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)

Sources/Foundation/FileManager+POSIX.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
internal func &(left: UInt32, right: mode_t) -> mode_t {
1212
return mode_t(left) & right
1313
}
14+
#elseif os(Android)
15+
internal func &(left: mode_t, right: Int32) -> mode_t {
16+
return left & mode_t(right)
17+
}
1418
#endif
1519

1620
#if os(WASI)
@@ -409,7 +413,7 @@ extension FileManager {
409413
if !parent.isEmpty && !fileExists(atPath: parent, isDirectory: &isDir) {
410414
try createDirectory(atPath: parent, withIntermediateDirectories: true, attributes: attributes)
411415
}
412-
if mkdir(pathFsRep, S_IRWXU | S_IRWXG | S_IRWXO) != 0 {
416+
if mkdir(pathFsRep, mode_t(S_IRWXU) | mode_t(S_IRWXG) | mode_t(S_IRWXO)) != 0 {
413417
let posixError = errno
414418
if posixError == EEXIST && fileExists(atPath: path, isDirectory: &isDir) && isDir.boolValue {
415419
// Continue; if there is an existing file and it is a directory, that is still a success.
@@ -428,7 +432,7 @@ extension FileManager {
428432
throw _NSErrorWithErrno(EEXIST, reading: false, path: path)
429433
}
430434
} else {
431-
if mkdir(pathFsRep, S_IRWXU | S_IRWXG | S_IRWXO) != 0 {
435+
if mkdir(pathFsRep, mode_t(S_IRWXU) | mode_t(S_IRWXG) | mode_t(S_IRWXO)) != 0 {
432436
throw _NSErrorWithErrno(errno, reading: false, path: path)
433437
} else if let attr = attributes {
434438
try self.setAttributes(attr, ofItemAtPath: path)

Sources/Foundation/FileManager.swift

+10-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import WinSDK
2525
import WASILibc
2626
#endif
2727

28+
#if os(Android)
29+
import Android
30+
#endif
31+
2832
#if os(Windows)
2933
internal typealias NativeFSRCharType = WCHAR
3034
internal let NativeFSREncoding = String.Encoding.utf16LittleEndian.rawValue
@@ -1342,12 +1346,12 @@ public struct FileAttributeType : RawRepresentable, Equatable, Hashable {
13421346
#else
13431347
internal init(statMode: mode_t) {
13441348
switch statMode & S_IFMT {
1345-
case S_IFCHR: self = .typeCharacterSpecial
1346-
case S_IFDIR: self = .typeDirectory
1347-
case S_IFBLK: self = .typeBlockSpecial
1348-
case S_IFREG: self = .typeRegular
1349-
case S_IFLNK: self = .typeSymbolicLink
1350-
case S_IFSOCK: self = .typeSocket
1349+
case mode_t(S_IFCHR): self = .typeCharacterSpecial
1350+
case mode_t(S_IFDIR): self = .typeDirectory
1351+
case mode_t(S_IFBLK): self = .typeBlockSpecial
1352+
case mode_t(S_IFREG): self = .typeRegular
1353+
case mode_t(S_IFLNK): self = .typeSymbolicLink
1354+
case mode_t(S_IFSOCK): self = .typeSocket
13511355
default: self = .typeUnknown
13521356
}
13531357
}

Sources/Foundation/Host.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import WinSDK
1313
#endif
1414

1515
#if os(Android)
16+
import Android
1617
// Android Glibc differs a little with respect to the Linux Glibc.
1718

1819
// IFF_LOOPBACK is part of the enumeration net_device_flags, which needs to
@@ -25,7 +26,7 @@ import WinSDK
2526

2627
// getnameinfo uses size_t for its 4th and 6th arguments.
2728
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)
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

+2
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
495495
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)
496496
#elseif canImport(WASILibc)
497497
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)
498+
#elseif canImport(Android)
499+
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)
498500
#endif
499501
guard let fh = FileHandle(path: path, flags: flags, createMode: createMode) else {
500502
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(Android)
15+
import Android
1416
#endif
1517

1618
#if os(Windows)

Sources/Foundation/NSPathUtilities.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ internal func _NSCreateTemporaryFile(_ filePath: String) throws -> (Int32, Strin
803803
}
804804

805805
// Set the file mode to match macOS
806-
guard fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) != -1 else {
806+
guard fchmod(fd, mode_t(S_IRUSR) | mode_t(S_IWUSR) | mode_t(S_IRGRP) | mode_t(S_IWGRP) | mode_t(S_IROTH) | mode_t(S_IWOTH)) != -1 else {
807807
let _errno = errno
808808
close(fd)
809809
throw _NSErrorWithErrno(_errno, reading: false, path: pathResult)

Sources/Foundation/NSSwiftRuntime.swift

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
@_exported import Glibc
1919
#elseif canImport(Musl)
2020
@_exported import Musl
21+
#elseif canImport(Android)
22+
@_exported import Android
2123
#elseif os(WASI)
2224
@_exported import WASILibc
2325
#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(Android)
26+
import Android
2527
#endif
2628

2729
// NOTE: this represents PLATFORM_PATH_STYLE

Sources/Foundation/Port.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,28 @@ open class SocketPort: Port {}
9090

9191
#else
9292

93-
#if canImport(Glibc) && !os(Android) && !os(OpenBSD)
93+
#if canImport(Glibc) && !os(OpenBSD)
9494
import Glibc
9595
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM.rawValue)
9696
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM.rawValue)
9797
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
9898
#endif
9999

100-
#if canImport(Glibc) && os(Android) || os(OpenBSD)
100+
#if canImport(Glibc) && os(OpenBSD)
101101
import Glibc
102102
fileprivate let SOCK_STREAM = Int32(Glibc.SOCK_STREAM)
103103
fileprivate let SOCK_DGRAM = Int32(Glibc.SOCK_DGRAM)
104104
fileprivate let IPPROTO_TCP = Int32(Glibc.IPPROTO_TCP)
105105
fileprivate let INADDR_ANY: in_addr_t = 0
106-
#if os(OpenBSD)
107106
fileprivate let INADDR_LOOPBACK = 0x7f000001
108107
#endif
108+
109+
#if canImport(Android)
110+
import Android
111+
fileprivate let SOCK_STREAM = Int32(Android.SOCK_STREAM)
112+
fileprivate let SOCK_DGRAM = Int32(Android.SOCK_DGRAM)
113+
fileprivate let IPPROTO_TCP = Int32(Android.IPPROTO_TCP)
114+
fileprivate let INADDR_ANY: in_addr_t = 0
109115
#endif
110116

111117

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(Android)
21+
import Android
2022
#endif
2123

2224
// WORKAROUND_SR9811

Sources/Tools/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(Android)
19+
import Foundation
20+
import Android
1821
#elseif canImport(CRT)
1922
import Foundation
2023
import CRT

0 commit comments

Comments
 (0)