-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path__main__.py
207 lines (196 loc) · 6.48 KB
/
__main__.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
import argparse
import typing
from collections.abc import Sequence
from importlib.metadata import version
from pathlib import Path
from puya.algo_constants import MAINNET_AVM_VERSION, SUPPORTED_AVM_VERSIONS
from puya.log import LogLevel, configure_logging
from puya.options import LocalsCoalescingStrategy
from puyapy.compile import compile_to_teal
from puyapy.options import PuyaPyOptions
from puyapy.template import parse_template_key_value
def main() -> None:
parser = argparse.ArgumentParser(
prog="puyapy",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument("--version", action="version", version=f"%(prog)s {version('puyapy')}")
parser.add_argument(
"-O",
"--optimization-level",
type=int,
choices=[0, 1, 2],
default=1,
help="Set optimization level of output TEAL / AVM bytecode",
)
parser.add_argument(
"--output-teal",
action=argparse.BooleanOptionalAction,
default=True,
help="Output TEAL code",
)
parser.add_argument(
"--output-source-map",
action=argparse.BooleanOptionalAction,
default=True,
help="Output debug source maps",
)
parser.add_argument(
"--output-arc32",
action=argparse.BooleanOptionalAction,
default=True,
help="Output {contract}.arc32.json ARC-32 app spec file",
)
parser.add_argument(
"--output-arc56",
action=argparse.BooleanOptionalAction,
default=False,
help="Output {contract}.arc56.json ARC-56 app spec file",
)
parser.add_argument(
"--output-client",
action=argparse.BooleanOptionalAction,
default=False,
help="Output Algorand Python contract client for typed ARC4 ABI calls",
)
parser.add_argument(
"--out-dir", type=Path, help="Path for outputting artefacts", default=False
)
parser.add_argument(
"--log-level",
type=LogLevel.from_string,
choices=list(LogLevel),
default=LogLevel.info,
help="Minimum level to log to console",
)
parser.add_argument(
"-g", # -g chosen because it is the same option for debug in gcc
"--debug-level",
type=int,
choices=[0, 1, 2],
default=1,
help="Output debug information level, 0 = none, 1 = debug, 2 = reserved for future use",
)
parser.add_argument(
"--output-awst",
action=argparse.BooleanOptionalAction,
default=False,
help="Output parsed result of AWST",
)
parser.add_argument(
"--output-awst-json",
action=argparse.BooleanOptionalAction,
default=False,
help="Output parsed result of AWST as JSON",
)
parser.add_argument(
"--output-ssa-ir",
action=argparse.BooleanOptionalAction,
default=False,
help="Output IR (in SSA form) before optimisations",
)
parser.add_argument(
"--output-optimization-ir",
action=argparse.BooleanOptionalAction,
default=False,
help="Output IR after each optimization",
)
parser.add_argument(
"--output-destructured-ir",
action=argparse.BooleanOptionalAction,
default=False,
help="Output IR after SSA destructuring and before MIR",
)
parser.add_argument(
"--output-memory-ir",
action=argparse.BooleanOptionalAction,
default=False,
help="Output MIR before lowering to TealOps",
)
parser.add_argument(
"--output-bytecode",
action=argparse.BooleanOptionalAction,
default=False,
help="Output AVM bytecode",
)
parser.add_argument(
"--match-algod-bytecode",
action=_EmitDeprecated,
dest=argparse.SUPPRESS,
nargs=0,
help="Deprecated: When outputting bytecode, ensure bytecode matches algod output",
)
parser.add_argument(
"-T",
"--template-var",
dest="cli_template_definitions",
metavar="VAR=VALUE",
action=_ParseAndStoreTemplateVar,
default={},
nargs="+",
help="Define template vars for use when assembling via --output-bytecode"
" should be specified without the prefix (see --template-vars-prefix), e.g."
' -T SOME_INT=1234 SOME_BYTES=0x1A2B SOME_BOOL=True SOME_STR=\\"hello\\"',
)
parser.add_argument(
"--template-vars-prefix",
help="Define the prefix to use with --template-var",
default="TMPL_",
)
parser.add_argument(
"--target-avm-version",
type=int,
choices=SUPPORTED_AVM_VERSIONS,
default=MAINNET_AVM_VERSION,
)
parser.add_argument(
"--locals-coalescing-strategy",
type=LocalsCoalescingStrategy,
choices=list(LocalsCoalescingStrategy),
default=LocalsCoalescingStrategy.root_operand,
help=(
"Strategy choice for out-of-ssa local variable coalescing. "
"The best choice for your app is best determined through experimentation"
),
)
parser.add_argument("paths", type=Path, nargs="+", metavar="PATH")
namespace = parser.parse_args()
options = PuyaPyOptions(**vars(namespace))
configure_logging(min_log_level=options.log_level)
compile_to_teal(options)
class _EmitDeprecated(argparse.Action):
@typing.override
def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: str | Sequence[typing.Any] | None,
option_string: str | None = None,
) -> None:
print(f"warning: {option_string} is deprecated and no longer does anything") # noqa: T201
class _ParseAndStoreTemplateVar(argparse.Action):
@typing.override
def __call__(
self,
parser: argparse.ArgumentParser,
namespace: argparse.Namespace,
values: str | Sequence[typing.Any] | None,
option_string: str | None = None,
) -> None:
mapping: dict[str, int | bytes] = dict(getattr(namespace, self.dest, {}))
lst = []
if isinstance(values, str):
lst = [values]
elif values:
for value in values:
assert isinstance(value, str)
lst.append(value)
for kv in lst:
try:
name, value = parse_template_key_value(kv)
except Exception as ex:
parser.error(str(ex))
mapping[name] = value
setattr(namespace, self.dest, mapping)
if __name__ == "__main__":
main()