diff --git a/buildSrc/src/main/kotlin/Utils.kt b/buildSrc/src/main/kotlin/Utils.kt index 63b3eae..8296d1a 100644 --- a/buildSrc/src/main/kotlin/Utils.kt +++ b/buildSrc/src/main/kotlin/Utils.kt @@ -86,4 +86,14 @@ fun NamedDomainObjectContainer.createWorkaroundNativeMainSource } } +} + +fun NamedDomainObjectContainer.createWorkaround32bitNativeMainSourceSet(name : String, nativeDeps : KotlinDependencyHandler.() -> Unit) : KotlinSourceSet { + return create("${name}Workaround") { + kotlin.srcDir("src/native32Main/kotlin") + dependencies { + nativeDeps.invoke(this) + } + } + } \ No newline at end of file diff --git a/multiplatform-crypto-delegated/build.gradle.kts b/multiplatform-crypto-delegated/build.gradle.kts index 064f36e..62df81f 100644 --- a/multiplatform-crypto-delegated/build.gradle.kts +++ b/multiplatform-crypto-delegated/build.gradle.kts @@ -76,7 +76,7 @@ kotlin { } } - linuxX64("linux") { + linuxX64() { binaries { staticLib { } @@ -171,6 +171,15 @@ kotlin { optimized = true } } + compilations.getByName("main") { + val libsodiumCinterop by cinterops.creating { + defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) + compilerOpts.add("-I${project.rootDir}/sodiumWrapper/static-macos-x86-64/include/") + } + kotlinOptions.freeCompilerArgs = listOf( + "-include-binary", "${project.rootDir}/sodiumWrapper/static-macos-x86-64/lib/libsodium.a" + ) + } } tvosX64() { binaries { @@ -220,6 +229,15 @@ kotlin { optimized = true } } + compilations.getByName("main") { + val libsodiumCinterop by cinterops.creating { + defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) + compilerOpts.add("-I${project.rootDir}/sodiumWrapper/static-mingw-x86-64/include/") + } + kotlinOptions.freeCompilerArgs = listOf( + "-include-binary", "${project.rootDir}/sodiumWrapper/static-mingw-x86-64/lib/libsodium.a" + ) + } } } @@ -275,34 +293,42 @@ kotlin { } + //Set up shared source sets + //linux, linuxArm32Hfp, linuxArm64 + val linux64Bit = setOf( + "linuxX64", "linuxArm64" + ) + val linux32Bit = setOf( + "linuxArm32Hfp" + ) + //iosArm32, iosArm64, iosX64, macosX64, metadata, tvosArm64, tvosX64, watchosArm32, watchosArm64, watchosX86 + val macos64Bit = setOf( + "macosX64" + ) + val ios64Bit = setOf( + "iosArm64", "iosX64", "tvosArm64", "tvosX64", "watchosArm64" + ) + val ios32Bit = setOf( + "iosArm32", "watchosX86" + ) + val mingw64Bit = setOf( + "mingwX64" + ) targets.withType { println("Target $name") - compilations.getByName("main") { - when(this@withType.name) { - } - if ((this@withType.name.contains("ios") || this@withType.name.contains("tvos") || this@withType.name.contains("watchos")).not()) { + compilations.getByName("main") { + if (linux64Bit.contains(this@withType.name)) { defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) -// println("Setting cinterop for $this") -// if (this@withType.name.contains("arm64")) { -// val libsodiumCinterop by cinterops.creating { -// defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) -// compilerOpts.add("-I${project.rootDir}/sodiumWrapper/libsodium-arm64/include/") -// } -// kotlinOptions.freeCompilerArgs = listOf( -// "-include-binary", "${project.rootDir}/sodiumWrapper/libsodium-arm64/lib/libsodium.a" -// ) -// } else { -// val libsodiumCinterop by cinterops.creating { -// defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) -// compilerOpts.add("-I${project.rootDir}/sodiumWrapper/include/") -// } -// kotlinOptions.freeCompilerArgs = listOf( -// "-include-binary", "${project.rootDir}/sodiumWrapper/lib/libsodium.a" -// ) -// } } - if (this@withType.name.contains("ios") || this@withType.name.contains("tvos") || this@withType.name.contains("watchos")) { + if (linux32Bit.contains(this@withType.name)) { + defaultSourceSet.dependsOn(createWorkaround32bitNativeMainSourceSet(this@withType.name, nativeDependencies)) + } + if (macos64Bit.contains(this@withType.name)) { + defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) + } + //All ioses share the same static library + if (ios64Bit.contains(this@withType.name)) { defaultSourceSet.dependsOn(createWorkaroundNativeMainSourceSet(this@withType.name, nativeDependencies)) println("Setting ios cinterop for $this") val libsodiumCinterop by cinterops.creating { @@ -314,11 +340,28 @@ kotlin { ) } + if (ios32Bit.contains(this@withType.name)) { + defaultSourceSet.dependsOn(createWorkaround32bitNativeMainSourceSet(this@withType.name, nativeDependencies)) + println("Setting ios cinterop for $this") + val libsodiumCinterop by cinterops.creating { + defFile(project.file("src/nativeInterop/cinterop/libsodium.def")) + compilerOpts.add("-I${project.rootDir}/sodiumWrapper/static-ios/") + } + kotlinOptions.freeCompilerArgs = listOf( + "-include-binary", "${project.rootDir}/sodiumWrapper/static-ios/libsodium.a" + ) + } } compilations.getByName("test") { println("Setting native test dep for $this@withType.name") - defaultSourceSet.dependsOn(nativeTest) + if (linux64Bit.contains(this@withType.name) || + macos64Bit.contains(this@withType.name) || + mingw64Bit.contains(this@withType.name)) { + defaultSourceSet.dependsOn(nativeTest) + } else { + defaultSourceSet.dependsOn(native32Test) + } } @@ -360,18 +403,31 @@ kotlin { implementation(npm(Deps.Js.Npm.libsodium.first, Deps.Js.Npm.libsodium.second)) } } - val linuxMain by getting { + val linuxX64Main by getting { dependsOn(nativeMain) isRunningInIdea { kotlin.srcDir("src/nativeMain/kotlin") } } - val linuxTest by getting { + val linuxX64Test by getting { dependsOn(nativeTest) isRunningInIdea { kotlin.srcDir("src/nativeTest/kotlin") } } + + val linuxArm32HfpMain by getting { + dependsOn(native32Main) + isRunningInIdea { + kotlin.srcDir("src/native32Main/kotlin") + } + } + val linuxArm32HfpTest by getting { + dependsOn(native32Test) + isRunningInIdea { + kotlin.srcDir("src/native32Test/kotlin") + } + } } runningOnMacos { @@ -478,7 +534,7 @@ tasks { } } - val linuxTest by getting(KotlinNativeTest::class) { + val linuxX64Test by getting(KotlinNativeTest::class) { testLogging { events("PASSED", "FAILED", "SKIPPED") diff --git a/multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/Placeholder b/multiplatform-crypto-delegated/src/linuxX64Main/kotlin/com/ionspin/kotlin/crypto/Placeholder similarity index 100% rename from multiplatform-crypto-delegated/src/linuxMain/kotlin/com/ionspin/kotlin/crypto/Placeholder rename to multiplatform-crypto-delegated/src/linuxX64Main/kotlin/com/ionspin/kotlin/crypto/Placeholder diff --git a/multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/Placeholder b/multiplatform-crypto-delegated/src/linuxX64Test/kotlin/com/ionspin/kotlin/crypto/Placeholder similarity index 100% rename from multiplatform-crypto-delegated/src/linuxTest/kotlin/com/ionspin/kotlin/crypto/Placeholder rename to multiplatform-crypto-delegated/src/linuxX64Test/kotlin/com/ionspin/kotlin/crypto/Placeholder diff --git a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt index b8c3f28..a3e734f 100644 --- a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt +++ b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/SRNG.kt @@ -31,7 +31,7 @@ actual object SRNG { actual fun getRandomBytes(amount: Int): UByteArray { memScoped { val array = allocArray(amount) - randombytes_buf(array, amount.toULong()) + randombytes_buf(array, amount.toUInt()) return UByteArray(amount) { array[it] } diff --git a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt index 619ac9e..03dc5fa 100644 --- a/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt +++ b/multiplatform-crypto-delegated/src/native32Main/kotlin/com/ionspin/kotlin/crypto/hash/blake2b/Blake2bDelegated.kt @@ -38,11 +38,11 @@ actual object Blake2bStateless : Blake2bStatelessInterface { val hashResultPinned = hashResult.pin() crypto_generichash( hashResultPinned.addressOf(0), - hashLength.toULong(), + hashLength.toUInt(), inputString.encodeToByteArray().toUByteArray().toCValues(), inputString.length.toULong(), key?.run { this.encodeToByteArray().toUByteArray().toCValues() }, - key?.length?.toULong() ?: 0UL + key?.length?.toUInt() ?: 0U ) println("HashPointer: ${hashResult.toHexString()}") println(hashResult.toHexString()) @@ -54,11 +54,11 @@ actual object Blake2bStateless : Blake2bStatelessInterface { crypto_generichash( StableRef.create(hashResult).asCPointer().reinterpret(), - hashLength.toULong(), + hashLength.toUInt(), inputMessage.toCValues(), inputMessage.size.toULong(), key.toCValues(), - key.size.toULong() ?: 0UL + key.size.toUInt() ?: 0U ) println("HashPointer: ${hashResult.toHexString()}") println(hashResult.toHexString()) diff --git a/sodiumWrapper/configureLinuxArm64.sh b/sodiumWrapper/configureLinuxArm64.sh index df4f12e..2bc4d2c 100755 --- a/sodiumWrapper/configureLinuxArm64.sh +++ b/sodiumWrapper/configureLinuxArm64.sh @@ -1,9 +1,8 @@ #! /bin/sh export PREFIX="$(pwd)/static-arm64" export CC=/usr/bin/aarch64-linux-gnu-gcc -export TARGET_ARCH=armv8-a -export CFLAGS="-Os -march=${TARGET_ARCH}" -export SYSROOT="$HOME/.konan/dependencies/target-sysroot-1-linux-glibc-arm64/" +export CFLAGS="-O3" +export SYSROOT=$HOME/.konan/dependencies/target-sysroot-1-linux-glibc-arm64/ cd libsodium -./configure --prefix=$PREFIX --host=aarch64-linux-gnu-gcc --with-sysroot=$SYSROOT "$@" +./configure --prefix=$PREFIX --host=aarch64-linux-gnu-gcc --with-sysroot=$SYSROOT "$@"