-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler_config.go
196 lines (169 loc) · 5.78 KB
/
compiler_config.go
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
package solc
import (
"fmt"
"regexp"
"strings"
)
// allowedArgs defines a list of allowed arguments for solc.
var allowedArgs = map[string]bool{
"--combined-json": true,
"-": true,
"--optimize": true,
"--optimize-runs": true,
"--evm-version": true,
"--overwrite": true,
"--libraries": true,
"--standard-json": true,
"--allow-paths": true,
"--base-path": true,
"--ignore-missing": true,
"--ast": true,
"--ast-json": true,
"--include-path": true,
"--output-dir": true,
"--asm": true,
"--bin": true,
"--abi": true,
"--asm-json": true,
"--bin-runtime": true,
"--ir": true,
"--opcodes": true,
"--ir-optimized": true,
"--ewasm": true,
"--ewasm-ir": true,
"--no-optimize-yul": true,
"--yul-optimizations": true,
"--yul": true,
"--assemble": true,
"--lsp": true,
"--hashes": true,
"--userdoc": true,
"--devdoc": true,
"--metadata": true,
"--storage-layout": true,
"--gas": true,
"--metadata-hash": true,
"--metadata-literal": true,
"--error-recovery": true,
}
// requiredArgs defines a list of required arguments for solc.
var requiredArgs = map[string]bool{
"--overwrite": true,
"--combined-json": true,
"-": true,
}
// CompilerConfig represents the compiler configuration for the solc binaries.
type CompilerConfig struct {
CompilerVersion string // The version of the compiler to use.
EntrySourceName string // The name of the entry source file.
Arguments []string // Arguments to pass to the solc tool.
JsonConfig *CompilerJsonConfig // The json config to pass to the solc tool.
}
// NewDefaultCompilerConfig creates and returns a default CompilerConfiguration for compiler to use.
func NewDefaultCompilerConfig(compilerVersion string) (*CompilerConfig, error) {
toReturn := &CompilerConfig{
CompilerVersion: compilerVersion,
Arguments: []string{
"--overwrite", "--combined-json", "bin,abi", "-", // Output to stdout.
},
}
if _, err := toReturn.SanitizeArguments(toReturn.Arguments); err != nil {
return nil, err
}
if err := toReturn.Validate(); err != nil {
return nil, err
}
return toReturn, nil
}
// NewDefaultCompilerConfig creates and returns a default CompilerConfiguration for compiler to use with provided JSON settings.
func NewCompilerConfigFromJSON(compilerVersion string, entrySourceName string, config *CompilerJsonConfig) (*CompilerConfig, error) {
toReturn := &CompilerConfig{
EntrySourceName: entrySourceName,
CompilerVersion: compilerVersion,
Arguments: []string{"--standard-json"},
JsonConfig: config,
}
if _, err := toReturn.SanitizeArguments(toReturn.Arguments); err != nil {
return nil, err
}
/*
TODO: Validation at this point for the JSON config is not done.
It's assumed that the caller has already validated the JSON config.
if err := toReturn.Validate(); err != nil {
return nil, err
} */
return toReturn, nil
}
// SetJsonConfig sets the json config to pass to the solc tool.
func (c *CompilerConfig) SetJsonConfig(config *CompilerJsonConfig) {
c.JsonConfig = config
}
// GetJsonConfig returns the json config to pass to the solc tool.
func (c *CompilerConfig) GetJsonConfig() *CompilerJsonConfig {
return c.JsonConfig
}
// SetEntrySourceName sets the name of the entry source file.
func (c *CompilerConfig) SetEntrySourceName(name string) {
c.EntrySourceName = name
}
// GetEntrySourceName returns the name of the entry source file.
func (c *CompilerConfig) GetEntrySourceName() string {
return c.EntrySourceName
}
// SetCompilerVersion sets the version of the solc compiler to use.
func (c *CompilerConfig) SetCompilerVersion(version string) {
c.CompilerVersion = version
}
// GetCompilerVersion returns the currently set version of the solc compiler.
func (c *CompilerConfig) GetCompilerVersion() string {
return c.CompilerVersion
}
// SanitizeArguments sanitizes the provided arguments against a list of allowed arguments.
// Returns an error if any of the provided arguments are not in the allowed list.
func (c *CompilerConfig) SanitizeArguments(args []string) ([]string, error) {
var sanitizedArgs []string
for _, arg := range args {
if strings.Contains(arg, "-") {
if _, ok := allowedArgs[arg]; !ok {
return nil, fmt.Errorf("invalid argument: %s", arg)
}
}
sanitizedArgs = append(sanitizedArgs, arg)
}
return sanitizedArgs, nil
}
// Validate checks if the current CompilerConfiguration's arguments are valid.
// It ensures that all required arguments are present.
func (c *CompilerConfig) Validate() error {
sanitized, err := c.SanitizeArguments(c.Arguments)
if err != nil {
return err
}
// Convert the sanitized slice into a map for easier lookup.
sanitizedMap := make(map[string]bool)
for _, arg := range sanitized {
sanitizedMap[arg] = true
}
for arg := range requiredArgs {
if _, ok := sanitizedMap[arg]; !ok {
return fmt.Errorf("missing required argument: %s", arg)
}
}
matched, _ := regexp.MatchString(`^(\d+\.\d+\.\d+)$`, c.CompilerVersion)
if !matched {
return fmt.Errorf("invalid compiler version: %s", c.CompilerVersion)
}
return nil
}
// SetArguments sets the arguments to be passed to the solc tool.
func (c *CompilerConfig) SetArguments(args []string) {
c.Arguments = args
}
// AppendArguments appends new arguments to the existing set of arguments.
func (c *CompilerConfig) AppendArguments(args ...string) {
c.Arguments = append(c.Arguments, args...)
}
// GetArguments returns the arguments to be passed to the solc tool.
func (c *CompilerConfig) GetArguments() []string {
return c.Arguments
}