-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdss_build.py
182 lines (149 loc) · 6.3 KB
/
dss_build.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from cffi import FFI
import sys, re, os
from dss_setup_common import PLATFORM_FOLDER
def process_header(src, extern_py=False, implement_py=False, prefix=''):
'''Prepare the DSS C-API headers for parsing and building with CFFI'''
call_convention = '__stdcall ' if (sys.platform == 'win32') else ''
src = src.replace('dss_long_bool', 'int32_t')
src = re.sub('^.*namespace dss.*$', '', src, flags=re.MULTILINE)
if not implement_py:
src = re.sub('^extern .*', '', src, flags=re.MULTILINE)
src = re.sub('^.*extern .*$', '', src, flags=re.MULTILINE)
src = re.sub('^#.*', '', src, flags=re.MULTILINE)
src = re.sub('DSS_CAPI_.*DLL', '', src)
src = re.sub(
r'DSS_MODEL_CALLBACK\(([^,]+), ([^\)]+)\)',
r'\1 ({call_convention}*\2)'.format(call_convention=call_convention),
src
)
if extern_py:
src = re.sub(
r'DSS_MODEL_DLL\(([^\)]+)\) ',
r'extern "Python" \1 {prefix}'.format(prefix=prefix),
src
)
elif implement_py:
# Extract the parameters from the definitions and implement a
# simple redirect to the Python-defined functions
out_lines = []
for line in src.split('\n'):
if 'DSS_MODEL_DLL' not in line:
out_lines.append(line)
continue
match = re.match(r'DSS_MODEL_DLL\((.*)\) (\w+)\((.*)\);', line)
rtrn = 'return ' if match.group(1) != 'void' else ''
name = match.group(2)
if match.group(3) == 'void':
params = []
else:
params = [
p.split(' ')[-1].strip('*')
for p in match.group(3).split(',')
]
out_lines.append(re.sub(
r'DSS_MODEL_DLL\(([^\)]+)\) \w+\((.*)\);',
r'''
static \1 {prefix}{name}(\2);
DSS_MODEL_DLL(\1) {name}(\2)
{{
{rtrn}{prefix}{name}({params});
}}
'''.format(rtrn=rtrn, name=name, prefix=prefix, params=', '.join(params)),
line
))
src = '\n'.join(out_lines)
return src
extra = {}
# This ensures the shared libraries in the module directory can be
# loaded without changing LD_LIBRARY_PATH.
if sys.platform == 'linux':
extra['extra_link_args'] = ["-Wl,-R,$ORIGIN/."]
ffi_builders = {}
src_path = os.environ.get('SRC_DIR', '')
DSS_CAPI_PATH = os.environ.get('DSS_CAPI_PATH', os.path.join(src_path, '..', 'dss_capi'))
for version in ('', 'd'):
ffi_builder_dss = FFI()
debug = 'd' if version.endswith('d') else ''
main_header_fn = os.path.join(DSS_CAPI_PATH, 'include', 'dss_capi.h')
dss_capi_ctx_path = os.path.join(DSS_CAPI_PATH, 'include', 'dss_capi_ctx.h')
headers = [main_header_fn, dss_capi_ctx_path]
# Temporary fixes for DSS C-API headers
for fn in headers:
with open(fn, 'r') as f:
patched = (f.read().
replace(' ctx_Fuses_Reset(void* ctx, int32_t Value)', ' ctx_Fuses_Reset(void* ctx)').
replace(' Fuses_Reset(int32_t Value)', ' Fuses_Reset(void)')
)
with open(fn, 'w') as f:
f.write(patched)
with open(main_header_fn, 'r') as f:
cffi_header_dss = process_header(f.read())
if os.path.exists(dss_capi_ctx_path):
with open(dss_capi_ctx_path, 'r') as f:
cffi_header_dss += process_header(f.read())
with open('cffi/dss_capi_custom.h', 'r') as f:
extra_header_dss = f.read()
cffi_header_dss += extra_header_dss
with open('cffi/dss_capi_custom.c', 'r') as f:
if os.path.exists(dss_capi_ctx_path):
extra_source_dss = '#include <dss_capi_ctx.h>\n'
extra_source_dss += f.read()
else:
extra_source_dss = f.read()
ffi_builder_dss.cdef(cffi_header_dss)
ffi_builder_dss.set_source("_dss_capi{}".format(debug), extra_source_dss,
libraries=["dss_capi{}".format(debug)],
library_dirs=[
os.path.join(DSS_CAPI_PATH, 'lib/{}'.format(PLATFORM_FOLDER))
],
include_dirs=[os.path.join(DSS_CAPI_PATH, 'include')],
source_extension='.c',
**extra
)
ffi_builders[version] = ffi_builder_dss
# User-model modules/DLLs
# Currently we build a separate DLL for each kind of model,
# since some functions are different. Some could probably be
# merged in a single DLL, but there's not much benefit.
with open(os.path.join(DSS_CAPI_PATH, 'include/dss_UserModels.h'), 'r') as f:
cffi_header_um = process_header(f.read())
user_models = [
'GenUserModel',
# 'PVSystemUserModel',
# 'StoreDynaModel',
# 'StoreUserModel',
# 'CapUserControl',
]
for user_model in user_models:
with open(os.path.join(DSS_CAPI_PATH, 'include', 'dss_{}.h'.format(user_model)), 'r') as f:
func_def = f.read()
prefix = "py{}_".format(user_model)
user_model_def = process_header(func_def, extern_py=True, prefix=prefix)
user_model_src = process_header(func_def, implement_py=True, prefix=prefix)
ffi_builder = FFI()
ffi_builder.cdef(cffi_header_um + user_model_def, packed=True)
ffi_builder.set_source("_dss_{}".format(user_model), user_model_src,
libraries=[],
library_dirs=[],
include_dirs=[os.path.join(DSS_CAPI_PATH, 'include')],
source_extension='.c',
#extra_compile_args=['/DYNAMICBASE:NO'],
#extra_link_args=['/DYNAMICBASE:NO', '/NXCOMPAT:NO']
)
ffi_builders[user_model] = ffi_builder
# Is there a better way to do this? Unfortunately setup(cffi_modules=...)
# needs a list of strings and cannot handle objects directly
ffi_builder_ = ffi_builders['']
ffi_builder_d = ffi_builders['d']
ffi_builder_GenUserModel = ffi_builders['GenUserModel']
#ffi_builder_PVSystemUserModel = ffi_builders['PVSystemUserModel']
#ffi_builder_StoreDynaModel = ffi_builders['StoreDynaModel']
#ffi_builder_StoreUserModel = ffi_builders['StoreUserModel']
#ffi_builder_CapUserControl = ffi_builders['CapUserControl']
if __name__ == "__main__":
for version, builder in ffi_builders.items():
print('-' * 40)
print('Building', version)
print('-' * 40)
builder.compile(verbose=True)
print()