-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_pynng.py
84 lines (71 loc) · 2.88 KB
/
build_pynng.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# build the pynng interface.
#
# This script assumes the nng library has already been built; the setup.py
# script should ensure that it is built before running. It looks in this file
# to see what the expected object file is based on the platform.
from cffi import FFI
import os
import sys
ffibuilder = FFI()
if sys.platform == 'win32':
objects = ['./nng/build/Release/nng.lib']
mbedtls_dir = './mbedtls/build/library/Release'
objects += [
mbedtls_dir + "/mbedtls.lib",
mbedtls_dir + "/mbedx509.lib",
mbedtls_dir + "/mbedcrypto.lib",
]
# system libraries determined to be necessary through trial and error
libraries = ['Ws2_32', 'Advapi32']
else:
objects = ['./nng/build/libnng.a', "./mbedtls/prefix/lib/libmbedtls.a",
"./mbedtls/prefix/lib/libmbedx509.a", "./mbedtls/prefix/lib/libmbedcrypto.a",
"/usr/local/lib/libmsquic.so", "nng/build/_deps/opensslquic-build/openssl/lib/libcrypto.a",
"nng/build/_deps/opensslquic-build/openssl/lib/libssl.a"]
libraries = ['pthread']
machine = os.uname().machine
# this is a pretty heuristic... but let's go with it anyway.
# it would be better to get linker information from cmake somehow.
if not ('x86' in machine or 'i386' in machine or 'i686' in machine):
libraries.append('atomic')
ffibuilder.set_source(
"pynng._nng",
r""" // passed to the real C compiler,
// contains implementation of things declared in cdef()
#define NNG_DECL
#define NNG_STATIC_LIB
#include <nng/nng.h>
#include <nng/protocol/bus0/bus.h>
#include <nng/protocol/pair0/pair.h>
#include <nng/protocol/pair1/pair.h>
#include <nng/protocol/pipeline0/pull.h>
#include <nng/protocol/pipeline0/push.h>
#include <nng/protocol/pubsub0/pub.h>
#include <nng/protocol/pubsub0/sub.h>
#include <nng/protocol/reqrep0/req.h>
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/survey0/respond.h>
#include <nng/protocol/survey0/survey.h>
#include <nng/supplemental/tls/tls.h>
#include <nng/transport/tls/tls.h>
#include <nng/mqtt/mqtt_client.h>
#include <nng/mqtt/mqtt_quic.h>
""",
libraries=libraries,
# library_dirs=['nng/build/Debug',],
# (more arguments like setup.py's Extension class:
include_dirs=['nng/include'],
extra_objects=objects,
)
with open('nng_api.h') as f:
api = f.read()
callbacks = """
// aio callback: https://nanomsg.github.io/nng/man/tip/nng_aio_alloc.3
extern "Python" void _async_complete(void *);
// nng_pipe_notify callback:
// https://nanomsg.github.io/nng/man/tip/nng_pipe_notify.3
extern "Python" void _nng_pipe_cb(nng_pipe, nng_pipe_ev, void *);
"""
ffibuilder.cdef(api + callbacks)
if __name__ == "__main__":
ffibuilder.compile(verbose=True)