forked from timxx/pywpsrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.py
480 lines (368 loc) · 17 KB
/
project.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#**
# * Copyright (c) 2020-2021 Weitian Leung
# *
# * This file is part of pywpsrpc.
# *
# * This file is distributed under the MIT License.
# * See the LICENSE file for details.
# *
#*
import sipbuild
import os
import sys
import shutil
import re
var_pattern = re.compile(
rb"(\s+::([a-zA-Z0-9_]+::)?[a-zA-Z0-9_]+\* a[0-9]+);$")
class PyWpsRpcProject(sipbuild.Project):
def __init__(self):
super().__init__(dunder_init=True)
self.abi_version = "12"
self.bindings_factories = [RpcCommon, RpcWpsApi, RpcWppApi, RpcEtApi]
self.sdk_inc_dir = os.path.join(self.root_dir, "include")
# For Arch Linux
if os.path.exists("/usr/lib/office6"):
self.sdk_lib_dir = "/usr/lib/office6"
else:
self.sdk_lib_dir = "/opt/kingsoft/wps-office/office6"
def apply_nonuser_defaults(self, tool):
if self.builder_factory is None:
self.builder_factory = RpcApiBuilder
if self.sip_files_dir is None:
self.sip_files_dir = "sip"
# must set sip_module
if self.sip_module is None:
self.sip_module = "pywpsrpc.sip"
if self.sip_include_dirs is None:
self.sip_include_dirs = [self.root_dir + "/sip/common"]
super().apply_nonuser_defaults(tool)
def get_dunder_init(self):
dunder_init = ""
with open(os.path.join(self.root_dir, "py", "__init__.py")) as f:
dunder_init = f.read()
return dunder_init
def get_requires_dists(self):
# the sip module already contains in the package
return []
class RpcApiBindings(sipbuild.Bindings):
def get_options(self):
options = super().get_options()
options.append(sipbuild.Option("QT", option_type=list))
options.append(sipbuild.Option("PCH", option_type=list))
return options
def apply_nonuser_defaults(self, tool):
if self.sip_file is None:
self.sip_file = self.name + "/" + self.name + ".sip"
super().apply_nonuser_defaults(tool)
self.builder_settings.append("QT = %s" % ' '.join(self.QT))
def apply_user_defaults(self, tool):
super().apply_user_defaults(tool)
def generate(self):
buildable = super().generate()
pch_file = os.path.join(buildable.build_dir, "stdafx.h")
with open(pch_file, "w+") as f:
f.write("#ifndef PYWPSRPC_%s_H\n" % buildable.target)
f.write("#define PYWPSRPC_%s_H\n\n" % buildable.target)
for pch in self.PCH:
f.write("#include <%s>\n" % pch)
f.write("#include <wpsapiex.h>\n")
f.write("#include <string>\n")
f.write("#include <vector>\n\n")
f.write("#endif\n")
buildable.headers.append(pch_file)
# asyncio seems no improves here
self._fix_uninited_vars(buildable.sources)
return buildable
def _fix_uninited_vars(self, sources):
""" Try init the pointer argument
We need it because of using sipEventCollectingWrapper
"""
# FIXME: this should be done by SIP
for src in sources:
tmp_file = src + "_tmp"
f = open(src, "rb")
f_tmp = open(tmp_file, "wb+")
for line in f:
m = var_pattern.match(line)
if m:
line = m.group(1) + b' = nullptr;\n'
f_tmp.write(line)
f.close()
f_tmp.close()
os.rename(tmp_file, src)
class RpcCommon(RpcApiBindings):
def __init__(self, project):
dirs = [project.sdk_inc_dir, project.sdk_inc_dir + "/common"]
super().__init__(project,
"common",
include_dirs=dirs,
QT=["core"],
PCH=["pre_stddef.h",
"kfc/guid.h",
"int.h",
"typedef.h",
"guiddef.h",
"objbase.h",
"strapi/strapi.h",
"comdef.h",
"ksoapi/ksoapi.h"])
def generate(self):
buildable = super().generate()
self._fix_sip(buildable)
return buildable
def _fix_sip(self, buildable):
for installable in buildable.installables:
if installable.name != "sip":
continue
self._fix_subdir_sip(buildable, installable)
# The export.sip include in other module
common_dir = os.path.join(self.project.root_dir, "sip", "common")
installable.files.append(common_dir + "/export.sip")
break
def _fix_subdir_sip(self, buildable, sip_installable):
subdirs = ["ksoapi", "wpsapiex"]
for subdir in subdirs:
sub_installable = sipbuild.Installable(
"sip_" + subdir,
target_subdir=sip_installable.target_subdir + "/" + subdir)
sub_path = "/" + subdir + "/"
sub_installable.files = [
f for f in sip_installable.files if sub_path in f]
sip_installable.files = [f for f in sip_installable.files
if f not in sub_installable.files]
buildable.installables.append(sub_installable)
class RpcWpsApi(RpcApiBindings):
def __init__(self, project):
dirs = [project.sdk_inc_dir, project.sdk_inc_dir + "/common"]
dirs.append(project.sdk_inc_dir + "/wps")
super().__init__(project,
"rpcwpsapi",
include_dirs=dirs,
libraries=["rpcwpsapi_sysqt5"],
library_dirs=[project.sdk_lib_dir],
QT=["core"],
PCH=["QString", "wps/wpsapi.h"])
class RpcWppApi(RpcApiBindings):
def __init__(self, project):
dirs = [project.sdk_inc_dir, project.sdk_inc_dir + "/common"]
dirs.append(project.sdk_inc_dir + "/wpp")
super().__init__(project,
"rpcwppapi",
include_dirs=dirs,
libraries=["rpcwppapi_sysqt5"],
library_dirs=[project.sdk_lib_dir],
PCH=["wpp/wppapi.h"])
class RpcEtApi(RpcApiBindings):
def __init__(self, project):
dirs = [project.sdk_inc_dir, project.sdk_inc_dir + "/common"]
dirs.append(project.sdk_inc_dir + "/et")
super().__init__(project,
"rpcetapi",
include_dirs=dirs,
libraries=["rpcetapi_sysqt5"],
library_dirs=[project.sdk_lib_dir],
PCH=["et/etapi.h"])
# seems that we have no easy way to change the cxx flags
# we need to set the RPATH flags at least
class RpcApiBuilder(sipbuild.Builder):
def __init__(self, project, **kwargs):
super().__init__(project, **kwargs)
def get_options(self):
options = super().get_options()
options.append(
sipbuild.Option("qmake",
metavar="<FILE>",
help="Specify the qmake executable"))
options.append(
sipbuild.Option("clang",
option_type=bool,
help="Use clang to compile the project"))
options.append(
sipbuild.Option("libcxx",
option_type=bool,
help="Link libc++ when --clang specified"))
return options
def apply_user_defaults(self, tool):
if tool in sipbuild.Option.BUILD_TOOLS:
if self.qmake is None:
self.qmake = shutil.which("qmake-qt5")
if not self.qmake:
self.qmake = shutil.which("qmake")
if not self.qmake:
raise sipbuild.UserException(
"Please specify a valid qmake executable")
super().apply_user_defaults(tool)
def build_executable(self, buildable, *, fatal=True):
raise sipbuild.UserException("RpcApiBuilder cannot build executables")
def build_project(self, target_dir, *, wheel_tag=None):
installed = []
sub_dirs = []
is_debug = False
for buildable in self.project.buildables:
if isinstance(buildable, sipbuild.BuildableModule):
is_debug = buildable.debug
self._gen_module_pro_file(buildable, target_dir, installed)
elif type(buildable) is sipbuild.Buildable:
for installable in buildable.installables:
installable.install(
target_dir, installed, do_install=False)
else:
raise sipbuild.UserException(
"RpcApiBuilder cannot build '{0}' buildables".format(
type(buildable).__name__))
sub_dirs.append(buildable.name)
self._gen_sip_project(target_dir, sub_dirs, installed, is_debug)
self.project.progress("Generating the top-level project")
pro = self.project.build_dir + "/" + self.project.name + ".pro"
with open(pro, "w+") as f:
f.write("TEMPLATE = subdirs\n")
f.write("CONFIG += ordered\n\n")
f.write("SUBDIRS = %s\n\n" % ' '.join(sub_dirs))
for installable in self.project.installables:
self._install(f, installable, target_dir, installed)
py_subdir = os.path.join(target_dir, self.project.name)
sip_py = sipbuild.Installable("sip_py", target_subdir=py_subdir)
py_dir = os.path.join(self.project.root_dir, "py")
sip_py_files = [(py_dir + "/" + f) for f in os.listdir(py_dir)
if f.endswith(".py") and f != "__init__.py"]
sip_py.files.extend(sip_py_files)
self._install(f, sip_py, target_dir, installed)
fake_root = os.path.join(self.project.build_dir, self.project.name)
for py in sip_py.files:
shutil.copy(py, fake_root)
shutil.copy(os.path.join(py_dir, "__init__.py"), fake_root)
# for distinfo
inventory_file = self.project.build_dir + "/inventory.txt"
with open(inventory_file, "w+") as inventory:
inventory.write('\n'.join(installed))
inventory.write('\n')
distinfo = ["sip-distinfo",
"--project-root", self.project.root_dir,
"--generator", os.path.basename(sys.argv[0]),
"--prefix", '\\"$(INSTALL_ROOT)\\"',
"--inventory", inventory_file]
if wheel_tag:
distinfo.append("--wheel-tag")
distinfo.append(wheel_tag)
distinfo.append(self.project.get_distinfo_dir(target_dir))
f.write("distinfo.extra = %s\n" % ' '.join(distinfo))
f.write("distinfo.path = %s\n" % target_dir)
f.write("INSTALLS += distinfo\n")
old_dir = os.getcwd()
os.chdir(self.project.build_dir)
args = [self.qmake, "-recursive", self.project.name + ".pro"]
if self.clang:
if self.libcxx:
args.extend(["-spec", "linux-clang-libc++"])
else:
args.extend(["-spec", "linux-clang"])
self.project.run_command(args, fatal=True)
self.project.progress("Compiling the project")
self._run_make()
os.chdir(old_dir)
def install_project(self, target_dir, *, wheel_tag=None):
self.project.progress("Installing the project")
old_dir = os.getcwd()
os.chdir(self.project.build_dir)
self._run_make(True)
os.chdir(old_dir)
def _gen_module_pro_file(self, buildable, target_dir, installed):
self.project.progress("Generating the %s project" % buildable.target)
with open(buildable.build_dir + "/" + buildable.target + ".pro", "w+") as f:
f.write("TEMPLATE = lib\n")
f.write("CONFIG += plugin no_plugin_name_prefix warn_off\n")
f.write("CONFIG += %s\n" %
("debug" if buildable.debug else "release"))
f.write("CONFIG += c++11 precompile_header\n")
f.write("%s\n" % ' '.join(buildable.builder_settings))
f.write("TARGET = %s\n\n" % buildable.target)
if buildable.define_macros:
f.write("DEFINES += %s\n" % ' '.join(buildable.define_macros))
f.write("INCLUDEPATH += .\n")
for dir in buildable.include_dirs:
f.write("INCLUDEPATH += %s\n" % dir)
f.write("INCLUDEPATH += %s\n\n" % self.project.py_include_dir)
for dir in buildable.library_dirs:
f.write("LIBS += -L%s\n" % dir)
for lib in buildable.libraries:
f.write("LIBS += -l%s\n" % lib)
f.write('\n')
# hidden symbols
with open(buildable.build_dir + "/" + buildable.target + ".exp", "w+") as exp:
exp.write("{ global: PyInit_%s; local: *; };" %
buildable.target)
f.write("QMAKE_LFLAGS += -Wl,--version-script=%s.exp\n" %
buildable.target)
if not buildable.debug:
f.write("QMAKE_LFLAGS += -s\n")
#f.write("QMAKE_LFLAGS_PLUGIN += -Wl,--no-undefined\n")
f.write(
"QMAKE_CXXFLAGS += -Wno-attributes -Wno-delete-non-virtual-dtor\n")
f.write("QMAKE_CXXFLAGS += -Wno-delete-incomplete -Wno-unused-variable\n")
f.write("QMAKE_RPATHDIR += $ORIGIN /opt/kingsoft/wps-office/office6\n")
f.write("QMAKE_RPATHDIR += /usr/lib/office6\n\n")
# for testing
rpc_dir = os.path.join(self.project.build_dir, self.project.name)
os.makedirs(rpc_dir, exist_ok=True)
f.write("QMAKE_POST_LINK = $(COPY_FILE) $(TARGET) %s\n\n" % rpc_dir)
f.write("PRECOMPILED_HEADER = stdafx.h\n\n")
f.write("HEADERS = %s\n" % " \\\n\t".join(buildable.headers))
f.write("SOURCES = %s" % " \\\n\t".join(buildable.sources))
f.write('\n\n')
f.write("target.path = %s\n" %
(target_dir + "/" + self.project.name))
f.write("INSTALLS += target\n\n")
# for dist-info
target_installable = sipbuild.Installable(
"target", target_subdir=buildable.get_install_subdir())
installed.append(
target_installable.get_full_target_dir(target_dir) +
"/%s.so" % buildable.target)
for installable in buildable.installables:
self._install(f, installable, target_dir, installed)
def _install(self, f, installable, target_dir, installed):
installable.install(target_dir, installed, do_install=False)
f.write("{}.path = {}\n".format(
installable.name,
installable.get_full_target_dir(target_dir)))
f.write("{}.files = {}\n".format(
installable.name,
" \\\n\t".join(installable.files)))
f.write("INSTALLS += %s\n\n" % installable.name)
def _run_make(self, install=False):
if install:
# single thread only
args = ["make", "install"]
else:
args = ["make", "-j%s" % os.cpu_count()]
self.project.run_command(args, fatal=True)
def _gen_sip_project(self, target_dir, sub_dirs, installed, is_debug):
self.project.progress("Generating the sip project")
sub_dir = self.project.build_dir + "/sip"
os.makedirs(sub_dir, exist_ok=True)
sources = sipbuild.module.copy_nonshared_sources(
self.project.abi_version, sub_dir)
# use copy_sip_h instead?
shutil.copy(self.project.build_dir + "/sip.h", sub_dir)
with open(sub_dir + "/sip.pro", "w+") as f:
f.write("TEMPLATE = lib\n")
f.write("CONFIG += plugin no_plugin_name_prefix warn_on\n")
f.write("CONFIG += %s\n" % ("debug" if is_debug else "release"))
f.write("QT =\n")
f.write("TARGET = sip\n\n")
f.write("INCLUDEPATH += %s\n\n" % self.project.py_include_dir)
with open(sub_dir + "/sip.exp", "w+") as exp:
exp.write("{ global: PyInit_sip; local: *; };")
f.write("QMAKE_LFLAGS += -Wl,--version-script=sip.exp\n")
if not is_debug:
f.write("QMAKE_LFLAGS += -s\n")
f.write("\n")
rpc_dir = os.path.join(self.project.build_dir, self.project.name)
os.makedirs(rpc_dir, exist_ok=True)
f.write("QMAKE_POST_LINK = $(COPY_FILE) $(TARGET) %s\n\n" % rpc_dir)
f.write("SOURCES = %s\n\n" % " \\\n\t".join(sources))
f.write("target.path = %s\n" %
(target_dir + "/" + self.project.name))
f.write("INSTALLS += target\n\n")
sub_dirs.append("sip")
installed.append(target_dir + "/" + self.project.name + "/sip.so")