Skip to content

Commit 6272016

Browse files
committed
ios armv7 support
1 parent 8f3b351 commit 6272016

File tree

3 files changed

+47
-20
lines changed

3 files changed

+47
-20
lines changed

build.gradle

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFrameworkConfig
22
buildscript {
3+
ext.kotlin_version = '1.6.20-RC'
34
repositories {
45
mavenCentral()
56
google()
67
jcenter()
78
}
89
dependencies {
910
classpath 'com.android.tools.build:gradle:3.6.3'
11+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1012
}
1113
}
1214

@@ -29,6 +31,7 @@ repositories {
2931
}
3032

3133
apply plugin: 'com.android.library'
34+
//apply plugin: 'kotlin-android'
3235

3336
apply plugin: 'maven-publish'
3437

@@ -75,6 +78,7 @@ android { // android specific configuration
7578
}
7679

7780
dependencies {
81+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
7882
//androidTestImplementation 'com.android.support.test:runner:1.0.2'
7983
}
8084

@@ -102,6 +106,15 @@ kotlin {
102106
}
103107
}
104108

109+
iosArm32("iosArmv7") {
110+
binaries {
111+
framework {
112+
baseName = "libpebblecommon"
113+
xcf.add(it)
114+
}
115+
}
116+
}
117+
105118
// use the android preset
106119
android {
107120
publishLibraryVariants("release", "debug")
@@ -200,6 +213,11 @@ project.afterEvaluate {
200213
!publication.name.contains("ios")
201214
}
202215
}
216+
tasks.withType(Jar) {
217+
onlyIf {
218+
!name.contains("ios")
219+
}
220+
}
203221
}
204222

