-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate_cython_setup.py
99 lines (77 loc) · 2.75 KB
/
template_cython_setup.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
85
86
87
88
89
90
91
92
93
94
95
"""XZCython
Cython:
-> interface: *.pxd # not allows pure python `def`s
-> wrapper, *.pyx & *.py # same name `interface` will be automatically searched
After compiled:
-> linux: *.so
-> windows: *.pyd
Note:
`wrapper` should be different name with `interface`,
otherwise, signatures inside wrapper will be used instead
Compiler directives:
https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#compiler-directives
Some important directives:
# distutils: language = C++
# distutils: libraries = lib1 lib2
# distutils: include_dirs = dir1 dir2
# distutils: sources = s.c s.cpp
# cython: language_level = 3 # python version, no need
#link: https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html
Decorators:
@cython.exceptval(check=True)
@cython.cfunc # create `cdef` function
@cython.cclass # create `cdef class`
@cython.ccall # create `cpdef` function
@cython.locals # local variables
@cython.inline # equivalent with C inline
@cython.returns
@cython.profile
@cython.declare
Keywords:
`cdef` : used for internal C functions
`cpdef` : visible to Python
cdef class cls
cdef [public|inline] [int|str|bint] var
cdef [struct|union|enum] block
DEF key value # work like macros in compiling time
Compile Process:
1) generate C/C++ source codes:
cython source.pyx # new file: `source.c` or `source.cpp`
2) compile:
gcc -pthread -B /home/xiang/Applications/miniconda3/compiler_compat \
-Wsign-compare -g -fwrapv -O3 -Wall -fPIC \
-I/home/xiang/Applications/miniconda3/include/python3.8 \
-I/all/my/includes \
-c source.cpp -o source.o
g++ -pthread -B /home/xiang/Applications/miniconda3/compiler_compat -shared \
-L/home/xiang/Applications/miniconda3/lib \
-Wl,-rpath=/home/xiang/Applications/miniconda3/lib \
-I/all/my/includes \
allmyobjs.o -o final.so
"""
from setuptools import Extension, setup
from Cython.Build import cythonize
setup(
name='obj',
ext_modules = cythonize([Extension("queue", ["queue.pyx"])])
)
"""Files
# interface file: cqueue.pxd
# (selectively copy-and-paste of header file)
cdef extern from "queue.h":
ctypedef struct Queue:
pass
Queue* queue_new()
# wrapper file: queue.pyx
# (following compiler directives should be defined)
# distutils: sources = path/to/source/queue.c
# distutils: include_dirs = path/to/source/
cimport cqueue
cdef func(int a):
pass
cdef class Temp:
def __cinit__(self):
pass
def __dealloc__(self):
pass
"""