Repository navigation
Expand file tree
/
Copy pathSConstruct
More file actions
251 lines (220 loc) · 9.61 KB
/
Copy pathSConstruct
File metadata and controls
251 lines (220 loc) · 9.61 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import os, glob, sysconfig, sys, yaml, shlex, stat
DEFW_PATH = Dir('.').abspath
LIBDEFW_SRC_FILES = glob.glob(os.path.join(DEFW_PATH, "src", "libdefw*.c"))
DEFW_FWSL_FILES = glob.glob(os.path.join(DEFW_PATH, "src", "*.c"))
DEFW_FWSL_FILES = [entry for entry in DEFW_FWSL_FILES if entry not in LIBDEFW_SRC_FILES \
and 'defw.c' != os.path.basename(entry) and '_wrap' not in entry]
DEFW_EXTERNAL_LIBRARIES = []
env = Environment()
def generate_include_paths():
import subprocess
try:
result = subprocess.check_output(
"gcc -xc -E -v /dev/null 2>&1 | grep '^ /' | tr -d ' '",
shell=True, text=True).strip()
include_paths = result.split("\n") if result else []
except subprocess.CalledProcessError as e:
print("Error running command:", e)
string = ''
for i in include_paths:
string += f"-I{i} "
return string
def generate_swig_intf(source, env):
cmd = env['PYTHON'] + " " + env['GEN_SWIG_INTF'] + " " + source
print(cmd)
os.system(cmd)
return os.path.splitext(source)[0]+'.i'
def swigify(ifile, target_lib, link_libs, include_path, lib_path, env):
cmd = env['SWIG'] + " -threads -python -includeall " + env['SWIG_FLAGS'] + " " + env['SWIG_INCLUDES'] + " " + include_path + " " + generate_include_paths() + " " + ifile
os.system(cmd)
wfile = os.path.splitext(ifile)[0]+"_wrap.c"
cmd = env['CC'] + " " + env['SWIG_COMP_FLAGS'] + " -I" + env['PYTHON_INCLUDE_DIR'] + \
" " + include_path + " " + lib_path + \
" -shared " + "-o " + target_lib + \
" -L" + env['LINK_PATH'] + " " + " ".join(link_libs) + \
" -L" + env['PYTHON_LIB_DIR'] + " -l" + env['PYTHON_LIB'] + " " + wfile
print(cmd)
os.system(cmd)
def build_shared_library(env):
for cfile in env['LIBDEFW_SRC_FILES']:
so = os.path.splitext(cfile)[0]+".so"
cmd = env['CC'] + " " + env['SWIG_COMP_FLAGS'] + " -shared -luuid -o " + so + " " + cfile
print(cmd)
os.system(cmd)
def mbuild_shared_library(env, files, so):
print("building shared library from ", " ".join(files))
cmd = env['CC'] + " " + env['SWIG_COMP_FLAGS'] + \
" -I" + env['PYTHON_INCLUDE_DIR'] + \
" -fPIC -shared -luuid -o " + so + " " + \
" ".join(files)
print(cmd)
os.system(cmd)
def swigify_all_files(env):
swgs = {}
for swg in env['DEFW_SWG_FILES']:
swgs[swg] = ["-l"+os.path.splitext(os.path.basename(swg))[0]]
if "_agent" in swg:
swgs[swg].append("-ldefw_connect")
for k, v in swgs.items():
ifile = generate_swig_intf(k, env)
base_name = os.path.basename(ifile)
lib_name = os.path.join(env['DEFW_PATH'], "src", "_c"+os.path.splitext(base_name)[0]+".so")
swigify(ifile, lib_name, v, '', '', env)
def cleanup_external_files(env):
cy = read_configuration_file(env)
if not cy:
return
swigify_info = cy['defw']['swigify']
for entry in swigify_info:
cleanup_name = os.path.join(env['DEFW_PATH'], "src", "*"+entry['name']+"*")
clean_up_cmd = f"rm -Rf {cleanup_name}"
print(clean_up_cmd)
os.system(clean_up_cmd)
def read_configuration_file(env):
cfg = ARGUMENTS.get('CONFIG', '')
if not cfg:
return None
with open(cfg, 'r') as f:
cy = yaml.load(f, Loader=yaml.FullLoader)
if 'defw' not in cy or 'swigify' not in cy['defw']:
print(f"Badly formed configuration file {cfg}")
return None
return cy
def swigify_externals(env):
cy = read_configuration_file(env)
if not cy:
return
swigify_info = cy['defw']['swigify']
for entry in swigify_info:
path = entry['path']
if 'files' in entry:
files = entry['files']
else:
files = glob.glob(os.path.join(path, "*.h"))
swg_name = os.path.join(env['DEFW_PATH'], "src", entry['name']+".swg")
with open(swg_name, 'w') as f:
f.write(f"%module c{entry['name']}\n")
f.write('%include "cwstring.i"\n')
f.write('%rename("%(strip:[__])s", regexmatch$name="__.*") "";\n')
f.write("%{\n")
if 'addendums' in entry:
for addendum in entry['addendums']:
with open(addendum, 'r') as a:
f.write(a.read())
for file in files:
f.write(f'#include "{file}"\n')
f.write("%}\n")
if 'typemaps' in entry:
for typemap in entry['typemaps']:
with open(typemap, 'r') as t:
f.write(t.read())
f.write("typedef long long ssize_t;\n")
f.write("typedef unsigned long long uint64_t;\n")
f.write("typedef unsigned int uint32_t;\n")
f.write("typedef unsigned short uint16_t;\n")
f.write("typedef unsigned char uint8_t;\n")
for ignore in entry['ignore']:
f.write(f"%ignore {ignore};\n")
for file in files:
f.write(f'%include "{file}"\n')
import subprocess
try:
result = subprocess.check_output(["pkg-config", "--variable=prefix",
entry['name']], text=True).strip()
except subprocess.CalledProcessError as e:
print("Error running pkg-config:", e)
include_path = "-I"+os.path.join(result, 'include')
lib_path = "-L"+os.path.join(result, 'lib')
ifile = generate_swig_intf(swg_name, env)
base_name = os.path.basename(ifile)
lib_name = os.path.join(env['DEFW_PATH'], "src", "_c"+os.path.splitext(base_name)[0]+".so")
libs = []
for lib in entry['libs']:
libs.append(f"-l{lib}")
swigify(ifile, lib_name, libs, include_path, lib_path, env)
def install(env, src, dst):
#cmd = "cp " + os.path.join(src, "*.so") + " " + dst
#print(cmd)
#os.system(cmd)
#cmd = "cp " + os.path.join(src, "*.py") + " " + dst
#print(cmd)
#os.system(cmd)
return
def build_bin(env):
binary = env['DEFW_MAIN_C']
path = os.path.join(env['DEFW_PATH'], "src", "defwp")
print("Building standard bin")
cmd = env['CC'] + " " + env['SWIG_COMP_FLAGS'] + \
" -I" + env['PYTHON_INCLUDE_DIR'] + " " + \
binary + " -L" + env['LINK_PATH'] + " -L" + env['PYTHON_LIB_DIR'] + \
" -lfwsl -ldefw_global -ldefw_connect -ldefw_agent -luuid -l" + env['PYTHON_LIB'] + \
" -o " + path
print(cmd)
os.system(cmd)
def build_defwp_wrapper(env):
path = os.path.join(env['DEFW_PATH'], "src", "defwp-wrapper")
template_path = os.path.join(env['DEFW_PATH'], "src", "defwp-wrapper.in")
python_lib_dir = env['PYTHON_LIB_DIR'] or ""
python_version = sys.version.split()[0].strip()
python_executable = sys.executable
with open(template_path, "r") as f:
wrapper = f.read()
wrapper = wrapper.replace("@DEFW_BUILD_PYTHON_LIB_DIR@",
shlex.quote(python_lib_dir))
wrapper = wrapper.replace("@DEFW_BUILD_PYTHON_VERSION@",
shlex.quote(python_version))
wrapper = wrapper.replace("@DEFW_BUILD_PYTHON_EXECUTABLE@",
shlex.quote(python_executable))
with open(path, "w") as f:
f.write(wrapper)
os.chmod(path, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR |
stat.S_IRGRP | stat.S_IXGRP |
stat.S_IROTH | stat.S_IXOTH)
print(f"Generated {path}")
def clean(env):
pfiles = os.path.join(env['DEFW_PATH'], "src", "*.py")
wfiles = os.path.join(env['DEFW_PATH'], "src", "*_wrap.c")
ofiles = os.path.join(env['DEFW_PATH'], "src", "*.o")
ifiles = os.path.join(env['DEFW_PATH'], "src", "*.i")
wrapper = os.path.join(env['DEFW_PATH'], "src", "defwp-wrapper")
cmd = "rm -Rf " + " ".join([pfiles, wfiles, ofiles, ifiles, wrapper])
print(cmd)
os.system(cmd)
cleanup_external_files(env)
print(sys.version)
env['PYTHON'] = "python3"
env['SWIG'] = "swig"
env['SWIG_COMP_FLAGS'] = "-g -Wall -fPIC"
env['SWIG_FLAGS'] = "-D__x86_64__ -D__arch_lib__ -D_LARGEFILE64_SOURCE=1"
env['SWIG_INCLUDES'] = ""
#env['SWIG_INCLUDES'] = "-I/usr/include/c++/13/tr1/ -I/usr/include -I/usr/include/linux -I/usr/include/x86_64-linux-gnu/"
env['GEN_SWIG_INTF'] = os.path.join('swig_templates', 'generate_swig_i.py')
env['DEFW_PATH'] = DEFW_PATH
env['DEFW_SWG_FILES'] = glob.glob(os.path.join(env['DEFW_PATH'], "src", "*.swg"))
env['LIBDEFW_SRC_FILES'] = LIBDEFW_SRC_FILES
env['DEFW_FWSL_FILES'] = DEFW_FWSL_FILES
env['DEFW_HDR_FILES'] = glob.glob(os.path.join(env['DEFW_PATH'], "src", "*.h"))
env['LINK_PATH'] = os.path.join(env['DEFW_PATH'], "src")
env['PYTHON_INCLUDE_DIR'] = sysconfig.get_config_var('INCLUDEPY')
env['PYTHON_LIB_DIR'] = sysconfig.get_config_var('LIBDIR')
env['PYTHON_LIB'] = os.path.splitext(sysconfig.get_config_var('LDLIBRARY').strip('lib'))[0]
env['DEFW_MAIN_C'] = os.path.join(DEFW_PATH, 'src', 'defw.c')
env.AddMethod(generate_swig_intf)
env.AddMethod(swigify)
env.AddMethod(swigify_all_files)
env.AddMethod(swigify_externals)
env.AddMethod(build_shared_library)
env.AddMethod(mbuild_shared_library)
env.AddMethod(install)
env.AddMethod(build_bin)
env.AddMethod(build_defwp_wrapper)
env.AddMethod(clean)
env.clean_all = env.clean()
env.shared_libs = env.build_shared_library()
env.swigify_files = env.swigify_all_files()
env.swigify_externals = env.swigify_externals()
env.fwsl = env.mbuild_shared_library(env['DEFW_FWSL_FILES'], os.path.join(env['DEFW_PATH'], "src", "libfwsl.so"))
env.bin = env.build_bin()
env.wrapper = env.build_defwp_wrapper()
env.install_defw = env.install(os.path.join(env['DEFW_PATH'], "src"),
os.path.join(env['DEFW_PATH'], "install"))