Windows Python extension docs.

This commit is contained in:
Nikolay Igotti
2017-12-12 19:29:30 +03:00
parent 0e89083a8d
commit 82755af754
3 changed files with 30 additions and 7 deletions
+5 -4
View File
@@ -3,16 +3,17 @@
This example shows how to use Kotlin/Native programs from other execution environments, such as Python.
Python has C native interface, which could be used to organize a bridge with the
Kotlin/Native application. File `kotlin_bridge.c` contains translation code between Kotlin and Python
lands.
lands. This demo works on Linux, Windows (64-bit) or macOS hosts.
To build and run the sample do the following:
* Install Python with development headers, i.e.
* Install Python with development headers, i.e. on Linux
```
sudo apt install python-dev
```
* Run `build.sh`, it will ask for superuser password to install Python extension
* Run `build.sh`, it will ask for superuser password to install Python extension.
Use build.bat on Windows.
* On macOS copy Kotlin binary to extension's directory and change install name with
`install_name_tool` tool, i.e.
```
@@ -39,7 +40,7 @@ To build and run the sample do the following:
The example works as following. Kotlin/Native API is implemented in `Server.kt`, and we run Kotlin/Native compiler
with `-produce dynamic` option. Compiler produces two artifacts: `server_api.h` which is C language API
to all public functions and classes available in the application. `libserver.dylib` or `libserver.so`
to all public functions and classes available in the application. `libserver.dylib` or `libserver.so` or `server.dll`
shared object contains C bridge to all above APIs.
This C bridge looks like a C struct, reflecting all scopes in program, with operations available. For example,
+16
View File
@@ -0,0 +1,16 @@
setlocal
set DIR=.
if defined KONAN_HOME (
set "PATH=%KONAN_HOME%\bin;%PATH%"
) else (
set "PATH=..\..\dist\bin;..\..\bin;%PATH%"
)
konanc -p dynamic src/main/kotlin/Server.kt -o server
rem Prepare MSVC build environment, and .lib file for linking with our .dll.
SET VS90COMNTOOLS=%VS140COMNTOOLS%
\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\lib.exe" /def:server.def /out:server.lib /machine:X64
python src/main/python/setup.py install
@@ -23,7 +23,13 @@
// Note, that as we cache this in the global, and Kotlin/Native object references
// are currently thread local, we make this global a TLS variable.
static __thread server_kref_demo_Server server = { 0 };
#ifdef _MSC_VER
#define TLSVAR __declspec(thread)
#else
#define TLSVAR __thread
#endif
static TLSVAR server_kref_demo_Server server = { 0 };
static T_(Server) getServer(void) {
if (!server.pinned) {
@@ -32,7 +38,7 @@ static T_(Server) getServer(void) {
return server;
}
static server_kref_demo_Session getSession(PyObject* args) {
static T_(Session) getSession(PyObject* args) {
T_(Session) result = { 0 };
long long pinned;
if (PyArg_ParseTuple(args, "L", &pinned)) {
@@ -46,7 +52,7 @@ static PyObject* open_session(PyObject* self, PyObject* args) {
char* string_arg = NULL;
int int_arg = 0;
if (PyArg_ParseTuple(args, "is", &int_arg, &string_arg)) {
server_kref_demo_Session session = __ kotlin.demo.Session.Session(string_arg, int_arg);
T_(Session) session = __ kotlin.demo.Session.Session(string_arg, int_arg);
result = Py_BuildValue("L", session.pinned);
}
return result;