From 86c3df5572be06c9131b961eb0447d26cbdf7c6b Mon Sep 17 00:00:00 2001 From: Marc Prud'hommeaux Date: Sat, 4 Jan 2025 15:47:28 -0500 Subject: [PATCH] Android support --- .github/workflows/android.yml | 20 ++++++++++++++++++++ Sources/PNG/System.swift | 22 +++++++++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/android.yml diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml new file mode 100644 index 00000000..f4eb58cb --- /dev/null +++ b/.github/workflows/android.yml @@ -0,0 +1,20 @@ +name: Android + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + android: + runs-on: ubuntu-24.04 + name: Android + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Build for Android + uses: skiptools/swift-android-action@v2 + with: + run-tests: false diff --git a/Sources/PNG/System.swift b/Sources/PNG/System.swift index 69672259..f7e0d682 100644 --- a/Sources/PNG/System.swift +++ b/Sources/PNG/System.swift @@ -6,6 +6,8 @@ import Darwin #elseif canImport(Glibc) import Glibc +#elseif canImport(Android) + import Android #elseif canImport(Musl) import Musl #elseif os(Windows) @@ -15,7 +17,7 @@ #warning("unsupported or untested platform (please open an issue at https://github.com/tayloraswift/swift-png/issues)") #endif -#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || os(Windows) +#if canImport(Darwin) || canImport(Glibc) || canImport(Android) || canImport(Musl) || os(Windows) /// A namespace for platform-dependent functionality. /// @@ -28,7 +30,11 @@ enum System public enum File { + #if os(Android) + typealias Descriptor = OpaquePointer + #else typealias Descriptor = UnsafeMutablePointer + #endif /// A type for reading data from files on disk. public @@ -100,7 +106,12 @@ extension System.File.Source { (buffer:inout UnsafeMutableBufferPointer, count:inout Int) in - count = fread(buffer.baseAddress, MemoryLayout.stride, + #if os(Android) + let baseAddress = buffer.baseAddress! + #else + let baseAddress = buffer.baseAddress + #endif + count = fread(baseAddress, MemoryLayout.stride, capacity, self.descriptor) } @@ -213,7 +224,12 @@ extension System.File.Destination { let count:Int = buffer.withUnsafeBufferPointer { - fwrite($0.baseAddress, MemoryLayout.stride, + #if os(Android) + let baseAddress = $0.baseAddress! + #else + let baseAddress = $0.baseAddress + #endif + return fwrite(baseAddress, MemoryLayout.stride, $0.count, self.descriptor) }