-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.py
More file actions
240 lines (213 loc) · 9.05 KB
/
Copy pathsetup.py
File metadata and controls
240 lines (213 loc) · 9.05 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
#*****************************************************************************
#* setup.py
#*
#* pssparser Python extension setup file
#*****************************************************************************
import glob
import os
import sys
import platform
from setuptools import Extension, setup, find_namespace_packages
proj_dir = os.path.dirname(os.path.abspath(__file__))
pythondir = os.path.join(proj_dir, "python")
def _get_version():
version_file = os.path.join(proj_dir, "python", "pssparser", "__version__.py")
glb = {}
with open(version_file) as f:
exec(f.read(), glb)
return glb["_pkg_version"]
version = _get_version()
isSrcBuild = False
isSrcTree = os.path.isdir(os.path.join(proj_dir, "src"))
try:
from ivpm_build.setup import setup
except ImportError as e:
# Any from-source build of pssparser (editable git checkout, sdist, or a
# pypi source-fallback when no wheel matches) goes through this setup.py and
# depends on the IVPM build backend: it runs the AST code generation (which
# produces python/PyBaseVisitor.cpp and friends) and copies the generated
# sources into place. Plain setuptools cannot do that, so the build would
# fail later with a confusing "PyBaseVisitor.cpp: No such file or directory".
# Fail now with an actionable message instead of silently falling back.
raise RuntimeError(
"pssparser: building from source requires the 'ivpm-build' package "
"(which depends on 'ivpm'), but it is not installed in the build "
"environment.\n"
"IVPM installs with --no-build-isolation, so [build-system].requires is "
"not auto-provisioned; install ivpm-build into the target venv first, "
"e.g.:\n"
" uv pip install ivpm-build\n"
"Original import error: %s" % e
) from e
isSrcBuild = isSrcTree
print("pssparser: isSrcBuild: %s" % str(isSrcBuild))
include_dirs = []
if isSrcTree:
include_dirs.append(pythondir)
include_dirs.append(os.path.join(proj_dir, "src", "include"))
include_dirs.append(os.path.join(proj_dir, "build", "include"))
# Add ciostream native header path (ciostream_native.h)
# Search pip-installed location first (Linux/macOS: lib/python*/site-packages,
# Windows: Lib/site-packages), then fall back to source layout.
import glob
_ciostream_found = False
for _pattern in [
os.path.join(proj_dir, "packages", "python", "lib", "python*", "site-packages", "ciostream"),
os.path.join(proj_dir, "packages", "python", "Lib", "site-packages", "ciostream"),
os.path.join(os.path.dirname(proj_dir), "ciostream", "src", "ciostream"),
os.path.join(proj_dir, "packages", "ciostream", "src", "ciostream"),
]:
_matches = glob.glob(_pattern)
if _matches and os.path.isdir(_matches[0]):
include_dirs.append(_matches[0])
_ciostream_found = True
break
# Add debug_mgr include path (needed for dmgr/IDebugMgr.h in PyParserUtils.h)
for _pattern in [
os.path.join(proj_dir, "packages", "python", "lib", "python*", "site-packages", "debug_mgr", "share", "include"),
os.path.join(proj_dir, "packages", "python", "Lib", "site-packages", "debug_mgr", "share", "include"),
]:
_matches = glob.glob(_pattern)
if _matches and os.path.isdir(_matches[0]):
include_dirs.append(_matches[0])
break
# Add site-packages to include_dirs so Cython can find .pxd files for
# debug_mgr, ciostream, etc. installed there by ivpm.
for _pattern in [
os.path.join(proj_dir, "packages", "python", "lib", "python*", "site-packages"),
os.path.join(proj_dir, "packages", "python", "Lib", "site-packages"),
]:
_matches = glob.glob(_pattern)
if _matches and os.path.isdir(_matches[0]):
include_dirs.append(_matches[0])
break
library_dirs = []
libraries = []
extra_link_args = []
# Note: the extensions deliberately link neither pssparser nor ast. Both
# libraries are reached at runtime through a ctypes-loaded *_getFactory entry
# point returning a pure-virtual interface (see Factory.inst() in core.pyx and
# in the generated ast.pyx), so the extensions carry no undefined symbols from
# them and need no import libraries on Windows.
ast_ext = Extension(
"pssparser.ast",
[
os.path.join(pythondir, "ast.pyx"),
os.path.join(pythondir, "PyBaseVisitor.cpp"),
],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
language="c++")
ext = Extension(
"pssparser.core",
[ os.path.join(pythondir, "core.pyx"),
os.path.join(pythondir, "PyParserUtils.cpp"),
],
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_link_args=extra_link_args,
language="c++")
extensions=[ast_ext, ext]
setup_requires=['cython', 'ivpm-build', 'ivpm']
if isSrcBuild:
pass # pyastbuilder used at cmake time, not setup time
setup_args = dict(
name="pssparser",
packages=find_namespace_packages(where='python'),
package_dir={'' : 'python' },
package_data={
'pssparser': [
"ast.pyi",
"ast.pxd",
"ast_decl.pxd",
"core.pyi",
"core.pxd",
"decl.pxd",
"tokens.pyi",
"cst.pyi",
# The standard-library sources. A tool that documents or
# cross-references the core library needs the .pss text, not just
# the compiled-in copy, and an installed wheel is the only place it
# can look. See pssparser.get_stdlib_dir().
"stdlib/*.pss",
]
},
version=version,
author="Matthew Ballance",
author_email="matt.ballance@gmail.com",
description="Provides a PSS parser and related tools",
long_description="""
PSSParser - PSS language parser with ANTLR4 backend
""",
ext_modules=extensions,
entry_points={
"console_scripts": [
"pssparser = pssparser.cli.app:main",
],
},
install_requires=[
'debug-mgr',
'ciostream'
],
setup_requires=setup_requires,
)
if isSrcBuild:
import shutil
setup_args["ivpm_extdep_pkgs"] = ["debug-mgr", "ciostream"]
setup_args["ivpm_extdep_data"] = [
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'ast_decl.pxd'),
os.path.join(proj_dir, "python", "pssparser", 'ast_decl.pxd')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'ast.pyi'),
os.path.join(proj_dir, "python", "pssparser", 'ast.pyi')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'ast.pxd'),
os.path.join(proj_dir, "python", "pssparser", 'ast.pxd')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'ast.pyx'),
os.path.join(proj_dir, "python", 'ast.pyx')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'PyBaseVisitor.cpp'),
os.path.join(proj_dir, "python", 'PyBaseVisitor.cpp')),
(os.path.join(proj_dir, "build", "pssparser_ast", "ext", 'PyBaseVisitor.h'),
os.path.join(proj_dir, "python", 'PyBaseVisitor.h')),
]
if platform.system() == "Linux":
antlr4_rt_lib = None
for libdir in ("lib", "lib64"):
if os.path.exists("build/%s/libantlr4-runtime.so" % libdir):
for f in os.listdir("build/%s" % libdir):
if f.startswith("libantlr4-runtime.so."):
antlr4_rt_lib = "build/{libdir}/%s" % f
break
if antlr4_rt_lib is not None:
break
print("antlr4_rt_lib: %s" % antlr4_rt_lib)
elif platform.system() == "Windows":
# Windows links antlr4 statically into pssparser.dll (see the
# ANTLR_BUILD_SHARED=OFF branch in CMakeLists.txt), so there is no
# runtime library to ship. Leaving the entry in would abort the wheel
# build: install_lib raises when an ivpm_extra_data source is missing.
antlr4_rt_lib = None
else:
antlr4_rt_lib = "build/{libdir}/{libpref}antlr4-runtime{dllext}"
# Ship the standard-library sources inside the package. They live in
# src/stdlib, outside package_dir, so package_data alone cannot reach them
# in a source build; each file is copied in explicitly. Globbed rather
# than listed so a new stdlib package is picked up without editing here.
_stdlib_data = [
(os.path.join("src", "stdlib", os.path.basename(f)), "stdlib")
for f in sorted(glob.glob(os.path.join(proj_dir, "src", "stdlib", "*.pss")))
]
extra_data = _stdlib_data + [
("build/include", "share"),
("build/{libdir}/{libpref}ast{dllext}", ""),
("build/{libdir}/{libpref}pssparser{dllext}", ""),
("python/PyBaseVisitor.h", "share/include"),
("python/PyParserUtils.h", "share/include"),
]
if antlr4_rt_lib is not None:
extra_data.insert(len(_stdlib_data) + 1, (antlr4_rt_lib, ""))
setup_args["ivpm_extra_data"] = {
"pssparser": extra_data
}
setup(**setup_args)