Alexander Larsson
04/16/2020, 8:06 AMcinterop
to call functions in the OpenSSL library. We already had it compiled as a .framework
for iOS (calling fron Obj-C) so we re-used now when we are trying to call from Kotlin. We have managed to build a .klib
using Gradle.
From `build.gradle.kts`:
val ios = iosArm64("ios")
val iosSim = iosX64("iosSim")
configure(listOf(iosSim, ios)) {
binaries.framework {
baseName = frameworkBaseName
}
compilations {
val main by getting {
cinterops {
val openssl by creating {
defFile("$projectDir/src/iosMain/cinterop/openssl.def")
includeDirs("$projectDir/src/iosMain/cinterop/OpenSSL-Apple/include")
}
}
}
}
Def file:
headers = openssl/asn1.h openssl/err.h openssl/pkcs12.h openssl/rsa.h openssl/x509.h
staticLibraries = openssl
libraryPaths = <beginning of absolute path>/KotlinMultiplatformTestSDK/shared/src/iosMain/cinterop/OpenSSL-Apple/frameworks/iPhone/openssl.framework
The above configuration works and produces a klib
and we can then go ahead and import it and try to make a call to OpenSSL from Kotlin:
package openssl
import interop.*
import kotlinx.cinterop.*
class TestOpenSSL {
fun TestOpenSSL_init() {
ERR_load_crypto_strings()
}
}
We get code completion and everything seems to work just fine until we build it and we get an error from the linker:
> Task :shared:linkDebugFrameworkIos
v: Using Kotlin home directory dist/kotlinc
e: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors
The /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld command returned non-zero exit code: 1.
output:
Undefined symbols for architecture arm64:
"_ERR_load_crypto_strings", referenced from:
_openssl_ERR_load_crypto_strings_wrapper662 in result.o
ld: symbol(s) not found for architecture arm64
> Task :shared:linkDebugFrameworkIos FAILED
Any ideas whats wrong here? How do we get this working? Would really appreciate any help! 🙂Artyom Degtyarev [JB]
04/16/2020, 8:43 AMbinaries.framework{
linkerOpts=mutableListOf("-framework openssl", "-Fsrc/iosMain/cinterop/OpenSSL-Apple/frameworks/iPhone")
*This is just example, cannot say if you need exactly this one.
Also maybe you should look at this GH issue, seems to be quite similar with your plan.Alexander Larsson
04/16/2020, 9:10 AM-framework
though:
> Task :shared:linkDebugFrameworkIos
v: Using Kotlin home directory dist/kotlinc
e: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors
The /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld command returned non-zero exit code: 1.
output:
ld: unknown option: -framework openssl
> Task :shared:linkDebugFrameworkIos FAILED
Alexander Larsson
04/16/2020, 9:25 AM-framework
and openssl
seems to work. Like this:
linkerOpts=mutableListOf("-framework", "openssl", "-F$projectDir/src/...")
Alexander Larsson
04/16/2020, 9:27 AMArtyom Degtyarev [JB]
04/16/2020, 9:29 AMAlexander Larsson
04/16/2020, 9:37 AMAlexander Larsson
04/16/2020, 9:46 AMlibraryPaths
in the .def
-file?Artyom Degtyarev [JB]
04/16/2020, 10:16 AMlibraryPaths
, I’m not sure this option is important at all. According to the doc, it’s about static libraries.Alexander Larsson
04/16/2020, 12:18 PMlibraryPaths
but we also had to remove staticLibraries
from the .def
-file. Our .framework
is static so that is why we used those options. Configuring this is very confusing. Any plans on more documentation? But it works now so I'm a happy coder! 😃Artyom Degtyarev [JB]
04/16/2020, 12:36 PMAlexander Larsson
04/16/2020, 12:45 PM