205223
/*task iosTest {

src/iosMain/kotlin/util/DataBuffer.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package io.rebble.libpebblecommon.util
22

33
import kotlinx.cinterop.*
44
import platform.Foundation.*
5-
import platform.darwin.UInt8Var
65

76
actual class DataBuffer {
87
private val actualBuf: NSMutableData
@@ -23,14 +22,14 @@ actual class DataBuffer {
2322
get() = _readPosition
2423

2524
actual constructor(size: Int) {
26-
actualBuf = NSMutableData.dataWithCapacity(size.toULong())!!
25+
actualBuf = NSMutableData.dataWithCapacity(castToNativeSize(size))!!
2726
}
2827

2928
actual constructor(bytes: UByteArray) {
3029
actualBuf = NSMutableData()
3130
memScoped {
3231
actualBuf.setData(
33-
NSData.create(bytes = allocArrayOf(bytes.asByteArray()), length = bytes.size.toULong())
32+
NSData.create(bytes = allocArrayOf(bytes.asByteArray()), length = castToNativeSize(bytes.size))
3433
)
3534
}
3635
}
@@ -47,13 +46,13 @@ actual class DataBuffer {
4746
memScoped {
4847
val pShort = alloc<UShortVar>()
4948
pShort.value = if (shouldReverse()) reverseOrd(short) else short
50-
actualBuf.appendBytes(pShort.ptr, UShort.SIZE_BYTES.toULong())
49+
actualBuf.appendBytes(pShort.ptr, castToNativeSize(UShort.SIZE_BYTES))
5150
}
5251
}
5352
actual fun getUShort(): UShort {
5453
memScoped {
5554
val pShort = alloc<UShortVar>()
56-
actualBuf.getBytes(pShort.ptr, NSMakeRange(_readPosition.toULong(), UShort.SIZE_BYTES.toULong()))
55+
actualBuf.getBytes(pShort.ptr, NSMakeRange(castToNativeSize(_readPosition), castToNativeSize(UShort.SIZE_BYTES)))
5756
_readPosition += UShort.SIZE_BYTES
5857
return if (shouldReverse()) reverseOrd(pShort.value) else pShort.value
5958
}
@@ -63,13 +62,13 @@ actual class DataBuffer {
6362
memScoped {
6463
val pShort = alloc<ShortVar>()
6564
pShort.value = if (shouldReverse()) reverseOrd(short.toUShort()).toShort() else short
66-
actualBuf.appendBytes(pShort.ptr, Short.SIZE_BYTES.toULong())
65+
actualBuf.appendBytes(pShort.ptr, castToNativeSize(Short.SIZE_BYTES))
6766
}
6867
}
6968
actual fun getShort(): Short {
7069
memScoped {
7170
val pShort = alloc<ShortVar>()
72-
actualBuf.getBytes(pShort.ptr, NSMakeRange(_readPosition.toULong(), Short.SIZE_BYTES.toULong()))
71+
actualBuf.getBytes(pShort.ptr, NSMakeRange(castToNativeSize(_readPosition), castToNativeSize(Short.SIZE_BYTES)))
7372
_readPosition += Short.SIZE_BYTES
7473
return if (shouldReverse()) reverseOrd(pShort.value.toUShort()).toShort() else pShort.value
7574
}
@@ -79,13 +78,13 @@ actual class DataBuffer {
7978
memScoped {
8079
val pByte = alloc<UByteVar>()
8180
pByte.value = byte
82-
actualBuf.appendBytes(pByte.ptr, UByte.SIZE_BYTES.toULong())
81+
actualBuf.appendBytes(pByte.ptr, castToNativeSize(UByte.SIZE_BYTES))
8382
}
8483
}
8584
actual fun getUByte(): UByte {
8685
memScoped {
8786
val pByte = alloc<UByteVar>()
88-
actualBuf.getBytes(pByte.ptr, NSMakeRange(_readPosition.toULong(), UByte.SIZE_BYTES.toULong()))
87+
actualBuf.getBytes(pByte.ptr, NSMakeRange(castToNativeSize(_readPosition), castToNativeSize(UByte.SIZE_BYTES)))
8988
_readPosition += UByte.SIZE_BYTES
9089
return pByte.value
9190
}
@@ -95,13 +94,13 @@ actual class DataBuffer {
9594
memScoped {
9695
val pByte = alloc<ByteVar>()
9796
pByte.value = byte
98-
actualBuf.appendBytes(pByte.ptr, Byte.SIZE_BYTES.toULong())
97+
actualBuf.appendBytes(pByte.ptr, castToNativeSize(Byte.SIZE_BYTES))
9998
}
10099
}
101100
actual fun getByte(): Byte {
102101
memScoped {
103102
val pByte = alloc<ByteVar>()
104-
actualBuf.getBytes(pByte.ptr, NSMakeRange(_readPosition.toULong(), Byte.SIZE_BYTES.toULong()))
103+
actualBuf.getBytes(pByte.ptr, NSMakeRange(castToNativeSize(_readPosition), castToNativeSize(Byte.SIZE_BYTES)))
105104
_readPosition += Byte.SIZE_BYTES
106105
return pByte.value
107106
}
@@ -110,13 +109,13 @@ actual class DataBuffer {
110109
actual fun putBytes(bytes: UByteArray) {
111110
memScoped {
112111
val pBytes = allocArrayOf(bytes.toByteArray())
113-
actualBuf.appendBytes(pBytes, bytes.size.toULong())
112+
actualBuf.appendBytes(pBytes, castToNativeSize(bytes.size))
114113
}
115114
}
116115
actual fun getBytes(count: Int): UByteArray {
117116
memScoped {
118117
val pBytes = allocArray<UByteVar>(count)
119-
actualBuf.getBytes(pBytes.getPointer(this), NSMakeRange(_readPosition.toULong(), count.toULong()))
118+
actualBuf.getBytes(pBytes.getPointer(this), NSMakeRange(castToNativeSize(_readPosition), castToNativeSize(count)))
120119
_readPosition += count
121120
return pBytes.readBytes(count).toUByteArray()
122121
}
@@ -132,13 +131,13 @@ actual class DataBuffer {
132131
memScoped {
133132
val pUInt = alloc<UIntVar>()
134133
pUInt.value = if (shouldReverse()) reverseOrd(uint) else uint
135-
actualBuf.appendBytes(pUInt.ptr, UInt.SIZE_BYTES.toULong())
134+
actualBuf.appendBytes(pUInt.ptr, castToNativeSize(UInt.SIZE_BYTES))
136135
}
137136
}
138137
actual fun getUInt(): UInt {
139138
memScoped {
140139
val pUInt = alloc<UIntVar>()
141-
actualBuf.getBytes(pUInt.ptr, NSMakeRange(_readPosition.toULong(), UInt.SIZE_BYTES.toULong()))
140+
actualBuf.getBytes(pUInt.ptr, NSMakeRange(castToNativeSize(_readPosition), castToNativeSize(UInt.SIZE_BYTES)))
142141
_readPosition += UInt.SIZE_BYTES
143142
return if (shouldReverse()) reverseOrd(pUInt.value) else pUInt.value
144143
}
@@ -148,13 +147,13 @@ actual class DataBuffer {
148147
memScoped {
149148
val pInt = alloc<IntVar>()
150149
pInt.value = if (shouldReverse()) reverseOrd(int.toUInt()).toInt() else int
151-
actualBuf.appendBytes(pInt.ptr, Int.SIZE_BYTES.toULong())
150+
actualBuf.appendBytes(pInt.ptr, castToNativeSize(Int.SIZE_BYTES))
152151
}
153152
}
154153
actual fun getInt(): Int {
155154
memScoped {
156155
val pInt = alloc<IntVar>()
157-
actualBuf.getBytes(pInt.ptr, NSMakeRange(_readPosition.toULong(), Int.SIZE_BYTES.toULong()))
156+
actualBuf.getBytes(pInt.ptr, NSMakeRange(castToNativeSize(_readPosition), castToNativeSize(Int.SIZE_BYTES)))
158157
_readPosition += Int.SIZE_BYTES
159158
return if (shouldReverse()) reverseOrd(pInt.value.toUInt()).toInt() else pInt.value
160159
}
@@ -164,13 +163,13 @@ actual class DataBuffer {
164163
memScoped {
165164
val pULong = alloc<ULongVar>()
166165
pULong.value = if (shouldReverse()) reverseOrd(ulong) else ulong
167-
actualBuf.appendBytes(pULong.ptr, ULong.SIZE_BYTES.toULong())
166+
actualBuf.appendBytes(pULong.ptr, castToNativeSize(ULong.SIZE_BYTES))
168167
}
169168
}
170169
actual fun getULong(): ULong {
171170
memScoped {
172171
val pULong = alloc<ULongVar>()
173-
actualBuf.getBytes(pULong.ptr, NSMakeRange(_readPosition.toULong(), ULong.SIZE_BYTES.toULong()))
172+
actualBuf.getBytes(pULong.ptr, NSMakeRange(castToNativeSize(_readPosition), castToNativeSize(ULong.SIZE_BYTES)))
174173
_readPosition += ULong.SIZE_BYTES
175174
return if (shouldReverse()) reverseOrd(pULong.value) else pULong.value
176175
}

src/iosMain/kotlin/util/UtilFunctions.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import platform.Foundation.create
77
import platform.darwin.UInt8
88
import platform.darwin.UInt8Var
99
import platform.posix.memcpy
10+
import platform.posix.size_t
1011
import kotlin.native.internal.NativePtr
1112

1213
actual fun runBlocking(block: suspend () -> Unit) = kotlinx.coroutines.runBlocking{block()}
@@ -27,7 +28,7 @@ internal fun reverseOrd(varr: UInt): UInt = ((reverseOrd((varr and 0xffffu).toUS
2728
internal fun reverseOrd(varr: ULong): ULong = ((reverseOrd((varr and 0xffffffffu).toUInt()).toLong() shl 32) or (reverseOrd((varr shr 32).toUInt()).toLong() and 0xffffffff)).toULong()
2829

2930
fun ByteArray.toNative(): NSData = memScoped {
30-
NSData.create(bytes = allocArrayOf(this@toNative), length = this@toNative.size.toULong())
31+
NSData.create(bytes = allocArrayOf(this@toNative), length = castToNativeSize(this@toNative.size))
3132
}
3233

3334
fun KUtil.byteArrayFromNative(arr: NSData): ByteArray = ByteArray(arr.length.toInt()).apply {
@@ -36,4 +37,13 @@ fun KUtil.byteArrayFromNative(arr: NSData): ByteArray = ByteArray(arr.length.toI
3637
memcpy(it.addressOf(0), arr.bytes, arr.length)
3738
}
3839
}
40+
}
41+
42+
internal fun castToNativeSize(v: Int): size_t {
43+
@Suppress("CAST_NEVER_SUCCEEDS", "USELESS_CAST") // Depending on the platform different side of if will trigger, lets ignore
44+
return if (size_t.SIZE_BITS == 32) {
45+
v.toUInt() as size_t
46+
}else {
47+
v.toULong() as size_t
48+
}
3949
}

0 commit comments

Comments
 (0)