Files
termux-packages/packages/swift/swift-android-ndk26.patch

298 lines
16 KiB
Diff

diff --git a/swift/stdlib/cmake/modules/AddSwiftStdlib.cmake b/swift/stdlib/cmake/modules/AddSwiftStdlib.cmake
index 61447d50f08..b533b9291af 100644
--- a/swift/stdlib/cmake/modules/AddSwiftStdlib.cmake
+++ b/swift/stdlib/cmake/modules/AddSwiftStdlib.cmake
@@ -522,7 +522,11 @@ function(_add_target_variant_link_flags)
# We need to add the math library, which is linked implicitly by libc++
list(APPEND result "-lm")
if(NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
- file(GLOB RESOURCE_DIR ${SWIFT_SDK_ANDROID_ARCH_${LFLAGS_ARCH}_PATH}/../lib64/clang/*)
+ if("${SWIFT_ANDROID_NDK_PATH}" MATCHES "r26")
+ file(GLOB RESOURCE_DIR ${SWIFT_SDK_ANDROID_ARCH_${LFLAGS_ARCH}_PATH}/../lib/clang/*)
+ else()
+ file(GLOB RESOURCE_DIR ${SWIFT_SDK_ANDROID_ARCH_${LFLAGS_ARCH}_PATH}/../lib64/clang/*)
+ endif()
list(APPEND result "-resource-dir=${RESOURCE_DIR}")
endif()
diff --git a/swift/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift b/swift/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift
index f8daa50325c..66be85a77f0 100644
--- a/swift/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift
+++ b/swift/stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift
@@ -131,6 +131,8 @@ public func _stdlib_pipe() -> (readEnd: CInt, writeEnd: CInt, error: CInt) {
return _pipe(unsafeFds.baseAddress, 0, 0)
#elseif os(WASI)
preconditionFailure("No pipes available on WebAssembly/WASI")
+#elseif os(Android)
+ return pipe(unsafeFds.baseAddress!)
#else
return pipe(unsafeFds.baseAddress)
#endif
diff --git a/swift/stdlib/public/SwiftShims/swift/shims/LibcShims.h b/swift/stdlib/public/SwiftShims/swift/shims/LibcShims.h
index 1e4132f6279..045b8a28746 100644
--- a/swift/stdlib/public/SwiftShims/swift/shims/LibcShims.h
+++ b/swift/stdlib/public/SwiftShims/swift/shims/LibcShims.h
@@ -61,7 +61,7 @@ SWIFT_READONLY
static inline int _swift_stdlib_memcmp(const void *s1, const void *s2,
__swift_size_t n) {
// FIXME: Is there a way to identify Glibc specifically?
-#if defined(__gnu_linux__)
+#if defined(__gnu_linux__) || defined(__ANDROID__)
extern int memcmp(const void * _Nonnull, const void * _Nonnull, __swift_size_t);
#else
extern int memcmp(const void * _Null_unspecified, const void * _Null_unspecified, __swift_size_t);
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift b/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
index a538a297..0a757c4b 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileHandle.swift
@@ -310,9 +310,15 @@ open class FileHandle : NSObject {
let data = mmap(nil, mapSize, PROT_READ, MAP_PRIVATE, _fd, 0)
// Swift does not currently expose MAP_FAILURE
if data != UnsafeMutableRawPointer(bitPattern: -1) {
+ #if os(Android)
+ return NSData.NSDataReadResult(bytes: data, length: mapSize) { buffer, length in
+ munmap(buffer, length)
+ }
+ #else
return NSData.NSDataReadResult(bytes: data!, length: mapSize) { buffer, length in
munmap(buffer, length)
}
+ #endif
}
}
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift b/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
index d90ece91..d2bbd22b 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileManager+POSIX.swift
@@ -741,20 +741,26 @@ extension FileManager {
if rmdir(fsRep) == 0 {
return
} else if errno == ENOTEMPTY {
+ #if os(Android)
+ let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>>.allocate(capacity: 2)
+ ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
+ ps.advanced(by: 1).initialize(to: unsafeBitCast(0, to: UnsafeMutablePointer<Int8>.self))
+ #else
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
ps.advanced(by: 1).initialize(to: nil)
+ #endif
let stream = fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
ps.deinitialize(count: 2)
ps.deallocate()
- if stream != nil {
+ if let openStream = stream {
defer {
- fts_close(stream)
+ fts_close(openStream)
}
- while let current = fts_read(stream)?.pointee {
- let itemPath = string(withFileSystemRepresentation: current.fts_path, length: Int(current.fts_pathlen))
+ while let current = fts_read(openStream)?.pointee, let current_path = current.fts_path {
+ let itemPath = string(withFileSystemRepresentation: current_path, length: Int(current.fts_pathlen))
guard alreadyConfirmed || shouldRemoveItemAtPath(itemPath, isURL: isURL) else {
continue
}
@@ -762,11 +768,11 @@ extension FileManager {
do {
switch Int32(current.fts_info) {
case FTS_DEFAULT, FTS_F, FTS_NSOK, FTS_SL, FTS_SLNONE:
- if unlink(current.fts_path) == -1 {
+ if unlink(current_path) == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
}
case FTS_DP:
- if rmdir(current.fts_path) == -1 {
+ if rmdir(current_path) == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: itemPath)
}
case FTS_DNR, FTS_ERR, FTS_NS:
@@ -1085,10 +1091,18 @@ extension FileManager {
do {
guard fm.fileExists(atPath: _url.path) else { throw _NSErrorWithErrno(ENOENT, reading: true, url: url) }
_stream = try FileManager.default._fileSystemRepresentation(withPath: _url.path) { fsRep in
+ #if os(Android)
+ let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>>.allocate(capacity: 2)
+ #else
let ps = UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>.allocate(capacity: 2)
+ #endif
defer { ps.deallocate() }
ps.initialize(to: UnsafeMutablePointer(mutating: fsRep))
+ #if os(Android)
+ ps.advanced(by: 1).initialize(to: unsafeBitCast(0, to: UnsafeMutablePointer<Int8>.self))
+ #else
ps.advanced(by: 1).initialize(to: nil)
+ #endif
return fts_open(ps, FTS_PHYSICAL | FTS_XDEV | FTS_NOCHDIR | FTS_NOSTAT, nil)
}
if _stream == nil {
@@ -1135,14 +1149,14 @@ extension FileManager {
}
_current = fts_read(stream)
- while let current = _current {
- let filename = FileManager.default.string(withFileSystemRepresentation: current.pointee.fts_path, length: Int(current.pointee.fts_pathlen))
+ while let current = _current, let current_path = current.pointee.fts_path {
+ let filename = FileManager.default.string(withFileSystemRepresentation: current_path, length: Int(current.pointee.fts_pathlen))
switch Int32(current.pointee.fts_info) {
case FTS_D:
let (showFile, skipDescendants) = match(filename: filename, to: _options, isDir: true)
if skipDescendants {
- fts_set(_stream, _current, FTS_SKIP)
+ fts_set(stream, current, FTS_SKIP)
}
if showFile {
return URL(fileURLWithPath: filename, isDirectory: true)
@@ -1315,7 +1329,7 @@ extension FileManager {
let finalErrno = originalItemURL.withUnsafeFileSystemRepresentation { (originalFS) -> Int32? in
return newItemURL.withUnsafeFileSystemRepresentation { (newItemFS) -> Int32? in
// This is an atomic operation in many OSes, but is not guaranteed to be atomic by the standard.
- if rename(newItemFS, originalFS) == 0 {
+ if let newFS = newItemFS, let origFS = originalFS, rename(newFS, origFS) == 0 {
return nil
} else {
return errno
diff --git a/swift-corelibs-foundation/Sources/Foundation/FileManager.swift b/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
index 1aa3038a..9fdb495c 100644
--- a/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/FileManager.swift
@@ -568,13 +568,13 @@ open class FileManager : NSObject {
let attributes = try windowsFileAttributes(atPath: path)
let type = FileAttributeType(attributes: attributes, atPath: path)
#else
- if let pwd = getpwuid(s.st_uid), pwd.pointee.pw_name != nil {
- let name = String(cString: pwd.pointee.pw_name)
+ if let pwd = getpwuid(s.st_uid), let pwd_name = pwd.pointee.pw_name {
+ let name = String(cString: pwd_name)
result[.ownerAccountName] = name
}
- if let grd = getgrgid(s.st_gid), grd.pointee.gr_name != nil {
- let name = String(cString: grd.pointee.gr_name)
+ if let grd = getgrgid(s.st_gid), let grd_name = grd.pointee.gr_name {
+ let name = String(cString: grd_name)
result[.groupOwnerAccountName] = name
}
diff --git a/swift-corelibs-foundation/Sources/Foundation/Host.swift b/swift-corelibs-foundation/Sources/Foundation/Host.swift
index 5fe7b29c..ce571abe 100644
--- a/swift-corelibs-foundation/Sources/Foundation/Host.swift
+++ b/swift-corelibs-foundation/Sources/Foundation/Host.swift
@@ -25,7 +25,8 @@ import WinSDK
// getnameinfo uses size_t for its 4th and 6th arguments.
private func getnameinfo(_ addr: UnsafePointer<sockaddr>?, _ addrlen: socklen_t, _ host: UnsafeMutablePointer<Int8>?, _ hostlen: socklen_t, _ serv: UnsafeMutablePointer<Int8>?, _ servlen: socklen_t, _ flags: Int32) -> Int32 {
- return Glibc.getnameinfo(addr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
+ guard let saddr = addr else { return -1 }
+ return Glibc.getnameinfo(saddr, addrlen, host, Int(hostlen), serv, Int(servlen), flags)
}
// getifaddrs and freeifaddrs are not available in Android 6.0 or earlier, so call these functions dynamically.
diff --git a/swift-driver/Sources/SwiftDriver/Driver/ToolExecutionDelegate.swift b/swift-driver/Sources/SwiftDriver/Driver/ToolExecutionDelegate.swift
index 4d6577d0..aa22dade 100644
--- a/swift-driver/Sources/SwiftDriver/Driver/ToolExecutionDelegate.swift
+++ b/swift-driver/Sources/SwiftDriver/Driver/ToolExecutionDelegate.swift
@@ -136,7 +136,7 @@ import var TSCBasic.stdoutStream
}
#else
case .signalled(let signal):
- let errorMessage = strsignal(signal).map { String(cString: $0) } ?? ""
+ let errorMessage = String(cString: strsignal(signal)) ?? ""
messages = constructJobSignalledMessages(job: job, error: errorMessage, output: output,
signal: signal, pid: pid).map {
ParsableMessage(name: job.kind.rawValue, kind: .signalled($0))
diff --git a/swift-tools-support-core/Sources/TSCBasic/FileSystem.swift b/swift-tools-support-core/Sources/TSCBasic/FileSystem.swift
index b33b1c6..cd2ce5b 100644
--- a/swift-tools-support-core/Sources/TSCBasic/FileSystem.swift
+++ b/swift-tools-support-core/Sources/TSCBasic/FileSystem.swift
@@ -491,8 +491,8 @@ private struct LocalFileSystem: FileSystem {
func readFileContents(_ path: AbsolutePath) throws -> ByteString {
// Open the file.
- let fp = fopen(path.pathString, "rb")
- if fp == nil {
+ let fpo = fopen(path.pathString, "rb")
+ guard let fp = fpo else {
throw FileSystemError(errno: errno, path)
}
defer { fclose(fp) }
@@ -521,8 +521,8 @@ private struct LocalFileSystem: FileSystem {
func writeFileContents(_ path: AbsolutePath, bytes: ByteString) throws {
// Open the file.
- let fp = fopen(path.pathString, "wb")
- if fp == nil {
+ let fpo = fopen(path.pathString, "wb")
+ guard let fp = fpo else {
throw FileSystemError(errno: errno, path)
}
defer { fclose(fp) }
diff --git a/swift-tools-support-core/Sources/TSCBasic/Process.swift b/swift-tools-support-core/Sources/TSCBasic/Process.swift
index 6c8aa11..71d829c 100644
--- a/swift-tools-support-core/Sources/TSCBasic/Process.swift
+++ b/swift-tools-support-core/Sources/TSCBasic/Process.swift
@@ -144,6 +144,9 @@ public final class Process {
/// The current OS does not support the workingDirectory API.
case workingDirectoryNotSupported
+
+ /// The stdin could not be opened.
+ case stdinNotOpening
}
public enum OutputRedirection {
@@ -677,7 +680,10 @@ public final class Process {
var stdinPipe: [Int32] = [-1, -1]
try open(pipe: &stdinPipe)
- let stdinStream = try LocalFileOutputByteStream(filePointer: fdopen(stdinPipe[1], "wb"), closeOnDeinit: true)
+ guard let fp = fdopen(stdinPipe[1], "wb") else {
+ throw Process.Error.stdinNotOpening
+ }
+ let stdinStream = try LocalFileOutputByteStream(filePointer: fp, closeOnDeinit: true)
// Dupe the read portion of the remote to 0.
posix_spawn_file_actions_adddup2(&fileActions, stdinPipe[0], 0)
@@ -1258,6 +1264,8 @@ extension Process.Error: CustomStringConvertible {
return "could not find executable for '\(program)'"
case .workingDirectoryNotSupported:
return "workingDirectory is not supported in this platform"
+ case .stdinNotOpening:
+ return "could not open stdin on this platform"
}
}
}
diff --git a/swift-tools-support-core/Sources/TSCBasic/WritableByteStream.swift b/swift-tools-support-core/Sources/TSCBasic/WritableByteStream.swift
index aee907e..2047d22 100644
--- a/swift-tools-support-core/Sources/TSCBasic/WritableByteStream.swift
+++ b/swift-tools-support-core/Sources/TSCBasic/WritableByteStream.swift
@@ -790,7 +790,11 @@ public final class LocalFileOutputByteStream: FileOutputByteStream {
override final func writeImpl(_ bytes: ArraySlice<UInt8>) {
bytes.withUnsafeBytes { bytesPtr in
while true {
+ #if os(Android)
+ let n = fwrite(bytesPtr.baseAddress!, 1, bytesPtr.count, filePointer)
+ #else
let n = fwrite(bytesPtr.baseAddress, 1, bytesPtr.count, filePointer)
+ #endif
if n < 0 {
if errno == EINTR { continue }
errorDetected(code: errno)
diff --git a/swift-tools-support-core/Sources/TSCTestSupport/PseudoTerminal.swift b/swift-tools-support-core/Sources/TSCTestSupport/PseudoTerminal.swift
index 59610b6..2797c71 100644
--- a/swift-tools-support-core/Sources/TSCTestSupport/PseudoTerminal.swift
+++ b/swift-tools-support-core/Sources/TSCTestSupport/PseudoTerminal.swift
@@ -24,7 +24,7 @@ public final class PseudoTerminal {
if openpty(&primary, &secondary, nil, nil, nil) != 0 {
return nil
}
- guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(secondary, "w"), closeOnDeinit: false) else {
+ guard let outStream = try? LocalFileOutputByteStream(filePointer: fdopen(secondary, "w")!, closeOnDeinit: false) else {
return nil
}
self.outStream = outStream