Has anyone successfully used kotlin to generate a ...
# kotlin-native
r
Has anyone successfully used kotlin to generate a native binary that they then create bindings for in python? Asking b/c I am attempting to do so using CFFI, unfortunately, the folllowing code results in an exception being thrown where cffi does not know how to parse the directives attached by kotlin
Copy code
import os

import cffi

ffi = cffi.FFI()
header_file = (
    "path/to/lib.h"
)

if __name__ == "__main__":
    with open(os.path.join(header_file)) as f:
        ffi.cdef(f.read())
Copy code
cffi.CDefError: cannot parse "#ifndef KONAN_LIBRQSWELL_H"
<cdef source string>:1:1: Directives not supported yet
k
It might be worthwhile to look at Kotlin’s jupyter notebook kernel. I wonder if that has some Kotlin/Python FFI
l
Looks like the cffi doesn't support directives (as the error says). I would generate a header for a small hello, world program and manually handle directives once as a test, then if it works, I believe gcc has a way to get the header after pre-processing.
That's kind of rough to not support directives, though. I believe the one you're seeing is a header guard, which most headers should have.
m
If you don't care much which KMP platform you target, you could use GraalVM + GraalPy, which gives you a Python3 environment that runs on the JVM and makes interop very easy.
r
hmm if i'm understanding, i don't think that will work. I don't need to bring python libs into my kotlin code, I need my kotlin code to compile to a library that my python code could use
m
It works both ways. You can run Python apps via GraalPy and then call into Kotlin/Java/other languages too.
GraalPy is an alternative interpreter. You can just install it and use it as if it were a regular python
r
oh i see... that's neat. unfortunately my company runs a pretty huge (antiquated) python monolith, there's no way I'm gonna convince them to swap out the interpreter 🥲 will have to find a different way
m
You could also experiment then with https://www.py4j.org/
Writing native Python extension is a pain, even if you use an FFI and do all the work python-side. If you can, it's definitely best to use a generic adapter that bridges memory management and types for you to the jvm