Skip to content

Commit

Permalink
Merge pull request #84 from marcprux/master
Browse files Browse the repository at this point in the history
Android Support
  • Loading branch information
tayloraswift authored Jan 5, 2025
2 parents 3f8b072 + 86c3df5 commit 4e3ed28
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -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
22 changes: 19 additions & 3 deletions Sources/PNG/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Android)
import Android
#elseif canImport(Musl)
import Musl
#elseif os(Windows)
Expand All @@ -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.
///
Expand All @@ -28,7 +30,11 @@ enum System
public
enum File
{
#if os(Android)
typealias Descriptor = OpaquePointer
#else
typealias Descriptor = UnsafeMutablePointer<FILE>
#endif

/// A type for reading data from files on disk.
public
Expand Down Expand Up @@ -100,7 +106,12 @@ extension System.File.Source
{
(buffer:inout UnsafeMutableBufferPointer<UInt8>, count:inout Int) in

count = fread(buffer.baseAddress, MemoryLayout<UInt8>.stride,
#if os(Android)
let baseAddress = buffer.baseAddress!
#else
let baseAddress = buffer.baseAddress
#endif
count = fread(baseAddress, MemoryLayout<UInt8>.stride,
capacity, self.descriptor)
}

Expand Down Expand Up @@ -213,7 +224,12 @@ extension System.File.Destination
{
let count:Int = buffer.withUnsafeBufferPointer
{
fwrite($0.baseAddress, MemoryLayout<UInt8>.stride,
#if os(Android)
let baseAddress = $0.baseAddress!
#else
let baseAddress = $0.baseAddress
#endif
return fwrite(baseAddress, MemoryLayout<UInt8>.stride,
$0.count, self.descriptor)
}

Expand Down

0 comments on commit 4e3ed28

Please sign in to comment.