-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
164 lines (144 loc) · 6.29 KB
/
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
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
from __future__ import annotations
from setuptools import Command, setup
import setuptools.command.build
from pathlib import Path
from enum import Enum
class Target(Enum):
python = 0
cpp = 1
def __str__(self):
if self == Target.python:
return 'Python3'
elif self == Target.cpp:
return 'Cpp'
raise ValueError(f'Unknown target {self}')
class Feature(Enum):
listener = 0
visitor = 1
lexer = 2
parser = 3
def __str__(self):
if self == Feature.listener:
return 'Listener'
if self == Feature.visitor:
return 'Visitor'
if self == Feature.lexer:
return 'Lexer'
if self == Feature.parser:
return 'Parser'
def BuildANTLRCommand(source: Path, destination: str, grammars):
def antlr4_runtime_version():
"""Retrieve the ANTLR4 version used by the available antlr4-python3-runtime"""
from importlib import metadata
try:
return metadata.metadata('antlr4-python3-runtime').get('version')
except metadata.PackageNotFoundError:
raise RuntimeError("antlr4-python3-runtime is a build dependency!")
def build_language(grammar_file: Path,
target: Target,
features: list[Feature],
output=None,
):
from subprocess import run
from antlr4_tool_runner import initialize_paths, install_jre_and_antlr
args = [
f'-Dlanguage={target}',
'-visitor' if Feature.visitor in features else '-no-visitor',
'-listener' if Feature.listener in features else '-no-listener',
'-o', output.resolve(),
grammar_file.name
]
print(f"Generating ANTLR {target} {' '.join(str(f) for f in features)} in {output} for {grammar_file}")
# The following copies the implementation of antlr4_tool_runner.tool,
# which pulls `args` from the system argv list
initialize_paths()
jar, java = install_jre_and_antlr(antlr4_runtime_version())
run([java, '-cp', jar, 'org.antlr.v4.Tool'] + args, cwd=grammar_file.parent)
class BuildANTLR(Command):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.build_lib = None
self.editable_mode = False
def initialize_options(self):
"""Initialize command state to defaults"""
...
def finalize_options(self):
"""
Populate the command state. This is where I traverse the directory
tree to search for the *.ksc files to compile them later.
The self.set_undefined_options is used to inherit the `build_lib`
attribute from the `build_py` command.
"""
self.set_undefined_options("build_py", ("build_lib", "build_lib"))
def run(self):
"""
Perform actions with side-effects, such as invoking a ksc to python compiler.
The directory to which outputs are written depends on `editable_mode` attribute.
When editable_mode == False, the outputs are written to directory pointed by build_lib.
When editable_mode == True, the outputs are written in-place,
i.e. into the directory containing the sources.
The `run` method is not executed during sdist builds.
"""
dest = Path(self.build_lib) / destination
for grammar, options in grammars.items():
build_language(source/f'{grammar}.g4',
target=options['target'],
features=options['features'],
output=dest)
def get_output_mapping(self):
"""
Return dict mapping output file paths to input file paths
Example:
dict = { "build/lib/output.py": "src/grammar/grammar.g4" }
"""
files = {}
dest = Path(self.build_lib) / destination
for grammar, options in grammars.items():
for feature in [Feature.lexer, Feature.parser] + options['features']:
files[dest / f"{grammar}{feature}.py"] = source / f"{grammar}.g4"
if deps := options.get("deps"):
for dep in deps:
files[dest / f"{dep}{feature}.py"] = source / f"{dep}.g4"
return {str(k): str(v) for k, v in files.items()}
def get_outputs(self):
"""Return list containing paths to output files (generated *.py files)"""
files = []
dest = Path(self.build_lib) / destination
for grammar, options in grammars.items():
for feature in [Feature.lexer, Feature.parser] + options['features']:
files.append(dest / f"{grammar}{feature}.py")
if deps := options.get("deps"):
files.extend(dest / f"{dep}{feature}.py" for dep in deps)
return [str(file) for file in files]
def get_source_files(self):
"""Returns list containing paths to input files (*.g4 ANTLR grammars)"""
files = []
for grammar, options in grammars.items():
files.append(source / f"{grammar}.g4")
if deps := options.get("deps"):
files.extend(source / f"{dep}.g4" for dep in deps)
return [str(file) for file in files]
return BuildANTLR
setuptools.command.build.build.sub_commands.append(("build_antlr", None))
setup(cmdclass={
"build_antlr": BuildANTLRCommand(
source=Path() / "src" / "grammar", # grammar file loc relative to this file
destination="mccode_antlr/grammar", # generated file loc in build dir
grammars={
'McComp': {
'target': Target.python,
'features': [Feature.visitor],
'deps': ('McCommon', 'c99'),
},
'McInstr': {
'target': Target.python,
'features': [Feature.visitor],
'deps': ('McCommon', 'c99'),
},
'C': {
'target': Target.python,
'features': [Feature.visitor, Feature.listener],
},
},
)
})